8000 fix: send message on non-platform thread. (#1458) · liliBestCoder/flutter-webrtc@0519f1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0519f1f

Browse files
authored
fix: send message on non-platform thread. (flutter-webrtc#1458)
1 parent 0d3ddb2 commit 0519f1f

File tree

7 files changed

+35
-26
lines changed

7 files changed

+35
-26
lines changed

common/darwin/Classes/FlutterRTCDataChannel.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
6363
NSEnumerator* enumerator = [self.eventQueue objectEnumerator];
6464
id event;
6565
while ((event = enumerator.nextObject) != nil) {
66-
self.eventSink(event);
66+
postEvent(sink, event);
6767
};
6868
self.eventQueue = nil;
6969
return nil;
@@ -149,7 +149,7 @@ - (NSString*)stringForDataChannelState:(RTCDataChannelState)state {
149149

150150
- (void)sendEvent:(id)event withChannel:(RTCDataChannel*)channel {
151151
if (channel.eventSink) {
152-
channel.eventSink(event);
152+
postEvent(channel.eventSink, event);
153153
} else {
154154
if (!channel.eventQueue) {
155155
channel.eventQueue = [NSMutableArray array];

common/darwin/Classes/FlutterRTCDesktopCapturer.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ - (void)didDesktopSourceAdded:(RTC_OBJC_TYPE(RTCDesktopSource) *)source {
346346
NSImage* resizedImg = [self resizeImage:image forSize:NSMakeSize(320, 180)];
347347
data = [resizedImg TIFFRepresentation];
348348
}
349-
self.eventSink(@{
349+
postEvent(self.eventSink, @{
350350
@"event" : @"desktopSourceAdded",
351351
@"id" : source.sourceId,
352352
@"name" : source.name,
@@ -361,7 +361,7 @@ - (void)didDesktopSourceAdded:(RTC_OBJC_TYPE(RTCDesktopSource) *)source {
361361
- (void)didDesktopSourceRemoved:(RTC_OBJC_TYPE(RTCDesktopSource) *)source {
362362
// NSLog(@"didDesktopSourceRemoved: %@, id %@", source.name, source.sourceId);
363363
if (self.eventSink) {
364-
self.eventSink(@{
364+
postEvent(self.eventSink, @{
365365
@"event" : @"desktopSourceRemoved",
366366
@"id" : source.sourceId,
367367
});
@@ -372,7 +372,7 @@ - (void)didDesktopSourceRemoved:(RTC_OBJC_TYPE(RTCDesktopSource) *)source {
372372
- (void)didDesktopSourceNameChanged:(RTC_OBJC_TYPE(RTCDesktopSource) *)source {
373373
// NSLog(@"didDesktopSourceNameChanged: %@, id %@", source.name, source.sourceId);
374374
if (self.eventSink) {
375-
self.eventSink(@{
375+
postEvent(self.eventSink, @{
376376
@"event" : @"desktopSourceNameChanged",
377377
@"id" : source.sourceId,
378378
@"name" : source.name,
@@ -386,7 +386,7 @@ - (void)didDesktopSourceThumbnailChanged:(RTC_OBJC_TYPE(RTCDesktopSource) *)sour
386386
if (self.eventSink) {
387387
NSImage* resizedImg = [self resizeImage:[source thumbnail] forSize:NSMakeSize(320, 180)];
388388
NSData* data = [resizedImg TIFFRepresentation];
389-
self.eventSink(@{
389+
postEvent(self.eventSink, @{
390390
@"event" : @"desktopSourceThumbnailChanged",
391391
@"id" : source.sourceId,
392392
@"thumbnail" : data

common/darwin/Classes/FlutterRTCFrameCryptor.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ - (void)frameCryptor:(RTC_OBJC_TYPE(RTCFrameCryptor) *)frameCryptor
588588
didStateChangeWithParticipantId:(NSString*)participantId
589589
withState:(FrameCryptionState)stateChanged {
590590
if (frameCryptor.eventSink) {
591-
frameCryptor.eventSink(@{
591+
postEvent(frameCryptor.eventSink, @{
592592
@"event" : @"frameCryptionStateChanged",
593593
@"participantId" : participantId,
594594
@"state" : [self stringFromState:stateChanged]

common/darwin/Classes/FlutterRTCPeerConnection.m

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
397397
didChangeSignalingState:(RTCSignalingState)newState {
398398
FlutterEventSink eventSink = peerConnection.eventSink;
399399
if (eventSink) {
400-
eventSink(@{@"event" : @"signalingState", @"state" : [self stringForSignalingState:newState]});
400+
postEvent(eventSink, @{@"event" : @"signalingState", @"state" : [self stringForSignalingState:newState]});
401401
}
402402
}
403403

@@ -410,7 +410,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
410410

411411
FlutterEventSink eventSink = peerConnection.eventSink;
412412
if (eventSink) {
413-
eventSink(@{
413+
postEvent(eventSink, @{
414414
@"event" : @"onAddTrack",
415415
@"streamId" : streamId,
416416
@"trackId" : track.trackId,
@@ -433,7 +433,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
433433
NSString* streamId = stream.streamId;
434434
FlutterEventSink eventSink = peerConnection.eventSink;
435435
if (eventSink) {
436-
eventSink(@{
436+
postEvent(eventSink, @{
437437
@"event" : @"onRemoveTrack",
438438
@"streamId" : streamId,
439439
@"trackId" : track.trackId,
@@ -488,7 +488,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection didAddStream:(RTCMedia
488488

489489
FlutterEventSink eventSink = peerConnection.eventSink;
490490
if (eventSink) {
491-
eventSink(@{
491+
postEvent(eventSink, @{
492492
@"event" : @"onAddStream",
493493
@"streamId" : streamId,
494494
@"audioTracks" : audioTracks,
@@ -515,7 +515,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection didRemoveStream:(RTCMe
515515

516516
FlutterEventSink eventSink = peerConnection.eventSink;
517517
if (eventSink) {
518-
eventSink(@{
518+
postEvent(eventSink, @{
519519
@"event" : @"onRemoveStream",
520520
@"streamId" : streamId,
521521
});
@@ -525,7 +525,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection didRemoveStream:(RTCMe
525525
- (void)peerConnectionShouldNegotiate:(RTCPeerConnection*)peerConnection {
526526
FlutterEventSink eventSink = peerConnection.eventSink;
527527
if (eventSink) {
528-
eventSink(@{
528+
postEvent(eventSink, @{
529529
@"event" : @"onRenegotiationNeeded",
530530
});
531531
}
@@ -535,7 +535,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
535535
didChangeIceConnectionState:(RTCIceConnectionState)newState {
536536
FlutterEventSink eventSink = peerConnection.eventSink;
537537
if (eventSink) {
538-
eventSink(@{
538+
postEvent(eventSink, @{
539539
@"event" : @"iceConnectionState",
540540
@"state" : [self stringForICEConnectionState:newState]
541541
});
@@ -546,16 +546,15 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
546546
didChangeIceGatheringState:(RTCIceGatheringState)newState {
547547
FlutterEventSink eventSink = peerConnection.eventSink;
548548
if (eventSink) {
549-
eventSink(
550-
@{@"event" : @"iceGatheringState", @"state" : [self stringForICEGatheringState:newState]});
549+
postEvent(eventSink, @{@"event" : @"iceGatheringState", @"state" : [self stringForICEGatheringState:newState]});
551550
}
552551
}
553552

554553
- (void)peerConnection:(RTCPeerConnection*)peerConnection
555554
didGenerateIceCandidate:(RTCIceCandidate*)candidate {
556555
FlutterEventSink eventSink = peerConnection.eventSink;
557556
if (eventSink) {
558-
eventSink(@{
557+
postEvent(eventSink, @{
559558
@"event" : @"onCandidate",
560559
@"candidate" : @{
561560
@"candidate" : candidate.sdp,
@@ -592,7 +591,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
592591
[eventChannel setStreamHandler:dataChannel];
593592
FlutterEventSink eventSink = peerConnection.eventSink;
594593
if (eventSink) {
595-
eventSink(@{
594+
postEvent(eventSink, @{
596595
@"event" : @"didOpenDataChannel",
597596
@"id" : dataChannelId,
598597
@"label" : dataChannel.label,
@@ -607,7 +606,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
607606
didChangeConnectionState:(RTCPeerConnectionState)newState {
608607
FlutterEventSink eventSink = peerConnection.eventSink;
609608
if (eventSink) {
610-
eventSink(@{
609+
postEvent(eventSink, @{
611610
@"event" : @"peerConnectionState",
612611
@"state" : [self stringForPeerConnectionState:newState]
613612
});
@@ -654,7 +653,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
654653
if ([rtpReceiver.track.kind isEqualToString:@"audio"]) {
655654
[self ensureAudioSession];
656655
}
657-
eventSink(event);
656+
postEvent(eventSink, event);
658657
}
659658
}
660659

@@ -671,7 +670,7 @@ - (void)peerConnection:(RTCPeerConnection*)peerConnection
671670
changeReason:(NSString*)reason {
672671
FlutterEventSink eventSink = peerConnection.eventSink;
673672
if (eventSink) {
674-
eventSink(@{
673+
postEvent(eventSink, @{
675674
@"event" : @"onSelectedCandidatePairChanged",
676675
@"local" : @{
677676
@"candidate" : local.sdp,

common/darwin/Classes/FlutterRTCVideoRenderer.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ - (void)renderFrame:(RTCVideoFrame*)frame {
187187
dispatch_async(dispatch_get_main_queue(), ^{
188188
FlutterRTCVideoRenderer* strongSelf = weakSelf;
189189
if (strongSelf.eventSink) {
190-
strongSelf.eventSink(@{
190+
postEvent( strongSelf.eventSink, @{
191191
@"event" : @"didTextureChangeVideoSize",
192192
@"id" : @(strongSelf.textureId),
193193
@"width" : @(frame.width),
@@ -202,7 +202,7 @@ - (void)renderFrame:(RTCVideoFrame*)frame {
202202
dispatch_async(dispatch_get_main_queue(), ^{
203203
FlutterRTCVideoRenderer* strongSelf = weakSelf;
204204
if (strongSelf.eventSink) {
205-
strongSelf.eventSink(@{
205+
postEvent( strongSelf.eventSink,@{
206206
@"event" : @"didTextureChangeRotation",
207207
@"id" : @(strongSelf.textureId),
208208
@"rotation" : @(frame.rotation),
@@ -219,7 +219,7 @@ - (void)renderFrame:(RTCVideoFrame*)frame {
219219
[strongSelf.registry textureFrameAvailable:strongSelf.textureId];
220220
if (!strongSelf->_isFirstFrameRendered) {
221221
if (strongSelf.eventSink) {
222-
strongSelf.eventSink(@{@"event" : @"didFirstFrameRendered"});
222+
postEvent(strongSelf.eventSink, @{@"event" : @"didFirstFrameRendered"});
223223
strongSelf->_isFirstFrameRendered = true;
224224
}
225225
}

common/darwin/Classes/FlutterWebRTCPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@class FlutterRTCVideoRenderer;
1111
@class FlutterRTCFrameCapturer;
1212

13+
void postEvent(FlutterEventSink sink, id _Nullable event);
14+
1315
typedef void (^CompletionHandler)(void);
1416

1517
typedef void (^CapturerStopHandler)(CompletionHandler handler);

common/darwin/Classes/FlutterWebRTCPlugin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ @implementation VideoEncoderFactorySimulcast
7575
}
7676
@end
7777

78+
void postEvent(FlutterEventSink sink, id _Nullable event) {
79+
if (sink) {
80+
dispatch_async(dis CFCF patch_get_main_queue(), ^{
81+
sink(event);
82+
});
83+
}
84+
}
85+
7886
@implementation FlutterWebRTCPlugin {
7987
#pragma clang diagnostic pop
8088
FlutterMethodChannel* _methodChannel;
@@ -157,7 +165,7 @@ - (instancetype)initWithChannel:(FlutterMethodChannel*)channel
157165
[_peerConnectionFactory.audioDeviceModule setDevicesUpdatedHandler:^(void) {
158166
NSLog(@"Handle Devices Updated!");
159167
if (self.eventSink) {
160-
self.eventSink(@{@"event" : @"onDeviceChange"});
168+
postEvent( self.eventSink, @{@"event" : @"onDeviceChange"});
161169
}
162170
}];
163171
#endif
@@ -209,7 +217,7 @@ - (void)didSessionRouteChange:(NSNotification*)notification {
209217
if (self.eventSink &&
210218
(routeChangeReason == AVAudioSessionRouteChangeReasonNewDeviceAvailable ||
211219
routeChangeReason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable)) {
212-
self.eventSink(@{@"event" : @"onDeviceChange"});
220+
postEvent(self.eventSink, @{@"event" : @"onDeviceChange"});
213221
}
214222
#endif
215223
}

0 commit comments

Comments
 (0)
0