Remote Video Aquisition using Flash
Flash because of it's video broadcasting capabilities and the reach of it's installed base may be an ideal way to allow live video of audience members or remote participants in a live broadcast.
Broadcasting
It allows anyone with the Flash plugin installed to be a broadcaster from a webcam or other camera connected to their computer.
Building off of our Flash Camera Viewing code from las week we can add a bit of code to send the video to the internet.
To do this, we need to make a connection to a server using the NetConnection object and additional create a NetStream object which sends the video.
Here is an update to the code that we have been using:
// First, declare all of the variables
// URL for the connection to the server
var rtmp_url:String = "rtmp://itp-flash.es.its.nyu.edu/sve204/live";
// Name of the stream that we'll send to the server
var stream_name:String = "live";
// NetConnection Object
var nc:NetConnection;
// NetStream Object
var ns:NetStream;
// Camera Object
var camera:Camera;
// Microphone Object
var microphone:Microphone;
// Video Display Object
var video:Video;
// End Variable Declaration
// Start Program
// Create connection object
nc = new NetConnection();
// Add netstatus event handler
// This will tell us when we have connected by calling the "netStatusHandler" function
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// Add security event handler
// This will tell us if their is a security issue by calling "securityErrorHandler"
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
// Connect to to the server
nc.connect(rtmp_url);
trace("Connecting to " + rtmp_url);
// End Main Program
// Start Declaring Functions
// netStatusHandler Function, called when we connect or the status changes in any way
function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
// Yay, we connected
// Let's call a function that sets up everything and starts streaming
trace("Connected!");
startStreaming();
break;
case "NetStream.Play.StreamNotFound":
// Problem with the connection or stream name
trace("Stream not found: " + rtmp_url);
break;
}
}
// securityErrorHandler function, called when there is a security issue
function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
// startStreaming function, called when we have a connection
// this function sets up the camera, microphone, video display and so on
function startStreaming()
{
trace("Starting Streaming");
// Camera or Video Input Capture
camera = Camera.getCamera();
// Microphone or Line-in Capture
microphone = Microphone.getMicrophone();
// Video Display Component
video = new Video();
// Set Video Postion
video.x = 0;
video.y = 0;
video.height = 240;
video.width = 320;
// Display video component
addChild(video);
// Attach camera to video component
video.attachCamera(camera);
// Create the NetStream object
ns = new NetStream(nc);
// Attach the camera to the NetStream
ns.attachCamera(camera);
// Attach the microphone to the NetStream
ns.attachAudio(microphone);
// Now start the streaming
ns.publish(stream_name);
trace("Called Publish: " + stream_name);
}
You can download the Flash Project (fla) or the try the compiled version.
Server
The Flash Media Server (and it's clones) allows this broadcast to be sent to a large audience or just pulled up by us in a browser for display on Television. The above code references the Flash Media Server that ITP has (using my account). If you are going to be doing projects using this, please ask me to set up an account for you or your group.
Playback
To display a live stream coming from elsewhere, we need a Flash video player application. It is easy enough to build one of these in Flash as well.
This code is almost exactly the same as above but it doesn't have a camera or a microphone and instead of "publish" it uses "play".
// First, declare all of the variables
// URL for the connection to the server
var rtmp_url:String = "rtmp://itp-flash.es.its.nyu.edu/sve204/live";
// Name of the stream that we'll send to the server
var stream_name:String = "live";
// NetConnection Object
var nc:NetConnection;
// NetStream Object
var ns:NetStream;
// Camera Object
var camera:Camera;
// Microphone Object
var microphone:Microphone;
// Video Display Object
var video:Video;
// End Variable Declaration
// Start Program
// Create connection object
nc = new NetConnection();
// Add netstatus event handler
// This will tell us when we have connected by calling the "netStatusHandler" function
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// Add security event handler
// This will tell us if their is a security issue by calling "securityErrorHandler"
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
// Connect to to the server
nc.connect(rtmp_url);
trace("Connecting to " + rtmp_url);
// End Main Program
// Start Declaring Functions
// netStatusHandler Function, called when we connect or the status changes in any way
function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
// Yay, we connected
// Let's call a function that sets up everything and starts streaming
trace("Connected!");
startStreaming();
break;
case "NetStream.Play.StreamNotFound":
// Problem with the connection or stream name
trace("Stream not found: " + rtmp_url);
break;
}
}
// securityErrorHandler function, called when there is a security issue
function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
// startStreaming function, called when we have a connection
// this function sets up the camera, microphone, video display and so on
function startStreaming()
{
trace("Starting Streaming");
// Camera or Video Input Capture
camera = Camera.getCamera();
// Microphone or Line-in Capture
microphone = Microphone.getMicrophone();
// Video Display Component
video = new Video();
// Set Video Postion
video.x = 0;
video.y = 0;
video.height = 240;
video.width = 320;
// Display video component
addChild(video);
// Attach camera to video component
video.attachCamera(camera);
// Create the NetStream object
ns = new NetStream(nc);
// Attach the camera to the NetStream
ns.attachCamera(camera);
// Attach the microphone to the NetStream
ns.attachAudio(microphone);
// Now start the streaming
ns.publish(stream_name);
trace("Called Publish: " + stream_name);
}
Flash Project (fla) or compiled version.