For a recent campaign we had to integrate a live video stream into a banner. Therefore the following code will focus mainly on AS2, but the code is easily portable to AS3.
The video was streamed using Wowza Media Server (though Flash Media Server works in much the same way) and the servers itself were behind a load balancer.
First, we tried using the FLVPlayback component, but the component was opening multiple connections, where only one connection was really required and it didn’t handle disconnects well. That’s why we opted for the good old NetConnection/NetStream approach.
However, because the content was behind a load balancer, we got back a ‘NetConnection.Connect.Rejected’ status code. The reason why is because the load balancer will interpret the request and then sends back a redirection link to the server it wants you to connect to.
We used the following code to fetch said redirection link (remember, this example uses AS2 syntax):
// define the variables to hold the NetConnection and NetStream classes.
var WowzaConnection:NetConnection = new NetConnection();
var WowzaStream:NetStream;
// initiate the connection and add an event handler
WowzaConnection.onStatus = statusHandler;
WowzaConnection.connect("rtmp://your-streamer-url/");
// this is just a quick helper function to easily close the connection
function closeConnection(){
WowzaConnection.close();
WowzaStream.close();
}
function cuePointHandler(infoObject:Object){
// insert cue point processing here
// or leave blank if you don't need to process cue points
// be sure to add this handler however, since Flash will throw an error if it isn't available
}
function metaDataHandler(infoObject:Object){
// insert metadata processing here
// or leave blank if you don't need to process metadata
// be sure to add this handler however, since Flash will throw an error if it isn't available
}
// definition of the status event handler
function statusHandler(infoObj:Object){
trace("onStatus: "+infoObj.code);
switch(infoObj.code){
// The connection was established. Initiate the stream and add event handlers.
case "NetConnection.Connect.Success":
WowzaStream = new NetStream(WowzaConnection);
WowzaStream.onCuePoint = cuePointHandler;
WowzaStream.onMetaData = metaDataHandler;
WowzaStream.onStatus = statusHandler;
WowzaStream.play("streamname");
// add the stream to a Video object placed on the stage, with it's instance name set to 'video'
video.attachVideo(WowzaStream);
break;
case "NetConnection.Connect.Rejected":
// here's the reject... The load balancer rejects the connection, but passes a redirect URL
// back through the infoObj.ex.redirect parameter.
if (infoObj.ex.code == 302) {
// connect to the new streamer URL after a timeout of 100ms
// the timeout isn't necessary, but is implemented to prevent
// unwanted behaviour
setTimeout(function(){
WowzaConnection.connect(infoObj.ex.redirect);
}, 100);
}
break;
}
}
And that’s about it really. The NetConnection will try to connect to the streamer URL, but will disconnect once the server throws the ‘NetConnection.Connect.Rejected’ status. After that, it will reconnect to the redirect URL and the connection should throw a ‘NetConnection.Connect.Success’ status.
Recent Comments