Skip to content

Commit

Permalink
fix: remove coarse seeking mode
Browse files Browse the repository at this point in the history
Remove format-specific seeking optimizations in favor of always using
accurate mode. While coarse seeking was potentially faster for CBR
MP3s, it could lead to inaccurate position reporting since the player
cannot reliably determine the actual playback position after seeking.
  • Loading branch information
roderickvd committed Jan 22, 2025
1 parent 9632111 commit 156c039
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
### Changed
- [codec] Split `frame_duration` into `max_frame_length` and `max_frame_duration`
- [decoder] Better error handling following Symphonia's recommendations
- [decoder] Always use accurate seeking mode for reliable position reporting
- [decoder] Fix logical error in `size_hint()` lower bound calculation
- [decoder] Remove `ExactSizeIterator` implementation as total samples can't be determined exactly

Expand Down
21 changes: 3 additions & 18 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use symphonia::{
codecs::{CodecParameters, CodecRegistry, DecoderOptions},
errors::Error as SymphoniaError,
formats::{FormatOptions, FormatReader, SeekMode, SeekTo},
io::{MediaSource, MediaSourceStream, MediaSourceStreamOptions},
io::{MediaSourceStream, MediaSourceStreamOptions},
meta::{MetadataOptions, StandardTagKey, Value},
probe::{Hint, Probe},
},
Expand Down Expand Up @@ -105,9 +105,6 @@ pub struct Decoder {
/// Codec decoder for converting encoded packets to PCM samples
decoder: Box<dyn symphonia::core::codecs::Decoder>,

/// Seeking strategy (Coarse for CBR, Accurate for VBR)
seek_mode: SeekMode,

/// Reusable sample buffer to minimize allocations
buffer: Option<SampleBuffer<SampleFormat>>,

Expand Down Expand Up @@ -206,14 +203,6 @@ impl Decoder {
)
};

// Coarse seeking without a known byte length causes a panic.
// Further, it's not reliable for VBR streams.
let seek_mode = if track.is_cbr() && stream.byte_len().is_some() {
SeekMode::Coarse
} else {
SeekMode::Accurate
};

let demuxer = probe
.format(
&hint,
Expand Down Expand Up @@ -246,7 +235,6 @@ impl Decoder {
Ok(Self {
demuxer,
decoder,
seek_mode,

buffer: None,
position: 0,
Expand Down Expand Up @@ -557,10 +545,7 @@ impl rodio::Source for Decoder {

/// Attempts to seek to the specified position in the audio stream.
///
/// Uses Symphonia's seeking capabilities with format-specific optimizations:
/// * Coarse seeking for CBR content (faster)
/// * Accurate seeking for VBR content (more precise)
///
/// Uses Symphonia's seeking capabilities to find the exact position in the stream.
/// Also resets the decoder state to prevent audio glitches that could occur
/// from seeking to a position that requires different decoding parameters.
///
Expand All @@ -573,7 +558,7 @@ impl rodio::Source for Decoder {
fn try_seek(&mut self, pos: Duration) -> std::result::Result<(), SeekError> {
self.demuxer
.seek(
self.seek_mode,
SeekMode::Accurate,
SeekTo::Time {
track_id: None, // implies the default or first track
time: pos.into(),
Expand Down

0 comments on commit 156c039

Please sign in to comment.