浏览代码

MediaConnection

Michelle Bu 12 年之前
父节点
当前提交
b3dd6c7088
共有 3 个文件被更改,包括 63 次插入62 次删除
  1. 11 26
      lib/dataconnection.js
  2. 44 33
      lib/mediaconnection.js
  3. 8 3
      lib/peer.js

+ 11 - 26
lib/dataconnection.js

@@ -2,11 +2,11 @@
  * Wraps a DataChannel between two Peers.
  */
 function DataConnection(peer, provider, options) {
-  if (!(this instanceof DataConnection)) return new DataConnection(peer, options);
+  if (!(this instanceof DataConnection)) return new DataConnection(peer, provider, options);
   EventEmitter.call(this);
 
   // TODO: perhaps default serialization should be binary-utf8?
-  options = util.extend({
+  this.options = util.extend({
     serialization: 'binary'
   }, options);
 
@@ -16,30 +16,19 @@ function DataConnection(peer, provider, options) {
   this.peer = peer;
   this.provider = provider;
 
-  if (options) {
-    this.label = options.label;
-    this.metadata = options.metadata;
-    this.serialization = options.serialization;
-    this.reliable = options.reliable;
+  this.label = this.options.label;
+  this.metadata = this.options.metadata; // TODO: metadata could also be a part of the paylod.
+  this.serialization = this.options.serialization;
+  this.reliable = this.options.reliable;
 
-    this.id = options._id;
-  }
-
-  if (!this.id) {
-    this.id = DataConnection._idPrefix + util.randomToken();
-  }
+  this.id = this.options._id || DataConnection._idPrefix + util.randomToken();
 
   this._pc = Negotiator.startConnection(
     this.peer,
     this.id,
     this.provider,
-    (options && options.payload) ? options.payload : null
+    this.options._payload
   );
-
-  /*this._dc = dc;
-  if (this._dc) {
-    this._configureDataChannel();
-  }*/
 }
 
 util.inherits(DataConnection, EventEmitter);
@@ -49,6 +38,7 @@ DataConnection._idPrefix = 'dc_';
 /** Called by the Negotiator when the DataChannel is ready. */
 DataConnection.prototype.initialize = function(dc) {
   this._dc = dc;
+  this._configureDataChannel();
 }
 
 DataConnection.prototype._configureDataChannel = function() {
@@ -92,6 +82,7 @@ DataConnection.prototype._cleanup = function() {
     this._dc = null;
   }
   this.open = false;
+  // Negotiator will listen for this and take care of the PC if appropriate.
   this.emit('close');
 };
 
@@ -121,12 +112,6 @@ DataConnection.prototype._handleDataMessage = function(e) {
   this.emit('data', data);
 }
 
-DataConnection.prototype.addDC = function(dc) {
-  this._dc = dc;
-  this._configureDataChannel();
-}
-
-
 /**
  * Exposed functionality for users.
  */
@@ -142,7 +127,7 @@ DataConnection.prototype.close = function() {
 /** Allows user to send data. */
 DataConnection.prototype.send = function(data) {
   if (!this.open) {
-    this.emit('error', new Error('Connection no longer open.'));
+    this.emit('error', new Error('Connection is not open. You should listen for the `open` event before sending messages.'));
   }
   if (this._reliable) {
     // Note: reliable shim sending will make it so that you cannot customize

+ 44 - 33
lib/mediaconnection.js

@@ -1,39 +1,59 @@
 /**
  * Wraps the streaming interface between two Peers.
  */
-function MediaConnection(peer, localStream, options) {
-  if (!(this instanceof MediaConnection)) return new MediaConnection(peer, localStream, options);
+function MediaConnection(peer, provider, options) {
+  if (!(this instanceof MediaConnection)) return new MediaConnection(peer, provider, options);
   EventEmitter.call(this);
 
-  options = util.extend({
-   
-  }, options);
+  this.options = util.extend({}, options);
 
-  this.localStream = localStream;
+  this.open = false;
+  this.type = 'media';
   this.peer = peer;
-  
+  this.provider = provider;
+
+  this.metadata = this.options.metadata;
+  this.localStream = this.options._stream;
+
+  this.id = this.options._id || MediaConnection._idPrefix + util.randomToken();
+  if (this.localStream) {
+    this._pc = Negotiator.startConnection(
+      this.peer,
+      this.id,
+      this.provider,
+      { _stream: this.localStream }
+    )
+  }
 };
 
 util.inherits(MediaConnection, EventEmitter);
 
-MediaConnection.prototype.receiveStream = function(stream) {
-  console.log('receiving stream', stream);
-  this.remoteStream = stream;
-  this.emit('stream', stream);
-  //this._cleanup();
+MediaConnection._idPrefix = 'mc_';
+
+MediaConnection.prototype.addStream = function(remoteStream) {
+  util.log('Receiving stream', remoteStream);
+
+  this.remoteStream = remoteStream;
+  this.emit('stream', remoteStream); // Should we call this `open`?
+  this.open = true;
 };
 
 MediaConnection.prototype.answer = function(stream) {
-  this.localStream = stream;
-  this.emit('answer', stream);
-  //this._cleanup();
-};
+  if (this.localStream) {
+    // Throw some error.
+    return;
+  }
 
-MediaConnection.prototype.answered = function(stream) {
-  this.emit('stream', this.remoteStream);
-  //this._cleanup();
-};
+  this.options._payload._stream = stream;
 
+  this.localStream = stream;
+  this._pc = Negotiator.startConnection(
+    this.peer,
+    this.id,
+    this.provider,
+    this.options._payload
+  )
+};
 
 /**
  * Exposed functionality for users.
@@ -41,17 +61,8 @@ MediaConnection.prototype.answered = function(stream) {
 
 /** Allows user to close connection. */
 MediaConnection.prototype.close = function() {
-  //this._cleanup();
-};
-
-
-
-/**
- * Gets the brokering ID of the peer that you are connected with.
- * Note that this ID may be out of date if the peer has disconnected from the
- *  server, so it's not recommended that you use this ID to identify this
- *  connection.
- */
-MediaConnection.prototype.getPeer = function() {
-  return this.peer;
+  if (this.open) {
+    this.open = false;
+    this.emit('close')
+  }
 };

+ 8 - 3
lib/peer.js

@@ -177,14 +177,19 @@ Peer.prototype._handleMessage = function(message) {
         if (payload.type === 'call') {
           var call = new MediaConnection(peer, this, {
             _id: connectionId,
-            _payload: payload // A regular *Connection would have no payload.
+            _payload: payload, // A regular *Connection would have no payload.
+            metadata: payload.metadata,
           });
           this._addConnection(peer, call);
           this.emit('call', call);
         } else if (payload.type === 'connect') {
           var connection = new DataConnection(peer, this, {
             _id: connectionId,
-            _payload: payload
+            _payload: payload,
+            metadata: payload.metadata,
+            label: payload.label,
+            serialization: payload.serialization,
+            reliable: payload.reliable
           });
           this._addConnection(peer, connection);
           this.emit('connection', connection);
@@ -226,7 +231,7 @@ Peer.prototype.call = function(peer, stream, options) {
     util.error('To call a peer, you must provide a stream from your browser\'s `getUserMedia`.');
     return;
   }
-  options.stream = stream;
+  options._stream = stream;
   var call = new MediaConnection(peer, this, options);
   this._addConnection(peer, call);
   return call;