How-to use php to query the Wowza Load Balancer

In this how-to, I will show you how to construct a php script to query the Wowza load balancer (2.0) to obtain the least loaded edge for playback. Wowza already has the ModuleLoadBalancerRedirector that will auto-redirect rtmp connections, but this will be useful for essentially any other protocol.

You use this to obtain the corresponding edge IP and then embed it in the player for playback. I’ve split this up into two sections for clarity.

PHP Script (Using CURL)

// configure variables
$application = "live";
$instance = "_definst_";
$stream = "myStream";
$port = "1935";

$playerUrl = "";
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_POST      ,0);
curl_setopt($ch, CURLOPT_HEADER      ,0);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
$contents = curl_exec($ch);

if($contents === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch); 

$parts = explode("=", $contents);
if(is_array($parts) && count($parts)==2){
   $edgeIp = $parts[1];
   $playerUrl = "http://".$edgeIp.":".$port."/".$application."/".$instance."/".$stream."/playlist.m3u8";
}

Then you would appropriately place your playback URL into JWPlayer (or otherwise) as follows:

<html>
<head>
<title>JW Player 6</title>
<body>

<div id="player">Loading the player...</div>
<script src="http://jwpsrv.com/library/mykey.js"></script>
<script type="text/javascript">
    jwplayer("player").setup({
   	sources: [{
            file: "<?php echo $playerUrl; ?>"
        },]
    });

    	jwplayer("player").onError ( function(event) {
			setTimeout(function()
			{
				jwplayer("player").play(true);
			},2000);
		}
	);

</script>
</body>
</html>

Comments are closed.