Compare commits
No commits in common. "e2902e862deb5a1928b5c29fd351715c7f4b1134" and "9ff36afb150f4f65779fbe429fe776bb0314fd9b" have entirely different histories.
e2902e862d
...
9ff36afb15
5 changed files with 36 additions and 20 deletions
|
@ -17,7 +17,7 @@ pub use playbin_song::Song as PlaybinSong;
|
|||
pub mod subsonic;
|
||||
|
||||
pub mod playbin;
|
||||
pub use playbin::Playbin;
|
||||
pub type Playbin = playbin::Playbin<PlaybinSong>;
|
||||
|
||||
mod signal;
|
||||
pub use signal::{Signal, SignalEmitter, SignalHandler};
|
||||
|
|
|
@ -1,14 +1,25 @@
|
|||
use crate::mpv;
|
||||
use crate::signal::{Signal, SignalEmitter};
|
||||
use crate::Event;
|
||||
use crate::PlaybinSong as Song;
|
||||
use event_listener::EventListener;
|
||||
use std::cell::{Ref, RefCell};
|
||||
use tracing::{event, span, Level};
|
||||
use url::Url;
|
||||
|
||||
pub struct Playbin {
|
||||
pub trait PlaybinEntry {
|
||||
fn url(&self) -> Url;
|
||||
}
|
||||
|
||||
impl PlaybinEntry for Url {
|
||||
fn url(&self) -> Url {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
// E: generic entry type
|
||||
pub struct Playbin<E> {
|
||||
mpv: mpv::Handle,
|
||||
entries: RefCell<Vec<Song>>,
|
||||
entries: RefCell<Vec<E>>,
|
||||
|
||||
sender: async_broadcast::Sender<Event>,
|
||||
|
||||
|
@ -18,7 +29,10 @@ pub struct Playbin {
|
|||
file_started: SignalEmitter<Self, ()>,
|
||||
}
|
||||
|
||||
impl Playbin {
|
||||
impl<E> Playbin<E>
|
||||
where
|
||||
E: PlaybinEntry,
|
||||
{
|
||||
pub fn new(sender: async_broadcast::Sender<Event>) -> Self {
|
||||
let mpv = mpv::Handle::new();
|
||||
mpv.set_property("audio-client-name", "audrey").unwrap();
|
||||
|
@ -102,14 +116,14 @@ impl Playbin {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn entries(&self) -> Ref<'_, [Song]> {
|
||||
pub fn entries(&self) -> Ref<'_, [E]> {
|
||||
Ref::map(self.entries.borrow(), Vec::as_ref)
|
||||
}
|
||||
|
||||
pub fn push_entry(&self, entry: Song) {
|
||||
pub fn push_entry(&self, entry: E) {
|
||||
let mut entries = self.entries.borrow_mut();
|
||||
self.mpv
|
||||
.command(["loadfile", &entry.stream_url(), "append-play"])
|
||||
.command(["loadfile", entry.url().as_str(), "append-play"])
|
||||
.unwrap();
|
||||
let index = entries.len();
|
||||
entries.push(entry);
|
||||
|
@ -120,15 +134,10 @@ impl Playbin {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn insert_entry(&self, index: usize, entry: Song) {
|
||||
pub fn insert_entry(&self, index: usize, entry: E) {
|
||||
let mut entries = self.entries.borrow_mut();
|
||||
self.mpv
|
||||
.command([
|
||||
"loadfile",
|
||||
&entry.stream_url(),
|
||||
"insert-at-play",
|
||||
index.to_string(),
|
||||
])
|
||||
.command(["loadfile", entry.url().as_str(), "insert-at-play"])
|
||||
.unwrap();
|
||||
entries.insert(index, entry);
|
||||
|
||||
|
@ -237,8 +246,8 @@ impl Playbin {
|
|||
|
||||
// sanity check
|
||||
assert_eq!(
|
||||
self.entries()[self.current_entry().unwrap()].stream_url(),
|
||||
self.mpv.get_property::<String>("path").unwrap()
|
||||
self.entries()[self.current_entry().unwrap()].url().as_str(),
|
||||
&self.mpv.get_property::<String>("path").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -259,7 +268,7 @@ impl Playbin {
|
|||
}
|
||||
}
|
||||
|
||||
impl Drop for Playbin {
|
||||
impl<E> Drop for Playbin<E> {
|
||||
fn drop(&mut self) {
|
||||
event!(Level::DEBUG, "dropping Playbin2");
|
||||
self.mpv.command(["quit"]).unwrap();
|
||||
|
|
|
@ -73,3 +73,9 @@ impl Song {
|
|||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::playbin::PlaybinEntry for Song {
|
||||
fn url(&self) -> url::Url {
|
||||
url::Url::parse(&self.stream_url()).unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -163,6 +163,7 @@ mod imp {
|
|||
}
|
||||
}
|
||||
|
||||
use crate::PlaybinSong;
|
||||
use adw::prelude::*;
|
||||
use glib::Object;
|
||||
use gtk::glib;
|
||||
|
|
|
@ -32,7 +32,7 @@ mod imp {
|
|||
pub(super) playbin: Rc<Playbin>,
|
||||
pub(super) api: RefCell<Option<Rc<crate::subsonic::Client>>>,
|
||||
|
||||
pub(super) _sender: async_broadcast::Sender<crate::Event>,
|
||||
pub(super) sender: async_broadcast::Sender<crate::Event>,
|
||||
pub(super) inactive_receiver: async_broadcast::InactiveReceiver<crate::Event>,
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ mod imp {
|
|||
setup: Default::default(),
|
||||
playbin: Rc::new(Playbin::new(sender.clone())),
|
||||
api: Default::default(),
|
||||
_sender: sender,
|
||||
sender,
|
||||
inactive_receiver: receiver.deactivate(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue