HTMLMediaElement.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <LibGC/MarkedVector.h>
  13. #include <LibGfx/Rect.h>
  14. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  15. #include <LibWeb/HTML/CORSSettingAttribute.h>
  16. #include <LibWeb/HTML/EventLoop/Task.h>
  17. #include <LibWeb/HTML/HTMLElement.h>
  18. #include <LibWeb/PixelUnits.h>
  19. #include <LibWeb/UIEvents/KeyCode.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. virtual bool is_focusable() const override { return true; }
  33. // NOTE: The function is wrapped in a GC::HeapFunction immediately.
  34. void queue_a_media_element_task(Function<void()>);
  35. GC::Ptr<MediaError> error() const { return m_error; }
  36. 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]] GC::Ref<TimeRanges> buffered() const;
  47. 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<GC::Ref<WebIDL::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. void page_mute_state_changed(Badge<Page>);
  79. double effective_media_volume() const;
  80. GC::Ref<AudioTrackList> audio_tracks() const { return *m_audio_tracks; }
  81. GC::Ref<VideoTrackList> video_tracks() const { return *m_video_tracks; }
  82. GC::Ref<TextTrackList> text_tracks() const { return *m_text_tracks; }
  83. GC::Ref<TextTrack> add_text_track(Bindings::TextTrackKind kind, String const& label, String const& language);
  84. WebIDL::ExceptionOr<bool> handle_keydown(Badge<Web::EventHandler>, UIEvents::KeyCode, u32 modifiers);
  85. enum class MouseTrackingComponent {
  86. Timeline,
  87. Volume,
  88. };
  89. void set_layout_mouse_tracking_component(Badge<Painting::MediaPaintable>, Optional<MouseTrackingComponent> mouse_tracking_component) { m_mouse_tracking_component = move(mouse_tracking_component); }
  90. Optional<MouseTrackingComponent> const& layout_mouse_tracking_component(Badge<Painting::MediaPaintable>) const { return m_mouse_tracking_component; }
  91. void set_layout_mouse_position(Badge<Painting::MediaPaintable>, Optional<CSSPixelPoint> mouse_position) { m_mouse_position = move(mouse_position); }
  92. Optional<CSSPixelPoint> const& layout_mouse_position(Badge<Painting::MediaPaintable>) const { return m_mouse_position; }
  93. void set_layout_display_time(Badge<Painting::MediaPaintable>, Optional<double> display_time);
  94. double layout_display_time(Badge<Painting::MediaPaintable>) const;
  95. struct CachedLayoutBoxes {
  96. Optional<CSSPixelRect> control_box_rect;
  97. Optional<CSSPixelRect> playback_button_rect;
  98. Optional<CSSPixelRect> timeline_rect;
  99. Optional<CSSPixelRect> speaker_button_rect;
  100. Optional<CSSPixelRect> volume_rect;
  101. Optional<CSSPixelRect> volume_scrub_rect;
  102. };
  103. CachedLayoutBoxes& cached_layout_boxes(Badge<Painting::MediaPaintable>) const { return m_layout_boxes; }
  104. protected:
  105. HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
  106. virtual void initialize(JS::Realm&) override;
  107. virtual void finalize() override;
  108. virtual void visit_edges(Cell::Visitor&) override;
  109. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
  110. virtual void removed_from(DOM::Node*) override;
  111. virtual void children_changed() override;
  112. // Override in subclasses to handle implementation-specific behavior when the element state changes
  113. // to playing or paused, e.g. to start/stop play timers.
  114. virtual void on_playing() { }
  115. virtual void on_paused() { }
  116. // Override in subclasses to handle implementation-specific seeking behavior. When seeking is complete,
  117. // subclasses must invoke set_current_playback_position() to unblock the user agent.
  118. virtual void on_seek(double, MediaSeekMode) { m_seek_in_progress = false; }
  119. virtual void on_volume_change() { }
  120. private:
  121. friend SourceElementSelector;
  122. struct EntireResource { };
  123. using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
  124. Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; }
  125. WebIDL::ExceptionOr<void> load_element();
  126. WebIDL::ExceptionOr<void> fetch_resource(URL::URL const&, ESCAPING Function<void(String)> failure_callback);
  127. static bool verify_response(GC::Ref<Fetch::Infrastructure::Response>, ByteRange const&);
  128. WebIDL::ExceptionOr<void> process_media_data(Function<void(String)> failure_callback);
  129. WebIDL::ExceptionOr<void> handle_media_source_failure(Span<GC::Ref<WebIDL::Promise>> promises, String error_message);
  130. void forget_media_resource_specific_tracks();
  131. void set_ready_state(ReadyState);
  132. WebIDL::ExceptionOr<void> play_element();
  133. WebIDL::ExceptionOr<void> pause_element();
  134. void seek_element(double playback_position, MediaSeekMode = MediaSeekMode::Accurate);
  135. void notify_about_playing();
  136. void set_show_poster(bool);
  137. void set_paused(bool);
  138. void set_duration(double);
  139. void volume_or_muted_attribute_changed();
  140. bool is_eligible_for_autoplay() const;
  141. bool has_ended_playback() const;
  142. void reached_end_of_media_playback();
  143. void dispatch_time_update_event();
  144. enum class TimeMarchesOnReason {
  145. NormalPlayback,
  146. Other,
  147. };
  148. void time_marches_on(TimeMarchesOnReason = TimeMarchesOnReason::NormalPlayback);
  149. GC::MarkedVector<GC::Ref<WebIDL::Promise>> take_pending_play_promises();
  150. void resolve_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises);
  151. void reject_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises, GC::Ref<WebIDL::DOMException> error);
  152. // https://html.spec.whatwg.org/multipage/media.html#reject-pending-play-promises
  153. template<typename ErrorType>
  154. void reject_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises, String message)
  155. {
  156. auto& realm = this->realm();
  157. auto error = ErrorType::create(realm, move(message));
  158. reject_pending_play_promises(promises, error);
  159. }
  160. // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
  161. UniqueTaskSource m_media_element_event_task_source {};
  162. // https://html.spec.whatwg.org/multipage/media.html#dom-media-error
  163. GC::Ptr<MediaError> m_error;
  164. // https://html.spec.whatwg.org/multipage/media.html#dom-media-crossorigin
  165. CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };
  166. // https://html.spec.whatwg.org/multipage/media.html#dom-media-currentsrc
  167. String m_current_src;
  168. // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
  169. NetworkState m_network_state { NetworkState::Empty };
  170. // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
  171. ReadyState m_ready_state { ReadyState::HaveNothing };
  172. bool m_first_data_load_event_since_load_start { false };
  173. // https://html.spec.whatwg.org/multipage/media.html#dom-media-seeking
  174. bool m_seeking { false };
  175. // https://html.spec.whatwg.org/multipage/media.html#current-playback-position
  176. double m_current_playback_position { 0 };
  177. // https://html.spec.whatwg.org/multipage/media.html#official-playback-position
  178. double m_official_playback_position { 0 };
  179. // https://html.spec.whatwg.org/multipage/media.html#default-playback-start-position
  180. double m_default_playback_start_position { 0 };
  181. // https://html.spec.whatwg.org/multipage/media.html#show-poster-flag
  182. bool m_show_poster { true };
  183. // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
  184. double m_duration { NAN };
  185. // https://html.spec.whatwg.org/multipage/media.html#list-of-pending-play-promises
  186. Vector<GC::Ref<WebIDL::Promise>> m_pending_play_promises;
  187. // https://html.spec.whatwg.org/multipage/media.html#dom-media-paused
  188. bool m_paused { true };
  189. // https://html.spec.whatwg.org/multipage/media.html#dom-media-volume
  190. double m_volume { 1.0 };
  191. // https://html.spec.whatwg.org/multipage/media.html#dom-media-muted
  192. bool m_muted { false };
  193. // https://html.spec.whatwg.org/multipage/media.html#dom-media-audiotracks
  194. GC::Ptr<AudioTrackList> m_audio_tracks;
  195. // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
  196. GC::Ptr<VideoTrackList> m_video_tracks;
  197. // https://html.spec.whatwg.org/multipage/media.html#dom-media-texttracks
  198. GC::Ptr<TextTrackList> m_text_tracks;
  199. // https://html.spec.whatwg.org/multipage/media.html#media-data
  200. ByteBuffer m_media_data;
  201. // https://html.spec.whatwg.org/multipage/media.html#can-autoplay-flag
  202. bool m_can_autoplay { true };
  203. // https://html.spec.whatwg.org/multipage/media.html#delaying-the-load-event-flag
  204. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  205. bool m_running_time_update_event_handler { false };
  206. Optional<MonotonicTime> m_last_time_update_event_time;
  207. GC::Ptr<DOM::DocumentObserver> m_document_observer;
  208. GC::Ptr<SourceElementSelector> m_source_element_selector;
  209. GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  210. bool m_seek_in_progress = false;
  211. // Cached state for layout.
  212. Optional<MouseTrackingComponent> m_mouse_tracking_component;
  213. bool m_tracking_mouse_position_while_playing { false };
  214. Optional<CSSPixelPoint> m_mouse_position;
  215. Optional<double> m_display_time;
  216. mutable CachedLayoutBoxes m_layout_boxes;
  217. };
  218. }