Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #71

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ bin
obj
*.user
.vs
/WebRtc.NET/Release
/WebRtc.NET/Release_x64
/WebRtc.NET/Debug
/WebRtcNative/include/webrtc/examples
/WebRtcNative/Debug
/WebRtcNative/Debug_x64
/WebRtc.NET.sdf
/WebRtc.NET.VC.opendb
/WebRtc.NET/include/webrtc/examples
/WebRtc.NET.VC.db
/WebRtc.NET.VC.VC.opendb
/ipch
42 changes: 28 additions & 14 deletions WebRtc.NET.Demo/MainForm.Designer.cs

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

102 changes: 44 additions & 58 deletions WebRtc.NET.Demo/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,11 @@ public MainForm()

textBoxExtIP.Text = "192.168.0.100";

comboBoxVideo.DataSource = ManagedConductor.GetVideoDevices();
comboBoxVideo.DataSource = WebRtcNative.VideoDevices();
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (encoderRemote != null)
{
encoderRemote.Dispose();
}

if (webSocketServer != null)
{
webSocketServer.Dispose();
Expand All @@ -53,21 +48,24 @@ private void MainForm_Shown(object sender, EventArgs e)
checkBoxWebsocket.Checked = true;
}

readonly TurboJpegEncoder encoderRemote = TurboJpegEncoder.CreateEncoder();
internal void ResetRemote()
{
pictureBoxRemote.Image = null;
if (remoteImg != null)
{
remoteImg.Dispose();
remoteImg = null;
}
}

byte[] bgrBuffremote;
Bitmap remoteImg;
public unsafe void OnRenderRemote(byte* yuv, uint w, uint h)
public void OnRenderRemote(IntPtr BGR24, uint w, uint h)
{
lock (pictureBoxRemote)
if (remoteImg == null)
{
if (0 == encoderRemote.EncodeI420toBGR24(yuv, w, h, ref bgrBuffremote, true))
lock (pictureBoxRemote)
{
if (remoteImg == null)
{
var bufHandle = GCHandle.Alloc(bgrBuffremote, GCHandleType.Pinned);
remoteImg = new Bitmap((int)w, (int)h, (int)w * 3, PixelFormat.Format24bppRgb, bufHandle.AddrOfPinnedObject());
}
remoteImg = new Bitmap((int)w, (int)h, (int)w * 3, PixelFormat.Format24bppRgb, BGR24);
}
}

Expand Down Expand Up @@ -97,20 +95,24 @@ public unsafe void OnRenderRemote(byte* yuv, uint w, uint h)
}
});

readonly TurboJpegEncoder encoderLocal = TurboJpegEncoder.CreateEncoder();
byte[] bgrBufflocal;
internal void ResetLocal()
{
pictureBoxLocal.Image = null;
if (localImg != null)
{
localImg.Dispose();
localImg = null;
}
}

Bitmap localImg;
public unsafe void OnRenderLocal(byte* yuv, uint w, uint h)
public void OnRenderLocal(IntPtr BGR24, uint w, uint h)
{
lock (pictureBoxLocal)
if (localImg == null)
{
if (0 == encoderLocal.EncodeI420toBGR24(yuv, w, h, ref bgrBufflocal, true))
lock (pictureBoxLocal)
{
if (remoteImg == null)
{
var bufHandle = GCHandle.Alloc(bgrBufflocal, GCHandleType.Pinned);
localImg = new Bitmap((int)w, (int)h, (int)w * 3, PixelFormat.Format24bppRgb, bufHandle.AddrOfPinnedObject());
}
localImg = new Bitmap((int)w, (int)h, (int)w * 3, PixelFormat.Format24bppRgb, BGR24);
}
}

Expand Down Expand Up @@ -186,7 +188,6 @@ private void checkBoxWebsocket_CheckedChanged(object sender, EventArgs e)
{
webSocketServer = new WebRTCServer((int)numericWebSocket.Value);
webSocketServer.Form = this;
unsafe
{
webSocketServer.OnRenderRemote = OnRenderRemote;
webSocketServer.OnRenderLocal = OnRenderLocal;
Expand All @@ -211,7 +212,6 @@ private void checkBoxWebsocket_CheckedChanged(object sender, EventArgs e)
public const int screenWidth = 640;
public const int screenHeight = 360;
public const int captureFps = 5;
public const bool barCodeScreen = false;

readonly byte[] imgBuf = new byte[screenWidth * 3 * screenHeight];
IntPtr imgBufPtr = IntPtr.Zero;
Expand All @@ -233,9 +233,10 @@ private void timerVirtualCam_Tick(object sender, EventArgs e)
img = new Bitmap(screenWidth, screenHeight, screenWidth * 3, PixelFormat.Format24bppRgb, imgBufPtr);
g = Graphics.FromImage(img);
}

{
// render
if (!checkBoxTest.Checked)
{
g.Clear(Color.DarkBlue);

Expand All @@ -252,19 +253,19 @@ private void timerVirtualCam_Tick(object sender, EventArgs e)

foreach (var s in webSocketServer.Streams)
{
s.Value.WebRtc.CaptureFrame();
unsafe
// if no editing is needed & size match
//s.Value.WebRtc.CaptureFrameAndPush();
//return;

//if (false)
{
var rgba = s.Value.WebRtc.DesktopCapturerRGBAbuffer();
if (rgba != null)
int x = -1, y = -1;
var rgbaPtr = s.Value.WebRtc.CaptureFrameBGRX(ref x, ref y);
if (rgbaPtr != IntPtr.Zero)
{
var rgbaPtr = new IntPtr(rgba);
Bitmap captureImg = null;

int x = -1, y = -1;
s.Value.WebRtc.DesktopCapturerSize(ref x, ref y);

if (x != captureWidth || y != captureHeight
if (x != captureWidth || y != captureHeight
|| !imgCapture.TryGetValue(rgbaPtr, out captureImg))
{
if (captureImg != null)
Expand All @@ -274,12 +275,8 @@ private void timerVirtualCam_Tick(object sender, EventArgs e)

captureWidth = x;
captureHeight = y;
}
}
g.DrawImage(captureImg, 0, 0, new Rectangle(Cursor.Position, new Size(screenWidth, screenHeight)), GraphicsUnit.Pixel);

// if no editing is needed
//var yuv = s.Value.WebRtc.VideoCapturerI420Buffer();
//encoderRemote.EncodeI420((byte*)rgba, screenWidth, screenHeight, (int)TJPF.TJPF_BGRX, 0, true, yuv);
}
}
break;
Expand All @@ -294,18 +291,7 @@ private void timerVirtualCam_Tick(object sender, EventArgs e)

foreach (var s in webSocketServer.Streams)
{
if (!barCodeScreen)
{
unsafe
{
var yuv = s.Value.WebRtc.VideoCapturerI420Buffer();
if (yuv != null)
{
encoderRemote.EncodeI420((byte*)imgBufPtr.ToPointer(), screenWidth, screenHeight, (int)TJPF.TJPF_BGR, 0, true, yuv);
}
}
}
s.Value.WebRtc.PushFrame();
s.Value.WebRtc.PushFrame(!checkBoxTest.Checked ? imgBufPtr : IntPtr.Zero);
}
}
}
Expand Down Expand Up @@ -335,7 +321,7 @@ private void checkBoxTurn_CheckedChanged(object sender, EventArgs e)

Task.Factory.StartNew(delegate ()
{
using (var mc = new ManagedConductor())
using (var mc = new WebRtcNative())
{
var ok = mc.RunTurnServer("0.0.0.0:3478", textBoxExtIP.Text, "test", "auth.txt");
if (!ok)
Expand All @@ -349,11 +335,11 @@ private void checkBoxTurn_CheckedChanged(object sender, EventArgs e)
{
using (turnCancel = new CancellationTokenSource())
{
var stop = turnCancel.Token;
var stop = turnCancel.Token;
while (!stop.IsCancellationRequested && mc.ProcessMessages(1000))
{
Debug.WriteLine(".");
}
}

Task.Factory.StartNew(delegate ()
{
Expand Down
Loading