|
@@ -1,4 +1,5 @@
|
|
|
function SourcePeer(options) {
|
|
|
+ // TODO: Allow passing in own ID.
|
|
|
this._config = options.config || {};
|
|
|
this._streams = options.streamType || 'd';
|
|
|
this._name = options.name || 'StreamAPI';
|
|
@@ -17,37 +18,54 @@ SourcePeer.prototype.socketInit = function() {
|
|
|
self._id = data.id;
|
|
|
|
|
|
self._socket.on('sink-connected', function(data) {
|
|
|
+ console.log('SINK CONNECTED');
|
|
|
// TODO: not just moz.
|
|
|
target = data.sink;
|
|
|
- var pc = new mozRTCPeerConnection(self._config);
|
|
|
+ var pc = new window.mozRTCPeerConnection(self._config);
|
|
|
self._pcs[target] = pc;
|
|
|
-
|
|
|
// Setups will create streams--then the callback sets up the offers.
|
|
|
self.handleStream(pc, target, function() {
|
|
|
- pc.createOffer(function(offer) {
|
|
|
- pc.setLocalDescription(offer);
|
|
|
- self._socket.emit('offer',
|
|
|
- { 'sdp': offer, 'sink': target, 'source': this._id });
|
|
|
- });
|
|
|
+ // 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': JSON.stringify(offer),
|
|
|
+ 'sink': target,
|
|
|
+ 'source': self._id });
|
|
|
+ }, function(err) {
|
|
|
+ console.log('failed to setLocalDescription, ', err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }, function(err) { console.log('crap'); });
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- self._socket.on('answer', function(data, fn) {
|
|
|
- self._pcs[data.sink].setRemoteDescription(data.sdp);
|
|
|
- fn();
|
|
|
- // Firefoxism
|
|
|
- self._pcs[data.sink].connectDataConnection(5000,5001);
|
|
|
+ self._socket.on('answer', function(data) {
|
|
|
+ self._pcs[data.sink].setRemoteDescription(JSON.parse(data.sdp), function() {
|
|
|
+ // Firefoxism
|
|
|
+ console.log('FIREFOX', new Date());
|
|
|
+ self._pcs[data.sink].connectDataConnection(5000, 5001);
|
|
|
+ console.log('FIREFOX-2');
|
|
|
+ console.log('SOURCE: PeerConnection success');
|
|
|
+ }, function(err) {
|
|
|
+ console.log('failed to setRemoteDescription, ', err)
|
|
|
+ });
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
// Based on stream type requested, sets up the stream for PC.
|
|
|
-SourcePeer.prototype.handleStream(pc, target, cb) {
|
|
|
+SourcePeer.prototype.handleStream = function(pc, target, cb) {
|
|
|
+ pc.onaddstream = function(obj) {
|
|
|
+ console.log('SOURCE: data stream get');
|
|
|
+ };
|
|
|
/*if (this._streams === 'v') {
|
|
|
} else if (this._streams === 'a') {
|
|
|
} else if (this._streams === 'av') {
|
|
|
} else if (this._streams === 'd') {*/
|
|
|
- this.setupDataChannel(pc, target);
|
|
|
+ this.setupDataChannel(pc, target, cb);
|
|
|
/*} else if (this._streams === 'dav') {
|
|
|
this.setupDataChannel(pc, target);
|
|
|
} else if (this._streams === 'da') {
|
|
@@ -59,13 +77,15 @@ SourcePeer.prototype.handleStream(pc, target, cb) {
|
|
|
}*/
|
|
|
};
|
|
|
|
|
|
-SourcePeer.prototype.setupDataChannel = function(pc, target) {
|
|
|
+SourcePeer.prototype.setupDataChannel = function(pc, target, cb) {
|
|
|
+ self = this;
|
|
|
pc.onconnection = function() {
|
|
|
- var dc = pc.createDataChannel(this._name, {}, target);
|
|
|
- this._dc[target] = dc;
|
|
|
+ console.log('SOURCE: onconnection triggered.');
|
|
|
+ var dc = pc.createDataChannel(self._name, {}, target);
|
|
|
+ self._dc[target] = dc;
|
|
|
dc.binaryType = 'blob';
|
|
|
dc.onmessage = function(e) {
|
|
|
- this.handleDataMessage(pc, e);
|
|
|
+ self.handleDataMessage(pc, e);
|
|
|
// process e.data
|
|
|
};
|
|
|
};
|
|
@@ -73,12 +93,9 @@ SourcePeer.prototype.setupDataChannel = function(pc, target) {
|
|
|
pc.onclosedconnection = function() {
|
|
|
// ??
|
|
|
};
|
|
|
+ cb();
|
|
|
};
|
|
|
|
|
|
SourcePeer.prototype.on = function(code, cb) {
|
|
|
// For enduser.
|
|
|
};
|
|
|
-
|
|
|
-SourcePeer.prototype.gotDescription = function(desc) {
|
|
|
- this._pc
|
|
|
-};
|