From 57df3fe53a0d56da38bf1b0a0198af8904f054cf Mon Sep 17 00:00:00 2001 From: baguette <35662205+peppizza@users.noreply.github.com> Date: Fri, 4 Dec 2020 08:43:35 -0500 Subject: [PATCH] TrackHandle: add metadata field (#25) * Adds metadata field to TrackHandle. Co-authored-by: Kyle Simpson --- src/tracks/handle.rs | 27 ++++++++++++++++++++++++--- src/tracks/mod.rs | 3 ++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/tracks/handle.rs b/src/tracks/handle.rs index da32339..0f6c7fb 100644 --- a/src/tracks/handle.rs +++ b/src/tracks/handle.rs @@ -1,6 +1,9 @@ use super::*; -use crate::events::{Event, EventData, EventHandler}; -use std::time::Duration; +use crate::{ + events::{Event, EventData, EventHandler}, + input::Metadata, +}; +use std::{sync::Arc, time::Duration}; use tokio::sync::{ mpsc::{error::SendError, UnboundedSender}, oneshot, @@ -20,6 +23,7 @@ pub struct TrackHandle { command_channel: UnboundedSender, seekable: bool, uuid: Uuid, + metadata: Arc, } impl TrackHandle { @@ -27,11 +31,17 @@ impl TrackHandle { /// the underlying [`Input`] supports seek operations. /// /// [`Input`]: crate::input::Input - pub fn new(command_channel: UnboundedSender, seekable: bool, uuid: Uuid) -> Self { + pub fn new( + command_channel: UnboundedSender, + seekable: bool, + uuid: Uuid, + metadata: Metadata, + ) -> Self { Self { command_channel, seekable, uuid, + metadata: Arc::new(metadata), } } @@ -157,6 +167,17 @@ impl TrackHandle { self.uuid } + /// Returns the metadata stored in the handle. + /// + /// Metadata is cloned from the inner [`Input`] at + /// the time a track/handle is created, and is effectively + /// read-only from then on. + /// + /// [`Input`]: crate::input::Input + pub fn metadata(&self) -> Arc { + self.metadata.clone() + } + #[inline] /// Send a raw command to the [`Track`] object. /// diff --git a/src/tracks/mod.rs b/src/tracks/mod.rs index 51a37e9..2b67eae 100644 --- a/src/tracks/mod.rs +++ b/src/tracks/mod.rs @@ -366,7 +366,8 @@ impl Track { pub fn create_player(source: Input) -> (Track, TrackHandle) { let (tx, rx) = mpsc::unbounded_channel(); let can_seek = source.is_seekable(); - let handle = TrackHandle::new(tx, can_seek, Uuid::new_v4()); + let metadata = source.metadata.clone(); + let handle = TrackHandle::new(tx, can_seek, Uuid::new_v4(), metadata); let player = Track::new_raw(source, rx, handle.clone());