winlin

refine bandwidth test

1 -// for bw to init url  
2 -// url: scheme://host:port/path?query#fragment  
3 -function srs_init_bwt(rtmp_url, hls_url) {  
4 - update_nav(); 1 +/**
  2 +* the SrsBandwidth object.
  3 +* @param container the html container id.
  4 +* @param width a float value specifies the width of bandwidth.
  5 +* @param height a float value specifies the height of bandwidth.
  6 +* @param private_object [optional] an object that used as private object,
  7 +* for example, the logic chat object which owner this bandwidth.
  8 +*/
  9 +function SrsBandwidth(container, width, height, private_object) {
  10 + if (!SrsBandwidth.__id) {
  11 + SrsBandwidth.__id = 100;
  12 + }
  13 + if (!SrsBandwidth.__bandwidths) {
  14 + SrsBandwidth.__bandwidths = [];
  15 + }
  16 +
  17 + SrsBandwidth.__bandwidths.push(this);
  18 +
  19 + this.private_object = private_object;
  20 + this.container = container;
  21 + this.width = width;
  22 + this.height = height;
  23 + this.id = SrsBandwidth.__id++;
  24 + this.stream_url = null;
  25 + this.callbackObj = null;
5 26
6 - if (rtmp_url) {  
7 - //var query = parse_query_string();  
8 - var search_filed = String(window.location.search).replace(" ", "").split("?")[1];  
9 - $(rtmp_url).val("rtmp://" + window.location.host + ":" + 1935 + "/app?" + search_filed); 27 + // the callback set data.
  28 + this.percent = 0;
  29 + this.status = "";
  30 +}
  31 +/**
  32 +* user can set some callback, then start the bandwidth.
  33 +* @param url the bandwidth test url.
  34 +* callbacks:
  35 +* on_bandwidth_ready():void, when srs bandwidth ready, user can play.
  36 +* on_update_progress(percent:Number):void, when srs bandwidth update the progress.
  37 +* percent:Number 100 means 100%.
  38 +* on_update_status(status:String):void, when srs bandwidth update the status.
  39 +* status:String the human readable status text.
  40 +*/
  41 +SrsBandwidth.prototype.start = function(url) {
  42 + if (url) {
  43 + this.stream_url = url;
10 } 44 }
11 - if (hls_url) {  
12 - $(hls_url).val(build_default_hls_url()); 45 +
  46 + // embed the flash.
  47 + var flashvars = {};
  48 + flashvars.id = this.id;
  49 + flashvars.on_bandwidth_ready = "__srs_on_bandwidth_ready";
  50 + flashvars.on_update_progress = "__srs_on_update_progress";
  51 + flashvars.on_update_status = "__srs_on_update_status";
  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_bwt/release/srs_bwt.swf?_version="+srs_get_version_code(),
  64 + this.container,
  65 + this.width, this.height,
  66 + "11.1.0", "js/AdobeFlashbandwidthInstall.swf",
  67 + flashvars, params, attributes,
  68 + function(callbackObj){
  69 + self.callbackObj = callbackObj;
13 } 70 }
  71 + );
  72 +
  73 + return this;
14 } 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 +SrsBandwidth.prototype.check_bandwidth = function(url) {
  81 + this.stop();
  82 + SrsBandwidth.__bandwidths.push(this);
15 83
16 -function srs_bwt_check_url(url) {  
17 - if (url.indexOf("key") != -1 && url.indexOf("vhost") != -1) {  
18 - return true; 84 + if (url) {
  85 + this.stream_url = url;
19 } 86 }
20 87
21 - return false; 88 + this.callbackObj.ref.__check_bandwidth(this.stream_url);
22 } 89 }
  90 +SrsBandwidth.prototype.stop = function(url) {
  91 + for (var i = 0; i < SrsBandwidth.__bandwidths.length; i++) {
  92 + var bandwidth = SrsBandwidth.__bandwidths[i];
23 93
24 -function srs_bwt_build_default_url() {  
25 - var url_default = "rtmp://" + window.location.host + ":" + 1935 + "/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com";  
26 - return url_default; 94 + if (bandwidth.id != this.id) {
  95 + continue;
  96 + }
  97 +
  98 + SrsBandwidth.__bandwidths.splice(i, 1);
  99 + break;
  100 + }
  101 +
  102 + this.callbackObj.ref.__stop();
  103 +}
  104 +SrsBandwidth.prototype.on_bandwidth_ready = function() {
  105 +}
  106 +SrsBandwidth.prototype.on_update_progress = function(percent) {
  107 +}
  108 +SrsBandwidth.prototype.on_update_status = function(status) {
  109 +}
  110 +function __srs_find_bandwidth(id) {
  111 + for (var i = 0; i < SrsBandwidth.__bandwidths.length; i++) {
  112 + var bandwidth = SrsBandwidth.__bandwidths[i];
  113 +
  114 + if (bandwidth.id != id) {
  115 + continue;
  116 + }
  117 +
  118 + return bandwidth;
  119 + }
  120 +
  121 + throw new Error("bandwidth not found. id=" + id);
  122 +}
  123 +function __srs_on_bandwidth_ready(id) {
  124 + var bandwidth = __srs_find_bandwidth(id);
  125 + bandwidth.on_bandwidth_ready();
  126 +}
  127 +function __srs_on_update_progress(id, percent) {
  128 + var bandwidth = __srs_find_bandwidth(id);
  129 + bandwidth.percent = percent;
  130 + bandwidth.on_update_progress(percent);
  131 +}
  132 +function __srs_on_update_status(id, status) {
  133 + var bandwidth = __srs_find_bandwidth(id);
  134 + bandwidth.status = status;
  135 + bandwidth.on_update_status(status);
27 } 136 }
@@ -80,6 +80,18 @@ function build_default_publish_rtmp_url() { @@ -80,6 +80,18 @@ function build_default_publish_rtmp_url() {
80 return "rtmp://" + server + ":" + port + "/" + app + "...vhost..." + vhost + "/" + stream; 80 return "rtmp://" + server + ":" + port + "/" + app + "...vhost..." + vhost + "/" + stream;
81 } 81 }
82 } 82 }
  83 +// for the bandwidth tool to init page
  84 +function build_default_bandwidth_rtmp_url() {
  85 + var query = parse_query_string();
  86 +
  87 + var server = (query.server == undefined)? window.location.hostname:query.server;
  88 + var port = (query.port == undefined)? 1935:query.port;
  89 + var vhost = (query.vhost == undefined)? "bandcheck.srs.com":query.vhost;
  90 + var app = (query.app == undefined)? "app":query.app;
  91 + var key = (query.key == undefined)? "35c9b402c12a7246868752e2878f7e0e":query.key;
  92 +
  93 + return "rtmp://" + server + ":" + port + "/" + app + "?key=" + key + "&vhost=" + vhost;
  94 +}
83 95
84 /** 96 /**
85 @param server the ip of server. default to window.location.hostname 97 @param server the ip of server. default to window.location.hostname
@@ -139,6 +151,15 @@ function srs_init_publish(rtmp_url) { @@ -139,6 +151,15 @@ function srs_init_publish(rtmp_url) {
139 $(rtmp_url).val(build_default_publish_rtmp_url()); 151 $(rtmp_url).val(build_default_publish_rtmp_url());
140 } 152 }
141 } 153 }
  154 +// for bw to init url
  155 +// url: scheme://host:port/path?query#fragment
  156 +function srs_init_bwt(rtmp_url, hls_url) {
  157 + update_nav();
  158 +
  159 + if (rtmp_url) {
  160 + $(rtmp_url).val(build_default_bandwidth_rtmp_url());
  161 + }
  162 +}
142 163
143 // check whether can republish 164 // check whether can republish
144 function srs_can_republish() { 165 function srs_can_republish() {
@@ -63,7 +63,7 @@ SrsPlayer.prototype.start = function(url) { @@ -63,7 +63,7 @@ SrsPlayer.prototype.start = function(url) {
63 "srs_player/release/srs_player.swf?_version="+srs_get_version_code(), 63 "srs_player/release/srs_player.swf?_version="+srs_get_version_code(),
64 this.container, 64 this.container,
65 this.width, this.height, 65 this.width, this.height,
66 - "11.1", "js/AdobeFlashPlayerInstall.swf", 66 + "11.1.0", "js/AdobeFlashPlayerInstall.swf",
67 flashvars, params, attributes, 67 flashvars, params, attributes,
68 function(callbackObj){ 68 function(callbackObj){
69 self.callbackObj = callbackObj; 69 self.callbackObj = callbackObj;
@@ -73,7 +73,7 @@ SrsPublisher.prototype.start = function() { @@ -73,7 +73,7 @@ SrsPublisher.prototype.start = function() {
73 "srs_publisher/release/srs_publisher.swf?_version="+srs_get_version_code(), 73 "srs_publisher/release/srs_publisher.swf?_version="+srs_get_version_code(),
74 this.container, 74 this.container,
75 this.width, this.height, 75 this.width, this.height,
76 - "11.1", "js/AdobeFlashPlayerInstall.swf", 76 + "11.1.0", "js/AdobeFlashPlayerInstall.swf",
77 flashvars, params, attributes, 77 flashvars, params, attributes,
78 function(callbackObj){ 78 function(callbackObj){
79 self.callbackObj = callbackObj; 79 self.callbackObj = callbackObj;
@@ -12,113 +12,60 @@ @@ -12,113 +12,60 @@
12 <script type="text/javascript" src="js/srs.player.js"></script> 12 <script type="text/javascript" src="js/srs.player.js"></script>
13 <script type="text/javascript" src="js/srs.publisher.js"></script> 13 <script type="text/javascript" src="js/srs.publisher.js"></script>
14 <script type="text/javascript" src="js/srs.utility.js"></script> 14 <script type="text/javascript" src="js/srs.utility.js"></script>
15 - <script type="text/javascript" src="js/srs.utility.js"></script>  
16 <script type="text/javascript" src="js/srs.bandwidth.js"></script> 15 <script type="text/javascript" src="js/srs.bandwidth.js"></script>
17 <style> 16 <style>
18 body{ 17 body{
19 padding-top: 55px; 18 padding-top: 55px;
20 } 19 }
21 #main_modal { 20 #main_modal {
22 - width: 600px;  
23 - margin-left: -300px;  
24 - }  
25 - #check_status {  
26 - margin-left: 20px;  
27 - margin-top: -55px;  
28 - }  
29 - #pb_buffer_bg {  
30 - margin-top: 40px;  
31 - margin-bottom: 10px; 21 + width: 700px;
  22 + margin-left: -350px;
32 } 23 }
33 </style> 24 </style>
34 <script type="text/javascript"> 25 <script type="text/javascript">
  26 + var bandwidth = null;
35 27
36 - function update_progress(percent) {  
37 - $("#progress_bar").width(percent);  
38 - }  
39 -  
40 - function progress_reset() {  
41 - $("#progress_bar").width("0%");  
42 - }  
43 -  
44 - function update_status(text) {  
45 - $("#check_status").text(text);  
46 - }  
47 -  
48 - function get_swf_width() {  
49 - return 1;  
50 - } 28 + $(function(){
  29 + srs_init_bwt("#txt_url");
51 30
52 - function get_swf_height() {  
53 - return 1;  
54 - } 31 + $("#btn_play").click(on_click_play);
  32 + $("#main_modal").on("show", on_start_bandwidth_test);
  33 + $("#main_modal").on("hide", on_stop_bandwidth_test);
  34 + });
55 35
56 - function show_modal() { 36 + function on_click_play() {
  37 + $("#check_status").text("");
  38 + $("#progress_bar").width("0%");
57 $("#main_modal").modal({show:true, keyboard:false}); 39 $("#main_modal").modal({show:true, keyboard:false});
58 } 40 }
  41 + function on_start_bandwidth_test() {
  42 + $("#div_container").remove();
59 43
60 - function band_check(url) { 44 + var div_container = $("<div/>");
  45 + $(div_container).attr("id", "div_container");
  46 + $("#player").append(div_container);
61 47
62 - // remove flash contet  
63 - var bw_div = $("<div/>");  
64 - $(bw_div).attr("id", "bw_div");  
65 - $("#bw_center").append(bw_div); 48 + var player = $("<div/>");
  49 + $(player).attr("id", "player_id");
  50 + $(div_container).append(player);
66 51
67 - var flashvars = {};  
68 - flashvars.url = url;  
69 - flashvars.update_progress = "update_progress";  
70 - flashvars.progress_reset = "progress_reset";  
71 - flashvars.update_status = "update_status";  
72 -  
73 - var params = {};  
74 - params.allowFullScreen = true;  
75 -  
76 - var attributes = {}; 52 + var url = $("#txt_url").val();
77 53
78 - swfobject.embedSWF(  
79 - "srs_bwt/release/srs_bwt.swf", "bw_div",  
80 - get_swf_width(), get_swf_height(),  
81 - "11.1.0", "js/AdobeFlashPlayerInstall.swf",  
82 - flashvars, params, attributes  
83 - ); 54 + bandwidth = new SrsBandwidth("player_id", 100, 1);
  55 + bandwidth.on_bandwidth_ready = function() {
  56 + this.check_bandwidth(url);
84 } 57 }
85 -  
86 - $(function(){  
87 - update_nav();  
88 - srs_init_bwt("#txt_url");  
89 -  
90 - var txt_input = $("#txt_url").val();  
91 - // if valid ?  
92 - if (!srs_bwt_check_url(txt_input)) {  
93 - $("#txt_url").val(srs_bwt_build_default_url()); 58 + bandwidth.on_update_progress = function(percent) {
  59 + $("#progress_bar").width(percent + "%");
94 } 60 }
95 -  
96 - $("#main_modal").on(  
97 - "show",  
98 - function()  
99 - {  
100 - progress_reset();  
101 - update_status("");  
102 - var url = $("#txt_url").val();  
103 - /*!  
104 - url encode  
105 - */  
106 - url = escape(url);  
107 - band_check(url); 61 + bandwidth.on_update_status = function(status) {
  62 + $("#check_status").text(status);
108 } 63 }
109 - );  
110 -  
111 - $("#main_modal").on("hide", function(){  
112 - $("#bw_div").remove();  
113 - });  
114 -  
115 - $("#btn_play").click(  
116 - function()  
117 - {  
118 - $("#main_modal").modal({show:true, keyboard:false}); 64 + bandwidth.start(url);
  65 + }
  66 + function on_stop_bandwidth_test() {
  67 + bandwidth.stop();
119 } 68 }
120 - );  
121 - });  
122 </script> 69 </script>
123 </head> 70 </head>
124 <body> 71 <body>
@@ -141,38 +88,43 @@ @@ -141,38 +88,43 @@
141 </div> 88 </div>
142 </div> 89 </div>
143 <div class="container"> 90 <div class="container">
144 - 91 + <div class="alert alert-info fade in">
  92 + <button type="button" class="close" data-dismiss="alert">×</button>
  93 + <strong><span>Usage:</span></strong> <span>点击“开始测速”即可测带宽,最大可测试带宽由服务器限制</span>
  94 + </div>
145 <div class="form-inline"> 95 <div class="form-inline">
146 URL: 96 URL:
147 - <input type="text" id="txt_url" class="input-xxlarge" value="" placeholder="例如:rtmp://host:port/app?key=xx&vhost=yy"></input> 97 + <input type="text" id="txt_url" class="input-xxlarge" value=""></input>
148 <button class="btn btn-primary" id="btn_play">开始测速</button> 98 <button class="btn btn-primary" id="btn_play">开始测速</button>
149 </div> 99 </div>
150 -  
151 <div id="main_modal" class="modal hide fade"> 100 <div id="main_modal" class="modal hide fade">
152 <div class="modal-header"> 101 <div class="modal-header">
153 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 102 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
154 - <h3>SRS Band Check</h3> 103 + <h3>SRS Bandwidth Check</h3>
155 </div> 104 </div>
156 <div class="modal-body"> 105 <div class="modal-body">
157 - <div id="player"></div> 106 + <div class="row-fluid">
  107 + <div class="span1"></div>
  108 + <div class="span10">
158 <div class="progress progress-striped active" id="pb_buffer_bg"> 109 <div class="progress progress-striped active" id="pb_buffer_bg">
159 - <div class="bar" style="width: 50%;" id="progress_bar"></div> 110 + <div class="bar" style="width: 0%;" id="progress_bar"></div>
160 </div> 111 </div>
161 </div> 112 </div>
162 -  
163 - <div class="modal-body" id="bw_center"> 113 + <div class="span1"></div>
  114 + </div>
  115 + <span id="check_status">status</span>
164 </div> 116 </div>
165 - <span id="check_status1"><font ><strong id="check_status">status</strong></font> </span>  
166 -  
167 <div class="modal-footer"> 117 <div class="modal-footer">
168 <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 关闭 </button> 118 <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 关闭 </button>
169 </div> 119 </div>
170 </div> 120 </div>
171 -  
172 <hr> 121 <hr>
173 <footer> 122 <footer>
174 <p><a href="https://github.com/winlinvip/simple-rtmp-server">SRS Team &copy; 2013</a></p> 123 <p><a href="https://github.com/winlinvip/simple-rtmp-server">SRS Team &copy; 2013</a></p>
175 </footer> 124 </footer>
  125 + <div class="container">
  126 + <div id="player"></div>
  127 + </div>
176 </div> 128 </div>
177 </body> 129 </body>
178 130
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <actionScriptProperties analytics="false" mainApplicationPath="srs_bwt.as" projectUUID="00251213-e6a2-4dd5-a033-125cc78f843c" version="10"> 2 <actionScriptProperties analytics="false" mainApplicationPath="srs_bwt.as" projectUUID="00251213-e6a2-4dd5-a033-125cc78f843c" version="10">
3 - <compiler additionalCompilerArguments="-locale en_US" autoRSLOrdering="true" copyDependentFiles="true" fteInMXComponents="false" generateAccessible="true" htmlExpressInstall="true" htmlGenerate="true" htmlHistoryManagement="true" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="bin-debug" removeUnusedRSL="true" sourceFolderPath="src" strict="true" targetPlayerVersion="0.0.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true"> 3 + <compiler additionalCompilerArguments="-locale en_US" autoRSLOrdering="true" copyDependentFiles="true" fteInMXComponents="false" generateAccessible="true" htmlExpressInstall="true" htmlGenerate="false" htmlHistoryManagement="true" htmlPlayerVersionCheck="true" includeNetmonSwc="false" outputFolderPath="release" removeUnusedRSL="true" sourceFolderPath="src" strict="true" targetPlayerVersion="0.0.0" useApolloConfig="false" useDebugRSLSwfs="true" verifyDigests="true" warn="true">
4 <compilerSourcePath/> 4 <compilerSourcePath/>
5 <libraryPath defaultLinkType="0"> 5 <libraryPath defaultLinkType="0">
6 <libraryPathEntry kind="4" path=""> 6 <libraryPathEntry kind="4" path="">
@@ -17,13 +17,13 @@ @@ -17,13 +17,13 @@
17 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark.swc" useDefaultLinkType="false"/> 17 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark.swc" useDefaultLinkType="false"/>
18 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/sparkskins.swc" useDefaultLinkType="false"/> 18 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/sparkskins.swc" useDefaultLinkType="false"/>
19 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/rpc.swc" useDefaultLinkType="false"/> 19 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/rpc.swc" useDefaultLinkType="false"/>
20 - <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/videoPlayer.swc" useDefaultLinkType="false"/>  
21 - <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp_air.swc" useDefaultLinkType="false"/>  
22 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/datavisualization.swc" useDefaultLinkType="false"/> 20 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/datavisualization.swc" useDefaultLinkType="false"/>
  21 + <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp_air.swc" useDefaultLinkType="false"/>
  22 + <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/videoPlayer.swc" useDefaultLinkType="false"/>
23 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark_dmv.swc" useDefaultLinkType="false"/> 23 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark_dmv.swc" useDefaultLinkType="false"/>
24 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation.swc" useDefaultLinkType="false"/> 24 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation.swc" useDefaultLinkType="false"/>
25 - <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/flash-integration.swc" useDefaultLinkType="false"/>  
26 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_dmv.swc" useDefaultLinkType="false"/> 25 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_dmv.swc" useDefaultLinkType="false"/>
  26 + <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/flash-integration.swc" useDefaultLinkType="false"/>
27 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_flashflexkit.swc" useDefaultLinkType="false"/> 27 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_flashflexkit.swc" useDefaultLinkType="false"/>
28 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_agent.swc" useDefaultLinkType="false"/> 28 <libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_agent.swc" useDefaultLinkType="false"/>
29 </excludedEntries> 29 </excludedEntries>
@@ -20,10 +20,15 @@ package @@ -20,10 +20,15 @@ package
20 20
21 public class srs_bwt extends Sprite 21 public class srs_bwt extends Sprite
22 { 22 {
23 - private var connection:NetConnection; 23 + private var connection:NetConnection = null;
24 24
25 - private var updatePlayProgressTimer:Timer;  
26 - private var elapTimer:SrsElapsedTimer; 25 + private var updatePlayProgressTimer:Timer = null;
  26 + private var elapTimer:SrsElapsedTimer = null;
  27 +
  28 + // user set id.
  29 + private var js_id:String = null;
  30 + // play param url.
  31 + private var user_url:String = null;
27 32
28 // server ip get from server 33 // server ip get from server
29 private var server_ip:String; 34 private var server_ip:String;
@@ -32,8 +37,8 @@ package @@ -32,8 +37,8 @@ package
32 private var stop_pub:Boolean = false; 37 private var stop_pub:Boolean = false;
33 38
34 // js interface 39 // js interface
  40 + private var js_on_player_ready:String;
35 private var js_update_progress:String; 41 private var js_update_progress:String;
36 - private var js_progress_reset:String;  
37 private var js_update_status:String; 42 private var js_update_status:String;
38 43
39 private var value_progressbar:Number = 0; 44 private var value_progressbar:Number = 0;
@@ -44,25 +49,66 @@ package @@ -44,25 +49,66 @@ package
44 49
45 public function srs_bwt() 50 public function srs_bwt()
46 { 51 {
  52 + if (!this.stage) {
  53 + this.addEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage);
  54 + } else {
  55 + this.system_on_add_to_stage(null);
  56 + }
  57 + }
  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 {
47 this.stage.scaleMode = StageScaleMode.NO_SCALE; 64 this.stage.scaleMode = StageScaleMode.NO_SCALE;
48 this.stage.align = StageAlign.TOP_LEFT; 65 this.stage.align = StageAlign.TOP_LEFT;
49 66
50 var flashvars:Object = this.root.loaderInfo.parameters; 67 var flashvars:Object = this.root.loaderInfo.parameters;
51 - this.js_update_progress = flashvars.update_progress;  
52 - this.js_progress_reset = flashvars.progress_reset;  
53 - this.js_update_status = flashvars.update_status;  
54 68
55 - // init context menu, add action "Srs 带宽测试工具 0.1" 69 + if (!flashvars.hasOwnProperty("id")) {
  70 + throw new Error("must specifies the id");
  71 + }
  72 +
  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
56 var myMenu:ContextMenu = new ContextMenu(); 79 var myMenu:ContextMenu = new ContextMenu();
57 myMenu.hideBuiltInItems(); 80 myMenu.hideBuiltInItems();
58 - myMenu.customItems.push(new ContextMenuItem("Srs 带宽测试工具 0.1", true)); 81 + myMenu.customItems.push(new ContextMenuItem("SRS 带宽测试工具", true));
59 this.contextMenu = myMenu; 82 this.contextMenu = myMenu;
60 83
  84 + flash.utils.setTimeout(this.system_on_js_ready, 0);
  85 + }
  86 +
  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 + }
  97 +
  98 + flash.external.ExternalInterface.addCallback("__check_bandwidth", this.js_call_check_bandwidth);
  99 + flash.external.ExternalInterface.addCallback("__stop", this.js_call_stop);
  100 +
  101 + flash.external.ExternalInterface.call(this.js_on_player_ready, this.js_id);
  102 + }
  103 +
  104 + private function js_call_check_bandwidth(url:String):void {
  105 + js_call_stop();
  106 +
61 // init connection 107 // init connection
62 connection = new NetConnection; 108 connection = new NetConnection;
63 connection.client = this; 109 connection.client = this;
64 connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus); 110 connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
65 - connection.connect(flashvars.url); 111 + connection.connect(url);
66 //connection.connect("rtmp://192.168.8.234:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com"); 112 //connection.connect("rtmp://192.168.8.234:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com");
67 113
68 // for play to update progress bar 114 // for play to update progress bar
@@ -73,10 +119,36 @@ package @@ -73,10 +119,36 @@ package
73 updatePlayProgressTimer.addEventListener(TimerEvent.TIMER, onTimerTimeout); 119 updatePlayProgressTimer.addEventListener(TimerEvent.TIMER, onTimerTimeout);
74 updatePlayProgressTimer.start(); 120 updatePlayProgressTimer.start();
75 } 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();
  133 + }
  134 + }
76 135
77 // get NetConnection NetStatusEvent 136 // get NetConnection NetStatusEvent
78 public function onStatus(evt:NetStatusEvent) : void{ 137 public function onStatus(evt:NetStatusEvent) : void{
79 trace(evt.info.code); 138 trace(evt.info.code);
  139 +
  140 + if (evt.info.hasOwnProperty("data") && evt.info.data) {
  141 + // for context menu
  142 + var customItems:Array = [new ContextMenuItem("SrsPlayer")];
  143 + if (evt.info.data.hasOwnProperty("srs_server")) {
  144 + customItems.push(new ContextMenuItem("Server: " + evt.info.data.srs_server));
  145 + }
  146 + if (evt.info.data.hasOwnProperty("srs_contributor")) {
  147 + customItems.push(new ContextMenuItem("Contributor: " + evt.info.data.srs_contributor));
  148 + }
  149 + contextMenu.customItems = customItems;
  150 + }
  151 +
80 switch(evt.info.code){ 152 switch(evt.info.code){
81 case "NetConnection.Connect.Failed": 153 case "NetConnection.Connect.Failed":
82 updateState("连接服务器失败!"); 154 updateState("连接服务器失败!");
@@ -92,7 +164,12 @@ package @@ -92,7 +164,12 @@ package
92 //updateState("连接已断开!"); 164 //updateState("连接已断开!");
93 break; 165 break;
94 } 166 }
  167 + }
95 168
  169 + public function onTimerTimeout(evt:TimerEvent):void
  170 + {
  171 + value_progressbar = elapTimer.elapsed();
  172 + updateProgess(value_progressbar, max_progressbar);
96 } 173 }
97 174
98 /** 175 /**
@@ -104,20 +181,13 @@ package @@ -104,20 +181,13 @@ package
104 var interval_ms:Number = evt.interval_ms; 181 var interval_ms:Number = evt.interval_ms;
105 182
106 connection.call("onSrsBandCheckStartingPlayBytes", null); 183 connection.call("onSrsBandCheckStartingPlayBytes", null);
107 - updateState("测试下行带宽(" + server_ip + ")"); 184 + updateState("开始测试下行带宽,服务器IP:" + server_ip);
108 185
109 // we suppose play duration_ms = pub duration_ms 186 // we suppose play duration_ms = pub duration_ms
110 max_progressbar = duration_ms * 2; 187 max_progressbar = duration_ms * 2;
111 } 188 }
112 189
113 public function onSrsBandCheckPlaying(evt:Object):void{ 190 public function onSrsBandCheckPlaying(evt:Object):void{
114 -  
115 - }  
116 -  
117 - public function onTimerTimeout(evt:TimerEvent):void  
118 - {  
119 - value_progressbar = elapTimer.elapsed();  
120 - updateProgess(value_progressbar, max_progressbar);  
121 } 191 }
122 192
123 public function onSrsBandCheckStopPlayBytes(evt:Object):void{ 193 public function onSrsBandCheckStopPlayBytes(evt:Object):void{
@@ -133,6 +203,7 @@ package @@ -133,6 +203,7 @@ package
133 kbps = (int(kbps * 10))/10.0; 203 kbps = (int(kbps * 10))/10.0;
134 204
135 flash.utils.setTimeout(stopPlayTest, 0); 205 flash.utils.setTimeout(stopPlayTest, 0);
  206 + updateState("下行带宽测试完毕:" + kbps + "kbps,开始测试上行带宽。");
136 } 207 }
137 208
138 private function stopPlayTest():void{ 209 private function stopPlayTest():void{
@@ -144,7 +215,6 @@ package @@ -144,7 +215,6 @@ package
144 var interval_ms:Number = evt.interval_ms; 215 var interval_ms:Number = evt.interval_ms;
145 216
146 connection.call("onSrsBandCheckStartingPublishBytes", null); 217 connection.call("onSrsBandCheckStartingPublishBytes", null);
147 - updateState("测试上行带宽(" + server_ip + ")");  
148 218
149 flash.utils.setTimeout(publisher, 0); 219 flash.utils.setTimeout(publisher, 0);
150 } 220 }
@@ -208,20 +278,19 @@ package @@ -208,20 +278,19 @@ package
208 connection.call("finalClientPacket", null); 278 connection.call("finalClientPacket", null);
209 } 279 }
210 280
211 - public function onBWDone():void{  
212 - // do nothing  
213 - }  
214 -  
215 // update progressBar's value 281 // update progressBar's value
216 private function updateProgess(value:Number, maxValue:Number):void{ 282 private function updateProgess(value:Number, maxValue:Number):void{
217 - flash.external.ExternalInterface.call(this.js_update_progress, value * 100 / maxValue + "%"); 283 + flash.external.ExternalInterface.call(this.js_update_progress, this.js_id, value * 100 / maxValue);
218 trace(value + "-" + maxValue + "-" + value * 100 / maxValue + "%"); 284 trace(value + "-" + maxValue + "-" + value * 100 / maxValue + "%");
219 } 285 }
220 286
221 // update checking status 287 // update checking status
222 private function updateState(text:String):void{ 288 private function updateState(text:String):void{
223 - flash.external.ExternalInterface.call(this.js_update_status, text); 289 + flash.external.ExternalInterface.call(this.js_update_status, this.js_id, text);
224 trace(text); 290 trace(text);
225 } 291 }
  292 +
  293 + public function onBWDone():void{
  294 + }
226 } 295 }
227 } 296 }