HTMLMediaElement.h 11 KB

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