Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopping AVCaptureSession iPhone takes 8 seconds

I create a new session, and start it on the iPhone like this:

AVCaptureSession* session;
...
[session startRunning];

This works fine. However, stopping the session later:

[session stopRunning];

Takes about 8 seconds usually! Does anyone know why this is possible and what could be done to make it faster?

like image 728
Jan Rüegg Avatar asked Jul 03 '13 07:07

Jan Rüegg


2 Answers

From the Apple documents:

Discussion

This method is used to stop the flow of data from the inputs to the outputs connected to the AVCaptureSession instance that is the receiver. This method is synchronous and blocks until the receiver has completely stopped running.

Because it's synchronous, you can put it into an async call to avoid freezing UI thread.


Adding more comments:

The idea above didn't make stopRunning faster. It just stop freezing UI thread.

like image 73
nickcheng Avatar answered Nov 08 '22 04:11

nickcheng


I ran into a similar problem. Calling stopRunning would freeze the app for 8 to 10 seconds.

I eventually tracked down the issue to this: I was calling stopRunning on a thread other than the main thread. This secondary thread was used for all transactions to the AVCaptureSession. The problem occurred because after dispatching the call to stopRunning, I blocked the main thread waiting for it to complete. Unfortunately, stopRunning posts something to the main thread and blocks waiting for that to complete. The thing stopRunning was waiting on eventually timed out and reported an error in the - (void)onRuntimeError:(NSNotification*)n callback: Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo=0x19e43c90 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}

The solution in my case was to simply not block the main thread after calling stopRunning. Fortunately for me that was easy to do (and something Apple more or less recommends anyway).

I've noticed variations on this theme in other questions, and the solution has always been to rework the code. Hopefully this will provide a better understanding of why the problem is occurring.

like image 38
Chris Cooksey Avatar answered Nov 08 '22 03:11

Chris Cooksey