aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs248
1 files changed, 184 insertions, 64 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs
index 0d5bd8559..ae33bbfce 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs
@@ -19,6 +19,7 @@ using Tango.RemoteDesktop.Input;
using Tango.RemoteDesktop.Network;
using Tango.Settings;
using Tango.Transport;
+using Tango.WebRTC;
using static Tango.RemoteDesktop.Input.MouseController;
namespace Tango.PPC.Common.RemoteDesktop
@@ -31,6 +32,8 @@ namespace Tango.PPC.Common.RemoteDesktop
public String Token { get; set; }
public ExternalBridgeReceiver Receiver { get; set; }
public bool InitialPacketSent { get; set; }
+ public WebRtcClient WebRtcClient { get; set; }
+ public bool IsWebRtcReady { get; set; }
}
private RemoteDesktopPacket _initialPacket;
@@ -65,19 +68,19 @@ namespace Tango.PPC.Common.RemoteDesktop
{
_engine.CaptureRegion = new CaptureRegion()
{
- Left = (int)mainWindow.Left,
- Top = (int)mainWindow.Top,
- Width = (int)mainWindow.Width,
- Height = (int)mainWindow.Height
+ Left = (int)mainWindow.Left + 10,
+ Top = (int)mainWindow.Top + 5,
+ Width = (int)mainWindow.Width - 20,
+ Height = (int)mainWindow.Height - 15
};
};
_engine.CaptureRegion = new CaptureRegion()
{
- Left = (int)mainWindow.Left,
- Top = (int)mainWindow.Top,
- Width = (int)mainWindow.Width,
- Height = (int)mainWindow.Height
+ Left = (int)mainWindow.Left + 10,
+ Top = (int)mainWindow.Top + 5,
+ Width = (int)mainWindow.Width - 20,
+ Height = (int)mainWindow.Height - 15
};
_engine.CaptureMethod = new GdiScreenCapture();
#endif
@@ -102,11 +105,11 @@ namespace Tango.PPC.Common.RemoteDesktop
_clients.Remove(client);
}
- _clients.Add(new RemoteDesktopClient()
- {
- Receiver = receiver,
- Token = token
- });
+ RemoteDesktopClient newClient = new RemoteDesktopClient();
+ newClient.Receiver = receiver;
+ newClient.Token = token;
+ newClient.WebRtcClient = new WebRtcClient();
+ _clients.Add(newClient);
await receiver.SendGenericResponse(new StartRemoteDesktopSessionResponse()
{
@@ -122,6 +125,78 @@ namespace Tango.PPC.Common.RemoteDesktop
}
}
+ [ExternalBridgeRequestHandlerMethod(typeof(WebRtcIceCandidateRequest))]
+ public async void OnWebRtcIceCandidateRequestReceived(WebRtcIceCandidateRequest request, String token, ExternalBridgeReceiver receiver)
+ {
+ var client = _clients.SingleOrDefault(x => x.Receiver == receiver);
+
+ if (client != null)
+ {
+ try
+ {
+ await receiver.SendGenericResponse(new WebRtcIceCandidateResponse() { }, token);
+ client.WebRtcClient.AddIceCandidate(request.IceCandidate);
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log($"Error adding WebRTC ice candidate received from the remote connection.\n{ex.FlattenMessage()}");
+ }
+ }
+ }
+
+ [ExternalBridgeRequestHandlerMethod(typeof(WebRtcOfferRequest))]
+ public async void OnWebRtcOfferRequestReceived(WebRtcOfferRequest request, String token, ExternalBridgeReceiver receiver)
+ {
+ var client = _clients.SingleOrDefault(x => x.Receiver == receiver);
+
+ if (client != null)
+ {
+ try
+ {
+
+ try
+ {
+ client.WebRtcClient.NewIceCandidate += async (x, e) =>
+ {
+ try
+ {
+ await receiver.SendGenericRequest<WebRtcIceCandidateRequest, WebRtcIceCandidateResponse>(new WebRtcIceCandidateRequest() { IceCandidate = e.IceCandidate });
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, $"Error sending ice candidate to remote peer.\n{ex.FlattenMessage()}");
+ }
+ };
+ client.WebRtcClient.Ready += (x, e) =>
+ {
+ client.IsWebRtcReady = true;
+ };
+ client.WebRtcClient.Disconnected += (x, e) =>
+ {
+ client.IsWebRtcReady = false;
+ };
+
+ client.WebRtcClient.FrameWidth = 800;
+ client.WebRtcClient.FrameHeight = 1280;
+ client.WebRtcClient.FrameRate = _engine.FrameRate;
+
+ await client.WebRtcClient.Init();
+
+ var answer = await client.WebRtcClient.CreateAnswer(request.Offer);
+ await receiver.SendGenericResponse(new WebRtcOfferResponse() { Answer = answer }, token);
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log($"Error initializing the web RTC client.\n{ex.FlattenMessage()}");
+ }
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log($"Error responding to WebRTC offer request.\n{ex.FlattenMessage()}");
+ }
+ }
+ }
+
[ExternalBridgeRequestHandlerMethod(typeof(StopRemoteDesktopSessionRequest))]
public async void OnStopRemoteDesktopSessionRequestReceived(StopRemoteDesktopSessionRequest request, String token, ExternalBridgeReceiver receiver)
{
@@ -130,6 +205,11 @@ namespace Tango.PPC.Common.RemoteDesktop
if (client != null)
{
_clients.Remove(client);
+
+ if (client.WebRtcClient != null)
+ {
+ client.WebRtcClient.Dispose();
+ }
}
if (_clients.Count == 0)
@@ -199,75 +279,103 @@ namespace Tango.PPC.Common.RemoteDesktop
Bitmap = e.Frame.ToEncoder<PngEncoder>().ToArray(),
};
- foreach (var client in _clients.ToList().Where(x => !x.InitialPacketSent))
+ if (_clients.Count > 0)
{
- try
- {
- await client.Receiver.SendGenericResponse(new StartRemoteDesktopSessionResponse()
- {
- Packet = _initialPacket,
- }, client.Token, new TransportResponseConfig()
- {
- Immediate = false,
- });
+ bool useWebRTC = _clients.ToList().All(x => x.IsWebRtcReady);
- client.InitialPacketSent = true;
- }
- catch (Exception ex)
+ if (useWebRTC)
{
- Debug.WriteLine(ex);
- }
- }
-
- if (e.Frame.DifferenceCount > 0)
- {
- RemoteDesktopPacket packet = null;
+ _engine.EnableComparer = false;
- if (!e.Frame.DifferenceAvailable)
- {
- Debug.WriteLine("Using Jpeg...");
- packet = new RemoteDesktopPacket()
+ foreach (var client in _clients.ToList())
{
- Bitmap = e.Frame.ToEncoder<TurboJpegEncoder>().ToArray(30)
- };
+ try
+ {
+ client.WebRtcClient.PushFrame(e.Frame.ToBitmap());
+ }
+ catch (Exception ex)
+ {
+ Debug.WriteLine(ex);
+ }
+ }
+
+ e.Frame.Dispose();
}
else
{
- var diffFrame = e.Frame.ToDifference();
- diffFrame = diffFrame.OptimizeBounds();
+ _engine.EnableComparer = true;
- packet = new RemoteDesktopPacket()
+ foreach (var client in _clients.ToList().Where(x => !x.InitialPacketSent))
{
- Bitmap = diffFrame.ToEncoder<PngEncoder>().ToArray(),
- IsPartial = true,
- PartialRegion = new CaptureRegion(diffFrame.Left, diffFrame.Top, diffFrame.Width, diffFrame.Height),
- };
-
- diffFrame.Dispose();
- }
-
- Debug.WriteLine($"Bitmap Size: {packet.Bitmap.Length / 1000} kb");
+ try
+ {
+ await client.Receiver.SendGenericResponse(new StartRemoteDesktopSessionResponse()
+ {
+ Packet = _initialPacket,
+ }, client.Token, new TransportResponseConfig()
+ {
+ Immediate = false,
+ });
- e.Frame.Dispose();
+ client.InitialPacketSent = true;
+ }
+ catch (Exception ex)
+ {
+ Debug.WriteLine(ex);
+ }
+ }
- foreach (var client in _clients.ToList().Where(x => x.InitialPacketSent))
- {
- try
+ if (e.Frame.DifferenceCount > 0)
{
- await client.Receiver.SendGenericResponse(new StartRemoteDesktopSessionResponse()
+ RemoteDesktopPacket packet = null;
+
+ if (!e.Frame.DifferenceAvailable)
{
- Packet = packet
- }, client.Token, new TransportResponseConfig()
+ Debug.WriteLine("Using Jpeg...");
+ packet = new RemoteDesktopPacket()
+ {
+ Bitmap = e.Frame.ToEncoder<TurboJpegEncoder>().ToArray(30)
+ };
+ }
+ else
{
- Immediate = false,
- });
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex);
+ var diffFrame = e.Frame.ToDifference();
+ diffFrame = diffFrame.OptimizeBounds();
+
+ packet = new RemoteDesktopPacket()
+ {
+ Bitmap = diffFrame.ToEncoder<PngEncoder>().ToArray(),
+ IsPartial = true,
+ PartialRegion = new CaptureRegion(diffFrame.Left, diffFrame.Top, diffFrame.Width, diffFrame.Height),
+ };
+
+ diffFrame.Dispose();
+ }
+
+ Debug.WriteLine($"Bitmap Size: {packet.Bitmap.Length / 1000} kb");
+
+ foreach (var client in _clients.ToList().Where(x => x.InitialPacketSent))
+ {
+ try
+ {
+ await client.Receiver.SendGenericResponse(new StartRemoteDesktopSessionResponse()
+ {
+ Packet = packet
+ }, client.Token, new TransportResponseConfig()
+ {
+ Immediate = false,
+ });
+ }
+ catch (Exception ex)
+ {
+ Debug.WriteLine(ex);
+ }
+ }
}
}
}
+
+ e.Frame.Dispose();
}
public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
@@ -277,6 +385,18 @@ namespace Tango.PPC.Common.RemoteDesktop
if (client != null)
{
_clients.Remove(client);
+
+ try
+ {
+ if (client.WebRtcClient != null)
+ {
+ client.WebRtcClient.Dispose();
+ }
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log($"Error disposing the WebRTC client.\n{ex.FlattenMessage()}");
+ }
}
if (_clients.Count == 0)