123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /**
- * Manages all negotiations between Peers.
- */
- var Negotiator = {
- pcs: {
- data: {},
- media: {}
- }, // 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_';
- /** Returns a PeerConnection object set up correctly (for data, media). */
- Negotiator.startConnection = function(connection, options) {
- var pc = Negotiator._getPeerConnection(connection, options);
- if (connection.type === 'media' && options._stream) {
- // Add the stream.
- 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 config = {};
- if (util.supports.reliable && !options.reliable) {
- // If we have canonical reliable support...
- config = {maxRetransmits: 0}
- }
- var dc = pc.createDataChannel(connection.label, config);
- connection.initialize(dc);
- }
- if (!util.supports.onnegotiationneeded) {
- Negotiator._makeOffer(connection);
- }
- } else {
- Negotiator.handleSDP('OFFER', connection, options.sdp);
- }
- }
- Negotiator._getPeerConnection = function(connection, options) {
- if (!Negotiator.pcs[connection.type]) {
- util.error(connection.type + ' is not a valid connection type. Maybe you overrode the `type` property somewhere.');
- }
- if (!Negotiator.pcs[connection.type][connection.peer]) {
- Negotiator.pcs[connection.type][connection.peer] = {};
- }
- var peerConnections = Negotiator.pcs[connection.type][connection.peer];
- var pc;
- // Not multiplexing while FF and Chrome have not-great support for it.
- /*if (options.multiplex) {
- ids = Object.keys(peerConnections);
- for (var i = 0, ii = ids.length; i < ii; i += 1) {
- pc = peerConnections[ids[i]];
- if (pc.signalingState === 'stable') {
- break; // We can go ahead and use this PC.
- }
- }
- } else */
- if (options.pc) { // Simplest case: PC id already provided for us.
- pc = Negotiator.pcs[connection.type][connection.peer][options.pc];
- }
- if (!pc || pc.signalingState !== 'stable') {
- pc = Negotiator._startPeerConnection(connection);
- }
- return pc;
- }
- /*
- Negotiator._addProvider = function(provider) {
- if ((!provider.id && !provider.disconnected) || !provider.socket.open) {
- // Wait for provider to obtain an ID.
- provider.on('open', function(id) {
- Negotiator._addProvider(provider);
- });
- } else {
- Negotiator.providers[provider.id] = provider;
- }
- }*/
- /** Start a PC. */
- Negotiator._startPeerConnection = function(connection) {
- util.log('Creating RTCPeerConnection.');
- var id = Negotiator._idPrefix + util.randomToken();
- var optional = {};
- if (connection.type === 'data' && !util.supports.reliable) {
- optional = {optional: [{RtpDataChannels: true}]};
- } else if (connection.type === 'media') {
- // Interop req for chrome.
- optional = {optional: [{DtlsSrtpKeyAgreement: true}]};
- }
- var pc = new RTCPeerConnection(connection.provider.options.config, optional);
- Negotiator.pcs[connection.type][connection.peer][id] = pc;
- Negotiator._setupListeners(connection, pc, id);
- return pc;
- }
- /** Set up various WebRTC listeners. */
- Negotiator._setupListeners = function(connection, pc, pc_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 for:', connection.peer);
- provider.socket.send({
- type: 'CANDIDATE',
- payload: {
- candidate: evt.candidate,
- type: connection.type,
- connectionId: connection.id
- },
- dst: peerId,
- });
- }
- };
- pc.oniceconnectionstatechange = function() {
- switch (pc.iceConnectionState) {
- case 'disconnected':
- case 'failed':
- util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
- connection.close();
- break;
- case 'completed':
- pc.onicecandidate = util.noop;
- break;
- }
- };
- // Fallback for older Chrome impls.
- pc.onicechange = pc.oniceconnectionstatechange;
- // ONNEGOTIATIONNEEDED (Chrome)
- util.log('Listening for `negotiationneeded`');
- pc.onnegotiationneeded = function() {
- util.log('`negotiationneeded` triggered');
- if (pc.signalingState == 'stable') {
- Negotiator._makeOffer(connection);
- } else {
- util.log('onnegotiationneeded triggered when not stable. Is another connection being established?');
- }
- };
- // DATACONNECTION.
- util.log('Listening for data channel');
- // Fired between offer and answer, so options should already be saved
- // in the options hash.
- pc.ondatachannel = function(evt) {
- util.log('Received data channel');
- var dc = evt.channel;
- var connection = provider.getConnection(peerId, connectionId);
- connection.initialize(dc);
- };
- // MEDIACONNECTION.
- util.log('Listening for remote stream');
- pc.onaddstream = function(evt) {
- util.log('Received remote stream');
- var stream = evt.stream;
- provider.getConnection(peerId, connectionId).addStream(stream);
- };
- }
- Negotiator.cleanup = function(connection) {
- util.log('Cleaning up PeerConnection to ' + connection.peer);
- var pc = connection.pc;
- if (!!pc && (pc.readyState !== 'closed' || pc.signalingState !== 'closed')) {
- pc.close();
- connection.pc = null;
- }
- }
- Negotiator._makeOffer = function(connection) {
- var pc = connection.pc;
- pc.createOffer(function(offer) {
- util.log('Created offer.');
- if (!util.supports.reliable && connection.type === 'data' && connection.reliable) {
- offer.sdp = Reliable.higherBandwidthSDP(offer.sdp);
- }
- pc.setLocalDescription(offer, function() {
- util.log('Set localDescription: offer', 'for:', connection.peer);
- connection.provider.socket.send({
- type: 'OFFER',
- payload: {
- sdp: offer,
- type: connection.type,
- label: connection.label,
- reliable: connection.reliable,
- serialization: connection.serialization,
- metadata: connection.metadata,
- connectionId: connection.id
- },
- dst: connection.peer,
- });
- }, function(err) {
- connection.provider.emit('error', err);
- util.log('Failed to setLocalDescription, ', err);
- });
- }, function(err) {
- connection.provider.emit('error', err);
- util.log('Failed to createOffer, ', err);
- }, connection.options.constraints);
- }
- Negotiator._makeAnswer = function(connection) {
- var pc = connection.pc;
- pc.createAnswer(function(answer) {
- util.log('Created answer.');
- if (!util.supports.reliable && connection.type === 'data' && connection.reliable) {
- answer.sdp = Reliable.higherBandwidthSDP(answer.sdp);
- }
- pc.setLocalDescription(answer, function() {
- util.log('Set localDescription: answer', 'for:', connection.peer);
- connection.provider.socket.send({
- type: 'ANSWER',
- payload: {
- sdp: answer,
- type: connection.type,
- connectionId: connection.id
- },
- dst: connection.peer
- });
- }, function(err) {
- connection.provider.emit('error', err);
- util.log('Failed to setLocalDescription, ', err);
- });
- }, function(err) {
- connection.provider.emit('error', err);
- util.log('Failed to create answer, ', err);
- });
- }
- /** Handle an SDP. */
- 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, 'for:', connection.peer);
- if (type === 'OFFER') {
- Negotiator._makeAnswer(connection);
- }
- }, function(err) {
- connection.provider.emit('error', err);
- util.log('Failed to setRemoteDescription, ', err);
- });
- }
- /** Handle a candidate. */
- Negotiator.handleCandidate = function(connection, ice) {
- var candidate = ice.candidate;
- var sdpMLineIndex = ice.sdpMLineIndex;
- connection.pc.addIceCandidate(new RTCIceCandidate({
- sdpMLineIndex: sdpMLineIndex,
- candidate: candidate
- }));
- util.log('Added ICE candidate for:', connection.peer);
- }
|