Communication and Security with the Flash Communication Server (Page 1 of 4 )
Streaming Audio, Video, and ActionScript Data
Once you establish a connection using a NetConnection object in the client, you can use it to send or receive a stream containing audio and/or video. Suppose you know that a recorded Flash video file named Ryerson_High_Speed.flv is available in the instanceís public directory. You can attach the video stream to a video object, here named videoArea, using Video.attachVideo( ) and play it using NetStream.play( ). The NetStream object,in_ns, is created by passing the NetConnection object,nc, from the preceding example to the NetStream( ) constructor function:
in_ns = new NetStream(nc); videoArea.attachVideo(in_ns); in_ns.play("public/Ryerson_High_Speed");
In this case, the video appears in a video object that was placed on the Stage during authoring and given the instance namevideoArea; the audio within the video stream is heard automatically. Note that the .flv extension is not included in the stream URI passed into the play( ) method.
Publishing a stream in a Flash movie is also easy. Here, we create a new NetStream object,out_ns, and attach the audio and video streams from the userís microphone and camera before publishing the stream using NetStream.publish( ):
out_ns = new NetStream(nc); out_ns.attachAudio(Microphone.get()); out_ns.attachVideo(Camera.get()); out_ns.publish(userName); // Name the stream after the user's name.
The Camera class can get a video stream from a web camera or other video source, and the Microphone class can get an audio stream from a microphone or other source.
You can create multiple streams (NetStream objects) for a single NetConnection,as shown in Figure 1-3. Each stream can publish or play a stream to or from the server, but data within a stream travels in only one direction. If each user must publish a single stream, it is easiest to name the stream after his unique username or unique ID number.
Figure 1-3. A client publishing a stream and receiving two streams over one NetConnection
On the server, the Stream class can be used to create playlists of streams and even to play and publish streams across multiple servers. A playlist defines a sequence of streams to be played one after the other. The server buffers them so they play seamlessly without interruption; when one stream ends, another begins. Chapter 5 covers the NetStream and Stream classes in depth.
Camera, Microphone, and Video
The Camera and Microphone classes, covered in depth in Chapter 6, provide access to video and audio sources (typically cameras and microphones) on the client system. Multiple cameras and microphones can be accessed by the same Flash movie. Only one video and audio stream can be published or played within a single NetStream; however, there can be multiple NetStreams within a single NetConnection, as shown in Figure1-4. The Flash Player is responsible for encoding all audio and video data. Video encoding from each video source is currently limited to constant bit rate encoding at a single resolution. The Camera and Microphone classes can be used to control the amount of audio and video data being sent to the server and can be dynamically adjusted to match the bandwidth limitations of the clients connected to an application. No real-time hardware encoding options are currently available, though that may have changed by the time you read this. Some video cards can present different resolution video streams from the same input as separate video sources so that multiple NetStream objects carrying different resolution video from the same source can be published simultaneously. Otherwise, the only way to provide different streams matched to the bandwidth of each client is to use multiple video sources.
Figure 1-4. Attaching audio and video sources to a published stream and attaching a playing stream to an embedded Video object to view incoming video
The Camera class provides a convenient way to change the resolution, frame rate, and quality settings for each video source. The Microphone class allows setting the sampling rate, gain, and silence level, among others. A Video object is used to display video within a Flash movie and can be dynamically resized and moved as video is played. See Chapter 6 for working examples.