forked from livekit/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetUserMediaImpl.java
More file actions
624 lines (509 loc) · 24.2 KB
/
GetUserMediaImpl.java
File metadata and controls
624 lines (509 loc) · 24.2 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package com.oney.WebRTCModule;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.projection.MediaProjectionManager;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.util.Log;
import androidx.core.util.Consumer;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.oney.WebRTCModule.videoEffects.ProcessorProvider;
import com.oney.WebRTCModule.videoEffects.VideoEffectProcessor;
import com.oney.WebRTCModule.videoEffects.VideoFrameProcessor;
import org.webrtc.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* The implementation of {@code getUserMedia} extracted into a separate file in
* order to reduce complexity and to (somewhat) separate concerns.
*/
class GetUserMediaImpl {
/**
* The {@link Log} tag with which {@code GetUserMediaImpl} is to log.
*/
private static final String TAG = WebRTCModule.TAG;
private static final int PERMISSION_REQUEST_CODE = (int) (Math.random() * Short.MAX_VALUE);
private CameraEnumerator cameraEnumerator;
private final ReactApplicationContext reactContext;
/**
* The application/library-specific private members of local
* {@link MediaStreamTrack}s created by {@code GetUserMediaImpl} mapped by
* track ID.
*/
private final Map<String, TrackPrivate> tracks = new HashMap<>();
private final WebRTCModule webRTCModule;
private Promise displayMediaPromise;
private Intent mediaProjectionPermissionResultData;
private final ServiceConnection mediaProjectionServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// Service is now bound, you can call createScreenStream()
Log.d(TAG, "MediaProjectionService bound, creating screen stream.");
ThreadUtils.runOnExecutor(() -> {
createScreenStream();
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "MediaProjectionService disconnected.");
}
};
GetUserMediaImpl(WebRTCModule webRTCModule, ReactApplicationContext reactContext) {
this.webRTCModule = webRTCModule;
this.reactContext = reactContext;
reactContext.addActivityEventListener(new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
super.onActivityResult(activity, requestCode, resultCode, data);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (resultCode != Activity.RESULT_OK) {
displayMediaPromise.reject("DOMException", "NotAllowedError");
displayMediaPromise = null;
return;
}
mediaProjectionPermissionResultData = data;
MediaProjectionService.launch(activity, mediaProjectionServiceConnection);
}
}
});
}
private AudioTrack createAudioTrack(ReadableMap constraints) {
ReadableMap audioConstraintsMap = constraints.getMap("audio");
Log.d(TAG, "getUserMedia(audio): " + audioConstraintsMap);
String id = UUID.randomUUID().toString();
PeerConnectionFactory pcFactory = webRTCModule.mFactory;
MediaConstraints peerConstraints = webRTCModule.constraintsForOptions(audioConstraintsMap);
// PeerConnectionFactory.createAudioSource will throw an error when mandatory constraints contain nulls.
// so, let's check for nulls
checkMandatoryConstraints(peerConstraints);
AudioSource audioSource = pcFactory.createAudioSource(peerConstraints);
AudioTrack track = pcFactory.createAudioTrack(id, audioSource);
// surfaceTextureHelper is initialized for videoTrack only, so its null here.
tracks.put(id, new TrackPrivate(track, audioSource, /* videoCapturer */ null, /* surfaceTextureHelper */ null));
return track;
}
private void checkMandatoryConstraints(MediaConstraints peerConstraints) {
ArrayList<MediaConstraints.KeyValuePair> valid = new ArrayList<>(peerConstraints.mandatory.size());
for (MediaConstraints.KeyValuePair constraint : peerConstraints.mandatory) {
if (constraint.getValue() != null) {
valid.add(constraint);
} else {
Log.d(TAG, String.format("constraint %s is null, ignoring it", constraint.getKey()));
}
}
peerConstraints.mandatory.clear();
peerConstraints.mandatory.addAll(valid);
}
private CameraEnumerator getCameraEnumerator() {
if (cameraEnumerator == null) {
if (Camera2Enumerator.isSupported(reactContext)) {
Log.d(TAG, "Creating camera enumerator using the Camera2 API");
cameraEnumerator = new Camera2Enumerator(reactContext);
} else {
Log.d(TAG, "Creating camera enumerator using the Camera1 API");
cameraEnumerator = new Camera1Enumerator(false);
}
}
return cameraEnumerator;
}
ReadableArray enumerateDevices() {
WritableArray array = Arguments.createArray();
String[] devices = getCameraEnumerator().getDeviceNames();
for (int i = 0; i < devices.length; ++i) {
String deviceName = devices[i];
boolean isFrontFacing;
try {
// This can throw an exception when using the Camera 1 API.
isFrontFacing = getCameraEnumerator().isFrontFacing(deviceName);
} catch (Exception e) {
Log.e(TAG, "Failed to check the facing mode of camera");
continue;
}
WritableMap params = Arguments.createMap();
params.putString("facing", isFrontFacing ? "front" : "environment");
params.putString("deviceId", "" + i);
params.putString("groupId", "");
params.putString("label", deviceName);
params.putString("kind", "videoinput");
array.pushMap(params);
}
WritableMap audio = Arguments.createMap();
audio.putString("deviceId", "audio-1");
audio.putString("groupId", "");
audio.putString("label", "Audio");
audio.putString("kind", "audioinput");
array.pushMap(audio);
return array;
}
MediaStreamTrack getTrack(String id) {
TrackPrivate private_ = tracks.get(id);
return private_ == null ? null : private_.track;
}
/**
* Implements {@code getUserMedia}. Note that at this point constraints have
* been normalized and permissions have been granted. The constraints only
* contain keys for which permissions have already been granted, that is,
* if audio permission was not granted, there will be no "audio" key in
* the constraints map.
*/
void getUserMedia(final ReadableMap constraints, final Callback successCallback, final Callback errorCallback) {
AudioTrack audioTrack = null;
VideoTrack videoTrack = null;
if (constraints.hasKey("audio")) {
audioTrack = createAudioTrack(constraints);
}
if (constraints.hasKey("video")) {
ReadableMap videoConstraintsMap = constraints.getMap("video");
Log.d(TAG, "getUserMedia(video): " + videoConstraintsMap);
Activity currentActivity = reactContext.getCurrentActivity();
if (currentActivity == null) {
// Fail with DOMException with name InvalidStateError as per:
// https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia
errorCallback.invoke("DOMException", "InvalidStateError");
return;
}
CameraCaptureController cameraCaptureController =
new CameraCaptureController(currentActivity, getCameraEnumerator(), videoConstraintsMap);
videoTrack = createVideoTrack(cameraCaptureController);
}
if (audioTrack == null && videoTrack == null) {
// Fail with DOMException with name AbortError as per:
// https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia
errorCallback.invoke("DOMException", "AbortError");
return;
}
createStream(new MediaStreamTrack[] {audioTrack, videoTrack}, (streamId, tracksInfo) -> {
WritableArray tracksInfoWritableArray = Arguments.createArray();
for (WritableMap trackInfo : tracksInfo) {
tracksInfoWritableArray.pushMap(trackInfo);
}
successCallback.invoke(streamId, tracksInfoWritableArray);
});
}
void mediaStreamTrackSetEnabled(String trackId, final boolean enabled) {
TrackPrivate track = tracks.get(trackId);
if (track != null && track.videoCaptureController != null) {
if (enabled) {
track.videoCaptureController.startCapture();
} else {
if (!track.isClone()) {
track.videoCaptureController.stopCapture();
}
}
}
}
void disposeTrack(String id) {
TrackPrivate track = tracks.remove(id);
if (track != null) {
track.dispose();
}
}
void applyConstraints(String trackId, ReadableMap constraints, Promise promise) {
TrackPrivate track = tracks.get(trackId);
if (track != null && track.videoCaptureController instanceof AbstractVideoCaptureController) {
AbstractVideoCaptureController captureController = (AbstractVideoCaptureController) track.videoCaptureController;
captureController.applyConstraints(constraints, new Consumer<Exception>() {
public void accept(Exception e) {
if(e != null) {
promise.reject(e);
return;
}
promise.resolve(captureController.getSettings());
}
});
} else {
promise.reject(new Exception("Camera track not found!"));
}
}
void getDisplayMedia(Promise promise) {
if (this.displayMediaPromise != null) {
promise.reject(new RuntimeException("Another operation is pending."));
return;
}
Activity currentActivity = this.reactContext.getCurrentActivity();
if (currentActivity == null) {
promise.reject(new RuntimeException("No current Activity."));
return;
}
this.displayMediaPromise = promise;
MediaProjectionManager mediaProjectionManager =
(MediaProjectionManager) currentActivity.getApplication().getSystemService(
Context.MEDIA_PROJECTION_SERVICE);
if (mediaProjectionManager != null) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
currentActivity.startActivityForResult(
mediaProjectionManager.createScreenCaptureIntent(), PERMISSION_REQUEST_CODE);
}
});
} else {
promise.reject(new RuntimeException("MediaProjectionManager is null."));
}
}
private void createScreenStream() {
VideoTrack track = createScreenTrack();
if (track == null) {
displayMediaPromise.reject(new RuntimeException("ScreenTrack is null."));
} else {
createStream(new MediaStreamTrack[] {track}, (streamId, tracksInfo) -> {
WritableMap data = Arguments.createMap();
data.putString("streamId", streamId);
if (tracksInfo.size() == 0) {
displayMediaPromise.reject(new RuntimeException("No ScreenTrackInfo found."));
} else {
data.putMap("track", tracksInfo.get(0));
displayMediaPromise.resolve(data);
}
});
}
// Cleanup
mediaProjectionPermissionResultData = null;
displayMediaPromise = null;
}
void createStream(MediaStreamTrack[] tracks, BiConsumer<String, ArrayList<WritableMap>> successCallback) {
String streamId = UUID.randomUUID().toString();
MediaStream mediaStream = webRTCModule.mFactory.createLocalMediaStream(streamId);
ArrayList<WritableMap> tracksInfo = new ArrayList<>();
for (MediaStreamTrack track : tracks) {
if (track == null) {
continue;
}
if (track instanceof AudioTrack) {
mediaStream.addTrack((AudioTrack) track);
} else {
mediaStream.addTrack((VideoTrack) track);
}
WritableMap trackInfo = Arguments.createMap();
String trackId = track.id();
trackInfo.putBoolean("enabled", track.enabled());
trackInfo.putString("id", trackId);
trackInfo.putString("kind", track.kind());
trackInfo.putString("readyState", "live");
trackInfo.putBoolean("remote", false);
if (track instanceof VideoTrack) {
TrackPrivate tp = this.tracks.get(trackId);
AbstractVideoCaptureController vcc = tp.videoCaptureController;
trackInfo.putMap("settings", vcc.getSettings());
}
if (track instanceof AudioTrack) {
WritableMap settings = Arguments.createMap();
settings.putString("deviceId", "audio-1");
settings.putString("groupId", "");
trackInfo.putMap("settings", settings);
}
tracksInfo.add(trackInfo);
}
Log.d(TAG, "MediaStream id: " + streamId);
webRTCModule.localStreams.put(streamId, mediaStream);
successCallback.accept(streamId, tracksInfo);
}
private VideoTrack createScreenTrack() {
DisplayMetrics displayMetrics = DisplayUtils.getDisplayMetrics(reactContext.getCurrentActivity());
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
ScreenCaptureController screenCaptureController = new ScreenCaptureController(
reactContext.getCurrentActivity(), width, height, mediaProjectionPermissionResultData);
return createVideoTrack(screenCaptureController);
}
VideoTrack createVideoTrack(AbstractVideoCaptureController videoCaptureController) {
videoCaptureController.initializeVideoCapturer();
VideoCapturer videoCapturer = videoCaptureController.videoCapturer;
if (videoCapturer == null) {
return null;
}
PeerConnectionFactory pcFactory = webRTCModule.mFactory;
EglBase.Context eglContext = EglUtils.getRootEglBaseContext();
SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglContext);
if (surfaceTextureHelper == null) {
Log.d(TAG, "Error creating SurfaceTextureHelper");
return null;
}
String id = UUID.randomUUID().toString();
TrackCapturerEventsEmitter eventsEmitter = new TrackCapturerEventsEmitter(webRTCModule, id);
videoCaptureController.setCapturerEventsListener(eventsEmitter);
VideoSource videoSource = pcFactory.createVideoSource(videoCapturer.isScreencast());
videoCapturer.initialize(surfaceTextureHelper, reactContext, videoSource.getCapturerObserver());
VideoTrack track = pcFactory.createVideoTrack(id, videoSource);
// Add dimension detection for local video tracks immediately when created
VideoTrackAdapter localTrackAdapter = new VideoTrackAdapter(webRTCModule, -1); // Use -1 for local tracks
localTrackAdapter.addDimensionDetector(track);
track.setEnabled(true);
tracks.put(id, new TrackPrivate(track, videoSource, videoCaptureController, surfaceTextureHelper, localTrackAdapter));
videoCaptureController.startCapture();
return track;
}
MediaStreamTrack cloneTrack(String trackId) {
TrackPrivate track = tracks.get(trackId);
if (track == null) {
throw new IllegalArgumentException("No track found for id: " + trackId);
}
PeerConnectionFactory pcFactory = webRTCModule.mFactory;
String id = UUID.randomUUID().toString();
MediaStreamTrack nativeTrack = track.track;
final MediaStreamTrack clonedNativeTrack;
VideoTrackAdapter clonedVideoTrackAdapter = null;
if (nativeTrack instanceof VideoTrack) {
clonedNativeTrack = pcFactory.createVideoTrack(id, (VideoSource) track.mediaSource);
// Create dimension detection for cloned video tracks
clonedVideoTrackAdapter = new VideoTrackAdapter(webRTCModule, -1);
clonedVideoTrackAdapter.addDimensionDetector((VideoTrack) clonedNativeTrack);
} else {
clonedNativeTrack = pcFactory.createAudioTrack(id, (AudioSource) track.mediaSource);
}
clonedNativeTrack.setEnabled(nativeTrack.enabled());
final TrackPrivate clone = new TrackPrivate(
clonedNativeTrack,
track.mediaSource,
track.videoCaptureController,
track.surfaceTextureHelper,
clonedVideoTrackAdapter
);
clone.setParent(track);
tracks.put(id, clone);
return clonedNativeTrack;
}
/**
* Set video effects to the TrackPrivate corresponding to the trackId with the help of VideoEffectProcessor
* corresponding to the names.
* @param trackId TrackPrivate id
* @param names VideoEffectProcessor names
*/
void setVideoEffects(String trackId, ReadableArray names) {
TrackPrivate track = tracks.get(trackId);
if (track != null && track.videoCaptureController instanceof CameraCaptureController) {
VideoSource videoSource = (VideoSource) track.mediaSource;
SurfaceTextureHelper surfaceTextureHelper = track.surfaceTextureHelper;
if (names != null) {
List<VideoFrameProcessor> processors = names.toArrayList().stream()
.filter(name -> name instanceof String)
.map(name -> {
VideoFrameProcessor videoFrameProcessor = ProcessorProvider.getProcessor((String) name);
if (videoFrameProcessor == null) {
Log.e(TAG, "no videoFrameProcessor associated with this name: " + name);
}
return videoFrameProcessor;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
VideoEffectProcessor videoEffectProcessor =
new VideoEffectProcessor(processors, surfaceTextureHelper);
videoSource.setVideoProcessor(videoEffectProcessor);
} else {
videoSource.setVideoProcessor(null);
}
}
}
/**
* Application/library-specific private members of local
* {@code MediaStreamTrack}s created by {@code GetUserMediaImpl}.
*/
private static class TrackPrivate {
/**
* The {@code MediaSource} from which {@link #track} was created.
*/
public final MediaSource mediaSource;
public final MediaStreamTrack track;
/**
* The {@code VideoCapturer} from which {@link #mediaSource} was created
* if {@link #track} is a {@link VideoTrack}.
*/
public final AbstractVideoCaptureController videoCaptureController;
private final SurfaceTextureHelper surfaceTextureHelper;
/**
* The {@code VideoTrackAdapter} for dimension detection if {@link #track} is a {@link VideoTrack}.
*/
public final VideoTrackAdapter videoTrackAdapter;
/**
* Whether this object has been disposed or not.
*/
private boolean disposed;
/**
* Whether this object is a clone of another object.
*/
private TrackPrivate parent = null;
/**
* Initializes a new {@code TrackPrivate} instance.
*
* @param track
* @param mediaSource the {@code MediaSource} from which the specified
* {@code code} was created
* @param videoCaptureController the {@code AbstractVideoCaptureController} from which the
* specified {@code mediaSource} was created if the specified
* {@code track} is a {@link VideoTrack}
* @param surfaceTextureHelper the {@code SurfaceTextureHelper} for video rendering
* @param videoTrackAdapter the {@code VideoTrackAdapter} for dimension detection if video track
*/
public TrackPrivate(MediaStreamTrack track, MediaSource mediaSource,
AbstractVideoCaptureController videoCaptureController, SurfaceTextureHelper surfaceTextureHelper,
VideoTrackAdapter videoTrackAdapter) {
this.track = track;
this.mediaSource = mediaSource;
this.videoCaptureController = videoCaptureController;
this.surfaceTextureHelper = surfaceTextureHelper;
this.videoTrackAdapter = videoTrackAdapter;
this.disposed = false;
}
/**
* Backwards compatibility constructor for audio tracks
*/
public TrackPrivate(MediaStreamTrack track, MediaSource mediaSource,
AbstractVideoCaptureController videoCaptureController, SurfaceTextureHelper surfaceTextureHelper) {
this(track, mediaSource, videoCaptureController, surfaceTextureHelper, null);
}
public void dispose() {
final boolean isClone = this.isClone();
if (!disposed) {
if (!isClone && videoCaptureController != null) {
if (videoCaptureController.stopCapture()) {
videoCaptureController.dispose();
}
}
// Clean up VideoTrackAdapter for video tracks
if (!isClone && videoTrackAdapter != null && track instanceof VideoTrack) {
videoTrackAdapter.removeDimensionDetector((VideoTrack) track);
}
/*
* As per webrtc library documentation - The caller still has ownership of {@code
* surfaceTextureHelper} and is responsible for making sure surfaceTextureHelper.dispose() is
* called. This also means that the caller can reuse the SurfaceTextureHelper to initialize a new
* VideoCapturer once the previous VideoCapturer has been disposed. */
if (!isClone && surfaceTextureHelper != null) {
surfaceTextureHelper.stopListening();
surfaceTextureHelper.dispose();
}
// clones should not dispose the mediaSource as that will affect the original track
// and other clones as well (since they share the same mediaSource).
if (!isClone) {
mediaSource.dispose();
}
track.dispose();
disposed = true;
}
}
public void setParent(TrackPrivate parent) {
this.parent = parent;
}
public boolean isClone() {
return this.parent != null;
}
}
public interface BiConsumer<T, U> { void accept(T t, U u); }
}