|
@@ -1,49 +1,41 @@
|
|
|
/**
|
|
|
- * Manages DataConnections between its peer and one other peer.
|
|
|
- * Internally, manages PeerConnection.
|
|
|
+ * Manages all negotiations between Peers.
|
|
|
*/
|
|
|
-function ConnectionManager(managerId, peer, options) {
|
|
|
- if (!(this instanceof ConnectionManager)) return new ConnectionManager(id, peer, options);
|
|
|
- EventEmitter.call(this);
|
|
|
+var Negotiator = {
|
|
|
+ pcs: {}, // pc id => pc.
|
|
|
+ providers: {} // peer id => provider.
|
|
|
+};
|
|
|
|
|
|
- options = util.extend({
|
|
|
- config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] }
|
|
|
- }, options);
|
|
|
- this._options = options;
|
|
|
+Negotiator._idPrefix = 'pc_'
|
|
|
|
|
|
- // PeerConnection is not yet dead.
|
|
|
- this.destroyed = false;
|
|
|
+Negotiator.startConnection = function(peer, connection, provider, options) {
|
|
|
+ // TODO
|
|
|
+ if (!Negotiator.providers[peer]) {
|
|
|
+ Negotiator.providers[peer] = provider;
|
|
|
+ }
|
|
|
|
|
|
- this.peer = peer;
|
|
|
- this.pc = null;
|
|
|
+ var pc;
|
|
|
+ if (!options || !options._pc) {
|
|
|
+ Negotiator._startPeerConnection(provider);
|
|
|
+ }
|
|
|
|
|
|
- // Mapping labels to metadata and serialization.
|
|
|
- // label => { metadata: ..., serialization: ..., reliable: ...}
|
|
|
- this.labels = {};
|
|
|
- // A default label in the event that none are passed in.
|
|
|
- this._default = 0;
|
|
|
+ if (options) {
|
|
|
+ pc =
|
|
|
+ if (options._stream) {
|
|
|
+ if (options.sdp) { // Is a MC receiver.
|
|
|
+ Negotiator.handleSDP(peer, connection, options);
|
|
|
+ } else { // Is a MC originator.
|
|
|
|
|
|
- // DataConnections on this PC.
|
|
|
- this.connections = {};
|
|
|
-
|
|
|
- // Media call on this PC
|
|
|
- this._call = null;
|
|
|
-
|
|
|
- // Queue of incomplete calls and connections
|
|
|
- this._queued = [];
|
|
|
-
|
|
|
- // The id of this manager
|
|
|
- this._managerId = managerId;
|
|
|
-
|
|
|
-};
|
|
|
+ }
|
|
|
+ } else { // Is a DC receiver.
|
|
|
+
|
|
|
+ }
|
|
|
+ } else { // Is a DC originator.
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
-util.inherits(ConnectionManager, EventEmitter);
|
|
|
|
|
|
-ConnectionManager.prototype.initialize = function(id, socket) {
|
|
|
- // The local peer id
|
|
|
- this.id = id;
|
|
|
-
|
|
|
- this._socket = socket;
|
|
|
+ // Return the PeerConnection.
|
|
|
|
|
|
// Set up PeerConnection.
|
|
|
this._startPeerConnection();
|
|
@@ -64,13 +56,15 @@ ConnectionManager.prototype.initialize = function(id, socket) {
|
|
|
// Listen for remote streams.
|
|
|
this._setupStreamListener();
|
|
|
|
|
|
- this.initialize = function() { };
|
|
|
-};
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/** Start a PC. */
|
|
|
-ConnectionManager.prototype._startPeerConnection = function() {
|
|
|
- util.log('Creating RTCPeerConnection');
|
|
|
- this.pc = new RTCPeerConnection(this._options.config, { optional: [ { RtpDataChannels: true } ]});
|
|
|
+Negotiator._startPeerConnection = function(provider) {
|
|
|
+ util.log('Creating RTCPeerConnection.');
|
|
|
+
|
|
|
+ var id = Negotiator._idPrefix + util.randomToken();
|
|
|
+ Negotiator.pcs[id] = new RTCPeerConnection(provider.options.config, { optional: [ { RtpDataChannels: true } ]});
|
|
|
};
|
|
|
|
|
|
/** Add DataChannels to all queued DataConnections. */
|