| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-28 22:46:44 +02:00
										 |  |  |  * Copyright (c) 2020, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2023-04-07 10:45:17 -04:00
										 |  |  |  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-10 09:52:53 -04:00
										 |  |  | #include <AK/Time.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  | #include <LibJS/Heap/MarkedVector.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  | #include <LibJS/SafeFunction.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-19 06:48:42 -04:00
										 |  |  | #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-18 16:38:54 -04:00
										 |  |  | #include <LibWeb/HTML/CORSSettingAttribute.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:11:58 -04:00
										 |  |  | #include <LibWeb/HTML/EventLoop/Task.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | #include <LibWeb/HTML/HTMLElement.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  | #include <LibWeb/WebIDL/DOMException.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-04 15:12:40 -04:00
										 |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  | enum class MediaSeekMode { | 
					
						
							|  |  |  |     Accurate, | 
					
						
							|  |  |  |     ApproximateForSpeed, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | class HTMLMediaElement : public HTMLElement { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     WEB_PLATFORM_OBJECT(HTMLMediaElement, HTMLElement); | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  |     virtual ~HTMLMediaElement() override; | 
					
						
							| 
									
										
										
										
											2022-03-04 18:49:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:11:58 -04:00
										 |  |  |     void queue_a_media_element_task(JS::SafeFunction<void()> steps); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 14:44:33 -04:00
										 |  |  |     JS::GCPtr<MediaError> error() const { return m_error; } | 
					
						
							| 
									
										
										
										
											2023-04-22 15:11:07 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> set_decoder_error(String error_message); | 
					
						
							| 
									
										
										
										
											2023-04-22 14:44:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 06:44:40 -04:00
										 |  |  |     String const& current_src() const { return m_current_src; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 11:11:18 -04:00
										 |  |  |     enum class NetworkState : u16 { | 
					
						
							|  |  |  |         Empty, | 
					
						
							|  |  |  |         Idle, | 
					
						
							|  |  |  |         Loading, | 
					
						
							|  |  |  |         NoSource, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     NetworkState network_state() const { return m_network_state; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 16:22:34 -04:00
										 |  |  |     WebIDL::ExceptionOr<JS::NonnullGCPtr<TimeRanges>> buffered() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:20:42 -04:00
										 |  |  |     WebIDL::ExceptionOr<Bindings::CanPlayTypeResult> can_play_type(DeprecatedString const& type) const; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 15:41:09 -04:00
										 |  |  |     enum class ReadyState : u16 { | 
					
						
							|  |  |  |         HaveNothing, | 
					
						
							|  |  |  |         HaveMetadata, | 
					
						
							|  |  |  |         HaveCurrentData, | 
					
						
							|  |  |  |         HaveFutureData, | 
					
						
							|  |  |  |         HaveEnoughData, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ReadyState ready_state() const { return m_ready_state; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  |     bool seeking() const { return m_seeking; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 10:45:17 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> load(); | 
					
						
							| 
									
										
										
										
											2023-04-10 10:07:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     double current_time() const; | 
					
						
							|  |  |  |     void set_current_time(double); | 
					
						
							| 
									
										
										
										
											2023-04-23 16:17:01 -04:00
										 |  |  |     void fast_seek(double); | 
					
						
							| 
									
										
										
										
											2023-04-20 06:47:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     double current_playback_position() const { return m_current_playback_position; } | 
					
						
							| 
									
										
										
										
											2023-04-10 10:07:23 -04:00
										 |  |  |     void set_current_playback_position(double); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 15:12:40 -04:00
										 |  |  |     double duration() const; | 
					
						
							| 
									
										
										
										
											2023-04-20 06:49:06 -04:00
										 |  |  |     bool show_poster() const { return m_show_poster; } | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  |     bool paused() const { return m_paused; } | 
					
						
							| 
									
										
										
										
											2023-04-10 20:04:46 -04:00
										 |  |  |     bool ended() const; | 
					
						
							| 
									
										
										
										
											2023-04-20 07:20:42 -04:00
										 |  |  |     bool potentially_playing() const; | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  |     WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> play(); | 
					
						
							| 
									
										
										
										
											2023-04-07 11:35:11 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> pause(); | 
					
						
							| 
									
										
										
										
											2022-10-28 15:38:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     JS::NonnullGCPtr<VideoTrackList> video_tracks() const { return *m_video_tracks; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | protected: | 
					
						
							|  |  |  |     HTMLMediaElement(DOM::Document&, DOM::QualifiedName); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  |     virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2023-04-03 11:11:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 06:34:59 -04:00
										 |  |  |     virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override; | 
					
						
							|  |  |  |     virtual void did_remove_attribute(DeprecatedFlyString const&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  |     // Override in subclasses to handle implementation-specific behavior when the element state changes
 | 
					
						
							|  |  |  |     // to playing or paused, e.g. to start/stop play timers.
 | 
					
						
							|  |  |  |     virtual void on_playing() { } | 
					
						
							|  |  |  |     virtual void on_paused() { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  |     // Override in subclasses to handle implementation-specific seeking behavior. When seeking is complete,
 | 
					
						
							|  |  |  |     // subclasses must invoke set_current_playback_position() to unblock the user agent.
 | 
					
						
							|  |  |  |     virtual void on_seek(double, MediaSeekMode) { m_seek_in_progress = false; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 11:11:18 -04:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     struct EntireResource { }; | 
					
						
							|  |  |  |     using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:11:58 -04:00
										 |  |  |     Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> load_element(); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> select_resource(); | 
					
						
							| 
									
										
										
										
											2023-04-22 14:44:33 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> fetch_resource(AK::URL const&, Function<void(String)> failure_callback); | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     static bool verify_response(JS::NonnullGCPtr<Fetch::Infrastructure::Response>, ByteRange const&); | 
					
						
							| 
									
										
										
										
											2023-04-22 14:44:33 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> process_media_data(Function<void(String)> failure_callback); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> handle_media_source_failure(Span<JS::NonnullGCPtr<WebIDL::Promise>> promises, String error_message); | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     void forget_media_resource_specific_tracks(); | 
					
						
							| 
									
										
										
										
											2023-04-04 15:41:09 -04:00
										 |  |  |     void set_ready_state(ReadyState); | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> play_element(); | 
					
						
							| 
									
										
										
										
											2023-04-07 11:35:11 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> pause_element(); | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  |     void seek_element(double playback_position, MediaSeekMode = MediaSeekMode::Accurate); | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  |     void notify_about_playing(); | 
					
						
							| 
									
										
										
										
											2023-04-20 06:49:06 -04:00
										 |  |  |     void set_show_poster(bool); | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  |     void set_paused(bool); | 
					
						
							| 
									
										
										
										
											2023-04-04 15:12:40 -04:00
										 |  |  |     void set_duration(double); | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 07:20:42 -04:00
										 |  |  |     bool blocked() const; | 
					
						
							| 
									
										
										
										
											2023-04-17 07:24:50 -04:00
										 |  |  |     bool is_eligible_for_autoplay() const; | 
					
						
							| 
									
										
										
										
											2023-04-10 20:04:46 -04:00
										 |  |  |     bool has_ended_playback() const; | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> reached_end_of_media_playback(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 09:52:53 -04:00
										 |  |  |     WebIDL::ExceptionOr<void> dispatch_time_update_event(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 10:07:23 -04:00
										 |  |  |     enum class TimeMarchesOnReason { | 
					
						
							|  |  |  |         NormalPlayback, | 
					
						
							|  |  |  |         Other, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     void time_marches_on(TimeMarchesOnReason = TimeMarchesOnReason::NormalPlayback); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  |     JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> take_pending_play_promises(); | 
					
						
							|  |  |  |     void resolve_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises); | 
					
						
							|  |  |  |     void reject_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises, JS::NonnullGCPtr<WebIDL::DOMException> error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/media.html#reject-pending-play-promises
 | 
					
						
							|  |  |  |     template<typename ErrorType> | 
					
						
							|  |  |  |     void reject_pending_play_promises(ReadonlySpan<JS::NonnullGCPtr<WebIDL::Promise>> promises, FlyString const& message) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto& realm = this->realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto error = ErrorType::create(realm, message.to_deprecated_fly_string()); | 
					
						
							|  |  |  |         reject_pending_play_promises(promises, error); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:11:58 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
 | 
					
						
							|  |  |  |     UniqueTaskSource m_media_element_event_task_source {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 14:44:33 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-error
 | 
					
						
							|  |  |  |     JS::GCPtr<MediaError> m_error; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 16:38:54 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-crossorigin
 | 
					
						
							|  |  |  |     CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 06:44:40 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-currentsrc
 | 
					
						
							|  |  |  |     String m_current_src; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 11:11:18 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
 | 
					
						
							|  |  |  |     NetworkState m_network_state { NetworkState::Empty }; | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 15:41:09 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
 | 
					
						
							|  |  |  |     ReadyState m_ready_state { ReadyState::HaveNothing }; | 
					
						
							|  |  |  |     bool m_first_data_load_event_since_load_start { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-seeking
 | 
					
						
							|  |  |  |     bool m_seeking { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 10:07:23 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#current-playback-position
 | 
					
						
							|  |  |  |     double m_current_playback_position { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/media.html#official-playback-position
 | 
					
						
							|  |  |  |     double m_official_playback_position { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/media.html#default-playback-start-position
 | 
					
						
							|  |  |  |     double m_default_playback_start_position { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-20 06:49:06 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#show-poster-flag
 | 
					
						
							|  |  |  |     bool m_show_poster { true }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 15:12:40 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
 | 
					
						
							|  |  |  |     double m_duration { NAN }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 12:52:41 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#list-of-pending-play-promises
 | 
					
						
							|  |  |  |     JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> m_pending_play_promises; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 11:10:57 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-paused
 | 
					
						
							|  |  |  |     bool m_paused { true }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
 | 
					
						
							|  |  |  |     JS::GCPtr<VideoTrackList> m_video_tracks; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/media.html#media-data
 | 
					
						
							|  |  |  |     ByteBuffer m_media_data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 07:24:50 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#can-autoplay-flag
 | 
					
						
							|  |  |  |     bool m_can_autoplay { true }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 06:48:42 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/media.html#delaying-the-load-event-flag
 | 
					
						
							|  |  |  |     Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 09:52:53 -04:00
										 |  |  |     bool m_running_time_update_event_handler { false }; | 
					
						
							|  |  |  |     Optional<Time> m_last_time_update_event_time; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:14:33 -04:00
										 |  |  |     JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller; | 
					
						
							| 
									
										
										
										
											2023-04-11 18:37:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_seek_in_progress = false; | 
					
						
							| 
									
										
										
										
											2020-08-01 03:05:43 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |