This commit is contained in:
Erica Z 2024-11-10 17:04:32 +01:00
parent 57b6024845
commit c4f17817e4

View file

@ -53,7 +53,7 @@ mod imp {
#[property(get)] #[property(get)]
albums_model: gio::ListStore, albums_model: gio::ListStore,
#[property(type = i64, get = Self::volume, set = Self::set_volume, minimum = 0, maximum = 100)] #[property(type = i64, get = Self::volume, set = Self::set_volume)]
_volume: (), _volume: (),
#[property(type = bool, get = Self::mute, set = Self::set_mute)] #[property(type = bool, get = Self::mute, set = Self::set_mute)]
_mute: (), _mute: (),
@ -102,6 +102,8 @@ mod imp {
// "Useful to drain property changes before a new file is loaded." // "Useful to drain property changes before a new file is loaded."
mpv.add_hook(0, "on_before_start_file", 0).unwrap(); mpv.add_hook(0, "on_before_start_file", 0).unwrap();
// "Useful to drain property changes after a file has finished."
mpv.add_hook(0, "o_after_start_file", 0).unwrap();
Self { Self {
state: Cell::new(State::Idle), state: Cell::new(State::Idle),
@ -354,27 +356,15 @@ mod imp {
} }
fn duration(&self) -> f64 { fn duration(&self) -> f64 {
let duration = match self.mpv.get_property::<f64>("duration") { match self.state.get() {
Ok(duration) => Ok(Some(duration)), State::Idle => 0.0,
Err(err) if err.is_property_unavailable() => {
Ok(self.song().as_ref().map(|song| song.duration() as f64))
}
Err(err) => Err(err),
}
.unwrap();
{ State::FileLoading | State::FileEnded => self.song().unwrap().duration() as f64,
let left = duration.map(|f| f as i64);
let right = self.song().as_ref().map(crate::model::Song::duration);
if left != right {
event!(
Level::WARN,
"mpv duration {left:?} doesn not match subsonic duration {right:?}"
);
}
}
duration.unwrap_or(0.0) // placeholder State::FileLoaded | State::Active | State::Seeking => {
self.mpv.get_property::<f64>("duration").unwrap()
}
}
} }
fn idle_active(&self) -> bool { fn idle_active(&self) -> bool {
@ -461,18 +451,11 @@ mod imp {
} }
fn on_hook(&self, event: crate::mpv::event::HookEvent) { fn on_hook(&self, event: crate::mpv::event::HookEvent) {
match event.reply_userdata { event!(Level::DEBUG, "{} hook triggered", event.name);
0 => {
assert_eq!(&event.name, "on_before_start_file");
event!(Level::DEBUG, "on_before_start_file triggered");
// just use this as a barrier // just use this as a barrier
self.mpv.continue_hook(event.id).unwrap(); self.mpv.continue_hook(event.id).unwrap();
} }
_ => unreachable!(),
}
}
fn on_log_message(&self, event: crate::mpv::event::LogMessageEvent) { fn on_log_message(&self, event: crate::mpv::event::LogMessageEvent) {
let span = span!(Level::DEBUG, "mpv_log", prefix = event.prefix); let span = span!(Level::DEBUG, "mpv_log", prefix = event.prefix);
let _guard = span.enter(); let _guard = span.enter();
@ -575,6 +558,17 @@ mod imp {
self.mpv.get_property::<String>("path").unwrap(), self.mpv.get_property::<String>("path").unwrap(),
self.obj().song().unwrap().stream_url() self.obj().song().unwrap().stream_url()
); );
// same with "duration"
{
let left = self.mpv.get_property::<f64>("duration").unwrap() as i64;
let right = self.song().unwrap().duration();
if left != right {
event!(
Level::WARN,
"mpv duration {left:?} doesn not match subsonic duration {right:?}"
);
}
}
} }
fn on_seek(&self) { fn on_seek(&self) {