Explorar el Código

change open to destroyed in cm, save bandwidth by sending fewer icecandidates when connection complete, call property defaults ot null

ericz hace 12 años
padre
commit
d80ec0f6f6
Se han modificado 1 ficheros con 24 adiciones y 15 borrados
  1. 24 15
      lib/connectionmanager.js

+ 24 - 15
lib/connectionmanager.js

@@ -12,7 +12,7 @@ function ConnectionManager(id, peer, socket, options) {
   this._options = options;
   this._options = options;
 
 
   // PeerConnection is not yet dead.
   // PeerConnection is not yet dead.
-  this.open = true;
+  this.destroyed = false;
 
 
   this.id = id;
   this.id = id;
   this.peer = peer;
   this.peer = peer;
@@ -26,6 +26,9 @@ function ConnectionManager(id, peer, socket, options) {
 
 
   // DataConnections on this PC.
   // DataConnections on this PC.
   this.connections = {};
   this.connections = {};
+  // Media call on this PC
+  this._call = null;
+  
   this._queued = [];
   this._queued = [];
 
 
   this._socket = socket;
   this._socket = socket;
@@ -75,17 +78,18 @@ ConnectionManager.prototype._startPeerConnection = function() {
 
 
 /** Add DataChannels to all queued DataConnections. */
 /** Add DataChannels to all queued DataConnections. */
 ConnectionManager.prototype._processQueue = function() {
 ConnectionManager.prototype._processQueue = function() {
-console.log(this._queued);
   for (var i = 0; i < this._queued.length; i++) {
   for (var i = 0; i < this._queued.length; i++) {
     var conn = this._queued[i];
     var conn = this._queued[i];
     if (conn.constructor == MediaConnection) {
     if (conn.constructor == MediaConnection) {
       console.log('adding', conn.localStream);
       console.log('adding', conn.localStream);
       this.pc.addStream(conn.localStream);
       this.pc.addStream(conn.localStream);
     } else if (conn.constructor = DataConnection) {
     } else if (conn.constructor = DataConnection) {
+      // reliable: true not yet implemented in Chrome
       var reliable = util.browserisms === 'Firefox' ? conn.reliable : false;
       var reliable = util.browserisms === 'Firefox' ? conn.reliable : false;
       conn.addDC(this.pc.createDataChannel(conn.label, { reliable: reliable }));
       conn.addDC(this.pc.createDataChannel(conn.label, { reliable: reliable }));
     }
     }
   }
   }
+  // onnegotiationneeded not yet implemented in Firefox, must manually create offer
   if (util.browserisms === 'Firefox' && this._queued.length > 0) {
   if (util.browserisms === 'Firefox' && this._queued.length > 0) {
     this._makeOffer();
     this._makeOffer();
   }
   }
@@ -109,18 +113,20 @@ ConnectionManager.prototype._setupIce = function() {
     }
     }
   };
   };
   this.pc.oniceconnectionstatechange = function() {
   this.pc.oniceconnectionstatechange = function() {
-    if (!!self.pc && self.pc.iceConnectionState === 'disconnected') {
-      util.log('iceConnectionState is disconnected, closing connections to ' + this.peer);
-      self.close();
+    if (!!self.pc) {
+      switch (self.pc.iceConnectionState) {
+        case 'failed':
+          util.log('iceConnectionState is disconnected, closing connections to ' + self.peer);
+          self.close();
+          break;
+        case 'completed':
+          self.pc.onicecandidate = null;
+          break;
+      }
     }
     }
   };
   };
   // Fallback for older Chrome impls.
   // Fallback for older Chrome impls.
-  this.pc.onicechange = function() {
-    if (!!self.pc && self.pc.iceConnectionState === 'disconnected') {
-      util.log('iceConnectionState is disconnected, closing connections to ' + this.peer);
-      self.close();
-    }
-  };
+  this.pc.onicechange = this.pc.oniceconnectionstatechange();
 };
 };
 
 
 /** Set up onnegotiationneeded. */
 /** Set up onnegotiationneeded. */
@@ -241,7 +247,7 @@ ConnectionManager.prototype._cleanup = function() {
     dst: self.peer
     dst: self.peer
   });
   });
 
 
-  this.open = false;
+  this.destroyed = true;
   this.emit('close');
   this.emit('close');
 };
 };
 
 
@@ -312,7 +318,7 @@ ConnectionManager.prototype.handleLeave = function() {
 
 
 /** Closes manager and all related connections. */
 /** Closes manager and all related connections. */
 ConnectionManager.prototype.close = function() {
 ConnectionManager.prototype.close = function() {
-  if (!this.open) {
+  if (this.destroyed) {
     this.emit('error', new Error('Connections to ' + this.peer + 'are already closed.'));
     this.emit('error', new Error('Connections to ' + this.peer + 'are already closed.'));
     return;
     return;
   }
   }
@@ -323,13 +329,16 @@ ConnectionManager.prototype.close = function() {
     var connection = this.connections[label];
     var connection = this.connections[label];
     connection.close();
     connection.close();
   }
   }
+  
+  // TODO: close the call
+  
   this.connections = null;
   this.connections = null;
   this._cleanup();
   this._cleanup();
 };
 };
 
 
 /** Create and returns a DataConnection with the peer with the given label. */
 /** Create and returns a DataConnection with the peer with the given label. */
 ConnectionManager.prototype.connect = function(options) {
 ConnectionManager.prototype.connect = function(options) {
-  if (!this.open) {
+  if (this.destroyed) {
     return;
     return;
   }
   }
 console.log('trying to connect');
 console.log('trying to connect');
@@ -368,7 +377,7 @@ console.log('trying to connect');
 };
 };
 
 
 ConnectionManager.prototype.call = function(stream, options) {
 ConnectionManager.prototype.call = function(stream, options) {
-  if (!this.open) {
+  if (this.destroyed) {
     return;
     return;
   }
   }