浏览代码

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

ericz 12 年之前
父节点
当前提交
d80ec0f6f6
共有 1 个文件被更改,包括 24 次插入15 次删除
  1. 24 15
      lib/connectionmanager.js

+ 24 - 15
lib/connectionmanager.js

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