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

refine player to support status change.

This commit is contained in:
winlin 2016-03-22 18:35:44 +08:00
parent 4841435b14
commit 6b5c880ff9
6 changed files with 136 additions and 15 deletions

View file

@ -1,7 +1,10 @@
package
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.external.ExternalInterface;
import flash.net.NetConnection;
import flash.net.NetStream;
@ -27,6 +30,8 @@ package
// play param url.
private var user_url:String = null;
private var conn:NetConnection = null;
/**
* create stream to play hls.
@ -40,6 +45,7 @@ package
this.m3u8_refresh_ratio = m3u8_refresh_ratio;
this.ts_parse_async_interval = ts_parse_async_interval;
hls = new HlsCodec(this);
this.conn = conn;
}
/**
@ -94,6 +100,8 @@ package
refresh_ts();
})
}
private var metadata:Object = null;
private function refresh_ts():void {
// all ts parsed.
if (parsed_ts_seq_no >= hls.seq_no + hls.tsCount) {
@ -125,9 +133,14 @@ package
//log("uv[" + k + "]=" + v);
}
if (client && client.hasOwnProperty("onMetaData")) {
client.onMetaData(obj);
// ignore when not changed.
if (!metadata || metadata.srs_server_ip != obj.srs_server_ip || metadata.srs_id != obj.srs_id || metadata.srs_pid != obj.srs_pid) {
if (client && client.hasOwnProperty("onMetaData")) {
log("got metadata for url " + uri);
client.onMetaData(obj);
}
}
metadata = obj;
}
download(uri, function(stream:ByteArray):void{
@ -177,6 +190,14 @@ package
completed(stream);
});
url.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void{
onPlayFailed(evt);
});
url.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(evt:SecurityErrorEvent):void{
onPlayRejected(evt);
});
// we set to the query.
uri += ((uri.indexOf("?") == -1)? "?":"&") + "shp_xpsid=" + XPlaybackSessionId;
var r:URLRequest = new URLRequest(uri);
@ -219,7 +240,35 @@ package
log("FLV: sps/pps " + flvHeader.length + " bytes");
writeFlv(flvHeader);
onPlayStart();
}
private function onPlayStart():void {
log("dispatch NetStream.Play.Start.");
dispatchEvent(new NetStatusEvent(NetStatusEvent.NET_STATUS, false, false, {
code: "NetStream.Play.Start",
stream: user_url,
descrption: "play start"
}));
}
private function onPlayFailed(evt:IOErrorEvent):void {
log("dispatch NetConnection.Connect.Failed.");
this.conn.dispatchEvent(new NetStatusEvent(NetStatusEvent.NET_STATUS, false, false, {
code: "NetConnection.Connect.Failed",
stream: user_url,
descrption: evt.text
}));
}
private function onPlayRejected(evt:SecurityErrorEvent):void {
log("dispatch NetConnection.Connect.Rejected.");
this.conn.dispatchEvent(new NetStatusEvent(NetStatusEvent.NET_STATUS, false, false, {
code: "NetConnection.Connect.Rejected",
stream: user_url,
descrption: evt.text
}));
}
private function onFlvBody(uri:String, flv:ByteArray):void {
if (!flvHeader) {

View file

@ -57,8 +57,19 @@ package
public function stream():NetStream {
return this.media_stream;
}
private function dumps_object(obj:Object):String {
var smr:String = "";
for (var k:String in obj) {
smr += k + "=" + obj[k] + ", ";
}
return smr;
}
public function play(url:String):void {
owner.on_player_status("init", "Ready to play");
var streamName:String;
this.user_url = url;
@ -66,7 +77,8 @@ package
this.media_conn.client = {};
this.media_conn.client.onBWDone = function():void {};
this.media_conn.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
log("NetConnection: code=" + evt.info.code);
log("NetConnection: type=" + evt.type + ", bub=" + evt.bubbles + ", can=" + evt.cancelable
+ ", info is " + dumps_object(evt.info));
if (evt.info.hasOwnProperty("data") && evt.info.data) {
owner.on_player_metadata(evt.info.data);
@ -87,7 +99,21 @@ package
owner.on_player_302(url);
return;
}
owner.on_player_status("rejected", "Server reject play");
close();
}
if (evt.info.code == "NetConnection.Connect.Success") {
owner.on_player_status("connected", "Connected at server");
}
if (evt.info.code == "NetConnection.Connect.Closed") {
close();
}
if (evt.info.code == "NetConnection.Connect.Failed") {
owner.on_player_status("failed", "Connect to server failed.");
close();
}
// TODO: FIXME: failed event.
if (evt.info.code != "NetConnection.Connect.Success") {
@ -100,7 +126,16 @@ package
media_stream = new NetStream(media_conn);
}
media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
log("NetStream: code=" + evt.info.code);
log("NetStream: type=" + evt.type + ", bub=" + evt.bubbles + ", can=" + evt.cancelable
+ ", info is " + dumps_object(evt.info));
if (evt.info.code == "NetStream.Play.Start") {
owner.on_player_status("play", "Start to play stream");
}
if (evt.info.code == "NetStream.Play.StreamNotFound") {
owner.on_player_status("rejected", "Stream not found");
close();
}
if (evt.info.code == "NetStream.Video.DimensionChange") {
owner.on_player_dimension_change();
@ -149,14 +184,23 @@ package
}
public function close():void {
var notify:Boolean = false;
if (this.media_stream) {
this.media_stream.close();
this.media_stream = null;
notify = true;
}
if (this.media_conn) {
this.media_conn.close();
this.media_conn = null;
notify = true;
}
if (notify) {
owner.on_player_status("closed", "Server closed.");
}
}
private function log(msg:String):void {

View file

@ -33,6 +33,7 @@ package
private var js_on_player_timer:String = null;
private var js_on_player_empty:String = null;
private var js_on_player_full:String = null;
private var js_on_player_status:String = null;
// play param, user set width and height
private var user_w:int = 0;
@ -105,6 +106,7 @@ package
this.js_on_player_timer = flashvars.on_player_timer;
this.js_on_player_empty = flashvars.on_player_empty;
this.js_on_player_full = flashvars.on_player_full;
this.js_on_player_status = flashvars.on_player_status;
this.media_timer.addEventListener(TimerEvent.TIMER, this.system_on_timer);
this.media_timer.start();
@ -497,6 +499,11 @@ package
public function on_player_buffer_full():void {
system_on_buffer_full();
}
public function on_player_status(code:String, desc:String):void {
log("[STATUS] code=" + code + ", desc=" + desc);
flash.external.ExternalInterface.call(this.js_on_player_status, this.js_id, code, desc);
}
/**
* get the "right" size of video,