|
@@ -752,6 +752,15 @@ var util = {
|
|
randomPort: function() {
|
|
randomPort: function() {
|
|
return Math.round(Math.random() * 60535) + 5000;
|
|
return Math.round(Math.random() * 60535) + 5000;
|
|
},
|
|
},
|
|
|
|
+
|
|
|
|
+ log: function () {
|
|
|
|
+ if (debug) {
|
|
|
|
+ for (var i = 0; i < arguments.length; i++) {
|
|
|
|
+ console.log('*', i, '-- ', arguments[i]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+
|
|
setZeroTimeout: (function(global) {
|
|
setZeroTimeout: (function(global) {
|
|
var timeouts = [];
|
|
var timeouts = [];
|
|
var messageName = 'zero-timeout-message';
|
|
var messageName = 'zero-timeout-message';
|
|
@@ -818,23 +827,40 @@ function Peer(options) {
|
|
|
|
|
|
EventEmitter.call(this);
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
+ options = util.extend({
|
|
|
|
+ // Defaults
|
|
|
|
+ }, options);
|
|
|
|
+
|
|
|
|
+
|
|
this._config = options.config || { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] };
|
|
this._config = options.config || { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] };
|
|
|
|
+ this._id = null;
|
|
|
|
+ // User handlers.
|
|
|
|
+ this._handlers = {};
|
|
|
|
+
|
|
|
|
+ // Source to connect to; null if waiting for a connection.
|
|
this._peer = options.source || null;
|
|
this._peer = options.source || null;
|
|
|
|
+
|
|
|
|
+ // Booleans to determine what streams to allow.
|
|
this._video = options.video;
|
|
this._video = options.video;
|
|
this._data = options.data != undefined ? options.data : true;
|
|
this._data = options.data != undefined ? options.data : true;
|
|
this._audio = options.audio;
|
|
this._audio = options.audio;
|
|
|
|
+
|
|
|
|
+ // Connections
|
|
this._pc = null;
|
|
this._pc = null;
|
|
- this._id = null;
|
|
|
|
this._dc = null;
|
|
this._dc = null;
|
|
this._socket = new WebSocket(options.ws || 'ws://localhost');
|
|
this._socket = new WebSocket(options.ws || 'ws://localhost');
|
|
|
|
+
|
|
|
|
+ // Local streams for multiple use.
|
|
|
|
+ this._localVideo = options.localVideo || null;
|
|
|
|
+ this._localAudio = options.localAudio || null;
|
|
|
|
+
|
|
|
|
+ // Init socket msg handlers
|
|
var self = this;
|
|
var self = this;
|
|
this._socket.onopen = function() {
|
|
this._socket.onopen = function() {
|
|
self.socketInit();
|
|
self.socketInit();
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
|
|
- // Testing firefox.
|
|
|
|
- // MULTICONNECTION doesn't work still.
|
|
|
|
|
|
+ // Firefoxism: connectDataConnection ports.
|
|
if (browserisms == 'Firefox' && !options.source) {
|
|
if (browserisms == 'Firefox' && !options.source) {
|
|
if (!Peer.usedPorts) {
|
|
if (!Peer.usedPorts) {
|
|
Peer.usedPorts = [];
|
|
Peer.usedPorts = [];
|
|
@@ -856,11 +882,12 @@ function Peer(options) {
|
|
|
|
|
|
util.inherits(Peer, EventEmitter);
|
|
util.inherits(Peer, EventEmitter);
|
|
|
|
|
|
|
|
+
|
|
/** Start up websocket communications. */
|
|
/** Start up websocket communications. */
|
|
Peer.prototype.socketInit = function() {
|
|
Peer.prototype.socketInit = function() {
|
|
var self = this;
|
|
var self = this;
|
|
- // Multiple sinks to one source.
|
|
|
|
if (!!this._peer) {
|
|
if (!!this._peer) {
|
|
|
|
+ // Announce as a sink if initiated with a source.
|
|
this._socket.send(JSON.stringify({
|
|
this._socket.send(JSON.stringify({
|
|
type: 'SINK',
|
|
type: 'SINK',
|
|
source: this._peer,
|
|
source: this._peer,
|
|
@@ -881,24 +908,36 @@ Peer.prototype.socketInit = function() {
|
|
try {
|
|
try {
|
|
sdp = new RTCSessionDescription(message.sdp);
|
|
sdp = new RTCSessionDescription(message.sdp);
|
|
} catch(e) {
|
|
} catch(e) {
|
|
- console.log('Firefox');
|
|
|
|
|
|
+ util.log('Firefox');
|
|
}
|
|
}
|
|
self._pc.setRemoteDescription(sdp, function() {
|
|
self._pc.setRemoteDescription(sdp, function() {
|
|
- console.log('setRemoteDescription: offer');
|
|
|
|
|
|
+ util.log('setRemoteDescription: offer');
|
|
|
|
|
|
// If we also have to set up a stream on the sink end, do so.
|
|
// If we also have to set up a stream on the sink end, do so.
|
|
self.handleStream(false, function() {
|
|
self.handleStream(false, function() {
|
|
self.maybeBrowserisms(false);
|
|
self.maybeBrowserisms(false);
|
|
});
|
|
});
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- console.log('failed to setRemoteDescription with offer, ', err);
|
|
|
|
|
|
+ util.log('failed to setRemoteDescription with offer, ', err);
|
|
});
|
|
});
|
|
break;
|
|
break;
|
|
case 'CANDIDATE':
|
|
case 'CANDIDATE':
|
|
- console.log(message.candidate);
|
|
|
|
|
|
+ util.log(message.candidate);
|
|
var candidate = new RTCIceCandidate(message.candidate);
|
|
var candidate = new RTCIceCandidate(message.candidate);
|
|
self._pc.addIceCandidate(candidate);
|
|
self._pc.addIceCandidate(candidate);
|
|
break;
|
|
break;
|
|
|
|
+ case 'LEAVE':
|
|
|
|
+ util.log('counterpart disconnected');
|
|
|
|
+ if (!!self._pc && self._pc.readyState != 'closed') {
|
|
|
|
+ self._pc.close();
|
|
|
|
+ self._pc = null;
|
|
|
|
+ self._peer = null;
|
|
|
|
+ }
|
|
|
|
+ if (!!self._dc && self._dc.readyState != 'closed') {
|
|
|
|
+ self._dc.close();
|
|
|
|
+ self._dc = null;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
case 'PORT':
|
|
case 'PORT':
|
|
if (browserisms && browserisms == 'Firefox') {
|
|
if (browserisms && browserisms == 'Firefox') {
|
|
if (!Peer.usedPorts) {
|
|
if (!Peer.usedPorts) {
|
|
@@ -910,14 +949,14 @@ Peer.prototype.socketInit = function() {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
case 'DEFAULT':
|
|
case 'DEFAULT':
|
|
- console.log('SINK: unrecognized message ', message.type);
|
|
|
|
|
|
+ util.log('SINK: unrecognized message ', message.type);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
} else {
|
|
} else {
|
|
// Otherwise, this sink is the originator to another sink and should wait
|
|
// Otherwise, this sink is the originator to another sink and should wait
|
|
- // for an alert.
|
|
|
|
|
|
+ // for an alert to begin the PC process.
|
|
this._socket.send(JSON.stringify({
|
|
this._socket.send(JSON.stringify({
|
|
type: 'SOURCE',
|
|
type: 'SOURCE',
|
|
isms: browserisms
|
|
isms: browserisms
|
|
@@ -943,10 +982,10 @@ Peer.prototype.socketInit = function() {
|
|
try {
|
|
try {
|
|
sdp = new RTCSessionDescription(message.sdp);
|
|
sdp = new RTCSessionDescription(message.sdp);
|
|
} catch(e) {
|
|
} catch(e) {
|
|
- console.log('Firefox');
|
|
|
|
|
|
+ util.log('Firefox');
|
|
}
|
|
}
|
|
self._pc.setRemoteDescription(sdp, function() {
|
|
self._pc.setRemoteDescription(sdp, function() {
|
|
- console.log('setRemoteDescription: answer');
|
|
|
|
|
|
+ util.log('setRemoteDescription: answer');
|
|
// Firefoxism
|
|
// Firefoxism
|
|
if (browserisms == 'Firefox') {
|
|
if (browserisms == 'Firefox') {
|
|
self._pc.connectDataConnection(self.localPort, self.remotePort);
|
|
self._pc.connectDataConnection(self.localPort, self.remotePort);
|
|
@@ -957,29 +996,42 @@ Peer.prototype.socketInit = function() {
|
|
local: self.remotePort
|
|
local: self.remotePort
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
- console.log('ORIGINATOR: PeerConnection success');
|
|
|
|
|
|
+ util.log('ORIGINATOR: PeerConnection success');
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- console.log('failed to setRemoteDescription, ', err);
|
|
|
|
|
|
+ util.log('failed to setRemoteDescription, ', err);
|
|
});
|
|
});
|
|
break;
|
|
break;
|
|
case 'CANDIDATE':
|
|
case 'CANDIDATE':
|
|
- console.log(message.candidate);
|
|
|
|
|
|
+ util.log(message.candidate);
|
|
var candidate = new RTCIceCandidate(message.candidate);
|
|
var candidate = new RTCIceCandidate(message.candidate);
|
|
self._pc.addIceCandidate(candidate);
|
|
self._pc.addIceCandidate(candidate);
|
|
break;
|
|
break;
|
|
|
|
+ case 'LEAVE':
|
|
|
|
+ util.log('counterpart disconnected');
|
|
|
|
+ if (!!self._pc && self._pc.readyState != 'closed') {
|
|
|
|
+ self._pc.close();
|
|
|
|
+ self._pc = null;
|
|
|
|
+ self._peer = null;
|
|
|
|
+ }
|
|
|
|
+ if (!!self._dc && self._dc.readyState != 'closed') {
|
|
|
|
+ self._dc.close();
|
|
|
|
+ self._dc = null;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
case 'DEFAULT':
|
|
case 'DEFAULT':
|
|
- console.log('ORIGINATOR: message not recognized ', message.type);
|
|
|
|
|
|
+ util.log('ORIGINATOR: message not recognized ', message.type);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
}
|
|
}
|
|
- // Makes sure things clean up neatly.
|
|
|
|
|
|
+
|
|
|
|
+ // Makes sure things clean up neatly when window is closed.
|
|
window.onbeforeunload = function() {
|
|
window.onbeforeunload = function() {
|
|
- if (!!self._pc) {
|
|
|
|
|
|
+ if (!!self._pc && self._pc.readyState != 'closed') {
|
|
self._pc.close();
|
|
self._pc.close();
|
|
}
|
|
}
|
|
if (!!self._socket && !!self._peer) {
|
|
if (!!self._socket && !!self._peer) {
|
|
self._socket.send(JSON.stringify({ type: 'LEAVE', dst: self._peer }));
|
|
self._socket.send(JSON.stringify({ type: 'LEAVE', dst: self._peer }));
|
|
- if (!!self._dc) {
|
|
|
|
|
|
+ if (!!self._dc && self._dc.readyState != 'closed') {
|
|
self._dc.close();
|
|
self._dc.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -991,7 +1043,7 @@ Peer.prototype.socketInit = function() {
|
|
Peer.prototype.setupIce = function() {
|
|
Peer.prototype.setupIce = function() {
|
|
var self = this;
|
|
var self = this;
|
|
this._pc.onicecandidate = function(event) {
|
|
this._pc.onicecandidate = function(event) {
|
|
- console.log('candidates received');
|
|
|
|
|
|
+ util.log('candidates received');
|
|
if (event.candidate) {
|
|
if (event.candidate) {
|
|
self._socket.send(JSON.stringify({
|
|
self._socket.send(JSON.stringify({
|
|
type: 'CANDIDATE',
|
|
type: 'CANDIDATE',
|
|
@@ -999,7 +1051,7 @@ Peer.prototype.setupIce = function() {
|
|
dst: self._peer
|
|
dst: self._peer
|
|
}));
|
|
}));
|
|
} else {
|
|
} else {
|
|
- console.log("End of candidates.");
|
|
|
|
|
|
+ util.log("End of candidates.");
|
|
}
|
|
}
|
|
};
|
|
};
|
|
};
|
|
};
|
|
@@ -1016,7 +1068,7 @@ Peer.prototype.startPeerConnection = function() {
|
|
/** Decide whether to handle Firefoxisms. */
|
|
/** Decide whether to handle Firefoxisms. */
|
|
Peer.prototype.maybeBrowserisms = function(originator) {
|
|
Peer.prototype.maybeBrowserisms = function(originator) {
|
|
var self = this;
|
|
var self = this;
|
|
- if (browserisms == 'Firefox' && !this._video && !this._audio && !this._stream) {
|
|
|
|
|
|
+ if (browserisms == 'Firefox' && !this._video && !this._audio/* && !this._stream*/) {
|
|
getUserMedia({ audio: true, fake: true }, function(s) {
|
|
getUserMedia({ audio: true, fake: true }, function(s) {
|
|
self._pc.addStream(s);
|
|
self._pc.addStream(s);
|
|
|
|
|
|
@@ -1026,7 +1078,7 @@ Peer.prototype.maybeBrowserisms = function(originator) {
|
|
self.makeAnswer();
|
|
self.makeAnswer();
|
|
}
|
|
}
|
|
|
|
|
|
- }, function(err) { console.log('crap'); });
|
|
|
|
|
|
+ }, function(err) { util.log('crap'); });
|
|
} else {
|
|
} else {
|
|
if (originator) {
|
|
if (originator) {
|
|
this.makeOffer();
|
|
this.makeOffer();
|
|
@@ -1042,9 +1094,9 @@ Peer.prototype.makeAnswer = function() {
|
|
var self = this;
|
|
var self = this;
|
|
|
|
|
|
this._pc.createAnswer(function(answer) {
|
|
this._pc.createAnswer(function(answer) {
|
|
- console.log('createAnswer');
|
|
|
|
|
|
+ util.log('createAnswer');
|
|
self._pc.setLocalDescription(answer, function() {
|
|
self._pc.setLocalDescription(answer, function() {
|
|
- console.log('setLocalDescription: answer');
|
|
|
|
|
|
+ util.log('setLocalDescription: answer');
|
|
self._socket.send(JSON.stringify({
|
|
self._socket.send(JSON.stringify({
|
|
type: 'ANSWER',
|
|
type: 'ANSWER',
|
|
src: self._id,
|
|
src: self._id,
|
|
@@ -1052,10 +1104,10 @@ Peer.prototype.makeAnswer = function() {
|
|
dst: self._peer
|
|
dst: self._peer
|
|
}));
|
|
}));
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- console.log('failed to setLocalDescription, ', err)
|
|
|
|
|
|
+ util.log('failed to setLocalDescription, ', err)
|
|
});
|
|
});
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- console.log('failed to create answer, ', err)
|
|
|
|
|
|
+ util.log('failed to create answer, ', err)
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1065,9 +1117,9 @@ Peer.prototype.makeOffer = function() {
|
|
var self = this;
|
|
var self = this;
|
|
|
|
|
|
this._pc.createOffer(function(offer) {
|
|
this._pc.createOffer(function(offer) {
|
|
- console.log('createOffer')
|
|
|
|
|
|
+ util.log('createOffer')
|
|
self._pc.setLocalDescription(offer, function() {
|
|
self._pc.setLocalDescription(offer, function() {
|
|
- console.log('setLocalDescription: offer');
|
|
|
|
|
|
+ util.log('setLocalDescription: offer');
|
|
self._socket.send(JSON.stringify({
|
|
self._socket.send(JSON.stringify({
|
|
type: 'OFFER',
|
|
type: 'OFFER',
|
|
sdp: offer,
|
|
sdp: offer,
|
|
@@ -1075,7 +1127,7 @@ Peer.prototype.makeOffer = function() {
|
|
src: self._id
|
|
src: self._id
|
|
}));
|
|
}));
|
|
}, function(err) {
|
|
}, function(err) {
|
|
- console.log('failed to setLocalDescription, ', err);
|
|
|
|
|
|
+ util.log('failed to setLocalDescription, ', err);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
};
|
|
};
|
|
@@ -1084,10 +1136,10 @@ Peer.prototype.makeOffer = function() {
|
|
/** Sets up A/V stream handler. */
|
|
/** Sets up A/V stream handler. */
|
|
Peer.prototype.setupAudioVideo = function() {
|
|
Peer.prototype.setupAudioVideo = function() {
|
|
var self = this;
|
|
var self = this;
|
|
- console.log('onaddstream handler added');
|
|
|
|
|
|
+ util.log('onaddstream handler added');
|
|
this._pc.onaddstream = function(obj) {
|
|
this._pc.onaddstream = function(obj) {
|
|
- console.log('Remote stream added');
|
|
|
|
- this._stream = true;
|
|
|
|
|
|
+ util.log('Remote stream added');
|
|
|
|
+ // this._stream = true;
|
|
self.emit('remotestream', obj.type, obj.stream);
|
|
self.emit('remotestream', obj.type, obj.stream);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
@@ -1105,35 +1157,49 @@ Peer.prototype.handleStream = function(originator, cb) {
|
|
/** Get A/V streams. */
|
|
/** Get A/V streams. */
|
|
Peer.prototype.getAudioVideo = function(originator, cb) {
|
|
Peer.prototype.getAudioVideo = function(originator, cb) {
|
|
var self = this;
|
|
var self = this;
|
|
- if (this._video) {
|
|
|
|
|
|
+ if (this._video && !this._localVideo) {
|
|
getUserMedia({ video: true }, function(vstream) {
|
|
getUserMedia({ video: true }, function(vstream) {
|
|
self._pc.addStream(vstream);
|
|
self._pc.addStream(vstream);
|
|
- console.log('Local video stream added');
|
|
|
|
|
|
+ self._localVideo = vstream;
|
|
|
|
+ util.log('Local video stream added');
|
|
|
|
|
|
self.emit('localstream', 'video', vstream);
|
|
self.emit('localstream', 'video', vstream);
|
|
|
|
|
|
- if (self._audio) {
|
|
|
|
|
|
+ if (self._audio && !self._localAudio) {
|
|
getUserMedia({ audio: true }, function(astream) {
|
|
getUserMedia({ audio: true }, function(astream) {
|
|
self._pc.addStream(astream);
|
|
self._pc.addStream(astream);
|
|
- console.log('Local audio stream added');
|
|
|
|
|
|
+ self._localAudio = astream;
|
|
|
|
+ util.log('Local audio stream added');
|
|
|
|
|
|
self.emit('localstream', 'audio', astream);
|
|
self.emit('localstream', 'audio', astream);
|
|
|
|
|
|
cb();
|
|
cb();
|
|
- }, function(err) { console.log('Audio cannot start'); cb(); });
|
|
|
|
|
|
+ }, function(err) { util.log('Audio cannot start'); cb(); });
|
|
} else {
|
|
} else {
|
|
|
|
+ if (self._audio) {
|
|
|
|
+ self._pc.addStream(self._localAudio);
|
|
|
|
+ }
|
|
cb();
|
|
cb();
|
|
}
|
|
}
|
|
- }, function(err) { console.log('Video cannot start', err); cb(); });
|
|
|
|
- } else if (this._audio) {
|
|
|
|
|
|
+ }, function(err) { util.log('Video cannot start', err); cb(); });
|
|
|
|
+ } else if (this._audio && !this._localAudio) {
|
|
getUserMedia({ audio: true }, function(astream) {
|
|
getUserMedia({ audio: true }, function(astream) {
|
|
self._pc.addStream(astream);
|
|
self._pc.addStream(astream);
|
|
|
|
+ self._localAudio = astream;
|
|
|
|
+ util.log('Local audio stream added');
|
|
|
|
|
|
self.emit('localstream', 'audio', astream);
|
|
self.emit('localstream', 'audio', astream);
|
|
|
|
|
|
cb();
|
|
cb();
|
|
- }, function(err) { console.log('Audio cannot start'); cb(); });
|
|
|
|
|
|
+ }, function(err) { util.log('Audio cannot start'); cb(); });
|
|
} else {
|
|
} else {
|
|
|
|
+ if (this._audio) {
|
|
|
|
+ this._pc.addStream(this._localAudio);
|
|
|
|
+ }
|
|
|
|
+ if (this._video) {
|
|
|
|
+ this._pc.addStream(this._localVideo);
|
|
|
|
+ }
|
|
|
|
+ util.log('no audio/video streams initiated');
|
|
cb();
|
|
cb();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1148,9 +1214,9 @@ Peer.prototype.setupDataChannel = function(originator, cb) {
|
|
if (browserisms == 'Webkit') {
|
|
if (browserisms == 'Webkit') {
|
|
|
|
|
|
this._pc.onstatechange = function() {
|
|
this._pc.onstatechange = function() {
|
|
- console.log('State Change: ', self._pc.readyState);
|
|
|
|
|
|
+ util.log('State Change: ', self._pc.readyState);
|
|
/*if (self._pc.readyState == 'active') {
|
|
/*if (self._pc.readyState == 'active') {
|
|
- console.log('ORIGINATOR: active state detected');
|
|
|
|
|
|
+ util.log('ORIGINATOR: active state detected');
|
|
|
|
|
|
self._dc = self._pc.createDataChannel('StreamAPI', { reliable: false });
|
|
self._dc = self._pc.createDataChannel('StreamAPI', { reliable: false });
|
|
self._dc.binaryType = 'blob';
|
|
self._dc.binaryType = 'blob';
|
|
@@ -1167,7 +1233,7 @@ Peer.prototype.setupDataChannel = function(originator, cb) {
|
|
|
|
|
|
} else {
|
|
} else {
|
|
this._pc.onconnection = function() {
|
|
this._pc.onconnection = function() {
|
|
- console.log('ORIGINATOR: onconnection triggered');
|
|
|
|
|
|
+ util.log('ORIGINATOR: onconnection triggered');
|
|
|
|
|
|
self.startDataChannel();
|
|
self.startDataChannel();
|
|
};
|
|
};
|
|
@@ -1175,7 +1241,7 @@ Peer.prototype.setupDataChannel = function(originator, cb) {
|
|
} else {
|
|
} else {
|
|
/** TARGET SETUP */
|
|
/** TARGET SETUP */
|
|
this._pc.ondatachannel = function(dc) {
|
|
this._pc.ondatachannel = function(dc) {
|
|
- console.log('SINK: ondatachannel triggered');
|
|
|
|
|
|
+ util.log('SINK: ondatachannel triggered');
|
|
self._dc = dc;
|
|
self._dc = dc;
|
|
self._dc.binaryType = 'blob';
|
|
self._dc.binaryType = 'blob';
|
|
|
|
|
|
@@ -1187,7 +1253,7 @@ Peer.prototype.setupDataChannel = function(originator, cb) {
|
|
};
|
|
};
|
|
|
|
|
|
this._pc.onconnection = function() {
|
|
this._pc.onconnection = function() {
|
|
- console.log('SINK: onconnection triggered');
|
|
|
|
|
|
+ util.log('SINK: onconnection triggered');
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1235,4 +1301,5 @@ Peer.prototype.handleDataMessage = function(e) {
|
|
|
|
|
|
exports.Peer = Peer;
|
|
exports.Peer = Peer;
|
|
|
|
|
|
|
|
+
|
|
})(this);
|
|
})(this);
|