Truly independent web browser
/*
* Copyright (c) 2023, Tim Flynn <[email protected]>
* Copyright (c) 2025, Gregory Bertilson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGC/Heap.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/AudioTrack.h>
#include <LibWeb/HTML/AudioTrackList.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/MediaSourceExtensions/MediaSource.h>
#include <LibWeb/MediaSourceExtensions/SourceBuffer.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(AudioTrack);
static GC::Ref<DOM::Event> create_event_for_element(HTMLElement& element, Utf16FlyString const& event_name)
{
return DOM::Event::create(event_name, HighResolutionTime::current_high_resolution_time(relevant_global_object(element)));
}
AudioTrack::AudioTrack(GC::Ref<HTMLMediaElement> media_element, Media::Track const& track)
: MediaTrackBase(media_element, track)
{
}
GC::Ref<AudioTrack> AudioTrack::create(GC::Ref<HTMLMediaElement> media_element, Media::Track const& track)
{
return GC::Heap::the().allocate<AudioTrack>(media_element, track);
}
AudioTrack::~AudioTrack() = default;
void AudioTrack::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_audio_track_lists);
}
void AudioTrack::add_audio_track_list(Badge<AudioTrackList>, AudioTrackList& audio_track_list)
{
m_audio_track_lists.append(audio_track_list);
}
void AudioTrack::remove_audio_track_list(Badge<AudioTrackList>, AudioTrackList& audio_track_list)
{
m_audio_track_lists.remove_first_matching([&](auto const& list) { return list.ptr() == &audio_track_list; });
}
// https://html.spec.whatwg.org/multipage/media.html#dom-audiotrack-enabled
void AudioTrack::set_enabled(bool enabled)
{
// 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 (m_enabled == enabled)
return;
m_enabled = enabled;
for (auto audio_track_list : m_audio_track_lists) {
// 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.
media_element().queue_a_media_element_task(GC::create_function(GC::Heap::the(), GC::captures(*this, audio_track_list), [](auto& track, auto& audio_track_list, HTMLMediaElement&) {
audio_track_list->dispatch_event(create_event_for_element(track.media_element(), HTML::EventNames::change));
}));
}
media_element().set_audio_track_enabled({}, this, enabled);
if (auto source_buffer = source_buffer_in_attached_media_source())
media_element().attached_media_source()->audio_track_enabled_changed({}, *source_buffer, enabled);
}
}