-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[camera_avfoundation] handle interruptions and use single offset #8982
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
base: main
Are you sure you want to change the base?
Conversation
I am not sure what this should mean. |
@@ -80,7 +80,9 @@ enum CameraTestUtils { | |||
|
|||
/// Creates a test sample buffer. | |||
/// @return a test sample buffer. | |||
static func createTestSampleBuffer() -> CMSampleBuffer { | |||
static func createTestSampleBuffer( | |||
timestamp: CMTime = CMTime.zero, duration: CMTime = CMTimeMake(value: 1, timescale: 44100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can infer .zero
nit2: split into multiple lines
## 0.9.18+14 | ||
|
||
* Handle video and audio interruptions and errors. | ||
* Use a single time offset for both video and audio. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... to fix a bug where audio and video are out of sync after interrupted by phone cals.
|
||
[NSNotificationCenter.defaultCenter addObserver:self | ||
selector:@selector(captureSessionRuntimeError:) | ||
name:AVCaptureSessionRuntimeErrorNotification |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when will run time error happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As is in the description "for example when there is an incoming phone call and the camera starts recording with audio". When startVideoRecording
is called with enabled audio while there is an incoming call (does not need to be answered in my case, ringing is enough). Audio session then stops running so no more audio can be recorded after this happens until the session is started again or FLTCam
recreated (seems the only possibility to recover from the dart side for now). Seems no reasonable detection of this state is possible from the dart side for now (sending error to dart side on this error notification enables such detection).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put some comments there?
@@ -788,10 +803,8 @@ - (void)newVideoSample:(CMSampleBufferRef)sampleBuffer { | |||
} | |||
return; | |||
} | |||
if (_videoWriterInput.readyForMoreMediaData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is moved to top right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
@property(assign, nonatomic) CMTime recordingTimeOffset; | ||
@property(assign, nonatomic) CMTime lastSampleEndTime; | ||
@property(nonatomic) AVCaptureOutput *outputForOffsetAdjusting; | ||
@property(assign, nonatomic) CMTime lastAppendedVideoSampleTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is lastAppendedVideoSampleTime
the same as lastSampleEndTime
?
Can you add some doc on these 4 properties?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lastAppendedVideoSampleTime
is related to this text from the description "Avoid errors when trying to append samples with timestamp less or equal to the previous sample.". appendPixelBuffer
throws an error if a sample with specified sample time (PresentationTime
) is appended after another sample with higher or equal time was already appended before (and maybe this should be end time rather than start time too).
@property(assign, nonatomic) CMTime videoTimeOffset; | ||
@property(assign, nonatomic) CMTime audioTimeOffset; | ||
@property(assign, nonatomic) CMTime recordingTimeOffset; | ||
@property(assign, nonatomic) CMTime lastSampleEndTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
am i reading it right that
(1) recordingTimeOffset
covers videoTimeOffset
and audioTimeOffset
and
(2) lastSampleEndTime
covers lastVideoSampleTime
and lastAudioSampleTime
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, lastAudioSampleTime
was actually the end time of a sample so I named it accordingly. And lastVideoSampleTime
was practically end time too, according to logic for offset calculation both before and after this PR, it just happens that video samples tend to not have duration which can be interpreted as zero duration in this case so end time is same as start time. For that logic to work right this needs to be end time of sample, not start time.
@property(assign, nonatomic) CMTime audioTimeOffset; | ||
@property(assign, nonatomic) CMTime recordingTimeOffset; | ||
@property(assign, nonatomic) CMTime lastSampleEndTime; | ||
@property(nonatomic) AVCaptureOutput *outputForOffsetAdjusting; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we didn't have this output. can this be just a local variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That output is either audio or video output depending on whether audio or video output should be used for offset adjusting.
@property(assign, nonatomic) CMTime recordingTimeOffset; | ||
@property(assign, nonatomic) CMTime lastSampleEndTime; | ||
@property(nonatomic) AVCaptureOutput *outputForOffsetAdjusting; | ||
@property(assign, nonatomic) CMTime lastAppendedVideoSampleTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is lastAppendedVideoSampleTime
the same as lastSampleEndTime
?
Can you add some doc on these 4 properties?
From triage: what's the status of this PR? It's not clear to me if it's waiting for updates, or waiting for re-review. |
It is waiting for updates. |
Handle interruptions of capture sessions by doing "internal pause". Another possibility would be to remove audio input from the session but this would be too specific for interruptions caused by an incoming call or alarm. Or to stop video recording as the native ios camera app does but this would require some additional event.
Handle asynchronous error notifications posted about capture sessions. This can happen for example when there is an incoming phone call and the camera starts recording with audio. For this specific case would be enough to just call
startRunning
on capture sessions but that is too specific and would require ability to restart capture session from dart side as seems there is no good way how to test when such phone call or other such event ends, and trying to restart it for example every second insideFLTCam
does not look like good idea. Instead the camera controller enters an error state and can be recovered by disposing and recreating which should be more generic.Use single offset for both video and audio as separate offsets can cause unsynchronized video with audio. Seems audio timestamps tend to be ignored except for the first sample so prefer them for calculating offset to avoid desynchronization. Avoid errors when trying to append samples with timestamp less or equal to the previous sample. Fix a bug when after adjusting offset also last sample time needs to be updated to avoid adding past offsets multiple times.
Pre-Review Checklist
[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3