forked from flutter-webrtc/flutter-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFlutterRTCMediaStream.m
More file actions
1098 lines (973 loc) · 41.8 KB
/
FlutterRTCMediaStream.m
File metadata and controls
1098 lines (973 loc) · 41.8 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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import <objc/runtime.h>
#import "AudioUtils.h"
#import "CameraUtils.h"
#import "FlutterRTCFrameCapturer.h"
#import "FlutterRTCMediaStream.h"
#import "FlutterRTCPeerConnection.h"
#import "VideoProcessingAdapter.h"
#import "LocalVideoTrack.h"
#import "LocalAudioTrack.h"
#import "AVKit/AVKit.h"
@implementation RTCMediaStreamTrack (Flutter)
- (id)settings {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setSettings:(id)settings {
objc_setAssociatedObject(self, @selector(settings), settings, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@implementation AVCaptureDevice (Flutter)
- (NSString*)positionString {
switch (self.position) {
case AVCaptureDevicePositionUnspecified:
return @"unspecified";
case AVCaptureDevicePositionBack:
return @"back";
case AVCaptureDevicePositionFront:
return @"front";
}
return nil;
}
@end
@implementation FlutterWebRTCPlugin (RTCMediaStream)
/**
* {@link https://www.w3.org/TR/mediacapture-streams/#navigatorusermediaerrorcallback}
*/
typedef void (^NavigatorUserMediaErrorCallback)(NSString* errorType, NSString* errorMessage);
/**
* {@link https://www.w3.org/TR/mediacapture-streams/#navigatorusermediasuccesscallback}
*/
typedef void (^NavigatorUserMediaSuccessCallback)(RTCMediaStream* mediaStream);
- (NSDictionary*)defaultVideoConstraints {
return @{@"minWidth" : @"1280", @"minHeight" : @"720", @"minFrameRate" : @"30"};
}
- (NSDictionary*)defaultAudioConstraints {
return @{};
}
- (RTCMediaConstraints*)defaultMediaStreamConstraints {
RTCMediaConstraints* constraints =
[[RTCMediaConstraints alloc] initWithMandatoryConstraints:[self defaultVideoConstraints]
optionalConstraints:nil];
return constraints;
}
- (NSArray<AVCaptureDevice*> *) captureDevices {
if (@available(iOS 13.0, macOS 10.15, macCatalyst 14.0, tvOS 17.0, *)) {
NSArray<AVCaptureDeviceType> *deviceTypes = @[
#if TARGET_OS_IPHONE
AVCaptureDeviceTypeBuiltInTripleCamera,
AVCaptureDeviceTypeBuiltInDualCamera,
AVCaptureDeviceTypeBuiltInDualWideCamera,
AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeBuiltInTelephotoCamera,
AVCaptureDeviceTypeBuiltInUltraWideCamera,
#else
AVCaptureDeviceTypeBuiltInWideAngleCamera,
#endif
];
#if !defined(TARGET_OS_IPHONE)
if (@available(macOS 13.0, *)) {
deviceTypes = [deviceTypes arrayByAddingObject:AVCaptureDeviceTypeDeskViewCamera];
}
#endif
if (@available(iOS 17.0, macOS 14.0, tvOS 17.0, *)) {
deviceTypes = [deviceTypes arrayByAddingObjectsFromArray: @[
AVCaptureDeviceTypeContinuityCamera,
AVCaptureDeviceTypeExternal,
]];
}
return [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:deviceTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified].devices;
}
return @[];
}
/**
* Initializes a new {@link RTCAudioTrack} which satisfies specific constraints,
* adds it to a specific {@link RTCMediaStream}, and reports success to a
* specific callback. Implements the audio-specific counterpart of the
* {@code getUserMedia()} algorithm.
*
* @param constraints The {@code MediaStreamConstraints} which the new
* {@code RTCAudioTrack} instance is to satisfy.
* @param successCallback The {@link NavigatorUserMediaSuccessCallback} to which
* success is to be reported.
* @param errorCallback The {@link NavigatorUserMediaErrorCallback} to which
* failure is to be reported.
* @param mediaStream The {@link RTCMediaStream} which is being initialized as
* part of the execution of the {@code getUserMedia()} algorithm, to which a
* new {@code RTCAudioTrack} is to be added, and which is to be reported to
* {@code successCallback} upon success.
*/
- (void)getUserAudio:(NSDictionary*)constraints
successCallback:(NavigatorUserMediaSuccessCallback)successCallback
errorCallback:(NavigatorUserMediaErrorCallback)errorCallback
mediaStream:(RTCMediaStream*)mediaStream {
id audioConstraints = constraints[@"audio"];
NSString* audioDeviceId = @"";
RTCMediaConstraints *rtcConstraints;
if ([audioConstraints isKindOfClass:[NSDictionary class]]) {
// constraints.audio.deviceId
NSString* deviceId = audioConstraints[@"deviceId"];
if (deviceId) {
audioDeviceId = deviceId;
}
rtcConstraints = [self parseMediaConstraints:audioConstraints];
// constraints.audio.optional.sourceId
id optionalConstraints = audioConstraints[@"optional"];
if (optionalConstraints && [optionalConstraints isKindOfClass:[NSArray class]] &&
!deviceId) {
NSArray* options = optionalConstraints;
for (id item in options) {
if ([item isKindOfClass:[NSDictionary class]]) {
NSString* sourceId = ((NSDictionary*)item)[@"sourceId"];
if (sourceId) {
audioDeviceId = sourceId;
}
}
}
}
} else {
rtcConstraints = [self parseMediaConstraints:[self defaultAudioConstraints]];
}
#if !defined(TARGET_OS_IPHONE)
if (audioDeviceId != nil) {
[self selectAudioInput:audioDeviceId result:nil];
}
#endif
NSString* trackId = [[NSUUID UUID] UUIDString];
RTCAudioSource *audioSource = [self.peerConnectionFactory audioSourceWithConstraints:rtcConstraints];
RTCAudioTrack* audioTrack = [self.peerConnectionFactory audioTrackWithSource:audioSource trackId:trackId];
LocalAudioTrack *localAudioTrack = [[LocalAudioTrack alloc] initWithTrack:audioTrack];
audioTrack.settings = @{
@"deviceId" : audioDeviceId,
@"kind" : @"audioinput",
@"autoGainControl" : @YES,
@"echoCancellation" : @YES,
@"noiseSuppression" : @YES,
@"channelCount" : @1,
@"latency" : @0,
};
[mediaStream addAudioTrack:audioTrack];
[self.localTracks setObject:localAudioTrack forKey:trackId];
[self ensureAudioSession];
successCallback(mediaStream);
}
// TODO: Use RCTConvert for constraints ...
- (void)getUserMedia:(NSDictionary*)constraints result:(FlutterResult)result {
// Initialize RTCMediaStream with a unique label in order to allow multiple
// RTCMediaStream instances initialized by multiple getUserMedia calls to be
// added to 1 RTCPeerConnection instance. As suggested by
// https://www.w3.org/TR/mediacapture-streams/#mediastream to be a good
// practice, use a UUID (conforming to RFC4122).
NSString* mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream* mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
[self getUserMedia:constraints
successCallback:^(RTCMediaStream* mediaStream) {
NSString* mediaStreamId = mediaStream.streamId;
NSMutableArray* audioTracks = [NSMutableArray array];
NSMutableArray* videoTracks = [NSMutableArray array];
for (RTCAudioTrack* track in mediaStream.audioTracks) {
[audioTracks addObject:@{
@"id" : track.trackId,
@"kind" : track.kind,
@"label" : track.trackId,
@"enabled" : @(track.isEnabled),
@"remote" : @(YES),
@"readyState" : @"live",
@"settings" : track.settings
}];
}
for (RTCVideoTrack* track in mediaStream.videoTracks) {
[videoTracks addObject:@{
@"id" : track.trackId,
@"kind" : track.kind,
@"label" : track.trackId,
@"enabled" : @(track.isEnabled),
@"remote" : @(YES),
@"readyState" : @"live",
@"settings" : track.settings
}];
}
self.localStreams[mediaStreamId] = mediaStream;
result(@{
@"streamId" : mediaStreamId,
@"audioTracks" : audioTracks,
@"videoTracks" : videoTracks
});
}
errorCallback:^(NSString* errorType, NSString* errorMessage) {
result([FlutterError errorWithCode:[NSString stringWithFormat:@"Error %@", errorType]
message:errorMessage
details:nil]);
}
mediaStream:mediaStream];
}
/**
* Initializes a new {@link RTCAudioTrack} or a new {@link RTCVideoTrack} which
* satisfies specific constraints and adds it to a specific
* {@link RTCMediaStream} if the specified {@code mediaStream} contains no track
* of the respective media type and the specified {@code constraints} specify
* that a track of the respective media type is required; otherwise, reports
* success for the specified {@code mediaStream} to a specific
* {@link NavigatorUserMediaSuccessCallback}. In other words, implements a media
* type-specific iteration of or successfully concludes the
* {@code getUserMedia()} algorithm. The method will be recursively invoked to
* conclude the whole {@code getUserMedia()} algorithm either with (successful)
* satisfaction of the specified {@code constraints} or with failure.
*
* @param constraints The {@code MediaStreamConstraints} which specifies the
* requested media types and which the new {@code RTCAudioTrack} or
* {@code RTCVideoTrack} instance is to satisfy.
* @param successCallback The {@link NavigatorUserMediaSuccessCallback} to which
* success is to be reported.
* @param errorCallback The {@link NavigatorUserMediaErrorCallback} to which
* failure is to be reported.
* @param mediaStream The {@link RTCMediaStream} which is being initialized as
* part of the execution of the {@code getUserMedia()} algorithm.
*/
- (void)getUserMedia:(NSDictionary*)constraints
successCallback:(NavigatorUserMediaSuccessCallback)successCallback
errorCallback:(NavigatorUserMediaErrorCallback)errorCallback
mediaStream:(RTCMediaStream*)mediaStream {
// If mediaStream contains no audioTracks and the constraints request such a
// track, then run an iteration of the getUserMedia() algorithm to obtain
// local audio content.
if (mediaStream.audioTracks.count == 0) {
// constraints.audio
id audioConstraints = constraints[@"audio"];
BOOL constraintsIsDictionary = [audioConstraints isKindOfClass:[NSDictionary class]];
if (audioConstraints && (constraintsIsDictionary || [audioConstraints boolValue])) {
[self requestAccessForMediaType:AVMediaTypeAudio
constraints:constraints
successCallback:successCallback
errorCallback:errorCallback
mediaStream:mediaStream];
return;
}
}
// If mediaStream contains no videoTracks and the constraints request such a
// track, then run an iteration of the getUserMedia() algorithm to obtain
// local video content.
if (mediaStream.videoTracks.count == 0) {
// constraints.video
id videoConstraints = constraints[@"video"];
if (videoConstraints) {
BOOL requestAccessForVideo = [videoConstraints isKindOfClass:[NSNumber class]]
? [videoConstraints boolValue]
: [videoConstraints isKindOfClass:[NSDictionary class]];
#if !TARGET_IPHONE_SIMULATOR
if (requestAccessForVideo) {
[self requestAccessForMediaType:AVMediaTypeVideo
constraints:constraints
successCallback:successCallback
errorCallback:errorCallback
mediaStream:mediaStream];
return;
}
#endif
}
}
// There are audioTracks and/or videoTracks in mediaStream as requested by
// constraints so the getUserMedia() is to conclude with success.
successCallback(mediaStream);
}
- (int)getConstrainInt:(NSDictionary*)constraints forKey:(NSString*)key {
if (![constraints isKindOfClass:[NSDictionary class]]) {
return 0;
}
id constraint = constraints[key];
if ([constraint isKindOfClass:[NSNumber class]]) {
return [constraint intValue];
} else if ([constraint isKindOfClass:[NSString class]]) {
int possibleValue = [constraint intValue];
if (possibleValue != 0) {
return possibleValue;
}
} else if ([constraint isKindOfClass:[NSDictionary class]]) {
id idealConstraint = constraint[@"ideal"];
if ([idealConstraint isKindOfClass:[NSString class]]) {
int possibleValue = [idealConstraint intValue];
if (possibleValue != 0) {
return possibleValue;
}
}
}
return 0;
}
- (RTCMediaStreamTrack*)cloneTrack:(nonnull NSString*)trackId {
NSString* newTrackId = [[NSUUID UUID] UUIDString];
RTCMediaStreamTrack *originalTrack = [self trackForId:trackId peerConnectionId: nil];
LocalVideoTrack* originalLocalTrack = self.localTracks[trackId];
if (originalTrack != nil && [originalTrack.kind isEqualToString:@"audio"]) {
RTCAudioTrack* originalAudioTrack = (RTCAudioTrack *)originalTrack;
RTCAudioSource* originalAudioSource = originalAudioTrack.source;
RTCAudioTrack* audioTrack = [self.peerConnectionFactory audioTrackWithSource:originalAudioSource trackId:newTrackId];
LocalAudioTrack *localAudioTrack = [[LocalAudioTrack alloc] initWithTrack:audioTrack];
audioTrack.settings = originalAudioTrack.settings;
[self.localTracks setObject:localAudioTrack forKey:newTrackId];
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];
}
}
}
return audioTrack;
} else if (originalTrack != nil && [originalTrack.kind isEqualToString:@"video"]) {
RTCVideoTrack *originalVideoTrack = (RTCVideoTrack *)originalTrack;
RTCVideoSource *videoSource = originalVideoTrack.source;
RTCVideoTrack* videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource
trackId:newTrackId];
LocalVideoTrack *localVideoTrack = [[LocalVideoTrack alloc] initWithTrack:videoTrack
videoProcessing:originalLocalTrack.processing];
videoTrack.settings = originalVideoTrack.settings;
[self.localTracks setObject:localVideoTrack forKey:newTrackId];
for (NSString* streamId in self.localStreams) {
RTCMediaStream* stream = [self.localStreams objectForKey:streamId];
for (RTCVideoTrack* track in stream.videoTracks) {
if ([trackId isEqualToString:trackId]) {
[stream addVideoTrack:videoTrack];
}
}
}
return videoTrack;
}
return originalTrack;
}
/**
* Initializes a new {@link RTCVideoTrack} which satisfies specific constraints,
* adds it to a specific {@link RTCMediaStream}, and reports success to a
* specific callback. Implements the video-specific counterpart of the
* {@code getUserMedia()} algorithm.
*
* @param constraints The {@code MediaStreamConstraints} which the new
* {@code RTCVideoTrack} instance is to satisfy.
* @param successCallback The {@link NavigatorUserMediaSuccessCallback} to which
* success is to be reported.
* @param errorCallback The {@link NavigatorUserMediaErrorCallback} to which
* failure is to be reported.
* @param mediaStream The {@link RTCMediaStream} which is being initialized as
* part of the execution of the {@code getUserMedia()} algorithm, to which a
* new {@code RTCVideoTrack} is to be added, and which is to be reported to
* {@code successCallback} upon success.
*/
- (void)getUserVideo:(NSDictionary*)constraints
successCallback:(NavigatorUserMediaSuccessCallback)successCallback
errorCallback:(NavigatorUserMediaErrorCallback)errorCallback
mediaStream:(RTCMediaStream*)mediaStream {
id videoConstraints = constraints[@"video"];
AVCaptureDevice* videoDevice;
NSString* videoDeviceId = nil;
NSString* facingMode = nil;
NSArray<AVCaptureDevice*>* captureDevices = [self captureDevices];
if ([videoConstraints isKindOfClass:[NSDictionary class]]) {
// constraints.video.deviceId
NSString* deviceId = videoConstraints[@"deviceId"];
if (deviceId) {
for (AVCaptureDevice *device in captureDevices) {
if( [deviceId isEqualToString:device.uniqueID]) {
videoDevice = device;
videoDeviceId = deviceId;
}
}
}
// constraints.video.optional
id optionalVideoConstraints = videoConstraints[@"optional"];
if (optionalVideoConstraints && [optionalVideoConstraints isKindOfClass:[NSArray class]] &&
!videoDevice) {
NSArray* options = optionalVideoConstraints;
for (id item in options) {
if ([item isKindOfClass:[NSDictionary class]]) {
NSString* sourceId = ((NSDictionary*)item)[@"sourceId"];
if (sourceId) {
for (AVCaptureDevice *device in captureDevices) {
if( [sourceId isEqualToString:device.uniqueID]) {
videoDevice = device;
videoDeviceId = sourceId;
}
}
if (videoDevice) {
break;
}
}
}
}
}
if (!videoDevice) {
// constraints.video.facingMode
// https://www.w3.org/TR/mediacapture-streams/#def-constraint-facingMode
facingMode = videoConstraints[@"facingMode"];
if (facingMode && [facingMode isKindOfClass:[NSString class]]) {
AVCaptureDevicePosition position;
if ([facingMode isEqualToString:@"environment"]) {
self._usingFrontCamera = NO;
position = AVCaptureDevicePositionBack;
} else if ([facingMode isEqualToString:@"user"]) {
self._usingFrontCamera = YES;
position = AVCaptureDevicePositionFront;
} else {
// If the specified facingMode value is not supported, fall back to
// the default video device.
self._usingFrontCamera = NO;
position = AVCaptureDevicePositionUnspecified;
}
videoDevice = [self findDeviceForPosition:position];
}
}
}
if ([videoConstraints isKindOfClass:[NSNumber class]]) {
videoConstraints = @{@"mandatory": [self defaultVideoConstraints]};
}
NSInteger targetWidth = 0;
NSInteger targetHeight = 0;
NSInteger targetFps = 0;
if (!videoDevice) {
videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
int possibleWidth = [self getConstrainInt:videoConstraints forKey:@"width"];
if (possibleWidth != 0) {
targetWidth = possibleWidth;
}
int possibleHeight = [self getConstrainInt:videoConstraints forKey:@"height"];
if (possibleHeight != 0) {
targetHeight = possibleHeight;
}
int possibleFps = [self getConstrainInt:videoConstraints forKey:@"frameRate"];
if (possibleFps != 0) {
targetFps = possibleFps;
}
id mandatory =
[videoConstraints isKindOfClass:[NSDictionary class]] ? videoConstraints[@"mandatory"] : nil;
// constraints.video.mandatory
if (mandatory && [mandatory isKindOfClass:[NSDictionary class]]) {
id widthConstraint = mandatory[@"minWidth"];
if ([widthConstraint isKindOfClass:[NSString class]] ||
[widthConstraint isKindOfClass:[NSNumber class]]) {
int possibleWidth = [widthConstraint intValue];
if (possibleWidth != 0) {
targetWidth = possibleWidth;
}
}
id heightConstraint = mandatory[@"minHeight"];
if ([heightConstraint isKindOfClass:[NSString class]] ||
[heightConstraint isKindOfClass:[NSNumber class]]) {
int possibleHeight = [heightConstraint intValue];
if (possibleHeight != 0) {
targetHeight = possibleHeight;
}
}
id fpsConstraint = mandatory[@"minFrameRate"];
if ([fpsConstraint isKindOfClass:[NSString class]] ||
[fpsConstraint isKindOfClass:[NSNumber class]]) {
int possibleFps = [fpsConstraint intValue];
if (possibleFps != 0) {
targetFps = possibleFps;
}
}
}
if (videoDevice) {
RTCVideoSource* videoSource = [self.peerConnectionFactory videoSource];
#if TARGET_OS_OSX
if (self.videoCapturer) {
[self.videoCapturer stopCapture];
}
#endif
VideoProcessingAdapter *videoProcessingAdapter = [[VideoProcessingAdapter alloc] initWithRTCVideoSource:videoSource];
self.videoCapturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:videoProcessingAdapter];
AVCaptureDeviceFormat* selectedFormat = [self selectFormatForDevice:videoDevice
targetWidth:targetWidth
targetHeight:targetHeight];
CMVideoDimensions selectedDimension = CMVideoFormatDescriptionGetDimensions(selectedFormat.formatDescription);
NSInteger selectedWidth = (NSInteger) selectedDimension.width;
NSInteger selectedHeight = (NSInteger) selectedDimension.height;
NSInteger selectedFps = [self selectFpsForFormat:selectedFormat targetFps:targetFps];
self._lastTargetFps = selectedFps;
self._lastTargetWidth = targetWidth;
self._lastTargetHeight = targetHeight;
NSLog(@"target format %ldx%ld, targetFps: %ld, selected format: %ldx%ld, selected fps %ld", targetWidth, targetHeight, targetFps, selectedWidth, selectedHeight, selectedFps);
if ([videoDevice lockForConfiguration:NULL]) {
@try {
videoDevice.activeVideoMaxFrameDuration = CMTimeMake(1, (int32_t)selectedFps);
videoDevice.activeVideoMinFrameDuration = CMTimeMake(1, (int32_t)selectedFps);
} @catch (NSException* exception) {
NSLog(@"Failed to set active frame rate!\n User info:%@", exception.userInfo);
}
[videoDevice unlockForConfiguration];
}
[self.videoCapturer startCaptureWithDevice:videoDevice
format:selectedFormat
fps:selectedFps
completionHandler:^(NSError* error) {
if (error) {
NSLog(@"Start capture error: %@", [error localizedDescription]);
}
}];
NSString* trackUUID = [[NSUUID UUID] UUIDString];
RTCVideoTrack* videoTrack = [self.peerConnectionFactory videoTrackWithSource:videoSource
trackId:trackUUID];
LocalVideoTrack *localVideoTrack = [[LocalVideoTrack alloc] initWithTrack:videoTrack videoProcessing:videoProcessingAdapter];
__weak RTCCameraVideoCapturer* capturer = self.videoCapturer;
self.videoCapturerStopHandlers[videoTrack.trackId] = ^(CompletionHandler handler) {
NSLog(@"Stop video capturer, trackID %@", videoTrack.trackId);
[capturer stopCaptureWithCompletionHandler:handler];
};
if (!videoDeviceId) {
videoDeviceId = videoDevice.uniqueID;
}
if (!facingMode) {
facingMode = videoDevice.position == AVCaptureDevicePositionBack ? @"environment"
: videoDevice.position == AVCaptureDevicePositionFront ? @"user"
: @"unspecified";
}
videoTrack.settings = @{
@"deviceId" : videoDeviceId,
@"kind" : @"videoinput",
@"width" : [NSNumber numberWithInteger:selectedWidth],
@"height" : [NSNumber numberWithInteger:selectedHeight],
@"frameRate" : [NSNumber numberWithInteger:selectedFps],
@"facingMode" : facingMode,
};
[mediaStream addVideoTrack:videoTrack];
[self.localTracks setObject:localVideoTrack forKey:trackUUID];
successCallback(mediaStream);
} else {
// According to step 6.2.3 of the getUserMedia() algorithm, if there is no
// source, fail with a new OverconstrainedError.
errorCallback(@"OverconstrainedError", /* errorMessage */ nil);
}
}
- (void)mediaStreamRelease:(RTCMediaStream*)stream {
if (stream) {
for (RTCVideoTrack* track in stream.videoTracks) {
[self.localTracks removeObjectForKey:track.trackId];
}
for (RTCAudioTrack* track in stream.audioTracks) {
[self.localTracks removeObjectForKey:track.trackId];
}
[self.localStreams removeObjectForKey:stream.streamId];
}
}
/**
* Obtains local media content of a specific type. Requests access for the
* specified {@code mediaType} if necessary. In other words, implements a media
* type-specific iteration of the {@code getUserMedia()} algorithm.
*
* @param mediaType Either {@link AVMediaTypAudio} or {@link AVMediaTypeVideo}
* which specifies the type of the local media content to obtain.
* @param constraints The {@code MediaStreamConstraints} which are to be
* satisfied by the obtained local media content.
* @param successCallback The {@link NavigatorUserMediaSuccessCallback} to which
* success is to be reported.
* @param errorCallback The {@link NavigatorUserMediaErrorCallback} to which
* failure is to be reported.
* @param mediaStream The {@link RTCMediaStream} which is to collect the
* obtained local media content of the specified {@code mediaType}.
*/
- (void)requestAccessForMediaType:(NSString*)mediaType
constraints:(NSDictionary*)constraints
successCallback:(NavigatorUserMediaSuccessCallback)successCallback
errorCallback:(NavigatorUserMediaErrorCallback)errorCallback
mediaStream:(RTCMediaStream*)mediaStream {
// According to step 6.2.1 of the getUserMedia() algorithm, if there is no
// source, fail "with a new DOMException object whose name attribute has the
// value NotFoundError."
// XXX The following approach does not work for audio in Simulator. That is
// because audio capture is done using AVAudioSession which does not use
// AVCaptureDevice there. Anyway, Simulator will not (visually) request access
// for audio.
if (mediaType == AVMediaTypeVideo && [self captureDevices].count == 0) {
// Since successCallback and errorCallback are asynchronously invoked
// elsewhere, make sure that the invocation here is consistent.
dispatch_async(dispatch_get_main_queue(), ^{
errorCallback(@"DOMException", @"NotFoundError");
});
return;
}
#if TARGET_OS_OSX
if (@available(macOS 10.14, *)) {
#endif
[AVCaptureDevice requestAccessForMediaType:mediaType
completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
NavigatorUserMediaSuccessCallback scb =
^(RTCMediaStream* mediaStream) {
[self getUserMedia:constraints
successCallback:successCallback
errorCallback:errorCallback
mediaStream:mediaStream];
};
if (mediaType == AVMediaTypeAudio) {
[self getUserAudio:constraints
successCallback:scb
errorCallback:errorCallback
mediaStream:mediaStream];
} else if (mediaType == AVMediaTypeVideo) {
[self getUserVideo:constraints
successCallback:scb
errorCallback:errorCallback
mediaStream:mediaStream];
}
} else {
// According to step 10 Permission Failure of the getUserMedia()
// algorithm, if the user has denied permission, fail "with a new
// DOMException object whose name attribute has the value
// NotAllowedError."
errorCallback(@"DOMException", @"NotAllowedError");
}
});
}];
#if TARGET_OS_OSX
} else {
// Fallback on earlier versions
NavigatorUserMediaSuccessCallback scb = ^(RTCMediaStream* mediaStream) {
[self getUserMedia:constraints
successCallback:successCallback
errorCallback:errorCallback
mediaStream:mediaStream];
};
if (mediaType == AVMediaTypeAudio) {
[self getUserAudio:constraints
successCallback:scb
errorCallback:errorCallback
mediaStream:mediaStream];
} else if (mediaType == AVMediaTypeVideo) {
[self getUserVideo:constraints
successCallback:scb
errorCallback:errorCallback
mediaStream:mediaStream];
}
}
#endif
}
- (void)createLocalMediaStream:(FlutterResult)result {
NSString* mediaStreamId = [[NSUUID UUID] UUIDString];
RTCMediaStream* mediaStream = [self.peerConnectionFactory mediaStreamWithStreamId:mediaStreamId];
self.localStreams[mediaStreamId] = mediaStream;
result(@{@"streamId" : [mediaStream streamId]});
}
- (void)getSources:(FlutterResult)result {
NSMutableArray* sources = [NSMutableArray array];
NSArray* videoDevices = [self captureDevices];
for (AVCaptureDevice* device in videoDevices) {
[sources addObject:@{
@"facing" : device.positionString,
@"deviceId" : device.uniqueID,
@"label" : device.localizedName,
@"kind" : @"videoinput",
}];
}
#if TARGET_OS_IPHONE
RTCAudioSession* session = [RTCAudioSession sharedInstance];
for (AVAudioSessionPortDescription* port in session.session.availableInputs) {
// NSLog(@"input portName: %@, type %@", port.portName,port.portType);
[sources addObject:@{
@"deviceId" : port.UID,
@"label" : port.portName,
@"groupId" : port.portType,
@"kind" : @"audioinput",
}];
}
for (AVAudioSessionPortDescription* port in session.currentRoute.outputs) {
// NSLog(@"output portName: %@, type %@", port.portName,port.portType);
if (session.currentRoute.outputs.count == 1 && ![port.UID isEqualToString:@"Speaker"]) {
[sources addObject:@{
@"deviceId" : @"Speaker",
@"label" : @"Speaker",
@"groupId" : @"Speaker",
@"kind" : @"audiooutput",
}];
}
[sources addObject:@{
@"deviceId" : port.UID,
@"label" : port.portName,
@"groupId" : port.portType,
@"kind" : @"audiooutput",
}];
}
#endif
#if TARGET_OS_OSX
RTCAudioDeviceModule* audioDeviceModule = [self.peerConnectionFactory audioDeviceModule];
NSArray* inputDevices = [audioDeviceModule inputDevices];
for (RTCIODevice* device in inputDevices) {
[sources addObject:@{
@"deviceId" : device.deviceId,
@"label" : device.name,
@"kind" : @"audioinput",
}];
}
NSArray* outputDevices = [audioDeviceModule outputDevices];
for (RTCIODevice* device in outputDevices) {
[sources addObject:@{
@"deviceId" : device.deviceId,
@"label" : device.name,
@"kind" : @"audiooutput",
}];
}
#endif
result(@{@"sources" : sources});
}
- (void)selectAudioInput:(NSString*)deviceId result:(FlutterResult)result {
#if TARGET_OS_OSX
RTCAudioDeviceModule* audioDeviceModule = [self.peerConnectionFactory audioDeviceModule];
NSArray* inputDevices = [audioDeviceModule inputDevices];
for (RTCIODevice* device in inputDevices) {
if ([deviceId isEqualToString:device.deviceId]) {
[audioDeviceModule setInputDevice:device];
if (result)
result(nil);
return;
}
}
#endif
#if TARGET_OS_IPHONE
RTCAudioSession* session = [RTCAudioSession sharedInstance];
for (AVAudioSessionPortDescription* port in session.session.availableInputs) {
if ([port.UID isEqualToString:deviceId]) {
if (self.preferredInput != port.portType) {
self.preferredInput = port.portType;
[AudioUtils selectAudioInput:self.preferredInput];
}
break;
}
}
if (result)
result(nil);
#endif
if (result)
result([FlutterError errorWithCode:@"selectAudioInputFailed"
message:[NSString stringWithFormat:@"Error: deviceId not found!"]
details:nil]);
}
- (void)selectAudioOutput:(NSString*)deviceId result:(FlutterResult)result {
#if TARGET_OS_OSX
RTCAudioDeviceModule* audioDeviceModule = [self.peerConnectionFactory audioDeviceModule];
NSArray* outputDevices = [audioDeviceModule outputDevices];
for (RTCIODevice* device in outputDevices) {
if ([deviceId isEqualToString:device.deviceId]) {
[audioDeviceModule setOutputDevice:device];
result(nil);
return;
}
}
#endif
#if TARGET_OS_IPHONE
RTCAudioSession* session = [RTCAudioSession sharedInstance];
NSError* setCategoryError = nil;
if ([deviceId isEqualToString:@"Speaker"]) {
[session.session overrideOutputAudioPort:kAudioSessionOverrideAudioRoute_Speaker
error:&setCategoryError];
} else {
[session.session overrideOutputAudioPort:kAudioSessionOverrideAudioRoute_None
error:&setCategoryError];
}
if (setCategoryError == nil) {
result(nil);
return;
}
result([FlutterError
errorWithCode:@"selectAudioOutputFailed"
message:[NSString
stringWithFormat:@"Error: %@", [setCategoryError localizedFailureReason]]
details:nil]);
#endif
result([FlutterError errorWithCode:@"selectAudioOutputFailed"
message:[NSString stringWithFormat:@"Error: deviceId not found!"]
details:nil]);
}
- (void)triggeriOSAudioRouteSelectionUI:(FlutterResult)result {
#if TARGET_OS_IPHONE
if (@available(iOS 11.0, *)) {
AVRoutePickerView *routePicker = [[AVRoutePickerView alloc] init];
routePicker.frame = CGRectMake(0, 0, 44, 44);
// Add the route picker to a temporary window to ensure it's in the view hierarchy
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if (!window) {
// Fallback for iOS 13+ where keyWindow is deprecated
for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) {
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
window = windowScene.windows.firstObject;
break;
}
}
}
if (window) {
[window addSubview:routePicker];
// Trigger the route picker programmatically
for (UIView *view in routePicker.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
break; // Only trigger the first button found
}
}
// Remove the route picker after a short delay
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[routePicker removeFromSuperview];
});
result(nil);
} else {
result([FlutterError errorWithCode:@"NoWindowError"
message:@"Could not find a window to present the route picker"
details:nil]);
}
} else {
result([FlutterError errorWithCode:@"UnsupportedVersionError"
message:@"AVRoutePickerView is only available on iOS 11.0 or later"
details:nil]);
}
#else
// macOS doesn't support iOS audio route selection UI
result([FlutterError errorWithCode:@"UnsupportedPlatformError"
message:@"triggeriOSAudioRouteSelectionUI is only supported on iOS"
details:nil]);
#endif
}
- (void)mediaStreamTrackRelease:(RTCMediaStream*)mediaStream track:(RTCMediaStreamTrack*)track {
// what's different to mediaStreamTrackStop? only call mediaStream explicitly?
if (mediaStream && track) {
track.isEnabled = NO;
// FIXME this is called when track is removed from the MediaStream,
// but it doesn't mean it can not be added back using MediaStream.addTrack
// TODO: [self.localTracks removeObjectForKey:trackID];
if ([track.kind isEqualToString:@"audio"]) {
[mediaStream removeAudioTrack:(RTCAudioTrack*)track];
} else if ([track.kind isEqualToString:@"video"]) {
[mediaStream removeVideoTrack:(RTCVideoTrack*)track];
}
}
}
- (void)mediaStreamTrackHasTorch:(RTCMediaStreamTrack*)track result:(FlutterResult)result {
if (!self.videoCapturer) {
result(@NO);
return;
}
if (self.videoCapturer.captureSession.inputs.count == 0) {
result(@NO);
return;
}
AVCaptureDeviceInput* deviceInput = [self.videoCapturer.captureSession.inputs objectAtIndex:0];
AVCaptureDevice* device = deviceInput.device;
result(@([device isTorchModeSupported:AVCaptureTorchModeOn]));
}
- (void)mediaStreamTrackSetTorch:(RTCMediaStreamTrack*)track
torch:(BOOL)torch
result:(FlutterResult)result {
if (!self.videoCapturer) {
NSLog(@"Video capturer is null. Can't set torch");
return;
}
if (self.videoCapturer.captureSession.inputs.count == 0) {
NSLog(@"Video capturer is missing an input. Can't set torch");
return;
}
AVCaptureDeviceInput* deviceInput = [self.videoCapturer.captureSession.inputs objectAtIndex:0];
AVCaptureDevice* device = deviceInput.device;
if (![device isTorchModeSupported:AVCaptureTorchModeOn]) {
NSLog(@"Current capture device does not support torch. Can't set torch");
return;
}
NSError* error;
if ([device lockForConfiguration:&error] == NO) {
NSLog(@"Failed to aquire configuration lock. %@", error.localizedDescription);
return;
}
device.torchMode = torch ? AVCaptureTorchModeOn : AVCaptureTorchModeOff;
[device unlockForConfiguration];
result(nil);
}
- (void)mediaStreamTrackSetZoom:(RTCMediaStreamTrack*)track
zoomLevel:(double)zoomLevel
result:(FlutterResult)result {
#if TARGET_OS_OSX
NSLog(@"Not supported on macOS. Can't set zoom");
return;