Skip to content

script: Use AtomicRefCell for HTMLMediaElement::current_src - #49087

Merged
jdm merged 1 commit into
servo:mainfrom
CynthiaOketch:media-current-src-atomicrefcell
Oct 20, 2026
Merged

jdm merged 1 commit into
servo:mainfrom
CynthiaOketch:media-current-src-atomicrefcell

Conversation

@CynthiaOketch

Copy link
Copy Markdown
Contributor

Changed HTMLMediaElement::current_src from DomRefCell<String> to AtomicRefCell<String> so layout could read it without borrow_for_layout(). Writes went through safe_borrow_mut(cx.no_gc()), and current_src_url() was no longer unsafe, which removed the unsafe block in HTMLVideoElement's layout code.

Testing: No behaviour change; covered by existing media WPT tests.
Fixes: #49050

@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. S-needs-rebase There are merge conflict errors. labels Oct 20, 2026
Changed `current_src` from `DomRefCell<String>` to `AtomicRefCell<String>`
so layout could read it without `borrow_for_layout()`. Writes went through
`safe_borrow_mut(cx.no_gc())`, and `current_src_url()` was no longer
`unsafe`, which removed the `unsafe` block in `HTMLVideoElement`'s layout
code and the now-unneeded `#[expect(unsafe_code)]` on
`LayoutDom<HTMLVideoElement>::data()`.

Signed-off-by: Cynthia Oketch <[email protected]>
@CynthiaOketch
CynthiaOketch force-pushed the media-current-src-atomicrefcell branch from 6569b7d to 24e6391 Compare October 20, 2026 11:24
@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Oct 20, 2026
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Oct 20, 2026
@jdm
jdm added this pull request to the merge queue Oct 20, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
Merged via the queue into servo:main with commit 2fed593 Oct 20, 2026
35 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 22, 2026
A media element fetched its media resource itself and pushed each chunk
into the player as it arrived. To keep the player from being flooded,
it backed off, suspended the fetch, cancelled it after a timeout, and
restarted it where the player had stopped, after a lost connection, a
416 or a seek, with a cap on the bytes it queued and a document
registry to cancel suspended fetches that nothing would resume. A loop
on a backend without its own buffer, a second element and a reload of
the same resource downloaded it again.

In the resource fetch algorithm, the element now opens a reader of the
media resource in the media cache of the content process instead, keyed
by the requested URL, the CORS mode and the origin of the document, with
a ScriptFetcher for the fetches that the cache needs. Blob and data URLs
take the same path; MediaStream sources are unchanged, and preload=none
still opens no reader until the media resource is needed. If the player
accepts the reader as its data source, it reads from it. Otherwise the
element pushes the data of the reader into the player: once the player
needs data, it reads 64 KiB chunks from the byte where the player
stopped and pushes them until the player has enough, when the refused
chunk is read again later, or until the reader has no data there yet,
when the waker of the reader continues; at most 1 MiB per task, so that
cached data does not block the script thread. SeekData moves that
position, and the end of the stream reaches the player as such.

The resource is keyed on its URL without the fragment, so that
`a.mp4#t=10` and `a.mp4` share it. The internal blob URL of a srcObject
Blob is revoked once the fetcher has claimed the blob.

The events of the reader map onto the media element:
- The metadata of the resource sets the input size and seekability of
  the player, before the first data and only then, with the size first,
  since the OpenHarmony player reads both once. The reader reports the
  metadata again once it learns the length of a resource of unknown
  length, which the player learns as well. A CORS-cross-origin
  response makes the media data not origin-clean until the resource
  fetch algorithm runs again, as before.
- Progress, which the reader reports at most every 350 ms, fires a
  progress event while the download is not suspended.
- A suspended download sets the networkState to NETWORK_IDLE and fires
  suspend, and a resumed one sets it back to NETWORK_LOADING. When the
  download is suspended for preload=metadata, before playback was
  requested, the element stops delaying the load event, as the spec
  asks for a download suspended until the user starts playback.
- Once the whole resource is cached, progress, NETWORK_IDLE and suspend
  follow.
- An error before the media engine has the metadata runs the media data
  processing failure steps, which try the next source, and later, or
  for a resource that changed between two responses, the fatal steps
  with MEDIA_ERR_NETWORK, as other browsers do. So does the failure of
  the first fetch of the element's fetcher.

The element tells the reader what it needs: playback at the bitrate of
the resource, its length divided by its duration once both are known,
while it plays; only the metadata for preload=metadata until playback
is requested, unless it autoplays; and otherwise, as while its document
is not fully active, what a paused reader keeps. Its reader is closed, also
for a player that still holds it, so that the player's reads fail and
the fetch stops, by the load algorithm, before the next source, by the failure and fatal
steps, when the player goes away, and when its document is unloaded
for good or its pipeline exits; documents keep weak references to the
media elements that opened a reader for that and for their activity,
and an adopted element moves to its new document. While it downloads
with a reader, the element has pending activity, which the fetch
listener provided before.

The cache now owns what the element did for fetches, which goes:
BufferedDataSource apart from the position of the push adapter,
HTMLMediaElementFetchContext, the fetch listener of the media data,
backing off, suspension and its timeout with the pref
media_fetch_suspension_timeout_ms, the restarts, the handling of 416,
the queue cap and the document registry of suspended fetches.

The blob URL that the element kept for the fetches it restarted
itself, for a Blob assigned to srcObject, goes too, since nothing reads
it any more.

Media fetches are no longer in the fetch group of the document. A
navigation away from a document with a media download in progress
still cancels it, since that makes the document unsalvageable, so that
its unloading closes the readers; window.stop() and document.open() no
longer cancel media downloads.

Fork note: meant for upstream. Drop once upstream has it.

Testing: the new mozilla/media-blob-url-revoked-seek.html seeks far into
a blob after its URL was revoked (passes 5 of 5; phase 1 fails 5 of 5).
fetch/range/non-matching-range-response.html now passes "Range
response out of range of request should succeed", and
html/browsers/browsing-the-web/read-media/cross-origin-video.html, whose
server answers in parts, now plays (3 of 3 runs, 3 of 3 failures with
phase 1); their expectations go; the other media, canvas and range tests cover the rest. See
Validation for the scenarios that no WPT test covers.

Validation: Linux host release builds with GStreamer, against the phase 1 build of ohos-main. `./mach test-wpt --release --timeout-multiplier 2` of html/semantics/embedded-content/media-elements, the-video-element, html/canvas/element/manual/drawing-images-to-the-canvas and imagebitmap (with drawimage_video_crossorigin_seek.sub.html), fetch/range and mozilla/media-autoplay-seek-after-ended.html, 493 tests: apart from the fixed range test, every unexpected result also occurs with the phase 1 build in isolated runs: load-removes-queued-error-event hangs while GStreamer never reports the metadata of the 539-byte sound_0.mp3 (also outside WPT: 8 of 12 runs here, 6 of 12 there), src_object_blob times out (2 of 16 and 3 of 16), seek-to-max-value fails (0 of 3 and 1 of 3), media_fragment_seek fails (3 of 3 either way), and autoplay-default-permissions-policy passes where a timeout is expected; track-cues-seeking, track-mode-disabled and video_loop_base passed 3 of 3 isolated runs on both. Scenarios with a 14 MB, 30 s H.264 file from a local server, requests and MB sent, with and without range support: play 2 / 14.3 and 1 / 14.3 (phase 1: 3 / 26.6 and 3 / 41.6), loop 2 / 14.3 and 1 / 14.3 (3 / 26.1 and 8 / 83.3), seek back 2 / 14.3 and 1 / 14.3 (3 / 25.9 and 3 / 40.1), two elements 2 / 14.3 and 1 / 14.3 (16 / 76.4 and 6 / 85.6), reload 2 / 14.3 and 1 / 14.3 (9 / 77.5 and 9 / 115.7), play again after the end 2 / 14.3 and 1 / 14.3 (3 / 26.0 and 3 / 40.9); the second request with range support reads the last 40 KB, and a loop, a seek back, a second element and a reload need no request. A 150 MB, 40 s file with preload=metadata at full speed: 1 request and 149.7 MB with and without range support (2 / 217.3 and 2 / 272.8); at 25 MB/s, two elements 2 and 1 requests for 149.7 MB (10 requests, 299.7 and 823.8 MB) and a pause of 40 s 1 request (2 / 155.4 with range support). Peak RSS is lower in every scenario, for example 397 instead of 418 MiB for the reload. At 5 MB/s, load() with another source 6 s in and a navigation away close the connection at 6.0 s, as with phase 1; a paused element removed from its document loads up to the read-ahead of a paused reader in the cache, here the whole file, where phase 1 closed the connection after 17 to 24 s. `./mach check` and `./mach check --ohos` pass; `./mach test-unit size_of` passes with HTMLVideoElement down from 1296 to 1200 bytes; `./mach test-tidy` reports only the known rustls advisories.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: the fetch listener this patch removes had upstream's new listener signatures (51d1117, servo#49032) and `LocalName` initiator type (servo#48953), which the fetcher of patch 99 now uses; imports merged next to upstream's `AtomicRefCell` (servo#49087) and `TracedScriptResult` (servo#48855); the patch's -96 bytes of `HTMLVideoElement` applied to upstream's new size (servo#48082). `./mach check` passes at this commit)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 30, 2026
A media element fetched its media resource itself and pushed each chunk
into the player as it arrived. To keep the player from being flooded,
it backed off, suspended the fetch, cancelled it after a timeout, and
restarted it where the player had stopped, after a lost connection, a
416 or a seek, with a cap on the bytes it queued and a document
registry to cancel suspended fetches that nothing would resume. A loop
on a backend without its own buffer, a second element and a reload of
the same resource downloaded it again.

In the resource fetch algorithm, the element now opens a reader of the
media resource in the media cache of the content process instead, keyed
by the requested URL, the CORS mode and the origin of the document, with
a ScriptFetcher for the fetches that the cache needs. Blob and data URLs
take the same path; MediaStream sources are unchanged, and preload=none
still opens no reader until the media resource is needed. If the player
accepts the reader as its data source, it reads from it. Otherwise the
element pushes the data of the reader into the player: once the player
needs data, it reads 64 KiB chunks from the byte where the player
stopped and pushes them until the player has enough, when the refused
chunk is read again later, or until the reader has no data there yet,
when the waker of the reader continues; at most 1 MiB per task, so that
cached data does not block the script thread. SeekData moves that
position, and the end of the stream reaches the player as such.

The resource is keyed on its URL without the fragment, so that
`a.mp4#t=10` and `a.mp4` share it. The internal blob URL of a srcObject
Blob is revoked once the fetcher has claimed the blob.

The events of the reader map onto the media element:
- The metadata of the resource sets the input size and seekability of
  the player, before the first data and only then, with the size first,
  since the OpenHarmony player reads both once. The reader reports the
  metadata again once it learns the length of a resource of unknown
  length, which the player learns as well. A CORS-cross-origin
  response makes the media data not origin-clean until the resource
  fetch algorithm runs again, as before.
- Progress, which the reader reports at most every 350 ms, fires a
  progress event while the download is not suspended.
- A suspended download sets the networkState to NETWORK_IDLE and fires
  suspend, and a resumed one sets it back to NETWORK_LOADING. When the
  download is suspended for preload=metadata, before playback was
  requested, the element stops delaying the load event, as the spec
  asks for a download suspended until the user starts playback.
- Once the whole resource is cached, progress, NETWORK_IDLE and suspend
  follow.
- An error before the media engine has the metadata runs the media data
  processing failure steps, which try the next source, and later, or
  for a resource that changed between two responses, the fatal steps
  with MEDIA_ERR_NETWORK, as other browsers do. So does the failure of
  the first fetch of the element's fetcher.

The element tells the reader what it needs: playback at the bitrate of
the resource, its length divided by its duration once both are known,
while it plays; only the metadata for preload=metadata until playback
is requested, unless it autoplays; and otherwise, as while its document
is not fully active, what a paused reader keeps. Its reader is closed, also
for a player that still holds it, so that the player's reads fail and
the fetch stops, by the load algorithm, before the next source, by the failure and fatal
steps, when the player goes away, and when its document is unloaded
for good or its pipeline exits; documents keep weak references to the
media elements that opened a reader for that and for their activity,
and an adopted element moves to its new document. While it downloads
with a reader, the element has pending activity, which the fetch
listener provided before.

The cache now owns what the element did for fetches, which goes:
BufferedDataSource apart from the position of the push adapter,
HTMLMediaElementFetchContext, the fetch listener of the media data,
backing off, suspension and its timeout with the pref
media_fetch_suspension_timeout_ms, the restarts, the handling of 416,
the queue cap and the document registry of suspended fetches.

The blob URL that the element kept for the fetches it restarted
itself, for a Blob assigned to srcObject, goes too, since nothing reads
it any more.

Media fetches are no longer in the fetch group of the document. A
navigation away from a document with a media download in progress
still cancels it, since that makes the document unsalvageable, so that
its unloading closes the readers; window.stop() and document.open() no
longer cancel media downloads.

Fork note: meant for upstream. Drop once upstream has it.

Testing: the new mozilla/media-blob-url-revoked-seek.html seeks far into
a blob after its URL was revoked (passes 5 of 5; phase 1 fails 5 of 5).
fetch/range/non-matching-range-response.html now passes "Range
response out of range of request should succeed", and
html/browsers/browsing-the-web/read-media/cross-origin-video.html, whose
server answers in parts, now plays (3 of 3 runs, 3 of 3 failures with
phase 1); their expectations go; the other media, canvas and range tests cover the rest. See
Validation for the scenarios that no WPT test covers.

Validation: Linux host release builds with GStreamer, against the phase 1 build of ohos-main. `./mach test-wpt --release --timeout-multiplier 2` of html/semantics/embedded-content/media-elements, the-video-element, html/canvas/element/manual/drawing-images-to-the-canvas and imagebitmap (with drawimage_video_crossorigin_seek.sub.html), fetch/range and mozilla/media-autoplay-seek-after-ended.html, 493 tests: apart from the fixed range test, every unexpected result also occurs with the phase 1 build in isolated runs: load-removes-queued-error-event hangs while GStreamer never reports the metadata of the 539-byte sound_0.mp3 (also outside WPT: 8 of 12 runs here, 6 of 12 there), src_object_blob times out (2 of 16 and 3 of 16), seek-to-max-value fails (0 of 3 and 1 of 3), media_fragment_seek fails (3 of 3 either way), and autoplay-default-permissions-policy passes where a timeout is expected; track-cues-seeking, track-mode-disabled and video_loop_base passed 3 of 3 isolated runs on both. Scenarios with a 14 MB, 30 s H.264 file from a local server, requests and MB sent, with and without range support: play 2 / 14.3 and 1 / 14.3 (phase 1: 3 / 26.6 and 3 / 41.6), loop 2 / 14.3 and 1 / 14.3 (3 / 26.1 and 8 / 83.3), seek back 2 / 14.3 and 1 / 14.3 (3 / 25.9 and 3 / 40.1), two elements 2 / 14.3 and 1 / 14.3 (16 / 76.4 and 6 / 85.6), reload 2 / 14.3 and 1 / 14.3 (9 / 77.5 and 9 / 115.7), play again after the end 2 / 14.3 and 1 / 14.3 (3 / 26.0 and 3 / 40.9); the second request with range support reads the last 40 KB, and a loop, a seek back, a second element and a reload need no request. A 150 MB, 40 s file with preload=metadata at full speed: 1 request and 149.7 MB with and without range support (2 / 217.3 and 2 / 272.8); at 25 MB/s, two elements 2 and 1 requests for 149.7 MB (10 requests, 299.7 and 823.8 MB) and a pause of 40 s 1 request (2 / 155.4 with range support). Peak RSS is lower in every scenario, for example 397 instead of 418 MiB for the reload. At 5 MB/s, load() with another source 6 s in and a navigation away close the connection at 6.0 s, as with phase 1; a paused element removed from its document loads up to the read-ahead of a paused reader in the cache, here the whole file, where phase 1 closed the connection after 17 to 24 s. `./mach check` and `./mach check --ohos` pass; `./mach test-unit size_of` passes with HTMLVideoElement down from 1296 to 1200 bytes; `./mach test-tidy` reports only the known rustls advisories. 2026-10-29 sync: upstream's unload cleanup now closes the reader of a kept-alive element; on a DAYU200 the media set of patch 65's validation, with a page that navigates away from a playing video and back, ran without a crash with SpiderMonkey and with QuickJS.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: the fetch listener this patch removes had upstream's new listener signatures (51d1117, servo#49032) and `LocalName` initiator type (servo#48953), which the fetcher of patch 99 now uses; imports merged next to upstream's `AtomicRefCell` (servo#49087) and `TracedScriptResult` (servo#48855); the patch's -96 bytes of `HTMLVideoElement` applied to upstream's new size (servo#48082). `./mach check` passes at this commit)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution next to upstream's keep-alive (servo#48979): the keep-alive while the resource downloads moved into upstream's `should_keep_alive`, as patch 65 now extends it; `update_reader_activity` into the split `autoplay` arm; upstream's unload cleanup closes the media reader where it cancelled the fetch, as the rest of this patch does; the removed fetch listener drops upstream's `CorsStatus` edit to it (servo#48748); `HTMLVideoElement` stays at 1168 bytes, measured at this commit with a compile-time probe)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
A media element fetched its media resource itself and pushed each chunk
into the player as it arrived. To keep the player from being flooded,
it backed off, suspended the fetch, cancelled it after a timeout, and
restarted it where the player had stopped, after a lost connection, a
416 or a seek, with a cap on the bytes it queued and a document
registry to cancel suspended fetches that nothing would resume. A loop
on a backend without its own buffer, a second element and a reload of
the same resource downloaded it again.

In the resource fetch algorithm, the element now opens a reader of the
media resource in the media cache of the content process instead, keyed
by the requested URL, the CORS mode and the origin of the document, with
a ScriptFetcher for the fetches that the cache needs. Blob and data URLs
take the same path; MediaStream sources are unchanged, and preload=none
still opens no reader until the media resource is needed. If the player
accepts the reader as its data source, it reads from it. Otherwise the
element pushes the data of the reader into the player: once the player
needs data, it reads 64 KiB chunks from the byte where the player
stopped and pushes them until the player has enough, when the refused
chunk is read again later, or until the reader has no data there yet,
when the waker of the reader continues; at most 1 MiB per task, so that
cached data does not block the script thread. SeekData moves that
position, and the end of the stream reaches the player as such.

The resource is keyed on its URL without the fragment, so that
`a.mp4#t=10` and `a.mp4` share it. The internal blob URL of a srcObject
Blob is revoked once the fetcher has claimed the blob.

The events of the reader map onto the media element:
- The metadata of the resource sets the input size and seekability of
  the player, before the first data and only then, with the size first,
  since the OpenHarmony player reads both once. The reader reports the
  metadata again once it learns the length of a resource of unknown
  length, which the player learns as well. A CORS-cross-origin
  response makes the media data not origin-clean until the resource
  fetch algorithm runs again, as before.
- Progress, which the reader reports at most every 350 ms, fires a
  progress event while the download is not suspended.
- A suspended download sets the networkState to NETWORK_IDLE and fires
  suspend, and a resumed one sets it back to NETWORK_LOADING. When the
  download is suspended for preload=metadata, before playback was
  requested, the element stops delaying the load event, as the spec
  asks for a download suspended until the user starts playback.
- Once the whole resource is cached, progress, NETWORK_IDLE and suspend
  follow.
- An error before the media engine has the metadata runs the media data
  processing failure steps, which try the next source, and later, or
  for a resource that changed between two responses, the fatal steps
  with MEDIA_ERR_NETWORK, as other browsers do. So does the failure of
  the first fetch of the element's fetcher.

The element tells the reader what it needs: playback at the bitrate of
the resource, its length divided by its duration once both are known,
while it plays; only the metadata for preload=metadata until playback
is requested, unless it autoplays; and otherwise, as while its document
is not fully active, what a paused reader keeps. Its reader is closed, also
for a player that still holds it, so that the player's reads fail and
the fetch stops, by the load algorithm, before the next source, by the failure and fatal
steps, when the player goes away, and when its document is unloaded
for good or its pipeline exits; documents keep weak references to the
media elements that opened a reader for that and for their activity,
and an adopted element moves to its new document. While it downloads
with a reader, the element has pending activity, which the fetch
listener provided before.

The cache now owns what the element did for fetches, which goes:
BufferedDataSource apart from the position of the push adapter,
HTMLMediaElementFetchContext, the fetch listener of the media data,
backing off, suspension and its timeout with the pref
media_fetch_suspension_timeout_ms, the restarts, the handling of 416,
the queue cap and the document registry of suspended fetches.

The blob URL that the element kept for the fetches it restarted
itself, for a Blob assigned to srcObject, goes too, since nothing reads
it any more.

Media fetches are no longer in the fetch group of the document. A
navigation away from a document with a media download in progress
still cancels it, since that makes the document unsalvageable, so that
its unloading closes the readers; window.stop() and document.open() no
longer cancel media downloads.

Fork note: meant for upstream. Drop once upstream has it.

Testing: the new mozilla/media-blob-url-revoked-seek.html seeks far into
a blob after its URL was revoked (passes 5 of 5; phase 1 fails 5 of 5).
fetch/range/non-matching-range-response.html now passes "Range
response out of range of request should succeed", and
html/browsers/browsing-the-web/read-media/cross-origin-video.html, whose
server answers in parts, now plays (3 of 3 runs, 3 of 3 failures with
phase 1); their expectations go; the other media, canvas and range tests cover the rest. See
Validation for the scenarios that no WPT test covers.

Validation: Linux host release builds with GStreamer, against the phase 1 build of ohos-main. `./mach test-wpt --release --timeout-multiplier 2` of html/semantics/embedded-content/media-elements, the-video-element, html/canvas/element/manual/drawing-images-to-the-canvas and imagebitmap (with drawimage_video_crossorigin_seek.sub.html), fetch/range and mozilla/media-autoplay-seek-after-ended.html, 493 tests: apart from the fixed range test, every unexpected result also occurs with the phase 1 build in isolated runs: load-removes-queued-error-event hangs while GStreamer never reports the metadata of the 539-byte sound_0.mp3 (also outside WPT: 8 of 12 runs here, 6 of 12 there), src_object_blob times out (2 of 16 and 3 of 16), seek-to-max-value fails (0 of 3 and 1 of 3), media_fragment_seek fails (3 of 3 either way), and autoplay-default-permissions-policy passes where a timeout is expected; track-cues-seeking, track-mode-disabled and video_loop_base passed 3 of 3 isolated runs on both. Scenarios with a 14 MB, 30 s H.264 file from a local server, requests and MB sent, with and without range support: play 2 / 14.3 and 1 / 14.3 (phase 1: 3 / 26.6 and 3 / 41.6), loop 2 / 14.3 and 1 / 14.3 (3 / 26.1 and 8 / 83.3), seek back 2 / 14.3 and 1 / 14.3 (3 / 25.9 and 3 / 40.1), two elements 2 / 14.3 and 1 / 14.3 (16 / 76.4 and 6 / 85.6), reload 2 / 14.3 and 1 / 14.3 (9 / 77.5 and 9 / 115.7), play again after the end 2 / 14.3 and 1 / 14.3 (3 / 26.0 and 3 / 40.9); the second request with range support reads the last 40 KB, and a loop, a seek back, a second element and a reload need no request. A 150 MB, 40 s file with preload=metadata at full speed: 1 request and 149.7 MB with and without range support (2 / 217.3 and 2 / 272.8); at 25 MB/s, two elements 2 and 1 requests for 149.7 MB (10 requests, 299.7 and 823.8 MB) and a pause of 40 s 1 request (2 / 155.4 with range support). Peak RSS is lower in every scenario, for example 397 instead of 418 MiB for the reload. At 5 MB/s, load() with another source 6 s in and a navigation away close the connection at 6.0 s, as with phase 1; a paused element removed from its document loads up to the read-ahead of a paused reader in the cache, here the whole file, where phase 1 closed the connection after 17 to 24 s. `./mach check` and `./mach check --ohos` pass; `./mach test-unit size_of` passes with HTMLVideoElement down from 1296 to 1200 bytes; `./mach test-tidy` reports only the known rustls advisories. 2026-10-29 sync: upstream's unload cleanup now closes the reader of a kept-alive element; on a DAYU200 the media set of patch 65's validation, with a page that navigates away from a playing video and back, ran without a crash with SpiderMonkey and with QuickJS.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: the fetch listener this patch removes had upstream's new listener signatures (51d1117, servo#49032) and `LocalName` initiator type (servo#48953), which the fetcher of patch 99 now uses; imports merged next to upstream's `AtomicRefCell` (servo#49087) and `TracedScriptResult` (servo#48855); the patch's -96 bytes of `HTMLVideoElement` applied to upstream's new size (servo#48082). `./mach check` passes at this commit)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution next to upstream's keep-alive (servo#48979): the keep-alive while the resource downloads moved into upstream's `should_keep_alive`, as patch 65 now extends it; `update_reader_activity` into the split `autoplay` arm; upstream's unload cleanup closes the media reader where it cancelled the fetch, as the rest of this patch does; the removed fetch listener drops upstream's `CorsStatus` edit to it (servo#48748); `HTMLVideoElement` stays at 1168 bytes, measured at this commit with a compile-time probe)
AI-assisted: Yes (agent session; 2026-11-09 sync -- fetch_request() stays deleted although upstream gave it a blob URL claim (servo#49206): the media cache's fetcher of patch 89 takes the same claim; its imports go with it)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 10, 2026
A media element fetched its media resource itself and pushed each chunk
into the player as it arrived. To keep the player from being flooded,
it backed off, suspended the fetch, cancelled it after a timeout, and
restarted it where the player had stopped, after a lost connection, a
416 or a seek, with a cap on the bytes it queued and a document
registry to cancel suspended fetches that nothing would resume. A loop
on a backend without its own buffer, a second element and a reload of
the same resource downloaded it again.

In the resource fetch algorithm, the element now opens a reader of the
media resource in the media cache of the content process instead, keyed
by the requested URL, the CORS mode and the origin of the document, with
a ScriptFetcher for the fetches that the cache needs. Blob and data URLs
take the same path; MediaStream sources are unchanged, and preload=none
still opens no reader until the media resource is needed. If the player
accepts the reader as its data source, it reads from it. Otherwise the
element pushes the data of the reader into the player: once the player
needs data, it reads 64 KiB chunks from the byte where the player
stopped and pushes them until the player has enough, when the refused
chunk is read again later, or until the reader has no data there yet,
when the waker of the reader continues; at most 1 MiB per task, so that
cached data does not block the script thread. SeekData moves that
position, and the end of the stream reaches the player as such.

The resource is keyed on its URL without the fragment, so that
`a.mp4#t=10` and `a.mp4` share it. The internal blob URL of a srcObject
Blob is revoked once the fetcher has claimed the blob.

The events of the reader map onto the media element:
- The metadata of the resource sets the input size and seekability of
  the player, before the first data and only then, with the size first,
  since the OpenHarmony player reads both once. The reader reports the
  metadata again once it learns the length of a resource of unknown
  length, which the player learns as well. A CORS-cross-origin
  response makes the media data not origin-clean until the resource
  fetch algorithm runs again, as before.
- Progress, which the reader reports at most every 350 ms, fires a
  progress event while the download is not suspended.
- A suspended download sets the networkState to NETWORK_IDLE and fires
  suspend, and a resumed one sets it back to NETWORK_LOADING. When the
  download is suspended for preload=metadata, before playback was
  requested, the element stops delaying the load event, as the spec
  asks for a download suspended until the user starts playback.
- Once the whole resource is cached, progress, NETWORK_IDLE and suspend
  follow.
- An error before the media engine has the metadata runs the media data
  processing failure steps, which try the next source, and later, or
  for a resource that changed between two responses, the fatal steps
  with MEDIA_ERR_NETWORK, as other browsers do. So does the failure of
  the first fetch of the element's fetcher.

The element tells the reader what it needs: playback at the bitrate of
the resource, its length divided by its duration once both are known,
while it plays; only the metadata for preload=metadata until playback
is requested, unless it autoplays; and otherwise, as while its document
is not fully active, what a paused reader keeps. Its reader is closed, also
for a player that still holds it, so that the player's reads fail and
the fetch stops, by the load algorithm, before the next source, by the failure and fatal
steps, when the player goes away, and when its document is unloaded
for good or its pipeline exits; documents keep weak references to the
media elements that opened a reader for that and for their activity,
and an adopted element moves to its new document. While it downloads
with a reader, the element has pending activity, which the fetch
listener provided before.

The cache now owns what the element did for fetches, which goes:
BufferedDataSource apart from the position of the push adapter,
HTMLMediaElementFetchContext, the fetch listener of the media data,
backing off, suspension and its timeout with the pref
media_fetch_suspension_timeout_ms, the restarts, the handling of 416,
the queue cap and the document registry of suspended fetches.

The blob URL that the element kept for the fetches it restarted
itself, for a Blob assigned to srcObject, goes too, since nothing reads
it any more.

Media fetches are no longer in the fetch group of the document. A
navigation away from a document with a media download in progress
still cancels it, since that makes the document unsalvageable, so that
its unloading closes the readers; window.stop() and document.open() no
longer cancel media downloads.

Fork note: meant for upstream. Drop once upstream has it.

Testing: the new mozilla/media-blob-url-revoked-seek.html seeks far into
a blob after its URL was revoked (passes 5 of 5; phase 1 fails 5 of 5).
fetch/range/non-matching-range-response.html now passes "Range
response out of range of request should succeed", and
html/browsers/browsing-the-web/read-media/cross-origin-video.html, whose
server answers in parts, now plays (3 of 3 runs, 3 of 3 failures with
phase 1); their expectations go; the other media, canvas and range tests cover the rest. See
Validation for the scenarios that no WPT test covers.

Validation: Linux host release builds with GStreamer, against the phase 1 build of ohos-main. `./mach test-wpt --release --timeout-multiplier 2` of html/semantics/embedded-content/media-elements, the-video-element, html/canvas/element/manual/drawing-images-to-the-canvas and imagebitmap (with drawimage_video_crossorigin_seek.sub.html), fetch/range and mozilla/media-autoplay-seek-after-ended.html, 493 tests: apart from the fixed range test, every unexpected result also occurs with the phase 1 build in isolated runs: load-removes-queued-error-event hangs while GStreamer never reports the metadata of the 539-byte sound_0.mp3 (also outside WPT: 8 of 12 runs here, 6 of 12 there), src_object_blob times out (2 of 16 and 3 of 16), seek-to-max-value fails (0 of 3 and 1 of 3), media_fragment_seek fails (3 of 3 either way), and autoplay-default-permissions-policy passes where a timeout is expected; track-cues-seeking, track-mode-disabled and video_loop_base passed 3 of 3 isolated runs on both. Scenarios with a 14 MB, 30 s H.264 file from a local server, requests and MB sent, with and without range support: play 2 / 14.3 and 1 / 14.3 (phase 1: 3 / 26.6 and 3 / 41.6), loop 2 / 14.3 and 1 / 14.3 (3 / 26.1 and 8 / 83.3), seek back 2 / 14.3 and 1 / 14.3 (3 / 25.9 and 3 / 40.1), two elements 2 / 14.3 and 1 / 14.3 (16 / 76.4 and 6 / 85.6), reload 2 / 14.3 and 1 / 14.3 (9 / 77.5 and 9 / 115.7), play again after the end 2 / 14.3 and 1 / 14.3 (3 / 26.0 and 3 / 40.9); the second request with range support reads the last 40 KB, and a loop, a seek back, a second element and a reload need no request. A 150 MB, 40 s file with preload=metadata at full speed: 1 request and 149.7 MB with and without range support (2 / 217.3 and 2 / 272.8); at 25 MB/s, two elements 2 and 1 requests for 149.7 MB (10 requests, 299.7 and 823.8 MB) and a pause of 40 s 1 request (2 / 155.4 with range support). Peak RSS is lower in every scenario, for example 397 instead of 418 MiB for the reload. At 5 MB/s, load() with another source 6 s in and a navigation away close the connection at 6.0 s, as with phase 1; a paused element removed from its document loads up to the read-ahead of a paused reader in the cache, here the whole file, where phase 1 closed the connection after 17 to 24 s. `./mach check` and `./mach check --ohos` pass; `./mach test-unit size_of` passes with HTMLVideoElement down from 1296 to 1200 bytes; `./mach test-tidy` reports only the known rustls advisories. 2026-10-29 sync: upstream's unload cleanup now closes the reader of a kept-alive element; on a DAYU200 the media set of patch 65's validation, with a page that navigates away from a playing video and back, ran without a crash with SpiderMonkey and with QuickJS.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: the fetch listener this patch removes had upstream's new listener signatures (51d1117, servo#49032) and `LocalName` initiator type (servo#48953), which the fetcher of patch 99 now uses; imports merged next to upstream's `AtomicRefCell` (servo#49087) and `TracedScriptResult` (servo#48855); the patch's -96 bytes of `HTMLVideoElement` applied to upstream's new size (servo#48082). `./mach check` passes at this commit)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution next to upstream's keep-alive (servo#48979): the keep-alive while the resource downloads moved into upstream's `should_keep_alive`, as patch 65 now extends it; `update_reader_activity` into the split `autoplay` arm; upstream's unload cleanup closes the media reader where it cancelled the fetch, as the rest of this patch does; the removed fetch listener drops upstream's `CorsStatus` edit to it (servo#48748); `HTMLVideoElement` stays at 1168 bytes, measured at this commit with a compile-time probe)
AI-assisted: Yes (agent session; 2026-11-09 sync -- fetch_request() stays deleted although upstream gave it a blob URL claim (servo#49206): the media cache's fetcher of patch 89 takes the same claim; its imports go with it)
Signed-off-by: Jonathan Schwender <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace HTMLMediaElement::current_src with AtomicRefCell

3 participants