1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 03:41:55 +00:00

for #5114, display metadata.

This commit is contained in:
winlin 2016-03-07 16:02:37 +08:00
parent a8ee3162d4
commit 82aa9d120c
6 changed files with 258 additions and 121 deletions

View file

@ -5,7 +5,7 @@
* depends: jquery1.10
* https://code.csdn.net/snippets/147103
* @see: http://blog.csdn.net/win_lin/article/details/17994347
* v 1.0.11
* v 1.0.14
*/
/**
@ -96,19 +96,76 @@ function system_array_get(arr, elem_or_function) {
/**
* to iterate on array.
* @param arr the array to iterate on.
* @param pfn the function to apply on it
* @param pfn the function to apply on it. return false to break loop.
* for example,
* arr = [10, 15, 20, 30, 20, 40]
* system_array_foreach(arr, function(elem, index){
* console.log('index=' + index + ',elem=' + elem);
* });
* @return true when iterate all elems.
*/
function system_array_foreach(arr, pfn) {
if (!pfn) {
return false;
}
for (var i = 0; i < arr.length; i++) {
if (pfn) {
pfn(arr[i], i)
if (!pfn(arr[i], i)) {
return false;
}
}
return true;
}
/**
* whether the str starts with flag.
*/
function system_string_startswith(str, flag) {
if (typeof flag == "object" && flag.constructor == Array) {
for (var i = 0; i < flag.length; i++) {
if (system_string_startswith(str, flag[i])) {
return true;
}
}
}
return str && flag && str.length >= flag.length && str.indexOf(flag) == 0;
}
/**
* whether the str ends with flag.
*/
function system_string_endswith(str, flag) {
if (typeof flag == "object" && flag.constructor == Array) {
for (var i = 0; i < flag.length; i++) {
if (system_string_endswith(str, flag[i])) {
return true;
}
}
}
return str && flag && str.length >= flag.length && str.indexOf(flag) == str.length - flag.length;
}
/**
* trim the start and end of flag in str.
* @param flag a string to trim.
*/
function system_string_trim(str, flag) {
if (!flag || !flag.length || typeof flag != "string") {
return str;
}
while (system_string_startswith(str, flag)) {
str = str.substr(flag.length);
}
while (system_string_endswith(str, flag)) {
str = str.substr(0, str.length - flag.length);
}
return str;
}
/**
@ -186,12 +243,19 @@ function parse_query_string(){
}
}
// split again for angularjs.
if (query_string.indexOf("?") > 0) {
query_string = query_string.split("?")[1];
}
var queries = query_string.split("&");
$(queries).each(function(){
var query = this.split("=");
for (var i = 0; i < queries.length; i++) {
var elem = queries[i];
var query = elem.split("=");
obj[query[0]] = query[1];
obj.user_query[query[0]] = query[1];
});
}
return obj;
}
@ -244,6 +308,8 @@ function parse_rtmp_url(rtmp_url) {
}
var ret = {
url: rtmp_url,
schema: a.protocol.replace(":", ""),
server: a.hostname, port: port,
vhost: vhost, app: app, stream: stream
};
@ -500,7 +566,14 @@ AsyncRefresh2.prototype.initialize = function(pfn, timeout) {
* stop refresh, the refresh pfn is set to null.
*/
AsyncRefresh2.prototype.stop = function() {
this.refresh_change(null, null);
this.__call.__enabled = false;
}
/**
* restart refresh, use previous config.
*/
AsyncRefresh2.prototype.restart = function() {
this.__call.__enabled = true;
this.request(0);
}
/**
* change refresh pfn, the old pfn will set to disabled.
@ -568,4 +641,4 @@ AsyncRefresh2.prototype.request = function(timeout) {
* depends: jquery1.10, boostrap2
* https://code.csdn.net/snippets/146160
* @see: http://blog.csdn.net/win_lin/article/details/17628631
*/
*/

View file

@ -24,6 +24,20 @@
<script type="text/javascript" src="js/jwplayer.js" ></script>
<script type='text/javascript'>jwplayer.key = 'N8zhkmYvvRwOhz4aTGkySoEri4x+9pQwR7GHIQ=='; </script>
<script type="text/javascript">
/****
* The parameters for this page:
* schema, the protocol schema, rtmp or http.
* server, the ip of the url.
* port, the rtmp port of url.
* vhost, the vhost of url, can equals to server.
* app, the app of url.
* stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
* autostart, whether auto play the stream.
* Additional params:
* hls_vhost, the vhost for hls.
* hls_port, the port for hls play.
* hls_autostart, whether auto play the hls stream.
*/
var _player = null;
var _url = null;
$(function(){

View file

@ -384,6 +384,9 @@
<input class="span2" style="width:135px" id="player_clock" type="text" placeholder="年-月-日 时:分:秒">
</div>
</div>
<div>
<span id="debug_info"></span>
</div>
</div>
</div>
<footer>
@ -466,6 +469,19 @@
__active_mbt.addClass("active");
}
/****
* The parameters for this page:
* schema, the protocol schema, rtmp or http.
* server, the ip of the url.
* port, the rtmp port of url.
* vhost, the vhost of url, can equals to server.
* app, the app of url.
* stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
* autostart, whether auto play the stream.
* for example:
* http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream&server=ossrs.net&port=1935&autostart=true&schema=rtmp
* http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream.flv&server=ossrs.net&port=8080&autostart=true&schema=http
*/
$(function(){
var query = parse_query_string();
@ -496,6 +512,9 @@
};
srs_player.on_player_metadata = function(metadata) {
$("#btn_dar_original").text("视频原始比例" + "(" + metadata.width + ":" + metadata.height + ")");
if (metadata.ip && metadata.pid && metadata.cid) {
$("#debug_info").text("DEBUG: " + metadata.ip + ' grep -in "\\[' + metadata.pid + '\\]\\[' + metadata.cid + '\\]"');
}
select_dar("#btn_dar_original", 0, 0);
select_fs_size("#btn_fs_size_screen_100", "screen", 100);
};
@ -551,6 +570,9 @@
+ "vhost=" + rtmp.vhost + "&app=" + rtmp.app + "&stream=" + rtmp.stream
+ "&server=" + rtmp.server + "&port=" + rtmp.port
+ "&autostart=true";
if (rtmp.schema == "http") {
url += "&schema=http";
}
$("#player_url").text($("#txt_url").val()).attr("href", url);
$("#link_server").text(rtmp.server);

View file

@ -39,3 +39,4 @@
<flashCatalyst validateFlashCatalystCompatibility="false"/>
</actionScriptProperties>

View file

@ -32,8 +32,8 @@ package
private var js_on_player_metadata:String = null;
private var js_on_player_timer:String = null;
private var js_on_player_empty:String = null;
private var js_on_player_full:String = null;
private var js_on_player_full:String = null;
// play param url.
private var user_url:String = null;
// play param, user set width and height
@ -98,8 +98,8 @@ package
this.js_on_player_ready = flashvars.on_player_ready;
this.js_on_player_metadata = flashvars.on_player_metadata;
this.js_on_player_timer = flashvars.on_player_timer;
this.js_on_player_empty = flashvars.on_player_empty;
this.js_on_player_full = flashvars.on_player_full;
this.js_on_player_empty = flashvars.on_player_empty;
this.js_on_player_full = flashvars.on_player_full;
this.media_timer.addEventListener(TimerEvent.TIMER, this.system_on_timer);
this.media_timer.start();
@ -121,11 +121,11 @@ package
flash.external.ExternalInterface.addCallback("__play", this.js_call_play);
flash.external.ExternalInterface.addCallback("__stop", this.js_call_stop);
flash.external.ExternalInterface.addCallback("__pause", this.js_call_pause);
flash.external.ExternalInterface.addCallback("__resume", this.js_call_resume);
flash.external.ExternalInterface.addCallback("__resume", this.js_call_resume);
flash.external.ExternalInterface.addCallback("__set_dar", this.js_call_set_dar);
flash.external.ExternalInterface.addCallback("__set_fs", this.js_call_set_fs_size);
flash.external.ExternalInterface.addCallback("__set_bt", this.js_call_set_bt);
flash.external.ExternalInterface.addCallback("__set_mbt", this.js_call_set_mbt);
flash.external.ExternalInterface.addCallback("__set_mbt", this.js_call_set_mbt);
flash.external.ExternalInterface.call(this.js_on_player_ready, this.js_id);
}
@ -134,39 +134,39 @@ package
* system callack event, timer to do some regular tasks.
*/
private function system_on_timer(evt:TimerEvent):void {
var ms:NetStream = this.media_stream;
var ms:NetStream = this.media_stream;
if (!ms) {
//log("stream is null, ignore timer event.");
return;
}
var rtime:Number = flash.utils.getTimer();
var bitrate:Number = Number((ms.info.videoBytesPerSecond + ms.info.audioBytesPerSecond) * 8 / 1000);
var rtime:Number = flash.utils.getTimer();
var bitrate:Number = Number((ms.info.videoBytesPerSecond + ms.info.audioBytesPerSecond) * 8 / 1000);
log("on timer, time=" + ms.time.toFixed(2) + "s, buffer=" + ms.bufferLength.toFixed(2) + "s"
+ ", bitrate=" + bitrate.toFixed(1) + "kbps"
+ ", fps=" + ms.currentFPS.toFixed(1)
+ ", rtime=" + rtime.toFixed(0)
);
+ ", bitrate=" + bitrate.toFixed(1) + "kbps"
+ ", fps=" + ms.currentFPS.toFixed(1)
+ ", rtime=" + rtime.toFixed(0)
);
flash.external.ExternalInterface.call(
this.js_on_player_timer, this.js_id, ms.time, ms.bufferLength,
bitrate, ms.currentFPS, rtime
);
bitrate, ms.currentFPS, rtime
);
}
/**
* system callback event, when stream is empty.
*/
private function system_on_buffer_empty():void {
var time:Number = flash.utils.getTimer();
log("stream is empty at " + time + "ms");
flash.external.ExternalInterface.call(this.js_on_player_empty, this.js_id, time);
}
private function system_on_buffer_full():void {
var time:Number = flash.utils.getTimer();
log("stream is full at " + time + "ms");
flash.external.ExternalInterface.call(this.js_on_player_full, this.js_id, time);
}
/**
* system callback event, when stream is empty.
*/
private function system_on_buffer_empty():void {
var time:Number = flash.utils.getTimer();
log("stream is empty at " + time + "ms");
flash.external.ExternalInterface.call(this.js_on_player_empty, this.js_id, time);
}
private function system_on_buffer_full():void {
var time:Number = flash.utils.getTimer();
log("stream is full at " + time + "ms");
flash.external.ExternalInterface.call(this.js_on_player_full, this.js_id, time);
}
/**
* system callack event, when got metadata from stream.
@ -175,6 +175,11 @@ package
private function system_on_metadata(metadata:Object):void {
this.media_metadata = metadata;
// update the debug info.
if (metadata) {
on_debug_info(metadata);
}
// for js.
var obj:Object = __get_video_size_object();
@ -190,6 +195,21 @@ package
if (srs_authors != null) {
obj.contributor = srs_authors;
}
if (srs_id != null) {
obj.cid = srs_id;
}
if (srs_pid != null) {
obj.pid = srs_pid;
}
if (srs_server_ip != null) {
obj.ip = srs_server_ip;
}
var s:String = "";
for (var key:String in obj) {
s += key + "=" + obj[key] + " ";
}
log("metadata is " + s);
var code:int = flash.external.ExternalInterface.call(js_on_player_metadata, js_id, obj);
if (code != 0) {
@ -231,7 +251,7 @@ package
private function js_call_pause():void {
if (this.media_stream) {
this.media_stream.pause();
log("user pause play");
log("user pause play");
}
}
@ -241,7 +261,7 @@ package
private function js_call_resume():void {
if (this.media_stream) {
this.media_stream.resume();
log("user resume play");
log("user resume play");
}
}
@ -259,7 +279,7 @@ package
user_dar_den = den;
flash.utils.setTimeout(__execute_user_set_dar, 0);
log("user set dar to " + num + "/" + den);
log("user set dar to " + num + "/" + den);
}
/**
@ -273,7 +293,7 @@ package
private function js_call_set_fs_size(refer:String, percent:int):void {
user_fs_refer = refer;
user_fs_percent = percent;
log("user set refer to " + refer + ", percent to" + percent);
log("user set refer to " + refer + ", percent to" + percent);
}
/**
@ -283,21 +303,21 @@ package
private function js_call_set_bt(buffer_time:Number):void {
if (this.media_stream) {
this.media_stream.bufferTime = buffer_time;
log("user set bufferTime to " + buffer_time.toFixed(2) + "s");
log("user set bufferTime to " + buffer_time.toFixed(2) + "s");
}
}
/**
* set the max stream buffer time in seconds.
* @max_buffer_time the max buffer time in seconds.
* @remark this is the key feature for realtime communication by flash.
*/
private function js_call_set_mbt(max_buffer_time:Number):void {
if (this.media_stream) {
this.media_stream.bufferTimeMax = max_buffer_time;
log("user set bufferTimeMax to " + max_buffer_time.toFixed(2) + "s");
}
}
/**
* set the max stream buffer time in seconds.
* @max_buffer_time the max buffer time in seconds.
* @remark this is the key feature for realtime communication by flash.
*/
private function js_call_set_mbt(max_buffer_time:Number):void {
if (this.media_stream) {
this.media_stream.bufferTimeMax = max_buffer_time;
log("user set bufferTimeMax to " + max_buffer_time.toFixed(2) + "s");
}
}
/**
* function for js to call: to stop the stream. ignore if not play.
@ -315,7 +335,7 @@ package
this.media_conn.close();
this.media_conn = null;
}
log("player stopped");
log("player stopped");
}
// srs infos
@ -349,13 +369,37 @@ package
contextMenu.customItems = customItems;
}
/**
* server can set the debug info in _result of RTMP connect, or metadata.
*/
private function on_debug_info(data:*):void {
if (data.hasOwnProperty("srs_server")) {
srs_server = data.srs_server;
}
if (data.hasOwnProperty("srs_primary")) {
srs_primary = data.srs_primary;
}
if (data.hasOwnProperty("srs_authors")) {
srs_authors = data.srs_authors;
}
if (data.hasOwnProperty("srs_id")) {
srs_id = data.srs_id;
}
if (data.hasOwnProperty("srs_pid")) {
srs_pid = data.srs_pid;
}
if (data.hasOwnProperty("srs_server_ip")) {
srs_server_ip = data.srs_server_ip;
}
}
/**
* function for js to call: to play the stream. stop then play.
* @param url, the rtmp/http url to play.
* @param _width, the player width.
* @param _height, the player height.
* @param buffer_time, the buffer time in seconds. recommend to >=0.5
* @param max_buffer_time, the max buffer time in seconds. recommend to 3 x buffer_time.
* @param max_buffer_time, the max buffer time in seconds. recommend to 3 x buffer_time.
* @param volume, the volume, 0 is mute, 1 is 100%, 2 is 200%.
*/
private function js_call_play(url:String, _width:int, _height:int, buffer_time:Number, max_buffer_time:Number, volume:Number):void {
@ -363,8 +407,8 @@ package
this.user_w = _width;
this.user_h = _height;
log("start to play url: " + this.user_url + ", w=" + this.user_w + ", h=" + this.user_h
+ ", buffer=" + buffer_time.toFixed(2) + "s, max_buffer=" + max_buffer_time.toFixed(2) + "s, volume=" + volume.toFixed(2)
);
+ ", buffer=" + buffer_time.toFixed(2) + "s, max_buffer=" + max_buffer_time.toFixed(2) + "s, volume=" + volume.toFixed(2)
);
js_call_stop();
@ -375,46 +419,29 @@ package
log("NetConnection: code=" + evt.info.code);
if (evt.info.hasOwnProperty("data") && evt.info.data) {
if (evt.info.data.hasOwnProperty("srs_server")) {
srs_server = evt.info.data.srs_server;
}
if (evt.info.data.hasOwnProperty("srs_primary")) {
srs_primary = evt.info.data.srs_primary;
}
if (evt.info.data.hasOwnProperty("srs_authors")) {
srs_authors = evt.info.data.srs_authors;
}
if (evt.info.data.hasOwnProperty("srs_id")) {
srs_id = evt.info.data.srs_id;
}
if (evt.info.data.hasOwnProperty("srs_pid")) {
srs_pid = evt.info.data.srs_pid;
}
if (evt.info.data.hasOwnProperty("srs_server_ip")) {
srs_server_ip = evt.info.data.srs_server_ip;
}
on_debug_info(evt.info.data);
update_context_items();
}
// reject by server, maybe redirect.
if (evt.info.code == "NetConnection.Connect.Rejected") {
// RTMP 302 redirect.
if (evt.info.hasOwnProperty("ex") && evt.info.ex.code == 302) {
var streamName:String = url.substr(url.lastIndexOf("/") + 1);
url = evt.info.ex.redirect + "/" + streamName;
log("Async RTMP 302 Redirect to: " + url);
// notify server.
media_conn.call("Redirected", null, evt.info.ex.redirect);
// do 302.
setTimeout(function(){
log("Async RTMP 302 Redirected.");
js_call_play(url, _width, _height, buffer_time, max_buffer_time, volume);
}, 1000);
return;
}
}
// reject by server, maybe redirect.
if (evt.info.code == "NetConnection.Connect.Rejected") {
// RTMP 302 redirect.
if (evt.info.hasOwnProperty("ex") && evt.info.ex.code == 302) {
var streamName:String = url.substr(url.lastIndexOf("/") + 1);
url = evt.info.ex.redirect + "/" + streamName;
log("Async RTMP 302 Redirect to: " + url);
// notify server.
media_conn.call("Redirected", null, evt.info.ex.redirect);
// do 302.
setTimeout(function(){
log("Async RTMP 302 Redirected.");
js_call_play(url, _width, _height, buffer_time, max_buffer_time, volume);
}, 1000);
return;
}
}
// TODO: FIXME: failed event.
if (evt.info.code != "NetConnection.Connect.Success") {
@ -424,7 +451,7 @@ package
media_stream = new NetStream(media_conn);
media_stream.soundTransform = new SoundTransform(volume);
media_stream.bufferTime = buffer_time;
media_stream.bufferTimeMax = max_buffer_time;
media_stream.bufferTimeMax = max_buffer_time;
media_stream.client = {};
media_stream.client.onMetaData = system_on_metadata;
media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
@ -433,10 +460,10 @@ package
if (evt.info.code == "NetStream.Video.DimensionChange") {
system_on_metadata(media_metadata);
} else if (evt.info.code == "NetStream.Buffer.Empty") {
system_on_buffer_empty();
} else if (evt.info.code == "NetStream.Buffer.Full") {
system_on_buffer_full();
}
system_on_buffer_empty();
} else if (evt.info.code == "NetStream.Buffer.Full") {
system_on_buffer_full();
}
// TODO: FIXME: failed event.
});
@ -539,10 +566,10 @@ package
// rescale to fs
__update_video_size(num, den,
obj.width * user_fs_percent / 100,
obj.height * user_fs_percent / 100,
this.stage.fullScreenWidth, this.stage.fullScreenHeight
);
obj.width * user_fs_percent / 100,
obj.height * user_fs_percent / 100,
this.stage.fullScreenWidth, this.stage.fullScreenHeight
);
}
/**
@ -621,18 +648,18 @@ package
this.control_fs_mask.graphics.drawRect(0, 0, _width, _height);
this.control_fs_mask.graphics.endFill();
}
private function log(msg:String):void {
msg = "[" + new Date() +"][srs-player][" + js_id + "] " + msg;
trace(msg);
if (!flash.external.ExternalInterface.available) {
flash.utils.setTimeout(log, 300, msg);
return;
}
ExternalInterface.call("console.log", msg);
}
private function log(msg:String):void {
msg = "[" + new Date() +"][srs-player][" + js_id + "] " + msg;
trace(msg);
if (!flash.external.ExternalInterface.available) {
flash.utils.setTimeout(log, 300, msg);
return;
}
ExternalInterface.call("console.log", msg);
}
}
}