import { render, html } from '/uhtml.js';
import loadStreams from '/stream.js';
let instance;
let thread;
let connection;
let mediaStream;
function loadCall() {
let div = document.getElementById('call');
div.innerHTML = '';
render(div, html.node`
no one is in call
`);
if (Object.keys(currentThread.call).length)
div.children['noone'].classList.add('hidden');
for (let id in currentThread.call) {
let p = document.createElement('p');
p.append(window.makeUser(currentThread.call[id], id.split?.('@')[1] || currentInstance.url));
div.children['users'].append(p);
}
if (!currentInstance.called) {
currentInstance.socket.on('call', msg => {
if (msg.thread !== currentThread.id)
return;
if (Object.keys(msg.call).length)
div.children['noone'].classList.add('hidden');
else
div.children['noone'].classList.remove('hidden');
currentThread.call = msg.call;
div.children['users'].replaceChildren(...Object.values(msg.call).map(user => {
let p = document.createElement('p');
p.append(window.makeUser(user, user.id.split?.('@')[1] || currentInstance.url));
return p;
}));
});
currentInstance.socket.on('offer', async (msg, respond) => {
if (!connection)
return;
await connection.setRemoteDescription(msg.sdp);
await connection.setLocalDescription(await connection.createAnswer());
respond({
success: true,
sdp: connection.localDescription
});
});
currentInstance.socket.on('ice', msg => {
if (!connection)
return;
connection.addIceCandidate(new RTCIceCandidate(msg.candidate));
});
currentInstance.called = true;
}
loadStreams();
}
export default loadCall;