Skip to content

Commit

Permalink
TrackHandle: add metadata field (#25)
Browse files Browse the repository at this point in the history
* Adds metadata field to TrackHandle.

Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
  • Loading branch information
peppizza and FelixMcFelix authored Dec 4, 2020
1 parent 94157b1 commit 57df3fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/tracks/handle.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,18 +23,25 @@ pub struct TrackHandle {
command_channel: UnboundedSender<TrackCommand>,
seekable: bool,
uuid: Uuid,
metadata: Arc<Metadata>,
}

impl TrackHandle {
/// Creates a new handle, using the given command sink and hint as to whether
/// the underlying [`Input`] supports seek operations.
///
/// [`Input`]: crate::input::Input
pub fn new(command_channel: UnboundedSender<TrackCommand>, seekable: bool, uuid: Uuid) -> Self {
pub fn new(
command_channel: UnboundedSender<TrackCommand>,
seekable: bool,
uuid: Uuid,
metadata: Metadata,
) -> Self {
Self {
command_channel,
seekable,
uuid,
metadata: Arc::new(metadata),
}
}

Expand Down Expand Up @@ -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<Metadata> {
self.metadata.clone()
}

#[inline]
/// Send a raw command to the [`Track`] object.
///
Expand Down
3 changes: 2 additions & 1 deletion src/tracks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 57df3fe

Please sign in to comment.