3
3
import android .content .Context ;
4
4
import android .hardware .camera2 .CameraManager ;
5
5
import android .util .Log ;
6
- import android . util . Pair ;
6
+
7
7
import androidx .annotation .Nullable ;
8
8
import androidx .core .util .Consumer ;
9
9
@@ -31,6 +31,7 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
31
31
32
32
private boolean isFrontFacing ;
33
33
34
+
34
35
/**
35
36
* Equivalent to the camera index as a String
36
37
*/
@@ -39,6 +40,9 @@ public class CameraCaptureController extends AbstractVideoCaptureController {
39
40
40
41
private final Context context ;
41
42
private final CameraEnumerator cameraEnumerator ;
43
+
44
+ private final String constraintDeviceId ;
45
+ private final String constraintFacingMode ;
42
46
private ReadableMap constraints ;
43
47
44
48
/**
@@ -62,6 +66,9 @@ public CameraCaptureController(Context context, CameraEnumerator cameraEnumerato
62
66
this .context = context ;
63
67
this .cameraEnumerator = cameraEnumerator ;
64
68
this .constraints = constraints ;
69
+
70
+ this .constraintDeviceId = ReactBridgeUtil .getMapStrValue (this .constraints , "deviceId" );
71
+ this .constraintFacingMode = ReactBridgeUtil .getMapStrValue (this .constraints , "facingMode" );
65
72
}
66
73
67
74
@ Nullable
@@ -113,8 +120,10 @@ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Excepti
113
120
114
121
// Find target camera to switch to.
115
122
String [] deviceNames = cameraEnumerator .getDeviceNames ();
116
- final String deviceId = ReactBridgeUtil .getMapStrValue (constraints , "deviceId" );
117
- final String facingMode = ReactBridgeUtil .getMapStrValue (constraints , "facingMode" );
123
+
124
+ // Use the initial deviceId/facingMode. It is a constraint violation to change these through applyConstraints.
125
+ final String deviceId = constraintDeviceId ;
126
+ final String facingMode = constraintFacingMode ;
118
127
int cameraIndex = -1 ;
119
128
String cameraName = null ;
120
129
@@ -144,11 +153,12 @@ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Excepti
144
153
145
154
if (cameraName == null ) {
146
155
if (onFinishedCallback != null ) {
147
- onFinishedCallback .accept (new Exception ("OverconstrainedError: could not find camera with deviceId: " + deviceId + " or facingMode: " + facingMode ));
156
+ onFinishedCallback .accept (new Exception (
157
+ "OverconstrainedError: could not find camera with deviceId: " + deviceId + " or facingMode: " + facingMode ));
148
158
}
149
159
return ;
150
160
}
151
-
161
+
152
162
// For lambda reference
153
163
final int finalCameraIndex = cameraIndex ;
154
164
final String finalCameraName = cameraName ;
@@ -200,11 +210,8 @@ public void onCameraSwitchError(String s) {
200
210
201
211
@ Override
202
212
protected VideoCapturer createVideoCapturer () {
203
- String deviceId = ReactBridgeUtil .getMapStrValue (this .constraints , "deviceId" );
204
- String facingMode = ReactBridgeUtil .getMapStrValue (this .constraints , "facingMode" );
205
-
206
- CreateCapturerResult result = createVideoCapturer (deviceId , facingMode );
207
- if (result == null ) {
213
+ CreateCapturerResult result = createVideoCapturer (constraintDeviceId , constraintFacingMode );
214
+ if (result == null ) {
208
215
return null ;
209
216
}
210
217
@@ -233,12 +240,12 @@ private void updateActualSize(int cameraIndex, String cameraName, VideoCapturer
233
240
* Constructs a new {@code VideoCapturer} instance attempting to satisfy
234
241
* specific constraints.
235
242
*
236
- * @param deviceId the ID of the requested video device. If not
237
- * {@code null} and a {@code VideoCapturer} can be created for it, then
238
- * {@code facingMode} is ignored.
243
+ * @param deviceId the ID of the requested video device. If not
244
+ * {@code null} and a {@code VideoCapturer} can be created for it, then
245
+ * {@code facingMode} is ignored.
239
246
* @param facingMode the facing of the requested video source such as
240
- * {@code user} and {@code environment}. If {@code null}, "user" is
241
- * presumed.
247
+ * {@code user} and {@code environment}. If {@code null}, "user" is
248
+ * presumed.
242
249
* @return a pair containing the deviceId and {@code VideoCapturer} satisfying the {@code facingMode} or
243
250
* {@code deviceId} constraint, or null.
244
251
*/
@@ -325,7 +332,7 @@ private static class CreateCapturerResult {
325
332
public final int cameraIndex ;
326
333
public final String cameraName ;
327
334
public final VideoCapturer videoCapturer ;
328
-
335
+
329
336
public CreateCapturerResult (int cameraIndex , String cameraName , VideoCapturer videoCapturer ) {
330
337
this .cameraIndex = cameraIndex ;
331
338
this .cameraName = cameraName ;
0 commit comments