.net5 C# MediaCapture Problems

jona varque 1 Reputation point
2021-05-13T18:37:13.717+00:00

I've been trying to get a MediaCapture class to work for previewing and grabbing frames from a camera.
I have 2 problems.

  1. If I set MemoryPreference = MediaCaptureMemoryPreference.Cpu, I can see frames but it stops after 19 frames, with no errors. The FrameArrived event just stops triggering. UNLESS I have opened Camera on Win10. Then, it works.
  2. I then set MemoryPreference = MediaCaptureMemoryPreference.Auto and in FrameArrived, I convert to a SoftwareBitmap like this:

softwareBitmap =
await SoftwareBitmap.CreateCopyFromSurfaceAsync(mediaFrameReference.VideoMediaFrame
.Direct3DSurface);

That works on all machines BUT, I get a massive memory leak which kills the app in short order.
I have tried Dispose on everything in the FrameArrived handler with no success.

The other problem I have is that I can not call
await TheCaptureMgr.VideoDeviceController.
SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, <some property>);

It causes an exception saying "The File was in use by another process". But there are no other processes using a camera.

This is a .net5 19041 targeted project C# WPF. I can not use a Xaml CaptureElement in this code. I guess it isn't supported in .Net5

If I could just solve the memory leak I would be happy to continue like that, with Auto as the mem preference.
If I could just get the Cpu mode to work for more than 19 frames, I would be happy to go with that.

ANY hints would be greatly appreciated

UPDATE:
This problem has been bothering me for about ten days. I have gone over my MediaCapture initialization, The FrameArrived Event handler and everything I could. After scouring this forum, I decided to try setting :
from
TheFrameReader = await TheCaptureMgr.CreateFrameReaderAsync(video,
MediaEncodingSubtypes.Yuy2);

to
TheFrameReader = await TheCaptureMgr.CreateFrameReaderAsync(video,
MediaEncodingSubtypes.Nv12);

And BOOM. Previewing a record stream from the camera now works. It does not stop at 19 frames as it did when set to Yuy2.
So, even though Auto mode worked with Direct3DSurface but leaked like a monster with a bladder infection, I can just use Cpu mode as ALL of the examples I have seen do. !

If anyone knows why, please post here. Before I release this for public consumption I want to make sure I've got this setup properly. That was weird!

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
786 questions
{count} votes

1 answer

Sort by: Most helpful
  1. jona varque 1 Reputation point
    2021-05-14T13:40:42.2+00:00

    Yes it is using MediaCapture. I am using .net5 with win 10 19041 in C#/WPF. The git repo is private and confidential but I can try to setup a test project that you can see. Otherwise, I can post snippets here if it is ok..

    And I have to retract the success story as it still stops at 19 frames on some machines. Depending on whether it is set to Yuy2 or NV12. One machine works with NV12 and the other works with Yuy2. With the same cameras attached.

    Let me see what I can do.

    0 comments No comments