Results 1 to 6 of 6

Thread: Dynamic Loadbalancer - PHP script for iOS load balancing

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Default Dynamic Loadbalancer - PHP script for iOS load balancing

    Repost of: http://www.wowza.com/forums/showthread.php?17318
    I was in the wrong forum and I am unable to delete my post there.. so here I go again (copy/paste):
    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):

    Code:
    <?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:
    Code:
    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:
    Code:
    http://apachehost-ios-lb.example.com/playlist.m3u8
    it actually calls the .php script and gets redirected to:
    Code:
    http://least-used-server/appname/streamname/playlist.m3u8
    Tested with iOS and Quicktime for Mac

    I hope this is useful for some of you!

  2. #2

    Default

    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

  3. #3

    Default

    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:

    Code:
    
    <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>
    PHP File:
    Code:
    <?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 = "smil:streamname.smil";
    // 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");
    ?>

  4. #4
    Join Date
    Dec 2007
    Posts
    28,412

    Default

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

    Code:
    <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

  5. #5

    Default

    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
    Last edited by alexmay; 02-19-2013 at 02:07 PM.

  6. #6
    Join Date
    Oct 2013
    Posts
    11

    Default

    Quote Originally Posted by alexmay View Post
    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
    This is the Walking Dead of threads, twice resurrected.

    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

Similar Threads

  1. Dynamic Load Balancing AddOn: loadbalancer?serverInfoXML empty
    By niemion in forum AddOn: Other AddOns
    Replies: 10
    Last Post: 02-13-2014, 02:35 PM
  2. Dynamic Load Balancing for VOD
    By nick_g in forum AddOn: Other AddOns
    Replies: 1
    Last Post: 01-04-2012, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •