player: Implement playback

This commit is contained in:
Elias Projahn 2023-11-03 17:48:27 +01:00
parent 9489aaf2ee
commit c378305465
6 changed files with 335 additions and 31 deletions

View file

@ -18,10 +18,12 @@ use self::{application::MusicusApplication, window::MusicusWindow};
use config::{GETTEXT_PACKAGE, LOCALEDIR, PKGDATADIR};
use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
use gstreamer_player::gst;
use gtk::{gio, glib, prelude::*};
fn main() -> glib::ExitCode {
tracing_subscriber::fmt::init();
gst::init().expect("Failed to initialize GStreamer!");
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8")

View file

@ -1,5 +1,12 @@
use crate::playlist_item::PlaylistItem;
use gtk::{gio, glib, glib::Properties, prelude::*, subclass::prelude::*};
use fragile::Fragile;
use gstreamer_player::gst;
use gtk::{
gio,
glib::{self, Properties},
prelude::*,
subclass::prelude::*,
};
use std::cell::{Cell, OnceCell};
mod imp {
@ -17,34 +24,48 @@ mod imp {
#[property(get, set = Self::set_current_index)]
pub current_index: Cell<u32>,
#[property(get, set)]
pub current_time: Cell<u32>,
pub duration_ms: Cell<u64>,
#[property(get, set)]
pub remaining_time: Cell<u32>,
pub current_time_ms: Cell<u64>,
#[property(get, set = Self::set_position)]
pub position: Cell<f64>,
#[property(get, construct_only)]
pub player: OnceCell<gstreamer_player::Player>,
}
impl MusicusPlayer {
pub fn set_current_index(&self, index: u32) {
let playlist = self.playlist.get().unwrap();
if let Some(item) = playlist.item(self.current_index.get()) {
item.downcast::<PlaylistItem>()
.unwrap()
.set_is_playing(false);
}
self.current_index.set(index);
if let Some(item) = playlist.item(index) {
item.downcast::<PlaylistItem>()
.unwrap()
.set_is_playing(true);
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);
}
}
pub fn set_position(&self, position: f64) {
self.position.set(position);
self.player
.get()
.unwrap()
.seek(gst::ClockTime::from_mseconds(
(position * self.duration_ms.get() as f64) as u64,
));
}
}
@ -55,7 +76,63 @@ mod imp {
}
#[glib::derived_properties]
impl ObjectImpl for MusicusPlayer {}
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();
}
});
}
}
}
glib::wrapper! {
@ -64,14 +141,22 @@ glib::wrapper! {
impl MusicusPlayer {
pub fn new() -> Self {
let player = gstreamer_player::Player::new(
None::<gstreamer_player::PlayerVideoRenderer>,
Some(gstreamer_player::PlayerGMainContextSignalDispatcher::new(
None,
)),
);
glib::Object::builder()
.property("active", false)
.property("playing", false)
.property("playlist", gio::ListStore::new::<PlaylistItem>())
.property("current-index", 0u32)
.property("current-time", 0u32)
.property("remaining-time", 10000u32)
.property("current-time-ms", 0u64)
.property("duration-ms", 60_000u64)
.property("position", 0.0)
.property("player", player)
.build()
}
@ -82,15 +167,21 @@ impl MusicusPlayer {
playlist.append(&track);
}
self.set_active(true);
if !self.active() && playlist.n_items() > 0 {
self.set_active(true);
self.set_current_index(0);
self.play();
}
}
pub fn play(&self) {
self.set_playing(true)
self.player().play();
self.set_playing(true);
}
pub fn pause(&self) {
self.set_playing(false)
self.player().pause();
self.set_playing(false);
}
pub fn current_item(&self) -> Option<PlaylistItem> {

View file

@ -111,17 +111,17 @@ mod imp {
.playlist()
.connect_items_changed(clone!(@weak obj => move |_, _, _, _| obj.imp().update()));
player
.bind_property("current-time", &self.current_time_label.get(), "label")
.transform_to(|_, t: u32| Some(format!("{:0>2}:{:0>2}", t / 60, t % 60)))
.sync_create()
.build();
player.connect_current_time_ms_notify(clone!(@weak obj => move |player| {
let imp = obj.imp();
imp.current_time_label.set_label(&format_time(player.current_time_ms()));
imp.remaining_time_label.set_label(&format_time(player.duration_ms() - player.current_time_ms()));
}));
player
.bind_property("remaining-time", &self.remaining_time_label.get(), "label")
.transform_to(|_, t: u32| Some(format!("{:0>2}:{:0>2}", t / 60, t % 60)))
.sync_create()
.build();
player.connect_duration_ms_notify(clone!(@weak obj => move |player| {
let imp = obj.imp();
imp.current_time_label.set_label(&format_time(player.current_time_ms()));
imp.remaining_time_label.set_label(&format_time(player.duration_ms() - player.current_time_ms()));
}));
player
.bind_property("position", &self.slider.adjustment(), "value")
@ -187,3 +187,15 @@ impl PlayerBar {
}
}
}
fn format_time(time_ms: u64) -> String {
let s = time_ms / 1000;
let (m, s) = (s / 60, s % 60);
let (h, m) = (m / 60, m % 60);
if h > 0 {
format!("{h:0>2}:{m:0>2}:{s:0>2}")
} else {
format!("{m:0>2}:{s:0>2}")
}
}