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>
|
2024-08-28 12:08:38 +02:00
|
|
|
#include <LibMedia/Audio/Loader.h>
|
2023-06-12 13:52:30 -04:00
|
|
|
#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>
|
2024-01-14 13:46:52 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2023-06-12 13:52:30 -04:00
|
|
|
#include <LibWeb/Platform/AudioCodecPlugin.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;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
AudioTrack::AudioTrack(JS::Realm& realm, GC::Ref<HTMLMediaElement> media_element, NonnullRefPtr<Audio::Loader> loader)
|
2023-06-12 13:52:30 -04:00
|
|
|
: PlatformObject(realm)
|
|
|
|
, m_media_element(media_element)
|
2023-06-20 12:43:04 -04:00
|
|
|
, m_audio_plugin(Platform::AudioCodecPlugin::create(move(loader)).release_value_but_fixme_should_propagate_errors())
|
2023-06-12 13:52:30 -04:00
|
|
|
{
|
2023-06-20 12:43:04 -04:00
|
|
|
m_audio_plugin->on_playback_position_updated = [this](auto position) {
|
2024-08-19 02:06:52 +02:00
|
|
|
if (auto* paintable = m_media_element->paintable())
|
2024-01-14 13:46:52 +01:00
|
|
|
paintable->set_needs_display();
|
2023-06-20 12:43:04 -04:00
|
|
|
|
|
|
|
auto playback_position = static_cast<double>(position.to_milliseconds()) / 1000.0;
|
|
|
|
m_media_element->set_current_playback_position(playback_position);
|
|
|
|
};
|
2024-04-11 23:08:46 +02:00
|
|
|
|
|
|
|
m_audio_plugin->on_decoder_error = [this](String error_message) {
|
2024-04-26 11:51:26 -04:00
|
|
|
m_media_element->set_decoder_error(move(error_message));
|
2024-04-11 23:08:46 +02:00
|
|
|
};
|
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
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioTrack);
|
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::play(Badge<HTMLAudioElement>)
|
|
|
|
{
|
|
|
|
m_audio_plugin->resume_playback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioTrack::pause(Badge<HTMLAudioElement>)
|
|
|
|
{
|
|
|
|
m_audio_plugin->pause_playback();
|
|
|
|
}
|
|
|
|
|
2024-07-16 23:45:44 -06:00
|
|
|
AK::Duration AudioTrack::duration()
|
2023-06-12 13:52:30 -04:00
|
|
|
{
|
2023-06-20 12:43:04 -04:00
|
|
|
return m_audio_plugin->duration();
|
2023-06-12 13:52:30 -04:00
|
|
|
}
|
|
|
|
|
2023-06-14 10:12:46 -04:00
|
|
|
void AudioTrack::seek(double position, MediaSeekMode seek_mode)
|
|
|
|
{
|
|
|
|
// FIXME: Implement seeking mode.
|
|
|
|
(void)seek_mode;
|
|
|
|
|
2023-06-20 12:43:04 -04:00
|
|
|
m_audio_plugin->seek(position);
|
2023-06-14 10:12:46 -04:00
|
|
|
}
|
|
|
|
|
2023-06-14 13:07:09 -04:00
|
|
|
void AudioTrack::update_volume()
|
|
|
|
{
|
|
|
|
m_audio_plugin->set_volume(m_media_element->effective_media_volume());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|