smourier / VCamSample

Windows 11 Virtual Camera sample code.
MIT License
32 stars 7 forks source link
camera direct2d directx-11 dxgi media-foundation virtual-camera windows-11

VCamSample

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

There are two projects in the solution:

Note there's a VCamNetSample .NET C# port of this project available here : https://github.com/smourier/VCamNetSample

To test the virtual cam:

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

Screenshot 2024-01-22 131726

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-01-22 133220

Something like this in OBS (Video Capture Device):

image

Notes

Troubleshooting "Access Denied" on IMFVirtualCamera::Start method

If you get access denied here, it's probably the same issue as here 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 VCamSampleSource.dll from there, or copy/checkout the whole repo where everyone has access and build and register there.

Also, if you downloaded the binaries from the internet, not compiled them by yourself, make sure you must remove the Mark of the Web (https://en.wikipedia.org/wiki/Mark_of_the_Web) click on "Unblock" on the .zip file you downloaded and press OK:

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 VCamSample 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.

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