Skip to content
Open
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
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ hound = ["dep:hound"] # WAV
minimp3 = ["dep:minimp3_fixed"] # MP3
lewton = ["dep:lewton"] # Ogg Vorbis

# Third party codec example
libopus = ["dep:symphonia-adapter-libopus"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
Expand All @@ -124,6 +126,8 @@ atomic_float = { version = "1.1.0", optional = true }
rtrb = { version = "0.3.2", optional = true }
num-rational = "0.4.2"

symphonia-adapter-libopus = {version="0.2", optional = true}

[dev-dependencies]
quickcheck = "1"
rstest = "0.25"
Expand Down Expand Up @@ -251,3 +255,7 @@ required-features = ["playback", "vorbis"]
[[example]]
name = "stereo"
required-features = ["playback", "vorbis"]

[[example]]
name = "third_party_codec"
required-features = ["playback", "symphonia", "libopus"]
Binary file added assets/music.opus
Binary file not shown.
26 changes: 26 additions & 0 deletions examples/third_party_codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::{error::Error, sync::Arc};

use rodio::decoder::DecoderBuilder;
use symphonia::{core::codecs::CodecRegistry, default::register_enabled_codecs};
use symphonia_adapter_libopus::OpusDecoder;

fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let sink = rodio::Sink::connect_new(stream_handle.mixer());

let mut codec_registry = CodecRegistry::new();
codec_registry.register_all::<OpusDecoder>();
register_enabled_codecs(&mut codec_registry);

let codec_registry_arc = Arc::new(codec_registry);

let file = std::fs::File::open("../assets/music.opus")?;
let decoder = DecoderBuilder::new()
.with_codec_registry(codec_registry_arc)
.with_data(file).build()?;
sink.append(decoder);

sink.sleep_until_end();

Ok(())
}
21 changes: 20 additions & 1 deletion src/decoder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@

use std::io::{Read, Seek};

#[cfg(feature = "symphonia")]
use crate::decoder::symphonia::SymphoniaRegistry;

#[cfg(feature = "symphonia")]
use self::read_seek_source::ReadSeekSource;
#[cfg(feature = "symphonia")]
use ::symphonia::core::io::{MediaSource, MediaSourceStream};
use ::symphonia::core::{
codecs::CodecRegistry,
io::{MediaSource, MediaSourceStream},
};

use super::*;

Expand Down Expand Up @@ -77,6 +83,9 @@ pub struct Settings {

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

#[cfg(feature = "symphonia")]
pub(crate) codec_registry: SymphoniaRegistry,
}

impl Default for Settings {
Expand All @@ -88,6 +97,7 @@ impl Default for Settings {
hint: None,
mime_type: None,
is_seekable: false,
codec_registry: SymphoniaRegistry::Default,
}
}
}
Expand Down Expand Up @@ -231,6 +241,15 @@ impl<R: Read + Seek + Send + Sync + 'static> DecoderBuilder<R> {
self
}

/// Set a custom codec registry
///
/// See the symphonia documentation of Registry for how to add additional (third party) codecs.
#[cfg(feature = "symphonia")]
pub fn with_codec_registry(mut self, codec_registry: Arc<CodecRegistry>) -> Self {
self.settings.codec_registry = SymphoniaRegistry::Custom(codec_registry);
self
}

/// Configure whether the data supports random access seeking. Without this,
/// only forward seeking may work.
///
Expand Down
31 changes: 27 additions & 4 deletions src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::time::Duration;
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};
use symphonia::{
core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec},
codecs::{Decoder, DecoderOptions, CODEC_TYPE_NULL},
codecs::{CodecRegistry, Decoder, DecoderOptions, CODEC_TYPE_NULL},
errors::Error,
formats::{FormatOptions, FormatReader, SeekMode, SeekTo, SeekedTo},
io::MediaSourceStream,
Expand All @@ -20,6 +20,25 @@ use crate::{
source, Source,
};

#[derive(Clone)]
/// Enum for choosing which codec registry to used with Symphonia decoders.
pub enum SymphoniaRegistry {
/// Use the symphonia default registry symphonia::default::get_codecs()
Default,

///Use a custom CodecRegistry
Custom(Arc<CodecRegistry>),
}

impl Debug for SymphoniaRegistry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Default => write!(f, "Default"),
Self::Custom(_) => write!(f, "Custom"),
}
}
}

pub(crate) struct SymphoniaDecoder {
decoder: Box<dyn Decoder>,
current_span_offset: usize,
Expand Down Expand Up @@ -102,8 +121,12 @@ impl SymphoniaDecoder {
None => return Ok(None),
};

let mut decoder = symphonia::default::get_codecs()
.make(&track.codec_params, &DecoderOptions::default())?;
let mut decoder = match &settings.codec_registry
{
SymphoniaRegistry::Default => symphonia::default::get_codecs(),
SymphoniaRegistry::Custom(cr) => cr.as_ref()
}.make(&track.codec_params, &DecoderOptions::default())?;

let total_duration = stream
.codec_params
.time_base
Expand Down
Loading