srs_bwt.as
9.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
The MIT License (MIT)
Copyright (c) 2013-2015 SRS(ossrs)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package
{
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.system.System;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.utils.Timer;
import flash.utils.setTimeout;
public class srs_bwt extends Sprite
{
/**
* the SRS bandwidth check/test library object.
*/
private var bandwidth:SrsBandwidth = new SrsBandwidth();
/**
* when not specifies any param, directly run the swf.
*/
private var default_url:String = "rtmp://dev:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com";
public function srs_bwt()
{
if (!this.stage) {
this.addEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage);
} else {
this.system_on_add_to_stage(null);
}
}
private function system_on_add_to_stage(evt:Event):void {
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
// init context menu
var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();
myMenu.customItems.push(new ContextMenuItem("SRS带宽测试工具", true));
this.contextMenu = myMenu;
check_bandwidth();
}
private function check_bandwidth():void {
// closure
var self:srs_bwt = this;
/////////////////////////////////////////////////////////////////////
// initialize the bandwidth check/test library
/////////////////////////////////////////////////////////////////////
// js callback, set to null if ignore.
var conf:Object = this.root.loaderInfo.parameters;
var js_id:String = conf.id? conf.id:null;
var js_on_ready:String = conf.on_bandwidth_ready? conf.on_bandwidth_ready:null;
var js_on_srs_info:String = conf.on_srs_info? conf.on_srs_info:null;
var js_on_progress_change:String = conf.on_update_progress? conf.on_update_progress:null;
var js_on_status_change:String = conf.on_update_status? conf.on_update_status:null;
var js_on_complete:String = conf.on_complete? conf.on_complete:null;
// js export, set to null to disable
var js_export_check_bandwidth:String = "__check_bandwidth";
var js_export_stop:String = "__stop";
// as callback, set to null if ignore.
var as_on_ready:Function = function():void {
self.on_ready();
};
var as_on_status_change:Function = function(code:String, data:String):void {
self.on_status_change(code, data);
};
var as_on_progress_change:Function = function(percent:Number):void {
self.on_progress(percent);
};
var as_on_srs_info:Function = function(srs_server:String, srs_primary:String, srs_authors:String, srs_id:String, srs_pid:String, srs_server_ip:String):void {
self.update_context_items(srs_server, srs_primary, srs_authors, srs_id, srs_pid, srs_server_ip);
};
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 {
self.on_complete(start_time, end_time, play_kbps, publish_kbps, play_bytes, publish_bytes, play_time, publish_time);
};
bandwidth.initialize(
as_on_ready, as_on_status_change, as_on_progress_change, as_on_srs_info, as_on_complete,
js_id, js_on_ready, js_on_status_change, js_on_progress_change, js_on_srs_info, js_on_complete,
js_export_check_bandwidth, js_export_stop
);
/////////////////////////////////////////////////////////////////////
}
private function on_ready():void {
var conf:Object = this.root.loaderInfo.parameters;
// for directly run swf.
if (!conf.id) {
trace("directly run swf, load default url: " + this.default_url);
this.bandwidth.check_bandwidth(this.default_url);
}
}
private function on_progress(percent:Number):void {
trace("progress:" + percent + "%");
}
private function update_context_items(
srs_server:String, srs_primary:String, srs_authors:String,
srs_id:String, srs_pid:String, srs_server_ip:String
):void {
// for context menu
var customItems:Array = [new ContextMenuItem("SrsPlayer")];
if (srs_server != null) {
customItems.push(new ContextMenuItem("Server: " + srs_server));
}
if (srs_primary != null) {
customItems.push(new ContextMenuItem("Primary: " + srs_primary));
}
if (srs_authors != null) {
customItems.push(new ContextMenuItem("Authors: " + srs_authors));
}
if (srs_server_ip != null) {
customItems.push(new ContextMenuItem("SrsIp: " + srs_server_ip));
}
if (srs_pid != null) {
customItems.push(new ContextMenuItem("SrsPid: " + srs_pid));
}
if (srs_id != null) {
customItems.push(new ContextMenuItem("SrsId: " + srs_id));
}
contextMenu.customItems = customItems;
}
public function on_status_change(code:String, data:String): void {
trace(code);
switch(code){
case "NetConnection.Connect.Failed":
trace("连接服务器失败!");
break;
case "NetConnection.Connect.Rejected":
trace("服务器拒绝连接!");
break;
case "NetConnection.Connect.Success":
trace("连接服务器成功!");
break;
case SrsBandwidth.StatusSrsBwtcPlayStart:
trace("开始测试下行带宽");
break;
case SrsBandwidth.StatusSrsBwtcPlayStop:
trace("下行带宽测试完毕," + data + "kbps,开始测试上行带宽。");
break;
case SrsBandwidth.StatusSrsBwtcPublishStart:
trace("开始测试上行带宽");
break;
case SrsBandwidth.StatusSrsBwtcPublishStop:
trace("上行带宽测试完毕," + data + "kbps,");
break;
case "NetConnection.Connect.Closed":
trace("连接已断开!");
break;
}
}
private function on_complete(
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 {
var status:String = "检测结束: 上行: " + publish_kbps + " kbps" + " 下行: " + play_kbps + " kbps"
+ " 测试时间: " + Number((end_time - start_time) / 1000).toFixed(1) + " 秒";
trace(status);
}
}
}