winlin

refine bandwidth test client, provides js and as library

@@ -27,6 +27,8 @@ function SrsBandwidth(container, width, height, private_object) { @@ -27,6 +27,8 @@ function SrsBandwidth(container, width, height, private_object) {
27 // the callback set data. 27 // the callback set data.
28 this.percent = 0; 28 this.percent = 0;
29 this.status = ""; 29 this.status = "";
  30 + this.report = "";
  31 + this.server = "";
30 } 32 }
31 /** 33 /**
32 * user can set some callback, then start the bandwidth. 34 * user can set some callback, then start the bandwidth.
@@ -38,7 +40,7 @@ function SrsBandwidth(container, width, height, private_object) { @@ -38,7 +40,7 @@ function SrsBandwidth(container, width, height, private_object) {
38 * on_update_status(status:String):void, when srs bandwidth update the status. 40 * on_update_status(status:String):void, when srs bandwidth update the status.
39 * status:String the human readable status text. 41 * status:String the human readable status text.
40 */ 42 */
41 -SrsBandwidth.prototype.start = function(url) { 43 +SrsBandwidth.prototype.render = function(url) {
42 if (url) { 44 if (url) {
43 this.stream_url = url; 45 this.stream_url = url;
44 } 46 }
@@ -49,6 +51,8 @@ SrsBandwidth.prototype.start = function(url) { @@ -49,6 +51,8 @@ SrsBandwidth.prototype.start = function(url) {
49 flashvars.on_bandwidth_ready = "__srs_on_bandwidth_ready"; 51 flashvars.on_bandwidth_ready = "__srs_on_bandwidth_ready";
50 flashvars.on_update_progress = "__srs_on_update_progress"; 52 flashvars.on_update_progress = "__srs_on_update_progress";
51 flashvars.on_update_status = "__srs_on_update_status"; 53 flashvars.on_update_status = "__srs_on_update_status";
  54 + flashvars.on_srs_info = "__srs_on_srs_info";
  55 + flashvars.on_complete = "__srs_on_complete";
52 56
53 var params = {}; 57 var params = {};
54 params.wmode = "opaque"; 58 params.wmode = "opaque";
@@ -75,7 +79,6 @@ SrsBandwidth.prototype.start = function(url) { @@ -75,7 +79,6 @@ SrsBandwidth.prototype.start = function(url) {
75 /** 79 /**
76 * play the stream. 80 * play the stream.
77 * @param stream_url the url of stream, rtmp or http. 81 * @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 */ 82 */
80 SrsBandwidth.prototype.check_bandwidth = function(url) { 83 SrsBandwidth.prototype.check_bandwidth = function(url) {
81 this.stop(); 84 this.stop();
@@ -107,6 +110,10 @@ SrsBandwidth.prototype.on_update_progress = function(percent) { @@ -107,6 +110,10 @@ SrsBandwidth.prototype.on_update_progress = function(percent) {
107 } 110 }
108 SrsBandwidth.prototype.on_update_status = function(status) { 111 SrsBandwidth.prototype.on_update_status = function(status) {
109 } 112 }
  113 +SrsBandwidth.prototype.on_srs_info = function(srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip) {
  114 +}
  115 +SrsBandwidth.prototype.on_complete = function(start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time) {
  116 +}
110 function __srs_find_bandwidth(id) { 117 function __srs_find_bandwidth(id) {
111 for (var i = 0; i < SrsBandwidth.__bandwidths.length; i++) { 118 for (var i = 0; i < SrsBandwidth.__bandwidths.length; i++) {
112 var bandwidth = SrsBandwidth.__bandwidths[i]; 119 var bandwidth = SrsBandwidth.__bandwidths[i];
@@ -129,8 +136,52 @@ function __srs_on_update_progress(id, percent) { @@ -129,8 +136,52 @@ function __srs_on_update_progress(id, percent) {
129 bandwidth.percent = percent; 136 bandwidth.percent = percent;
130 bandwidth.on_update_progress(percent); 137 bandwidth.on_update_progress(percent);
131 } 138 }
132 -function __srs_on_update_status(id, status) { 139 +function __srs_on_update_status(id, code, data) {
133 var bandwidth = __srs_find_bandwidth(id); 140 var bandwidth = __srs_find_bandwidth(id);
  141 +
  142 + var status = "";
  143 + switch(code){
  144 + case "NetConnection.Connect.Failed":
  145 + status = "连接服务器失败!";
  146 + break;
  147 + case "NetConnection.Connect.Rejected":
  148 + status = "服务器拒绝连接!";
  149 + break;
  150 + case "NetConnection.Connect.Success":
  151 + status = "连接服务器成功!";
  152 + break;
  153 + case "NetConnection.Connect.Closed":
  154 + if (bandwidth.report) {
  155 + return;
  156 + }
  157 + status = "连接已断开!";
  158 + break;
  159 + case "srs.bwtc.play.start":
  160 + status = "开始测试下行带宽";
  161 + break;
  162 + case "srs.bwtc.play.stop":
  163 + status = "下行带宽测试完毕," + data + "kbps,开始测试上行带宽。";
  164 + break;
  165 + default:
  166 + return;
  167 + }
  168 +
134 bandwidth.status = status; 169 bandwidth.status = status;
135 bandwidth.on_update_status(status); 170 bandwidth.on_update_status(status);
  171 +}
  172 +function __srs_on_srs_info(id, srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip) {
  173 + var bandwidth = __srs_find_bandwidth(id);
  174 + bandwidth.status = status;
  175 + bandwidth.server = srs_server_ip;
  176 + bandwidth.on_srs_info(srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip);
  177 +}
  178 +function __srs_on_complete(id, start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time) {
  179 + var bandwidth = __srs_find_bandwidth(id);
  180 +
  181 + var status = "检测结束: " + bandwidth.server + " 上行: " + publish_kbps + " kbps" + " 下行: " + play_kbps + " kbps"
  182 + + " 测试时间: " + Number((end_time - start_time) / 1000).toFixed(1) + " 秒";
  183 + bandwidth.report = status;
  184 + bandwidth.on_update_status(status);
  185 +
  186 + bandwidth.on_complete(start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time);
136 } 187 }
@@ -35,6 +35,7 @@ @@ -35,6 +35,7 @@
35 35
36 function on_click_play() { 36 function on_click_play() {
37 $("#check_status").text(""); 37 $("#check_status").text("");
  38 + $("#check_info").text("");
38 $("#progress_bar").width("0%"); 39 $("#progress_bar").width("0%");
39 $("#main_modal").modal({show:true, keyboard:false}); 40 $("#main_modal").modal({show:true, keyboard:false});
40 } 41 }
@@ -61,7 +62,13 @@ @@ -61,7 +62,13 @@
61 bandwidth.on_update_status = function(status) { 62 bandwidth.on_update_status = function(status) {
62 $("#check_status").text(status); 63 $("#check_status").text(status);
63 } 64 }
64 - bandwidth.start(url); 65 + bandwidth.on_srs_info = function(srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip) {
  66 + $("#check_info").text(
  67 + "server:" + srs_server + ", authors:" + srs_primary_authors +
  68 + ", srs_id:" + srs_id + ", srs_pid:" + srs_pid + ", ip:" + srs_server_ip
  69 + );
  70 + }
  71 + bandwidth.render(url);
65 } 72 }
66 function on_stop_bandwidth_test() { 73 function on_stop_bandwidth_test() {
67 bandwidth.stop(); 74 bandwidth.stop();
@@ -112,7 +119,8 @@ @@ -112,7 +119,8 @@
112 </div> 119 </div>
113 <div class="span1"></div> 120 <div class="span1"></div>
114 </div> 121 </div>
115 - <span id="check_status">status</span> 122 + <div id="check_status">status</div>
  123 + <div id="check_info">info</div>
116 </div> 124 </div>
117 <div class="modal-footer"> 125 <div class="modal-footer">
118 <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 关闭 </button> 126 <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 关闭 </button>
1 -package SrsClass  
2 -{  
3 - import flash.system.System;  
4 -  
5 - public class SrsElapsedTimer  
6 - {  
7 - private var beginDate:Date;  
8 - public function SrsElapsedTimer()  
9 - {  
10 - beginDate = new Date;  
11 - }  
12 -  
13 - public function elapsed():Number{  
14 - var endDate:Date = new Date;  
15 -  
16 - // get deiff by ms  
17 - return (endDate.time - beginDate.time);  
18 - }  
19 -  
20 - public function restart():void{  
21 - beginDate = new Date;  
22 - }  
23 - }  
24 -}  
1 -package SrsClass  
2 -{  
3 - import flash.net.SharedObject;  
4 -  
5 - public class SrsSettings  
6 - {  
7 - private var settings:SharedObject;  
8 - private var key:String = "SrsBandCheck";  
9 -  
10 - public function SrsSettings()  
11 - {  
12 - settings = SharedObject.getLocal(key);  
13 - }  
14 -  
15 - public function addAddressText(val:String):void{  
16 - settings.data.address_text = val;  
17 - }  
18 -  
19 - public function addressText():String{  
20 - return settings.data.address_text;  
21 - }  
22 -  
23 - static public function instance():SrsSettings{  
24 - return new SrsSettings;  
25 - }  
26 - }  
27 -}  
  1 +package srs
  2 +{
  3 + import flash.events.NetStatusEvent;
  4 + import flash.external.ExternalInterface;
  5 + import flash.net.NetConnection;
  6 + import flash.net.ObjectEncoding;
  7 + import flash.utils.clearTimeout;
  8 + import flash.utils.setTimeout;
  9 +
  10 + /**
  11 + * SRS bandwidth check/test library,
  12 + * user can copy this file and use it directly,
  13 + * this library will export as callback functions, and js callback functions.
  14 + *
  15 + * Usage:
  16 + * var bandwidth:SrsBandwidth = new SrsBandwidth();
  17 + * bandwidth.initialize(......); // required
  18 + * bandwidth.check_bandwidth(......); // required
  19 + * bandwidth.stop(); // optional
  20 + *
  21 + * @remark we donot use event, but use callback functions set by initialize.
  22 + */
  23 + public class SrsBandwidth
  24 + {
  25 + /**
  26 + * server notice client to do the downloading/play bandwidth test.
  27 + */
  28 + public static const StatusSrsBwtcPlayStart:String = "srs.bwtc.play.start";
  29 + /**
  30 + * server notice client to complete the downloading/play bandwidth test.
  31 + */
  32 + public static const StatusSrsBwtcPlayStop:String = "srs.bwtc.play.stop";
  33 + /**
  34 + * server notice client to do the uploading/publish bandwidth test.
  35 + */
  36 + public static const StatusSrsBwtcPublishStart:String = "srs.bwtc.publish.start";
  37 + /**
  38 + * server notice client to complete the uploading/publish bandwidth test.
  39 + */
  40 + public static const StatusSrsBwtcPublishStop:String = "srs.bwtc.publish.stop";
  41 +
  42 + /**
  43 + * constructor, do nothing
  44 + */
  45 + public function SrsBandwidth()
  46 + {
  47 + }
  48 +
  49 + /**
  50 + * initialize the bandwidth test tool, the callbacks. null to ignore.
  51 + *
  52 + * the as callbacks.
  53 + * @param as_on_ready, function():void, callback when bandwidth tool is ready to run.
  54 + * @param as_on_status_change, function(code:String, data:String):void, where:
  55 + * code can be:
  56 + * "NetConnection.Connect.Failed", see NetStatusEvent(evt.info.code).
  57 + * "NetConnection.Connect.Rejected", see NetStatusEvent(evt.info.code).
  58 + * "NetConnection.Connect.Success", see NetStatusEvent(evt.info.code).
  59 + * "NetConnection.Connect.Closed", see NetStatusEvent(evt.info.code).
  60 + * SrsBandwidth.StatusSrsBwtcPlayStart, "srs.bwtc.play.start", when srs start test play bandwidth.
  61 + * SrsBandwidth.StatusSrsBwtcPlayStop, "srs.bwtc.play.stop", when srs complete test play bandwidth.
  62 + * SrsBandwidth.StatusSrsBwtcPublishStart, "srs.bwtc.publish.start", when srs start test publish bandwidth.
  63 + * SrsBandwidth.StatusSrsBwtcPublishStop, "srs.bwtc.publish.stop", when srs complete test publish bandwidth.
  64 + * data is extra parameter:
  65 + * kbps, for code is SrsBandwidth.StatusSrsBwtcPlayStop or SrsBandwidth.StatusSrsBwtcPublishStop.
  66 + * "", otherwise empty string.
  67 + * @param as_on_progress_change, function(percent:Number):void, where:
  68 + * percent, the progress percent, 0 means 0%, 100 means 100%.
  69 + * @param as_on_srs_info, function(srs_server:String, srs_primary_authors:String, srs_id:String, srs_pid:String, srs_server_ip:String):void, where:
  70 + * srs_server: the srs server info.
  71 + * srs_primary_authors: the srs version info.
  72 + * srs_id: the tracable log id, to direclty grep the log..
  73 + * srs_pid: the srs process id, to direclty grep the log.
  74 + * srs_server_ip: the srs server ip, where client connected at.
  75 + * @param as_on_complete, function(start_time:Number, end_time:Number, play_kbps:Number, publish_kbps:Number, play_bytes:Number, publish_bytes:Number, play_time:Number, publish_time:Number):void, where
  76 + * start_time, the start timestamp, in ms.
  77 + * end_time, the finish timestamp, in ms.
  78 + * play_kbps, the play/downloading kbps.
  79 + * publish_kbps, the publish/uploading kbps.
  80 + * play_bytes, the bytes play/download from server, in bytes.
  81 + * publish_bytes, the bytes publish/upload to server, in bytes.
  82 + * play_time, the play/download duration time, in ms.
  83 + * publish_time, the publish/upload duration time, in ms.
  84 + *
  85 + * the js callback id.
  86 + * @param js_id, specifies the id of swfobject, used to identify the bandwidth object.
  87 + * for all js callback, the first param always be the js_id, to identify the callback object.
  88 + *
  89 + * the js callbacks.
  90 + * @param js_on_ready, function(js_id:String):void, callback when bandwidth tool is ready to run.
  91 + * @param js_on_status_change, function(js_id:String, code:String, data:String):void
  92 + * @param as_on_progress_change, function(js_id:String, percent:Number):void
  93 + * @param as_on_srs_info, function(js_id:String, srs_server:String, srs_primary_authors:String, srs_id:String, srs_pid:String, srs_server_ip:String):void
  94 + * @param as_on_complete, function(js_id:String, start_time:Number, end_time:Number, play_kbps:Number, publish_kbps:Number, play_bytes:Number, publish_bytes:Number, play_time:Number, publish_time:Number):void
  95 + *
  96 + * the js export functions.
  97 + * @param js_export_check_bandwidth, function(url:String):void, for js to start bandwidth check, @see: check_bandwidth(url:String):void
  98 + * @param js_export_stop, function():void, for js to stop bandwidth check, @see: stop():void
  99 + *
  100 + * @remark, all parameters can be null.
  101 + * @remark, as and js callback use same parameter, except that the js calblack first parameter is js_id:String.
  102 + */
  103 + public function initialize(
  104 + as_on_ready:Function, as_on_status_change:Function, as_on_progress_change:Function, as_on_srs_info:Function, as_on_complete:Function,
  105 + js_id:String, js_on_ready:String, js_on_status_change:String, js_on_progress_change:String, js_on_srs_info:String, js_on_complete:String,
  106 + js_export_check_bandwidth:String, js_export_stop:String
  107 + ):void {
  108 + this.as_on_ready = as_on_ready;
  109 + this.as_on_srs_info = as_on_srs_info;
  110 + this.as_on_status_change = as_on_status_change;
  111 + this.as_on_progress_change = as_on_progress_change;
  112 + this.as_on_complete = as_on_complete;
  113 +
  114 + this.js_id = js_id;
  115 + this.js_on_srs_info = js_on_srs_info;
  116 + this.js_on_ready = js_on_ready;
  117 + this.js_on_status_change = js_on_status_change;
  118 + this.js_on_progress_change = js_on_progress_change;
  119 + this.js_on_complete = js_on_complete;
  120 +
  121 + this.js_export_check_bandwidth = js_export_check_bandwidth;
  122 + this.js_export_stop = js_export_stop;
  123 +
  124 + flash.utils.setTimeout(this.system_on_js_ready, 0);
  125 + }
  126 +
  127 + /**
  128 + * start check bandwidth.
  129 + * @param url, a String indicates the url to check bandwidth,
  130 + * format as: rtmp://server:port/app?key=xxx&&vhost=xxx
  131 + * for example, rtmp://dev:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com
  132 + * where the key and vhost must be config in SRS, like:
  133 + * vhost bandcheck.srs.com {
  134 + * enabled on;
  135 + * chunk_size 65000;
  136 + * bandcheck {
  137 + * enabled on;
  138 + * key "35c9b402c12a7246868752e2878f7e0e";
  139 + * interval 30;
  140 + * limit_kbps 4000;
  141 + * }
  142 + * }
  143 + *
  144 + * @remark user must invoke this as method, or js exported method.
  145 + */
  146 + public function check_bandwidth(url:String):void {
  147 + this.js_call_check_bandwidth(url);
  148 + }
  149 +
  150 + /**
  151 + * stop check bancwidth.
  152 + * @remark it's optional, however, user can abort the bandwidth check.
  153 + */
  154 + public function stop():void {
  155 + this.js_call_stop();
  156 + }
  157 +
  158 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  165 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  168 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  169 + ////////////////////////// ///////////////////////////////
  170 + ////////////////////////// ///////////////////////////////
  171 + ////////////////////////// ///////////////////////////////
  172 + ////////////////////////// ///////////////////////////////
  173 + ////////////////////////// Private Section, ignore please. ///////////////////////////////
  174 + ////////////////////////// ///////////////////////////////
  175 + ////////////////////////// ///////////////////////////////
  176 + ////////////////////////// ///////////////////////////////
  177 + ////////////////////////// ///////////////////////////////
  178 + ////////////////////////// ///////////////////////////////
  179 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  180 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  184 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  185 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  186 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  187 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  188 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  189 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  190 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  191 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  193 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  194 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  195 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  196 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  198 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  200 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  202 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  206 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  208 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  210 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  213 +
  214 + /**
  215 + * ***********************************************************************
  216 + * private section, including private fields, method and embeded classes.
  217 + * ***********************************************************************
  218 + */
  219 +
  220 + /**
  221 + * as callback.
  222 + */
  223 + private var as_on_ready:Function;
  224 + private var as_on_srs_info:Function;
  225 + private var as_on_status_change:Function;
  226 + private var as_on_progress_change:Function;
  227 + private var as_on_complete:Function;
  228 +
  229 + /**
  230 + * js callback.
  231 + */
  232 + private var js_id:String;
  233 + private var js_on_ready:String;
  234 + private var js_on_srs_info:String;
  235 + private var js_on_status_change:String;
  236 + private var js_on_progress_change:String;
  237 + private var js_on_complete:String;
  238 +
  239 + /**
  240 + * js export functions.
  241 + */
  242 + private var js_export_check_bandwidth:String;
  243 + private var js_export_stop:String;
  244 +
  245 + /**
  246 + * srs debug infos
  247 + */
  248 + private var srs_server:String = null;
  249 + private var srs_primary_authors:String = null;
  250 + private var srs_id:String = null;
  251 + private var srs_pid:String = null;
  252 + private var srs_server_ip:String = null;
  253 +
  254 + /**
  255 + * the underlayer connection, to send call message to do the bandwidth
  256 + * check/test with server.
  257 + */
  258 + private var connection:NetConnection = null;
  259 +
  260 + /**
  261 + * use timeout to sendout publish call packets.
  262 + * when got stop publish packet from server, stop publish call loop.
  263 + */
  264 + private var publish_timeout_handler:uint = 0;
  265 +
  266 + /**
  267 + * system callack event, when js ready, register callback for js.
  268 + * the actual main function.
  269 + */
  270 + private function system_on_js_ready():void {
  271 + if (!flash.external.ExternalInterface.available) {
  272 + trace("js not ready, try later.");
  273 + flash.utils.setTimeout(this.system_on_js_ready, 100);
  274 + return;
  275 + }
  276 +
  277 + if (this.js_export_check_bandwidth != null) {
  278 + flash.external.ExternalInterface.addCallback(this.js_export_check_bandwidth, this.js_call_check_bandwidth);
  279 + }
  280 + if (this.js_export_stop != null) {
  281 + flash.external.ExternalInterface.addCallback(this.js_export_stop, this.js_call_stop);
  282 + }
  283 +
  284 + if (as_on_ready != null) {
  285 + as_on_ready();
  286 + }
  287 + if (js_on_ready != null) {
  288 + flash.external.ExternalInterface.call(this.js_on_ready, this.js_id);
  289 + }
  290 + }
  291 + private function js_call_check_bandwidth(url:String):void {
  292 + js_call_stop();
  293 +
  294 + __on_progress_change(0);
  295 +
  296 + // init connection
  297 + connection = new NetConnection;
  298 + connection.objectEncoding = ObjectEncoding.AMF0;
  299 + connection.client = {
  300 + onStatus: onStatus,
  301 + // play
  302 + onSrsBandCheckStartPlayBytes: onSrsBandCheckStartPlayBytes,
  303 + onSrsBandCheckPlaying: onSrsBandCheckPlaying,
  304 + onSrsBandCheckStopPlayBytes: onSrsBandCheckStopPlayBytes,
  305 + // publish
  306 + onSrsBandCheckStartPublishBytes: onSrsBandCheckStartPublishBytes,
  307 + onSrsBandCheckStopPublishBytes: onSrsBandCheckStopPublishBytes,
  308 + onSrsBandCheckFinished: onSrsBandCheckFinished
  309 + };
  310 + connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
  311 + connection.connect(url);
  312 +
  313 + __on_progress_change(3);
  314 + }
  315 + private function js_call_stop():void {
  316 + if (connection) {
  317 + connection.close();
  318 + connection = null;
  319 + }
  320 + }
  321 +
  322 + /**
  323 + * NetConnection callback this function, when recv server call "onSrsBandCheckStartPlayBytes"
  324 + * then start @updatePlayProgressTimer for updating the progressbar
  325 + * */
  326 + private function onSrsBandCheckStartPlayBytes(evt:Object):void{
  327 + var duration_ms:Number = evt.duration_ms;
  328 + var interval_ms:Number = evt.interval_ms;
  329 + trace("start play test, duration=" + duration_ms + ", interval=" + interval_ms);
  330 +
  331 + connection.call("onSrsBandCheckStartingPlayBytes", null);
  332 + __on_status_change(SrsBandwidth.StatusSrsBwtcPlayStart);
  333 +
  334 + __on_progress_change(10);
  335 + }
  336 + private function onSrsBandCheckPlaying(evt:Object):void{
  337 + }
  338 + private function onSrsBandCheckStopPlayBytes(evt:Object):void{
  339 + var duration_ms:Number = evt.duration_ms;
  340 + var interval_ms:Number = evt.interval_ms;
  341 + var duration_delta:Number = evt.duration_delta;
  342 + var bytes_delta:Number = evt.bytes_delta;
  343 +
  344 + var kbps:Number = 0;
  345 + if(duration_delta > 0){
  346 + kbps = bytes_delta * 8.0 / duration_delta; // b/ms == kbps
  347 + }
  348 + kbps = (int(kbps * 10))/10.0;
  349 +
  350 + flash.utils.setTimeout(stopPlayTest, 0);
  351 + __on_status_change(SrsBandwidth.StatusSrsBwtcPlayStop, String(kbps));
  352 +
  353 + __on_progress_change(40);
  354 + }
  355 + private function stopPlayTest():void{
  356 + connection.call("onSrsBandCheckStoppedPlayBytes", null);
  357 + }
  358 + /**
  359 + * publishing methods.
  360 + */
  361 + private function onSrsBandCheckStartPublishBytes(evt:Object):void{
  362 + var duration_ms:Number = evt.duration_ms;
  363 + var interval_ms:Number = evt.interval_ms;
  364 +
  365 + connection.call("onSrsBandCheckStartingPublishBytes", null);
  366 +
  367 + flash.utils.setTimeout(publisher, 0);
  368 + __on_status_change(SrsBandwidth.StatusSrsBwtcPublishStart);
  369 +
  370 + __on_progress_change(60);
  371 + }
  372 + private function publisher():void{
  373 + var data:Array = new Array();
  374 +
  375 + var data_size:int = 100;
  376 + for(var i:int; i < data_size; i++) {
  377 + data.push("SrS band check data from client's publishing......");
  378 + }
  379 +
  380 + connection.call("onSrsBandCheckPublishing", null, data);
  381 +
  382 + publish_timeout_handler = flash.utils.setTimeout(publisher, 0);
  383 + }
  384 + private function onSrsBandCheckStopPublishBytes(evt:Object):void{
  385 + var duration_ms:Number = evt.duration_ms;
  386 + var interval_ms:Number = evt.interval_ms;
  387 + var duration_delta:Number = evt.duration_delta;
  388 + var bytes_delta:Number = evt.bytes_delta;
  389 +
  390 + var kbps:Number = 0;
  391 + if(duration_delta > 0){
  392 + kbps = bytes_delta * 8.0 / duration_delta; // b/ms == kbps
  393 + }
  394 + kbps = (int(kbps * 10))/10.0;
  395 +
  396 + stopPublishTest();
  397 +
  398 + __on_progress_change(90);
  399 + }
  400 + private function stopPublishTest():void{
  401 + // the stop publish response packet can not send out, for the queue is full.
  402 + //connection.call("onSrsBandCheckStoppedPublishBytes", null);
  403 +
  404 + // clear the timeout to stop the send loop.
  405 + if (publish_timeout_handler > 0) {
  406 + flash.utils.clearTimeout(publish_timeout_handler);
  407 + publish_timeout_handler = 0;
  408 + }
  409 + }
  410 + private function onSrsBandCheckFinished(evt:Object):void{
  411 + var code:Number = evt.code;
  412 + var start_time:Number = evt.start_time;
  413 + var end_time:Number = evt.end_time;
  414 + var play_kbps:Number = evt.play_kbps;
  415 + var publish_kbps:Number = evt.publish_kbps;
  416 + var play_bytes:Number = evt.play_bytes;
  417 + var play_time:Number = evt.play_time;
  418 + var publish_bytes:Number = evt.publish_bytes;
  419 + var publish_time:Number = evt.publish_time;
  420 +
  421 + if (this.as_on_complete != null) {
  422 + this.as_on_complete(start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time);
  423 + }
  424 + if (this.js_on_complete != null) {
  425 + flash.external.ExternalInterface.call(this.js_on_complete, this.js_id,
  426 + start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time);
  427 + }
  428 +
  429 + __on_progress_change(100);
  430 +
  431 + // when got finish packet, directly close connection.
  432 + js_call_stop();
  433 +
  434 + // the last final packet can not send out, for the queue is full.
  435 + //connection.call("finalClientPacket", null);
  436 + }
  437 +
  438 + /**
  439 + * get NetConnection NetStatusEvent
  440 + */
  441 + private function onStatus(evt:NetStatusEvent): void {
  442 + trace(evt.info.code);
  443 +
  444 + if (evt.info.hasOwnProperty("data") && evt.info.data) {
  445 + if (evt.info.data.hasOwnProperty("srs_server")) {
  446 + srs_server = evt.info.data.srs_server;
  447 + }
  448 + if (evt.info.data.hasOwnProperty("srs_primary_authors")) {
  449 + srs_primary_authors = evt.info.data.srs_primary_authors;
  450 + }
  451 + if (evt.info.data.hasOwnProperty("srs_id")) {
  452 + srs_id = evt.info.data.srs_id;
  453 + }
  454 + if (evt.info.data.hasOwnProperty("srs_pid")) {
  455 + srs_pid = evt.info.data.srs_pid;
  456 + }
  457 + if (evt.info.data.hasOwnProperty("srs_server_ip")) {
  458 + srs_server_ip = evt.info.data.srs_server_ip;
  459 + }
  460 +
  461 + if (this.as_on_srs_info != null) {
  462 + this.as_on_srs_info(srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip);
  463 + }
  464 + if (this.js_on_srs_info != null) {
  465 + flash.external.ExternalInterface.call(this.js_on_srs_info, this.js_id,
  466 + srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip);
  467 + }
  468 + }
  469 + if (evt.info.code) {
  470 + __on_status_change(evt.info.code);
  471 + }
  472 + switch(evt.info.code){
  473 + case "NetConnection.Connect.Success":
  474 + __on_progress_change(8);
  475 + break;
  476 + }
  477 +
  478 + }
  479 +
  480 + /**
  481 + * invoke the callback.
  482 + */
  483 + private function __on_progress_change(percent:Number):void {
  484 + if (this.as_on_progress_change != null) {
  485 + this.as_on_progress_change(percent);
  486 + }
  487 + if (this.js_on_progress_change != null) {
  488 + flash.external.ExternalInterface.call(this.js_on_progress_change, this.js_id,
  489 + percent);
  490 + }
  491 + }
  492 + private function __on_status_change(code:String, data:String=""):void {
  493 + if (this.as_on_status_change != null) {
  494 + this.as_on_status_change(code, data);
  495 + }
  496 + if (this.js_on_status_change != null) {
  497 + flash.external.ExternalInterface.call(this.js_on_status_change, this.js_id,
  498 + code, data);
  499 + }
  500 + }
  501 + }
  502 +}
1 package 1 package
2 { 2 {
3 - import SrsClass.SrsElapsedTimer;  
4 -  
5 import flash.display.LoaderInfo; 3 import flash.display.LoaderInfo;
6 import flash.display.Sprite; 4 import flash.display.Sprite;
7 import flash.display.StageAlign; 5 import flash.display.StageAlign;
@@ -17,35 +15,20 @@ package @@ -17,35 +15,20 @@ package
17 import flash.ui.ContextMenuItem; 15 import flash.ui.ContextMenuItem;
18 import flash.utils.Timer; 16 import flash.utils.Timer;
19 import flash.utils.setTimeout; 17 import flash.utils.setTimeout;
  18 +
  19 + import srs.SrsBandwidth;
20 20
21 public class srs_bwt extends Sprite 21 public class srs_bwt extends Sprite
22 - {  
23 - private var connection:NetConnection = null;  
24 -  
25 - private var updatePlayProgressTimer:Timer = null;  
26 - private var elapTimer:SrsElapsedTimer = null; 22 + {
  23 + /**
  24 + * the SRS bandwidth check/test library object.
  25 + */
  26 + private var bandwidth:SrsBandwidth = new SrsBandwidth();
27 27
28 - // user set id.  
29 - private var js_id:String = null;  
30 - // play param url.  
31 - private var user_url:String = null;  
32 -  
33 - // server ip get from server  
34 - private var server_ip:String;  
35 -  
36 - // test wheth publish should to stop  
37 - private var stop_pub:Boolean = false;  
38 -  
39 - // js interface  
40 - private var js_on_player_ready:String;  
41 - private var js_update_progress:String;  
42 - private var js_update_status:String;  
43 -  
44 - private var value_progressbar:Number = 0;  
45 - private var max_progressbar:Number = 0;  
46 -  
47 - // set NetConnection ObjectEncoding to AMF0  
48 - NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0; 28 + /**
  29 + * when not specifies any param, directly run the swf.
  30 + */
  31 + private var default_url:String = "rtmp://dev:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com";
49 32
50 public function srs_bwt() 33 public function srs_bwt()
51 { 34 {
@@ -55,91 +38,81 @@ package @@ -55,91 +38,81 @@ package
55 this.system_on_add_to_stage(null); 38 this.system_on_add_to_stage(null);
56 } 39 }
57 } 40 }
58 -  
59 - /**  
60 - * system event callback, when this control added to stage.  
61 - * the main function.  
62 - */  
63 private function system_on_add_to_stage(evt:Event):void { 41 private function system_on_add_to_stage(evt:Event):void {
64 this.stage.scaleMode = StageScaleMode.NO_SCALE; 42 this.stage.scaleMode = StageScaleMode.NO_SCALE;
65 this.stage.align = StageAlign.TOP_LEFT; 43 this.stage.align = StageAlign.TOP_LEFT;
66 -  
67 - var flashvars:Object = this.root.loaderInfo.parameters;  
68 44
69 - if (!flashvars.hasOwnProperty("id")) {  
70 - throw new Error("must specifies the id");  
71 - } 45 + // init context menu
  46 + var myMenu:ContextMenu = new ContextMenu();
  47 + myMenu.hideBuiltInItems();
  48 + myMenu.customItems.push(new ContextMenuItem("SRS带宽测试工具", true));
  49 + this.contextMenu = myMenu;
72 50
73 - this.js_id = flashvars.id;  
74 - this.js_on_player_ready = flashvars.on_bandwidth_ready;  
75 - this.js_update_progress = flashvars.on_update_progress;  
76 - this.js_update_status = flashvars.on_update_status;  
77 -  
78 - // init context menu  
79 - var myMenu:ContextMenu = new ContextMenu();  
80 - myMenu.hideBuiltInItems();  
81 - myMenu.customItems.push(new ContextMenuItem("SRS 带宽测试工具", true));  
82 - this.contextMenu = myMenu;  
83 -  
84 - flash.utils.setTimeout(this.system_on_js_ready, 0); 51 + check_bandwidth();
85 } 52 }
86 53
87 - /**  
88 - * system callack event, when js ready, register callback for js.  
89 - * the actual main function.  
90 - */  
91 - private function system_on_js_ready():void {  
92 - if (!flash.external.ExternalInterface.available) {  
93 - trace("js not ready, try later.");  
94 - flash.utils.setTimeout(this.system_on_js_ready, 100);  
95 - return;  
96 - } 54 + private function check_bandwidth():void {
  55 + // closure
  56 + var self:srs_bwt = this;
  57 +
  58 + /////////////////////////////////////////////////////////////////////
  59 + // initialize the bandwidth check/test library
  60 + /////////////////////////////////////////////////////////////////////
  61 + // js callback, set to null if ignore.
  62 + var conf:Object = this.root.loaderInfo.parameters;
  63 + var js_id:String = conf.id? conf.id:null;
  64 + var js_on_ready:String = conf.on_bandwidth_ready? conf.on_bandwidth_ready:null;
  65 + var js_on_srs_info:String = conf.on_srs_info? conf.on_srs_info:null;
  66 + var js_on_progress_change:String = conf.on_update_progress? conf.on_update_progress:null;
  67 + var js_on_status_change:String = conf.on_update_status? conf.on_update_status:null;
  68 + var js_on_complete:String = conf.on_complete? conf.on_complete:null;
97 69
98 - flash.external.ExternalInterface.addCallback("__check_bandwidth", this.js_call_check_bandwidth);  
99 - flash.external.ExternalInterface.addCallback("__stop", this.js_call_stop); 70 + // js export, set to null to disable
  71 + var js_export_check_bandwidth:String = "__check_bandwidth";
  72 + var js_export_stop:String = "__stop";
100 73
101 - flash.external.ExternalInterface.call(this.js_on_player_ready, this.js_id); 74 + // as callback, set to null if ignore.
  75 + var as_on_ready:Function = function():void {
  76 + self.on_ready();
  77 + };
  78 + var as_on_status_change:Function = function(code:String, data:String):void {
  79 + self.on_status_change(code, data);
  80 + };
  81 + var as_on_progress_change:Function = function(percent:Number):void {
  82 + self.on_progress(percent);
  83 + };
  84 + var as_on_srs_info:Function = function(srs_server:String, srs_primary_authors:String, srs_id:String, srs_pid:String, srs_server_ip:String):void {
  85 + self.update_context_items(srs_server, srs_primary_authors, srs_id, srs_pid, srs_server_ip);
  86 + };
  87 + var as_on_complete:Function = function(start_time:Number, end_time:Number, play_kbps:Number, publish_kbps:Number, play_bytes:Number, publish_bytes:Number, play_time:Number, publish_time:Number):void {
  88 + self.on_complete(start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time);
  89 + };
  90 +
  91 + bandwidth.initialize(
  92 + as_on_ready, as_on_status_change, as_on_progress_change, as_on_srs_info, as_on_complete,
  93 + js_id, js_on_ready, js_on_status_change, js_on_progress_change, js_on_srs_info, js_on_complete,
  94 + js_export_check_bandwidth, js_export_stop
  95 + );
  96 + /////////////////////////////////////////////////////////////////////
102 } 97 }
103 -  
104 - private function js_call_check_bandwidth(url:String):void {  
105 - js_call_stop(); 98 +
  99 + private function on_ready():void {
  100 + var conf:Object = this.root.loaderInfo.parameters;
106 101
107 - // init connection  
108 - connection = new NetConnection;  
109 - connection.client = this;  
110 - connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);  
111 - connection.connect(url);  
112 - //connection.connect("rtmp://192.168.8.234:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com");  
113 -  
114 - // for play to update progress bar  
115 - elapTimer = new SrsElapsedTimer;  
116 -  
117 - // we suppose the check time = 7 S  
118 - updatePlayProgressTimer = new Timer(100);  
119 - updatePlayProgressTimer.addEventListener(TimerEvent.TIMER, onTimerTimeout);  
120 - updatePlayProgressTimer.start();  
121 - }  
122 - private function js_call_stop():void {  
123 - if (connection) {  
124 - connection.close();  
125 - connection = null;  
126 - }  
127 - if (updatePlayProgressTimer) {  
128 - updatePlayProgressTimer.stop();  
129 - updatePlayProgressTimer = null;  
130 - }  
131 - if (elapTimer) {  
132 - elapTimer.restart(); 102 + // for directly run swf.
  103 + if (!conf.id) {
  104 + trace("directly run swf, load default url: " + this.default_url);
  105 + this.bandwidth.check_bandwidth(this.default_url);
133 } 106 }
  107 +
134 } 108 }
135 -  
136 - // srs infos  
137 - private var srs_server:String = null;  
138 - private var srs_primary_authors:String = null;  
139 - private var srs_id:String = null;  
140 - private var srs_pid:String = null;  
141 - private var srs_server_ip:String = null;  
142 - private function update_context_items():void { 109 + private function on_progress(percent:Number):void {
  110 + trace("progress:" + percent + "%");
  111 + }
  112 + private function update_context_items(
  113 + srs_server:String, srs_primary_authors:String,
  114 + srs_id:String, srs_pid:String, srs_server_ip:String
  115 + ):void {
143 // for context menu 116 // for context menu
144 var customItems:Array = [new ContextMenuItem("SrsPlayer")]; 117 var customItems:Array = [new ContextMenuItem("SrsPlayer")];
145 if (srs_server != null) { 118 if (srs_server != null) {
@@ -159,172 +132,42 @@ package @@ -159,172 +132,42 @@ package
159 } 132 }
160 contextMenu.customItems = customItems; 133 contextMenu.customItems = customItems;
161 } 134 }
162 -  
163 - // get NetConnection NetStatusEvent  
164 - public function onStatus(evt:NetStatusEvent) : void{  
165 - trace(evt.info.code);  
166 -  
167 - if (evt.info.hasOwnProperty("data") && evt.info.data) {  
168 - if (evt.info.data.hasOwnProperty("srs_server")) {  
169 - srs_server = evt.info.data.srs_server;  
170 - }  
171 - if (evt.info.data.hasOwnProperty("srs_primary_authors")) {  
172 - srs_primary_authors = evt.info.data.srs_primary_authors;  
173 - }  
174 - if (evt.info.data.hasOwnProperty("srs_id")) {  
175 - srs_id = evt.info.data.srs_id;  
176 - }  
177 - if (evt.info.data.hasOwnProperty("srs_pid")) {  
178 - srs_pid = evt.info.data.srs_pid;  
179 - }  
180 - if (evt.info.data.hasOwnProperty("srs_server_ip")) {  
181 - srs_server_ip = evt.info.data.srs_server_ip;  
182 - }  
183 - update_context_items();  
184 - }  
185 -  
186 - switch(evt.info.code){ 135 + public function on_status_change(code:String, data:String): void {
  136 + trace(code);
  137 + switch(code){
187 case "NetConnection.Connect.Failed": 138 case "NetConnection.Connect.Failed":
188 - updateState("连接服务器失败!"); 139 + trace("连接服务器失败!");
189 break; 140 break;
190 case "NetConnection.Connect.Rejected": 141 case "NetConnection.Connect.Rejected":
191 - updateState("服务器拒绝连接!"); 142 + trace("服务器拒绝连接!");
192 break; 143 break;
193 case "NetConnection.Connect.Success": 144 case "NetConnection.Connect.Success":
194 - server_ip = evt.info.data.srs_server_ip;  
195 - updateState("连接服务器成功!"); 145 + trace("连接服务器成功!");
196 break; 146 break;
  147 + case SrsBandwidth.StatusSrsBwtcPlayStart:
  148 + trace("开始测试下行带宽");
  149 + break;
  150 + case SrsBandwidth.StatusSrsBwtcPlayStop:
  151 + trace("下行带宽测试完毕," + data + "kbps,开始测试上行带宽。");
  152 + break;
  153 + case SrsBandwidth.StatusSrsBwtcPublishStart:
  154 + trace("开始测试上行带宽");
  155 + break;
  156 + case SrsBandwidth.StatusSrsBwtcPublishStop:
  157 + trace("上行带宽测试完毕," + data + "kbps,");
  158 + break;
197 case "NetConnection.Connect.Closed": 159 case "NetConnection.Connect.Closed":
198 - //updateState("连接已断开!"); 160 + trace("连接已断开!");
199 break; 161 break;
200 } 162 }
201 } 163 }
202 -  
203 - public function onTimerTimeout(evt:TimerEvent):void  
204 - {  
205 - value_progressbar = elapTimer.elapsed();  
206 - updateProgess(value_progressbar, max_progressbar);  
207 - }  
208 -  
209 - /**  
210 - * NetConnection callback this function, when recv server call "onSrsBandCheckStartPlayBytes"  
211 - * then start @updatePlayProgressTimer for updating the progressbar  
212 - * */  
213 - public function onSrsBandCheckStartPlayBytes(evt:Object):void{  
214 - var duration_ms:Number = evt.duration_ms;  
215 - var interval_ms:Number = evt.interval_ms;  
216 -  
217 - connection.call("onSrsBandCheckStartingPlayBytes", null);  
218 - updateState("开始测试下行带宽,服务器IP:" + server_ip);  
219 -  
220 - // we suppose play duration_ms = pub duration_ms  
221 - max_progressbar = duration_ms * 2;  
222 - }  
223 -  
224 - public function onSrsBandCheckPlaying(evt:Object):void{  
225 - }  
226 -  
227 - public function onSrsBandCheckStopPlayBytes(evt:Object):void{  
228 - var duration_ms:Number = evt.duration_ms;  
229 - var interval_ms:Number = evt.interval_ms;  
230 - var duration_delta:Number = evt.duration_delta;  
231 - var bytes_delta:Number = evt.bytes_delta;  
232 -  
233 - var kbps:Number = 0;  
234 - if(duration_delta > 0){  
235 - kbps = bytes_delta * 8.0 / duration_delta; // b/ms == kbps  
236 - }  
237 - kbps = (int(kbps * 10))/10.0;  
238 -  
239 - flash.utils.setTimeout(stopPlayTest, 0);  
240 - updateState("下行带宽测试完毕,服务器: " + server_ip + "," + kbps + "kbps,开始测试上行带宽。");  
241 - }  
242 -  
243 - private function stopPlayTest():void{  
244 - connection.call("onSrsBandCheckStoppedPlayBytes", null);  
245 - }  
246 -  
247 - public function onSrsBandCheckStartPublishBytes(evt:Object):void{  
248 - var duration_ms:Number = evt.duration_ms;  
249 - var interval_ms:Number = evt.interval_ms;  
250 -  
251 - connection.call("onSrsBandCheckStartingPublishBytes", null);  
252 -  
253 - flash.utils.setTimeout(publisher, 0);  
254 - }  
255 -  
256 - private function publisher():void{  
257 - if (stop_pub) {  
258 - return;  
259 - }  
260 -  
261 - var data:Array = new Array();  
262 -  
263 - var data_size:int = 100;  
264 - for(var i:int; i < data_size; i++){  
265 - data.push("SrS band check data from client's publishing......");  
266 - }  
267 - data_size += 100;  
268 - connection.call("onSrsBandCheckPublishing", null, data);  
269 -  
270 - flash.utils.setTimeout(publisher, 0);  
271 - }  
272 -  
273 - public function onSrsBandCheckStopPublishBytes(evt:Object):void{  
274 - var duration_ms:Number = evt.duration_ms;  
275 - var interval_ms:Number = evt.interval_ms;  
276 - var duration_delta:Number = evt.duration_delta;  
277 - var bytes_delta:Number = evt.bytes_delta;  
278 -  
279 - var kbps:Number = 0;  
280 - if(duration_delta > 0){  
281 - kbps = bytes_delta * 8.0 / duration_delta; // b/ms == kbps  
282 - }  
283 - kbps = (int(kbps * 10))/10.0;  
284 -  
285 - stopPublishTest();  
286 - }  
287 -  
288 - private function stopPublishTest():void{  
289 - if(connection.connected){  
290 - connection.call("onSrsBandCheckStoppedPublishBytes", null);  
291 - }  
292 - stop_pub = true;  
293 -  
294 - value_progressbar = max_progressbar;  
295 - updateProgess(value_progressbar, max_progressbar);  
296 - updatePlayProgressTimer.stop();  
297 - }  
298 -  
299 - public function onSrsBandCheckFinished(evt:Object):void{  
300 - var code:Number = evt.code;  
301 - var start_time:Number = evt.start_time;  
302 - var end_time:Number = evt.end_time;  
303 - var play_kbps:Number = evt.play_kbps;  
304 - var publish_kbps:Number = evt.publish_kbps;  
305 - var play_bytes:Number = evt.play_bytes;  
306 - var play_time:Number = evt.play_time;  
307 - var publish_bytes:Number = evt.publish_bytes;  
308 - var publish_time:Number = evt.publish_time;  
309 -  
310 - updateState("检测结束: 服务器: " + server_ip + " 上行: " + publish_kbps + " kbps" + " 下行: " + play_kbps + " kbps"  
311 - + " 测试时间: " + (end_time-start_time)/1000 + " 秒");  
312 - connection.call("finalClientPacket", null);  
313 - }  
314 -  
315 - // update progressBar's value  
316 - private function updateProgess(value:Number, maxValue:Number):void{  
317 - flash.external.ExternalInterface.call(this.js_update_progress, this.js_id, value * 100 / maxValue);  
318 - trace(value + "-" + maxValue + "-" + value * 100 / maxValue + "%");  
319 - }  
320 -  
321 - // update checking status  
322 - private function updateState(text:String):void{  
323 - flash.external.ExternalInterface.call(this.js_update_status, this.js_id, text);  
324 - trace(text);  
325 - }  
326 -  
327 - public function onBWDone():void{ 164 + private function on_complete(
  165 + start_time:Number, end_time:Number, play_kbps:Number, publish_kbps:Number,
  166 + play_bytes:Number, publish_bytes:Number, play_time:Number, publish_time:Number
  167 + ):void {
  168 + var status:String = "检测结束: 上行: " + publish_kbps + " kbps" + " 下行: " + play_kbps + " kbps"
  169 + + " 测试时间: " + Number((end_time - start_time) / 1000).toFixed(1) + " 秒";
  170 + trace(status);
328 } 171 }
329 } 172 }
330 } 173 }
@@ -115,19 +115,23 @@ int SrsBandwidth::do_bandwidth_check() @@ -115,19 +115,23 @@ int SrsBandwidth::do_bandwidth_check()
115 115
116 int64_t start_time = srs_get_system_time_ms(); 116 int64_t start_time = srs_get_system_time_ms();
117 117
  118 + srs_info("start play test.");
118 ret = check_play(play_duration_ms, 119 ret = check_play(play_duration_ms,
119 play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps); 120 play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps);
120 if (ret != ERROR_SUCCESS) { 121 if (ret != ERROR_SUCCESS) {
121 srs_error("band width play check failed. ret=%d", ret); 122 srs_error("band width play check failed. ret=%d", ret);
122 return ret; 123 return ret;
123 } 124 }
  125 + srs_info("stop play test.");
124 126
  127 + srs_info("start publish test.");
125 ret = check_publish(publish_duration_ms, 128 ret = check_publish(publish_duration_ms,
126 publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps); 129 publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps);
127 if (ret != ERROR_SUCCESS) { 130 if (ret != ERROR_SUCCESS) {
128 srs_error("band width publish check failed. ret=%d", ret); 131 srs_error("band width publish check failed. ret=%d", ret);
129 return ret; 132 return ret;
130 } 133 }
  134 + srs_info("stop publish test.");
131 135
132 int64_t end_time = srs_get_system_time_ms(); 136 int64_t end_time = srs_get_system_time_ms();
133 int play_kbps = play_bytes * 8 / play_actual_duration_ms; 137 int play_kbps = play_bytes * 8 / play_actual_duration_ms;
@@ -138,7 +142,9 @@ int SrsBandwidth::do_bandwidth_check() @@ -138,7 +142,9 @@ int SrsBandwidth::do_bandwidth_check()
138 start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps, 142 start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps,
139 _req->tcUrl.c_str(), ret); 143 _req->tcUrl.c_str(), ret);
140 144
141 - // send finished msg 145 + // send finished msg,
  146 + // flash client will close connection when got this packet,
  147 + // for the publish queue may contains packets.
142 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_finish(); 148 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_finish();
143 pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS)); 149 pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS));
144 pkt->data->set("start_time", SrsAmf0Any::number(start_time)); 150 pkt->data->set("start_time", SrsAmf0Any::number(start_time));
@@ -154,9 +160,12 @@ int SrsBandwidth::do_bandwidth_check() @@ -154,9 +160,12 @@ int SrsBandwidth::do_bandwidth_check()
154 srs_error("send bandwidth check finish message failed. ret=%d", ret); 160 srs_error("send bandwidth check finish message failed. ret=%d", ret);
155 return ret; 161 return ret;
156 } 162 }
  163 + srs_info("send finish packet.");
157 164
158 - // if flash, we notice the result, and expect a final packet.  
159 - while (true) { 165 + // we notice the result, and expect a final packet if not flash.
  166 + // if flash client, client will disconnect when got finish packet.
  167 + bool is_flash = (_req->swfUrl != "");
  168 + while (!is_flash) {
160 SrsMessage* msg = NULL; 169 SrsMessage* msg = NULL;
161 SrsBandwidthPacket* pkt = NULL; 170 SrsBandwidthPacket* pkt = NULL;
162 if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 171 if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
@@ -196,8 +205,8 @@ int SrsBandwidth::check_play( @@ -196,8 +205,8 @@ int SrsBandwidth::check_play(
196 srs_error("send bandwidth check start play message failed. ret=%d", ret); 205 srs_error("send bandwidth check start play message failed. ret=%d", ret);
197 return ret; 206 return ret;
198 } 207 }
199 - srs_info("BW check begin.");  
200 } 208 }
  209 + srs_info("BW check begin.");
201 210
202 while (true) { 211 while (true) {
203 // recv client's starting play response 212 // recv client's starting play response
@@ -212,10 +221,10 @@ int SrsBandwidth::check_play( @@ -212,10 +221,10 @@ int SrsBandwidth::check_play(
212 srs_info("get bandwidth message succes."); 221 srs_info("get bandwidth message succes.");
213 222
214 if (pkt->is_starting_play()) { 223 if (pkt->is_starting_play()) {
215 - srs_info("BW check recv play begin response.");  
216 break; 224 break;
217 } 225 }
218 } 226 }
  227 + srs_info("BW check recv play begin response.");
219 228
220 // send play data to client 229 // send play data to client
221 int64_t current_time = srs_get_system_time_ms(); 230 int64_t current_time = srs_get_system_time_ms();
@@ -277,8 +286,8 @@ int SrsBandwidth::check_play( @@ -277,8 +286,8 @@ int SrsBandwidth::check_play(
277 srs_error("send bandwidth check stop play message failed. ret=%d", ret); 286 srs_error("send bandwidth check stop play message failed. ret=%d", ret);
278 return ret; 287 return ret;
279 } 288 }
280 - srs_info("BW check stop play bytes.");  
281 } 289 }
  290 + srs_info("BW check stop play bytes.");
282 291
283 while (true) { 292 while (true) {
284 // recv client's stop play response. 293 // recv client's stop play response.
@@ -293,10 +302,10 @@ int SrsBandwidth::check_play( @@ -293,10 +302,10 @@ int SrsBandwidth::check_play(
293 srs_info("get bandwidth message succes."); 302 srs_info("get bandwidth message succes.");
294 303
295 if (pkt->is_stopped_play()) { 304 if (pkt->is_stopped_play()) {
296 - srs_info("BW check recv stop play response.");  
297 break; 305 break;
298 } 306 }
299 } 307 }
  308 + srs_info("BW check recv stop play response.");
300 309
301 return ret; 310 return ret;
302 } 311 }
@@ -318,8 +327,8 @@ int SrsBandwidth::check_publish( @@ -318,8 +327,8 @@ int SrsBandwidth::check_publish(
318 srs_error("send bandwidth check start publish message failed. ret=%d", ret); 327 srs_error("send bandwidth check start publish message failed. ret=%d", ret);
319 return ret; 328 return ret;
320 } 329 }
321 - srs_info("BW check publish begin.");  
322 } 330 }
  331 + srs_info("BW check publish begin.");
323 332
324 while (true) { 333 while (true) {
325 // read client's notification of starting publish 334 // read client's notification of starting publish
@@ -334,14 +343,14 @@ int SrsBandwidth::check_publish( @@ -334,14 +343,14 @@ int SrsBandwidth::check_publish(
334 srs_info("get bandwidth message succes."); 343 srs_info("get bandwidth message succes.");
335 344
336 if (pkt->is_starting_publish()) { 345 if (pkt->is_starting_publish()) {
337 - srs_info("BW check recv publish begin response.");  
338 break; 346 break;
339 } 347 }
340 } 348 }
  349 + srs_info("BW check recv publish begin response.");
341 350
342 // recv publish msgs until @duration_ms ms 351 // recv publish msgs until @duration_ms ms
343 int64_t current_time = srs_get_system_time_ms(); 352 int64_t current_time = srs_get_system_time_ms();
344 - while ( (srs_get_system_time_ms() - current_time) < duration_ms ) { 353 + while ((srs_get_system_time_ms() - current_time) < duration_ms) {
345 st_usleep(0); 354 st_usleep(0);
346 355
347 SrsMessage* msg = NULL; 356 SrsMessage* msg = NULL;
@@ -380,16 +389,16 @@ int SrsBandwidth::check_publish( @@ -380,16 +389,16 @@ int SrsBandwidth::check_publish(
380 srs_error("send bandwidth check stop publish message failed. ret=%d", ret); 389 srs_error("send bandwidth check stop publish message failed. ret=%d", ret);
381 return ret; 390 return ret;
382 } 391 }
383 - srs_info("BW check stop publish bytes.");  
384 } 392 }
  393 + srs_info("BW check stop publish bytes.");
385 394
386 // expect client to stop publish 395 // expect client to stop publish
387 // if flash client, we never expect the client stop publish bytes, 396 // if flash client, we never expect the client stop publish bytes,
388 // for the flash send call packet to test publish bandwidth, 397 // for the flash send call packet to test publish bandwidth,
389 // there are many many packets in the queue. 398 // there are many many packets in the queue.
390 // we just ignore the packet and send the bandwidth test data. 399 // we just ignore the packet and send the bandwidth test data.
391 - // TODO: FIXME: check whether flash client.  
392 - while (false) { 400 + bool is_flash = (_req->swfUrl != "");
  401 + while (!is_flash) {
393 // recv client's stop publish response. 402 // recv client's stop publish response.
394 SrsMessage* msg = NULL; 403 SrsMessage* msg = NULL;
395 SrsBandwidthPacket* pkt = NULL; 404 SrsBandwidthPacket* pkt = NULL;
@@ -402,10 +411,10 @@ int SrsBandwidth::check_publish( @@ -402,10 +411,10 @@ int SrsBandwidth::check_publish(
402 srs_info("get bandwidth message succes."); 411 srs_info("get bandwidth message succes.");
403 412
404 if (pkt->is_stopped_publish()) { 413 if (pkt->is_stopped_publish()) {
405 - srs_info("BW check recv stop publish response.");  
406 break; 414 break;
407 } 415 }
408 } 416 }
  417 + srs_info("BW check recv stop publish response.");
409 418
410 return ret; 419 return ret;
411 } 420 }
@@ -67,10 +67,10 @@ class SrsRtmpServer; @@ -67,10 +67,10 @@ class SrsRtmpServer;
67 * | final(2)------------> | finalClientPacket 67 * | final(2)------------> | finalClientPacket
68 * | <END> | 68 * | <END> |
69 * 69 *
70 -* 1. when flash client, server ignore the publish stopped result,  
71 -* and directly send the report to flash client.  
72 -* 2. flash client only. when got report, flash client should send out  
73 -* a final packet and close the connection immediately. 70 +* 1. when flash client, server never wait the stop publish response,
  71 +* for the flash client queue is fullfill with other packets.
  72 +* 2. when flash client, server never wait the final packet,
  73 +* for the flash client directly close when got report packet.
74 */ 74 */
75 class SrsBandwidth 75 class SrsBandwidth
76 { 76 {
@@ -69,7 +69,7 @@ int SrsConnection::cycle() @@ -69,7 +69,7 @@ int SrsConnection::cycle()
69 69
70 // success. 70 // success.
71 if (ret == ERROR_SUCCESS) { 71 if (ret == ERROR_SUCCESS) {
72 - srs_trace("client process normally finished. ret=%d", ret); 72 + srs_trace("client finished.");
73 } 73 }
74 74
75 // client close peer. 75 // client close peer.
@@ -720,7 +720,7 @@ int SrsProtocol::do_decode_message(SrsMessageHeader& header, SrsStream* stream, @@ -720,7 +720,7 @@ int SrsProtocol::do_decode_message(SrsMessageHeader& header, SrsStream* stream,
720 *ppacket = packet = new SrsSetChunkSizePacket(); 720 *ppacket = packet = new SrsSetChunkSizePacket();
721 return packet->decode(stream); 721 return packet->decode(stream);
722 } else { 722 } else {
723 - if (!header.is_set_peer_bandwidth()) { 723 + if (!header.is_set_peer_bandwidth() && !header.is_ackledgement()) {
724 srs_trace("drop unknown message, type=%d", header.message_type); 724 srs_trace("drop unknown message, type=%d", header.message_type);
725 } 725 }
726 } 726 }
@@ -223,7 +223,9 @@ public: @@ -223,7 +223,9 @@ public:
223 while (true) { 223 while (true) {
224 SrsMessage* msg = NULL; 224 SrsMessage* msg = NULL;
225 if ((ret = recv_message(&msg)) != ERROR_SUCCESS) { 225 if ((ret = recv_message(&msg)) != ERROR_SUCCESS) {
226 - srs_error("recv message failed. ret=%d", ret); 226 + if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
  227 + srs_error("recv message failed. ret=%d", ret);
  228 + }
227 return ret; 229 return ret;
228 } 230 }
229 srs_verbose("recv message success."); 231 srs_verbose("recv message success.");