winlin

for #5114, display metadata.

@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 * depends: jquery1.10 5 * depends: jquery1.10
6 * https://code.csdn.net/snippets/147103 6 * https://code.csdn.net/snippets/147103
7 * @see: http://blog.csdn.net/win_lin/article/details/17994347 7 * @see: http://blog.csdn.net/win_lin/article/details/17994347
8 - * v 1.0.11 8 + * v 1.0.14
9 */ 9 */
10 10
11 /** 11 /**
@@ -96,19 +96,76 @@ function system_array_get(arr, elem_or_function) { @@ -96,19 +96,76 @@ function system_array_get(arr, elem_or_function) {
96 /** 96 /**
97 * to iterate on array. 97 * to iterate on array.
98 * @param arr the array to iterate on. 98 * @param arr the array to iterate on.
99 - * @param pfn the function to apply on it 99 + * @param pfn the function to apply on it. return false to break loop.
100 * for example, 100 * for example,
101 * arr = [10, 15, 20, 30, 20, 40] 101 * arr = [10, 15, 20, 30, 20, 40]
102 * system_array_foreach(arr, function(elem, index){ 102 * system_array_foreach(arr, function(elem, index){
103 * console.log('index=' + index + ',elem=' + elem); 103 * console.log('index=' + index + ',elem=' + elem);
104 * }); 104 * });
  105 + * @return true when iterate all elems.
105 */ 106 */
106 function system_array_foreach(arr, pfn) { 107 function system_array_foreach(arr, pfn) {
  108 + if (!pfn) {
  109 + return false;
  110 + }
  111 +
107 for (var i = 0; i < arr.length; i++) { 112 for (var i = 0; i < arr.length; i++) {
108 - if (pfn) {  
109 - pfn(arr[i], i) 113 + if (!pfn(arr[i], i)) {
  114 + return false;
  115 + }
  116 + }
  117 +
  118 + return true;
  119 +}
  120 +
  121 +/**
  122 + * whether the str starts with flag.
  123 + */
  124 +function system_string_startswith(str, flag) {
  125 + if (typeof flag == "object" && flag.constructor == Array) {
  126 + for (var i = 0; i < flag.length; i++) {
  127 + if (system_string_startswith(str, flag[i])) {
  128 + return true;
  129 + }
  130 + }
  131 + }
  132 +
  133 + return str && flag && str.length >= flag.length && str.indexOf(flag) == 0;
  134 +}
  135 +
  136 +/**
  137 + * whether the str ends with flag.
  138 + */
  139 +function system_string_endswith(str, flag) {
  140 + if (typeof flag == "object" && flag.constructor == Array) {
  141 + for (var i = 0; i < flag.length; i++) {
  142 + if (system_string_endswith(str, flag[i])) {
  143 + return true;
  144 + }
  145 + }
110 } 146 }
  147 +
  148 + return str && flag && str.length >= flag.length && str.indexOf(flag) == str.length - flag.length;
  149 +}
  150 +
  151 +/**
  152 + * trim the start and end of flag in str.
  153 + * @param flag a string to trim.
  154 + */
  155 +function system_string_trim(str, flag) {
  156 + if (!flag || !flag.length || typeof flag != "string") {
  157 + return str;
111 } 158 }
  159 +
  160 + while (system_string_startswith(str, flag)) {
  161 + str = str.substr(flag.length);
  162 + }
  163 +
  164 + while (system_string_endswith(str, flag)) {
  165 + str = str.substr(0, str.length - flag.length);
  166 + }
  167 +
  168 + return str;
112 } 169 }
113 170
114 /** 171 /**
@@ -186,12 +243,19 @@ function parse_query_string(){ @@ -186,12 +243,19 @@ function parse_query_string(){
186 } 243 }
187 } 244 }
188 245
  246 + // split again for angularjs.
  247 + if (query_string.indexOf("?") > 0) {
  248 + query_string = query_string.split("?")[1];
  249 + }
  250 +
189 var queries = query_string.split("&"); 251 var queries = query_string.split("&");
190 - $(queries).each(function(){  
191 - var query = this.split("="); 252 + for (var i = 0; i < queries.length; i++) {
  253 + var elem = queries[i];
  254 +
  255 + var query = elem.split("=");
192 obj[query[0]] = query[1]; 256 obj[query[0]] = query[1];
193 obj.user_query[query[0]] = query[1]; 257 obj.user_query[query[0]] = query[1];
194 - }); 258 + }
195 259
196 return obj; 260 return obj;
197 } 261 }
@@ -244,6 +308,8 @@ function parse_rtmp_url(rtmp_url) { @@ -244,6 +308,8 @@ function parse_rtmp_url(rtmp_url) {
244 } 308 }
245 309
246 var ret = { 310 var ret = {
  311 + url: rtmp_url,
  312 + schema: a.protocol.replace(":", ""),
247 server: a.hostname, port: port, 313 server: a.hostname, port: port,
248 vhost: vhost, app: app, stream: stream 314 vhost: vhost, app: app, stream: stream
249 }; 315 };
@@ -500,7 +566,14 @@ AsyncRefresh2.prototype.initialize = function(pfn, timeout) { @@ -500,7 +566,14 @@ AsyncRefresh2.prototype.initialize = function(pfn, timeout) {
500 * stop refresh, the refresh pfn is set to null. 566 * stop refresh, the refresh pfn is set to null.
501 */ 567 */
502 AsyncRefresh2.prototype.stop = function() { 568 AsyncRefresh2.prototype.stop = function() {
503 - this.refresh_change(null, null); 569 + this.__call.__enabled = false;
  570 +}
  571 +/**
  572 + * restart refresh, use previous config.
  573 + */
  574 +AsyncRefresh2.prototype.restart = function() {
  575 + this.__call.__enabled = true;
  576 + this.request(0);
504 } 577 }
505 /** 578 /**
506 * change refresh pfn, the old pfn will set to disabled. 579 * change refresh pfn, the old pfn will set to disabled.
@@ -24,6 +24,20 @@ @@ -24,6 +24,20 @@
24 <script type="text/javascript" src="js/jwplayer.js" ></script> 24 <script type="text/javascript" src="js/jwplayer.js" ></script>
25 <script type='text/javascript'>jwplayer.key = 'N8zhkmYvvRwOhz4aTGkySoEri4x+9pQwR7GHIQ=='; </script> 25 <script type='text/javascript'>jwplayer.key = 'N8zhkmYvvRwOhz4aTGkySoEri4x+9pQwR7GHIQ=='; </script>
26 <script type="text/javascript"> 26 <script type="text/javascript">
  27 + /****
  28 + * The parameters for this page:
  29 + * schema, the protocol schema, rtmp or http.
  30 + * server, the ip of the url.
  31 + * port, the rtmp port of url.
  32 + * vhost, the vhost of url, can equals to server.
  33 + * app, the app of url.
  34 + * stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
  35 + * autostart, whether auto play the stream.
  36 + * Additional params:
  37 + * hls_vhost, the vhost for hls.
  38 + * hls_port, the port for hls play.
  39 + * hls_autostart, whether auto play the hls stream.
  40 + */
27 var _player = null; 41 var _player = null;
28 var _url = null; 42 var _url = null;
29 $(function(){ 43 $(function(){
@@ -384,6 +384,9 @@ @@ -384,6 +384,9 @@
384 <input class="span2" style="width:135px" id="player_clock" type="text" placeholder="年-月-日 时:分:秒"> 384 <input class="span2" style="width:135px" id="player_clock" type="text" placeholder="年-月-日 时:分:秒">
385 </div> 385 </div>
386 </div> 386 </div>
  387 + <div>
  388 + <span id="debug_info"></span>
  389 + </div>
387 </div> 390 </div>
388 </div> 391 </div>
389 <footer> 392 <footer>
@@ -466,6 +469,19 @@ @@ -466,6 +469,19 @@
466 __active_mbt.addClass("active"); 469 __active_mbt.addClass("active");
467 } 470 }
468 471
  472 + /****
  473 + * The parameters for this page:
  474 + * schema, the protocol schema, rtmp or http.
  475 + * server, the ip of the url.
  476 + * port, the rtmp port of url.
  477 + * vhost, the vhost of url, can equals to server.
  478 + * app, the app of url.
  479 + * stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
  480 + * autostart, whether auto play the stream.
  481 + * for example:
  482 + * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream&server=ossrs.net&port=1935&autostart=true&schema=rtmp
  483 + * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream.flv&server=ossrs.net&port=8080&autostart=true&schema=http
  484 + */
469 $(function(){ 485 $(function(){
470 var query = parse_query_string(); 486 var query = parse_query_string();
471 487
@@ -496,6 +512,9 @@ @@ -496,6 +512,9 @@
496 }; 512 };
497 srs_player.on_player_metadata = function(metadata) { 513 srs_player.on_player_metadata = function(metadata) {
498 $("#btn_dar_original").text("视频原始比例" + "(" + metadata.width + ":" + metadata.height + ")"); 514 $("#btn_dar_original").text("视频原始比例" + "(" + metadata.width + ":" + metadata.height + ")");
  515 + if (metadata.ip && metadata.pid && metadata.cid) {
  516 + $("#debug_info").text("DEBUG: " + metadata.ip + ' grep -in "\\[' + metadata.pid + '\\]\\[' + metadata.cid + '\\]"');
  517 + }
499 select_dar("#btn_dar_original", 0, 0); 518 select_dar("#btn_dar_original", 0, 0);
500 select_fs_size("#btn_fs_size_screen_100", "screen", 100); 519 select_fs_size("#btn_fs_size_screen_100", "screen", 100);
501 }; 520 };
@@ -551,6 +570,9 @@ @@ -551,6 +570,9 @@
551 + "vhost=" + rtmp.vhost + "&app=" + rtmp.app + "&stream=" + rtmp.stream 570 + "vhost=" + rtmp.vhost + "&app=" + rtmp.app + "&stream=" + rtmp.stream
552 + "&server=" + rtmp.server + "&port=" + rtmp.port 571 + "&server=" + rtmp.server + "&port=" + rtmp.port
553 + "&autostart=true"; 572 + "&autostart=true";
  573 + if (rtmp.schema == "http") {
  574 + url += "&schema=http";
  575 + }
554 $("#player_url").text($("#txt_url").val()).attr("href", url); 576 $("#player_url").text($("#txt_url").val()).attr("href", url);
555 577
556 $("#link_server").text(rtmp.server); 578 $("#link_server").text(rtmp.server);
@@ -39,3 +39,4 @@ @@ -39,3 +39,4 @@
39 <flashCatalyst validateFlashCatalystCompatibility="false"/> 39 <flashCatalyst validateFlashCatalystCompatibility="false"/>
40 </actionScriptProperties> 40 </actionScriptProperties>
41 41
  42 +
@@ -175,6 +175,11 @@ package @@ -175,6 +175,11 @@ package
175 private function system_on_metadata(metadata:Object):void { 175 private function system_on_metadata(metadata:Object):void {
176 this.media_metadata = metadata; 176 this.media_metadata = metadata;
177 177
  178 + // update the debug info.
  179 + if (metadata) {
  180 + on_debug_info(metadata);
  181 + }
  182 +
178 // for js. 183 // for js.
179 var obj:Object = __get_video_size_object(); 184 var obj:Object = __get_video_size_object();
180 185
@@ -190,6 +195,21 @@ package @@ -190,6 +195,21 @@ package
190 if (srs_authors != null) { 195 if (srs_authors != null) {
191 obj.contributor = srs_authors; 196 obj.contributor = srs_authors;
192 } 197 }
  198 + if (srs_id != null) {
  199 + obj.cid = srs_id;
  200 + }
  201 + if (srs_pid != null) {
  202 + obj.pid = srs_pid;
  203 + }
  204 + if (srs_server_ip != null) {
  205 + obj.ip = srs_server_ip;
  206 + }
  207 +
  208 + var s:String = "";
  209 + for (var key:String in obj) {
  210 + s += key + "=" + obj[key] + " ";
  211 + }
  212 + log("metadata is " + s);
193 213
194 var code:int = flash.external.ExternalInterface.call(js_on_player_metadata, js_id, obj); 214 var code:int = flash.external.ExternalInterface.call(js_on_player_metadata, js_id, obj);
195 if (code != 0) { 215 if (code != 0) {
@@ -350,6 +370,30 @@ package @@ -350,6 +370,30 @@ package
350 } 370 }
351 371
352 /** 372 /**
  373 + * server can set the debug info in _result of RTMP connect, or metadata.
  374 + */
  375 + private function on_debug_info(data:*):void {
  376 + if (data.hasOwnProperty("srs_server")) {
  377 + srs_server = data.srs_server;
  378 + }
  379 + if (data.hasOwnProperty("srs_primary")) {
  380 + srs_primary = data.srs_primary;
  381 + }
  382 + if (data.hasOwnProperty("srs_authors")) {
  383 + srs_authors = data.srs_authors;
  384 + }
  385 + if (data.hasOwnProperty("srs_id")) {
  386 + srs_id = data.srs_id;
  387 + }
  388 + if (data.hasOwnProperty("srs_pid")) {
  389 + srs_pid = data.srs_pid;
  390 + }
  391 + if (data.hasOwnProperty("srs_server_ip")) {
  392 + srs_server_ip = data.srs_server_ip;
  393 + }
  394 + }
  395 +
  396 + /**
353 * function for js to call: to play the stream. stop then play. 397 * function for js to call: to play the stream. stop then play.
354 * @param url, the rtmp/http url to play. 398 * @param url, the rtmp/http url to play.
355 * @param _width, the player width. 399 * @param _width, the player width.
@@ -375,24 +419,7 @@ package @@ -375,24 +419,7 @@ package
375 log("NetConnection: code=" + evt.info.code); 419 log("NetConnection: code=" + evt.info.code);
376 420
377 if (evt.info.hasOwnProperty("data") && evt.info.data) { 421 if (evt.info.hasOwnProperty("data") && evt.info.data) {
378 - if (evt.info.data.hasOwnProperty("srs_server")) {  
379 - srs_server = evt.info.data.srs_server;  
380 - }  
381 - if (evt.info.data.hasOwnProperty("srs_primary")) {  
382 - srs_primary = evt.info.data.srs_primary;  
383 - }  
384 - if (evt.info.data.hasOwnProperty("srs_authors")) {  
385 - srs_authors = evt.info.data.srs_authors;  
386 - }  
387 - if (evt.info.data.hasOwnProperty("srs_id")) {  
388 - srs_id = evt.info.data.srs_id;  
389 - }  
390 - if (evt.info.data.hasOwnProperty("srs_pid")) {  
391 - srs_pid = evt.info.data.srs_pid;  
392 - }  
393 - if (evt.info.data.hasOwnProperty("srs_server_ip")) {  
394 - srs_server_ip = evt.info.data.srs_server_ip;  
395 - } 422 + on_debug_info(evt.info.data);
396 update_context_items(); 423 update_context_items();
397 } 424 }
398 425