forked from livekit/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCameraCaptureController.java
More file actions
339 lines (296 loc) · 13.1 KB
/
CameraCaptureController.java
File metadata and controls
339 lines (296 loc) · 13.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
package com.oney.WebRTCModule;
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.util.Log;
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.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;
/**
* 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);
}
};
public CameraCaptureController(Context context, CameraEnumerator cameraEnumerator, ReadableMap constraints) {
super(constraints.getInt("width"), constraints.getInt("height"), constraints.getInt("frameRate"));
this.context = context;
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;
}
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;
}
@Override
public WritableMap getSettings() {
WritableMap settings = super.getSettings();
settings.putString("facingMode", isFrontFacing ? "user" : "environment");
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 from the incoming constraints so `MediaStreamTrack._switchCamera()`
// can flip the camera via `applyConstraints({facingMode})` — the documented
// W3C pattern that browsers also implement.
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);
} else if (videoCapturer instanceof Camera2Capturer) {
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
actualSize = Camera2Helper.findClosestCaptureFormat(cameraManager, cameraName, targetWidth, targetHeight);
}
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;
}
}
}