Conversation
…load verification
Author
|
Draft, as I am not very experienced in iOS development, and so need to configure my environment for proper testing unless someone else is willing to help me test :) Thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bugfix
This PR attempts to fix two separate bugs I ran into, both (I believe) resulting from inaccurate audio duration estimations in
AVFoundation:I believe these bugs are direct consequences of Variable Bit Rate (VBR) encoding. This is unfortunate, as audiobooks are inherently well-suited to VBR and spectrum-based compression (given the relatively narrow frequency range of the spoken human voice).
Related tasks
Approach
AVFoundation returns quick, estimated durations for stream-oriented formats (like MP3s or Ogg/Opus containers) when loaded via
AVURLAssetwithout precise timing flags. This estimate is what gets baked into BookPlayer's CoreData store upon import.This PR introduces targeted logic to explicitly pass
AVURLAssetPreferPreciseDurationAndTimingKey: trueduring metadata extraction, library sync, and download verification.To ensure we don't introduce performance regressions (i.e., slowing down imports) for formats that AVFoundation natively evaluates perfectly (like
.m4aand.m4b), I gated this "precise" scanning is strictly gated behind an extension check. It only applies to.opus,.ogg, and.mp3files.Not as smart as checking for VBR encoding, but this seemed lighter touch.
These specific formats are structurally designed as continuous streams that can be appended to, meaning they lack a strict, reliable global duration header. While AVFoundation's default "quick-and-dirty" estimation based on file size and initial bitrate works perfectly fine on Constant Bit Rate (CBR) encoding, it can fail wildly for VBR (if it's using only a handful of samples to determine the bitrate of the entire file).
This would explain the variable "starts" described in one of the Volume import issues listed. I myself experienced that -- when moving to the next chapter, it would start at 30-110s after the start of the file.
Another thing I experience when using my VBR Opus files on a Volume-imported book: It would play to the end of the scrubber, then jump the next chapter at the incorrect time.
All in all, this made the "Volume" experience very difficult to use. But even outside of "Volume" mode, all the durations are wrong. including on the "Now Playing" screen. I'd regularly hit the end and watch the scrubber start counting in the opposite direction. Which was annoying but usable.
By requesting precise timing, we force AVFoundation to scan the stream. (Notably, web searches indicate that AVFoundation is smart enough to quickly detect if the file is actually CBR during this scan, meaning the performance penalty for CBR files should remain negligible).
Things to be aware of / Things to focus on
.opus,.ogg, and.mp3files will now take slightly longer during import because AVFoundation is being forced to perform a full stream scan to determine exact duration for VBR files.await), this should not block the main UI thread..m4a,.m4b) are completely unaffected by this change and will experience no performance regressions.