forked from livekit/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebRTCModule+RTCMediaStream.m
More file actions
678 lines (578 loc) · 25.2 KB
/
WebRTCModule+RTCMediaStream.m
File metadata and controls
678 lines (578 loc) · 25.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#import <objc/runtime.h>
#import <WebRTC/RTCCameraVideoCapturer.h>
#import <WebRTC/RTCMediaConstraints.h>
#import <WebRTC/RTCMediaStreamTrack.h>
#import <WebRTC/RTCVideoTrack.h>
#import <WebRTC/WebRTC.h>
#import "RTCMediaStreamTrack+React.h"
#import "WebRTCModuleOptions.h"
#import "WebRTCModule+RTCMediaStream.h"
#import "WebRTCModule+RTCPeerConnection.h"
#import "WebRTCModule+VideoTrackAdapter.h"
#import "ProcessorProvider.h"
#import "ScreenCaptureController.h"
#import "ScreenCapturer.h"
#import "TrackCapturerEventsEmitter.h"
#import "VideoCaptureController.h"
@implementation WebRTCModule (RTCMediaStream)
- (VideoEffectProcessor *)videoEffectProcessor
{
return objc_getAssociatedObject(self, _cmd);
}
- (void)setVideoEffectProcessor:(VideoEffectProcessor *)videoEffectProcessor
{
objc_setAssociatedObject(self, @selector(videoEffectProcessor), videoEffectProcessor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - Default Media Constraints
/**
* Returns common optional constraints for improved audio quality.
* Ported from stream-video-swift DefaultRTCMediaConstraints.
* https://github.com/GetStream/stream-video-swift/blob/develop/Sources/StreamVideo/WebRTC/DefaultRTCMediaConstraints.swift
*/
+ (NSDictionary *)commonOptionalConstraints {
return @{
@"DtlsSrtpKeyAgreement": kRTCMediaConstraintsValueTrue,
@"googAutoGainControl": kRTCMediaConstraintsValueTrue,
@"googNoiseSuppression": kRTCMediaConstraintsValueTrue,
@"googEchoCancellation": kRTCMediaConstraintsValueTrue,
@"googHighpassFilter": kRTCMediaConstraintsValueTrue,
@"googTypingNoiseDetection": kRTCMediaConstraintsValueTrue,
@"googAudioMirroring": kRTCMediaConstraintsValueFalse
};
}
#pragma mark - getUserMedia
- (NSString *)convertBoolToString:(id)value {
return value ? @"true" : @"false";
}
/**
* Initializes a new {@link RTCAudioTrack} which satisfies the given constraints.
*
* @param constraints The {@code MediaStreamConstraints} which the new
* {@code RTCAudioTrack} instance is to satisfy.
*/
- (RTCAudioTrack *)createAudioTrack:(NSDictionary *)constraints {
NSString *trackId = [[NSUUID UUID] UUIDString];
NSDictionary *audioConstraints = constraints[@"audio"];
// Start with common optional constraints as defaults
NSMutableDictionary *optionalConstraints = [[[self class] commonOptionalConstraints] mutableCopy];
// Override with user-provided constraints if specified
if (audioConstraints[@"autoGainControl"] != nil) {
optionalConstraints[@"googAutoGainControl"] = [self convertBoolToString:audioConstraints[@"autoGainControl"]];
}
if (audioConstraints[@"noiseSuppression"] != nil) {
optionalConstraints[@"googNoiseSuppression"] = [self convertBoolToString:audioConstraints[@"noiseSuppression"]];
}
if (audioConstraints[@"echoCancellation"] != nil) {
optionalConstraints[@"googEchoCancellation"] = [self convertBoolToString:audioConstraints[@"echoCancellation"]];
}
if (audioConstraints[@"highpassFilter"] != nil) {
optionalConstraints[@"googHighpassFilter"] = [self convertBoolToString:audioConstraints[@"highpassFilter"]];
}
RTCMediaConstraints *mediaConstraints =
[[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:optionalConstraints];
RTCAudioSource *audioSource = [self.peerConnectionFactory audioSourceWithConstraints:mediaConstraints];
RTCAudioTrack *audioTrack = [self.peerConnectionFactory audioTrackWithSource:audioSource trackId:trackId];
return audioTrack;
}
/**
* Initializes a new {@link RTCVideoTrack} with the given capture controller
*/
- (RTCVideoTrack *)createVideoTrackWithCaptureController:
(CaptureController * (^)(RTCVideoSource *))captureControllerCreator {
#if TARGET_OS_TV
return nil;
#else
RTCVideoSource *videoSource = [self.peerConnectionFactory videoSource];
NSString *trackUUID = [[NSUUID UUID] UUIDString];
RTCVideoTrack *videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource trackId:trackUUID];
CaptureController *captureController = captureControllerCreator(videoSource);
videoTrack.captureController = captureController;
[captureController startCapture];
// Add dimension detection for local video tracks immediately
[self addLocalVideoTrackDimensionDetection:videoTrack];
return videoTrack;
#endif
}
/**
* Initializes a new {@link RTCMediaTrack} with the given tracks.
*
* @return An array with the mediaStreamId in index 0, and track infos in index 1.
*/
- (NSArray *)createMediaStream:(NSArray<RTCMediaStreamTrack *> *)tracks {
#if TARGET_OS_TV
return nil;
#else
NSString *mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream *mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
NSMutableArray<NSDictionary *> *trackInfos = [NSMutableArray array];
for (RTCMediaStreamTrack *track in tracks) {
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream addAudioTrack:(RTCAudioTrack *)track];
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream addVideoTrack:(RTCVideoTrack *)track];
}
NSString *trackId = track.trackId;
self.localTracks[trackId] = track;
NSDictionary *settings = @{};
if ([track.kind isEqualToString:@"video"]) {
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
if ([videoTrack.captureController isKindOfClass:[CaptureController class]]) {
settings = [videoTrack.captureController getSettings];
}
} else if ([track.kind isEqualToString:@"audio"]) {
settings = @{
@"deviceId": @"audio",
@"groupId": @"",
};
}
[trackInfos addObject:@{
@"enabled" : @(track.isEnabled),
@"id" : trackId,
@"kind" : track.kind,
@"readyState" : @"live",
@"remote" : @(NO),
@"settings" : settings
}];
}
self.localStreams[mediaStreamId] = mediaStream;
return @[ mediaStreamId, trackInfos ];
#endif
}
/**
* Initializes a new {@link RTCVideoTrack} which satisfies the given constraints.
*/
- (RTCVideoTrack *)createVideoTrack:(NSDictionary *)constraints {
#if TARGET_OS_TV
return nil;
#else
RTCVideoSource *videoSource = [self.peerConnectionFactory videoSource];
NSString *trackUUID = [[NSUUID UUID] UUIDString];
RTCVideoTrack *videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource trackId:trackUUID];
#if !TARGET_IPHONE_SIMULATOR
RTCCameraVideoCapturer *videoCapturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:videoSource];
VideoCaptureController *videoCaptureController =
[[VideoCaptureController alloc] initWithCapturer:videoCapturer andConstraints:constraints[@"video"]];
videoCaptureController.enableMultitaskingCameraAccess = [WebRTCModuleOptions sharedInstance].enableMultitaskingCameraAccess;
videoTrack.captureController = videoCaptureController;
[videoCaptureController startCapture];
#endif
// Add dimension detection for local video tracks immediately
[self addLocalVideoTrackDimensionDetection:videoTrack];
return videoTrack;
#endif
}
- (RTCVideoTrack *)createScreenCaptureVideoTrack {
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_OSX || TARGET_OS_TV
return nil;
#endif
RTCVideoSource *videoSource = [self.peerConnectionFactory videoSourceForScreenCast:YES];
NSString *trackUUID = [[NSUUID UUID] UUIDString];
RTCVideoTrack *videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource trackId:trackUUID];
ScreenCapturer *screenCapturer = [[ScreenCapturer alloc] initWithDelegate:videoSource];
ScreenCaptureController *screenCaptureController =
[[ScreenCaptureController alloc] initWithCapturer:screenCapturer];
TrackCapturerEventsEmitter *emitter = [[TrackCapturerEventsEmitter alloc] initWith:trackUUID webRTCModule:self];
screenCaptureController.eventsDelegate = emitter;
videoTrack.captureController = screenCaptureController;
[screenCaptureController startCapture];
// Add dimension detection for local video tracks immediately
[self addLocalVideoTrackDimensionDetection:videoTrack];
return videoTrack;
}
RCT_EXPORT_METHOD(getDisplayMedia : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject) {
#if TARGET_OS_TV
reject(@"unsupported_platform", @"tvOS is not supported", nil);
return;
#else
RTCVideoTrack *videoTrack = [self createScreenCaptureVideoTrack];
if (videoTrack == nil) {
reject(@"DOMException", @"AbortError", nil);
return;
}
NSString *mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream *mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
[mediaStream addVideoTrack:videoTrack];
NSString *trackId = videoTrack.trackId;
self.localTracks[trackId] = videoTrack;
NSDictionary *trackInfo = @{
@"enabled" : @(videoTrack.isEnabled),
@"id" : videoTrack.trackId,
@"kind" : videoTrack.kind,
@"readyState" : @"live",
@"remote" : @(NO)
};
self.localStreams[mediaStreamId] = mediaStream;
resolve(@{@"streamId" : mediaStreamId, @"track" : trackInfo});
#endif
}
/**
* 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 dictionary.
*/
RCT_EXPORT_METHOD(getUserMedia
: (NSDictionary *)constraints successCallback
: (RCTResponseSenderBlock)successCallback errorCallback
: (RCTResponseSenderBlock)errorCallback) {
#if TARGET_OS_TV
errorCallback(@[ @"PlatformNotSupported", @"getUserMedia is not supported on tvOS." ]);
return;
#else
RTCAudioTrack *audioTrack = nil;
RTCVideoTrack *videoTrack = nil;
if (constraints[@"audio"]) {
audioTrack = [self createAudioTrack:constraints];
[self ensureAudioSessionWithRecording];
}
if (constraints[@"video"]) {
videoTrack = [self createVideoTrack:constraints];
}
if (audioTrack == nil && videoTrack == nil) {
// Fail with DOMException with name AbortError as per:
// https://www.w3.org/TR/mediacapture-streams/#dom-mediadevices-getusermedia
errorCallback(@[ @"DOMException", @"AbortError" ]);
return;
}
NSString *mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream *mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
NSMutableArray *tracks = [NSMutableArray array];
NSMutableArray *tmp = [NSMutableArray array];
if (audioTrack)
[tmp addObject:audioTrack];
if (videoTrack)
[tmp addObject:videoTrack];
for (RTCMediaStreamTrack *track in tmp) {
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream addAudioTrack:(RTCAudioTrack *)track];
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream addVideoTrack:(RTCVideoTrack *)track];
}
NSString *trackId = track.trackId;
self.localTracks[trackId] = track;
NSDictionary *settings = @{};
if ([track.kind isEqualToString:@"video"]) {
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
if ([videoTrack.captureController isKindOfClass:[CaptureController class]]) {
settings = [videoTrack.captureController getSettings];
}
} else if ([track.kind isEqualToString:@"audio"]) {
settings = @{
@"deviceId": @"audio",
@"groupId": @"",
};
}
[tracks addObject:@{
@"enabled" : @(track.isEnabled),
@"id" : trackId,
@"kind" : track.kind,
@"readyState" : @"live",
@"remote" : @(NO),
@"settings" : settings
}];
}
self.localStreams[mediaStreamId] = mediaStream;
successCallback(@[ mediaStreamId, tracks ]);
#endif
}
#pragma mark - enumerateDevices
RCT_EXPORT_METHOD(enumerateDevices : (RCTResponseSenderBlock)callback) {
#if TARGET_OS_TV
callback(@[]);
#else
NSMutableArray *devices = [NSMutableArray array];
NSMutableArray *deviceTypes = [NSMutableArray array];
[deviceTypes addObjectsFromArray:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInUltraWideCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera, AVCaptureDeviceTypeBuiltInDualCamera, AVCaptureDeviceTypeBuiltInDualWideCamera, AVCaptureDeviceTypeBuiltInTripleCamera]];
if (@available(macos 14.0, ios 17.0, tvos 17.0, *)) {
[deviceTypes addObject:AVCaptureDeviceTypeExternal];
}
AVCaptureDeviceDiscoverySession *videoDevicesSession =
[AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:deviceTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
for (AVCaptureDevice *device in videoDevicesSession.devices) {
if (device.uniqueID == nil) {
continue;
}
NSString *position = @"unknown";
if (device.position == AVCaptureDevicePositionBack) {
position = @"environment";
} else if (device.position == AVCaptureDevicePositionFront) {
position = @"front";
}
NSString *label = @"Unknown video device";
if (device.localizedName != nil) {
label = device.localizedName;
}
[devices addObject:@{
@"facing" : position,
@"deviceId" : device.uniqueID,
@"groupId" : @"",
@"label" : label,
@"kind" : @"videoinput",
}];
}
AVCaptureDeviceDiscoverySession *audioDevicesSession =
[AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInMicrophone ]
mediaType:AVMediaTypeAudio
position:AVCaptureDevicePositionUnspecified];
for (AVCaptureDevice *device in audioDevicesSession.devices) {
if (device.uniqueID == nil) {
continue;
}
NSString *label = @"Unknown audio device";
if (device.localizedName != nil) {
label = device.localizedName;
}
[devices addObject:@{
@"deviceId" : device.uniqueID,
@"groupId" : @"",
@"label" : label,
@"kind" : @"audioinput",
}];
}
callback(@[ devices ]);
#endif
}
#pragma mark - Local Video Track Dimension Detection
- (void)addLocalVideoTrackDimensionDetection:(RTCVideoTrack *)videoTrack {
if (!videoTrack) {
return;
}
// Create a dimension detector for this local track
VideoDimensionDetector *detector = [[VideoDimensionDetector alloc] initWith:@(-1) // -1 for local tracks
trackId:videoTrack.trackId
webRTCModule:self];
// Store the detector using associated objects on the track itself
objc_setAssociatedObject(videoTrack, @selector(addLocalVideoTrackDimensionDetection:), detector, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Add the detector as a renderer to the track
[videoTrack addRenderer:detector];
RCTLogTrace(@"[VideoTrackAdapter] Local dimension detector created for track %@", videoTrack.trackId);
}
- (void)removeLocalVideoTrackDimensionDetection:(RTCVideoTrack *)videoTrack {
if (!videoTrack) {
return;
}
// Get the associated detector
VideoDimensionDetector *detector = objc_getAssociatedObject(videoTrack, @selector(addLocalVideoTrackDimensionDetection:));
if (detector) {
[videoTrack removeRenderer:detector];
[detector dispose];
objc_setAssociatedObject(videoTrack, @selector(addLocalVideoTrackDimensionDetection:), nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
RCTLogTrace(@"[VideoTrackAdapter] Local dimension detector removed for track %@", videoTrack.trackId);
}
}
#pragma mark - Other stream related APIs
RCT_EXPORT_METHOD(mediaStreamCreate : (nonnull NSString *)streamID) {
RTCMediaStream *mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:streamID];
self.localStreams[streamID] = mediaStream;
}
RCT_EXPORT_METHOD(mediaStreamAddTrack
: (nonnull NSString *)streamID
: (nonnull NSNumber *)pcId
: (nonnull NSString *)trackID) {
RTCMediaStream *mediaStream = self.localStreams[streamID];
if (mediaStream == nil) {
return;
}
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];
if (track == nil) {
return;
}
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream addAudioTrack:(RTCAudioTrack *)track];
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream addVideoTrack:(RTCVideoTrack *)track];
}
}
RCT_EXPORT_METHOD(mediaStreamRemoveTrack
: (nonnull NSString *)streamID
: (nonnull NSNumber *)pcId
: (nonnull NSString *)trackID) {
RTCMediaStream *mediaStream = self.localStreams[streamID];
if (mediaStream == nil) {
return;
}
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];
if (track == nil) {
return;
}
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream removeAudioTrack:(RTCAudioTrack *)track];
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream removeVideoTrack:(RTCVideoTrack *)track];
}
}
RCT_EXPORT_METHOD(mediaStreamRelease : (nonnull NSString *)streamID) {
RTCMediaStream *stream = self.localStreams[streamID];
if (stream) {
[self.localStreams removeObjectForKey:streamID];
}
}
RCT_EXPORT_METHOD(mediaStreamTrackRelease : (nonnull NSString *)trackID) {
#if TARGET_OS_TV
return;
#else
RTCMediaStreamTrack *track = self.localTracks[trackID];
if (track) {
// Clean up dimension detection for local video tracks
if ([track.kind isEqualToString:@"video"]) {
[self removeLocalVideoTrackDimensionDetection:(RTCVideoTrack *)track];
}
track.isEnabled = NO;
[track.captureController stopCapture];
[self.localTracks removeObjectForKey:trackID];
}
#endif
}
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(mediaStreamTrackClone : (nonnull NSString *)trackID) {
#if TARGET_OS_TV
return;
#else
RTCMediaStreamTrack *originalTrack = self.localTracks[trackID];
if (originalTrack) {
NSString *trackUUID = [[NSUUID UUID] UUIDString];
if ([originalTrack.kind isEqualToString:@"audio"]) {
RTCAudioTrack *audioTrack = [self.peerConnectionFactory audioTrackWithTrackId:trackUUID];
audioTrack.isEnabled = originalTrack.isEnabled;
[self.localTracks setObject:audioTrack forKey:trackUUID];
for (NSString* streamId in self.localStreams) {
RTCMediaStream* stream = [self.localStreams objectForKey:streamId];
for (RTCAudioTrack* track in stream.audioTracks) {
if ([trackID isEqualToString:track.trackId]) {
[stream addAudioTrack:audioTrack];
}
}
}
} else {
RTCVideoTrack *originalVideoTrack = (RTCVideoTrack *)originalTrack;
RTCVideoSource *videoSource = originalVideoTrack.source;
RTCVideoTrack *videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource trackId:trackUUID];
videoTrack.isEnabled = originalTrack.isEnabled;
// Add dimension detection for cloned local video tracks
[self addLocalVideoTrackDimensionDetection:videoTrack];
[self.localTracks setObject:videoTrack forKey:trackUUID];
for (NSString* streamId in self.localStreams) {
RTCMediaStream* stream = [self.localStreams objectForKey:streamId];
for (RTCVideoTrack* track in stream.videoTracks) {
if ([trackID isEqualToString:track.trackId]) {
[stream addVideoTrack:videoTrack];
}
}
}
}
return trackUUID;
}
return @"";
#endif
}
RCT_EXPORT_METHOD(mediaStreamTrackSetEnabled : (nonnull NSNumber *)pcId : (nonnull NSString *)trackID : (BOOL)enabled) {
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];
if (track == nil) {
return;
}
track.isEnabled = enabled;
#if !TARGET_OS_TV
if (track.captureController) { // It could be a remote track!
if (enabled) {
[track.captureController startCapture];
} else {
[track.captureController stopCapture];
}
}
#endif
}
RCT_EXPORT_METHOD(mediaStreamTrackApplyConstraints : (nonnull NSString *)trackID : (NSDictionary *)constraints : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) {
#if TARGET_OS_TV
reject(@"unsupported_platform", @"tvOS is not supported", nil);
return;
#else
RTCMediaStreamTrack *track = self.localTracks[trackID];
if (track) {
if ([track.kind isEqualToString:@"video"]) {
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
if ([videoTrack.captureController isKindOfClass:[CaptureController class]]) {
CaptureController *vcc = (CaptureController *)videoTrack.captureController;
NSError* error = nil;
[vcc applyConstraints:constraints error:&error];
if (error) {
reject(@"E_INVALID", error.localizedDescription, error);
} else {
resolve([vcc getSettings]);
}
}
} else {
RCTLogWarn(@"mediaStreamTrackApplyConstraints() track is not video");
reject(@"E_INVALID", @"Can't apply constraints on audio tracks", nil);
}
} else {
RCTLogWarn(@"mediaStreamTrackApplyConstraints() track is null");
reject(@"E_INVALID", @"Could not get track", nil);
}
#endif
}
RCT_EXPORT_METHOD(mediaStreamTrackSetVolume : (nonnull NSNumber *)pcId : (nonnull NSString *)trackID : (double)volume) {
RTCMediaStreamTrack *track = [self trackForId:trackID pcId:pcId];
if (track && [track.kind isEqualToString:@"audio"]) {
RTCAudioTrack *audioTrack = (RTCAudioTrack *)track;
audioTrack.source.volume = volume;
}
}
RCT_EXPORT_METHOD(mediaStreamTrackSetVideoEffects:(nonnull NSString *)trackID names:(nonnull NSArray<NSString *> *)names)
{
RTCMediaStreamTrack *track = self.localTracks[trackID];
if (track) {
RTCVideoTrack *videoTrack = (RTCVideoTrack *)track;
RTCVideoSource *videoSource = videoTrack.source;
NSMutableArray *processors = [[NSMutableArray alloc] init];
for (NSString *name in names) {
NSObject<VideoFrameProcessorDelegate> *processor = [ProcessorProvider getProcessor:name];
if (processor != nil) {
[processors addObject:processor];
}
}
self.videoEffectProcessor = [[VideoEffectProcessor alloc] initWithProcessors:processors
videoSource:videoSource];
VideoCaptureController *vcc = (VideoCaptureController *)videoTrack.captureController;
RTCVideoCapturer *capturer = vcc.capturer;
capturer.delegate = self.videoEffectProcessor;
}
}
#pragma mark - Helpers
- (RTCMediaStreamTrack *)trackForId:(nonnull NSString *)trackId pcId:(nonnull NSNumber *)pcId {
if ([pcId isEqualToNumber:[NSNumber numberWithInt:-1]]) {
return self.localTracks[trackId];
}
RTCPeerConnection *peerConnection = self.peerConnections[pcId];
if (peerConnection == nil) {
return nil;
}
return peerConnection.remoteTracks[trackId];
}
- (void)ensureAudioSessionWithRecording {
RTCAudioSession* session = [RTCAudioSession sharedInstance];
// we also need to set default WebRTC audio configuration, since it may be activated after
// this method is called
RTCAudioSessionConfiguration* config = [RTCAudioSessionConfiguration webRTCConfiguration];
// require audio session to be either PlayAndRecord or MultiRoute
if (session.category != AVAudioSessionCategoryPlayAndRecord) {
[session lockForConfiguration];
config.category = AVAudioSessionCategoryPlayAndRecord;
config.categoryOptions =
AVAudioSessionCategoryOptionAllowBluetooth|
AVAudioSessionCategoryOptionDefaultToSpeaker;
config.mode = AVAudioSessionModeVideoChat;
NSError* error = nil;
bool success = [session setCategory:config.category withOptions:config.categoryOptions error:&error];
if (!success) {
NSLog(@"ensureAudioSessionWithRecording: setCategory failed due to: %@", [error localizedDescription]);
}
success = [session setMode:config.mode error:&error];
if (!success) {
NSLog(@"ensureAudioSessionWithRecording: Error setting category: %@", [error localizedDescription]);
}
[session unlockForConfiguration];
}
}
@end