Wowza Community

ActionScript code to attempt to connect over rtmp and if that fails rtmpt (rollover)

Hi Charlie,

We implemented this into our wowza player and most of the time it works well. Unfortunately there have been some random instances of our video beginning to play and then being interupted after say 5 seconds with the error message we set to display at your “Failed to connect after “+ncTryIndex+” tries”);" point.

As mentioned it is only happening to a selection of people. We are not sure if it may be a strain on the server perhaps? That somehow sends a failed status when it has actually already connected?

We are using the code like below and replaced nc.connect(serverName) in the doConnect() function in your standard as2 player with tryConnect();

We set init() to run just before mainInit() at the bottom of the standard player.

As mentioned most of the time it works fine and it definitely switches ports if the first one is blocked. Any ideas? Thanks

var ncConnectTimer:Number = 0;

var ncConnectTimeout:Number = 8000; // 8 seconds

var ncDomain:String = “OUR IP”;

var ncProtocol:String = “rtmp”;

var ncProtocol2:String = “rtmpt”;

var ncPort:Number = 1935;

var ncPort2:Number = 80;

var ncApplication:String = “flvsecure”;

var ncTryIndex:Number = 0;

var url:String = “”;

function tryConnect()

{

clearConnectTimer();

switch(ncTryIndex)

{

case 0:

ncConnectTimeout= 8000

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

case 1:

ncConnectTimeout= 15000

ncPort = ncPort2;

ncProtocol = ncProtocol2;

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

default:

{trace(“Failed to connect after “+ncTryIndex+” tries”);

url = “”;

statusBox.text=“failed to connect”;

nc.close();

nc=null;

gotoAndStop(2);

}

break;

}

if (url.length > 0)

{

nc.connect(url);

ncConnectTimer = setInterval(_root, “tryReconnect”, ncConnectTimeout);

}

}

I don’t think it has anything to do with this code. It sounds like the player can’t connect to Wowza. Does it happen to same people over and over again or can it happen to any one at any time?

Charlie

I think if you put a port in your initial rtmp connection string this won’t happen, i.e., make your first attempt with something like this:

rtmp://[wowza-address]:1935/yourApp

Because Flash player 10 has a built-in protocol rollover, but it is only active if you leave off the port.

Richad

In that case, make sure the process is stopped when you get “NetConnection.Connect.Succcess” notification in your NetConnection NetStatusEvent handler.

Richard

Try adding a boolean: var isConnected:Boolean = false, then set to true on connect success, and test in tryReconnect:

function tryReconnect()
{
	trace("tryReconnect");
	clearConnectTimer();
	if (isConnected)
		return;
	ncTryIndex++;
	tryConnect();
}

Richard

I’m not sure. The only thing I can suggest is to add many trace statements to see what’s happening. (you can’t use break points because a timer is involved so you don’t want to stop the code)

Richard

The client-side of this is using a timer that is set for 3000 milliseconde (3 seconds). If the server returns “NetConnection.Connect.Failed” notification before that, a rollover happens, but otherwise 3 seconds is the interval. So check your code. Should be 3 seconds, and you can set it for less if you want.

Richard

Jake Hilton has a port tester here:

http://www.jakehilton.com/?q=node/43

You can use the one running on that page by putting your Wowza IP address and an Appname in the text boxes, and there is downloads for AS2 and Flex versions.

Richard

Flowplayer might have a plugin for this already. But if not you will have to modify Flowplayer source code. You have to set up a development environment for Flowplayer:

http://flowplayer.org/documentation/developer/development-environment.html

Then use Flex or Flash Builder menu: Search > Find in Files, and search for “.connect”

When you find where the RTMP connection happens, you can work that code in.

Richard

Flowplayer RTMP plugin can natively handle RTMP/RTMPT/RTMPE/RMPTE rollover. See plugin code here.

But there’s a weird behaviour when using RTMPE : http://flowplayer.org/forum/5/37221.

Still no fix …

Hi,

Thanks for your quick response but if I understand it correctly, I am already doing this.

i.e

var ncProtocol:String = “rtmp”;

var ncProtocol2:String = “rtmpt”;

var ncPort:Number = 1935;

var ncPort2:Number = 80;

var ncApplication:String = “flvsecure”;

var ncTryIndex:Number = 0;

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

Thanks,

Joe

For reference, I am using flash player 9 when i compile the swf…

Thanks, I have used

if(infoObj.code == “NetConnection.Connect.Success”) clearConnectTimer();

with the same clearConnectTimer function from this post, is this what you mean? When I test by closing the ports on my firewall it works ask expected and works on our live server 99% of the time. It’s just some cases that video plays for a few seconds and then brings up the error message.

Thanks,

Joe

Hi Richard, Thanks for this suggestion. I am going to try it. I should have really thought of this myself, as it is almost as though we are getting a NetConnection.Connect.Failed status after we have already got NetConnection.Connect.Success? If this is what’s happening, I have no idea why, but it makes sense that your change would solve this.

Nice one

Hi,

I am still having this problem, so this fix doesn’t seem to have sorted it.

It seems that the problem is happening in Firefox, I have not yet had a report of it happening in IE and cannot recreate it in IE.

I am using the as2 simple player example with this mod as I have detailed.

I am using the simple player in an external swf and loading it into my main swf everytime I start a new video, simply passing the file name in. Is there anything in the external swf that could be affecting it or perhaps cache of the last version of swf’s net connection info. Excuse my poor knowledge if this sounds stupid!

Thanks, this problem is really difficult to dianose as it is intermittent but I am pretty sure it is Firefox specific, unless it is Flash Player specific?

Thanks,

Joe

Thanks, one of the reasons I haven’t been able to do it this way is that it only seems to be happening on our production servers, I’m not sure if this is useful information. I have flash player debugger on IE so that I can get traces from the live swf, I’ll set this up for Firefox and see if I can see anything more. This was just my first steps incase there were any known issues with certain versions of firefox/certain versions of flash player! thanks

Hi charlie,

Just to to recap on what I wrote before, I have added something very similar to what you have suggested in this thread to your simple as2 player example and most of the time it works really well.

The player is being loaded into a parent swf each time we load a video.

For 99% of our customers it is fine, but sometimes it seems to intermittently be getting to the end of your switch statement and doing what I have set it to do when it cannot connect. That is go to frame 2 which contains and error message.

About 1% of our users have reported that in some cases the video plays for around 5 ses and then the error message is shown.

I think the problem may be specifically happening in Firefox as we have been able to recreate it in ff but it’s not happening everytime.

I also mentioned that it was only happening on our production server not when testing locally.

Hope the problem is clearer now,

Thanks!

Joe

Hi Charlie,

I don’t think it is to do with the code either, we have about 200 people on at the same time sometimes and only have this problem reported about once every couple of days. I think there may be something underlying in the way that the swf is loaded that effects some browsers and it has only become apparent since adding this code as it is skipping to frame 2 and the only point in the code that there is a goToAndStop(2) is here

function tryConnect()

{

clearConnectTimer();

switch(ncTryIndex)

{

case 0:

ncConnectTimeout= 8000

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

case 1:

ncConnectTimeout= 15000

ncPort = ncPort2;

ncProtocol = ncProtocol2;

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

default:

{trace(“Failed to connect after “+ncTryIndex+” tries”);

url = “”;

statusBox.text=“failed to connect”;

nc.close();

nc=null;

gotoAndStop(2);

}

break;

}

if (url.length > 0)

{

nc.connect(url);

ncConnectTimer = setInterval(_root, “tryReconnect”, ncConnectTimeout);

}

}

It is definitely starting to play before it moves on to frame 2, as this happened the only time that I managed to recreate it.

I am loading the simple player swf in using the loader component and do it in a few different places in my main swf, but they all use the same simple function and add the video file name as a parameter to url e.g simplePlayer.swf?fileName=

function loadExternalMovie(url:String) {

videoBgLoader._visible = true;

_root.externalMovieLoaded = false;

movieHolder.unloadMovie();

movieHolder._lockroot = true;

var listener:Object = new Object();

listener.onLoadInit = movieLoaded;

movieHolderLoader.addListener(listener);

movieHolderLoader.loadClip(url,movieHolder);

}

Is there any way you can think of that in certain browsers the loader component is caching any information from the last time it loaded that player swf , which would confuse it into thinking it was at a different stage in switch statement or confusing the net connection status’?

I was just wondering if there was anything off the top of your heads you could think of, as I’m finding it very hard to diagnose.

Thanks again,

Joe

I’m using an AS3 player with a version of the rtmp to rtmpt rollover in this thread. It works fine but I’m hearing from a handful of corporate viewers who’s Proxy servers are still blocking the connection. Do the folks at wowza have any documentation or port testing apps to help in these situations?

Thanks!

Im confused where i place this code?

We use flowplayer.

var nc:NetConnection = new NetConnection()
var ncConnectTimer:Number = 0;
var ncConnectTimeout:Number = 3000; // 3 seconds
var ncDomain:String = "localhost";
var ncPort:Number = 443;
var ncApplication:String = "fastplay";
var ncTryIndex:Number = 0;
function init()
{
	nc.onStatus = function(infoObj:Object)
	{
		trace("nc.onStatus: "+infoObj.code);
		if (infoObj.code == "NetConnection.Connect.Success")
			clearConnectTimer();
		else if (infoObj.code == "NetConnection.Connect.Failed")
			tryReconnect();
	}
}
function clearConnectTimer()
{
	if (ncConnectTimer != 0)
		clearInterval(ncConnectTimer);
	ncConnectTimer = 0;
}
function tryConnect()
{
	clearConnectTimer();
	
	var url:String = "";
	switch(ncTryIndex)
	{
		case 0:
			url = "rtmp://"+ncDomain+":"+ncPort+"/"+ncApplication;
			break;
		case 1:
			url = "rtmpt://"+ncDomain+":"+ncPort+"/"+ncApplication;
			break;
		default:
			trace("Failed to connect after "+ncTryIndex+" tries");
			break;
	}
	
	if (url.length > 0)
	{
		nc.connect(url);
		ncConnectTimer = setInterval(_root, "tryReconnect", ncConnectTimeout);
	}
}
function tryReconnect()
{
	trace("tryReconnect");
	clearConnectTimer();
	ncTryIndex++;
	tryConnect();
}
init();
tryConnect();