Skip to content
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

RecorderThread: Send input timestamps to MediaCodec encoders #59

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

chenxiaolong
Copy link
Owner

This was previously disabled due to incorrect troubleshooting of an
issue where the c2.android.flac.encoder component would crash during
recording. It turns out the crash was caused by incorrect math:

inputTimestamp += frames * 1000000 / audioRecord.sampleRate

frames and 1000000 are both 32-bit integers, so the multiplication
result overflowed and caused inputTimestamp (a 64-bit integer) to
accumulate negative values, which would crash the encoder. This bug was
inadvertently fixed during refactoring in commit
3d3eb82.

This commit reenables the submission of timestamps to MediaCodec. To
avoid accumulated error due to integer division when the microseconds
per sample count is not perfectly divisible, the recording loop will
keep track of the total frame count and only calculate the timestamp
during buffer submission.

This should fix issues where the encoded output files had overlapping
audio or other weird audible artifacts. These were caused by
MediaCodec's multithreaded encoding, where the lack of timestamps would
cause encoded samples to be produced out of order.

Fixes: #39 #54

This was previously disabled due to incorrect troubleshooting of an
issue where the `c2.android.flac.encoder` component would crash during
recording. It turns out the crash was caused by incorrect math:

    inputTimestamp += frames * 1000000 / audioRecord.sampleRate

`frames` and `1000000` are both 32-bit integers, so the multiplication
result overflowed and caused `inputTimestamp` (a 64-bit integer) to
accumulate negative values, which would crash the encoder. This bug was
inadvertently fixed during refactoring in commit
3d3eb82.

This commit reenables the submission of timestamps to MediaCodec. To
avoid accumulated error due to integer division when the microseconds
per sample count is not perfectly divisible, the recording loop will
keep track of the total frame count and only calculate the timestamp
during buffer submission.

This should fix issues where the encoded output files had overlapping
audio or other weird audible artifacts. These were caused by
MediaCodec's multithreaded encoding, where the lack of timestamps would
cause encoded samples to be produced out of order.

Fixes: #39 #54
Signed-off-by: Andrew Gunnerson <[email protected]>
@chenxiaolong chenxiaolong self-assigned this Jun 1, 2022
@chenxiaolong
Copy link
Owner Author

Test build: BCR-1.7.r4.g39767d3-debug.zip

This was referenced Jun 1, 2022
@chenxiaolong chenxiaolong merged commit 1c95846 into master Jun 1, 2022
@chenxiaolong chenxiaolong deleted the timestamps branch June 1, 2022 04:13
chenxiaolong added a commit that referenced this pull request Jun 1, 2022
Signed-off-by: Andrew Gunnerson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recordings Overlapping
1 participant