Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera]rename dispatchQueue to captureSessionQueue #4687

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* Minor internal code cleanup.

## 0.9.4+6

* Fixes a crash in iOS when using image stream due to calling Flutter engine API on non-main thread.
Expand Down
41 changes: 22 additions & 19 deletions packages/camera/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ @interface FLTSavePhotoDelegate : NSObject <AVCapturePhotoCaptureDelegate>

@interface FLTImageStreamHandler : NSObject <FlutterStreamHandler>
// The queue on which `eventSink` property should be accessed
@property(nonatomic, strong) dispatch_queue_t dispatchQueue;
// `eventSink` property should be accessed on `dispatchQueue`.
@property(nonatomic, strong) dispatch_queue_t captureSessionQueue;
// `eventSink` property should be accessed on `captureSessionQueue`.
// The block itself should be invoked on the main queue.
@property FlutterEventSink eventSink;
@end

@implementation FLTImageStreamHandler

- (instancetype)initWithDispatchQueue:(dispatch_queue_t)dispatchQueue {
- (instancetype)initWithCaptureSessionQueue:(dispatch_queue_t)captureSessionQueue {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_dispatchQueue = dispatchQueue;
_captureSessionQueue = captureSessionQueue;
return self;
}

- (FlutterError *_Nullable)onCancelWithArguments:(id _Nullable)arguments {
dispatch_async(self.dispatchQueue, ^{
dispatch_async(self.captureSessionQueue, ^{
self.eventSink = nil;
});
return nil;
}

- (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
eventSink:(nonnull FlutterEventSink)events {
dispatch_async(self.dispatchQueue, ^{
dispatch_async(self.captureSessionQueue, ^{
self.eventSink = events;
});
return nil;
Expand Down Expand Up @@ -360,7 +360,9 @@ @interface FLTCam : NSObject <FlutterTexture,
@end

@implementation FLTCam {
dispatch_queue_t _dispatchQueue;
// All FLTCam's state access and capture session related operations should be on run on this
// queue.
dispatch_queue_t _captureSessionQueue;
UIDeviceOrientation _deviceOrientation;
}
// Format used for video and image streaming.
Expand All @@ -371,7 +373,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
resolutionPreset:(NSString *)resolutionPreset
enableAudio:(BOOL)enableAudio
orientation:(UIDeviceOrientation)orientation
dispatchQueue:(dispatch_queue_t)dispatchQueue
captureSessionQueue:(dispatch_queue_t)captureSessionQueue
error:(NSError **)error {
self = [super init];
NSAssert(self, @"super init cannot be nil");
Expand All @@ -381,7 +383,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
*error = e;
}
_enableAudio = enableAudio;
_dispatchQueue = dispatchQueue;
_captureSessionQueue = captureSessionQueue;
_captureSession = [[AVCaptureSession alloc] init];
_captureDevice = [AVCaptureDevice deviceWithUniqueID:cameraName];
_flashMode = _captureDevice.hasFlash ? FlashModeAuto : FlashModeOff;
Expand Down Expand Up @@ -1141,10 +1143,11 @@ - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messen
FLTThreadSafeEventChannel *threadSafeEventChannel =
[[FLTThreadSafeEventChannel alloc] initWithEventChannel:eventChannel];

_imageStreamHandler = [[FLTImageStreamHandler alloc] initWithDispatchQueue:_dispatchQueue];
_imageStreamHandler =
[[FLTImageStreamHandler alloc] initWithCaptureSessionQueue:_captureSessionQueue];
[threadSafeEventChannel setStreamHandler:_imageStreamHandler
completion:^{
dispatch_async(self->_dispatchQueue, ^{
dispatch_async(self->_captureSessionQueue, ^{
self.isStreamingImages = YES;
});
}];
Expand Down Expand Up @@ -1268,7 +1271,7 @@ - (BOOL)setupWriterForPath:(NSString *)path {
_audioWriterInput.expectsMediaDataInRealTime = YES;

[_videoWriter addInput:_audioWriterInput];
[_audioOutput setSampleBufferDelegate:self queue:_dispatchQueue];
[_audioOutput setSampleBufferDelegate:self queue:_captureSessionQueue];
}

if (_flashMode == FlashModeTorch) {
Expand All @@ -1279,7 +1282,7 @@ - (BOOL)setupWriterForPath:(NSString *)path {

[_videoWriter addInput:_videoWriterInput];

[_captureVideoOutput setSampleBufferDelegate:self queue:_dispatchQueue];
[_captureVideoOutput setSampleBufferDelegate:self queue:_captureSessionQueue];

return YES;
}
Expand Down Expand Up @@ -1320,7 +1323,7 @@ @interface CameraPlugin ()
@end

@implementation CameraPlugin {
dispatch_queue_t _dispatchQueue;
dispatch_queue_t _captureSessionQueue;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
Expand Down Expand Up @@ -1382,12 +1385,12 @@ - (void)sendDeviceOrientation:(UIDeviceOrientation)orientation {
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if (_dispatchQueue == nil) {
_dispatchQueue = dispatch_queue_create("io.flutter.camera.dispatchqueue", NULL);
if (_captureSessionQueue == nil) {
_captureSessionQueue = dispatch_queue_create("io.flutter.camera.dispatchqueue", NULL);
}

// Invoke the plugin on another dispatch queue to avoid blocking the UI.
dispatch_async(_dispatchQueue, ^{
dispatch_async(_captureSessionQueue, ^{
FLTThreadSafeFlutterResult *threadSafeResult =
[[FLTThreadSafeFlutterResult alloc] initWithResult:result];

Expand Down Expand Up @@ -1438,7 +1441,7 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call
resolutionPreset:resolutionPreset
enableAudio:[enableAudio boolValue]
orientation:[[UIDevice currentDevice] orientation]
dispatchQueue:_dispatchQueue
captureSessionQueue:_captureSessionQueue
error:&error];

if (error) {
Expand Down Expand Up @@ -1504,7 +1507,7 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call
} else if ([@"dispose" isEqualToString:call.method]) {
[_registry unregisterTexture:cameraId];
[_camera close];
_dispatchQueue = nil;
_captureSessionQueue = nil;
[result sendSuccess];
} else if ([@"prepareForVideoRecording" isEqualToString:call.method]) {
[_camera setUpCaptureSessionForAudio];
Expand Down