2023-06-12 13:52:30 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/IDAllocator.h>
|
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
|
#include <LibWeb/Bindings/AudioTrackPrototype.h>
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.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/Layout/Node.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(AudioTrack);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2023-06-12 13:52:30 -04:00
|
|
|
static IDAllocator s_audio_track_id_allocator;
|
|
|
|
|
|
2025-09-24 14:08:34 -05:00
|
|
|
AudioTrack::AudioTrack(JS::Realm& realm, GC::Ref<HTMLMediaElement> media_element, Media::Track const& track)
|
2023-06-12 13:52:30 -04:00
|
|
|
: PlatformObject(realm)
|
|
|
|
|
, m_media_element(media_element)
|
2025-09-24 14:08:34 -05:00
|
|
|
, m_track_in_playback_manager(track)
|
2023-06-12 13:52:30 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioTrack::~AudioTrack()
|
|
|
|
|
{
|
|
|
|
|
auto id = m_id.to_number<int>();
|
|
|
|
|
VERIFY(id.has_value());
|
|
|
|
|
|
|
|
|
|
s_audio_track_id_allocator.deallocate(id.value());
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void AudioTrack::initialize(JS::Realm& realm)
|
2023-06-12 13:52:30 -04:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioTrack);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2023-06-12 13:52:30 -04:00
|
|
|
|
|
|
|
|
auto id = s_audio_track_id_allocator.allocate();
|
2024-10-14 10:05:01 +02:00
|
|
|
m_id = String::number(id);
|
2023-06-12 13:52:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioTrack::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_media_element);
|
|
|
|
|
visitor.visit(m_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;
|
|
|
|
|
|
|
|
|
|
if (m_audio_track_list) {
|
|
|
|
|
// 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.
|
|
|
|
|
m_media_element->queue_a_media_element_task([this]() {
|
2023-08-13 13:05:26 +02:00
|
|
|
m_audio_track_list->dispatch_event(DOM::Event::create(realm(), HTML::EventNames::change));
|
2023-06-12 13:52:30 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_enabled = enabled;
|
2025-09-24 14:08:34 -05:00
|
|
|
m_media_element->set_audio_track_enabled({}, this, enabled);
|
2023-06-12 13:52:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|