HTMLMediaElement.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/RootVector.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. static inline constexpr auto supported_video_subtypes = Array {
  48. "webm"sv,
  49. "mp4"sv,
  50. "mpeg"sv,
  51. "ogg"sv,
  52. };
  53. static inline constexpr auto supported_audio_subtypes = Array {
  54. "flac"sv,
  55. "mp3"sv,
  56. "mpeg"sv,
  57. "ogg"sv,
  58. "wav"sv,
  59. "webm"sv,
  60. };
  61. Bindings::CanPlayTypeResult can_play_type(StringView type) const;
  62. enum class ReadyState : u16 {
  63. HaveNothing,
  64. HaveMetadata,
  65. HaveCurrentData,
  66. HaveFutureData,
  67. HaveEnoughData,
  68. };
  69. ReadyState ready_state() const { return m_ready_state; }
  70. bool blocked() const;
  71. bool stalled() const;
  72. bool seeking() const { return m_seeking; }
  73. void set_seeking(bool);
  74. WebIDL::ExceptionOr<void> load();
  75. double current_time() const;
  76. void set_current_time(double);
  77. void fast_seek(double);
  78. double current_playback_position() const { return m_current_playback_position; }
  79. void set_current_playback_position(double);
  80. double duration() const;
  81. bool show_poster() const { return m_show_poster; }
  82. bool paused() const { return m_paused; }
  83. bool ended() const;
  84. bool potentially_playing() const;
  85. WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> play();
  86. WebIDL::ExceptionOr<void> pause();
  87. WebIDL::ExceptionOr<void> toggle_playback();
  88. double volume() const { return m_volume; }
  89. WebIDL::ExceptionOr<void> set_volume(double);
  90. bool muted() const { return m_muted; }
  91. void set_muted(bool);
  92. void page_mute_state_changed(Badge<Page>);
  93. double effective_media_volume() const;
  94. GC::Ref<AudioTrackList> audio_tracks() const { return *m_audio_tracks; }
  95. GC::Ref<VideoTrackList> video_tracks() const { return *m_video_tracks; }
  96. GC::Ref<TextTrackList> text_tracks() const { return *m_text_tracks; }
  97. GC::Ref<TextTrack> add_text_track(Bindings::TextTrackKind kind, String const& label, String const& language);
  98. WebIDL::ExceptionOr<bool> handle_keydown(Badge<Web::EventHandler>, UIEvents::KeyCode, u32 modifiers);
  99. enum class MouseTrackingComponent {
  100. Timeline,
  101. Volume,
  102. };
  103. void set_layout_mouse_tracking_component(Badge<Painting::MediaPaintable>, Optional<MouseTrackingComponent> mouse_tracking_component) { m_mouse_tracking_component = move(mouse_tracking_component); }
  104. Optional<MouseTrackingComponent> const& layout_mouse_tracking_component(Badge<Painting::MediaPaintable>) const { return m_mouse_tracking_component; }
  105. void set_layout_mouse_position(Badge<Painting::MediaPaintable>, Optional<CSSPixelPoint> mouse_position) { m_mouse_position = move(mouse_position); }
  106. Optional<CSSPixelPoint> const& layout_mouse_position(Badge<Painting::MediaPaintable>) const { return m_mouse_position; }
  107. void set_layout_display_time(Badge<Painting::MediaPaintable>, Optional<double> display_time);
  108. double layout_display_time(Badge<Painting::MediaPaintable>) const;
  109. struct CachedLayoutBoxes {
  110. Optional<CSSPixelRect> control_box_rect;
  111. Optional<CSSPixelRect> playback_button_rect;
  112. Optional<CSSPixelRect> timeline_rect;
  113. Optional<CSSPixelRect> speaker_button_rect;
  114. Optional<CSSPixelRect> volume_rect;
  115. Optional<CSSPixelRect> volume_scrub_rect;
  116. };
  117. CachedLayoutBoxes& cached_layout_boxes(Badge<Painting::MediaPaintable>) const { return m_layout_boxes; }
  118. protected:
  119. HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
  120. virtual void initialize(JS::Realm&) override;
  121. virtual void finalize() override;
  122. virtual void visit_edges(Cell::Visitor&) override;
  123. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
  124. virtual void removed_from(DOM::Node*) override;
  125. virtual void children_changed() override;
  126. // Override in subclasses to handle implementation-specific behavior when the element state changes
  127. // to playing or paused, e.g. to start/stop play timers.
  128. virtual void on_playing() { }
  129. virtual void on_paused() { }
  130. // Override in subclasses to handle implementation-specific seeking behavior. When seeking is complete,
  131. // subclasses must invoke set_current_playback_position() to unblock the user agent.
  132. virtual void on_seek(double, MediaSeekMode) { m_seek_in_progress = false; }
  133. virtual void on_volume_change() { }
  134. private:
  135. friend SourceElementSelector;
  136. struct EntireResource { };
  137. using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
  138. Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; }
  139. WebIDL::ExceptionOr<void> load_element();
  140. WebIDL::ExceptionOr<void> fetch_resource(URL::URL const&, ESCAPING Function<void(String)> failure_callback);
  141. static bool verify_response(GC::Ref<Fetch::Infrastructure::Response>, ByteRange const&);
  142. WebIDL::ExceptionOr<void> process_media_data(Function<void(String)> failure_callback);
  143. WebIDL::ExceptionOr<void> handle_media_source_failure(Span<GC::Ref<WebIDL::Promise>> promises, String error_message);
  144. void forget_media_resource_specific_tracks();
  145. void set_ready_state(ReadyState);
  146. WebIDL::ExceptionOr<void> play_element();
  147. WebIDL::ExceptionOr<void> pause_element();
  148. void seek_element(double playback_position, MediaSeekMode = MediaSeekMode::Accurate);
  149. void notify_about_playing();
  150. void set_show_poster(bool);
  151. void set_paused(bool);
  152. void set_duration(double);
  153. void volume_or_muted_attribute_changed();
  154. bool is_eligible_for_autoplay() const;
  155. bool has_ended_playback() const;
  156. void reached_end_of_media_playback();
  157. void dispatch_time_update_event();
  158. enum class TimeMarchesOnReason {
  159. NormalPlayback,
  160. Other,
  161. };
  162. void time_marches_on(TimeMarchesOnReason = TimeMarchesOnReason::NormalPlayback);
  163. GC::RootVector<GC::Ref<WebIDL::Promise>> take_pending_play_promises();
  164. void resolve_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises);
  165. void reject_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises, GC::Ref<WebIDL::DOMException> error);
  166. // https://html.spec.whatwg.org/multipage/media.html#reject-pending-play-promises
  167. template<typename ErrorType>
  168. void reject_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises, String message)
  169. {
  170. auto& realm = this->realm();
  171. auto error = ErrorType::create(realm, move(message));
  172. reject_pending_play_promises(promises, error);
  173. }
  174. // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
  175. UniqueTaskSource m_media_element_event_task_source {};
  176. // https://html.spec.whatwg.org/multipage/media.html#dom-media-error
  177. GC::Ptr<MediaError> m_error;
  178. // https://html.spec.whatwg.org/multipage/media.html#dom-media-crossorigin
  179. CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };
  180. // https://html.spec.whatwg.org/multipage/media.html#dom-media-currentsrc
  181. String m_current_src;
  182. // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
  183. NetworkState m_network_state { NetworkState::Empty };
  184. // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
  185. ReadyState m_ready_state { ReadyState::HaveNothing };
  186. bool m_first_data_load_event_since_load_start { false };
  187. // https://html.spec.whatwg.org/multipage/media.html#dom-media-seeking
  188. bool m_seeking { false };
  189. // https://html.spec.whatwg.org/multipage/media.html#current-playback-position
  190. double m_current_playback_position { 0 };
  191. // https://html.spec.whatwg.org/multipage/media.html#official-playback-position
  192. double m_official_playback_position { 0 };
  193. // https://html.spec.whatwg.org/multipage/media.html#default-playback-start-position
  194. double m_default_playback_start_position { 0 };
  195. // https://html.spec.whatwg.org/multipage/media.html#show-poster-flag
  196. bool m_show_poster { true };
  197. // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
  198. double m_duration { NAN };
  199. // https://html.spec.whatwg.org/multipage/media.html#list-of-pending-play-promises
  200. Vector<GC::Ref<WebIDL::Promise>> m_pending_play_promises;
  201. // https://html.spec.whatwg.org/multipage/media.html#dom-media-paused
  202. bool m_paused { true };
  203. // https://html.spec.whatwg.org/multipage/media.html#dom-media-volume
  204. double m_volume { 1.0 };
  205. // https://html.spec.whatwg.org/multipage/media.html#dom-media-muted
  206. bool m_muted { false };
  207. // https://html.spec.whatwg.org/multipage/media.html#dom-media-audiotracks
  208. GC::Ptr<AudioTrackList> m_audio_tracks;
  209. // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
  210. GC::Ptr<VideoTrackList> m_video_tracks;
  211. // https://html.spec.whatwg.org/multipage/media.html#dom-media-texttracks
  212. GC::Ptr<TextTrackList> m_text_tracks;
  213. // https://html.spec.whatwg.org/multipage/media.html#media-data
  214. ByteBuffer m_media_data;
  215. // https://html.spec.whatwg.org/multipage/media.html#can-autoplay-flag
  216. bool m_can_autoplay { true };
  217. // https://html.spec.whatwg.org/multipage/media.html#delaying-the-load-event-flag
  218. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  219. bool m_running_time_update_event_handler { false };
  220. Optional<MonotonicTime> m_last_time_update_event_time;
  221. GC::Ptr<DOM::DocumentObserver> m_document_observer;
  222. GC::Ptr<SourceElementSelector> m_source_element_selector;
  223. GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  224. bool m_seek_in_progress = false;
  225. // Cached state for layout.
  226. Optional<MouseTrackingComponent> m_mouse_tracking_component;
  227. bool m_tracking_mouse_position_while_playing { false };
  228. Optional<CSSPixelPoint> m_mouse_position;
  229. Optional<double> m_display_time;
  230. mutable CachedLayoutBoxes m_layout_boxes;
  231. };
  232. }