From 78bff4350869f6ba7500c2eb330fed03bf96e85b Mon Sep 17 00:00:00 2001 From: Song Zheng Date: Tue, 15 Apr 2014 01:32:29 -0700 Subject: [PATCH] implement android - refactor event passing --- scripts/OpenTok.coffee | 128 +++++++---- .../tokbox/cordova/OpenTokAndroidPlugin.java | 173 +++++++------- www/opentok.js | 213 ++++++++++++------ 3 files changed, 316 insertions(+), 198 deletions(-) diff --git a/scripts/OpenTok.coffee b/scripts/OpenTok.coffee index af7e2986..8af72445 100644 --- a/scripts/OpenTok.coffee +++ b/scripts/OpenTok.coffee @@ -101,7 +101,7 @@ class TBPublisher streamCreatedHandler: (response) => pdebug "publisher streamCreatedHandler", response arr = response.split( StringSplitter ) - stream = new TBStream( arr ) + stream = new TBStream( {}, "" ) for e in @userHandlers["streamCreated"] e( {streams:[stream.toJSON()], stream: stream.toJSON()} ) return @ @@ -219,14 +219,7 @@ class TBSession TB.showError( "Session.connect() takes a token and an optional completionHandler" ) return if( connectCompletionCallback? ) then @addEventHandlers( "sessionConnected", connectCompletionCallback ) - Cordova.exec(@connectionCreatedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionCreated"] ) - Cordova.exec(@connectionDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionDestroyed"] ) - Cordova.exec(@sessionConnectedHandler, TBError, OTPlugin, "addEvent", ["sessSessionConnected"] ) - Cordova.exec(@sessionDisconnectedHandler, TBError, OTPlugin, "addEvent", ["sessSessionDisconnected"] ) - Cordova.exec(@streamCreatedHandler, TBSuccess, OTPlugin, "addEvent", ["sessStreamCreated"] ) - Cordova.exec(@streamDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessStreamDestroyed"] ) - Cordova.exec(@streamPropertyChanged, TBError, OTPlugin, "addEvent", ["sessStreamPropertyChanged"] ) - Cordova.exec(@signalReceived, TBError, OTPlugin, "addEvent", ["signalReceived"] ) + Cordova.exec(@eventReceived, TBError, OTPlugin, "addEvent", ["sessionEvents"] ) Cordova.exec(TBSuccess, TBError, OTPlugin, "connect", [@token] ) return disconnect: () -> @@ -306,6 +299,8 @@ class TBSession constructor: (@apiKey, @sessionId) -> @userHandlers = {} + @connections = {} + @streams = {} Cordova.exec(TBSuccess, TBSuccess, OTPlugin, "initSession", [@apiKey, @sessionId] ) cleanUpDom: -> objects = document.getElementsByClassName('OT_root') @@ -332,43 +327,65 @@ class TBSession # event listeners - streamDestroyedHandler: (streamId) => - pdebug "streamDestroyedHandler", streamId - element = streamElements[ streamId ] - stream = {streamId: streamId} - event = { - stream: stream, - streams: [stream] - } - if(element) - element.parentNode.removeChild(element) - delete( streamElements[ streamId ] ) - TBUpdateObjects() - if @userHandlers["streamDestroyed"] - for e in @userHandlers["streamDestroyed"] - e(event) + eventReceived: (response) => + pdebug "session event received", response + @[response.eventType](response.data) + connectionCreated: (event) => + connection = new TBConnection( event.connection ) + connectionEvent = new TBEvent( {connection: connection } ) + @connections[connection.connectionId] = connection + if @userHandlers["connectionCreated"] + for e in @userHandlers["connectionCreated"] + e( connectionEvent ) + return @ + connectionDestroyed: (event) => + pdebug "connectionDestroyedHandler", event + connection = @connections[ event.connection.connectionId ] + connectionEvent = new TBEvent( {connection: connection, reason: "clientDisconnected" } ) + if @userHandlers["connectionDestroyed"] + for e in @userHandlers["connectionDestroyed"] + e( connectionEvent ) + delete( @connections[ connection.connectionId] ) return @ - sessionConnectedHandler: (event) => + sessionConnected: (event) => pdebug "sessionConnectedHandler", event - pdebug "what is apiKey: #{@apiKey}", {} - pdebug "what is token: #{@token}", {} pdebug "what is userHandlers", @userHandlers - @connection = event.connection - for e in @userHandlers["sessionConnected"] - e(event) - return @ - streamCreatedHandler: (response) => - pdebug "streamCreatedHandler", response - arr = response.split( StringSplitter ) - stream = new TBStream( arr ) - for e in @userHandlers["streamCreated"] - e( {streams:[stream.toJSON()], stream: stream.toJSON()} ) + event = null + if @userHandlers["sessionConnected"] + for e in @userHandlers["sessionConnected"] + e(event) return @ - sessionDisconnectedHandler: (event) => - pdebug "sessionDisconnectedHandler", event + sessionDisconnected: (event) => + pdebug "sessionDisconnected event", event + sessionDisconnectedEvent = new TBEvent( { reason: event.reason } ) + if @userHandlers["sessionDisconnected"] + for e in @userHandlers["sessionDisconnected"] + e( sessionDisconnectedEvent ) @cleanUpDom() - for e in @userHandlers["sessionDisconnected"] - e( event ) + return @ + streamCreated: (event) => + pdebug "streamCreatedHandler", event + stream = new TBStream( event.stream, @connections[event.stream.connectionId] ) + @streams[ stream.streamId ] = stream + streamEvent = new TBEvent( {stream: stream } ) + if @userHandlers["streamCreated"] + for e in @userHandlers["streamCreated"] + e( streamEvent ) + return @ + streamDestroyed: (event) => + pdebug "streamDestroyed event", event + stream = @streams[event.stream.streamId] + streamEvent = new TBEvent( {stream: stream, reason: "clientDisconnected" } ) + if @userHandlers["streamDestroyed"] + for e in @userHandlers["streamDestroyed"] + e( streamEvent ) + # remove stream DOM + element = streamElements[ stream.streamId ] + if(element) + element.parentNode.removeChild(element) + delete( streamElements[ stream.streamId ] ) + TBUpdateObjects() + delete( @streams[ stream.streamId ] ) return @ # deprecating @@ -454,14 +471,9 @@ class TBSubscriber # videoDimensions( Object ) - width and height, numbers # name( String ) - name of the stream class TBStream - constructor: ( props ) -> - pdebug "stream object being created with data:", props - @connection = new TBConnection( props[0] ) - @streamId = props[1] - @name = props[2] - @hasAudio = if props[3]== "T" then true else false - @hasVideo = if props[4]== "T" then true else false - @creationTime = props[5] + constructor: ( prop, @connection ) -> + for k,v of prop + @[k] = v @videoDimensions = {width: 0, height: 0} toJSON: -> return { @@ -479,13 +491,29 @@ class TBStream # creationTime( number ) - timestamp for creation of the connection ( in milliseconds ) # data ( string ) - string containing metadata describing the connection class TBConnection - constructor: (@connectionId) -> + constructor: (prop) -> + @connectionId = prop.connectionId + @creationTime = prop.creationTime + @data = prop.data return toJSON: -> return { connectionId: @connectionId + creationTime: @creationTime + data: @data } +class TBEvent + constructor: (prop) -> + for k,v of prop + @[k] = v + @defaultPrevented = false + return + isDefaultPrevented: => + return @defaultValue + preventDefault: => + # todo: implement preventDefault + return streamElements = {} # keep track of DOM elements for each stream diff --git a/src/android/com/tokbox/cordova/OpenTokAndroidPlugin.java b/src/android/com/tokbox/cordova/OpenTokAndroidPlugin.java index 55f2570d..b12ebca3 100644 --- a/src/android/com/tokbox/cordova/OpenTokAndroidPlugin.java +++ b/src/android/com/tokbox/cordova/OpenTokAndroidPlugin.java @@ -361,40 +361,42 @@ public void addPublisher(Session arg0, PublisherKit arg1) { @Override public void connected(Session arg0) { - // TODO Auto-generated method stub - Log.i(TAG, "session connected, triggering sessionConnected Event. My Cid is: "+ mSession.getConnection().getConnectionId() ); sessionConnected = true; - JSONObject message = new JSONObject(); - try{ - - JSONObject connection= new JSONObject(); - connection.put("connectionId", mSession.getConnection().getConnectionId()); - message.put("connection", connection); + JSONObject data = new JSONObject(); + try{ + data.put("status", "connected"); }catch (JSONException e) {} - - myEventListeners.get("sessSessionConnected").success( message ); + triggerJSEvent( "sessionEvents", "sessionConnected", data); } @Override public void connectionCreated(Session arg0, Connection arg1) { - // TODO Auto-generated method stub + Log.i(TAG, "connectionCreated"); - Log.i(TAG, "connection created: " + arg1.getConnectionId()); + JSONObject data= new JSONObject(); + try{ + JSONObject connection = createDataFromConnection( arg1 ); + data.put("connection", connection); + }catch (JSONException e) {} + triggerJSEvent( "sessionEvents", "connectionCreated", data); } @Override - public void connectionDestroyed(Session arg0, Connection arg1) { - // TODO Auto-generated method stub - - Log.i(TAG, "connection dropped: " + arg1.getConnectionId()); + public void connectionDestroyed(Session arg0, Connection arg1) {Log.i(TAG, "connection dropped: " + arg1.getConnectionId()); + + JSONObject data= new JSONObject(); + try{ + JSONObject connection = createDataFromConnection( arg1 ); + data.put("connection", connection); + }catch (JSONException e) {} + triggerJSEvent( "sessionEvents", "connectionDestroyed", data); } @Override public void disconnected(Session arg0) { - // TODO Auto-generated method stub ViewGroup parent = (ViewGroup) cordova.getActivity().findViewById(android.R.id.content); for (Map.Entry entry : subscriberCollection.entrySet() ) { if (null != parent) { @@ -404,41 +406,53 @@ public void disconnected(Session arg0) { if( myPublisher != null ){ parent.removeView( myPublisher.mView ); } + + JSONObject data = new JSONObject(); + try{ + data.put("reason", "clientDisconnected"); + }catch (JSONException e) {} - myEventListeners.get("sessSessionDisconnected").success(); + triggerJSEvent( "sessionEvents", "sessionDisconnected", data); } @Override - public void droppedStream(Session arg0, Stream stream) { - // TODO Auto-generated method stub + public void receivedStream(Session arg0, Stream arg1) { + Log.i(TAG, "stream received"); +// String message = stream.getConnection().getConnectionId() + "$2#9$" +// +stream.getStreamId() + "$2#9$" +// +stream.getName() + "$2#9$" +// + (stream.hasAudio() ? "T" : "F") + "$2#9$" +// + (stream.hasVideo() ? "T" : "F") + "$2#9$" +// + stream.getCreationTime() + "$2#9$" ; +// Log.i(TAG, "stream array ready, returning: " + message.toString() ); +// Log.i(TAG, "stream name: " + stream.getName() ); + + streamCollection.put(arg1.getStreamId(), arg1); + + JSONObject data= new JSONObject(); + try{ + JSONObject stream = createDataFromStream( arg1 ); + data.put("stream", stream); + triggerJSEvent( "sessionEvents", "streamCreated", data); + }catch (JSONException e) {} + + Log.i(TAG, "stream received done"); + } + @Override + public void droppedStream(Session arg0, Stream arg1) { Log.i(TAG, "session dropped stream"); - CallbackContext streamDisconnectedCallback = myEventListeners.get( "sessStreamDestroyed" ); - if( streamDisconnectedCallback != null ){ - Log.i(TAG, "dropped stream callback found"); + RunnableSubscriber subscriber = subscriberCollection.get( arg1.getStreamId() ); + subscriber.removeStreamView(); - - Log.i(TAG, "destroying corresponding subscriber"); - RunnableSubscriber subscriber = subscriberCollection.get( stream.getStreamId() ); - if( subscriber == null ){ - Log.i(TAG, "stream does not exist in subscriber collection"); - return; - } - subscriber.removeStreamView(); - Log.i(TAG, "removed subscriber stream view"); - subscriberCollection.remove( stream.getStreamId() ); - - PluginResult myResult = new PluginResult(PluginResult.Status.OK, stream.getStreamId()); - myResult.setKeepCallback(true); - if( stream.getConnection() != null ){ - if( stream.getConnection().getConnectionId().equalsIgnoreCase( mSession.getConnection().getConnectionId() )){ - streamDisconnectedCallback = myEventListeners.get( "pubStreamDestroyed" ); - } - } - streamDisconnectedCallback.sendPluginResult(myResult); - Log.i(TAG, "stream disconnected callback sent"); - - } + subscriberCollection.remove( arg1.getStreamId() ); + + JSONObject data= new JSONObject(); + try{ + JSONObject stream = createDataFromStream( arg1 ); + data.put("stream", stream); + triggerJSEvent( "sessionEvents", "streamDestroyed", data); + }catch (JSONException e) {} } @Override @@ -455,36 +469,6 @@ public void onSignal(Session arg0, String arg1, String arg2, Connection arg3) { } - @Override - public void receivedStream(Session arg0, Stream stream) { - // TODO Auto-generated method stub - - Log.i(TAG, "stream received"); - // JSONArray message = new JSONArray(); - // message.put( stream.getConnection().getConnectionId() ); - // message.put( stream.getStreamId() ); - String message = stream.getConnection().getConnectionId() + "$2#9$" - +stream.getStreamId() + "$2#9$" - +stream.getName() + "$2#9$" - + (stream.hasAudio() ? "T" : "F") + "$2#9$" - + (stream.hasVideo() ? "T" : "F") + "$2#9$" - + stream.getCreationTime() + "$2#9$" ; - - Log.i(TAG, "stream array ready, returning: " + message.toString() ); - Log.i(TAG, "stream name: " + stream.getName() ); - - streamCollection.put(stream.getStreamId(), stream); - // myEventListeners.get("streamCreated").success( message ); - //myEventListeners.get("streamCreated").( message ); - PluginResult myResult = new PluginResult(PluginResult.Status.OK, message); - myResult.setKeepCallback(true); - if( stream.getConnection().getConnectionId().equalsIgnoreCase( mSession.getConnection().getConnectionId() )){ - myEventListeners.get("pubStreamCreated").sendPluginResult(myResult); - }else{ - myEventListeners.get("sessStreamCreated").sendPluginResult(myResult); - } - } - @Override public void removePublisher(Session arg0, PublisherKit arg1) { // TODO Auto-generated method stub @@ -509,6 +493,41 @@ public void streamChangeVideoDimensions(Session arg0, Stream arg1, int arg2, // TODO Auto-generated method stub } -} - + + // Helper Methods + public JSONObject createDataFromConnection( Connection arg1 ){ + JSONObject connection = new JSONObject(); + + try{ + connection.put("connectionId", arg1.getConnectionId()); + connection.put("creationTime", arg1.getCreationTime()); + connection.put("data", arg1.getData()); + }catch (JSONException e) {} + return connection; + } + public JSONObject createDataFromStream( Stream arg1 ){ + JSONObject stream = new JSONObject(); + try{ + stream.put("connectionId", arg1.getConnection().getConnectionId() ); + stream.put("creationTime", arg1.getCreationTime() ); + stream.put("fps", -999); + stream.put("hasAudio", arg1.hasAudio()); + stream.put("hasVideo", arg1.hasVideo()); + stream.put("name", arg1.getName()); + stream.put("streamId", arg1.getStreamId()); + }catch (JSONException e) {} + return stream; + } + public void triggerJSEvent(String event, String type, JSONObject data ){ + JSONObject message = new JSONObject(); + try{ + message.put("eventType", type); + message.put("data", data); + }catch (JSONException e) {} + + PluginResult myResult = new PluginResult(PluginResult.Status.OK, message); + myResult.setKeepCallback(true); + myEventListeners.get(event).sendPluginResult(myResult); + } +} diff --git a/www/opentok.js b/www/opentok.js index 3ec62e94..83da6557 100644 --- a/www/opentok.js +++ b/www/opentok.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.6.3 (function() { - var DefaultHeight, DefaultWidth, OTPlugin, PublisherStreamId, PublisherTypeClass, StringSplitter, SubscriberTypeClass, TBConnection, TBError, TBGenerateDomHelper, TBGetZIndex, TBPublisher, TBSession, TBStream, TBSubscriber, TBSuccess, TBUpdateObjects, VideoContainerClass, getPosition, pdebug, replaceWithVideoStream, streamElements, + var DefaultHeight, DefaultWidth, OTPlugin, PublisherStreamId, PublisherTypeClass, StringSplitter, SubscriberTypeClass, TBConnection, TBError, TBEvent, TBGenerateDomHelper, TBGetZIndex, TBPublisher, TBSession, TBStream, TBSubscriber, TBSuccess, TBUpdateObjects, VideoContainerClass, getPosition, pdebug, replaceWithVideoStream, streamElements, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; OTPlugin = "OpenTokPlugin"; @@ -107,7 +107,7 @@ var arr, e, stream, _i, _len, _ref; pdebug("publisher streamCreatedHandler", response); arr = response.split(StringSplitter); - stream = new TBStream(arr); + stream = new TBStream({}, ""); _ref = this.userHandlers["streamCreated"]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { e = _ref[_i]; @@ -257,14 +257,7 @@ if ((connectCompletionCallback != null)) { this.addEventHandlers("sessionConnected", connectCompletionCallback); } - Cordova.exec(this.connectionCreatedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionCreated"]); - Cordova.exec(this.connectionDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionDestroyed"]); - Cordova.exec(this.sessionConnectedHandler, TBError, OTPlugin, "addEvent", ["sessSessionConnected"]); - Cordova.exec(this.sessionDisconnectedHandler, TBError, OTPlugin, "addEvent", ["sessSessionDisconnected"]); - Cordova.exec(this.streamCreatedHandler, TBSuccess, OTPlugin, "addEvent", ["sessStreamCreated"]); - Cordova.exec(this.streamDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessStreamDestroyed"]); - Cordova.exec(this.streamPropertyChanged, TBError, OTPlugin, "addEvent", ["sessStreamPropertyChanged"]); - Cordova.exec(this.signalReceived, TBError, OTPlugin, "addEvent", ["signalReceived"]); + Cordova.exec(this.eventReceived, TBError, OTPlugin, "addEvent", ["sessionEvents"]); Cordova.exec(TBSuccess, TBError, OTPlugin, "connect", [this.token]); }; @@ -391,14 +384,19 @@ function TBSession(apiKey, sessionId) { this.apiKey = apiKey; this.sessionId = sessionId; - this.sessionDisconnectedHandler = __bind(this.sessionDisconnectedHandler, this); - this.streamCreatedHandler = __bind(this.streamCreatedHandler, this); - this.sessionConnectedHandler = __bind(this.sessionConnectedHandler, this); - this.streamDestroyedHandler = __bind(this.streamDestroyedHandler, this); + this.streamDestroyed = __bind(this.streamDestroyed, this); + this.streamCreated = __bind(this.streamCreated, this); + this.sessionDisconnected = __bind(this.sessionDisconnected, this); + this.sessionConnected = __bind(this.sessionConnected, this); + this.connectionDestroyed = __bind(this.connectionDestroyed, this); + this.connectionCreated = __bind(this.connectionCreated, this); + this.eventReceived = __bind(this.eventReceived, this); this.removeEventHandler = __bind(this.removeEventHandler, this); this.addEventHandlers = __bind(this.addEventHandlers, this); this.on = __bind(this.on, this); this.userHandlers = {}; + this.connections = {}; + this.streams = {}; Cordova.exec(TBSuccess, TBSuccess, OTPlugin, "initSession", [this.apiKey, this.sessionId]); } @@ -439,72 +437,119 @@ return this; }; - TBSession.prototype.streamDestroyedHandler = function(streamId) { - var e, element, event, stream, _i, _len, _ref; - pdebug("streamDestroyedHandler", streamId); - element = streamElements[streamId]; - stream = { - streamId: streamId - }; - event = { - stream: stream, - streams: [stream] - }; - if (element) { - element.parentNode.removeChild(element); - delete streamElements[streamId]; - TBUpdateObjects(); + TBSession.prototype.eventReceived = function(response) { + pdebug("session event received", response); + return this[response.eventType](response.data); + }; + + TBSession.prototype.connectionCreated = function(event) { + var connection, connectionEvent, e, _i, _len, _ref; + connection = new TBConnection(event.connection); + connectionEvent = new TBEvent({ + connection: connection + }); + this.connections[connection.connectionId] = connection; + if (this.userHandlers["connectionCreated"]) { + _ref = this.userHandlers["connectionCreated"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + e = _ref[_i]; + e(connectionEvent); + } } - if (this.userHandlers["streamDestroyed"]) { - _ref = this.userHandlers["streamDestroyed"]; + return this; + }; + + TBSession.prototype.connectionDestroyed = function(event) { + var connection, connectionEvent, e, _i, _len, _ref; + pdebug("connectionDestroyedHandler", event); + connection = this.connections[event.connection.connectionId]; + connectionEvent = new TBEvent({ + connection: connection, + reason: "clientDisconnected" + }); + if (this.userHandlers["connectionDestroyed"]) { + _ref = this.userHandlers["connectionDestroyed"]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { e = _ref[_i]; - e(event); + e(connectionEvent); } } + delete this.connections[connection.connectionId]; return this; }; - TBSession.prototype.sessionConnectedHandler = function(event) { + TBSession.prototype.sessionConnected = function(event) { var e, _i, _len, _ref; pdebug("sessionConnectedHandler", event); - pdebug("what is apiKey: " + this.apiKey, {}); - pdebug("what is token: " + this.token, {}); pdebug("what is userHandlers", this.userHandlers); - this.connection = event.connection; - _ref = this.userHandlers["sessionConnected"]; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - e = _ref[_i]; - e(event); + event = null; + if (this.userHandlers["sessionConnected"]) { + _ref = this.userHandlers["sessionConnected"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + e = _ref[_i]; + e(event); + } } return this; }; - TBSession.prototype.streamCreatedHandler = function(response) { - var arr, e, stream, _i, _len, _ref; - pdebug("streamCreatedHandler", response); - arr = response.split(StringSplitter); - stream = new TBStream(arr); - _ref = this.userHandlers["streamCreated"]; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - e = _ref[_i]; - e({ - streams: [stream.toJSON()], - stream: stream.toJSON() - }); + TBSession.prototype.sessionDisconnected = function(event) { + var e, sessionDisconnectedEvent, _i, _len, _ref; + pdebug("sessionDisconnected event", event); + sessionDisconnectedEvent = new TBEvent({ + reason: event.reason + }); + if (this.userHandlers["sessionDisconnected"]) { + _ref = this.userHandlers["sessionDisconnected"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + e = _ref[_i]; + e(sessionDisconnectedEvent); + } } + this.cleanUpDom(); return this; }; - TBSession.prototype.sessionDisconnectedHandler = function(event) { - var e, _i, _len, _ref; - pdebug("sessionDisconnectedHandler", event); - this.cleanUpDom(); - _ref = this.userHandlers["sessionDisconnected"]; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - e = _ref[_i]; - e(event); + TBSession.prototype.streamCreated = function(event) { + var e, stream, streamEvent, _i, _len, _ref; + pdebug("streamCreatedHandler", event); + stream = new TBStream(event.stream, this.connections[event.stream.connectionId]); + this.streams[stream.streamId] = stream; + streamEvent = new TBEvent({ + stream: stream + }); + if (this.userHandlers["streamCreated"]) { + _ref = this.userHandlers["streamCreated"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + e = _ref[_i]; + e(streamEvent); + } + } + return this; + }; + + TBSession.prototype.streamDestroyed = function(event) { + var e, element, stream, streamEvent, _i, _len, _ref; + pdebug("streamDestroyed event", event); + stream = this.streams[event.stream.streamId]; + streamEvent = new TBEvent({ + stream: stream, + reason: "clientDisconnected" + }); + if (this.userHandlers["streamDestroyed"]) { + _ref = this.userHandlers["streamDestroyed"]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + e = _ref[_i]; + e(streamEvent); + } + } + element = streamElements[stream.streamId]; + if (element) { + element.parentNode.removeChild(element); + delete streamElements[stream.streamId]; + TBUpdateObjects(); } + delete this.streams[stream.streamId]; return this; }; @@ -602,14 +647,13 @@ })(); TBStream = (function() { - function TBStream(props) { - pdebug("stream object being created with data:", props); - this.connection = new TBConnection(props[0]); - this.streamId = props[1]; - this.name = props[2]; - this.hasAudio = props[3] === "T" ? true : false; - this.hasVideo = props[4] === "T" ? true : false; - this.creationTime = props[5]; + function TBStream(prop, connection) { + var k, v; + this.connection = connection; + for (k in prop) { + v = prop[k]; + this[k] = v; + } this.videoDimensions = { width: 0, height: 0 @@ -632,14 +676,18 @@ })(); TBConnection = (function() { - function TBConnection(connectionId) { - this.connectionId = connectionId; + function TBConnection(prop) { + this.connectionId = prop.connectionId; + this.creationTime = prop.creationTime; + this.data = prop.data; return; } TBConnection.prototype.toJSON = function() { return { - connectionId: this.connectionId + connectionId: this.connectionId, + creationTime: this.creationTime, + data: this.data }; }; @@ -647,6 +695,29 @@ })(); + TBEvent = (function() { + function TBEvent(prop) { + this.preventDefault = __bind(this.preventDefault, this); + this.isDefaultPrevented = __bind(this.isDefaultPrevented, this); + var k, v; + for (k in prop) { + v = prop[k]; + this[k] = v; + } + this.defaultPrevented = false; + return; + } + + TBEvent.prototype.isDefaultPrevented = function() { + return this.defaultValue; + }; + + TBEvent.prototype.preventDefault = function() {}; + + return TBEvent; + + })(); + streamElements = {}; getPosition = function(divName) {