public
Last active

Creating a buffered netstream object to capture perfect webcam video from flash

  • Download Gist
gistfile1.as
ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// This file is a super-simple example of capturing a webcam up to a flash media server
// You can use RED5 1.0, Wowza, or FMS for this example - there are demo recording apps
// (oflaDemo for red5, vod for Wowza) included with each. Note: red5 .9xx reportedly
// does not like the buffered upstream sending we're doing here.
// All of the existing examples I could find simply had you "stop" the netstream object
// when the user was done recording, which, if you have any client-side buffer at all completely
// borks the recorded FLV. This example uses a 60 second client-side buffer, which
// allows you to capture high-quality webcam videos even if the upstream bandwidth
// isn't perfect or consistent. Once the user is done, the buffer continues sending up
// the data, and as long as you didn't overflow the client-side buffer, the video should
// be perfect.
 
// create basic netConnection object
var nc:NetConnection = new NetConnection();
 
// connect to the flash media server (FMS, RED5, WOWZA)
nc.connect(YOUR_FMS_HOST);
 
// create the netStream object and pass the netConnection object in the constructor
var ns:NetStream = new NetStream(nc);
 
// get references to the camera and mic
var cam:Camera = Camera.get();
var mic:Microphone = Microphone.get();
 
// setup some simple settings with the cam and mic for quality
setupCameraMic();
 
function playClicked():Void
{
ns.setBufferTime(60);
ns.attachVideo(cam);
ns.attachAudio(mic);
}
 
function stopClicked():Void
{
// pause the netStream object
ns.pause();
//detach video
ns.attachVideo(null);
ns.attachAudio(null);
//also detach video from our monitor, in our case an object called videoContainer
videoContainer.attachVideo(null);
//spin up a thread to check the buffer every 500ms and close it once empty
flushinterval = setInterval(flusher, 500);
}
 
function flusher():Void{
//This gets called by the setInterval above every 500ms until the interval is
//cleared below, when the buffer is empty
if((ns.bufferLength == 0)){
//great, the buffer is empty, close it up and stop this interval
clearInterval(flushinterval);
ns.close();
//You can now tell the user their recording is complete
}else{
trace("still flushing: " + ns.bufferLength)
//You'll probably want to show some message to the user here that
//their recording is uploading/completing
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.