1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

extract iplayer for srs-player.

This commit is contained in:
winlin 2016-03-19 10:06:08 +08:00
parent 10ce961a00
commit 9ab5039b36
7 changed files with 387 additions and 119 deletions

View file

@ -12,6 +12,9 @@
margin-top: -20px;
padding-top: 3px;
}
#main_modal {
margin-top: -60px;
}
.div_play_time {
margin-top: 10px;
}
@ -378,15 +381,13 @@
</div>
</div>
<div style="margin-top:-12px;">
<span id="debug_info"></span>
URL: <a href="#" id="player_url"></a>
<div class="input-prepend div_play_time" title="当前时间:年-月-日 时:分:秒">
<span class="add-on">@N</span>
<input class="span2" style="width:135px" id="player_clock" type="text" placeholder="年-月-日 时:分:秒">
</div>
</div>
<div>
<span id="debug_info"></span>
</div>
</div>
</div>
<footer>
@ -496,6 +497,7 @@
}
$("#div_container").remove();
$("#debug_info").text("");
var div_container = $("<div/>");
$(div_container).attr("id", "div_container");
@ -513,7 +515,7 @@
srs_player.on_player_metadata = function(metadata) {
$("#btn_dar_original").text("视频原始比例" + "(" + metadata.width + ":" + metadata.height + ")");
if (metadata.ip && metadata.pid && metadata.cid) {
$("#debug_info").text("DEBUG: " + metadata.ip + ' grep -in "\\[' + metadata.pid + '\\]\\[' + metadata.cid + '\\]"');
$("#debug_info").text("ID:" + metadata.ip + '/' + metadata.pid + '/' + metadata.cid + '');
}
select_dar("#btn_dar_original", 0, 0);
select_fs_size("#btn_fs_size_screen_100", "screen", 100);

View file

@ -40,3 +40,4 @@
</actionScriptProperties>

View file

@ -0,0 +1,141 @@
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.Security;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.utils.setTimeout;
import flashx.textLayout.formats.Float;
/**
* common player to play rtmp/flv stream,
* use system NetStream.
*/
public class CommonPlayer implements IPlayer
{
private var js_id:String = null;
// play param url.
private var user_url:String = null;
private var media_stream:NetStream = null;
private var media_conn:NetConnection = null;
private var owner:srs_player = null;
public function CommonPlayer(o:srs_player) {
owner = o;
}
public function init(flashvars:Object):void {
this.js_id = flashvars.id;
}
public function stream():NetStream {
return this.media_stream;
}
public function play(url:String):void {
var streamName:String;
this.user_url = url;
this.media_conn = new NetConnection();
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);
if (evt.info.hasOwnProperty("data") && evt.info.data) {
owner.on_player_metadata(evt.info.data);
}
// reject by server, maybe redirect.
if (evt.info.code == "NetConnection.Connect.Rejected") {
// RTMP 302 redirect.
if (evt.info.hasOwnProperty("ex") && evt.info.ex.code == 302) {
streamName = url.substr(url.lastIndexOf("/") + 1);
url = evt.info.ex.redirect + "/" + streamName;
log("Async RTMP 302 Redirect to: " + url);
// notify server.
media_conn.call("Redirected", null, evt.info.ex.redirect);
// do 302.
owner.on_player_302(url);
return;
}
}
// TODO: FIXME: failed event.
if (evt.info.code != "NetConnection.Connect.Success") {
return;
}
media_stream = new NetStream(media_conn);
media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
log("NetStream: code=" + evt.info.code);
if (evt.info.code == "NetStream.Video.DimensionChange") {
owner.on_player_dimension_change();
} else if (evt.info.code == "NetStream.Buffer.Empty") {
owner.on_player_buffer_empty();
} else if (evt.info.code == "NetStream.Buffer.Full") {
owner.on_player_buffer_full();
}
// TODO: FIXME: failed event.
});
// setup stream before play.
owner.on_player_before_play();
if (url.indexOf("http") == 0) {
media_stream.play(url);
} else {
streamName = url.substr(url.lastIndexOf("/") + 1);
media_stream.play(streamName);
}
owner.on_player_play();
});
if (url.indexOf("http") == 0) {
this.media_conn.connect(null);
} else {
var tcUrl:String = this.user_url.substr(0, this.user_url.lastIndexOf("/"));
this.media_conn.connect(tcUrl);
}
}
public function close():void {
if (this.media_stream) {
this.media_stream.close();
this.media_stream = null;
}
if (this.media_conn) {
this.media_conn.close();
this.media_conn = null;
}
}
private function log(msg:String):void {
Utility.log(js_id, msg);
}
}
}

View file

@ -0,0 +1,33 @@
package
{
import flash.net.NetStream;
/**
* the player interface.
*/
public interface IPlayer
{
/**
* initialize the player by flashvars for config.
* @param flashvars the config.
*/
function init(flashvars:Object):void;
/**
* get the NetStream to play the stream.
* @return the underlayer stream object.
*/
function stream():NetStream;
/**
* connect and play url.
* @param url the stream url to play.
*/
function play(url:String):void;
/**
* close the player.
*/
function close():void;
}
}

View file

@ -0,0 +1,72 @@
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.Security;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.utils.setTimeout;
import flashx.textLayout.formats.Float;
/**
* the m3u8 player.
*/
public class M3u8Player implements IPlayer
{
private var js_id:String = null;
// play param url.
private var user_url:String = null;
private var media_stream:NetStream = null;
private var media_conn:NetConnection = null;
private var owner:srs_player = null;
public function M3u8Player(o:srs_player) {
owner = o;
}
public function init(flashvars:Object):void {
this.js_id = flashvars.id;
}
public function stream():NetStream {
return this.media_stream;
}
public function play(url:String):void {
this.user_url = url;
}
public function close():void {
if (this.media_stream) {
this.media_stream.close();
this.media_stream = null;
}
if (this.media_conn) {
this.media_conn.close();
this.media_conn = null;
}
}
private function log(msg:String):void {
Utility.log(js_id, msg);
}
}
}

View file

@ -0,0 +1,36 @@
package
{
import flash.external.ExternalInterface;
import flash.utils.setTimeout;
/**
* the utility functions.
*/
public class Utility
{
/**
* initialize the player by flashvars for config.
* @param flashvars the config.
*/
public static function stringEndswith(s:String, f:String):Boolean {
return s && f && s.indexOf(f) == s.length - f.length;
}
/**
* write log to trace and console.log.
* @param msg the log message.
*/
public static function log(js_id:String, msg:String):void {
msg = "[" + new Date() +"][srs-player][" + js_id + "] " + msg;
trace(msg);
if (!flash.external.ExternalInterface.available) {
flash.utils.setTimeout(log, 300, msg);
return;
}
ExternalInterface.call("console.log", msg);
}
}
}

View file

@ -33,12 +33,13 @@ 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;
// play param url.
private var user_url:String = null;
// play param, user set width and height
private var user_w:int = 0;
private var user_h:int = 0;
private var user_buffer_time:Number = 0;
private var user_max_buffer_time:Number = 0;
private var user_volume:Number = 0;
// user set dar den:num
private var user_dar_den:int = 0;
private var user_dar_num:int = 0;
@ -47,8 +48,6 @@ package
private var user_fs_percent:int = 0;
// media specified.
private var media_conn:NetConnection = null;
private var media_stream:NetStream = null;
private var media_video:Video = null;
private var media_metadata:Object = {};
private var media_timer:Timer = new Timer(300);
@ -57,6 +56,11 @@ package
// flash donot allow js to set to fullscreen,
// only allow user click to enter fullscreen.
private var control_fs_mask:Sprite = new Sprite();
// the common player to play stream.
private var player:IPlayer = null;
// the flashvars config.
private var config:Object = null;
public function srs_player()
{
@ -93,7 +97,8 @@ package
if (!flashvars.hasOwnProperty("id")) {
throw new Error("must specifies the id");
}
this.config = flashvars;
this.js_id = flashvars.id;
this.js_on_player_ready = flashvars.on_player_ready;
this.js_on_player_metadata = flashvars.on_player_metadata;
@ -134,7 +139,11 @@ package
* system callack event, timer to do some regular tasks.
*/
private function system_on_timer(evt:TimerEvent):void {
var ms:NetStream = this.media_stream;
if (!player) {
return;
}
var ms:NetStream = player.stream();
if (!ms) {
//log("stream is null, ignore timer event.");
@ -249,8 +258,8 @@ package
* function for js to call: to pause the stream. ignore if not play.
*/
private function js_call_pause():void {
if (this.media_stream) {
this.media_stream.pause();
if (player && player.stream()) {
player.stream().pause();
log("user pause play");
}
}
@ -259,8 +268,8 @@ package
* function for js to call: to resume the stream. ignore if not play.
*/
private function js_call_resume():void {
if (this.media_stream) {
this.media_stream.resume();
if (player && player.stream()) {
player.stream().resume();
log("user resume play");
}
}
@ -301,8 +310,8 @@ package
* @buffer_time the buffer time in seconds.
*/
private function js_call_set_bt(buffer_time:Number):void {
if (this.media_stream) {
this.media_stream.bufferTime = buffer_time;
if (player && player.stream()) {
player.stream().bufferTime = buffer_time;
log("user set bufferTime to " + buffer_time.toFixed(2) + "s");
}
}
@ -313,8 +322,8 @@ package
* @remark this is the key feature for realtime communication by flash.
*/
private function js_call_set_mbt(max_buffer_time:Number):void {
if (this.media_stream) {
this.media_stream.bufferTimeMax = max_buffer_time;
if (player && player.stream()) {
player.stream().bufferTimeMax = max_buffer_time;
log("user set bufferTimeMax to " + max_buffer_time.toFixed(2) + "s");
}
}
@ -327,13 +336,10 @@ package
this.removeChild(this.media_video);
this.media_video = null;
}
if (this.media_stream) {
this.media_stream.close();
this.media_stream = null;
}
if (this.media_conn) {
this.media_conn.close();
this.media_conn = null;
if (player) {
player.close();
player = null;
}
log("player stopped");
}
@ -403,97 +409,83 @@ package
* @param volume, the volume, 0 is mute, 1 is 100%, 2 is 200%.
*/
private function js_call_play(url:String, _width:int, _height:int, buffer_time:Number, max_buffer_time:Number, volume:Number):void {
this.user_url = url;
this.user_w = _width;
this.user_h = _height;
log("start to play url: " + this.user_url + ", w=" + this.user_w + ", h=" + this.user_h
this.user_buffer_time = buffer_time;
this.user_max_buffer_time = max_buffer_time;
this.user_volume = volume;
log("start to play url: " + url + ", w=" + this.user_w + ", h=" + this.user_h
+ ", buffer=" + buffer_time.toFixed(2) + "s, max_buffer=" + max_buffer_time.toFixed(2) + "s, volume=" + volume.toFixed(2)
);
js_call_stop();
this.media_conn = new NetConnection();
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);
if (evt.info.hasOwnProperty("data") && evt.info.data) {
on_debug_info(evt.info.data);
update_context_items();
}
// reject by server, maybe redirect.
if (evt.info.code == "NetConnection.Connect.Rejected") {
// RTMP 302 redirect.
if (evt.info.hasOwnProperty("ex") && evt.info.ex.code == 302) {
var streamName:String = url.substr(url.lastIndexOf("/") + 1);
url = evt.info.ex.redirect + "/" + streamName;
log("Async RTMP 302 Redirect to: " + url);
// notify server.
media_conn.call("Redirected", null, evt.info.ex.redirect);
// do 302.
setTimeout(function(){
log("Async RTMP 302 Redirected.");
js_call_play(url, _width, _height, buffer_time, max_buffer_time, volume);
}, 1000);
return;
}
}
// TODO: FIXME: failed event.
if (evt.info.code != "NetConnection.Connect.Success") {
return;
}
media_stream = new NetStream(media_conn);
media_stream.soundTransform = new SoundTransform(volume);
media_stream.bufferTime = buffer_time;
media_stream.bufferTimeMax = max_buffer_time;
media_stream.client = {};
media_stream.client.onMetaData = system_on_metadata;
media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
log("NetStream: code=" + evt.info.code);
if (evt.info.code == "NetStream.Video.DimensionChange") {
system_on_metadata(media_metadata);
} else if (evt.info.code == "NetStream.Buffer.Empty") {
system_on_buffer_empty();
} else if (evt.info.code == "NetStream.Buffer.Full") {
system_on_buffer_full();
}
// TODO: FIXME: failed event.
});
if (url.indexOf("http") == 0) {
media_stream.play(url);
} else {
var streamName:String = url.substr(url.lastIndexOf("/") + 1);
media_stream.play(streamName);
}
media_video = new Video();
media_video.width = _width;
media_video.height = _height;
media_video.attachNetStream(media_stream);
media_video.smoothing = true;
addChild(media_video);
__draw_black_background(_width, _height);
// lowest layer, for mask to cover it.
setChildIndex(media_video, 0);
});
if (url.indexOf("http") == 0) {
this.media_conn.connect(null);
// create player.
if (Utility.stringEndswith(url, ".m3u8")) {
player = new M3u8Player(this);
log("create M3U8 player.");
} else {
var tcUrl:String = this.user_url.substr(0, this.user_url.lastIndexOf("/"));
this.media_conn.connect(tcUrl);
player = new CommonPlayer(this);
log("create Common player.");
}
// init player by config.
player.init(config);
// play the url.
player.play(url);
}
public function on_player_before_play():void {
if (!player) {
return;
}
var ms:NetStream = player.stream();
if (!ms) {
return;
}
ms.soundTransform = new SoundTransform(user_volume);
ms.bufferTime = user_buffer_time;
ms.bufferTimeMax = user_max_buffer_time;
ms.client = {};
ms.client.onMetaData = system_on_metadata;
}
public function on_player_play():void {
if (!player) {
return;
}
media_video = new Video();
media_video.width = user_w;
media_video.height = user_h;
media_video.attachNetStream(player.stream());
media_video.smoothing = true;
addChild(media_video);
__draw_black_background(user_w, user_h);
// lowest layer, for mask to cover it.
setChildIndex(media_video, 0);
}
public function on_player_metadata(data:Object):void {
on_debug_info(data);
update_context_items();
}
public function on_player_302(url:String):void {
setTimeout(function():void{
log("Async RTMP 302 Redirected.");
js_call_play(url, user_w, user_h, user_buffer_time, user_max_buffer_time, user_volume);
}, 1000);
}
public function on_player_dimension_change():void {
system_on_metadata(media_metadata);
}
public function on_player_buffer_empty():void {
system_on_buffer_empty();
}
public function on_player_buffer_full():void {
system_on_buffer_full();
}
/**
@ -650,16 +642,7 @@ package
}
private function log(msg:String):void {
msg = "[" + new Date() +"][srs-player][" + js_id + "] " + msg;
trace(msg);
if (!flash.external.ExternalInterface.available) {
flash.utils.setTimeout(log, 300, msg);
return;
}
ExternalInterface.call("console.log", msg);
Utility.log(js_id, msg);
}
}
}