Home > Enterprise >  Camera2 background repeating capture session errors when screen is toggled from off to on
Camera2 background repeating capture session errors when screen is toggled from off to on

Time:10-19

I am using a background-cam example repo that runs a camera2 API repeating capture session in a background service. The app works as expected continuing to capture session when you close the app and shut the phone screen off, but when I turn my screen back on I receive the following error(s):

Session 0: Exception while stopping repeating: 
 android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): cancelRequest:534: Camera 1: Error clearing streaming request: Function not implemented (-38)
    at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1118)
    at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:99)
    at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1284)
    at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:592)
    at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:803)
    at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:251)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:233)
    at android.app.ActivityThread.main(ActivityThread.java:8068)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
 Caused by: android.os.ServiceSpecificException: cancelRequest:534: Camera 1: Error clearing streaming request: Function not implemented (-38) (code 10)
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2387)
    at android.os.Parcel.createException(Parcel.java:2357)
    at android.os.Parcel.readException(Parcel.java:2340)
    at android.os.Parcel.readException(Parcel.java:2282)
    at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:732)
    at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:97)
    at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1284) 
    at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:592) 
    at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:803) 
    at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:251) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:233) 
    at android.app.ActivityThread.main(ActivityThread.java:8068) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978) 

Something, possibly at the OS level, is triggering a cancelRequest. I would like to prevent this. I tried using a wake lock in onCreate() of the CamService service and adding the WAKE_LOCK permission to my AndroidManifest.xml that has not made a difference.

AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />

CamService.kt:

  override fun onCreate() {
    super.onCreate()

    val wakeLock: PowerManager.WakeLock =
        (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
            newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CamService::MyWakelockTag").apply {
                acquire()
            }
        }
    ...

I would like my capture session to continue when the screen is turned back on if that is possible.

I have logged all the lifecycle events for MainActivity and CamService and none of them are triggered directly before/after this error.

I have also removed Battery Optimization for this app on the phone.

Device: OnePlus 6 (A6003)

Android Version: 11

SDK API: 30

OxygenOS Version: 11.2.2

UPDATED SOLUTION: It was FACE UNLOCK! When I turned the screen back on the OS wanted to use the camera for face unlock and would override my app's control!

CodePudding user response:

Are you creating a foreground service that you're running the camera session within?

In the few last major Android releases, using the camera when you're not in the foreground isn't supported, and you get disconnected shortly after going to the background.

It looks like here, you're getting a disconnect callback from the camera service, which leads to the camera shutdown that then produces that error. That seems most likely to be because of the background access enforcement, but it's possible there's a weird interaction in your app.

CodePudding user response:

It was FACE UNLOCK! When I turned the screen back on the OS wanted to use the camera for face unlock and would override my app's control!

  • Related