diff --git a/Desktop.Core/Services/CasterSocket.cs b/Desktop.Core/Services/CasterSocket.cs index eb67c609..34917270 100644 --- a/Desktop.Core/Services/CasterSocket.cs +++ b/Desktop.Core/Services/CasterSocket.cs @@ -28,7 +28,7 @@ namespace Remotely.Desktop.Core.Services Task SendCtrlAltDelToAgent(); Task SendDeviceInfo(string serviceID, string machineName, string deviceID); Task SendDtoToViewer(T baseDto, string viewerId); - Task SendIceCandidateToBrowser(string candidateJson, string viewerConnectionID); + Task SendIceCandidateToBrowser(string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment, string viewerConnectionID); Task SendMessageToViewer(string viewerID, string message); Task SendRtcOfferToBrowser(string sdp, string viewerID, IceServerModel[] iceServers); Task SendViewerConnected(string viewerConnectionId); @@ -164,9 +164,9 @@ namespace Remotely.Desktop.Core.Services var serializedDto = MessagePack.MessagePackSerializer.Serialize(baseDto); return Connection.SendAsync("SendDtoToBrowser", serializedDto, viewerId); } - public async Task SendIceCandidateToBrowser(string candidateJson, string viewerConnectionID) + public async Task SendIceCandidateToBrowser(string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment, string viewerConnectionID) { - await Connection.SendAsync("SendIceCandidateToBrowser", candidateJson, viewerConnectionID); + await Connection.SendAsync("SendIceCandidateToBrowser", candidate, sdpMid, sdpMLineIndex, usernameFragment, viewerConnectionID); } public async Task SendRtcOfferToBrowser(string sdp, string viewerID, IceServerModel[] iceServers) @@ -237,13 +237,13 @@ namespace Remotely.Desktop.Core.Services }); - Connection.On("ReceiveIceCandidate", (string candidateJson, string viewerID) => + Connection.On("ReceiveIceCandidate", (string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment, string viewerID) => { try { if (conductor.Viewers.TryGetValue(viewerID, out var viewer)) { - viewer.RtcSession.AddIceCandidate(candidateJson); + viewer.RtcSession.AddIceCandidate(candidate, sdpMid, sdpMLineIndex, usernameFragment); } } catch (Exception ex) diff --git a/Desktop.Core/Services/Viewer.cs b/Desktop.Core/Services/Viewer.cs index 1e168631..ce61a759 100644 --- a/Desktop.Core/Services/Viewer.cs +++ b/Desktop.Core/Services/Viewer.cs @@ -104,8 +104,11 @@ namespace Remotely.Desktop.Core.Services }; RtcSession.IceCandidateReady += async (sender, candidate) => { - var candidateJson = JsonSerializer.Serialize(candidate); - await CasterSocket.SendIceCandidateToBrowser(candidateJson, ViewerConnectionID); + await CasterSocket.SendIceCandidateToBrowser(candidate.candidate, + candidate.sdpMid, + candidate.sdpMLineIndex, + candidate.usernameFragment, + ViewerConnectionID); }; await RtcSession.Init(iceServers); diff --git a/Desktop.Core/Services/WebRtcSession.cs b/Desktop.Core/Services/WebRtcSession.cs index 03f1c954..8c108603 100644 --- a/Desktop.Core/Services/WebRtcSession.cs +++ b/Desktop.Core/Services/WebRtcSession.cs @@ -40,10 +40,17 @@ namespace Remotely.Desktop.Core.Services private IDtoMessageHandler RtcMessageHandler { get; } private Timer DataChannelBufferMonitor { get; } private Viewer Viewer { get; } - public void AddIceCandidate(string candidateJson) + public void AddIceCandidate(string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment) { - var candidate = JsonSerializer.Deserialize(candidateJson); - PeerSession.addIceCandidate(candidate); + var rtcCandidate = new RTCIceCandidateInit() + { + candidate = candidate, + sdpMid = sdpMid, + sdpMLineIndex = sdpMLineIndex, + usernameFragment = usernameFragment + }; + + PeerSession.addIceCandidate(rtcCandidate); } public void Dispose() diff --git a/Server/Hubs/CasterHub.cs b/Server/Hubs/CasterHub.cs index 6c5c5fdf..6a02c857 100644 --- a/Server/Hubs/CasterHub.cs +++ b/Server/Hubs/CasterHub.cs @@ -168,11 +168,11 @@ namespace Remotely.Server.Hubs return ViewerHubContext.Clients.Client(viewerId).SendAsync("SendDtoToBrowser", dto); } - public Task SendIceCandidateToBrowser(string candidateJson, string viewerID) + public Task SendIceCandidateToBrowser(string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment, string viewerID) { if (_appConfig.UseWebRtc) { - return ViewerHubContext.Clients.Client(viewerID).SendAsync("ReceiveIceCandidate", candidateJson); + return ViewerHubContext.Clients.Client(viewerID).SendAsync("ReceiveIceCandidate", candidate, sdpMid, sdpMLineIndex, usernameFragment); } return Task.CompletedTask; diff --git a/Server/Hubs/ViewerHub.cs b/Server/Hubs/ViewerHub.cs index f1694770..a1472a2f 100644 --- a/Server/Hubs/ViewerHub.cs +++ b/Server/Hubs/ViewerHub.cs @@ -122,9 +122,9 @@ namespace Remotely.Server.Hubs return base.OnDisconnectedAsync(exception); } - public Task SendIceCandidateToAgent(string candidateJson) + public Task SendIceCandidateToAgent(string candidate, string sdpMid, ushort sdpMLineIndex, string usernameFragment) { - return CasterHubContext.Clients.Client(ScreenCasterID).SendAsync("ReceiveIceCandidate", candidateJson, Context.ConnectionId); + return CasterHubContext.Clients.Client(ScreenCasterID).SendAsync("ReceiveIceCandidate", candidate, sdpMid, sdpMLineIndex, usernameFragment, Context.ConnectionId); } public Task SendRtcAnswerToAgent(string sdp) diff --git a/Server/wwwroot/src/RemoteControl/RtcSession.ts b/Server/wwwroot/src/RemoteControl/RtcSession.ts index 014f5820..c9c032f2 100644 --- a/Server/wwwroot/src/RemoteControl/RtcSession.ts +++ b/Server/wwwroot/src/RemoteControl/RtcSession.ts @@ -84,17 +84,20 @@ export class RtcSession { await ViewerApp.ViewerHubConnection.SendRtcAnswer(this.PeerConnection.localDescription); console.log("Set RTC offer."); } - async ReceiveCandidate(candidate: RTCIceCandidate) { + async ReceiveCandidate(candidate: string, sdpMid: string, sdpMLineIndex: number, usernameFragment: string) { When(() => !!this.PeerConnection).then(async () => { - if (!candidate.candidate.startsWith("candidate:")) { - var normalizedCandidate = {} as any; - Object.assign(normalizedCandidate, candidate); - normalizedCandidate.candidate = `candidate:${candidate.candidate}`; - await this.PeerConnection.addIceCandidate(normalizedCandidate); - } - else { - await this.PeerConnection.addIceCandidate(candidate); + if (!candidate.startsWith("candidate:")) { + candidate = `candidate:${candidate}`; } + + var rtcCandidate = { + candidate: candidate, + sdpMid: sdpMid, + sdpMLineIndex: sdpMLineIndex, + usernameFragment: usernameFragment + } as RTCIceCandidateInit; + + await this.PeerConnection.addIceCandidate(rtcCandidate); console.log("Set ICE candidate.", candidate); }); } diff --git a/Server/wwwroot/src/RemoteControl/ViewerHubConnection.ts b/Server/wwwroot/src/RemoteControl/ViewerHubConnection.ts index a6c7c499..adc40b6a 100644 --- a/Server/wwwroot/src/RemoteControl/ViewerHubConnection.ts +++ b/Server/wwwroot/src/RemoteControl/ViewerHubConnection.ts @@ -55,7 +55,7 @@ export class ViewerHubConnection { SendIceCandidate(candidate: RTCIceCandidate) { if (candidate) { - this.Connection.invoke("SendIceCandidateToAgent", JSON.stringify(candidate)); + this.Connection.invoke("SendIceCandidateToAgent", candidate.candidate, candidate.sdpMid, candidate.sdpMLineIndex, candidate.usernameFragment); } else { this.Connection.invoke("SendIceCandidateToAgent", "{}"); @@ -147,10 +147,9 @@ export class ViewerHubConnection { await ViewerApp.RtcSession.ReceiveRtcOffer(sdp); }); - hubConnection.on("ReceiveIceCandidate", async (candidateJson: string) => { - var candidate = JSON.parse(candidateJson); + hubConnection.on("ReceiveIceCandidate", async (candidate: string, sdpMid: string, sdpMLineIndex: number, usernameFragment: string) => { console.log("Ice candidate received.", candidate); - await ViewerApp.RtcSession.ReceiveCandidate(candidate); + await ViewerApp.RtcSession.ReceiveCandidate(candidate, sdpMid, sdpMLineIndex, usernameFragment); }); hubConnection.on("ShowMessage", (message: string) => { ShowMessage(message);