vcs-controller/ui/static/rtc.js
2024-08-20 20:58:38 -07:00

89 lines
2.1 KiB
JavaScript

import { emit, listen } from '@tauri-apps/api/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;
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 });
}
}