Wowza Community

Dynamic Loadbalancer - PHP script for iOS load balancing

My first post here - so hi, I’m Philipp and I come from (-> username) Munich in Germany.

I setup Wowza on EC2 with the Dynamic Loadbalancing Module. This works just fine with any compatible flash player like JWPLayer. But I was not able to find a way on how to get loadbalancing to work with iOS clients.

So I went and wrote some php (my first .php script actually, so please enhance it as needed and let me know what you did):

<?php
// CONFIG START - Input in your Wowza Dynamic Loadbalancers address, your application's name and the streamname
 $lburl = "http://url-or-ip-of-lb-instance:1935/loadbalancer";
 $appname = "liveedge";
 $streamname = "myStream";
// Config END
$content = file_get_contents ($lburl);
$replacewith = "";
$ip = str_replace('redirect=', $replacewith, $content);
header("Cache-Control: no-cache, must-revalidate");
header("Location:http://$ip/$appname/$streamname/playlist.m3u8");
?>

I guess it is pretty self-explanatory, it connects to the loadbalancer URL and gets the output (least loaded server). Then it removes the string “redirect=” and puts together a 302 redirect.

So if you open the .php file from your ios device it will work just fine.

To be on the safe side I would recommend to call the file playlist.m3u8 and add an Apache Directive to the server hosting the .php:

AddType application/x-httpd-php .m3u8

Of course you should only do this if you dont plan to deliver real .m3u8 files using the same apache VHost - I recommend using a dedicated VHost for this.

So when you configure your player to use:

http://apachehost-ios-lb.example.com/playlist.m3u8

it actually calls the .php script and gets redirected to:

http://least-used-server/appname/streamname/playlist.m3u8

Tested with iOS and Quicktime for Mac

I hope this is useful for some of you!

JWPlayer already is capable of handling RTMP redirects from Wowza’s load balancer, but this method should work reasonably well with http clients. To my knowledge, Roku doesn’t handle http redirects very well, however.

You may also want to look at http_request2 library for PHP to pull XML data from the Wowza servers - the http request mechanism is about 20-30x faster than the normal PHP http mechanism.

There are a number of interesting PHP bits for Wowza on my blog at http://streamnerd.com

-Ian

you may want to

Just testing JW Player 6 here. Form this in your PHP script and replace the [wowza-address] with the least loaded edge:

<html>
<head>
<title>JW Player 6</title>
<script type="text/javascript" src="/jwplayer/jwplayer.js" ></script>
<script>jwplayer.key="[your-key-is-required-for-ios]</script>
</head>
<body>
<div id="myElement">Loading the player...</div>
<script type="text/javascript">
    jwplayer("myElement").setup({
	sources: [{ 
            file: "rtmp://[wowza-address]:1935/vod/mp4:sample.mp4"
        },{
            file: "http://[wowza-address]:1935/vod/mp4:sample.mp4/playlist.m3u8"
        }]
        
    });
</script>
</body>
</html>

Richard

Hi There -

Munich’s PHP script works great to get a redirect on an IOS device and the video play right away with no issues.

Now that JW Player has come out with their JW Player 6 which supports HLS streaming, I’m trying to figure out how to create a simple embed for HLS only which will play in flash browsers or on IOS. I can get it to work great if I hard code the http url to an edge server like follows. If I create a PHP script like munichs above and reference it via a URL, I’m getting an error “Manifest is not a valid M3U8 File”. Does anyone have a suggestion on how one might modify or build a php file to create or build a vaild M3U8 URL after the PHP script gets the least loaded edge server via the http redirect. The PHP File is below:

Thank you in advance for any pointers or suggestions.

Tim

Embed CODE:

[B]
<head>
<script type='text/javascript' src='/jwplayer6/jwplayer.js'></script>
<script type="text/javascript">jwplayer.key="key.... ";</script>

</head>

<body>

<div id="livestream"></div>
<script type="text/javascript">
jwplayer("livestream").setup({
        file: "http://edgeserver.domain.com/livestreams/smil:streamname.smil/playlist.m3u8",
        type: "hls",
        height: 576,
        width: 1024,
        autostart: true,
    
});
</script>
</body>[/B]

PHP File:

[B]<?php
// CONFIG START - Input in your Wowza Dynamic Loadbalancers address, your application's name and the streamname
 $lburl = "http://load.domain.com:1935/loadbalancer";
 $appname = "livestreams";
 $streamname = "[/B][B]smil:streamname.smil[/B][B]";
// Config END

$content = file_get_contents ($lburl);
$replacewith = "";
$ip = str_replace('redirect=', $replacewith, $content);
header("Cache-Control: no-cache, must-revalidate");
header("Location:http://$ip/$appname/$streamname/playlist.m3u8");
?>[/B]

I apologise for posting in this thread since it’s dormant for a bit.

I’ve managed to implement a version of this php for my usage, simply using the script to fill in [wowza-address] on my player.

Ian - you mentioned a faster XML version of calling the least loaded server. Could you elaborate on how this might be achieved?

Thanks,

Alex

I apologise for posting in this thread since it’s dormant for a bit.

I’ve managed to implement a version of this php for my usage, simply using the script to fill in [wowza-address] on my player.

Ian - you mentioned a faster XML version of calling the least loaded server. Could you elaborate on how this might be achieved?

Thanks,

Alex

I’m trying to get this to work but I don’t know how to pass the redirect server address from the PHP script to the JW player [wowza-address]. I see the two code snippets above but how do they interact with each other?

Tom