mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-05 18:52:56 +00:00
The main thread in the WebContent process is often busy with layout and running JavaScript. This can cause audio to sound jittery and crack. To avoid this behavior, we now drive audio on a secondary thread. Note: Browser on Serenity uses AudioServer, the connection for which is already handled on a secondary thread within LibAudio. So this only applies to Lagom. Rather than using LibThreading, our hands are tied to QThread for now. Internally, the Qt media objects use a QTimer, which is forbidden from running on a thread that is not a QThread (the debug console is spammed with messages pointing this out). Ideally, in the future AudioServer will be able to run for non-Serenity platforms, and most of this can be aligned with the Serenity implementation.
54 lines
1.9 KiB
C++
54 lines
1.9 KiB
C++
/*
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibAudio/Loader.h>
|
|
#include <LibAudio/Resampler.h>
|
|
#include <LibAudio/Sample.h>
|
|
#include <LibWeb/Platform/AudioCodecPlugin.h>
|
|
|
|
namespace Web::Platform {
|
|
|
|
static AudioCodecPlugin::AudioCodecPluginCreator s_creation_hook;
|
|
|
|
AudioCodecPlugin::AudioCodecPlugin() = default;
|
|
AudioCodecPlugin::~AudioCodecPlugin() = default;
|
|
|
|
void AudioCodecPlugin::install_creation_hook(AudioCodecPluginCreator creation_hook)
|
|
{
|
|
VERIFY(!s_creation_hook);
|
|
s_creation_hook = move(creation_hook);
|
|
}
|
|
|
|
ErrorOr<NonnullOwnPtr<AudioCodecPlugin>> AudioCodecPlugin::create(NonnullRefPtr<Audio::Loader> loader)
|
|
{
|
|
VERIFY(s_creation_hook);
|
|
return s_creation_hook(move(loader));
|
|
}
|
|
|
|
ErrorOr<FixedArray<Audio::Sample>> AudioCodecPlugin::read_samples_from_loader(Audio::Loader& loader, size_t samples_to_load, size_t device_sample_rate)
|
|
{
|
|
auto buffer_or_error = loader.get_more_samples(samples_to_load);
|
|
if (buffer_or_error.is_error()) {
|
|
dbgln("Error while loading samples: {}", buffer_or_error.error().description);
|
|
return Error::from_string_literal("Error while loading samples");
|
|
}
|
|
|
|
Audio::ResampleHelper<Audio::Sample> resampler(loader.sample_rate(), device_sample_rate);
|
|
return FixedArray<Audio::Sample>::create(resampler.resample(buffer_or_error.release_value()).span());
|
|
}
|
|
|
|
Duration AudioCodecPlugin::current_loader_position(Audio::Loader const& loader, size_t device_sample_rate)
|
|
{
|
|
auto samples_played = static_cast<double>(loader.loaded_samples());
|
|
auto sample_rate = static_cast<double>(loader.sample_rate());
|
|
|
|
auto source_to_device_ratio = sample_rate / static_cast<double>(device_sample_rate);
|
|
samples_played *= source_to_device_ratio;
|
|
|
|
return Duration::from_milliseconds(static_cast<i64>(samples_played / sample_rate * 1000.0));
|
|
}
|
|
|
|
}
|