2
2
#import " FlutterWebRTCPlugin.h"
3
3
#import " FlutterRTCPeerConnection.h"
4
4
#import " FlutterRTCDataChannel.h"
5
+ #import " AudioUtils.h"
5
6
6
7
#import < WebRTC/WebRTC.h>
7
8
9
+
8
10
@implementation RTCPeerConnection (Flutter)
9
11
10
12
@dynamic eventSink;
@@ -170,11 +172,11 @@ -(void) peerConnectionAddICECandidate:(RTCIceCandidate*)candidate
170
172
-(void ) peerConnectionClose : (RTCPeerConnection *)peerConnection
171
173
{
172
174
[peerConnection close ];
173
-
175
+
174
176
// Clean up peerConnection's streams and tracks
175
177
[peerConnection.remoteStreams removeAllObjects ];
176
178
[peerConnection.remoteTracks removeAllObjects ];
177
-
179
+
178
180
// Clean up peerConnection's dataChannels.
179
181
NSMutableDictionary <NSNumber *, RTCDataChannel *> *dataChannels
180
182
= peerConnection.dataChannels ;
@@ -198,17 +200,17 @@ -(void) peerConnectionGetStats:(nonnull NSString *)trackID
198
200
[peerConnection statsForTrack: track
199
201
statsOutputLevel: RTCStatsOutputLevelStandard
200
202
completionHandler: ^(NSArray <RTCLegacyStatsReport *> *reports) {
201
-
203
+
202
204
NSMutableArray *stats = [NSMutableArray array ];
203
-
205
+
204
206
for (RTCLegacyStatsReport *report in reports) {
205
207
[stats addObject: @{@" id" : report.reportId ,
206
208
@" type" : report.type ,
207
209
@" timestamp" : @(report.timestamp ),
208
210
@" values" : report.values
209
211
}];
210
212
}
211
-
213
+
212
214
result (@{@" stats" : stats});
213
215
}];
214
216
}else {
@@ -281,7 +283,7 @@ - (void)parseJavaScriptConstraints:(NSDictionary *)src
281
283
for (id srcKey in src) {
282
284
id srcValue = src[srcKey];
283
285
NSString *dstValue;
284
-
286
+
285
287
if ([srcValue isKindOfClass: [NSNumber class ]]) {
286
288
dstValue = [srcValue boolValue ] ? @" true" : @" false" ;
287
289
} else {
@@ -304,16 +306,16 @@ - (RTCMediaConstraints *)parseMediaConstraints:(NSDictionary *)constraints {
304
306
id mandatory = constraints[@" mandatory" ];
305
307
NSMutableDictionary <NSString *, NSString *> *mandatory_
306
308
= [NSMutableDictionary new ];
307
-
309
+
308
310
if ([mandatory isKindOfClass: [NSDictionary class ]]) {
309
311
[self parseJavaScriptConstraints: (NSDictionary *)mandatory
310
312
intoWebRTCConstraints: mandatory_];
311
313
}
312
-
314
+
313
315
id optional = constraints[@" optional" ];
314
316
NSMutableDictionary <NSString *, NSString *> *optional_
315
317
= [NSMutableDictionary new ];
316
-
318
+
317
319
if ([optional isKindOfClass: [NSArray class ]]) {
318
320
for (id o in (NSArray *)optional) {
319
321
if ([o isKindOfClass: [NSDictionary class ]]) {
@@ -322,7 +324,7 @@ - (RTCMediaConstraints *)parseMediaConstraints:(NSDictionary *)constraints {
322
324
}
323
325
}
324
326
}
325
-
327
+
326
328
return [[RTCMediaConstraints alloc ] initWithMandatoryConstraints: mandatory_
327
329
optionalConstraints: optional_];
328
330
}
@@ -341,11 +343,11 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeSignalingSta
341
343
342
344
-(void )peerConnection : (RTCPeerConnection *)peerConnection
343
345
mediaStream : (RTCMediaStream *)stream didAddTrack : (RTCVideoTrack*)track {
344
-
346
+
345
347
peerConnection.remoteTracks [track.trackId] = track;
346
348
NSString *streamId = stream.streamId ;
347
349
peerConnection.remoteStreams [streamId] = stream;
348
-
350
+
349
351
FlutterEventSink eventSink = peerConnection.eventSink ;
350
352
if (eventSink){
351
353
eventSink (@{
@@ -388,19 +390,25 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection didAddStream:(RTCMedi
388
390
NSMutableArray *audioTracks = [NSMutableArray array ];
389
391
NSMutableArray *videoTracks = [NSMutableArray array ];
390
392
393
+ BOOL hasAudio = NO ;
391
394
for (RTCAudioTrack *track in stream.audioTracks ) {
392
395
peerConnection.remoteTracks [track.trackId] = track;
393
396
[audioTracks addObject: @{@" id" : track.trackId , @" kind" : track.kind , @" label" : track.trackId , @" enabled" : @(track.isEnabled ), @" remote" : @(YES ), @" readyState" : @" live" }];
397
+ hasAudio = YES ;
394
398
}
395
-
399
+
396
400
for (RTCVideoTrack *track in stream.videoTracks ) {
397
401
peerConnection.remoteTracks [track.trackId] = track;
398
402
[videoTracks addObject: @{@" id" : track.trackId , @" kind" : track.kind , @" label" : track.trackId , @" enabled" : @(track.isEnabled ), @" remote" : @(YES ), @" readyState" : @" live" }];
399
403
}
400
-
404
+
401
405
NSString *streamId = stream.streamId ;
402
406
peerConnection.remoteStreams [streamId] = stream;
403
-
407
+
408
+ if (hasAudio) {
409
+ [AudioUtils ensureAudioSessionWithRecording: NO ];
410
+ }
411
+
404
412
FlutterEventSink eventSink = peerConnection.eventSink ;
405
413
if (eventSink){
406
414
eventSink (@{
@@ -419,15 +427,15 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveStream:(RTCM
419
427
NSLog (@" didRemoveStream - more than one stream entry found for stream instance with id: %@ " , stream.streamId );
420
428
}
421
429
NSString *streamId = stream.streamId ;
422
-
430
+
423
431
for (RTCVideoTrack *track in stream.videoTracks ) {
424
432
[peerConnection.remoteTracks removeObjectForKey: track.trackId];
425
433
}
426
434
for (RTCAudioTrack *track in stream.audioTracks ) {
427
435
[peerConnection.remoteTracks removeObjectForKey: track.trackId];
428
436
}
429
437
[peerConnection.remoteStreams removeObjectForKey: streamId];
430
-
438
+
431
439
FlutterEventSink eventSink = peerConnection.eventSink ;
432
440
if (eventSink){
433
441
eventSink (@{
@@ -483,15 +491,15 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection didOpenDataChannel:(RT
483
491
dataChannel.peerConnectionId = peerConnection.flutterId ;
484
492
dataChannel.delegate = self;
485
493
peerConnection.dataChannels [dataChannelId] = dataChannel;
486
-
494
+
487
495
FlutterEventChannel *eventChannel = [FlutterEventChannel
488
496
eventChannelWithName: [NSString stringWithFormat: @" FlutterWebRTC/dataChannelEvent%1$@ %2$d " , peerConnection.flutterId, dataChannel.channelId]
489
497
binaryMessenger: self .messenger];
490
-
498
+
491
499
dataChannel.eventChannel = eventChannel;
492
500
dataChannel.flutterChannelId = dataChannelId;
493
501
[eventChannel setStreamHandler: dataChannel];
494
-
502
+
495
503
FlutterEventSink eventSink = peerConnection.eventSink ;
496
504
if (eventSink){
497
505
eventSink (@{
@@ -516,7 +524,7 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection
516
524
517
525
- (void )peerConnection : (RTCPeerConnection *)peerConnection
518
526
didStartReceivingOnTransceiver : (RTCRtpTransceiver *)transceiver {
519
-
527
+
520
528
}
521
529
522
530
/* * Called when a receiver and its track are created. */
@@ -537,7 +545,7 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection
537
545
@" receiver" : [self receiverToMap: rtpReceiver],
538
546
@" streams" : streams,
539
547
}];
540
-
548
+
541
549
if (peerConnection.configuration .sdpSemantics == RTCSdpSemanticsUnifiedPlan) {
542
550
for (RTCRtpTransceiver *transceiver in peerConnection.transceivers ) {
543
551
if (transceiver.receiver != nil && [transceiver.receiver.receiverId isEqualToString: rtpReceiver.receiverId]) {
@@ -550,15 +558,18 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection
550
558
if (mediaStreams.count > 0 ) {
551
559
peerConnection.remoteStreams [mediaStreams[0 ].streamId] = mediaStreams[0 ];
552
560
}
553
-
561
+
562
+ if ([rtpReceiver.track.kind isEqualToString: @" audio" ]) {
563
+ [AudioUtils ensureAudioSessionWithRecording: NO ];
564
+ }
554
565
eventSink (event);
555
566
}
556
567
}
557
568
558
569
/* * Called when the receiver and its track are removed. */
559
570
- (void )peerConnection : (RTCPeerConnection *)peerConnection
560
571
didRemoveReceiver : (RTCRtpReceiver *)rtpReceiver {
561
-
572
+
562
573
}
563
574
564
575
/* * Called when the selected ICE candidate pair is changed. */
@@ -567,7 +578,7 @@ - (void)peerConnection:(RTCPeerConnection *)peerConnection
567
578
remoteCandidate : (RTCIceCandidate *)remote
568
579
lastReceivedMs : (int )lastDataReceivedMs
569
580
changeReason : (NSString *)reason {
570
-
581
+
571
582
FlutterEventSink eventSink = peerConnection.eventSink ;
572
583
if (eventSink){
573
584
eventSink (@{
0 commit comments