S026: drop the update-end settle work-around; remove() reports its own updateend (gst_mse's remove is a stub)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-12 11:17:20 -06:00
commit ce682bcae7

View file

@ -9,8 +9,7 @@
use std::any::Any;
use std::ops::Range;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::sync::Arc;
use glib::object::ObjectType;
use glib::translate::ToGlibPtr;
@ -68,27 +67,15 @@ impl MediaSourceBackend for GStreamerMediaSource {
let index = self.source.source_buffers().length() as usize - 1;
let source_buffer = Arc::new(GStreamerSourceBuffer {
buffer,
pending: Arc::new(Mutex::new(None)),
index,
events: self.events.clone(),
});
// gst_mse 1.28 signals update-end once the bytes reach its parser thread, before the
// coded frames are in the track buffers (and again every second while idle). Wait for
// `buffered` to move, at most SETTLE, before telling the DOM the append is done.
// Nacre's patched gst_mse (vendor/gst-mse) signals update-end once the appended samples
// are in the track buffers, and only for an append.
let sink = self.events.clone();
let weak = Arc::downgrade(&source_buffer);
source_buffer.buffer.connect_on_update_end(move |_| {
let Some(this) = weak.upgrade() else { return };
let Some(before) = this.pending.lock().unwrap().take() else {
return; // nothing outstanding: the idle tick
};
let sink = sink.clone();
std::thread::spawn(move || {
let start = Instant::now();
while this.buffered() == before && start.elapsed() < SETTLE {
std::thread::sleep(Duration::from_millis(5));
}
sink(MseEvent::UpdateEnd { buffer: index });
});
});
source_buffer
.buffer
.connect_on_update_end(move |_| sink(MseEvent::UpdateEnd { buffer: index }));
let sink = self.events.clone();
source_buffer
.buffer
@ -134,23 +121,14 @@ impl MediaSourceBackend for GStreamerMediaSource {
}
}
const SETTLE: Duration = Duration::from_millis(250);
struct GStreamerSourceBuffer {
buffer: gst_mse::SourceBuffer,
/// `buffered` when the outstanding append or removal started.
pending: Arc<Mutex<Option<Vec<Range<f64>>>>>,
}
impl GStreamerSourceBuffer {
fn start(&self) {
*self.pending.lock().unwrap() = Some(self.buffered());
}
index: usize,
events: MseEventSink,
}
impl SourceBufferBackend for GStreamerSourceBuffer {
fn append_buffer(&self, data: Vec<u8>) -> Result<(), MseError> {
self.start();
self.buffer
.append_buffer(gst::Buffer::from_mut_slice(data))
.map_err(error)
@ -161,9 +139,12 @@ impl SourceBufferBackend for GStreamerSourceBuffer {
}
fn remove(&self, start: f64, end: f64) -> Result<(), MseError> {
self.start();
let end = if end.is_infinite() { None } else { Some(clock(end)) };
self.buffer.remove(Some(clock(start)), end).map_err(error)
self.buffer.remove(Some(clock(start)), end).map_err(error)?;
// gst_mse 1.28.9's remove is a stub that removes nothing and signals nothing, so the
// removal ends here (the DOM gets updateend through its task queue, after Remove returns).
(self.events)(MseEvent::UpdateEnd { buffer: self.index });
Ok(())
}
fn buffered(&self) -> Vec<Range<f64>> {