|
|
|
|
|
|
|
|
const allRPCs = {} |
|
|
const allRPCs = {} |
|
|
|
|
|
|
|
|
|
|
|
// ensure no data gets lost when sent through the wire |
|
|
|
|
|
function K(obj) { |
|
|
|
|
|
return JSON.parse(JSON.stringify(obj)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/2117523 |
|
|
// https://stackoverflow.com/a/2117523 |
|
|
function uuidv4() { |
|
|
function uuidv4() { |
|
|
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => |
|
|
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => |
|
|
|
|
|
|
|
|
const rpc = new RTCPeerConnection(rpcConfig) |
|
|
const rpc = new RTCPeerConnection(rpcConfig) |
|
|
rpc.onicecandidate = ({candidate}) => { |
|
|
rpc.onicecandidate = ({candidate}) => { |
|
|
if(candidate && candidate.candidate) { |
|
|
if(candidate && candidate.candidate) { |
|
|
const cand = JSON.parse(JSON.stringify(candidate)) |
|
|
|
|
|
wire({kind: 'ice-candidate', value: {...cand, uid}, target}) |
|
|
|
|
|
|
|
|
wire({kind: 'ice-candidate', value: {...K(candidate), uid}, target}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
rpc.ontrack = ({streams: [stream]}) => { |
|
|
rpc.ontrack = ({streams: [stream]}) => { |
|
|
|
|
|
|
|
|
const localOffer = await rpc.createOffer() |
|
|
const localOffer = await rpc.createOffer() |
|
|
await rpc.setLocalDescription(localOffer) |
|
|
await rpc.setLocalDescription(localOffer) |
|
|
|
|
|
|
|
|
wire({kind: 'rpc-offer', value: {...localOffer, uid}, target}) |
|
|
|
|
|
|
|
|
wire({kind: 'rpc-offer', value: {...K(localOffer), uid}, target}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
addEventListener('rpc-offer', async ({detail}) => { |
|
|
addEventListener('rpc-offer', async ({detail}) => { |
|
|
|
|
|
|
|
|
const localAnswer = await rpc.createAnswer() |
|
|
const localAnswer = await rpc.createAnswer() |
|
|
await rpc.setLocalDescription(localAnswer) |
|
|
await rpc.setLocalDescription(localAnswer) |
|
|
|
|
|
|
|
|
wire({kind: 'rpc-answer', value: {...localAnswer, uid}, target}) |
|
|
|
|
|
|
|
|
wire({kind: 'rpc-answer', value: {...K(localAnswer), uid}, target}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
addEventListener('rpc-answer', async ({detail}) => { |
|
|
addEventListener('rpc-answer', async ({detail}) => { |