1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +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:
Timothy Flynn 2023-06-20 12:43:04 -04:00 committed by Andreas Kling
parent 0fd35b4dd8
commit 1c4dd0caad
10 changed files with 383 additions and 205 deletions

View file

@ -9,28 +9,31 @@
#include <AK/FixedArray.h>
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/NonnullRefPtr.h>
#include <LibAudio/Forward.h>
namespace Web::Platform {
class AudioCodecPlugin {
public:
static void install_creation_hook(Function<ErrorOr<NonnullOwnPtr<AudioCodecPlugin>>()>);
static ErrorOr<NonnullOwnPtr<AudioCodecPlugin>> create();
using AudioCodecPluginCreator = Function<ErrorOr<NonnullOwnPtr<AudioCodecPlugin>>(NonnullRefPtr<Audio::Loader>)>;
static void install_creation_hook(AudioCodecPluginCreator);
static ErrorOr<NonnullOwnPtr<AudioCodecPlugin>> create(NonnullRefPtr<Audio::Loader>);
virtual ~AudioCodecPlugin();
virtual size_t device_sample_rate() = 0;
virtual void enqueue_samples(FixedArray<Audio::Sample>) = 0;
virtual size_t remaining_samples() const = 0;
static ErrorOr<FixedArray<Audio::Sample>> read_samples_from_loader(Audio::Loader&, size_t samples_to_load, size_t device_sample_rate);
static Duration current_loader_position(Audio::Loader const&, size_t device_sample_rate);
virtual void resume_playback() = 0;
virtual void pause_playback() = 0;
virtual void playback_ended() = 0;
virtual void set_volume(double) = 0;
virtual void seek(double) = 0;
virtual Duration duration() = 0;
Function<void(Duration)> on_playback_position_updated;
protected:
AudioCodecPlugin();