srs_player.html 10.4 KB
<!DOCTYPE html>
<html>
<head>
    <title>SrsPlayers</title>   
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/swfobject.js"></script>
    <script type="text/javascript" src="js/srs.js"></script>
    <style>
        body{
            padding-top: 55px;
        }
    </style>
    <script type="text/javascript">
        $(function(){
            // get the vhost and port to set the default url.
            // for example: http://192.168.1.213/players/jwplayer6.html?port=1935&vhost=demo
            // url set to: rtmp://demo:1935/live/livestream
            srs_init($("#txt_url"));
            
            var srs_player = null;
        
            $("#main_modal").on("show", function(){
                $("#div_container").remove();
                
                var obj = $("<div/>");
                $(obj).attr("id", "div_container");
                
                var player = $("<div/>");
                $(obj).append(player);
                $(obj).attr("id", "player_id");
                
                $("#player").append(obj);
                
                var url = $("#txt_url").val();
                
                srs_player = new SrsPlayer("player_id", url, 530, 300, 0.8);
                srs_player.on_player_ready = function() {
                    return srs_player.play();
                }
                srs_player.on_player_metadata = function(metadata) {
                    $("#btn_dar_original").text("视频原始比例" + "(" + metadata.width + ":" + metadata.height + ")");
                    return 0;
                }
                srs_player.start();
            });
            
            $("#main_modal").on("hide", function(){
                srs_player.stop();
            });
            
            $("#btn_play").click(function(){
                $("#main_modal").modal({show:true, keyboard:false});
            });
            
            $("#btn_pause").click(function(){
                if ($("#btn_pause").text() == "暂停") {
                    $("#btn_pause").text("继续");
                    srs_player.pause();
                } else {
                    $("#btn_pause").text("暂停");
                    srs_player.resume();
                }
            });
            
            $("#btn_dar_original").click(function(){
                srs_player.dar(0, 0);
            });
            $("#btn_dar_21_9").click(function(){
                srs_player.dar(9, 21);
            });
            $("#btn_dar_16_9").click(function(){
                srs_player.dar(9, 16);
            });
            $("#btn_dar_4_3").click(function(){
                srs_player.dar(3, 4);
            });
            $("#btn_dar_fill").click(function(){
                srs_player.dar(-1, -1);
            });
        });
        
        /*
        * the SrsPlayer object.
        */
        function SrsPlayer(container, stream_url, width, height, buffer_time) {
            if (!SrsPlayer.__id) {
                SrsPlayer.__id = 100;
            }
            if (!SrsPlayer.__players) {
                SrsPlayer.__players = [];
            }
            
            SrsPlayer.__players.push(this);
            
            this.container = container;
            this.stream_url = stream_url;
            this.width = width;
            this.height = height;
            this.buffer_time = buffer_time;
            this.id = SrsPlayer.__id++;
            this.callbackObj = null;
            
            // callback set the following values.
            this.meatadata = {}; // for on_player_metadata
        }
        /*
        * user can set some callback, then start the player.
        * callbacks:
        *      on_player_ready():int, when srs player ready, user can play.
        *      on_player_metadata(metadata:Object):int, when srs player get metadata.
        */
        SrsPlayer.prototype.start = function() {
            // embed the flash.
            var flashvars = {};
            flashvars.id = this.id;
            flashvars.on_player_ready = "__srs_on_player_ready";
            flashvars.on_player_metadata = "__srs_on_player_metadata";
            
            var params = {};
            params.wmode = "opaque";
            params.allowFullScreen = true;
            params.allowScriptAccess = "always";
            
            var attributes = {};
            
            var self = this;
            
            swfobject.embedSWF(
                "srs_player/release/srs_player.swf", this.container,
                this.width, this.height,
                "11.1", "js/AdobeFlashPlayerInstall.swf",
                flashvars, params, attributes,
                function(callbackObj){
                    self.callbackObj = callbackObj;
                }
            );
            
            return this;
        }
        SrsPlayer.prototype.play = function() {
            return this.callbackObj.ref.__play(this.stream_url, this.width, this.height, this.buffer_time);
        }
        SrsPlayer.prototype.stop = function() {
            for (var i = 0; i < SrsPlayer.__players.length; i++) {
                var player = SrsPlayer.__players[i];
                
                if (player.id != this.id) {
                    continue;
                }
                
                SrsPlayer.__players.splice(i, 1);
                break;
            }
            return this.callbackObj.ref.__stop();
        }
        SrsPlayer.prototype.pause = function() {
            return this.callbackObj.ref.__pause();
        }
        SrsPlayer.prototype.resume = function() {
            return this.callbackObj.ref.__resume();
        }
        /*
        * to set the DAR, for example, DAR=16:9
        * @param num, for example, 9. 
        *       use metadata height if 0.
        *       use user specified height if -1.
        * @param den, for example, 16. 
        *       use metadata width if 0.
        *       use user specified width if -1.
        */
        SrsPlayer.prototype.dar = function(num, den) {
            if (num == 0 && this.metadata) {
                num = this.metadata.height;
            } else if (num == -1) {
                num = this.height;
            }
            
            if (den == 0 && this.metadata) {
                den = this.metadata.width;
            } else if (den == -1) {
                den = this.width;
            }
            
            return this.callbackObj.ref.__dar(num, den);
        }
        SrsPlayer.prototype.on_player_ready = function() {
            return this.play();
        }
        SrsPlayer.prototype.on_player_metadata = function(metadata) {
            return 0;
        }
        function __srs_on_player_ready(id) {
            for (var i = 0; i < SrsPlayer.__players.length; i++) {
                var player = SrsPlayer.__players[i];
                
                if (player.id != id) {
                    continue;
                }
                
                return player.on_player_ready();
            }
            
            throw new Error("player not found. id=" + id);
        }
        function __srs_on_player_metadata(id, metadata) {
            for (var i = 0; i < SrsPlayer.__players.length; i++) {
                var player = SrsPlayer.__players[i];
                
                if (player.id != id) {
                    continue;
                }
                
                // user may override the on_player_metadata, 
                // so set the data before invoke it.
                player.metadata = metadata;
                
                return player.on_player_metadata(metadata);
            }
            
            throw new Error("player not found. id=" + id);
        }
    </script>
</head>
<body>
<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
        <a class="brand" href="#">SrsPlayers</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    <li class="active"><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
                    <li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>
                    <li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>
                    <li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>
                    <li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>
                    <li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div class="alert alert-info fade in">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <strong><span>Usage:</span></strong> <span>输入地址后点击播放按钮</span>
    </div>
    <div class="form-inline">
        URL:
        <input type="text" id="txt_url" class="input-xxlarge" value=""></input>
        <button class="btn" id="btn_play">播放视频</button>
    </div>
    <div id="main_modal" class="modal hide fade">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3>SrsPlayer</h3>
        </div>
        <div class="modal-body" id="player">
        </div>
        <div class="modal-footer">
            <div class="btn-group dropup">
                <button class="btn dropdown-toggle" data-toggle="dropdown">宽高比 <span class="caret"></span></button>
                <ul class="dropdown-menu">
                    <li><a id="btn_dar_original" href="#">视频原始比例</a></li>
                    <li><a id="btn_dar_21_9" href="#">宽屏影院(21:9)</a></li>
                    <li><a id="btn_dar_16_9" href="#">宽屏电影(16:9)</a></li>
                    <li><a id="btn_dar_4_3" href="#">窄屏(4:3)</a></li>
                    <li><a id="btn_dar_fill" href="#">填充</a></li>
                </ul>
            </div>
            <button id="btn_pause" class="btn">暂停</button>
            <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">关闭</button>
        </div>
    </div>
    <hr>
    <footer>
        <p><a href="https://github.com/winlinvip/simple-rtmp-server">SRS Team &copy; 2013</a></p>
    </footer>
</div>
</body>