HTMLVideoElement.cpp 7.3 KB

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