forked from Green-Sky/tomato
399bc709b build-scripts.pl: Added add-source-to-projects.pl ac6827187 Visual-WinRT: dos2unix the project files to match other Visual Studio targets. 34719cba9 Fixed crash in hid_init() if the HIDDeviceManager isn't available 2e92e94eb Make sure we update device->sample_frames in SDL_AudioDeviceFormatChangedAlreadyLocked() 9964e5c5b wayland: Don't retrieve the drag offer strings with every pointer motion event bac7eeaaa Added missing include a541e2ac1 audio: Change a few SDL_memcpy calls to SDL_copyp. 54125c140 audio: Only update bound audiostreams' formats when necessary. e0b0f9a36 testaudio: Fix mouseover testing. 2f3deec24 wayland: Don't process drag & drop events from surfaces not owned by SDL 42bdced05 events: Log file drop position events and print the pointer coordinates c10d93d3a wayland: Replace magic constant with define 500852153 emscripten: Restore compatibility with existing emsdk releases. 953b55dd6 Use EM_ASM_PTR when the return value is a pointer a4541a255 audio: SDL_GetAudioStreamQueued now returns bytes, not frames. 703aefbce Sync SDL3 wiki -> header 99421b64d linux: Add portal drag and drop 952c5059b Remove stray  eebd5d18a linux: Handle upower's UP_DEVICE_STATE_PENDING_CHARGE, PENDING_DISCHARGE f8fdb20d8 audio: Destroy all existing SDL_AudioStreams on shutdown. 62d445997 audio: Removed declarations of functions that don't exist anymore. b656720bc loopwave: Use SDL_GetAudioStreamQueued() for more accurate results. 34b931f7e audio: Added SDL_GetAudioStreamQueued 23206b9e3 audio: Added SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED c7e6d7a1f audio: Changed debug logging output. 87ec6acf2 audio: Added a FIXME ac88ffb7e audio: don't allocate buffer in SDL_SetAudioPostmixCallback for NULL callback. 2a950f6ae audio: Replace some SDL_memcpy calls with SDL_copyp. 0dc0434a3 audio: Fixed race condition in subsystem shutdown. 23f60203a audio: precalculate if we can use simple copies instead of the full mixer. 36b0f1141 audio: Optimize setting device formats during audio thread iteration. 4c3e84897 testspriteminimal: make standalone by embedding icon.bmp 2a01f9dcb tests: plug leaks when running with --trackmem f42bbeca2 SDL_test: track stack frames of allocations on Windows 12c0be028 SDL_test: clear text cache on exit event b4bfb1831 SDL_test: free state before logging allocations 248b1edd3 SDL_test: destroy windows in SDL_CommonQuit 98da2dd30 SDL_test: don't warn about expected allocations when running with --trackmem 6a381567b Support audio rate conversion up to 384KHz b2b548a1f Don't hang if IAudioRenderClient_GetBuffer() fails indefinitely a3a5e1728 Fixed build warning '=': conversion from 'Uint32' to 'Uint16', possible loss of data 6d3e21c27 Fixed android build warnings fca2f5318 Fixed warning: this function declaration is not a prototype a72dfa6a5 Fixed sensor timestamp units for third-party PS5 controllers f6756047a Fixed error: array subscript 2 is above array bounds of ‘const Uint8[2]’ 7059a55cc Fixed sensor timestamp calculation for third-party PS5 controllers c0443e5d1 Fixed crash in SDL_IMMDevice_FindByDevID() fde8499f6 Use around 20ms for the audio buffer size e5739d7d1 video: Remove SDL_GetFocusWindow() 39c2f9737 Fix NULL dereference in SDL_OpenAudio 9a23d0e3f Added new audio files to the Xcode project a62e62f97 Refactored SDL_audiocvt.c 31229fd47 include: Added a note about SDL's iOS app delegate functions. 65aaf3a9a x11: Always update clipboard owner f622f21e6 Fixed build 5774c9638 Prefer hidraw over libusb when libusb whitelisting is not enabled 9301f7ace hidapi/libusb: only enumerate each interface once 859dc14ad Replaced SDL_GetGamepadBindForAxis() and SDL_GetGamepadBindForButton() with SDL_GetGamepadBindings() 9e50048ab Revert "Removed SDL_GamepadBinding from the API" 9f17d1a9d Don't reference the same function in "see also" 86505ea63 fix SDL_AudioStreamCallback documentation d885d5c31 Sync SDL3 wiki -> header 2f43f7bc5 audio: Allow querying of device buffer size. cf9572113 audio: Added a hint to let apps force device buffer size. 47d8c77c6 audio: Choose better default sample frame counts. 8b26e95f9 audio: Change SDL_AudioStreamCallback 9da34e8fb docs: Updated README-emscripten.md. fd1c54a00 detect fanatec steering wheels cb4414608 docs: Whoops, this got added by the wiki bridge by accident! cd633b9a8 Renamed SDL_IsAudioDevicePaused() to SDL_AudioDevicePaused() c6cad07fa Sync SDL3 wiki -> header a6e52f9e4 Sync SDL3 wiki -> header 2de2e9d03 Fix flickering of window when using desktop-fullscreen and borderless window on multiple monitors on Linux. Closes #8186. 723835d16 Windows: fix for client rect resizing larger each time we came from exclusive fullscreen -> windowed on a monitor with HiDPI set. The problem was we were using the monitor DPI rather than the window DPI so AdjustWindowRectExForDpi was giving us an incorrect size which would be too large for the client rect. Closes #8237. ce27363df wikiheaders: Sort undocumented functions. e22282b09 Added README about transparent windows in Win32 1d1c6e630 Turn off COREAUDIO debug logging by default 52efefca0 wayland: Fix drag offer leak 3a992af44 audio: Added a postmix callback to logical devices. 7207bdce5 render: Enable clipping for zero-sized rectangles 22d81fb3e cmake: use MSVC_RUNTIME_LIBRARY to force MT a2e17852d cmake: make sure SDL_GetPrefPath is run before testfilesystem 2fb266e0a ci: run tests in parallel ad1313e75 testaudio: Patched to compile. 5747ddc01 testaudio: Clean up some messy memory management. fafbea1ce audio: Move internal float32 mixing to a simplified function 116b0ec97 include: minor tweak to audio API documentation fb1377035 include: Replaced old Bugzilla URL. 38c8fc05c audio: Remove ChooseMixStrategy. b00cbd76a wikiheaders.pl: create Unsupported.md file with list of functions undocumented in either the headers or the wiki 37e1fc3b5 wayland: Ensure that the toplevel window is recreated when switching decoration modes f2ca9a615 Added SDL_AUDIO_FRAMESIZE 53122593f Added SDL_AUDIO_BYTESIZE 544351c98 Sync SDL3 wiki -> header 2e7d2b94e Clarify that SDL_BlitSurface() ignores the width and height in dstrect a2c1984d3 Detect Simagic wheel bases as wheels (#8198) 1d8dfbb22 avoid type redefinition errors after PR/8181 266b91d2f Detect Logitech G923 Playstation as wheel G923 have two different versions - Xbox version is already present in the wheel list, but not the PS version. cde67ea49 Detect Logitech PRO Racing Wheel for Xbox (PC mode) as wheel Logitech PRO Racing Wheel have two different versions - for Playstation and Xbox. Vendor + Product ID for Playstation version already present in SDL sources, but not an Xbox version 3a932141e Restore audio format binary compatibility with SDL 2.0 e85206ffd wikiheaders.pl: add --rev= option to pass revision string 233789b0d Audio types have the same naming convention as other SDL endian types, e.g. [S|U][BITS][LE|BE] 36b5f3e35 Sync SDL3 wiki -> header 0e552761b Renamed AudioStreamSpeed to AudioStreamFrequencyRatio 47bcb078f Fixed some incorrect SDL_AUDIO_F32 uses 2833f2e7b Fixed OOB access in audio_convertAccuracy test 8387fae69 Sync SDL3 wiki -> header 832181345 docs: Add note about Wayland application icons 825d34475 Make sure that the same timestamp is used for all PS5 events from the same packet 9c1430324 Removed SDL_dataqueue 28b28bd8f Added audio_formatChange test a59152688 Try and avoid overflow when handling very large audio streams 5394a805f Improved testaudiostreamdynamicresample e55844274 Added SDL_(Get|Set)AudioStreamSpeed 43c3c5736 Track the formats of data in an SDL_AudioStream 337fed3df Tweaked ResampleFrame_SSE Use _mm_unpack(lo|hi)_ps instead of _mm_shuffle_ps fd7cd91dc audio: Mix multiple streams in float32 to prevent clipping. 9097573e3 audio: Choose a mixing strategy on each iteration. bbe2e012a Don't provide the SDL3 header path c17a35f09 Fixed typo 4f72255eb Fixed README.md link e0ab59754 Simplified SDL_main.h migration notes d44bde61e Added SDL migration information to the top level README.md 6ff31e10c metal: Add hint to select low power device instead of the default one (#8182) 8a8aed477 Make sure that we process touch events that position the mouse f84c87f20 Sync SDL3 wiki -> header a7eea9997 macOS: Don't raise the parent top-level window when raising a child window, only raise the child window to the top of the parent a5e721479 Add SDL_WINDOW_NOT_FOCUSABLE flag to set that the window should not be able to gain key focus b385dc3b6 n3dsaudio: Patched to compile. 4e0c7c91f audio: PlayDevice() should return an error code. a94d724f1 wayland: add SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL da5d93d3d wayland: don't define SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_* macro's f002f7d12 ci: build emscripten with Debug buid type 3699b12ed audio: Fixed some "is_*" variables to be cleaner and/or more specific. 2471d8cc2 audio: Fixed logic error in SDL_OpenAudioDeviceStream. 1b03a2430 testsurround: fix order of arguments of callback 82db2b58f Renamed audio stream callback and moved the userdata parameter first 5bdad5210 Sync SDL3 wiki -> header 58c859f64 audio: Rename SDL_GetAudioStreamBinding to SDL_GetAudioStreamDevice. efd2023a7 audio: Fixed documentation. 1e775e0ee audio: Replace SDL_CreateAndBindAudioStream with SDL_OpenAudioDeviceStream. bd088c2f9 Revert "Clarify whether an audio function expects a physical or logical device ID" 82e481b52 Added --randmem test parameter ea68bb802 Add some additional checks to audio_convertAudio f8286df16 Fixed ResampleFrame_SSE doing unnecessary work b1d63be53 Fixed audio_resampleLoss test c191d6c30 Better Win32 transparent window support 923d612ca hidapi: sync macOS code with mainstream. 363f4fa9c avoid type redefinition errors after commit ee806597b9. 615824a80 Updated documentation now that SDL_GetAudioDevices() has been split into separate functions for output and capture devices 506a133d8 Clarify whether an audio function expects a physical or logical device ID 3b1d1e4e3 hidapi: sync the hidraw changes with mainstream f617918e0 cmake: check linkage to libusb too, instead of libusb.h presence only. 041dbd6b5 Fixed GetResamplerAvailableOutputFrames Non-euclidean division is a pain b49d0a607 x11: Avoid including full Vulkan headers. 4d2f9f3a3 yuv_rgb: Comment out unused code. 3c3486e2a wayland: Don't include full Vulkan headers when not necessary. f066bbe98 x11: Don't include system headers twice. d86d02bbb updated dynapi after SDL_GDKGetDefaultUser addition 4355f9cec Fixed warning C4389: '!=': signed/unsigned mismatch 5755de07a Fixed build warnings 0f80d47bb Fixed thread-safety warning ee806597b Removed SDL_vulkan_internal.h from SDL_sysvideo.h 34860b932 Fixed testautomation --filter pixels_allocFreeFormat 6f8a6a31c gdk: GetBasePath should be a UTF8 version of Win32 GetBasePath e30e5c77e Sync SDL3 wiki -> header c0cd8c814 gdk: Add SDL_GDKGetDefaultUser, SDL_GetPrefPath implementation 106abce69 Refactored GetAudioStreamDataInternal buffer handling The final conversion step should now always go straight into the output buffer. e44f54ec5 Avoid using hex-floats 5b696996c Added ResampleFrame_SSE 958b3cfae Tweaked and enabled audio_convertAudio test 7dbb9b65b audio_convertAccuracy: Shuffle the data in case of a bad SIMD implementation f6a4080ff audio_resampleLoss: Add support for multiple channels 4f894e748 audio_resampleLoss: SDL_GetAudioStreamData now returns the correct length ab83f75bb Make sure GetAudioStreamDataInternal is called with a valid length 6a73f74b6 Rebuild full ResamplerFilter (left wing + right wing) at runtime 0c15ce006 Add a missing int cast b74ee86b1 Optimized ResampleAudio, with special cases for 1 and 2 channels This would also benefit from some SIMD, since it's just a bunch of multiply-adds fba6e1e3d Removed ResamplerFilterDifference It takes 1 extra multiply to calculate the correct interpolation, but I think the improvement in cache locality (and binary size) outweighs that. 9f7a22fa4 Removed 64-bit handling from AudioConvertByteswap 1f5327a9f Removed future_buffer, left_padding, and right_padding from SDL_AudioStream 71ad52d6d Lowered SDL_GetAudioStreamData to 32 KB No particular reason for this number, but 1 MB was a bit silly 69aec8c91 Fixed the report format for the Razer Wolverine V2 Pro 7c2669c9d Accept key events from any source 1e9d31448 Updated to Android minSdkVersion 19 and targetSdkVersion 34 8924d0d92 Added missing function prototype for SDL_WriteS64BE() 845f3c745 Fixed mismatch between stdlib calloc() and SDL free() fb7921173 emscriptenaudio: Fire the capture silence_callback at an interval. 5191b2054 emscriptenaudio: Don't bother undefining things about to be unreachable. fd75a4ca0 emscriptenaudio: Deal with blocked audio devices better. 981b8a337 emscriptenaudio: Remove unnecessary functions. c7588e426 Transparent window for Win32 + OpenGL (#8143) f9581178d cmake: fixed a typo. e6c878824 Fixed ResampleAudio interpolation factor calculation 498363863 Misc audio tweaks/cleanup 72d9d53de Invert the inner ResampleAudio loops to avoid doing unnecessary work 88123a510 The history buffer should always have the maximum possible padding frames 96e47f165 Clamp results of GetResampler(AvailableOutput|NeededInput)Frames d2b9c8b80 Fixed maths in testaudiostreamdynamicresample (and just show the actual scale) 14e38b17d Removed assertions from inner ResampleAudio loop 9d413dfdc The history buffer doesn't need to be so large 2788e848f Allow resampling less than 1 frame of input 383084e0a Pre-calculate resampling rate, and use it instead of .freq in most places 40a6a445c Update resample_offset inside ResampleAudio 47fea7f06 Used fixed-point arithmetic in ResampleAudio 7bb4e806e Clear resample_offset in SDL_ClearAudioStream, not SetAudioStreamFormat Not entirely sure if ClearAudioStream is the right place, but SetAudioStreamFormat was the wrong place b9541b9ea Improved ResampleAudio * filterindex2 was off-by-one * Generate ResamplerFilter using doubles * Transpose ResamplerFilter to improve access patterns cdaa19869 Track offset within the current sample when resampling d60ebb06d mouse: Ensure that the dummy default cursor is removed from the cursor list e58c2731f mouse: Free the default cursor when destroyed 789ce17e1 audio: Don't resample in chunks for now. cbab33482 audio: Don't call SDL_AudioStream callbacks for empty data sets. 3e1ae0c86 Clearified the libusb whitelist default logic f4520821e Removed some unnecessary integer casts 0989b7e86 Avoid using designated initializers c6c1e673c Optimized SDL_Convert_*_to_*_Scalar f97b920b3 Optimized SDL_Convert_*_to_*_SSE2 Some of the SDL_Convert_F32_to_*_SSE2 do not explicitly clamp the input, but instead rely on saturating casts. Inputs very far outside the valid [-1.0, 1.0] range may produce an incorrect result, but I believe that is an acceptable trade-off. 300d1ec3e Added audio_convertAccuracy test 32cecc2ea Fixed assertion in audio_convertAudio 33f11e21e Removed assertions in AudioConvert(To|From)Float c2f388fd8 cmake: add SDL_HIDAPI_LIBUSB_SHARED option + test on ci 371cc2d17 wayland: Remove unnecessary flag and state settings fe85e6e75 cocoa: Send a maximized event instead of restored if a deminiaturized window is zoomed ddddcb78c cocoa: Use the close method to hide a miniaturized window be8c42cfd Clarify that a window being 'hidden' means that it is unmapped/ordered out a44338cbc Fix typo in SDL_audiocvt.c f464eb2c5 SDL_hidapi.c: change 'use_libusb_whitelist_default' into a macro. 6607a3cfa Disable cache in python http server 181d5d285 hidapi: Enable libusb support by default. f0f15e365 hidapi: Use a whitelist for libusb when other backends are available c3f7a7dc4 Convert audio using SDL_AUDIO_F32SYS format instead of SDL_AUDIO_F32 796713b9d xxd.py: always write \n line endings 723bcd0a8 SDL_TriggerBreakppoint for riscv arch (both 32/64) version. git-subtree-dir: external/sdl/SDL git-subtree-split: 399bc709b7485bab57880f8261f826f29dc0d7b2
1280 lines
43 KiB
C
1280 lines
43 KiB
C
/*
|
|
Simple DirectMedia Layer
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
warranty. In no event will the authors be held liable for any damages
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
including commercial applications, and to alter it and redistribute it
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
claim that you wrote the original software. If you use this software
|
|
in a product, an acknowledgment in the product documentation would be
|
|
appreciated but is not required.
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
misrepresented as being the original software.
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
*/
|
|
|
|
#include "SDL_internal.h"
|
|
|
|
#ifdef SDL_AUDIO_DRIVER_PIPEWIRE
|
|
|
|
#include "SDL_pipewire.h"
|
|
|
|
#include <pipewire/extensions/metadata.h>
|
|
#include <spa/param/audio/format-utils.h>
|
|
#include <spa/utils/json.h>
|
|
|
|
/*
|
|
* The following keys are defined for compatibility when building against older versions of Pipewire
|
|
* prior to their introduction and can be removed if the minimum required Pipewire build version is
|
|
* increased to or beyond their point of introduction.
|
|
*/
|
|
|
|
/*
|
|
* Introduced in 0.3.22
|
|
* Taken from /src/pipewire/keys.h
|
|
*/
|
|
#ifndef PW_KEY_CONFIG_NAME
|
|
#define PW_KEY_CONFIG_NAME "config.name"
|
|
#endif
|
|
|
|
/*
|
|
* Introduced in 0.3.33
|
|
* Taken from src/pipewire/keys.h
|
|
*/
|
|
#ifndef PW_KEY_NODE_RATE
|
|
#define PW_KEY_NODE_RATE "node.rate"
|
|
#endif
|
|
|
|
/*
|
|
* Introduced in 0.3.44
|
|
* Taken from src/pipewire/keys.h
|
|
*/
|
|
#ifndef PW_KEY_TARGET_OBJECT
|
|
#define PW_KEY_TARGET_OBJECT "target.object"
|
|
#endif
|
|
|
|
/*
|
|
* This seems to be a sane lower limit as Pipewire
|
|
* uses it in several of it's own modules.
|
|
*/
|
|
#define PW_MIN_SAMPLES 32 /* About 0.67ms at 48kHz */
|
|
#define PW_BASE_CLOCK_RATE 48000
|
|
|
|
#define PW_POD_BUFFER_LENGTH 1024
|
|
#define PW_THREAD_NAME_BUFFER_LENGTH 128
|
|
#define PW_MAX_IDENTIFIER_LENGTH 256
|
|
|
|
enum PW_READY_FLAGS
|
|
{
|
|
PW_READY_FLAG_BUFFER_ADDED = 0x1,
|
|
PW_READY_FLAG_STREAM_READY = 0x2,
|
|
PW_READY_FLAG_ALL_BITS = 0x3
|
|
};
|
|
|
|
#define PW_ID_TO_HANDLE(x) (void *)((uintptr_t)x)
|
|
#define PW_HANDLE_TO_ID(x) (uint32_t)((uintptr_t)x)
|
|
|
|
static SDL_bool pipewire_initialized = SDL_FALSE;
|
|
|
|
/* Pipewire entry points */
|
|
static const char *(*PIPEWIRE_pw_get_library_version)(void);
|
|
static void (*PIPEWIRE_pw_init)(int *, char ***);
|
|
static void (*PIPEWIRE_pw_deinit)(void);
|
|
static struct pw_thread_loop *(*PIPEWIRE_pw_thread_loop_new)(const char *, const struct spa_dict *);
|
|
static void (*PIPEWIRE_pw_thread_loop_destroy)(struct pw_thread_loop *);
|
|
static void (*PIPEWIRE_pw_thread_loop_stop)(struct pw_thread_loop *);
|
|
static struct pw_loop *(*PIPEWIRE_pw_thread_loop_get_loop)(struct pw_thread_loop *);
|
|
static void (*PIPEWIRE_pw_thread_loop_lock)(struct pw_thread_loop *);
|
|
static void (*PIPEWIRE_pw_thread_loop_unlock)(struct pw_thread_loop *);
|
|
static void (*PIPEWIRE_pw_thread_loop_signal)(struct pw_thread_loop *, bool);
|
|
static void (*PIPEWIRE_pw_thread_loop_wait)(struct pw_thread_loop *);
|
|
static int (*PIPEWIRE_pw_thread_loop_start)(struct pw_thread_loop *);
|
|
static struct pw_context *(*PIPEWIRE_pw_context_new)(struct pw_loop *, struct pw_properties *, size_t);
|
|
static void (*PIPEWIRE_pw_context_destroy)(struct pw_context *);
|
|
static struct pw_core *(*PIPEWIRE_pw_context_connect)(struct pw_context *, struct pw_properties *, size_t);
|
|
static void (*PIPEWIRE_pw_proxy_add_object_listener)(struct pw_proxy *, struct spa_hook *, const void *, void *);
|
|
static void *(*PIPEWIRE_pw_proxy_get_user_data)(struct pw_proxy *);
|
|
static void (*PIPEWIRE_pw_proxy_destroy)(struct pw_proxy *);
|
|
static int (*PIPEWIRE_pw_core_disconnect)(struct pw_core *);
|
|
static struct pw_stream *(*PIPEWIRE_pw_stream_new_simple)(struct pw_loop *, const char *, struct pw_properties *,
|
|
const struct pw_stream_events *, void *);
|
|
static void (*PIPEWIRE_pw_stream_destroy)(struct pw_stream *);
|
|
static int (*PIPEWIRE_pw_stream_connect)(struct pw_stream *, enum pw_direction, uint32_t, enum pw_stream_flags,
|
|
const struct spa_pod **, uint32_t);
|
|
static enum pw_stream_state (*PIPEWIRE_pw_stream_get_state)(struct pw_stream *stream, const char **error);
|
|
static struct pw_buffer *(*PIPEWIRE_pw_stream_dequeue_buffer)(struct pw_stream *);
|
|
static int (*PIPEWIRE_pw_stream_queue_buffer)(struct pw_stream *, struct pw_buffer *);
|
|
static struct pw_properties *(*PIPEWIRE_pw_properties_new)(const char *, ...)SPA_SENTINEL;
|
|
static int (*PIPEWIRE_pw_properties_set)(struct pw_properties *, const char *, const char *);
|
|
static int (*PIPEWIRE_pw_properties_setf)(struct pw_properties *, const char *, const char *, ...) SPA_PRINTF_FUNC(3, 4);
|
|
|
|
static int pipewire_version_major;
|
|
static int pipewire_version_minor;
|
|
static int pipewire_version_patch;
|
|
|
|
#ifdef SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC
|
|
|
|
static const char *pipewire_library = SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC;
|
|
static void *pipewire_handle = NULL;
|
|
|
|
static int pipewire_dlsym(const char *fn, void **addr)
|
|
{
|
|
*addr = SDL_LoadFunction(pipewire_handle, fn);
|
|
if (*addr == NULL) {
|
|
/* Don't call SDL_SetError(): SDL_LoadFunction already did. */
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
#define SDL_PIPEWIRE_SYM(x) \
|
|
if (!pipewire_dlsym(#x, (void **)(char *)&PIPEWIRE_##x)) { \
|
|
return -1; \
|
|
}
|
|
|
|
static int load_pipewire_library(void)
|
|
{
|
|
pipewire_handle = SDL_LoadObject(pipewire_library);
|
|
return pipewire_handle != NULL ? 0 : -1;
|
|
}
|
|
|
|
static void unload_pipewire_library(void)
|
|
{
|
|
if (pipewire_handle) {
|
|
SDL_UnloadObject(pipewire_handle);
|
|
pipewire_handle = NULL;
|
|
}
|
|
}
|
|
|
|
#else
|
|
|
|
#define SDL_PIPEWIRE_SYM(x) PIPEWIRE_##x = x
|
|
|
|
static int load_pipewire_library(void)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static void unload_pipewire_library(void)
|
|
{ /* Nothing to do */
|
|
}
|
|
|
|
#endif /* SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC */
|
|
|
|
static int load_pipewire_syms(void)
|
|
{
|
|
SDL_PIPEWIRE_SYM(pw_get_library_version);
|
|
SDL_PIPEWIRE_SYM(pw_init);
|
|
SDL_PIPEWIRE_SYM(pw_deinit);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_new);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_destroy);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_stop);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_get_loop);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_lock);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_unlock);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_signal);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_wait);
|
|
SDL_PIPEWIRE_SYM(pw_thread_loop_start);
|
|
SDL_PIPEWIRE_SYM(pw_context_new);
|
|
SDL_PIPEWIRE_SYM(pw_context_destroy);
|
|
SDL_PIPEWIRE_SYM(pw_context_connect);
|
|
SDL_PIPEWIRE_SYM(pw_proxy_add_object_listener);
|
|
SDL_PIPEWIRE_SYM(pw_proxy_get_user_data);
|
|
SDL_PIPEWIRE_SYM(pw_proxy_destroy);
|
|
SDL_PIPEWIRE_SYM(pw_core_disconnect);
|
|
SDL_PIPEWIRE_SYM(pw_stream_new_simple);
|
|
SDL_PIPEWIRE_SYM(pw_stream_destroy);
|
|
SDL_PIPEWIRE_SYM(pw_stream_connect);
|
|
SDL_PIPEWIRE_SYM(pw_stream_get_state);
|
|
SDL_PIPEWIRE_SYM(pw_stream_dequeue_buffer);
|
|
SDL_PIPEWIRE_SYM(pw_stream_queue_buffer);
|
|
SDL_PIPEWIRE_SYM(pw_properties_new);
|
|
SDL_PIPEWIRE_SYM(pw_properties_set);
|
|
SDL_PIPEWIRE_SYM(pw_properties_setf);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static SDL_bool pipewire_version_at_least(int major, int minor, int patch)
|
|
{
|
|
return (pipewire_version_major >= major) &&
|
|
(pipewire_version_major > major || pipewire_version_minor >= minor) &&
|
|
(pipewire_version_major > major || pipewire_version_minor > minor || pipewire_version_patch >= patch);
|
|
}
|
|
|
|
static int init_pipewire_library(void)
|
|
{
|
|
if (!load_pipewire_library()) {
|
|
if (!load_pipewire_syms()) {
|
|
int nargs;
|
|
const char *version = PIPEWIRE_pw_get_library_version();
|
|
nargs = SDL_sscanf(version, "%d.%d.%d", &pipewire_version_major, &pipewire_version_minor, &pipewire_version_patch);
|
|
if (nargs < 3) {
|
|
return -1;
|
|
}
|
|
|
|
/* SDL can build against 0.3.20, but requires 0.3.24 */
|
|
if (pipewire_version_at_least(0, 3, 24)) {
|
|
PIPEWIRE_pw_init(NULL, NULL);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
static void deinit_pipewire_library(void)
|
|
{
|
|
PIPEWIRE_pw_deinit();
|
|
unload_pipewire_library();
|
|
}
|
|
|
|
/* A generic Pipewire node object used for enumeration. */
|
|
struct node_object
|
|
{
|
|
struct spa_list link;
|
|
|
|
Uint32 id;
|
|
int seq;
|
|
SDL_bool persist;
|
|
|
|
/*
|
|
* NOTE: If used, this is *must* be allocated with SDL_malloc() or similar
|
|
* as SDL_free() will be called on it when the node_object is destroyed.
|
|
*
|
|
* If ownership of the referenced memory is transferred, this must be set
|
|
* to NULL or the memory will be freed when the node_object is destroyed.
|
|
*/
|
|
void *userdata;
|
|
|
|
struct pw_proxy *proxy;
|
|
struct spa_hook node_listener;
|
|
struct spa_hook core_listener;
|
|
};
|
|
|
|
/* A sink/source node used for stream I/O. */
|
|
struct io_node
|
|
{
|
|
struct spa_list link;
|
|
|
|
Uint32 id;
|
|
SDL_bool is_capture;
|
|
SDL_AudioSpec spec;
|
|
|
|
const char *name; /* Friendly name */
|
|
const char *path; /* OS identifier (i.e. ALSA endpoint) */
|
|
|
|
char buf[]; /* Buffer to hold the name and path strings. */
|
|
};
|
|
|
|
/* The global hotplug thread and associated objects. */
|
|
static struct pw_thread_loop *hotplug_loop;
|
|
static struct pw_core *hotplug_core;
|
|
static struct pw_context *hotplug_context;
|
|
static struct pw_registry *hotplug_registry;
|
|
static struct spa_hook hotplug_registry_listener;
|
|
static struct spa_hook hotplug_core_listener;
|
|
static struct spa_list hotplug_pending_list;
|
|
static struct spa_list hotplug_io_list;
|
|
static int hotplug_init_seq_val;
|
|
static SDL_bool hotplug_init_complete;
|
|
static SDL_bool hotplug_events_enabled;
|
|
|
|
static char *pipewire_default_sink_id = NULL;
|
|
static char *pipewire_default_source_id = NULL;
|
|
|
|
/* The active node list */
|
|
static SDL_bool io_list_check_add(struct io_node *node)
|
|
{
|
|
struct io_node *n;
|
|
SDL_bool ret = SDL_TRUE;
|
|
|
|
/* See if the node is already in the list */
|
|
spa_list_for_each (n, &hotplug_io_list, link) {
|
|
if (n->id == node->id) {
|
|
ret = SDL_FALSE;
|
|
goto dup_found;
|
|
}
|
|
}
|
|
|
|
/* Add to the list if the node doesn't already exist */
|
|
spa_list_append(&hotplug_io_list, &node->link);
|
|
|
|
if (hotplug_events_enabled) {
|
|
SDL_AddAudioDevice(node->is_capture, node->name, &node->spec, PW_ID_TO_HANDLE(node->id));
|
|
}
|
|
|
|
dup_found:
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void io_list_remove(Uint32 id)
|
|
{
|
|
struct io_node *n, *temp;
|
|
|
|
/* Find and remove the node from the list */
|
|
spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
|
|
if (n->id == id) {
|
|
spa_list_remove(&n->link);
|
|
|
|
if (hotplug_events_enabled) {
|
|
SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(id)));
|
|
}
|
|
|
|
SDL_free(n);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void io_list_clear(void)
|
|
{
|
|
struct io_node *n, *temp;
|
|
|
|
spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
|
|
spa_list_remove(&n->link);
|
|
SDL_free(n);
|
|
}
|
|
}
|
|
|
|
static struct io_node *io_list_get_by_id(Uint32 id)
|
|
{
|
|
struct io_node *n, *temp;
|
|
spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
|
|
if (n->id == id) {
|
|
return n;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void node_object_destroy(struct node_object *node)
|
|
{
|
|
SDL_assert(node);
|
|
|
|
spa_list_remove(&node->link);
|
|
spa_hook_remove(&node->node_listener);
|
|
spa_hook_remove(&node->core_listener);
|
|
SDL_free(node->userdata);
|
|
PIPEWIRE_pw_proxy_destroy(node->proxy);
|
|
}
|
|
|
|
/* The pending node list */
|
|
static void pending_list_add(struct node_object *node)
|
|
{
|
|
SDL_assert(node);
|
|
spa_list_append(&hotplug_pending_list, &node->link);
|
|
}
|
|
|
|
static void pending_list_remove(Uint32 id)
|
|
{
|
|
struct node_object *node, *temp;
|
|
|
|
spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
|
|
if (node->id == id) {
|
|
node_object_destroy(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void pending_list_clear(void)
|
|
{
|
|
struct node_object *node, *temp;
|
|
|
|
spa_list_for_each_safe (node, temp, &hotplug_pending_list, link) {
|
|
node_object_destroy(node);
|
|
}
|
|
}
|
|
|
|
static void *node_object_new(Uint32 id, const char *type, Uint32 version, const void *funcs, const struct pw_core_events *core_events)
|
|
{
|
|
struct pw_proxy *proxy;
|
|
struct node_object *node;
|
|
|
|
/* Create the proxy object */
|
|
proxy = pw_registry_bind(hotplug_registry, id, type, version, sizeof(struct node_object));
|
|
if (proxy == NULL) {
|
|
SDL_SetError("Pipewire: Failed to create proxy object (%i)", errno);
|
|
return NULL;
|
|
}
|
|
|
|
node = PIPEWIRE_pw_proxy_get_user_data(proxy);
|
|
SDL_zerop(node);
|
|
|
|
node->id = id;
|
|
node->proxy = proxy;
|
|
|
|
/* Add the callbacks */
|
|
pw_core_add_listener(hotplug_core, &node->core_listener, core_events, node);
|
|
PIPEWIRE_pw_proxy_add_object_listener(node->proxy, &node->node_listener, funcs, node);
|
|
|
|
/* Add the node to the active list */
|
|
pending_list_add(node);
|
|
|
|
return node;
|
|
}
|
|
|
|
/* Core sync points */
|
|
static void core_events_hotplug_init_callback(void *object, uint32_t id, int seq)
|
|
{
|
|
if (id == PW_ID_CORE && seq == hotplug_init_seq_val) {
|
|
/* This core listener is no longer needed. */
|
|
spa_hook_remove(&hotplug_core_listener);
|
|
|
|
/* Signal that the initial I/O list is populated */
|
|
hotplug_init_complete = SDL_TRUE;
|
|
PIPEWIRE_pw_thread_loop_signal(hotplug_loop, false);
|
|
}
|
|
}
|
|
|
|
static void core_events_interface_callback(void *object, uint32_t id, int seq)
|
|
{
|
|
struct node_object *node = object;
|
|
struct io_node *io = node->userdata;
|
|
|
|
if (id == PW_ID_CORE && seq == node->seq) {
|
|
/*
|
|
* Move the I/O node to the connected list.
|
|
* On success, the list owns the I/O node object.
|
|
*/
|
|
if (io_list_check_add(io)) {
|
|
node->userdata = NULL;
|
|
}
|
|
|
|
node_object_destroy(node);
|
|
}
|
|
}
|
|
|
|
static void core_events_metadata_callback(void *object, uint32_t id, int seq)
|
|
{
|
|
struct node_object *node = object;
|
|
|
|
if (id == PW_ID_CORE && seq == node->seq && !node->persist) {
|
|
node_object_destroy(node);
|
|
}
|
|
}
|
|
|
|
static const struct pw_core_events hotplug_init_core_events = { PW_VERSION_CORE_EVENTS, .done = core_events_hotplug_init_callback };
|
|
static const struct pw_core_events interface_core_events = { PW_VERSION_CORE_EVENTS, .done = core_events_interface_callback };
|
|
static const struct pw_core_events metadata_core_events = { PW_VERSION_CORE_EVENTS, .done = core_events_metadata_callback };
|
|
|
|
static void hotplug_core_sync(struct node_object *node)
|
|
{
|
|
/*
|
|
* Node sync events *must* come before the hotplug init sync events or the initial
|
|
* I/O list will be incomplete when the main hotplug sync point is hit.
|
|
*/
|
|
if (node) {
|
|
node->seq = pw_core_sync(hotplug_core, PW_ID_CORE, node->seq);
|
|
}
|
|
|
|
if (!hotplug_init_complete) {
|
|
hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, hotplug_init_seq_val);
|
|
}
|
|
}
|
|
|
|
/* Helpers for retrieving values from params */
|
|
static SDL_bool get_range_param(const struct spa_pod *param, Uint32 key, int *def, int *min, int *max)
|
|
{
|
|
const struct spa_pod_prop *prop;
|
|
struct spa_pod *value;
|
|
Uint32 n_values, choice;
|
|
|
|
prop = spa_pod_find_prop(param, NULL, key);
|
|
|
|
if (prop && prop->value.type == SPA_TYPE_Choice) {
|
|
value = spa_pod_get_values(&prop->value, &n_values, &choice);
|
|
|
|
if (n_values == 3 && choice == SPA_CHOICE_Range) {
|
|
Uint32 *v = SPA_POD_BODY(value);
|
|
|
|
if (v) {
|
|
if (def) {
|
|
*def = (int)v[0];
|
|
}
|
|
if (min) {
|
|
*min = (int)v[1];
|
|
}
|
|
if (max) {
|
|
*max = (int)v[2];
|
|
}
|
|
|
|
return SDL_TRUE;
|
|
}
|
|
}
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
static SDL_bool get_int_param(const struct spa_pod *param, Uint32 key, int *val)
|
|
{
|
|
const struct spa_pod_prop *prop;
|
|
Sint32 v;
|
|
|
|
prop = spa_pod_find_prop(param, NULL, key);
|
|
|
|
if (prop && spa_pod_get_int(&prop->value, &v) == 0) {
|
|
if (val) {
|
|
*val = (int)v;
|
|
}
|
|
|
|
return SDL_TRUE;
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
/* Interface node callbacks */
|
|
static void node_event_info(void *object, const struct pw_node_info *info)
|
|
{
|
|
struct node_object *node = object;
|
|
struct io_node *io = node->userdata;
|
|
const char *prop_val;
|
|
Uint32 i;
|
|
|
|
if (info) {
|
|
prop_val = spa_dict_lookup(info->props, PW_KEY_AUDIO_CHANNELS);
|
|
if (prop_val) {
|
|
io->spec.channels = (Uint8)SDL_atoi(prop_val);
|
|
}
|
|
|
|
/* Need to parse the parameters to get the sample rate */
|
|
for (i = 0; i < info->n_params; ++i) {
|
|
pw_node_enum_params(node->proxy, 0, info->params[i].id, 0, 0, NULL);
|
|
}
|
|
|
|
hotplug_core_sync(node);
|
|
}
|
|
}
|
|
|
|
static void node_event_param(void *object, int seq, uint32_t id, uint32_t index, uint32_t next, const struct spa_pod *param)
|
|
{
|
|
struct node_object *node = object;
|
|
struct io_node *io = node->userdata;
|
|
|
|
/* Get the default frequency */
|
|
if (io->spec.freq == 0) {
|
|
get_range_param(param, SPA_FORMAT_AUDIO_rate, &io->spec.freq, NULL, NULL);
|
|
}
|
|
|
|
/*
|
|
* The channel count should have come from the node properties,
|
|
* but it is stored here as well. If one failed, try the other.
|
|
*/
|
|
if (io->spec.channels == 0) {
|
|
int channels;
|
|
if (get_int_param(param, SPA_FORMAT_AUDIO_channels, &channels)) {
|
|
io->spec.channels = (Uint8)channels;
|
|
}
|
|
}
|
|
}
|
|
|
|
static const struct pw_node_events interface_node_events = { PW_VERSION_NODE_EVENTS, .info = node_event_info,
|
|
.param = node_event_param };
|
|
|
|
static char *get_name_from_json(const char *json)
|
|
{
|
|
struct spa_json parser[2];
|
|
char key[7]; /* "name" */
|
|
char value[PW_MAX_IDENTIFIER_LENGTH];
|
|
spa_json_init(&parser[0], json, SDL_strlen(json));
|
|
if (spa_json_enter_object(&parser[0], &parser[1]) <= 0) {
|
|
/* Not actually JSON */
|
|
return NULL;
|
|
}
|
|
if (spa_json_get_string(&parser[1], key, sizeof(key)) <= 0) {
|
|
/* Not actually a key/value pair */
|
|
return NULL;
|
|
}
|
|
if (spa_json_get_string(&parser[1], value, sizeof(value)) <= 0) {
|
|
/* Somehow had a key with no value? */
|
|
return NULL;
|
|
}
|
|
return SDL_strdup(value);
|
|
}
|
|
|
|
static void change_default_device(const char *path)
|
|
{
|
|
if (hotplug_events_enabled) {
|
|
struct io_node *n, *temp;
|
|
spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
|
|
if (SDL_strcmp(n->path, path) == 0) {
|
|
SDL_DefaultAudioDeviceChanged(SDL_FindPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(n->id)));
|
|
return; // found it, we're done.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Metadata node callback */
|
|
static int metadata_property(void *object, Uint32 subject, const char *key, const char *type, const char *value)
|
|
{
|
|
struct node_object *node = object;
|
|
|
|
if (subject == PW_ID_CORE && key != NULL && value != NULL) {
|
|
if (!SDL_strcmp(key, "default.audio.sink")) {
|
|
if (pipewire_default_sink_id != NULL) {
|
|
SDL_free(pipewire_default_sink_id);
|
|
}
|
|
pipewire_default_sink_id = get_name_from_json(value);
|
|
node->persist = SDL_TRUE;
|
|
change_default_device(pipewire_default_sink_id);
|
|
} else if (!SDL_strcmp(key, "default.audio.source")) {
|
|
if (pipewire_default_source_id != NULL) {
|
|
SDL_free(pipewire_default_source_id);
|
|
}
|
|
pipewire_default_source_id = get_name_from_json(value);
|
|
node->persist = SDL_TRUE;
|
|
change_default_device(pipewire_default_source_id);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct pw_metadata_events metadata_node_events = { PW_VERSION_METADATA_EVENTS, .property = metadata_property };
|
|
|
|
/* Global registry callbacks */
|
|
static void registry_event_global_callback(void *object, uint32_t id, uint32_t permissions, const char *type, uint32_t version,
|
|
const struct spa_dict *props)
|
|
{
|
|
struct node_object *node;
|
|
|
|
/* We're only interested in interface and metadata nodes. */
|
|
if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Node)) {
|
|
const char *media_class = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS);
|
|
|
|
if (media_class) {
|
|
const char *node_desc;
|
|
const char *node_path;
|
|
struct io_node *io;
|
|
SDL_bool is_capture;
|
|
int desc_buffer_len;
|
|
int path_buffer_len;
|
|
|
|
/* Just want sink and capture */
|
|
if (!SDL_strcasecmp(media_class, "Audio/Sink")) {
|
|
is_capture = SDL_FALSE;
|
|
} else if (!SDL_strcasecmp(media_class, "Audio/Source")) {
|
|
is_capture = SDL_TRUE;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
node_desc = spa_dict_lookup(props, PW_KEY_NODE_DESCRIPTION);
|
|
node_path = spa_dict_lookup(props, PW_KEY_NODE_NAME);
|
|
|
|
if (node_desc && node_path) {
|
|
node = node_object_new(id, type, version, &interface_node_events, &interface_core_events);
|
|
if (node == NULL) {
|
|
SDL_SetError("Pipewire: Failed to allocate interface node");
|
|
return;
|
|
}
|
|
|
|
/* Allocate and initialize the I/O node information struct */
|
|
desc_buffer_len = SDL_strlen(node_desc) + 1;
|
|
path_buffer_len = SDL_strlen(node_path) + 1;
|
|
node->userdata = io = SDL_calloc(1, sizeof(struct io_node) + desc_buffer_len + path_buffer_len);
|
|
if (io == NULL) {
|
|
node_object_destroy(node);
|
|
SDL_OutOfMemory();
|
|
return;
|
|
}
|
|
|
|
/* Begin setting the node properties */
|
|
io->id = id;
|
|
io->is_capture = is_capture;
|
|
io->spec.format = SDL_AUDIO_F32; /* Pipewire uses floats internally, other formats require conversion. */
|
|
io->name = io->buf;
|
|
io->path = io->buf + desc_buffer_len;
|
|
SDL_strlcpy(io->buf, node_desc, desc_buffer_len);
|
|
SDL_strlcpy(io->buf + desc_buffer_len, node_path, path_buffer_len);
|
|
|
|
/* Update sync points */
|
|
hotplug_core_sync(node);
|
|
}
|
|
}
|
|
} else if (!SDL_strcmp(type, PW_TYPE_INTERFACE_Metadata)) {
|
|
node = node_object_new(id, type, version, &metadata_node_events, &metadata_core_events);
|
|
if (node == NULL) {
|
|
SDL_SetError("Pipewire: Failed to allocate metadata node");
|
|
return;
|
|
}
|
|
|
|
/* Update sync points */
|
|
hotplug_core_sync(node);
|
|
}
|
|
}
|
|
|
|
static void registry_event_remove_callback(void *object, uint32_t id)
|
|
{
|
|
io_list_remove(id);
|
|
pending_list_remove(id);
|
|
}
|
|
|
|
static const struct pw_registry_events registry_events = { PW_VERSION_REGISTRY_EVENTS, .global = registry_event_global_callback,
|
|
.global_remove = registry_event_remove_callback };
|
|
|
|
/* The hotplug thread */
|
|
static int hotplug_loop_init(void)
|
|
{
|
|
int res;
|
|
|
|
spa_list_init(&hotplug_pending_list);
|
|
spa_list_init(&hotplug_io_list);
|
|
|
|
hotplug_loop = PIPEWIRE_pw_thread_loop_new("SDLAudioHotplug", NULL);
|
|
if (hotplug_loop == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create hotplug detection loop (%i)", errno);
|
|
}
|
|
|
|
hotplug_context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(hotplug_loop), NULL, 0);
|
|
if (hotplug_context == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create hotplug detection context (%i)", errno);
|
|
}
|
|
|
|
hotplug_core = PIPEWIRE_pw_context_connect(hotplug_context, NULL, 0);
|
|
if (hotplug_core == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to connect hotplug detection context (%i)", errno);
|
|
}
|
|
|
|
hotplug_registry = pw_core_get_registry(hotplug_core, PW_VERSION_REGISTRY, 0);
|
|
if (hotplug_registry == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to acquire hotplug detection registry (%i)", errno);
|
|
}
|
|
|
|
spa_zero(hotplug_registry_listener);
|
|
pw_registry_add_listener(hotplug_registry, &hotplug_registry_listener, ®istry_events, NULL);
|
|
|
|
spa_zero(hotplug_core_listener);
|
|
pw_core_add_listener(hotplug_core, &hotplug_core_listener, &hotplug_init_core_events, NULL);
|
|
|
|
hotplug_init_seq_val = pw_core_sync(hotplug_core, PW_ID_CORE, 0);
|
|
|
|
res = PIPEWIRE_pw_thread_loop_start(hotplug_loop);
|
|
if (res != 0) {
|
|
return SDL_SetError("Pipewire: Failed to start hotplug detection loop");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void hotplug_loop_destroy(void)
|
|
{
|
|
if (hotplug_loop) {
|
|
PIPEWIRE_pw_thread_loop_stop(hotplug_loop);
|
|
}
|
|
|
|
pending_list_clear();
|
|
io_list_clear();
|
|
|
|
hotplug_init_complete = SDL_FALSE;
|
|
hotplug_events_enabled = SDL_FALSE;
|
|
|
|
if (pipewire_default_sink_id != NULL) {
|
|
SDL_free(pipewire_default_sink_id);
|
|
pipewire_default_sink_id = NULL;
|
|
}
|
|
if (pipewire_default_source_id != NULL) {
|
|
SDL_free(pipewire_default_source_id);
|
|
pipewire_default_source_id = NULL;
|
|
}
|
|
|
|
if (hotplug_registry) {
|
|
PIPEWIRE_pw_proxy_destroy((struct pw_proxy *)hotplug_registry);
|
|
hotplug_registry = NULL;
|
|
}
|
|
|
|
if (hotplug_core) {
|
|
PIPEWIRE_pw_core_disconnect(hotplug_core);
|
|
hotplug_core = NULL;
|
|
}
|
|
|
|
if (hotplug_context) {
|
|
PIPEWIRE_pw_context_destroy(hotplug_context);
|
|
hotplug_context = NULL;
|
|
}
|
|
|
|
if (hotplug_loop) {
|
|
PIPEWIRE_pw_thread_loop_destroy(hotplug_loop);
|
|
hotplug_loop = NULL;
|
|
}
|
|
}
|
|
|
|
static void PIPEWIRE_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioDevice **default_capture)
|
|
{
|
|
struct io_node *io;
|
|
|
|
PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
|
|
|
|
/* Wait until the initial registry enumeration is complete */
|
|
if (!hotplug_init_complete) {
|
|
PIPEWIRE_pw_thread_loop_wait(hotplug_loop);
|
|
}
|
|
|
|
spa_list_for_each (io, &hotplug_io_list, link) {
|
|
SDL_AudioDevice *device = SDL_AddAudioDevice(io->is_capture, io->name, &io->spec, PW_ID_TO_HANDLE(io->id));
|
|
if (pipewire_default_sink_id != NULL && SDL_strcmp(io->path, pipewire_default_sink_id) == 0) {
|
|
SDL_assert(!io->is_capture);
|
|
*default_output = device;
|
|
} else if (pipewire_default_source_id != NULL && SDL_strcmp(io->path, pipewire_default_source_id) == 0) {
|
|
SDL_assert(io->is_capture);
|
|
*default_capture = device;
|
|
}
|
|
}
|
|
|
|
hotplug_events_enabled = SDL_TRUE;
|
|
|
|
PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
|
|
}
|
|
|
|
/* Channel maps that match the order in SDL_Audio.h */
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_1[] = { SPA_AUDIO_CHANNEL_MONO };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_2[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_3[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_LFE };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_4[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_RL,
|
|
SPA_AUDIO_CHANNEL_RR };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_5[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
|
|
SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_6[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
|
|
SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_7[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
|
|
SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RC, SPA_AUDIO_CHANNEL_RL,
|
|
SPA_AUDIO_CHANNEL_RR };
|
|
static const enum spa_audio_channel PIPEWIRE_channel_map_8[] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, SPA_AUDIO_CHANNEL_FC,
|
|
SPA_AUDIO_CHANNEL_LFE, SPA_AUDIO_CHANNEL_RL, SPA_AUDIO_CHANNEL_RR,
|
|
SPA_AUDIO_CHANNEL_SL, SPA_AUDIO_CHANNEL_SR };
|
|
|
|
#define COPY_CHANNEL_MAP(c) SDL_memcpy(info->position, PIPEWIRE_channel_map_##c, sizeof(PIPEWIRE_channel_map_##c))
|
|
|
|
static void initialize_spa_info(const SDL_AudioSpec *spec, struct spa_audio_info_raw *info)
|
|
{
|
|
info->channels = spec->channels;
|
|
info->rate = spec->freq;
|
|
|
|
switch (spec->channels) {
|
|
case 1:
|
|
COPY_CHANNEL_MAP(1);
|
|
break;
|
|
case 2:
|
|
COPY_CHANNEL_MAP(2);
|
|
break;
|
|
case 3:
|
|
COPY_CHANNEL_MAP(3);
|
|
break;
|
|
case 4:
|
|
COPY_CHANNEL_MAP(4);
|
|
break;
|
|
case 5:
|
|
COPY_CHANNEL_MAP(5);
|
|
break;
|
|
case 6:
|
|
COPY_CHANNEL_MAP(6);
|
|
break;
|
|
case 7:
|
|
COPY_CHANNEL_MAP(7);
|
|
break;
|
|
case 8:
|
|
COPY_CHANNEL_MAP(8);
|
|
break;
|
|
}
|
|
|
|
/* Pipewire natively supports all of SDL's sample formats */
|
|
switch (spec->format) {
|
|
case SDL_AUDIO_U8:
|
|
info->format = SPA_AUDIO_FORMAT_U8;
|
|
break;
|
|
case SDL_AUDIO_S8:
|
|
info->format = SPA_AUDIO_FORMAT_S8;
|
|
break;
|
|
case SDL_AUDIO_S16LE:
|
|
info->format = SPA_AUDIO_FORMAT_S16_LE;
|
|
break;
|
|
case SDL_AUDIO_S16BE:
|
|
info->format = SPA_AUDIO_FORMAT_S16_BE;
|
|
break;
|
|
case SDL_AUDIO_S32LE:
|
|
info->format = SPA_AUDIO_FORMAT_S32_LE;
|
|
break;
|
|
case SDL_AUDIO_S32BE:
|
|
info->format = SPA_AUDIO_FORMAT_S32_BE;
|
|
break;
|
|
case SDL_AUDIO_F32LE:
|
|
info->format = SPA_AUDIO_FORMAT_F32_LE;
|
|
break;
|
|
case SDL_AUDIO_F32BE:
|
|
info->format = SPA_AUDIO_FORMAT_F32_BE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static Uint8 *PIPEWIRE_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
|
|
{
|
|
// See if a buffer is available. If this returns NULL, SDL_OutputAudioThreadIterate will return SDL_FALSE, but since we own the thread, it won't kill playback.
|
|
// !!! FIXME: It's not clear to me if this ever returns NULL or if this was just defensive coding.
|
|
|
|
struct pw_stream *stream = device->hidden->stream;
|
|
struct pw_buffer *pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream);
|
|
if (pw_buf == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
struct spa_buffer *spa_buf = pw_buf->buffer;
|
|
if (spa_buf->datas[0].data == NULL) {
|
|
PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
|
|
return NULL;
|
|
}
|
|
|
|
device->hidden->pw_buf = pw_buf;
|
|
return (Uint8 *) spa_buf->datas[0].data;
|
|
}
|
|
|
|
static int PIPEWIRE_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size)
|
|
{
|
|
struct pw_stream *stream = device->hidden->stream;
|
|
struct pw_buffer *pw_buf = device->hidden->pw_buf;
|
|
struct spa_buffer *spa_buf = pw_buf->buffer;
|
|
spa_buf->datas[0].chunk->offset = 0;
|
|
spa_buf->datas[0].chunk->stride = device->hidden->stride;
|
|
spa_buf->datas[0].chunk->size = buffer_size;
|
|
|
|
PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
|
|
device->hidden->pw_buf = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void output_callback(void *data)
|
|
{
|
|
SDL_OutputAudioThreadIterate((SDL_AudioDevice *)data);
|
|
}
|
|
|
|
static void PIPEWIRE_FlushCapture(SDL_AudioDevice *device)
|
|
{
|
|
struct pw_stream *stream = device->hidden->stream;
|
|
struct pw_buffer *pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream);
|
|
if (pw_buf != NULL) { // just requeue it without any further thought.
|
|
PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
|
|
}
|
|
}
|
|
|
|
static int PIPEWIRE_CaptureFromDevice(SDL_AudioDevice *device, void *buffer, int buflen)
|
|
{
|
|
struct pw_stream *stream = device->hidden->stream;
|
|
struct pw_buffer *pw_buf = PIPEWIRE_pw_stream_dequeue_buffer(stream);
|
|
if (!pw_buf) {
|
|
return 0;
|
|
}
|
|
|
|
struct spa_buffer *spa_buf = pw_buf->buffer;
|
|
if (!spa_buf) {
|
|
PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
|
|
return 0;
|
|
}
|
|
|
|
const Uint8 *src = (const Uint8 *)spa_buf->datas[0].data;
|
|
const Uint32 offset = SPA_MIN(spa_buf->datas[0].chunk->offset, spa_buf->datas[0].maxsize);
|
|
const Uint32 size = SPA_MIN(spa_buf->datas[0].chunk->size, spa_buf->datas[0].maxsize - offset);
|
|
const int cpy = SDL_min(buflen, (int) size);
|
|
|
|
SDL_assert(size <= buflen); // We'll have to reengineer some stuff if this turns out to not be true.
|
|
|
|
SDL_memcpy(buffer, src + offset, cpy);
|
|
PIPEWIRE_pw_stream_queue_buffer(stream, pw_buf);
|
|
|
|
return cpy;
|
|
}
|
|
|
|
static void input_callback(void *data)
|
|
{
|
|
SDL_CaptureAudioThreadIterate((SDL_AudioDevice *)data);
|
|
}
|
|
|
|
static void stream_add_buffer_callback(void *data, struct pw_buffer *buffer)
|
|
{
|
|
SDL_AudioDevice *device = (SDL_AudioDevice *) data;
|
|
|
|
if (device->iscapture == SDL_FALSE) {
|
|
/* Clamp the output spec samples and size to the max size of the Pipewire buffer.
|
|
If they exceed the maximum size of the Pipewire buffer, double buffering will be used. */
|
|
if (device->buffer_size > buffer->buffer->datas[0].maxsize) {
|
|
SDL_LockMutex(device->lock);
|
|
device->sample_frames = buffer->buffer->datas[0].maxsize / device->hidden->stride;
|
|
device->buffer_size = buffer->buffer->datas[0].maxsize;
|
|
SDL_UnlockMutex(device->lock);
|
|
}
|
|
}
|
|
|
|
device->hidden->stream_init_status |= PW_READY_FLAG_BUFFER_ADDED;
|
|
PIPEWIRE_pw_thread_loop_signal(device->hidden->loop, false);
|
|
}
|
|
|
|
static void stream_state_changed_callback(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *error)
|
|
{
|
|
SDL_AudioDevice *device = (SDL_AudioDevice *) data;
|
|
|
|
if (state == PW_STREAM_STATE_STREAMING) {
|
|
device->hidden->stream_init_status |= PW_READY_FLAG_STREAM_READY;
|
|
}
|
|
|
|
if (state == PW_STREAM_STATE_STREAMING || state == PW_STREAM_STATE_ERROR) {
|
|
PIPEWIRE_pw_thread_loop_signal(device->hidden->loop, false);
|
|
}
|
|
}
|
|
|
|
static const struct pw_stream_events stream_output_events = { PW_VERSION_STREAM_EVENTS,
|
|
.state_changed = stream_state_changed_callback,
|
|
.add_buffer = stream_add_buffer_callback,
|
|
.process = output_callback };
|
|
static const struct pw_stream_events stream_input_events = { PW_VERSION_STREAM_EVENTS,
|
|
.state_changed = stream_state_changed_callback,
|
|
.add_buffer = stream_add_buffer_callback,
|
|
.process = input_callback };
|
|
|
|
static int PIPEWIRE_OpenDevice(SDL_AudioDevice *device)
|
|
{
|
|
/*
|
|
* NOTE: The PW_STREAM_FLAG_RT_PROCESS flag can be set to call the stream
|
|
* processing callback from the realtime thread. However, it comes with some
|
|
* caveats: no file IO, allocations, locking or other blocking operations
|
|
* must occur in the mixer callback. As this cannot be guaranteed when the
|
|
* callback is in the calling application, this flag is omitted.
|
|
*/
|
|
static const enum pw_stream_flags STREAM_FLAGS = PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS;
|
|
|
|
char thread_name[PW_THREAD_NAME_BUFFER_LENGTH];
|
|
Uint8 pod_buffer[PW_POD_BUFFER_LENGTH];
|
|
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(pod_buffer, sizeof(pod_buffer));
|
|
struct spa_audio_info_raw spa_info = { 0 };
|
|
const struct spa_pod *params = NULL;
|
|
struct SDL_PrivateAudioData *priv;
|
|
struct pw_properties *props;
|
|
const char *app_name, *app_id, *stream_name, *stream_role, *error;
|
|
Uint32 node_id = device->handle == NULL ? PW_ID_ANY : PW_HANDLE_TO_ID(device->handle);
|
|
const SDL_bool iscapture = device->iscapture;
|
|
int res;
|
|
|
|
/* Clamp the period size to sane values */
|
|
const int min_period = PW_MIN_SAMPLES * SPA_MAX(device->spec.freq / PW_BASE_CLOCK_RATE, 1);
|
|
|
|
/* Get the hints for the application name, stream name and role */
|
|
app_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_APP_NAME);
|
|
if (app_name == NULL || *app_name == '\0') {
|
|
app_name = SDL_GetHint(SDL_HINT_APP_NAME);
|
|
if (app_name == NULL || *app_name == '\0') {
|
|
app_name = "SDL Application";
|
|
}
|
|
}
|
|
|
|
/* App ID. Default to NULL if not available. */
|
|
app_id = SDL_GetHint(SDL_HINT_APP_ID);
|
|
|
|
stream_name = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_NAME);
|
|
if (stream_name == NULL || *stream_name == '\0') {
|
|
stream_name = "Audio Stream";
|
|
}
|
|
|
|
/*
|
|
* 'Music' is the default used internally by Pipewire and it's modules,
|
|
* but 'Game' seems more appropriate for the majority of SDL applications.
|
|
*/
|
|
stream_role = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_STREAM_ROLE);
|
|
if (stream_role == NULL || *stream_role == '\0') {
|
|
stream_role = "Game";
|
|
}
|
|
|
|
/* Initialize the Pipewire stream info from the SDL audio spec */
|
|
initialize_spa_info(&device->spec, &spa_info);
|
|
params = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &spa_info);
|
|
if (params == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to set audio format parameters");
|
|
}
|
|
|
|
priv = SDL_calloc(1, sizeof(struct SDL_PrivateAudioData));
|
|
device->hidden = priv;
|
|
if (priv == NULL) {
|
|
return SDL_OutOfMemory();
|
|
}
|
|
|
|
/* Size of a single audio frame in bytes */
|
|
priv->stride = SDL_AUDIO_FRAMESIZE(device->spec);
|
|
|
|
if (device->sample_frames < min_period) {
|
|
device->sample_frames = min_period;
|
|
}
|
|
|
|
SDL_UpdatedAudioDeviceFormat(device);
|
|
|
|
SDL_GetAudioThreadName(device, thread_name, sizeof(thread_name));
|
|
priv->loop = PIPEWIRE_pw_thread_loop_new(thread_name, NULL);
|
|
if (priv->loop == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create stream loop (%i)", errno);
|
|
}
|
|
|
|
/* Load the realtime module so Pipewire can set the loop thread to the appropriate priority. */
|
|
props = PIPEWIRE_pw_properties_new(PW_KEY_CONFIG_NAME, "client-rt.conf", NULL);
|
|
if (props == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create stream context properties (%i)", errno);
|
|
}
|
|
|
|
priv->context = PIPEWIRE_pw_context_new(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), props, 0);
|
|
if (priv->context == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create stream context (%i)", errno);
|
|
}
|
|
|
|
props = PIPEWIRE_pw_properties_new(NULL, NULL);
|
|
if (props == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create stream properties (%i)", errno);
|
|
}
|
|
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_TYPE, "Audio");
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_CATEGORY, iscapture ? "Capture" : "Playback");
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_MEDIA_ROLE, stream_role);
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_APP_NAME, app_name);
|
|
if (app_id != NULL) {
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_APP_ID, app_id);
|
|
}
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_NAME, stream_name);
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_DESCRIPTION, stream_name);
|
|
PIPEWIRE_pw_properties_setf(props, PW_KEY_NODE_LATENCY, "%u/%i", device->sample_frames, device->spec.freq);
|
|
PIPEWIRE_pw_properties_setf(props, PW_KEY_NODE_RATE, "1/%u", device->spec.freq);
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_ALWAYS_PROCESS, "true");
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_NODE_DONT_RECONNECT, "true"); // Requesting a specific device, don't migrate to new default hardware.
|
|
|
|
/*
|
|
* Pipewire 0.3.44 introduced PW_KEY_TARGET_OBJECT that takes either a path
|
|
* (PW_KEY_NODE_NAME) or node serial number (PE_KEY_OBJECT_SERIAL) to connect
|
|
* the stream to its target. The target_id parameter in pw_stream_connect() is
|
|
* now deprecated and should always be PW_ID_ANY.
|
|
*/
|
|
if (pipewire_version_at_least(0, 3, 44)) {
|
|
if (node_id != PW_ID_ANY) {
|
|
const struct io_node *node;
|
|
|
|
PIPEWIRE_pw_thread_loop_lock(hotplug_loop);
|
|
node = io_list_get_by_id(node_id);
|
|
if (node != NULL) {
|
|
PIPEWIRE_pw_properties_set(props, PW_KEY_TARGET_OBJECT, node->path);
|
|
}
|
|
PIPEWIRE_pw_thread_loop_unlock(hotplug_loop);
|
|
|
|
node_id = PW_ID_ANY;
|
|
}
|
|
}
|
|
|
|
/* Create the new stream */
|
|
priv->stream = PIPEWIRE_pw_stream_new_simple(PIPEWIRE_pw_thread_loop_get_loop(priv->loop), stream_name, props,
|
|
iscapture ? &stream_input_events : &stream_output_events, device);
|
|
if (priv->stream == NULL) {
|
|
return SDL_SetError("Pipewire: Failed to create stream (%i)", errno);
|
|
}
|
|
|
|
res = PIPEWIRE_pw_stream_connect(priv->stream, iscapture ? PW_DIRECTION_INPUT : PW_DIRECTION_OUTPUT, node_id, STREAM_FLAGS,
|
|
¶ms, 1);
|
|
if (res != 0) {
|
|
return SDL_SetError("Pipewire: Failed to connect stream");
|
|
}
|
|
|
|
res = PIPEWIRE_pw_thread_loop_start(priv->loop);
|
|
if (res != 0) {
|
|
return SDL_SetError("Pipewire: Failed to start stream loop");
|
|
}
|
|
|
|
/* Wait until all init flags are set or the stream has failed. */
|
|
PIPEWIRE_pw_thread_loop_lock(priv->loop);
|
|
while (priv->stream_init_status != PW_READY_FLAG_ALL_BITS &&
|
|
PIPEWIRE_pw_stream_get_state(priv->stream, NULL) != PW_STREAM_STATE_ERROR) {
|
|
PIPEWIRE_pw_thread_loop_wait(priv->loop);
|
|
}
|
|
PIPEWIRE_pw_thread_loop_unlock(priv->loop);
|
|
|
|
if (PIPEWIRE_pw_stream_get_state(priv->stream, &error) == PW_STREAM_STATE_ERROR) {
|
|
return SDL_SetError("Pipewire: Stream error: %s", error);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void PIPEWIRE_CloseDevice(SDL_AudioDevice *device)
|
|
{
|
|
if (!device->hidden) {
|
|
return;
|
|
}
|
|
|
|
if (device->hidden->loop) {
|
|
PIPEWIRE_pw_thread_loop_stop(device->hidden->loop);
|
|
}
|
|
|
|
if (device->hidden->stream) {
|
|
PIPEWIRE_pw_stream_destroy(device->hidden->stream);
|
|
}
|
|
|
|
if (device->hidden->context) {
|
|
PIPEWIRE_pw_context_destroy(device->hidden->context);
|
|
}
|
|
|
|
if (device->hidden->loop) {
|
|
PIPEWIRE_pw_thread_loop_destroy(device->hidden->loop);
|
|
}
|
|
|
|
SDL_free(device->hidden);
|
|
device->hidden = NULL;
|
|
|
|
SDL_AudioThreadFinalize(device);
|
|
}
|
|
|
|
static void PIPEWIRE_Deinitialize(void)
|
|
{
|
|
if (pipewire_initialized) {
|
|
hotplug_loop_destroy();
|
|
deinit_pipewire_library();
|
|
pipewire_initialized = SDL_FALSE;
|
|
}
|
|
}
|
|
|
|
static SDL_bool PIPEWIRE_Init(SDL_AudioDriverImpl *impl)
|
|
{
|
|
if (!pipewire_initialized) {
|
|
if (init_pipewire_library() < 0) {
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
pipewire_initialized = SDL_TRUE;
|
|
|
|
if (hotplug_loop_init() < 0) {
|
|
PIPEWIRE_Deinitialize();
|
|
return SDL_FALSE;
|
|
}
|
|
}
|
|
|
|
/* Set the function pointers */
|
|
impl->DetectDevices = PIPEWIRE_DetectDevices;
|
|
impl->OpenDevice = PIPEWIRE_OpenDevice;
|
|
impl->Deinitialize = PIPEWIRE_Deinitialize;
|
|
impl->PlayDevice = PIPEWIRE_PlayDevice;
|
|
impl->GetDeviceBuf = PIPEWIRE_GetDeviceBuf;
|
|
impl->CaptureFromDevice = PIPEWIRE_CaptureFromDevice;
|
|
impl->FlushCapture = PIPEWIRE_FlushCapture;
|
|
impl->CloseDevice = PIPEWIRE_CloseDevice;
|
|
|
|
impl->HasCaptureSupport = SDL_TRUE;
|
|
impl->ProvidesOwnCallbackThread = SDL_TRUE;
|
|
|
|
return SDL_TRUE;
|
|
}
|
|
|
|
AudioBootStrap PIPEWIRE_bootstrap = { "pipewire", "Pipewire", PIPEWIRE_Init, SDL_FALSE };
|
|
|
|
#endif /* SDL_AUDIO_DRIVER_PIPEWIRE */
|