Skip to content

Truly independent web browser

LibWeb: Mirror a video in its Picture-in-Picture window

Luke Wildea7c60e647eb9
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 40e56bddfb7..d56a950ea4b 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -1110,6 +1110,7 @@ generate_css_implementation()
generate_html_implementation()
+generate_picture_in_picture_implementation()
set(GENERATED_SOURCES
ARIA/AriaRoles.cpp
@@ -1132,6 +1133,7 @@ set(GENERATED_SOURCES
SVG/SVGStyleSheetSource.cpp
HTML/MediaControlsDOM.cpp
HTML/Parser/NamedCharacterReferences.cpp
+ PictureInPicture/PictureInPicturePlayerDOM.cpp
)
ladybird_lib(LibWeb web EXPLICIT_SYMBOL_EXPORT)
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index 624de5bafeb..a4703f647cb 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -2701,6 +2701,9 @@ bool HTMLMediaElement::video_sink_should_tick() const
Layout::ForcedReadScope read { document() };
if (m_video_frame_was_recently_captured)
return true;
+ // Another element, such as one in a Picture-in-Picture window, shows the frames whatever happens to this one's box.
+ if (is_shown_elsewhere())
+ return true;
if (document().visibility_state() != VisibilityState::Visible)
return false;
auto const* layout_node = this->layout_node(read);
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index d7e2727cf6a..b357d0baf0e 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -288,6 +288,7 @@ private:
void attach_selected_video_track_sink(Media::Track const&);
bool video_sink_should_tick() const;
+ virtual bool is_shown_elsewhere() const { return false; }
// Mirrors what PlaybackManager and the compositor were last told; a freshly reserved sink is assumed to tick.
mutable bool m_video_sink_is_ticking { true };
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
index 35e228a88d6..5d28cc77f43 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
@@ -66,6 +66,45 @@ void HTMLVideoElement::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_fetch_controller);
visitor.visit(m_picture_in_picture_window);
visitor.visit(m_pending_picture_in_picture_promises);
+ visitor.visit(m_visual_source);
+}
+
+void HTMLVideoElement::set_visual_source(GC::Ptr<HTMLVideoElement> source)
+{
+ if (m_visual_source == source)
+ return;
+
+ if (auto previous_source = exchange(m_visual_source, source)) {
+ previous_source->m_visual_clones.remove_all_matching([&](auto const& clone) { return !clone || clone.ptr().ptr() == this; });
+ previous_source->video_sink_ticking_inputs_changed();
+ }
+ if (source) {
+ source->m_visual_clones.append(*this);
+ source->video_sink_ticking_inputs_changed();
+ }
+ Painting::push_video_paint_facts(*this);
+}
+
+void HTMLVideoElement::for_each_visual_clone(Function<void(HTMLVideoElement&)> const& callback) const
+{
+ for (auto const& clone : m_visual_clones) {
+ if (clone)
+ callback(*clone);
+ }
+}
+
+void HTMLVideoElement::clear_visual_clones()
+{
+ auto clones = move(m_visual_clones);
+ for (auto const& clone : clones) {
+ if (clone)
+ clone->set_visual_source(nullptr);
+ }
+}
+
+bool HTMLVideoElement::is_shown_elsewhere() const
+{
+ return any_of(m_visual_clones, [](auto const& clone) { return clone.ptr() != nullptr; });
}
void HTMLVideoElement::adopted_from(DOM::Document& old_document)
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.h b/Libraries/LibWeb/HTML/HTMLVideoElement.h
index ec9d0766b63..15c5d15604a 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.h
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.h
@@ -64,6 +64,12 @@ public:
bool has_pending_picture_in_picture_promise(WebIDL::Promise const&) const;
bool take_pending_picture_in_picture_promise(WebIDL::Promise const&);
+ // The element whose frames this element shows in place of its own, such as in a Picture-in-Picture window.
+ GC::Ptr<HTMLVideoElement> visual_source() const { return m_visual_source; }
+ void set_visual_source(GC::Ptr<HTMLVideoElement>);
+ void for_each_visual_clone(Function<void(HTMLVideoElement&)> const&) const;
+ void clear_visual_clones();
+
WebIDL::CallbackType* onenterpictureinpicture();
void set_onenterpictureinpicture(WebIDL::CallbackType*);
WebIDL::CallbackType* onleavepictureinpicture();
@@ -81,6 +87,7 @@ private:
virtual bool supports_dimension_attributes() const override { return true; }
virtual bool is_html_video_element() const override { return true; }
+ virtual bool is_shown_elsewhere() const override;
virtual CSS::ElementBoxKind box_kind() const override;
@@ -101,6 +108,9 @@ private:
GC::Ptr<PictureInPicture::PictureInPictureWindow> m_picture_in_picture_window;
Vector<GC::Ref<WebIDL::Promise>> m_pending_picture_in_picture_promises;
+
+ GC::Ptr<HTMLVideoElement> m_visual_source;
+ Vector<GC::Weak<HTMLVideoElement>> m_visual_clones;
};
}
diff --git a/Libraries/LibWeb/Painting/PaintFacts.cpp b/Libraries/LibWeb/Painting/PaintFacts.cpp
index 8bf8ef7aeb3..b2772002eff 100644
--- a/Libraries/LibWeb/Painting/PaintFacts.cpp
+++ b/Libraries/LibWeb/Painting/PaintFacts.cpp
@@ -216,8 +216,11 @@ static void with_replaced_image_paint_facts(Layout::ImageProvider const& image_p
queue(facts);
}
-static void queue_video_paint_facts(HTML::HTMLVideoElement const& video_element)
+static void queue_video_paint_facts(HTML::HTMLVideoElement const& element)
{
+ // An element that shows another element's frames paints whatever that element would.
+ auto const& video_element = element.visual_source() ? *element.visual_source() : element;
+
Layout::RustFFI::FfiVideoPaintFacts facts {};
switch (video_element.current_representation()) {
case HTML::HTMLVideoElement::Representation::FirstVideoFrame:
@@ -243,8 +246,8 @@ static void queue_video_paint_facts(HTML::HTMLVideoElement const& video_element)
facts.representation = Layout::RustFFI::FfiVideoRepresentation::TransparentBlack;
break;
}
- queue_element_paint_facts(video_element, [&](auto* host, u32 element) {
- Layout::RustFFI::render_state_set_video_paint_facts(host, element, facts);
+ queue_element_paint_facts(element, [&](auto* host, u32 style_node) {
+ Layout::RustFFI::render_state_set_video_paint_facts(host, style_node, facts);
});
}
@@ -293,6 +296,9 @@ void push_replaced_image_paint_facts(DOM::Element const& element, Layout::ImageP
void push_video_paint_facts(HTML::HTMLVideoElement const& video_element)
{
+ video_element.for_each_visual_clone([](HTML::HTMLVideoElement& clone) {
+ push_video_paint_facts(clone);
+ });
if (!video_element.has_layout_box())
return;
queue_video_paint_facts(video_element);
diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
index ce72bd74050..f023f64efcb 100644
--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
@@ -18,6 +18,7 @@
#include <LibWeb/Page/Page.h>
#include <LibWeb/PictureInPicture/PictureInPictureController.h>
#include <LibWeb/PictureInPicture/PictureInPictureEvent.h>
+#include <LibWeb/PictureInPicture/PictureInPicturePlayerDOM.h>
#include <LibWeb/PictureInPicture/PictureInPictureWindow.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/Promise.h>
@@ -145,6 +146,13 @@ void PictureInPictureController::process_pending_operations()
}
}
+// The window's page is a player whose video shows the frames of the video in Picture-in-Picture.
+static void create_player(DOM::Document& document, HTML::HTMLVideoElement& source)
+{
+ PictureInPicturePlayerDOM player { document, *document.body() };
+ as<HTML::HTMLVideoElement>(*player.video).set_visual_source(source);
+}
+
// https://w3c.github.io/picture-in-picture/#request-picture-in-picture
void PictureInPictureController::run_request_steps(Request const& request)
{
@@ -187,6 +195,7 @@ void PictureInPictureController::run_request_steps(Request const& request)
}
auto traversable = HTML::LocalTraversableNavigable::create_for_new_web_view(move(new_web_view), nullptr);
+ create_player(*traversable->active_document(), video);
m_window_awaiting_open = WindowAwaitingOpen { request, traversable };
}
@@ -326,6 +335,7 @@ void PictureInPictureController::close_window(PictureInPictureWindow& window, Wi
{
if (window.is_open()) {
window.close();
+ window.video().clear_visual_clones();
window.traversable().close_top_level_traversable(HTML::LocalTraversableNavigable::PromptToUnload::No);
}
diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.css b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.css
new file mode 100644
index 00000000000..ec2d9427b4e
--- /dev/null
+++ b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.css
@@ -0,0 +1,12 @@
+body {
+ margin: 0;
+ background: black;
+ overflow: hidden;
+}
+
+.video {
+ display: block;
+ width: 100vw;
+ height: 100vh;
+ object-fit: contain;
+}
diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html
new file mode 100644
index 00000000000..b2985bb671a
--- /dev/null
+++ b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html
@@ -0,0 +1,2 @@
+<link rel="stylesheet" href="PictureInPicturePlayer.css">
+<video data-name="video" class="video"></video>
diff --git a/Meta/CMake/libweb_generators.cmake b/Meta/CMake/libweb_generators.cmake
index 4dc7f519c95..414a4812620 100644
--- a/Meta/CMake/libweb_generators.cmake
+++ b/Meta/CMake/libweb_generators.cmake
@@ -208,6 +208,41 @@ function (generate_html_implementation)
list(APPEND LIBWEB_ALL_GENERATED_HEADERS ${HTML_GENERATED_HEADERS})
set(LIBWEB_ALL_GENERATED_HEADERS ${LIBWEB_ALL_GENERATED_HEADERS} PARENT_SCOPE)
endfunction()
+
+function (generate_picture_in_picture_implementation)
+ set(LIBWEB_INPUT_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}")
+
+ invoke_py_generator(
+ "PictureInPicturePlayerDOM.cpp"
+ "generate_dom_tree.py"
+ "${LIBWEB_INPUT_FOLDER}/PictureInPicture/PictureInPicturePlayer.html"
+ "PictureInPicture/PictureInPicturePlayerDOM.h"
+ "PictureInPicture/PictureInPicturePlayerDOM.cpp"
+ arguments -i "${LIBWEB_INPUT_FOLDER}/PictureInPicture/PictureInPicturePlayer.html"
+ -s PictureInPicturePlayerDOM
+ -n "Web::PictureInPicture"
+ --html-tags "${LIBWEB_INPUT_FOLDER}/HTML/TagNames.h"
+ --html-attributes "${LIBWEB_INPUT_FOLDER}/HTML/AttributeNames.h"
+ --svg-tags "${LIBWEB_INPUT_FOLDER}/SVG/TagNames.h"
+ --svg-attributes "${LIBWEB_INPUT_FOLDER}/SVG/AttributeNames.h"
+ dependencies "${LIBWEB_INPUT_FOLDER}/HTML/TagNames.h"
+ "${LIBWEB_INPUT_FOLDER}/HTML/AttributeNames.h"
+ "${LIBWEB_INPUT_FOLDER}/SVG/TagNames.h"
+ "${LIBWEB_INPUT_FOLDER}/SVG/AttributeNames.h"
+ "${LIBWEB_INPUT_FOLDER}/PictureInPicture/PictureInPicturePlayer.css"
+ )
+
+ set(PICTURE_IN_PICTURE_GENERATED_HEADERS
+ "PictureInPicture/PictureInPicturePlayerDOM.h"
+ )
+ list(TRANSFORM PICTURE_IN_PICTURE_GENERATED_HEADERS PREPEND "${CMAKE_CURRENT_BINARY_DIR}/")
+ if (ENABLE_INSTALL_HEADERS)
+ install(FILES ${PICTURE_IN_PICTURE_GENERATED_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/LibWeb/PictureInPicture")
+ endif()
+ list(APPEND LIBWEB_ALL_GENERATED_HEADERS ${PICTURE_IN_PICTURE_GENERATED_HEADERS})
+ set(LIBWEB_ALL_GENERATED_HEADERS ${LIBWEB_ALL_GENERATED_HEADERS} PARENT_SCOPE)
+endfunction()
+
function (generate_js_bindings target)
set(LIBWEB_INPUT_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(Python3 REQUIRED COMPONENTS Interpreter)
diff --git a/Tests/LibWeb/Text/expected/PictureInPicture/video-sink-ticks-while-shown-in-window.txt b/Tests/LibWeb/Text/expected/PictureInPicture/video-sink-ticks-while-shown-in-window.txt
new file mode 100644
index 00000000000..9c78efb5bdf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PictureInPicture/video-sink-ticks-while-shown-in-window.txt
@@ -0,0 +1,2 @@
+ticking while hidden in Picture-in-Picture: true
+ticking while hidden after exiting: false
diff --git a/Tests/LibWeb/Text/input/PictureInPicture/video-sink-ticks-while-shown-in-window.html b/Tests/LibWeb/Text/input/PictureInPicture/video-sink-ticks-while-shown-in-window.html
new file mode 100644
index 00000000000..3e7fcb2ee9b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PictureInPicture/video-sink-ticks-while-shown-in-window.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<video id="video" muted loop src="../../../Assets/test-webm.webm"></video>
+<script>
+ const video = document.getElementById("video");
+
+ asyncTest(async done => {
+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)
+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));
+ await video.play();
+
+ internals.grantTransientActivation();
+ await video.requestPictureInPicture();
+
+ await setSystemVisibilityState("hidden");
+ println(`ticking while hidden in Picture-in-Picture: ${internals.mediaElementVideoSinkIsTicking(video)}`);
+
+ await document.exitPictureInPicture();
+ println(`ticking while hidden after exiting: ${internals.mediaElementVideoSinkIsTicking(video)}`);
+
+ await setSystemVisibilityState("visible");
+ done();
+ });
+</script>