Repository navigation
Expand file tree
/
Copy pathCameraCaptureController.java
More file actions
450 lines (394 loc) · 18.1 KB
/
Copy pathCameraCaptureController.java
File metadata and controls
450 lines (394 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package com.oney.WebRTCModule;
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.util.Log;
import android.view.Surface;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import org.webrtc.Camera1Capturer;
import org.webrtc.Camera1Helper;
import org.webrtc.Camera2Capturer;
import org.webrtc.Camera2Enumerator;
import org.webrtc.Camera2Helper;
import org.webrtc.CameraEnumerator;
import org.webrtc.CameraVideoCapturer;
import org.webrtc.Size;
import org.webrtc.VideoCapturer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class CameraCaptureController extends AbstractVideoCaptureController {
/**
* The {@link Log} tag with which {@code CameraCaptureController} is to log.
*/
private static final String TAG = CameraCaptureController.class.getSimpleName();
private boolean isFrontFacing;
/** Cached SENSOR_ORIENTATION of the active camera (0/90/180/270), or -1 if unknown. Updated on every camera open/switch. */
private int sensorOrientation = -1;
/**
* Equivalent to the camera index as a String
*/
@Nullable
private String currentDeviceId;
private final Context context;
private final CameraEnumerator cameraEnumerator;
private final String constraintDeviceId;
private final String constraintFacingMode;
private ReadableMap constraints;
/**
* The {@link CameraEventsHandler} used with
* {@link CameraEnumerator#createCapturer}.
*/
private final CameraEventsHandler cameraEventsHandler = new CameraEventsHandler() {
@Override
public void onCameraOpening(String cameraName) {
super.onCameraOpening(cameraName);
int cameraIndex = findCameraIndex(cameraName);
updateActualSize(cameraIndex, cameraName, videoCapturer);
CameraCaptureController.this.currentDeviceId = cameraIndex == -1 ? null : String.valueOf(cameraIndex);
CameraCaptureController.this.currentCameraName = cameraName;
}
// Android took our camera (another app opened a camera).
@Override
public void onCameraDisconnected() {
super.onCameraDisconnected();
cameraRecovery.onCameraDisconnected(currentCameraManagerId());
}
// The camera could not be opened, a fatal camera error happened, or the camera is slow to
// start (a warning: the open continues).
@Override
public void onCameraError(String errorDescription) {
super.onCameraError(errorDescription);
cameraRecovery.onCameraError(errorDescription);
}
@Override
public void onFirstFrameAvailable() {
super.onFirstFrameAvailable();
cameraRecovery.onFramesArrived();
}
};
/** Gets the camera back after another app took it. See its class doc for which Android versions. */
private final CameraRecoveryController cameraRecovery;
/** Name of the camera that was opened last. For Camera2 this is the CameraManager id. */
@Nullable
private volatile String currentCameraName;
/**
* The CameraManager id of the camera that was opened last, or null if unknown. Camera2: the
* camera name is the CameraManager id. Camera1: the camera index is the same number.
*/
@Nullable
private String currentCameraManagerId() {
return cameraEnumerator instanceof Camera2Enumerator ? currentCameraName : currentDeviceId;
}
public CameraCaptureController(Context context, CameraEnumerator cameraEnumerator, ReadableMap constraints) {
super(constraints.getInt("width"), constraints.getInt("height"), constraints.getInt("frameRate"));
this.context = context;
this.cameraRecovery = new CameraRecoveryController(
context,
// The parent's startCapture: the app's request (appRequestsCapture) does not change.
CameraCaptureController.super::startCapture,
new CameraRecoveryController.Listener() {
@Override
public void onCaptureInterruptedChanged(boolean interrupted) {
if (capturerEventsListener != null) {
capturerEventsListener.onCapturerMuteChanged(interrupted);
}
}
@Override
public void onRecoveryFailed() {
if (capturerEventsListener != null) {
capturerEventsListener.onCapturerEnded();
}
}
});
this.cameraEnumerator = cameraEnumerator;
this.constraints = constraints;
this.constraintDeviceId = ReactBridgeUtil.getMapStrValue(this.constraints, "deviceId");
this.constraintFacingMode = ReactBridgeUtil.getMapStrValue(this.constraints, "facingMode");
}
@Nullable
@Override
public String getDeviceId() {
return currentDeviceId;
}
@Override
public void startCapture() {
cameraRecovery.setCaptureRequested(true);
super.startCapture();
}
@Override
public boolean stopCapture() {
// First, so a reopen that is waiting can't turn the camera back on.
cameraRecovery.setCaptureRequested(false);
return super.stopCapture();
}
@Override
public void dispose() {
cameraRecovery.setCaptureRequested(false);
super.dispose();
}
private int findCameraIndex(String cameraName) {
String[] deviceNames = cameraEnumerator.getDeviceNames();
for (int i = 0; i < deviceNames.length; i++) {
if (Objects.equals(deviceNames[i], cameraName)) {
return i;
}
}
return -1;
}
/** True when the delivered frame is rotated 90/270 vs the sensor, so reported W/H must swap.
* Unknown sensor orientation (-1) → false: report the capture format unchanged (status quo, no
* regression) rather than guessing. Pure → testable. */
static boolean shouldSwapDimensions(int sensorOrientation, int displayRotationDegrees) {
if (sensorOrientation < 0) return false;
return ((sensorOrientation + displayRotationDegrees) % 180) == 90;
}
/** Current display rotation in degrees (0/90/180/270). Matches CameraSession.getDeviceOrientation. */
private int getDeviceRotationDegrees() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
switch (wm.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90: return 90;
case Surface.ROTATION_180: return 180;
case Surface.ROTATION_270: return 270;
case Surface.ROTATION_0:
default: return 0;
}
}
@Override
public WritableMap getSettings() {
WritableMap settings = super.getSettings();
settings.putString("facingMode", isFrontFacing ? "user" : "environment");
// M145 delivers frames pre-rotated to the device orientation; super.getSettings() reports the
// capture format in sensor space. Swap so reported dims match the delivered/rendered frame.
if (shouldSwapDimensions(sensorOrientation, getDeviceRotationDegrees())) {
settings.putInt("width", getHeight());
settings.putInt("height", getWidth());
}
return settings;
}
@Override
public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Exception> onFinishedCallback) {
ReadableMap oldConstraints = this.constraints;
int oldTargetWidth = this.targetWidth;
int oldTargetHeight = this.targetHeight;
int oldTargetFps = this.targetFps;
// Don't save constraints yet, since we may fail to find a fit.
Runnable saveConstraints = () -> {
this.constraints = constraints;
this.targetWidth = constraints.getInt("width");
this.targetHeight = constraints.getInt("height");
this.targetFps = constraints.getInt("frameRate");
};
if (videoCapturer == null) {
// No existing capturer, just let it initialize normally.
saveConstraints.run();
if (onFinishedCallback != null) {
onFinishedCallback.accept(null);
}
return;
}
// Find target camera to switch to.
String[] deviceNames = cameraEnumerator.getDeviceNames();
// Re-read the incoming constraints so MediaStreamTrack._switchCamera() can
// flip the camera via applyConstraints({facingMode}) — the W3C pattern browsers use.
final String deviceId = ReactBridgeUtil.getMapStrValue(constraints, "deviceId");
final String facingMode = ReactBridgeUtil.getMapStrValue(constraints, "facingMode");
int cameraIndex = -1;
String cameraName = null;
// If deviceId is specified, then it takes precedence over facingMode.
if (deviceId != null) {
try {
cameraIndex = Integer.parseInt(deviceId);
cameraName = deviceNames[cameraIndex];
} catch (Exception e) {
Log.d(TAG, "failed to find device with id: " + deviceId);
}
}
// Otherwise, use facingMode (defaulting to front/user facing).
if (cameraName == null) {
cameraIndex = -1;
final boolean isFrontFacing = facingMode == null || facingMode.equals("user");
for (String name : deviceNames) {
cameraIndex++;
if (cameraEnumerator.isFrontFacing(name) == isFrontFacing) {
cameraName = name;
break;
}
}
}
if (cameraName == null) {
if (onFinishedCallback != null) {
onFinishedCallback.accept(new Exception("OverconstrainedError: could not find camera with deviceId: "
+ deviceId + " or facingMode: " + facingMode));
}
return;
}
// For lambda reference
final int finalCameraIndex = cameraIndex;
final String finalCameraName = cameraName;
boolean shouldSwitchCamera = false;
try {
int currentCameraIndex = Integer.parseInt(currentDeviceId);
shouldSwitchCamera = cameraIndex != currentCameraIndex;
} catch (Exception e) {
shouldSwitchCamera = true;
Log.d(TAG, "Forcing camera switch, couldn't parse current device id: " + currentDeviceId);
}
CameraVideoCapturer capturer = (CameraVideoCapturer) videoCapturer;
Runnable changeFormatIfNeededAndFinish = () -> {
saveConstraints.run();
if (targetWidth != oldTargetWidth || targetHeight != oldTargetHeight || targetFps != oldTargetFps) {
updateActualSize(finalCameraIndex, finalCameraName, videoCapturer);
capturer.changeCaptureFormat(targetWidth, targetHeight, targetFps);
}
if (onFinishedCallback != null) {
onFinishedCallback.accept(null);
}
};
if (shouldSwitchCamera) {
capturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
@Override
public void onCameraSwitchDone(boolean isFrontCamera) {
CameraCaptureController.this.isFrontFacing = isFrontCamera;
changeFormatIfNeededAndFinish.run();
}
@Override
public void onCameraSwitchError(String s) {
Exception e = new Exception("Error switching camera: " + s);
Log.e(TAG, "OnCameraSwitchError", e);
if (onFinishedCallback != null) {
onFinishedCallback.accept(e);
}
}
}, cameraName);
} else {
// No camera switch needed, just change format if needed.
changeFormatIfNeededAndFinish.run();
}
}
@Override
protected VideoCapturer createVideoCapturer() {
CreateCapturerResult result = createVideoCapturer(constraintDeviceId, constraintFacingMode);
if (result == null) {
return null;
}
updateActualSize(result.cameraIndex, result.cameraName, result.videoCapturer);
return result.videoCapturer;
}
private void updateActualSize(int cameraIndex, String cameraName, VideoCapturer videoCapturer) {
// Find actual capture format.
Size actualSize = null;
if (videoCapturer instanceof Camera1Capturer) {
actualSize = Camera1Helper.findClosestCaptureFormat(cameraIndex, targetWidth, targetHeight);
sensorOrientation = Camera1Helper.getSensorOrientation(cameraIndex); // may be -1 (unknown)
if (sensorOrientation < 0) Log.w(TAG, "Sensor orientation unavailable for camera " + cameraIndex + "; dimension swap disabled");
} else if (videoCapturer instanceof Camera2Capturer) {
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
actualSize = Camera2Helper.findClosestCaptureFormat(cameraManager, cameraName, targetWidth, targetHeight);
sensorOrientation = Camera2Helper.getSensorOrientation(cameraManager, cameraName); // may be -1
if (sensorOrientation < 0) Log.w(TAG, "Sensor orientation unavailable for camera " + cameraName + "; dimension swap disabled");
}
if (actualSize != null) {
actualWidth = actualSize.width;
actualHeight = actualSize.height;
}
}
/**
* Constructs a new {@code VideoCapturer} instance attempting to satisfy
* specific constraints.
*
* @param deviceId the ID of the requested video device. If not
* {@code null} and a {@code VideoCapturer} can be created for it, then
* {@code facingMode} is ignored.
* @param facingMode the facing of the requested video source such as
* {@code user} and {@code environment}. If {@code null}, "user" is
* presumed.
* @return a pair containing the deviceId and {@code VideoCapturer} satisfying the {@code facingMode} or
* {@code deviceId} constraint, or null.
*/
@Nullable
private CreateCapturerResult createVideoCapturer(String deviceId, String facingMode) {
String[] deviceNames = cameraEnumerator.getDeviceNames();
List<String> failedDevices = new ArrayList<>();
String cameraName = null;
int cameraIndex = -1;
try {
cameraIndex = Integer.parseInt(deviceId);
cameraName = deviceNames[cameraIndex];
} catch (Exception e) {
Log.d(TAG, "failed to find device with id: " + deviceId);
}
// If deviceId is specified, then it takes precedence over facingMode.
if (cameraName != null) {
VideoCapturer videoCapturer = cameraEnumerator.createCapturer(cameraName, cameraEventsHandler);
String message = "Create user-specified camera " + cameraName;
if (videoCapturer != null) {
Log.d(TAG, message + " succeeded");
this.isFrontFacing = cameraEnumerator.isFrontFacing(cameraName);
this.currentDeviceId = String.valueOf(cameraIndex);
return new CreateCapturerResult(cameraIndex, cameraName, videoCapturer);
} else {
// fallback to facingMode
Log.d(TAG, message + " failed");
failedDevices.add(cameraName);
}
}
// Otherwise, use facingMode (defaulting to front/user facing).
final boolean isFrontFacing = facingMode == null || facingMode.equals("user");
cameraIndex = -1;
for (String name : deviceNames) {
cameraIndex++;
if (failedDevices.contains(name)) {
continue;
}
if (cameraEnumerator.isFrontFacing(name) != isFrontFacing) {
continue;
}
VideoCapturer videoCapturer = cameraEnumerator.createCapturer(name, cameraEventsHandler);
String message = "Create camera " + name;
if (videoCapturer != null) {
Log.d(TAG, message + " succeeded");
this.isFrontFacing = cameraEnumerator.isFrontFacing(name);
this.currentDeviceId = String.valueOf(cameraIndex);
return new CreateCapturerResult(cameraIndex, name, videoCapturer);
} else {
Log.d(TAG, message + " failed");
failedDevices.add(name);
}
}
cameraIndex = -1;
// Fallback to any available camera.
for (String name : deviceNames) {
cameraIndex++;
if (!failedDevices.contains(name)) {
VideoCapturer videoCapturer = cameraEnumerator.createCapturer(name, cameraEventsHandler);
String message = "Create fallback camera " + name;
if (videoCapturer != null) {
Log.d(TAG, message + " succeeded");
this.isFrontFacing = cameraEnumerator.isFrontFacing(name);
this.currentDeviceId = String.valueOf(cameraIndex);
return new CreateCapturerResult(cameraIndex, name, videoCapturer);
} else {
Log.d(TAG, message + " failed");
failedDevices.add(name);
}
}
}
currentDeviceId = null;
Log.w(TAG, "Unable to identify a suitable camera.");
return null;
}
private static class CreateCapturerResult {
public final int cameraIndex;
public final String cameraName;
public final VideoCapturer videoCapturer;
public CreateCapturerResult(int cameraIndex, String cameraName, VideoCapturer videoCapturer) {
this.cameraIndex = cameraIndex;
this.cameraName = cameraName;
this.videoCapturer = videoCapturer;
}
}
}