winlin

extract the components of js

@@ -7,8 +7,7 @@ @@ -7,8 +7,7 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script>  
11 - <script type="text/javascript" src="js/srs.log.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
12 <style> 11 <style>
13 body{ 12 body{
14 padding-top: 55px; 13 padding-top: 55px;
@@ -26,18 +26,6 @@ function srs_get_player_chat_vhost(src_vhost) { return (src_vhost != srs_get_pla @@ -26,18 +26,6 @@ function srs_get_player_chat_vhost(src_vhost) { return (src_vhost != srs_get_pla
26 ////////////////////////////////////////////////////////////////////////////////// 26 //////////////////////////////////////////////////////////////////////////////////
27 ////////////////////////////////////////////////////////////////////////////////// 27 //////////////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////////////////// 28 //////////////////////////////////////////////////////////////////////////////////
29 -/**  
30 -* padding the output.  
31 -* padding(3, 5, '0') is 00003  
32 -* padding(3, 5, 'x') is xxxx3  
33 -* @see http://blog.csdn.net/win_lin/article/details/12065413  
34 -*/  
35 -function padding(number, length, prefix) {  
36 - if(String(number).length >= length){  
37 - return String(number);  
38 - }  
39 - return padding(prefix+number, length, prefix);  
40 -}  
41 29
42 /** 30 /**
43 * update the navigator, add same query string. 31 * update the navigator, add same query string.
@@ -53,40 +41,6 @@ function update_nav() { @@ -53,40 +41,6 @@ function update_nav() {
53 } 41 }
54 42
55 /** 43 /**
56 -* parse the query string to object.  
57 -*/  
58 -function parse_query_string(){  
59 - var obj = {};  
60 -  
61 - // parse the host(hostname:http_port), pathname(dir/filename)  
62 - obj.host = window.location.host;  
63 - obj.hostname = window.location.hostname;  
64 - obj.http_port = (window.location.port == "")? 80:window.location.port;  
65 - obj.pathname = window.location.pathname;  
66 - if (obj.pathname.lastIndexOf("/") <= 0) {  
67 - obj.dir = "/";  
68 - obj.filename = "";  
69 - } else {  
70 - obj.dir = obj.pathname.substr(0, obj.pathname.lastIndexOf("/"));  
71 - obj.filename = obj.pathname.substr(obj.pathname.lastIndexOf("/"));  
72 - }  
73 -  
74 - // parse the query string.  
75 - var query_string = String(window.location.search).replace(" ", "").split("?")[1];  
76 - if(query_string == undefined){  
77 - return obj;  
78 - }  
79 -  
80 - var queries = query_string.split("&");  
81 - $(queries).each(function(){  
82 - var query = this.split("=");  
83 - obj[query[0]] = query[1];  
84 - });  
85 -  
86 - return obj;  
87 -}  
88 -  
89 -/**  
90 @param server the ip of server. default to window.location.hostname 44 @param server the ip of server. default to window.location.hostname
91 @param vhost the vhost of rtmp. default to window.location.hostname 45 @param vhost the vhost of rtmp. default to window.location.hostname
92 @param port the port of rtmp. default to 1935 46 @param port the port of rtmp. default to 1935
@@ -157,43 +111,6 @@ function build_default_hls_url() { @@ -157,43 +111,6 @@ function build_default_hls_url() {
157 } 111 }
158 112
159 /** 113 /**
160 -* parse the rtmp url,  
161 -* for example: rtmp://demo.srs.com:1935/live...vhost...players/livestream  
162 -* @return object {server, port, vhost, app, stream}  
163 -*/  
164 -function srs_parse_rtmp_url(rtmp_url) {  
165 - // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri  
166 - var a = document.createElement("a");  
167 - a.href = rtmp_url.replace("rtmp://", "http://");  
168 -  
169 - var vhost = a.hostname;  
170 - var port = (a.port == "")? "1935":a.port;  
171 - var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);  
172 - var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);  
173 -  
174 - // parse the vhost in the params of app, that srs supports.  
175 - app = app.replace("...vhost...", "?vhost=");  
176 - if (app.indexOf("?") >= 0) {  
177 - var params = app.substr(app.indexOf("?"));  
178 - app = app.substr(0, app.indexOf("?"));  
179 -  
180 - if (params.indexOf("vhost=") > 0) {  
181 - vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);  
182 - if (vhost.indexOf("&") > 0) {  
183 - vhost = vhost.substr(0, vhost.indexOf("&"));  
184 - }  
185 - }  
186 - }  
187 -  
188 - var ret = {  
189 - server: a.hostname, port: port,  
190 - vhost: vhost, app: app, stream: stream  
191 - };  
192 -  
193 - return ret;  
194 -}  
195 -  
196 -/**  
197 * initialize the page. 114 * initialize the page.
198 * @param rtmp_url the div id contains the rtmp stream url to play 115 * @param rtmp_url the div id contains the rtmp stream url to play
199 * @param hls_url the div id contains the hls stream url to play 116 * @param hls_url the div id contains the hls stream url to play
@@ -404,351 +321,3 @@ function srs_publiser_get_codec( @@ -404,351 +321,3 @@ function srs_publiser_get_codec(
404 vcodec.size = $(sl_size).find("option:selected").val(); 321 vcodec.size = $(sl_size).find("option:selected").val();
405 vcodec.bitrate = $(sl_bitrate).find("option:selected").val(); 322 vcodec.bitrate = $(sl_bitrate).find("option:selected").val();
406 } 323 }
407 -  
408 -//////////////////////////////////////////////////////////////////////////////////  
409 -//////////////////////////////////////////////////////////////////////////////////  
410 -//////////////////////////////////////////////////////////////////////////////////  
411 -/**  
412 -* the SrsPlayer object.  
413 -* @param container the html container id.  
414 -* @param width a float value specifies the width of player.  
415 -* @param height a float value specifies the height of player.  
416 -* @param private_object [optional] an object that used as private object,  
417 -* for example, the logic chat object which owner this player.  
418 -*/  
419 -function SrsPlayer(container, width, height, private_object) {  
420 - if (!SrsPlayer.__id) {  
421 - SrsPlayer.__id = 100;  
422 - }  
423 - if (!SrsPlayer.__players) {  
424 - SrsPlayer.__players = [];  
425 - }  
426 -  
427 - SrsPlayer.__players.push(this);  
428 -  
429 - this.private_object = private_object;  
430 - this.container = container;  
431 - this.width = width;  
432 - this.height = height;  
433 - this.id = SrsPlayer.__id++;  
434 - this.stream_url = null;  
435 - this.buffer_time = 0.8; // default to 0.8  
436 - this.volume = 1.0; // default to 100%  
437 - this.callbackObj = null;  
438 -  
439 - // callback set the following values.  
440 - this.meatadata = {}; // for on_player_metadata  
441 - this.time = 0; // current stream time.  
442 - this.buffer_length = 0; // current stream buffer length.  
443 -}  
444 -/**  
445 -* user can set some callback, then start the player.  
446 -* @param url the default url.  
447 -* callbacks:  
448 -* on_player_ready():int, when srs player ready, user can play.  
449 -* on_player_metadata(metadata:Object):int, when srs player get metadata.  
450 -*/  
451 -SrsPlayer.prototype.start = function(url) {  
452 - if (url) {  
453 - this.stream_url = url;  
454 - }  
455 -  
456 - // embed the flash.  
457 - var flashvars = {};  
458 - flashvars.id = this.id;  
459 - flashvars.on_player_ready = "__srs_on_player_ready";  
460 - flashvars.on_player_metadata = "__srs_on_player_metadata";  
461 - flashvars.on_player_timer = "__srs_on_player_timer";  
462 -  
463 - var params = {};  
464 - params.wmode = "opaque";  
465 - params.allowFullScreen = "true";  
466 - params.allowScriptAccess = "always";  
467 -  
468 - var attributes = {};  
469 -  
470 - var self = this;  
471 -  
472 - swfobject.embedSWF(  
473 - "srs_player/release/srs_player.swf?_version="+srs_get_version_code(),  
474 - this.container,  
475 - this.width, this.height,  
476 - "11.1", "js/AdobeFlashPlayerInstall.swf",  
477 - flashvars, params, attributes,  
478 - function(callbackObj){  
479 - self.callbackObj = callbackObj;  
480 - }  
481 - );  
482 -  
483 - return this;  
484 -}  
485 -/**  
486 -* play the stream.  
487 -* @param stream_url the url of stream, rtmp or http.  
488 -* @param volume the volume, 0 is mute, 1 is 100%, 2 is 200%.  
489 -*/  
490 -SrsPlayer.prototype.play = function(url, volume) {  
491 - if (url) {  
492 - this.stream_url = url;  
493 - }  
494 -  
495 - // volume maybe 0, so never use if(volume) to check its value.  
496 - if (volume != null && volume != undefined) {  
497 - this.volume = volume;  
498 - }  
499 -  
500 - this.callbackObj.ref.__play(this.stream_url, this.width, this.height, this.buffer_time, this.volume);  
501 -}  
502 -SrsPlayer.prototype.stop = function() {  
503 - for (var i = 0; i < SrsPlayer.__players.length; i++) {  
504 - var player = SrsPlayer.__players[i];  
505 -  
506 - if (player.id != this.id) {  
507 - continue;  
508 - }  
509 -  
510 - SrsPlayer.__players.splice(i, 1);  
511 - break;  
512 - }  
513 -  
514 - this.callbackObj.ref.__stop();  
515 -}  
516 -SrsPlayer.prototype.pause = function() {  
517 - this.callbackObj.ref.__pause();  
518 -}  
519 -SrsPlayer.prototype.resume = function() {  
520 - this.callbackObj.ref.__resume();  
521 -}  
522 -/**  
523 -* to set the DAR, for example, DAR=16:9  
524 -* @param num, for example, 9.  
525 -* use metadata height if 0.  
526 -* use user specified height if -1.  
527 -* @param den, for example, 16.  
528 -* use metadata width if 0.  
529 -* use user specified width if -1.  
530 -*/  
531 -SrsPlayer.prototype.dar = function(num, den) {  
532 - this.callbackObj.ref.__dar(num, den);  
533 -}  
534 -/**  
535 -* set the fullscreen size data.  
536 -* @refer the refer fullscreen mode. it can be:  
537 -* video: use video orignal size.  
538 -* screen: use screen size to rescale video.  
539 -* @param percent, the rescale percent, where  
540 -* 100 means 100%.  
541 -*/  
542 -SrsPlayer.prototype.set_fs = function(refer, percent) {  
543 - this.callbackObj.ref.__set_fs(refer, percent);  
544 -}  
545 -/**  
546 -* set the stream buffer time in seconds.  
547 -* @buffer_time the buffer time in seconds.  
548 -*/  
549 -SrsPlayer.prototype.set_bt = function(buffer_time) {  
550 - this.buffer_time = buffer_time;  
551 - this.callbackObj.ref.__set_bt(buffer_time);  
552 -}  
553 -SrsPlayer.prototype.on_player_ready = function() {  
554 -}  
555 -SrsPlayer.prototype.on_player_metadata = function(metadata) {  
556 - // ignore.  
557 -}  
558 -SrsPlayer.prototype.on_player_timer = function(time, buffer_length) {  
559 - // ignore.  
560 -}  
561 -function __srs_find_player(id) {  
562 - for (var i = 0; i < SrsPlayer.__players.length; i++) {  
563 - var player = SrsPlayer.__players[i];  
564 -  
565 - if (player.id != id) {  
566 - continue;  
567 - }  
568 -  
569 - return player;  
570 - }  
571 -  
572 - throw new Error("player not found. id=" + id);  
573 -}  
574 -function __srs_on_player_ready(id) {  
575 - var player = __srs_find_player(id);  
576 - player.on_player_ready();  
577 -}  
578 -function __srs_on_player_metadata(id, metadata) {  
579 - var player = __srs_find_player(id);  
580 -  
581 - // user may override the on_player_metadata,  
582 - // so set the data before invoke it.  
583 - player.metadata = metadata;  
584 -  
585 - player.on_player_metadata(metadata);  
586 -}  
587 -function __srs_on_player_timer(id, time, buffer_length) {  
588 - var player = __srs_find_player(id);  
589 -  
590 - buffer_length = Math.max(0, buffer_length);  
591 - buffer_length = Math.min(player.buffer_time, buffer_length);  
592 -  
593 - time = Math.max(0, time);  
594 -  
595 - // user may override the on_player_timer,  
596 - // so set the data before invoke it.  
597 - player.time = time;  
598 - player.buffer_length = buffer_length;  
599 -  
600 - player.on_player_timer(time, buffer_length);  
601 -}  
602 -  
603 -//////////////////////////////////////////////////////////////////////////////////  
604 -//////////////////////////////////////////////////////////////////////////////////  
605 -//////////////////////////////////////////////////////////////////////////////////  
606 -/**  
607 -* the SrsPublisher object.  
608 -* @param container the html container id.  
609 -* @param width a float value specifies the width of publisher.  
610 -* @param height a float value specifies the height of publisher.  
611 -* @param private_object [optional] an object that used as private object,  
612 -* for example, the logic chat object which owner this publisher.  
613 -*/  
614 -function SrsPublisher(container, width, height, private_object) {  
615 - if (!SrsPublisher.__id) {  
616 - SrsPublisher.__id = 100;  
617 - }  
618 - if (!SrsPublisher.__publishers) {  
619 - SrsPublisher.__publishers = [];  
620 - }  
621 -  
622 - SrsPublisher.__publishers.push(this);  
623 -  
624 - this.private_object = private_object;  
625 - this.container = container;  
626 - this.width = width;  
627 - this.height = height;  
628 - this.id = SrsPublisher.__id++;  
629 - this.callbackObj = null;  
630 -  
631 - // set the values when publish.  
632 - this.url = null;  
633 - this.vcodec = {};  
634 - this.acodec = {};  
635 -  
636 - // callback set the following values.  
637 - this.cameras = [];  
638 - this.microphones = [];  
639 - this.code = 0;  
640 -  
641 - // error code defines.  
642 - this.errors = {  
643 - "100": "无法获取指定的摄像头", //error_camera_get  
644 - "101": "无法获取指定的麦克风", //error_microphone_get  
645 - "102": "摄像头为禁用状态,推流时请允许flash访问摄像头" //error_camera_muted  
646 - };  
647 -}  
648 -/**  
649 -* user can set some callback, then start the publisher.  
650 -* callbacks:  
651 -* on_publisher_ready(cameras, microphones):int, when srs publisher ready, user can publish.  
652 -* on_publisher_error(code, desc):int, when srs publisher error, callback this method.  
653 -* on_publisher_warn(code, desc):int, when srs publisher warn, callback this method.  
654 -*/  
655 -SrsPublisher.prototype.start = function() {  
656 - // embed the flash.  
657 - var flashvars = {};  
658 - flashvars.id = this.id;  
659 - flashvars.on_publisher_ready = "__srs_on_publisher_ready";  
660 - flashvars.on_publisher_error = "__srs_on_publisher_error";  
661 - flashvars.on_publisher_warn = "__srs_on_publisher_warn";  
662 -  
663 - var params = {};  
664 - params.wmode = "opaque";  
665 - params.allowFullScreen = "true";  
666 - params.allowScriptAccess = "always";  
667 -  
668 - var attributes = {};  
669 -  
670 - var self = this;  
671 -  
672 - swfobject.embedSWF(  
673 - "srs_publisher/release/srs_publisher.swf?_version="+srs_get_version_code(),  
674 - this.container,  
675 - this.width, this.height,  
676 - "11.1", "js/AdobeFlashPlayerInstall.swf",  
677 - flashvars, params, attributes,  
678 - function(callbackObj){  
679 - self.callbackObj = callbackObj;  
680 - }  
681 - );  
682 -  
683 - return this;  
684 -}  
685 -/**  
686 -* publish stream to server.  
687 -* @param url a string indicates the rtmp url to publish.  
688 -* @param vcodec an object contains the video codec info.  
689 -* @param acodec an object contains the audio codec info.  
690 -*/  
691 -SrsPublisher.prototype.publish = function(url, vcodec, acodec) {  
692 - this.url = url;  
693 - this.vcodec = vcodec;  
694 - this.acodec = acodec;  
695 -  
696 - this.callbackObj.ref.__publish(url, this.width, this.height, vcodec, acodec);  
697 -}  
698 -SrsPublisher.prototype.stop = function() {  
699 - this.callbackObj.ref.__stop();  
700 -}  
701 -/**  
702 -* when publisher ready.  
703 -* @param cameras a string array contains the names of cameras.  
704 -* @param microphones a string array contains the names of microphones.  
705 -*/  
706 -SrsPublisher.prototype.on_publisher_ready = function(cameras, microphones) {  
707 -}  
708 -/**  
709 -* when publisher error.  
710 -* @code the error code.  
711 -* @desc the error desc message.  
712 -*/  
713 -SrsPublisher.prototype.on_publisher_error = function(code, desc) {  
714 - throw new Error("publisher error. code=" + code + ", desc=" + desc);  
715 -}  
716 -SrsPublisher.prototype.on_publisher_warn = function(code, desc) {  
717 - throw new Error("publisher warn. code=" + code + ", desc=" + desc);  
718 -}  
719 -function __srs_find_publisher(id) {  
720 - for (var i = 0; i < SrsPublisher.__publishers.length; i++) {  
721 - var publisher = SrsPublisher.__publishers[i];  
722 -  
723 - if (publisher.id != id) {  
724 - continue;  
725 - }  
726 -  
727 - return publisher;  
728 - }  
729 -  
730 - throw new Error("publisher not found. id=" + id);  
731 -}  
732 -function __srs_on_publisher_ready(id, cameras, microphones) {  
733 - var publisher = __srs_find_publisher(id);  
734 -  
735 - publisher.cameras = cameras;  
736 - publisher.microphones = microphones;  
737 -  
738 - publisher.on_publisher_ready(cameras, microphones);  
739 -}  
740 -function __srs_on_publisher_error(id, code) {  
741 - var publisher = __srs_find_publisher(id);  
742 -  
743 - publisher.code = code;  
744 -  
745 - publisher.on_publisher_error(code, publisher.errors[""+code]);  
746 -}  
747 -function __srs_on_publisher_warn(id, code) {  
748 - var publisher = __srs_find_publisher(id);  
749 -  
750 - publisher.code = code;  
751 -  
752 - publisher.on_publisher_warn(code, publisher.errors[""+code]);  
753 -}  
754 -  
  1 +/**
  2 +* the SrsPlayer object.
  3 +* @param container the html container id.
  4 +* @param width a float value specifies the width of player.
  5 +* @param height a float value specifies the height of player.
  6 +* @param private_object [optional] an object that used as private object,
  7 +* for example, the logic chat object which owner this player.
  8 +*/
  9 +function SrsPlayer(container, width, height, private_object) {
  10 + if (!SrsPlayer.__id) {
  11 + SrsPlayer.__id = 100;
  12 + }
  13 + if (!SrsPlayer.__players) {
  14 + SrsPlayer.__players = [];
  15 + }
  16 +
  17 + SrsPlayer.__players.push(this);
  18 +
  19 + this.private_object = private_object;
  20 + this.container = container;
  21 + this.width = width;
  22 + this.height = height;
  23 + this.id = SrsPlayer.__id++;
  24 + this.stream_url = null;
  25 + this.buffer_time = 0.8; // default to 0.8
  26 + this.volume = 1.0; // default to 100%
  27 + this.callbackObj = null;
  28 +
  29 + // callback set the following values.
  30 + this.meatadata = {}; // for on_player_metadata
  31 + this.time = 0; // current stream time.
  32 + this.buffer_length = 0; // current stream buffer length.
  33 +}
  34 +/**
  35 +* user can set some callback, then start the player.
  36 +* @param url the default url.
  37 +* callbacks:
  38 +* on_player_ready():int, when srs player ready, user can play.
  39 +* on_player_metadata(metadata:Object):int, when srs player get metadata.
  40 +*/
  41 +SrsPlayer.prototype.start = function(url) {
  42 + if (url) {
  43 + this.stream_url = url;
  44 + }
  45 +
  46 + // embed the flash.
  47 + var flashvars = {};
  48 + flashvars.id = this.id;
  49 + flashvars.on_player_ready = "__srs_on_player_ready";
  50 + flashvars.on_player_metadata = "__srs_on_player_metadata";
  51 + flashvars.on_player_timer = "__srs_on_player_timer";
  52 +
  53 + var params = {};
  54 + params.wmode = "opaque";
  55 + params.allowFullScreen = "true";
  56 + params.allowScriptAccess = "always";
  57 +
  58 + var attributes = {};
  59 +
  60 + var self = this;
  61 +
  62 + swfobject.embedSWF(
  63 + "srs_player/release/srs_player.swf?_version="+srs_get_version_code(),
  64 + this.container,
  65 + this.width, this.height,
  66 + "11.1", "js/AdobeFlashPlayerInstall.swf",
  67 + flashvars, params, attributes,
  68 + function(callbackObj){
  69 + self.callbackObj = callbackObj;
  70 + }
  71 + );
  72 +
  73 + return this;
  74 +}
  75 +/**
  76 +* play the stream.
  77 +* @param stream_url the url of stream, rtmp or http.
  78 +* @param volume the volume, 0 is mute, 1 is 100%, 2 is 200%.
  79 +*/
  80 +SrsPlayer.prototype.play = function(url, volume) {
  81 + if (url) {
  82 + this.stream_url = url;
  83 + }
  84 +
  85 + // volume maybe 0, so never use if(volume) to check its value.
  86 + if (volume != null && volume != undefined) {
  87 + this.volume = volume;
  88 + }
  89 +
  90 + this.callbackObj.ref.__play(this.stream_url, this.width, this.height, this.buffer_time, this.volume);
  91 +}
  92 +SrsPlayer.prototype.stop = function() {
  93 + for (var i = 0; i < SrsPlayer.__players.length; i++) {
  94 + var player = SrsPlayer.__players[i];
  95 +
  96 + if (player.id != this.id) {
  97 + continue;
  98 + }
  99 +
  100 + SrsPlayer.__players.splice(i, 1);
  101 + break;
  102 + }
  103 +
  104 + this.callbackObj.ref.__stop();
  105 +}
  106 +SrsPlayer.prototype.pause = function() {
  107 + this.callbackObj.ref.__pause();
  108 +}
  109 +SrsPlayer.prototype.resume = function() {
  110 + this.callbackObj.ref.__resume();
  111 +}
  112 +/**
  113 +* to set the DAR, for example, DAR=16:9
  114 +* @param num, for example, 9.
  115 +* use metadata height if 0.
  116 +* use user specified height if -1.
  117 +* @param den, for example, 16.
  118 +* use metadata width if 0.
  119 +* use user specified width if -1.
  120 +*/
  121 +SrsPlayer.prototype.dar = function(num, den) {
  122 + this.callbackObj.ref.__dar(num, den);
  123 +}
  124 +/**
  125 +* set the fullscreen size data.
  126 +* @refer the refer fullscreen mode. it can be:
  127 +* video: use video orignal size.
  128 +* screen: use screen size to rescale video.
  129 +* @param percent, the rescale percent, where
  130 +* 100 means 100%.
  131 +*/
  132 +SrsPlayer.prototype.set_fs = function(refer, percent) {
  133 + this.callbackObj.ref.__set_fs(refer, percent);
  134 +}
  135 +/**
  136 +* set the stream buffer time in seconds.
  137 +* @buffer_time the buffer time in seconds.
  138 +*/
  139 +SrsPlayer.prototype.set_bt = function(buffer_time) {
  140 + this.buffer_time = buffer_time;
  141 + this.callbackObj.ref.__set_bt(buffer_time);
  142 +}
  143 +SrsPlayer.prototype.on_player_ready = function() {
  144 +}
  145 +SrsPlayer.prototype.on_player_metadata = function(metadata) {
  146 + // ignore.
  147 +}
  148 +SrsPlayer.prototype.on_player_timer = function(time, buffer_length) {
  149 + // ignore.
  150 +}
  151 +function __srs_find_player(id) {
  152 + for (var i = 0; i < SrsPlayer.__players.length; i++) {
  153 + var player = SrsPlayer.__players[i];
  154 +
  155 + if (player.id != id) {
  156 + continue;
  157 + }
  158 +
  159 + return player;
  160 + }
  161 +
  162 + throw new Error("player not found. id=" + id);
  163 +}
  164 +function __srs_on_player_ready(id) {
  165 + var player = __srs_find_player(id);
  166 + player.on_player_ready();
  167 +}
  168 +function __srs_on_player_metadata(id, metadata) {
  169 + var player = __srs_find_player(id);
  170 +
  171 + // user may override the on_player_metadata,
  172 + // so set the data before invoke it.
  173 + player.metadata = metadata;
  174 +
  175 + player.on_player_metadata(metadata);
  176 +}
  177 +function __srs_on_player_timer(id, time, buffer_length) {
  178 + var player = __srs_find_player(id);
  179 +
  180 + buffer_length = Math.max(0, buffer_length);
  181 + buffer_length = Math.min(player.buffer_time, buffer_length);
  182 +
  183 + time = Math.max(0, time);
  184 +
  185 + // user may override the on_player_timer,
  186 + // so set the data before invoke it.
  187 + player.time = time;
  188 + player.buffer_length = buffer_length;
  189 +
  190 + player.on_player_timer(time, buffer_length);
  191 +}
  1 +/**
  2 +* the SrsPublisher object.
  3 +* @param container the html container id.
  4 +* @param width a float value specifies the width of publisher.
  5 +* @param height a float value specifies the height of publisher.
  6 +* @param private_object [optional] an object that used as private object,
  7 +* for example, the logic chat object which owner this publisher.
  8 +*/
  9 +function SrsPublisher(container, width, height, private_object) {
  10 + if (!SrsPublisher.__id) {
  11 + SrsPublisher.__id = 100;
  12 + }
  13 + if (!SrsPublisher.__publishers) {
  14 + SrsPublisher.__publishers = [];
  15 + }
  16 +
  17 + SrsPublisher.__publishers.push(this);
  18 +
  19 + this.private_object = private_object;
  20 + this.container = container;
  21 + this.width = width;
  22 + this.height = height;
  23 + this.id = SrsPublisher.__id++;
  24 + this.callbackObj = null;
  25 +
  26 + // set the values when publish.
  27 + this.url = null;
  28 + this.vcodec = {};
  29 + this.acodec = {};
  30 +
  31 + // callback set the following values.
  32 + this.cameras = [];
  33 + this.microphones = [];
  34 + this.code = 0;
  35 +
  36 + // error code defines.
  37 + this.errors = {
  38 + "100": "无法获取指定的摄像头", //error_camera_get
  39 + "101": "无法获取指定的麦克风", //error_microphone_get
  40 + "102": "摄像头为禁用状态,推流时请允许flash访问摄像头" //error_camera_muted
  41 + };
  42 +}
  43 +/**
  44 +* user can set some callback, then start the publisher.
  45 +* callbacks:
  46 +* on_publisher_ready(cameras, microphones):int, when srs publisher ready, user can publish.
  47 +* on_publisher_error(code, desc):int, when srs publisher error, callback this method.
  48 +* on_publisher_warn(code, desc):int, when srs publisher warn, callback this method.
  49 +*/
  50 +SrsPublisher.prototype.start = function() {
  51 + // embed the flash.
  52 + var flashvars = {};
  53 + flashvars.id = this.id;
  54 + flashvars.on_publisher_ready = "__srs_on_publisher_ready";
  55 + flashvars.on_publisher_error = "__srs_on_publisher_error";
  56 + flashvars.on_publisher_warn = "__srs_on_publisher_warn";
  57 +
  58 + var params = {};
  59 + params.wmode = "opaque";
  60 + params.allowFullScreen = "true";
  61 + params.allowScriptAccess = "always";
  62 +
  63 + var attributes = {};
  64 +
  65 + var self = this;
  66 +
  67 + swfobject.embedSWF(
  68 + "srs_publisher/release/srs_publisher.swf?_version="+srs_get_version_code(),
  69 + this.container,
  70 + this.width, this.height,
  71 + "11.1", "js/AdobeFlashPlayerInstall.swf",
  72 + flashvars, params, attributes,
  73 + function(callbackObj){
  74 + self.callbackObj = callbackObj;
  75 + }
  76 + );
  77 +
  78 + return this;
  79 +}
  80 +/**
  81 +* publish stream to server.
  82 +* @param url a string indicates the rtmp url to publish.
  83 +* @param vcodec an object contains the video codec info.
  84 +* @param acodec an object contains the audio codec info.
  85 +*/
  86 +SrsPublisher.prototype.publish = function(url, vcodec, acodec) {
  87 + this.url = url;
  88 + this.vcodec = vcodec;
  89 + this.acodec = acodec;
  90 +
  91 + this.callbackObj.ref.__publish(url, this.width, this.height, vcodec, acodec);
  92 +}
  93 +SrsPublisher.prototype.stop = function() {
  94 + this.callbackObj.ref.__stop();
  95 +}
  96 +/**
  97 +* when publisher ready.
  98 +* @param cameras a string array contains the names of cameras.
  99 +* @param microphones a string array contains the names of microphones.
  100 +*/
  101 +SrsPublisher.prototype.on_publisher_ready = function(cameras, microphones) {
  102 +}
  103 +/**
  104 +* when publisher error.
  105 +* @code the error code.
  106 +* @desc the error desc message.
  107 +*/
  108 +SrsPublisher.prototype.on_publisher_error = function(code, desc) {
  109 + throw new Error("publisher error. code=" + code + ", desc=" + desc);
  110 +}
  111 +SrsPublisher.prototype.on_publisher_warn = function(code, desc) {
  112 + throw new Error("publisher warn. code=" + code + ", desc=" + desc);
  113 +}
  114 +function __srs_find_publisher(id) {
  115 + for (var i = 0; i < SrsPublisher.__publishers.length; i++) {
  116 + var publisher = SrsPublisher.__publishers[i];
  117 +
  118 + if (publisher.id != id) {
  119 + continue;
  120 + }
  121 +
  122 + return publisher;
  123 + }
  124 +
  125 + throw new Error("publisher not found. id=" + id);
  126 +}
  127 +function __srs_on_publisher_ready(id, cameras, microphones) {
  128 + var publisher = __srs_find_publisher(id);
  129 +
  130 + publisher.cameras = cameras;
  131 + publisher.microphones = microphones;
  132 +
  133 + publisher.on_publisher_ready(cameras, microphones);
  134 +}
  135 +function __srs_on_publisher_error(id, code) {
  136 + var publisher = __srs_find_publisher(id);
  137 +
  138 + publisher.code = code;
  139 +
  140 + publisher.on_publisher_error(code, publisher.errors[""+code]);
  141 +}
  142 +function __srs_on_publisher_warn(id, code) {
  143 + var publisher = __srs_find_publisher(id);
  144 +
  145 + publisher.code = code;
  146 +
  147 + publisher.on_publisher_warn(code, publisher.errors[""+code]);
  148 +}
  1 +/**
  2 +* padding the output.
  3 +* padding(3, 5, '0') is 00003
  4 +* padding(3, 5, 'x') is xxxx3
  5 +* @see http://blog.csdn.net/win_lin/article/details/12065413
  6 +*/
  7 +function padding(number, length, prefix) {
  8 + if(String(number).length >= length){
  9 + return String(number);
  10 + }
  11 + return padding(prefix+number, length, prefix);
  12 +}
  13 +
  14 +/**
  15 +* parse the query string to object.
  16 +*/
  17 +function parse_query_string(){
  18 + var obj = {};
  19 +
  20 + // parse the host(hostname:http_port), pathname(dir/filename)
  21 + obj.host = window.location.host;
  22 + obj.hostname = window.location.hostname;
  23 + obj.http_port = (window.location.port == "")? 80:window.location.port;
  24 + obj.pathname = window.location.pathname;
  25 + if (obj.pathname.lastIndexOf("/") <= 0) {
  26 + obj.dir = "/";
  27 + obj.filename = "";
  28 + } else {
  29 + obj.dir = obj.pathname.substr(0, obj.pathname.lastIndexOf("/"));
  30 + obj.filename = obj.pathname.substr(obj.pathname.lastIndexOf("/"));
  31 + }
  32 +
  33 + // parse the query string.
  34 + var query_string = String(window.location.search).replace(" ", "").split("?")[1];
  35 + if(query_string == undefined){
  36 + return obj;
  37 + }
  38 +
  39 + var queries = query_string.split("&");
  40 + $(queries).each(function(){
  41 + var query = this.split("=");
  42 + obj[query[0]] = query[1];
  43 + });
  44 +
  45 + return obj;
  46 +}
  47 +
  48 +/**
  49 +* parse the rtmp url,
  50 +* for example: rtmp://demo.srs.com:1935/live...vhost...players/livestream
  51 +* @return object {server, port, vhost, app, stream}
  52 +*/
  53 +function srs_parse_rtmp_url(rtmp_url) {
  54 + // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
  55 + var a = document.createElement("a");
  56 + a.href = rtmp_url.replace("rtmp://", "http://");
  57 +
  58 + var vhost = a.hostname;
  59 + var port = (a.port == "")? "1935":a.port;
  60 + var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);
  61 + var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);
  62 +
  63 + // parse the vhost in the params of app, that srs supports.
  64 + app = app.replace("...vhost...", "?vhost=");
  65 + if (app.indexOf("?") >= 0) {
  66 + var params = app.substr(app.indexOf("?"));
  67 + app = app.substr(0, app.indexOf("?"));
  68 +
  69 + if (params.indexOf("vhost=") > 0) {
  70 + vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);
  71 + if (vhost.indexOf("&") > 0) {
  72 + vhost = vhost.substr(0, vhost.indexOf("&"));
  73 + }
  74 + }
  75 + }
  76 +
  77 + var ret = {
  78 + server: a.hostname, port: port,
  79 + vhost: vhost, app: app, stream: stream
  80 + };
  81 +
  82 + return ret;
  83 +}
@@ -6,8 +6,11 @@ @@ -6,8 +6,11 @@
6 <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/> 6 <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 - <script type="text/javascript" src="js/srs.js"></script> 9 + <script type="text/javascript" src="js/srs.page.js"></script>
10 <script type="text/javascript" src="js/srs.log.js"></script> 10 <script type="text/javascript" src="js/srs.log.js"></script>
  11 + <script type="text/javascript" src="js/srs.player.js"></script>
  12 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  13 + <script type="text/javascript" src="js/srs.utility.js"></script>
11 <style> 14 <style>
12 body{ 15 body{
13 padding-top: 55px; 16 padding-top: 55px;
@@ -7,8 +7,11 @@ @@ -7,8 +7,11 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
11 <script type="text/javascript" src="js/srs.log.js"></script> 11 <script type="text/javascript" src="js/srs.log.js"></script>
  12 + <script type="text/javascript" src="js/srs.player.js"></script>
  13 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  14 + <script type="text/javascript" src="js/srs.utility.js"></script>
12 <style> 15 <style>
13 body{ 16 body{
14 padding-top: 55px; 17 padding-top: 55px;
@@ -7,8 +7,12 @@ @@ -7,8 +7,12 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
11 <script type="text/javascript" src="js/srs.log.js"></script> 11 <script type="text/javascript" src="js/srs.log.js"></script>
  12 + <script type="text/javascript" src="js/srs.player.js"></script>
  13 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  14 + <script type="text/javascript" src="js/srs.utility.js"></script>
  15 + <script type="text/javascript" src="js/srs.utility.js"></script>
12 <style> 16 <style>
13 body{ 17 body{
14 padding-top: 55px; 18 padding-top: 55px;
@@ -7,8 +7,11 @@ @@ -7,8 +7,11 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
11 <script type="text/javascript" src="js/srs.log.js"></script> 11 <script type="text/javascript" src="js/srs.log.js"></script>
  12 + <script type="text/javascript" src="js/srs.player.js"></script>
  13 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  14 + <script type="text/javascript" src="js/srs.utility.js"></script>
12 <style> 15 <style>
13 body{ 16 body{
14 padding-top: 55px; 17 padding-top: 55px;
@@ -415,7 +418,7 @@ @@ -415,7 +418,7 @@
415 418
416 $("#btn_join").text("退出会议"); 419 $("#btn_join").text("退出会议");
417 420
418 - info("开始推流到服务器"); 421 + info("开始推流到服务器。请戴耳机聊天,否则音箱的声音会进入麦克风");
419 srs_publisher.publish(url, vcodec, acodec); 422 srs_publisher.publish(url, vcodec, acodec);
420 423
421 if (realtime_player) { 424 if (realtime_player) {
@@ -7,8 +7,11 @@ @@ -7,8 +7,11 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
11 <script type="text/javascript" src="js/srs.log.js"></script> 11 <script type="text/javascript" src="js/srs.log.js"></script>
  12 + <script type="text/javascript" src="js/srs.player.js"></script>
  13 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  14 + <script type="text/javascript" src="js/srs.utility.js"></script>
12 <style> 15 <style>
13 body{ 16 body{
14 padding-top: 55px; 17 padding-top: 55px;
@@ -7,8 +7,11 @@ @@ -7,8 +7,11 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
11 <script type="text/javascript" src="js/srs.log.js"></script> 11 <script type="text/javascript" src="js/srs.log.js"></script>
  12 + <script type="text/javascript" src="js/srs.player.js"></script>
  13 + <script type="text/javascript" src="js/srs.publisher.js"></script>
  14 + <script type="text/javascript" src="js/srs.utility.js"></script>
12 <style> 15 <style>
13 body{ 16 body{
14 padding-top: 55px; 17 padding-top: 55px;
@@ -7,8 +7,7 @@ @@ -7,8 +7,7 @@
7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> 7 <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
8 <script type="text/javascript" src="js/bootstrap.min.js"></script> 8 <script type="text/javascript" src="js/bootstrap.min.js"></script>
9 <script type="text/javascript" src="js/swfobject.js"></script> 9 <script type="text/javascript" src="js/swfobject.js"></script>
10 - <script type="text/javascript" src="js/srs.js"></script>  
11 - <script type="text/javascript" src="js/srs.log.js"></script> 10 + <script type="text/javascript" src="js/srs.page.js"></script>
12 <style> 11 <style>
13 body{ 12 body{
14 padding-top: 55px; 13 padding-top: 55px;