musicus/src/player.rs

214 lines
6.3 KiB
Rust
Raw Normal View History

2023-10-25 17:45:32 +02:00
use crate::playlist_item::PlaylistItem;
2023-11-03 17:48:27 +01:00
use fragile::Fragile;
use gstreamer_player::gst;
use gtk::{
gio,
glib::{self, Properties},
prelude::*,
subclass::prelude::*,
};
2023-10-25 17:45:32 +02:00
use std::cell::{Cell, OnceCell};
2023-09-29 21:18:28 +02:00
mod imp {
use super::*;
#[derive(Properties, Debug, Default)]
#[properties(wrapper_type = super::MusicusPlayer)]
pub struct MusicusPlayer {
#[property(get, set)]
pub active: Cell<bool>,
#[property(get, set)]
pub playing: Cell<bool>,
2023-10-25 17:45:32 +02:00
#[property(get, construct_only)]
pub playlist: OnceCell<gio::ListStore>,
2023-10-26 11:48:42 +02:00
#[property(get, set = Self::set_current_index)]
2023-10-25 17:45:32 +02:00
pub current_index: Cell<u32>,
2023-11-03 16:22:58 +01:00
#[property(get, set)]
2023-11-03 17:48:27 +01:00
pub duration_ms: Cell<u64>,
2023-11-03 16:22:58 +01:00
#[property(get, set)]
2023-11-03 17:48:27 +01:00
pub current_time_ms: Cell<u64>,
2023-11-03 16:22:58 +01:00
#[property(get, set = Self::set_position)]
pub position: Cell<f64>,
2023-11-03 17:48:27 +01:00
#[property(get, construct_only)]
pub player: OnceCell<gstreamer_player::Player>,
2023-09-29 21:18:28 +02:00
}
2023-10-26 11:48:42 +02:00
impl MusicusPlayer {
pub fn set_current_index(&self, index: u32) {
let playlist = self.playlist.get().unwrap();
2023-10-27 12:32:40 +02:00
2023-10-26 11:48:42 +02:00
if let Some(item) = playlist.item(index) {
2023-11-03 17:48:27 +01:00
if let Some(old_item) = playlist.item(self.current_index.get()) {
old_item.downcast::<PlaylistItem>()
.unwrap()
.set_is_playing(false);
}
let item = item.downcast::<PlaylistItem>().unwrap();
let uri = glib::filename_to_uri(&item.path(), None)
.expect("track path should be parsable as an URI");
let player = self.player.get().unwrap();
player.set_uri(Some(&uri));
if self.playing.get() {
player.play();
}
self.current_index.set(index);
item.set_is_playing(true);
2023-10-26 11:48:42 +02:00
}
}
2023-11-03 16:22:58 +01:00
pub fn set_position(&self, position: f64) {
2023-11-03 17:48:27 +01:00
self.player
.get()
.unwrap()
.seek(gst::ClockTime::from_mseconds(
(position * self.duration_ms.get() as f64) as u64,
));
2023-11-03 16:22:58 +01:00
}
2023-10-26 11:48:42 +02:00
}
2023-09-29 21:18:28 +02:00
#[glib::object_subclass]
impl ObjectSubclass for MusicusPlayer {
const NAME: &'static str = "MusicusPlayer";
type Type = super::MusicusPlayer;
}
#[glib::derived_properties]
2023-11-03 17:48:27 +01:00
impl ObjectImpl for MusicusPlayer {
fn constructed(&self) {
self.parent_constructed();
let player = self.player.get().unwrap();
let mut config = player.config();
config.set_position_update_interval(250);
player.set_config(config).unwrap();
player.set_video_track_enabled(false);
let obj = Fragile::new(self.obj().to_owned());
player.connect_end_of_stream(move |_| {
obj.get().next();
});
let obj = Fragile::new(self.obj().to_owned());
player.connect_position_updated(move |_, current_time| {
if let Some(current_time) = current_time {
let obj = obj.get();
let imp = obj.imp();
let current_time_ms = current_time.mseconds();
let duration_ms = imp.duration_ms.get();
let mut position = current_time_ms as f64 / duration_ms as f64;
if position > 1.0 {
position = 1.0
}
imp.current_time_ms.set(current_time_ms);
obj.notify_current_time_ms();
imp.position.set(position);
obj.notify_position();
}
});
let obj = Fragile::new(self.obj().to_owned());
player.connect_duration_changed(move |_, duration| {
if let Some(duration) = duration {
let obj = obj.get();
let imp = obj.imp();
let duration_ms = duration.mseconds();
imp.duration_ms.set(duration_ms);
obj.notify_duration_ms();
imp.current_time_ms.set(0);
obj.notify_current_time_ms();
imp.position.set(0.0);
obj.notify_position();
}
});
}
}
2023-09-29 21:18:28 +02:00
}
glib::wrapper! {
pub struct MusicusPlayer(ObjectSubclass<imp::MusicusPlayer>);
}
impl MusicusPlayer {
pub fn new() -> Self {
2023-11-03 17:48:27 +01:00
let player = gstreamer_player::Player::new(
None::<gstreamer_player::PlayerVideoRenderer>,
Some(gstreamer_player::PlayerGMainContextSignalDispatcher::new(
None,
)),
);
2023-10-25 17:45:32 +02:00
glib::Object::builder()
.property("active", false)
.property("playing", false)
.property("playlist", gio::ListStore::new::<PlaylistItem>())
.property("current-index", 0u32)
2023-11-03 17:48:27 +01:00
.property("current-time-ms", 0u64)
.property("duration-ms", 60_000u64)
2023-11-03 16:22:58 +01:00
.property("position", 0.0)
2023-11-03 17:48:27 +01:00
.property("player", player)
2023-10-25 17:45:32 +02:00
.build()
2023-09-29 21:18:28 +02:00
}
2023-10-25 17:45:32 +02:00
pub fn append(&self, tracks: Vec<PlaylistItem>) {
let playlist = self.playlist();
2023-10-27 12:32:40 +02:00
2023-10-25 17:45:32 +02:00
for track in tracks {
playlist.append(&track);
2023-09-29 21:18:28 +02:00
}
2023-11-03 17:48:27 +01:00
if !self.active() && playlist.n_items() > 0 {
self.set_active(true);
self.set_current_index(0);
self.play();
}
2023-10-25 17:45:32 +02:00
}
pub fn play(&self) {
2023-11-03 17:48:27 +01:00
self.player().play();
self.set_playing(true);
2023-09-29 21:18:28 +02:00
}
pub fn pause(&self) {
2023-11-03 17:48:27 +01:00
self.player().pause();
self.set_playing(false);
2023-09-29 21:18:28 +02:00
}
2023-10-27 12:32:40 +02:00
pub fn current_item(&self) -> Option<PlaylistItem> {
let imp = self.imp();
imp.playlist
.get()
.unwrap()
.item(imp.current_index.get())
.and_downcast::<PlaylistItem>()
}
2023-11-03 16:22:58 +01:00
pub fn next(&self) {
if self.current_index() < self.playlist().n_items() - 1 {
self.set_current_index(self.current_index() + 1);
}
}
pub fn previous(&self) {
if self.current_index() > 0 {
self.set_current_index(self.current_index() - 1);
}
}
2023-09-29 21:18:28 +02:00
}
impl Default for MusicusPlayer {
fn default() -> Self {
Self::new()
}
}