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