|
@@ -12,42 +12,31 @@ function SourcePeer(options) {
|
|
|
this.socketInit();
|
|
|
};
|
|
|
|
|
|
+
|
|
|
SourcePeer.prototype.socketInit = function() {
|
|
|
- self = this;
|
|
|
+ var self = this;
|
|
|
this._socket.emit('source', function(data) {
|
|
|
self._id = data.id;
|
|
|
|
|
|
+ if (!!self._readyHandler) {
|
|
|
+ self._readyHandler(self._id);
|
|
|
+ }
|
|
|
+
|
|
|
self._socket.on('sink-connected', function(data) {
|
|
|
- console.log('SINK CONNECTED');
|
|
|
- // TODO: not just moz.
|
|
|
target = data.sink;
|
|
|
- var pc = new window.mozRTCPeerConnection(self._config);
|
|
|
+ var pc = new RTCPeerConnection(self._config);
|
|
|
self._pcs[target] = pc;
|
|
|
- // Setups will create streams--then the callback sets up the offers.
|
|
|
- self.handleStream(pc, target, function() {
|
|
|
- // Firefoxisms, or just dumb?
|
|
|
- navigator.mozGetUserMedia({ audio: true, fake: true }, function(s) {
|
|
|
- pc.addStream(s);
|
|
|
- pc.createOffer(function(offer) {
|
|
|
- pc.setLocalDescription(offer, function() {
|
|
|
- self._socket.emit('offer',
|
|
|
- { 'sdp': offer,
|
|
|
- 'sink': target,
|
|
|
- 'source': self._id });
|
|
|
- }, function(err) {
|
|
|
- console.log('failed to setLocalDescription, ', err);
|
|
|
- });
|
|
|
- });
|
|
|
- }, function(err) { console.log('crap'); });
|
|
|
+ self.handleStream(pc, target, function(pc, target) {
|
|
|
+ self.maybeBrowserisms(pc, target);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
self._socket.on('answer', function(data) {
|
|
|
self._pcs[data.sink].setRemoteDescription(data.sdp, function() {
|
|
|
// Firefoxism
|
|
|
- console.log('FIREFOX', new Date());
|
|
|
- self._pcs[data.sink].connectDataConnection(5000, 5001);
|
|
|
- console.log('FIREFOX-2');
|
|
|
+ if (browserisms == 'Firefox') {
|
|
|
+ self._pcs[data.sink].connectDataConnection(5000, 5001);
|
|
|
+ }
|
|
|
console.log('SOURCE: PeerConnection success');
|
|
|
}, function(err) {
|
|
|
console.log('failed to setRemoteDescription, ', err)
|
|
@@ -56,6 +45,39 @@ SourcePeer.prototype.socketInit = function() {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+// Stream Firefoxism... can be removed when DataChannel no longer requires
|
|
|
+// a stream.
|
|
|
+SourcePeer.prototype.maybeBrowserisms = function(pc, target) {
|
|
|
+ var self = this;
|
|
|
+ if (browserisms == 'Firefox') {
|
|
|
+ getUserMedia({ audio: true, fake: true }, function(s) {
|
|
|
+ pc.addStream(s);
|
|
|
+ self.makeOffer(target);
|
|
|
+ }, function(err) { console.log('crap'); });
|
|
|
+ } else {
|
|
|
+ this.makeOffer(target);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+// Make an offer.
|
|
|
+SourcePeer.prototype.makeOffer = function(target) {
|
|
|
+ var pc = this._pcs[target];
|
|
|
+ var self = this;
|
|
|
+ pc.createOffer(function(offer) {
|
|
|
+ pc.setLocalDescription(offer, function() {
|
|
|
+ self._socket.emit('offer',
|
|
|
+ { 'sdp': offer,
|
|
|
+ 'sink': target,
|
|
|
+ 'source': self._id });
|
|
|
+ }, function(err) {
|
|
|
+ console.log('failed to setLocalDescription, ', err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
// Based on stream type requested, sets up the stream for PC.
|
|
|
SourcePeer.prototype.handleStream = function(pc, target, cb) {
|
|
|
pc.onaddstream = function(obj) {
|
|
@@ -77,13 +99,20 @@ SourcePeer.prototype.handleStream = function(pc, target, cb) {
|
|
|
}*/
|
|
|
};
|
|
|
|
|
|
+
|
|
|
SourcePeer.prototype.setupDataChannel = function(pc, target, cb) {
|
|
|
- self = this;
|
|
|
+ var self = this;
|
|
|
pc.onconnection = function() {
|
|
|
console.log('SOURCE: onconnection triggered.');
|
|
|
var dc = pc.createDataChannel(self._name, {}, target);
|
|
|
self._dcs[target] = dc;
|
|
|
dc.binaryType = 'arraybuffer';
|
|
|
+
|
|
|
+ // User handler
|
|
|
+ if (!!self._sinkHandler) {
|
|
|
+ self._sinkHandler(target);
|
|
|
+ }
|
|
|
+
|
|
|
dc.onmessage = function(e) {
|
|
|
self.handleDataMessage(e);
|
|
|
};
|
|
@@ -96,9 +125,10 @@ SourcePeer.prototype.setupDataChannel = function(pc, target, cb) {
|
|
|
pc.onclosedconnection = function() {
|
|
|
// ??
|
|
|
};
|
|
|
- cb();
|
|
|
+ cb(pc, target);
|
|
|
};
|
|
|
|
|
|
+
|
|
|
SourcePeer.prototype.send = function(data, sink) {
|
|
|
// TODO: try/catch
|
|
|
var ab = MsgPack.encode(data);
|
|
@@ -110,12 +140,12 @@ SourcePeer.prototype.send = function(data, sink) {
|
|
|
|
|
|
for (var key in this._dcs) {
|
|
|
if (this._dcs.hasOwnProperty(key)) {
|
|
|
- this._dcs[key].send(blob);
|
|
|
+ this._dcs[key].send(ab);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// Handles a DataChannel message.
|
|
|
SourcePeer.prototype.handleDataMessage = function(e) {
|
|
|
var data = MsgPack.decode(e.data);
|
|
@@ -126,10 +156,22 @@ SourcePeer.prototype.handleDataMessage = function(e) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
SourcePeer.prototype.on = function(code, cb) {
|
|
|
// For enduser.
|
|
|
if (code === 'data') {
|
|
|
this._dataHandler = cb;
|
|
|
+ } else if (code === 'sink') {
|
|
|
+ // SUCCESSFUL sink connection.
|
|
|
+ this._sinkHandler = cb;
|
|
|
+ } else if (code === 'stream') {
|
|
|
+ this._streamHandler = cb;
|
|
|
+ } else if (code === 'ready') {
|
|
|
+ // Source has set up socket.
|
|
|
+ this._readyHandler = cb;
|
|
|
+ } else if (code == 'disconnect') {
|
|
|
+ // A sink has disconnected.
|
|
|
+ this._disconnectHandler = cb;
|
|
|
}
|
|
|
};
|
|
|
|