This isn't FULLY functional, but it's what I've made thus far... I'm still working on a Ice Disconnect somewhere at least for me - welcome to test.
This commit is contained in:
@@ -610,11 +610,11 @@ public partial class MainPage : ContentPage
|
|||||||
Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.ChannelId}");
|
Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.ChannelId}");
|
||||||
} //Remove?
|
} //Remove?
|
||||||
|
|
||||||
//public async Task<string> GetRtcParticipants() // TODO: UNCOMMENT AND ADD
|
public async Task<string> GetRtcParticipants()
|
||||||
//{
|
{
|
||||||
// var participants = await ServerAPI.GetRtcParticipantsAsync(_currentChannelId);
|
var participants = await ServerAPI.GetRtcParticipantsAsync(_currentChannelId);
|
||||||
// return JsonSerializer.Serialize(participants);
|
return JsonSerializer.Serialize(participants);
|
||||||
//}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
private void OnSendMessageButtonClicked(object sender, EventArgs e)
|
private void OnSendMessageButtonClicked(object sender, EventArgs e)
|
||||||
|
|||||||
@@ -35,11 +35,7 @@
|
|||||||
<div id="localMediaStatus">Waiting for local media...</div>
|
<div id="localMediaStatus">Waiting for local media...</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="display: inline-block; vertical-align: top;">
|
<div id="remoteMediaContainer" class="remote-media-container"></div>
|
||||||
<video id="remoteVideo" autoplay playsinline style="width: 320px; height: 240px; background: #111;"></video>
|
|
||||||
<div id="remoteVideoStatus">Remote video: waiting...</div>
|
|
||||||
<div id="remoteMediaStatus">Remote media: waiting...</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="margin-top: 10px;">
|
<div style="margin-top: 10px;">
|
||||||
|
|||||||
@@ -42,63 +42,6 @@ function hasAudioTrack() {
|
|||||||
return !!localStream && localStream.getAudioTracks().length > 0;
|
return !!localStream && localStream.getAudioTracks().length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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}`;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
peerConnection.onconnectionstatechange = () => {
|
|
||||||
LogMessage("Connection state: " + peerConnection.connectionState);
|
|
||||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
|
||||||
if (remoteMediaStatus && peerConnection.connectionState === "connected") {
|
|
||||||
remoteMediaStatus.textContent += " | connected";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
peerConnection.oniceconnectionstatechange = () => {
|
|
||||||
LogMessage("ICE connection state: " + peerConnection.iceConnectionState);
|
|
||||||
};
|
|
||||||
|
|
||||||
peerConnection.onicegatheringstatechange = () => {
|
|
||||||
LogMessage("ICE gathering state: " + peerConnection.iceGatheringState);
|
|
||||||
};
|
|
||||||
} //Remove?
|
|
||||||
async function ensureLocalMedia(forceReload = false) {
|
async function ensureLocalMedia(forceReload = false) {
|
||||||
const localMediaStatus = document.getElementById("localMediaStatus");
|
const localMediaStatus = document.getElementById("localMediaStatus");
|
||||||
const localVideoStatus = document.getElementById("localVideoStatus");
|
const localVideoStatus = document.getElementById("localVideoStatus");
|
||||||
@@ -174,31 +117,34 @@ async function ensureLocalMedia(forceReload = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function applyLocalStreamToPeerConnection() {
|
async function applyLocalStreamToPeerConnections() {
|
||||||
if (!peerConnection || !localStream) return;
|
if (!localStream) return;
|
||||||
|
|
||||||
const senders = peerConnection.getSenders();
|
|
||||||
|
|
||||||
const audioTrack = localStream.getAudioTracks()[0] || null;
|
const audioTrack = localStream.getAudioTracks()[0] || null;
|
||||||
const videoTrack = localStream.getVideoTracks()[0] || null;
|
const videoTrack = localStream.getVideoTracks()[0] || null;
|
||||||
|
|
||||||
|
for (const username of Object.keys(peerConnections)) {
|
||||||
|
const pc = peerConnections[username];
|
||||||
|
const senders = pc.getSenders();
|
||||||
|
|
||||||
const audioSender = senders.find(s => s.track && s.track.kind === "audio");
|
const audioSender = senders.find(s => s.track && s.track.kind === "audio");
|
||||||
const videoSender = senders.find(s => s.track && s.track.kind === "video");
|
const videoSender = senders.find(s => s.track && s.track.kind === "video");
|
||||||
|
|
||||||
if (audioSender) {
|
if (audioSender) {
|
||||||
await audioSender.replaceTrack(audioTrack);
|
await audioSender.replaceTrack(audioTrack);
|
||||||
LogMessage("Replaced audio track on peer connection");
|
LogMessage(`Replaced audio track for ${username}`);
|
||||||
} else if (audioTrack) {
|
} else if (audioTrack) {
|
||||||
peerConnection.addTrack(audioTrack, localStream);
|
pc.addTrack(audioTrack, localStream);
|
||||||
LogMessage("Added audio track to peer connection");
|
LogMessage(`Added audio track for ${username}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (videoSender) {
|
if (videoSender) {
|
||||||
await videoSender.replaceTrack(videoTrack);
|
await videoSender.replaceTrack(videoTrack);
|
||||||
LogMessage("Replaced video track on peer connection");
|
LogMessage(`Replaced video track for ${username}`);
|
||||||
} else if (videoTrack) {
|
} else if (videoTrack) {
|
||||||
peerConnection.addTrack(videoTrack, localStream);
|
pc.addTrack(videoTrack, localStream);
|
||||||
LogMessage("Added video track to peer connection");
|
LogMessage(`Added video track for ${username}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,238 +152,188 @@ async function refreshDevicesAndPreview() {
|
|||||||
await loadDevices();
|
await loadDevices();
|
||||||
await ensureLocalMedia(true);
|
await ensureLocalMedia(true);
|
||||||
|
|
||||||
if (peerConnection) {
|
if (Object.keys(peerConnections).length > 0) {
|
||||||
await applyLocalStreamToPeerConnection();
|
await applyLocalStreamToPeerConnections();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function joinChannelCall() {
|
async function joinChannelCall() {
|
||||||
LogMessage("Current username: " + currentUsername);
|
LogMessage("Current username: " + currentUsername);
|
||||||
LogMessage("Current channel: " + currentChannelId);
|
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
|
|
||||||
|
|
||||||
async function ensurePeerConnectionForUser(username)
|
await window.HybridWebView.InvokeDotNet("JoinRtcChannel");
|
||||||
{
|
|
||||||
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 ensurePeerConnectionForUser();
|
|
||||||
await ensureLocalMedia();
|
await ensureLocalMedia();
|
||||||
await applyLocalStreamToPeerConnection();
|
|
||||||
|
|
||||||
if (activeCall)
|
const rawParticipants = await window.HybridWebView.InvokeDotNet("GetRtcParticipants");
|
||||||
{
|
const participants = typeof rawParticipants === "string"
|
||||||
const rawJson = await window.HybridWebView.InvokeDotNet("GetRtcOffer");
|
? JSON.parse(rawParticipants)
|
||||||
const offer = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
: rawParticipants;
|
||||||
await peerConnection.setRemoteDescription(offer);
|
|
||||||
const answer = await peerConnection.createAnswer();
|
|
||||||
await peerConnection.setLocalDescription(answer);
|
|
||||||
|
|
||||||
// LogMessage("Joining call with media answer: " + JSON.stringify(answer));
|
LogMessage("Participants: " + JSON.stringify(participants)); // TODO: Remove
|
||||||
// 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)]);
|
for (const username of participants) {
|
||||||
LogMessage(`Joining call with media offer: ${JSON.stringify(offer)}`);
|
if (username === currentUsername) continue;
|
||||||
}
|
|
||||||
}
|
|
||||||
async function AnswerCallbackJS(answer)
|
|
||||||
{
|
|
||||||
answer.sdp = answer.sdp.replaceAll("(rn)", "\r\n");
|
|
||||||
// LogMessage("Answer: " + JSON.stringify(answer));
|
|
||||||
|
|
||||||
// LogMessage("RemoteDescription: " + peerConnection.currentRemoteDescription);
|
const pc = await ensurePeerConnectionForUser(username);
|
||||||
|
|
||||||
if (!peerConnection.currentRemoteDescription && answer)
|
const offer = await pc.createOffer();
|
||||||
{
|
await pc.setLocalDescription(offer);
|
||||||
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);
|
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
type: "rtc_offer",
|
||||||
|
from: currentUsername,
|
||||||
|
to: username,
|
||||||
|
channelId: currentChannelId,
|
||||||
|
sdp: offer.sdp
|
||||||
|
};
|
||||||
|
|
||||||
|
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||||
|
LogMessage(`Sent offer to ${username}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function RtcLeaveCall()
|
async function ensurePeerConnectionForUser(username) {
|
||||||
{}
|
if (peerConnections[username]) return peerConnections[username];
|
||||||
|
|
||||||
|
const pc = new RTCPeerConnection(configuration);
|
||||||
|
peerConnections[username] = pc;
|
||||||
|
|
||||||
|
pc.onicegatheringstatechange = () => {
|
||||||
|
console.log(`ICE gathering state changed for ${username}: ${pc.iceGatheringState}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
pc.onconnectionstatechange = () => {
|
||||||
|
console.log(`Connection state change for ${username}: ${pc.connectionState}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
pc.onsignalingstatechange = () => {
|
||||||
|
console.log(`Signaling state change for ${username}: ${pc.signalingState}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
pc.oniceconnectionstatechange = () => {
|
||||||
|
console.log(`ICE connection state change for ${username}: ${pc.iceConnectionState}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
pc.onicecandidate = async (event) => {
|
||||||
|
if (!event.candidate) return;
|
||||||
|
|
||||||
|
await window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(event.candidate)]);
|
||||||
|
};
|
||||||
|
|
||||||
|
pc.ontrack = (event) => {
|
||||||
|
LogMessage(`Remote track received from ${username}`);
|
||||||
|
|
||||||
|
if (!remoteStreams[username]) {
|
||||||
|
remoteStreams[username] = new MediaStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = remoteStreams[username];
|
||||||
|
|
||||||
|
event.streams[0].getTracks().forEach(track => {
|
||||||
|
if (!stream.getTracks().some(t => t.id === track.id)) {
|
||||||
|
stream.addTrack(track);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const remoteVideo = ensureRemoteTile(username);
|
||||||
|
if (remoteVideo) {
|
||||||
|
remoteVideo.srcObject = stream;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (localStream) {
|
||||||
|
const existingKinds = pc.getSenders()
|
||||||
|
.map(sender => sender.track?.kind)
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
for (const track of localStream.getTracks()) {
|
||||||
|
if (!existingKinds.includes(track.kind)) {
|
||||||
|
pc.addTrack(track, localStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pc;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function RtcLeaveCall() { // TODO: Just a minimal function so it's not empty.
|
||||||
|
for (const username of Object.keys(peerConnections)) {
|
||||||
|
peerConnections[username].close();
|
||||||
|
removeRemoteTile(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
peerConnections = {};
|
||||||
|
remoteStreams = {};
|
||||||
|
candidateQueue = [];
|
||||||
|
|
||||||
|
LogMessage("RTC call cleaned up");
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeParticipant(username) {
|
||||||
|
const pc = peerConnections[username];
|
||||||
|
if (pc) {
|
||||||
|
pc.close();
|
||||||
|
delete peerConnections[username];
|
||||||
|
}
|
||||||
|
|
||||||
|
delete remoteStreams[username];
|
||||||
|
removeRemoteTile(username);
|
||||||
|
|
||||||
|
LogMessage(`Removed participant ${username}`);
|
||||||
|
}
|
||||||
|
|
||||||
async function handleRtcSignal(rawJson) {
|
async function handleRtcSignal(rawJson) {
|
||||||
try {
|
try {
|
||||||
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||||
|
|
||||||
LogMessage("Received signal: " + msg.type + " from " + msg.from + " in " + msg.channelId);
|
LogMessage("Received signal: " + msg.type + " from " + msg.from + " in " + msg.channelId);
|
||||||
|
|
||||||
await ensurePeerConnection();
|
if (!msg.from || msg.from === currentUsername) return;
|
||||||
|
|
||||||
if (msg.type === "rtc_join_state") {
|
const pc = await ensurePeerConnectionForUser(msg.from);
|
||||||
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") {
|
if (msg.type === "rtc_offer") {
|
||||||
LogMessage("Incoming channel call offer from " + msg.from);
|
|
||||||
await ensureLocalMedia();
|
await ensureLocalMedia();
|
||||||
|
|
||||||
LogMessage(`Answering call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
|
await pc.setRemoteDescription({
|
||||||
LogMessage("Applying remote offer");
|
|
||||||
|
|
||||||
await peerConnection.setRemoteDescription({
|
|
||||||
type: "offer",
|
type: "offer",
|
||||||
sdp: msg.sdp
|
sdp: msg.sdp
|
||||||
});
|
});
|
||||||
|
|
||||||
const answer = await peerConnection.createAnswer();
|
const answer = await pc.createAnswer();
|
||||||
await peerConnection.setLocalDescription(answer);
|
await pc.setLocalDescription(answer);
|
||||||
// await waitForIceGatheringComplete(peerConnection);
|
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
type: "rtc_answer",
|
type: "rtc_answer",
|
||||||
from: currentUsername,
|
from: currentUsername,
|
||||||
|
to: msg.from,
|
||||||
channelId: msg.channelId,
|
channelId: msg.channelId,
|
||||||
sdp: peerConnection.localDescription.sdp
|
sdp: answer.sdp
|
||||||
};
|
};
|
||||||
|
|
||||||
LogMessage("Sending answer to channel " + msg.channelId);
|
|
||||||
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||||
|
LogMessage(`Sent answer to ${msg.from}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.type === "rtc_answer") {
|
if (msg.type === "rtc_answer") {
|
||||||
LogMessage("Applying remote answer");
|
await pc.setRemoteDescription({
|
||||||
|
|
||||||
await peerConnection.setRemoteDescription({
|
|
||||||
type: "answer",
|
type: "answer",
|
||||||
sdp: msg.sdp
|
sdp: msg.sdp
|
||||||
});
|
});
|
||||||
|
|
||||||
LogMessage("Remote answer applied");
|
LogMessage(`Remote answer applied for ${msg.from}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.type === "rtc_ice_candidate") {
|
if (msg.type === "rtc_ice_candidate") {
|
||||||
LogMessage("Applying remote ICE candidate");
|
await pc.addIceCandidate({
|
||||||
|
|
||||||
await peerConnection.addIceCandidate({
|
|
||||||
candidate: msg.candidate,
|
candidate: msg.candidate,
|
||||||
sdpMid: msg.sdpMid,
|
sdpMid: msg.sdpMid,
|
||||||
sdpMLineIndex: msg.sdpMLineIndex
|
sdpMLineIndex: msg.sdpMLineIndex
|
||||||
});
|
});
|
||||||
|
|
||||||
LogMessage("Remote ICE candidate applied");
|
LogMessage(`Remote ICE candidate applied for ${msg.from}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,7 +341,7 @@ async function handleRtcSignal(rawJson) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
LogMessage("handleRtcSignal failed: " + err);
|
LogMessage("handleRtcSignal failed: " + err);
|
||||||
}
|
}
|
||||||
} //Remove?
|
}
|
||||||
|
|
||||||
async function loadDevices() {
|
async function loadDevices() {
|
||||||
try {
|
try {
|
||||||
@@ -503,7 +399,7 @@ function wireDeviceSelectors() {
|
|||||||
cameraSelect.onchange = async () => {
|
cameraSelect.onchange = async () => {
|
||||||
LogMessage("Camera changed");
|
LogMessage("Camera changed");
|
||||||
await ensureLocalMedia(true);
|
await ensureLocalMedia(true);
|
||||||
await applyLocalStreamToPeerConnection();
|
await applyLocalStreamToPeerConnections();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -511,7 +407,7 @@ function wireDeviceSelectors() {
|
|||||||
micSelect.onchange = async () => {
|
micSelect.onchange = async () => {
|
||||||
LogMessage("Microphone changed");
|
LogMessage("Microphone changed");
|
||||||
await ensureLocalMedia(true);
|
await ensureLocalMedia(true);
|
||||||
await applyLocalStreamToPeerConnection();
|
await applyLocalStreamToPeerConnections();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -531,6 +427,42 @@ async function waitForIceGatheringComplete(pc) {
|
|||||||
});
|
});
|
||||||
} //Remove?
|
} //Remove?
|
||||||
|
|
||||||
|
function ensureRemoteTile(username) {
|
||||||
|
let tile = document.getElementById(`remote-tile-${username}`);
|
||||||
|
if (tile) {
|
||||||
|
return tile.querySelector("video");
|
||||||
|
}
|
||||||
|
|
||||||
|
const container = document.getElementById("remoteMediaContainer");
|
||||||
|
if (!container) return null;
|
||||||
|
|
||||||
|
tile = document.createElement("div");
|
||||||
|
tile.id = `remote-tile-${username}`;
|
||||||
|
tile.className = "remote-tile";
|
||||||
|
|
||||||
|
const video = document.createElement("video");
|
||||||
|
video.id = `remote-video-${username}`;
|
||||||
|
video.autoplay = true;
|
||||||
|
video.playsInline = true;
|
||||||
|
|
||||||
|
const label = document.createElement("div");
|
||||||
|
label.className = "remote-label";
|
||||||
|
label.textContent = username;
|
||||||
|
|
||||||
|
tile.appendChild(video);
|
||||||
|
tile.appendChild(label);
|
||||||
|
container.appendChild(tile);
|
||||||
|
|
||||||
|
return video;
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeRemoteTile(username) {
|
||||||
|
const tile = document.getElementById(`remote-tile-${username}`);
|
||||||
|
if (tile) {
|
||||||
|
tile.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.handleRtcSignal = handleRtcSignal;
|
window.handleRtcSignal = handleRtcSignal;
|
||||||
|
|
||||||
window.addEventListener("HybridWebViewMessageReceived", function (e) {
|
window.addEventListener("HybridWebViewMessageReceived", function (e) {
|
||||||
|
|||||||
@@ -111,6 +111,19 @@ public class ServerAPI
|
|||||||
var json = await response.Content.ReadAsStringAsync();
|
var json = await response.Content.ReadAsStringAsync();
|
||||||
return JsonSerializer.Deserialize<RtcDescription>(json);
|
return JsonSerializer.Deserialize<RtcDescription>(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task<List<string>> GetRtcParticipantsAsync(string? channelId)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(channelId))
|
||||||
|
return new List<string>();
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.GetAsync($"api/rtc/participants/{channelId}");
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<List<string>>(json) ?? new List<string>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RtcDescription
|
public class RtcDescription
|
||||||
|
|||||||
@@ -71,11 +71,10 @@ public static class RtcEndpoints
|
|||||||
return Results.Ok(await rtcCallService.GetAnswersAsync(channelId));
|
return Results.Ok(await rtcCallService.GetAnswersAsync(channelId));
|
||||||
});
|
});
|
||||||
|
|
||||||
//app.MapGet("/api/rtc/participants/{channelId}", (string channelId) => // TODO: UNCOMMENT AND ADD
|
app.MapGet("/api/rtc/participants/{channelId}", (string channelId) =>
|
||||||
//{
|
{
|
||||||
// var participants = RtcChannelPresenceService.GetUsernamesInChannel(channelId);
|
return Results.Ok(RtcChannelPresenceService.GetUsersInChannel(channelId));
|
||||||
// return Results.Ok(participants);
|
});
|
||||||
//});
|
|
||||||
|
|
||||||
// Return the latest answer stored for the specified channel.
|
// Return the latest answer stored for the specified channel.
|
||||||
app.MapGet("/api/rtc/answer/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
app.MapGet("/api/rtc/answer/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||||
|
|||||||
@@ -69,9 +69,72 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IsEncryptedRtcSignal(msg))
|
||||||
|
{
|
||||||
|
HandleEncryptedRtcSignal(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
HandleEncryptedChatMessage(msg);
|
HandleEncryptedChatMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsEncryptedRtcSignal(string msg)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var doc = JsonDocument.Parse(msg);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
|
||||||
|
if (!root.TryGetProperty("Type", out var typeProp))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var type = (SignalType)typeProp.GetInt32();
|
||||||
|
|
||||||
|
return type == SignalType.EncryptedSignal;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleEncryptedRtcSignal(string msg)
|
||||||
|
{
|
||||||
|
Console.WriteLine("RTC SIGNAL HIT");
|
||||||
|
SocketRtcSignalMessage? clientPayload;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
clientPayload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(msg);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("Failed to parse encrypted RTC signal payload.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clientPayload is null || clientPayload.Type != SignalType.EncryptedSignal)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(clientPayload.ChannelId))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Encrypted RTC signal missing channel id.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sessionIds = RtcChannelPresenceService.GetSessionsInChannel(clientPayload.ChannelId);
|
||||||
|
|
||||||
|
foreach (var sessionId in sessionIds)
|
||||||
|
{
|
||||||
|
if (sessionId == ID)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Sessions.SendTo(JsonSerializer.Serialize(clientPayload), sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Forwarded encrypted RTC signal from {clientPayload.SenderUsername} to channel {clientPayload.ChannelId}");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ public static class RtcChannelPresenceService
|
|||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<string> GetUsernamesInChannel(string channelId)
|
||||||
|
{
|
||||||
|
return GetUsersInChannel(channelId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public static IReadOnlyList<string> GetUsersInChannel(string channelId)
|
public static IReadOnlyList<string> GetUsersInChannel(string channelId)
|
||||||
{
|
{
|
||||||
var sessionIds = GetSessionsInChannel(channelId);
|
var sessionIds = GetSessionsInChannel(channelId);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using SurrealDb.Net.Models;
|
using System.Text.Json.Serialization;
|
||||||
|
using RelayShared.Rtc;
|
||||||
|
using SurrealDb.Net.Models;
|
||||||
|
|
||||||
#region Resharper Stuff
|
#region Resharper Stuff
|
||||||
// ReSharper disable ClassNeverInstantiated.Global
|
// ReSharper disable ClassNeverInstantiated.Global
|
||||||
@@ -26,13 +28,31 @@ public enum SignalType
|
|||||||
|
|
||||||
public sealed class RtcSignalMessage
|
public sealed class RtcSignalMessage
|
||||||
{
|
{
|
||||||
|
[JsonPropertyName("type")]
|
||||||
public SignalType Type { get; set; }
|
public SignalType Type { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("from")]
|
||||||
public string From { get; set; } = string.Empty;
|
public string From { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("to")]
|
||||||
|
public string To { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("channelId")]
|
||||||
public string ChannelId { get; set; } = string.Empty;
|
public string ChannelId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("sdp")]
|
||||||
public string? Sdp { get; set; }
|
public string? Sdp { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("candidate")]
|
||||||
public string? Candidate { get; set; }
|
public string? Candidate { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("sdpMid")]
|
||||||
public string? SdpMid { get; set; }
|
public string? SdpMid { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("sdpMLineIndex")]
|
||||||
public int? SdpMLineIndex { get; set; }
|
public int? SdpMLineIndex { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("isInitiator")]
|
||||||
public bool IsInitiator { get; set; }
|
public bool IsInitiator { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user