X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/e2da0c51692b64318a0f1b8d888536ee632c53d6..3f2e36eabf1494ac60a3cf533d3db86d25a6cc39:/extension/peers.js diff --git a/extension/peers.js b/extension/peers.js index 41155d4..be494b0 100644 --- a/extension/peers.js +++ b/extension/peers.js @@ -1,41 +1,99 @@ -'use strict'; - const internals = { peers: {}, createAudioElement(source) { + const audioElement = document.createElement("audio"); + audioElement.setAttribute("class", "junction-call-audio"); + audioElement.autoplay = "autoplay"; + audioElement.srcObject = source; - const audioElement = document.createElement('audio'); - audioElement.setAttribute('class', 'junction-call-audio'); - audioElement.src = source; - audioElement.autoplay = 'autoplay'; - document.querySelector('body').appendChild(audioElement); + document.querySelector("body").appendChild(audioElement); return audioElement; - } + }, }; -module.exports = { - add(id, url) { +export function addPeer({ peerId, shouldCreateOffer, mediaStream, onOffer, socket }) { + const peerConnection = new RTCPeerConnection( + { iceServers: internals.kIceServers }, + { optional: [{ DtlsSrtpKeyAgreement: true }] }, + ); - internals.peers[id] && this.remove(id); - internals.peers[id] = internals.createAudioElement(url) - }, + internals.peers[peerId] = peerConnection; + mediaStream.getTracks().forEach((track) => { + peerConnection.addTrack(track, mediaStream); + }); - remove(id) { + peerConnection.onicecandidate = (event) => { + if (event.candidate) { + socket.emit("relayICECandidate", { + peerId: peerId, + candidate: event.candidate, + }); + } + }; - internals.peers[id] && internals.peers[id].remove(); - delete internals.peers[id]; - }, + const remoteStream = new MediaStream(); + peerConnection.ontrack = (event) => { + remoteStream.addTrack(event.track); + const remoteAudioElement = new Audio(); + remoteAudioElement.srcObject = remoteStream; + remoteAudioElement.play(); + }; - count() { + peerConnection.onnegotiationneeded = async () => { + if (shouldCreateOffer) { + console.debug("Creating RTC offer to ", peerId); + const offer = await peerConnection.createOffer(); + await peerConnection.setLocalDescription(offer); - return Object.keys(internals.peers).length; - }, + onOffer({ peerId, offer }); + } + }; - reset() { + console.info(`There are now ${countPeers()} participants`); +} - internals.peers = {}; - document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove()); - } -}; +export function removePeer({ peerId }) { + delete internals.peers[peerId]; + console.info(`There are now ${countPeers()} participants`); +} + +export async function answerPeerOffer({ peerId, offer }) { + console.info(`Answering peer ${peerId}`); + const peerConnection = internals.peers[peerId]; + + const remoteDescription = new RTCSessionDescription(offer); + await peerConnection.setRemoteDescription(remoteDescription); + + const answer = await peerConnection.createAnswer(); + await peerConnection.setLocalDescription(answer); + + return { peerId, answer }; +} + +export async function processPeerAnswer({ peerId, answer }) { + console.info(`Processing answer for peer ${peerId}`); + const peerConnection = internals.peers[peerId]; + const remoteDescription = new RTCSessionDescription(answer); + await peerConnection.setRemoteDescription(remoteDescription); +} + +export async function addIceCandidate({ peerId, candidate }) { + console.info(`Adding ICE candidate for peer ${peerId}`); + const peerConnection = internals.peers[peerId]; + console.info(peerConnection.signalingState); + const iceCandidate = new RTCIceCandidate(candidate); + await peerConnection.addIceCandidate(iceCandidate); +} + +export function countPeers() { + return Object.keys(internals.peers).length; +} + +export function resetPeers() { + internals.peers = {}; + document + .querySelectorAll(".junction-call-audio") + .forEach((audioElement) => audioElement.remove()); +}