SDL_alsa_audio.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_AUDIO_DRIVER_ALSA
  20. #ifndef SDL_ALSA_NON_BLOCKING
  21. #define SDL_ALSA_NON_BLOCKING 0
  22. #endif
  23. // without the thread, you will detect devices on startup, but will not get further hotplug events. But that might be okay.
  24. #ifndef SDL_ALSA_HOTPLUG_THREAD
  25. #define SDL_ALSA_HOTPLUG_THREAD 1
  26. #endif
  27. // this turns off debug logging completely (but by default this goes to the bitbucket).
  28. #ifndef SDL_ALSA_DEBUG
  29. #define SDL_ALSA_DEBUG 1
  30. #endif
  31. #include "../SDL_sysaudio.h"
  32. #include "SDL_alsa_audio.h"
  33. #if SDL_ALSA_DEBUG
  34. #define LOGDEBUG(...) SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "ALSA: " __VA_ARGS__)
  35. #else
  36. #define LOGDEBUG(...)
  37. #endif
  38. //TODO: cleanup once the code settled down
  39. static int (*ALSA_snd_pcm_open)(snd_pcm_t **, const char *, snd_pcm_stream_t, int);
  40. static int (*ALSA_snd_pcm_close)(snd_pcm_t *pcm);
  41. static int (*ALSA_snd_pcm_start)(snd_pcm_t *pcm);
  42. static snd_pcm_sframes_t (*ALSA_snd_pcm_writei)(snd_pcm_t *, const void *, snd_pcm_uframes_t);
  43. static snd_pcm_sframes_t (*ALSA_snd_pcm_readi)(snd_pcm_t *, void *, snd_pcm_uframes_t);
  44. static int (*ALSA_snd_pcm_recover)(snd_pcm_t *, int, int);
  45. static int (*ALSA_snd_pcm_prepare)(snd_pcm_t *);
  46. static int (*ALSA_snd_pcm_drain)(snd_pcm_t *);
  47. static const char *(*ALSA_snd_strerror)(int);
  48. static size_t (*ALSA_snd_pcm_hw_params_sizeof)(void);
  49. static size_t (*ALSA_snd_pcm_sw_params_sizeof)(void);
  50. static void (*ALSA_snd_pcm_hw_params_copy)(snd_pcm_hw_params_t *, const snd_pcm_hw_params_t *);
  51. static int (*ALSA_snd_pcm_hw_params_any)(snd_pcm_t *, snd_pcm_hw_params_t *);
  52. static int (*ALSA_snd_pcm_hw_params_set_access)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
  53. static int (*ALSA_snd_pcm_hw_params_set_format)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
  54. static int (*ALSA_snd_pcm_hw_params_set_channels)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
  55. static int (*ALSA_snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *, unsigned int *);
  56. static int (*ALSA_snd_pcm_hw_params_set_rate_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  57. static int (*ALSA_snd_pcm_hw_params_set_period_size_near)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
  58. static int (*ALSA_snd_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
  59. static int (*ALSA_snd_pcm_hw_params_set_periods_min)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  60. static int (*ALSA_snd_pcm_hw_params_set_periods_first)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  61. static int (*ALSA_snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *, unsigned int *, int *);
  62. static int (*ALSA_snd_pcm_hw_params_set_buffer_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
  63. static int (*ALSA_snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
  64. static int (*ALSA_snd_pcm_hw_params)(snd_pcm_t *, snd_pcm_hw_params_t *);
  65. static int (*ALSA_snd_pcm_sw_params_current)(snd_pcm_t *,
  66. snd_pcm_sw_params_t *);
  67. static int (*ALSA_snd_pcm_sw_params_set_start_threshold)(snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
  68. static int (*ALSA_snd_pcm_sw_params)(snd_pcm_t *, snd_pcm_sw_params_t *);
  69. static int (*ALSA_snd_pcm_nonblock)(snd_pcm_t *, int);
  70. static int (*ALSA_snd_pcm_wait)(snd_pcm_t *, int);
  71. static int (*ALSA_snd_pcm_sw_params_set_avail_min)(snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
  72. static int (*ALSA_snd_pcm_reset)(snd_pcm_t *);
  73. static int (*ALSA_snd_device_name_hint)(int, const char *, void ***);
  74. static char *(*ALSA_snd_device_name_get_hint)(const void *, const char *);
  75. static int (*ALSA_snd_device_name_free_hint)(void **);
  76. static snd_pcm_sframes_t (*ALSA_snd_pcm_avail)(snd_pcm_t *);
  77. static size_t (*ALSA_snd_ctl_card_info_sizeof)(void);
  78. static size_t (*ALSA_snd_pcm_info_sizeof)(void);
  79. static int (*ALSA_snd_card_next)(int*);
  80. static int (*ALSA_snd_ctl_open)(snd_ctl_t **,const char *,int);
  81. static int (*ALSA_snd_ctl_close)(snd_ctl_t *);
  82. static int (*ALSA_snd_ctl_card_info)(snd_ctl_t *, snd_ctl_card_info_t *);
  83. static int (*ALSA_snd_ctl_pcm_next_device)(snd_ctl_t *, int *);
  84. static unsigned int (*ALSA_snd_pcm_info_get_subdevices_count)(const snd_pcm_info_t *);
  85. static void (*ALSA_snd_pcm_info_set_device)(snd_pcm_info_t *, unsigned int);
  86. static void (*ALSA_snd_pcm_info_set_subdevice)(snd_pcm_info_t *, unsigned int);
  87. static void (*ALSA_snd_pcm_info_set_stream)(snd_pcm_info_t *, snd_pcm_stream_t);
  88. static int (*ALSA_snd_ctl_pcm_info)(snd_ctl_t *, snd_pcm_info_t *);
  89. static unsigned int (*ALSA_snd_pcm_info_get_subdevices_count)(const snd_pcm_info_t *);
  90. static const char *(*ALSA_snd_ctl_card_info_get_id)(const snd_ctl_card_info_t *);
  91. static const char *(*ALSA_snd_pcm_info_get_name)(const snd_pcm_info_t *);
  92. static const char *(*ALSA_snd_pcm_info_get_subdevice_name)(const snd_pcm_info_t *);
  93. static const char *(*ALSA_snd_ctl_card_info_get_name)(const snd_ctl_card_info_t *);
  94. static void (*ALSA_snd_ctl_card_info_clear)(snd_ctl_card_info_t *);
  95. static int (*ALSA_snd_pcm_hw_free)(snd_pcm_t *);
  96. static int (*ALSA_snd_pcm_hw_params_set_channels_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *);
  97. static snd_pcm_chmap_query_t **(*ALSA_snd_pcm_query_chmaps)(snd_pcm_t *pcm);
  98. static void (*ALSA_snd_pcm_free_chmaps)(snd_pcm_chmap_query_t **maps);
  99. static int (*ALSA_snd_pcm_set_chmap)(snd_pcm_t *, const snd_pcm_chmap_t *);
  100. static int (*ALSA_snd_pcm_chmap_print)(const snd_pcm_chmap_t *, size_t, char *);
  101. #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  102. #define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof
  103. #define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof
  104. static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
  105. static SDL_SharedObject *alsa_handle = NULL;
  106. static bool load_alsa_sym(const char *fn, void **addr)
  107. {
  108. *addr = SDL_LoadFunction(alsa_handle, fn);
  109. if (!*addr) {
  110. // Don't call SDL_SetError(): SDL_LoadFunction already did.
  111. return false;
  112. }
  113. return true;
  114. }
  115. // cast funcs to char* first, to please GCC's strict aliasing rules.
  116. #define SDL_ALSA_SYM(x) \
  117. if (!load_alsa_sym(#x, (void **)(char *)&ALSA_##x)) \
  118. return false
  119. #else
  120. #define SDL_ALSA_SYM(x) ALSA_##x = x
  121. #endif
  122. static bool load_alsa_syms(void)
  123. {
  124. SDL_ALSA_SYM(snd_pcm_open);
  125. SDL_ALSA_SYM(snd_pcm_close);
  126. SDL_ALSA_SYM(snd_pcm_start);
  127. SDL_ALSA_SYM(snd_pcm_writei);
  128. SDL_ALSA_SYM(snd_pcm_readi);
  129. SDL_ALSA_SYM(snd_pcm_recover);
  130. SDL_ALSA_SYM(snd_pcm_prepare);
  131. SDL_ALSA_SYM(snd_pcm_drain);
  132. SDL_ALSA_SYM(snd_strerror);
  133. SDL_ALSA_SYM(snd_pcm_hw_params_sizeof);
  134. SDL_ALSA_SYM(snd_pcm_sw_params_sizeof);
  135. SDL_ALSA_SYM(snd_pcm_hw_params_copy);
  136. SDL_ALSA_SYM(snd_pcm_hw_params_any);
  137. SDL_ALSA_SYM(snd_pcm_hw_params_set_access);
  138. SDL_ALSA_SYM(snd_pcm_hw_params_set_format);
  139. SDL_ALSA_SYM(snd_pcm_hw_params_set_channels);
  140. SDL_ALSA_SYM(snd_pcm_hw_params_get_channels);
  141. SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
  142. SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
  143. SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
  144. SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_min);
  145. SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_first);
  146. SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
  147. SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
  148. SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
  149. SDL_ALSA_SYM(snd_pcm_hw_params);
  150. SDL_ALSA_SYM(snd_pcm_sw_params_current);
  151. SDL_ALSA_SYM(snd_pcm_sw_params_set_start_threshold);
  152. SDL_ALSA_SYM(snd_pcm_sw_params);
  153. SDL_ALSA_SYM(snd_pcm_nonblock);
  154. SDL_ALSA_SYM(snd_pcm_wait);
  155. SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
  156. SDL_ALSA_SYM(snd_pcm_reset);
  157. SDL_ALSA_SYM(snd_device_name_hint);
  158. SDL_ALSA_SYM(snd_device_name_get_hint);
  159. SDL_ALSA_SYM(snd_device_name_free_hint);
  160. SDL_ALSA_SYM(snd_pcm_avail);
  161. SDL_ALSA_SYM(snd_ctl_card_info_sizeof);
  162. SDL_ALSA_SYM(snd_pcm_info_sizeof);
  163. SDL_ALSA_SYM(snd_card_next);
  164. SDL_ALSA_SYM(snd_ctl_open);
  165. SDL_ALSA_SYM(snd_ctl_close);
  166. SDL_ALSA_SYM(snd_ctl_card_info);
  167. SDL_ALSA_SYM(snd_ctl_pcm_next_device);
  168. SDL_ALSA_SYM(snd_pcm_info_get_subdevices_count);
  169. SDL_ALSA_SYM(snd_pcm_info_set_device);
  170. SDL_ALSA_SYM(snd_pcm_info_set_subdevice);
  171. SDL_ALSA_SYM(snd_pcm_info_set_stream);
  172. SDL_ALSA_SYM(snd_ctl_pcm_info);
  173. SDL_ALSA_SYM(snd_pcm_info_get_subdevices_count);
  174. SDL_ALSA_SYM(snd_ctl_card_info_get_id);
  175. SDL_ALSA_SYM(snd_pcm_info_get_name);
  176. SDL_ALSA_SYM(snd_pcm_info_get_subdevice_name);
  177. SDL_ALSA_SYM(snd_ctl_card_info_get_name);
  178. SDL_ALSA_SYM(snd_ctl_card_info_clear);
  179. SDL_ALSA_SYM(snd_pcm_hw_free);
  180. SDL_ALSA_SYM(snd_pcm_hw_params_set_channels_near);
  181. SDL_ALSA_SYM(snd_pcm_query_chmaps);
  182. SDL_ALSA_SYM(snd_pcm_free_chmaps);
  183. SDL_ALSA_SYM(snd_pcm_set_chmap);
  184. SDL_ALSA_SYM(snd_pcm_chmap_print);
  185. return true;
  186. }
  187. #undef SDL_ALSA_SYM
  188. #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  189. static void UnloadALSALibrary(void)
  190. {
  191. if (alsa_handle) {
  192. SDL_UnloadObject(alsa_handle);
  193. alsa_handle = NULL;
  194. }
  195. }
  196. static bool LoadALSALibrary(void)
  197. {
  198. bool retval = true;
  199. if (!alsa_handle) {
  200. alsa_handle = SDL_LoadObject(alsa_library);
  201. if (!alsa_handle) {
  202. retval = false;
  203. // Don't call SDL_SetError(): SDL_LoadObject already did.
  204. } else {
  205. retval = load_alsa_syms();
  206. if (!retval) {
  207. UnloadALSALibrary();
  208. }
  209. }
  210. }
  211. return retval;
  212. }
  213. #else
  214. static void UnloadALSALibrary(void)
  215. {
  216. }
  217. static bool LoadALSALibrary(void)
  218. {
  219. load_alsa_syms();
  220. return true;
  221. }
  222. #endif // SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  223. static const char *ALSA_device_prefix = NULL;
  224. static void ALSA_guess_device_prefix(void)
  225. {
  226. if (ALSA_device_prefix) {
  227. return; // already calculated.
  228. }
  229. // Apparently there are several different ways that ALSA lists
  230. // actual hardware. It could be prefixed with "hw:" or "default:"
  231. // or "sysdefault:" and maybe others. Go through the list and see
  232. // if we can find a preferred prefix for the system.
  233. static const char *const prefixes[] = {
  234. "hw:", "sysdefault:", "default:"
  235. };
  236. void **hints = NULL;
  237. if (ALSA_snd_device_name_hint(-1, "pcm", &hints) == 0) {
  238. for (int i = 0; hints[i]; i++) {
  239. char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
  240. if (name) {
  241. for (int j = 0; j < SDL_arraysize(prefixes); j++) {
  242. const char *prefix = prefixes[j];
  243. const size_t prefixlen = SDL_strlen(prefix);
  244. if (SDL_strncmp(name, prefix, prefixlen) == 0) {
  245. ALSA_device_prefix = prefix;
  246. break;
  247. }
  248. }
  249. free(name); // This should NOT be SDL_free()
  250. if (ALSA_device_prefix) {
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. if (!ALSA_device_prefix) {
  257. ALSA_device_prefix = prefixes[0]; // oh well.
  258. }
  259. LOGDEBUG("device prefix is probably '%s'", ALSA_device_prefix);
  260. }
  261. typedef struct ALSA_Device
  262. {
  263. // the unicity key is the couple (id,recording)
  264. char *id; // empty means canonical default
  265. char *name;
  266. bool recording;
  267. struct ALSA_Device *next;
  268. } ALSA_Device;
  269. static const ALSA_Device default_playback_handle = {
  270. "",
  271. "default",
  272. false,
  273. NULL
  274. };
  275. static const ALSA_Device default_recording_handle = {
  276. "",
  277. "default",
  278. true,
  279. NULL
  280. };
  281. // TODO: Figure out the "right"(TM) way. For the moment we presume that if a system is using a
  282. // software mixer for application audio sharing which is not the linux native alsa[dmix], for
  283. // instance jack/pulseaudio2[pipewire]/pulseaudio1/esound/etc, we expect the system integrators did
  284. // configure the canonical default to the right alsa PCM plugin for their software mixer.
  285. //
  286. // All the above may be completely wrong.
  287. static char *get_pcm_str(void *handle)
  288. {
  289. SDL_assert(handle != NULL); // SDL2 used NULL to mean "default" but that's not true in SDL3.
  290. ALSA_Device *dev = (ALSA_Device *)handle;
  291. char *pcm_str = NULL;
  292. if (SDL_strlen(dev->id) == 0) {
  293. // If the user does not want to go thru the default PCM or the canonical default, the
  294. // the configuration space being _massive_, give the user the ability to specify
  295. // its own PCMs using environment variables. It will have to fit SDL constraints though.
  296. const char *devname = SDL_GetHint(dev->recording ? SDL_HINT_AUDIO_ALSA_DEFAULT_RECORDING_DEVICE : SDL_HINT_AUDIO_ALSA_DEFAULT_PLAYBACK_DEVICE);
  297. if (!devname) {
  298. devname = SDL_GetHint(SDL_HINT_AUDIO_ALSA_DEFAULT_DEVICE);
  299. if (!devname) {
  300. devname = "default";
  301. }
  302. }
  303. pcm_str = SDL_strdup(devname);
  304. } else {
  305. SDL_asprintf(&pcm_str, "%sCARD=%s", ALSA_device_prefix, dev->id);
  306. }
  307. return pcm_str;
  308. }
  309. // This function waits until it is possible to write a full sound buffer
  310. static bool ALSA_WaitDevice(SDL_AudioDevice *device)
  311. {
  312. const int fulldelay = (int) ((((Uint64) device->sample_frames) * 1000) / device->spec.freq);
  313. const int delay = SDL_max(fulldelay, 10);
  314. while (!SDL_GetAtomicInt(&device->shutdown)) {
  315. const int rc = ALSA_snd_pcm_wait(device->hidden->pcm, delay);
  316. if (rc < 0 && (rc != -EAGAIN)) {
  317. const int status = ALSA_snd_pcm_recover(device->hidden->pcm, rc, 0);
  318. if (status < 0) {
  319. // Hmm, not much we can do - abort
  320. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA: snd_pcm_wait failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  321. return false;
  322. }
  323. continue;
  324. }
  325. if (rc > 0) {
  326. break; // ready to go!
  327. }
  328. // Timed out! Make sure we aren't shutting down and then wait again.
  329. }
  330. return true;
  331. }
  332. static bool ALSA_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buflen)
  333. {
  334. SDL_assert(buffer == device->hidden->mixbuf);
  335. Uint8 *sample_buf = (Uint8 *) buffer; // !!! FIXME: deal with this without casting away constness
  336. const int frame_size = SDL_AUDIO_FRAMESIZE(device->spec);
  337. snd_pcm_uframes_t frames_left = (snd_pcm_uframes_t) (buflen / frame_size);
  338. while ((frames_left > 0) && !SDL_GetAtomicInt(&device->shutdown)) {
  339. const int rc = ALSA_snd_pcm_writei(device->hidden->pcm, sample_buf, frames_left);
  340. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA PLAYDEVICE: WROTE %d of %d bytes", (rc >= 0) ? ((int) (rc * frame_size)) : rc, (int) (frames_left * frame_size));
  341. SDL_assert(rc != 0); // assuming this can't happen if we used snd_pcm_wait and queried for available space.
  342. if (rc < 0) {
  343. SDL_assert(rc != -EAGAIN); // assuming this can't happen if we used snd_pcm_wait and queried for available space. snd_pcm_recover won't handle it!
  344. const int status = ALSA_snd_pcm_recover(device->hidden->pcm, rc, 0);
  345. if (status < 0) {
  346. // Hmm, not much we can do - abort
  347. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA write failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  348. return false;
  349. }
  350. continue;
  351. }
  352. sample_buf += rc * frame_size;
  353. frames_left -= rc;
  354. }
  355. return true;
  356. }
  357. static Uint8 *ALSA_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
  358. {
  359. snd_pcm_sframes_t rc = ALSA_snd_pcm_avail(device->hidden->pcm);
  360. if (rc <= 0) {
  361. // Wait a bit and try again, maybe the hardware isn't quite ready yet?
  362. SDL_Delay(1);
  363. rc = ALSA_snd_pcm_avail(device->hidden->pcm);
  364. if (rc <= 0) {
  365. // We'll catch it next time
  366. *buffer_size = 0;
  367. return NULL;
  368. }
  369. }
  370. const int requested_frames = SDL_min(device->sample_frames, rc);
  371. const int requested_bytes = requested_frames * SDL_AUDIO_FRAMESIZE(device->spec);
  372. SDL_assert(requested_bytes <= *buffer_size);
  373. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA GETDEVICEBUF: NEED %d BYTES", requested_bytes);
  374. *buffer_size = requested_bytes;
  375. return device->hidden->mixbuf;
  376. }
  377. static int ALSA_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen)
  378. {
  379. const int frame_size = SDL_AUDIO_FRAMESIZE(device->spec);
  380. SDL_assert((buflen % frame_size) == 0);
  381. const snd_pcm_sframes_t total_available = ALSA_snd_pcm_avail(device->hidden->pcm);
  382. const int total_frames = SDL_min(buflen / frame_size, total_available);
  383. const int rc = ALSA_snd_pcm_readi(device->hidden->pcm, buffer, total_frames);
  384. SDL_assert(rc != -EAGAIN); // assuming this can't happen if we used snd_pcm_wait and queried for available space. snd_pcm_recover won't handle it!
  385. if (rc < 0) {
  386. const int status = ALSA_snd_pcm_recover(device->hidden->pcm, rc, 0);
  387. if (status < 0) {
  388. // Hmm, not much we can do - abort
  389. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA read failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  390. return -1;
  391. }
  392. return 0; // go back to WaitDevice and try again.
  393. }
  394. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: recorded %d bytes", rc * frame_size);
  395. return rc * frame_size;
  396. }
  397. static void ALSA_FlushRecording(SDL_AudioDevice *device)
  398. {
  399. ALSA_snd_pcm_reset(device->hidden->pcm);
  400. }
  401. static void ALSA_CloseDevice(SDL_AudioDevice *device)
  402. {
  403. if (device->hidden) {
  404. if (device->hidden->pcm) {
  405. // Wait for the submitted audio to drain. ALSA_snd_pcm_drop() can hang, so don't use that.
  406. SDL_Delay(((device->sample_frames * 1000) / device->spec.freq) * 2);
  407. ALSA_snd_pcm_close(device->hidden->pcm);
  408. }
  409. SDL_free(device->hidden->mixbuf);
  410. SDL_free(device->hidden);
  411. }
  412. }
  413. // To make easier to track parameters during the whole alsa pcm configuration:
  414. struct ALSA_pcm_cfg_ctx {
  415. SDL_AudioDevice *device;
  416. snd_pcm_hw_params_t *hwparams;
  417. snd_pcm_sw_params_t *swparams;
  418. SDL_AudioFormat matched_sdl_format;
  419. unsigned int chans_n;
  420. unsigned int target_chans_n;
  421. unsigned int rate;
  422. snd_pcm_uframes_t persize; // alsa period size, SDL audio device sample_frames
  423. snd_pcm_chmap_query_t **chmap_queries;
  424. unsigned int sdl_chmap[SDL_AUDIO_ALSA__CHMAP_CHANS_N_MAX];
  425. unsigned int alsa_chmap_installed[SDL_AUDIO_ALSA__CHMAP_CHANS_N_MAX];
  426. unsigned int periods;
  427. };
  428. // The following are SDL channel maps with alsa position values, from 0 channels to 8 channels.
  429. // See SDL3/SDL_audio.h
  430. // Strictly speaking those are "parameters" of channel maps, like alsa hwparams and swparams, they
  431. // have to be "reduced/refined" until an exact channel map. Only the 6 channels map requires such
  432. // "reduction/refine".
  433. static enum snd_pcm_chmap_position sdl_channel_maps[SDL_AUDIO_ALSA__SDL_CHMAPS_N][SDL_AUDIO_ALSA__CHMAP_CHANS_N_MAX] = {
  434. // 0 channels
  435. {
  436. 0
  437. },
  438. // 1 channel
  439. {
  440. SND_CHMAP_MONO,
  441. },
  442. // 2 channels
  443. {
  444. SND_CHMAP_FL,
  445. SND_CHMAP_FR,
  446. },
  447. // 3 channels
  448. {
  449. SND_CHMAP_FL,
  450. SND_CHMAP_FR,
  451. SND_CHMAP_LFE,
  452. },
  453. // 4 channels
  454. {
  455. SND_CHMAP_FL,
  456. SND_CHMAP_FR,
  457. SND_CHMAP_RL,
  458. SND_CHMAP_RR,
  459. },
  460. // 5 channels
  461. {
  462. SND_CHMAP_FL,
  463. SND_CHMAP_FR,
  464. SND_CHMAP_LFE,
  465. SND_CHMAP_RL,
  466. SND_CHMAP_RR,
  467. },
  468. // 6 channels
  469. // XXX: here we encode not a uniq channel map but a set of channel maps. We will reduce it each
  470. // time we are going to work with an alsa 6 channels map.
  471. {
  472. SND_CHMAP_FL,
  473. SND_CHMAP_FR,
  474. SND_CHMAP_FC,
  475. SND_CHMAP_LFE,
  476. // The 2 following channel positions are (SND_CHMAP_SL,SND_CHMAP_SR) or
  477. // (SND_CHMAP_RL,SND_CHMAP_RR)
  478. SND_CHMAP_UNKNOWN,
  479. SND_CHMAP_UNKNOWN,
  480. },
  481. // 7 channels
  482. {
  483. SND_CHMAP_FL,
  484. SND_CHMAP_FR,
  485. SND_CHMAP_FC,
  486. SND_CHMAP_LFE,
  487. SND_CHMAP_RC,
  488. SND_CHMAP_SL,
  489. SND_CHMAP_SR,
  490. },
  491. // 8 channels
  492. {
  493. SND_CHMAP_FL,
  494. SND_CHMAP_FR,
  495. SND_CHMAP_FC,
  496. SND_CHMAP_LFE,
  497. SND_CHMAP_RL,
  498. SND_CHMAP_RR,
  499. SND_CHMAP_SL,
  500. SND_CHMAP_SR,
  501. },
  502. };
  503. // Helper for the function right below.
  504. static bool has_pos(unsigned int *chmap, unsigned int pos)
  505. {
  506. for (unsigned int chan_idx = 0; ; chan_idx++) {
  507. if (chan_idx == 6) {
  508. return false;
  509. }
  510. if (chmap[chan_idx] == pos) {
  511. return true;
  512. }
  513. }
  514. SDL_assert(!"Shouldn't hit this code.");
  515. return false;
  516. }
  517. // XXX: Each time we are going to work on an alsa 6 channels map, we must reduce the set of channel
  518. // maps which is encoded in sdl_channel_maps[6] to a uniq one.
  519. #define HAVE_NONE 0
  520. #define HAVE_REAR 1
  521. #define HAVE_SIDE 2
  522. #define HAVE_BOTH 3
  523. static void sdl_6chans_set_rear_or_side_channels_from_alsa_6chans(unsigned int *sdl_6chans, unsigned int *alsa_6chans)
  524. {
  525. // For alsa channel maps with 6 channels and with SND_CHMAP_FL,SND_CHMAP_FR,SND_CHMAP_FC,
  526. // SND_CHMAP_LFE, reduce our 6 channels maps to a uniq one.
  527. if ( !has_pos(alsa_6chans, SND_CHMAP_FL) ||
  528. !has_pos(alsa_6chans, SND_CHMAP_FR) ||
  529. !has_pos(alsa_6chans, SND_CHMAP_FC) ||
  530. !has_pos(alsa_6chans, SND_CHMAP_LFE)) {
  531. sdl_6chans[4] = SND_CHMAP_UNKNOWN;
  532. sdl_6chans[5] = SND_CHMAP_UNKNOWN;
  533. LOGDEBUG("6channels:unsupported channel map");
  534. return;
  535. }
  536. unsigned int state = HAVE_NONE;
  537. for (unsigned int chan_idx = 0; chan_idx < 6; chan_idx++) {
  538. if ((alsa_6chans[chan_idx] == SND_CHMAP_SL) || (alsa_6chans[chan_idx] == SND_CHMAP_SR)) {
  539. if (state == HAVE_NONE) {
  540. state = HAVE_SIDE;
  541. } else if (state == HAVE_REAR) {
  542. state = HAVE_BOTH;
  543. break;
  544. }
  545. } else if ((alsa_6chans[chan_idx] == SND_CHMAP_RL) || (alsa_6chans[chan_idx] == SND_CHMAP_RR)) {
  546. if (state == HAVE_NONE) {
  547. state = HAVE_REAR;
  548. } else if (state == HAVE_SIDE) {
  549. state = HAVE_BOTH;
  550. break;
  551. }
  552. }
  553. }
  554. if ((state == HAVE_BOTH) || (state == HAVE_NONE)) {
  555. sdl_6chans[4] = SND_CHMAP_UNKNOWN;
  556. sdl_6chans[5] = SND_CHMAP_UNKNOWN;
  557. LOGDEBUG("6channels:unsupported channel map");
  558. } else if (state == HAVE_REAR) {
  559. sdl_6chans[4] = SND_CHMAP_RL;
  560. sdl_6chans[5] = SND_CHMAP_RR;
  561. LOGDEBUG("6channels:sdl map set to rear");
  562. } else { // state == HAVE_SIDE
  563. sdl_6chans[4] = SND_CHMAP_SL;
  564. sdl_6chans[5] = SND_CHMAP_SR;
  565. LOGDEBUG("6channels:sdl map set to side");
  566. }
  567. }
  568. #undef HAVE_NONE
  569. #undef HAVE_REAR
  570. #undef HAVE_SIDE
  571. #undef HAVE_BOTH
  572. static void swizzle_map_compute_alsa_subscan(struct ALSA_pcm_cfg_ctx *ctx, int *swizzle_map, unsigned int sdl_pos_idx)
  573. {
  574. swizzle_map[sdl_pos_idx] = -1;
  575. for (unsigned int alsa_pos_idx = 0; ; alsa_pos_idx++) {
  576. SDL_assert(alsa_pos_idx != ctx->chans_n); // no 0 channels or not found matching position should happen here (actually enforce playback/recording symmetry).
  577. if (ctx->alsa_chmap_installed[alsa_pos_idx] == ctx->sdl_chmap[sdl_pos_idx]) {
  578. LOGDEBUG("swizzle SDL %u <-> alsa %u", sdl_pos_idx,alsa_pos_idx);
  579. swizzle_map[sdl_pos_idx] = (int) alsa_pos_idx;
  580. return;
  581. }
  582. }
  583. }
  584. // XXX: this must stay playback/recording symetric.
  585. static void swizzle_map_compute(struct ALSA_pcm_cfg_ctx *ctx, int *swizzle_map, bool *needs_swizzle)
  586. {
  587. *needs_swizzle = false;
  588. for (unsigned int sdl_pos_idx = 0; sdl_pos_idx != ctx->chans_n; sdl_pos_idx++) {
  589. swizzle_map_compute_alsa_subscan(ctx, swizzle_map, sdl_pos_idx);
  590. if (swizzle_map[sdl_pos_idx] != sdl_pos_idx) {
  591. *needs_swizzle = true;
  592. }
  593. }
  594. }
  595. #define CHMAP_INSTALLED 0
  596. #define CHANS_N_NEXT 1
  597. #define CHMAP_NOT_FOUND 2
  598. // Should always be a queried alsa channel map unless the queried alsa channel map was of type VAR,
  599. // namely we can program the channel positions directly from the SDL channel map.
  600. static int alsa_chmap_install(struct ALSA_pcm_cfg_ctx *ctx, unsigned int *chmap)
  601. {
  602. bool isstack;
  603. snd_pcm_chmap_t *chmap_to_install = (snd_pcm_chmap_t*)SDL_small_alloc(unsigned int, 1 + ctx->chans_n, &isstack);
  604. if (!chmap_to_install) {
  605. return -1;
  606. }
  607. chmap_to_install->channels = ctx->chans_n;
  608. SDL_memcpy(chmap_to_install->pos, chmap, sizeof (unsigned int) * ctx->chans_n);
  609. #if SDL_ALSA_DEBUG
  610. char logdebug_chmap_str[128];
  611. ALSA_snd_pcm_chmap_print(chmap_to_install,sizeof(logdebug_chmap_str),logdebug_chmap_str);
  612. LOGDEBUG("channel map to install:%s",logdebug_chmap_str);
  613. #endif
  614. int status = ALSA_snd_pcm_set_chmap(ctx->device->hidden->pcm, chmap_to_install);
  615. if (status < 0) {
  616. SDL_SetError("ALSA: failed to install channel map: %s", ALSA_snd_strerror(status));
  617. return -1;
  618. }
  619. SDL_memcpy(ctx->alsa_chmap_installed, chmap, ctx->chans_n * sizeof (unsigned int));
  620. SDL_small_free(chmap_to_install, isstack);
  621. return CHMAP_INSTALLED;
  622. }
  623. // We restrict the alsa channel maps because in the unordered matches we do only simple accounting.
  624. // In the end, this will handle mostly alsa channel maps with more than one SND_CHMAP_NA position fillers.
  625. static bool alsa_chmap_has_duplicate_position(struct ALSA_pcm_cfg_ctx *ctx, unsigned int *pos)
  626. {
  627. if (ctx->chans_n < 2) {// we need at least 2 positions
  628. LOGDEBUG("channel map:no duplicate");
  629. return false;
  630. }
  631. for (unsigned int chan_idx = 1; chan_idx != ctx->chans_n; chan_idx++) {
  632. for (unsigned int seen_idx = 0; seen_idx != chan_idx; seen_idx++) {
  633. if (pos[seen_idx] == pos[chan_idx]) {
  634. LOGDEBUG("channel map:have duplicate");
  635. return true;
  636. }
  637. }
  638. }
  639. LOGDEBUG("channel map:no duplicate");
  640. return false;
  641. }
  642. static int alsa_chmap_cfg_ordered_fixed_or_paired(struct ALSA_pcm_cfg_ctx *ctx)
  643. {
  644. for (snd_pcm_chmap_query_t **chmap_query = ctx->chmap_queries; *chmap_query; chmap_query++) {
  645. if ( ((*chmap_query)->map.channels != ctx->chans_n) ||
  646. (((*chmap_query)->type != SND_CHMAP_TYPE_FIXED) && ((*chmap_query)->type != SND_CHMAP_TYPE_PAIRED)) ) {
  647. continue;
  648. }
  649. #if SDL_ALSA_DEBUG
  650. char logdebug_chmap_str[128];
  651. ALSA_snd_pcm_chmap_print(&(*chmap_query)->map,sizeof(logdebug_chmap_str),logdebug_chmap_str);
  652. LOGDEBUG("channel map:ordered:fixed|paired:%s",logdebug_chmap_str);
  653. #endif
  654. for (int i = 0; i < ctx->chans_n; i++) {
  655. ctx->sdl_chmap[i] = (unsigned int) sdl_channel_maps[ctx->chans_n][i];
  656. }
  657. unsigned int *alsa_chmap = (*chmap_query)->map.pos;
  658. if (ctx->chans_n == 6) {
  659. sdl_6chans_set_rear_or_side_channels_from_alsa_6chans(ctx->sdl_chmap, alsa_chmap);
  660. }
  661. if (alsa_chmap_has_duplicate_position(ctx, alsa_chmap)) {
  662. continue;
  663. }
  664. for (unsigned int chan_idx = 0; ctx->sdl_chmap[chan_idx] == alsa_chmap[chan_idx]; chan_idx++) {
  665. if (chan_idx == ctx->chans_n) {
  666. return alsa_chmap_install(ctx, alsa_chmap);
  667. }
  668. }
  669. }
  670. return CHMAP_NOT_FOUND;
  671. }
  672. // Here, the alsa channel positions can be programmed in the alsa frame (cf HDMI).
  673. // If the alsa channel map is VAR, we only check we have the unordered set of channel positions we
  674. // are looking for.
  675. static int alsa_chmap_cfg_ordered_var(struct ALSA_pcm_cfg_ctx *ctx)
  676. {
  677. for (snd_pcm_chmap_query_t **chmap_query = ctx->chmap_queries; *chmap_query; chmap_query++) {
  678. if (((*chmap_query)->map.channels != ctx->chans_n) || ((*chmap_query)->type != SND_CHMAP_TYPE_VAR)) {
  679. continue;
  680. }
  681. #if SDL_ALSA_DEBUG
  682. char logdebug_chmap_str[128];
  683. ALSA_snd_pcm_chmap_print(&(*chmap_query)->map,sizeof(logdebug_chmap_str),logdebug_chmap_str);
  684. LOGDEBUG("channel map:ordered:var:%s",logdebug_chmap_str);
  685. #endif
  686. for (int i = 0; i < ctx->chans_n; i++) {
  687. ctx->sdl_chmap[i] = (unsigned int) sdl_channel_maps[ctx->chans_n][i];
  688. }
  689. unsigned int *alsa_chmap = (*chmap_query)->map.pos;
  690. if (ctx->chans_n == 6) {
  691. sdl_6chans_set_rear_or_side_channels_from_alsa_6chans(ctx->sdl_chmap, alsa_chmap);
  692. }
  693. if (alsa_chmap_has_duplicate_position(ctx, alsa_chmap)) {
  694. continue;
  695. }
  696. unsigned int pos_matches_n = 0;
  697. for (unsigned int chan_idx = 0; chan_idx != ctx->chans_n; chan_idx++) {
  698. for (unsigned int subscan_chan_idx = 0; subscan_chan_idx != ctx->chans_n; subscan_chan_idx++) {
  699. if (ctx->sdl_chmap[chan_idx] == alsa_chmap[subscan_chan_idx]) {
  700. pos_matches_n++;
  701. break;
  702. }
  703. }
  704. }
  705. if (pos_matches_n == ctx->chans_n) {
  706. return alsa_chmap_install(ctx, ctx->sdl_chmap); // XXX: we program the SDL chmap here
  707. }
  708. }
  709. return CHMAP_NOT_FOUND;
  710. }
  711. static int alsa_chmap_cfg_ordered(struct ALSA_pcm_cfg_ctx *ctx)
  712. {
  713. const int status = alsa_chmap_cfg_ordered_fixed_or_paired(ctx);
  714. return (status != CHMAP_NOT_FOUND) ? status : alsa_chmap_cfg_ordered_var(ctx);
  715. }
  716. // In the unordered case, we are just interested to get the same unordered set of alsa channel
  717. // positions than in the SDL channel map since we will swizzle (no duplicate channel position).
  718. static int alsa_chmap_cfg_unordered(struct ALSA_pcm_cfg_ctx *ctx)
  719. {
  720. for (snd_pcm_chmap_query_t **chmap_query = ctx->chmap_queries; *chmap_query; chmap_query++) {
  721. if ( ((*chmap_query)->map.channels != ctx->chans_n) ||
  722. (((*chmap_query)->type != SND_CHMAP_TYPE_FIXED) && ((*chmap_query)->type != SND_CHMAP_TYPE_PAIRED)) ) {
  723. continue;
  724. }
  725. #if SDL_ALSA_DEBUG
  726. char logdebug_chmap_str[128];
  727. ALSA_snd_pcm_chmap_print(&(*chmap_query)->map,sizeof(logdebug_chmap_str),logdebug_chmap_str);
  728. LOGDEBUG("channel map:unordered:fixed|paired:%s",logdebug_chmap_str);
  729. #endif
  730. for (int i = 0; i < ctx->chans_n; i++) {
  731. ctx->sdl_chmap[i] = (unsigned int) sdl_channel_maps[ctx->chans_n][i];
  732. }
  733. unsigned int *alsa_chmap = (*chmap_query)->map.pos;
  734. if (ctx->chans_n == 6) {
  735. sdl_6chans_set_rear_or_side_channels_from_alsa_6chans(ctx->sdl_chmap, alsa_chmap);
  736. }
  737. if (alsa_chmap_has_duplicate_position(ctx, alsa_chmap)) {
  738. continue;
  739. }
  740. unsigned int pos_matches_n = 0;
  741. for (unsigned int chan_idx = 0; chan_idx != ctx->chans_n; chan_idx++) {
  742. for (unsigned int subscan_chan_idx = 0; subscan_chan_idx != ctx->chans_n; subscan_chan_idx++) {
  743. if (ctx->sdl_chmap[chan_idx] == alsa_chmap[subscan_chan_idx]) {
  744. pos_matches_n++;
  745. break;
  746. }
  747. }
  748. }
  749. if (pos_matches_n == ctx->chans_n) {
  750. return alsa_chmap_install(ctx, alsa_chmap);
  751. }
  752. }
  753. return CHMAP_NOT_FOUND;
  754. }
  755. static int alsa_chmap_cfg(struct ALSA_pcm_cfg_ctx *ctx)
  756. {
  757. int status;
  758. ctx->chmap_queries = ALSA_snd_pcm_query_chmaps(ctx->device->hidden->pcm);
  759. if (ctx->chmap_queries == NULL) {
  760. // We couldn't query the channel map, assume no swizzle necessary
  761. LOGDEBUG("couldn't query channel map, swizzling off");
  762. return CHMAP_INSTALLED;
  763. }
  764. //----------------------------------------------------------------------------------------------
  765. status = alsa_chmap_cfg_ordered(ctx); // we prefer first channel maps we don't need to swizzle
  766. if (status == CHMAP_INSTALLED) {
  767. LOGDEBUG("swizzling off");
  768. return status;
  769. } else if (status != CHMAP_NOT_FOUND) {
  770. return status; // < 0 error code
  771. }
  772. // Fall-thru
  773. //----------------------------------------------------------------------------------------------
  774. status = alsa_chmap_cfg_unordered(ctx); // those we will have to swizzle
  775. if (status == CHMAP_INSTALLED) {
  776. LOGDEBUG("swizzling on");
  777. bool isstack;
  778. int *swizzle_map = SDL_small_alloc(int, ctx->chans_n, &isstack);
  779. if (!swizzle_map) {
  780. status = -1;
  781. } else {
  782. bool needs_swizzle;
  783. swizzle_map_compute(ctx, swizzle_map, &needs_swizzle); // fine grained swizzle configuration
  784. if (needs_swizzle) {
  785. // let SDL's swizzler handle this one.
  786. ctx->device->chmap = SDL_ChannelMapDup(swizzle_map, ctx->chans_n);
  787. if (!ctx->device->chmap) {
  788. status = -1;
  789. }
  790. }
  791. SDL_small_free(swizzle_map, isstack);
  792. }
  793. }
  794. if (status == CHMAP_NOT_FOUND) {
  795. return CHANS_N_NEXT;
  796. }
  797. return status; // < 0 error code
  798. }
  799. #define CHANS_N_SCAN_MODE__EQUAL_OR_ABOVE_REQUESTED_CHANS_N 0 // target more hardware pressure
  800. #define CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N 1 // target less hardware pressure
  801. #define CHANS_N_CONFIGURED 0
  802. #define CHANS_N_NOT_CONFIGURED 1
  803. static int ALSA_pcm_cfg_hw_chans_n_scan(struct ALSA_pcm_cfg_ctx *ctx, unsigned int mode)
  804. {
  805. unsigned int target_chans_n = ctx->device->spec.channels; // we start at what was specified
  806. if (mode == CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N) {
  807. target_chans_n--;
  808. }
  809. while (true) {
  810. if (mode == CHANS_N_SCAN_MODE__EQUAL_OR_ABOVE_REQUESTED_CHANS_N) {
  811. if (target_chans_n > SDL_AUDIO_ALSA__CHMAP_CHANS_N_MAX) {
  812. return CHANS_N_NOT_CONFIGURED;
  813. }
  814. // else: CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N
  815. } else if (target_chans_n == 0) {
  816. return CHANS_N_NOT_CONFIGURED;
  817. }
  818. LOGDEBUG("target chans_n is %u", target_chans_n);
  819. int status = ALSA_snd_pcm_hw_params_any(ctx->device->hidden->pcm, ctx->hwparams);
  820. if (status < 0) {
  821. SDL_SetError("ALSA: Couldn't get hardware config: %s", ALSA_snd_strerror(status));
  822. return -1;
  823. }
  824. // SDL only uses interleaved sample output
  825. status = ALSA_snd_pcm_hw_params_set_access(ctx->device->hidden->pcm, ctx->hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  826. if (status < 0) {
  827. SDL_SetError("ALSA: Couldn't set interleaved access: %s", ALSA_snd_strerror(status));
  828. return -1;
  829. }
  830. // Try for a closest match on audio format
  831. snd_pcm_format_t alsa_format = 0;
  832. const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(ctx->device->spec.format);
  833. ctx->matched_sdl_format = 0;
  834. while ((ctx->matched_sdl_format = *(closefmts++)) != 0) {
  835. // XXX: we are forcing the same endianness, namely we won't need byte swapping upon
  836. // writing/reading to/from the SDL audio buffer.
  837. switch (ctx->matched_sdl_format) {
  838. case SDL_AUDIO_U8:
  839. alsa_format = SND_PCM_FORMAT_U8;
  840. break;
  841. case SDL_AUDIO_S8:
  842. alsa_format = SND_PCM_FORMAT_S8;
  843. break;
  844. case SDL_AUDIO_S16LE:
  845. alsa_format = SND_PCM_FORMAT_S16_LE;
  846. break;
  847. case SDL_AUDIO_S16BE:
  848. alsa_format = SND_PCM_FORMAT_S16_BE;
  849. break;
  850. case SDL_AUDIO_S32LE:
  851. alsa_format = SND_PCM_FORMAT_S32_LE;
  852. break;
  853. case SDL_AUDIO_S32BE:
  854. alsa_format = SND_PCM_FORMAT_S32_BE;
  855. break;
  856. case SDL_AUDIO_F32LE:
  857. alsa_format = SND_PCM_FORMAT_FLOAT_LE;
  858. break;
  859. case SDL_AUDIO_F32BE:
  860. alsa_format = SND_PCM_FORMAT_FLOAT_BE;
  861. break;
  862. default:
  863. continue;
  864. }
  865. if (ALSA_snd_pcm_hw_params_set_format(ctx->device->hidden->pcm, ctx->hwparams, alsa_format) >= 0) {
  866. break;
  867. }
  868. }
  869. if (ctx->matched_sdl_format == 0) {
  870. SDL_SetError("ALSA: Unsupported audio format: %s", ALSA_snd_strerror(status));
  871. return -1;
  872. }
  873. // let alsa approximate the number of channels
  874. ctx->chans_n = target_chans_n;
  875. status = ALSA_snd_pcm_hw_params_set_channels_near(ctx->device->hidden->pcm, ctx->hwparams, &(ctx->chans_n));
  876. if (status < 0) {
  877. SDL_SetError("ALSA: Couldn't set audio channels: %s", ALSA_snd_strerror(status));
  878. return -1;
  879. }
  880. // let alsa approximate the audio rate
  881. ctx->rate = ctx->device->spec.freq;
  882. status = ALSA_snd_pcm_hw_params_set_rate_near(ctx->device->hidden->pcm, ctx->hwparams, &(ctx->rate), NULL);
  883. if (status < 0) {
  884. SDL_SetError("ALSA: Couldn't set audio frequency: %s", ALSA_snd_strerror(status));
  885. return -1;
  886. }
  887. // let approximate the period size to the requested buffer size
  888. ctx->persize = ctx->device->sample_frames;
  889. status = ALSA_snd_pcm_hw_params_set_period_size_near(ctx->device->hidden->pcm, ctx->hwparams, &(ctx->persize), NULL);
  890. if (status < 0) {
  891. SDL_SetError("ALSA: Couldn't set the period size: %s", ALSA_snd_strerror(status));
  892. return -1;
  893. }
  894. // let approximate the minimun number of periods per buffer (we target a double buffer)
  895. ctx->periods = 2;
  896. status = ALSA_snd_pcm_hw_params_set_periods_min(ctx->device->hidden->pcm, ctx->hwparams, &(ctx->periods), NULL);
  897. if (status < 0) {
  898. SDL_SetError("ALSA: Couldn't set the minimum number of periods per buffer: %s", ALSA_snd_strerror(status));
  899. return -1;
  900. }
  901. // restrict the number of periods per buffer to an approximation of the approximated minimum
  902. // number of periods per buffer done right above
  903. status = ALSA_snd_pcm_hw_params_set_periods_first(ctx->device->hidden->pcm, ctx->hwparams, &(ctx->periods), NULL);
  904. if (status < 0) {
  905. SDL_SetError("ALSA: Couldn't set the number of periods per buffer: %s", ALSA_snd_strerror(status));
  906. return -1;
  907. }
  908. // install the hw parameters
  909. status = ALSA_snd_pcm_hw_params(ctx->device->hidden->pcm, ctx->hwparams);
  910. if (status < 0) {
  911. SDL_SetError("ALSA: installation of hardware parameter failed: %s", ALSA_snd_strerror(status));
  912. return -1;
  913. }
  914. //==========================================================================================
  915. // Here the alsa pcm is in SND_PCM_STATE_PREPARED state, let's figure out a good fit for
  916. // SDL channel map, it may request to change the target number of channels though.
  917. status = alsa_chmap_cfg(ctx);
  918. if (status < 0) {
  919. return status; // we forward the SDL error
  920. } else if (status == CHMAP_INSTALLED) {
  921. return CHANS_N_CONFIGURED; // we are finished here
  922. }
  923. // status == CHANS_N_NEXT
  924. ALSA_snd_pcm_free_chmaps(ctx->chmap_queries);
  925. ALSA_snd_pcm_hw_free(ctx->device->hidden->pcm); // uninstall those hw params
  926. if (mode == CHANS_N_SCAN_MODE__EQUAL_OR_ABOVE_REQUESTED_CHANS_N) {
  927. target_chans_n++;
  928. } else { // CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N
  929. target_chans_n--;
  930. }
  931. }
  932. SDL_assert(!"Shouldn't reach this code.");
  933. return CHANS_N_NOT_CONFIGURED;
  934. }
  935. #undef CHMAP_INSTALLED
  936. #undef CHANS_N_NEXT
  937. #undef CHMAP_NOT_FOUND
  938. static bool ALSA_pcm_cfg_hw(struct ALSA_pcm_cfg_ctx *ctx)
  939. {
  940. LOGDEBUG("target chans_n, equal or above requested chans_n mode");
  941. int status = ALSA_pcm_cfg_hw_chans_n_scan(ctx, CHANS_N_SCAN_MODE__EQUAL_OR_ABOVE_REQUESTED_CHANS_N);
  942. if (status < 0) { // something went too wrong
  943. return false;
  944. } else if (status == CHANS_N_CONFIGURED) {
  945. return true;
  946. }
  947. // Here, status == CHANS_N_NOT_CONFIGURED
  948. LOGDEBUG("target chans_n, below requested chans_n mode");
  949. status = ALSA_pcm_cfg_hw_chans_n_scan(ctx, CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N);
  950. if (status < 0) { // something went too wrong
  951. return false;
  952. } else if (status == CHANS_N_CONFIGURED) {
  953. return true;
  954. }
  955. // Here, status == CHANS_N_NOT_CONFIGURED
  956. return SDL_SetError("ALSA: Coudn't configure targetting any SDL supported channel number");
  957. }
  958. #undef CHANS_N_SCAN_MODE__EQUAL_OR_ABOVE_REQUESTED_CHANS_N
  959. #undef CHANS_N_SCAN_MODE__BELOW_REQUESTED_CHANS_N
  960. #undef CHANS_N_CONFIGURED
  961. #undef CHANS_N_NOT_CONFIGURED
  962. static bool ALSA_pcm_cfg_sw(struct ALSA_pcm_cfg_ctx *ctx)
  963. {
  964. int status;
  965. status = ALSA_snd_pcm_sw_params_current(ctx->device->hidden->pcm, ctx->swparams);
  966. if (status < 0) {
  967. return SDL_SetError("ALSA: Couldn't get software config: %s", ALSA_snd_strerror(status));
  968. }
  969. status = ALSA_snd_pcm_sw_params_set_avail_min(ctx->device->hidden->pcm, ctx->swparams, ctx->persize); // will become device->sample_frames if the alsa pcm configuration is successful
  970. if (status < 0) {
  971. return SDL_SetError("Couldn't set minimum available samples: %s", ALSA_snd_strerror(status));
  972. }
  973. status = ALSA_snd_pcm_sw_params_set_start_threshold(ctx->device->hidden->pcm, ctx->swparams, 1);
  974. if (status < 0) {
  975. return SDL_SetError("ALSA: Couldn't set start threshold: %s", ALSA_snd_strerror(status));
  976. }
  977. status = ALSA_snd_pcm_sw_params(ctx->device->hidden->pcm, ctx->swparams);
  978. if (status < 0) {
  979. return SDL_SetError("Couldn't set software audio parameters: %s", ALSA_snd_strerror(status));
  980. }
  981. return true;
  982. }
  983. static bool ALSA_OpenDevice(SDL_AudioDevice *device)
  984. {
  985. const bool recording = device->recording;
  986. struct ALSA_pcm_cfg_ctx cfg_ctx; // used to track everything here
  987. char *pcm_str;
  988. int status = 0;
  989. //device->spec.channels = 8;
  990. //SDL_SetLogPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE);
  991. LOGDEBUG("channels requested %u",device->spec.channels);
  992. // XXX: We do not use the SDL internal swizzler yet.
  993. device->chmap = NULL;
  994. SDL_zero(cfg_ctx);
  995. cfg_ctx.device = device;
  996. // Initialize all variables that we clean on shutdown
  997. cfg_ctx.device->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*cfg_ctx.device->hidden));
  998. if (!cfg_ctx.device->hidden) {
  999. return false;
  1000. }
  1001. // Open the audio device
  1002. pcm_str = get_pcm_str(cfg_ctx.device->handle);
  1003. if (pcm_str == NULL) {
  1004. goto err_free_device_hidden;
  1005. }
  1006. LOGDEBUG("PCM open '%s'", pcm_str);
  1007. status = ALSA_snd_pcm_open(&cfg_ctx.device->hidden->pcm,
  1008. pcm_str,
  1009. recording ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
  1010. SND_PCM_NONBLOCK);
  1011. SDL_free(pcm_str);
  1012. if (status < 0) {
  1013. SDL_SetError("ALSA: Couldn't open audio device: %s", ALSA_snd_strerror(status));
  1014. goto err_free_device_hidden;
  1015. }
  1016. // Now we need to configure the opened pcm as close as possible from the requested parameters we
  1017. // can reasonably deal with (and that could change)
  1018. snd_pcm_hw_params_alloca(&(cfg_ctx.hwparams));
  1019. snd_pcm_sw_params_alloca(&(cfg_ctx.swparams));
  1020. if (!ALSA_pcm_cfg_hw(&cfg_ctx)) { // alsa pcm "hardware" part of the pcm
  1021. goto err_close_pcm;
  1022. }
  1023. // from here, we get only the alsa chmap queries in cfg_ctx to explicitely clean, hwparams is
  1024. // uninstalled upon pcm closing
  1025. // This is useful for debugging
  1026. #if SDL_ALSA_DEBUG
  1027. snd_pcm_uframes_t bufsize;
  1028. ALSA_snd_pcm_hw_params_get_buffer_size(cfg_ctx.hwparams, &bufsize);
  1029. SDL_LogError(SDL_LOG_CATEGORY_AUDIO,
  1030. "ALSA: period size = %ld, periods = %u, buffer size = %lu",
  1031. cfg_ctx.persize, cfg_ctx.periods, bufsize);
  1032. #endif
  1033. if (!ALSA_pcm_cfg_sw(&cfg_ctx)) { // alsa pcm "software" part of the pcm
  1034. goto err_cleanup_ctx;
  1035. }
  1036. // Now we can update the following parameters in the spec:
  1037. cfg_ctx.device->spec.format = cfg_ctx.matched_sdl_format;
  1038. cfg_ctx.device->spec.channels = cfg_ctx.chans_n;
  1039. cfg_ctx.device->spec.freq = cfg_ctx.rate;
  1040. cfg_ctx.device->sample_frames = cfg_ctx.persize;
  1041. // Calculate the final parameters for this audio specification
  1042. SDL_UpdatedAudioDeviceFormat(cfg_ctx.device);
  1043. // Allocate mixing buffer
  1044. if (!recording) {
  1045. cfg_ctx.device->hidden->mixbuf = (Uint8 *)SDL_malloc(cfg_ctx.device->buffer_size);
  1046. if (cfg_ctx.device->hidden->mixbuf == NULL) {
  1047. goto err_cleanup_ctx;
  1048. }
  1049. SDL_memset(cfg_ctx.device->hidden->mixbuf, cfg_ctx.device->silence_value, cfg_ctx.device->buffer_size);
  1050. }
  1051. #if !SDL_ALSA_NON_BLOCKING
  1052. if (!recording) {
  1053. ALSA_snd_pcm_nonblock(cfg_ctx.device->hidden->pcm, 0);
  1054. }
  1055. #endif
  1056. ALSA_snd_pcm_start(cfg_ctx.device->hidden->pcm);
  1057. return true; // We're ready to rock and roll. :-)
  1058. err_cleanup_ctx:
  1059. ALSA_snd_pcm_free_chmaps(cfg_ctx.chmap_queries);
  1060. err_close_pcm:
  1061. ALSA_snd_pcm_close(cfg_ctx.device->hidden->pcm);
  1062. err_free_device_hidden:
  1063. SDL_free(cfg_ctx.device->hidden);
  1064. cfg_ctx.device->hidden = NULL;
  1065. return false;
  1066. }
  1067. static ALSA_Device *hotplug_devices = NULL;
  1068. static int hotplug_device_process(snd_ctl_t *ctl, snd_ctl_card_info_t *ctl_card_info, int dev_idx,
  1069. snd_pcm_stream_t direction, ALSA_Device **unseen, ALSA_Device **seen)
  1070. {
  1071. unsigned int subdevs_n = 1; // we have at least one subdevice (substream since the direction is a stream in alsa terminology)
  1072. unsigned int subdev_idx = 0;
  1073. const bool recording = direction == SND_PCM_STREAM_CAPTURE ? true : false; // used for the unicity of the device
  1074. bool isstack;
  1075. snd_pcm_info_t *pcm_info = (snd_pcm_info_t*)SDL_small_alloc(Uint8, ALSA_snd_pcm_info_sizeof(), &isstack);
  1076. SDL_memset(pcm_info, 0, ALSA_snd_pcm_info_sizeof());
  1077. while (true) {
  1078. ALSA_snd_pcm_info_set_stream(pcm_info, direction);
  1079. ALSA_snd_pcm_info_set_device(pcm_info, dev_idx);
  1080. ALSA_snd_pcm_info_set_subdevice(pcm_info, subdev_idx); // we have at least one subdevice (substream) of index 0
  1081. const int r = ALSA_snd_ctl_pcm_info(ctl, pcm_info);
  1082. if (r < 0) {
  1083. SDL_small_free(pcm_info, isstack);
  1084. // first call to ALSA_snd_ctl_pcm_info
  1085. if (subdev_idx == 0 && r == -ENOENT) { // no such direction/stream for this device
  1086. return 0;
  1087. }
  1088. return -1;
  1089. }
  1090. if (subdev_idx == 0) {
  1091. subdevs_n = ALSA_snd_pcm_info_get_subdevices_count(pcm_info);
  1092. }
  1093. // building the unseen list scanning the list of hotplug devices, if it is already there
  1094. // using the id, move it to the seen list.
  1095. ALSA_Device *unseen_prev_adev = NULL;
  1096. ALSA_Device *adev;
  1097. for (adev = *unseen; adev; adev = adev->next) {
  1098. // the unicity key is the couple (id,recording)
  1099. if ((SDL_strcmp(adev->id, ALSA_snd_ctl_card_info_get_id(ctl_card_info)) == 0) && (adev->recording == recording)) {
  1100. // unchain from unseen
  1101. if (*unseen == adev) { // head
  1102. *unseen = adev->next;
  1103. } else {
  1104. unseen_prev_adev->next = adev->next;
  1105. }
  1106. // chain to seen
  1107. adev->next = *seen;
  1108. *seen = adev;
  1109. break;
  1110. }
  1111. unseen_prev_adev = adev;
  1112. }
  1113. if (adev == NULL) { // newly seen device
  1114. adev = SDL_calloc(1, sizeof(*adev));
  1115. if (adev == NULL) {
  1116. SDL_small_free(pcm_info, isstack);
  1117. return -1;
  1118. }
  1119. adev->id = SDL_strdup(ALSA_snd_ctl_card_info_get_id(ctl_card_info));
  1120. if (adev->id == NULL) {
  1121. SDL_small_free(pcm_info, isstack);
  1122. SDL_free(adev);
  1123. return -1;
  1124. }
  1125. if (SDL_asprintf(&adev->name, "%s:%s", ALSA_snd_ctl_card_info_get_name(ctl_card_info), ALSA_snd_pcm_info_get_name(pcm_info)) == -1) {
  1126. SDL_small_free(pcm_info, isstack);
  1127. SDL_free(adev->id);
  1128. SDL_free(adev);
  1129. return -1;
  1130. }
  1131. if (direction == SND_PCM_STREAM_CAPTURE) {
  1132. adev->recording = true;
  1133. } else {
  1134. adev->recording = false;
  1135. }
  1136. if (SDL_AddAudioDevice(recording, adev->name, NULL, adev) == NULL) {
  1137. SDL_small_free(pcm_info, isstack);
  1138. SDL_free(adev->id);
  1139. SDL_free(adev->name);
  1140. SDL_free(adev);
  1141. return -1;
  1142. }
  1143. adev->next = *seen;
  1144. *seen = adev;
  1145. }
  1146. subdev_idx++;
  1147. if (subdev_idx == subdevs_n) {
  1148. SDL_small_free(pcm_info, isstack);
  1149. return 0;
  1150. }
  1151. SDL_memset(pcm_info, 0, ALSA_snd_pcm_info_sizeof());
  1152. }
  1153. SDL_small_free(pcm_info, isstack);
  1154. SDL_assert(!"Shouldn't reach this code");
  1155. return -1;
  1156. }
  1157. static void ALSA_HotplugIteration(bool *has_default_output, bool *has_default_recording)
  1158. {
  1159. if (has_default_output != NULL) {
  1160. *has_default_output = true;
  1161. }
  1162. if (has_default_recording != NULL) {
  1163. *has_default_recording = true;
  1164. }
  1165. bool isstack;
  1166. snd_ctl_card_info_t *ctl_card_info = (snd_ctl_card_info_t *) SDL_small_alloc(Uint8, ALSA_snd_ctl_card_info_sizeof(), &isstack);
  1167. if (!ctl_card_info) {
  1168. return; // oh well.
  1169. }
  1170. SDL_memset(ctl_card_info, 0, ALSA_snd_ctl_card_info_sizeof());
  1171. snd_ctl_t *ctl = NULL;
  1172. ALSA_Device *unseen = hotplug_devices;
  1173. ALSA_Device *seen = NULL;
  1174. int card_idx = -1;
  1175. while (true) {
  1176. int r = ALSA_snd_card_next(&card_idx);
  1177. if (r < 0) {
  1178. goto failed;
  1179. } else if (card_idx == -1) {
  1180. break;
  1181. }
  1182. char ctl_name[64];
  1183. SDL_snprintf(ctl_name, sizeof (ctl_name), "%s%d", ALSA_device_prefix, card_idx); // card_idx >= 0
  1184. LOGDEBUG("hotplug ctl_name = '%s'", ctl_name);
  1185. r = ALSA_snd_ctl_open(&ctl, ctl_name, 0);
  1186. if (r < 0) {
  1187. continue;
  1188. }
  1189. r = ALSA_snd_ctl_card_info(ctl, ctl_card_info);
  1190. if (r < 0) {
  1191. goto failed;
  1192. }
  1193. int dev_idx = -1;
  1194. while (true) {
  1195. r = ALSA_snd_ctl_pcm_next_device(ctl, &dev_idx);
  1196. if (r < 0) {
  1197. goto failed;
  1198. } else if (dev_idx == -1) {
  1199. break;
  1200. }
  1201. r = hotplug_device_process(ctl, ctl_card_info, dev_idx, SND_PCM_STREAM_PLAYBACK, &unseen, &seen);
  1202. if (r < 0) {
  1203. goto failed;
  1204. }
  1205. r = hotplug_device_process(ctl, ctl_card_info, dev_idx, SND_PCM_STREAM_CAPTURE, &unseen, &seen);
  1206. if (r < 0) {
  1207. goto failed;
  1208. }
  1209. }
  1210. ALSA_snd_ctl_close(ctl);
  1211. ALSA_snd_ctl_card_info_clear(ctl_card_info);
  1212. }
  1213. // remove only the unseen devices
  1214. while (unseen) {
  1215. SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(unseen));
  1216. SDL_free(unseen->name);
  1217. SDL_free(unseen->id);
  1218. ALSA_Device *next = unseen->next;
  1219. SDL_free(unseen);
  1220. unseen = next;
  1221. }
  1222. // update hotplug devices to be the seen devices
  1223. hotplug_devices = seen;
  1224. SDL_small_free(ctl_card_info, isstack);
  1225. return;
  1226. failed:
  1227. if (ctl) {
  1228. ALSA_snd_ctl_close(ctl);
  1229. }
  1230. // remove the unseen
  1231. while (unseen) {
  1232. SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(unseen));
  1233. SDL_free(unseen->name);
  1234. SDL_free(unseen->id);
  1235. ALSA_Device *next = unseen->next;
  1236. SDL_free(unseen);
  1237. unseen = next;
  1238. }
  1239. // remove the seen
  1240. while (seen) {
  1241. SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(seen));
  1242. SDL_free(seen->name);
  1243. SDL_free(seen->id);
  1244. ALSA_Device *next = seen->next;
  1245. SDL_free(seen);
  1246. seen = next;
  1247. }
  1248. hotplug_devices = NULL;
  1249. SDL_small_free(ctl_card_info, isstack);
  1250. }
  1251. #if SDL_ALSA_HOTPLUG_THREAD
  1252. static SDL_AtomicInt ALSA_hotplug_shutdown;
  1253. static SDL_Thread *ALSA_hotplug_thread;
  1254. static int SDLCALL ALSA_HotplugThread(void *arg)
  1255. {
  1256. SDL_SetCurrentThreadPriority(SDL_THREAD_PRIORITY_LOW);
  1257. while (!SDL_GetAtomicInt(&ALSA_hotplug_shutdown)) {
  1258. // Block awhile before checking again, unless we're told to stop.
  1259. const Uint64 ticks = SDL_GetTicks() + 5000;
  1260. while (!SDL_GetAtomicInt(&ALSA_hotplug_shutdown) && (SDL_GetTicks() < ticks)) {
  1261. SDL_Delay(100);
  1262. }
  1263. ALSA_HotplugIteration(NULL, NULL); // run the check.
  1264. }
  1265. return 0;
  1266. }
  1267. #endif
  1268. static void ALSA_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording)
  1269. {
  1270. ALSA_guess_device_prefix();
  1271. // ALSA doesn't have a concept of a changeable default device, afaik, so we expose a generic default
  1272. // device here. It's the best we can do at this level.
  1273. bool has_default_playback = false, has_default_recording = false;
  1274. ALSA_HotplugIteration(&has_default_playback, &has_default_recording); // run once now before a thread continues to check.
  1275. if (has_default_playback) {
  1276. *default_playback = SDL_AddAudioDevice(/*recording=*/false, "ALSA default playback device", NULL, (void*)&default_playback_handle);
  1277. }
  1278. if (has_default_recording) {
  1279. *default_recording = SDL_AddAudioDevice(/*recording=*/true, "ALSA default recording device", NULL, (void*)&default_recording_handle);
  1280. }
  1281. #if SDL_ALSA_HOTPLUG_THREAD
  1282. SDL_SetAtomicInt(&ALSA_hotplug_shutdown, 0);
  1283. ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", NULL);
  1284. // if the thread doesn't spin, oh well, you just don't get further hotplug events.
  1285. #endif
  1286. }
  1287. static void ALSA_DeinitializeStart(void)
  1288. {
  1289. ALSA_Device *dev;
  1290. ALSA_Device *next;
  1291. #if SDL_ALSA_HOTPLUG_THREAD
  1292. if (ALSA_hotplug_thread) {
  1293. SDL_SetAtomicInt(&ALSA_hotplug_shutdown, 1);
  1294. SDL_WaitThread(ALSA_hotplug_thread, NULL);
  1295. ALSA_hotplug_thread = NULL;
  1296. }
  1297. #endif
  1298. // Shutting down! Clean up any data we've gathered.
  1299. for (dev = hotplug_devices; dev; dev = next) {
  1300. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: at shutdown, removing %s device '%s'", dev->recording ? "recording" : "playback", dev->name);
  1301. next = dev->next;
  1302. SDL_free(dev->name);
  1303. SDL_free(dev);
  1304. }
  1305. hotplug_devices = NULL;
  1306. }
  1307. static void ALSA_Deinitialize(void)
  1308. {
  1309. UnloadALSALibrary();
  1310. }
  1311. static bool ALSA_Init(SDL_AudioDriverImpl *impl)
  1312. {
  1313. if (!LoadALSALibrary()) {
  1314. return false;
  1315. }
  1316. impl->DetectDevices = ALSA_DetectDevices;
  1317. impl->OpenDevice = ALSA_OpenDevice;
  1318. impl->WaitDevice = ALSA_WaitDevice;
  1319. impl->GetDeviceBuf = ALSA_GetDeviceBuf;
  1320. impl->PlayDevice = ALSA_PlayDevice;
  1321. impl->CloseDevice = ALSA_CloseDevice;
  1322. impl->DeinitializeStart = ALSA_DeinitializeStart;
  1323. impl->Deinitialize = ALSA_Deinitialize;
  1324. impl->WaitRecordingDevice = ALSA_WaitDevice;
  1325. impl->RecordDevice = ALSA_RecordDevice;
  1326. impl->FlushRecording = ALSA_FlushRecording;
  1327. impl->HasRecordingSupport = true;
  1328. return true;
  1329. }
  1330. AudioBootStrap ALSA_bootstrap = {
  1331. "alsa", "ALSA PCM audio", ALSA_Init, false
  1332. };
  1333. #endif // SDL_AUDIO_DRIVER_ALSA