WIP from Aug 27 2014
This commit is contained in:
parent
d8c9209017
commit
a7e832b59f
|
@ -4,8 +4,8 @@ function chatEvent(nym, status, utterance, stamp) {
|
||||||
return ["chatEvent", nym, status, utterance, stamp || +(new Date())];
|
return ["chatEvent", nym, status, utterance, stamp || +(new Date())];
|
||||||
}
|
}
|
||||||
|
|
||||||
function halfConnection(fromNym, toNym, sdp) {
|
function halfConnection(fromNym, toNym, sdp, ice) {
|
||||||
return ["route", fromNym, toNym, sdp];
|
return ["route", fromNym, toNym, sdp, ice];
|
||||||
}
|
}
|
||||||
|
|
||||||
function remoteValue(x, metaLevel) {
|
function remoteValue(x, metaLevel) {
|
||||||
|
@ -46,7 +46,7 @@ $(document).ready(function () {
|
||||||
//
|
//
|
||||||
|
|
||||||
var dm = new Minimart.DemandMatcher(remoteValue(chatEvent(_$, __, __, __)), 0, {
|
var dm = new Minimart.DemandMatcher(remoteValue(chatEvent(_$, __, __, __)), 0, {
|
||||||
supplyProjection: remoteValue(halfConnection(__, _$, __))
|
supplyProjection: remoteValue(halfConnection(__, _$, __, __))
|
||||||
});
|
});
|
||||||
dm.onDemandIncrease = function (captures) {
|
dm.onDemandIncrease = function (captures) {
|
||||||
spawnRoute(captures[0]);
|
spawnRoute(captures[0]);
|
||||||
|
@ -59,9 +59,11 @@ $(document).ready(function () {
|
||||||
//
|
//
|
||||||
|
|
||||||
function spawnRoute(remoteNym) {
|
function spawnRoute(remoteNym) {
|
||||||
console.log("spawnRoute", remoteNym);
|
|
||||||
var RTCPeerConnection = (window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
|
var RTCPeerConnection = (window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
|
||||||
var RTCSessionDescription = (window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription);
|
var RTCSessionDescription = (window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription);
|
||||||
|
var RTCIceCandidate = (window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate);
|
||||||
|
|
||||||
|
console.log("spawnRoute", remoteNym);
|
||||||
|
|
||||||
World.spawn(new Actor(function () {
|
World.spawn(new Actor(function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -70,41 +72,81 @@ $(document).ready(function () {
|
||||||
|
|
||||||
if (localNym !== remoteNym) {
|
if (localNym !== remoteNym) {
|
||||||
self.pc = new RTCPeerConnection(rtcConfig);
|
self.pc = new RTCPeerConnection(rtcConfig);
|
||||||
console.log(self.pc);
|
|
||||||
self.ch = self.pc.createDataChannel('x'); // TODO -- label
|
self.ch = self.pc.createDataChannel('x'); // TODO -- label
|
||||||
|
self.localSdp = null;
|
||||||
|
self.localIce = [];
|
||||||
|
self.remoteSdp = null;
|
||||||
|
self.remoteIce = [];
|
||||||
|
|
||||||
|
// WebRTC likes to pretend UDP is like a phone call.
|
||||||
|
self.isOriginator = localNym < remoteNym;
|
||||||
|
|
||||||
var errback = World.wrap(function (err) { // TODO - make a standard World utility?
|
var errback = World.wrap(function (err) { // TODO - make a standard World utility?
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(World.wrap(function () {
|
function flushLocalIce() {
|
||||||
self.pc.onicecandidate = World.wrap(function (e) {
|
if (!self.remoteSdp) { return; }
|
||||||
self.localSdp = self.pc.localDescription.sdp;
|
while (self.localIce.length) {
|
||||||
self.updateRoutes();
|
World.send(remoteValue(halfConnection(localNym,
|
||||||
});
|
remoteNym,
|
||||||
|
self.localSdp,
|
||||||
|
self.localIce.shift())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.pc.onicecandidate = World.wrap(function (e) {
|
||||||
|
if (e && e.candidate) {
|
||||||
|
var ice = [e.candidate.candidate, e.candidate.sdpMLineIndex, e.candidate.sdpMid];
|
||||||
|
console.log("LOCAL CANDIDATE", JSON.stringify(ice));
|
||||||
|
self.localIce.push(ice);
|
||||||
|
flushLocalIce();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (self.isOriginator) {
|
||||||
self.pc.createOffer(function (offer) {
|
self.pc.createOffer(function (offer) {
|
||||||
console.log("HERE", self.pc.setLocalDescription);
|
self.pc.setLocalDescription(new RTCSessionDescription(offer), function () {
|
||||||
console.log("BLAH", new RTCSessionDescription(offer));
|
self.localSdp = self.pc.localDescription.sdp;
|
||||||
self.pc.setLocalDescription(new RTCSessionDescription(offer), function () {}, errback);
|
self.updateRoutes();
|
||||||
|
}, errback);
|
||||||
}, errback);
|
}, errback);
|
||||||
}), 0);
|
}
|
||||||
|
|
||||||
Actor.advertise(
|
Actor.advertise(
|
||||||
function () { return remoteValue(halfConnection(localNym, remoteNym, self.localSdp)) },
|
function () { return remoteValue(halfConnection(localNym,
|
||||||
{ when: function () { return self.localSdp } });
|
remoteNym,
|
||||||
|
self.localSdp,
|
||||||
|
__)) },
|
||||||
|
{ when: function () { return self.local.sdp } });
|
||||||
|
|
||||||
|
Actor.subscribe(
|
||||||
|
function () { return remoteValue(halfConnection(remoteNym,
|
||||||
|
localNym,
|
||||||
|
__,
|
||||||
|
_$("ice"))) },
|
||||||
|
function (ice) {
|
||||||
|
console.log("REMOTE CANDIDATE", JSON.stringify(ice));
|
||||||
|
var candidate = new RTCIceCandidate({ candidate: ice[0],
|
||||||
|
sdpMLineIndex: ice[1],
|
||||||
|
sdpMid: ice[2] });
|
||||||
|
self.pc.addIceCandidate(candidate);
|
||||||
|
});
|
||||||
|
|
||||||
Actor.observeAdvertisers(
|
Actor.observeAdvertisers(
|
||||||
function () { return remoteValue(halfConnection(remoteNym, localNym, _$("sdp"))) },
|
function () { return remoteValue(halfConnection(remoteNym,
|
||||||
{ singleton: "remoteSdp" },
|
localNym,
|
||||||
|
_$("sdp"),
|
||||||
|
__)) },
|
||||||
|
{ singleton: "remote" },
|
||||||
function () {
|
function () {
|
||||||
if (self.localSdp && self.remoteSdp) {
|
if (self.local.sdp && self.remote) {
|
||||||
var params = {
|
var remoteSessionDescription = new RTCSessionDescription({
|
||||||
type: "offer",
|
type: "offer",
|
||||||
sdp: self.remoteSdp.sdp
|
sdp: self.remote.sdp
|
||||||
};
|
});
|
||||||
console.log("Local SDP is", self.localSdp);
|
// console.log("Local SDP is", self.localSdp);
|
||||||
console.log("Remote SDP is", params);
|
// console.log("Remote SDP is", params);
|
||||||
self.pc.setRemoteDescription(new RTCSessionDescription(params), function () {}, errback);
|
self.pc.setRemoteDescription(new RTCSessionDescription(params), function () {}, errback);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue