Skip to content

Commit

Permalink
Restructure for sending cursor change through WebRTC.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 21, 2020
1 parent 731dd92 commit 6471e4b
Show file tree
Hide file tree
Showing 27 changed files with 248 additions and 243 deletions.
118 changes: 59 additions & 59 deletions Desktop.Linux/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public MainWindowViewModel()
BuildServices();

Conductor = Services.GetRequiredService<Conductor>();
CasterSocket = Services.GetRequiredService<CasterSocket>();

Conductor.SessionIDChanged += SessionIDChanged;
Conductor.ViewerRemoved += ViewerRemoved;
Expand All @@ -54,8 +55,6 @@ public MainWindowViewModel()

public static MainWindowViewModel Current { get; private set; }

public static IServiceProvider Services => ServiceContainer.Instance;

public ICommand ChangeServerCommand => new Executor(async (param) =>
{
await PromptForHostName();
Expand All @@ -67,8 +66,6 @@ public MainWindowViewModel()
(param as Window)?.Close();
});

public Conductor Conductor { get; }

public ICommand CopyLinkCommand => new Executor(async (param) =>
{
await App.Current.Clipboard.SetTextAsync($"{Host}/RemoteControl?sessionID={SessionID.Replace(" ", "")}");
Expand Down Expand Up @@ -121,7 +118,7 @@ public bool IsCopyMessageVisible
foreach (Viewer viewer in viewerList)
{
viewer.DisconnectRequested = true;
await Conductor.CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
await CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
}
});

Expand All @@ -132,52 +129,13 @@ public string SessionID
}

public ObservableCollection<Viewer> Viewers { get; } = new ObservableCollection<Viewer>();
private async Task CheckDependencies()
{
var dependencies = new string[]
{
"libx11-dev",
"libc6-dev",
"libgdiplus",
"libxtst-dev",
"xclip"
};

foreach (var dependency in dependencies)
{
var proc = Process.Start("dpkg", $"-s {dependency}");
proc.WaitForExit();
if (proc.ExitCode != 0)
{
var commands = "sudo apt-get -y install libx11-dev ; " +
"sudo apt-get -y install libc6-dev ; " +
"sudo apt-get -y install libgdiplus ; " +
"sudo apt-get -y install libxtst-dev ; " +
"sudo apt-get -y install xclip";

await App.Current.Clipboard.SetTextAsync(commands);

var message = "The following dependencies are required. Install commands have been copied to your clipboard." +
Environment.NewLine + Environment.NewLine +
"Please paste them into a terminal and run, then try opening Remotely again." +
Environment.NewLine + Environment.NewLine +
"libx11-dev" + Environment.NewLine +
"libc6-dev" + Environment.NewLine +
"libgdiplus" + Environment.NewLine +
"libxtst-dev" + Environment.NewLine +
"xclip";

await MessageBox.Show(message, "Dependencies Required", MessageBoxType.OK);

Environment.Exit(0);
}
}
}

private static IServiceProvider Services => ServiceContainer.Instance;
private CasterSocket CasterSocket { get; }
private Conductor Conductor { get; }
public async Task GetSessionID()
{
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
await CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await CasterSocket.GetSessionID();
}

public async Task Init()
Expand All @@ -198,32 +156,32 @@ public async Task Init()
}
Conductor.ProcessArgs(new string[] { "-mode", "Normal", "-host", Host });

await Conductor.Connect();
await CasterSocket.Connect(Conductor.Host);

Conductor.CasterSocket.Connection.Closed += async (ex) =>
CasterSocket.Connection.Closed += async (ex) =>
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
SessionID = "Disconnected";
});
};

Conductor.CasterSocket.Connection.Reconnecting += async (ex) =>
CasterSocket.Connection.Reconnecting += async (ex) =>
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
SessionID = "Reconnecting";
});
};

Conductor.CasterSocket.Connection.Reconnected += async (arg) =>
CasterSocket.Connection.Reconnected += async (arg) =>
{
await GetSessionID();
};


await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
await CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await CasterSocket.GetSessionID();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -264,7 +222,7 @@ private void BuildServices()
builder.AddConsole().AddDebug();
});

serviceCollection.AddSingleton<IScreenCaster, ScreenCasterLinux>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCaster>();
serviceCollection.AddSingleton<IKeyboardMouseInput, KeyboardMouseInputLinux>();
serviceCollection.AddSingleton<IClipboardService, ClipboardServiceLinux>();
serviceCollection.AddSingleton<IAudioCapturer, AudioCapturerLinux>();
Expand All @@ -275,21 +233,63 @@ private void BuildServices()
serviceCollection.AddTransient<Viewer>();
serviceCollection.AddScoped<IFileTransferService, FileTransferService>();
serviceCollection.AddScoped<IWebRtcSessionFactory, WebRtcSessionFactory>();
serviceCollection.AddSingleton<ICursorIconWatcher, CursorIconWatcherLinux>();


ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
}

private async Task CheckDependencies()
{
var dependencies = new string[]
{
"libx11-dev",
"libc6-dev",
"libgdiplus",
"libxtst-dev",
"xclip"
};

foreach (var dependency in dependencies)
{
var proc = Process.Start("dpkg", $"-s {dependency}");
proc.WaitForExit();
if (proc.ExitCode != 0)
{
var commands = "sudo apt-get -y install libx11-dev ; " +
"sudo apt-get -y install libc6-dev ; " +
"sudo apt-get -y install libgdiplus ; " +
"sudo apt-get -y install libxtst-dev ; " +
"sudo apt-get -y install xclip";

await App.Current.Clipboard.SetTextAsync(commands);

var message = "The following dependencies are required. Install commands have been copied to your clipboard." +
Environment.NewLine + Environment.NewLine +
"Please paste them into a terminal and run, then try opening Remotely again." +
Environment.NewLine + Environment.NewLine +
"libx11-dev" + Environment.NewLine +
"libc6-dev" + Environment.NewLine +
"libgdiplus" + Environment.NewLine +
"libxtst-dev" + Environment.NewLine +
"xclip";

await MessageBox.Show(message, "Dependencies Required", MessageBoxType.OK);

Environment.Exit(0);
}
}
}
private void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequest)
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
var result = await MessageBox.Show($"You've received a connection request from {screenCastRequest.RequesterName}. Accept?", "Connection Request", MessageBoxType.YesNo);
if (result == MessageBoxResult.Yes)
{
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
await Conductor.CasterSocket.SendCursorChange(new CursorInfo(null, Point.Empty, "default"), new List<string>() { screenCastRequest.ViewerID });
_ = Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
});
}
});
Expand Down
39 changes: 21 additions & 18 deletions Desktop.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public MainWindowViewModel()

BuildServices();

CursorIconWatcher = Services.GetRequiredService<CursorIconWatcher>();
CursorIconWatcher = Services.GetRequiredService<ICursorIconWatcher>();
CursorIconWatcher.OnChange += CursorIconWatcher_OnChange;
Services.GetRequiredService<IClipboardService>().BeginWatching();
Conductor = Services.GetRequiredService<Conductor>();
CasterSocket = Services.GetRequiredService<CasterSocket>();
Conductor.SessionIDChanged += SessionIDChanged;
Conductor.ViewerRemoved += ViewerRemoved;
Conductor.ViewerAdded += ViewerAdded;
Expand All @@ -59,9 +60,9 @@ public ICommand ChangeServerCommand
}
}

public Conductor Conductor { get; }

public CursorIconWatcher CursorIconWatcher { get; private set; }
private Conductor Conductor { get; }
private CasterSocket CasterSocket { get; }
private ICursorIconWatcher CursorIconWatcher { get; set; }

public ICommand ElevateToAdminCommand
{
Expand Down Expand Up @@ -139,7 +140,7 @@ public ICommand RemoveViewersCommand
{
viewer.DisconnectRequested = true;
ViewerRemoved(this, viewer.ViewerConnectionID);
await Conductor.CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
await CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
}
},
(param) =>
Expand Down Expand Up @@ -169,8 +170,8 @@ public void CopyLink()

public async Task GetSessionID()
{
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
await CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await CasterSocket.GetSessionID();
}

public async Task Init()
Expand All @@ -188,10 +189,10 @@ public async Task Init()
Conductor.ProcessArgs(new string[] { "-mode", "Normal", "-host", Host });
try
{
await Conductor.Connect();
await CasterSocket.Connect(Conductor.Host);


Conductor.CasterSocket.Connection.Closed += async (ex) =>
CasterSocket.Connection.Closed += async (ex) =>
{
await App.Current.Dispatcher.InvokeAsync(() =>
{
Expand All @@ -200,7 +201,7 @@ await App.Current.Dispatcher.InvokeAsync(() =>
});
};

Conductor.CasterSocket.Connection.Reconnecting += async (ex) =>
CasterSocket.Connection.Reconnecting += async (ex) =>
{
await App.Current.Dispatcher.InvokeAsync(() =>
{
Expand All @@ -209,7 +210,7 @@ await App.Current.Dispatcher.InvokeAsync(() =>
});
};

Conductor.CasterSocket.Connection.Reconnected += async (arg) =>
CasterSocket.Connection.Reconnected += async (arg) =>
{
await GetSessionID();
};
Expand Down Expand Up @@ -266,8 +267,8 @@ private void BuildServices()
builder.AddConsole().AddDebug().AddEventLog();
});

serviceCollection.AddSingleton<CursorIconWatcher>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCasterWin>();
serviceCollection.AddSingleton<ICursorIconWatcher, CursorIconWatcherWin>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCaster>();
serviceCollection.AddSingleton<IKeyboardMouseInput, KeyboardMouseInputWin>();
serviceCollection.AddSingleton<IClipboardService, ClipboardServiceWin>();
serviceCollection.AddSingleton<IAudioCapturer, AudioCapturerWin>();
Expand All @@ -285,9 +286,12 @@ private void BuildServices()

private async void CursorIconWatcher_OnChange(object sender, CursorInfo cursor)
{
if (Conductor?.CasterSocket?.IsConnected == true && Conductor?.Viewers?.Count > 0)
if (Conductor?.Viewers?.Count > 0)
{
await Conductor?.CasterSocket?.SendCursorChange(cursor, Conductor.Viewers.Keys.ToList());
foreach (var viewer in Conductor.Viewers.Values)
{
await viewer.SendCursorChange(cursor);
}
}
}

Expand All @@ -299,10 +303,9 @@ private void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequ
var result = MessageBox.Show(Application.Current.MainWindow, $"You've received a connection request from {screenCastRequest.RequesterName}. Accept?", "Connection Request", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Task.Run(async () =>
Task.Run(() =>
{
await Conductor.CasterSocket.SendCursorChange(CursorIconWatcher.GetCurrentCursor(), new List<string>() { screenCastRequest.ViewerID });
_ = Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions ScreenCast.Core/Communication/CasterSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public async Task SendCtrlAltDel()
await Connection.SendAsync("CtrlAltDel");
}

public async Task SendCursorChange(CursorInfo cursor, List<string> viewerIDs)
public async Task SendCursorChange(CursorInfo cursor, string viewerID)
{
await Connection.SendAsync("SendCursorChange", cursor, viewerIDs);
await Connection.SendAsync("SendCursorChange", cursor, viewerID);
}

public async Task SendDeviceInfo(string serviceID, string machineName, string deviceID)
Expand Down
5 changes: 5 additions & 0 deletions ScreenCast.Core/Communication/WebRtcSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public void SendScreenSize(int width, int height)
SendDto(new ScreenSizeDto(width, height));
}

public void SendCursorChange(CursorInfo cursorInfo)
{
SendDto(new CursorChangeDto(cursorInfo.ImageBytes, cursorInfo.HotSpot.X, cursorInfo.HotSpot.Y, cursorInfo.CssOverride));
}

public void SetRemoteDescription(string type, string sdp)
{
PeerConnection.SetRemoteDescription(type, sdp);
Expand Down
10 changes: 0 additions & 10 deletions ScreenCast.Core/Conductor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Remotely.ScreenCast.Core
{
public class Conductor
{
public Conductor(CasterSocket casterSocket)
{
CasterSocket = casterSocket;
}

public event EventHandler<ScreenCastRequest> ScreenCastRequested;

public event EventHandler<string> SessionIDChanged;
Expand All @@ -26,18 +21,13 @@ public Conductor(CasterSocket casterSocket)
public event EventHandler<string> ViewerRemoved;

public Dictionary<string, string> ArgDict { get; set; }
public CasterSocket CasterSocket { get; private set; }
public string DeviceID { get; private set; }
public string Host { get; private set; }
public AppMode Mode { get; private set; }
public string OrganizationName { get; private set; }
public string RequesterID { get; private set; }
public string ServiceID { get; private set; }
public ConcurrentDictionary<string, Viewer> Viewers { get; } = new ConcurrentDictionary<string, Viewer>();
public async Task Connect()
{
await CasterSocket.Connect(Host);
}

public void InvokeScreenCastRequested(ScreenCastRequest viewerIdAndRequesterName)
{
Expand Down
15 changes: 15 additions & 0 deletions ScreenCast.Core/Interfaces/ICursorIconWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace Remotely.ScreenCast.Core.Interfaces
{
public interface ICursorIconWatcher
{
event EventHandler<CursorInfo> OnChange;

CursorInfo GetCurrentCursor();
}

}
Loading

0 comments on commit 6471e4b

Please sign in to comment.