Commit graph

18 commits

Author SHA1 Message Date
Andreas Kling
aad293dacc LibMedia: Use direct references to main event loops
Pass direct Core::EventLoop references through media producer and
playback plumbing that posts work back to the main thread. The browser
process main loops stay alive for the process lifetime, so these paths
no longer need weak event loop references.

Update LibMedia tests to pass their stack event loop directly to the
producer helpers.
2026-06-05 09:18:39 +02:00
Zaggy1024
a3177c5f71 LibMedia: Give PlaybackStream a way to wake up after underrun
This gives us a cheap way to wake up all PlaybackStream implementations
when they are sleeping due to an underrun. The new function is meant to
be very cheap so that it can be unconditionally called when new data is
ready to be written.

This will allow the audio sink to rely on streams to track the time in
written audio frames instead of writing silence when data isn't ready.

The WASAPI implementation was actually polling for new data every 10ms
after hitting an underrun, so now it drains the buffers and stops when
entering the underrun-paused state.
2026-05-13 02:05:35 -05:00
R-Goc
02bb892d7a LibThreading/LibSync: Split out sync primitives
This commit splits out synchronization primitives from LibThreading into
LibSync. This is because LibThreading depends on LibCore, while LibCore
needs the synchronization primitives from LibThreading. This worked
while they were header only, but when I tried to add an implementation
file it ran into the circular dependency. To abstract away the pthread
implementation using cpp files is necessary so the synchronization
primitives were moved to a separate library.
2026-05-08 18:58:35 -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
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
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
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
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
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
Timothy Flynn
93712b24bf Everywhere: Hoist the Libraries folder to the top-level 2024-11-10 12:50:45 +01:00
Renamed from Userland/Libraries/LibMedia/Audio/PlaybackStreamPulseAudio.cpp (Browse further)