Skip to content

Commit

Permalink
symphonia throws error if duration is unknown and seek is beyond sour…
Browse files Browse the repository at this point in the history
…ce length
  • Loading branch information
dvdsk committed Oct 10, 2023
1 parent 244e960 commit ca83a2c
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? <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 {
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(())
}
}

Expand Down

0 comments on commit ca83a2c

Please sign in to comment.