Blame view

trunk/research/players/js/srs.utility.js 5.1 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
* padding the output.
* padding(3, 5, '0') is 00003
* padding(3, 5, 'x') is xxxx3
* @see http://blog.csdn.net/win_lin/article/details/12065413
*/
function padding(number, length, prefix) {
    if(String(number).length >= length){
        return String(number);
    }
    return padding(prefix+number, length, prefix);
}

/**
* parse the query string to object.
winlin authored
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
* parse the url location object as: host(hostname:http_port), pathname(dir/filename)
* for example, url http://192.168.1.168:1980/ui/players.html?vhost=player.vhost.com&app=test&stream=livestream
* parsed to object:
{
    host        : "192.168.1.168:1980",
    hostname    : "192.168.1.168",
    http_port   : 1980,
    pathname    : "/ui/players.html",
    dir         : "/ui",
    filename    : "/players.html",
    
    vhost       : "player.vhost.com",
    app         : "test",
    stream      : "livestream"
}
winlin authored
31 32 33 34
*/
function parse_query_string(){
    var obj = {};
    
35
    // add the uri object.
winlin authored
36 37 38 39 40 41 42 43 44 45 46 47 48
    // parse the host(hostname:http_port), pathname(dir/filename)
    obj.host = window.location.host;
    obj.hostname = window.location.hostname;
    obj.http_port = (window.location.port == "")? 80:window.location.port;
    obj.pathname = window.location.pathname;
    if (obj.pathname.lastIndexOf("/") <= 0) {
        obj.dir = "/";
        obj.filename = "";
    } else {
        obj.dir = obj.pathname.substr(0, obj.pathname.lastIndexOf("/"));
        obj.filename = obj.pathname.substr(obj.pathname.lastIndexOf("/"));
    }
    
49 50 51
    // pure user query object.
    obj.user_query = {};
    
winlin authored
52 53 54 55 56 57 58 59 60 61
    // parse the query string.
    var query_string = String(window.location.search).replace(" ", "").split("?")[1];
    if(query_string == undefined){
        return obj;
    }
    
    var queries = query_string.split("&");
    $(queries).each(function(){
        var query = this.split("=");
        obj[query[0]] = query[1];
62
        obj.user_query[query[0]] = query[1];
winlin authored
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    });
    
    return obj;
}

/**
* parse the rtmp url,
* for example: rtmp://demo.srs.com:1935/live...vhost...players/livestream
* @return object {server, port, vhost, app, stream}
*/
function srs_parse_rtmp_url(rtmp_url) {
    // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
    var a = document.createElement("a");
    a.href = rtmp_url.replace("rtmp://", "http://");
    
    var vhost = a.hostname;
    var port = (a.port == "")? "1935":a.port;
    var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);
    var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);

    // parse the vhost in the params of app, that srs supports.
    app = app.replace("...vhost...", "?vhost=");
    if (app.indexOf("?") >= 0) {
        var params = app.substr(app.indexOf("?"));
        app = app.substr(0, app.indexOf("?"));
        
        if (params.indexOf("vhost=") > 0) {
            vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);
            if (vhost.indexOf("&") > 0) {
                vhost = vhost.substr(0, vhost.indexOf("&"));
            }
        }
    }
    
    var ret = {
        server: a.hostname, port: port, 
        vhost: vhost, app: app, stream: stream
    };
    
    return ret;
}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

/**
* get the agent.
* @return an object specifies some browser.
*   for example, get_browser_agents().MSIE
*/
function get_browser_agents() {
    var agent = navigator.userAgent;
    
    /**
    WindowsPC platform, Win7:
        chrome 31.0.1650.63:
            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 
            (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
        firefox 23.0.1:
            Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 
            Firefox/23.0
        safari 5.1.7(7534.57.2):
            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 
            (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
        opera 15.0.1147.153:
            Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 
            (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 
            OPR/15.0.1147.153
        360 6.2.1.272: 
            Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; 
            Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; 
            .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; 
            .NET4.0E)
        IE 10.0.9200.16750(update: 10.0.12):
            Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; 
            Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; 
            .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; 
            .NET4.0E)
    */
    
    return {
        // platform
        Android: agent.indexOf("Android") != -1,
        Windows: agent.indexOf("Windows") != -1,
        iPhone: agent.indexOf("iPhone") != -1,
        // Windows Browsers
        Chrome: agent.indexOf("Chrome") != -1,
        Firefox: agent.indexOf("Firefox") != -1,
        QQBrowser: agent.indexOf("QQBrowser") != -1,
        MSIE: agent.indexOf("MSIE") != -1, 
        // Android Browsers
        Opera: agent.indexOf("Presto") != -1,
        MQQBrowser: agent.indexOf("MQQBrowser") != -1
    };
}