Skip to content

Commit

Permalink
Refactor BitBlt and DirectX capturer into one service.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 3, 2020
1 parent 0970b2e commit 7cc2820
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 344 deletions.
10 changes: 4 additions & 6 deletions Desktop.Linux/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
using Remotely.Desktop.Linux.Services;
using Remotely.Desktop.Linux.Views;
using Remotely.ScreenCast.Core;
using Remotely.ScreenCast.Core.Capture;
using Remotely.ScreenCast.Core.Interfaces;
using Remotely.ScreenCast.Core.Models;
using Remotely.ScreenCast.Core.Services;
using Remotely.ScreenCast.Core.Communication;
using Remotely.ScreenCast.Linux.Capture;
using Remotely.ScreenCast.Linux.Services;
using Remotely.Shared.Models;
using Remotely.Shared.Services;
Expand Down Expand Up @@ -225,14 +223,14 @@ private void BuildServices()
builder.AddConsole().AddEventLog();
});

serviceCollection.AddSingleton<IScreenCaster, LinuxScreenCaster>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCasterLinux>();
serviceCollection.AddSingleton<IKeyboardMouseInput, X11Input>();
serviceCollection.AddSingleton<IClipboardService, LinuxClipboardService>();
serviceCollection.AddSingleton<IAudioCapturer, LinuxAudioCapturer>();
serviceCollection.AddSingleton<IClipboardService, ClipboardServiceLinux>();
serviceCollection.AddSingleton<IAudioCapturer, AudioCapturerLinux>();
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddTransient<ICapturer, X11Capture>();
serviceCollection.AddTransient<IScreenCapturer, X11Capture>();


ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
Expand Down
32 changes: 5 additions & 27 deletions Desktop.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using Remotely.Desktop.Win.Services;
using Remotely.Shared.Models;
using Remotely.ScreenCast.Core;
using Remotely.ScreenCast.Core.Capture;
using Remotely.ScreenCast.Core.Models;
using Remotely.ScreenCast.Core.Services;
using Remotely.ScreenCast.Win.Capture;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -255,34 +253,14 @@ private void BuildServices()
});

serviceCollection.AddSingleton<CursorIconWatcher>();
serviceCollection.AddSingleton<IScreenCaster, WinScreenCaster>();
serviceCollection.AddSingleton<IKeyboardMouseInput, WinInput>();
serviceCollection.AddSingleton<IClipboardService, WinClipboardService>();
serviceCollection.AddSingleton<IAudioCapturer, WinAudioCapturer>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCasterWin>();
serviceCollection.AddSingleton<IKeyboardMouseInput, KeyboardMouseInputWin>();
serviceCollection.AddSingleton<IClipboardService, ClipboardServiceWin>();
serviceCollection.AddSingleton<IAudioCapturer, AudioCapturerWin>();
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddTransient<ICapturer>(provider =>
{
try
{
var dxCapture = new DXCapture();
if (dxCapture.GetScreenCount() == Screen.AllScreens.Length)
{
return dxCapture;
}
else
{
Logger.Write("DX screen count doesn't match. Using CPU capturer instead.");
dxCapture.Dispose();
return new BitBltCapture();
}
}
catch
{
return new BitBltCapture();
}
});
serviceCollection.AddTransient<IScreenCapturer, ScreenCapturerWin>();


ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
Expand Down
9 changes: 4 additions & 5 deletions ScreenCast.Core/Communication/CasterSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Remotely.ScreenCast.Core.Capture;
using System.Diagnostics;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -129,9 +128,9 @@ public async Task SendScreenCapture(byte[] captureBytes, string viewerID, int le
await Connection.SendAsync("SendScreenCapture", captureBytes, viewerID, left, top, width, height, captureTime);
}

public async Task SendScreenCount(int primaryScreenIndex, int screenCount, string viewerID)
public async Task SendScreenData(string selectedScreen, string[] displayNames, string viewerID)
{
await Connection.SendAsync("SendScreenCountToBrowser", primaryScreenIndex, screenCount, viewerID);
await Connection.SendAsync("SendScreenDataToBrowser", selectedScreen, displayNames, viewerID);
}

public async Task SendScreenSize(int width, int height, string viewerID)
Expand Down Expand Up @@ -332,11 +331,11 @@ private void ApplyConnectionHandlers()
}
});

Connection.On("SelectScreen", (int screenIndex, string viewerID) =>
Connection.On("SelectScreen", (string displayName, string viewerID) =>
{
if (conductor.Viewers.TryGetValue(viewerID, out var viewer))
{
viewer.Capturer.SetSelectedScreen(screenIndex);
viewer.Capturer.SetSelectedScreen(displayName);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@

namespace Remotely.ScreenCast.Core.Interfaces
{
public interface ICapturer : IDisposable
public interface IScreenCapturer : IDisposable
{
event EventHandler<Rectangle> ScreenChanged;

bool CaptureFullscreen { get; set; }
Bitmap CurrentFrame { get; set; }
Rectangle CurrentScreenBounds { get; }
Bitmap PreviousFrame { get; set; }
event EventHandler<Rectangle> ScreenChanged;
int SelectedScreen { get; }
void SetSelectedScreen(int screenNumber);
string SelectedScreen { get; }

IEnumerable<string> GetDisplayNames();

void GetNextFrame();

int GetScreenCount();

int GetSelectedScreenIndex();
Rectangle GetVirtualScreenBounds();

void GetNextFrame();
void Init();

void SetSelectedScreen(string displayName);
}
}
5 changes: 2 additions & 3 deletions ScreenCast.Core/Models/Viewer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Remotely.ScreenCast.Core.Capture;
using Remotely.ScreenCast.Core.Communication;
using Remotely.ScreenCast.Core.Communication;
using Remotely.ScreenCast.Core.Interfaces;
using Remotely.ScreenCast.Core.Services;
using System;
Expand All @@ -20,7 +19,7 @@ public Viewer()
ImageQuality = 75;
}
public bool AutoAdjustQuality { get; internal set; } = true;
public ICapturer Capturer { get; set; }
public IScreenCapturer Capturer { get; set; }
public bool DisconnectRequested { get; set; }
public EncoderParameters EncoderParams { get; private set; }
public bool FullScreenRefreshNeeded { get; internal set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
using System.Threading;
using System.Drawing.Imaging;
using Microsoft.Extensions.DependencyInjection;
using Remotely.ScreenCast.Core.Utilities;

namespace Remotely.ScreenCast.Core.Capture
namespace Remotely.ScreenCast.Core.Services
{
public class ScreenCasterBase
{
public async Task BeginScreenCasting(string viewerID,
string requesterName,
ICapturer capturer)
IScreenCapturer capturer)
{
var conductor = ServiceContainer.Instance.GetRequiredService<Conductor>();
var viewers = conductor.Viewers;
Expand Down Expand Up @@ -59,9 +60,9 @@ public async Task BeginScreenCasting(string viewerID,

await casterSocket.SendMachineName(Environment.MachineName, viewerID);

await casterSocket.SendScreenCount(
await casterSocket.SendScreenData(
capturer.SelectedScreen,
capturer.GetScreenCount(),
capturer.GetDisplayNames().ToArray(),
viewerID);

await casterSocket.SendScreenSize(capturer.CurrentScreenBounds.Width, capturer.CurrentScreenBounds.Height, viewerID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Remotely.ScreenCast.Core.Capture
namespace Remotely.ScreenCast.Core.Utilities
{
public class ImageUtils
{
Expand Down
9 changes: 4 additions & 5 deletions ScreenCast.Linux/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Threading;
using Remotely.ScreenCast.Linux.Services;
using Remotely.ScreenCast.Linux.Capture;
using Remotely.ScreenCast.Core.Communication;
using Remotely.ScreenCast.Core.Interfaces;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -60,15 +59,15 @@ private static void BuildServices()
builder.AddConsole().AddEventLog();
});

serviceCollection.AddSingleton<IScreenCaster, LinuxScreenCaster>();
serviceCollection.AddSingleton<IScreenCaster, ScreenCasterLinux>();
serviceCollection.AddSingleton<IKeyboardMouseInput, X11Input>();
serviceCollection.AddSingleton<IClipboardService, LinuxClipboardService>();
serviceCollection.AddSingleton<IAudioCapturer, LinuxAudioCapturer>();
serviceCollection.AddSingleton<IClipboardService, ClipboardServiceLinux>();
serviceCollection.AddSingleton<IAudioCapturer, AudioCapturerLinux>();
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddSingleton<ChatHostService>();
serviceCollection.AddTransient<ICapturer, X11Capture>();
serviceCollection.AddTransient<IScreenCapturer, X11Capture>();

ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Remotely.ScreenCast.Linux.Services
{
public class LinuxAudioCapturer : IAudioCapturer
public class AudioCapturerLinux : IAudioCapturer
{
public void ToggleAudio(bool toggleOn)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Remotely.ScreenCast.Linux.Services
{
public class LinuxClipboardService : IClipboardService
public class ClipboardServiceLinux : IClipboardService
{
public event EventHandler<string> ClipboardTextChanged;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Remotely.ScreenCast.Core;
using Remotely.ScreenCast.Core.Capture;
using Remotely.ScreenCast.Core.Interfaces;
using Remotely.ScreenCast.Core.Services;
using Remotely.ScreenCast.Linux.Capture;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
Expand All @@ -13,15 +11,15 @@

namespace Remotely.ScreenCast.Linux.Services
{
public class LinuxScreenCaster : ScreenCasterBase, IScreenCaster
public class ScreenCasterLinux : ScreenCasterBase, IScreenCaster
{
public async Task BeginScreenCasting(ScreenCastRequest screenCastRequest)
{
try
{
var conductor = ServiceContainer.Instance.GetRequiredService<Conductor>();
await conductor.CasterSocket.SendCursorChange(new CursorInfo(null, Point.Empty, "default"), new List<string>() { screenCastRequest.ViewerID });
_ = BeginScreenCasting(screenCastRequest.ViewerID, screenCastRequest.RequesterName, ServiceContainer.Instance.GetRequiredService<ICapturer>());
_ = BeginScreenCasting(screenCastRequest.ViewerID, screenCastRequest.RequesterName, ServiceContainer.Instance.GetRequiredService<IScreenCapturer>());
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 7cc2820

Please sign in to comment.