home *** CD-ROM | disk | FTP | other *** search
- package mx.controls.videoClasses
- {
- import flash.events.Event;
- import flash.events.NetStatusEvent;
- import flash.events.TimerEvent;
- import flash.net.NetConnection;
- import flash.net.ObjectEncoding;
- import flash.net.Responder;
- import flash.utils.Timer;
- import mx.core.mx_internal;
- import mx.resources.IResourceManager;
- import mx.resources.ResourceManager;
-
- use namespace mx_internal;
-
- public class NCManager implements INCManager
- {
- mx_internal static const VERSION:String = "3.5.0.12683";
-
- public static const DEFAULT_TIMEOUT:Number = 60000;
-
- private static const DEFAULT_NC_TIMEOUT:uint = 1500;
-
- private static var RTMP_CONN:Array = [{
- "protocol":"rtmp:/",
- "port":"1935"
- },{
- "protocol":"rtmp:/",
- "port":"443"
- },{
- "protocol":"rtmpt:/",
- "port":"80"
- },{
- "protocol":"rtmps:/",
- "port":"443"
- }];
-
- private var connectionTimer:Timer;
-
- private var _timeout:uint;
-
- private var portNumber:String;
-
- private var _bitrate:Number;
-
- private var contentPath:String;
-
- private var appName:String;
-
- private var _netConnection:NetConnection;
-
- private var _streamLength:Number;
-
- private var _streamWidth:Number;
-
- private var serverName:String;
-
- private var tryNCTimer:Timer;
-
- private var ncURI:String;
-
- public var payload:uint;
-
- private var connTypeCounter:uint;
-
- private var protocol:String;
-
- private var _streamHeight:Number;
-
- private var wrappedURL:String;
-
- private var _isRTMP:int = -1;
-
- public var tryNC:Array;
-
- private var owner:VideoPlayer;
-
- private var resourceManager:IResourceManager = ResourceManager.getInstance();
-
- private var _streamName:String;
-
- private var autoSenseBW:Boolean;
-
- private var ncConnected:Boolean;
-
- public var fallbackServerName:String;
-
- private var streams:Array;
-
- public function NCManager()
- {
- super();
- initNCInfo();
- initOtherInfo();
- owner = null;
- _netConnection = null;
- ncConnected = false;
- timeout = DEFAULT_TIMEOUT;
- }
-
- private function getStreamLengthStatus(param1:Object) : void
- {
- }
-
- private function initNCInfo() : void
- {
- _isRTMP = -1;
- serverName = null;
- wrappedURL = null;
- portNumber = null;
- appName = null;
- }
-
- public function get streamWidth() : Number
- {
- return _streamWidth;
- }
-
- public function reconnectOnStatus(param1:NetStatusEvent) : void
- {
- if(param1.info.code == "NetConnection.Connect.Failed" || param1.info.code == "NetConnection.Connect.Rejected")
- {
- _netConnection = null;
- ncConnected = false;
- owner.ncReconnected();
- }
- }
-
- public function get streamName() : String
- {
- return _streamName;
- }
-
- public function connectToURL(param1:String) : Boolean
- {
- var _loc3_:Boolean = false;
- var _loc4_:Boolean = false;
- initOtherInfo();
- contentPath = param1;
- if(!contentPath || contentPath == "")
- {
- throw new VideoError(VideoError.INVALID_CONTENT_PATH);
- }
- var _loc2_:Object = parseURL(contentPath);
- if(!_loc2_.streamName || _loc2_.streamName == "")
- {
- throw new VideoError(VideoError.INVALID_CONTENT_PATH,param1);
- }
- if(_loc2_.isRTMP)
- {
- _loc3_ = canReuseOldConnection(_loc2_);
- _isRTMP = 1;
- protocol = _loc2_.protocol;
- _streamName = _loc2_.streamName;
- serverName = _loc2_.serverName;
- wrappedURL = _loc2_.wrappedURL;
- portNumber = _loc2_.portNumber;
- appName = _loc2_.appName;
- if(!appName || appName == "" || !_streamName || _streamName == "")
- {
- throw new VideoError(VideoError.INVALID_CONTENT_PATH,param1);
- }
- autoSenseBW = _streamName.indexOf(",") >= 0;
- return _loc3_ || connectRTMP();
- }
- _loc4_ = canReuseOldConnection(_loc2_);
- _isRTMP = 0;
- _streamName = _loc2_.streamName;
- return _loc4_ || connectHTTP();
- }
-
- private function getStreamLengthResult(param1:Number) : void
- {
- if(param1 > 0)
- {
- _streamLength = param1;
- }
- owner.ncConnected();
- }
-
- public function get videoPlayer() : VideoPlayer
- {
- return owner;
- }
-
- private function canReuseOldConnection(param1:Object) : Boolean
- {
- if(_netConnection == null || !ncConnected)
- {
- return false;
- }
- if(!param1.isRTMP)
- {
- if(!_isRTMP)
- {
- return true;
- }
- owner.close();
- _netConnection = null;
- ncConnected = false;
- initNCInfo();
- return false;
- }
- if(_isRTMP)
- {
- if(param1.serverName == serverName && param1.appName == appName && param1.protocol == protocol && param1.portNumber == portNumber && param1.wrappedURL == wrappedURL)
- {
- return true;
- }
- owner.close();
- _netConnection = null;
- ncConnected = false;
- }
- initNCInfo();
- return false;
- }
-
- private function connectRTMP() : Boolean
- {
- connectionTimer.reset();
- connectionTimer.start();
- tryNC = [];
- tryNCTimer = new Timer(DEFAULT_NC_TIMEOUT);
- tryNCTimer.addEventListener(TimerEvent.TIMER,nextConnect);
- var _loc1_:uint = 0;
- while(_loc1_ < RTMP_CONN.length)
- {
- tryNC[_loc1_] = new NetConnection();
- tryNC[_loc1_].objectEncoding = ObjectEncoding.AMF0;
- tryNC[_loc1_].client = new NCManagerConnectClient(tryNC[_loc1_],this,_loc1_);
- tryNC[_loc1_].addEventListener(NetStatusEvent.NET_STATUS,connectOnStatus);
- _loc1_++;
- }
- nextConnect(null);
- return false;
- }
-
- public function reconnect() : void
- {
- var _loc1_:String = null;
- if(!_isRTMP)
- {
- _loc1_ = resourceManager.getString("controls","invalidCall");
- throw new Error(_loc1_);
- }
- _netConnection.addEventListener(NetStatusEvent.NET_STATUS,reconnectOnStatus);
- _netConnection.client = new NCManagerReconnectClient(this);
- _netConnection.connect(ncURI,false);
- }
-
- private function bitrateMatch() : void
- {
- var _loc1_:Number = NaN;
- var _loc2_:Number = _bitrate;
- if(isNaN(_loc2_))
- {
- _loc2_ = 0;
- }
- var _loc3_:uint = 0;
- while(_loc3_ < streams.length)
- {
- if(isNaN(streams[_loc3_].bitrate) || _loc2_ >= streams[_loc3_].bitrate)
- {
- _loc1_ = _loc3_;
- break;
- }
- _loc3_++;
- }
- if(isNaN(_loc1_))
- {
- throw new VideoError(VideoError.NO_BITRATE_MATCH);
- }
- if(_streamName)
- {
- _streamName += streams[_loc1_].src;
- }
- else
- {
- _streamName = streams[_loc1_].src;
- }
- _streamLength = streams[_loc1_].dur;
- }
-
- public function get netConnection() : NetConnection
- {
- return _netConnection;
- }
-
- public function get timeout() : uint
- {
- return _timeout;
- }
-
- public function disconnectOnStatus(param1:NetStatusEvent) : void
- {
- if(param1.info.code == "NetConnection.Connect.Success")
- {
- param1.target.removeEventListener(NetStatusEvent.NET_STATUS,disconnectOnStatus);
- NetConnection(param1.target).close();
- }
- }
-
- private function connectHTTP() : Boolean
- {
- _netConnection = new NetConnection();
- _netConnection.objectEncoding = ObjectEncoding.AMF0;
- _netConnection.connect(null);
- ncConnected = true;
- return true;
- }
-
- private function nextConnect(param1:Event) : void
- {
- var _loc2_:String = null;
- var _loc3_:String = null;
- var _loc5_:Number = NaN;
- tryNCTimer.reset();
- if(connTypeCounter == 0)
- {
- _loc2_ = protocol;
- if(portNumber)
- {
- _loc3_ = portNumber;
- }
- else
- {
- _loc5_ = 0;
- while(_loc5_ < RTMP_CONN.length)
- {
- if(protocol == RTMP_CONN[_loc5_].protocol)
- {
- _loc3_ = RTMP_CONN[_loc5_].port;
- break;
- }
- _loc5_++;
- }
- }
- }
- else
- {
- _loc2_ = RTMP_CONN[connTypeCounter].protocol;
- _loc3_ = RTMP_CONN[connTypeCounter].port;
- }
- var _loc4_:String = _loc2_ + (!serverName ? "" : "/" + serverName + ":" + _loc3_ + "/") + (!wrappedURL ? "" : wrappedURL + "/") + appName;
- tryNC[connTypeCounter].client.pending = true;
- tryNC[connTypeCounter].connect(_loc4_,autoSenseBW);
- if(connTypeCounter < RTMP_CONN.length - 1)
- {
- ++connTypeCounter;
- tryNCTimer.start();
- }
- }
-
- public function set videoPlayer(param1:VideoPlayer) : void
- {
- owner = param1;
- }
-
- public function get bitrate() : Number
- {
- return _bitrate;
- }
-
- public function get streamLength() : Number
- {
- return _streamLength;
- }
-
- public function isRTMP() : Boolean
- {
- return _isRTMP != 0;
- }
-
- public function connectAgain() : Boolean
- {
- var _loc1_:Number = Number(appName.indexOf("/"));
- if(_loc1_ < 0)
- {
- _loc1_ = Number(_streamName.indexOf("/"));
- if(_loc1_ >= 0)
- {
- appName += "/";
- appName += _streamName.slice(0,_loc1_);
- _streamName = _streamName.slice(_loc1_ + 1);
- }
- return false;
- }
- var _loc2_:* = appName.slice(_loc1_ + 1);
- _loc2_ += "/";
- _loc2_ += _streamName;
- _streamName = _loc2_;
- appName = appName.slice(0,_loc1_);
- close();
- payload = 0;
- connTypeCounter = 0;
- cleanConns();
- connectRTMP();
- return true;
- }
-
- private function initOtherInfo() : void
- {
- contentPath = null;
- _streamName = null;
- _streamLength = -1;
- _streamWidth = -1;
- _streamHeight = -1;
- streams = null;
- autoSenseBW = false;
- payload = 0;
- connTypeCounter = 0;
- cleanConns();
- }
-
- public function connectOnStatus(param1:NetStatusEvent) : void
- {
- var _loc2_:Object = null;
- var _loc3_:NetConnection = NetConnection(param1.target);
- _loc3_.client.pending = false;
- if(param1.info.code == "NetConnection.Connect.Success")
- {
- _netConnection = tryNC[_loc3_.client.connIndex];
- if(!owner.autoBandWidthDetection)
- {
- onConnected(_netConnection,0);
- }
- tryNC[_loc3_.client.connIndex] = null;
- cleanConns();
- }
- else if((param1.info.code == "NetConnection.Connect.Failed" || param1.info.code == "NetConnection.Connect.Rejected") && _loc3_.client.connIndex == RTMP_CONN.length - 1)
- {
- if(!connectAgain())
- {
- tryFallBack();
- }
- }
- }
-
- private function parseURL(param1:String) : Object
- {
- var _loc5_:Number = NaN;
- var _loc6_:Number = NaN;
- var _loc7_:String = null;
- var _loc8_:Object = null;
- var _loc2_:Object = {};
- var _loc3_:uint = 0;
- var _loc4_:int = int(param1.indexOf(":/",_loc3_));
- if(_loc4_ >= 0)
- {
- _loc4_ += 2;
- _loc2_.protocol = param1.slice(_loc3_,_loc4_);
- _loc2_.isRelative = false;
- }
- else
- {
- _loc2_.isRelative = true;
- }
- if(_loc2_.protocol != null && (_loc2_.protocol == "rtmp:/" || _loc2_.protocol == "rtmpt:/" || _loc2_.protocol == "rtmps:/" || _loc2_.protocol == "rtmpe:/" || _loc2_.protocol == "rmpte:/"))
- {
- _loc2_.isRTMP = true;
- _loc3_ = uint(_loc4_);
- if(param1.charAt(_loc3_) == "/")
- {
- _loc3_++;
- _loc5_ = Number(param1.indexOf(":",_loc3_));
- _loc6_ = Number(param1.indexOf("/",_loc3_));
- if(_loc6_ < 0)
- {
- if(_loc5_ < 0)
- {
- _loc2_.serverName = param1.slice(_loc3_);
- }
- else
- {
- _loc4_ = _loc5_;
- _loc2_.portNumber = param1.slice(_loc3_,_loc4_);
- _loc3_ = uint(_loc4_ + 1);
- _loc2_.serverName = param1.slice(_loc3_);
- }
- return _loc2_;
- }
- if(_loc5_ >= 0 && _loc5_ < _loc6_)
- {
- _loc4_ = _loc5_;
- _loc2_.serverName = param1.slice(_loc3_,_loc4_);
- _loc3_ = uint(_loc4_ + 1);
- _loc4_ = _loc6_;
- _loc2_.portNumber = param1.slice(_loc3_,_loc4_);
- }
- else
- {
- _loc4_ = _loc6_;
- _loc2_.serverName = param1.slice(_loc3_,_loc4_);
- }
- _loc3_ = uint(_loc4_ + 1);
- }
- if(param1.charAt(_loc3_) == "?")
- {
- _loc7_ = param1.slice(_loc3_ + 1);
- _loc8_ = parseURL(_loc7_);
- if(!_loc8_.protocol || !_loc8_.isRTMP)
- {
- throw new VideoError(VideoError.INVALID_CONTENT_PATH,param1);
- }
- _loc2_.wrappedURL = "?";
- _loc2_.wrappedURL += _loc8_.protocol;
- if(_loc8_.serverName != null)
- {
- _loc2_.wrappedURL += "/";
- _loc2_.wrappedURL += _loc8_.server;
- }
- if(_loc8_.wrappedURL != null)
- {
- _loc2_.wrappedURL += "/?";
- _loc2_.wrappedURL += _loc8_.wrappedURL;
- }
- _loc2_.appName = _loc8_.appName;
- _loc2_.streamName = _loc8_.streamName;
- return _loc2_;
- }
- _loc4_ = int(param1.indexOf("/",_loc3_));
- if(_loc4_ < 0)
- {
- _loc2_.appName = param1.slice(_loc3_);
- return _loc2_;
- }
- _loc2_.appName = param1.slice(_loc3_,_loc4_);
- _loc3_ = uint(_loc4_ + 1);
- _loc4_ = int(param1.indexOf("/",_loc3_));
- if(_loc4_ < 0)
- {
- _loc2_.streamName = param1.slice(_loc3_);
- if(_loc2_.streamName.slice(-5).toLowerCase() == ".flv2")
- {
- _loc2_.streamName = _loc2_.streamName.slice(0,-5);
- }
- else if(_loc2_.streamName.slice(-4).toLowerCase() == ".flv")
- {
- _loc2_.streamName = _loc2_.streamName.slice(0,-4);
- }
- return _loc2_;
- }
- _loc2_.appName += "/";
- _loc2_.appName += param1.slice(_loc3_,_loc4_);
- _loc3_ = uint(_loc4_ + 1);
- _loc2_.streamName = param1.slice(_loc3_);
- if(_loc2_.streamName.slice(-5).toLowerCase() == ".flv2")
- {
- _loc2_.streamName = _loc2_.streamName.slice(0,-5);
- }
- else if(_loc2_.streamName.slice(-4).toLowerCase() == ".flv")
- {
- _loc2_.streamName = _loc2_.streamName.slice(0,-4);
- }
- }
- else
- {
- _loc2_.isRTMP = false;
- _loc2_.streamName = param1;
- }
- return _loc2_;
- }
-
- public function set timeout(param1:uint) : void
- {
- _timeout = param1;
- connectionTimer = new Timer(_timeout,1);
- connectionTimer.addEventListener(TimerEvent.TIMER,onFCSConnectTimeOut);
- }
-
- public function get streamHeight() : Number
- {
- return _streamHeight;
- }
-
- private function onFCSConnectTimeOut(param1:Event) : void
- {
- cleanConns();
- _netConnection = null;
- ncConnected = false;
- if(!connectAgain())
- {
- owner.ncConnected();
- }
- }
-
- public function onReconnected() : void
- {
- _netConnection.removeEventListener(NetStatusEvent.NET_STATUS,connectOnStatus);
- ncConnected = true;
- owner.ncReconnected();
- }
-
- public function set bitrate(param1:Number) : void
- {
- if(_isRTMP == 0)
- {
- _bitrate = param1;
- }
- }
-
- private function tryFallBack() : void
- {
- if(serverName == fallbackServerName || !fallbackServerName)
- {
- _netConnection = null;
- ncConnected = false;
- owner.ncConnected();
- }
- else
- {
- connTypeCounter = 0;
- cleanConns();
- serverName = fallbackServerName;
- connectRTMP();
- }
- }
-
- public function onConnected(param1:NetConnection, param2:Number) : void
- {
- var _loc3_:Array = null;
- var _loc4_:uint = 0;
- var _loc5_:String = null;
- var _loc6_:Responder = null;
- connectionTimer.reset();
- param1.removeEventListener(NetStatusEvent.NET_STATUS,connectOnStatus);
- _netConnection = param1;
- ncURI = _netConnection.uri;
- ncConnected = true;
- if(autoSenseBW)
- {
- _bitrate = param2 * 1024;
- if(streams)
- {
- bitrateMatch();
- }
- else
- {
- _loc3_ = _streamName.split(",");
- _loc4_ = 0;
- while(_loc4_ < _loc3_.length)
- {
- _loc5_ = _loc3_[_loc4_].replace(/^\s*(\S.*\S)\s*$/,"$1");
- if(_loc4_ + 1 >= _loc3_.length)
- {
- _streamName = _loc5_;
- break;
- }
- if(param2 <= Number(_loc3_[_loc4_ + 1]))
- {
- _streamName = _loc5_;
- break;
- }
- _loc4_ += 2;
- }
- if(_streamName.slice(-5).toLowerCase() == ".flv2")
- {
- _streamName = _streamName.slice(0,-5);
- }
- else if(_streamName.slice(-4).toLowerCase() == ".flv")
- {
- _streamName = _streamName.slice(0,-4);
- }
- }
- }
- if(!owner.isLive && _streamLength == -1 && owner.autoBandWidthDetection)
- {
- _loc6_ = new Responder(getStreamLengthResult,getStreamLengthStatus);
- _netConnection.call("getStreamLength",_loc6_,_streamName);
- }
- else
- {
- owner.ncConnected();
- }
- }
-
- public function close() : void
- {
- if(_netConnection)
- {
- _netConnection.close();
- ncConnected = false;
- }
- }
-
- private function cleanConns() : void
- {
- var _loc1_:uint = 0;
- if(tryNCTimer != null)
- {
- tryNCTimer.reset();
- tryNCTimer = null;
- }
- if(tryNC)
- {
- _loc1_ = 0;
- while(_loc1_ < tryNC.length)
- {
- if(tryNC[_loc1_])
- {
- tryNC[_loc1_].removeEventListener(NetStatusEvent.NET_STATUS,connectOnStatus);
- if(tryNC[_loc1_].client.pending)
- {
- tryNC[_loc1_].addEventListener(NetStatusEvent.NET_STATUS,disconnectOnStatus);
- }
- else
- {
- tryNC[_loc1_].close();
- }
- }
- tryNC[_loc1_] = null;
- _loc1_++;
- }
- tryNC = null;
- }
- }
- }
- }
-
-