Skip to content

Commit

Permalink
Auto quality tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 6, 2020
1 parent b5810fd commit 28d2d15
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 21 deletions.
32 changes: 18 additions & 14 deletions ScreenCast.Core/Models/Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ namespace Remotely.ScreenCast.Core.Models
{
public class Viewer : IDisposable
{
private long imageQuality;
private int imageQuality;

public Viewer()
{
EncoderParams = new EncoderParameters();
ImageQuality = 60;
}
public bool AutoAdjustQuality { get; internal set; } = true;
Expand All @@ -25,7 +24,7 @@ public Viewer()
public EncoderParameters EncoderParams { get; private set; }
public bool FullScreenRefreshNeeded { get; internal set; }
public bool HasControl { get; set; }
public long ImageQuality
public int ImageQuality
{
get
{
Expand All @@ -43,14 +42,19 @@ public long ImageQuality
}
imageQuality = value;

EncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, value);
EncoderParams = new EncoderParameters()
{
Param = new[]
{
new EncoderParameter(Encoder.Quality, value)
}
};
}
}
public string Name { get; set; }
public int WebSocketBuffer { get; set; }
public WebRtcSession RtcSession { get; set; }
public string ViewerConnectionID { get; set; }

public int WebSocketBuffer { get; set; }
public void Dispose()
{
RtcSession?.Dispose();
Expand All @@ -61,6 +65,11 @@ public bool IsStalled()
return RtcSession?.CurrentBuffer > 1_000_000 || WebSocketBuffer > 1_000_000;
}

public bool IsUsingWebRtc()
{
return RtcSession?.IsPeerConnected == true && RtcSession?.IsDataChannelOpen == true;
}

public async Task ThrottleIfNeeded()
{
var currentBuffer = IsUsingWebRtc() ?
Expand All @@ -71,23 +80,18 @@ public async Task ThrottleIfNeeded()
{
if (AutoAdjustQuality)
{
ImageQuality -= 10;
ImageQuality = Math.Max(ImageQuality - 1, 0);
Logger.Debug($"Auto-adjusting image quality. Quality: {ImageQuality}");
}

var delay = (int)Math.Ceiling((currentBuffer - 100_000) * .0025);
Logger.Debug($"Throttling output due to buffer size. Size: {currentBuffer}. Delay: {delay}");
await Task.Delay(delay);
}
else
else if (AutoAdjustQuality)
{
ImageQuality = Math.Min(ImageQuality + 10, 60);
ImageQuality = Math.Min(ImageQuality + 1, 60);
}
}

public bool IsUsingWebRtc()
{
return RtcSession?.IsPeerConnected == true && RtcSession?.IsDataChannelOpen == true;
}
}
}
3 changes: 1 addition & 2 deletions ScreenCast.Core/Services/ScreenCasterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ await casterSocket.SendScreenData(
else
{
await casterSocket.SendScreenCapture(encodedImageBytes, viewerID, diffArea.Left, diffArea.Top, diffArea.Width, diffArea.Height, viewer.ImageQuality);
// Shave some off so it doesn't get deadlocked by dropped frames.
viewer.WebSocketBuffer += (int)(encodedImageBytes.Length * .9);
viewer.WebSocketBuffer += encodedImageBytes.Length;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion ScreenCast.Core/Utilities/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static byte[] EncodeBitmap(Bitmap bitmap, EncoderParameters encoderParams
{
using (var ms = new MemoryStream())
{
//bitmap.Save(ms, ImageFormat.Jpeg);
bitmap.Save(ms, JpegEncoder, encoderParams);
return ms.ToArray();
}
Expand Down
3 changes: 2 additions & 1 deletion Server/wwwroot/scripts/RemoteControl/RCBrowserSockets.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Server/wwwroot/scripts/RemoteControl/RCBrowserSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export class RCBrowserSockets {
//console.log("Websocket frame received.");
this.SendBufferUpdate(buffer.byteLength);

if (Number(UI.QualitySlider.value) != imageQuality) {
if (UI.AutoQualityAdjustCheckBox.checked &&
Number(UI.QualitySlider.value) != imageQuality) {
UI.QualitySlider.value = String(imageQuality);
}

Expand Down
4 changes: 4 additions & 0 deletions Server/wwwroot/scripts/RemoteControl/RtcSession.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Server/wwwroot/scripts/RemoteControl/RtcSession.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Server/wwwroot/scripts/RemoteControl/RtcSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export class RtcSession {
}
}
ProcessFrameInfo(frameInfo: FrameInfo) {
if (UI.AutoQualityAdjustCheckBox.checked &&
Number(UI.QualitySlider.value) != frameInfo.ImageQuality) {
UI.QualitySlider.value = String(frameInfo.ImageQuality);
}

if (frameInfo.EndOfFrame) {
var url = window.URL.createObjectURL(new Blob(this.PartialFrames));
var img = document.createElement("img");
Expand Down

0 comments on commit 28d2d15

Please sign in to comment.