From ca83a2c9bf47a92335b092e31fab96c5db8c2977 Mon Sep 17 00:00:00 2001 From: dvdsk Date: Tue, 10 Oct 2023 13:35:13 +0200 Subject: [PATCH] symphonia throws error if duration is unknown and seek is beyond source length --- src/decoder/symphonia.rs | 55 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/decoder/symphonia.rs b/src/decoder/symphonia.rs index 6eef152e..459c88e7 100644 --- a/src/decoder/symphonia.rs +++ b/src/decoder/symphonia.rs @@ -148,39 +148,40 @@ impl Source for SymphoniaDecoder { .map(|Time { seconds, frac }| Duration::new(seconds, (1f64 / frac) as u32)) } - // TODO: do we return till where we seeked? fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> { - use symphonia::core::errors::SeekErrorKind; use symphonia::core::formats::{SeekMode, SeekTo}; - let pos_fract = if pos.subsec_nanos() == 0 { - 0f64 - } else { - 1f64 / pos.subsec_nanos() as f64 - }; + let seek_beyond_end = self + .total_duration() + .is_some_and(|dur| dur.saturating_sub(pos).as_millis() < 1); - let mut seek_to_time = Time::new(pos.as_secs(), pos_fract); - if let Some(total_duration) = self.total_duration() { - if total_duration.saturating_sub(pos).as_millis() < 1 { - seek_to_time = self.total_duration.unwrap(); - } + if seek_beyond_end { + self.format.seek( + SeekMode::Accurate, + SeekTo::Time { + time: self.total_duration.unwrap(), + track_id: None, + }, + )?; + } else { + let frac = if pos.subsec_nanos() == 0 { + 0f64 + } else { + 1f64 / pos.subsec_nanos() as f64 + }; + self.format.seek( + SeekMode::Accurate, + SeekTo::Time { + time: Time { + seconds: pos.as_secs(), + frac, + }, + track_id: None, + }, + )?; } - let res = self.format.seek( - SeekMode::Accurate, - SeekTo::Time { - time: seek_to_time, - track_id: None, - }, - ); - assert!(self.total_duration().is_some()); - - match res { - Err(Error::IoError(e)) if e.kind() == ErrorKind::UnexpectedEof => Ok(()), - Err(Error::SeekError(SeekErrorKind::OutOfRange)) => Ok(()), - Err(e) => Err(SeekError::SymphoniaDecoder(e)), - Ok(_) => Ok(()), - } + Ok(()) } }