Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / video_track.lucb

#==============================================================================================
#
#   video_track - HTML::VideoTrack, one of a media resource's video tracks
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/VideoTrack.cpp (lines 1-78) and the inline accessors of
#       VideoTrack.h.
#       Only a demuxer makes tracks (HTMLMediaElement::on_audio_track_added / on_video_track_added,
#       from the PlaybackManager); without one (external/lib_media) none is made, so this runs
#       when playback is ported.
#
#       Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
#       Copyright (c) 2025, Gregory Bertilson <gregory@ladybird.org>
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## The captures of set_selected's task: `[this, video_track_list = m_video_track_list]`.
struct HtmlVideoTrackChangeTask:
    var this: HtmlVideoTrack*
    var video_track_list: HtmlVideoTrackList*

    static func run(context: void*?):
        let captures = (HtmlVideoTrackChangeTask*)(context else trap("VideoTrack change: no captures"))
        let realm = bind_platform_object_realm(captures.this.bind_platform_object())
        _ = dom_event_target_dispatch_event(captures.video_track_list.dom_event_target(), dom_event_create(realm, html_event_names_change))

## Ported from VideoTrack.cpp:23 Web::HTML::VideoTrack::VideoTrack.
pub func html_video_track_construct(this: HtmlVideoTrack*, realm: Realm*, media_element: HtmlMediaElement*, track: MediaTrack):
    html_media_track_base_construct(this.html_media_track_base(), realm, media_element, track)

## Ported from VideoTrack.cpp:30 Web::HTML::VideoTrack::initialize (override).
pub func html_video_track_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.video_track)
    # Base::initialize(realm): PlatformObject inherits JS::Object's.
    js_cell_initialize_impl(base, realm)

## Ported from VideoTrack.cpp:36 Web::HTML::VideoTrack::visit_edges (override).
pub func html_video_track_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlVideoTrack*)base
    html_media_track_base_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_video_track_list)

## Ported from VideoTrack.h:25 Web::HTML::VideoTrack::set_video_track_list.
pub func html_video_track_set_video_track_list(this: HtmlVideoTrack*, video_track_list: HtmlVideoTrackList*?):
    this.m_video_track_list = video_track_list

## Ported from VideoTrack.h:27 Web::HTML::VideoTrack::selected.
pub func html_video_track_selected(this: HtmlVideoTrack*) -> bool:
    return this.m_selected

## Ported from VideoTrack.h (MediaTrackBase::track_in_playback_manager).
pub func html_video_track_track_in_playback_manager(this: HtmlVideoTrack*) -> MediaTrack:
    return html_media_track_base_track_in_playback_manager(this.html_media_track_base())

## Ported from VideoTrack.cpp:43 Web::HTML::VideoTrack::set_selected.
# https://html.spec.whatwg.org/multipage/media.html#dom-videotrack-selected
pub func html_video_track_set_selected(this: HtmlVideoTrack*, selected: bool):
    # On setting, it must select the track if the new value is true, and unselect it otherwise.
    if this.m_selected == selected:
        return

    # If the track is in a VideoTrackList, then all the other VideoTrack objects in that list must be unselected. (If the track is
    # no longer in a VideoTrackList object, then the track being selected or unselected has no effect beyond changing the value of
    # the attribute on the VideoTrack object.)
    if let video_track_list = this.m_video_track_list:
        for video_track in html_video_track_list_video_tracks(video_track_list):
            if video_track != this:
                video_track.m_selected = false

        # Whenever a track in a VideoTrackList that was previously not selected is selected, and whenever the selected track in a
        # VideoTrackList is unselected without a new track being selected in its stead, the user agent must queue a media element
        # task given the media element to fire an event named change at the VideoTrackList object. This task must be queued before
        # the task that fires the resize event, if any.
        let previously_unselected_track_is_selected = not this.m_selected and selected
        let selected_track_was_unselected_without_another_selection = this.m_selected and not selected

        if previously_unselected_track_is_selected or selected_track_was_unselected_without_another_selection:
            let task = new HtmlVideoTrackChangeTask(this = this, video_track_list = video_track_list) else trap("out of memory")
            html_media_element_queue_a_media_element_task(this.media_track_base.m_media_element, ak.Function0[unit](call = HtmlVideoTrackChangeTask.run, context = task))

    this.m_selected = selected

    # AD-HOC: Inform the element node that we have (un)selected a video track for layout.
    html_media_element_set_selected_video_track(this.media_track_base.m_media_element, this if this.m_selected else none)