bac744d506
Signed-off-by: Nickiel12 <nickiel@nickiel.net>
101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
|
|
|
|
import { event } from "./node_modules/@tauri-apps/api/index.js";
|
|
|
|
async function rtc_init() {
|
|
const videoview = document.getElementById("remoteview");
|
|
|
|
const config = {
|
|
iceServers: [{ urls: "stun:localhost" }]
|
|
};
|
|
const polite = true;
|
|
|
|
const pc = new RTCPeerConnection(config);
|
|
window.pc = pc;
|
|
|
|
pc.ontrack = (e) => {
|
|
console.log(e);
|
|
e.track.onunmute = () => {
|
|
console.log("Unmuted?");
|
|
if (remoteview.srcObject) {
|
|
console.log("Skipping srcobject");
|
|
return;
|
|
}
|
|
videoview.srcObject = e.streams[0];
|
|
Object.assign(videoview.style, { "background-color": "black"});
|
|
};
|
|
};
|
|
|
|
let makingOffer = false;
|
|
|
|
pc.onnegotionationneeded = async () => {
|
|
try {
|
|
makingOffer = true;
|
|
await pc.setLocalDescription();
|
|
console.log("emitting response webrtc packet");
|
|
event.emit("webrtc-message", pc.localDescription );
|
|
} catch (err) {
|
|
console.error(err);
|
|
} finally {
|
|
makingOffer = false;
|
|
}
|
|
};
|
|
|
|
pc.onicecandidate = ({ candidate }) => {
|
|
console.log("emitting response webrtc packet");
|
|
event.emit("webrtc-message", candidate);
|
|
};
|
|
pc.oniceconnectionstatechange = () => {
|
|
console.log('ICE state: ',pc.iceConnectionState);
|
|
if (pc.iceConnectionState === "failed") {
|
|
pc.restartIce();
|
|
}
|
|
};
|
|
|
|
let ignoreOffer = false;
|
|
|
|
console.log("registering listener");
|
|
const application_message = await event.listen('frontend_message', async (msg) => {
|
|
|
|
const payload = JSON.parse(msg.payload);
|
|
|
|
console.log(payload);
|
|
|
|
try {
|
|
if (payload.type) {
|
|
const offerCollision =
|
|
payload.type === "offer" &&
|
|
(makingOffer || pc.signalingState !== "stable");
|
|
|
|
ignoreOffer = !polite && offerCollision;
|
|
|
|
if (ignoreOffer) {
|
|
return;
|
|
}
|
|
|
|
await pc.setRemoteDescription(payload);
|
|
if (payload.type === "offer") {
|
|
console.log("Settings local description");
|
|
await pc.setLocalDescription();
|
|
console.log("emitting response webrtc packet");
|
|
event.emit( "webrtc-message", pc.localDescription);
|
|
}
|
|
} else if (payload.candidate) {
|
|
try {
|
|
console.log("Adding trickle ICE candidate");
|
|
await pc.addIceCandidate(payload.candidate);
|
|
} catch (err) {
|
|
if (!ignoreOffer) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
|
|
export { rtc_init };
|