StreamPack icon indicating copy to clipboard operation
StreamPack copied to clipboard

[Feat]: Change the brightness of the flashlight ?

Open cryinrain69 opened this issue 10 months ago • 9 comments

Version

2.6.1

Environment that reproduces the issue

Samsung Galaxy S22 Ultra - Android 14

Use case description

Hi. I can enable/disable the flash (when using the rear camera) by calling:

cameraSrtLiveStreamer.settings.camera.flash.enable = true/false

Is there any way I can change that brightness level ? I really need it.

Proposed solution

No response

Alternative solutions

No response

cryinrain69 avatar Feb 07 '25 07:02 cryinrain69

Hi,

Setting the torch strength is a relatively new features on Android devices. The API I found are:

  • https://developer.android.com/reference/android/hardware/camera2/CaptureRequest#FLASH_STRENGTH_LEVEL API >= 35
  • https://developer.android.com/reference/android/hardware/camera2/CameraManager#turnOnTorchWithStrengthLevel(java.lang.String,%20int) API >= 33

You can use turnOnTorchWithStrengthLevel in your code.

ThibaultBee avatar Feb 07 '25 10:02 ThibaultBee

Hi,

Setting the torch strength is a relatively new features on Android devices. The API I found are:

  • https://developer.android.com/reference/android/hardware/camera2/CaptureRequest#FLASH_STRENGTH_LEVEL API >= 35
  • https://developer.android.com/reference/android/hardware/camera2/CameraManager#turnOnTorchWithStrengthLevel(java.lang.String,%20int) API >= 33

You can use turnOnTorchWithStrengthLevel in your code.

Hi. turnOnTorchWithStrengthLevel is exactly what I need. But when using it with **io.github.thibaultbee.streampack.views.PreviewView**, I can only turn on the flash with the desired brightness level with the front camera (even though flash.available = false ). When using the rear camera, it gives the error: android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): turnOnTorchWithStrengthLevel:2928: Torch for camera "0" is not available due to an existing camera user

It seems that because PreviewView is using the rear camera, I cannot use that function from outside. My wish is to be able to adjust the brightness level with both the front and rear cameras using PreviewView.

Can you integrate brightness adjustment feature into your library? I use it with API >= 33

Here is the code I use to adjust the brightness of the flash (works fine without using PreviewView):

        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || torchStrength < 1) {
                return
            }
            val camManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
            val cameraList = camManager.cameraIdList
            if (cameraList.isEmpty()) return
            val cameraId = cameraList[0]
            val cameraCharacteristics = camManager.getCameraCharacteristics(cameraId)
            val maxLevel =
                cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL)
            Loggers.e("maxLevel: $maxLevel")
            if (maxLevel == null || maxLevel <= 1) {
                Loggers.e("changeBrightnessFlashLight", "This camera not support change brightness")
                return
            }
            var level = torchStrength
            if (level > maxLevel) {
                level = maxLevel
            }
            camManager.turnOnTorchWithStrengthLevel(cameraId, level)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

cryinrain69 avatar Feb 10 '25 05:02 cryinrain69

Indeed, if you call turnOnTorchWithStrengthLevel after the camera has been started, it throws an exception. Could you call turnOnTorchWithStrengthLevel before the camera is used by StreamPack?

ThibaultBee avatar Feb 10 '25 09:02 ThibaultBee

Indeed, if you call turnOnTorchWithStrengthLevel after the camera has been started, it throws an exception. Could you call turnOnTorchWithStrengthLevel before the camera is used by StreamPack?

Unfortunately I can't. Because I need to turn on the flash at the lowest brightness when the livestream is already running. And I need to turn it on whenever there is an event request from my server (via socket). If I'm using the front camera, I can do it perfectly. But the rear camera throws that exception.

cryinrain69 avatar Feb 10 '25 09:02 cryinrain69

My guess is that turnOnTorchWithStrengthLevel is incompatible with the usage of a camera and there is nothing we can do.

I could add an API for FLASH_STRENGTH_LEVEL in the CameraSettings but as it is API >= 35, it does not match your requirement.

ThibaultBee avatar Feb 10 '25 10:02 ThibaultBee

My guess is that turnOnTorchWithStrengthLevel is incompatible with the usage of a camera and there is nothing we can do.

I could add an API for FLASH_STRENGTH_LEVEL in the CameraSettings but as it is API >= 35, it does not match your requirement.

Adding FLASH_STRENGTH_LEVEL with API >= 35 is also a good fallback option for later. I'm really stuck and can't find a way to do it with API 33 :cry:

cryinrain69 avatar Feb 10 '25 10:02 cryinrain69

If you find another option, I could give a look.

ThibaultBee avatar Feb 10 '25 12:02 ThibaultBee

FLASH_STRENGTH_LEVEL will be implement. Unfortunately my phone will not receive the update to API 35.

It is on hold till I get a new phone.

ThibaultBee avatar Feb 11 '25 14:02 ThibaultBee

If you find another option, I could give a look.

Thank you very much. I will try to find a solution. If there is any new information, please comment to let me know.

cryinrain69 avatar Feb 12 '25 03:02 cryinrain69