1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine bandwidth test

This commit is contained in:
winlin 2013-12-27 18:49:34 +08:00
parent 20d1732ced
commit 19f9342034
8 changed files with 322 additions and 171 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<actionScriptProperties analytics="false" mainApplicationPath="srs_bwt.as" projectUUID="00251213-e6a2-4dd5-a033-125cc78f843c" version="10">
<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">
<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">
<compilerSourcePath/>
<libraryPath defaultLinkType="0">
<libraryPathEntry kind="4" path="">
@ -17,13 +17,13 @@
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/sparkskins.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/rpc.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/videoPlayer.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp_air.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/datavisualization.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/qtp_air.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/videoPlayer.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/spark_dmv.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/flash-integration.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_dmv.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/flash-integration.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_flashflexkit.swc" useDefaultLinkType="false"/>
<libraryPathEntry kind="3" linkType="1" path="${PROJECT_FRAMEWORKS}/libs/automation_agent.swc" useDefaultLinkType="false"/>
</excludedEntries>

View file

@ -20,10 +20,15 @@ package
public class srs_bwt extends Sprite
{
private var connection:NetConnection;
private var connection:NetConnection = null;
private var updatePlayProgressTimer:Timer;
private var elapTimer:SrsElapsedTimer;
private var updatePlayProgressTimer:Timer = null;
private var elapTimer:SrsElapsedTimer = null;
// user set id.
private var js_id:String = null;
// play param url.
private var user_url:String = null;
// server ip get from server
private var server_ip:String;
@ -32,8 +37,8 @@ package
private var stop_pub:Boolean = false;
// js interface
private var js_on_player_ready:String;
private var js_update_progress:String;
private var js_progress_reset:String;
private var js_update_status:String;
private var value_progressbar:Number = 0;
@ -44,25 +49,66 @@ package
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);
}
}
/**
* system event callback, when this control added to stage.
* the main function.
*/
private function system_on_add_to_stage(evt:Event):void {
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
var flashvars:Object = this.root.loaderInfo.parameters;
this.js_update_progress = flashvars.update_progress;
this.js_progress_reset = flashvars.progress_reset;
this.js_update_status = flashvars.update_status;
if (!flashvars.hasOwnProperty("id")) {
throw new Error("must specifies the id");
}
this.js_id = flashvars.id;
this.js_on_player_ready = flashvars.on_bandwidth_ready;
this.js_update_progress = flashvars.on_update_progress;
this.js_update_status = flashvars.on_update_status;
// init context menu, add action "Srs 带宽测试工具 0.1"
// init context menu
var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();
myMenu.customItems.push(new ContextMenuItem("Srs 带宽测试工具 0.1", true));
myMenu.customItems.push(new ContextMenuItem("SRS 带宽测试工具", true));
this.contextMenu = myMenu;
flash.utils.setTimeout(this.system_on_js_ready, 0);
}
/**
* system callack event, when js ready, register callback for js.
* the actual main function.
*/
private function system_on_js_ready():void {
if (!flash.external.ExternalInterface.available) {
trace("js not ready, try later.");
flash.utils.setTimeout(this.system_on_js_ready, 100);
return;
}
flash.external.ExternalInterface.addCallback("__check_bandwidth", this.js_call_check_bandwidth);
flash.external.ExternalInterface.addCallback("__stop", this.js_call_stop);
flash.external.ExternalInterface.call(this.js_on_player_ready, this.js_id);
}
private function js_call_check_bandwidth(url:String):void {
js_call_stop();
// init connection
connection = new NetConnection;
connection.client = this;
connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
connection.connect(flashvars.url);
connection.connect(url);
//connection.connect("rtmp://192.168.8.234:1935/app?key=35c9b402c12a7246868752e2878f7e0e&vhost=bandcheck.srs.com");
// for play to update progress bar
@ -73,10 +119,36 @@ package
updatePlayProgressTimer.addEventListener(TimerEvent.TIMER, onTimerTimeout);
updatePlayProgressTimer.start();
}
private function js_call_stop():void {
if (connection) {
connection.close();
connection = null;
}
if (updatePlayProgressTimer) {
updatePlayProgressTimer.stop();
updatePlayProgressTimer = null;
}
if (elapTimer) {
elapTimer.restart();
}
}
// get NetConnection NetStatusEvent
public function onStatus(evt:NetStatusEvent) : void{
trace(evt.info.code);
trace(evt.info.code);
if (evt.info.hasOwnProperty("data") && evt.info.data) {
// for context menu
var customItems:Array = [new ContextMenuItem("SrsPlayer")];
if (evt.info.data.hasOwnProperty("srs_server")) {
customItems.push(new ContextMenuItem("Server: " + evt.info.data.srs_server));
}
if (evt.info.data.hasOwnProperty("srs_contributor")) {
customItems.push(new ContextMenuItem("Contributor: " + evt.info.data.srs_contributor));
}
contextMenu.customItems = customItems;
}
switch(evt.info.code){
case "NetConnection.Connect.Failed":
updateState("连接服务器失败!");
@ -92,8 +164,13 @@ package
//updateState("连接已断开!");
break;
}
}
public function onTimerTimeout(evt:TimerEvent):void
{
value_progressbar = elapTimer.elapsed();
updateProgess(value_progressbar, max_progressbar);
}
/**
* NetConnection callback this function, when recv server call "onSrsBandCheckStartPlayBytes"
@ -104,20 +181,13 @@ package
var interval_ms:Number = evt.interval_ms;
connection.call("onSrsBandCheckStartingPlayBytes", null);
updateState("测试下行带宽(" + server_ip + ")");
updateState("开始测试下行带宽服务器IP" + server_ip);
// we suppose play duration_ms = pub duration_ms
max_progressbar = duration_ms * 2;
}
public function onSrsBandCheckPlaying(evt:Object):void{
}
public function onTimerTimeout(evt:TimerEvent):void
{
value_progressbar = elapTimer.elapsed();
updateProgess(value_progressbar, max_progressbar);
}
public function onSrsBandCheckStopPlayBytes(evt:Object):void{
@ -133,6 +203,7 @@ package
kbps = (int(kbps * 10))/10.0;
flash.utils.setTimeout(stopPlayTest, 0);
updateState("下行带宽测试完毕:" + kbps + "kbps开始测试上行带宽。");
}
private function stopPlayTest():void{
@ -144,7 +215,6 @@ package
var interval_ms:Number = evt.interval_ms;
connection.call("onSrsBandCheckStartingPublishBytes", null);
updateState("测试上行带宽(" + server_ip + ")");
flash.utils.setTimeout(publisher, 0);
}
@ -208,20 +278,19 @@ package
connection.call("finalClientPacket", null);
}
public function onBWDone():void{
// do nothing
}
// update progressBar's value
private function updateProgess(value:Number, maxValue:Number):void{
flash.external.ExternalInterface.call(this.js_update_progress, value * 100 / maxValue + "%");
flash.external.ExternalInterface.call(this.js_update_progress, this.js_id, value * 100 / maxValue);
trace(value + "-" + maxValue + "-" + value * 100 / maxValue + "%");
}
// update checking status
private function updateState(text:String):void{
flash.external.ExternalInterface.call(this.js_update_status, text);
flash.external.ExternalInterface.call(this.js_update_status, this.js_id, text);
trace(text);
}
public function onBWDone():void{
}
}
}