8000 Add hasTorch support for Android (Camera2 API) · flutter-webrtc/flutter-webrtc@f397f84 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit f397f84

Browse files
committed
Add hasTorch support for Android (Camera2 API)
1 parent 433aa54 commit f397f84

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/GetUserMediaImpl.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.hardware.Camera;
1010
import android.hardware.camera2.CameraAccessException;
1111
import android.hardware.camera2.CameraCaptureSession;
12+
import android.hardware.camera2.CameraCharacteristics;
1213
import android.hardware.camera2.CameraDevice;
1314
import android.hardware.camera2.CameraManager;
1415
import android.hardware.camera2.CaptureRequest;
@@ -814,6 +815,65 @@ void hasTorch(String trackId, Result result) {
814815
return;
815816
}
816817

818+
if (videoCapturer instanceof Camera2Capturer) {
819+
Object session;
820+
try {
821+
Field currentSessionField = Camera2Capturer.class.getSuperclass().getDeclaredField("currentSession");
822+
currentSessionField.setAccessible(true);
823+
session = currentSessionField.get(videoCapturer);
824+
} catch (NoSuchFieldException e) {
825+
// Most likely the upstream Camera1Capturer class have changed
826+
Log.e(TAG, "[TORCH] Failed to get `currentSession` from `Camera1Capturer`");
827+
result.error("Failed to get `currentSession` from `Camera1Capturer`", null, null);
828+
return;
829+
} catch (IllegalAccessException e) {
830+
// Should never happen since we are calling `setAccessible(true)`
831+
throw new RuntimeException(e);
832+
}
833+
834+
CameraManager manager;
835+
try {
836+
Field field = videoCapturer.getClass().getDeclaredField("cameraManager");
837+
field.setAccessible(true);
838+
manager = (CameraManager) field.get(videoCapturer);
839+
} catch (NoSuchFieldException e) {
840+
// Most likely the upstream Camera2Capturer class have changed
841+
Log.e(TAG, "[TORCH] Failed to get `cameraManager` from `Camera2Capturer`");
842+
result.error("Failed to get `cameraManager` from `Camera2Capturer`", null, null);
843+
return;
844+
} catch (IllegalAccessException e) {
845+
// Should never happen since we are calling `setAccessible(true)`
846+
throw new RuntimeException(e);
847+
}
848+
849+
CameraDevice cameraDevice;
850+
try {
851+
Field field = session.getClass().getDeclaredField("cameraDevice");
852+
field.setAccessible(true);
853+
cameraDevice = (CameraDevice) field.get(session);
854+
} catch (NoSuchFieldException e) {
855+
// Most likely the upstream Camera2Capturer class have changed
856+
Log.e(TAG, "[TORCH] Failed to get `cameraDevice` from `Camera2Capturer`");
857+
result.error("Failed to get `cameraDevice` from `Camera2Capturer`", null, null);
858+
return;
859+
} catch (IllegalAccessException e) {
860+
// Should never happen since we are calling `setAccessible(true)`
861+
throw new RuntimeException(e);
862+
}
863+
864+
boolean flashIsAvailable;
865+
try {
866+
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
867+
flashIsAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
868+
} catch (CameraAccessException e) {
869+
// Should never happen since we are already accessing the camera
870+
throw new RuntimeException(e);
871+
}
872+
873+
result.success(flashIsAvailable);
874+
return;
875+
}
876+
817877
if (videoCapturer instanceof Camera1Capturer) {
818878
Object session;
819879
try {

0 commit comments

Comments
 (0)
0