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:
Create a new FlashCom application on the server. Within the FlashCom
applications directory, create a new
subdirectory named surveillance. Create a new Flash document, and save it as
Surveillance.fla. Within the Flash document, create a new Video symbol in the Library. 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. 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. Rename the default layer to Contents, and create
a new layer above it named Actions. Select the first keyframe of the Actions layer,
and open the Actions panel. 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( );
Test the movie. You should see the TextArea report when the video is
recording and pausing. Create a new Flash document, and save it as
SurveillanceView.fla. Create a new Video symbol in the Library of
SurveillanceView.fla. Create an instance of the Video object on the Stage, and give it an
instance name of content_vid. Rename the default layer to Content, and create
a new layer named Actions. Select the first keyframe of the Actions layer,
and open the Actions panel. 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);
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;
}
|