Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfinished webrtc negotiation rework #1293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 71 additions & 44 deletions lib/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,34 @@
opt.rtc.sdp = opt.rtc.sdp || {mandatory: {OfferToReceiveAudio: false, OfferToReceiveVideo: false}};
opt.rtc.max = opt.rtc.max || 55; // is this a magic number? // For Future WebRTC notes: Chrome 500 max limit, however 256 likely - FF "none", webtorrent does 55 per torrent.
opt.rtc.room = opt.rtc.room || Gun.window && (location.hash.slice(1) || location.pathname.slice(1));
var room = () => root.$.get('/RTC/'+opt.rtc.room+'<?99');
opt.announce = function(to){
opt.rtc.start = +new Date; // handle room logic:
root.$.get('/RTC/'+opt.rtc.room+'<?99').get('+').put(opt.pid, function(ack){
room().get('+').put(opt.pid, function(ack){
if(!ack.ok || !ack.ok.rtc){ return }
open(ack);
}, {acks: opt.rtc.max}).on(function(last,key, msg){
room().get('+'+ack.ok.rtc.id).put(opt.pid, function(ack){
if(!ack.ok || !ack.ok.rtc){ return }
open(ack);
});
}, {acks: opt.rtc.max}).on(function (last,key, msg){
if(last === opt.pid || opt.rtc.start > msg.put['>']){ return }
room().get('+'+last).put(opt.pid, function(ack){
if(!ack.ok || !ack.ok.rtc){ return }
open({'#': ''+ack['#'], ok: {rtc: {id: last, respect: true}}});
});
root.on('out', {'@': msg['#'], ok: {rtc: {id: opt.pid}}});
});
// TODO: implement negotiation setup here
// TODO: order greetings in this function
room().get('+'+opt.pid).on(function(last, key, msg){
if(opt.rtc.start > msg.put['>']){ return }
open({'#': ''+msg['#'], ok: {rtc: {id: last}}});
});
};

var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
root.on('create', function(at){
console.log(opt.pid);
this.to.next(at);
setTimeout(opt.announce, 1);
});
Expand All @@ -56,57 +71,69 @@
if(!msg.ok){ return }
var rtc = msg.ok.rtc, peer, tmp;
if(!rtc || !rtc.id || rtc.id === opt.pid){ return }
//console.log("webrtc:", JSON.stringify(msg));
peer = opt.peers[rtc.id] || open[rtc.id];
if(!peer){
(peer = new opt.RTCPeerConnection(opt.rtc)).id = rtc.id;
var wire = peer.wire = peer.createDataChannel('dc', opt.rtc.dataChannel);
open[rtc.id] = peer;
wire.to = setTimeout(function(){delete open[rtc.id]},1000*60);
wire.onclose = function(){ mesh.bye(peer) };
wire.onerror = function(err){ };
wire.onopen = function(e){
delete open[rtc.id];
mesh.hi(peer);
}
wire.onmessage = function(msg){
if(!msg){ return }
mesh.hear(msg.data || msg, peer);
};
peer.onicecandidate = function(e){ // source: EasyRTC!
if(!e.candidate){ return }
// root.on('out', {'@': msg['#'], ok: {rtc: {candidate: e.candidate, id: opt.pid}}});
root.$.get('/RTC/'+opt.rtc.room+'<?99').get('='+rtc.id).put({candidate: e.candidate, id: opt.pid}); //previous data has to be erased here, or use pub-sub
}
peer.ondatachannel = function(e){
var rc = e.channel;
rc.onmessage = wire.onmessage;
rc.onopen = wire.onopen;
rc.onclose = wire.onclose;
}
}
if(tmp = rtc.answer){
if(!(peer = opt.peers[rtc.id] || open[rtc.id]) || peer.remoteSet){ return }
tmp.sdp = tmp.sdp.replace(/\\r\\n/g, '\r\n');
return peer.setRemoteDescription(peer.remoteSet = new opt.RTCSessionDescription(tmp));
}
if(tmp = rtc.candidate){
peer = opt.peers[rtc.id] || open[rtc.id] || open({ok: {rtc: {id: rtc.id}}});
return peer.addIceCandidate(new opt.RTCIceCandidate(tmp));
}
//if(opt.peers[rtc.id]){ return }
if(open[rtc.id]){ return }
(peer = new opt.RTCPeerConnection(opt.rtc)).id = rtc.id;
var wire = peer.wire = peer.createDataChannel('dc', opt.rtc.dataChannel);
open[rtc.id] = peer;
wire.to = setTimeout(function(){delete open[rtc.id]},1000*60);
wire.onclose = function(){ mesh.bye(peer) };
wire.onerror = function(err){ };
wire.onopen = function(e){
delete open[rtc.id];
mesh.hi(peer);
}
wire.onmessage = function(msg){
if(!msg){ return }
//console.log('via rtc');
mesh.hear(msg.data || msg, peer);
};
peer.onicecandidate = function(e){ // source: EasyRTC!
if(!e.candidate){ return }
root.on('out', {'@': msg['#'], ok: {rtc: {candidate: e.candidate, id: opt.pid}}});
}
peer.ondatachannel = function(e){
var rc = e.channel;
rc.onmessage = wire.onmessage;
rc.onopen = wire.onopen;
rc.onclose = wire.onclose;
return peer.setRemoteDescription(new opt.RTCSessionDescription(tmp));
}
if(tmp = rtc.candidate){ return peer.addIceCandidate(new opt.RTCIceCandidate(tmp)) }
if(tmp = rtc.offer){
rtc.offer.sdp = rtc.offer.sdp.replace(/\\r\\n/g, '\r\n')
peer.setRemoteDescription(new opt.RTCSessionDescription(tmp));
peer.createAnswer(function(answer){
peer.setLocalDescription(answer);
root.on('out', {'@': msg['#'], ok: {rtc: {answer: answer, id: opt.pid}}});
// root.on('out', {'@': msg['#'], ok: {rtc: {answer: answer, id: opt.pid}}});
root.$.get('/RTC/'+opt.rtc.room+'<?99').get('='+rtc.id).put({answer: answer, id: opt.pid});
}, function(){}, opt.rtc.sdp);
return;
}
if(tmp = rtc.negotiationneeded){
peer.createOffer(function(offer){
peer.setLocalDescription(offer);
// root.on('out', {'@': msg['#'], '#': root.ask(open), ok: {rtc: {offer: offer, id: opt.pid}}});
console.log('sending offer')
root.$.get('/RTC/'+opt.rtc.room+'<?99').get('='+rtc.id).put({offer: offer, id: opt.pid});
}, function(){}, opt.rtc.sdp);
return;
}
peer.createOffer(function(offer){
peer.setLocalDescription(offer);
root.on('out', {'@': msg['#'], '#': root.ask(open), ok: {rtc: {offer: offer, id: opt.pid}}});
}, function(){}, opt.rtc.sdp);
return peer;
// room join
peer.negotiate = msg['#'];
if(rtc.respect){ peer.respect = true }
console.log('sending to', msg['#']);
// setTimeout(function() {
// root.on('out', {'@': msg['#'], ok: {rtc: {id: opt.pid}}}); //FIXME: does not send
// }, 1);
setTimeout(function() {
root.on('out', {'@': msg['#'], ok: {rtc: {id: opt.pid}}}); //FIXME: does not send
root.on('out', {'@': msg['#'], ok: {rtc: {id: 'heh'}}}); //FIXME: does not send
}, 10);
}
});
}());
}());