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: (),
#[property(type = u32, get = Self::playlist_count)]
_playlist_count: (),
pub(super) queued_seek: Cell<Option<f64>>,
}
impl Default for Window {
@ -95,6 +97,8 @@ mod imp {
_duration: (),
_idle_active: (),
_playlist_count: (),
queued_seek: Cell::new(None),
}
}
}
@ -242,6 +246,23 @@ mod imp {
// ^ 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:?}"),
}
}
@ -369,12 +390,17 @@ mod imp {
}
fn time_pos(&self) -> f64 {
match self.mpv.get_property("time-pos") {
Ok(time_pos) => Ok(time_pos),
Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder
Err(err) => Err(err),
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") {
Ok(time_pos) => Ok(time_pos),
Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder
Err(err) => Err(err),
}
.unwrap()
}
.unwrap()
}
fn duration(&self) -> f64 {
@ -498,9 +524,11 @@ impl Window {
.command(["seek", &new_position.to_string(), "absolute", "exact"])
{
Ok(()) => {}
// TODO: buffer this on fail (usually happens when the file isn't done loading)
// to repro, mash the seek forward button
Err(err) => event!(Level::ERROR, "could seek to {new_position}: {err}"),
Err(err) => {
event!(Level::INFO, "queuing seek to {new_position}: {err}");
self.imp().queued_seek.set(Some(new_position));
self.notify("time-pos");
}
}
}
}