Publishing Live Video with AS3 to a
Flash Media Server

March 29, 2010

Last week I wrote about closed captions on video players and the week before that I wrote about live streaming commencement. Well, we (UNC) got the proposal back from a company offering a solution; it was around $32k. As a result of such a high price tag there is a good chance that we will have to provide our own solution. Since we have been using flash video the past few days I decided to do a tutorial on publishing a live stream to a media server.

Here is the solution I will be testing:

Publish Video to a Media Server:

package {

import flash.display.Sprite;

import flash.events.*;

import flash.media.Camera;

import flash.media.Video;

import flash.net.NetStream;

import flash.net.NetConnection;

public class PublishStream extends Sprite {

private var video:Video;

var ns1:NetStream;

var ns2:NetStream;

var nc:NetConnection = new NetConnection();

var camera:Camera = Camera.getCamera();

 

public function PublishStream() {

camera.setMode(160,120,15,false);

camera.setQuality(0,75);

video = new Video(380, 240);

addChild(video);

nc.objectEncoding = 0;

nc.connect("address to media servers");

nc.addEventListener("netStatus", onNCStatus);

}

 

private function onNCStatus(event:NetStatusEvent):void {

trace("Connected to Server");

 

//publish the feed:

ns1 = new NetStream(nc);

ns1.attachCamera(camera);

ns1.publish("myStream", "live");

//grab the feed and display it:

ns2 = new NetStream(nc);

ns2.play("myStream");

video.attachNetStream(ns2);

}

}

}

Set Up Variables

“ns1” and “ns2” are the streams between the flash client and the media server. There are two of them because one will publish the video to the server, and the second will grab the source off the server and display it on the page. We could grab it directly from the camera, but then we wouldn’t know if there was a lag in what the user sees.

“nc” is the connection that those streams run over.

Finally “camera” is the user’s web camera.

PublishStream()

We need to set up the users cameras, and create a video item on the stage. Next we connect to the media server and create an event listener to listen for a response.

Set Mode:

Camera.setMode(width, height, fps, override)

FPS, or Frames per Seconds defaults to 15; and Override, is a true/false and basically asks if the camera cannot perform these settings (width,height,FPS) can Actionscript overwrite.

onNCStatus()

Once we hear back from the media server, the object “ns1” attaches the camera and then publishes it to the streaming server under the instanceName of “myStream”.

Finally, “ns2” grabs the video feed “myStream” off the server and displays it on our “video” instance on the stage.

Conclusion

After trying different setQuality levels, I have determined that the quality is too poor to be displayed on a projector. Stay tuned, we have a meeting on Wednesday to discuss solutions, or perhaps we will shell out $32k!

Tags: , ,

2 Responses to “Publishing Live Video with AS3 to a
Flash Media Server”

  1. pete says:

    I know this is a VERY late reply, but this might help any other people looking here.

    In response to your conclusion, the reason you experienced such loss of quality is that you should be specifying either the bandwith or the compression, not both.

    If you use setQuality(0,75) you will get the best stream for a 75% compression quality. Whereas if you used setQuality(16384,0) you would get the best compression for the bandwidth limitation of 16384 bps. When you give both values it sets up a lowest common denominator scenario. So it has to keep stepping down the quality until a solution will work for both specified values simultaneously.

    I hope this helps someone. :)

  2. Jesse says:

    @pete Awesome! Thank you very much for the comment, I will change the example to this, and update my code.