Skip to content

Commit

Permalink
Added WebRTC video stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 7, 2020
1 parent 0f2bd4f commit 23ada6e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 41 deletions.
5 changes: 5 additions & 0 deletions Desktop.Core/Models/Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public async Task InitializeWebRtc()
}
}

public bool IsUsingWebRtcVideo()
{
return RtcSession?.IsVideoTrackConnected == true;
}

public bool IsStalled()
{
return PendingSentFrames.TryPeek(out var result) && DateTimeOffset.Now - result > TimeSpan.FromSeconds(15);
Expand Down
6 changes: 6 additions & 0 deletions Desktop.Core/Services/ScreenCaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Remotely.Shared.Models;
using Remotely.Shared.Win32;
using System.Drawing;
using System.Threading;

namespace Remotely.Desktop.Core.Services
{
Expand Down Expand Up @@ -95,6 +96,11 @@ public async Task BeginScreenCasting(ScreenCastRequest screenCastRequest)
{
try
{
if (viewer.IsUsingWebRtcVideo())
{
Thread.Sleep(10);
continue;
}
if (viewer.IsStalled())
{
// Viewer isn't responding. Abort sending.
Expand Down
83 changes: 47 additions & 36 deletions Desktop.Core/Services/WebRtcSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public WebRtcSession(Viewer viewer, IRtcMessageHandler rtcMessageHandler)
private Transceiver Transceiver { get; set; }
private ExternalVideoTrackSource VideoSource { get; set; }
private Viewer Viewer { get; }
public bool IsVideoTrackConnected
{
get
{
return PeerSession?.LocalVideoTracks?.FirstOrDefault()?.Source?.Enabled == true;
}
}

public void AddIceCandidate(string sdpMid, int sdpMlineIndex, string candidate)
{
Expand All @@ -47,7 +54,12 @@ public void AddIceCandidate(string sdpMid, int sdpMlineIndex, string candidate)

public void Dispose()
{
PeerSession?.Dispose();
try
{
PeerSession?.Close();
PeerSession?.Dispose();
}
catch { }
}

public async Task Init(IceServerModel[] iceServers)
Expand Down Expand Up @@ -82,12 +94,12 @@ public async Task Init(IceServerModel[] iceServers)
CaptureChannel.MessageReceived += CaptureChannel_MessageReceived;
CaptureChannel.StateChanged += CaptureChannel_StateChanged;

//VideoSource = ExternalVideoTrackSource.CreateFromArgb32Callback(GetCaptureFrame);
//Transceiver = PeerSession.AddTransceiver(MediaKind.Video);
//Transceiver.LocalVideoTrack = LocalVideoTrack.CreateFromSource(VideoSource, new LocalVideoTrackInitConfig()
//{
// trackName = "ScreenCapture"
//});
VideoSource = ExternalVideoTrackSource.CreateFromArgb32Callback(GetCaptureFrame);
Transceiver = PeerSession.AddTransceiver(MediaKind.Video);
Transceiver.LocalVideoTrack = LocalVideoTrack.CreateFromSource(VideoSource, new LocalVideoTrackInitConfig()
{
trackName = "ScreenCapture"
});

PeerSession.CreateOffer();
}
Expand Down Expand Up @@ -193,35 +205,34 @@ private void DataChannel_BufferingChanged(ulong previous, ulong current, ulong l
CurrentBuffer = current;
}

//private void GetCaptureFrame(in FrameRequest request)
//{
// Viewer.Capturer.GetNextFrame();
// using (var bitmapCopy = (Bitmap)Viewer.Capturer.CurrentFrame.Clone())
// {
// var bitmapData = bitmapCopy.LockBits(
// Viewer.Capturer.CurrentScreenBounds,
// System.Drawing.Imaging.ImageLockMode.ReadOnly,
// System.Drawing.Imaging.PixelFormat.Format32bppArgb);

// try
// {
// var frame = new Argb32VideoFrame()
// {
// data = bitmapData.Scan0,
// height = (uint)bitmapCopy.Height,
// width = (uint)bitmapCopy.Width,
// stride = bitmapData.Stride
// };
// request.CompleteRequest(in frame);
// }
// finally
// {
// bitmapCopy.UnlockBits(bitmapData);
// }

// }

//}
private void GetCaptureFrame(in FrameRequest request)
{
using (var currentFrame = Viewer.Capturer.GetNextFrame())
{
var bitmapData = currentFrame.LockBits(
Viewer.Capturer.CurrentScreenBounds,
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

try
{
var frame = new Argb32VideoFrame()
{
data = bitmapData.Scan0,
height = (uint)currentFrame.Height,
width = (uint)currentFrame.Width,
stride = bitmapData.Stride
};
request.CompleteRequest(in frame);
}
finally
{
currentFrame.UnlockBits(bitmapData);
}

}

}

private void PeerConnection_Connected()
{
Expand Down
4 changes: 2 additions & 2 deletions Server/wwwroot/scripts/RemoteControl/UI.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/UI.js.map

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

4 changes: 2 additions & 2 deletions Server/wwwroot/scripts/RemoteControl/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export function Prompt(promptMessage: string): Promise<string> {
export function SetScreenSize(width: number, height: number) {
ScreenViewer.width = width;
ScreenViewer.height = height;
VideoScreenViewer.width = width;
VideoScreenViewer.height = height;
//VideoScreenViewer.width = width;
//VideoScreenViewer.height = height;
Screen2DContext.clearRect(0, 0, width, height);
}

Expand Down

0 comments on commit 23ada6e

Please sign in to comment.