Bug 55375 - "Audio CPU Activity" remains 10% after using AVCaptureSession
Summary: "Audio CPU Activity" remains 10% after using AVCaptureSession
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-04-19 14:06 UTC by Kristof Verbiest
Modified: 2017-04-20 17:53 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Kristof Verbiest 2017-04-19 14:06:40 UTC
I display a live camera preview in my Xamarin.iOS app using the AVFoundation infrastructure. 
Using XCode Instruments, I see that my app uses about 10% of the 'Audio CPU Actvity' once the preview is displayed. Even if the preview is closed again, the app keeps on using the 'Audio CPU Activity'.

My app uses the camera only for short periods. However, because this activity remains high, I believe my app is draining the iPhone's battery (which is a complaint that I get from the users and which is the reason why I started investigating this).

To reproduce this:
1) Create an Xamarin.iOS app that displays a live camera preview.
Example 1 (Xamarin.iOS): https://github.com/pierceboggan/xamarin-blog-samples/tree/master/ios/avfoundation-camera/4.%20Completed%20Camera%20Control
Example 2 (Xamarin.Forms): https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/ContentPage

In both samples, you need to add this in info.plist (otherwise they will crash):

<key>NSCameraUsageDescription</key>
<string>Some description...</string>

2) Deploy the app on an iPhone

3) Start XCode Instruments, select 'Energy Usage Log' and profile the app

4) You will see that, from the moment that the camera is opened, the app uses ~10% 'Audio CPU Activity'. If the camera closes, this remains the case.
Comment 1 Kristof Verbiest 2017-04-20 08:08:52 UTC
After some further investigation, the issues seems to be caused by a bug in both sample projects that I've linked to.

It can be solved by adding this line in the Dispose() method of the view:

_captureSession.RemoveInput(_captureDeviceInput);

So no bug in Xamarin, but maybe the sample code should be updated?
Comment 2 Vincent Dondain [MSFT] 2017-04-20 17:47:58 UTC
Hi Kristof,

I was actually commenting the same thing and got a mid air collision because of your last comment.

You/we could also call AVCaptureSession's `StopRunning()`.

I've seen people complain (against Apple) about the fact AVCaptureSession always record audio several times because they don't always care about the audio, but as far as I know there's no way around that.

Also example 1 has the camera always on so it's normal that Audio CPU Activity doesn't go back to 0.

Note: Audio CPU Activity stops when you quit the app.

That said I agree our samples are incorrect and I'll make sure we fix them.

-----

Now I want to emphasize that Xamarin.iOS is a thin layer on top of the native iOS frameworks and what you experienced here was a native iOS behavior and was due to how the APIs were used.

The broader developer community would have been the best and quickest place for troubleshooting help on this issue.
Posting a question on Stack Overflow [0] or the Xamarin Forums [1] would have provided this help.

[0] http://stackoverflow.com/questions/tagged/xamarin
[1] https://forums.xamarin.com/

In case it might also be a helpful reference, you can find some general recommendations on the best places to ask various kinds of questions about Xamarin on:

https://developer.xamarin.com/guides/cross-platform/troubleshooting/support-options/
Comment 3 Vincent Dondain [MSFT] 2017-04-20 17:53:28 UTC
https://bugzilla.xamarin.com/show_bug.cgi?id=55434