Skip to content

Truly independent web browser

LibWeb: Resume media resource selection after a moveBefore()

sideshowbarker196c0e0d2ee2
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index f4ddda59aa4..cd12ff20c16 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -72,6 +72,7 @@
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLInsertion.h>
#include <LibWeb/HTML/HTMLLegendElement.h>
+#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/HTMLOptGroupElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
@@ -2124,11 +2125,17 @@ WebIDL::ExceptionOr<void> Node::move_node(Node& new_parent, Node* child)
old_parent->invalidate_html_collection_caches_in_ancestors(affects_elements);
new_parent.invalidate_html_collection_caches_in_ancestors(affects_elements);
- // NB: A move runs no children changed steps, which is where an <html> element publishes which child is the body.
+ // NB: A move runs no children changed steps, which is where an <html> element publishes which child is the body,
+ // and where a media element keeps its resource selection pointer in place and resumes a selection that waits
+ // for a new source.
for (auto* parent : { old_parent, &new_parent }) {
if (auto* html_element = as_if<HTML::HTMLHtmlElement>(*parent))
html_element->publish_body_construction_facts();
}
+ if (auto* media_element = as_if<HTML::HTMLMediaElement>(*old_parent))
+ media_element->child_moved_out(*this, old_previous_sibling);
+ if (auto* media_element = as_if<HTML::HTMLMediaElement>(new_parent))
+ media_element->child_moved_in();
old_parent->bump_dom_tree_version();
new_parent.bump_dom_tree_version();
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index 01f0d77dca4..56bd6c1f621 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -1268,6 +1268,20 @@ void HTMLMediaElement::children_changed(ChildrenChangedMetadata const& metadata)
}
}
+void HTMLMediaElement::child_moved_out(DOM::Node const& child, GC::Ptr<DOM::Node> old_previous_sibling)
+{
+ if (!m_source_element_selector)
+ return;
+ m_source_element_selector->node_removed(child, old_previous_sibling);
+ m_source_element_selector->process_next_candidate();
+}
+
+void HTMLMediaElement::child_moved_in()
+{
+ if (m_source_element_selector)
+ m_source_element_selector->process_next_candidate();
+}
+
// https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithm
void HTMLMediaElement::select_resource()
{
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index 663c40074f7..d14747f2107 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -62,6 +62,10 @@ class HTMLMediaElement : public HTMLElement {
WEB_WRAPPABLE(HTMLMediaElement, HTMLElement);
public:
+ // NB: A move runs no children changed steps, so the move algorithm reports these instead.
+ void child_moved_out(DOM::Node const& child, GC::Ptr<DOM::Node> old_previous_sibling);
+ void child_moved_in();
+
static constexpr size_t audio_tracks_offset() { return offsetof(HTMLMediaElement, m_audio_tracks); }
static constexpr size_t video_tracks_offset() { return offsetof(HTMLMediaElement, m_video_tracks); }
static constexpr size_t text_tracks_offset() { return offsetof(HTMLMediaElement, m_text_tracks); }
diff --git a/Libraries/LibWeb/HTML/HTMLSourceElement.cpp b/Libraries/LibWeb/HTML/HTMLSourceElement.cpp
index 3c69849debf..d6dab6f3fd6 100644
--- a/Libraries/LibWeb/HTML/HTMLSourceElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSourceElement.cpp
@@ -59,7 +59,15 @@ void HTMLSourceElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<DOM::N
// The source HTML element moving steps, given movedNode, isSubtreeRoot, and oldAncestor are:
Base::moved_from(is_subtree_root, old_ancestor);
- // 1. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
+ // 1. If movedNode's parent is a media element that has no src attribute and whose networkState has the value
+ // NETWORK_EMPTY, then invoke that media element's resource selection algorithm.
+ if (auto* media_element = as_if<HTMLMediaElement>(parent()); media_element
+ && !media_element->has_attribute(HTML::AttributeNames::src)
+ && media_element->network_state() == HTMLMediaElement::NetworkState::Empty) {
+ media_element->select_resource();
+ }
+
+ // 2. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
// children: if child is an img element, then count this as a relevant mutation for child.
if (is_subtree_root == IsSubtreeRoot::Yes) {
if (auto* picture = as_if<HTMLPictureElement>(old_ancestor.ptr()))
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-element-moved-while-waiting.txt b/Tests/LibWeb/Text/expected/HTML/media-source-element-moved-while-waiting.txt
new file mode 100644
index 00000000000..99e957fafdc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-element-moved-while-waiting.txt
@@ -0,0 +1,2 @@
+failed source moved out: loaded metadata: 160x120
+new source moved in: loaded metadata: 160x120
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-moved-into-empty-video.txt b/Tests/LibWeb/Text/expected/HTML/media-source-moved-into-empty-video.txt
new file mode 100644
index 00000000000..7a6b412c7c9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-moved-into-empty-video.txt
@@ -0,0 +1,2 @@
+networkState before the move: 0
+after the move: loaded metadata
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-element-moved-while-waiting.html b/Tests/LibWeb/Text/input/HTML/media-source-element-moved-while-waiting.html
new file mode 100644
index 00000000000..cd8ddd95891
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-element-moved-while-waiting.html
@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="elsewhere"></div>
+<script>
+ // Resolves once resource selection has failed on the source and waits for another one. The selection goes on to
+ // wait after the error event's dispatch, in the same task, so check in a later one.
+ async function selectionWaits(video, source) {
+ await new Promise(resolve => source.addEventListener("error", resolve, { once: true }));
+ await new Promise(resolve => setTimeout(resolve, 0));
+ if (video.networkState !== HTMLMediaElement.NETWORK_NO_SOURCE)
+ println(`not waiting: networkState ${video.networkState}`);
+ }
+
+ async function waitingVideo() {
+ // Resource selection fails on a source without a src and waits for another one.
+ const video = document.createElement("video");
+ const source = document.createElement("source");
+ const waits = selectionWaits(video, source);
+ video.appendChild(source);
+ document.body.appendChild(video);
+ await waits;
+ return video;
+ }
+
+ function playableSource() {
+ const source = document.createElement("source");
+ source.src = "../../../Assets/h264-reordered-frames.mp4";
+ return source;
+ }
+
+ function loadedMetadata(video, label) {
+ return new Promise(resolve => {
+ video.addEventListener("loadedmetadata", () => {
+ println(`${label}: loaded metadata: ${video.videoWidth}x${video.videoHeight}`);
+ resolve();
+ });
+ });
+ }
+
+ asyncTest(async done => {
+ const elsewhere = document.getElementById("elsewhere");
+
+ // The failed source is moved out of the element with moveBefore(), and a new source appended.
+ const movedOut = await waitingVideo();
+ const movedOutLoaded = loadedMetadata(movedOut, "failed source moved out");
+ elsewhere.moveBefore(movedOut.firstChild, null);
+ movedOut.appendChild(playableSource());
+ await movedOutLoaded;
+
+ // A new source is moved into the element with moveBefore().
+ const movedIn = await waitingVideo();
+ const movedInLoaded = loadedMetadata(movedIn, "new source moved in");
+ const source = playableSource();
+ elsewhere.appendChild(source);
+ movedIn.moveBefore(source, null);
+ await movedInLoaded;
+
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-moved-into-empty-video.html b/Tests/LibWeb/Text/input/HTML/media-source-moved-into-empty-video.html
new file mode 100644
index 00000000000..43deadc711e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-moved-into-empty-video.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ // A video with no src and no children hasn't started resource selection. Moving a source into it starts it.
+ const video = document.createElement("video");
+ const elsewhere = document.createElement("div");
+ const source = document.createElement("source");
+ source.src = "../../../Assets/h264-reordered-frames.mp4";
+ elsewhere.appendChild(source);
+ document.body.append(video, elsewhere);
+ println(`networkState before the move: ${video.networkState}`);
+ const loaded = new Promise(resolve => video.addEventListener("loadedmetadata", () => resolve("loaded metadata")));
+ const timedOut = new Promise(resolve => setTimeout(() => resolve("never loaded"), 2000));
+ video.moveBefore(source, null);
+ println(`after the move: ${await Promise.race([loaded, timedOut])}`);
+ done();
+ });
+</script>