Compare commits

..

2 commits

Author SHA1 Message Date
48c7e29eca Fixed buffering for GStreamer 2024-05-20 04:03:38 -05:00
dd7f447d46 Implemented for the GStreamer backend 2024-05-20 02:32:37 -05:00
5 changed files with 308 additions and 263 deletions

View file

@ -73,7 +73,7 @@ impl<P: Player> Controller<P> {
queue: Queue::default(),
config: config_.clone(),
library,
player: Box::new(P::new()),
player: Box::new(P::new()?),
})
}

View file

@ -1,7 +1,7 @@
// Crate things
//use crate::music_controller::config::Config;
use crate::music_storage::library::URI;
use crossbeam_channel::unbounded;
use crossbeam_channel::{unbounded, Receiver, Sender};
use std::error::Error;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
@ -13,29 +13,10 @@ use gstreamer::prelude::*;
// Extra things
use chrono::Duration;
use thiserror::Error;
use super::player::{Player, PlayerError};
use super::player::{Player, PlayerCommand, PlayerError, PlayerState};
#[derive(Debug)]
pub enum GstCmd {
Play,
Pause,
Eos,
AboutToFinish,
}
#[derive(Debug, PartialEq, Eq)]
pub enum GstState {
Playing,
Paused,
Ready,
Buffering(u8),
Null,
VoidPending,
}
impl From<gst::State> for GstState {
impl From<gst::State> for PlayerState {
fn from(value: gst::State) -> Self {
match value {
gst::State::VoidPending => Self::VoidPending,
@ -47,7 +28,7 @@ impl From<gst::State> for GstState {
}
}
impl TryInto<gst::State> for GstState {
impl TryInto<gst::State> for PlayerState {
fn try_into(self) -> Result<gst::State, Box<dyn Error>> {
match self {
Self::VoidPending => Ok(gst::State::VoidPending),
@ -63,191 +44,48 @@ impl TryInto<gst::State> for GstState {
}
#[derive(Debug, PartialEq, Eq)]
enum PlaybackStats {
enum PlaybackInfo {
Idle,
Switching,
Playing{
start: Duration,
end: Duration,
},
Finished // When this is sent, the thread will die!
/// When this is sent, the thread will die! Use it when the [Player] is
/// done playing
Finished
}
/// An instance of a music player with a GStreamer backend
#[derive(Debug)]
pub struct GStreamer {
source: Option<URI>,
//pub message_tx: Sender<PlayerCmd>,
pub message_rx: crossbeam::channel::Receiver<GstCmd>,
playback_tx: crossbeam::channel::Sender<PlaybackStats>,
message_rx: crossbeam::channel::Receiver<PlayerCommand>,
playback_tx: crossbeam::channel::Sender<PlaybackInfo>,
playbin: Arc<RwLock<Element>>,
volume: f64,
start: Option<Duration>,
end: Option<Duration>,
paused: bool,
paused: Arc<RwLock<bool>>,
position: Arc<RwLock<Option<Duration>>>,
}
impl From<gst::StateChangeError> for PlayerError {
fn from(value: gst::StateChangeError) -> Self {
PlayerError::StateChange(value.to_string())
}
}
impl From<glib::BoolError> for PlayerError {
fn from(value: glib::BoolError) -> Self {
PlayerError::General(value.to_string())
}
}
impl GStreamer {
pub fn new() -> Result<Self, PlayerError> {
// Initialize GStreamer, maybe figure out how to nicely fail here
gst::init()?;
let ctx = glib::MainContext::default();
let _guard = ctx.acquire();
let mainloop = glib::MainLoop::new(Some(&ctx), false);
let playbin_arc = Arc::new(RwLock::new(
gst::ElementFactory::make("playbin3").build()?,
));
let playbin = playbin_arc.clone();
let flags = playbin.read().unwrap().property_value("flags");
let flags_class = FlagsClass::with_type(flags.type_()).unwrap();
// Set up the Playbin flags to only play audio
let flags = flags_class
.builder_with_value(flags)
.ok_or(PlayerError::Build)?
.set_by_nick("audio")
.set_by_nick("download")
.unset_by_nick("video")
.unset_by_nick("text")
.build()
.ok_or(PlayerError::Build)?;
playbin.write().unwrap().set_property_from_value("flags", &flags);
playbin.write().unwrap().set_property("instant-uri", true);
let position = Arc::new(RwLock::new(None));
// Set up the thread to monitor the position
let (playback_tx, playback_rx) = unbounded();
let (stat_tx, stat_rx) = unbounded::<PlaybackStats>();
let position_update = Arc::clone(&position);
let _playback_monitor = std::thread::spawn(move || { //TODO: Figure out how to return errors nicely in threads
let mut stats = PlaybackStats::Idle;
let mut pos_temp;
loop {
// Check for new messages or updates about how to proceed
if let Ok(res) = stat_rx.recv_timeout(std::time::Duration::from_millis(100)) {
stats = res
}
pos_temp = playbin_arc
.read()
.unwrap()
.query_position::<ClockTime>()
.map(|pos| Duration::nanoseconds(pos.nseconds() as i64));
match stats {
PlaybackStats::Playing{start, end} if pos_temp.is_some() => {
// Check if the current playback position is close to the end
let finish_point = end - Duration::milliseconds(250);
if pos_temp.unwrap() >= end {
let _ = playback_tx.try_send(GstCmd::Eos);
playbin_arc
.write()
.unwrap()
.set_state(gst::State::Ready)
.expect("Unable to set the pipeline state");
} else if pos_temp.unwrap() >= finish_point {
let _ = playback_tx.try_send(GstCmd::AboutToFinish);
}
// This has to be done AFTER the current time in the file
// is calculated, or everything else is wrong
pos_temp = Some(pos_temp.unwrap() - start)
},
PlaybackStats::Finished => {
*position_update.write().unwrap() = None;
break
},
PlaybackStats::Idle | PlaybackStats::Switching => {},
_ => ()
}
*position_update.write().unwrap() = pos_temp;
}
});
// Set up the thread to monitor bus messages
let playbin_bus_ctrl = Arc::clone(&playbin);
let bus_watch = playbin
.read()
.unwrap()
.bus()
.expect("Failed to get GStreamer message bus")
.add_watch(move |_bus, msg| {
match msg.view() {
gst::MessageView::Eos(_) => {}
gst::MessageView::StreamStart(_) => println!("Stream start"),
gst::MessageView::Error(_) => {
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Ready)
.unwrap();
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Playing)
.unwrap();
}
/* TODO: Fix buffering!!
gst::MessageView::Buffering(buffering) => {
let percent = buffering.percent();
if percent < 100 {
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Paused)
.unwrap();
} else if !(buffering) {
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Playing)
.unwrap();
}
}
*/
_ => (),
}
glib::ControlFlow::Continue
})
.expect("Failed to connect to GStreamer message bus");
// Set up a thread to watch the messages
std::thread::spawn(move || {
let _watch = bus_watch;
mainloop.run()
});
let source = None;
Ok(Self {
source,
playbin,
message_rx: playback_rx,
playback_tx: stat_tx,
volume: 1.0,
start: None,
end: None,
paused: false,
position,
})
}
pub fn source(&self) -> &Option<URI> {
&self.source
}
pub fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError> {
self.set_source(next_track)
}
/// Set the playback URI
fn set_source(&mut self, source: &URI) -> Result<(), PlayerError> {
if !source.exists().is_ok_and(|x| x) {
@ -256,7 +94,7 @@ impl GStreamer {
}
// Make sure the playback tracker knows the stuff is stopped
self.playback_tx.send(PlaybackStats::Switching).unwrap();
self.playback_tx.send(PlaybackInfo::Switching).unwrap();
let uri = self.playbin.read().unwrap().property_value("current-uri");
self.source = Some(source.clone());
@ -272,7 +110,7 @@ impl GStreamer {
self.end = Some(Duration::from_std(*end).unwrap());
// Send the updated position to the tracker
self.playback_tx.send(PlaybackStats::Playing{
self.playback_tx.send(PlaybackInfo::Playing{
start: self.start.unwrap(),
end: self.end.unwrap()
}).unwrap();
@ -286,7 +124,8 @@ impl GStreamer {
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
panic!("Couldn't seek to beginning of cue track in reasonable time (>20ms)");
//panic!("Couldn't seek to beginning of cue track in reasonable time (>20ms)");
return Err(PlayerError::StateChange("Could not seek to beginning of CUE track".into()))
}
_ => {
self.playbin
@ -307,7 +146,7 @@ impl GStreamer {
self.end = self.raw_duration();
// Send the updated position to the tracker
self.playback_tx.send(PlaybackStats::Playing{
self.playback_tx.send(PlaybackInfo::Playing{
start: self.start.unwrap(),
end: self.end.unwrap()
}).unwrap();
@ -341,62 +180,212 @@ impl GStreamer {
Ok(element)
}
/// Set the playback volume, accepts a float from 0 to 1
pub fn set_volume(&mut self, volume: f64) {
self.volume = volume.clamp(0.0, 1.0);
self.set_gstreamer_volume(self.volume);
}
/// Set volume of the internal playbin player, can be
/// used to bypass the main volume control for seeking
fn set_gstreamer_volume(&mut self, volume: f64) {
self.playbin_mut().unwrap().set_property("volume", volume)
}
/// Returns the current volume level, a float from 0 to 1
pub fn volume(&mut self) -> f64 {
self.volume
}
fn set_state(&mut self, state: gst::State) -> Result<(), gst::StateChangeError> {
self.playbin_mut().unwrap().set_state(state)?;
Ok(())
}
pub fn ready(&mut self) -> Result<(), gst::StateChangeError> {
self.set_state(gst::State::Ready)
fn raw_duration(&self) -> Option<Duration> {
self.playbin()
.unwrap()
.query_duration::<ClockTime>()
.map(|pos| Duration::nanoseconds(pos.nseconds() as i64))
}
/// Get the current state of the playback
fn state(&mut self) -> PlayerState {
self.playbin().unwrap().current_state().into()
/*
match *self.buffer.read().unwrap() {
None => self.playbin().unwrap().current_state().into(),
Some(value) => PlayerState::Buffering(value),
}
*/
}
fn property(&self, property: &str) -> glib::Value {
self.playbin().unwrap().property_value(property)
}
}
impl Player for GStreamer {
fn new() -> Result<Self, PlayerError> {
// Initialize GStreamer, maybe figure out how to nicely fail here
if let Err(err) = gst::init() {
return Err(PlayerError::Init(err.to_string()))
};
let ctx = glib::MainContext::default();
let _guard = ctx.acquire();
let mainloop = glib::MainLoop::new(Some(&ctx), false);
let playbin_arc = Arc::new(RwLock::new(
match gst::ElementFactory::make("playbin3").build() {
Ok(playbin) => playbin,
Err(error) => return Err(PlayerError::Init(error.to_string())),
}
));
let playbin = playbin_arc.clone();
let flags = playbin.read().unwrap().property_value("flags");
let flags_class = FlagsClass::with_type(flags.type_()).unwrap();
// Set up the Playbin flags to only play audio
let flags = flags_class
.builder_with_value(flags)
.ok_or(PlayerError::Build)?
.set_by_nick("audio")
.set_by_nick("download")
.unset_by_nick("video")
.unset_by_nick("text")
.build()
.ok_or(PlayerError::Build)?;
playbin.write().unwrap().set_property_from_value("flags", &flags);
playbin.write().unwrap().set_property("instant-uri", true);
let position = Arc::new(RwLock::new(None));
// Set up the thread to monitor the position
let (playback_tx, playback_rx) = unbounded();
let (status_tx, status_rx) = unbounded::<PlaybackInfo>();
let position_update = Arc::clone(&position);
let _playback_monitor =
std::thread::spawn(|| playback_monitor(playbin_arc, status_rx, playback_tx, position_update));
// Set up the thread to monitor bus messages
let playbin_bus_ctrl = Arc::clone(&playbin);
let paused = Arc::new(RwLock::new(false));
let bus_paused = Arc::clone(&paused);
let bus_watch = playbin
.read()
.unwrap()
.bus()
.expect("Failed to get GStreamer message bus")
.add_watch(move |_bus, msg| {
match msg.view() {
gst::MessageView::Eos(_) => println!("End of stream"),
gst::MessageView::StreamStart(_) => println!("Stream start"),
gst::MessageView::Error(err) => {
println!("Error recieved: {}", err);
return glib::ControlFlow::Break
}
gst::MessageView::Buffering(buffering) => {
if *bus_paused.read().unwrap() == true {
return glib::ControlFlow::Continue
}
// If the player is not paused, pause it
let percent = buffering.percent();
if percent < 100 {
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Paused)
.unwrap();
} else if percent >= 100 {
println!("Finished buffering");
playbin_bus_ctrl
.write()
.unwrap()
.set_state(gst::State::Playing)
.unwrap();
}
}
_ => (),
}
glib::ControlFlow::Continue
})
.expect("Failed to connect to GStreamer message bus");
// Set up a thread to watch the messages
std::thread::spawn(move || {
let _watch = bus_watch;
mainloop.run()
});
let source = None;
Ok(Self {
source,
playbin,
message_rx: playback_rx,
playback_tx: status_tx,
volume: 1.0,
start: None,
end: None,
paused,
position,
})
}
fn source(&self) -> &Option<URI> {
&self.source
}
/// Insert a new track to be played. This method should be called at the
/// beginning to start playback of something, and once the [PlayerCommand]
/// indicates the track is about to finish to enqueue gaplessly.
fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError> {
self.set_source(next_track)
}
/// Set the playback volume, accepts a float from 0 to 1
fn set_volume(&mut self, volume: f64) {
self.volume = volume.clamp(0.0, 1.0);
self.set_gstreamer_volume(self.volume);
}
/// Returns the current volume level, a float from 0 to 1
fn volume(&mut self) -> f64 {
self.volume
}
fn ready(&mut self) -> Result<(), PlayerError> {
self.set_state(gst::State::Ready)?;
Ok(())
}
/// If the player is paused or stopped, starts playback
pub fn play(&mut self) -> Result<(), gst::StateChangeError> {
self.set_state(gst::State::Playing)
fn play(&mut self) -> Result<(), PlayerError> {
*self.paused.write().unwrap() = false;
self.set_state(gst::State::Playing)?;
Ok(())
}
/// Pause, if playing
pub fn pause(&mut self) -> Result<(), gst::StateChangeError> {
//*self.paused.write().unwrap() = true;
self.set_state(gst::State::Paused)
fn pause(&mut self) -> Result<(), PlayerError> {
*self.paused.write().unwrap() = true;
self.set_state(gst::State::Paused)?;
Ok(())
}
/// Resume from being paused
pub fn resume(&mut self) -> Result<(), gst::StateChangeError> {
//*self.paused.write().unwrap() = false;
self.set_state(gst::State::Playing)
fn resume(&mut self) -> Result<(), PlayerError> {
*self.paused.write().unwrap() = false;
self.set_state(gst::State::Playing)?;
Ok(())
}
/// Check if playback is paused
pub fn is_paused(&mut self) -> bool {
fn is_paused(&mut self) -> bool {
self.playbin().unwrap().current_state() == gst::State::Paused
}
/// Get the current playback position of the player
pub fn position(&mut self) -> Option<Duration> {
fn position(&mut self) -> Option<Duration> {
*self.position.read().unwrap()
}
/// Get the duration of the currently playing track
pub fn duration(&mut self) -> Option<Duration> {
fn duration(&mut self) -> Option<Duration> {
if self.end.is_some() && self.start.is_some() {
Some(self.end.unwrap() - self.start.unwrap())
} else {
@ -404,18 +393,11 @@ impl GStreamer {
}
}
pub fn raw_duration(&self) -> Option<Duration> {
self.playbin()
.unwrap()
.query_duration::<ClockTime>()
.map(|pos| Duration::nanoseconds(pos.nseconds() as i64))
}
/// Seek relative to the current position
pub fn seek_by(&mut self, seek_amount: Duration) -> Result<(), Box<dyn Error>> {
fn seek_by(&mut self, seek_amount: Duration) -> Result<(), PlayerError> {
let time_pos = match *self.position.read().unwrap() {
Some(pos) => pos,
None => return Err("No position".into()),
None => return Err(PlayerError::Seek("No position".into())),
};
let seek_pos = time_pos + seek_amount;
@ -424,15 +406,15 @@ impl GStreamer {
}
/// Seek absolutely
pub fn seek_to(&mut self, target_pos: Duration) -> Result<(), Box<dyn Error>> {
fn seek_to(&mut self, target_pos: Duration) -> Result<(), PlayerError> {
let start = if self.start.is_none() {
return Err("Failed to seek: No START time".into());
return Err(PlayerError::Seek("No START time".into()));
} else {
self.start.unwrap()
};
let end = if self.end.is_none() {
return Err("Failed to seek: No END time".into());
return Err(PlayerError::Seek("No END time".into()));
} else {
self.end.unwrap()
};
@ -451,28 +433,13 @@ impl GStreamer {
Ok(())
}
/// Get the current state of the playback
pub fn state(&mut self) -> GstState {
self.playbin().unwrap().current_state().into()
/*
match *self.buffer.read().unwrap() {
None => self.playbin().unwrap().current_state().into(),
Some(value) => PlayerState::Buffering(value),
}
*/
}
pub fn property(&self, property: &str) -> glib::Value {
self.playbin().unwrap().property_value(property)
}
/// Stop the playback entirely
pub fn stop(&mut self) -> Result<(), gst::StateChangeError> {
fn stop(&mut self) -> Result<(), PlayerError> {
self.pause()?;
self.ready()?;
// Send the updated position to the tracker
self.playback_tx.send(PlaybackStats::Idle).unwrap();
self.playback_tx.send(PlaybackInfo::Idle).unwrap();
// Set all positions to none
*self.position.write().unwrap() = None;
@ -480,9 +447,12 @@ impl GStreamer {
self.end = None;
Ok(())
}
}
// impl Player for GStreamer {}
/// Return a reference to the player message channel
fn message_channel(&self) -> &crossbeam::channel::Receiver<PlayerCommand> {
&self.message_rx
}
}
impl Drop for GStreamer {
/// Cleans up the `GStreamer` pipeline and the monitoring
@ -492,6 +462,63 @@ impl Drop for GStreamer {
.unwrap()
.set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state");
let _ = self.playback_tx.send(PlaybackStats::Finished);
let _ = self.playback_tx.send(PlaybackInfo::Finished);
}
}
fn playback_monitor(
playbin: Arc<RwLock<Element>>,
status_rx: Receiver<PlaybackInfo>,
playback_tx: Sender<PlayerCommand>,
position: Arc<RwLock<Option<Duration>>>,
) {
let mut stats = PlaybackInfo::Idle;
let mut pos_temp;
let mut sent_atf = false;
loop {
// Check for new messages to decide how to proceed
if let Ok(result) = status_rx.recv_timeout(std::time::Duration::from_millis(10)) {
stats = result
}
pos_temp = playbin
.read()
.unwrap()
.query_position::<ClockTime>()
.map(|pos| Duration::nanoseconds(pos.nseconds() as i64));
match stats {
PlaybackInfo::Playing{start, end} if pos_temp.is_some() => {
// Check if the current playback position is close to the end
let finish_point = end - Duration::milliseconds(250);
if pos_temp.unwrap().num_microseconds() >= end.num_microseconds() {
let _ = playback_tx.try_send(PlayerCommand::EndOfStream);
playbin
.write()
.unwrap()
.set_state(gst::State::Ready)
.expect("Unable to set the pipeline state");
sent_atf = false
} else if pos_temp.unwrap().num_microseconds() >= finish_point.num_microseconds()
&& !sent_atf {
let _ = playback_tx.try_send(PlayerCommand::AboutToFinish);
sent_atf = true;
}
// This has to be done AFTER the current time in the file
// is calculated, or everything else is wrong
pos_temp = Some(pos_temp.unwrap() - start)
},
PlaybackInfo::Finished => {
*position.write().unwrap() = None;
break
},
PlaybackInfo::Idle | PlaybackInfo::Switching => {
sent_atf = false
},
_ => ()
}
*position.write().unwrap() = pos_temp;
}
}

View file

@ -1,17 +1,16 @@
use chrono::Duration;
use gstreamer as gst;
use thiserror::Error;
use crate::music_storage::library::URI;
#[derive(Error, Debug)]
pub enum PlayerError {
#[error("player initialization failed")]
Init(#[from] glib::Error),
#[error("element factory failed to create playbin3")]
Factory(#[from] glib::BoolError),
#[error("player initialization failed: {0}")]
Init(String),
#[error("could not change playback state")]
StateChange(#[from] gst::StateChangeError),
StateChange(String),
#[error("seeking failed: {0}")]
Seek(String),
#[error("the file or source is not found")]
NotFound,
#[error("failed to build gstreamer item")]
@ -19,11 +18,31 @@ pub enum PlayerError {
#[error("poison error")]
Poison,
#[error("general player error")]
General,
General(String),
}
#[derive(Debug, PartialEq, Eq)]
pub enum PlayerState {
Playing,
Paused,
Ready,
Buffering(u8),
Null,
VoidPending,
}
#[derive(Debug, PartialEq, Eq)]
pub enum PlayerCommand {
Play,
Pause,
EndOfStream,
AboutToFinish,
}
pub trait Player {
fn new() -> Self;
/// Create a new player
fn new() -> Result<Self, PlayerError> where Self: Sized;
fn source(&self) -> &Option<URI>;
fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError>;
@ -48,9 +67,9 @@ pub trait Player {
fn duration(&mut self) -> Option<Duration>;
fn raw_duration(&self) -> Option<Duration>;
fn seek_by(&mut self, seek_amount: Duration) -> Result<(), PlayerError>;
fn seek_to(&mut self, target_pos: Duration) -> Result<(), PlayerError>;
fn message_channel(&self) -> &crossbeam::channel::Receiver<PlayerCommand>;
}

View file

@ -218,8 +218,8 @@ impl Song {
self.tags.remove(target_key);
}
/// Creates a `Song` from a song file
pub fn from_file(target_file: &Path) -> Result<Self, Box<dyn Error>> {
/// Creates a `Song` from a music file
pub fn from_file<P: ?Sized + AsRef<Path>>(target_file: &P) -> Result<Self, Box<dyn Error>> {
let normal_options = ParseOptions::new().parsing_mode(lofty::ParsingMode::Relaxed);
let blank_tag = &lofty::Tag::new(TagType::Id3v2);
@ -283,7 +283,7 @@ impl Song {
}
// Find images around the music file that can be used
let mut found_images = find_images(target_file).unwrap();
let mut found_images = find_images(target_file.as_ref()).unwrap();
album_art.append(&mut found_images);
// Get the format as a string
@ -548,7 +548,7 @@ impl URI {
match self {
URI::Local(loc) => loc.try_exists(),
URI::Cue { location, .. } => location.try_exists(),
URI::Remote(_, _loc) => todo!(),
URI::Remote(_, _loc) => Ok(true), // TODO: Investigate a way to do this?
}
}
}

View file

@ -84,7 +84,6 @@ pub fn find_images(song_path: &Path) -> Result<Vec<AlbumArt>, Box<dyn Error>> {
.filter(|e| e.depth() < 3)
// Don't recurse very deep
{
println!("{:?}", target_file);
let path = target_file.path();
if !path.is_file() || !path.exists() {
continue;