Skip to main content

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