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

6.4. Building a Surveillance Application

In the previous example, you had the chance to build an application that uses the Camera and Microphone classes. Next, we'll build a simple surveillance application that records to a stream only when the activity levels are high enough to warrant it.

To build the application, complete the following steps:

  1. Create a new FlashCom application on the server. Within the FlashCom applications directory, create a new subdirectory named surveillance.

  2. Create a new Flash document, and save it as Surveillance.fla.

  3. Within the Flash document, create a new Video symbol in the Library.

  4. Create an instance of the Video symbol on the main timeline, and give it the instance name content_vid. Place the instance approximately in the center of the Stage.

  5. Create a TextArea component instance on the Stage just below the Video object. Use the Properties panel to give it an instance name of ctaLog and change its dimensions to 160 x 200.

  6. Rename the default layer to Contents, and create a new layer above it named Actions.

  7. Select the first keyframe of the Actions layer, and open the Actions panel.

  8. Add the following code to the Actions panel:

    // Create the Camera and Microphone objects.
    var user_mic = Microphone.get( );
    var user_cam = Camera.get( );
    
    // Create the NetConnection object to connect to the server.
    var fcs_nc = new NetConnection( );
    
    // Create a NetStream variable.
    var record_ns;
    
    // Initialize two Boolean variables to false. They indicate whether the
    // activity level is high enough and whether the stream is currently recording.
    var activity = false;
    var recording = false;
    
    // Set the minimum activity level for the microphone and camera.
    user_mic.setSilenceLevel(10, 2000);
    user_cam.setMotionLevel(50, 2000);
    
    // Set the camera mode and quality.
    user_cam.setMode(320, 240, 30);
    user_cam.setQuality(0, 100);
    
    // Attach the video to the Video object.
    content_vid.attachVideo(user_cam);
    
    // Connect to the FlashCom application, and open a stream.
    fcs_nc.connect("rtmp:/ surveillance");
    record_ns = new NetStream(fcs_nc);
    
    // Determine when the microphone and camera are activated and deactivated.
    // In all cases, call the calculate( ) function.
    user_mic.onActivity = function (activated) {
      calculate(activated);
    };
    
    user_cam.onActivity = function (activated) {
      calculate(activated);
    };
    
    // calculate( ) determines what action should take place when
    // the microphone and/or camera is activated or deactivated.
    function calculate (activated) {
      activity = activated;
      if (activity && !recording) {
        startRecording( );
      }
      else if (!activity && recording) {
        pauseRecording( );
      }
    };
    
    // Start recording the stream.
    function startRecording ( ) {
      recording = true;
      record_ns.attachAudio(user_mic);
      record_ns.attachVideo(user_cam);
      record_ns.publish("security", "append");
      // Display date/time recording begins, such as "Thu Dec 9 22:32:32 GMT-0500 2004"
      ctaLog.text += "Started recording" + new Date( ) + newline;
    }
    
    // Stop recording the stream.
    function pauseRecording ( ) {
      recording = false;
      record_ns.publish(false);
      ctaLog.text += "Paused recording" + new Date( ) + newline;
    }
    
    // Initialize application by starting to record.
    startRecording( );

  9. Test the movie. You should see the TextArea report when the video is recording and pausing.

  10. Create a new Flash document, and save it as SurveillanceView.fla.

  11. Create a new Video symbol in the Library of SurveillanceView.fla.

  12. Create an instance of the Video object on the Stage, and give it an instance name of content_vid.

  13. Rename the default layer to Content, and create a new layer named Actions.

  14. Select the first keyframe of the Actions layer, and open the Actions panel.

  15. Add the following code to the Actions panel:

    // Create the NetConnection object, and connect to the FlashComm
    // application on the server.
    var fcs_nc = new NetConnection(  );
    fcs_nc.connect("rtmp:/surveillance");
    
    // Create the NetStream object, and play back the stream.
    var playback_ns = new NetStream(fcs_nc);
    playback_ns.play("security");
    content_vid.attachVideo(playback_ns);

  16. Test the movie. You should see the surveillance stream play. Only the portions in which the activity level was high enough were recorded.

The code from SurveillanceView.fla is fairly straightforward. However, the code from Surveillance.fla is complex enough that it could benefit from some further explanation.

As with any application that uses audio and video input, the first step is to create Camera and Microphone objects:

var user_mic = Microphone.get(  );
var user_cam = Camera.get(  );

After creating the NetConnection object and a variable to store the NetStream object, you next want to declare and initialize two Boolean variables. The activity variable indicates whether the activity level of the microphone and/or camera is currently high enough. The recording variable simply indicates whether the stream is currently recording.

var activity = false;
var recording = false;

The application records audio and video only when the activity levels exceed the thresholds set via Microphone.setSilenceLevel( ) and Camera.setMotionLevel( ). In the example code, the minimum activity levels are set to 10 and 50, with a 2-second timeout. You may want to adjust those levels in your own application if you find that they are not optimal.

user_mic.setSilenceLevel(10, 2000);
user_cam.setMotionLevel(50, 2000);

Even when the stream is not recording, the code still displays the video. Cameras usually take a short while to initialize, and so the delay could cause difficulties if you had to reinitialize the camera connection each time you resumed recording. By continuously displaying the camera input in the Video object, we initialize the camera only once:

content_vid.attachVideo(user_cam);

The onActivity( ) event handlers are called automatically when the microphone or camera is activated or deactivated. This surveillance application handles both in the same way, so each of the event handler methods simply proxies the call to a custom function called calculate( ).

The calculate( ) function determines what action should take place when a camera and/or microphone is activated and/or deactivated. It's not as simple as starting to publish the stream when one of the devices is activated and stopping it when one of the devices is deactivated. Remember that, since either device can cause the stream to start publishing, you need to check to see whether the stream is already being published. The calculate( ) function does that. It starts to publish the stream (by calling startRecording( )) only if the activity level is high enough and if the recording variable is false. Otherwise, it stops publishing the stream only if the activity level is not high enough and the stream is currently publishing.

Publishing the stream is fairly basic. Of course, you'll want to set the recording variable to true so that Flash will be able to make the appropriate calculations subsequently. Then, as usual, attach the audio and video to the NetStream object and publish it. Additionally, add a line of text to the text area to indicate when the stream started recording:

function startRecording (  ) {
  recording = true;
  record_ns.attachAudio(user_mic);
  record_ns.attachVideo(user_cam);
  record_ns.publish("security", "append");
  // Display date/time recording begins, such as "Thu Dec 9 22:32:32 GMT-0500 2004"
  ctaLog.text += "Started recording" + new Date( ) + newline;
}

Pausing the stream is also very basic. Of course, you'll want to set recording to false so adjustments can be made correctly when a microphone or camera is activated or deactivated. Then, as usual, call publish( ) with a value of false to stop publishing the stream. And add a line of text to the text area to indicate when the stream paused:

function pauseRecording (  ) {
  recording = false;
  record_ns.publish(false);
  ctaLog.text += "Paused recording" + new Date( ) + newline;
}