winlin

refine js for player.

@@ -5,6 +5,22 @@ @@ -5,6 +5,22 @@
5 * @param height a float value specifies the height 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, 6 * @param private_object [optional] an object that used as private object,
7 * for example, the logic chat object which owner this player. 7 * for example, the logic chat object which owner this player.
  8 +* Usage:
  9 + <script type="text/javascript" src="js/swfobject.js"></script>
  10 + <script type="text/javascript" src="js/srs.player.js"></script>
  11 + <div id="player"></div>
  12 + var p = new SrsPlayer("player", 640, 480);
  13 + p.set_srs_player_url("srs_player.swf?v=1.0.0");
  14 + p.on_player_ready = function() {
  15 + p.set_bt(0.8);
  16 + p.set_mbt(1.2);
  17 + p.play("rtmp://ossrs.net/live/livestream");
  18 + };
  19 + p.on_player_metadata = function(metadata) {
  20 + console.log(metadata);
  21 + console.log(p.dump_log());
  22 + };
  23 + p.start();
8 */ 24 */
9 function SrsPlayer(container, width, height, private_object) { 25 function SrsPlayer(container, width, height, private_object) {
10 if (!SrsPlayer.__id) { 26 if (!SrsPlayer.__id) {
@@ -63,8 +79,12 @@ function SrsPlayer(container, width, height, private_object) { @@ -63,8 +79,12 @@ function SrsPlayer(container, width, height, private_object) {
63 * user can set some callback, then start the player. 79 * user can set some callback, then start the player.
64 * @param url the default url. 80 * @param url the default url.
65 * callbacks: 81 * callbacks:
66 -* on_player_ready():int, when srs player ready, user can play. 82 +* on_player_ready():int, when srs player ready, user can play().
67 * on_player_metadata(metadata:Object):int, when srs player get metadata. 83 * on_player_metadata(metadata:Object):int, when srs player get metadata.
  84 +* methods:
  85 +* set_bt(t:Number):void, set the buffer time in seconds.
  86 +* set_mbt(t:Number):void, set the max buffer time in seconds.
  87 +* dump_log():String, get all logs of player.
68 */ 88 */
69 SrsPlayer.prototype.start = function(url) { 89 SrsPlayer.prototype.start = function(url) {
70 if (url) { 90 if (url) {
@@ -52,6 +52,68 @@ package @@ -52,6 +52,68 @@ package
52 52
53 public function play(url:String):void { 53 public function play(url:String):void {
54 this.user_url = url; 54 this.user_url = url;
  55 +
  56 + this.media_conn = new NetConnection();
  57 + this.media_conn.client = {};
  58 + this.media_conn.client.onBWDone = function():void {};
  59 + this.media_conn.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
  60 + log("NetConnection: code=" + evt.info.code);
  61 +
  62 + if (evt.info.hasOwnProperty("data") && evt.info.data) {
  63 + owner.on_player_metadata(evt.info.data);
  64 + }
  65 +
  66 + // reject by server, maybe redirect.
  67 + if (evt.info.code == "NetConnection.Connect.Rejected") {
  68 + // RTMP 302 redirect.
  69 + if (evt.info.hasOwnProperty("ex") && evt.info.ex.code == 302) {
  70 + streamName = url.substr(url.lastIndexOf("/") + 1);
  71 + url = evt.info.ex.redirect + "/" + streamName;
  72 + log("Async RTMP 302 Redirect to: " + url);
  73 +
  74 + // notify server.
  75 + media_conn.call("Redirected", null, evt.info.ex.redirect);
  76 +
  77 + // do 302.
  78 + owner.on_player_302(url);
  79 + return;
  80 + }
  81 + }
  82 +
  83 + // TODO: FIXME: failed event.
  84 + if (evt.info.code != "NetConnection.Connect.Success") {
  85 + return;
  86 + }
  87 +
  88 + media_stream = new NetStream(media_conn);
  89 + media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
  90 + log("NetStream: code=" + evt.info.code);
  91 +
  92 + if (evt.info.code == "NetStream.Video.DimensionChange") {
  93 + owner.on_player_dimension_change();
  94 + } else if (evt.info.code == "NetStream.Buffer.Empty") {
  95 + owner.on_player_buffer_empty();
  96 + } else if (evt.info.code == "NetStream.Buffer.Full") {
  97 + owner.on_player_buffer_full();
  98 + }
  99 +
  100 + // TODO: FIXME: failed event.
  101 + });
  102 +
  103 + // setup stream before play.
  104 + owner.on_player_before_play();
  105 +
  106 + if (url.indexOf("http") == 0) {
  107 + media_stream.play(url);
  108 + } else {
  109 + streamName = url.substr(url.lastIndexOf("/") + 1);
  110 + media_stream.play(streamName);
  111 + }
  112 +
  113 + owner.on_player_play();
  114 + });
  115 +
  116 + this.media_conn.connect(null);
55 } 117 }
56 118
57 public function close():void { 119 public function close():void {
@@ -14,14 +14,20 @@ package @@ -14,14 +14,20 @@ package
14 public static var logData:String = ""; 14 public static var logData:String = "";
15 15
16 /** 16 /**
17 - * initialize the player by flashvars for config.  
18 - * @param flashvars the config. 17 + * whether string s endswith f.
19 */ 18 */
20 public static function stringEndswith(s:String, f:String):Boolean { 19 public static function stringEndswith(s:String, f:String):Boolean {
21 return s && f && s.indexOf(f) == s.length - f.length; 20 return s && f && s.indexOf(f) == s.length - f.length;
22 } 21 }
23 22
24 /** 23 /**
  24 + * whether string s startswith f.
  25 + */
  26 + public static function stringStartswith(s:String, f:String):Boolean {
  27 + return s && f && s.indexOf(f) == 0;
  28 + }
  29 +
  30 + /**
25 * write log to trace and console.log. 31 * write log to trace and console.log.
26 * @param msg the log message. 32 * @param msg the log message.
27 */ 33 */
@@ -429,7 +429,7 @@ package @@ -429,7 +429,7 @@ package
429 js_call_stop(); 429 js_call_stop();
430 430
431 // create player. 431 // create player.
432 - if (Utility.stringEndswith(url, ".m3u8")) { 432 + if (Utility.stringEndswith(url, ".m3u8") && Utility.stringStartswith(url, "http://")) {
433 player = new M3u8Player(this); 433 player = new M3u8Player(this);
434 log("create M3U8 player."); 434 log("create M3U8 player.");
435 } else { 435 } else {