Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / audio_track.lucb

#==============================================================================================
#
#   audio_track - HTML::AudioTrack, one of a media resource's audio tracks
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/AudioTrack.cpp (lines 1-64) and the inline accessors of
#       AudioTrack.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_enabled's task: `[this]`.
struct HtmlAudioTrackChangeTask:
    var this: HtmlAudioTrack*

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

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

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

## Ported from AudioTrack.cpp:36 Web::HTML::AudioTrack::visit_edges (override).
pub func html_audio_track_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlAudioTrack*)base
    html_media_track_base_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_audio_track_list)

## Ported from AudioTrack.h:24 Web::HTML::AudioTrack::set_audio_track_list.
pub func html_audio_track_set_audio_track_list(this: HtmlAudioTrack*, audio_track_list: HtmlAudioTrackList*?):
    this.m_audio_track_list = audio_track_list

## Ported from AudioTrack.h:26 Web::HTML::AudioTrack::enabled.
pub func html_audio_track_enabled(this: HtmlAudioTrack*) -> bool:
    return this.m_enabled

## Ported from AudioTrack.h (MediaTrackBase::track_in_playback_manager).
pub func html_audio_track_track_in_playback_manager(this: HtmlAudioTrack*) -> MediaTrack:
    return html_media_track_base_track_in_playback_manager(this.html_media_track_base())

## Ported from AudioTrack.cpp:43 Web::HTML::AudioTrack::set_enabled.
# https://html.spec.whatwg.org/multipage/media.html#dom-audiotrack-enabled
pub func html_audio_track_set_enabled(this: HtmlAudioTrack*, enabled: bool):
    # On setting, it must enable the track if the new value is true, and disable it otherwise. (If the track is no
    # longer in an AudioTrackList object, then the track being enabled or disabled has no effect beyond changing the
    # value of the attribute on the AudioTrack object.)
    if this.m_enabled == enabled:
        return

    if this.m_audio_track_list != none:
        # Whenever an audio track in an AudioTrackList that was disabled is enabled, and whenever one that was enabled
        # is disabled, the user agent must queue a media element task given the media element to fire an event named
        # change at the AudioTrackList object.
        let task = new HtmlAudioTrackChangeTask(this = this) else trap("out of memory")
        html_media_element_queue_a_media_element_task(this.media_track_base.m_media_element, ak.Function0[unit](call = HtmlAudioTrackChangeTask.run, context = task))

    this.m_enabled = enabled
    html_media_element_set_audio_track_enabled(this.media_track_base.m_media_element, this, enabled)