SDL_alsa_audio.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. // Allow access to a raw mixing buffer
  28. #include <sys/types.h>
  29. #include <signal.h> // For kill()
  30. #include <string.h>
  31. #include "../SDL_sysaudio.h"
  32. #include "SDL_alsa_audio.h"
  33. #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  34. #endif
  35. static int (*ALSA_snd_pcm_open)(snd_pcm_t **, const char *, snd_pcm_stream_t, int);
  36. static int (*ALSA_snd_pcm_close)(snd_pcm_t *pcm);
  37. static int (*ALSA_snd_pcm_start)(snd_pcm_t *pcm);
  38. static snd_pcm_sframes_t (*ALSA_snd_pcm_writei)(snd_pcm_t *, const void *, snd_pcm_uframes_t);
  39. static snd_pcm_sframes_t (*ALSA_snd_pcm_readi)(snd_pcm_t *, void *, snd_pcm_uframes_t);
  40. static int (*ALSA_snd_pcm_recover)(snd_pcm_t *, int, int);
  41. static int (*ALSA_snd_pcm_prepare)(snd_pcm_t *);
  42. static int (*ALSA_snd_pcm_drain)(snd_pcm_t *);
  43. static const char *(*ALSA_snd_strerror)(int);
  44. static size_t (*ALSA_snd_pcm_hw_params_sizeof)(void);
  45. static size_t (*ALSA_snd_pcm_sw_params_sizeof)(void);
  46. static void (*ALSA_snd_pcm_hw_params_copy)(snd_pcm_hw_params_t *, const snd_pcm_hw_params_t *);
  47. static int (*ALSA_snd_pcm_hw_params_any)(snd_pcm_t *, snd_pcm_hw_params_t *);
  48. static int (*ALSA_snd_pcm_hw_params_set_access)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
  49. static int (*ALSA_snd_pcm_hw_params_set_format)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
  50. static int (*ALSA_snd_pcm_hw_params_set_channels)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
  51. static int (*ALSA_snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *, unsigned int *);
  52. static int (*ALSA_snd_pcm_hw_params_set_rate_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  53. static int (*ALSA_snd_pcm_hw_params_set_period_size_near)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
  54. static int (*ALSA_snd_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
  55. static int (*ALSA_snd_pcm_hw_params_set_periods_min)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  56. static int (*ALSA_snd_pcm_hw_params_set_periods_first)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
  57. static int (*ALSA_snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *, unsigned int *, int *);
  58. static int (*ALSA_snd_pcm_hw_params_set_buffer_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
  59. static int (*ALSA_snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
  60. static int (*ALSA_snd_pcm_hw_params)(snd_pcm_t *, snd_pcm_hw_params_t *);
  61. static int (*ALSA_snd_pcm_sw_params_current)(snd_pcm_t *,
  62. snd_pcm_sw_params_t *);
  63. static int (*ALSA_snd_pcm_sw_params_set_start_threshold)(snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
  64. static int (*ALSA_snd_pcm_sw_params)(snd_pcm_t *, snd_pcm_sw_params_t *);
  65. static int (*ALSA_snd_pcm_nonblock)(snd_pcm_t *, int);
  66. static int (*ALSA_snd_pcm_wait)(snd_pcm_t *, int);
  67. static int (*ALSA_snd_pcm_sw_params_set_avail_min)(snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
  68. static int (*ALSA_snd_pcm_reset)(snd_pcm_t *);
  69. static int (*ALSA_snd_device_name_hint)(int, const char *, void ***);
  70. static char *(*ALSA_snd_device_name_get_hint)(const void *, const char *);
  71. static int (*ALSA_snd_device_name_free_hint)(void **);
  72. static snd_pcm_sframes_t (*ALSA_snd_pcm_avail)(snd_pcm_t *);
  73. #ifdef SND_CHMAP_API_VERSION
  74. static snd_pcm_chmap_t *(*ALSA_snd_pcm_get_chmap)(snd_pcm_t *);
  75. static int (*ALSA_snd_pcm_chmap_print)(const snd_pcm_chmap_t *map, size_t maxlen, char *buf);
  76. #endif
  77. #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  78. #define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof
  79. #define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof
  80. static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
  81. static void *alsa_handle = NULL;
  82. static int load_alsa_sym(const char *fn, void **addr)
  83. {
  84. *addr = SDL_LoadFunction(alsa_handle, fn);
  85. if (!*addr) {
  86. // Don't call SDL_SetError(): SDL_LoadFunction already did.
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. // cast funcs to char* first, to please GCC's strict aliasing rules.
  92. #define SDL_ALSA_SYM(x) \
  93. if (!load_alsa_sym(#x, (void **)(char *)&ALSA_##x)) \
  94. return -1
  95. #else
  96. #define SDL_ALSA_SYM(x) ALSA_##x = x
  97. #endif
  98. static int load_alsa_syms(void)
  99. {
  100. SDL_ALSA_SYM(snd_pcm_open);
  101. SDL_ALSA_SYM(snd_pcm_close);
  102. SDL_ALSA_SYM(snd_pcm_start);
  103. SDL_ALSA_SYM(snd_pcm_writei);
  104. SDL_ALSA_SYM(snd_pcm_readi);
  105. SDL_ALSA_SYM(snd_pcm_recover);
  106. SDL_ALSA_SYM(snd_pcm_prepare);
  107. SDL_ALSA_SYM(snd_pcm_drain);
  108. SDL_ALSA_SYM(snd_strerror);
  109. SDL_ALSA_SYM(snd_pcm_hw_params_sizeof);
  110. SDL_ALSA_SYM(snd_pcm_sw_params_sizeof);
  111. SDL_ALSA_SYM(snd_pcm_hw_params_copy);
  112. SDL_ALSA_SYM(snd_pcm_hw_params_any);
  113. SDL_ALSA_SYM(snd_pcm_hw_params_set_access);
  114. SDL_ALSA_SYM(snd_pcm_hw_params_set_format);
  115. SDL_ALSA_SYM(snd_pcm_hw_params_set_channels);
  116. SDL_ALSA_SYM(snd_pcm_hw_params_get_channels);
  117. SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
  118. SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
  119. SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
  120. SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_min);
  121. SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_first);
  122. SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
  123. SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
  124. SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
  125. SDL_ALSA_SYM(snd_pcm_hw_params);
  126. SDL_ALSA_SYM(snd_pcm_sw_params_current);
  127. SDL_ALSA_SYM(snd_pcm_sw_params_set_start_threshold);
  128. SDL_ALSA_SYM(snd_pcm_sw_params);
  129. SDL_ALSA_SYM(snd_pcm_nonblock);
  130. SDL_ALSA_SYM(snd_pcm_wait);
  131. SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
  132. SDL_ALSA_SYM(snd_pcm_reset);
  133. SDL_ALSA_SYM(snd_device_name_hint);
  134. SDL_ALSA_SYM(snd_device_name_get_hint);
  135. SDL_ALSA_SYM(snd_device_name_free_hint);
  136. SDL_ALSA_SYM(snd_pcm_avail);
  137. #ifdef SND_CHMAP_API_VERSION
  138. SDL_ALSA_SYM(snd_pcm_get_chmap);
  139. SDL_ALSA_SYM(snd_pcm_chmap_print);
  140. #endif
  141. return 0;
  142. }
  143. #undef SDL_ALSA_SYM
  144. #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  145. static void UnloadALSALibrary(void)
  146. {
  147. if (alsa_handle) {
  148. SDL_UnloadObject(alsa_handle);
  149. alsa_handle = NULL;
  150. }
  151. }
  152. static int LoadALSALibrary(void)
  153. {
  154. int retval = 0;
  155. if (!alsa_handle) {
  156. alsa_handle = SDL_LoadObject(alsa_library);
  157. if (!alsa_handle) {
  158. retval = -1;
  159. // Don't call SDL_SetError(): SDL_LoadObject already did.
  160. } else {
  161. retval = load_alsa_syms();
  162. if (retval < 0) {
  163. UnloadALSALibrary();
  164. }
  165. }
  166. }
  167. return retval;
  168. }
  169. #else
  170. static void UnloadALSALibrary(void)
  171. {
  172. }
  173. static int LoadALSALibrary(void)
  174. {
  175. load_alsa_syms();
  176. return 0;
  177. }
  178. #endif // SDL_AUDIO_DRIVER_ALSA_DYNAMIC
  179. typedef struct ALSA_Device
  180. {
  181. char *name;
  182. SDL_bool iscapture;
  183. struct ALSA_Device *next;
  184. } ALSA_Device;
  185. static const ALSA_Device default_output_handle = {
  186. "default",
  187. SDL_FALSE,
  188. NULL
  189. };
  190. static const ALSA_Device default_capture_handle = {
  191. "default",
  192. SDL_TRUE,
  193. NULL
  194. };
  195. static const char *get_audio_device(void *handle, const int channels)
  196. {
  197. SDL_assert(handle); // SDL2 used NULL to mean "default" but that's not true in SDL3.
  198. ALSA_Device *dev = (ALSA_Device *)handle;
  199. if (SDL_strcmp(dev->name, "default") == 0) {
  200. const char *device = SDL_getenv("AUDIODEV"); // Is there a standard variable name?
  201. if (device) {
  202. return device;
  203. } else if (channels == 6) {
  204. return "plug:surround51";
  205. } else if (channels == 4) {
  206. return "plug:surround40";
  207. }
  208. return "default";
  209. }
  210. return dev->name;
  211. }
  212. // !!! FIXME: is there a channel swizzler in alsalib instead?
  213. // https://bugzilla.libsdl.org/show_bug.cgi?id=110
  214. // "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
  215. // and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
  216. #define SWIZ6(T) \
  217. static void swizzle_alsa_channels_6_##T(void *buffer, const Uint32 bufferlen) \
  218. { \
  219. T *ptr = (T *)buffer; \
  220. Uint32 i; \
  221. for (i = 0; i < bufferlen; i++, ptr += 6) { \
  222. T tmp; \
  223. tmp = ptr[2]; \
  224. ptr[2] = ptr[4]; \
  225. ptr[4] = tmp; \
  226. tmp = ptr[3]; \
  227. ptr[3] = ptr[5]; \
  228. ptr[5] = tmp; \
  229. } \
  230. }
  231. // !!! FIXME: is there a channel swizzler in alsalib instead?
  232. // !!! FIXME: this screams for a SIMD shuffle operation.
  233. // https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/mapping-stream-formats-to-speaker-configurations
  234. // For Linux ALSA, this appears to be FL-FR-RL-RR-C-LFE-SL-SR
  235. // and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-SL-SR-RL-RR"
  236. #define SWIZ8(T) \
  237. static void swizzle_alsa_channels_8_##T(void *buffer, const Uint32 bufferlen) \
  238. { \
  239. T *ptr = (T *)buffer; \
  240. Uint32 i; \
  241. for (i = 0; i < bufferlen; i++, ptr += 6) { \
  242. const T center = ptr[2]; \
  243. const T subwoofer = ptr[3]; \
  244. const T side_left = ptr[4]; \
  245. const T side_right = ptr[5]; \
  246. const T rear_left = ptr[6]; \
  247. const T rear_right = ptr[7]; \
  248. ptr[2] = rear_left; \
  249. ptr[3] = rear_right; \
  250. ptr[4] = center; \
  251. ptr[5] = subwoofer; \
  252. ptr[6] = side_left; \
  253. ptr[7] = side_right; \
  254. } \
  255. }
  256. #define CHANNEL_SWIZZLE(x) \
  257. x(Uint64) \
  258. x(Uint32) \
  259. x(Uint16) \
  260. x(Uint8)
  261. CHANNEL_SWIZZLE(SWIZ6)
  262. CHANNEL_SWIZZLE(SWIZ8)
  263. #undef CHANNEL_SWIZZLE
  264. #undef SWIZ6
  265. #undef SWIZ8
  266. // Called right before feeding device->hidden->mixbuf to the hardware. Swizzle
  267. // channels from Windows/Mac order to the format alsalib will want.
  268. static void swizzle_alsa_channels(SDL_AudioDevice *device, void *buffer, Uint32 bufferlen)
  269. {
  270. switch (device->spec.channels) {
  271. #define CHANSWIZ(chans) \
  272. case chans: \
  273. switch ((device->spec.format & (0xFF))) { \
  274. case 8: \
  275. swizzle_alsa_channels_##chans##_Uint8(buffer, bufferlen); \
  276. break; \
  277. case 16: \
  278. swizzle_alsa_channels_##chans##_Uint16(buffer, bufferlen); \
  279. break; \
  280. case 32: \
  281. swizzle_alsa_channels_##chans##_Uint32(buffer, bufferlen); \
  282. break; \
  283. case 64: \
  284. swizzle_alsa_channels_##chans##_Uint64(buffer, bufferlen); \
  285. break; \
  286. default: \
  287. SDL_assert(!"unhandled bitsize"); \
  288. break; \
  289. } \
  290. return;
  291. CHANSWIZ(6);
  292. CHANSWIZ(8);
  293. #undef CHANSWIZ
  294. default:
  295. break;
  296. }
  297. }
  298. #ifdef SND_CHMAP_API_VERSION
  299. // Some devices have the right channel map, no swizzling necessary
  300. static void no_swizzle(SDL_AudioDevice *device, void *buffer, Uint32 bufferlen)
  301. {
  302. }
  303. #endif // SND_CHMAP_API_VERSION
  304. // This function waits until it is possible to write a full sound buffer
  305. static int ALSA_WaitDevice(SDL_AudioDevice *device)
  306. {
  307. const int fulldelay = (int) ((((Uint64) device->sample_frames) * 1000) / device->spec.freq);
  308. const int delay = SDL_max(fulldelay, 10);
  309. while (!SDL_AtomicGet(&device->shutdown)) {
  310. const int rc = ALSA_snd_pcm_wait(device->hidden->pcm_handle, delay);
  311. if (rc < 0 && (rc != -EAGAIN)) {
  312. const int status = ALSA_snd_pcm_recover(device->hidden->pcm_handle, rc, 0);
  313. if (status < 0) {
  314. // Hmm, not much we can do - abort
  315. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA: snd_pcm_wait failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  316. return -1;
  317. }
  318. continue;
  319. }
  320. if (rc > 0) {
  321. break; // ready to go!
  322. }
  323. // Timed out! Make sure we aren't shutting down and then wait again.
  324. }
  325. return 0;
  326. }
  327. static int ALSA_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buflen)
  328. {
  329. SDL_assert(buffer == device->hidden->mixbuf);
  330. Uint8 *sample_buf = (Uint8 *) buffer; // !!! FIXME: deal with this without casting away constness
  331. const int frame_size = SDL_AUDIO_FRAMESIZE(device->spec);
  332. snd_pcm_uframes_t frames_left = (snd_pcm_uframes_t) (buflen / frame_size);
  333. device->hidden->swizzle_func(device, sample_buf, frames_left);
  334. while ((frames_left > 0) && !SDL_AtomicGet(&device->shutdown)) {
  335. const int rc = ALSA_snd_pcm_writei(device->hidden->pcm_handle, sample_buf, frames_left);
  336. //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));
  337. SDL_assert(rc != 0); // assuming this can't happen if we used snd_pcm_wait and queried for available space.
  338. if (rc < 0) {
  339. 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!
  340. const int status = ALSA_snd_pcm_recover(device->hidden->pcm_handle, rc, 0);
  341. if (status < 0) {
  342. // Hmm, not much we can do - abort
  343. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA write failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  344. return -1;
  345. }
  346. continue;
  347. }
  348. sample_buf += rc * frame_size;
  349. frames_left -= rc;
  350. }
  351. return 0;
  352. }
  353. static Uint8 *ALSA_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
  354. {
  355. snd_pcm_sframes_t rc = ALSA_snd_pcm_avail(device->hidden->pcm_handle);
  356. if (rc <= 0) {
  357. // Wait a bit and try again, maybe the hardware isn't quite ready yet?
  358. SDL_Delay(1);
  359. rc = ALSA_snd_pcm_avail(device->hidden->pcm_handle);
  360. if (rc <= 0) {
  361. // We'll catch it next time
  362. *buffer_size = 0;
  363. return NULL;
  364. }
  365. }
  366. const int requested_frames = SDL_min(device->sample_frames, rc);
  367. const int requested_bytes = requested_frames * SDL_AUDIO_FRAMESIZE(device->spec);
  368. SDL_assert(requested_bytes <= *buffer_size);
  369. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA GETDEVICEBUF: NEED %d BYTES", requested_bytes);
  370. *buffer_size = requested_bytes;
  371. return device->hidden->mixbuf;
  372. }
  373. static int ALSA_CaptureFromDevice(SDL_AudioDevice *device, void *buffer, int buflen)
  374. {
  375. const int frame_size = SDL_AUDIO_FRAMESIZE(device->spec);
  376. SDL_assert((buflen % frame_size) == 0);
  377. const snd_pcm_sframes_t total_available = ALSA_snd_pcm_avail(device->hidden->pcm_handle);
  378. const int total_frames = SDL_min(buflen / frame_size, total_available);
  379. const int rc = ALSA_snd_pcm_readi(device->hidden->pcm_handle, buffer, total_frames);
  380. 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!
  381. if (rc < 0) {
  382. const int status = ALSA_snd_pcm_recover(device->hidden->pcm_handle, rc, 0);
  383. if (status < 0) {
  384. // Hmm, not much we can do - abort
  385. SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA read failed (unrecoverable): %s", ALSA_snd_strerror(rc));
  386. return -1;
  387. }
  388. return 0; // go back to WaitDevice and try again.
  389. } else if (rc > 0) {
  390. device->hidden->swizzle_func(device, buffer, total_frames - rc);
  391. }
  392. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: captured %d bytes", rc * frame_size);
  393. return rc * frame_size;
  394. }
  395. static void ALSA_FlushCapture(SDL_AudioDevice *device)
  396. {
  397. ALSA_snd_pcm_reset(device->hidden->pcm_handle);
  398. }
  399. static void ALSA_CloseDevice(SDL_AudioDevice *device)
  400. {
  401. if (device->hidden) {
  402. if (device->hidden->pcm_handle) {
  403. // Wait for the submitted audio to drain. ALSA_snd_pcm_drop() can hang, so don't use that.
  404. SDL_Delay(((device->sample_frames * 1000) / device->spec.freq) * 2);
  405. ALSA_snd_pcm_close(device->hidden->pcm_handle);
  406. }
  407. SDL_free(device->hidden->mixbuf);
  408. SDL_free(device->hidden);
  409. }
  410. }
  411. static int ALSA_set_buffer_size(SDL_AudioDevice *device, snd_pcm_hw_params_t *params)
  412. {
  413. int status;
  414. snd_pcm_hw_params_t *hwparams;
  415. snd_pcm_uframes_t persize;
  416. unsigned int periods;
  417. // Copy the hardware parameters for this setup
  418. snd_pcm_hw_params_alloca(&hwparams);
  419. ALSA_snd_pcm_hw_params_copy(hwparams, params);
  420. // Attempt to match the period size to the requested buffer size
  421. persize = device->sample_frames;
  422. status = ALSA_snd_pcm_hw_params_set_period_size_near(
  423. device->hidden->pcm_handle, hwparams, &persize, NULL);
  424. if (status < 0) {
  425. return -1;
  426. }
  427. // Need to at least double buffer
  428. periods = 2;
  429. status = ALSA_snd_pcm_hw_params_set_periods_min(
  430. device->hidden->pcm_handle, hwparams, &periods, NULL);
  431. if (status < 0) {
  432. return -1;
  433. }
  434. status = ALSA_snd_pcm_hw_params_set_periods_first(
  435. device->hidden->pcm_handle, hwparams, &periods, NULL);
  436. if (status < 0) {
  437. return -1;
  438. }
  439. // "set" the hardware with the desired parameters
  440. status = ALSA_snd_pcm_hw_params(device->hidden->pcm_handle, hwparams);
  441. if (status < 0) {
  442. return -1;
  443. }
  444. device->sample_frames = persize;
  445. // This is useful for debugging
  446. if (SDL_getenv("SDL_AUDIO_ALSA_DEBUG")) {
  447. snd_pcm_uframes_t bufsize;
  448. ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
  449. SDL_LogError(SDL_LOG_CATEGORY_AUDIO,
  450. "ALSA: period size = %ld, periods = %u, buffer size = %lu",
  451. persize, periods, bufsize);
  452. }
  453. return 0;
  454. }
  455. static int ALSA_OpenDevice(SDL_AudioDevice *device)
  456. {
  457. const SDL_bool iscapture = device->iscapture;
  458. int status = 0;
  459. // Initialize all variables that we clean on shutdown
  460. device->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*device->hidden));
  461. if (!device->hidden) {
  462. return SDL_OutOfMemory();
  463. }
  464. // Open the audio device
  465. // Name of device should depend on # channels in spec
  466. snd_pcm_t *pcm_handle = NULL;
  467. status = ALSA_snd_pcm_open(&pcm_handle,
  468. get_audio_device(device->handle, device->spec.channels),
  469. iscapture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
  470. SND_PCM_NONBLOCK);
  471. if (status < 0) {
  472. return SDL_SetError("ALSA: Couldn't open audio device: %s", ALSA_snd_strerror(status));
  473. }
  474. device->hidden->pcm_handle = pcm_handle;
  475. // Figure out what the hardware is capable of
  476. snd_pcm_hw_params_t *hwparams = NULL;
  477. snd_pcm_hw_params_alloca(&hwparams);
  478. status = ALSA_snd_pcm_hw_params_any(pcm_handle, hwparams);
  479. if (status < 0) {
  480. return SDL_SetError("ALSA: Couldn't get hardware config: %s", ALSA_snd_strerror(status));
  481. }
  482. // SDL only uses interleaved sample output
  483. status = ALSA_snd_pcm_hw_params_set_access(pcm_handle, hwparams,
  484. SND_PCM_ACCESS_RW_INTERLEAVED);
  485. if (status < 0) {
  486. return SDL_SetError("ALSA: Couldn't set interleaved access: %s", ALSA_snd_strerror(status));
  487. }
  488. // Try for a closest match on audio format
  489. snd_pcm_format_t format = 0;
  490. const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format);
  491. SDL_AudioFormat test_format;
  492. while ((test_format = *(closefmts++)) != 0) {
  493. switch (test_format) {
  494. case SDL_AUDIO_U8:
  495. format = SND_PCM_FORMAT_U8;
  496. break;
  497. case SDL_AUDIO_S8:
  498. format = SND_PCM_FORMAT_S8;
  499. break;
  500. case SDL_AUDIO_S16LE:
  501. format = SND_PCM_FORMAT_S16_LE;
  502. break;
  503. case SDL_AUDIO_S16BE:
  504. format = SND_PCM_FORMAT_S16_BE;
  505. break;
  506. case SDL_AUDIO_S32LE:
  507. format = SND_PCM_FORMAT_S32_LE;
  508. break;
  509. case SDL_AUDIO_S32BE:
  510. format = SND_PCM_FORMAT_S32_BE;
  511. break;
  512. case SDL_AUDIO_F32LE:
  513. format = SND_PCM_FORMAT_FLOAT_LE;
  514. break;
  515. case SDL_AUDIO_F32BE:
  516. format = SND_PCM_FORMAT_FLOAT_BE;
  517. break;
  518. default:
  519. continue;
  520. }
  521. if (ALSA_snd_pcm_hw_params_set_format(pcm_handle, hwparams, format) >= 0) {
  522. break;
  523. }
  524. }
  525. if (!test_format) {
  526. return SDL_SetError("ALSA: Unsupported audio format");
  527. }
  528. device->spec.format = test_format;
  529. // Validate number of channels and determine if swizzling is necessary.
  530. // Assume original swizzling, until proven otherwise.
  531. device->hidden->swizzle_func = swizzle_alsa_channels;
  532. #ifdef SND_CHMAP_API_VERSION
  533. snd_pcm_chmap_t *chmap = ALSA_snd_pcm_get_chmap(pcm_handle);
  534. if (chmap) {
  535. char chmap_str[64];
  536. if (ALSA_snd_pcm_chmap_print(chmap, sizeof(chmap_str), chmap_str) > 0) {
  537. if (SDL_strcmp("FL FR FC LFE RL RR", chmap_str) == 0 ||
  538. SDL_strcmp("FL FR FC LFE SL SR", chmap_str) == 0) {
  539. device->hidden->swizzle_func = no_swizzle;
  540. }
  541. }
  542. free(chmap); // This should NOT be SDL_free()
  543. }
  544. #endif // SND_CHMAP_API_VERSION
  545. // Set the number of channels
  546. status = ALSA_snd_pcm_hw_params_set_channels(pcm_handle, hwparams,
  547. device->spec.channels);
  548. unsigned int channels = device->spec.channels;
  549. if (status < 0) {
  550. status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels);
  551. if (status < 0) {
  552. return SDL_SetError("ALSA: Couldn't set audio channels");
  553. }
  554. device->spec.channels = channels;
  555. }
  556. // Set the audio rate
  557. unsigned int rate = device->spec.freq;
  558. status = ALSA_snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
  559. &rate, NULL);
  560. if (status < 0) {
  561. return SDL_SetError("ALSA: Couldn't set audio frequency: %s", ALSA_snd_strerror(status));
  562. }
  563. device->spec.freq = rate;
  564. // Set the buffer size, in samples
  565. status = ALSA_set_buffer_size(device, hwparams);
  566. if (status < 0) {
  567. return SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status));
  568. }
  569. // Set the software parameters
  570. snd_pcm_sw_params_t *swparams = NULL;
  571. snd_pcm_sw_params_alloca(&swparams);
  572. status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams);
  573. if (status < 0) {
  574. return SDL_SetError("ALSA: Couldn't get software config: %s", ALSA_snd_strerror(status));
  575. }
  576. status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, device->sample_frames);
  577. if (status < 0) {
  578. return SDL_SetError("Couldn't set minimum available samples: %s", ALSA_snd_strerror(status));
  579. }
  580. status =
  581. ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
  582. if (status < 0) {
  583. return SDL_SetError("ALSA: Couldn't set start threshold: %s", ALSA_snd_strerror(status));
  584. }
  585. status = ALSA_snd_pcm_sw_params(pcm_handle, swparams);
  586. if (status < 0) {
  587. return SDL_SetError("Couldn't set software audio parameters: %s", ALSA_snd_strerror(status));
  588. }
  589. // Calculate the final parameters for this audio specification
  590. SDL_UpdatedAudioDeviceFormat(device);
  591. // Allocate mixing buffer
  592. if (!iscapture) {
  593. device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size);
  594. if (!device->hidden->mixbuf) {
  595. return SDL_OutOfMemory();
  596. }
  597. SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
  598. }
  599. #if !SDL_ALSA_NON_BLOCKING
  600. if (!iscapture) {
  601. ALSA_snd_pcm_nonblock(pcm_handle, 0);
  602. }
  603. #endif
  604. ALSA_snd_pcm_start(pcm_handle);
  605. return 0; // We're ready to rock and roll. :-)
  606. }
  607. static void add_device(const SDL_bool iscapture, const char *name, void *hint, ALSA_Device **pSeen)
  608. {
  609. ALSA_Device *dev = SDL_malloc(sizeof(ALSA_Device));
  610. char *desc;
  611. char *ptr;
  612. if (!dev) {
  613. return;
  614. }
  615. // Not all alsa devices are enumerable via snd_device_name_get_hint
  616. // (i.e. bluetooth devices). Therefore if hint is passed in to this
  617. // function as NULL, assume name contains desc.
  618. // Make sure not to free the storage associated with desc in this case
  619. if (hint) {
  620. desc = ALSA_snd_device_name_get_hint(hint, "DESC");
  621. if (!desc) {
  622. SDL_free(dev);
  623. return;
  624. }
  625. } else {
  626. desc = (char *)name;
  627. }
  628. SDL_assert(name);
  629. // some strings have newlines, like "HDA NVidia, HDMI 0\nHDMI Audio Output".
  630. // just chop the extra lines off, this seems to get a reasonable device
  631. // name without extra details.
  632. ptr = SDL_strchr(desc, '\n');
  633. if (ptr) {
  634. *ptr = '\0';
  635. }
  636. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: adding %s device '%s' (%s)", iscapture ? "capture" : "output", name, desc);
  637. dev->name = SDL_strdup(name);
  638. if (!dev->name) {
  639. if (hint) {
  640. free(desc); // This should NOT be SDL_free()
  641. }
  642. SDL_free(dev->name);
  643. SDL_free(dev);
  644. return;
  645. }
  646. // Note that spec is NULL, because we are required to open the device before
  647. // acquiring the mix format, making this information inaccessible at
  648. // enumeration time
  649. SDL_AddAudioDevice(iscapture, desc, NULL, dev);
  650. if (hint) {
  651. free(desc); // This should NOT be SDL_free()
  652. }
  653. dev->iscapture = iscapture;
  654. dev->next = *pSeen;
  655. *pSeen = dev;
  656. }
  657. static ALSA_Device *hotplug_devices = NULL;
  658. static void ALSA_HotplugIteration(SDL_bool *has_default_output, SDL_bool *has_default_capture)
  659. {
  660. void **hints = NULL;
  661. ALSA_Device *unseen = NULL;
  662. ALSA_Device *seen = NULL;
  663. if (ALSA_snd_device_name_hint(-1, "pcm", &hints) == 0) {
  664. const char *match = NULL;
  665. int bestmatch = 0xFFFF;
  666. int has_default = -1;
  667. size_t match_len = 0;
  668. static const char *const prefixes[] = {
  669. "hw:", "sysdefault:", "default:", NULL
  670. };
  671. unseen = hotplug_devices;
  672. seen = NULL;
  673. // Apparently there are several different ways that ALSA lists
  674. // actual hardware. It could be prefixed with "hw:" or "default:"
  675. // or "sysdefault:" and maybe others. Go through the list and see
  676. // if we can find a preferred prefix for the system.
  677. for (int i = 0; hints[i]; i++) {
  678. char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
  679. if (!name) {
  680. continue;
  681. }
  682. if (SDL_strcmp(name, "default") == 0) {
  683. if (has_default < 0) {
  684. has_default = i;
  685. }
  686. } else {
  687. for (int j = 0; prefixes[j]; j++) {
  688. const char *prefix = prefixes[j];
  689. const size_t prefixlen = SDL_strlen(prefix);
  690. if (SDL_strncmp(name, prefix, prefixlen) == 0) {
  691. if (j < bestmatch) {
  692. bestmatch = j;
  693. match = prefix;
  694. match_len = prefixlen;
  695. }
  696. }
  697. }
  698. free(name); // This should NOT be SDL_free()
  699. }
  700. }
  701. // look through the list of device names to find matches
  702. if (match || (has_default >= 0)) { // did we find a device name prefix we like at all...?
  703. for (int i = 0; hints[i]; i++) {
  704. char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
  705. if (!name) {
  706. continue;
  707. }
  708. // only want physical hardware interfaces
  709. const SDL_bool is_default = (has_default == i);
  710. if (is_default || (match && SDL_strncmp(name, match, match_len) == 0)) {
  711. char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID");
  712. const SDL_bool isoutput = (!ioid) || (SDL_strcmp(ioid, "Output") == 0);
  713. const SDL_bool isinput = (!ioid) || (SDL_strcmp(ioid, "Input") == 0);
  714. SDL_bool have_output = SDL_FALSE;
  715. SDL_bool have_input = SDL_FALSE;
  716. free(ioid); // This should NOT be SDL_free()
  717. if (!isoutput && !isinput) {
  718. free(name); // This should NOT be SDL_free()
  719. continue;
  720. }
  721. if (is_default) {
  722. if (has_default_output && isoutput) {
  723. *has_default_output = SDL_TRUE;
  724. } else if (has_default_capture && isinput) {
  725. *has_default_capture = SDL_TRUE;
  726. }
  727. free(name); // This should NOT be SDL_free()
  728. continue;
  729. }
  730. ALSA_Device *prev = NULL;
  731. ALSA_Device *next;
  732. for (ALSA_Device *dev = unseen; dev; dev = next) {
  733. next = dev->next;
  734. if ((SDL_strcmp(dev->name, name) == 0) && (((isinput) && dev->iscapture) || ((isoutput) && !dev->iscapture))) {
  735. if (prev) {
  736. prev->next = next;
  737. } else {
  738. unseen = next;
  739. }
  740. dev->next = seen;
  741. seen = dev;
  742. if (isinput) {
  743. have_input = SDL_TRUE;
  744. }
  745. if (isoutput) {
  746. have_output = SDL_TRUE;
  747. }
  748. } else {
  749. prev = dev;
  750. }
  751. }
  752. if (isinput && !have_input) {
  753. add_device(SDL_TRUE, name, hints[i], &seen);
  754. }
  755. if (isoutput && !have_output) {
  756. add_device(SDL_FALSE, name, hints[i], &seen);
  757. }
  758. }
  759. free(name); // This should NOT be SDL_free()
  760. }
  761. }
  762. ALSA_snd_device_name_free_hint(hints);
  763. hotplug_devices = seen; // now we have a known-good list of attached devices.
  764. // report anything still in unseen as removed.
  765. ALSA_Device *next = NULL;
  766. for (ALSA_Device *dev = unseen; dev; dev = next) {
  767. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: removing %s device '%s'", dev->iscapture ? "capture" : "output", dev->name);
  768. next = dev->next;
  769. SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(dev));
  770. SDL_free(dev->name);
  771. SDL_free(dev);
  772. }
  773. }
  774. }
  775. #if SDL_ALSA_HOTPLUG_THREAD
  776. static SDL_AtomicInt ALSA_hotplug_shutdown;
  777. static SDL_Thread *ALSA_hotplug_thread;
  778. static int SDLCALL ALSA_HotplugThread(void *arg)
  779. {
  780. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
  781. while (!SDL_AtomicGet(&ALSA_hotplug_shutdown)) {
  782. // Block awhile before checking again, unless we're told to stop.
  783. const Uint64 ticks = SDL_GetTicks() + 5000;
  784. while (!SDL_AtomicGet(&ALSA_hotplug_shutdown) && SDL_GetTicks() < ticks) {
  785. SDL_Delay(100);
  786. }
  787. ALSA_HotplugIteration(NULL, NULL); // run the check.
  788. }
  789. return 0;
  790. }
  791. #endif
  792. static void ALSA_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioDevice **default_capture)
  793. {
  794. // ALSA doesn't have a concept of a changeable default device, afaik, so we expose a generic default
  795. // device here. It's the best we can do at this level.
  796. SDL_bool has_default_output = SDL_FALSE, has_default_capture = SDL_FALSE;
  797. ALSA_HotplugIteration(&has_default_output, &has_default_capture); // run once now before a thread continues to check.
  798. if (has_default_output) {
  799. *default_output = SDL_AddAudioDevice(/*iscapture=*/SDL_FALSE, "ALSA default output device", NULL, (void*)&default_output_handle);
  800. }
  801. if (has_default_capture) {
  802. *default_capture = SDL_AddAudioDevice(/*iscapture=*/SDL_TRUE, "ALSA default capture device", NULL, (void*)&default_capture_handle);
  803. }
  804. #if SDL_ALSA_HOTPLUG_THREAD
  805. SDL_AtomicSet(&ALSA_hotplug_shutdown, 0);
  806. ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", NULL);
  807. // if the thread doesn't spin, oh well, you just don't get further hotplug events.
  808. #endif
  809. }
  810. static void ALSA_DeinitializeStart(void)
  811. {
  812. ALSA_Device *dev;
  813. ALSA_Device *next;
  814. #if SDL_ALSA_HOTPLUG_THREAD
  815. if (ALSA_hotplug_thread) {
  816. SDL_AtomicSet(&ALSA_hotplug_shutdown, 1);
  817. SDL_WaitThread(ALSA_hotplug_thread, NULL);
  818. ALSA_hotplug_thread = NULL;
  819. }
  820. #endif
  821. // Shutting down! Clean up any data we've gathered.
  822. for (dev = hotplug_devices; dev; dev = next) {
  823. //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: at shutdown, removing %s device '%s'", dev->iscapture ? "capture" : "output", dev->name);
  824. next = dev->next;
  825. SDL_free(dev->name);
  826. SDL_free(dev);
  827. }
  828. hotplug_devices = NULL;
  829. }
  830. static void ALSA_Deinitialize(void)
  831. {
  832. UnloadALSALibrary();
  833. }
  834. static SDL_bool ALSA_Init(SDL_AudioDriverImpl *impl)
  835. {
  836. if (LoadALSALibrary() < 0) {
  837. return SDL_FALSE;
  838. }
  839. impl->DetectDevices = ALSA_DetectDevices;
  840. impl->OpenDevice = ALSA_OpenDevice;
  841. impl->WaitDevice = ALSA_WaitDevice;
  842. impl->GetDeviceBuf = ALSA_GetDeviceBuf;
  843. impl->PlayDevice = ALSA_PlayDevice;
  844. impl->CloseDevice = ALSA_CloseDevice;
  845. impl->DeinitializeStart = ALSA_DeinitializeStart;
  846. impl->Deinitialize = ALSA_Deinitialize;
  847. impl->WaitCaptureDevice = ALSA_WaitDevice;
  848. impl->CaptureFromDevice = ALSA_CaptureFromDevice;
  849. impl->FlushCapture = ALSA_FlushCapture;
  850. impl->HasCaptureSupport = SDL_TRUE;
  851. return SDL_TRUE;
  852. }
  853. AudioBootStrap ALSA_bootstrap = {
  854. "alsa", "ALSA PCM audio", ALSA_Init, SDL_FALSE
  855. };
  856. #endif // SDL_AUDIO_DRIVER_ALSA