|
@@ -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')
|
|
|
+ }
|
|
|
};
|