Quantcast
Channel: Recent Discussions — Mixed Reality Developer Forum
Viewing all articles
Browse latest Browse all 10543

MediaCapture/FrameReader only reads 10 frames on first run

$
0
0

I have a problem where the MediaCapture and FrameReader don't record more than 10 frames on the first run. I'm doing some image processing in a second thread and for some reason it just stops. I've checked the FrameArrived event that it is only being fired 10 times. This is a project built with Unity with an image recording/processing script written with the Windows API. On the first run it stops, but if I stop the recording and restart it everything works perfectly.

So first, I'm initializing the camera like this:

private async Task InitializeCameraAsync()
    {
        if (mediaCapture != null) return;
        var allGroups = await MediaFrameSourceGroup.FindAllAsync();

        if (allGroups.Count <= 0) return;

        mediaCapture = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings
        {
            SourceGroup = allGroups[0],
            SharingMode = MediaCaptureSharingMode.SharedReadOnly,
            StreamingCaptureMode = StreamingCaptureMode.Video,
            MemoryPreference = MediaCaptureMemoryPreference.Cpu
        };

        await mediaCapture.InitializeAsync(settings);

        try
        {
            var mediaFrameSourceVideoPreview = mediaCapture.FrameSources.Values.Single(x => x.Info.MediaStreamType == MediaStreamType.VideoPreview);
            var minFormat = mediaFrameSourceVideoPreview.SupportedFormats.OrderBy(x => x.VideoFormat.Width * x.VideoFormat.Height).FirstOrDefault();
            await mediaFrameSourceVideoPreview.SetFormatAsync(minFormat);
            frameReader = await mediaCapture.CreateFrameReaderAsync(mediaFrameSourceVideoPreview, minFormat.Subtype);
            frameReader.FrameArrived += FrameReader_FrameArrived;
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: " + e.Message);
            return;
        }

        await frameReader.StartAsync();
    }

And the image processing runs in an IAsyncAction, where recording is set to true in another function before calling this one and it is set to false from the main thread with a voice command.

public void Record()
    {
        IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
            async (workItem) =>
            {
                while (true)
                {
                    if (!recording)
                    {
                        await CleanupCameraAsync();
                        return;
                    }
                    else
                    {
                        var frame = frameReader.TryAcquireLatestFrame();
                        if (frame == null) continue;

                        var softwareBitmap = SoftwareBitmap.Convert(frame.VideoMediaFrame.SoftwareBitmap, BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore);
                        Process(softwareBitmap);
                        frame.Dispose();
                        await Task.Delay((int)(1.0f / fps) * 1000);
                    }
                }
            });
    }

CleaupCameraAsync() looks like this:

    private async Task CleanupCameraAsync()
    {
        if (frameReader != null)
        {
            await frameReader.StopAsync();
            frameReader.Dispose();
        }
        if (mediaCapture != null)
        {
            mediaCapture.Dispose();
            mediaCapture = null;
        }
    }

For some reason this isn't working and I'm not sure why. I read somewhere that it might have to do with frames not being disposed, but I am disposing the frames I get and everything works after running it once.

So any ideas what the problem might be?


Viewing all articles
Browse latest Browse all 10543

Trending Articles