From 071fe5df29f368c4d1df0eab02beea28eaeefac1 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Thu, 16 Jan 2020 19:32:29 -0800 Subject: [PATCH] Added monitoring and throttling of WebRTC output buffer. --- ScreenCast.Core/Capture/ScreenCasterBase.cs | 6 ++++++ ScreenCast.Core/Communication/WebRtcSession.cs | 3 +++ Server/Pages/_DataGrid.cshtml | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ScreenCast.Core/Capture/ScreenCasterBase.cs b/ScreenCast.Core/Capture/ScreenCasterBase.cs index 5442b98d..e075f26c 100644 --- a/ScreenCast.Core/Capture/ScreenCasterBase.cs +++ b/ScreenCast.Core/Capture/ScreenCasterBase.cs @@ -96,6 +96,12 @@ namespace Remotely.ScreenCast.Core.Capture continue; } + if (viewer.RtcSession?.CurrentBuffer > 100_000) + { + Debug.WriteLine($"Throttling output due to WebRTC buffer. Size: {viewer.RtcSession.CurrentBuffer}"); + await Task.Delay((int)(viewer.RtcSession.CurrentBuffer - 100_000)); + } + capturer.GetNextFrame(); var diffArea = ImageUtils.GetDiffArea(capturer.CurrentFrame, capturer.PreviousFrame, capturer.CaptureFullscreen); diff --git a/ScreenCast.Core/Communication/WebRtcSession.cs b/ScreenCast.Core/Communication/WebRtcSession.cs index b3caec1d..417016f8 100644 --- a/ScreenCast.Core/Communication/WebRtcSession.cs +++ b/ScreenCast.Core/Communication/WebRtcSession.cs @@ -16,6 +16,8 @@ namespace Remotely.ScreenCast.Core.Communication public event EventHandler LocalSdpReady; + public ulong CurrentBuffer { get; private set; } + public bool IsDataChannelOpen => CaptureChannel?.State == DataChannel.ChannelState.Open; private DataChannel CaptureChannel { get; set; } private PeerConnection PeerConnection { get; set; } @@ -84,6 +86,7 @@ namespace Remotely.ScreenCast.Core.Communication private void DataChannel_BufferingChanged(ulong previous, ulong current, ulong limit) { Debug.WriteLine($"DataChannel buffering changed. Previous: {previous}. Current: {current}. Limit: {limit}."); + CurrentBuffer = current; } private void PeerConnection_Connected() diff --git a/Server/Pages/_DataGrid.cshtml b/Server/Pages/_DataGrid.cshtml index d8d94355..a0e831a4 100644 --- a/Server/Pages/_DataGrid.cshtml +++ b/Server/Pages/_DataGrid.cshtml @@ -19,7 +19,7 @@ - +
Online