vcs-controller/ui/static/rtc.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-07-31 19:42:30 -07:00
2024-08-17 13:11:59 -07:00
import { emit, listen } from '@tauri-apps/api/event';
2024-07-31 19:42:30 -07:00
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 });
2024-07-31 19:42:30 -07:00
} catch (err) {
console.error(err);
} finally {
makingOffer = false;
}
};
pc.onicecandidate = ({ candidate }) => emit("webrtc-message", { candidate });
2024-07-31 19:42:30 -07:00
pc.oniceconnectionstatechange = () => {
if (pc.iceConnectionState === "failed") {
pc.restartIce();
}
};
let ignoreOffer = false;
2024-08-17 13:11:59 -07:00
const application_message = await listen('frontend_message', async (event) => {
console.log("Event: ");
console.log(event);
const { description, candidate } = event.payload.data;
2024-07-31 19:42:30 -07:00
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 });
2024-07-31 19:42:30 -07:00
}
} else if (candidate) {
try {
await pc.addIceCandidate(candidate);
} catch (err) {
if (!ignoreOffer) {
throw err;
}
}
}
} catch (err) {
console.error(err);
}
2024-08-17 13:11:59 -07:00
});
2024-07-31 19:42:30 -07:00
async function start_rtc_connection() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: false });
}
2024-07-31 19:42:30 -07:00
}