In some "dual camera open" device, App with CameraX can't open camera and show preview

524 views
Skip to first unread message
Assigned to trevor...@google.com by eri...@google.com

successfox tao

unread,
Dec 14, 2020, 2:59:28 AM12/14/20
to Android CameraX Discussion Group
hello CameraX team,
     In some education pad device, the devices generally have the special feature "dual camera open", it means the feature opened the front and back camera  at the same time in
background. I found that another camera app with CameraX can't open camera. And the preview is black.   
     I have gone through the CameraX codes, maybe the CameraX init two unavailable "CameraAvailability"s in Camera2CameraImpl.java. So the other app with camerax can not grab the camera and open it.

    if (!mCameraAvailability.isCameraAvailable() ||                      !mCameraStateRegistry.tryOpenCamera(this)) {
debugLog("No cameras available. Waiting for available camera before opening camera.");
setState(InternalState.PENDING_OPEN);
return;
} else {
setState(InternalState.OPENING);
}

     So could you help check this case for me? Thanks.
  
   

successfox tao

unread,
Dec 14, 2020, 4:54:05 AM12/14/20
to Android CameraX Discussion Group, successfox tao
The log  is :

12-14 17:51:51.864 28104 28199 D Camera2CameraImpl: {Camera@32aae26[id=0]} Resetting Capture Session
12-14 17:51:51.864 28104 28199 D Camera2CameraImpl: {Camera@32aae26[id=0]} Releasing session in state INITIALIZED
12-14 17:51:51.865 28104 28104 D PreviewView: Surface requested by Preview.
12-14 17:51:51.866 28104 28199 D Camera2CameraImpl: {Camera@32aae26[id=0]} No cameras available. Waiting for available camera before opening camera.
12-14 17:51:51.868 28104 28199 D Camera2CameraImpl: {Camera@32aae26[id=0]} Transitioning camera internal state: INITIALIZED --> PENDING_OPEN
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: Recalculating open cameras:
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: Camera                                       State                 
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: -------------------------------------------------------------------
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: Camera@7b6c603[id=1]                         UNKNOWN               
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: Camera@32aae26[id=0]                         PENDING_OPEN          
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: -------------------------------------------------------------------
12-14 17:51:51.871 28104 28199 D CameraStateRegistry: Open count: 0 (Max allowed: 1)

successfox tao 在 2020年12月14日 星期一下午3:59:28 [UTC+8] 的信中寫道:

Trevor McGuire

unread,
Dec 14, 2020, 7:54:26 PM12/14/20
to Android CameraX Discussion Group, succe...@gmail.com
Hi successfox,

Thanks for bringing up the issue. Maybe you can help me to understand your setup a little better.

Do you have 2 applications, each trying to open a different camera? From the log it seems the same camera is trying to be opened while it's being held by a different application or different API (such as camera2) within the same application. The camera should become available once the other application/API closes the camera and CameraX will open it automatically.

Just a note on opening cameras in background applications: since Android 10 (API 29), there is now a restriction that the camera cannot be held by an application in the background. If the application does not close the camera when it goes to the background, it will be closed automatically after 30 seconds. If an application needs to open the camera from a non-foreground application, it can be done from a foreground service. See this link for more info.

As for opening 2 cameras within the same application, CameraX currently limits an application to open only one camera at a time since most devices do not support opening multiple cameras at once. However, if this is something you need, we could take a feature request to allow explicitly disabling this restriction with the caveat that we can't guarantee it will work on every device.

Trevor

successfox tao

unread,
Dec 14, 2020, 10:32:37 PM12/14/20
to Android CameraX Discussion Group, trevor...@google.com, successfox tao
Hello Trevor, 
    Thanks for your reply. 
    Yes, In some special  devices, the OEM vendor had changed the android10's camera mechanism.
They run a special background application or service which hold one or two camera devices , in order to measure or detect the user's action/motion/distance.
And maybe their camera service would close the held camera when another app to opencamera by CameraManager.openCamera(). 
    But CameraX would not run "CameraManager.openCamera()" till all applicatoins held camera devices release the camera. So since Camera has Camera2InterOp
to monitor the CameraDevice's opending state, Do you have plan to change this cameraX's behavior? I mean CameraX directly openCamera,  not wait the  CameraAvailability.

br/chenggong

trevor...@google.com 在 2020年12月15日 星期二上午8:54:26 [UTC+8] 的信中寫道:

Trevor McGuire

unread,
Dec 16, 2020, 8:02:41 PM12/16/20
to successfox tao, Android CameraX Discussion Group
Thanks for the info. That clears things up.

The current behavior was intended to keep other apps from crashing when the camera is stolen from them, as many applications do not handle this case well. However, that approach might have been a bit too conservative as it could cause the foreground CameraX app to show a black screen for 30+ seconds (or indefinitely in your case). We'll look into changing this behavior. I've created https://issuetracker.google.com/175820568 to track this issue.

successfox tao

unread,
Dec 16, 2020, 9:18:09 PM12/16/20
to Android CameraX Discussion Group, trevor...@google.com, Android CameraX Discussion Group, successfox tao
hello Trevor,
    Thanks very much. Yes, i can understand cameraX current "CameraAvailiablity" mechanism, it's safe to all Applications.
I have a suggestion, maybe you can design a mechanism or method to "kick out" the low priority camera client, for example , the application in background or under other cases, incase the foreground App start to use CameraX or Camera2Api to open camera or wanting to  open camera.
   Anyway, thank you for your help in this case. i will track this case in  https://issuetracker.google.com/175820568 . 
br/Chenggong

trevor...@google.com 在 2020年12月17日 星期四上午9:02:41 [UTC+8] 的信中寫道:

Javier Gerardo Martinez Salomon

unread,
May 19, 2021, 3:20:01 PM5/19/21
to Android CameraX Discussion Group, succe...@gmail.com, trevor...@google.com, Android CameraX Discussion Group
Hi everyone,

I'm using 1.0.0-stable and I've got a use case where I would want to first verify the camera availability and its capabilities before actually running a full session using the camera and attaching use cases, I've got a custom selector that returns a camera and I use  bindToLifecycle(lifecycleOwner, cameraSelector) to get the camera instance and the info beforehand. However, this won't tell me if the selected camera it's in use by another app and will ultimately fall into PENDING_OPEN once I decide to attach use cases.

Is there a way to get this information beforehand and tell if the selected camera is really ready to use?

Best
Reply all
Reply to author
Forward
0 new messages