const { emit, listen } = window.__TAURI.tauri.event; const videoview = document.getElementById("remoteview"); const config = { iceServers: [{ urls: "stun:localhost" }] }; const polite = true; const pc = new RTCPeerConnection(config); pc.ontrack = ({ track, streams }) => { track.onunmute = () => { if (remoteview.srcObject) { return; } remoteview.srcObject = streams[0]; }; }; let makingOffer = false; pc.onnegotionationneeded = async () => { try { makingOffer = true; await pc.setLocalDescription(); emit("webrtc-message", { description: pc.localDescription }); } catch (err) { console.error(err); } finally { makingOffer = false; } }; pc.onicecandidate = ({ candidate }) => emit("webrtc-message", { candidate }); pc.oniceconnectionstatechange = () => { if (pc.iceConnectionState === "failed") { pc.restartIce(); } }; let ignoreOffer = false; console.log("registering listner"); const application_message = await listen('frontend_message', async (event) => { console.log("Event: "); console.log(event); const { description, candidate } = event.payload.data; try { if (description) { const offerCollision = description.type === "offer" && (makingOffer || pc.signalingState !== "stable"); ignoreOffer = !polite && offerCollision; if (ignoreOffer) { return; } await pc.setRemoteDescription(description); if (description.type === "offer") { await pc.setLocalDescription(); emit( "webrtc-message", { description: pc.localDescription }); } } else if (candidate) { try { await pc.addIceCandidate(candidate); } catch (err) { if (!ignoreOffer) { throw err; } } } } catch (err) { console.error(err); } }); // async function start_rtc_connection() { //try { // const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: false }); //} //} export { videoview };