question

jonavarque-7291 avatar image
0 Votes"
jonavarque-7291 asked jonavarque-7291 answered

.net5 C# MediaCapture Problems

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!

dotnet-wpf-xaml
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@jonavarque-7291
Did your MediaCapture refer to UWP MediaCapture? Could you provide a sample using OneDriver or Github which can reproduce the error for us to test and analyze?


0 Votes 0 ·

1 Answer

jonavarque-7291 avatar image
0 Votes"
jonavarque-7291 answered jonavarque-7291 edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.