smourier / VCamNetSample

Windows 11 Virtual Camera sample .NET code.
MIT License
10 stars 2 forks source link
dotnet dotnet-core media-foundation virtual-webcam webcam wic

VCamNetSample

This solution contains a Media Foundation Virtual Camera Sample developed using .NET. It works only on Windows 11 thanks to the MFCreateVirtualCamera API.

It's a port of the same project written in C++ VCamSample: https://github.com/smourier/VCamSample and it's based on DirectN for all DirectX, WIC and Media Foundation interop.

There are two projects in the solution:

To test the .NET virtual cam:

You should now see something like this in the Windows Camera App

Screenshot 2024-03-22 174749

Something like this in Windows' Edge Web Browser, using this testing page: https://googlechrome.github.io/samples/image-capture/grab-frame-take-photo.html

Screenshot 2024-03-22 181438

Something like this in OBS (Video Capture Device):

Screenshot 2024-03-22 181650

Something like this in Teams, yes, this is your avatar :-)

Screenshot 2024-03-22 181914

Notes

Troubleshooting "Access Denied" on IMFVirtualCamera::Start method

If you get access denied here, it's probably the same issue as for the native version https://github.com/smourier/VCamSample/issues/1

Here is a summary:

=> So the solution is just to either copy the output directory once built (or downloaded) somewhere where everyone has access and register VCamNetSampleSource.comhost.dll from there, or copy/checkout the whole repo where everyone has access and build and register there.

image

Tracing

The code output lots of interesting traces. It's quite important in this virtual camera environment because there's not just your process that's involved but at least 4: the VCamNetSample app, the Windows Frame Server, the Windows camera monitor, and the reader app (camera, etc.). They all load the media source COM object in-process.

The solution also contains a small DebuggerAttach project that can be ran to attach to the Windows Frame Server (hosted in svchost.exe). For it to run you must run Visual Studio and DebuggerAttach as administrator. You can also attach to the Frame Server using Visual Studio attach to process once it's been started.

Tracing here doesn't use OutputDebugString because it's 100% old, crappy, truncating text, slow, etc. Instead it uses Event Tracing for Windows ("ETW") in "string-only" mode (the mode where it's very simple and you don't have to register painfull traces records and use complex readers...).

So to read these ETW traces, use WpfTraceSpy you can download here https://github.com/smourier/TraceSpy. Configure an ETW Provider with the GUID set to 964d4572-adb9-4f3a-8170-fcbecec27467