Commit Graph

41 Commits

Author SHA1 Message Date
Jonathan Gamble
baefb51902 LibWeb: Add Media Capture and Stream APIs 2026-04-21 16:40:46 -05:00
Andreas Kling
b23aa38546 AK: Adopt mimalloc v2 as main allocator
Use mimalloc for Ladybird-owned allocations without overriding malloc().
Route kmalloc(), kcalloc(), krealloc(), and kfree() through mimalloc,
and put the embedded Rust crates on the same allocator via a shared
shim in AK/kmalloc.cpp.

This also lets us drop kfree_sized(), since it no longer used its size
argument. StringData, Utf16StringData, JS object storage, Rust error
strings, and the CoreAudio playback helpers can all free their AK-backed
storage with plain kfree().

Sanitizer builds still use the system allocator. LeakSanitizer does not
reliably trace references stored in mimalloc-managed AK containers, so
static caches and other long-lived roots can look leaked. Pass the old
size into the Rust realloc shim so aligned fallback reallocations can
move posix_memalign-backed blocks safely.

Static builds still need a little linker help. macOS app binaries need
the Rust allocator entry points forced in from liblagom-ak.a, while
static ELF links can pull in identical allocator shim definitions from
multiple Rust staticlibs. Keep the Apple -u flags and allow those
duplicate shim symbols for LibJS and LibRegex links on Linux and BSD.
2026-04-08 09:57:53 +02:00
Zaggy1024
1467127d35 LibMedia+LibWeb: Disable audio output in headless mode
Audio output on macOS was consuming Core Audio resources until the
PlaybackStream creation took well over the timeout for some tests.
This was observed in media-source-buffered.html, where it would time
out due to the long-running callback on the main thread to create the
PlaybackStream for AudioMixingSink.

However, the AudioUnit init should definitely not be blocking the main
thread, so I've added a FIXME there.
2026-04-01 02:54:22 -05:00
Zaggy1024
792771bfa0 LibMedia: Avoid unnecessary atomic refs in PulseAudio streams 2026-03-30 20:57:04 -05:00
Zaggy1024
f1c05aefde LibMedia: Don't keep PlaybackStreamPulseAudio alive in its thread
As soon as the promise is resolved, we need to unref it, so move it to
the deferred_invoke() callback. This allows PulseAudio streams to be
released properly and gives TestPlaybackStream a chance to pass instead
of timing out.
2026-03-30 20:57:04 -05:00
Zaggy1024
6e8f6df872 LibMedia: Skip PlaybackStream rejection if the main event loop is dead
This was causing a flake on CI due to the race between the teardown of
the event loop and the rejection of the promise from another thread.

Regressed in 39d865b.
2026-03-30 20:57:04 -05:00
Zaggy1024
39d865b403 LibMedia: Provide new PlaybackStreams through promises
This allows us to avoid returning a PlaybackStream in cases where the
async initialization fails.

This is a step towards more graceful fallbacks when audio fails in
AudioMixingSink.
2026-03-21 23:11:47 -05:00
Zaggy1024
e2635af2ed Everywhere: Move the thread name parameter for Thread constructors
The name parameter formats very poorly when a lambda is passed to
Thread, so let's instead put it first now that all Threads are named.
2026-01-26 15:51:46 -06:00
Zaggy1024
d2a1d727ac Everywhere: Give unnamed threads names 2026-01-26 15:51:46 -06:00
Zaggy1024
2867f87592 Everywhere: Shorten existing thread names to 15 characters or less
pthread_setname_np only accepts 16-byte null-terminated strings, so any
names longer than this will need to be truncated.
2026-01-26 15:51:46 -06:00
Jonathan Gamble
d821508775 LibMedia: Fix byte/frame mismatch in conversion to buffer size
Note that `pa_buffer_attr.tlength` is in bytes, not frames (see
`/usr/include/pulse/def.h`). I think the previous code asked for a
buffer that is 1/8 what the latency calculation wants (for float32
stereo), resulting in too frequent write callbacks, higher wakeup/CPU
churn, higher risk of crackles under load.
2026-01-17 02:20:20 -06:00
Zaggy1024
732661bce1 LibMedia: Avoid a UAF on PlaybackStreamPulseAudio in task handlers
There's no guarantee that the PlaybackStream will always live as long
as the control thread, but we don't actually need the PlaybackStream in
any of the tasks.

This is an attempt to fix a null deref in test-web, but the issue is
very rare, so it is unconfirmed. The call to InternalState->exit()
should most likely prevent such a UAF, but it's not correct to hold
a weak reference to the PlaybackStream anyway.

Since the UAF happens either rarely or not at all due to the mutexes
involved, testing this doesn't appear to be possible.
2025-12-29 19:58:22 -06:00
R-Goc
1f3e20cebf LibMedia: Add a WASAPI playback stream for Windows
Implement PlaybackStream using WASAPI. The design is similar to
PlaybackStreamAudioUnit in that it uses a task queue. A high priority
thread is used to render the stream. All the stream controls save for
the exit being requested which happens on destruction of the stream are
managed by the render thread.

Due to the design of the windows audio mixer the audio we receive must
be resampled to match the sample rate of the mixer. We use a float based
interleaved PCM stream which matches both our existing code and the
audio mixer which internally usues floats.

Having to use a mutex around a queue for the task queue is suboptimal,
in a future PR a MPSC queue could be added to AK and used instead.
2025-12-29 18:02:02 -06:00
Zaggy1024
9d6bc89ed7 LibMedia: Remove the now-unused audio loader plugins 2025-12-15 18:03:03 -06:00
Zaggy1024
0f5bd00e3a LibMedia: Give ChannelMap an invalid state
By doing this, we can default-initialize it and not have to rely on
Optional with its extra size.
2025-12-15 18:03:03 -06:00
Zaggy1024
65c0be66e4 LibMedia: Implement audio conversion in AudioDataProvider 2025-12-13 08:58:26 +01:00
Zaggy1024
c75284591f LibMedia: Make PlaybackStream use the output's sample specification
Instead of specifying the sample rate, channel count/map, etc. to the
PlaybackStream, we'll now use the output device's sample specification
whenever possible. If necessary, the stream will fall back to sane
default.

This hugely simplifies AudioMixingSink, since it no longer has to take
care of reinitializing the stream with a new sample specification when
it encounters a track with a higher sample rate or more channels. We
wouldn't be likely to benefit from this anyway, since it turns out that
at least Windows's virtual surround doesn't work through WASAPI at all,
and WASAPI likely wouldn't support downmixing.

This commit breaks playback of audio files that don't match the system
default audio device's sample rate and channel count. The next commit
introduces a converter into the pipeline to allow mixing of any sample
specification.
2025-12-13 08:58:26 +01:00
Zaggy1024
40d1088b5c LibMedia: Avoid unnecessary NNRP copy in PulseAudio stream init 2025-12-13 08:58:26 +01:00
Zaggy1024
5c9a34d84a LibMedia: Only use floats for audio data callbacks
We were already assuming that our streams were using floats, we may as
well hardcode this. If we ever encounter a platform API that doesn't
support or convert from float, we can always bring this back.

Also, since we don't support big-endian systems, remove that check in
PulseAudioWrappers.
2025-12-13 08:58:26 +01:00
Zaggy1024
d5e980aa61 LibMedia: Drop the Oboe audio stream implementation for Android
The Android build is likely bitrotted, and breaking changes need to be
made to the PlaybackStream interface. This should be reverted and the
implementation updated if the Android build is maintained again.
2025-12-13 08:58:26 +01:00
Zaggy1024
19ccec2c10 LibMedia: Create a channel map for audio blocks from decoders
Audio blocks now contain a sample specification with the sample rate
and channel map for the audio data they contain. This will facilitate
conversion from one sample specification to another in order to allow
playback on devices with more or less speakers than the audio data
contains.
2025-12-13 08:58:26 +01:00
Zaggy1024
6b4f98392b LibMedia: Remove the forward declaration of ConnectionToServer
This doesn't exist anymore.
2025-12-13 08:58:26 +01:00
Zaggy1024
fab987d937 LibMedia: Ensure that PulseAudioContext's atexit() is only called once 2025-11-24 15:09:34 -06:00
Zaggy1024
13cf0e72d8 LibMedia: Stop using threading-unsafe Weakable for PulseAudioContext
We can't control whether the instantiation mutex is held when
~Weakable() is called, so we need to implement this via a static raw
pointer instead to ensure that all operations on it are effectively
atomic.
2025-11-24 15:09:34 -06:00
Zaggy1024
3e485ba51c LibMedia: Move the PulseAudioStream constructor out of line 2025-11-24 15:09:34 -06:00
Zaggy1024
2c73848f78 LibMedia: Remove an explicit cast when constructing PulseAudioStream 2025-11-24 15:09:34 -06:00
Zaggy1024
b8bc129a3c LibMedia: Rename PulseAudioContext::instance() to the() 2025-11-24 15:09:34 -06:00
Zaggy1024
27742ef26d LibMedia+LibWeb: Never return errors when getting PlaybackStream time
We should be fine to just fall back to zero or the last returned value
if we encounter an error in PlaybackStream::total_time_played(), and
this also simplifies the using code.
2025-10-27 17:28:49 -07:00
Zaggy1024
2aaf53bd2c Everywhere: Use a forward declaration for pointers to Threading::Thread 2025-09-22 17:28:21 -05:00
Zaggy1024
7811c2e71b LibMedia: Ensure that the PulseAudioContext atexit() call succeeds 2025-09-04 19:37:56 +02:00
Zaggy1024
33e97fa5d7 LibMedia: Note that PlaybackStream callbacks cannot run simultaneously
This is a requirement of implementations of PlaybackStream, so we
should have a little comment about it. Otherwise, we may encounter data
races later.
2025-09-04 13:29:18 -04:00
ayeteadoe
8150fb4cbb LibMedia: Enable EXPLICIT_SYMBOL_EXPORT 2025-08-24 12:58:27 -06:00
ayeteadoe
dbba6c0df9 LibWeb: Enable in Windows CI 2025-06-30 10:50:36 -06:00
Timothy Flynn
ad4634d0ed LibMedia: Use a simple locked vector to handle audio commands on macOS
The SharedSingleProducerCircularQueue used here has dubious value, This
queue is used to pass commands to the audio thread, such as play/pause/
seek/volume change/etc. We can make do with a simple locked vector, as
we were blocking to enqueue tasks anyways. We can also use an atomic
bool to tell the audio thread when it needs to take a lock on the task
queue, to keep the thread lock-free most of the time.
2025-05-25 08:45:50 -04:00
Timothy Flynn
7280ed6312 Meta: Enforce newlines around namespaces
This has come up several times during code review, so let's just enforce
it using a new clang-format 20 option.
2025-05-14 02:01:59 -06:00
Luke Wilde
4df2de2f20 LibMedia/Audio: Use duration in the container if stream doesn't have one 2025-03-13 19:33:44 +01:00
Luke Wilde
3412935a62 LibMedia: Move FFmpegIOContext into it's own file
This allows it to be reused for video.
2025-03-13 19:33:44 +01:00
Timothy Flynn
dfa727a4d4 LibMedia: Remove preprocessor chain when initializing audio streams
Instead of having to duplicate the audio stream backend conditions, just
define PlaybackStream::create in each audio backend implementation file.
We provide a weak definition in PlaybackStream.cpp as the fallback.
2024-12-25 12:00:43 +01:00
Jonne Ransijn
4d38e04e48 LibMedia: Remove LibMedia::Audio::LoaderError
This class was used as little more than a wrapper around `Error`,
so we might as well use `Error` in the first place.
2024-11-22 12:43:57 +01:00
Pavel Shliak
b60cb699a9 LibMedia: Clean up #include directives
This change aims to improve the speed of incremental builds.
2024-11-21 14:08:33 +01:00
Timothy Flynn
93712b24bf Everywhere: Hoist the Libraries folder to the top-level 2024-11-10 12:50:45 +01:00