|
@@ -7,12 +7,12 @@ var Negotiator = {
|
|
|
pcs: {
|
|
|
data: {},
|
|
|
media: {}
|
|
|
- }, // type => {peer_id: {pc_id: pc}}.
|
|
|
+ }, // type => {peerId: {pc_id: pc}}.
|
|
|
//providers: {}, // provider's id => providers (there may be multiple providers/client.
|
|
|
queue: [] // connections that are delayed due to a PC being in use.
|
|
|
}
|
|
|
|
|
|
-Negotiator._idPrefix = 'pc_'
|
|
|
+Negotiator._idPrefix = 'pc_';
|
|
|
|
|
|
/** Returns a PeerConnection object set up correctly (for data, media). */
|
|
|
// Options preceeded with _ are ones we add artificially.
|
|
@@ -25,11 +25,14 @@ Negotiator.startConnection = function(connection, options) {
|
|
|
pc.addStream(options._stream);
|
|
|
}
|
|
|
|
|
|
+ // Set the connection's PC.
|
|
|
+ connection.pc = pc;
|
|
|
+
|
|
|
// What do we need to do now?
|
|
|
if (options.originator) {
|
|
|
if (connection.type === 'data') {
|
|
|
// Create the datachannel.
|
|
|
- var dc = pc.createDataChannel(options.label, {reliable: reliable});
|
|
|
+ var dc = pc.createDataChannel(connection.label, {reliable: options.reliable});
|
|
|
connection.initialize(dc);
|
|
|
}
|
|
|
|
|
@@ -39,8 +42,6 @@ Negotiator.startConnection = function(connection, options) {
|
|
|
} else {
|
|
|
Negotiator.handleSDP('OFFER', connection, options.sdp);
|
|
|
}
|
|
|
-
|
|
|
- return pc;
|
|
|
}
|
|
|
|
|
|
Negotiator._getPeerConnection = function(connection, options) {
|
|
@@ -51,14 +52,14 @@ Negotiator._getPeerConnection = function(connection, options) {
|
|
|
if (!Negotiator.pcs[connection.type][connection.peer]) {
|
|
|
Negotiator.pcs[connection.type][connection.peer] = {};
|
|
|
}
|
|
|
- peer_connections = Negotiator.pcs[connection.type][connection.peer];
|
|
|
+ var peerConnections = Negotiator.pcs[connection.type][connection.peer];
|
|
|
|
|
|
var pc;
|
|
|
if (options.multiplex) {
|
|
|
// Find an existing PC to use.
|
|
|
- ids = Object.keys(peer_connections);
|
|
|
+ ids = Object.keys(peerConnections);
|
|
|
for (var i = 0, ii = ids.length; i < ii; i += 1) {
|
|
|
- pc = peer_connections[ids[i]];
|
|
|
+ pc = peerConnections[ids[i]];
|
|
|
if (pc.signalingState === 'stable') {
|
|
|
break; // We can go ahead and use this PC.
|
|
|
}
|
|
@@ -101,22 +102,23 @@ Negotiator._startPeerConnection = function(connection) {
|
|
|
|
|
|
/** Set up various WebRTC listeners. */
|
|
|
Negotiator._setupListeners = function(connection, pc, pc_id) {
|
|
|
- var peer_id = connection.peer;
|
|
|
- var connection_id = connection.id;
|
|
|
+ var peerId = connection.peer;
|
|
|
+ var connectionId = connection.id;
|
|
|
var provider = connection.provider;
|
|
|
|
|
|
// ICE CANDIDATES.
|
|
|
util.log('Listening for ICE candidates.');
|
|
|
pc.onicecandidate = function(evt) {
|
|
|
if (evt.candidate) {
|
|
|
- util.log('Received ICE candidates.');
|
|
|
+ util.log('Received ICE candidates for:', connection.peer);
|
|
|
provider.socket.send({
|
|
|
type: 'CANDIDATE',
|
|
|
payload: {
|
|
|
candidate: evt.candidate,
|
|
|
- connection_id: connection.id
|
|
|
+ type: connection.type,
|
|
|
+ connectionId: connection.id
|
|
|
},
|
|
|
- dst: peer_id,
|
|
|
+ dst: peerId,
|
|
|
});
|
|
|
}
|
|
|
};
|
|
@@ -124,7 +126,7 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
|
|
|
pc.oniceconnectionstatechange = function() {
|
|
|
switch (pc.iceConnectionState) {
|
|
|
case 'failed':
|
|
|
- util.log('iceConnectionState is disconnected, closing connections to ' + peer_id);
|
|
|
+ util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
|
|
|
Negotiator._cleanup();
|
|
|
break;
|
|
|
case 'completed':
|
|
@@ -150,7 +152,7 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
|
|
|
pc.ondatachannel = function(evt) {
|
|
|
util.log('Received data channel');
|
|
|
var dc = evt.channel;
|
|
|
- var connection = provider.getConnection(peer_id, connection_id);
|
|
|
+ var connection = provider.getConnection(peerId, connectionId);
|
|
|
connection.initialize(dc);
|
|
|
};
|
|
|
|
|
@@ -159,21 +161,21 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
|
|
|
pc.onaddstream = function(evt) {
|
|
|
util.log('Received remote stream');
|
|
|
var stream = evt.stream;
|
|
|
- provider.getConnection(peer_id, id).receiveStream(stream);
|
|
|
+ provider.getConnection(peerId, id).receiveStream(stream);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-Negotiator._cleanup = function(provider, peer_id, connection_id) {
|
|
|
- // TODO
|
|
|
- util.log('Cleanup PeerConnection for ' + peer_id);
|
|
|
- if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
|
|
|
+Negotiator._cleanup = function(provider, peerId, connectionId) {
|
|
|
+ // TODO: close PeerConnection when all connections are closed.
|
|
|
+ util.log('Cleanup PeerConnection for ' + peerId);
|
|
|
+ /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
|
|
|
this.pc.close();
|
|
|
this.pc = null;
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
provider.socket.send({
|
|
|
type: 'LEAVE',
|
|
|
- dst: peer_id
|
|
|
+ dst: peerId
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -188,12 +190,13 @@ Negotiator._makeOffer = function(connection) {
|
|
|
}
|
|
|
|
|
|
pc.setLocalDescription(offer, function() {
|
|
|
- util.log('Set localDescription to offer');
|
|
|
+ util.log('Set localDescription: offer', 'for:', connection.peer);
|
|
|
connection.provider.socket.send({
|
|
|
type: 'OFFER',
|
|
|
payload: {
|
|
|
sdp: offer,
|
|
|
- connection_id: connection.id
|
|
|
+ type: connection.type,
|
|
|
+ connectionId: connection.id
|
|
|
},
|
|
|
dst: connection.peer,
|
|
|
});
|
|
@@ -219,12 +222,13 @@ Negotiator._makeAnswer = function(connection) {
|
|
|
}
|
|
|
|
|
|
pc.setLocalDescription(answer, function() {
|
|
|
- util.log('Set localDescription to answer.');
|
|
|
+ util.log('Set localDescription: answer', 'for:', connection.peer);
|
|
|
connection.provider.socket.send({
|
|
|
type: 'ANSWER',
|
|
|
payload: {
|
|
|
sdp: answer,
|
|
|
- connection_id: connection.id
|
|
|
+ type: connection.type,
|
|
|
+ connectionId: connection.id
|
|
|
},
|
|
|
dst: connection.peer
|
|
|
});
|
|
@@ -243,8 +247,9 @@ Negotiator.handleSDP = function(type, connection, sdp) {
|
|
|
sdp = new RTCSessionDescription(sdp);
|
|
|
var pc = connection.pc;
|
|
|
|
|
|
+ util.log('Setting remote description', sdp);
|
|
|
pc.setRemoteDescription(sdp, function() {
|
|
|
- util.log('Set remoteDescription: ' + type);
|
|
|
+ util.log('Set remoteDescription:', type, 'for:', connection.peer);
|
|
|
|
|
|
if (type === 'OFFER') {
|
|
|
if (connection.type === 'media') {
|
|
@@ -258,7 +263,7 @@ Negotiator.handleSDP = function(type, connection, sdp) {
|
|
|
});
|
|
|
}
|
|
|
// TODO. also, why setZeroTimeout up there?
|
|
|
- Negotiator._makeAnswer();
|
|
|
+ Negotiator._makeAnswer(connection);
|
|
|
}
|
|
|
}, function(err) {
|
|
|
connection.provider.emit('error', err);
|
|
@@ -270,7 +275,7 @@ Negotiator.handleSDP = function(type, connection, sdp) {
|
|
|
Negotiator.handleCandidate = function(connection, candidate) {
|
|
|
var candidate = new RTCIceCandidate(candidate);
|
|
|
connection.pc.addIceCandidate(candidate);
|
|
|
- util.log('Added ICE candidate.');
|
|
|
+ util.log('Added ICE candidate for:', connection.peer);
|
|
|
}
|
|
|
|
|
|
/** Handle peer leaving. */
|