HTMLMediaElement.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Time.h>
  11. #include <AK/Variant.h>
  12. #include <LibGfx/Rect.h>
  13. #include <LibJS/Heap/MarkedVector.h>
  14. #include <LibJS/SafeFunction.h>
  15. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  16. #include <LibWeb/HTML/CORSSettingAttribute.h>
  17. #include <LibWeb/HTML/EventLoop/Task.h>
  18. #include <LibWeb/HTML/HTMLElement.h>
  19. #include <LibWeb/PixelUnits.h>
  20. #include <LibWeb/WebIDL/DOMException.h>
  21. #include <math.h>
  22. namespace Web::HTML {
  23. enum class MediaSeekMode {
  24. Accurate,
  25. ApproximateForSpeed,
  26. };
  27. class SourceElementSelector;
  28. class HTMLMediaElement : public HTMLElement {
  29. WEB_PLATFORM_OBJECT(HTMLMediaElement, HTMLElement);
  30. public:
  31. virtual ~HTMLMediaElement() override;
  32. void queue_a_media_element_task(JS::SafeFunction<void()> steps);
  33. JS::GCPtr<MediaError> error() const { return m_error; }
  34. WebIDL::ExceptionOr<void> set_decoder_error(String error_message);
  35. String const& current_src() const { return m_current_src; }
  36. WebIDL::ExceptionOr<void> select_resource();
  37. enum class NetworkState : u16 {
  38. Empty,
  39. Idle,
  40. Loading,
  41. NoSource,
  42. };
  43. NetworkState network_state() const { return m_network_state; }
  44. WebIDL::ExceptionOr<JS::NonnullGCPtr<TimeRanges>> buffered() const;
  45. WebIDL::ExceptionOr<Bindings::CanPlayTypeResult> can_play_type(DeprecatedString const& type) const;
  46. enum class ReadyState : u16 {
  47. HaveNothing,
  48. HaveMetadata,
  49. HaveCurrentData,
  50. HaveFutureData,
  51. HaveEnoughData,
  52. };
  53. ReadyState ready_state() const { return m_ready_state; }
  54. bool seeking() const { return m_seeking; }
  55. WebIDL::ExceptionOr<void> load();
  56. double current_time() const;
  57. void set_current_time(double);
  58. void fast_seek(double);
  59. double current_playback_position() const { return m_current_playback_position; }
  60. void set_current_playback_position(double);
  61. double duration() const;
  62. bool show_poster() const { return m_show_poster; }
  63. bool paused() const { return m_paused; }
  64. bool ended() const;
  65. bool potentially_playing() const;
  66. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> play();
  67. WebIDL::ExceptionOr<void> pause();
  68. JS::NonnullGCPtr<VideoTrackList> video_tracks() const { return *m_video_tracks; }
  69. void set_layout_mouse_position(Badge<Painting::MediaPaintable>, Optional<CSSPixelPoint> mouse_position) { m_mouse_position = move(mouse_position); }
  70. Optional<CSSPixelPoint> const& layout_mouse_position(Badge<Painting::MediaPaintable>) const { return m_mouse_position; }
  71. struct CachedLayoutBoxes {
  72. Optional<CSSPixelRect> control_box_rect;
  73. Optional<CSSPixelRect> playback_button_rect;
  74. Optional<CSSPixelRect> timeline_rect;
  75. };
  76. CachedLayoutBoxes& cached_layout_boxes(Badge<Painting::MediaPaintable>) const { return m_layout_boxes; }
  77. protected:
  78. HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
  79. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  80. virtual void visit_edges(Cell::Visitor&) override;
  81. virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  82. virtual void did_remove_attribute(DeprecatedFlyString const&) override;
  83. virtual void removed_from(DOM::Node*) override;
  84. virtual void children_changed() override;
  85. // Override in subclasses to handle implementation-specific behavior when the element state changes
  86. // to playing or paused, e.g. to start/stop play timers.
  87. virtual void on_playing() { }
  88. virtual void on_paused() { }
  89. // Override in subclasses to handle implementation-specific seeking behavior. When seeking is complete,
  90. // subclasses must invoke set_current_playback_position() to unblock the user agent.
  91. virtual void on_seek(double, MediaSeekMode) { m_seek_in_progress = false; }
  92. private:
  93. friend SourceElementSelector;
  94. struct EntireResource { };
  95. using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
  96. Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; }
  97. WebIDL::ExceptionOr<void> load_element();
  98. WebIDL::ExceptionOr<void> fetch_resource(AK::URL const&, Function<void(String)> failure_callback);
  99. static bool verify_response(JS::NonnullGCPtr<Fetch::Infrastructure::Response>, ByteRange const&);
  100. WebIDL::ExceptionOr<void> process_media_data(Function<void(String)> failure_callback);
  101. WebIDL::ExceptionOr<void> handle_media_source_failure(Span<JS::NonnullGCPtr<WebIDL::Promise>> promises, String error_message);
  102. void forget_media_resource_specific_tracks();
  103. void set_ready_state(ReadyState);
  104. WebIDL::ExceptionOr<void> play_element();
  105. WebIDL::ExceptionOr<void> pause_element();
  106. void seek_element(double playback_position, MediaSeekMode = MediaSeekMode::Accurate);
  107. void notify_about_playing();
  108. void set_show_poster(bool);
  109. void set_paused(bool);
  110. void set_duration(double);
  111. bool blocked() const;
  112. bool is_eligible_for_autoplay() const;
  113. bool has_ended_playback() const;
  114. WebIDL::ExceptionOr<void> reached_end_of_media_playback();
  115. WebIDL::ExceptionOr<void> dispatch_time_update_event();
  116. enum class TimeMarchesOnReason {
  117. NormalPlayback,
  118. Other,
  119. };
  120. void time_marches_on(TimeMarchesOnReason = TimeMarchesOnReason::NormalPlayback);
  121. JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> take_pending_play_promises();
  122. void resolve_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises);
  123. void reject_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises, JS::NonnullGCPtr<WebIDL::DOMException> error);
  124. // https://html.spec.whatwg.org/multipage/media.html#reject-pending-play-promises
  125. template<typename ErrorType>
  126. void reject_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises, FlyString const& message)
  127. {
  128. auto& realm = this->realm();
  129. auto error = ErrorType::create(realm, message.to_deprecated_fly_string());
  130. reject_pending_play_promises(promises, error);
  131. }
  132. // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
  133. UniqueTaskSource m_media_element_event_task_source {};
  134. // https://html.spec.whatwg.org/multipage/media.html#dom-media-error
  135. JS::GCPtr<MediaError> m_error;
  136. // https://html.spec.whatwg.org/multipage/media.html#dom-media-crossorigin
  137. CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };
  138. // https://html.spec.whatwg.org/multipage/media.html#dom-media-currentsrc
  139. String m_current_src;
  140. // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
  141. NetworkState m_network_state { NetworkState::Empty };
  142. // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
  143. ReadyState m_ready_state { ReadyState::HaveNothing };
  144. bool m_first_data_load_event_since_load_start { false };
  145. // https://html.spec.whatwg.org/multipage/media.html#dom-media-seeking
  146. bool m_seeking { false };
  147. // https://html.spec.whatwg.org/multipage/media.html#current-playback-position
  148. double m_current_playback_position { 0 };
  149. // https://html.spec.whatwg.org/multipage/media.html#official-playback-position
  150. double m_official_playback_position { 0 };
  151. // https://html.spec.whatwg.org/multipage/media.html#default-playback-start-position
  152. double m_default_playback_start_position { 0 };
  153. // https://html.spec.whatwg.org/multipage/media.html#show-poster-flag
  154. bool m_show_poster { true };
  155. // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
  156. double m_duration { NAN };
  157. // https://html.spec.whatwg.org/multipage/media.html#list-of-pending-play-promises
  158. JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> m_pending_play_promises;
  159. // https://html.spec.whatwg.org/multipage/media.html#dom-media-paused
  160. bool m_paused { true };
  161. // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
  162. JS::GCPtr<VideoTrackList> m_video_tracks;
  163. // https://html.spec.whatwg.org/multipage/media.html#media-data
  164. ByteBuffer m_media_data;
  165. // https://html.spec.whatwg.org/multipage/media.html#can-autoplay-flag
  166. bool m_can_autoplay { true };
  167. // https://html.spec.whatwg.org/multipage/media.html#delaying-the-load-event-flag
  168. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  169. bool m_running_time_update_event_handler { false };
  170. Optional<MonotonicTime> m_last_time_update_event_time;
  171. JS::GCPtr<DOM::DocumentObserver> m_document_observer;
  172. JS::GCPtr<SourceElementSelector> m_source_element_selector;
  173. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  174. bool m_seek_in_progress = false;
  175. // Cached state for layout.
  176. Optional<CSSPixelPoint> m_mouse_position;
  177. mutable CachedLayoutBoxes m_layout_boxes;
  178. };
  179. }