Windows-universal-samples: CameraStarterKit crash when recording and minimizing the application (the sequel)

I’ve got a new variation of this issue from back in 2017. The repro is the same but the result is slightly different for me.

Repro:

  1. Build CameraStarterKit sample for x64 and run.
  2. Start a video recording.
  3. Minimize the app.

Result:

System.Runtime.InteropServices.COMException (0xC00D3E82): A media source cannot go from the stopped state to the paused state. A media source cannot go from the stopped state to the paused state.

Yes, the exception error message appears twice!?!

Exception call stack:

System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task task)
System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task task)
System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.GetResult()
CameraStarterKit.exe!CameraStarterKit.MainPage.StopPreviewAsync() Line 351
[Resuming Async Method]
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.RunWithDefaultContext()
System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
System.Private.CoreLib.ni.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
System.Private.CoreLib.ni.dll!System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.GetActionLogDelegate.AnonymousMethod__0()
System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.Invoke()
[Async Call Stack]
[Async] CameraStarterKit.exe!CameraStarterKit.MainPage.CleanupCameraAsync() Line 468
[Async] CameraStarterKit.exe!CameraStarterKit.MainPage.SetUpBasedOnStateAsync.AnonymousMethod__0() Line 526
[Async] CameraStarterKit.exe!CameraStarterKit.MainPage.SetUpBasedOnStateAsync() Line 533
[Async] CameraStarterKit.exe!CameraStarterKit.MainPage.Window_VisibilityChanged(object sender, Windows.UI.Core.VisibilityChangedEventArgs args) Line 131

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 21 (1 by maintainers)

Most upvoted comments

@LPBourret - Media Foundation API from C++ is working just fine for my application now. Our initial blocker was trying to use them from UWP C# apps. After multiple enterprise support calls and emails directly with Sr. Engineers from Microsoft’s platform team, their answer was pretty unanimous that Media Foundation simply is not meant to run as managed code - they didn’t go into too much detail on exactly why but it was more of a ‘trust us’ response.

Specifically, backgroundMediaRecording works if you start a recording then minimize, but if you are not actively recording (just running a preview stream for say frame inference) when you minimize then the UWP frameworks fail and kill the media foundation connection.

I wanted to follow up here after I had some discussions with Microsoft. tl;dr cameras via media foundation (what UWP is using under the hood) do NOT play nicely with managed code… YMMV but from the horse’s mouth we should NOT be trying to do long running media HW access with managed code.

I will say I am having some luck with Telerik’s web cam control (good old winforms for me right now). I can minimize, go to background etc and the preview and recordings seem to continue to work fine. Time will tell how stable their control is.

However the proper solution is to roll our sleeves up and implement this as a C++/Win32 native application to get the ideal HW level control over our cameras.

So personally for now I am going to get my winforms app shipped ASAP then immediately revisit it and implement a bare metal solution - should be fun!

@Zofware thanks for that - it showed me some new behavior. After adding that rescap, here is what I see:

  • Preview only – no recording – preview stream stops ~9-10 seconds after we minimize

  • Recording in progress – while minimized, everything continues to work properly – as soon as the recording stops, the preview stream stops

I have a ticket open with Microsoft (enterprise support) so if we get to the bottom of our issue I will try and reply here.

Hi Raymond,

I was able to fix this in my app by requesting the backgroundMediaRecording capability. It took 2 weeks to get approval but now I no longer have the issue and recordings continue even after my app is minimized. There may, in fact, be a problem with the platform lurking here but it might still be worth mentioning in the sample’s readme that a restricted capability is required to avoid bad behavior when minimizing the app.

Thanks, Adam

Hi, Adam. This is not an issue with the sample, but rather a question about the platform. You can open a support case with our Developer Support team who can further help with the specific problem you are experiencing. Please use these steps below for further help:

  1. Visit http://aka.ms/storesupport and sign-in with your Microsoft account
  2. Under the “App Development” section choose “Windows 10 universal app development”, then choose the appropriate problem type and category to get engaged through a formal support channel.

I’m in the same boat as @AdamSzofran - mine is a simple UWP desktop app. Experiencing media capture stoppage with extended execution bits. In my case, the rest of the app seems to work (i.e. UI updates, timers) but the media capture is killing the preview and/or recording stream(s).

This article (from a long time ago) mentions some creator’s update broke this functionality.

https://social.msdn.microsoft.com/Forums/en-US/0e2937a1-13c2-45fa-8008-36e7b227b124/uwp-auto-continue-recording-media-capture-after-sleep-mode?forum=wpdevelop

Please help!

What I’m really try to do is to get my UWP app to continue recording video from the camera even when the app is minimized. Since my app is used to record sporting events, it’s critical that the recording not be interrupted if the app is accidentally minimized.

I’ve experimented with ExtendedExecutionSession to no avail. That’s able to keep my app from being suspended but MediaCapture still sends me the CameraStreamStateChanged message saying the camera is Shutdown which kills the recording.

The sequence of events goes roughly like this:

  1. Call ExtendedExecutionSession.RequestExtensionAsync() result is Allowed.
  2. Start recording video.
  3. Minimize the app.
  4. Receive AudioStateMonitor.SoundLevelChanged notifying that the volume is now Muted.
  5. Receive Window.VisibilityChanged notifying that the window is no longer visible.
  6. Receive Application.EnteredBackground.
  7. Receive MediaCapture.CameraStreamStateChanged notifying that the camera is now Shutdown.

At this point the recording has already been terminated by Windows or MediaCapture.

Is there a way to keep the recording going without interruption?