This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Publishing Streams in Detail
|
163
During publishing, bufferLength returns the amount of data, in seconds, currently in
the buffer. The
bufferTime property returns the total buffer time. In practice, the
buffer is always larger than the time set via setBufferTime( ); this allows the amount
of data in the buffer to start to exceed the buffer time without any frames being
immediately dropped. When the buffer time is exceeded, NetStream.onStatus( ) is
passed an information object with a
code value of “NetStream.Buffer.Full”. This pro-
vides an opportunity to increase the buffer time, reduce the amount of data being
sent into the stream, or stop recording altogether. If the buffer length reaches one
and a half times the buffer time, all video messages are dropped. If it reaches two
times the buffer time, all audio and video frames are dropped. When a buffer is
empty, a “NetStream.Buffer.Empty” message will be sent to the onStatus( ) handler.
See Example 5-2 and Example 5-5 for examples of responding to “NetStream.Buffer”
messages.
Snapshots, Stop Motion, and Time-Lapse Video
FlashCom makes it possible to capture a single video frame or snapshot from a cam-
era or video source, instead of capturing all the frames as they arrive from the source.
The NetStream.attachVideo( ) method has an optional second parameter named
snapShotMilliseconds. When the parameter is omitted, each frame from the source is
sent to the stream according to the frame rate set using Camera.setMode( ). If a value
of 0 is passed in, a single frame of video is placed in the stream. For example:
out_ns.attachVideo(cam, 0);
To be certain the frame is not dropped and has been sent to the server before the
stream is closed, two things must be done. The NetStream.setBufferTime( ) method
must be used to make sure the frame is buffered, and an onStatus( ) handler must be
used to make sure the buffer is empty before closing the stream. Example 5-2 lists the
essential code that sets up a stream to publish a snapshot and then takes one when
the Record button is clicked. The complete listing is available on the book’s web site.
Example 5-2. Taking buffered snapshots
// Called after a network connection is made.
function initProgram( ) {
if (out_ns) out_ns.close( );
out_ns = new NetStream(nc);
out_ns.setBufferTime(2);
out_ns.onStatus = function (info) {
writeln(info.code);
if (info.code == "NetStream.Buffer.Empty") {
this.close( );
in_ns.play("mugshot");
}
};
if (in_ns) in_ns.close( );
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
164
|
Chapter 5: Managing Streams
In Example 5-2, whenever a network connection is created, the code creates a Net-
Stream object, gives it a buffer time of 2 seconds, and creates an onStatus( ) event
handler:
out_ns = new NetStream(nc);
out_ns.setBufferTime(2);
out_ns.onStatus = function (info) {
writeln(info.code);
if (info.code == "NetStream.Buffer.Empty") {
this.close( );
in_ns.play("mugshot");
}
};
When the snapshot is taken, the buffer briefly holds some video information until it
is successfully sent. When all the video data has been sent, the onStatus( ) handler
receives an
info.code value of “NetStream.Buffer.Empty”. At that point, it can safely
close the stream without the snapshot frame being lost. To take the actual snapshot,
the code calls doRecord( ) to attach the video source to the stream and publish the
stream:
out_ns.attachVideo(cam, 0);
out_ns.publish("mugshot", "record");
Shortly afterward, the buffer empties and the stream closes.
The
snapshotMilliseconds parameter can also be used to create time-lapse videos in
which snapshots are taken at regular (usually long) intervals and then played back
quickly. This technique can be used to speed up an otherwise slow process, such as
watching a plant grow.
To make time-lapse video work, we cannot just publish a stream, leave it open, and
send snapshots at regular intervals. The stream time progresses as long as the stream
is being published. When such a stream is played back, the frames change at the
in_ns = new NetStream(nc);
snapshot_video.attachVideo(in_ns);
cam = Camera.get( );
preview_video.attachVideo(cam);
System.showSettings(0);
record_btn.setEnabled(true);
connect_btn.setLabel("Disconnect");
in_ns.play("mugshot");
}
// Called when the user clicks the Record button.
function doRecord( ) {
if (out_ns) out_ns.close( );
out_ns.attachVideo(cam, 0);
out_ns.publish("mugshot", "record");
}
Example 5-2. Taking buffered snapshots (continued)

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.