Skip to main content

script/dom/html/embedded_content/
htmlmediaelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::{Cell, Ref, RefCell};
6use std::collections::VecDeque;
7use std::ops::Deref;
8use std::sync::{Arc, Mutex, OnceLock};
9use std::time::{Duration, Instant};
10
11use atomic_refcell::AtomicRefCell;
12use bytes::Bytes;
13use content_security_policy::sandboxing_directive::SandboxingFlagSet;
14use dom_struct::dom_struct;
15use embedder_traits::{MediaPositionState, MediaSessionEvent, MediaSessionPlaybackState};
16use headers::{ContentLength, ContentRange, HeaderMapExt, Range as RangeHeader};
17use html5ever::{LocalName, Prefix, local_name};
18use http::StatusCode;
19use http::header::HeaderMap;
20use js::context::{JSContext, NoGC};
21use js::realm::CurrentRealm;
22use layout_api::MediaFrame;
23use net_traits::fetch::fetch_controller::FetchController;
24use net_traits::request::{Destination, RequestId};
25use net_traits::{CoreResourceThread, FetchMetadata, Metadata, NetworkError, ResourceFetchTiming};
26use pixels::{CorsStatus, RasterImage};
27use script_bindings::assert::assert_in_script;
28use script_bindings::cell::DomRefCell;
29use script_bindings::codegen::InheritTypes::{
30    ElementTypeId, HTMLElementTypeId, HTMLMediaElementTypeId, NodeTypeId,
31};
32use script_bindings::weakref::WeakRef;
33use servo_base::generic_channel::{self};
34use servo_config::pref;
35use servo_media::player::audio::AudioRenderer;
36use servo_media::player::video::{VideoFrame, VideoFrameRenderer};
37use servo_media::player::{PlaybackState, Player, PlayerError, PlayerEvent, SeekLock, StreamType};
38use servo_media::{ClientContextId, ServoMedia, SupportsMediaType};
39use servo_url::ServoUrl;
40use stylo_atoms::Atom;
41use uuid::Uuid;
42
43use crate::dom::audio::audiotrack::AudioTrack;
44use crate::dom::audio::audiotracklist::AudioTrackList;
45use crate::dom::bindings::cell::AtomicSafeBorrowMut;
46use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
47use crate::dom::bindings::codegen::Bindings::HTMLMediaElementBinding::{
48    CanPlayTypeResult, HTMLMediaElementConstants, HTMLMediaElementMethods,
49};
50use crate::dom::bindings::codegen::Bindings::MediaErrorBinding::MediaErrorConstants::*;
51use crate::dom::bindings::codegen::Bindings::MediaErrorBinding::MediaErrorMethods;
52use crate::dom::bindings::codegen::Bindings::NavigatorBinding::Navigator_Binding::NavigatorMethods;
53use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
54use crate::dom::bindings::codegen::Bindings::TextTrackBinding::{
55    TextTrackKind, TextTrackMethods, TextTrackMode,
56};
57use crate::dom::bindings::codegen::Bindings::TextTrackCueBinding::TextTrackCueMethods;
58use crate::dom::bindings::codegen::Bindings::URLBinding::URLMethods;
59use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
60use crate::dom::bindings::codegen::UnionTypes::{
61    MediaStreamOrBlob, VideoTrackOrAudioTrackOrTextTrack,
62};
63use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
64use crate::dom::bindings::inheritance::Castable;
65use crate::dom::bindings::num::Finite;
66use crate::dom::bindings::refcounted::Trusted;
67use crate::dom::bindings::reflector::DomGlobal;
68use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom, UnrootedDom};
69use crate::dom::bindings::str::{DOMString, USVString};
70use crate::dom::blob::Blob;
71use crate::dom::csp::{GlobalCspReporting, Violation};
72use crate::dom::document::Document;
73use crate::dom::element::attributes::storage::AttrRef;
74use crate::dom::element::{
75    AttributeMutation, Element, cors_setting_for_element, reflect_cross_origin_attribute,
76    set_cross_origin_attribute,
77};
78use crate::dom::event::Event;
79use crate::dom::eventtarget::EventTarget;
80use crate::dom::globalscope::GlobalScope;
81use crate::dom::html::embedded_content::media_elements::media_frame_renderer::MediaFrameRenderer;
82use crate::dom::html::htmlaudioelement::HTMLAudioElement;
83use crate::dom::html::htmlelement::HTMLElement;
84use crate::dom::html::htmlsourceelement::HTMLSourceElement;
85use crate::dom::html::htmlvideoelement::HTMLVideoElement;
86use crate::dom::mediaerror::MediaError;
87use crate::dom::mediafragmentparser::MediaFragmentParser;
88use crate::dom::medialist::MediaList;
89use crate::dom::mediastream::MediaStream;
90use crate::dom::node::virtualmethods::VirtualMethods;
91use crate::dom::node::{Node, NodeDamage, NodeTraits, UnbindContext};
92use crate::dom::performance::performanceresourcetiming::InitiatorType;
93use crate::dom::promise::Promise;
94use crate::dom::rules_for_rendering::{
95    RulesForUpdatingTheTextTrackRendering, ShouldResetRenderingControls,
96};
97use crate::dom::shadowroot::ShadowRoot;
98use crate::dom::shadowroot::ua_shadowroot::{CreateUAShadowRootForSelf, UAShadowRoot};
99use crate::dom::texttrack::TextTrack;
100use crate::dom::texttrackcue::TextTrackCue;
101use crate::dom::texttracklist::TextTrackList;
102use crate::dom::timeranges::{TimeRanges, TimeRangesContainer};
103use crate::dom::trackevent::TrackEvent;
104use crate::dom::url::URL;
105use crate::dom::videotrack::VideoTrack;
106use crate::dom::videotracklist::VideoTrackList;
107use crate::dom::{RootedPromise, TracedPromise, referrer_policy_for_element};
108use crate::event_loop::document_loader::{LoadBlocker, LoadType};
109use crate::event_loop::script_thread::ScriptThread;
110use crate::fetch::fetch::{RequestWithGlobalScope, create_a_potential_cors_request_with_claim};
111use crate::fetch::network_listener::{self, FetchResponseListener, ResourceTimingListener};
112use crate::realms::enter_auto_realm;
113use crate::runtime::job_queue::MicrotaskRunnable;
114use crate::url::ensure_blob_referenced_by_url_is_kept_alive;
115
116/// A CSS file to style the media controls.
117static MEDIA_CONTROL_CSS: &str = include_str!("../../../resources/media-controls.css");
118
119/// A JS file to control the media controls.
120static MEDIA_CONTROL_JS: &str = include_str!("../../../resources/media-controls.js");
121
122/// The media engine may report a seek-done position that differs slightly from the
123/// requested position (e.g. snapping to the nearest keyframe), so we use a threshold
124/// instead of strict equality. (Unit is second)
125const SEEK_POSITION_THRESHOLD: f64 = 0.5;
126
127#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
128#[derive(JSTraceable, MallocSizeOf)]
129enum SrcObject {
130    MediaStream(Dom<MediaStream>),
131    Blob(Dom<Blob>),
132}
133
134impl From<MediaStreamOrBlob> for SrcObject {
135    fn from(src_object: MediaStreamOrBlob) -> SrcObject {
136        match src_object {
137            MediaStreamOrBlob::Blob(blob) => SrcObject::Blob(Dom::from_ref(&*blob)),
138            MediaStreamOrBlob::MediaStream(stream) => {
139                SrcObject::MediaStream(Dom::from_ref(&*stream))
140            },
141        }
142    }
143}
144
145#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
146enum LoadState {
147    NotLoaded,
148    LoadingFromSrcObject,
149    LoadingFromSrcAttribute,
150    LoadingFromSourceChild,
151    WaitingForSource,
152}
153
154/// <https://html.spec.whatwg.org/multipage/#loading-the-media-resource:media-element-29>
155#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
156#[derive(JSTraceable, MallocSizeOf)]
157struct SourceChildrenPointer {
158    source_before_pointer: Dom<HTMLSourceElement>,
159    inclusive: bool,
160}
161
162impl SourceChildrenPointer {
163    fn new(source_before_pointer: &HTMLSourceElement, inclusive: bool) -> Self {
164        Self {
165            source_before_pointer: Dom::from_ref(source_before_pointer),
166            inclusive,
167        }
168    }
169}
170
171/// Generally the presence of the loop attribute should be considered to mean playback has not
172/// "ended", as "ended" and "looping" are mutually exclusive.
173/// <https://html.spec.whatwg.org/multipage/#ended-playback>
174#[derive(Clone, Copy, Debug, PartialEq)]
175enum LoopCondition {
176    Included,
177    Ignored,
178}
179
180type BoxedSliceOfPromises = Box<[TracedPromise]>;
181
182#[dom_struct]
183pub(crate) struct HTMLMediaElement {
184    htmlelement: HTMLElement,
185    /// <https://html.spec.whatwg.org/multipage/#dom-media-networkstate>
186    network_state: Cell<NetworkState>,
187    /// <https://html.spec.whatwg.org/multipage/#dom-media-readystate>
188    ready_state: Cell<ReadyState>,
189    /// <https://html.spec.whatwg.org/multipage/#dom-media-srcobject>
190    src_object: DomRefCell<Option<SrcObject>>,
191    /// <https://html.spec.whatwg.org/multipage/#dom-media-currentsrc>
192    #[no_trace]
193    current_src: AtomicRefCell<String>,
194    /// Incremented whenever tasks associated with this element are cancelled.
195    generation_id: Cell<u32>,
196    /// <https://html.spec.whatwg.org/multipage/#fire-loadeddata>
197    ///
198    /// Reset to false every time the load algorithm is invoked.
199    fired_loadeddata_event: Cell<bool>,
200    /// <https://html.spec.whatwg.org/multipage/#dom-media-error>
201    error: MutNullableDom<MediaError>,
202    /// <https://html.spec.whatwg.org/multipage/#dom-media-paused>
203    paused: Cell<bool>,
204    /// <https://html.spec.whatwg.org/multipage/#dom-media-defaultplaybackrate>
205    default_playback_rate: Cell<f64>,
206    /// <https://html.spec.whatwg.org/multipage/#dom-media-playbackrate>
207    playback_rate: Cell<f64>,
208    /// <https://html.spec.whatwg.org/multipage/#attr-media-autoplay>
209    autoplaying: Cell<bool>,
210    /// <https://html.spec.whatwg.org/multipage/#delaying-the-load-event-flag>
211    delaying_the_load_event_flag: DomRefCell<Option<LoadBlocker>>,
212    /// <https://html.spec.whatwg.org/multipage/#list-of-pending-play-promises>
213    pending_play_promises: DomRefCell<Vec<TracedPromise>>,
214    /// Play promises which are soon to be fulfilled by a queued task.
215    in_flight_play_promises_queue: DomRefCell<VecDeque<(BoxedSliceOfPromises, ErrorResult)>>,
216    #[ignore_malloc_size_of = "servo_media"]
217    #[no_trace]
218    player: DomRefCell<Option<Arc<Mutex<dyn Player>>>>,
219    #[conditional_malloc_size_of]
220    #[no_trace]
221    video_renderer: Arc<Mutex<MediaFrameRenderer>>,
222    #[ignore_malloc_size_of = "servo_media"]
223    #[no_trace]
224    audio_renderer: DomRefCell<Option<Arc<Mutex<dyn AudioRenderer>>>>,
225    #[conditional_malloc_size_of]
226    #[no_trace]
227    event_handler: RefCell<Option<Arc<Mutex<HTMLMediaElementEventHandler>>>>,
228    /// <https://html.spec.whatwg.org/multipage/#show-poster-flag>
229    show_poster: Cell<bool>,
230    /// <https://html.spec.whatwg.org/multipage/#dom-media-duration>
231    duration: Cell<f64>,
232    /// <https://html.spec.whatwg.org/multipage/#current-playback-position>
233    current_playback_position: Cell<f64>,
234    /// <https://html.spec.whatwg.org/multipage/#official-playback-position>
235    official_playback_position: Cell<f64>,
236    /// <https://html.spec.whatwg.org/multipage/#default-playback-start-position>
237    default_playback_start_position: Cell<f64>,
238    /// <https://html.spec.whatwg.org/multipage/#dom-media-volume>
239    volume: Cell<f64>,
240    /// <https://html.spec.whatwg.org/multipage/#dom-media-seeking>
241    seeking: Cell<bool>,
242    /// The latest seek position (in seconds) is used to distinguish whether the seek request was
243    /// initiated by a script or by the user agent itself, rather than by the media engine and to
244    /// abort other running instance of the `seek` algorithm.
245    current_seek_position: Cell<f64>,
246    /// <https://html.spec.whatwg.org/multipage/#concept-media-muted-state>
247    /// > Each media element has a muted state, which is either true, false,
248    /// > or "default"; it is initially "default".
249    /// We model "default" as None
250    muted_state: Cell<Option<bool>>,
251    /// Loading state from source, if any.
252    load_state: Cell<LoadState>,
253    source_children_pointer: DomRefCell<Option<SourceChildrenPointer>>,
254    current_source_child: MutNullableDom<HTMLSourceElement>,
255    /// URL of the media resource, if any.
256    #[no_trace]
257    resource_url: DomRefCell<Option<ServoUrl>>,
258    /// URL of the media resource, if the resource is set through the src_object attribute and it
259    /// is a blob.
260    #[no_trace]
261    blob_url: DomRefCell<Option<ServoUrl>>,
262    /// <https://html.spec.whatwg.org/multipage/#dom-media-played>
263    played: DomRefCell<TimeRangesContainer>,
264    // https://html.spec.whatwg.org/multipage/#dom-media-audiotracks
265    audio_tracks_list: MutNullableDom<AudioTrackList>,
266    // https://html.spec.whatwg.org/multipage/#dom-media-videotracks
267    video_tracks_list: MutNullableDom<VideoTrackList>,
268    /// <https://html.spec.whatwg.org/multipage/#list-of-text-tracks>
269    text_tracks_list: MutNullableDom<TextTrackList>,
270    /// Time of last timeupdate notification.
271    next_timeupdate_event: Cell<Instant>,
272    /// Latest fetch request context.
273    current_fetch_context: RefCell<Option<HTMLMediaElementFetchContext>>,
274    /// Media controls id.
275    /// In order to workaround the lack of privileged JS context, we secure the
276    /// the access to the "privileged" document.servoGetMediaControls(id) API by
277    /// keeping a whitelist of media controls identifiers.
278    media_controls_id: DomRefCell<Option<String>>,
279    /// <https://html.spec.whatwg.org/multipage/#did-perform-automatic-track-selection>
280    did_perform_automatic_track_selection: Cell<bool>,
281    /// Used to track the
282    /// <https://html.spec.whatwg.org/multipage/#current-playback-position>
283    /// when
284    /// <https://html.spec.whatwg.org/multipage/#time-marches-on>
285    /// was last invoked (if any)
286    position_when_time_marches_on_ran: Cell<Option<f64>>,
287    /// <https://html.spec.whatwg.org/multipage/#list-of-newly-introduced-cues>
288    newly_introduced_cues: DomRefCell<Vec<Dom<TextTrackCue>>>,
289    shadow_tree: DomRefCell<Option<ShadowTree>>,
290}
291
292/// <https://html.spec.whatwg.org/multipage/#dom-media-networkstate>
293#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
294#[repr(u8)]
295pub(crate) enum NetworkState {
296    Empty = HTMLMediaElementConstants::NETWORK_EMPTY as u8,
297    Idle = HTMLMediaElementConstants::NETWORK_IDLE as u8,
298    Loading = HTMLMediaElementConstants::NETWORK_LOADING as u8,
299    NoSource = HTMLMediaElementConstants::NETWORK_NO_SOURCE as u8,
300}
301
302/// <https://html.spec.whatwg.org/multipage/#dom-media-readystate>
303#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq, PartialOrd)]
304#[repr(u8)]
305#[expect(clippy::enum_variant_names)] // Clippy warning silenced here because these names are from the specification.
306pub(crate) enum ReadyState {
307    HaveNothing = HTMLMediaElementConstants::HAVE_NOTHING as u8,
308    HaveMetadata = HTMLMediaElementConstants::HAVE_METADATA as u8,
309    HaveCurrentData = HTMLMediaElementConstants::HAVE_CURRENT_DATA as u8,
310    HaveFutureData = HTMLMediaElementConstants::HAVE_FUTURE_DATA as u8,
311    HaveEnoughData = HTMLMediaElementConstants::HAVE_ENOUGH_DATA as u8,
312}
313
314/// <https://html.spec.whatwg.org/multipage/#direction-of-playback>
315#[derive(Clone, Copy, PartialEq)]
316enum PlaybackDirection {
317    Forwards,
318    Backwards,
319}
320
321/// Used to determine whether the current playback position was changed
322/// during normal playback or not in
323/// <https://html.spec.whatwg.org/multipage/#time-marches-on>
324#[derive(Clone, Copy, Default, MallocSizeOf, PartialEq)]
325enum PlaybackPositionWasMoved {
326    #[default]
327    ExplicitMove,
328    NormalPlayback,
329}
330
331/// Holds handles to all slots in the UA shadow tree
332#[derive(Clone, JSTraceable, MallocSizeOf)]
333#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
334struct ShadowTree {
335    cue_container: Dom<Element>,
336}
337
338impl UAShadowRoot<ShadowTree> for HTMLMediaElement {
339    fn store_for_shadow_tree(&self, cx: &mut JSContext, shadow_root: DomRoot<ShadowRoot>) {
340        let root = self.create_element_in_ua_shadowroot(cx, local_name!("div"));
341        root.SetClassName(cx, DOMString::from_static("root"));
342        shadow_root
343            .upcast::<Node>()
344            .AppendChild(cx, root.upcast::<Node>())
345            .unwrap();
346
347        let cue_container = self.create_element_in_ua_shadowroot(cx, local_name!("div"));
348        cue_container.SetClassName(cx, DOMString::from_static("cue-container"));
349        root.upcast::<Node>()
350            .AppendChild(cx, cue_container.upcast::<Node>())
351            .unwrap();
352
353        let script = self.create_element_in_ua_shadowroot(cx, local_name!("script"));
354        // This is our hacky way to temporarily workaround the lack of a privileged
355        // JS context.
356        // The media controls UI accesses the document.servoGetMediaControls(id) API
357        // to get an instance to the media controls ShadowRoot.
358        // `id` needs to match the internally generated UUID assigned to a media element.
359        let id = Uuid::new_v4().to_string();
360        let document = self.owner_document();
361        document.register_media_controls(&id, &shadow_root);
362        let media_controls_script = MEDIA_CONTROL_JS.replace("@@@id@@@", &id);
363        *self.media_controls_id.borrow_mut() = Some(id);
364        script
365            .upcast::<Node>()
366            .set_text_content_for_element(cx, Some(DOMString::from(media_controls_script)));
367        if let Err(e) = shadow_root
368            .upcast::<Node>()
369            .AppendChild(cx, script.upcast::<Node>())
370        {
371            warn!("Could not render media controls {:?}", e);
372            return;
373        }
374
375        let style = self.create_element_in_ua_shadowroot(cx, local_name!("style"));
376
377        style
378            .upcast::<Node>()
379            .set_text_content_for_element(cx, Some(DOMString::from(MEDIA_CONTROL_CSS)));
380
381        if let Err(e) = shadow_root
382            .upcast::<Node>()
383            .AppendChild(cx, style.upcast::<Node>())
384        {
385            warn!("Could not render media controls {:?}", e);
386        }
387
388        let _ = self.shadow_tree.borrow_mut().insert(ShadowTree {
389            cue_container: cue_container.as_traced(),
390        });
391    }
392
393    fn borrow_for_shadow_tree(&self) -> Ref<'_, Option<ShadowTree>> {
394        self.shadow_tree.borrow()
395    }
396
397    // TODO: Remove this and properly implement controls in the UA shadow root
398    fn should_mark_as_ua_widget(&self) -> bool {
399        false
400    }
401}
402
403impl HTMLMediaElement {
404    pub(crate) fn new_inherited(
405        tag_name: LocalName,
406        prefix: Option<Prefix>,
407        document: &Document,
408    ) -> Self {
409        Self {
410            htmlelement: HTMLElement::new_inherited(tag_name, prefix, document),
411            network_state: Cell::new(NetworkState::Empty),
412            ready_state: Cell::new(ReadyState::HaveNothing),
413            src_object: Default::default(),
414            current_src: Default::default(),
415            generation_id: Cell::new(0),
416            fired_loadeddata_event: Cell::new(false),
417            error: Default::default(),
418            paused: Cell::new(true),
419            default_playback_rate: Cell::new(1.0),
420            playback_rate: Cell::new(1.0),
421            muted_state: Default::default(),
422            load_state: Cell::new(LoadState::NotLoaded),
423            source_children_pointer: DomRefCell::new(None),
424            current_source_child: Default::default(),
425            // FIXME(nox): Why is this initialised to true?
426            autoplaying: Cell::new(true),
427            delaying_the_load_event_flag: Default::default(),
428            pending_play_promises: Default::default(),
429            in_flight_play_promises_queue: Default::default(),
430            player: Default::default(),
431            video_renderer: Arc::new(Mutex::new(MediaFrameRenderer::new(
432                document.webview_id(),
433                document.window().paint_api().clone(),
434                document.window().get_player_context(),
435            ))),
436            audio_renderer: Default::default(),
437            event_handler: Default::default(),
438            show_poster: Cell::new(true),
439            duration: Cell::new(f64::NAN),
440            current_playback_position: Cell::new(0.),
441            official_playback_position: Cell::new(0.),
442            default_playback_start_position: Cell::new(0.),
443            volume: Cell::new(1.0),
444            seeking: Cell::new(false),
445            current_seek_position: Cell::new(f64::NAN),
446            resource_url: DomRefCell::new(None),
447            blob_url: DomRefCell::new(None),
448            played: DomRefCell::new(TimeRangesContainer::default()),
449            audio_tracks_list: Default::default(),
450            video_tracks_list: Default::default(),
451            text_tracks_list: Default::default(),
452            next_timeupdate_event: Cell::new(Instant::now() + Duration::from_millis(250)),
453            current_fetch_context: RefCell::new(None),
454            media_controls_id: DomRefCell::new(None),
455            did_perform_automatic_track_selection: Default::default(),
456            position_when_time_marches_on_ran: Default::default(),
457            newly_introduced_cues: Default::default(),
458            shadow_tree: Default::default(),
459        }
460    }
461
462    pub(crate) fn network_state(&self) -> NetworkState {
463        self.network_state.get()
464    }
465
466    pub(crate) fn get_ready_state(&self) -> ReadyState {
467        self.ready_state.get()
468    }
469
470    fn media_type_id(&self) -> HTMLMediaElementTypeId {
471        match self.upcast::<Node>().type_id() {
472            NodeTypeId::Element(ElementTypeId::HTMLElement(
473                HTMLElementTypeId::HTMLMediaElement(media_type_id),
474            )) => media_type_id,
475            _ => unreachable!(),
476        }
477    }
478
479    fn update_media_state(&self) {
480        let is_playing = self
481            .player
482            .borrow()
483            .as_ref()
484            .is_some_and(|player| !player.lock().unwrap().paused());
485
486        if self.is_potentially_playing() && !is_playing {
487            if let Some(ref player) = *self.player.borrow() {
488                let player = player.lock().unwrap();
489
490                if let Err(error) = player.set_playback_rate(self.playback_rate.get()) {
491                    warn!("Could not set the playback rate: {error:?}");
492                }
493                if let Err(error) = player.set_volume(self.volume.get()) {
494                    warn!("Could not set the volume: {error:?}");
495                }
496                if let Err(error) = player.play() {
497                    error!("Could not play media: {error:?}");
498                }
499            }
500        } else if is_playing &&
501            let Some(ref player) = *self.player.borrow() &&
502            let Err(error) = player.lock().unwrap().pause()
503        {
504            error!("Could not pause player: {error:?}");
505        }
506
507        self.update_keep_alive();
508    }
509
510    /// <https://html.spec.whatwg.org/multipage/#playing-the-media-resource>
511    /// > Media elements must not stop playing just because all references to them have been
512    /// > removed; only once a media element is in a state where no further audio could ever be
513    /// > played by that element may the element be garbage collected.
514    fn should_keep_alive(&self) -> bool {
515        let document = self.owner_document();
516        if !document.is_active() || document.is_prompting_or_unloading() {
517            return false;
518        }
519
520        if self.in_error_state() {
521            return false;
522        }
523
524        if self.ended_playback(LoopCondition::Included) {
525            return false;
526        }
527
528        if !self.Paused() {
529            return true;
530        }
531
532        if self.eligible_for_autoplay() {
533            return true;
534        }
535
536        false
537    }
538
539    pub(crate) fn update_keep_alive(&self) {
540        let document = self.owner_document();
541        if self.should_keep_alive() {
542            document.register_active_media_element(self);
543        } else {
544            document.unregister_active_media_element(self);
545        }
546    }
547
548    pub(crate) fn clean_up_on_document_unload(&self, unloading_doc: &Document, no_gc: &NoGC) {
549        if &*self.owner_document() != unloading_doc {
550            return;
551        }
552        self.paused.set(true);
553        if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() {
554            current_fetch_context.cancel(CancelReason::Abort);
555        }
556        self.reset_media_player(no_gc);
557    }
558
559    /// Marks that element as delaying the load event or not.
560    ///
561    /// Nothing happens if the element was already delaying the load event and
562    /// we pass true to that method again.
563    ///
564    /// <https://html.spec.whatwg.org/multipage/#delaying-the-load-event-flag>
565    fn delay_load_event(&self, cx: &mut JSContext, delay: bool) {
566        let blocker = &self.delaying_the_load_event_flag;
567
568        if delay {
569            if blocker.borrow().is_none() {
570                *blocker.borrow_mut() =
571                    Some(LoadBlocker::new(&self.owner_document(), LoadType::Media));
572            }
573        } else {
574            LoadBlocker::terminate(cx, blocker);
575        }
576    }
577
578    pub(crate) fn cue_container(&self, cx: &mut JSContext) -> DomRoot<Element> {
579        self.shadow_tree(cx).cue_container.as_rooted()
580    }
581
582    /// <https://html.spec.whatwg.org/multipage/#time-marches-on>
583    fn current_and_other_cues<'no_gc>(
584        &self,
585        no_gc: &'no_gc NoGC,
586        text_tracks_list: &TextTrackList,
587    ) -> (
588        Vec<UnrootedDom<'no_gc, TextTrackCue>>,
589        Vec<UnrootedDom<'no_gc, TextTrackCue>>,
590    ) {
591        // Step 1. Let current cues be a list of cues,
592        // initialized to contain all the cues of all the hidden or
593        // showing text tracks of the media element (not the disabled ones)
594        // whose start times are less than or equal to the current playback position
595        // and whose end times are greater than the current playback position.
596        // Step 2. Let other cues be a list of cues, initialized to contain
597        // all the cues of hidden and showing text tracks of the media element
598        // that are not present in current cues.
599        let current_playback_position = self.current_playback_position.get();
600        text_tracks_list
601            .iter(no_gc)
602            .filter(|text_track| text_track.Mode() != TextTrackMode::Disabled)
603            .flat_map(|text_track| text_track.cues(no_gc))
604            .partition(|cue| {
605                cue.start_time() <= current_playback_position &&
606                    cue.end_time() > current_playback_position
607            })
608    }
609
610    /// <https://html.spec.whatwg.org/multipage/#time-marches-on>
611    fn time_marches_on(&self, cx: &mut JSContext, playback_was_moved: PlaybackPositionWasMoved) {
612        let playback_was_moved_monotonic_increase =
613            playback_was_moved == PlaybackPositionWasMoved::NormalPlayback;
614
615        // Step 1. Let current cues be a list of cues, initialized to contain all the cues of all
616        // the hidden or showing text tracks of the media element (not the disabled ones) whose
617        // start times are less than or equal to the current playback position and whose end times
618        // are greater than the current playback position.
619        //
620        // Step 2. Let other cues be a list of cues, initialized to contain all the cues of hidden
621        // and showing text tracks of the media element that are not present in current cues.
622        let text_tracks_list = self.TextTracks(cx);
623        let (current_cues, other_cues) = self.current_and_other_cues(cx.no_gc(), &text_tracks_list);
624
625        // Step 3. Let last time be the current playback position at the time this algorithm was
626        // last run for this media element, if this is not the first time it has run.
627        let current_playback_position = self.current_playback_position.get();
628        let last_time = self.position_when_time_marches_on_ran.get();
629        self.position_when_time_marches_on_ran
630            .set(Some(current_playback_position));
631
632        // Step 4. If the current playback position has, since the last time this algorithm was run,
633        // only changed through its usual monotonic increase during normal playback, then let missed
634        // cues be the list of cues in other cues whose start times are greater than or equal to
635        // last time and whose end times are less than or equal to the current playback position.
636        // Otherwise, let missed cues be an empty list.
637        //
638        // Step 5. Remove all the cues in missed cues that are also in the media element's list of
639        // newly introduced cues, and then empty the element's list of newly introduced cues.
640        let missed_cues =
641            if playback_was_moved_monotonic_increase && let Some(last_time) = last_time {
642                let newly_introduced_cues: Vec<DomRoot<TextTrackCue>> =
643                    std::mem::take(&mut *self.newly_introduced_cues.safe_borrow_mut(cx.no_gc()))
644                        .iter()
645                        .map(|cue| cue.as_rooted())
646                        .collect();
647                other_cues
648                    .iter()
649                    .filter(|cue| {
650                        cue.start_time() >= last_time &&
651                            cue.end_time() <= current_playback_position &&
652                            !newly_introduced_cues
653                                .iter()
654                                .any(|newly_cue| **newly_cue == ****cue)
655                    })
656                    .map(|cue| cue.as_rooted())
657                    .collect()
658            } else {
659                self.newly_introduced_cues
660                    .safe_borrow_mut(cx.no_gc())
661                    .clear();
662                vec![]
663            };
664
665        // Step 6. If the time was reached through the usual monotonic increase of the current
666        // playback position during normal playback, and if the user agent has not fired a
667        // timeupdate event at the element in the past 15 to 250ms and is not still running event
668        // handlers for such an event, then the user agent must queue a media element task given the
669        // media element to fire an event named timeupdate at the element.
670        if playback_was_moved_monotonic_increase &&
671            Instant::now() > self.next_timeupdate_event.get()
672        {
673            self.queue_media_element_task_to_fire_event(atom!("timeupdate"));
674            self.next_timeupdate_event
675                .set(Instant::now() + Duration::from_millis(250));
676        }
677
678        // Step 7. If all of the cues in current cues have their text track cue active flag set,
679        // none of the cues in other cues have their text track cue active flag set, and missed cues
680        // is empty, then return.
681        if current_cues.iter().all(|cue| cue.is_active()) &&
682            !other_cues.iter().any(|cue| cue.is_active()) &&
683            missed_cues.is_empty()
684        {
685            return;
686        }
687
688        let current_cues: Vec<DomRoot<TextTrackCue>> =
689            current_cues.iter().map(|cue| cue.as_rooted()).collect();
690        let other_cues: Vec<DomRoot<TextTrackCue>> =
691            other_cues.iter().map(|cue| cue.as_rooted()).collect();
692
693        // Step 8. If the time was reached through the usual monotonic increase of the current
694        // playback position during normal playback, and there are cues in other cues that have
695        // their text track cue pause-on-exit flag set and that either have their text track cue
696        // active flag set or are also in missed cues, then immediately pause the media element.
697        if playback_was_moved_monotonic_increase &&
698            other_cues.iter().any(|cue| {
699                cue.PauseOnExit() &&
700                    (cue.is_active() || missed_cues.iter().any(|missed_cue| missed_cue == cue))
701            })
702        {
703            self.Pause(cx);
704        }
705
706        // Step 9. Let events be a list of tasks, initially empty. Each task in this list will be
707        // associated with a text track, a text track cue, and a time, which are used to sort the
708        // list before the tasks are queued. Let affected tracks be a list of text tracks, initially
709        // empty. When the steps below say to prepare an event named event for a text track cue
710        // target with a time time, the user agent must run these steps:
711        let mut events: Vec<(f64, (Atom, DomRoot<TextTrackCue>))> = vec![];
712        let no_gc = cx.no_gc();
713        let mut affected_tracks = vec![];
714        // https://html.spec.whatwg.org/multipage/#prepare-an-event
715        let mut prepare_an_event =
716            |time: f64, event: Atom, text_track_cue: DomRoot<TextTrackCue>| {
717                // Step 1. Let track be the text track with which the text track cue target is associated.
718                let track = text_track_cue
719                    .get_text_track()
720                    .expect("Must always have an associated text track");
721                // Step 2. Create a task to fire an event named event at target.
722                //
723                // We create the task in the for-loops below
724
725                // Step 3. Add the newly created task to events, associated with the time time,
726                // the text track track, and the text track cue target.
727                events.push((time, (event, text_track_cue)));
728                // Step 4. Add track to affected tracks.
729                affected_tracks.push(track.as_unrooted(no_gc));
730            };
731
732        // Step 10. For each text track cue in missed cues, prepare an event named enter for the
733        // TextTrackCue object with the text track cue start time.
734        for text_track_cue in &missed_cues {
735            prepare_an_event(
736                text_track_cue.start_time(),
737                atom!("enter"),
738                text_track_cue.clone(),
739            );
740        }
741
742        // Step 11. For each text track cue in other cues that either has its text track cue active
743        // flag set or is in missed cues, prepare an event named exit for the TextTrackCue object
744        // with the later of the text track cue end time and the text track cue start time.
745        for text_track_cue in &other_cues {
746            if text_track_cue.is_active() || missed_cues.iter().any(|cue| cue == text_track_cue) {
747                prepare_an_event(
748                    text_track_cue.start_time().max(text_track_cue.end_time()),
749                    atom!("exit"),
750                    text_track_cue.clone(),
751                );
752            }
753        }
754
755        // Step 12. For each text track cue in current cues that does not have its text track cue
756        // active flag set, prepare an event named enter for the TextTrackCue object with the text
757        // track cue start time.
758        for text_track_cue in &current_cues {
759            if !text_track_cue.is_active() {
760                prepare_an_event(
761                    text_track_cue.start_time(),
762                    atom!("enter"),
763                    text_track_cue.clone(),
764                );
765            }
766        }
767
768        // Step 13. Sort the tasks in events in ascending time order (tasks with earlier times first).
769        events.sort_by(|(a_time, _), (b_time, _)| a_time.total_cmp(b_time));
770
771        // Step 14. Queue a media element task given the media element for each task in events,
772        // in list order.
773        for (_, (event, text_track_cue)) in events {
774            let target = Trusted::new(&*text_track_cue);
775
776            self.owner_global()
777                .task_manager()
778                .media_element_task_source()
779                .queue(task!(queue_event: move |cx| {
780                    target.root().upcast::<EventTarget>().fire_event(cx, event);
781                }));
782        }
783
784        // Step 15. Sort affected tracks in the same order as the text tracks appear in the media
785        // element's list of text tracks, and remove duplicates.
786        let affected_tracks: Vec<DomRoot<TextTrack>> = text_tracks_list
787            .iter(cx.no_gc())
788            .filter(|text_track| affected_tracks.contains(text_track))
789            .map(|text_track| text_track.as_rooted())
790            .collect();
791
792        // Step 16. For each text track in affected tracks, in the list order, queue a media element
793        // task given the media element to fire an event named cuechange at the TextTrack object,
794        // and, if the text track has a corresponding track element, to then fire an event named
795        // cuechange at the track element as well.
796        for text_track in &affected_tracks {
797            let text_track = Trusted::new(&**text_track);
798
799            self.owner_global()
800                .task_manager()
801                .media_element_task_source()
802                .queue(task!(queue_event: move |cx| {
803                    let text_track = text_track.root();
804                    text_track.upcast::<EventTarget>().fire_event(cx, atom!("cuechange"));
805
806                    if let Some(track_element) = text_track.associated_track() {
807                        track_element.upcast::<EventTarget>().fire_event(cx, atom!("cuechange"));
808                    }
809                }));
810        }
811
812        // Step 17. Set the text track cue active flag of all the cues in the current cues,
813        // and unset the text track cue active flag of all the cues in the other cues.
814        for text_track_cue in current_cues {
815            text_track_cue.set_active(true);
816        }
817        for text_track_cue in other_cues {
818            text_track_cue.set_active(false);
819        }
820
821        // Step 18. Run the rules for updating the text track rendering of each of the text tracks
822        // in affected tracks that are showing, providing the text track's text track language as
823        // the fallback language if it is not the empty string. For example, for text tracks based
824        // on WebVTT, the rules for updating the display of WebVTT text tracks. [WEBVTT]
825        //
826        // TODO(https://github.com/whatwg/html/issues/12994): Figure out how to pass in language
827        // as well as handling multiple text tracks with different updating rules
828        RulesForUpdatingTheTextTrackRendering::WebVTT.run(
829            cx,
830            self,
831            affected_tracks,
832            None,
833            ShouldResetRenderingControls::No,
834        );
835    }
836
837    /// <https://html.spec.whatwg.org/multipage/#internal-play-steps>
838    fn internal_play_steps(&self, cx: &mut JSContext) {
839        // Step 1. If the media element's networkState attribute has the value NETWORK_EMPTY, invoke
840        // the media element's resource selection algorithm.
841        if self.network_state.get() == NetworkState::Empty {
842            self.invoke_resource_selection_algorithm(cx);
843        }
844
845        // Step 2. If the playback has ended and the direction of playback is forwards, seek to the
846        // earliest possible position of the media resource.
847        // Generally "ended" and "looping" are exclusive. Here, the loop attribute is ignored to
848        // seek back to start in case loop was set after playback ended.
849        // <https://github.com/whatwg/html/issues/4487>
850        if self.ended_playback(LoopCondition::Ignored) &&
851            self.direction_of_playback() == PlaybackDirection::Forwards
852        {
853            self.seek(
854                self.earliest_possible_position(),
855                /* approximate_for_speed */ false,
856            );
857        }
858
859        let state = self.ready_state.get();
860
861        // Step 3. If the media element's paused attribute is true, then:
862        if self.Paused() {
863            // Step 3.1. Change the value of paused to false.
864            self.paused.set(false);
865
866            // Step 3.2. If the show poster flag is true, set the element's show poster flag to
867            // false and run the time marches on steps.
868            if self.show_poster.get() {
869                self.show_poster.set(false);
870                self.time_marches_on(cx, PlaybackPositionWasMoved::NormalPlayback);
871            }
872
873            // Step 3.3. Queue a media element task given the media element to fire an event named
874            // play at the element.
875            self.queue_media_element_task_to_fire_event(atom!("play"));
876
877            // Step 3.4. If the media element's readyState attribute has the value HAVE_NOTHING,
878            // HAVE_METADATA, or HAVE_CURRENT_DATA, queue a media element task given the media
879            // element to fire an event named waiting at the element. Otherwise, the media element's
880            // readyState attribute has the value HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA: notify about
881            // playing for the element.
882            match state {
883                ReadyState::HaveNothing |
884                ReadyState::HaveMetadata |
885                ReadyState::HaveCurrentData => {
886                    self.queue_media_element_task_to_fire_event(atom!("waiting"));
887                },
888                ReadyState::HaveFutureData | ReadyState::HaveEnoughData => {
889                    self.notify_about_playing();
890                },
891            }
892        }
893        // Step 4. Otherwise, if the media element's readyState attribute has the value
894        // HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA, take pending play promises and queue a media
895        // element task given the media element to resolve pending play promises with the
896        // result.
897        else if state == ReadyState::HaveFutureData || state == ReadyState::HaveEnoughData {
898            self.take_pending_play_promises(Ok(()));
899
900            let this = Trusted::new(self);
901            let generation_id = self.generation_id.get();
902
903            self.owner_global()
904                .task_manager()
905                .media_element_task_source()
906                .queue(task!(resolve_pending_play_promises: move |cx| {
907                    let this = this.root();
908                    if generation_id != this.generation_id.get() {
909                        return;
910                    }
911
912                    this.fulfill_in_flight_play_promises(cx, |_| {});
913                }));
914        }
915
916        // Step 5. Set the media element's can autoplay flag to false.
917        self.autoplaying.set(false);
918
919        self.update_media_state();
920    }
921
922    /// <https://html.spec.whatwg.org/multipage/#internal-pause-steps>
923    fn internal_pause_steps(&self) {
924        // Step 1. Set the media element's can autoplay flag to false.
925        self.autoplaying.set(false);
926
927        // Step 2. If the media element's paused attribute is false, run the following steps:
928        if !self.Paused() {
929            // Step 2.1. Change the value of paused to true.
930            self.paused.set(true);
931
932            // Step 2.2. Take pending play promises and let promises be the result.
933            self.take_pending_play_promises(Err(Error::Abort(Some(
934                "Media element was paused".into(),
935            ))));
936
937            // Step 2.3. Queue a media element task given the media element and the following steps:
938            let this = Trusted::new(self);
939            let generation_id = self.generation_id.get();
940
941            self.owner_global()
942                .task_manager()
943                .media_element_task_source()
944                .queue(task!(internal_pause_steps: move |cx| {
945                    let this = this.root();
946                    if generation_id != this.generation_id.get() {
947                        return;
948                    }
949
950                    this.fulfill_in_flight_play_promises(cx, |cx| {
951                        // Step 2.3.1. Fire an event named timeupdate at the element.
952                        this.upcast::<EventTarget>().fire_event(cx, atom!("timeupdate"));
953
954                        // Step 2.3.2. Fire an event named pause at the element.
955                        this.upcast::<EventTarget>().fire_event(cx, atom!("pause"));
956
957                        // Step 2.3.3. Reject pending play promises with promises and an
958                        // "AbortError" DOMException.
959                        // Done after running this closure in `fulfill_in_flight_play_promises`.
960                    });
961                }));
962
963            // Step 2.4. Set the official playback position to the current playback position.
964            self.official_playback_position
965                .set(self.current_playback_position.get());
966        }
967
968        self.update_media_state();
969    }
970
971    /// <https://html.spec.whatwg.org/multipage/#allowed-to-play>
972    fn is_allowed_to_play(&self) -> bool {
973        true
974    }
975
976    /// <https://html.spec.whatwg.org/multipage/#notify-about-playing>
977    fn notify_about_playing(&self) {
978        // Step 1. Take pending play promises and let promises be the result.
979        self.take_pending_play_promises(Ok(()));
980
981        // Step 2. Queue a media element task given the element and the following steps:
982        let this = Trusted::new(self);
983        let generation_id = self.generation_id.get();
984
985        self.owner_global()
986            .task_manager()
987            .media_element_task_source()
988            .queue(task!(notify_about_playing: move |cx| {
989                let this = this.root();
990                if generation_id != this.generation_id.get() {
991                    return;
992                }
993
994                this.fulfill_in_flight_play_promises(cx, |cx| {
995                    // Step 2.1. Fire an event named playing at the element.
996                    this.upcast::<EventTarget>().fire_event(cx, atom!("playing"));
997
998                    // Step 2.2. Resolve pending play promises with promises.
999                    // Done after running this closure in `fulfill_in_flight_play_promises`.
1000                });
1001            }));
1002    }
1003
1004    /// <https://html.spec.whatwg.org/multipage/#ready-states>
1005    fn change_ready_state(&self, cx: &mut JSContext, ready_state: ReadyState) {
1006        let old_ready_state = self.ready_state.get();
1007        let was_potentially_playing = self.is_potentially_playing();
1008        self.ready_state.set(ready_state);
1009
1010        // > When the ready state of a media element whose networkState is not NETWORK_EMPTY changes,
1011        // > the user agent must follow the steps given below:
1012        if self.network_state.get() == NetworkState::Empty {
1013            return;
1014        }
1015        if old_ready_state == ready_state {
1016            return;
1017        }
1018
1019        // Step 1. Apply the first applicable set of substeps from the following list:
1020        match (old_ready_state, ready_state) {
1021            // => "If the previous ready state was HAVE_NOTHING, and the new ready state is
1022            // HAVE_METADATA"
1023            (ReadyState::HaveNothing, ReadyState::HaveMetadata) => {
1024                // > Queue a media element task given the media element to fire an event named
1025                // > loadedmetadata at the element.
1026                self.queue_media_element_task_to_fire_event(atom!("loadedmetadata"));
1027                return;
1028            },
1029            // => "If the previous ready state was HAVE_METADATA and the new ready state is
1030            // HAVE_CURRENT_DATA or greater"
1031            (ReadyState::HaveMetadata, new) if new >= ReadyState::HaveCurrentData => {
1032                // > If this is the first time this occurs for this media element since the load()
1033                // > algorithm was last invoked, the user agent must queue a media element task given
1034                // > the media element to fire an event named loadeddata at the element.
1035                if !self.fired_loadeddata_event.get() {
1036                    self.fired_loadeddata_event.set(true);
1037
1038                    let this = Trusted::new(self);
1039                    let generation_id = self.generation_id.get();
1040
1041                    self.owner_global()
1042                        .task_manager()
1043                        .media_element_task_source()
1044                        .queue(task!(media_reached_current_data: move |cx| {
1045                            let this = this.root();
1046                            if generation_id != this.generation_id.get() {
1047                                return;
1048                            }
1049
1050                            this.upcast::<EventTarget>().fire_event(cx, atom!("loadeddata"));
1051                            // Once the readyState attribute reaches HAVE_CURRENT_DATA, after the
1052                            // loadeddata event has been fired, set the element's
1053                            // delaying-the-load-event flag to false.
1054                            this.delay_load_event(cx, false);
1055                        }));
1056                }
1057
1058                // > If the new ready state is HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA,
1059                // > then the relevant steps below must then be run also.
1060                if new != ReadyState::HaveFutureData && new != ReadyState::HaveEnoughData {
1061                    return;
1062                }
1063            },
1064            _ => {},
1065        }
1066        match (old_ready_state, ready_state) {
1067            // => "If the previous ready state was HAVE_FUTURE_DATA or more,
1068            // and the new ready state is HAVE_CURRENT_DATA or less"
1069            (old, new)
1070                if old >= ReadyState::HaveFutureData && new <= ReadyState::HaveCurrentData =>
1071            {
1072                // > If the media element was potentially playing before its readyState
1073                // > attribute changed to a value lower than HAVE_FUTURE_DATA,
1074                // > and the element has not ended playback,
1075                // > and playback has not stopped due to errors,
1076                // > paused for user interaction, or paused for in-band content,
1077                // > the user agent must queue a media element task given the media element
1078                // > to fire an event named timeupdate at the element,
1079                // > and queue a media element task given the media element
1080                // > to fire an event named waiting at the element.
1081                if was_potentially_playing &&
1082                    !self.ended_playback(LoopCondition::Included) &&
1083                    !self.error.get().is_none() &&
1084                    !self.is_paused_for_user_interaction() &&
1085                    !self.is_paused_for_in_band_content()
1086                {
1087                    self.queue_media_element_task_to_fire_event(atom!("timeupdate"));
1088                    self.queue_media_element_task_to_fire_event(atom!("waiting"));
1089                }
1090            },
1091
1092            // => "If the previous ready state was HAVE_CURRENT_DATA or less,
1093            // and the new ready state is HAVE_FUTURE_DATA"
1094            (old, ReadyState::HaveFutureData) if old <= ReadyState::HaveCurrentData => {
1095                // > The user agent must queue a media element task given the media element to fire an
1096                // > event named canplay at the element.
1097                self.queue_media_element_task_to_fire_event(atom!("canplay"));
1098
1099                // > If the element's paused attribute is false, the user agent must notify about playing
1100                // > for the element.
1101                if !self.Paused() {
1102                    self.notify_about_playing();
1103                }
1104            },
1105
1106            // >= "If the new ready state is HAVE_ENOUGH_DATA"
1107            (_, ReadyState::HaveEnoughData) => {
1108                // > If the previous ready state was HAVE_CURRENT_DATA or less,
1109                // > the user agent must queue a media element task given the media element
1110                // > to fire an event named canplay at the element, and,
1111                // > if the element's paused attribute is false, notify about playing for the element.
1112                if old_ready_state <= ReadyState::HaveCurrentData {
1113                    self.queue_media_element_task_to_fire_event(atom!("canplay"));
1114                    if !self.Paused() {
1115                        self.notify_about_playing();
1116                    }
1117                }
1118
1119                // > The user agent must queue a media element task given the media element to fire an
1120                // > event named canplaythrough at the element.
1121                self.queue_media_element_task_to_fire_event(atom!("canplaythrough"));
1122
1123                // > If the element is not eligible for autoplay,
1124                // > then the user agent must abort these substeps.
1125                if !self.eligible_for_autoplay() {
1126                    self.update_media_state();
1127                    return;
1128                }
1129
1130                // > The user agent may run the following substeps:
1131                if self.eligible_for_autoplay() {
1132                    // Step 1. Set the paused attribute to false.
1133                    self.paused.set(false);
1134
1135                    // Step 2. If the element's show poster flag is true, set it to false and run the
1136                    // time marches on steps.
1137                    if self.show_poster.get() {
1138                        self.show_poster.set(false);
1139                        self.time_marches_on(cx, PlaybackPositionWasMoved::NormalPlayback);
1140                    }
1141
1142                    // Step 3. Queue a media element task given the element to fire an event named play
1143                    // at the element.
1144                    self.queue_media_element_task_to_fire_event(atom!("play"));
1145
1146                    // Step 4. Notify about playing for the element.
1147                    self.notify_about_playing();
1148                }
1149
1150                // > Alternatively, if the element is a video element,
1151                // > the user agent may start observing whether the element intersects the viewport.
1152                // > When the element starts intersecting the viewport,
1153                // > if the element is still eligible for autoplay, run the substeps above.
1154                // > Optionally, when the element stops intersecting the viewport,
1155                // > if the can autoplay flag is still true and the autoplay attribute
1156                // > is still specified, run the following substeps:
1157                // TODO
1158            },
1159
1160            _ => (),
1161        }
1162
1163        self.update_media_state();
1164    }
1165
1166    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1167    fn invoke_resource_selection_algorithm(&self, cx: &mut JSContext) {
1168        // Step 1. Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1169        self.network_state.set(NetworkState::NoSource);
1170
1171        // Step 2. Set the element's show poster flag to true.
1172        self.show_poster.set(true);
1173
1174        // Step 3. Set the media element's delaying-the-load-event flag to true (this delays the
1175        // load event).
1176        self.delay_load_event(cx, true);
1177
1178        // Step 4. Await a stable state, allowing the task that invoked this algorithm to continue.
1179        // If the resource selection mode in the synchronous section is
1180        // "attribute", the URL of the resource to fetch is relative to the
1181        // media element's node document when the src attribute was last
1182        // changed, which is why we need to pass the base URL in the task
1183        // right here.
1184        let task = MediaElementMicrotask::ResourceSelection {
1185            elem: Dom::from_ref(self),
1186            generation_id: self.generation_id.get(),
1187            base_url: self.owner_document().base_url(),
1188        };
1189
1190        // FIXME(nox): This will later call the resource_selection_algorithm_sync
1191        // method from below, if microtasks were trait objects, we would be able
1192        // to put the code directly in this method, without the boilerplate
1193        // indirections.
1194        ScriptThread::await_stable_state(cx, Box::new(task));
1195    }
1196
1197    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1198    fn resource_selection_algorithm_sync(&self, cx: &mut JSContext, base_url: ServoUrl) {
1199        // TODO Step 5. If the media element's blocked-on-parser flag is false, then populate the
1200        // list of pending text tracks.
1201        // FIXME(ferjm): Implement blocked_on_parser logic
1202        // https://html.spec.whatwg.org/multipage/#blocked-on-parser
1203        // FIXME(nox): Maybe populate the list of pending text tracks.
1204
1205        enum Mode {
1206            Object,
1207            Attribute(String),
1208            Children(DomRoot<HTMLSourceElement>),
1209        }
1210
1211        // Step 6.
1212        let mode = if self.src_object.borrow().is_some() {
1213            // If the media element has an assigned media provider object, then let mode be object.
1214            Mode::Object
1215        } else if let Some(src) = self
1216            .upcast::<Element>()
1217            .get_attribute_string_value(&local_name!("src"))
1218        {
1219            // Otherwise, if the media element has no assigned media provider object but has a src
1220            // attribute, then let mode be attribute.
1221            Mode::Attribute(src)
1222        } else if let Some(source) = self
1223            .upcast::<Node>()
1224            .children_unrooted(cx.no_gc())
1225            .find_map(UnrootedDom::downcast::<HTMLSourceElement>)
1226        {
1227            // Otherwise, if the media element does not have an assigned media provider object and
1228            // does not have a src attribute, but does have a source element child, then let mode be
1229            // children and let candidate be the first such source element child in tree order.
1230            Mode::Children(source.as_rooted())
1231        } else {
1232            // Otherwise, the media element has no assigned media provider object and has neither a
1233            // src attribute nor a source element child:
1234            self.load_state.set(LoadState::NotLoaded);
1235
1236            // Step 6.none.1. Set the networkState to NETWORK_EMPTY.
1237            self.network_state.set(NetworkState::Empty);
1238
1239            // Step 6.none.2. Set the element's delaying-the-load-event flag to false. This stops
1240            // delaying the load event.
1241            self.delay_load_event(cx, false);
1242
1243            // Step 6.none.3. End the synchronous section and return.
1244            return;
1245        };
1246
1247        // Step 7. Set the media element's networkState to NETWORK_LOADING.
1248        self.network_state.set(NetworkState::Loading);
1249
1250        // Step 8. Queue a media element task given the media element to fire an event named
1251        // loadstart at the media element.
1252        self.queue_media_element_task_to_fire_event(atom!("loadstart"));
1253
1254        // Step 9. Run the appropriate steps from the following list:
1255        match mode {
1256            Mode::Object => {
1257                // => "If mode is object"
1258                self.load_from_src_object(cx);
1259            },
1260            Mode::Attribute(src) => {
1261                // => "If mode is attribute"
1262                self.load_from_src_attribute(cx, base_url, &src);
1263            },
1264            Mode::Children(source) => {
1265                // => "Otherwise (mode is children)""
1266                self.load_from_source_child(cx, &source);
1267            },
1268        }
1269    }
1270
1271    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1272    fn load_from_src_object(&self, cx: &JSContext) {
1273        self.load_state.set(LoadState::LoadingFromSrcObject);
1274
1275        // Step 9.object.1. Set the currentSrc attribute to the empty string.
1276        "".clone_into(&mut self.current_src.safe_borrow_mut(cx.no_gc()));
1277
1278        // Step 9.object.3. Run the resource fetch algorithm with the assigned media
1279        // provider object. If that algorithm returns without aborting this one, then the
1280        // load failed.
1281        // Note that the resource fetch algorithm itself takes care of the cleanup in case
1282        // of failure itself.
1283        self.resource_fetch_algorithm(cx, Resource::Object);
1284    }
1285
1286    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1287    fn load_from_src_attribute(&self, cx: &JSContext, base_url: ServoUrl, src: &str) {
1288        self.load_state.set(LoadState::LoadingFromSrcAttribute);
1289
1290        // Step 9.attribute.1. If the src attribute's value is the empty string, then end
1291        // the synchronous section, and jump down to the failed with attribute step below.
1292        if src.is_empty() {
1293            self.queue_dedicated_media_source_failure_steps();
1294            return;
1295        }
1296
1297        // Step 9.attribute.2. Let urlRecord be the result of encoding-parsing a URL given
1298        // the src attribute's value, relative to the media element's node document when the
1299        // src attribute was last changed.
1300        let Ok(url_record) = self
1301            .owner_document()
1302            .encoding_parse_a_url_with_base(src, &base_url)
1303        else {
1304            self.queue_dedicated_media_source_failure_steps();
1305            return;
1306        };
1307
1308        // Step 9.attribute.3. If urlRecord is not failure, then set the currentSrc
1309        // attribute to the result of applying the URL serializer to urlRecord.
1310        *self.current_src.safe_borrow_mut(cx.no_gc()) = url_record.as_str().into();
1311
1312        // Step 9.attribute.5. If urlRecord is not failure, then run the resource fetch
1313        // algorithm with urlRecord. If that algorithm returns without aborting this one,
1314        // then the load failed.
1315        // Note that the resource fetch algorithm itself takes care
1316        // of the cleanup in case of failure itself.
1317        self.resource_fetch_algorithm(cx, Resource::Url(url_record));
1318    }
1319
1320    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1321    fn load_from_source_child(&self, cx: &JSContext, source: &HTMLSourceElement) {
1322        self.load_state.set(LoadState::LoadingFromSourceChild);
1323
1324        // Step 9.children.1. Let pointer be a position defined by two adjacent nodes in the media
1325        // element's child list, treating the start of the list (before the first child in the list,
1326        // if any) and end of the list (after the last child in the list, if any) as nodes in their
1327        // own right. One node is the node before pointer, and the other node is the node after
1328        // pointer. Initially, let pointer be the position between the candidate node and the next
1329        // node, if there are any, or the end of the list, if it is the last node.
1330        *self.source_children_pointer.borrow_mut() =
1331            Some(SourceChildrenPointer::new(source, false));
1332
1333        let element = source.upcast::<Element>();
1334
1335        // Step 9.children.2. Process candidate: If candidate does not have a src attribute, or if
1336        // its src attribute's value is the empty string, then end the synchronous section, and jump
1337        // down to the failed with elements step below.
1338        let Some(src) = element
1339            .get_attribute_string_value(&local_name!("src"))
1340            .filter(|value| !value.is_empty())
1341        else {
1342            self.load_from_source_child_failure_steps(cx, source);
1343            return;
1344        };
1345
1346        // Step 9.children.3. If candidate has a media attribute whose value does not match the
1347        // environment, then end the synchronous section, and jump down to the failed with elements
1348        // step below.
1349        if let Some(media) = element.get_attribute_string_value(&local_name!("media")) &&
1350            !MediaList::matches_environment(&element.owner_document(), &media)
1351        {
1352            self.load_from_source_child_failure_steps(cx, source);
1353            return;
1354        }
1355
1356        // Step 9.children.4. Let urlRecord be the result of encoding-parsing a URL given
1357        // candidate's src attribute's value, relative to candidate's node document when the src
1358        // attribute was last changed.
1359        let Ok(url_record) = source.owner_document().encoding_parse_a_url(&src) else {
1360            // Step 9.children.5. If urlRecord is failure, then end the synchronous section,
1361            // and jump down to the failed with elements step below.
1362            self.load_from_source_child_failure_steps(cx, source);
1363            return;
1364        };
1365
1366        // Step 9.children.6. If candidate has a type attribute whose value, when parsed as a MIME
1367        // type (including any codecs described by the codecs parameter, for types that define that
1368        // parameter), represents a type that the user agent knows it cannot render, then end the
1369        // synchronous section, and jump down to the failed with elements step below.
1370        if let Some(type_) = element.get_attribute_string_value(&local_name!("type")) &&
1371            ServoMedia::get().can_play_type(&type_) == SupportsMediaType::No
1372        {
1373            self.load_from_source_child_failure_steps(cx, source);
1374            return;
1375        }
1376
1377        // Reset the media player before loading the next source child.
1378        self.reset_media_player(cx.no_gc());
1379
1380        self.current_source_child.set(Some(source));
1381
1382        // Step 9.children.7. Set the currentSrc attribute to the result of applying the URL
1383        // serializer to urlRecord.
1384        *self.current_src.safe_borrow_mut(cx.no_gc()) = url_record.as_str().into();
1385
1386        // Step 9.children.9. Run the resource fetch algorithm with urlRecord. If that
1387        // algorithm returns without aborting this one, then the load failed.
1388        // Note that the resource fetch algorithm itself takes care
1389        // of the cleanup in case of failure itself.
1390        self.resource_fetch_algorithm(cx, Resource::Url(url_record));
1391    }
1392
1393    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1394    fn load_from_source_child_failure_steps(&self, cx: &JSContext, source: &HTMLSourceElement) {
1395        // Step 9.children.10. Failed with elements: Queue a media element task given the media
1396        // element to fire an event named error at candidate.
1397        let trusted_this = Trusted::new(self);
1398        let trusted_source = Trusted::new(source);
1399        let generation_id = self.generation_id.get();
1400
1401        self.owner_global()
1402            .task_manager()
1403            .media_element_task_source()
1404            .queue(task!(queue_error_event: move |cx| {
1405                let this = trusted_this.root();
1406                if generation_id != this.generation_id.get() {
1407                    return;
1408                }
1409
1410                let source = trusted_source.root();
1411                source.upcast::<EventTarget>().fire_event(cx, atom!("error"));
1412            }));
1413
1414        // Step 9.children.11. Await a stable state.
1415        let task = MediaElementMicrotask::SelectNextSourceChild {
1416            elem: Dom::from_ref(self),
1417            generation_id: self.generation_id.get(),
1418        };
1419
1420        ScriptThread::await_stable_state(cx, Box::new(task));
1421    }
1422
1423    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1424    fn select_next_source_child(&self, cx: &mut JSContext) {
1425        // Step 9.children.12. Forget the media element's media-resource-specific tracks.
1426        self.AudioTracks(cx).clear();
1427        self.VideoTracks(cx).clear();
1428
1429        // Step 9.children.13. Find next candidate: Let candidate be null.
1430        let mut source_candidate = None;
1431
1432        // Step 9.children.14. Search loop: If the node after pointer is the end of the list, then
1433        // jump to the waiting step below.
1434        // Step 9.children.15. If the node after pointer is a source element, let candidate be that
1435        // element.
1436        // Step 9.children.16. Advance pointer so that the node before pointer is now the node that
1437        // was after pointer, and the node after pointer is the node after the node that used to be
1438        // after pointer, if any.
1439        if let Some(ref source_children_pointer) = *self.source_children_pointer.borrow() {
1440            // Note that shared implementation between opaque types from
1441            // `inclusively_following_siblings` and `following_siblings` if not possible due to
1442            // precise capturing.
1443            if source_children_pointer.inclusive {
1444                for next_sibling in source_children_pointer
1445                    .source_before_pointer
1446                    .upcast::<Node>()
1447                    .inclusively_following_siblings()
1448                {
1449                    if let Some(next_source) = DomRoot::downcast::<HTMLSourceElement>(next_sibling)
1450                    {
1451                        source_candidate = Some(next_source);
1452                        break;
1453                    }
1454                }
1455            } else {
1456                for next_sibling in source_children_pointer
1457                    .source_before_pointer
1458                    .upcast::<Node>()
1459                    .following_siblings()
1460                {
1461                    if let Some(next_source) = DomRoot::downcast::<HTMLSourceElement>(next_sibling)
1462                    {
1463                        source_candidate = Some(next_source);
1464                        break;
1465                    }
1466                }
1467            };
1468        }
1469
1470        // Step 9.children.17. If candidate is null, jump back to the search loop step. Otherwise,
1471        // jump back to the process candidate step.
1472        if let Some(source_candidate) = source_candidate {
1473            self.load_from_source_child(cx, &source_candidate);
1474            return;
1475        }
1476
1477        self.load_state.set(LoadState::WaitingForSource);
1478
1479        *self.source_children_pointer.borrow_mut() = None;
1480
1481        // Step 9.children.18. Waiting: Set the element's networkState attribute to the
1482        // NETWORK_NO_SOURCE value.
1483        self.network_state.set(NetworkState::NoSource);
1484
1485        // Step 9.children.19. Set the element's show poster flag to true.
1486        self.show_poster.set(true);
1487
1488        // Step 9.children.20. Queue a media element task given the media element to set the
1489        // element's delaying-the-load-event flag to false. This stops delaying the load event.
1490        let this = Trusted::new(self);
1491        let generation_id = self.generation_id.get();
1492
1493        self.owner_global()
1494            .task_manager()
1495            .media_element_task_source()
1496            .queue(task!(queue_delay_load_event: move |cx| {
1497                let this = this.root();
1498                if generation_id != this.generation_id.get() {
1499                    return;
1500                }
1501
1502                this.delay_load_event(cx, false);
1503            }));
1504
1505        // Step 9.children.22. Wait until the node after pointer is a node other than the end of the
1506        // list. (This step might wait forever.)
1507    }
1508
1509    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
1510    fn resource_selection_algorithm_failure_steps(&self, cx: &JSContext) {
1511        match self.load_state.get() {
1512            LoadState::LoadingFromSrcObject => {
1513                // Step 9.object.4. Failed with media provider: Reaching this step indicates that
1514                // the media resource failed to load. Take pending play promises and queue a media
1515                // element task given the media element to run the dedicated media source failure
1516                // steps with the result.
1517                self.queue_dedicated_media_source_failure_steps();
1518            },
1519            LoadState::LoadingFromSrcAttribute => {
1520                // Step 9.attribute.6. Failed with attribute: Reaching this step indicates that the
1521                // media resource failed to load or that urlRecord is failure. Take pending play
1522                // promises and queue a media element task given the media element to run the
1523                // dedicated media source failure steps with the result.
1524                self.queue_dedicated_media_source_failure_steps();
1525            },
1526            LoadState::LoadingFromSourceChild => {
1527                // Step 9.children.10. Failed with elements: Queue a media element task given the
1528                // media element to fire an event named error at candidate.
1529                if let Some(source) = self.current_source_child.take() {
1530                    self.load_from_source_child_failure_steps(cx, &source);
1531                }
1532            },
1533            _ => {},
1534        }
1535    }
1536
1537    fn fetch_request(&self, cx: &JSContext, offset: Option<u64>, seek_lock: Option<SeekLock>) {
1538        if self.resource_url.borrow().is_none() && self.blob_url.borrow().is_none() {
1539            error!("Missing request url");
1540            if let Some(seek_lock) = seek_lock {
1541                seek_lock.unlock(/* successful seek */ false);
1542            }
1543            self.resource_selection_algorithm_failure_steps(cx);
1544            return;
1545        }
1546
1547        let document = self.owner_document();
1548        let destination = match self.media_type_id() {
1549            HTMLMediaElementTypeId::HTMLAudioElement => Destination::Audio,
1550            HTMLMediaElementTypeId::HTMLVideoElement => Destination::Video,
1551        };
1552        let mut headers = HeaderMap::new();
1553        if let Ok(range_header_value) = RangeHeader::bytes(offset.unwrap_or(0)..) {
1554            headers.typed_insert(range_header_value);
1555        }
1556        let url = match self.resource_url.borrow().as_ref() {
1557            Some(url) => url.clone(),
1558            None => self.blob_url.borrow().as_ref().unwrap().clone(),
1559        };
1560
1561        let cors_setting = cors_setting_for_element(self.upcast());
1562        let global = self.global();
1563        let url_with_blob_claim = ensure_blob_referenced_by_url_is_kept_alive(&global, url.clone());
1564        let request = create_a_potential_cors_request_with_claim(
1565            Some(document.webview_id()),
1566            url_with_blob_claim,
1567            destination,
1568            cors_setting,
1569            None,
1570            global.get_referrer(),
1571        )
1572        .with_global_scope(&global)
1573        .headers(headers)
1574        .referrer_policy(referrer_policy_for_element(self.upcast()));
1575
1576        let mut current_fetch_context = self.current_fetch_context.borrow_mut();
1577        if let Some(ref mut current_fetch_context) = *current_fetch_context {
1578            current_fetch_context.cancel(CancelReason::Abort);
1579        }
1580
1581        *current_fetch_context = Some(HTMLMediaElementFetchContext::new(
1582            request.id,
1583            global.core_resource_thread(),
1584        ));
1585        let listener =
1586            HTMLMediaElementFetchListener::new(self, request.id, url, offset.unwrap_or(0));
1587
1588        self.owner_document().fetch_background(request, listener);
1589
1590        // Since we cancelled the previous fetch, from now on the media element
1591        // will only receive response data from the new fetch that's been
1592        // initiated. This means the player can resume operation, since all subsequent data
1593        // pushes will originate from the new seek offset.
1594        if let Some(seek_lock) = seek_lock {
1595            seek_lock.unlock(/* successful seek */ true);
1596        }
1597    }
1598
1599    /// <https://html.spec.whatwg.org/multipage/#eligible-for-autoplay>
1600    fn eligible_for_autoplay(&self) -> bool {
1601        // its can autoplay flag is true;
1602        self.autoplaying.get() &&
1603
1604        // its paused attribute is true;
1605        self.Paused() &&
1606
1607        // it has an autoplay attribute specified;
1608        self.Autoplay() &&
1609
1610        // its node document's active sandboxing flag set does not have the sandboxed automatic
1611        // features browsing context flag set; and
1612        {
1613            let document = self.owner_document();
1614
1615            !document.has_active_sandboxing_flag(
1616                SandboxingFlagSet::SANDBOXED_AUTOMATIC_FEATURES_BROWSING_CONTEXT_FLAG,
1617            )
1618        }
1619
1620        // its node document is allowed to use the "autoplay" feature.
1621        // TODO: Feature policy: https://html.spec.whatwg.org/iframe-embed-object.html#allowed-to-use
1622    }
1623
1624    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-resource>
1625    fn resource_fetch_algorithm(&self, cx: &JSContext, resource: Resource) {
1626        if let Err(e) = self.create_media_player(&resource) {
1627            error!("Create media player error {:?}", e);
1628            self.resource_selection_algorithm_failure_steps(cx);
1629            return;
1630        }
1631
1632        // Steps 1-2.
1633        // Unapplicable, the `resource` variable already conveys which mode
1634        // is in use.
1635
1636        // Step 3.
1637        // FIXME(nox): Remove all media-resource-specific text tracks.
1638
1639        // Step 5. Run the appropriate steps from the following list:
1640        match resource {
1641            Resource::Url(url) => {
1642                // Step 5.remote.1. Optionally, run the following substeps. This is the expected
1643                // behavior if the user agent intends to not attempt to fetch the resource until the
1644                // user requests it explicitly (e.g. as a way to implement the preload attribute's
1645                // none keyword).
1646                if self.Preload() == "none" && !self.autoplaying.get() {
1647                    // Step 5.remote.1.1. Set the networkState to NETWORK_IDLE.
1648                    self.network_state.set(NetworkState::Idle);
1649
1650                    // Step 5.remote.1.2. Queue a media element task given the media element to fire
1651                    // an event named suspend at the element.
1652                    self.queue_media_element_task_to_fire_event(atom!("suspend"));
1653
1654                    // Step 5.remote.1.3. Queue a media element task given the media element to set
1655                    // the element's delaying-the-load-event flag to false. This stops delaying the
1656                    // load event.
1657                    let this = Trusted::new(self);
1658                    let generation_id = self.generation_id.get();
1659
1660                    self.owner_global()
1661                        .task_manager()
1662                        .media_element_task_source()
1663                        .queue(task!(queue_delay_load_event: move |cx| {
1664                            let this = this.root();
1665                            if generation_id != this.generation_id.get() {
1666                                return;
1667                            }
1668
1669                            this.delay_load_event(cx, false);
1670                        }));
1671
1672                    // TODO Steps 5.remote.1.4. Wait for the task to be run.
1673                    // FIXME(nox): Somehow we should wait for the task from previous
1674                    // step to be ran before continuing.
1675
1676                    // TODO Steps 5.remote.1.5-5.remote.1.7.
1677                    // FIXME(nox): Wait for an implementation-defined event and
1678                    // then continue with the normal set of steps instead of just
1679                    // returning.
1680                    return;
1681                }
1682
1683                *self.resource_url.borrow_mut() = Some(url);
1684
1685                // Steps 5.remote.2-5.remote.8
1686                self.fetch_request(cx, None, None);
1687            },
1688            Resource::Object => {
1689                if let Some(ref src_object) = *self.src_object.borrow() {
1690                    match src_object {
1691                        SrcObject::Blob(blob) => {
1692                            let blob_url = URL::CreateObjectURL(&self.global(), blob);
1693                            *self.blob_url.borrow_mut() =
1694                                Some(ServoUrl::parse(&blob_url.str()).expect("infallible"));
1695                            self.fetch_request(cx, None, None);
1696                        },
1697                        SrcObject::MediaStream(stream) => {
1698                            let tracks = &*stream.get_tracks();
1699                            for (pos, track) in tracks.iter().enumerate() {
1700                                if self
1701                                    .player
1702                                    .borrow()
1703                                    .as_ref()
1704                                    .unwrap()
1705                                    .lock()
1706                                    .unwrap()
1707                                    .set_stream(&track.id(), pos == tracks.len() - 1)
1708                                    .is_err()
1709                                {
1710                                    self.resource_selection_algorithm_failure_steps(cx);
1711                                }
1712                            }
1713                        },
1714                    }
1715                }
1716            },
1717        }
1718    }
1719
1720    /// Queues a task to run the [dedicated media source failure steps][steps].
1721    ///
1722    /// [steps]: https://html.spec.whatwg.org/multipage/#dedicated-media-source-failure-steps
1723    fn queue_dedicated_media_source_failure_steps(&self) {
1724        let this = Trusted::new(self);
1725        let generation_id = self.generation_id.get();
1726        self.take_pending_play_promises(Err(Error::NotSupported(Some(
1727            "Media source is not supported".into(),
1728        ))));
1729        self.owner_global()
1730            .task_manager()
1731            .media_element_task_source()
1732            .queue(task!(dedicated_media_source_failure_steps: move |cx| {
1733                let this = this.root();
1734                if generation_id != this.generation_id.get() {
1735                    return;
1736                }
1737
1738                this.fulfill_in_flight_play_promises(cx, |cx| {
1739                    // Step 1. Set the error attribute to the result of creating a MediaError with
1740                    // MEDIA_ERR_SRC_NOT_SUPPORTED.
1741                    this.error.set(Some(&*MediaError::new(
1742                        cx,
1743                        &this.owner_window(),
1744                        MEDIA_ERR_SRC_NOT_SUPPORTED)));
1745
1746                    // Step 2. Forget the media element's media-resource-specific tracks.
1747                    this.AudioTracks(cx).clear();
1748                    this.VideoTracks(cx).clear();
1749
1750                    // Step 3. Set the element's networkState attribute to the NETWORK_NO_SOURCE
1751                    // value.
1752                    this.network_state.set(NetworkState::NoSource);
1753
1754                    // Step 4. Set the element's show poster flag to true.
1755                    this.show_poster.set(true);
1756
1757                    // Step 5. Fire an event named error at the media element.
1758                    this.upcast::<EventTarget>().fire_event(cx, atom!("error"));
1759
1760                    if let Some(ref player) = *this.player.borrow()
1761                        && let Err(error) = player.lock().unwrap().stop() {
1762                            error!("Could not stop player: {error:?}");
1763                        }
1764
1765                    // Step 6. Reject pending play promises with promises and a "NotSupportedError"
1766                    // DOMException.
1767                    // Done after running this closure in `fulfill_in_flight_play_promises`.
1768                });
1769
1770                // Step 7. Set the element's delaying-the-load-event flag to false. This stops
1771                // delaying the load event.
1772                this.delay_load_event(cx, false);
1773
1774                this.update_keep_alive();
1775            }));
1776    }
1777
1778    fn in_error_state(&self) -> bool {
1779        self.error.get().is_some()
1780    }
1781
1782    /// <https://html.spec.whatwg.org/multipage/#potentially-playing>
1783    fn is_potentially_playing(&self) -> bool {
1784        // > A media element is said to be potentially playing when
1785        // > its paused attribute is false, the element has not ended playback,
1786        // > playback has not stopped due to errors,
1787        // > and the element is not a blocked media element.
1788        !self.paused.get() &&
1789            !self.ended_playback(LoopCondition::Included) &&
1790            self.error.get().is_none() &&
1791            !self.is_blocked_media_element()
1792    }
1793
1794    /// <https://html.spec.whatwg.org/multipage/#blocked-media-element>
1795    fn is_blocked_media_element(&self) -> bool {
1796        self.ready_state.get() <= ReadyState::HaveCurrentData ||
1797            self.is_paused_for_user_interaction() ||
1798            self.is_paused_for_in_band_content()
1799    }
1800
1801    /// <https://html.spec.whatwg.org/multipage/#paused-for-user-interaction>
1802    fn is_paused_for_user_interaction(&self) -> bool {
1803        // FIXME: we will likely be able to fill this placeholder once (if) we
1804        //        implement the MediaSession API.
1805        false
1806    }
1807
1808    /// <https://html.spec.whatwg.org/multipage/#paused-for-in-band-content>
1809    fn is_paused_for_in_band_content(&self) -> bool {
1810        // FIXME: we will likely be able to fill this placeholder once (if) we
1811        //        implement https://github.com/servo/servo/issues/22314
1812        false
1813    }
1814
1815    /// <https://html.spec.whatwg.org/multipage/#media-element-load-algorithm>
1816    fn media_element_load_algorithm(&self, cx: &mut JSContext) {
1817        // Reset the flag that signals whether loadeddata was ever fired for
1818        // this invokation of the load algorithm.
1819        self.fired_loadeddata_event.set(false);
1820
1821        // TODO Step 1. Set this element's is currently stalled to false.
1822
1823        // Step 2. Abort any already-running instance of the resource selection algorithm for this
1824        // element.
1825        self.generation_id.set(self.generation_id.get() + 1);
1826
1827        self.load_state.set(LoadState::NotLoaded);
1828        *self.source_children_pointer.borrow_mut() = None;
1829        self.current_source_child.set(None);
1830
1831        // Step 3. Let pending tasks be a list of all tasks from the media element's media element
1832        // event task source in one of the task queues.
1833
1834        // Step 4. For each task in pending tasks that would resolve pending play promises or reject
1835        // pending play promises, immediately resolve or reject those promises in the order the
1836        // corresponding tasks were queued.
1837        while !self.in_flight_play_promises_queue.borrow().is_empty() {
1838            self.fulfill_in_flight_play_promises(cx, |_| ());
1839        }
1840
1841        // Step 5. Remove each task in pending tasks from its task queue.
1842        // Note that each media element's pending event and callback is scheduled with associated
1843        // generation id and will be aborted eventually (from Step 2).
1844
1845        let network_state = self.network_state.get();
1846
1847        // Step 6. If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE,
1848        // queue a media element task given the media element to fire an event named abort at the
1849        // media element.
1850        if network_state == NetworkState::Loading || network_state == NetworkState::Idle {
1851            self.queue_media_element_task_to_fire_event(atom!("abort"));
1852        }
1853
1854        // Reset the media player for any previously playing media resource (see Step 11).
1855        self.reset_media_player(cx.no_gc());
1856
1857        // Step 7. If the media element's networkState is not set to NETWORK_EMPTY, then:
1858        if network_state != NetworkState::Empty {
1859            // Step 7.1. Queue a media element task given the media element to fire an event named
1860            // emptied at the media element.
1861            self.queue_media_element_task_to_fire_event(atom!("emptied"));
1862
1863            // Step 7.2. If a fetching process is in progress for the media element, the user agent
1864            // should stop it.
1865            if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() {
1866                current_fetch_context.cancel(CancelReason::Abort);
1867            }
1868
1869            // TODO Step 7.3. If the media element's assigned media provider object is a MediaSource
1870            // object, then detach it.
1871
1872            // Step 7.4. Forget the media element's media-resource-specific tracks.
1873            self.AudioTracks(cx).clear();
1874            self.VideoTracks(cx).clear();
1875
1876            // Step 7.5. If readyState is not set to HAVE_NOTHING, then set it to that state.
1877            if self.ready_state.get() != ReadyState::HaveNothing {
1878                self.change_ready_state(cx, ReadyState::HaveNothing);
1879            }
1880
1881            // Step 7.6. If the paused attribute is false, then:
1882            if !self.Paused() {
1883                // Step 7.6.1. Set the paused attribute to true.
1884                self.paused.set(true);
1885
1886                // Step 7.6.2. Take pending play promises and reject pending play promises with the
1887                // result and an "AbortError" DOMException.
1888                self.take_pending_play_promises(Err(Error::Abort(Some(
1889                    "Playback interrupted by new resource load".into(),
1890                ))));
1891                self.fulfill_in_flight_play_promises(cx, |_| ());
1892            }
1893
1894            // Step 7.7. If seeking is true, set it to false.
1895            self.seeking.set(false);
1896
1897            self.current_seek_position.set(f64::NAN);
1898
1899            // Step 7.8. Set the current playback position to 0.
1900            // Set the official playback position to 0.
1901            // If this changed the official playback position, then queue a media element task given
1902            // the media element to fire an event named timeupdate at the media element.
1903            self.current_playback_position.set(0.);
1904            if self.official_playback_position.get() != 0. {
1905                self.queue_media_element_task_to_fire_event(atom!("timeupdate"));
1906            }
1907            self.official_playback_position.set(0.);
1908
1909            // TODO Step 7.9. Set the timeline offset to Not-a-Number (NaN).
1910
1911            // Step 7.10. Update the duration attribute to Not-a-Number (NaN).
1912            self.duration.set(f64::NAN);
1913        }
1914
1915        // Step 8. Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
1916        self.playback_rate.set(self.default_playback_rate.get());
1917
1918        // Step 9. Set the error attribute to null and the can autoplay flag to true.
1919        self.error.set(None);
1920        self.autoplaying.set(true);
1921
1922        // Step 10. Invoke the media element's resource selection algorithm.
1923        self.invoke_resource_selection_algorithm(cx);
1924
1925        // Step 11. Note: Playback of any previously playing media resource for this element stops.
1926        self.update_keep_alive();
1927    }
1928
1929    /// Queue a media element task given the media element to fire an event at the media element.
1930    /// <https://html.spec.whatwg.org/multipage/#queue-a-media-element-task>
1931    fn queue_media_element_task_to_fire_event(&self, name: Atom) {
1932        let this = Trusted::new(self);
1933        let generation_id = self.generation_id.get();
1934
1935        self.owner_global()
1936            .task_manager()
1937            .media_element_task_source()
1938            .queue(task!(queue_event: move |cx| {
1939                let this = this.root();
1940                if generation_id != this.generation_id.get() {
1941                    return;
1942                }
1943
1944                this.upcast::<EventTarget>().fire_event(cx, name);
1945            }));
1946    }
1947
1948    /// Appends a promise to the list of pending play promises.
1949    fn push_pending_play_promise(&self, promise: &RootedPromise) {
1950        self.pending_play_promises
1951            .borrow_mut()
1952            .push(promise.to_traced());
1953    }
1954
1955    /// Takes the pending play promises.
1956    ///
1957    /// The result with which these promises will be fulfilled is passed here
1958    /// and this method returns nothing because we actually just move the
1959    /// current list of pending play promises to the
1960    /// `in_flight_play_promises_queue` field.
1961    ///
1962    /// Each call to this method must be followed by a call to
1963    /// `fulfill_in_flight_play_promises`, to actually fulfill the promises
1964    /// which were taken and moved to the in-flight queue.
1965    fn take_pending_play_promises(&self, result: ErrorResult) {
1966        self.in_flight_play_promises_queue.borrow_mut().push_back((
1967            std::mem::take(&mut *self.pending_play_promises.borrow_mut()).into(),
1968            result,
1969        ));
1970    }
1971
1972    /// Fulfills the next in-flight play promises queue after running a closure.
1973    ///
1974    /// See the comment on `take_pending_play_promises` for why this method
1975    /// does not take a list of promises to fulfill. Callers cannot just pop
1976    /// the front list off of `in_flight_play_promises_queue` and later fulfill
1977    /// the promises because that would mean putting
1978    /// `#[cfg_attr(crown, expect(crown::unrooted_must_root))]` on even more functions, potentially
1979    /// hiding actual safety bugs.
1980    fn fulfill_in_flight_play_promises<F>(&self, cx: &mut JSContext, f: F)
1981    where
1982        F: FnOnce(&mut JSContext),
1983    {
1984        let (promises, result) = self
1985            .in_flight_play_promises_queue
1986            .borrow_mut()
1987            .pop_front()
1988            .map(|(promises, result)| {
1989                let promises: Vec<RootedPromise> =
1990                    promises.iter().map(|promise| promise.root(cx)).collect();
1991                (promises, result)
1992            })
1993            .expect("there should be at least one list of in flight play promises");
1994        f(cx);
1995        for promise in &promises {
1996            match result {
1997                Ok(ref value) => promise.resolve_native(cx, value),
1998                Err(ref error) => promise.reject_error(cx, error.clone()),
1999            }
2000        }
2001    }
2002
2003    pub(crate) fn handle_source_child_insertion(
2004        &self,
2005        cx: &mut JSContext,
2006        source: &HTMLSourceElement,
2007    ) {
2008        // <https://html.spec.whatwg.org/multipage/#the-source-element:html-element-insertion-steps>
2009        // Step 2. If parent is a media element that has no src attribute and whose networkState has
2010        // the value NETWORK_EMPTY, then invoke that media element's resource selection algorithm.
2011        if self.upcast::<Element>().has_attribute(&local_name!("src")) {
2012            return;
2013        }
2014
2015        if self.network_state.get() == NetworkState::Empty {
2016            self.invoke_resource_selection_algorithm(cx);
2017            return;
2018        }
2019
2020        // <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
2021        // Step 9.children.22. Wait until the node after pointer is a node other than the end of the
2022        // list. (This step might wait forever.)
2023        if self.load_state.get() != LoadState::WaitingForSource {
2024            return;
2025        }
2026
2027        self.load_state.set(LoadState::LoadingFromSourceChild);
2028
2029        *self.source_children_pointer.borrow_mut() = Some(SourceChildrenPointer::new(source, true));
2030
2031        // Step 9.children.23. Await a stable state.
2032        let task = MediaElementMicrotask::SelectNextSourceChildAfterWait {
2033            elem: Dom::from_ref(self),
2034            generation_id: self.generation_id.get(),
2035        };
2036
2037        ScriptThread::await_stable_state(cx, Box::new(task));
2038    }
2039
2040    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-algorithm>
2041    fn select_next_source_child_after_wait(&self, cx: &mut JSContext) {
2042        // Step 9.children.24. Set the element's delaying-the-load-event flag back to true (this
2043        // delays the load event again, in case it hasn't been fired yet).
2044        self.delay_load_event(cx, true);
2045
2046        // Step 9.children.25. Set the networkState back to NETWORK_LOADING.
2047        self.network_state.set(NetworkState::Loading);
2048
2049        // Step 9.children.26. Jump back to the find next candidate step above.
2050        self.select_next_source_child(cx);
2051    }
2052
2053    /// <https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list>
2054    /// => "If the media data cannot be fetched at all, due to network errors..."
2055    /// => "If the media data can be fetched but is found by inspection to be in an unsupported
2056    /// format, or can otherwise not be rendered at all"
2057    fn media_data_processing_failure_steps(&self, cx: &JSContext) {
2058        // Step 1. The user agent should cancel the fetching process.
2059        if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() {
2060            current_fetch_context.cancel(CancelReason::Error);
2061        }
2062
2063        // Step 2. Abort this subalgorithm, returning to the resource selection algorithm.
2064        self.resource_selection_algorithm_failure_steps(cx);
2065    }
2066
2067    /// <https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list>
2068    /// => "If the connection is interrupted after some media data has been received..."
2069    /// => "If the media data is corrupted"
2070    fn media_data_processing_fatal_steps(&self, cx: &mut JSContext, error: u16) {
2071        *self.source_children_pointer.borrow_mut() = None;
2072        self.current_source_child.set(None);
2073
2074        // Step 1. The user agent should cancel the fetching process.
2075        if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() {
2076            current_fetch_context.cancel(CancelReason::Error);
2077        }
2078
2079        // Step 2. Set the error attribute to the result of creating a MediaError with
2080        // MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
2081        self.error
2082            .set(Some(&*MediaError::new(cx, &self.owner_window(), error)));
2083
2084        // Step 3. Set the element's networkState attribute to the NETWORK_IDLE value.
2085        self.network_state.set(NetworkState::Idle);
2086
2087        // Step 4. Set the element's delaying-the-load-event flag to false. This stops delaying
2088        // the load event.
2089        self.delay_load_event(cx, false);
2090
2091        // Step 5. Fire an event named error at the media element.
2092        self.upcast::<EventTarget>().fire_event(cx, atom!("error"));
2093
2094        // Step 6. Abort the overall resource selection algorithm.
2095        self.update_keep_alive();
2096    }
2097
2098    /// <https://html.spec.whatwg.org/multipage/#dom-media-seek>
2099    fn seek(&self, time: f64, _approximate_for_speed: bool) {
2100        // Step 1. Set the media element's show poster flag to false.
2101        self.show_poster.set(false);
2102
2103        // Step 2. If the media element's readyState is HAVE_NOTHING, return.
2104        if self.ready_state.get() == ReadyState::HaveNothing {
2105            return;
2106        }
2107
2108        // Step 3. If the element's seeking IDL attribute is true, then another instance of this
2109        // algorithm is already running. Abort that other instance of the algorithm without waiting
2110        // for the step that it is running to complete.
2111        self.current_seek_position.set(f64::NAN);
2112
2113        // Step 4. Set the seeking IDL attribute to true.
2114        self.seeking.set(true);
2115
2116        // Step 5. If the seek was in response to a DOM method call or setting of an IDL attribute,
2117        // then continue the script. The remainder of these steps must be run in parallel.
2118
2119        // Step 6. If the new playback position is later than the end of the media resource, then
2120        // let it be the end of the media resource instead.
2121        let time = f64::min(time, self.Duration());
2122
2123        // Step 7. If the new playback position is less than the earliest possible position, let it
2124        // be that position instead.
2125        let time = f64::max(time, self.earliest_possible_position());
2126
2127        // Step 8. If the (possibly now changed) new playback position is not in one of the ranges
2128        // given in the seekable attribute, then let it be the position in one of the ranges given
2129        // in the seekable attribute that is the nearest to the new playback position. If there are
2130        // no ranges given in the seekable attribute, then set the seeking IDL attribute to false
2131        // and return.
2132        let seekable = self.seekable();
2133
2134        if seekable.is_empty() {
2135            self.seeking.set(false);
2136            return;
2137        }
2138
2139        let mut nearest_seekable_position = 0.0;
2140        let mut in_seekable_range = false;
2141        let mut nearest_seekable_distance = f64::MAX;
2142        for i in 0..seekable.len() {
2143            let start = seekable.start(i).unwrap().abs();
2144            let end = seekable.end(i).unwrap().abs();
2145            if time >= start && time <= end {
2146                nearest_seekable_position = time;
2147                in_seekable_range = true;
2148                break;
2149            } else if time < start {
2150                let distance = start - time;
2151                if distance < nearest_seekable_distance {
2152                    nearest_seekable_distance = distance;
2153                    nearest_seekable_position = start;
2154                }
2155            } else {
2156                let distance = time - end;
2157                if distance < nearest_seekable_distance {
2158                    nearest_seekable_distance = distance;
2159                    nearest_seekable_position = end;
2160                }
2161            }
2162        }
2163        let time = if in_seekable_range {
2164            time
2165        } else {
2166            nearest_seekable_position
2167        };
2168
2169        // Step 9. If the approximate-for-speed flag is set, adjust the new playback position to a
2170        // value that will allow for playback to resume promptly. If new playback position before
2171        // this step is before current playback position, then the adjusted new playback position
2172        // must also be before the current playback position. Similarly, if the new playback
2173        // position before this step is after current playback position, then the adjusted new
2174        // playback position must also be after the current playback position.
2175        // TODO: Note that servo-media with gstreamer does not support inaccurate seeking for now.
2176
2177        // Step 10. Queue a media element task given the media element to fire an event named
2178        // seeking at the element.
2179        self.queue_media_element_task_to_fire_event(atom!("seeking"));
2180
2181        // Step 11. Set the current playback position to the new playback position.
2182        self.current_playback_position.set(time);
2183
2184        if let Some(ref player) = *self.player.borrow() &&
2185            let Err(error) = player.lock().unwrap().seek(time)
2186        {
2187            error!("Could not seek player: {error:?}");
2188        }
2189
2190        self.current_seek_position.set(time);
2191
2192        // Step 12. Wait until the user agent has established whether or not the media data for the
2193        // new playback position is available, and, if it is, until it has decoded enough data to
2194        // play back that position.
2195        // The rest of the steps are handled when the media engine signals a ready state change or
2196        // otherwise satisfies seek completion and signals a position change.
2197    }
2198
2199    /// <https://html.spec.whatwg.org/multipage/#dom-media-seek>
2200    fn seek_end(&self, cx: &mut JSContext) {
2201        // Any time the user agent provides a stable state, the official playback position must be
2202        // set to the current playback position.
2203        self.official_playback_position
2204            .set(self.current_playback_position.get());
2205
2206        // Step 14. Set the seeking IDL attribute to false.
2207        self.seeking.set(false);
2208
2209        self.current_seek_position.set(f64::NAN);
2210
2211        // Step 15. Run the time marches on steps.
2212        self.time_marches_on(cx, PlaybackPositionWasMoved::ExplicitMove);
2213
2214        // Step 16. Queue a media element task given the media element to fire an event named
2215        // timeupdate at the element.
2216        self.queue_media_element_task_to_fire_event(atom!("timeupdate"));
2217
2218        // Step 17. Queue a media element task given the media element to fire an event named seeked
2219        // at the element.
2220        self.queue_media_element_task_to_fire_event(atom!("seeked"));
2221    }
2222
2223    /// <https://html.spec.whatwg.org/multipage/#poster-frame>
2224    pub(crate) fn set_poster_frame(&self, no_gc: &NoGC, image: Option<Arc<RasterImage>>) {
2225        if pref!(media_testing_enabled) && image.is_some() {
2226            self.queue_media_element_task_to_fire_event(atom!("postershown"));
2227        }
2228
2229        self.video_renderer.lock().unwrap().set_poster_frame(image);
2230
2231        self.upcast::<Node>().dirty(no_gc, NodeDamage::Other);
2232    }
2233
2234    fn player_id(&self) -> Option<usize> {
2235        self.player
2236            .borrow()
2237            .as_ref()
2238            .map(|player| player.lock().unwrap().get_id())
2239    }
2240
2241    fn create_media_player(&self, resource: &Resource) -> Result<(), ()> {
2242        let stream_type = match *resource {
2243            Resource::Object => {
2244                if let Some(ref src_object) = *self.src_object.borrow() {
2245                    match src_object {
2246                        SrcObject::MediaStream(_) => StreamType::Stream,
2247                        _ => StreamType::Seekable,
2248                    }
2249                } else {
2250                    return Err(());
2251                }
2252            },
2253            _ => StreamType::Seekable,
2254        };
2255
2256        let window = self.owner_window();
2257
2258        let video_renderer: Option<Arc<Mutex<dyn VideoFrameRenderer>>> = match self.media_type_id()
2259        {
2260            HTMLMediaElementTypeId::HTMLAudioElement => None,
2261            HTMLMediaElementTypeId::HTMLVideoElement => Some(self.video_renderer.clone()),
2262        };
2263
2264        let audio_renderer = self.audio_renderer.borrow().as_ref().cloned();
2265
2266        let pipeline_id = window.pipeline_id();
2267        let client_context_id =
2268            ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get());
2269
2270        // This player id will be set later when we have a player.
2271        // The callback will be the only valid user after this function.
2272        let shared_player_id = Arc::new(OnceLock::new());
2273
2274        let event_handler = Arc::new(Mutex::new(HTMLMediaElementEventHandler::new(self)));
2275        let weak_event_handler = Arc::downgrade(&event_handler);
2276        *self.event_handler.borrow_mut() = Some(event_handler);
2277
2278        let task_source = self
2279            .owner_global()
2280            .task_manager()
2281            .media_element_task_source()
2282            .to_sendable();
2283
2284        let shared_player_id_clone = shared_player_id.clone();
2285        let action_callback = generic_channel::GenericCallback::new(move |message| {
2286            let event = message.unwrap();
2287            let weak_event_handler = weak_event_handler.clone();
2288
2289            let shared_player_id_clone = shared_player_id_clone.clone();
2290            task_source.queue(task!(handle_player_event: move |cx| {
2291                trace!("HTMLMediaElement event: {event:?}");
2292
2293                let Some(event_handler) = weak_event_handler.upgrade() else {
2294                    return;
2295                };
2296
2297                if let Some(shared_player_id) = shared_player_id_clone.get() {
2298                    event_handler.lock().unwrap().handle_player_event(cx, *shared_player_id, event);
2299                } else {
2300                    error!("Player Action without ID being assigned yet.");
2301                }
2302            }));
2303        })
2304        .unwrap();
2305        let player = ServoMedia::get().create_player(
2306            &client_context_id,
2307            stream_type,
2308            action_callback,
2309            video_renderer,
2310            audio_renderer,
2311            Box::new(window.get_player_context()),
2312        );
2313
2314        let player_id = {
2315            let player_guard = player.lock().unwrap();
2316
2317            // We flag the media to enable download buffering when it's supposed to be big and
2318            // that happens heuristically for videos with preload="auto" and audio that loops.
2319            let is_video = matches!(
2320                self.media_type_id(),
2321                HTMLMediaElementTypeId::HTMLVideoElement
2322            );
2323            let should_enable_download_buffering =
2324                (is_video || self.Loop()) && self.Preload() == "auto";
2325            if let Err(error) =
2326                player_guard.set_download_buffering_enabled(should_enable_download_buffering)
2327            {
2328                warn!("Could not set download buffering: {error:?}");
2329            }
2330
2331            if let Err(error) = player_guard.set_mute(self.is_muted()) {
2332                warn!("Could not set mute state: {error:?}");
2333            }
2334
2335            let id = player_guard.get_id();
2336            if shared_player_id.set(id).is_err() {
2337                error!("Error setting player id. Already set?");
2338            }
2339            id
2340        };
2341
2342        *self.player.borrow_mut() = Some(player);
2343
2344        let task_source = self
2345            .owner_global()
2346            .task_manager()
2347            .media_element_task_source()
2348            .to_sendable();
2349        let weak_video_renderer = Arc::downgrade(&self.video_renderer);
2350
2351        self.video_renderer
2352            .lock()
2353            .unwrap()
2354            .setup(player_id, task_source, weak_video_renderer);
2355
2356        Ok(())
2357    }
2358
2359    fn reset_media_player(&self, no_gc: &NoGC) {
2360        if self.player.borrow().is_none() {
2361            return;
2362        }
2363
2364        if let Some(ref player) = *self.player.borrow() &&
2365            let Err(error) = player.lock().unwrap().stop()
2366        {
2367            error!("Could not stop player: {error:?}");
2368        }
2369
2370        *self.player.borrow_mut() = None;
2371        self.video_renderer.lock().unwrap().reset();
2372        *self.event_handler.borrow_mut() = None;
2373
2374        if let Some(video_element) = self.downcast::<HTMLVideoElement>() {
2375            video_element.set_natural_dimensions(no_gc, None, None);
2376        }
2377    }
2378
2379    pub(crate) fn set_audio_track(&self, idx: usize, enabled: bool) {
2380        if let Some(ref player) = *self.player.borrow() &&
2381            let Err(error) = player.lock().unwrap().set_audio_track(idx as i32, enabled)
2382        {
2383            warn!("Could not set audio track {error:?}");
2384        }
2385    }
2386
2387    pub(crate) fn set_video_track(&self, idx: usize, enabled: bool) {
2388        if let Some(ref player) = *self.player.borrow() &&
2389            let Err(error) = player.lock().unwrap().set_video_track(idx as i32, enabled)
2390        {
2391            warn!("Could not set video track: {error:?}");
2392        }
2393    }
2394
2395    /// <https://html.spec.whatwg.org/multipage/#direction-of-playback>
2396    fn direction_of_playback(&self) -> PlaybackDirection {
2397        // If the element's playbackRate is positive or zero, then the direction of playback is
2398        // forwards. Otherwise, it is backwards.
2399        if self.playback_rate.get() >= 0. {
2400            PlaybackDirection::Forwards
2401        } else {
2402            PlaybackDirection::Backwards
2403        }
2404    }
2405
2406    /// <https://html.spec.whatwg.org/multipage/#ended-playback>
2407    fn ended_playback(&self, loop_condition: LoopCondition) -> bool {
2408        // A media element is said to have ended playback when:
2409
2410        // The element's readyState attribute is HAVE_METADATA or greater, and
2411        if self.ready_state.get() < ReadyState::HaveMetadata {
2412            return false;
2413        }
2414
2415        let playback_position = self.current_playback_position.get();
2416
2417        match self.direction_of_playback() {
2418            // Either: The current playback position is the end of the media resource, and the
2419            // direction of playback is forwards, and the media element does not have a loop
2420            // attribute specified.
2421            PlaybackDirection::Forwards => {
2422                playback_position >= self.Duration() &&
2423                    (loop_condition == LoopCondition::Ignored || !self.Loop())
2424            },
2425            // Or: The current playback position is the earliest possible position, and the
2426            // direction of playback is backwards.
2427            PlaybackDirection::Backwards => playback_position <= self.earliest_possible_position(),
2428        }
2429    }
2430
2431    /// <https://html.spec.whatwg.org/multipage/#reaches-the-end>
2432    fn end_of_playback_in_forwards_direction(&self, cx: &mut JSContext) {
2433        // When the current playback position reaches the end of the media resource when the
2434        // direction of playback is forwards, then the user agent must follow these steps:
2435
2436        // Step 1. If the media element has a loop attribute specified, then seek to the earliest
2437        // posible position of the media resource and return.
2438        if self.Loop() {
2439            self.seek(
2440                self.earliest_possible_position(),
2441                /* approximate_for_speed */ false,
2442            );
2443            return;
2444        }
2445
2446        // Step 2. As defined above, the ended IDL attribute starts returning true once the event
2447        // loop returns to step 1.
2448
2449        // Step 3. Queue a media element task given the media element and the following steps:
2450        let this = Trusted::new(self);
2451        let generation_id = self.generation_id.get();
2452
2453        self.owner_global()
2454            .task_manager()
2455            .media_element_task_source()
2456            .queue(task!(reaches_the_end_steps: move |cx| {
2457                let this = this.root();
2458                if generation_id != this.generation_id.get() {
2459                    return;
2460                }
2461
2462                // Step 3.1. Fire an event named timeupdate at the media element.
2463                this.upcast::<EventTarget>().fire_event(cx, atom!("timeupdate"));
2464
2465                // Step 3.2. If the media element has ended playback, the direction of playback is
2466                // forwards, and paused is false, then:
2467                if this.ended_playback(LoopCondition::Included) &&
2468                    this.direction_of_playback() == PlaybackDirection::Forwards &&
2469                    !this.Paused() {
2470                    // Step 3.2.1. Set the paused attribute to true.
2471                    this.paused.set(true);
2472
2473                    // Step 3.2.2. Fire an event named pause at the media element.
2474                    this.upcast::<EventTarget>().fire_event(cx, atom!("pause"));
2475
2476                    // Step 3.2.3. Take pending play promises and reject pending play promises with
2477                    // the result and an "AbortError" DOMException.
2478                    this.take_pending_play_promises(Err(Error::Abort(Some("Media playback finished".into()))));
2479                    this.fulfill_in_flight_play_promises(cx, |_| ());
2480                }
2481
2482                // Step 3.3. Fire an event named ended at the media element.
2483                this.upcast::<EventTarget>().fire_event(cx, atom!("ended"));
2484
2485                this.update_keep_alive();
2486            }));
2487
2488        // <https://html.spec.whatwg.org/multipage/#dom-media-have_current_data>
2489        self.change_ready_state(cx, ReadyState::HaveCurrentData);
2490    }
2491
2492    /// <https://html.spec.whatwg.org/multipage/#reaches-the-end>
2493    fn end_of_playback_in_backwards_direction(&self) {
2494        // When the current playback position reaches the earliest possible position of the media
2495        // resource when the direction of playback is backwards, then the user agent must only queue
2496        // a media element task given the media element to fire an event named timeupdate at the
2497        // element.
2498        if self.current_playback_position.get() <= self.earliest_possible_position() {
2499            self.queue_media_element_task_to_fire_event(atom!("timeupdate"));
2500            self.update_keep_alive();
2501        }
2502    }
2503
2504    fn playback_end(&self, cx: &mut JSContext) {
2505        // Abort the following steps of the end of playback if seeking is in progress.
2506        if self.seeking.get() {
2507            return;
2508        }
2509
2510        match self.direction_of_playback() {
2511            PlaybackDirection::Forwards => self.end_of_playback_in_forwards_direction(cx),
2512            PlaybackDirection::Backwards => self.end_of_playback_in_backwards_direction(),
2513        }
2514    }
2515
2516    fn playback_error(&self, cx: &mut JSContext, error: &str) {
2517        error!("Player error: {:?}", error);
2518
2519        // If we have already flagged an error condition while processing
2520        // the network response, we should silently skip any observable
2521        // errors originating while decoding the erroneous response.
2522        if self.in_error_state() {
2523            return;
2524        }
2525
2526        // <https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list>
2527        if self.ready_state.get() == ReadyState::HaveNothing {
2528            // => "If the media data can be fetched but is found by inspection to be in an
2529            // unsupported format, or can otherwise not be rendered at all"
2530            self.media_data_processing_failure_steps(cx);
2531        } else {
2532            // => "If the media data is corrupted"
2533            self.media_data_processing_fatal_steps(cx, MEDIA_ERR_DECODE);
2534        }
2535    }
2536
2537    fn playback_metadata_updated(
2538        &self,
2539        cx: &mut JSContext,
2540        metadata: &servo_media::player::metadata::Metadata,
2541    ) {
2542        // The following steps should be run once on the initial `metadata` signal from the media
2543        // engine.
2544        if self.ready_state.get() != ReadyState::HaveNothing {
2545            return;
2546        }
2547
2548        // https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list
2549        // => "If the media resource is found to have an audio track"
2550        for (i, _track) in metadata.audio_tracks.iter().enumerate() {
2551            let audio_track_list = self.AudioTracks(cx);
2552
2553            // Step 1. Create an AudioTrack object to represent the audio track.
2554            let kind = match i {
2555                0 => DOMString::from_static("main"),
2556                _ => DOMString::new(),
2557            };
2558
2559            let audio_track = AudioTrack::new(
2560                cx,
2561                self.global().as_window(),
2562                DOMString::new(),
2563                kind,
2564                DOMString::new(),
2565                DOMString::new(),
2566                Some(&*audio_track_list),
2567            );
2568
2569            // Steps 2. Update the media element's audioTracks attribute's AudioTrackList object
2570            // with the new AudioTrack object.
2571            audio_track_list.add(&audio_track);
2572
2573            // Step 3. Let enable be unknown.
2574            // Step 4. If either the media resource or the URL of the current media resource
2575            // indicate a particular set of audio tracks to enable, or if the user agent has
2576            // information that would facilitate the selection of specific audio tracks to
2577            // improve the user's experience, then: if this audio track is one of the ones to
2578            // enable, then set enable to true, otherwise, set enable to false.
2579            if let Some(servo_url) = self.resource_url.borrow().as_ref() {
2580                let fragment = MediaFragmentParser::from(servo_url);
2581                if let Some(id) = fragment.id() &&
2582                    audio_track.id() == id
2583                {
2584                    audio_track_list.set_enabled(audio_track_list.len() - 1, true);
2585                }
2586
2587                if fragment.tracks().contains(&audio_track.kind().into()) {
2588                    audio_track_list.set_enabled(audio_track_list.len() - 1, true);
2589                }
2590            }
2591
2592            // Step 5. If enable is still unknown, then, if the media element does not yet have an
2593            // enabled audio track, then set enable to true, otherwise, set enable to false.
2594            // Step 6. If enable is true, then enable this audio track, otherwise, do not enable
2595            // this audio track.
2596            if audio_track_list.enabled_index().is_none() {
2597                audio_track_list.set_enabled(audio_track_list.len() - 1, true);
2598            }
2599
2600            // Step 7. Fire an event named addtrack at this AudioTrackList object, using TrackEvent,
2601            // with the track attribute initialized to the new AudioTrack object.
2602            let event = TrackEvent::new(
2603                cx,
2604                self.global().as_window(),
2605                atom!("addtrack"),
2606                false,
2607                false,
2608                &Some(VideoTrackOrAudioTrackOrTextTrack::AudioTrack(audio_track)),
2609            );
2610
2611            event
2612                .upcast::<Event>()
2613                .fire(cx, audio_track_list.upcast::<EventTarget>());
2614        }
2615
2616        // => "If the media resource is found to have a video track"
2617        for (i, _track) in metadata.video_tracks.iter().enumerate() {
2618            let video_track_list = self.VideoTracks(cx);
2619
2620            // Step 1. Create a VideoTrack object to represent the video track.
2621            let kind = match i {
2622                0 => DOMString::from_static("main"),
2623                _ => DOMString::new(),
2624            };
2625
2626            let video_track = VideoTrack::new(
2627                cx,
2628                self.global().as_window(),
2629                DOMString::new(),
2630                kind,
2631                DOMString::new(),
2632                DOMString::new(),
2633                Some(&*video_track_list),
2634            );
2635
2636            // Steps 2. Update the media element's videoTracks attribute's VideoTrackList object
2637            // with the new VideoTrack object.
2638            video_track_list.add(&video_track);
2639
2640            // Step 3. Let enable be unknown.
2641            // Step 4. If either the media resource or the URL of the current media resource
2642            // indicate a particular set of video tracks to enable, or if the user agent has
2643            // information that would facilitate the selection of specific video tracks to
2644            // improve the user's experience, then: if this video track is the first such video
2645            // track, then set enable to true, otherwise, set enable to false.
2646            if let Some(track) = video_track_list.item(0) &&
2647                let Some(servo_url) = self.resource_url.borrow().as_ref()
2648            {
2649                let fragment = MediaFragmentParser::from(servo_url);
2650                if let Some(id) = fragment.id() {
2651                    if track.id() == id {
2652                        video_track_list.set_selected(0, true);
2653                    }
2654                } else if fragment.tracks().contains(&track.kind().into()) {
2655                    video_track_list.set_selected(0, true);
2656                }
2657            }
2658
2659            // Step 5. If enable is still unknown, then, if the media element does not yet have a
2660            // selected video track, then set enable to true, otherwise, set enable to false.
2661            // Step 6. If enable is true, then select this track and unselect any previously
2662            // selected video tracks, otherwise, do not select this video track. If other tracks are
2663            // unselected, then a change event will be fired.
2664            if video_track_list.selected_index().is_none() {
2665                video_track_list.set_selected(video_track_list.len() - 1, true);
2666            }
2667
2668            // Step 7. Fire an event named addtrack at this VideoTrackList object, using TrackEvent,
2669            // with the track attribute initialized to the new VideoTrack object.
2670            let event = TrackEvent::new(
2671                cx,
2672                self.global().as_window(),
2673                atom!("addtrack"),
2674                false,
2675                false,
2676                &Some(VideoTrackOrAudioTrackOrTextTrack::VideoTrack(video_track)),
2677            );
2678
2679            event
2680                .upcast::<Event>()
2681                .fire(cx, video_track_list.upcast::<EventTarget>());
2682        }
2683
2684        // => "Once enough of the media data has been fetched to determine the duration..."
2685
2686        // TODO Step 1. Establish the media timeline for the purposes of the current playback
2687        // position and the earliest possible position, based on the media data.
2688
2689        // TODO Step 2. Update the timeline offset to the date and time that corresponds to the zero
2690        // time in the media timeline established in the previous step, if any. If no explicit time
2691        // and date is given by the media resource, the timeline offset must be set to Not-a-Number
2692        // (NaN).
2693
2694        // Step 3. Set the current playback position and the official playback position to the
2695        // earliest possible position.
2696        let earliest_possible_position = self.earliest_possible_position();
2697        self.current_playback_position
2698            .set(earliest_possible_position);
2699        self.official_playback_position
2700            .set(earliest_possible_position);
2701
2702        // Step 4. Update the duration attribute with the time of the last frame of the resource, if
2703        // known, on the media timeline established above. If it is not known (e.g. a stream that is
2704        // in principle infinite), update the duration attribute to the value positive Infinity.
2705        // Note: The user agent will queue a media element task given the media element to fire an
2706        // event named durationchange at the element at this point.
2707        self.duration.set(
2708            metadata
2709                .duration
2710                .map_or(f64::INFINITY, |duration| duration.as_secs_f64()),
2711        );
2712        self.queue_media_element_task_to_fire_event(atom!("durationchange"));
2713
2714        // Step 5. For video elements, set the videoWidth and videoHeight attributes, and queue a
2715        // media element task given the media element to fire an event named resize at the media
2716        // element.
2717        if let Some(video_element) = self.downcast::<HTMLVideoElement>() {
2718            video_element.set_natural_dimensions(
2719                cx.no_gc(),
2720                Some(metadata.width),
2721                Some(metadata.height),
2722            );
2723            self.queue_media_element_task_to_fire_event(atom!("resize"));
2724        }
2725
2726        // Step 6. Set the readyState attribute to HAVE_METADATA.
2727        self.change_ready_state(cx, ReadyState::HaveMetadata);
2728
2729        // Step 7. Let jumped be false.
2730        let mut jumped = false;
2731
2732        // Step 8. If the media element's default playback start position is greater than zero, then
2733        // seek to that time, and let jumped be true.
2734        if self.default_playback_start_position.get() > 0. {
2735            self.seek(
2736                self.default_playback_start_position.get(),
2737                /* approximate_for_speed */ false,
2738            );
2739            jumped = true;
2740        }
2741
2742        // Step 9. Set the media element's default playback start position to zero.
2743        self.default_playback_start_position.set(0.);
2744
2745        // Step 10. Let the initial playback position be 0.
2746        // Step 11. If either the media resource or the URL of the current media resource indicate a
2747        // particular start time, then set the initial playback position to that time and, if jumped
2748        // is still false, seek to that time.
2749        if let Some(servo_url) = self.resource_url.borrow().as_ref() {
2750            let fragment = MediaFragmentParser::from(servo_url);
2751            if let Some(initial_playback_position) = fragment.start() &&
2752                initial_playback_position > 0. &&
2753                initial_playback_position < self.duration.get() &&
2754                !jumped
2755            {
2756                self.seek(
2757                    initial_playback_position,
2758                    /* approximate_for_speed */ false,
2759                )
2760            }
2761        }
2762
2763        // Step 12. If there is no enabled audio track, then enable an audio track. This will cause
2764        // a change event to be fired.
2765        // Step 13. If there is no selected video track, then select a video track. This will cause
2766        // a change event to be fired.
2767        // Note that these steps are already handled by the earlier media track processing.
2768
2769        let global = self.global();
2770        let window = global.as_window();
2771
2772        // Update the media session metadata title with the obtained metadata.
2773        window.Navigator(cx).MediaSession(cx).update_title(
2774            metadata
2775                .title
2776                .clone()
2777                .unwrap_or(window.get_url().into_string()),
2778        );
2779    }
2780
2781    fn playback_duration_changed(&self, duration: Option<Duration>) {
2782        let duration = duration.map_or(f64::INFINITY, |duration| duration.as_secs_f64());
2783
2784        if self.duration.get() == duration {
2785            return;
2786        }
2787
2788        self.duration.set(duration);
2789
2790        // When the length of the media resource changes to a known value (e.g. from being unknown
2791        // to known, or from a previously established length to a new length), the user agent must
2792        // queue a media element task given the media element to fire an event named durationchange
2793        // at the media element.
2794        // <https://html.spec.whatwg.org/multipage/#offsets-into-the-media-resource:media-resource-22>
2795        self.queue_media_element_task_to_fire_event(atom!("durationchange"));
2796
2797        // If the duration is changed such that the current playback position ends up being greater
2798        // than the time of the end of the media resource, then the user agent must also seek to the
2799        // time of the end of the media resource.
2800        if self.current_playback_position.get() > duration {
2801            self.seek(duration, /* approximate_for_speed */ false);
2802        }
2803    }
2804
2805    fn playback_video_frame_updated(&self, no_gc: &NoGC) {
2806        let Some(video_element) = self.downcast::<HTMLVideoElement>() else {
2807            return;
2808        };
2809
2810        // Whenever the natural width or natural height of the video changes (including, for
2811        // example, because the selected video track was changed), if the element's readyState
2812        // attribute is not HAVE_NOTHING, the user agent must queue a media element task given
2813        // the media element to fire an event named resize at the media element.
2814        // <https://html.spec.whatwg.org/multipage/#concept-video-intrinsic-width>
2815
2816        // The event for the prerolled frame from media engine could reached us before the media
2817        // element HAVE_METADATA ready state so subsequent steps will be cancelled.
2818        if self.ready_state.get() == ReadyState::HaveNothing {
2819            return;
2820        }
2821
2822        if let Some(frame) = self.video_renderer.lock().unwrap().current_frame() {
2823            if video_element.set_natural_dimensions(
2824                no_gc,
2825                Some(frame.width as u32),
2826                Some(frame.height as u32),
2827            ) {
2828                self.queue_media_element_task_to_fire_event(atom!("resize"));
2829            } else {
2830                // If the natural dimensions have not been changed, the node should be marked as
2831                // damaged to force a repaint with the new frame contents.
2832                self.upcast::<Node>().dirty(no_gc, NodeDamage::Other);
2833            }
2834        }
2835    }
2836
2837    fn playback_need_data(&self) {
2838        // The media engine signals that the source needs more data. If we already have a valid
2839        // fetch request, we do nothing. Otherwise, if we have no request and the previous request
2840        // was cancelled because we got an EnoughData event, we restart fetching where we left.
2841        if let Some(ref current_fetch_context) = *self.current_fetch_context.borrow() &&
2842            let Some(reason) = current_fetch_context.cancel_reason()
2843        {
2844            // XXX(ferjm) Ideally we should just create a fetch request from
2845            // where we left. But keeping track of the exact next byte that the
2846            // media backend expects is not the easiest task, so I'm simply
2847            // seeking to the current playback position for now which will create
2848            // a new fetch request for the last rendered frame.
2849            if *reason == CancelReason::Backoff {
2850                self.seek(
2851                    self.current_playback_position.get(),
2852                    /* approximate_for_speed */ false,
2853                );
2854            }
2855            return;
2856        }
2857
2858        if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() &&
2859            let Err(e) = {
2860                let mut data_source = current_fetch_context.data_source().borrow_mut();
2861                data_source.set_locked(false);
2862                data_source.process_into_player_from_queue(self.player.borrow().as_ref().unwrap())
2863            }
2864        {
2865            // If we are pushing too much data and we know that we can
2866            // restart the download later from where we left, we cancel
2867            // the current request. Otherwise, we continue the request
2868            // assuming that we may drop some frames.
2869            if e == PlayerError::EnoughData {
2870                current_fetch_context.cancel(CancelReason::Backoff);
2871            }
2872        }
2873    }
2874
2875    fn playback_enough_data(&self) {
2876        // The media engine signals that the source has enough data and asks us to stop pushing bytes
2877        // to avoid excessive buffer queueing, so we cancel the ongoing fetch request if we are able
2878        // to restart it from where we left. Otherwise, we continue the current fetch request,
2879        // assuming that some frames will be dropped.
2880        if let Some(ref mut current_fetch_context) = *self.current_fetch_context.borrow_mut() &&
2881            current_fetch_context.is_seekable()
2882        {
2883            current_fetch_context.cancel(CancelReason::Backoff);
2884        }
2885    }
2886
2887    fn playback_position_changed(&self, cx: &mut JSContext, position: f64) {
2888        // Abort the following steps of the current time update if seeking is in progress.
2889        if self.seeking.get() {
2890            return;
2891        }
2892
2893        let _ = self
2894            .played
2895            .borrow_mut()
2896            .add(self.current_playback_position.get(), position);
2897        self.current_playback_position.set(position);
2898        self.official_playback_position.set(position);
2899        // https://html.spec.whatwg.org/multipage/#playing-the-media-resource
2900        // > When the current playback position of a media element changes (e.g. due to playback or seeking),
2901        // > the user agent must run the time marches on steps.
2902        // > To support use cases that depend on the timing accuracy of cue event firing,
2903        // > such as synchronizing captions with shot changes in a video,
2904        // > user agents should fire cue events as close as possible to their position on the media timeline,
2905        // > and ideally within 20 milliseconds.
2906        // > If the current playback position changes while the steps are running,
2907        // > then the user agent must wait for the steps to complete, and then must immediately rerun the steps.
2908        // > These steps are thus run as often as possible or needed.
2909        self.time_marches_on(cx, PlaybackPositionWasMoved::NormalPlayback);
2910
2911        let media_position_state =
2912            MediaPositionState::new(self.duration.get(), self.playback_rate.get(), position);
2913        debug!(
2914            "Sending media session event set position state {:?}",
2915            media_position_state
2916        );
2917        self.send_media_session_event(
2918            cx,
2919            MediaSessionEvent::SetPositionState(media_position_state),
2920        );
2921    }
2922
2923    fn playback_seek_done(&self, cx: &JSContext, position: f64) {
2924        // If the seek was initiated by script or by the user agent itself continue with the
2925        // following steps, otherwise abort.
2926        let delta = (position - self.current_seek_position.get()).abs();
2927        if !self.seeking.get() || delta > SEEK_POSITION_THRESHOLD {
2928            return;
2929        }
2930
2931        // <https://html.spec.whatwg.org/multipage/#dom-media-seek>
2932        // Step 13. Await a stable state.
2933        let task = MediaElementMicrotask::Seeked {
2934            elem: Dom::from_ref(self),
2935            generation_id: self.generation_id.get(),
2936        };
2937
2938        ScriptThread::await_stable_state(cx, Box::new(task));
2939    }
2940
2941    fn playback_state_changed(&self, cx: &mut JSContext, state: &PlaybackState) {
2942        let mut media_session_playback_state = MediaSessionPlaybackState::None_;
2943        match *state {
2944            PlaybackState::Paused => {
2945                media_session_playback_state = MediaSessionPlaybackState::Paused;
2946                if self.ready_state.get() == ReadyState::HaveMetadata {
2947                    self.change_ready_state(cx, ReadyState::HaveEnoughData);
2948                }
2949            },
2950            PlaybackState::Playing => {
2951                media_session_playback_state = MediaSessionPlaybackState::Playing;
2952                if self.ready_state.get() == ReadyState::HaveMetadata {
2953                    self.change_ready_state(cx, ReadyState::HaveEnoughData);
2954                }
2955            },
2956            PlaybackState::Buffering => {
2957                // Do not send the media session playback state change event
2958                // in this case as a None_ state is expected to clean up the
2959                // session.
2960                return;
2961            },
2962            _ => {},
2963        };
2964        debug!(
2965            "Sending media session event playback state changed to {:?}",
2966            media_session_playback_state
2967        );
2968        self.send_media_session_event(
2969            cx,
2970            MediaSessionEvent::PlaybackStateChange(media_session_playback_state),
2971        );
2972    }
2973
2974    fn seekable(&self) -> TimeRangesContainer {
2975        let mut seekable = TimeRangesContainer::default();
2976        if let Some(ref player) = *self.player.borrow() {
2977            let ranges = player.lock().unwrap().seekable();
2978            for range in ranges {
2979                let _ = seekable.add(range.start, range.end);
2980            }
2981        }
2982        seekable
2983    }
2984
2985    /// <https://html.spec.whatwg.org/multipage/#earliest-possible-position>
2986    fn earliest_possible_position(&self) -> f64 {
2987        self.seekable()
2988            .start(0)
2989            .unwrap_or_else(|_| self.current_playback_position.get())
2990    }
2991
2992    fn render_controls(&self, cx: &mut JSContext) {
2993        if self.borrow_for_shadow_tree().is_some() {
2994            // Bail out if we are already showing the controls.
2995            return;
2996        }
2997        let _ = self.shadow_tree(cx);
2998
2999        // https://html.spec.whatwg.org/multipage/#embedded-content-rendering-rules:rules-for-updating-the-text-track-rendering
3000        // > When the user agent starts exposing a user interface for a video element,
3001        // > the user agent should run the rules for updating the text track rendering
3002        // > of each of the text tracks in the video element's list of text tracks
3003        // > that are showing and whose text track kind is one of subtitles or captions
3004        // > (e.g., for text tracks based on WebVTT,
3005        // > the rules for updating the display of WebVTT text tracks). [WEBVTT]
3006        self.run_rules_for_updating_the_text_track_rendering_for_current_tracks(cx);
3007    }
3008
3009    fn remove_controls(&self, cx: &mut JSContext) {
3010        if let Some(id) = self.media_controls_id.borrow_mut().take() {
3011            // We also rerun this when removing controls, even if time marches on
3012            // hasn't run yet. That way, existing cues that are rendered will be
3013            // repositioned accordingly
3014            self.run_rules_for_updating_the_text_track_rendering_for_current_tracks(cx);
3015            self.owner_document().unregister_media_controls(&id);
3016        }
3017    }
3018
3019    /// <https://html.spec.whatwg.org/multipage/#embedded-content-rendering-rules:rules-for-updating-the-text-track-rendering>
3020    fn run_rules_for_updating_the_text_track_rendering_for_current_tracks(
3021        &self,
3022        cx: &mut JSContext,
3023    ) {
3024        if let Some(text_track_list) = self.text_tracks_list.get() {
3025            RulesForUpdatingTheTextTrackRendering::WebVTT.run(
3026                cx,
3027                self,
3028                text_track_list
3029                    .iter(cx.no_gc())
3030                    .map(|text_track| text_track.as_rooted())
3031                    .collect(),
3032                None,
3033                ShouldResetRenderingControls::Yes,
3034            );
3035        }
3036    }
3037
3038    /// Gets the video frame at the current playback position.
3039    pub(crate) fn get_current_frame(&self) -> Option<VideoFrame> {
3040        self.video_renderer
3041            .lock()
3042            .unwrap()
3043            .current_frame_holder()
3044            .map(|holder| holder.get_frame())
3045    }
3046
3047    /// Gets the current frame of the video element to present, if any.
3048    /// <https://html.spec.whatwg.org/multipage/#the-video-element:the-video-element-7>
3049    pub(crate) fn get_current_frame_to_present(&self) -> Option<MediaFrame> {
3050        let (current_frame, poster_frame) = {
3051            let renderer = self.video_renderer.lock().unwrap();
3052            (renderer.current_frame(), renderer.poster_frame())
3053        };
3054
3055        // If the show poster flag is set (or there is no current video frame to
3056        // present) AND there is a poster frame, present that.
3057        if (self.show_poster.get() || current_frame.is_none()) && poster_frame.is_some() {
3058            return poster_frame;
3059        }
3060
3061        current_frame
3062    }
3063
3064    pub(crate) fn show_poster(&self) -> bool {
3065        self.show_poster.get()
3066    }
3067
3068    pub(crate) fn current_src_url(&self) -> Option<ServoUrl> {
3069        ServoUrl::parse(&self.current_src.borrow()).ok()
3070    }
3071
3072    /// By default the audio is rendered through the audio sink automatically
3073    /// selected by the servo-media Player instance. However, in some cases, like
3074    /// the WebAudio MediaElementAudioSourceNode, we need to set a custom audio
3075    /// renderer.
3076    pub(crate) fn set_audio_renderer(
3077        &self,
3078        cx: &mut JSContext,
3079        audio_renderer: Option<Arc<Mutex<dyn AudioRenderer>>>,
3080    ) {
3081        *self.audio_renderer.borrow_mut() = audio_renderer;
3082
3083        let had_player = {
3084            if let Some(ref player) = *self.player.borrow() {
3085                if let Err(error) = player.lock().unwrap().stop() {
3086                    error!("Could not stop player: {error:?}");
3087                }
3088                true
3089            } else {
3090                false
3091            }
3092        };
3093
3094        if had_player {
3095            self.media_element_load_algorithm(cx);
3096        }
3097    }
3098
3099    fn send_media_session_event(&self, cx: &mut JSContext, event: MediaSessionEvent) {
3100        let global = self.global();
3101        let media_session = global.as_window().Navigator(cx).MediaSession(cx);
3102
3103        media_session.register_media_instance(self);
3104
3105        media_session.send_event(event);
3106    }
3107
3108    /// <https://html.spec.whatwg.org/multipage/#concept-media-load-resource>
3109    pub(crate) fn origin_is_clean(&self) -> bool {
3110        // Step 5.local (media provider object).
3111        if self.src_object.borrow().is_some() {
3112            // The resource described by the current media resource, if any,
3113            // contains the media data. It is CORS-same-origin.
3114            return true;
3115        }
3116
3117        // Step 5.remote (URL record).
3118        if self.resource_url.borrow().is_some() {
3119            // Update the media data with the contents
3120            // of response's unsafe response obtained in this fashion.
3121            // Response can be CORS-same-origin or CORS-cross-origin;
3122            if let Some(ref current_fetch_context) = *self.current_fetch_context.borrow() {
3123                return current_fetch_context.origin_is_clean();
3124            }
3125        }
3126
3127        true
3128    }
3129
3130    /// <https://html.spec.whatwg.org/multipage/#list-of-newly-introduced-cues>
3131    pub(crate) fn add_newly_added_cue(&self, cx: &mut JSContext, cue: &TextTrackCue) {
3132        // > Whenever a text track cue is added to the list of cues of a text track
3133        // > that is in the list of text tracks for a media element,
3134        // > that cue must be added to the media element's list of newly introduced cues.
3135        self.newly_introduced_cues
3136            .borrow_mut()
3137            .push(Dom::from_ref(cue));
3138        // > When a media element's list of newly introduced cues has new cues added
3139        // > while the media element's show poster flag is not set,
3140        // > then the user agent must run the time marches on steps.
3141        if !self.show_poster.get() {
3142            self.time_marches_on(cx, PlaybackPositionWasMoved::ExplicitMove);
3143        }
3144    }
3145
3146    /// <https://html.spec.whatwg.org/multipage/#list-of-newly-introduced-cues>
3147    pub(crate) fn was_added_to_list_of_text_tracks(&self, cx: &mut JSContext, track: &TextTrack) {
3148        // > Whenever a text track is added to the list of text tracks for a media element,
3149        // all of the cues in that text track's list of cues must be added to
3150        // the media element's list of newly introduced cues.
3151        let has_new_cues = {
3152            let cues = track.cues(cx.no_gc());
3153            for cue in &cues {
3154                self.newly_introduced_cues
3155                    .borrow_mut()
3156                    .push(cue.deref().clone());
3157            }
3158            !cues.is_empty()
3159        };
3160        // > When a media element's list of newly introduced cues has new cues added
3161        // > while the media element's show poster flag is not set,
3162        // > then the user agent must run the time marches on steps.
3163        if has_new_cues && !self.show_poster.get() {
3164            self.time_marches_on(cx, PlaybackPositionWasMoved::ExplicitMove);
3165        }
3166
3167        // https://html.spec.whatwg.org/multipage/#sourcing-out-of-band-text-tracks
3168        // > When a text track corresponding to a track element is added to a media element's list of text tracks,
3169        // > the user agent must queue a media element task given the media element to
3170        // > run the following steps for the media element:
3171        let this = Trusted::new(self);
3172        self.global()
3173            .task_manager()
3174            .media_element_task_source()
3175            .queue(task!(track_event_queue: move |cx| {
3176                let element = this.root();
3177                // Step 1. If the element's blocked-on-parser flag is true, then return.
3178                // TODO
3179                // Step 2. If the element's did-perform-automatic-track-selection flag is true, then return.
3180                if element.did_perform_automatic_track_selection.get() {
3181                    return;
3182                }
3183                // Step 3. Honor user preferences for automatic text track selection for this element.
3184                element.honor_user_preferences_for_automatic_text_track_selection(cx);
3185            }));
3186    }
3187
3188    /// <https://html.spec.whatwg.org/multipage/#honor-user-preferences-for-automatic-text-track-selection>
3189    fn honor_user_preferences_for_automatic_text_track_selection(&self, cx: &mut JSContext) {
3190        // Step 1. Perform automatic text track selection for subtitles and captions.
3191        self.perform_automatic_text_track_selection(
3192            cx,
3193            vec![TextTrackKind::Subtitles, TextTrackKind::Captions],
3194        );
3195        // Step 2. Perform automatic text track selection for descriptions.
3196        self.perform_automatic_text_track_selection(cx, vec![TextTrackKind::Descriptions]);
3197        // Step 3. If there are any text tracks in the media element's list of
3198        // text tracks whose text track kind is chapters or metadata that correspond to
3199        // track elements with a default attribute set whose text track mode is set to disabled,
3200        // then set the text track mode of all such tracks to hidden.
3201        // TODO
3202        // Step 4. Set the element's did-perform-automatic-track-selection flag to true.
3203        self.did_perform_automatic_track_selection.set(true);
3204    }
3205
3206    /// <https://html.spec.whatwg.org/multipage/#perform-automatic-text-track-selection>
3207    fn perform_automatic_text_track_selection(
3208        &self,
3209        cx: &mut JSContext,
3210        text_track_kinds: Vec<TextTrackKind>,
3211    ) {
3212        // Step 1. Let candidates be a list consisting of the text tracks in the media element's
3213        // list of text tracks whose text track kind is one of the kinds that were passed to the algorithm,
3214        // if any, in the order given in the list of text tracks.
3215        let Some(text_tracks_list) = self.text_tracks_list.get() else {
3216            return;
3217        };
3218        let candidates: Vec<DomRoot<TextTrack>> = text_tracks_list
3219            .iter(cx.no_gc())
3220            .filter(|track| text_track_kinds.contains(&track.Kind()))
3221            .map(|track| track.as_rooted())
3222            .collect();
3223        // Step 2. If candidates is empty, then return.
3224        if candidates.is_empty() {
3225            return;
3226        }
3227        // Step 3. If any of the text tracks in candidates have a text track mode set to showing, return.
3228        if candidates
3229            .iter()
3230            .any(|candidate| candidate.Mode() == TextTrackMode::Showing)
3231        {
3232            return;
3233        }
3234        // Step 4. If the user has expressed an interest in having a track from candidates enabled
3235        // based on its text track kind, text track language, and text track label,
3236        // then set its text track mode to showing.
3237        // Otherwise, if there are any text tracks in candidates that correspond to track elements with
3238        // a default attribute set whose text track mode is set to disabled,
3239        // then set the text track mode of the first such track to showing.
3240        if let Some(default_candidate) = candidates.iter().find(|candidate| {
3241            candidate.associated_track().is_some_and(|track| {
3242                track
3243                    .upcast::<Element>()
3244                    .has_attribute(&local_name!("default"))
3245            }) && candidate.Mode() == TextTrackMode::Disabled
3246        }) {
3247            default_candidate.set_text_track_mode(cx, TextTrackMode::Showing);
3248        }
3249    }
3250
3251    /// <https://html.spec.whatwg.org/multipage/#concept-media-muted>
3252    fn is_muted(&self) -> bool {
3253        // > A media element is muted if any of the following are true:
3254        // > * The direction of playback is backwards.
3255        self.direction_of_playback() == PlaybackDirection::Backwards
3256        // > * Its muted state is true.
3257        || self.muted_state.get().unwrap_or_else(|| {
3258            // > * Its muted state is "default" and it has a muted content attribute.
3259            self.upcast::<Element>()
3260                    .has_attribute(&local_name!("muted"))
3261            // > * Its playbackRate is so low or so high that
3262            // > * the user agent cannot play audio usefully.
3263            // TODO
3264        })
3265    }
3266}
3267
3268impl HTMLMediaElementMethods<crate::DomTypeHolder> for HTMLMediaElement {
3269    /// <https://html.spec.whatwg.org/multipage/#dom-media-networkstate>
3270    fn NetworkState(&self) -> u16 {
3271        self.network_state.get() as u16
3272    }
3273
3274    /// <https://html.spec.whatwg.org/multipage/#dom-media-readystate>
3275    fn ReadyState(&self) -> u16 {
3276        self.ready_state.get() as u16
3277    }
3278
3279    // https://html.spec.whatwg.org/multipage/#dom-media-autoplay
3280    make_bool_getter!(Autoplay, "autoplay");
3281    // https://html.spec.whatwg.org/multipage/#dom-media-autoplay
3282    make_bool_setter!(SetAutoplay, "autoplay");
3283
3284    // https://html.spec.whatwg.org/multipage/#attr-media-loop
3285    make_bool_getter!(Loop, "loop");
3286    // https://html.spec.whatwg.org/multipage/#attr-media-loop
3287    make_bool_setter!(SetLoop, "loop");
3288
3289    // https://html.spec.whatwg.org/multipage/#dom-media-defaultmuted
3290    make_bool_getter!(DefaultMuted, "muted");
3291    // https://html.spec.whatwg.org/multipage/#dom-media-defaultmuted
3292    make_bool_setter!(SetDefaultMuted, "muted");
3293
3294    // https://html.spec.whatwg.org/multipage/#dom-media-controls
3295    make_bool_getter!(Controls, "controls");
3296    // https://html.spec.whatwg.org/multipage/#dom-media-controls
3297    make_bool_setter!(SetControls, "controls");
3298
3299    // https://html.spec.whatwg.org/multipage/#dom-media-src
3300    make_url_getter!(Src, "src");
3301
3302    // https://html.spec.whatwg.org/multipage/#dom-media-src
3303    make_url_setter!(SetSrc, "src");
3304
3305    /// <https://html.spec.whatwg.org/multipage/#dom-media-crossOrigin>
3306    fn GetCrossOrigin(&self) -> Option<DOMString> {
3307        reflect_cross_origin_attribute(self.upcast::<Element>())
3308    }
3309    /// <https://html.spec.whatwg.org/multipage/#dom-media-crossOrigin>
3310    fn SetCrossOrigin(&self, cx: &mut JSContext, value: Option<DOMString>) {
3311        set_cross_origin_attribute(cx, self.upcast::<Element>(), value);
3312    }
3313
3314    /// <https://html.spec.whatwg.org/multipage/#dom-media-muted>
3315    fn Muted(&self) -> bool {
3316        // > The muted getter steps are to return true if this is muted; otherwise false.
3317        self.is_muted()
3318    }
3319
3320    /// <https://html.spec.whatwg.org/multipage/#dom-media-muted>
3321    fn SetMuted(&self, _cx: &mut JSContext, value: bool) {
3322        // > The muted setter steps are to set the muted state of this to the given value.
3323        // https://html.spec.whatwg.org/multipage/#set-the-muted-state
3324        let idl_value_changed = self.Muted() != value;
3325
3326        // Step 1. If element's muted state equals value, then return.
3327        if self.muted_state.get() == Some(value) {
3328            return;
3329        }
3330
3331        // Step 2. Set element's muted state to value.
3332        self.muted_state.set(Some(value));
3333
3334        if let Some(ref player) = *self.player.borrow() &&
3335            let Err(error) = player.lock().unwrap().set_mute(value)
3336        {
3337            warn!("Could not set mute state: {error:?}");
3338        }
3339
3340        // Step 3. If element is not allowed to play,
3341        // then run the internal pause steps for element.
3342        if !self.is_allowed_to_play() {
3343            self.internal_pause_steps();
3344        }
3345
3346        // It's implicit in the spec, but since this is an IDL setter, this
3347        // should only run when the value actually changed. We still need
3348        // to update the internal muted state, but we only should fire the
3349        // volumechange event when there was actually a difference in the
3350        // computed value of the IDL attribute.
3351        if idl_value_changed {
3352            // Step 4. Queue a media element task given element to
3353            // fire an event named volumechange at element.
3354            self.queue_media_element_task_to_fire_event(atom!("volumechange"));
3355        }
3356    }
3357
3358    /// <https://html.spec.whatwg.org/multipage/#dom-media-srcobject>
3359    fn GetSrcObject(&self) -> Option<MediaStreamOrBlob> {
3360        (*self.src_object.borrow())
3361            .as_ref()
3362            .map(|src_object| match src_object {
3363                SrcObject::Blob(blob) => MediaStreamOrBlob::Blob(DomRoot::from_ref(blob)),
3364                SrcObject::MediaStream(stream) => {
3365                    MediaStreamOrBlob::MediaStream(DomRoot::from_ref(stream))
3366                },
3367            })
3368    }
3369
3370    /// <https://html.spec.whatwg.org/multipage/#dom-media-srcobject>
3371    fn SetSrcObject(&self, cx: &mut JSContext, value: Option<MediaStreamOrBlob>) {
3372        *self.src_object.borrow_mut() = value.map(|value| value.into());
3373        self.media_element_load_algorithm(cx);
3374    }
3375
3376    // https://html.spec.whatwg.org/multipage/#attr-media-preload
3377    // Missing/Invalid values are user-agent defined.
3378    make_enumerated_getter!(
3379        Preload,
3380        "preload",
3381        "none" | "metadata" | "auto",
3382        missing => "auto",
3383        invalid => "auto"
3384    );
3385
3386    // https://html.spec.whatwg.org/multipage/#attr-media-preload
3387    make_setter!(SetPreload, "preload");
3388
3389    /// <https://html.spec.whatwg.org/multipage/#dom-media-currentsrc>
3390    fn CurrentSrc(&self) -> USVString {
3391        USVString(self.current_src.borrow().clone())
3392    }
3393
3394    /// <https://html.spec.whatwg.org/multipage/#dom-media-load>
3395    fn Load(&self, cx: &mut JSContext) {
3396        self.media_element_load_algorithm(cx);
3397    }
3398
3399    /// <https://html.spec.whatwg.org/multipage/#dom-navigator-canplaytype>
3400    fn CanPlayType(&self, type_: DOMString) -> CanPlayTypeResult {
3401        match ServoMedia::get().can_play_type(&type_.str()) {
3402            SupportsMediaType::No => CanPlayTypeResult::_empty,
3403            SupportsMediaType::Maybe => CanPlayTypeResult::Maybe,
3404            SupportsMediaType::Probably => CanPlayTypeResult::Probably,
3405        }
3406    }
3407
3408    /// <https://html.spec.whatwg.org/multipage/#dom-media-error>
3409    fn GetError(&self) -> Option<DomRoot<MediaError>> {
3410        self.error.get()
3411    }
3412
3413    /// <https://html.spec.whatwg.org/multipage/#dom-media-play>
3414    fn Play(&self, cx: &mut CurrentRealm) -> RootedPromise {
3415        let promise = Promise::new_in_realm(cx);
3416
3417        // TODO Step 1. If the media element is not allowed to play, then return a promise rejected
3418        // with a "NotAllowedError" DOMException.
3419
3420        // Step 2. If the media element's error attribute is not null and its code is
3421        // MEDIA_ERR_SRC_NOT_SUPPORTED, then return a promise rejected with a "NotSupportedError"
3422        // DOMException.
3423        if self
3424            .error
3425            .get()
3426            .is_some_and(|e| e.Code() == MEDIA_ERR_SRC_NOT_SUPPORTED)
3427        {
3428            promise.reject_error(
3429                cx,
3430                Error::NotSupported(Some("Media element source not supported".into())),
3431            );
3432            return promise;
3433        }
3434
3435        // Step 3. Let promise be a new promise and append promise to the list of pending play
3436        // promises.
3437        self.push_pending_play_promise(&promise);
3438
3439        // Step 4. Run the internal play steps for the media element.
3440        self.internal_play_steps(cx);
3441
3442        // Step 5. Return promise.
3443        promise
3444    }
3445
3446    /// <https://html.spec.whatwg.org/multipage/#dom-media-pause>
3447    fn Pause(&self, cx: &mut JSContext) {
3448        // Step 1. If the media element's networkState attribute has the value NETWORK_EMPTY, invoke
3449        // the media element's resource selection algorithm.
3450        if self.network_state.get() == NetworkState::Empty {
3451            self.invoke_resource_selection_algorithm(cx);
3452        }
3453
3454        // Step 2. Run the internal pause steps for the media element.
3455        self.internal_pause_steps();
3456    }
3457
3458    /// <https://html.spec.whatwg.org/multipage/#dom-media-paused>
3459    fn Paused(&self) -> bool {
3460        self.paused.get()
3461    }
3462
3463    /// <https://html.spec.whatwg.org/multipage/#dom-media-defaultplaybackrate>
3464    fn GetDefaultPlaybackRate(&self) -> Fallible<Finite<f64>> {
3465        Ok(Finite::wrap(self.default_playback_rate.get()))
3466    }
3467
3468    /// <https://html.spec.whatwg.org/multipage/#dom-media-defaultplaybackrate>
3469    fn SetDefaultPlaybackRate(&self, _cx: &mut JSContext, value: Finite<f64>) -> ErrorResult {
3470        // If the given value is not supported by the user agent, then throw a "NotSupportedError"
3471        // DOMException.
3472        let min_allowed = -64.0;
3473        let max_allowed = 64.0;
3474        if *value < min_allowed || *value > max_allowed {
3475            return Err(Error::NotSupported(Some(
3476                "Playback rate value must be between -64.0 and 64.0".into(),
3477            )));
3478        }
3479
3480        if self.default_playback_rate.get() == *value {
3481            return Ok(());
3482        }
3483
3484        self.default_playback_rate.set(*value);
3485
3486        // The user agent must queue a media element task given the media element to fire an event
3487        // named ratechange at the media element.
3488        self.queue_media_element_task_to_fire_event(atom!("ratechange"));
3489
3490        Ok(())
3491    }
3492
3493    /// <https://html.spec.whatwg.org/multipage/#dom-media-playbackrate>
3494    fn GetPlaybackRate(&self) -> Fallible<Finite<f64>> {
3495        Ok(Finite::wrap(self.playback_rate.get()))
3496    }
3497
3498    /// <https://html.spec.whatwg.org/multipage/#dom-media-playbackrate>
3499    fn SetPlaybackRate(&self, _cx: &mut JSContext, value: Finite<f64>) -> ErrorResult {
3500        // The attribute is mutable: on setting, the user agent must follow these steps:
3501
3502        // Step 1. If the given value is not supported by the user agent, then throw a
3503        // "NotSupportedError" DOMException.
3504        let min_allowed = -64.0;
3505        let max_allowed = 64.0;
3506        if *value < min_allowed || *value > max_allowed {
3507            return Err(Error::NotSupported(Some(
3508                "Playback rate value must be between -64.0 and 64.0".into(),
3509            )));
3510        }
3511
3512        if self.playback_rate.get() == *value {
3513            return Ok(());
3514        }
3515
3516        // Step 2. Set playbackRate to the new value, and if the element is potentially playing,
3517        // change the playback speed.
3518        self.playback_rate.set(*value);
3519
3520        if self.is_potentially_playing() &&
3521            let Some(ref player) = *self.player.borrow() &&
3522            let Err(error) = player.lock().unwrap().set_playback_rate(*value)
3523        {
3524            warn!("Could not set the playback rate: {error:?}");
3525        }
3526
3527        // The user agent must queue a media element task given the media element to fire an event
3528        // named ratechange at the media element.
3529        self.queue_media_element_task_to_fire_event(atom!("ratechange"));
3530
3531        Ok(())
3532    }
3533
3534    /// <https://html.spec.whatwg.org/multipage/#dom-media-duration>
3535    fn Duration(&self) -> f64 {
3536        self.duration.get()
3537    }
3538
3539    /// <https://html.spec.whatwg.org/multipage/#dom-media-currenttime>
3540    fn CurrentTime(&self) -> Finite<f64> {
3541        Finite::wrap(if self.default_playback_start_position.get() != 0. {
3542            self.default_playback_start_position.get()
3543        } else if self.seeking.get() {
3544            // Note that the other browsers do the similar (by checking `seeking` value or clamp the
3545            // `official` position to the earliest possible position, the duration, and the seekable
3546            // ranges.
3547            // <https://github.com/whatwg/html/issues/11773>
3548            self.current_seek_position.get()
3549        } else {
3550            self.official_playback_position.get()
3551        })
3552    }
3553
3554    /// <https://html.spec.whatwg.org/multipage/#dom-media-currenttime>
3555    fn SetCurrentTime(&self, _cx: &mut JSContext, time: Finite<f64>) {
3556        if self.ready_state.get() == ReadyState::HaveNothing {
3557            self.default_playback_start_position.set(*time);
3558        } else {
3559            self.official_playback_position.set(*time);
3560            self.seek(*time, /* approximate_for_speed */ false);
3561        }
3562    }
3563
3564    /// <https://html.spec.whatwg.org/multipage/#dom-media-seeking>
3565    fn Seeking(&self) -> bool {
3566        self.seeking.get()
3567    }
3568
3569    /// <https://html.spec.whatwg.org/multipage/#dom-media-ended>
3570    fn Ended(&self) -> bool {
3571        self.ended_playback(LoopCondition::Included) &&
3572            self.direction_of_playback() == PlaybackDirection::Forwards
3573    }
3574
3575    /// <https://html.spec.whatwg.org/multipage/#dom-media-fastseek>
3576    fn FastSeek(&self, time: Finite<f64>) {
3577        self.seek(*time, /* approximate_for_speed */ true);
3578    }
3579
3580    /// <https://html.spec.whatwg.org/multipage/#dom-media-played>
3581    fn Played(&self, cx: &mut JSContext) -> DomRoot<TimeRanges> {
3582        TimeRanges::new(cx, self.global().as_window(), self.played.borrow().clone())
3583    }
3584
3585    /// <https://html.spec.whatwg.org/multipage/#dom-media-seekable>
3586    fn Seekable(&self, cx: &mut JSContext) -> DomRoot<TimeRanges> {
3587        TimeRanges::new(cx, self.global().as_window(), self.seekable())
3588    }
3589
3590    /// <https://html.spec.whatwg.org/multipage/#dom-media-buffered>
3591    fn Buffered(&self, cx: &mut JSContext) -> DomRoot<TimeRanges> {
3592        let mut buffered = TimeRangesContainer::default();
3593        if let Some(ref player) = *self.player.borrow() {
3594            let ranges = player.lock().unwrap().buffered();
3595            for range in ranges {
3596                let _ = buffered.add(range.start, range.end);
3597            }
3598        }
3599        TimeRanges::new(cx, self.global().as_window(), buffered)
3600    }
3601
3602    /// <https://html.spec.whatwg.org/multipage/#dom-media-audiotracks>
3603    fn AudioTracks(&self, cx: &mut JSContext) -> DomRoot<AudioTrackList> {
3604        let window = self.owner_window();
3605        self.audio_tracks_list
3606            .or_init(|| AudioTrackList::new(cx, &window, &[], Some(self)))
3607    }
3608
3609    /// <https://html.spec.whatwg.org/multipage/#dom-media-videotracks>
3610    fn VideoTracks(&self, cx: &mut JSContext) -> DomRoot<VideoTrackList> {
3611        let window = self.owner_window();
3612        self.video_tracks_list
3613            .or_init(|| VideoTrackList::new(cx, &window, &[], Some(self)))
3614    }
3615
3616    /// <https://html.spec.whatwg.org/multipage/#dom-media-texttracks>
3617    fn TextTracks(&self, cx: &mut JSContext) -> DomRoot<TextTrackList> {
3618        // > The textTracks attribute of media elements must return a
3619        // > TextTrackList object representing the TextTrack objects of
3620        // > the text tracks in the media element's list of text tracks,
3621        // > in the same order as in the list of text tracks.
3622        let window = self.owner_window();
3623        self.text_tracks_list
3624            .or_init(|| TextTrackList::new(cx, self, &window, &[]))
3625    }
3626
3627    /// <https://html.spec.whatwg.org/multipage/#dom-media-addtexttrack>
3628    fn AddTextTrack(
3629        &self,
3630        cx: &mut JSContext,
3631        kind: TextTrackKind,
3632        label: DOMString,
3633        language: DOMString,
3634    ) -> DomRoot<TextTrack> {
3635        let window = self.owner_window();
3636        // Step 1. Create a new TextTrack object.
3637        // Step 2. Create a new text track corresponding to the new object,
3638        // and set its text track kind to kind, its text track label to label,
3639        // its text track language to language, its text track readiness state
3640        // to the text track loaded state, its text track mode to the text
3641        // track hidden mode, and its text track list of cues to an empty list.
3642        let track = TextTrack::new(
3643            cx,
3644            &window,
3645            DOMString::new(),
3646            kind,
3647            label,
3648            language,
3649            TextTrackMode::Hidden,
3650            None,
3651        );
3652        // Step 3. Add the new text track to the media element's list of text tracks.
3653        // Step 4. Queue a media element task given the media element to fire an event
3654        // named addtrack at the media element's textTracks attribute's TextTrackList object,
3655        // using TrackEvent, with the track attribute initialized to
3656        // the new text track's TextTrack object.
3657        self.TextTracks(cx).add(cx, &track);
3658        // Step 5. Return the new TextTrack object.
3659        DomRoot::from_ref(&track)
3660    }
3661
3662    /// <https://html.spec.whatwg.org/multipage/#dom-media-volume>
3663    fn GetVolume(&self) -> Fallible<Finite<f64>> {
3664        Ok(Finite::wrap(self.volume.get()))
3665    }
3666
3667    /// <https://html.spec.whatwg.org/multipage/#dom-media-volume>
3668    fn SetVolume(&self, _cx: &mut JSContext, value: Finite<f64>) -> ErrorResult {
3669        // If the new value is outside the range 0.0 to 1.0 inclusive, then, on setting, an
3670        // "IndexSizeError" DOMException must be thrown instead.
3671        let minimum_volume = 0.0;
3672        let maximum_volume = 1.0;
3673        if *value < minimum_volume || *value > maximum_volume {
3674            return Err(Error::IndexSize(Some(
3675                "Volume value must be between 0.0 and 1.0".into(),
3676            )));
3677        }
3678
3679        if self.volume.get() == *value {
3680            return Ok(());
3681        }
3682
3683        self.volume.set(*value);
3684
3685        if let Some(ref player) = *self.player.borrow() &&
3686            let Err(error) = player.lock().unwrap().set_volume(*value)
3687        {
3688            warn!("Could not set the volume: {error:?}");
3689        }
3690
3691        // The user agent must queue a media element task given the media element to fire an event
3692        // named volumechange at the media element.
3693        self.queue_media_element_task_to_fire_event(atom!("volumechange"));
3694
3695        // Then, if the media element is not allowed to play, the user agent must run the internal
3696        // pause steps for the media element.
3697        if !self.is_allowed_to_play() {
3698            self.internal_pause_steps();
3699        }
3700
3701        Ok(())
3702    }
3703}
3704
3705impl VirtualMethods for HTMLMediaElement {
3706    fn super_type(&self) -> Option<&dyn VirtualMethods> {
3707        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
3708    }
3709
3710    fn attribute_mutated(
3711        &self,
3712        cx: &mut JSContext,
3713        attr: AttrRef<'_>,
3714        mutation: AttributeMutation,
3715    ) {
3716        self.super_type()
3717            .unwrap()
3718            .attribute_mutated(cx, attr, mutation);
3719
3720        match *attr.local_name() {
3721            local_name!("src") => {
3722                // <https://html.spec.whatwg.org/multipage/#location-of-the-media-resource>
3723                // If a src attribute of a media element is set or changed, the user agent must invoke
3724                // the media element's media element load algorithm (Removing the src attribute does
3725                // not do this, even if there are source elements present).
3726                if !mutation.is_removal() {
3727                    self.media_element_load_algorithm(cx);
3728                }
3729            },
3730            local_name!("autoplay") | local_name!("loop") => {
3731                self.update_keep_alive();
3732            },
3733            local_name!("controls") => {
3734                if mutation.new_value(attr).is_some() {
3735                    self.render_controls(cx);
3736                } else {
3737                    self.remove_controls(cx);
3738                }
3739            },
3740            _ => (),
3741        };
3742    }
3743
3744    /// <https://html.spec.whatwg.org/multipage/#playing-the-media-resource:remove-an-element-from-a-document>
3745    fn unbind_from_tree(&self, cx: &mut JSContext, context: &UnbindContext) {
3746        self.super_type().unwrap().unbind_from_tree(cx, context);
3747
3748        self.remove_controls(cx);
3749
3750        // Step 1. Await a stable state, allowing the task that removed the media element from the Document to continue.
3751        // The synchronous section consists of all the remaining steps of this algorithm.
3752        // (Steps in the synchronous section are marked with ⌛.)
3753        if context.tree_connected {
3754            let task = MediaElementMicrotask::PauseIfNotInDocument {
3755                elem: Dom::from_ref(self),
3756            };
3757            ScriptThread::await_stable_state(cx, Box::new(task));
3758        }
3759    }
3760
3761    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
3762        self.super_type().unwrap().adopting_steps(cx, old_doc);
3763
3764        old_doc.unregister_active_media_element(self);
3765        self.update_keep_alive();
3766
3767        // Note that media control id should be adopting between documents so "privileged"
3768        // document.servoGetMediaControls(id) API is keeping access to the whitelist of media
3769        // controls identifiers.
3770        if let Some(id) = &*self.media_controls_id.borrow() {
3771            let Some(shadow_root) = self.upcast::<Element>().shadow_root() else {
3772                error!("Missing media controls shadow root");
3773                return;
3774            };
3775
3776            old_doc.unregister_media_controls(id);
3777            self.owner_document()
3778                .register_media_controls(id, &shadow_root);
3779        }
3780    }
3781}
3782
3783#[derive(JSTraceable, MallocSizeOf)]
3784pub(crate) enum MediaElementMicrotask {
3785    ResourceSelection {
3786        elem: Dom<HTMLMediaElement>,
3787        generation_id: u32,
3788        #[no_trace]
3789        base_url: ServoUrl,
3790    },
3791    PauseIfNotInDocument {
3792        elem: Dom<HTMLMediaElement>,
3793    },
3794    Seeked {
3795        elem: Dom<HTMLMediaElement>,
3796        generation_id: u32,
3797    },
3798    SelectNextSourceChild {
3799        elem: Dom<HTMLMediaElement>,
3800        generation_id: u32,
3801    },
3802    SelectNextSourceChildAfterWait {
3803        elem: Dom<HTMLMediaElement>,
3804        generation_id: u32,
3805    },
3806}
3807
3808impl MicrotaskRunnable for MediaElementMicrotask {
3809    fn handler(&self, cx: &mut JSContext) {
3810        let mut realm = match self {
3811            &MediaElementMicrotask::ResourceSelection { ref elem, .. } |
3812            &MediaElementMicrotask::PauseIfNotInDocument { ref elem } |
3813            &MediaElementMicrotask::Seeked { ref elem, .. } |
3814            &MediaElementMicrotask::SelectNextSourceChild { ref elem, .. } |
3815            &MediaElementMicrotask::SelectNextSourceChildAfterWait { ref elem, .. } => {
3816                enter_auto_realm(cx, &**elem)
3817            },
3818        };
3819        let cx = &mut realm;
3820        match self {
3821            &MediaElementMicrotask::ResourceSelection {
3822                ref elem,
3823                generation_id,
3824                ref base_url,
3825            } => {
3826                if generation_id == elem.generation_id.get() {
3827                    elem.resource_selection_algorithm_sync(cx, base_url.clone());
3828                }
3829            },
3830            // https://html.spec.whatwg.org/multipage/#playing-the-media-resource:remove-an-element-from-a-document
3831            MediaElementMicrotask::PauseIfNotInDocument { elem } => {
3832                // Step 2. ⌛ If the media element is in a document, return.
3833                if elem.upcast::<Node>().is_connected() {
3834                    return;
3835                }
3836                // Step 3. ⌛ Run the internal pause steps for the media element.
3837                elem.internal_pause_steps();
3838            },
3839            &MediaElementMicrotask::Seeked {
3840                ref elem,
3841                generation_id,
3842            } => {
3843                if generation_id == elem.generation_id.get() {
3844                    elem.seek_end(cx);
3845                }
3846            },
3847            &MediaElementMicrotask::SelectNextSourceChild {
3848                ref elem,
3849                generation_id,
3850            } => {
3851                if generation_id == elem.generation_id.get() {
3852                    elem.select_next_source_child(cx);
3853                }
3854            },
3855            &MediaElementMicrotask::SelectNextSourceChildAfterWait {
3856                ref elem,
3857                generation_id,
3858            } => {
3859                if generation_id == elem.generation_id.get() {
3860                    elem.select_next_source_child_after_wait(cx);
3861                }
3862            },
3863        }
3864    }
3865}
3866
3867enum Resource {
3868    Object,
3869    Url(ServoUrl),
3870}
3871
3872#[derive(Debug, MallocSizeOf, PartialEq)]
3873enum DataBuffer {
3874    Payload(Vec<u8>),
3875    EndOfStream,
3876}
3877
3878#[derive(MallocSizeOf)]
3879struct BufferedDataSource {
3880    /// During initial setup and seeking (including clearing the buffer queue
3881    /// and resetting the end-of-stream state), the data source should be locked and
3882    /// any request for processing should be ignored until the media player informs us
3883    /// via the NeedData event that it is ready to accept incoming data.
3884    locked: Cell<bool>,
3885    /// Temporary storage for incoming data.
3886    buffers: VecDeque<DataBuffer>,
3887}
3888
3889impl BufferedDataSource {
3890    fn new() -> BufferedDataSource {
3891        BufferedDataSource {
3892            locked: Cell::new(true),
3893            buffers: VecDeque::default(),
3894        }
3895    }
3896
3897    fn set_locked(&self, locked: bool) {
3898        self.locked.set(locked)
3899    }
3900
3901    fn add_buffer_to_queue(&mut self, buffer: DataBuffer) {
3902        debug_assert_ne!(
3903            self.buffers.back(),
3904            Some(&DataBuffer::EndOfStream),
3905            "The media backend not expects any further data after end of stream"
3906        );
3907
3908        self.buffers.push_back(buffer);
3909    }
3910
3911    fn process_into_player_from_queue(
3912        &mut self,
3913        player: &Arc<Mutex<dyn Player>>,
3914    ) -> Result<(), PlayerError> {
3915        // Early out if any request for processing should be ignored.
3916        if self.locked.get() {
3917            return Ok(());
3918        }
3919
3920        while let Some(buffer) = self.buffers.pop_front() {
3921            match buffer {
3922                DataBuffer::Payload(payload) => {
3923                    if let Err(error) = player.lock().unwrap().push_data(payload) {
3924                        warn!("Could not push input data to player: {error:?}");
3925                        return Err(error);
3926                    }
3927                },
3928                DataBuffer::EndOfStream => {
3929                    if let Err(error) = player.lock().unwrap().end_of_stream() {
3930                        warn!("Could not signal EOS to player: {error:?}");
3931                        return Err(error);
3932                    }
3933                },
3934            }
3935        }
3936
3937        Ok(())
3938    }
3939
3940    fn reset(&mut self) {
3941        self.locked.set(true);
3942        self.buffers.clear();
3943    }
3944}
3945
3946/// Indicates the reason why a fetch request was cancelled.
3947#[derive(Debug, MallocSizeOf, PartialEq)]
3948enum CancelReason {
3949    /// We were asked to stop pushing data to the player.
3950    Backoff,
3951    /// An error ocurred while fetching the media data.
3952    Error,
3953    /// The fetching process is aborted by the user.
3954    Abort,
3955}
3956
3957#[derive(MallocSizeOf)]
3958pub(crate) struct HTMLMediaElementFetchContext {
3959    /// The fetch request id.
3960    request_id: RequestId,
3961    /// Some if the request has been cancelled.
3962    cancel_reason: Option<CancelReason>,
3963    /// Indicates whether the fetched stream is seekable.
3964    is_seekable: bool,
3965    /// Indicates whether the fetched stream is origin clean.
3966    origin_clean: bool,
3967    /// The buffered data source which to be processed by media backend.
3968    data_source: RefCell<BufferedDataSource>,
3969    /// Fetch controller. Allows cancelling the current fetch request by
3970    /// manually calling its .cancel() method or automatically on Drop.
3971    fetch_controller: FetchController,
3972}
3973
3974impl HTMLMediaElementFetchContext {
3975    fn new(
3976        request_id: RequestId,
3977        core_resource_thread: CoreResourceThread,
3978    ) -> HTMLMediaElementFetchContext {
3979        HTMLMediaElementFetchContext {
3980            request_id,
3981            cancel_reason: None,
3982            is_seekable: false,
3983            origin_clean: true,
3984            data_source: RefCell::new(BufferedDataSource::new()),
3985            fetch_controller: FetchController::new(request_id, false, core_resource_thread),
3986        }
3987    }
3988
3989    fn request_id(&self) -> RequestId {
3990        self.request_id
3991    }
3992
3993    fn is_seekable(&self) -> bool {
3994        self.is_seekable
3995    }
3996
3997    fn set_seekable(&mut self, seekable: bool) {
3998        self.is_seekable = seekable;
3999    }
4000
4001    fn origin_is_clean(&self) -> bool {
4002        self.origin_clean
4003    }
4004
4005    fn set_origin_clean(&mut self, origin_clean: bool) {
4006        self.origin_clean = origin_clean;
4007    }
4008
4009    fn data_source(&self) -> &RefCell<BufferedDataSource> {
4010        &self.data_source
4011    }
4012
4013    fn cancel(&mut self, reason: CancelReason) {
4014        if self.cancel_reason.is_some() {
4015            return;
4016        }
4017        self.cancel_reason = Some(reason);
4018        self.data_source.borrow_mut().reset();
4019        self.fetch_controller.abort();
4020    }
4021
4022    fn cancel_reason(&self) -> &Option<CancelReason> {
4023        &self.cancel_reason
4024    }
4025}
4026
4027struct HTMLMediaElementFetchListener {
4028    /// The element that initiated the request.
4029    element: Trusted<HTMLMediaElement>,
4030    /// The generation of the media element when this fetch started.
4031    generation_id: u32,
4032    /// The fetch request id.
4033    request_id: RequestId,
4034    /// Time of last progress notification.
4035    next_progress_event: Instant,
4036    /// Url for the resource.
4037    url: ServoUrl,
4038    /// Expected content length of the media asset being fetched or played.
4039    expected_content_length: Option<u64>,
4040    /// Actual content length of the media asset was fetched.
4041    fetched_content_length: u64,
4042    /// Discarded content length from the network for the ongoing
4043    /// request if range requests are not supported. Seek requests set it
4044    /// to the required position (in bytes).
4045    content_length_to_discard: u64,
4046}
4047
4048impl FetchResponseListener for HTMLMediaElementFetchListener {
4049    fn process_request_body(&mut self, _: &GlobalScope, _: RequestId) {}
4050
4051    fn process_response(
4052        &mut self,
4053        cx: &mut JSContext,
4054        _: &GlobalScope,
4055        _: RequestId,
4056        metadata: Result<FetchMetadata, NetworkError>,
4057    ) {
4058        let element = self.element.root();
4059
4060        let (metadata, cors_status) = match metadata {
4061            Ok(fetch_metadata) => {
4062                let cors_status = fetch_metadata.cors_status();
4063                (Some(fetch_metadata.into()), cors_status)
4064            },
4065            Err(_) => (None, CorsStatus::Safe),
4066        };
4067
4068        let (status_is_success, is_seekable) =
4069            metadata
4070                .as_ref()
4071                .map_or((false, false), |metadata: &Metadata| {
4072                    let status = &metadata.status;
4073                    (status.is_success(), *status == StatusCode::PARTIAL_CONTENT)
4074                });
4075
4076        // <https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list>
4077        if !status_is_success {
4078            if element.ready_state.get() == ReadyState::HaveNothing {
4079                // => "If the media data cannot be fetched at all, due to network errors..."
4080                element.media_data_processing_failure_steps(cx);
4081            } else {
4082                // => "If the connection is interrupted after some media data has been received..."
4083                element.media_data_processing_fatal_steps(cx, MEDIA_ERR_NETWORK);
4084            }
4085            return;
4086        }
4087
4088        if let Some(ref mut current_fetch_context) = *element.current_fetch_context.borrow_mut() {
4089            current_fetch_context.set_seekable(is_seekable);
4090            current_fetch_context.set_origin_clean(cors_status == CorsStatus::Safe);
4091        }
4092
4093        if let Some(metadata) = metadata.as_ref() &&
4094            let Some(headers) = metadata.headers.as_ref()
4095        {
4096            // For range requests we get the size of the media asset from the Content-Range
4097            // header. Otherwise, we get it from the Content-Length header.
4098            let content_length = if let Some(content_range) = headers.typed_get::<ContentRange>() {
4099                content_range.bytes_len()
4100            } else {
4101                headers
4102                    .typed_get::<ContentLength>()
4103                    .map(|content_length| content_length.0)
4104            };
4105
4106            // We only set the expected input size if it changes.
4107            if content_length != self.expected_content_length &&
4108                let Some(content_length) = content_length
4109            {
4110                self.expected_content_length = Some(content_length);
4111            }
4112        }
4113
4114        // Explicit media player initialization with live/seekable source.
4115        if let Err(e) = element
4116            .player
4117            .borrow()
4118            .as_ref()
4119            .unwrap()
4120            .lock()
4121            .unwrap()
4122            .set_seekable(is_seekable)
4123        {
4124            warn!("Could not set player seekable {:?}", e);
4125        }
4126
4127        if let Some(expected_content_length) = self.expected_content_length &&
4128            let Err(e) = element
4129                .player
4130                .borrow()
4131                .as_ref()
4132                .unwrap()
4133                .lock()
4134                .unwrap()
4135                .set_input_size(expected_content_length)
4136        {
4137            warn!("Could not set player input size {:?}", e);
4138        }
4139    }
4140
4141    fn process_response_chunk(
4142        &mut self,
4143        _: &mut JSContext,
4144        _: &GlobalScope,
4145        _: RequestId,
4146        chunk: Bytes,
4147    ) {
4148        let element = self.element.root();
4149
4150        self.fetched_content_length += chunk.len() as u64;
4151
4152        // If an error was received previously, we skip processing the payload.
4153        if let Some(ref mut current_fetch_context) = *element.current_fetch_context.borrow_mut() {
4154            if let Some(CancelReason::Backoff) = current_fetch_context.cancel_reason() {
4155                return;
4156            }
4157
4158            // Discard chunk of the response body if fetch context doesn't support range requests.
4159            let payload =
4160                if !current_fetch_context.is_seekable() && self.content_length_to_discard != 0 {
4161                    if chunk.len() as u64 > self.content_length_to_discard {
4162                        let shrink_chunk = chunk.slice(self.content_length_to_discard as usize..);
4163                        self.content_length_to_discard = 0;
4164                        shrink_chunk
4165                    } else {
4166                        // Completely discard this response chunk.
4167                        self.content_length_to_discard -= chunk.len() as u64;
4168                        return;
4169                    }
4170                } else {
4171                    chunk
4172                };
4173
4174            if let Err(e) = {
4175                let mut data_source = current_fetch_context.data_source().borrow_mut();
4176                data_source.add_buffer_to_queue(DataBuffer::Payload(payload.to_vec()));
4177                data_source
4178                    .process_into_player_from_queue(element.player.borrow().as_ref().unwrap())
4179            } {
4180                // If we are pushing too much data and we know that we can
4181                // restart the download later from where we left, we cancel
4182                // the current request. Otherwise, we continue the request
4183                // assuming that we may drop some frames.
4184                if e == PlayerError::EnoughData {
4185                    current_fetch_context.cancel(CancelReason::Backoff);
4186                }
4187                return;
4188            }
4189        }
4190
4191        // <https://html.spec.whatwg.org/multipage/#concept-media-load-resource>
4192        // While the load is not suspended (see below), every 350ms (±200ms) or for every byte
4193        // received, whichever is least frequent, queue a media element task given the media element
4194        // to fire an event named progress at the element.
4195        if Instant::now() > self.next_progress_event {
4196            element.queue_media_element_task_to_fire_event(atom!("progress"));
4197            self.next_progress_event = Instant::now() + Duration::from_millis(350);
4198        }
4199    }
4200
4201    fn process_response_eof(
4202        self,
4203        cx: &mut JSContext,
4204        global: &GlobalScope,
4205        _: RequestId,
4206        status: Result<(), NetworkError>,
4207        timing: ResourceFetchTiming,
4208    ) {
4209        let element = self.element.root();
4210
4211        // <https://html.spec.whatwg.org/multipage/#media-data-processing-steps-list>
4212        if status.is_ok() && self.fetched_content_length != 0 {
4213            // => "Once the entire media resource has been fetched..."
4214
4215            // There are no more chunks of the response body forthcoming, so we can
4216            // go ahead and notify the media backend not to expect any further data.
4217            if let Some(ref mut current_fetch_context) = *element.current_fetch_context.borrow_mut()
4218            {
4219                // On initial state change READY -> PAUSED the media player perform
4220                // seek to initial position by event with seek segment (TIME format)
4221                // while media stack operates in BYTES format and configuring segment
4222                // start and stop positions without the total size of the stream is not
4223                // possible. As fallback the media player perform seek with BYTES format
4224                // and initiate seek request via "seek-data" callback with required offset.
4225                if self.expected_content_length.is_none() &&
4226                    let Err(e) = element
4227                        .player
4228                        .borrow()
4229                        .as_ref()
4230                        .unwrap()
4231                        .lock()
4232                        .unwrap()
4233                        .set_input_size(self.fetched_content_length)
4234                {
4235                    warn!("Could not set player input size {:?}", e);
4236                }
4237
4238                let mut data_source = current_fetch_context.data_source().borrow_mut();
4239
4240                data_source.add_buffer_to_queue(DataBuffer::EndOfStream);
4241                let _ = data_source
4242                    .process_into_player_from_queue(element.player.borrow().as_ref().unwrap());
4243            }
4244
4245            // Step 1. Fire an event named progress at the media element.
4246            element
4247                .upcast::<EventTarget>()
4248                .fire_event(cx, atom!("progress"));
4249
4250            // Step 2. Set the networkState to NETWORK_IDLE and fire an event named suspend at the
4251            // media element.
4252            element.network_state.set(NetworkState::Idle);
4253
4254            element
4255                .upcast::<EventTarget>()
4256                .fire_event(cx, atom!("suspend"));
4257        } else if status.is_err() && element.ready_state.get() != ReadyState::HaveNothing {
4258            // => "If the connection is interrupted after some media data has been received..."
4259            element.media_data_processing_fatal_steps(cx, MEDIA_ERR_NETWORK);
4260        } else {
4261            // => "If the media data can be fetched but is found by inspection to be in an
4262            // unsupported format, or can otherwise not be rendered at all"
4263            element.media_data_processing_failure_steps(cx);
4264        }
4265
4266        network_listener::submit_timing(cx, global, &self, &status, &timing);
4267    }
4268
4269    fn process_csp_violations(
4270        &mut self,
4271        cx: &mut JSContext,
4272        global: &GlobalScope,
4273        _request_id: RequestId,
4274        violations: Vec<Violation>,
4275    ) {
4276        global.report_csp_violations(cx, violations, None, None);
4277    }
4278
4279    fn should_invoke(&self) -> bool {
4280        let element = self.element.root();
4281
4282        if element.generation_id.get() != self.generation_id || element.player.borrow().is_none() {
4283            return false;
4284        }
4285
4286        let Some(ref current_fetch_context) = *element.current_fetch_context.borrow() else {
4287            return false;
4288        };
4289
4290        // Whether the new fetch request was triggered.
4291        if current_fetch_context.request_id() != self.request_id {
4292            return false;
4293        }
4294
4295        // Whether the current fetch request was cancelled due to a network or decoding error, or
4296        // was aborted by the user.
4297        if let Some(cancel_reason) = current_fetch_context.cancel_reason() &&
4298            matches!(*cancel_reason, CancelReason::Error | CancelReason::Abort)
4299        {
4300            return false;
4301        }
4302
4303        true
4304    }
4305}
4306
4307impl ResourceTimingListener for HTMLMediaElementFetchListener {
4308    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
4309        let initiator_type =
4310            InitiatorType::LocalName(self.element.root().upcast::<Element>().local_name().clone());
4311        (initiator_type, self.url.clone())
4312    }
4313}
4314
4315impl HTMLMediaElementFetchListener {
4316    fn new(element: &HTMLMediaElement, request_id: RequestId, url: ServoUrl, offset: u64) -> Self {
4317        Self {
4318            element: Trusted::new(element),
4319            generation_id: element.generation_id.get(),
4320            request_id,
4321            next_progress_event: Instant::now() + Duration::from_millis(350),
4322            url,
4323            expected_content_length: None,
4324            fetched_content_length: 0,
4325            content_length_to_discard: offset,
4326        }
4327    }
4328}
4329
4330/// A weak reference to an [`HTMLMediaElement`], remembering the concrete type of
4331/// the referenced element.
4332///
4333/// A `WeakRef<HTMLMediaElement>` would only remember the parent type, which is not
4334/// the type an audio or video element is allocated as.
4335#[derive(JSTraceable, MallocSizeOf)]
4336pub(crate) enum MediaElementWeakRef {
4337    Audio(WeakRef<HTMLAudioElement>),
4338    Video(WeakRef<HTMLVideoElement>),
4339}
4340
4341impl MediaElementWeakRef {
4342    /// Create a weak reference to the given media element.
4343    pub(crate) fn new(element: &HTMLMediaElement) -> Self {
4344        if let Some(audio) = element.downcast::<HTMLAudioElement>() {
4345            return Self::Audio(WeakRef::new(audio));
4346        }
4347
4348        match element.downcast::<HTMLVideoElement>() {
4349            Some(video) => Self::Video(WeakRef::new(video)),
4350            None => unreachable!(
4351                "Only HTMLAudioElement and HTMLVideoElement derive from HTMLMediaElement."
4352            ),
4353        }
4354    }
4355
4356    /// Root the referenced element, if it has not been collected yet.
4357    pub(crate) fn root(&self) -> Option<DomRoot<HTMLMediaElement>> {
4358        match self {
4359            Self::Audio(audio) => audio.root().map(DomRoot::upcast),
4360            Self::Video(video) => video.root().map(DomRoot::upcast),
4361        }
4362    }
4363}
4364
4365/// The [`HTMLMediaElementEventHandler`] is a structure responsible for handling media events for
4366/// the [`HTMLMediaElement`] and exists to decouple ownership of the [`HTMLMediaElement`] from IPC
4367/// router callback.
4368#[derive(JSTraceable, MallocSizeOf)]
4369struct HTMLMediaElementEventHandler {
4370    element: MediaElementWeakRef,
4371}
4372
4373#[expect(unsafe_code)]
4374unsafe impl Send for HTMLMediaElementEventHandler {}
4375
4376impl HTMLMediaElementEventHandler {
4377    fn new(element: &HTMLMediaElement) -> Self {
4378        Self {
4379            element: MediaElementWeakRef::new(element),
4380        }
4381    }
4382
4383    fn handle_player_event(&self, cx: &mut JSContext, player_id: usize, event: PlayerEvent) {
4384        let Some(element) = self.element.root() else {
4385            return;
4386        };
4387
4388        // Abort event processing if the associated media player is outdated.
4389        if element.player_id().is_none_or(|id| id != player_id) {
4390            return;
4391        }
4392
4393        match event {
4394            PlayerEvent::DurationChanged(duration) => element.playback_duration_changed(duration),
4395            PlayerEvent::EndOfStream => element.playback_end(cx),
4396            PlayerEvent::EnoughData => element.playback_enough_data(),
4397            PlayerEvent::Error(ref error) => element.playback_error(cx, error),
4398            PlayerEvent::MetadataUpdated(ref metadata) => {
4399                element.playback_metadata_updated(cx, metadata)
4400            },
4401            PlayerEvent::NeedData => element.playback_need_data(),
4402            PlayerEvent::PositionChanged(position) => {
4403                element.playback_position_changed(cx, position)
4404            },
4405            PlayerEvent::SeekData(offset, seek_lock) => {
4406                element.fetch_request(cx, Some(offset), Some(seek_lock))
4407            },
4408            PlayerEvent::SeekDone(position) => element.playback_seek_done(cx, position),
4409            PlayerEvent::StateChanged(ref state) => element.playback_state_changed(cx, state),
4410            PlayerEvent::VideoFrameUpdated => element.playback_video_frame_updated(cx.no_gc()),
4411        }
4412    }
4413}
4414
4415impl Drop for HTMLMediaElementEventHandler {
4416    fn drop(&mut self) {
4417        // The weak reference to the media element is not thread-safe and MUST be deleted on the
4418        // script thread, which is guaranteed by ownership of the `event handler` in the IPC router
4419        // callback (queued task to the media element task source) and the media element itself.
4420        assert_in_script();
4421    }
4422}