@@ -845,13 +845,17 @@ - (CVPixelBufferRef)copyPixelBuffer {
845
845
846
846
- (void )startVideoRecordingWithCompletion : (void (^)(FlutterError *_Nullable))completion
847
847
messengerForStreaming : (nullable NSObject <FlutterBinaryMessenger> *)messenger {
848
- if (!_isRecording) {
849
- if (messenger != nil ) {
850
- [self startImageStreamWithMessenger: messenger];
851
- }
848
+ if (_isRecording){
849
+ completion ([FlutterError errorWithCode: @" Error"
850
+ message: @" Video is already recording"
851
+ details: nil ]);
852
+ return ;
853
+ }
852
854
855
+ // Main logic to setup the video recording in a block to reuse in the completion block.
856
+ void (^videoRecordingSetupWithCompletion)(void ) = ^void (void ) {
853
857
NSError *error;
854
- _videoRecordingPath = [self getTemporaryFilePathWithExtension: @" mp4"
858
+ self-> _videoRecordingPath = [self getTemporaryFilePathWithExtension: @" mp4"
855
859
subfolder: @" videos"
856
860
prefix: @" REC_"
857
861
error: error];
@@ -871,20 +875,25 @@ - (void)startVideoRecordingWithCompletion:(void (^)(FlutterError *_Nullable))com
871
875
// didOutputSampleBuffer had chance to call startWriting and lag at start of video
872
876
// https://github.com/flutter/flutter/issues/132016
873
877
// https://github.com/flutter/flutter/issues/151319
874
- [_videoWriter startWriting ];
875
- _isFirstVideoSample = YES ;
876
- _isRecording = YES ;
877
- _isRecordingPaused = NO ;
878
- _videoTimeOffset = CMTimeMake (0 , 1 );
879
- _audioTimeOffset = CMTimeMake (0 , 1 );
880
- _videoIsDisconnected = NO ;
881
- _audioIsDisconnected = NO ;
878
+ [self -> _videoWriter startWriting ];
879
+ self-> _isFirstVideoSample = YES ;
880
+ self-> _isRecording = YES ;
881
+ self-> _isRecordingPaused = NO ;
882
+ self-> _videoTimeOffset = CMTimeMake (0 , 1 );
883
+ self-> _audioTimeOffset = CMTimeMake (0 , 1 );
884
+ self-> _videoIsDisconnected = NO ;
885
+ self-> _audioIsDisconnected = NO ;
882
886
completion (nil );
883
- } else {
884
- completion ([FlutterError errorWithCode: @" Error"
885
- message: @" Video is already recording"
886
- details: nil ]);
887
+ };
888
+
889
+ if (messenger != nil ) {
890
+ [self startImageStreamWithMessenger: messenger
891
+ completion: ^(FlutterError *_Nullable error) {
892
+ videoRecordingSetupWithCompletion ();
893
+ }];
894
+ return ;
887
895
}
896
+ videoRecordingSetupWithCompletion ();
888
897
}
889
898
890
899
- (void )stopVideoRecordingWithCompletion : (void (^)(NSString *_Nullable,
@@ -1176,14 +1185,17 @@ - (void)setExposureOffset:(double)offset {
1176
1185
[_captureDevice unlockForConfiguration ];
1177
1186
}
1178
1187
1179
- - (void )startImageStreamWithMessenger : (NSObject <FlutterBinaryMessenger> *)messenger {
1188
+ - (void )startImageStreamWithMessenger : (NSObject <FlutterBinaryMessenger> *)messenger
1189
+ completion : (void (^)(FlutterError *))completion {
1180
1190
[self startImageStreamWithMessenger: messenger
1181
1191
imageStreamHandler: [[FLTImageStreamHandler alloc ]
1182
- initWithCaptureSessionQueue: _captureSessionQueue]];
1192
+ initWithCaptureSessionQueue: _captureSessionQueue]
1193
+ completion: completion];
1183
1194
}
1184
1195
1185
1196
- (void )startImageStreamWithMessenger : (NSObject <FlutterBinaryMessenger> *)messenger
1186
- imageStreamHandler : (FLTImageStreamHandler *)imageStreamHandler {
1197
+ imageStreamHandler : (FLTImageStreamHandler *)imageStreamHandler
1198
+ completion : (void (^)(FlutterError *))completion {
1187
1199
if (!_isStreamingImages) {
1188
1200
id <FLTEventChannel> eventChannel = [FlutterEventChannel
1189
1201
eventChannelWithName: @" plugins.flutter.io/camera_avfoundation/imageStream"
@@ -1196,19 +1208,27 @@ - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messen
1196
1208
[threadSafeEventChannel setStreamHandler: _imageStreamHandler
1197
1209
completion: ^{
1198
1210
typeof (self) strongSelf = weakSelf;
1199
- if (!strongSelf) return ;
1211
+ if (!strongSelf) {
1212
+ completion (nil );
1213
+ return ;
1214
+ }
1200
1215
1201
1216
dispatch_async (strongSelf.captureSessionQueue , ^{
1202
1217
// cannot use the outter strongSelf
1203
1218
typeof (self) strongSelf = weakSelf;
1204
- if (!strongSelf) return ;
1219
+ if (!strongSelf) {
1220
+ completion (nil );
1221
+ return ;
1222
+ }
1205
1223
1206
1224
strongSelf.isStreamingImages = YES ;
1207
1225
strongSelf.streamingPendingFramesCount = 0 ;
1226
+ completion (nil );
1208
1227
});
1209
1228
}];
1210
1229
} else {
1211
1230
[self reportErrorMessage: @" Images from camera are already streaming!" ];
1231
+ completion (nil );
1212
1232
}
1213
1233
}
1214
1234
0 commit comments