|
6 | 6 | import android.content.ContentValues;
|
7 | 7 | import android.content.Context;
|
8 | 8 | import android.content.pm.PackageManager;
|
| 9 | +import android.hardware.Camera; |
9 | 10 | import android.os.Build;
|
10 | 11 | import android.os.Bundle;
|
11 | 12 | import android.os.Handler;
|
|
33 | 34 | import com.cloudwebrtc.webrtc.utils.PermissionUtils;
|
34 | 35 |
|
35 | 36 | import java.io.File;
|
| 37 | +import java.lang.reflect.Field; |
36 | 38 | import java.util.ArrayList;
|
37 | 39 | import java.util.HashMap;
|
38 | 40 | import java.util.List;
|
@@ -797,4 +799,53 @@ void stopRecording(Integer id) {
|
797 | 799 | }
|
798 | 800 | }
|
799 | 801 |
|
| 802 | + void setTorch(String trackId, boolean torch, Result result) { |
| 803 | + VideoCapturer videoCapturer = mVideoCapturers.get(trackId); |
| 804 | + if (videoCapturer == null) { |
| 805 | + result.error("Video capturer not found for id: " + trackId, null, null); |
| 806 | + return; |
| 807 | + } |
| 808 | + |
| 809 | + if (videoCapturer instanceof Camera1Capturer) { |
| 810 | + Object session; |
| 811 | + try { |
| 812 | + Field currentSessionField = Camera1Capturer.class.getSuperclass().getDeclaredField("currentSession"); |
| 813 | + currentSessionField.setAccessible(true); |
| 814 | + session = currentSessionField.get(videoCapturer); |
| 815 | + } catch (NoSuchFieldException e) { |
| 816 | + // Most likely the upstream Camera1Capturer class have changed |
| 817 | + Log.e(TAG, "[TORCH] Failed to get `currentSession` from `Camera1Capturer`"); |
| 818 | + result.error("Failed to get `currentSession` from `Camera1Capturer`", null, null); |
| 819 | + return; |
| 820 | + } catch (IllegalAccessException e) { |
| 821 | + // Should never happen since we are calling `setAccessible(true)` |
| 822 | + throw new RuntimeException(e); |
| 823 | + } |
| 824 | + |
| 825 | + Camera camera; |
| 826 | + try { |
| 827 | + Field cameraField = session.getClass().getDeclaredField("camera"); |
| 828 | + cameraField.setAccessible(true); |
| 829 | + camera = (Camera) cameraField.get(session); |
| 830 | + } catch (NoSuchFieldException e) { |
| 831 | + // Most likely the upstream Camera1Session class have changed |
| 832 | + Log.e(TAG, "[TORCH] Failed to get `camera` from `Camera1Session`"); |
| 833 | + result.error("Failed to get `camera` from `Camera1Session`", null, null); |
| 834 | + return; |
| 835 | + } catch (IllegalAccessException e) { |
| 836 | + // Should never happen since we are calling `setAccessible(true)` |
| 837 | + throw new RuntimeException(e); |
| 838 | + } |
| 839 | + |
| 840 | + Camera.Parameters params = camera.getParameters(); |
| 841 | + params.setFlashMode(torch ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF); |
| 842 | + camera.setParameters(params); |
| 843 | + |
| 844 | + result.success(null); |
| 845 | + return; |
| 846 | + } |
| 847 | + |
| 848 | + Log.e(TAG, "[TORCH] Video capturer not compatible"); |
| 849 | + result.error("Video capturer not compatible", null, null); |
| 850 | + } |
800 | 851 | }
|
0 commit comments