1

The great developers. I am using a webRTC library of io.pristine.libjingle:11139 for video calling. All is going good, now I just want to turn FLASHLIGHT on, I researched almost all the questions related to the FLASHLIGHT, where I found that the FLASHLIGHT is a feature of camera, so to turn on the FLASHLIGHT one have to go with CAMERA object. Now I stuck here because I am using the library, it does not allow me to access the camera object already opened. So how to turn on flashlight on without using camera, because camera is already used by the webrtc library ? Is there any any other latest library that allow to access the camera object of libjingle for webrtc for android ?

I need help it's a really like challenge.

regards, Dharma

Dharma Cool
  • 197
  • 1
  • 2
  • 11

2 Answers2

3

I was facing this issue today and haven't found a solution anywhere so I wanted to share my solution, although it's quite late after the question was asked. There are basically two options: modify source & compile webrtc library yourself or an easier solution - override the functionality of the library a bit. I have to say that I'm using latest prebuilt library straight from Google's repository, so my libjingle library might be a bit different.

implementation 'org.webrtc:google-webrtc:1.0.28262'

Now, to the code itself. Create a package org.webrtc to be able to access package-private classes and interfaces you need to implement or modify.

First of these is interface CameraSession. Instance of this interface handles access to android's Camera. So I created class FlaslightCameraSession implements CameraSession by copy-pasting code from class Camera1Session and adding a function to turn on/off the flashlight as follows.

    void setFlashlightActive(boolean isActive) {
        Camera.Parameters params = camera.getParameters();

        if (isActive) {
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        } else {
            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        }

        camera.setParameters(params);
    }

Next step is to modify VideoCapturer used to get VideoFrames of the camera. To do this I simply made a class FlashlightCameraCapturer by extending webrtc's class Camera1Capturer and added simple modification to control the flashlight.

    @Override
    protected void createCameraSession(CameraSession.CreateSessionCallback createSessionCallback, CameraSession.Events events, Context applicationContext, SurfaceTextureHelper surfaceTextureHelper, String cameraName, int width, int height, int framerate) {
        CameraSession.CreateSessionCallback myCallback = new CameraSession.CreateSessionCallback() {
            @Override
            public void onDone(CameraSession cameraSession) {
                FlashlightCameraCapturer.this.cameraSession = (FlashlightCameraSession) cameraSession;
                createSessionCallback.onDone(cameraSession);
            }

            @Override
            public void onFailure(CameraSession.FailureType failureType, String s) {
                createSessionCallback.onFailure(failureType, s);
            }
        };

        FlashlightCameraSession.create(myCallback, events, captureToTexture, applicationContext, surfaceTextureHelper, Camera1Enumerator.getCameraIndex(cameraName), width, height, framerate);
    }

    public void turnOnFlashlight() {
        cameraSession.setFlashlightActive(true);
    }

    public void turnOffFlashlight() {
        cameraSession.setFlashlightActive(false);
    }

Final step is to modify CameraEnumerator. Specifically, you need to override createCapturer function to create instance of our modified capturer. So I extended class Camera1Enumerator to override this function as follows.

    @Override
    public CameraVideoCapturer createCapturer(String deviceName, CameraVideoCapturer.CameraEventsHandler eventsHandler) {
        return new FlashlightCameraCapturer(deviceName, eventsHandler, true);
    }

Now you can simply use your new modified camera enumerator to get instance of camera capturer that can control the flashlight.

Hope this helps :)

Dzejrin
  • 31
  • 4
  • Could you please share full source code or any links, i have followed all those steps. But unable to turn on flash light. – Android Oct 25 '19 at 05:57
0

To compromise @Dzerjrin's answer I finally implemented Flashlight functionality into working WebRTC based Android App.

First of all, I was using Gradle dependency of WebRTC library;

implementation 'org.webrtc:google-webrtc:1.0.32006'

As suggested, in order to reach package-private classes we need to create a package org.webrtc

Then we need to create three classes FlashlightCameraCapturer, FlashlightCameraNumerator and FlaslightCameraSession.

The main trick for the feature is, using created CameraNumerator in your existing VideoChat class like below; Then you could use the flashlight. In the scenario below, FlashlightCameraNumerator was used.

 private fun createCameraCapturer(enumerator: FlashlightCameraNumerator): FlashlightCameraCapturer? {
        val deviceNames = enumerator.deviceNames
        if (expertOfTheCall) {
            // First, try to find front facing camera
            Logging.d(RtcAppConstant.TAG, "Looking for front facing cameras.")
            for (i in deviceNames.indices) {
                val deviceName = deviceNames[i]
                if (enumerator.isFrontFacing(deviceName)) {
                    Logging.d(RtcAppConstant.TAG, "Creating front facing camera capturer.")
                    val videoCapturer: FlashlightCameraCapturer? = enumerator.createCapturer(deviceName, null) as FlashlightCameraCapturer?
                    cameraDeviceId = i
                    cameraDeviceName = deviceName
                    if (videoCapturer != null) {
                        return videoCapturer
                    }
                }
            }

            // Front facing camera not found, try something else
            Logging.d(RtcAppConstant.TAG, "Looking for other cameras.")
            for (i in deviceNames.indices) {
                val deviceName = deviceNames[i]
                if (!enumerator.isFrontFacing(deviceName)) {
                    Logging.d(RtcAppConstant.TAG, "Creating other camera capturer.")
                    val videoCapturer: FlashlightCameraCapturer? = enumerator.createCapturer(deviceName, null) as FlashlightCameraCapturer?
                    cameraDeviceId = i
                    cameraDeviceName = deviceName
                    if (videoCapturer != null) {
                        return videoCapturer
                    }
                }
            }
        } else {
            // First, try to find back camera
            Logging.d(RtcAppConstant.TAG, "Looking for other cameras.")
            for (i in deviceNames.indices) {
                val deviceName = deviceNames[i]
                if (!enumerator.isFrontFacing(deviceName)) {
                    Logging.d(RtcAppConstant.TAG, "Creating other camera capturer.")
                    val videoCapturer: FlashlightCameraCapturer? = enumerator.createCapturer(deviceName, null) as FlashlightCameraCapturer?
                    cameraDeviceId = i
                    cameraDeviceName = deviceName
                    if (videoCapturer != null) {
                        return videoCapturer
                    }
                }
            }

            // Back camera not found, try something else
            Logging.d(RtcAppConstant.TAG, "Looking for front facing cameras.")
            for (i in deviceNames.indices) {
                val deviceName = deviceNames[i]
                if (enumerator.isFrontFacing(deviceName)) {
                    Logging.d(RtcAppConstant.TAG, "Creating front facing camera capturer.")
                    val videoCapturer: FlashlightCameraCapturer? = enumerator.createCapturer(deviceName, null) as FlashlightCameraCapturer?
                    cameraDeviceId = i
                    cameraDeviceName = deviceName
                    if (videoCapturer != null) {
                        return videoCapturer
                    }
                }
            }
        }
        return null
    }
salih
  • 727
  • 13
  • 36