Allows connections to RTMP servers

rtmpconnection allows applications to connect to RTMP servers, like http://osflash.org/red5 or http://www.wowzamedia.com/. Note that you'll need a server running with the appropriate URLs set up for this example to work.

<canvas> <rtmpconnection src="rtmp://localhost:1935/simplevideostreaming/" autoconnect="true"/> <videoplayer url="Extremists.flv" type="rtmp" autoplay="true" width="320" height="240"/> </canvas>
import flash.net.NetConnection; import flash.events.ErrorEvent; import flash.events.StatusEvent; import flash.events.IOErrorEvent; import flash.events.NetStatusEvent; import flash.events.SecurityErrorEvent; super.init(); // Store new RTMP connections in canvas.rtmpconnections. If no // name is set for an rtmpconnection use "_default". Other real-time // objects will use the canvas.rtmpconnections["_default"] NetConnection if (this.name == null) { this.name = "_default"; } if (!canvas['rtmpconnections'] || null == canvas.rtmpconnections ) { canvas.rtmpconnections = new Array(); } if (canvas.rtmpconnections[this.name] != null) { if ($debug) { if (this.name == "_default") { Debug.warn("A default RTMP connection already exists, please name rtmpconnections if you use more than one"); } else { Debug.warn("An RTMP connection already exists with the name '%s': %w", this.name, canvas.rtmpconnections[this.name]); } } } canvas.rtmpconnections[this.name] = this; // Local reference to rtmpconnection. this._nc = new NetConnection(); if ($as3) { this._nc.addEventListener(NetStatusEvent.NET_STATUS, this._onStatus); this._nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this._onSecurityError); } else { // $swf8 or $swf7 // For some reason this cannot be done as a closure (in FP7). // See bug lpp-2197. this._nc.t = this; this._nc.onStatus = function(info) { this.t._onStatus(info); }; } if (this.debug) { // This is the way a function is called by the remote Interface has // to be implemented ... /** this._nc.setId = function(id) { _root.Debug.write("########## Method called by rtmp Server"); _root.Debug.write("---------------> id: ", id); } **/ } /** the following code should work now, but it hasn't been tested, so I'll leave it commented out -sa this._nc.__resolve = function(name) { //_root.Debug.write("__resolve " , name, typeof(this[name])); //_root.Debug.write(" arguments.callee.name " , // arguments.callee.name); if (typeof(this.t[name]) == "function") { this.t[name].apply(arguments); } }; **/ if (this.autoconnect) { this.connect(); } var errstr = error + ''; if ($debug) Debug.warn("rtmpconnection _onSecurityError() " + errstr); if (this.onerror.ready) { this.onerror.sendEvent(errstr); } //Debug.write("rtmpconnection.connect, stage", this.stage); if (this.stage > 0) { return; // already connected or connecting } if ($debug) { if (this.src == "") { Debug.write("no src url defined for", this); return; } else if (typeof(this.src) != "string") { Debug.write("src", this.src, "must be a string in", this); return; } } if ($as3) { //Debug.write('connecting', this._nc, this.src == "null" ? null : this.src) this._nc.connect(this.src == "null" ? null : this.src); } else { var ok = this._nc.connect(this.src == "null" ? null : this.src); if (!ok) { if ($debug) Debug.warn("Tried to use src:", this.src, "connection failed bad url"); this.setAttribute("status", "disconnected"); this.setAttribute("stage", 0); return; } } this.setAttribute("status", "connecting"); this.setAttribute("stage", 1); if ($as3) evt = evt.info; var code = evt.code; // only used for debugging var description = evt['description'] ? evt.description : ""; //Debug.write(evt); //Debug.write("rtmpconnection", this, "_onStatus", code, description); if (this.debug) { if ($debug) Debug.write("rtmpconnection", this, "_onStatus", code); } var msg = ""; var s = this.stage; switch (code) { case "NetConnection.Connect.AppShutdown": { // The application has been shut down // (for example, if the application is out of memory // resources and must be shut down to prevent the // server from crashing) or the server has been shut down. msg = "disconnected (error)"; s = 0; break; } case "NetConnection.Connect.Closed": { // The connection was successfully closed. msg = "disconnected"; s = 0; break; } case "NetConnection.Connect.Failed": { // The connection attempt failed; // for example, the server is not running. msg = "connection failed"; s = 0; break; } case "NetConnection.Connect.Rejected": { // The client does not have permission to connect // to the application, or the application expected // different parameters from those that were passed, // or the application specifically rejected the client. msg = "connection rejected"; s = 0; break; } case "NetConnection.Connect.Success": { // The connection attempt succeeded. msg = "connected"; s = 2; break; } default: { msg = code; break; } } this.setAttribute("status", msg); this.setAttribute("stage", s); if (this.debug) { if ($debug) { if (s == 2) { Debug.write("connected to " + this.src); } else { Debug.write("connection failed to", this.src, ":", msg); } } } if (s == 2) { if (this.onconnect.ready) { this.onconnect.sendEvent(); } } if (this.debug) { if ($debug) Debug.write("callMethod: func, obj, params", func, obj, params); } this._nc.call(func, obj, params);