mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +00:00
Ladybird+LibWeb+WebConent: Drive audio in Ladybird off the main thread
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.
This commit is contained in:
parent
0fd35b4dd8
commit
1c4dd0caad
10 changed files with 383 additions and 205 deletions
|
@ -4,25 +4,51 @@
|
|||
* 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 Function<ErrorOr<NonnullOwnPtr<AudioCodecPlugin>>()> s_creation_hook;
|
||||
static AudioCodecPlugin::AudioCodecPluginCreator s_creation_hook;
|
||||
|
||||
AudioCodecPlugin::AudioCodecPlugin() = default;
|
||||
AudioCodecPlugin::~AudioCodecPlugin() = default;
|
||||
|
||||
void AudioCodecPlugin::install_creation_hook(Function<ErrorOr<NonnullOwnPtr<AudioCodecPlugin>>()> creation_hook)
|
||||
void AudioCodecPlugin::install_creation_hook(AudioCodecPluginCreator creation_hook)
|
||||
{
|
||||
VERIFY(!s_creation_hook);
|
||||
s_creation_hook = move(creation_hook);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<AudioCodecPlugin>> AudioCodecPlugin::create()
|
||||
ErrorOr<NonnullOwnPtr<AudioCodecPlugin>> AudioCodecPlugin::create(NonnullRefPtr<Audio::Loader> loader)
|
||||
{
|
||||
VERIFY(s_creation_hook);
|
||||
return 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue