I've been trying to get a MediaCapture class to work for previewing and grabbing frames from a camera.
I have 2 problems.
- 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.
- 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!