More Books
Flash Communication Server
Flash Communication Server
Table of Contents
Copyright
About the Authors
Brian Lesser
Giacomo
Joey Lott
Robert Reinhardt
Justin Watkins
Foreword
Preface
What Does FlashCom Offer?
What's in This Book?
How to Use This Book
Audience
ActionScript 1.0 Versus ActionScript 2.0
Server-Side ActionScript
The flash-communications.net Site
Director, Breeze, and Other Options
Flash Video Options
Licensing and Hosting Options
Conventions Used in This Book
Voice
Using Code Examples
Safari Enabled
Comments and Questions
Acknowledgments
Part I:  FlashCom Foundation
Chapter 1.  Introducing the Flash Communication Server
Section 1.1.  Clients and Servers
Section 1.2.  Creating an Application
Section 1.3.  Real-Time Messaging Protocol
Section 1.4.  The Communication Classes
Section 1.5.  Communicating with Application Servers, Databases, and Directory Servers
Section 1.6.  Firewalls and Security
Section 1.7.  Getting Started
Section 1.8.  Hello Video!
Section 1.9.  Conclusion
Chapter 2.  Communication Components
Section 2.1.  Overview of Communication Components
Section 2.2.  Summary of Communication Components
Section 2.3.  Creating an Application that Monitorsa Connection
Section 2.4.  Building a Simple Chat Room
Section 2.5.  Adding Audio and Video to the Chat Room
Section 2.6.  Forgoing the SimpleConnect Component
Section 2.7.  Conclusion
Chapter 3.  Managing Connections
Section 3.1.  Making a Connection
Section 3.2.  Managing a Connection
Section 3.3.  Reusing a NetConnection Object
Section 3.4.  Multiple Simultaneous NetConnection Objects
Section 3.5.  Testing and Debugging Network Connections
Section 3.6.  Subclassing the NetConnection Class
Section 3.7.  Communication Components Without SimpleConnect
Section 3.8.  Conclusion
Chapter 4.  Applications, Instances, and Server-Side ActionScript
Section 4.1.  Scripting Application Instances
Section 4.2.  Differences Between Flash ActionScript and Server-Side ActionScript
Section 4.3.  The Life of an Application Instance
Section 4.4.  Running a Simple Hello World Test Script
Section 4.5.  A More Realistic Example
Section 4.6.  Instance-to-Instance Communications
Section 4.7.  Script Filenames and Locations in Detail
Section 4.8.  Testing and Debugging Server-SideScript Files
Section 4.9.  Designing Communication Applications
Section 4.10.  Conclusion
Part II:  Audio, Video, and Data Streams
Chapter 5.  Managing Streams
Section 5.1.  A Simple Publisher/Subscriber Example
Section 5.2.  Stream Names
Section 5.3.  Publishing Streams in Detail
Section 5.4.  Playing Streams in Detail
Section 5.5.  The Stream Class
Section 5.6.  Publishing and Playing ActionScript Data
Section 5.7.  Creating Synchronized Presentations
Section 5.8.  The NetStream and Stream Information Objects
Section 5.9.  Stream Enhancements and Limitations
Section 5.10.  Conclusion
Chapter 6.  Microphone and Camera
Section 6.1.  Working with Microphone/Audio Input
Section 6.2.  Working with Camera Input
Section 6.3.  Building a Message-Taking Application
Section 6.4.  Building a Surveillance Application
Section 6.5.  Conclusion
Chapter 7.  Media Preparation and Delivery
Section 7.1.  Audio and Video Compression
Section 7.2.  Converting Prerecorded Materialto FLV Format
Section 7.3.  Using Flash Pro's Media Components
Section 7.4.  Enabling Multiple Bit Rate FLVsWithin an Application
Section 7.5.  Streaming MP3 Audio
Section 7.6.  Conclusion
Part III:  Remote Connectivity and Communication
Chapter 8.  Shared Objects
Section 8.1.  Objects and Shared Objects
Section 8.2.  Getting a Shared Object in Flash
Section 8.3.  Updates and Frame Rates
Section 8.4.  Scripting Shared Objects on the Server
Section 8.5.  Temporary and Persistent Shared Objects
Section 8.6.  Proxied Shared Objects
Section 8.7.  Shared Objects and Custom Classes
Section 8.8.  Avoiding Collisions
Section 8.9.  Optimizing Shared Object Performance
Section 8.10.  Broadcasting Remote Method Callswith send( )
Section 8.11.  A Simple Video and Text Chat Application
Section 8.12.  Conclusion
Chapter 9.  Remote Methods
Section 9.1.  Why Use Calls?
Section 9.2.  The send( ) and call( ) Methods
Section 9.3.  Client-to-Server Calls
Section 9.4.  Server-to-Client Calls
Section 9.5.  Server-to-Server Calls
Section 9.6.  A Simple Lobby/Rooms Application
Section 9.7.  Debugging Calls
Section 9.8.  Advanced Topics
Section 9.9.  Conclusion
Chapter 10.  Server Management API
Section 10.1.  Connecting to the Admin Service
Section 10.2.  Using the Server Management API
Section 10.3.  Server Management API Uses
Section 10.4.  Conclusion
Chapter 11.  Flash Remoting
Section 11.1.  The Remoting Gateway
Section 11.2.  Remoting Basics
Section 11.3.  Role of Remoting in FlashCom Applications
Section 11.4.  Securing Access
Section 11.5.  Conclusion
Chapter 12.  ColdFusion MX and FlashCom
Section 12.1.  Understanding ColdFusion MXand Flash Remoting
Section 12.2.  Using Flash Remoting to Log Events
Section 12.3.  Getting a List of Streams
Section 12.4.  Using ColdFusion and FTP to Mirror Streams
Section 12.5.  Conclusion
Part IV:  Design and Deployment
Chapter 13.  Building Communication Components
Section 13.1.  Source Files
Section 13.2.  People Lists
Section 13.3.  A Simple People List
Section 13.4.  Listenable Shared Objects
Section 13.5.  Status and People List
Section 13.6.  Text Chat
Section 13.7.  Shared Text
Section 13.8.  Video Conference and Video Window
Section 13.9.  PeopleGrid
Section 13.10.  Summary
Section 13.11.  Conclusion
Chapter 14.  Understanding the Macromedia Component Framework
Section 14.1.  The Component Framework
Section 14.2.  Under the Hood of the Chat Component
Section 14.3.  Creating a Simple Component from Scratch: SharedTextInput
Section 14.4.  Creating a Container Component: SharedAddressForm
Section 14.5.  Creating an Authenticating Component
Section 14.6.  Integrating Components with Your Existing Applications
Section 14.7.  Understanding the Framework
Section 14.8.  Conclusion
Chapter 15.  Application Design Patterns and Best Practices
Section 15.1.  Shared Object Management
Section 15.2.  Moving Code to the Server
Section 15.3.  Building Façades on the Server
Section 15.4.  Server-Side Client Queues
Section 15.5.  A Framework for Recording and Playing Back Componentized Applications
Section 15.6.  Components and Component Frameworks
Section 15.7.  Conclusion
Chapter 16.  Building Scalable Applications
Section 16.1.  Coordinating Instances
Section 16.2.  Scalability and Load Balancing
Section 16.3.  Conclusion
Chapter 17.  Network Performance, Latency,and Concurrency
Section 17.1.  Latency
Section 17.2.  Bandwidth
Section 17.3.  Concurrency
Section 17.4.  Conclusion
Chapter 18.  Securing Applications
Section 18.1.  The Three A's: Authentication, Authorization, and Accounting
Section 18.2.  Authentication
Section 18.3.  Authorization
Section 18.4.  Accounting
Section 18.5.  Suggestions and References
Section 18.6.  Conclusion
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W

7.4. Enabling Multiple Bit Rate FLVsWithin an Application

As you build audio/video streaming-enabled applications with FlashCom, you'll want to know how to serve streams designed to meet the bandwidth restrictions of each client connecting to the application. For example, if you have an FLV file that was encoded for 256 Kbps, chances are that stream won't play very well to someone using a 56 Kbps dial-up connection. One of the drawbacks to FlashCom's audio/video streaming is that it cannot recompress an FLV file on the fly. Other AV streaming servers, such as the Apple QuickTime Streaming Server, can take a high-resolution QuickTime movie (MOV file) and compress it further for low-bandwidth clients. With FlashCom, you have two options when faced with this problem.

  • Change the frame rate of the stream for the receiving client. With client-side ActionScript, you can use the NetStream.receiveFps( ) method to lower the frame rate of a streaming FLV published from a FlashCom application.

  • Encode multiple FLV files, with each file targeted at a specific bit rate. For example, if you know that you'll have dial-up and broadband users subscribing to streams on your FlashCom Server, you could encode your source video clips at 39 Kbps and 384 Kbps, respectively. The higher bit rate stream could be viewed by the broadband users, while the lower bit rate stream would be available for the dial-up users.

In this section, you learn how to efficiently handle the latter option by building a Flash movie that can dynamically switch between two FLV files of the same content. To see an example of the system you're about to dissect, check out the following URL:

http://www.flash-communications.net/examples/ch07/MultiBitrateStreamer

Because FlashCom can seek anywhere within a stream and serve specific portions of that stream, the MultiBitrateStreamer component used in this exercise does not have to download either stream in its entirety.

7.4.1. Preparing the FLV Files

The first step to enabling multiple bit rate streams for your FlashCom application is to create an FLV file for each bit rate. With the MultiBitrateStreamer component used in this section, you can switch between two qualities of the same content. Thus, two FLV files can be created with your preferred FLV encoding application. In the following example, download the MPEG-2 file from the Open Video Project web site:

http://www.open-video.org/details.php?videoid=4425

Once you have downloaded this video clip (or have located a source clip of your own), you're ready to compress two versions of the clip in FLV format. We recommend using an encoder that can batch process files, such as Sorenson Squeeze or Wildform Flix. Otherwise, you can use the Flash Video Exporter tool that ships with Flash Pro.

To create two bit rate-specific FLV files using Sorenson Squeeze 4.0:

  1. In Squeeze's Input panel, click the Import File button. Browse to the location of your source clip.

  2. In Squeeze's Format & Compression Settings panel (see Figure 7-13), expand the Macromedia Flash Video (.flv) group, and select the 384K_Stream preset. Ctrl-click (Cmd-click on Mac) the 56K_Dial_Up_Stream preset. Click the Apply button. The presets are now applied to the source clip in the Job list (located at the lower right of the Squeeze UI).

  3. In the Job list, expand the 384K_Stream node and double-click the Spark Pro setting. In the Video settings of the dialog box, change the Method setting to Sorenson 2-Pass VBR. Clear the Maintain Aspect Ratio checkbox, and set the Frame Rate to 24, as shown in Figure 7-27. The 2-Pass VBR option produces higher-quality video output. Since the source footage for our example is already MPEG, the source aspect ratio is 1:1, instead of 4:3.

    Figure 7-27. Optimal settings for the source clip at 384 Kbps


  4. Repeat Step 3 for the 56K_Dial_Up_Stream preset. Instead of using a frame rate of 24, choose 10, as shown in Figure 7-28.

    Figure 7-28. Optimal settings for the source clip at 56 Kbps


  5. Click the Squeeze It! button located at the lower-right corner of the Squeeze UI. Squeeze starts to compress the source clip with the selected presets.

  6. When Squeeze has finished creating the FLV files, rename the 384 Kbps file to sample_high.flv. Rename the 56 Kbps file to sample_low.flv. Read the next section for information on relocating these files to their final destination.

7.4.2. Configuring the FCS Application

The process for using multiple bit rate FLV files is the same as utilizing any FLV file for a FlashCom application. The FLV files are copied to an instance folder of the streams folder within your application folder. For example, if you want your FLV files accessible to the default instance, _definst_, of an application named broadcast, you would create the folder structure shown in Figure 7-29.

Figure 7-29. The folder structure for the stream locations


You can also set up your FlashCom Server to point to a location outside of your application instance folder by using a virtual directory. Virtual directories are visited later in this chapter and discussed in "Uploading Prerecorded Streams" in Chapter 5.

Once you have copied your FLV files to the streams/_definst_ folder (or your preferred application instance folder), you need to add the Server-Side ActionScript code necessary for the custom StreamController component used for this exercise. At the root of your application folder (e.g., applications/broadcast), create a text file named main.asc with the following code:

Client.prototype.getLength = function (streamName) {
  return Stream.length(streamName);
};

The getLength( ) method enables the StreamController component to retrieve the duration (in seconds) of a stream or FLV file, accessible from the application instance.

7.4.3. Providing an Interface to Select and Switch the Streams

To understand how a multi-bitrate controller works, download the following file from the book's web site:

http://www.flash-communications.net/examples/ch07/MultiBitrateStreamer/MultiBitrateStreamer_100.zip

After you have downloaded the file, uncompress the archive to access the .fla and .as files associated with this sample. Open MultiBitrateStream_100.fla in Flash MX 2004, and open the Library panel. Double-click the MultiBitrateStreamer component. As shown in Figure 7-30, this component has three primary elements:


Display area

The mcVideo instance at the top of the component's Stage is a movie clip instance containing a nested Video object named vWindow. The mcVideo instance can scale based on the dimensions of the source video clip it is displaying.


Control bar

Below the mcVideo instance is the cscControl instance, which is an instance of the StreamController component, located in the MultiBitrateStreamer assets folder of the Library. This component works much like the MediaController component, yet it adds less weight to the overall file size of the Flash movie. The StreamController can pause, play, and scrub the video stream. The current time and duration of the stream are also displayed below the control interface.


Radio buttons

Two RadioButton instances are located below the cscControl instance. The crbLow instance, labeled Low, is the default option. When this button is selected, the stream playing in the mcVideo instance will be the low-quality FLV stream. The crbHigh instance, labeled High, enables the high-quality FLV stream to play.

Figure 7-30. The timeline of the MultiBitrateStreamer component


The StreamController and RadioButton instances use a Miniml font face called hooge 05_53. While you may not be able to see this font display within the Flash MX 2004 IDE, the published file loads the font from the MultiBitrateStreamer_library.swf file. This file must be uploaded to the web server along with the Flash movie containing the MultiBitrateStreamer component. You can purchase several pixel-based fonts designed for Flash movies at http://www.miniml.com.

Now, open the MultiBitrateStreamer.as file. This is the class file used by the MultiBitrateStreamer component. For the purposes of this example, the publicly inspectable variables of the component are discussed, in addition to the switchQuality( ) method. At the top of the .as file, the following public variables are exposed. You can set these values in the Parameters tab of the Properties panel for each instance of the MultiBitrateStreamer component:

[Inspectable(defaultValue=false)]
public var initHigh:Boolean;
[Inspectable(defaultValue=160)]
public var lowWidth:Number;
[Inspectable(defaultValue=120)]
public var lowHeight:Number;
[Inspectable(defaultValue="stream_low")]
public var lowStreamName:String;
[Inspectable(defaultValue=320)]
public var highWidth:Number;
[Inspectable(defaultValue=240)]
public var highHeight:Number;
[Inspectable(defaultValue="stream_high")]
public var highStreamName:String;
[Inspectable(defaultValue="rtmp://localhost/broadcast")]
public var fcsURL:String;

The first variable, initHigh, determines whether the high-quality stream is the first stream displayed in the mcVideo instance. By default, the value is set to false.

The lowWidth and lowHeight values establish the dimensions of the low-quality stream, as displayed by the mcVideo instance. The lowStreamName value sets the low-quality stream's name, which is the name of the low-quality FLV file you compressed earlier in this sectionwithout the .flv extension.

The highWidth, highHeight, and highStreamName values work the same as their low counterparts, except that they are used with the high-quality stream.

The fcsURL is the RTMP path to your FlashCom application instance.

The switchQuality( ) method is the listener method used by the crbLow and crbHigh instances, as initialized by the init( ) method. The switchQuality( ) method stops the currently playing stream by invoking play(false) and begins playback of the alternative stream. The playedTime variable retrieves the current time of the stream from the StreamController instance, cscControl. Note that this time value is not equivalent to the NetStream.time of the currently playing streamNetStream.time is a relative time value; it will reset to 0 each time you initialize a new NetStream.play( ):

public function switchQuality (oEvent:Object):Void {
  var newQuality:String = oEvent.target.data;
  var owner = oEvent.target._parent;
  var playedTime:Number = owner.cscControl.time;
  var streamToPlay:String =
       newQuality == "low" ? owner.lowStreamName : owner.highStreamName;
  owner.activeStream.play(false);
  owner.vWindow.attachVideo(null);
  owner.activeStream.play(streamToPlay, playedTime,
                        (owner.cscControl.isPaused ? 0 : -1) );
  owner.vWindow.attachVideo(owner.activeStream);
  owner.cscControl.streamName = streamToPlay;
  if (!owner.cscControl.isPaused) owner.cscControl.streamTime =  playedTime;
  owner.streamQuality = newQuality;
  owner.size( );
}

If you go back to the main timeline of the MultiBitrateStream_100.fla document and select the instance of the component on the Stage, you'll notice that the Properties panel contains working values for the sample stream compressed earlier in this section. If you test the movie and have a connection to the Internet, the low-quality FLV stream will load into the mcVideo instance. Clicking the high-quality radio button resizes the mcVideo instance and loads the high-quality stream. If you have configured your FlashCom Server with an application instance containing your multiple bit rate FLV files, go back to the Flash MX 2004 IDE and change the fcsURL path in the Properties panel to reflect the location of your application.