Skip to content

Commit

Permalink
fixes symphonia seek beyond end of file returning an error
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsk committed Oct 8, 2023
1 parent c93a897 commit c29b442
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{io::ErrorKind, time::Duration};
use symphonia::{
core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec},
Expand Down Expand Up @@ -142,6 +142,7 @@ impl Source for SymphoniaDecoder {

// TODO: do we return till where we seeked? <dvdsk [email protected]>
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 {
Expand All @@ -150,14 +151,20 @@ impl Source for SymphoniaDecoder {
1f64 / pos.subsec_nanos() as f64
};

self.format.seek(
let res = self.format.seek(
SeekMode::Accurate,
SeekTo::Time {
time: Time::new(pos.as_secs(), pos_fract),
track_id: None,
},
)?;
Ok(())
);

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(()),
}
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
#[inline]
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
let samples = pos.as_secs_f32() * self.sample_rate() as f32;
self.reader.reader.seek(samples as u32).map_err(SeekError::Hound)
self.reader.reader.seek(samples as u32).map_err(SeekError::HoundDecoder)
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,16 @@ pub enum SeekError {
NotSupported { underlying_source: &'static str },
#[cfg(feature = "symphonia")]
#[error("Error seeking: {0}")]
SymphoniaSeekFailed(#[from] symphonia::core::errors::Error),
SymphoniaDecoder(#[from] symphonia::core::errors::Error),
#[cfg(all(feature = "wav", not(feature = "symphonia-wav")))]
#[error("Error seeking in wav source: {0}")]
Hound(std::io::Error),
HoundDecoder(std::io::Error),
#[cfg(all(feature = "vorbis", not(feature = "symphonia-vorbis")))]
#[error("Error seeking in ogg source: {0}")]
Lewton(#[from] lewton::VorbisError),
LewtonDecoder(#[from] lewton::VorbisError),
#[cfg(all(feature = "minimp3", not(feature = "symphonia-mp3")))]
#[error("Error seeking in mp3 source: {0}")]
Minimp3(#[from] minimp3::Error),
Minimp3Decorder(#[from] minimp3::Error),
}

impl<S> Source for Box<dyn Source<Item = S>>
Expand Down

0 comments on commit c29b442

Please sign in to comment.