opentok / opentok-android-sdk-samples

Sample applications illustrating best practices using OpenTok Android SDK.
https://tokbox.com/developer/sdks/android/
MIT License
211 stars 169 forks source link

setTorchMode error: camera is in use after cycling camera #488

Closed milevy1 closed 10 months ago

milevy1 commented 11 months ago

Describe the bug My team is attempting to migrate our application from the deprecated Camera API to Camera2. However we are encountering an issue with toggling the flash while on a video call.

Toggling flash (torch) is throwing an error that the camera is in use, but only after after cycling the camera, every other cycle.

I found these other two old issues that seem similar, but they were closed without a resolution and they don't mention the cycleCamera causing issues. https://github.com/opentok/opentok-android-sdk-samples/issues/110 https://github.com/opentok/opentok-android-sdk-samples/issues/282

To Reproduce

  1. Start a call publishing the front facing camera. (Our Publisher is not using a custom videoCapturer)
  2. Cycle to the back camera. For this, we are using the [OpenTok Publisher method cycleCamera](https://tokbox.com/developer/sdks/android/reference/com/opentok/android/Publisher.html#cycleCamera()) (flash will work here). Below is a snippet using the CameraManager setTorchMode:
    private CameraManager cameraManager;
    private boolean isFlashOn = false;

    public boolean toggleFlashlight() {
        try {
            for (String id: cameraManager.getCameraIdList()) {
                if (cameraManager.getCameraCharacteristics(id)
                        .get(CameraCharacteristics.LENS_FACING) == LENS_FACING_BACK) {
                    isFlashOn = !isFlashOn;

                    cameraManager.setTorchMode(id, isFlashOn);
                    break;
                }
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        return isFlashOn;
    }
  1. Cycle to the front camera
  2. Cycle to the back camera
  3. Try to toggle the flash on again. It now throws this error:
E/CameraService: setTorchMode: torch mode of camera 0 is not available because camera is in use
W/System.err: android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): setTorchMode:2501: Torch for camera "0" is not available due to an existing camera user
W/System.err:     at android.hardware.camera2.CameraManager$CameraManagerGlobal.setTorchMode(CameraManager.java:2175)
W/System.err:     at android.hardware.camera2.CameraManager.setTorchMode(CameraManager.java:1128)
W/System.err: Caused by: android.os.ServiceSpecificException: setTorchMode:2501: Torch for camera "0" is not available due to an existing camera user (code 7)

Expected behavior I expect to be able to toggle the flash on/off. It works for every other cycleCamera just fine. However, every other cycleCamera, toggling the flash does not work

Screenshots n/a

Device (please compete the following information):

Additional context n/a

goncalocostamendes commented 11 months ago

@milevy1 I was able to replicate the issue. It seems that our Custom Capturer Sample was not updated with out latest improvements to our default video capturer, where we are now properly releasing the capturer on switching cameras and onPause() and onDestroy(). I will work on updating the sample and will let you know when it is ready to use.

milevy1 commented 11 months ago

@goncalocostamendes, just to clarify, we are not using the Custom Capturer Sample. Our application is using a Publisher without a custom capturer so we are using the BaseVideoCapturer, for example:

new Publisher.Builder(context).build();

Do we need to add a custom capturer to fix this or will the BaseVideoCapturer include this fix in an updated release?

v-kpheng commented 10 months ago

@milevy1, I'm sorry, but please go through Support. I'll close this in the meantime.