attempt at queued seeks

This commit is contained in:
Erica Z 2024-11-05 10:48:50 +01:00
parent 048ab58bf5
commit d3641a6f33

View file

@ -52,6 +52,8 @@ mod imp {
_idle_active: (), _idle_active: (),
#[property(type = u32, get = Self::playlist_count)] #[property(type = u32, get = Self::playlist_count)]
_playlist_count: (), _playlist_count: (),
pub(super) queued_seek: Cell<Option<f64>>,
} }
impl Default for Window { impl Default for Window {
@ -95,6 +97,8 @@ mod imp {
_duration: (), _duration: (),
_idle_active: (), _idle_active: (),
_playlist_count: (), _playlist_count: (),
queued_seek: Cell::new(None),
} }
} }
} }
@ -242,6 +246,23 @@ mod imp {
// ^ ignore // ^ ignore
} }
Event::PlaybackRestart => {
if let Some(queued_seek) = window.imp().queued_seek.take() {
// a seek was tried before and failed, try again now
event!(Level::INFO, "performing queued seek to {queued_seek}");
window.seek(queued_seek);
}
}
Event::EndFile(event) => {
if let Err(err) = event.reason {
event!(Level::ERROR, "end file error: {err}");
}
// cancel queued seek always
window.imp().queued_seek.set(None);
}
_ => event!(Level::DEBUG, "unhandled {event:?}"), _ => event!(Level::DEBUG, "unhandled {event:?}"),
} }
} }
@ -369,6 +390,10 @@ mod imp {
} }
fn time_pos(&self) -> f64 { fn time_pos(&self) -> f64 {
if let Some(queued_seek) = self.queued_seek.get() {
// counterfeit time-pos while the seek is ongoing
queued_seek
} else {
match self.mpv.get_property("time-pos") { match self.mpv.get_property("time-pos") {
Ok(time_pos) => Ok(time_pos), Ok(time_pos) => Ok(time_pos),
Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder
@ -376,6 +401,7 @@ mod imp {
} }
.unwrap() .unwrap()
} }
}
fn duration(&self) -> f64 { fn duration(&self) -> f64 {
let duration = match self.mpv.get_property::<f64>("duration") { let duration = match self.mpv.get_property::<f64>("duration") {
@ -498,9 +524,11 @@ impl Window {
.command(["seek", &new_position.to_string(), "absolute", "exact"]) .command(["seek", &new_position.to_string(), "absolute", "exact"])
{ {
Ok(()) => {} Ok(()) => {}
// TODO: buffer this on fail (usually happens when the file isn't done loading) Err(err) => {
// to repro, mash the seek forward button event!(Level::INFO, "queuing seek to {new_position}: {err}");
Err(err) => event!(Level::ERROR, "could seek to {new_position}: {err}"), self.imp().queued_seek.set(Some(new_position));
self.notify("time-pos");
}
} }
} }
} }