Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hound = { version = "3.3.1", optional = true }
lewton = { version = "0.10", optional = true }
minimp3_fixed = { version = "0.5.4", optional = true }
symphonia = { version = "0.5.4", optional = true, default-features = false }
# symphonia = { git = "https://github.com/roderickvd/Symphonia", branch = "perf/faster-seeking", optional = true }
crossbeam-channel = { version = "0.5.15", optional = true }

rand = { version = "0.9.0", features = ["small_rng", "os_rng"], optional = true }
Expand Down
35 changes: 0 additions & 35 deletions src/decoder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ pub struct Settings {
/// An MIME type hint for the decoder about the format of the stream.
/// When known, this can help the decoder to select the correct demuxer.
pub(crate) mime_type: Option<String>,

/// Whether the decoder should report as seekable.
pub(crate) is_seekable: bool,
}

impl Default for Settings {
Expand All @@ -87,7 +84,6 @@ impl Default for Settings {
gapless: true,
hint: None,
mime_type: None,
is_seekable: false,
}
}
}
Expand Down Expand Up @@ -228,37 +224,6 @@ impl<R: Read + Seek + Send + Sync + 'static> DecoderBuilder<R> {
self
}

/// Configure whether the decoder should report as seekable.
///
/// For reliable seeking behavior, `byte_len` should be set either from file metadata
/// or by seeking to the end of the stream. While seeking may work without `byte_len`
/// for some formats, it is not guaranteed.
///
/// # Examples
/// ```no_run
/// use std::fs::File;
/// use rodio::Decoder;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let file = File::open("audio.mp3")?;
/// let len = file.metadata()?.len();
///
/// // Recommended: Set both byte_len and seekable
/// let decoder = Decoder::builder()
/// .with_data(file)
/// .with_byte_len(len)
/// .with_seekable(true)
/// .build()?;
///
/// // Use the decoder...
/// Ok(())
/// }
/// ```
pub fn with_seekable(mut self, is_seekable: bool) -> Self {
self.settings.is_seekable = is_seekable;
self
}

/// Creates the decoder implementation with configured settings.
fn build_impl(self) -> Result<(DecoderImpl<R>, Settings), DecoderError> {
let data = self.data.ok_or(DecoderError::UnrecognizedFormat)?;
Expand Down
2 changes: 0 additions & 2 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
//! let decoder = Decoder::builder()
//! .with_data(file)
//! .with_byte_len(len) // Enable seeking and duration calculation
//! .with_seekable(true) // Enable seeking operations
//! .with_hint("mp3") // Optional format hint
//! .with_gapless(true) // Enable gapless playback
//! .build()
Expand Down Expand Up @@ -276,7 +275,6 @@ impl TryFrom<std::fs::File> for Decoder<BufReader<std::fs::File>> {
Self::builder()
.with_data(BufReader::new(file))
.with_byte_len(len)
.with_seekable(true)
.build()
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/decoder/read_seek_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub struct ReadSeekSource<T: Read + Seek + Send + Sync> {
/// Optional length of the media source in bytes.
/// When known, this can help with seeking and duration calculations.
byte_len: Option<u64>,
/// Whether this media source reports as seekable.
is_seekable: bool,
}

impl<T: Read + Seek + Send + Sync> ReadSeekSource<T> {
Expand All @@ -29,16 +27,15 @@ impl<T: Read + Seek + Send + Sync> ReadSeekSource<T> {
ReadSeekSource {
inner,
byte_len: settings.byte_len,
is_seekable: settings.is_seekable,
}
}
}

impl<T: Read + Seek + Send + Sync> MediaSource for ReadSeekSource<T> {
/// Returns whether this media source reports as seekable.
/// symphonia sources are only seekable if `byte_len.is_some()`
#[inline]
fn is_seekable(&self) -> bool {
self.is_seekable
self.byte_len.is_some()
}

/// Returns the total length of the media source in bytes, if known.
Expand Down
14 changes: 11 additions & 3 deletions src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use symphonia::{
codecs::{Decoder, DecoderOptions, CODEC_TYPE_NULL},
errors::Error,
formats::{FormatOptions, FormatReader, SeekMode, SeekTo, SeekedTo},
io::MediaSourceStream,
io::{MediaSource, MediaSourceStream},
meta::MetadataOptions,
probe::Hint,
units,
Expand All @@ -28,6 +28,8 @@ pub(crate) struct SymphoniaDecoder {
buffer: SampleBuffer<Sample>,
spec: SignalSpec,
seek_mode: SeekMode,
/// Symphonia can only seek if bytes_len is availabile. When it is then this is true
can_seek: bool,
}

impl SymphoniaDecoder {
Expand All @@ -36,8 +38,8 @@ impl SymphoniaDecoder {
Err(e) => match e {
Error::IoError(e) => Err(DecoderError::IoError(e.to_string())),
Error::DecodeError(e) => Err(DecoderError::DecodeError(e)),
Error::SeekError(_) => {
unreachable!("Seek errors should not occur during initialization")
Error::SeekError(e) => {
unreachable!("Seek errors should not occur during initialization, got: {e:?}")
}
Error::Unsupported(_) => Err(DecoderError::UnrecognizedFormat),
Error::LimitError(e) => Err(DecoderError::LimitError(e)),
Expand Down Expand Up @@ -74,6 +76,7 @@ impl SymphoniaDecoder {
} else {
SeekMode::Accurate
};
let can_seek = mss.byte_len().is_some();
let mut probed = get_probe().format(&hint, mss, &format_opts, &metadata_opts)?;

let stream = match probed.format.default_track() {
Expand Down Expand Up @@ -145,6 +148,7 @@ impl SymphoniaDecoder {
buffer,
spec,
seek_mode,
can_seek,
}))
}

Expand Down Expand Up @@ -179,6 +183,10 @@ impl Source for SymphoniaDecoder {
}

fn try_seek(&mut self, pos: Duration) -> Result<(), source::SeekError> {
if !self.can_seek {
return Err(source::SeekError::NeedsBytesLen);
}

if matches!(self.seek_mode, SeekMode::Accurate)
&& self.decoder.codec_params().time_base.is_none()
{
Expand Down
5 changes: 5 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ pub enum SeekError {
/// The source that did not support seek
underlying_source: &'static str,
},
/// The decoder needs to know the length of the underlying byte stream
NeedsBytesLen,
#[cfg(feature = "symphonia")]
/// The symphonia decoder ran into an issue
SymphoniaDecoder(crate::decoder::symphonia::SeekError),
Expand All @@ -640,6 +642,7 @@ impl fmt::Display for SeekError {
#[cfg(feature = "wav")]
SeekError::HoundDecoder(err) => write!(f, "Error seeking in wav source: {}", err),
SeekError::Other(_) => write!(f, "An error occurred"),
SeekError::NeedsBytesLen => write!(f, "The decoder needs to know the length of the file/byte stream to be able to seek. You can set that by using the `DecoderBuilder` or creating a decoder using `Decoder::try_from(some_file)`"),
}
}
}
Expand All @@ -651,6 +654,7 @@ impl std::error::Error for SeekError {
SeekError::SymphoniaDecoder(err) => Some(err),
#[cfg(feature = "wav")]
SeekError::HoundDecoder(err) => Some(err),
SeekError::NeedsBytesLen => None,
SeekError::Other(err) => Some(err.as_ref()),
}
}
Expand All @@ -669,6 +673,7 @@ impl SeekError {
pub fn source_intact(&self) -> bool {
match self {
SeekError::NotSupported { .. } => true,
SeekError::NeedsBytesLen => true,
#[cfg(feature = "symphonia")]
SeekError::SymphoniaDecoder(_) => false,
#[cfg(feature = "wav")]
Expand Down
25 changes: 25 additions & 0 deletions tests/seek.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rodio::source::SeekError;
use rodio::{ChannelCount, Decoder, Source};
use rstest::rstest;
use rstest_reuse::{self, *};
use std::fs::File;
use std::io::{Read, Seek};
use std::path::Path;
use std::time::Duration;
Expand Down Expand Up @@ -255,3 +257,26 @@ fn get_rl(format: &str) -> Decoder<impl Read + Seek> {
let file = std::fs::File::open(asset).unwrap();
Decoder::try_from(file).unwrap()
}

#[apply(supported_decoders)]
#[trace]
fn decoder_that_need_bytes_len_return_error(
#[case] format: &'static str,
#[case] decoder_name: &'static str,
) {
if decoder_name != "symphonia" {
return;
}

let asset = Path::new("assets/music").with_extension(format);
let file = File::open(asset).unwrap();
let mut decoder = Decoder::builder()
.with_data(file)
.build()
.unwrap();

let res = decoder.try_seek(Duration::from_secs(2));
let Err(SeekError::NeedsBytesLen) = res else {
panic!("should not be able to seek without bytes_len.is_some()");
};
}
1 change: 0 additions & 1 deletion tests/total_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn get_music(format: &str) -> Decoder<impl Read + Seek> {
rodio::Decoder::builder()
.with_data(file)
.with_byte_len(len)
.with_seekable(true)
.with_gapless(false)
.build()
.unwrap()
Expand Down
Loading