Camera.release() takes 30 seconds to release the camera in Nexus 10. Is there any way to speed up the process?
Asked Answered
C

3

12

I'm using the following code to release camera in onPause. But the line mCamera.release() takes 30 seconds on average to release the camera in Nexus 10 device. I've added logging before and after mCamera.release() and found that the time difference between printing these logs is 30 seconds.

private void releaseCamera() {
    if (mCamera != null) {
        previewing = false;
        mCamera.setPreviewCallback(null);
        if(mPreview != null)
        mPreview.getHolder().removeCallback(mPreview);
        Log.e("QR","Starting to call mCamera.release()");
        mCamera.release();
        Log.e("QR","Released Camera");
        mCamera = null;
    }
}

I'm calling mCamera.stopPreview() before calling releaseCamera()

Is there any way by which I can do it asynchrounously? Because it takes just less than a minute to go from the Camerapreview activity to the next activity.

Edit1: We reduced the preview size from the highest(1080x1920) to a middle range (480x800) and everything started working fine. Is the preview size has anything to do with the camera release in HAL?

Crosson answered 24/1, 2014 at 4:19 Comment(11)
I'm creating many threads in my app. Is it causing the problem? If so, how can I check that without rooting the device?Crosson
Reducing the preview size from (1080,1920) to some lower size like (480,800) has some significant changes in the performance. Is the Camera.release() has any relationship with the preview size? And if there is a delay in releasing camera, the time taken is almost a constant - either 320ms or 30 secs for Nexus 10 - What is the reason behind it?Crosson
Are you calling mCamera.stopPreview()?Infecund
@Infecund Yes, I'm calling mCamera.stopPreview() before calling releaseCamera().Crosson
Do you see similar issues in other apps? Grafika (github.com/google/grafika) has a couple of camera-based activities, e.g. "show + capture camera", and I run it on the Nexus 10 regularly. Are you calling this from onPause()? Is there anything that looks camera-related in logcat during this time?Infecund
@Infecund : All other camera apps are running fine. In my app, when I don't create the threads, the camera activity is working fine. Is the number of threads have anything to do with releasing the camera?Crosson
I can't think of a reason why having lots of threads would stall things, unless there were so many of them and they were so busy that they totally starved your thread out for 30 seconds. Seems unlikely. Looking at a stack trace might help.Infecund
Even I thought having so many threads was the problem. What I don't understand is lowering the preview size prevents the stall with the same no of threads. What is the relationship among camera.release(), previewSize and threads. @Infecund I'll upload the stack trace as soon as possible.Crosson
If you look at the code for the Camera2 API (github.com/android/platform_hardware_libhardware/blob/master/…), line 284, you get the stack trace errors that you are seeing in your app. It seems like the HAL is waiting for all operations on the camera to be finished before closing it, and that something is blocking your release. At the timeout of release (30 seconds I guess), the HAL disconnects. There is an interesting line in this code: // TODO: Set up notifications from HAL, instead of sleeping here. This could be an unimplemented on the nexus 10...Laughter
Are you saving the image or something before releasing the camera. iF so, move the saving part into a background thread. I/O operations are time consuming and may block the main thread.Giovannigip
In our example we do not save anything.Genvieve
H
5

You can try to release the camera inside a Thread as a workaround for this, though this is not an ideal solution. You can launch your next activity while the release function executes in background

   new AsyncTask() {

        @Override
        protected Object doInBackground(Object... params) {
            releaseCamera();
            return null;
        };
    }.execute();
Harebrained answered 18/11, 2014 at 18:17 Comment(1)
We use that workaround. However it is not ideal as if camera needs to be open again, the user will not be able to do so.Genvieve
E
1

Dilip, it is know issue in the Nexus 10, Check this Nexus 10 camera.release hangs for 30 seconds.

We tried with these things,

camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;

It is working for me, but i have to test this same code on other devices too (it is good put the above code in try/catch statements ).

Also, you can add this functionality in thread:

new Thread(new Runnable(){
    public void run(){
        camera.stopPreview();
    camera.setPreviewCallback(null);
    camera.unlock();
    camera.release();
    camera = null;
    }
}).start();

Try to create camera management code with Camera2 API, hopefully this will not cause problem, check this http://blog.csdn.net/torvalbill/article/details/40376145

Execute answered 21/11, 2014 at 3:30 Comment(2)
Suggestion does not work here on Nexus 10. We are looking for a solution or a workaround that would work.Genvieve
@Genvieve We identified these errors are happening in nexus if you did not open and close properly, after a while it is becoming a problem. That is the reason, I provided the script we used for closing the camera properly and also, work around, we are implementing this week Camera 2 API. Probably, by this week end I can comment some thing on that.Execute
A
0

I don't have needed level of reputation to add a comment so I'll put it here:

new Thread(new Runnable(){
public void run(){
    camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;
}

}).start();

When the release call will be run in separate thread it will cause a problem to other application that uses camera and will be launched on top of that. I'm also looking for the solution. I don't have Nexus10. We have own device.

Asyllabic answered 5/2, 2016 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.