Commit Graph

1107 Commits (8b6eae2d4fad188c6c82c9b41fd944b70fbe5d39)

Author SHA1 Message Date
Ryan C. Gordon 8ddddd36cd
audio: Deal with race conditions against default device changes.
This catches the case where we obtain a logical device while the default is
changing in another thread, so you accidentally end up with the previous
default physical device locked and returned from ObtainLogicalAudioDevice.
2024-02-15 14:13:26 -05:00
Ryan C. Gordon 5e1d9d19a4
audio: Don't use SDL_BindAudioStream inside SDL_OpenAudioDeviceStream().
It has a lot of complex validation and locking we want to avoid here.
2024-02-13 15:23:27 -05:00
Ryan C. Gordon bc984f78bf android: Remove blocking permission request code. Async only in SDL3!
(this actually still blocks at our internal points of usage, though, for
replacement at a later time.)
2024-02-13 12:06:51 -05:00
Sam Lantinga faeb2b1f22 Fixed warning C4204: nonstandard extension used: non-constant aggregate initializer when built with Visual Studio 2019 2024-02-07 13:16:35 -08:00
Sam Lantinga c79462dc50 Fixed Xcode build warnings 2024-02-06 12:29:44 -08:00
Frank Praznik 9462eec57b pipewire: Don't assert if the default capture device is a sink or vice versa
When no source devices are connected, the default source string can contain a sink name. If the default source and sink match, it will be caught as a sink device first and handled correctly, but if the default sink/source don't match, which happens when the sink is an HDMI output and the source is still an onboard audio chipset output name, an assert can result since the requested source device won't be flagged as a capture device. Rather than asserting, simply don't assign default devices that don't match the correct capabilities, as it's not an uncommon scenario and can be handled gracefully.

Additionally, if asserting is a no-op in release mode, sinks can be returned as sources and vice versa, which is incorrect.
2024-01-30 13:32:22 -05:00
Sam Lantinga b9ea2dde44 Added VisionOS as a supported target to the Xcode project
Also added SDL_PLATFORM_VISIONOS to the platform definitions and generally switched from TARGET_OS_* macros to SDL_PLATFORM_* macros.
2024-01-27 11:07:21 -08:00
Ryan C. Gordon 7a7875c904
wasapi: A few tweaks to buffer management.
These were previously applied, in a different form, in SDL2.

Reference Issue #8924.
2024-01-27 14:05:09 -05:00
Anonymous Maarten 31d133db40
Define SDL_PLATFORM_* macros instead of underscored ones (#8875) 2024-01-24 01:40:51 +00:00
Sam Lantinga 9fc1135e3b Fixed warning C4127: conditional expression is constant 2024-01-19 06:44:32 -08:00
Sam Lantinga fc0c774976 Renamed SDL_ThreadID() to SDL_GetCurrentThreadID()
Also renamed SDL_threadID to SDL_ThreadID and made it Uint64 for consistency with other ID types
2024-01-18 08:18:37 -08:00
Sam Lantinga a2665f54c8 Updated the atomic API for SDL 3.0 naming conventions
Fixes https://github.com/libsdl-org/SDL/issues/7388
2024-01-18 04:41:34 -08:00
Sam Lantinga e2080f74fd Fixed building with the joystick subsystem disabled 2024-01-17 09:24:04 -08:00
Sam Lantinga 6e1b11368d Removed SDL_CPUINFO_DISABLED
CPU info is a core part of the SDL API, and shouldn't be disabled
2024-01-17 09:24:04 -08:00
Sam Lantinga 9408299bad Set the number of audio devices to 0 if audio hasn't been initialized
This updates GetAudioDevices() to have the same behavior as SDL_GetJoysticks() where the return value will only be NULL if there is an error. Returning no devices will return a valid array containing NULL.
2024-01-16 12:15:13 -08:00
Sam Lantinga 5b3ee51c6c Updated copyright for 2024 2024-01-01 13:15:26 -08:00
Ryan C. Gordon f3d8a2def5
audio: Fixed resource leak in unlikely failure case during device add. 2023-12-09 15:41:45 -05:00
Sam Lantinga 226f8fde09 Fixed warning C28182: Dereferencing NULL pointer. 'streams[j]' contains the same NULL value as 'stream' did. 2023-12-05 09:31:13 -08:00
Sam Lantinga f3b0149756 Fixed warning C26451: Arithmetic overflow: Using operator '*' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '*' to avoid overflow (io.2). 2023-12-05 09:31:13 -08:00
Ryan C. Gordon 447b508a77
error: SDL's allocators now call SDL_OutOfMemory on error.
This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.

This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.

The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.

Fixes #8642.
2023-11-30 00:14:27 -05:00
Sam Lantinga 8043dad369 Fixed build warning 2023-11-16 10:28:54 -08:00
Ryan C. Gordon e923a458ea
audio: Protect against race conditions when closing a physical device.
This specifically deals with two threads closing the same device at the same
time, and a thread trying to reopen the device as it's still in process of
being closed. This can happen in normal usage if a device is disconnected:
the OS might send a disconnect event, while the device thread also attempts
to manage a disconnect as system calls start to report failure.

This effort is necessary because we have to release the device lock during
close to allow the device thread to unblock and cleanly shutdown. But the
good news is that all the places that call ClosePhysicalAudioDevice can now
safely hold the device lock on entry, and that one function will manage the
lock tapdancing.
2023-11-15 14:17:14 -05:00
Ryan C. Gordon 8fa0746d4a
audio: Fix postmix state when migrating to new default devices.
Otherwise buffers might not be allocated, etc.
2023-11-15 14:17:13 -05:00
Ryan C. Gordon a58af6d0d1
wasapi: Reference devices when proxying to management thread.
Otherwise, a disconnect/default change on another thread may cause the
device pointer to become invalid by the time the management thread runs the
task.
2023-11-14 16:20:14 -05:00
Ryan C. Gordon 0efb3d90e0
audio: removed a fixed FIXME comment. 2023-11-12 23:41:22 -05:00
Sylvain Becker 04b6b2979f
Re-add SDL_assert() with non boolean ptr syntax (#8530) 2023-11-11 12:28:24 +03:00
Ryan C. Gordon 89408a9705
wasapi: ResetWasapiDevice no longer blocks on management thread.
It just proxies all its necessary releases and frees to these without
blocking, and sets the appropriate fields to NULL so they can be used again
immediately, regardless of when the old stuff actually gets released.
2023-11-09 20:27:58 -05:00
Sylvain d8600f717e Pointer as bool (libsdl-org#7214) 2023-11-09 14:18:36 -08:00
Sam Lantinga fea2504a37 Prioritize the pipewire audio driver over ALSA
ALSA is used very rarely anymore and the pipewire ALSA emulation isn't as good as using pipewire directly. The Pulseaudio emulation is very good, and Pulseaudio is still commonly available on Linux systems, so we'll default to that first and fall back to pipewire if it's not available. We'll finally try ALSA, to handle very old systems.

Fixes https://github.com/libsdl-org/SDL/issues/7541
2023-11-07 22:02:27 -08:00
Sam Lantinga f3261fedcc Code cleanup now that SDL_bool is equivalent to a C boolean expression 2023-11-03 09:54:04 -07:00
Ryan C. Gordon 759cdf6159
audio: Fixed GetFirstAudioDeviceAdded().
It forgot to check for the type of device needed.
2023-11-01 00:39:51 -04:00
Ryan C. Gordon 0e614d9179
audio: Massive reworking on thread locking.
This cleans up a ton of race conditions, and starts moving towards something
we can use with Clang's -Wthread-safety (but that has a ways to go still).
2023-11-01 00:10:25 -04:00
Ryan C. Gordon 40fb76196c
audio: Don't let simplified audio streams bind to new devices.
This can happen if you close the stream's underlying device directly, which
removes the binding but doesn't destroy the object.

In this case, the stream remains valid until destroyed, but still should not
be able to be bound to a new device.
2023-10-31 10:52:50 -04:00
Ryan C. Gordon 24e3328cca
audio: Don't reset device ID counter on subsystem init/quit.
Otherwise you risk a buggy app holding an obsolete device ID that now refers
to a different device after reinit.
2023-10-31 10:50:37 -04:00
Ryan C. Gordon 9cb259e865
audio: Never SDL_PushEvent from anywhere but SDL_UpdateAudio().
Fixes some corner-case deadlocks.
2023-10-30 13:08:10 -04:00
Ryan C. Gordon 468c386686
wasapi: Handle disconnect notifications from the management thread, too.
These are also pretty heavyweight, don't do them from the notification
thread, which can deadlock everything.
2023-10-27 01:28:51 -04:00
Ryan C. Gordon ce3be02b48
wasapi: If device is marked as a zombie, don't try to resuscitate it. 2023-10-27 01:27:22 -04:00
Ryan C. Gordon 85923049a6
wasapi: Patched to compile. 2023-10-26 23:09:11 -04:00
Ryan C. Gordon 9bec57309c
wasapi: Proxy default device change handling to management thread.
This does a ton of work that can deadlock, because several crucial WASAPI
things that we want to do in response to this will block until the
notification callback has returned, so we can't call them from the handler
directly, or we'll be waiting until the thing that called us returns.
2023-10-26 23:03:27 -04:00
Ryan C. Gordon c45b5121ce
audio: Fixed potential race condition.
We need to check if the device is ready to close before releasing the lock,
in case other things are messing with the list of logical devices.
2023-10-26 23:03:27 -04:00
Ryan C. Gordon 899eb0d042 thread: Locking mutexes and rwlocks are now void functions.
Almost nothing checks these return values, and there's no reason a valid
lock should fail to operate. The cases where a lock isn't valid (it's a
bogus pointer, it was previously destroyed, a thread is unlocking a lock it
doesn't own, etc) are undefined behavior and always were, and should be
treated as an application bug.

Reference Issue #8096.
2023-10-26 08:57:34 -04:00
Frank Praznik 082ef41566 alsa: Fix crash from invalid handle pointer
ALSA expects handles to be of type ALSA_Device, and passing the handle for the default device as a plain string causes a crash as it attempts to deference the string contents itself as a pointer to a string.

Create immutable static ALSA_Device structs for the default devices and pass those as the handles. They are not placed in the hotplug list, and the audio layer doesn't attempt to free ALSA handles, so there is no need to worry about them being erroneously freed.
2023-10-25 19:37:43 -04:00
ds-sloth 1023d8ec84
SDL_n3dsaudio.c - don't risk leaving current_priority uninitialized
(cherry picked from commit 6623c87d0b7f6bae2ac16991aea53551899481b9)
2023-10-24 23:48:53 -04:00
ds-sloth 07171be596
SDL_n3dsaudio.h: use triple buffering
(cherry picked from commit 070f57820f401cb962e743bb3a4d12e346d6ed4b)
2023-10-24 23:48:17 -04:00
ds-sloth 6efe957159
SDL_n3dsaudio.c: separate mixer locks from audio device locks
(cherry picked from commit 62266dbd4fa79090025317a71473eb764b2e1abe)

(SDL3 audio backends don't have the LockDevice interfaces, so this just
ended up being a comment.)
2023-10-24 23:47:13 -04:00
Sam Lantinga 124a0050b6 Fixed warning: no previous prototype for function 'SDL_UpdateAudio' 2023-10-24 14:22:41 -07:00
Ryan C. Gordon 865dd04068
pulseaudio: Don't use a hash for device change detection.
Both strings are _right there_ for comparing, so we can just set a flag to
note the device definitely changed.

Also simplified string management further; hotplug thread now makes a copy
of the string before releasing the lock if there was a change event, so when
the lock releases further events don't see a NULL and assume it's a new
device, causing a lot of work to ripple out and decide nothing has changed,
until the system stabilizes again. Now, it just does the right thing once.
2023-10-24 00:36:30 -04:00
Sam Lantinga b8cc51875a Fixed build 2023-10-23 19:36:14 -07:00
Sam Lantinga 0413e21e54 Fixed audio device removed events for ALSA
We don't match dev->name by string, since we might use the same string for both capture and output devices. Instead use the device pointer itself as the handle.

@icculus, are we guaranteed the device pointer is valid in ALSA_OpenDevice()?
2023-10-23 19:05:27 -07:00
Sam Lantinga 5ba03d377a Revert "Fixed audio device removed events for ALSA"
This reverts commit e57fef8f0b.

We actually need to match on a unique handle
2023-10-23 19:05:27 -07:00