After installing Red5 you will have HTTP server on port 5080 (it is the default value, but you can change it during install). You can find there sample named Publisher. It uses Red5 sample application oflaDemo. In this post I’ll tell you how to write something simple like that sample.
First we will extend Flash class Video to work with network streams. We will need for that flash.net.NetConnection to connect to Red5 server.
package media.video
{
import flash.net.NetConnection;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.events.Event;
public class Red5Video extends Video
{
protected var connection:NetConnection;
public function Red5Video(
url:String, width:int = 320, height:int = 240
) : void {
super(width, height);
connection = new NetConnection();
connection.addEventListener(
NetStatusEvent.NET_STATUS,
netStatusHandler
);
connection.connect(url);
}
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case 'NetConnection.Connect.Success':
// Dispatching 'connected' event when
// network connection succeeded
this.dispatchEvent(new Event('connected'));
break;
}
}
}
}
In the next step we are going to create flash.net.NetStream instance on established connection and listen for some errors encountered during execution.
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case 'NetConnection.Connect.Success':
connectStream();
// Dispatching 'connected' event when
// network connection succeeded
this.dispatchEvent(new Event('connected'));
break;
case 'NetStream.Play.StreamNotFound':
trace('Stream not found');
break;
}
}
private function connectStream():void {
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}
The last thing we are going to do is to add streaming and playing methods to our class.
public function streamCam(streamName:String):void {
// Fetching camera
var camera = Camera.getCamera();
// Attaching camera to our video object
this.attachCamera(camera);
// Attaching camera to the stream
stream.attachCamera(camera);
// And finally publishing the stream
// assigning him a streamName identifier
stream.publish(streamName);
}
public function playStream(streamName:String):void {
// Starting to play published stream
// with streamName identifier
stream.play(streamName);
// Attaching it to out video object
this.attachNetStream(stream);
}
Finally we get this.
package media.video
{
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.media.Camera;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.Event;
public class Red5Video extends Video
{
protected var connection:NetConnection;
protected var stream:NetStream;
public function Red5Video(
url:String, width:int = 320, height:int = 240
) : void {
super(width, height);
connection = new NetConnection();
connection.addEventListener(
NetStatusEvent.NET_STATUS,
netStatusHandler
);
connection.connect(url);
}
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case 'NetConnection.Connect.Success':
connectStream();
// Dispatching `connected` event when
// network connection succeeded
this.dispatchEvent(new Event('connected'));
break;
case 'NetStream.Play.StreamNotFound':
trace('Stream not found');
break;
}
}
private function connectStream():void {
stream = new NetStream(connection);
stream.addEventListener(
NetStatusEvent.NET_STATUS,
netStatusHandler
);
}
public function streamCam(streamName:String):void {
// Fetching camera
var camera = Camera.getCamera();
// Attaching camera to our video object
this.attachCamera(camera);
// Attaching camera to the stream
stream.attachCamera(camera);
// And finally publishing the stream
// assigning him a streamName identifier
stream.publish(streamName);
}
public function playStream(streamName:String):void {
// Starting to play published stream
// with streamName identifier
stream.play(streamName);
// Attaching it to out video object
this.attachNetStream(stream);
}
}
}
Here are two samples for understanding how to use this class.
1. Simple Broadcaster
package media.video
{
import media.video.Red5Video;
import flash.events.*;
import flash.display.Sprite;
public class Broadcaster extends Sprite
{
protected var video:Red5Video;
public function Broadcaster():void {
video = new Red5Video(stage.loaderInfo.parameters.url);
video.addEventListener('connected', connectHandler);
addChild(video);
}
private function connectHandler(event:Event):void {
video.streamCam('TestStream');
}
}
}
2. Simple Subscriber
package media.video
{
import media.video.Red5Video;
import flash.events.*;
import flash.display.Sprite;
public class Subscriber extends Sprite
{
protected var video:Red5Video;
public function Subscriber():void {
video = new Red5Video(stage.loaderInfo.parameters.url);
video.addEventListener('connected', connectHandler);
addChild(video);
}
private function connectHandler(event:Event):void {
video.playStream('TestStream');
}
}
}