Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(playback): avoid unnecessary boxing #60

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion anni-playback/src/decoder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl Decoder {
buffer_signal: Arc<AtomicBool>,
) -> anyhow::Result<Playback> {
let duration_hint = source.duration_hint();
let mss = MediaSourceStream::new(Box::new(source), Default::default());
let mss = MediaSourceStream::new(source.into(), Default::default());
let format_options = FormatOptions {
enable_gapless: true,
..Default::default()
Expand Down
19 changes: 18 additions & 1 deletion anni-playback/src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Receiver {
receiver: std::sync::mpsc::Receiver<(usize, Vec<u8>)>,
}

pub trait AnniSource: MediaSource {
pub trait AnniSource: MediaSource + IntoBoxedMediaSource {
/// The duration of underlying source in seconds.
fn duration_hint(&self) -> Option<u64> {
None
Expand All @@ -46,6 +46,23 @@ impl MediaSource for Box<dyn AnniSource> {

impl AnniSource for std::fs::File {}

// helper trait to do upcasting
pub trait IntoBoxedMediaSource {
fn into_media_source(self: Box<Self>) -> Box<dyn MediaSource>;
}

impl<T: MediaSource + 'static> IntoBoxedMediaSource for T {
fn into_media_source(self: Box<Self>) -> Box<dyn MediaSource> {
self
}
}

impl From<Box<dyn AnniSource>> for Box<dyn MediaSource> {
fn from(value: Box<dyn AnniSource>) -> Self {
value.into_media_source()
}
}

// Specialization is not well-supported so far (even the unstable feature is unstable ww).
// Therefore, we do not provide the default implementation below.
// Users can use a newtype pattern if needed.
Expand Down
Loading