-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[camera_avfoundation] fix race condition when starting image stream on iOS #8733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 31 commits into
flutter:main
from
SkillDevs:fix_startstream_race_condition
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a978667
fix race condition when starting image stream on iOS
js2702 494fe89
update pubspec
js2702 e902881
update changelog
js2702 c4aa926
Test not working
js2702 e6dd1a6
fix test
js2702 423c038
Merge branch 'main' into fix_startstream_race_condition
js2702 0e16acb
Lint + add async/await
js2702 364d2d6
Merge tag 'camera_avfoundation-v0.9.18+9' into fix_startstream_race_c…
js2702 8502b67
remove async from tests
js2702 7c462d9
fix lint + missing parameter
js2702 5dc4fb0
Fix lint again
js2702 680820f
lint
js2702 d46b835
lint once again
js2702 2ed081e
Merge remote-tracking branch 'origin/main' into fix_startstream_race_…
js2702 80b733b
fixes
js2702 0add70e
empty callback
js2702 7bd57e5
Merge branch 'main' into fix_startstream_race_condition
js2702 b50bab8
call completion in all paths
js2702 42be013
formatting
js2702 2e78c19
call completion in all paths
js2702 870f7f5
lint
js2702 fba0373
Merge branch 'main' into fix_startstream_race_condition
js2702 a2b3269
separate in function
js2702 488bf75
Merge remote-tracking branch 'origin/main' into fix_startstream_race_…
js2702 c5075a0
adapt after merging
js2702 5dfac20
formatting
js2702 c0f8e90
rename setup method
davidmartos96 987f067
update error as param
davidmartos96 48354d9
lint
js2702 ce94fad
trigger tests
js2702 4661cc8
trigger tests
js2702 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| ## 0.9.19+3 | ||
|
|
||
| * Fixes race condition when starting image stream. | ||
|
|
||
|
|
||
| ## 0.9.19+2 | ||
|
|
||
| * Adds the `Camera` Swift protocol. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,7 +306,7 @@ - (void)captureToFileWithCompletion:(void (^)(NSString *_Nullable, | |
| NSString *path = [self getTemporaryFilePathWithExtension:extension | ||
| subfolder:@"pictures" | ||
| prefix:@"CAP_" | ||
| error:error]; | ||
| error:&error]; | ||
| if (error) { | ||
| completion(nil, FlutterErrorFromNSError(error)); | ||
| return; | ||
|
|
@@ -362,7 +362,7 @@ - (AVCaptureVideoOrientation)getVideoOrientationForDeviceOrientation: | |
| - (NSString *)getTemporaryFilePathWithExtension:(NSString *)extension | ||
| subfolder:(NSString *)subfolder | ||
| prefix:(NSString *)prefix | ||
| error:(NSError *)error { | ||
| error:(NSError **)error { | ||
| NSString *docDir = | ||
| NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; | ||
| NSString *fileDir = | ||
|
|
@@ -373,11 +373,11 @@ - (NSString *)getTemporaryFilePathWithExtension:(NSString *)extension | |
|
|
||
| NSFileManager *fm = [NSFileManager defaultManager]; | ||
| if (![fm fileExistsAtPath:fileDir]) { | ||
| [[NSFileManager defaultManager] createDirectoryAtPath:fileDir | ||
| withIntermediateDirectories:true | ||
| attributes:nil | ||
| error:&error]; | ||
| if (error) { | ||
| BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:fileDir | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch on this change! |
||
| withIntermediateDirectories:true | ||
| attributes:nil | ||
| error:error]; | ||
| if (!success) { | ||
| return nil; | ||
| } | ||
| } | ||
|
|
@@ -498,43 +498,50 @@ - (void)dealloc { | |
| [_motionManager stopAccelerometerUpdates]; | ||
| } | ||
|
|
||
| /// Main logic to setup the video recording. | ||
| - (void)setUpVideoRecordingWithCompletion:(void (^)(FlutterError *_Nullable))completion { | ||
| NSError *error; | ||
| _videoRecordingPath = [self getTemporaryFilePathWithExtension:@"mp4" | ||
| subfolder:@"videos" | ||
| prefix:@"REC_" | ||
| error:&error]; | ||
| if (error) { | ||
| completion(FlutterErrorFromNSError(error)); | ||
| return; | ||
| } | ||
| if (![self setupWriterForPath:_videoRecordingPath]) { | ||
| completion([FlutterError errorWithCode:@"IOError" message:@"Setup Writer Failed" details:nil]); | ||
| return; | ||
| } | ||
| // startWriting should not be called in didOutputSampleBuffer where it can cause state | ||
| // in which _isRecording is YES but _videoWriter.status is AVAssetWriterStatusUnknown | ||
| // in stopVideoRecording if it is called after startVideoRecording but before | ||
| // didOutputSampleBuffer had chance to call startWriting and lag at start of video | ||
| // https://github.com/flutter/flutter/issues/132016 | ||
| // https://github.com/flutter/flutter/issues/151319 | ||
| [_videoWriter startWriting]; | ||
| _isFirstVideoSample = YES; | ||
| _isRecording = YES; | ||
| _isRecordingPaused = NO; | ||
| _videoTimeOffset = CMTimeMake(0, 1); | ||
| _audioTimeOffset = CMTimeMake(0, 1); | ||
| _videoIsDisconnected = NO; | ||
| _audioIsDisconnected = NO; | ||
| completion(nil); | ||
| } | ||
|
|
||
| - (void)startVideoRecordingWithCompletion:(void (^)(FlutterError *_Nullable))completion | ||
| messengerForStreaming:(nullable NSObject<FlutterBinaryMessenger> *)messenger { | ||
| if (!_isRecording) { | ||
| if (messenger != nil) { | ||
| [self startImageStreamWithMessenger:messenger]; | ||
| } | ||
|
|
||
| NSError *error; | ||
| _videoRecordingPath = [self getTemporaryFilePathWithExtension:@"mp4" | ||
| subfolder:@"videos" | ||
| prefix:@"REC_" | ||
| error:error]; | ||
| if (error) { | ||
| completion(FlutterErrorFromNSError(error)); | ||
| return; | ||
| } | ||
| if (![self setupWriterForPath:_videoRecordingPath]) { | ||
| completion([FlutterError errorWithCode:@"IOError" | ||
| message:@"Setup Writer Failed" | ||
| details:nil]); | ||
| [self startImageStreamWithMessenger:messenger | ||
| completion:^(FlutterError *_Nullable error) { | ||
| [self setUpVideoRecordingWithCompletion:completion]; | ||
| }]; | ||
| return; | ||
| } | ||
| // startWriting should not be called in didOutputSampleBuffer where it can cause state | ||
| // in which _isRecording is YES but _videoWriter.status is AVAssetWriterStatusUnknown | ||
| // in stopVideoRecording if it is called after startVideoRecording but before | ||
| // didOutputSampleBuffer had chance to call startWriting and lag at start of video | ||
| // https://github.com/flutter/flutter/issues/132016 | ||
| // https://github.com/flutter/flutter/issues/151319 | ||
| [_videoWriter startWriting]; | ||
| _isFirstVideoSample = YES; | ||
| _isRecording = YES; | ||
| _isRecordingPaused = NO; | ||
| _videoTimeOffset = CMTimeMake(0, 1); | ||
| _audioTimeOffset = CMTimeMake(0, 1); | ||
| _videoIsDisconnected = NO; | ||
| _audioIsDisconnected = NO; | ||
| completion(nil); | ||
|
|
||
| [self setUpVideoRecordingWithCompletion:completion]; | ||
| } else { | ||
| completion([FlutterError errorWithCode:@"Error" | ||
| message:@"Video is already recording" | ||
|
|
@@ -831,14 +838,17 @@ - (void)setExposureOffset:(double)offset { | |
| [_captureDevice unlockForConfiguration]; | ||
| } | ||
|
|
||
| - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger { | ||
| - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger | ||
| completion:(void (^)(FlutterError *))completion { | ||
| [self startImageStreamWithMessenger:messenger | ||
| imageStreamHandler:[[FLTImageStreamHandler alloc] | ||
| initWithCaptureSessionQueue:_captureSessionQueue]]; | ||
| initWithCaptureSessionQueue:_captureSessionQueue] | ||
| completion:completion]; | ||
| } | ||
|
|
||
| - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger | ||
| imageStreamHandler:(FLTImageStreamHandler *)imageStreamHandler { | ||
| imageStreamHandler:(FLTImageStreamHandler *)imageStreamHandler | ||
| completion:(void (^)(FlutterError *))completion { | ||
| if (!_isStreamingImages) { | ||
| id<FLTEventChannel> eventChannel = [FlutterEventChannel | ||
| eventChannelWithName:@"plugins.flutter.io/camera_avfoundation/imageStream" | ||
|
|
@@ -851,19 +861,27 @@ - (void)startImageStreamWithMessenger:(NSObject<FlutterBinaryMessenger> *)messen | |
| [threadSafeEventChannel setStreamHandler:_imageStreamHandler | ||
| completion:^{ | ||
| typeof(self) strongSelf = weakSelf; | ||
| if (!strongSelf) return; | ||
| if (!strongSelf) { | ||
| completion(nil); | ||
| return; | ||
| } | ||
|
|
||
| dispatch_async(strongSelf.captureSessionQueue, ^{ | ||
| // cannot use the outter strongSelf | ||
| typeof(self) strongSelf = weakSelf; | ||
| if (!strongSelf) return; | ||
| if (!strongSelf) { | ||
| completion(nil); | ||
| return; | ||
| } | ||
|
|
||
| strongSelf.isStreamingImages = YES; | ||
| strongSelf.streamingPendingFramesCount = 0; | ||
| completion(nil); | ||
| }); | ||
| }]; | ||
| } else { | ||
| [self reportErrorMessage:@"Images from camera are already streaming!"]; | ||
| completion(nil); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.