HTMLVideoElement.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibWeb/Bindings/HTMLVideoElementPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/Fetch/Fetching/Fetching.h>
  12. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  13. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  14. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  15. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  16. #include <LibWeb/HTML/HTMLVideoElement.h>
  17. #include <LibWeb/HTML/VideoTrack.h>
  18. #include <LibWeb/HTML/VideoTrackList.h>
  19. #include <LibWeb/Layout/VideoBox.h>
  20. #include <LibWeb/Painting/Paintable.h>
  21. #include <LibWeb/Platform/ImageCodecPlugin.h>
  22. namespace Web::HTML {
  23. JS_DEFINE_ALLOCATOR(HTMLVideoElement);
  24. HTMLVideoElement::HTMLVideoElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  25. : HTMLMediaElement(document, move(qualified_name))
  26. {
  27. }
  28. HTMLVideoElement::~HTMLVideoElement() = default;
  29. void HTMLVideoElement::initialize(JS::Realm& realm)
  30. {
  31. Base::initialize(realm);
  32. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLVideoElement);
  33. }
  34. void HTMLVideoElement::finalize()
  35. {
  36. Base::finalize();
  37. for (auto video_track : video_tracks()->video_tracks())
  38. video_track->stop_video({});
  39. }
  40. void HTMLVideoElement::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_video_track);
  44. visitor.visit(m_fetch_controller);
  45. }
  46. void HTMLVideoElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  47. {
  48. Base::attribute_changed(name, old_value, value);
  49. if (name == HTML::AttributeNames::poster) {
  50. determine_element_poster_frame(value).release_value_but_fixme_should_propagate_errors();
  51. }
  52. }
  53. JS::GCPtr<Layout::Node> HTMLVideoElement::create_layout_node(CSS::StyleProperties style)
  54. {
  55. return heap().allocate_without_realm<Layout::VideoBox>(document(), *this, move(style));
  56. }
  57. Layout::VideoBox* HTMLVideoElement::layout_node()
  58. {
  59. return static_cast<Layout::VideoBox*>(Node::layout_node());
  60. }
  61. Layout::VideoBox const* HTMLVideoElement::layout_node() const
  62. {
  63. return static_cast<Layout::VideoBox const*>(Node::layout_node());
  64. }
  65. // https://html.spec.whatwg.org/multipage/media.html#dom-video-videowidth
  66. u32 HTMLVideoElement::video_width() const
  67. {
  68. // The videoWidth IDL attribute must return the intrinsic width of the video in CSS pixels. The videoHeight IDL
  69. // attribute must return the intrinsic height of the video in CSS pixels. If the element's readyState attribute
  70. // is HAVE_NOTHING, then the attributes must return 0.
  71. if (ready_state() == ReadyState::HaveNothing)
  72. return 0;
  73. return m_video_width;
  74. }
  75. // https://html.spec.whatwg.org/multipage/media.html#dom-video-videoheight
  76. u32 HTMLVideoElement::video_height() const
  77. {
  78. // The videoWidth IDL attribute must return the intrinsic width of the video in CSS pixels. The videoHeight IDL
  79. // attribute must return the intrinsic height of the video in CSS pixels. If the element's readyState attribute
  80. // is HAVE_NOTHING, then the attributes must return 0.
  81. if (ready_state() == ReadyState::HaveNothing)
  82. return 0;
  83. return m_video_height;
  84. }
  85. void HTMLVideoElement::set_video_track(JS::GCPtr<HTML::VideoTrack> video_track)
  86. {
  87. set_needs_style_update(true);
  88. document().set_needs_layout();
  89. if (m_video_track)
  90. m_video_track->pause_video({});
  91. m_video_track = video_track;
  92. }
  93. void HTMLVideoElement::set_current_frame(Badge<VideoTrack>, RefPtr<Gfx::Bitmap> frame, double position)
  94. {
  95. m_current_frame = { move(frame), position };
  96. if (paintable())
  97. paintable()->set_needs_display();
  98. }
  99. void HTMLVideoElement::on_playing()
  100. {
  101. if (m_video_track)
  102. m_video_track->play_video({});
  103. }
  104. void HTMLVideoElement::on_paused()
  105. {
  106. if (m_video_track)
  107. m_video_track->pause_video({});
  108. }
  109. void HTMLVideoElement::on_seek(double position, MediaSeekMode seek_mode)
  110. {
  111. if (m_video_track)
  112. m_video_track->seek(AK::Duration::from_milliseconds(position * 1000.0), seek_mode);
  113. }
  114. // https://html.spec.whatwg.org/multipage/media.html#attr-video-poster
  115. WebIDL::ExceptionOr<void> HTMLVideoElement::determine_element_poster_frame(Optional<String> const& poster)
  116. {
  117. auto& realm = this->realm();
  118. auto& vm = realm.vm();
  119. m_poster_frame = nullptr;
  120. // 1. If there is an existing instance of this algorithm running for this video element, abort that instance of
  121. // this algorithm without changing the poster frame.
  122. if (m_fetch_controller)
  123. m_fetch_controller->stop_fetch();
  124. // 2. If the poster attribute's value is the empty string or if the attribute is absent, then there is no poster
  125. // frame; return.
  126. if (!poster.has_value() || poster->is_empty())
  127. return {};
  128. // 3. Parse the poster attribute's value relative to the element's node document. If this fails, then there is no
  129. // poster frame; return.
  130. auto url_record = document().parse_url(*poster);
  131. if (!url_record.is_valid())
  132. return {};
  133. // 4. Let request be a new request whose URL is the resulting URL record, client is the element's node document's
  134. // relevant settings object, destination is "image", initiator type is "video", credentials mode is "include",
  135. // and whose use-URL-credentials flag is set.
  136. auto request = Fetch::Infrastructure::Request::create(vm);
  137. request->set_url(move(url_record));
  138. request->set_client(&document().relevant_settings_object());
  139. request->set_destination(Fetch::Infrastructure::Request::Destination::Image);
  140. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Video);
  141. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  142. request->set_use_url_credentials(true);
  143. // 5. Fetch request. This must delay the load event of the element's node document.
  144. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  145. m_load_event_delayer.emplace(document());
  146. fetch_algorithms_input.process_response = [this](auto response) mutable {
  147. ScopeGuard guard { [&] { m_load_event_delayer.clear(); } };
  148. auto& realm = this->realm();
  149. auto& global = document().realm().global_object();
  150. if (response->is_network_error())
  151. return;
  152. if (response->type() == Fetch::Infrastructure::Response::Type::Opaque || response->type() == Fetch::Infrastructure::Response::Type::OpaqueRedirect) {
  153. auto& filtered_response = static_cast<Fetch::Infrastructure::FilteredResponse&>(*response);
  154. response = filtered_response.internal_response();
  155. }
  156. auto on_image_data_read = JS::create_heap_function(heap(), [this](ByteBuffer image_data) mutable {
  157. m_fetch_controller = nullptr;
  158. // 6. If an image is thus obtained, the poster frame is that image. Otherwise, there is no poster frame.
  159. (void)Platform::ImageCodecPlugin::the().decode_image(
  160. image_data,
  161. [strong_this = JS::Handle(*this)](Web::Platform::DecodedImage& image) -> ErrorOr<void> {
  162. if (!image.frames.is_empty())
  163. strong_this->m_poster_frame = move(image.frames[0].bitmap);
  164. return {};
  165. },
  166. [](auto&) {});
  167. });
  168. VERIFY(response->body());
  169. auto empty_algorithm = JS::create_heap_function(heap(), [](JS::Value) {});
  170. response->body()->fully_read(realm, on_image_data_read, empty_algorithm, JS::NonnullGCPtr { global });
  171. };
  172. m_fetch_controller = TRY(Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
  173. return {};
  174. }
  175. }