setup client to make calls to servers api for webrtc data management

This commit is contained in:
2026-04-03 19:45:06 -04:00
parent 63e427a4a1
commit c89a0cf88b
3 changed files with 217 additions and 66 deletions

View File

@@ -166,33 +166,35 @@ async function ensureLocalMedia() {
async function joinChannelCall() {
LogMessage("Current username: " + currentUsername);
LogMessage("Current channel: " + currentChannelId);
//TODO: Update Server DB to hold bool if channel has an active call
//TODO: First check if channel already has an active offer, if it does join with an answer, otherwise make a new offer
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);
}
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);
// }
}
async function ensurePeerConnection2()
{
if (peerConnection) return;
@@ -215,60 +217,36 @@ async function ensurePeerConnection2()
console.log(
`ICE connection state change: ${peerConnection.iceConnectionState}`);
});
}
async function channelCallJoin(activeCall)
{
LogMessage("Active call: " + activeCall);
await ensurePeerConnection2();
if (activeCall)
{
const offer = roomSnapshot.data().offer; //TODO: Replace with active call offer from DB using the active ID for current channel
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);
const roomAnswer = {
answer: {
type: answer.type,
sdp: answer.sdp
}
}
await roomRef.update(roomAnswer); //TODO: Update offer in SurrealDB to include answer
LogMessage(`Joining call with media answer: ${JSON.stringify(answer)}`);
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(roomAnswer)]);
//TODO: Update offer in SurrealDB to include answer
}
else
{
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
const roomOffer = {
offer: {
type: offer.type,
sdp: offer.sdp
}
}
await window.HybridWebView.InvokeDotNet("WriteRtcOffer", [JSON.stringify(offer)]);
//TODO: Write roomId to surreal DB with channel id as active call
//TODO: Add callback function for when call is answered to replace following code block
roomRef.onSnapshot(async snapshot => {
console.log('Got updated room:', snapshot.data());
const data = snapshot.data();
if (!peerConnection.currentRemoteDescription && data.answer) {
console.log('Set remote description: ', data.answer);
const answer = new RTCSessionDescription(data.answer)
await peerConnection.setRemoteDescription(answer);
}
});
LogMessage(`Joining call with media offer: ${JSON.stringify(offer)}`);
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
});
//TODO: collect ICE candidates
peerConnection.addEventListener('track', event => {
LogMessage("Received track: " + event.streams[0]);
event.streams[0].getTracks().forEach(track => {
@@ -277,9 +255,23 @@ async function channelCallJoin(activeCall)
});
});
}
}
async function AnswerCallback(answer)
{
LogMessage("Answer: " + JSON.stringify(answer));
if (!peerConnection.currentRemoteDescription && answer.answer)
{
LogMessage("Current answer: " + answer);
const desc = new RTCSessionDescription(answer);
await peerConnection.setRemoteDescription(desc);
}
}
async function CollectIceCandidates()
{
//TODO: collect ICE candidates
}
async function handleRtcSignal(rawJson) {
try {
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;