AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

This commit is contained in:
2026-04-01 14:32:23 -04:00
parent bb34b7b0fa
commit 6287f4d19b
2 changed files with 138 additions and 31 deletions

View File

@@ -36,20 +36,10 @@ async function ensurePeerConnection() {
iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
});
peerConnection.onicecandidate = async (event) => {
if (!event.candidate || !currentChannelId || !currentUsername) return;
const payload = {
type: "rtc_ice_candidate",
from: currentUsername,
channelId: currentChannelId,
candidate: event.candidate.candidate,
sdpMid: event.candidate.sdpMid,
sdpMLineIndex: event.candidate.sdpMLineIndex
};
LogMessage("Sending ICE candidate");
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
peerConnection.onicecandidate = (event) => {
if (event.candidate) {
LogMessage("ICE candidate gathered");
}
};
peerConnection.ontrack = (event) => {
@@ -180,17 +170,13 @@ async function joinChannelCall() {
LogMessage(`Joining call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
const payload = {
type: "rtc_offer",
type: "rtc_join",
from: currentUsername,
channelId: currentChannelId,
sdp: offer.sdp
channelId: currentChannelId
};
LogMessage("Sending offer to channel " + currentChannelId);
LogMessage("Requesting join for channel " + currentChannelId);
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
} catch (err) {
LogMessage("joinChannelCall failed: " + err);
@@ -254,6 +240,30 @@ async function handleRtcSignal(rawJson) {
await ensurePeerConnection();
if (msg.type === "rtc_join_state") {
if (msg.isInitiator) {
LogMessage("No active call found. Becoming initiator.");
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
await waitForIceGatheringComplete(peerConnection);
const payload = {
type: "rtc_offer",
from: currentUsername,
channelId: currentChannelId,
sdp: peerConnection.localDescription.sdp
};
LogMessage("Sending offer to channel " + currentChannelId);
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
} else {
LogMessage("Active call exists. Waiting for stored offer.");
}
return;
}
if (msg.type === "rtc_offer") {
LogMessage("Incoming channel call offer from " + msg.from);
await ensureLocalMedia();
@@ -268,12 +278,13 @@ async function handleRtcSignal(rawJson) {
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
await waitForIceGatheringComplete(peerConnection);
const payload = {
type: "rtc_answer",
from: currentUsername,
channelId: msg.channelId,
sdp: answer.sdp
sdp: peerConnection.localDescription.sdp
};
LogMessage("Sending answer to channel " + msg.channelId);
@@ -360,6 +371,21 @@ async function loadDevices() {
}
}
async function waitForIceGatheringComplete(pc) {
if (pc.iceGatheringState === "complete") return;
await new Promise(resolve => {
function checkState() {
if (pc.iceGatheringState === "complete") {
pc.removeEventListener("icegatheringstatechange", checkState);
resolve();
}
}
pc.addEventListener("icegatheringstatechange", checkState);
});
}
window.handleRtcSignal = handleRtcSignal;
window.addEventListener("HybridWebViewMessageReceived", function (e) {