]> git.r.bdr.sh - rbdr/junction/blame - extension/peers.js
Add safari code
[rbdr/junction] / extension / peers.js
CommitLineData
e2da0c51
RBR
1const internals = {
2 peers: {},
3
4 createAudioElement(source) {
b9a2baf2
RBR
5 const audioElement = document.createElement("audio");
6 audioElement.setAttribute("class", "junction-call-audio");
7 audioElement.autoplay = "autoplay";
06b7617a 8 audioElement.srcObject = source;
d38261f1 9
b9a2baf2 10 document.querySelector("body").appendChild(audioElement);
e2da0c51
RBR
11
12 return audioElement;
b9a2baf2 13 },
e2da0c51
RBR
14};
15
06b7617a
RBR
16export function addPeer({ peerId, mediaStream, onOffer, socket }) {
17 const peerConnection = new RTCPeerConnection(
18 { iceServers: internals.kIceServers },
19 { optional: [{ DtlsSrtpKeyAgreement: true }] },
20 );
e2da0c51 21
06b7617a
RBR
22 internals.peers[peerId] = peerConnection;
23 mediaStream.getTracks().forEach((track) => {
24 peerConnection.addTrack(track, mediaStream);
25 });
e2da0c51 26
06b7617a
RBR
27 peerConnection.onicecandidate = (event) => {
28 if (event.candidate) {
29 socket.emit("relayICECandidate", {
30 peerId: peerId,
31 candidate: event.candidate,
32 });
33 }
34 };
e2da0c51 35
06b7617a
RBR
36 const remoteStream = new MediaStream();
37 peerConnection.ontrack = (event) => {
38 remoteStream.addTrack(event.track);
39 const remoteAudioElement = new Audio();
40 remoteAudioElement.srcObject = remoteStream;
41 remoteAudioElement.play();
42 };
43
44 peerConnection.onnegotiationneeded = async () => {
45 console.debug("Creating RTC offer to ", peerId);
46 const offer = await peerConnection.createOffer();
47 await peerConnection.setLocalDescription(offer);
48
49 onOffer({ peerId, offer });
50 };
51
52 console.info(`There are now ${countPeers()} participants`);
53}
54
55export function removePeer({ peerId }) {
56 delete internals.peers[peerId];
57 console.info(`There are now ${countPeers()} participants`);
58}
59
60export async function answerPeerOffer({ peerId, offer }) {
61 const peerConnection = internals.peers[peerId];
62
63 const remoteDescription = new RTCSessionDescription(offer);
64 await peerConnection.setRemoteDescription(remoteDescription);
65
66 const answer = await peerConnection.createAnswer();
67 await peerConnection.setLocalDescription(answer);
68
69 return { peerId, answer };
70}
71
72export async function processPeerAnswer({ peerId, answer }) {
73 const peerConnection = internals.peers[peerId];
74 const remoteDescription = new RTCSessionDescription(answer);
75 await peerConnection.setRemoteDescription(remoteDescription);
76}
77
78export async function addIceCandidate({ peerId, candidate }) {
79 const peerConnection = internals.peers[peerId];
80 const iceCandidate = new RTCIceCandidate(candidate);
81 await peerConnection.addIceCandidate(iceCandidate);
82}
83
84export function countPeers() {
85 return Object.keys(internals.peers).length;
86}
87
88export function resetPeers() {
89 internals.peers = {};
90 document
91 .querySelectorAll(".junction-call-audio")
92 .forEach((audioElement) => audioElement.remove());
93}