12 KiB
gst_mse patches (S026)
Nacre carries its own build of GStreamer's MSE library (libgstmse-1.0) and plugin (mse, element msesrc) from
gst-plugins-bad 1.28.9 (vendor/gst-mse/, LGPL-2.1, COPYING kept). Every change is marked Nacre patch in the
source; vendor/gst-mse/nacre.patch is the whole diff against the Debian 1.28.9-1 sources. This page is the write-up
for upstream. No merge request has been opened (that needs the user's OK).
Build and test: tools/gst-mse.sh (library and plugin into target/gst-mse/), tools/gst-mse.sh test (C tests in
tests/gst-mse/mse-fixes.c). Every test passes on the patched build; the tests for 1–3 fail on the system 1.28.9
(LD_LIBRARY_PATH=/usr/lib, no GST_PLUGIN_PATH).
1. update-end fires before the append is parsed
What. append_to_buffer_task_func (gstsourcebuffer.c) calls gst_append_pipeline_append() and signals
on-update / on-update-end right after. gst_append_pipeline_append() only queues the buffer and an
end-of-append event in appsrc. The samples reach the track buffers later, when the append pipeline's background
task handles the end-of-append bus message (consume_all_samples).
Why it matters. Players read SourceBuffer.buffered at updateend to decide what to fetch next. With 1.28.9 it
is still empty (or one append behind), so a player either fetches the same range again or stalls. Spec: the
"segment parser loop" runs the coded frame processing before "the buffer append" algorithm fires update and
updateend.
Fix. GstAppendPipeline counts appends sent and appends done (appends_sent, appends_done, a mutex and a
cond). gst_append_pipeline_append() takes a ticket and waits until the background task has handled that append's
end-of-append (after consume_all_samples), or the pipeline shut down, aborted or failed (finish_appends(all)),
or 10 s passed (a warning; a stuck parser must not leave the buffer updating forever). The append task drops the
source buffer's object lock while it waits, because the new-sample callback takes that lock; updating stays set, so
no second append can start, and the task re-checks is_removed after re-locking.
Test. /mse/update-end-after-parse: append a whole WebM clip (init + media) in one call; at the first
update-end, gst_source_buffer_get_buffered() must be non-empty. System 1.28.9: 0 ranges.
2. update / update-end every second while idle
What. The append task waits for pending data with a 1 s timeout. On timeout it took the "no pending data" branch
and called append_successful_unlocked(), which schedules on-update and on-update-end.
Why it matters. A page sees update/updateend without having appended anything, every second. Players that
chain the next append on updateend append twice, or hit InvalidStateError because they think the buffer is free.
Fix. The "no pending data" branch returns without signalling.
Test. /mse/no-idle-updates: no signals during 3 s idle; one append gives exactly one update and one
update-end; 3 s more idle add none. System 1.28.9: 3 updates in the first 3 s.
3. msesrc exposes streams before every SourceBuffer has an init segment
What. gst_mse_src_emit_streams() (gstmsesrc.c) runs on every SourceBuffer's first init segment and exposes the
tracks it knows right away. update_ready_state_for_init_segment() checks whether every buffer has an init segment,
but only to set HAVE_METADATA, and its "not yet" path returned with media_source_lock still held.
Why it matters. Sites with separate audio and video SourceBuffers (YouTube always) append one init segment before
the other. msesrc exposed the first buffer's track alone, playbin prerolled on it and never took the second, so the
element stayed at HAVE_NOTHING. The leaked mutex then deadlocked the ready-state task and every later caller of
MEDIA_SOURCE_LOCK (attach, detach, seek). Spec: "init segment received" step 5 moves to HAVE_METADATA only once
every SourceBuffer in activeSourceBuffers has received one; WebKit's GStreamer MSE also waits for all of them.
Fix. update_ready_state_for_init_segment() returns whether every buffer has its init segment (TRUE once past
HAVE_NOTHING, or when detached), always unlocks, and releases the buffer list it was leaking. emit_streams()
returns early, exposing nothing, until it is TRUE. Each call passes all tracks of all buffers, so the last init
segment exposes every stream at once with one stream collection.
Test. /mse/split-audio-video: playbin3 on mse://, a video-only WebM (VP9) SourceBuffer and an audio-only
fMP4 (AAC) SourceBuffer; the video clip is appended, then 2 s later the audio. During those 2 s msesrc has
n-video = n-audio = 0 and ready-state = HAVE_NOTHING; after the audio append both are 1, and playback passes
1 s. System 1.28.9: n-video = 1 after the video append alone.
4. msesrc pad task gives up when its peer is not active yet
What. pad_task (gstmsesrc.c) waits until its pad is linked, pops a sample from the track and pushes it. Under
playbin3, urisourcebin links each new msesrc pad to a fresh typefind element and only then activates that element,
and linking resumes the pad task. A push in that gap returns GST_FLOW_FLUSHING; the task treated it as a flush,
paused for good, and the popped sample was lost. That stream never started, playbin3 waited for it, and the element
stayed at HAVE_NOTHING. Which stream lost depends on the order of the streams hash table and on timing: about one
start in four in Nacre's split smoke page, one in eight of the C test under CPU load, none when idle.
Why it matters. It hits every multi-stream MediaSource, but split audio/video (patch 3, YouTube) makes it common, because both pads appear at once.
Fix. Before taking data from the track, the task checks whether the peer pad is still flushing (not activated); if so it sleeps 10 ms and returns, so the task loops until the peer is ready. Sticky events pushed meanwhile stay on the pad and go out with the first buffer.
Test. /mse/split-audio-video runs its scenario in SPLIT_ROUNDS fresh processes (default 4) with playbin3 set up
like Servo's player (appsink sinks, download flag, 500 MiB buffer-size). The race needs load to show there (2 of
15 runs failed while a smoke loop ran, 0 of 30 on an idle machine), so the evidence is Nacre's
tests/smoke/mse.txt ?split page: 10 of 10 played with the patch, against 6 stalls in 23 runs before it.
5. remove() is a stub (S027)
What. gst_source_buffer_remove() returned TRUE and removed nothing, so buffered never shrank and players that
trim old media (YouTube does, to stay under quota) kept growing the buffer until eviction.
Fix. It removes, synchronously, every coded frame group (key frame to key frame) that lies inside [start, end)
from every track buffer (gst_media_source_track_buffer_remove_range, the helper eviction already uses), then
recomputes the size. A group that only overlaps the range stays, so no kept frame loses its key frame; the spec removes
partial groups up to the next random access point, so Nacre keeps slightly more than a spec browser. Nacre's backend
still reports update/updateend itself (the library signals nothing for a removal). Test: /mse/remove.
6. abort() breaks the source buffer (S027)
What. gst_source_buffer_abort() sent end-of-stream into the append pipeline's appsrc. Every later append then
failed, so a page that aborted (on a seek, a quality switch, changeType) could never append again.
Fix. abort drops the pending data; if an append is inside the pipeline it waits for it (new appending /
abort_requested flags and append_done_cond; the append task discards that append's result and signals nothing)
and then runs the spec's "reset parser state" (reset_parser_state_unlocked, as the append error path does) and
clears updating. The next append must start with an initialization segment (the reset forgets the old one; the spec
keeps it). Test: /mse/abort-then-append.
7. Parser reset leaves the old appsinks in the append pipeline (S027)
What. gst_append_pipeline_reset() emptied its track list but left the tracks' appsinks in the bin. The next
initialization segment named its appsink video-0 again, gst_bin_add() refused the duplicate and freed it, and
add_track() used the freed element: a crash on the first append after any reset (abort, changeType, append error).
Fix. The reset sets each old appsink to NULL and removes it from the bin first. Test: /mse/abort-then-append
(abort after the init segment was parsed, then append; crashed before).
8. Track feeds keyed by object, not by track id (S027)
What. After a reset, the new initialization segment brings new GstMediaSourceTrack objects with the same ids.
track_buffers is keyed by id and finds the existing buffer, but track_feeds used g_direct_hash, so the new
samples hit get_track_feed_unlocked: assertion ... failed (fatal in the tests).
Fix. track_feeds uses the same id hash and equality as track_buffers, so samples after a reset reach the
existing feeds and msesrc's streams. Test: /mse/abort-then-append.
9. appendWindowStart refuses every valid value (S027)
What. gst_source_buffer_set_append_window_start() failed when start <= append_window_end, the inverse of the
spec's check, so any start below the end (all valid ones) gave a TYPE error. SourceBuffer.abort() resets the window
to 0..+inf, so it failed too.
Fix. The check is start >= append_window_end (an end of GST_CLOCK_TIME_NONE is +infinity in the unsigned
compare). Test: /mse/remove sets the window.
10. endOfStream never reaches msesrc (S028)
What. Upstream gst_media_source_end_of_stream() relied on abort_all_source_buffers() pushing EOS into each
appsrc, which then reached the track buffers and msesrc. Patch 6 made abort drop data instead, so after
endOfStream() msesrc never sent EOS and the element never fired ended.
Fix. End of stream (no error) marks every track buffer of every source buffer EOS (gst_source_buffer_set_eos,
under TRACKS_LOCK); gst_media_source_open() clears it again when the state was ENDED (a new append reopens the
source; new gst_media_source_track_buffer_clear_eos). gst_source_buffer_append_buffer() drops its own TRACKS_LOCK
around that reopen (it held it, and the clear takes it again: a deadlock that hung the script thread). Test:
mse-player smoke case ended; C test /mse/reopen-append.
11. Coded frames shrink a duration the page set (S028)
What. on_duration_changed() set the duration to the highest buffered end whenever buffers arrived, so a page
that set duration = 5 and appended the first second got its duration cut back to 1.0; the element then clamped a
seek to 3.5 back to 1.0. The spec only raises the duration from coded frames.
Fix. The handler returns when the current duration is valid and not below the new maximum. Test: mse-player
smoke case seekUnbuffered.
12. Coded frames don't raise the duration (S028)
What. on_duration_changed() took only the append pipeline's (demuxer's) duration. For fragmented MP4 with an
empty moov that is 0 or the first fragment's length, so after appending a 5 s clip MediaSource.duration read 1.0
(Chromium: 5.08), and nothing re-ran the handler after later fragments.
Fix. The handler also takes each source buffer's highest buffered end, and every successful append calls it
(outside the source buffer's object lock, before update-end). Test: C test /mse/duration-from-frames.
Seen: /mse/split-audio-video failed its final ready-state check (HAVE_METADATA after playing past 1 s) 2 times
in ~70 rounds after this patch, 0 in 24 before (not conclusive). msesrc reads its position from a pad's
segment.position (the feed's push position, not playback); Nacre's DOM computes readyState itself.
Found, not fixed here
gst_source_buffer_change_content_type()always fails (NotSupported). Nacre's backend accepts a change within the same container (parser reset, the next init segment may bring other codecs) and refuses another container.timestampOffsetis aGstClockTimein the API: negative offsets clamp to 0.- Duration:
gst_media_source_end_of_stream()doesn't apply the spec's duration change; Nacre's DOM does.