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 VideoFrame
s 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 :)