1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 20:01:56 +00:00
srs/trunk/research/players/js/srs.js

279 lines
8.2 KiB
JavaScript
Raw Normal View History

2013-12-21 02:06:51 +00:00
/**
* 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);
}
2013-12-20 17:04:29 +00:00
/**
* update the navigator, add same query string.
*/
2013-12-18 05:44:18 +00:00
function update_nav() {
$("#nav_srs_player").attr("href", "srs_player.html" + window.location.search);
$("#nav_srs_publisher").attr("href", "srs_publisher.html" + window.location.search);
$("#nav_srs_bwt").attr("href", "srs_bwt.html" + window.location.search);
$("#nav_jwplayer6").attr("href", "jwplayer6.html" + window.location.search);
$("#nav_osmf").attr("href", "osmf.html" + window.location.search);
$("#nav_vlc").attr("href", "vlc.html" + window.location.search);
}
2013-12-20 17:04:29 +00:00
/**
* parse the query string to object.
*/
2013-12-18 05:44:18 +00:00
function parse_query_string(){
var query_string = String(window.location.search).replace(" ", "").split("?")[1];
if(query_string == undefined){
return {};
}
var queries = query_string.split("&");
var obj = {};
$(queries).each(function(){
var query = this.split("=");
obj[query[0]] = query[1];
});
return obj;
}
2013-12-18 06:29:55 +00:00
/**
@param vhost the vhost of rtmp. default to window.location.hostname
@param port the port of rtmp. default to 1935
@param app the app of rtmp. default to live.
@param stream the stream of rtmp. default to livestream.
*/
function build_default_rtmp_url() {
2013-12-18 05:44:18 +00:00
var query = parse_query_string();
2013-12-18 06:29:55 +00:00
2013-12-18 05:44:18 +00:00
var port = (query.port == undefined)? 1935:query.port;
var vhost = (query.vhost == undefined)? window.location.hostname:query.vhost;
2013-12-18 05:48:59 +00:00
var app = (query.app == undefined)? "live":query.app;
var stream = (query.stream == undefined)? "livestream":query.stream;
2013-12-18 06:29:55 +00:00
return "rtmp://" + vhost + ":" + port + "/" + app + "/" + stream;
}
/**
@param vhost the vhost of hls. default to window.location.hostname
@param hls_port the port of hls. default to window.location.port
@param app the app of hls. default to live.
@param stream the stream of hls. default to livestream.
*/
function build_default_hls_url() {
var query = parse_query_string();
var vhost = (query.vhost == undefined)? window.location.hostname:query.vhost;
var port = (query.hls_port == undefined)? window.location.port:query.hls_port;
var app = (query.app == undefined)? "live":query.app;
var stream = (query.stream == undefined)? "livestream":query.stream;
if (port == "" || port == null || port == undefined) {
port = 80;
}
return "http://" + vhost + ":" + port + "/" + app + "/" + stream + ".m3u8";
2013-12-18 05:44:18 +00:00
}
2013-12-20 17:04:29 +00:00
/**
* initialize the page.
* @param rtmp_url the rtmp stream url to play
* @param hls_url the hls stream url to play
*/
2013-12-18 06:29:55 +00:00
function srs_init(rtmp_url, hls_url) {
2013-12-18 05:44:18 +00:00
update_nav();
2013-12-18 06:29:55 +00:00
if (rtmp_url) {
$(rtmp_url).val(build_default_rtmp_url());
}
if (hls_url) {
$(hls_url).val(build_default_hls_url());
2013-12-18 05:44:18 +00:00
}
}
2013-12-20 17:04:29 +00:00
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
* the SrsPlayer object.
*/
2013-12-21 02:06:51 +00:00
function SrsPlayer(container, stream_url, width, height) {
2013-12-20 17:04:29 +00:00
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.id = SrsPlayer.__id++;
this.callbackObj = null;
2013-12-21 02:06:51 +00:00
this.buffer_time = 0.8; // default to 0.8
2013-12-20 17:04:29 +00:00
// callback set the following values.
this.meatadata = {}; // for on_player_metadata
2013-12-21 02:06:51 +00:00
this.time = 0; // current stream time.
this.buffer_length = 0; // current stream buffer length.
2013-12-20 17:04:29 +00:00
}
/**
* 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";
2013-12-21 02:06:51 +00:00
flashvars.on_player_timer = "__srs_on_player_timer";
2013-12-20 17:04:29 +00:00
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() {
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__play(this.stream_url, this.width, this.height, this.buffer_time);
2013-12-20 17:04:29 +00:00
}
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;
}
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__stop();
2013-12-20 17:04:29 +00:00
}
SrsPlayer.prototype.pause = function() {
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__pause();
2013-12-20 17:04:29 +00:00
}
SrsPlayer.prototype.resume = function() {
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__resume();
2013-12-20 17:04:29 +00:00
}
/**
* 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) {
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__dar(num, den);
2013-12-20 17:04:29 +00:00
}
/**
* set the fullscreen size data.
* @refer the refer fullscreen mode. it can be:
* video: use video orignal size.
* screen: use screen size to rescale video.
* @param percent, the rescale percent, where
* 100 means 100%.
*/
SrsPlayer.prototype.set_fs = function(refer, percent) {
2013-12-21 02:06:51 +00:00
this.callbackObj.ref.__set_fs(refer, percent);
}
/**
* set the stream buffer time in seconds.
* @buffer_time the buffer time in seconds.
*/
SrsPlayer.prototype.set_bt = function(buffer_time) {
this.buffer_time = buffer_time;
this.callbackObj.ref.__set_bt(buffer_time);
2013-12-20 17:04:29 +00:00
}
SrsPlayer.prototype.on_player_ready = function() {
2013-12-21 02:06:51 +00:00
this.play();
2013-12-20 17:04:29 +00:00
}
SrsPlayer.prototype.on_player_metadata = function(metadata) {
2013-12-21 02:06:51 +00:00
// ignore.
}
SrsPlayer.prototype.on_player_timer = function(time, buffer_length) {
// ignore.
2013-12-20 17:04:29 +00:00
}
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;
}
2013-12-21 02:06:51 +00:00
player.on_player_ready();
return;
2013-12-20 17:04:29 +00:00
}
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;
2013-12-21 02:06:51 +00:00
player.on_player_metadata(metadata);
return;
}
throw new Error("player not found. id=" + id);
}
function __srs_on_player_timer(id, time, buffer_length) {
for (var i = 0; i < SrsPlayer.__players.length; i++) {
var player = SrsPlayer.__players[i];
if (player.id != id) {
continue;
}
buffer_length = Math.max(0, buffer_length);
buffer_length = Math.min(player.buffer_time, buffer_length);
// user may override the on_player_timer,
// so set the data before invoke it.
player.time = time;
player.buffer_length = buffer_length;
player.on_player_timer(time, buffer_length);
return;
2013-12-20 17:04:29 +00:00
}
throw new Error("player not found. id=" + id);
}