Messaging works, let's start there.
This commit is contained in:
@@ -1,102 +1,137 @@
|
||||
let peerConnection = null;
|
||||
let localStream = null;
|
||||
let currentUsername = null;
|
||||
let currentChannelId = null;
|
||||
let localStream = null;
|
||||
let availableCameras = [];
|
||||
let availableMics = [];
|
||||
let candidateQueue = [];
|
||||
const configuration = {
|
||||
iceServers:[
|
||||
{
|
||||
urls:[
|
||||
'stun:stun1.l.google.com:19302',
|
||||
'stun:stun2.l.google.com:19302',
|
||||
],
|
||||
},
|
||||
],
|
||||
iceCandidatePoolSize: 10,
|
||||
}
|
||||
|
||||
window.setUsername = function(name) {
|
||||
const peerConnections = new Map();
|
||||
const candidateQueues = new Map();
|
||||
|
||||
const configuration = {
|
||||
iceServers: [
|
||||
{ urls: ["stun:stun1.l.google.com:19302", "stun:stun2.l.google.com:19302"] }
|
||||
],
|
||||
iceCandidatePoolSize: 10
|
||||
};
|
||||
|
||||
window.setUsername = function (name) {
|
||||
currentUsername = name;
|
||||
LogMessage("Username set to: " + currentUsername);
|
||||
};
|
||||
window.setChannelId = function(channelId) {
|
||||
|
||||
window.setChannelId = function (channelId) {
|
||||
currentChannelId = channelId;
|
||||
LogMessage("Channel set to: " + currentChannelId);
|
||||
};
|
||||
|
||||
window.handleRtcOffer = async function (remoteUsername, offer) {
|
||||
await ensureLocalMedia();
|
||||
const peer = await ensurePeerConnection(remoteUsername);
|
||||
|
||||
LogMessage("Incoming offer from " + remoteUsername);
|
||||
await peer.setRemoteDescription(new RTCSessionDescription(offer));
|
||||
|
||||
const answer = await peer.createAnswer();
|
||||
await peer.setLocalDescription(answer);
|
||||
|
||||
const payload = {
|
||||
channelId: currentChannelId,
|
||||
username: currentUsername,
|
||||
targetUsername: remoteUsername,
|
||||
sessionDescription: answer
|
||||
};
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(payload)]);
|
||||
};
|
||||
|
||||
window.handleRtcAnswer = async function (remoteUsername, answer) {
|
||||
const peer = await ensurePeerConnection(remoteUsername);
|
||||
LogMessage("Incoming answer from " + remoteUsername);
|
||||
|
||||
await peer.setRemoteDescription(new RTCSessionDescription(answer));
|
||||
await flushCandidateQueue(remoteUsername);
|
||||
};
|
||||
|
||||
window.handleRtcCandidate = async function (remoteUsername, candidate) {
|
||||
const peer = await ensurePeerConnection(remoteUsername);
|
||||
|
||||
if (peer.remoteDescription) {
|
||||
await peer.addIceCandidate(new RTCIceCandidate(candidate));
|
||||
} else {
|
||||
const queue = candidateQueues.get(remoteUsername) || [];
|
||||
queue.push(candidate);
|
||||
candidateQueues.set(remoteUsername, queue);
|
||||
}
|
||||
};
|
||||
|
||||
window.handleRtcParticipantLeft = function (remoteUsername) {
|
||||
LogMessage(remoteUsername + " left the call");
|
||||
closePeerConnection(remoteUsername);
|
||||
removeRemoteTile(remoteUsername);
|
||||
};
|
||||
|
||||
function LogMessage(msg) {
|
||||
const messageLog = document.getElementById("messageLog");
|
||||
messageLog.value += '\r\n' + msg;
|
||||
messageLog.value += "\r\n" + msg;
|
||||
messageLog.scrollTop = messageLog.scrollHeight;
|
||||
}
|
||||
|
||||
function hasVideoTrack() {
|
||||
return !!localStream && localStream.getVideoTracks().length > 0;
|
||||
}
|
||||
async function ensurePeerConnection(remoteUsername) {
|
||||
if (peerConnections.has(remoteUsername)) {
|
||||
return peerConnections.get(remoteUsername);
|
||||
}
|
||||
|
||||
function hasAudioTrack() {
|
||||
return !!localStream && localStream.getAudioTracks().length > 0;
|
||||
}
|
||||
const peer = new RTCPeerConnection(configuration);
|
||||
peerConnections.set(remoteUsername, peer);
|
||||
candidateQueues.set(remoteUsername, []);
|
||||
|
||||
async function ensurePeerConnection() {
|
||||
if (peerConnection) return;
|
||||
|
||||
peerConnection = new RTCPeerConnection({
|
||||
iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
|
||||
});
|
||||
|
||||
peerConnection.onicecandidate = (event) => {
|
||||
if (event.candidate) {
|
||||
LogMessage("ICE candidate gathered");
|
||||
peer.onicecandidate = async (event) => {
|
||||
if (!event.candidate) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
channelId: currentChannelId,
|
||||
username: currentUsername,
|
||||
targetUsername: remoteUsername,
|
||||
candidate: event.candidate
|
||||
};
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(payload)]);
|
||||
};
|
||||
|
||||
peerConnection.ontrack = (event) => {
|
||||
LogMessage("Remote track received");
|
||||
|
||||
const remoteVideo = document.getElementById("remoteVideo");
|
||||
const remoteVideoStatus = document.getElementById("remoteVideoStatus");
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
|
||||
peer.ontrack = (event) => {
|
||||
const stream = event.streams[0];
|
||||
const hasVideo = stream.getVideoTracks().length > 0;
|
||||
const hasAudio = stream.getAudioTracks().length > 0;
|
||||
attachRemoteStream(remoteUsername, stream);
|
||||
};
|
||||
|
||||
if (hasVideo) {
|
||||
remoteVideo.srcObject = stream;
|
||||
} else {
|
||||
remoteVideo.srcObject = null;
|
||||
}
|
||||
|
||||
if (remoteVideoStatus) {
|
||||
remoteVideoStatus.textContent = hasVideo
|
||||
? "Remote video: active"
|
||||
: "Remote video: unavailable";
|
||||
}
|
||||
|
||||
if (remoteMediaStatus) {
|
||||
remoteMediaStatus.textContent = `Remote media: audio=${hasAudio} video=${hasVideo}`;
|
||||
peer.onconnectionstatechange = () => {
|
||||
LogMessage(remoteUsername + " connection state: " + peer.connectionState);
|
||||
if (peer.connectionState === "failed" || peer.connectionState === "closed" || peer.connectionState === "disconnected") {
|
||||
closePeerConnection(remoteUsername);
|
||||
removeRemoteTile(remoteUsername);
|
||||
}
|
||||
};
|
||||
|
||||
peerConnection.onconnectionstatechange = () => {
|
||||
LogMessage("Connection state: " + peerConnection.connectionState);
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
if (remoteMediaStatus && peerConnection.connectionState === "connected") {
|
||||
remoteMediaStatus.textContent += " | connected";
|
||||
if (localStream) {
|
||||
for (const track of localStream.getTracks()) {
|
||||
peer.addTrack(track, localStream);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
peerConnection.oniceconnectionstatechange = () => {
|
||||
LogMessage("ICE connection state: " + peerConnection.iceConnectionState);
|
||||
};
|
||||
return peer;
|
||||
}
|
||||
|
||||
async function flushCandidateQueue(remoteUsername) {
|
||||
const peer = peerConnections.get(remoteUsername);
|
||||
const queue = candidateQueues.get(remoteUsername) || [];
|
||||
|
||||
while (peer && queue.length > 0) {
|
||||
const candidate = queue.shift();
|
||||
await peer.addIceCandidate(new RTCIceCandidate(candidate));
|
||||
}
|
||||
}
|
||||
|
||||
peerConnection.onicegatheringstatechange = () => {
|
||||
LogMessage("ICE gathering state: " + peerConnection.iceGatheringState);
|
||||
};
|
||||
} //Remove?
|
||||
async function ensureLocalMedia(forceReload = false) {
|
||||
const localMediaStatus = document.getElementById("localMediaStatus");
|
||||
const localVideoStatus = document.getElementById("localVideoStatus");
|
||||
@@ -113,353 +148,141 @@ async function ensureLocalMedia(forceReload = false) {
|
||||
localStream = null;
|
||||
}
|
||||
|
||||
let selectedCameraId = cameraSelect ? cameraSelect.value : "";
|
||||
let selectedMicId = micSelect ? micSelect.value : "";
|
||||
const selectedCameraId = cameraSelect ? cameraSelect.value : "";
|
||||
const selectedMicId = micSelect ? micSelect.value : "";
|
||||
|
||||
const videoConstraint = selectedCameraId
|
||||
? { deviceId: { exact: selectedCameraId } }
|
||||
: false;
|
||||
|
||||
const audioConstraint = selectedMicId
|
||||
? { deviceId: { exact: selectedMicId } }
|
||||
: true;
|
||||
const videoConstraint = selectedCameraId ? { deviceId: { exact: selectedCameraId } } : false;
|
||||
const audioConstraint = selectedMicId ? { deviceId: { exact: selectedMicId } } : true;
|
||||
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: videoConstraint,
|
||||
audio: audioConstraint
|
||||
});
|
||||
|
||||
LogMessage("Local media initialized");
|
||||
} catch (err) {
|
||||
LogMessage("selected media failed: " + err);
|
||||
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: audioConstraint
|
||||
});
|
||||
|
||||
LogMessage("Local media initialized with audio only fallback");
|
||||
} catch (audioErr) {
|
||||
LogMessage("audio-only failed: " + audioErr);
|
||||
|
||||
if (localMediaStatus) localMediaStatus.textContent = "Local media failed";
|
||||
if (localVideoStatus) localVideoStatus.textContent = "Local video: unavailable";
|
||||
if (localVideo) localVideo.srcObject = null;
|
||||
|
||||
throw audioErr;
|
||||
}
|
||||
LogMessage("Selected media failed: " + err);
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: audioConstraint
|
||||
});
|
||||
}
|
||||
|
||||
const hasVideo = localStream.getVideoTracks().length > 0;
|
||||
const hasAudio = localStream.getAudioTracks().length > 0;
|
||||
|
||||
localVideo.srcObject = hasVideo ? localStream : null;
|
||||
localVideoStatus.textContent = hasVideo ? "Local video: active" : "Local video: unavailable";
|
||||
localMediaStatus.textContent = `Local media: audio=${hasAudio} video=${hasVideo}`;
|
||||
|
||||
if (localVideoStatus) {
|
||||
localVideoStatus.textContent = hasVideo
|
||||
? "Local video: active"
|
||||
: "Local video: unavailable";
|
||||
}
|
||||
|
||||
if (localMediaStatus) {
|
||||
localMediaStatus.textContent = `Local media: audio=${hasAudio} video=${hasVideo}`;
|
||||
}
|
||||
|
||||
if (!hasVideo) {
|
||||
LogMessage("No camera available, continuing without video");
|
||||
for (const [remoteUsername, peer] of peerConnections) {
|
||||
const senders = peer.getSenders();
|
||||
for (const track of localStream.getTracks()) {
|
||||
const existingSender = senders.find(sender => sender.track && sender.track.kind === track.kind);
|
||||
if (existingSender) {
|
||||
await existingSender.replaceTrack(track);
|
||||
} else {
|
||||
peer.addTrack(track, localStream);
|
||||
}
|
||||
}
|
||||
LogMessage("Updated local media for " + remoteUsername);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyLocalStreamToPeerConnection() {
|
||||
if (!peerConnection || !localStream) return;
|
||||
function attachRemoteStream(remoteUsername, stream) {
|
||||
const remoteVideos = document.getElementById("remoteVideos");
|
||||
let tile = document.getElementById(`remote-${remoteUsername}`);
|
||||
|
||||
const senders = peerConnection.getSenders();
|
||||
if (!tile) {
|
||||
tile = document.createElement("div");
|
||||
tile.id = `remote-${remoteUsername}`;
|
||||
tile.style.display = "inline-block";
|
||||
tile.style.marginRight = "20px";
|
||||
tile.style.verticalAlign = "top";
|
||||
|
||||
const audioTrack = localStream.getAudioTracks()[0] || null;
|
||||
const videoTrack = localStream.getVideoTracks()[0] || null;
|
||||
const title = document.createElement("div");
|
||||
title.textContent = remoteUsername;
|
||||
title.style.marginBottom = "6px";
|
||||
|
||||
const audioSender = senders.find(s => s.track && s.track.kind === "audio");
|
||||
const videoSender = senders.find(s => s.track && s.track.kind === "video");
|
||||
const video = document.createElement("video");
|
||||
video.autoplay = true;
|
||||
video.playsInline = true;
|
||||
video.style.width = "320px";
|
||||
video.style.height = "240px";
|
||||
video.style.background = "#111";
|
||||
video.id = `remote-video-${remoteUsername}`;
|
||||
|
||||
if (audioSender) {
|
||||
await audioSender.replaceTrack(audioTrack);
|
||||
LogMessage("Replaced audio track on peer connection");
|
||||
} else if (audioTrack) {
|
||||
peerConnection.addTrack(audioTrack, localStream);
|
||||
LogMessage("Added audio track to peer connection");
|
||||
const status = document.createElement("div");
|
||||
status.id = `remote-status-${remoteUsername}`;
|
||||
status.textContent = "Remote media: active";
|
||||
|
||||
tile.appendChild(title);
|
||||
tile.appendChild(video);
|
||||
tile.appendChild(status);
|
||||
remoteVideos.appendChild(tile);
|
||||
}
|
||||
|
||||
if (videoSender) {
|
||||
await videoSender.replaceTrack(videoTrack);
|
||||
LogMessage("Replaced video track on peer connection");
|
||||
} else if (videoTrack) {
|
||||
peerConnection.addTrack(videoTrack, localStream);
|
||||
LogMessage("Added video track to peer connection");
|
||||
const video = document.getElementById(`remote-video-${remoteUsername}`);
|
||||
const status = document.getElementById(`remote-status-${remoteUsername}`);
|
||||
video.srcObject = stream;
|
||||
status.textContent = `Remote media: audio=${stream.getAudioTracks().length > 0} video=${stream.getVideoTracks().length > 0}`;
|
||||
}
|
||||
|
||||
function removeRemoteTile(remoteUsername) {
|
||||
const tile = document.getElementById(`remote-${remoteUsername}`);
|
||||
if (tile) {
|
||||
tile.remove();
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshDevicesAndPreview() {
|
||||
await loadDevices();
|
||||
await ensureLocalMedia(true);
|
||||
|
||||
if (peerConnection) {
|
||||
await applyLocalStreamToPeerConnection();
|
||||
function closePeerConnection(remoteUsername) {
|
||||
const peer = peerConnections.get(remoteUsername);
|
||||
if (peer) {
|
||||
peer.close();
|
||||
}
|
||||
|
||||
peerConnections.delete(remoteUsername);
|
||||
candidateQueues.delete(remoteUsername);
|
||||
}
|
||||
|
||||
async function joinChannelCall() {
|
||||
LogMessage("Current username: " + currentUsername);
|
||||
LogMessage("Current channel: " + currentChannelId);
|
||||
// LogMessage("Joining RTCChannel");
|
||||
let active = await window.HybridWebView.InvokeDotNet("JoinRtcChannel");
|
||||
await channelCallJoin(active);
|
||||
// LogMessage("Joined RTCChannel");
|
||||
// return;
|
||||
// try {
|
||||
// if (!currentChannelId) {
|
||||
// LogMessage("No current channel set.");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// await ensurePeerConnection();
|
||||
// await ensureLocalMedia();
|
||||
//
|
||||
// LogMessage(`Joining call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
|
||||
//
|
||||
// const payload = {
|
||||
// type: "rtc_join",
|
||||
// from: currentUsername,
|
||||
// channelId: currentChannelId
|
||||
// };
|
||||
//
|
||||
// LogMessage("Requesting join for channel " + currentChannelId);
|
||||
// await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||
// } catch (err) {
|
||||
// LogMessage("joinChannelCall failed: " + err);
|
||||
// }
|
||||
} //Combine with channelCallJoin
|
||||
if (!currentUsername || !currentChannelId) {
|
||||
LogMessage("RTC context is not ready yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
async function ensurePeerConnection2()
|
||||
{
|
||||
if (peerConnection) return;
|
||||
peerConnection = new RTCPeerConnection(configuration);
|
||||
|
||||
peerConnection.onicegatheringstatechange = () => {
|
||||
console.log(`ICE gathering state changed: ${peerConnection.iceGatheringState}`);
|
||||
};
|
||||
peerConnection.onconnectionstatechange = () => {
|
||||
console.log(`Connection state change: ${peerConnection.connectionState}`);
|
||||
};
|
||||
peerConnection.onsignalingstatechange = () => {
|
||||
console.log(`Signaling state change: ${peerConnection.signalingState}`);
|
||||
};
|
||||
peerConnection.oniceconnectionstatechange = () => {
|
||||
console.log(`ICE connection state change: ${peerConnection.iceConnectionState}`);
|
||||
};
|
||||
|
||||
peerConnection.onicecandidate = async (event) => {
|
||||
console.log(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
|
||||
// LogMessage(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
|
||||
await window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(event.candidate)]);
|
||||
await IceCandidateAdded(event.candidate);
|
||||
};
|
||||
|
||||
peerConnection.ontrack = (event) => {
|
||||
LogMessage("Remote track received");
|
||||
|
||||
const remoteVideo = document.getElementById("remoteVideo");
|
||||
const remoteVideoStatus = document.getElementById("remoteVideoStatus");
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
|
||||
const stream = event.streams[0];
|
||||
const hasVideo = stream.getVideoTracks().length > 0;
|
||||
const hasAudio = stream.getAudioTracks().length > 0;
|
||||
|
||||
if (hasVideo) {
|
||||
remoteVideo.srcObject = stream;
|
||||
} else {
|
||||
remoteVideo.srcObject = null;
|
||||
}
|
||||
|
||||
if (remoteVideoStatus) {
|
||||
remoteVideoStatus.textContent = hasVideo
|
||||
? "Remote video: active"
|
||||
: "Remote video: unavailable";
|
||||
}
|
||||
|
||||
if (remoteMediaStatus) {
|
||||
remoteMediaStatus.textContent = `Remote media: audio=${hasAudio} video=${hasVideo}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
async function channelCallJoin(activeCall)
|
||||
{
|
||||
// LogMessage("Active call: " + activeCall);
|
||||
await ensurePeerConnection2();
|
||||
await ensureLocalMedia();
|
||||
await applyLocalStreamToPeerConnection();
|
||||
|
||||
if (activeCall)
|
||||
{
|
||||
const rawJson = await window.HybridWebView.InvokeDotNet("GetRtcOffer");
|
||||
const offer = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
await peerConnection.setRemoteDescription(offer);
|
||||
const answer = await peerConnection.createAnswer();
|
||||
await peerConnection.setLocalDescription(answer);
|
||||
|
||||
// LogMessage("Joining call with media answer: " + JSON.stringify(answer));
|
||||
// LogMessage("Calling C# WriteRtcAnswer with: " + JSON.stringify(answer));
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(answer)]);
|
||||
}
|
||||
else
|
||||
{
|
||||
const offer = await peerConnection.createOffer();
|
||||
await peerConnection.setLocalDescription(offer);
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcOffer", [JSON.stringify(offer)]);
|
||||
LogMessage(`Joining call with media offer: ${JSON.stringify(offer)}`);
|
||||
const rawParticipants = await window.HybridWebView.InvokeDotNet("JoinRtcChannel");
|
||||
const participants = typeof rawParticipants === "string" ? JSON.parse(rawParticipants) : rawParticipants;
|
||||
|
||||
LogMessage("Joining call with participants: " + (participants.length ? participants.join(", ") : "none"));
|
||||
|
||||
for (const remoteUsername of participants) {
|
||||
const peer = await ensurePeerConnection(remoteUsername);
|
||||
const offer = await peer.createOffer();
|
||||
await peer.setLocalDescription(offer);
|
||||
|
||||
const payload = {
|
||||
channelId: currentChannelId,
|
||||
username: currentUsername,
|
||||
targetUsername: remoteUsername,
|
||||
sessionDescription: offer
|
||||
};
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcOffer", [JSON.stringify(payload)]);
|
||||
LogMessage("Created offer for " + remoteUsername);
|
||||
}
|
||||
}
|
||||
async function AnswerCallbackJS(answer)
|
||||
{
|
||||
answer.sdp = answer.sdp.replaceAll("(rn)", "\r\n");
|
||||
// LogMessage("Answer: " + JSON.stringify(answer));
|
||||
|
||||
// LogMessage("RemoteDescription: " + peerConnection.currentRemoteDescription);
|
||||
|
||||
if (!peerConnection.currentRemoteDescription && answer)
|
||||
{
|
||||
LogMessage("Current answer: " + JSON.stringify(answer));
|
||||
const desc = new RTCSessionDescription(answer);
|
||||
await peerConnection.setRemoteDescription(desc);
|
||||
for (const candidate of candidateQueue) {
|
||||
await peerConnection.addIceCandidate(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
async function IceCandidateAdded(candidate)
|
||||
{
|
||||
if (peerConnection.currentRemoteDescription) {
|
||||
await peerConnection.addIceCandidate(candidate);
|
||||
// LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate));
|
||||
}
|
||||
else {
|
||||
// LogMessage("RemoteDescription Missing")
|
||||
candidateQueue.push(candidate);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async function RtcLeaveCall()
|
||||
{}
|
||||
async function handleRtcSignal(rawJson) {
|
||||
try {
|
||||
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
|
||||
LogMessage("Received signal: " + msg.type + " from " + msg.from + " in " + msg.channelId);
|
||||
|
||||
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();
|
||||
|
||||
LogMessage(`Answering call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
|
||||
LogMessage("Applying remote offer");
|
||||
|
||||
await peerConnection.setRemoteDescription({
|
||||
type: "offer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
const answer = await peerConnection.createAnswer();
|
||||
await peerConnection.setLocalDescription(answer);
|
||||
// await waitForIceGatheringComplete(peerConnection);
|
||||
|
||||
const payload = {
|
||||
type: "rtc_answer",
|
||||
from: currentUsername,
|
||||
channelId: msg.channelId,
|
||||
sdp: peerConnection.localDescription.sdp
|
||||
};
|
||||
|
||||
LogMessage("Sending answer to channel " + msg.channelId);
|
||||
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_answer") {
|
||||
LogMessage("Applying remote answer");
|
||||
|
||||
await peerConnection.setRemoteDescription({
|
||||
type: "answer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
LogMessage("Remote answer applied");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_ice_candidate") {
|
||||
LogMessage("Applying remote ICE candidate");
|
||||
|
||||
await peerConnection.addIceCandidate({
|
||||
candidate: msg.candidate,
|
||||
sdpMid: msg.sdpMid,
|
||||
sdpMLineIndex: msg.sdpMLineIndex
|
||||
});
|
||||
|
||||
LogMessage("Remote ICE candidate applied");
|
||||
return;
|
||||
}
|
||||
|
||||
LogMessage("Unhandled signal type: " + msg.type);
|
||||
} catch (err) {
|
||||
LogMessage("handleRtcSignal failed: " + err);
|
||||
}
|
||||
} //Remove?
|
||||
|
||||
async function loadDevices() {
|
||||
try {
|
||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||
|
||||
availableCameras = devices.filter(d => d.kind === "videoinput");
|
||||
availableMics = devices.filter(d => d.kind === "audioinput");
|
||||
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (!cameraSelect || !micSelect) {
|
||||
LogMessage("Device dropdowns not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
cameraSelect.innerHTML = "";
|
||||
micSelect.innerHTML = "";
|
||||
|
||||
@@ -483,7 +306,7 @@ async function loadDevices() {
|
||||
for (const mic of availableMics) {
|
||||
const option = document.createElement("option");
|
||||
option.value = mic.deviceId;
|
||||
option.text = mic.label || `Microphone ${micSelect.options.length + 1}`;
|
||||
option.text = mic.label || `Microphone ${micSelect.options.length}`;
|
||||
micSelect.appendChild(option);
|
||||
}
|
||||
|
||||
@@ -499,37 +322,21 @@ function wireDeviceSelectors() {
|
||||
|
||||
if (cameraSelect) {
|
||||
cameraSelect.onchange = async () => {
|
||||
LogMessage("Camera changed");
|
||||
await ensureLocalMedia(true);
|
||||
await applyLocalStreamToPeerConnection();
|
||||
};
|
||||
}
|
||||
|
||||
if (micSelect) {
|
||||
micSelect.onchange = async () => {
|
||||
LogMessage("Microphone changed");
|
||||
await ensureLocalMedia(true);
|
||||
await applyLocalStreamToPeerConnection();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
} //Remove?
|
||||
|
||||
// window.handleRtcSignal = handleRtcSignal;
|
||||
async function refreshDevicesAndPreview() {
|
||||
await loadDevices();
|
||||
await ensureLocalMedia(true);
|
||||
}
|
||||
|
||||
window.addEventListener("HybridWebViewMessageReceived", function (e) {
|
||||
LogMessage("Raw message: " + e.detail.message);
|
||||
@@ -541,4 +348,4 @@ window.addEventListener("load", async () => {
|
||||
await loadDevices();
|
||||
wireDeviceSelectors();
|
||||
await ensureLocalMedia(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user