SDL_pulseaudio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. /*
  19. The PulseAudio target for SDL 1.3 is based on the 1.3 arts target, with
  20. the appropriate parts replaced with the 1.2 PulseAudio target code. This
  21. was the cleanest way to move it to 1.3. The 1.2 target was written by
  22. Stéphan Kochen: stephan .a.t. kochen.nl
  23. */
  24. #include "../../SDL_internal.h"
  25. #include "SDL_assert.h"
  26. #if SDL_AUDIO_DRIVER_PULSEAUDIO
  27. /* Allow access to a raw mixing buffer */
  28. #ifdef HAVE_SIGNAL_H
  29. #include <signal.h>
  30. #endif
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <pulse/pulseaudio.h>
  34. #include "SDL_timer.h"
  35. #include "SDL_audio.h"
  36. #include "../SDL_audio_c.h"
  37. #include "SDL_pulseaudio.h"
  38. #include "SDL_loadso.h"
  39. #include "../../thread/SDL_systhread.h"
  40. #if (PA_API_VERSION < 12)
  41. /** Return non-zero if the passed state is one of the connected states */
  42. static SDL_INLINE int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
  43. return
  44. x == PA_CONTEXT_CONNECTING ||
  45. x == PA_CONTEXT_AUTHORIZING ||
  46. x == PA_CONTEXT_SETTING_NAME ||
  47. x == PA_CONTEXT_READY;
  48. }
  49. /** Return non-zero if the passed state is one of the connected states */
  50. static SDL_INLINE int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
  51. return
  52. x == PA_STREAM_CREATING ||
  53. x == PA_STREAM_READY;
  54. }
  55. #endif /* pulseaudio <= 0.9.10 */
  56. static const char *(*PULSEAUDIO_pa_get_library_version) (void);
  57. static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
  58. pa_channel_map *, unsigned, pa_channel_map_def_t);
  59. static const char * (*PULSEAUDIO_pa_strerror) (int);
  60. static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (void);
  61. static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
  62. static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int, int *);
  63. static int (*PULSEAUDIO_pa_mainloop_run) (pa_mainloop *, int *);
  64. static void (*PULSEAUDIO_pa_mainloop_quit) (pa_mainloop *, int);
  65. static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
  66. static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
  67. pa_operation *);
  68. static void (*PULSEAUDIO_pa_operation_cancel) (pa_operation *);
  69. static void (*PULSEAUDIO_pa_operation_unref) (pa_operation *);
  70. static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
  71. const char *);
  72. static int (*PULSEAUDIO_pa_context_connect) (pa_context *, const char *,
  73. pa_context_flags_t, const pa_spawn_api *);
  74. static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_list) (pa_context *, pa_sink_info_cb_t, void *);
  75. static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_list) (pa_context *, pa_source_info_cb_t, void *);
  76. static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_by_index) (pa_context *, uint32_t, pa_sink_info_cb_t, void *);
  77. static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_by_index) (pa_context *, uint32_t, pa_source_info_cb_t, void *);
  78. static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
  79. static pa_operation * (*PULSEAUDIO_pa_context_subscribe) (pa_context *, pa_subscription_mask_t, pa_context_success_cb_t, void *);
  80. static void (*PULSEAUDIO_pa_context_set_subscribe_callback) (pa_context *, pa_context_subscribe_cb_t, void *);
  81. static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
  82. static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
  83. static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *, const char *,
  84. const pa_sample_spec *, const pa_channel_map *);
  85. static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *, const char *,
  86. const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
  87. static int (*PULSEAUDIO_pa_stream_connect_record) (pa_stream *, const char *,
  88. const pa_buffer_attr *, pa_stream_flags_t);
  89. static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
  90. static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
  91. static size_t (*PULSEAUDIO_pa_stream_readable_size) (pa_stream *);
  92. static int (*PULSEAUDIO_pa_stream_write) (pa_stream *, const void *, size_t,
  93. pa_free_cb_t, int64_t, pa_seek_mode_t);
  94. static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
  95. pa_stream_success_cb_t, void *);
  96. static int (*PULSEAUDIO_pa_stream_peek) (pa_stream *, const void **, size_t *);
  97. static int (*PULSEAUDIO_pa_stream_drop) (pa_stream *);
  98. static pa_operation * (*PULSEAUDIO_pa_stream_flush) (pa_stream *,
  99. pa_stream_success_cb_t, void *);
  100. static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
  101. static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
  102. static int load_pulseaudio_syms(void);
  103. #ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
  104. static const char *pulseaudio_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
  105. static void *pulseaudio_handle = NULL;
  106. static int
  107. load_pulseaudio_sym(const char *fn, void **addr)
  108. {
  109. *addr = SDL_LoadFunction(pulseaudio_handle, fn);
  110. if (*addr == NULL) {
  111. /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. /* cast funcs to char* first, to please GCC's strict aliasing rules. */
  117. #define SDL_PULSEAUDIO_SYM(x) \
  118. if (!load_pulseaudio_sym(#x, (void **) (char *) &PULSEAUDIO_##x)) return -1
  119. static void
  120. UnloadPulseAudioLibrary(void)
  121. {
  122. if (pulseaudio_handle != NULL) {
  123. SDL_UnloadObject(pulseaudio_handle);
  124. pulseaudio_handle = NULL;
  125. }
  126. }
  127. static int
  128. LoadPulseAudioLibrary(void)
  129. {
  130. int retval = 0;
  131. if (pulseaudio_handle == NULL) {
  132. pulseaudio_handle = SDL_LoadObject(pulseaudio_library);
  133. if (pulseaudio_handle == NULL) {
  134. retval = -1;
  135. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  136. } else {
  137. retval = load_pulseaudio_syms();
  138. if (retval < 0) {
  139. UnloadPulseAudioLibrary();
  140. }
  141. }
  142. }
  143. return retval;
  144. }
  145. #else
  146. #define SDL_PULSEAUDIO_SYM(x) PULSEAUDIO_##x = x
  147. static void
  148. UnloadPulseAudioLibrary(void)
  149. {
  150. }
  151. static int
  152. LoadPulseAudioLibrary(void)
  153. {
  154. load_pulseaudio_syms();
  155. return 0;
  156. }
  157. #endif /* SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC */
  158. static int
  159. load_pulseaudio_syms(void)
  160. {
  161. SDL_PULSEAUDIO_SYM(pa_get_library_version);
  162. SDL_PULSEAUDIO_SYM(pa_mainloop_new);
  163. SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
  164. SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
  165. SDL_PULSEAUDIO_SYM(pa_mainloop_run);
  166. SDL_PULSEAUDIO_SYM(pa_mainloop_quit);
  167. SDL_PULSEAUDIO_SYM(pa_mainloop_free);
  168. SDL_PULSEAUDIO_SYM(pa_operation_get_state);
  169. SDL_PULSEAUDIO_SYM(pa_operation_cancel);
  170. SDL_PULSEAUDIO_SYM(pa_operation_unref);
  171. SDL_PULSEAUDIO_SYM(pa_context_new);
  172. SDL_PULSEAUDIO_SYM(pa_context_connect);
  173. SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_list);
  174. SDL_PULSEAUDIO_SYM(pa_context_get_source_info_list);
  175. SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_by_index);
  176. SDL_PULSEAUDIO_SYM(pa_context_get_source_info_by_index);
  177. SDL_PULSEAUDIO_SYM(pa_context_get_state);
  178. SDL_PULSEAUDIO_SYM(pa_context_subscribe);
  179. SDL_PULSEAUDIO_SYM(pa_context_set_subscribe_callback);
  180. SDL_PULSEAUDIO_SYM(pa_context_disconnect);
  181. SDL_PULSEAUDIO_SYM(pa_context_unref);
  182. SDL_PULSEAUDIO_SYM(pa_stream_new);
  183. SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
  184. SDL_PULSEAUDIO_SYM(pa_stream_connect_record);
  185. SDL_PULSEAUDIO_SYM(pa_stream_get_state);
  186. SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
  187. SDL_PULSEAUDIO_SYM(pa_stream_readable_size);
  188. SDL_PULSEAUDIO_SYM(pa_stream_write);
  189. SDL_PULSEAUDIO_SYM(pa_stream_drain);
  190. SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
  191. SDL_PULSEAUDIO_SYM(pa_stream_peek);
  192. SDL_PULSEAUDIO_SYM(pa_stream_drop);
  193. SDL_PULSEAUDIO_SYM(pa_stream_flush);
  194. SDL_PULSEAUDIO_SYM(pa_stream_unref);
  195. SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
  196. SDL_PULSEAUDIO_SYM(pa_strerror);
  197. return 0;
  198. }
  199. static SDL_INLINE int
  200. squashVersion(const int major, const int minor, const int patch)
  201. {
  202. return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
  203. }
  204. /* Workaround for older pulse: pa_context_new() must have non-NULL appname */
  205. static const char *
  206. getAppName(void)
  207. {
  208. const char *verstr = PULSEAUDIO_pa_get_library_version();
  209. if (verstr != NULL) {
  210. int maj, min, patch;
  211. if (SDL_sscanf(verstr, "%d.%d.%d", &maj, &min, &patch) == 3) {
  212. if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
  213. return NULL; /* 0.9.15+ handles NULL correctly. */
  214. }
  215. }
  216. }
  217. return "SDL Application"; /* oh well. */
  218. }
  219. static void
  220. stream_operation_complete_no_op(pa_stream *s, int success, void *userdata)
  221. {
  222. /* no-op for pa_stream_drain(), etc, to use for callback. */
  223. }
  224. static void
  225. WaitForPulseOperation(pa_mainloop *mainloop, pa_operation *o)
  226. {
  227. /* This checks for NO errors currently. Either fix that, check results elsewhere, or do things you don't care about. */
  228. if (mainloop && o) {
  229. SDL_bool okay = SDL_TRUE;
  230. while (okay && (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_RUNNING)) {
  231. okay = (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) >= 0);
  232. }
  233. PULSEAUDIO_pa_operation_unref(o);
  234. }
  235. }
  236. static void
  237. DisconnectFromPulseServer(pa_mainloop *mainloop, pa_context *context)
  238. {
  239. if (context) {
  240. PULSEAUDIO_pa_context_disconnect(context);
  241. PULSEAUDIO_pa_context_unref(context);
  242. }
  243. if (mainloop != NULL) {
  244. PULSEAUDIO_pa_mainloop_free(mainloop);
  245. }
  246. }
  247. static int
  248. ConnectToPulseServer_Internal(pa_mainloop **_mainloop, pa_context **_context)
  249. {
  250. pa_mainloop *mainloop = NULL;
  251. pa_context *context = NULL;
  252. pa_mainloop_api *mainloop_api = NULL;
  253. int state = 0;
  254. *_mainloop = NULL;
  255. *_context = NULL;
  256. /* Set up a new main loop */
  257. if (!(mainloop = PULSEAUDIO_pa_mainloop_new())) {
  258. return SDL_SetError("pa_mainloop_new() failed");
  259. }
  260. *_mainloop = mainloop;
  261. mainloop_api = PULSEAUDIO_pa_mainloop_get_api(mainloop);
  262. SDL_assert(mainloop_api); /* this never fails, right? */
  263. context = PULSEAUDIO_pa_context_new(mainloop_api, getAppName());
  264. if (!context) {
  265. return SDL_SetError("pa_context_new() failed");
  266. }
  267. *_context = context;
  268. /* Connect to the PulseAudio server */
  269. if (PULSEAUDIO_pa_context_connect(context, NULL, 0, NULL) < 0) {
  270. return SDL_SetError("Could not setup connection to PulseAudio");
  271. }
  272. do {
  273. if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
  274. return SDL_SetError("pa_mainloop_iterate() failed");
  275. }
  276. state = PULSEAUDIO_pa_context_get_state(context);
  277. if (!PA_CONTEXT_IS_GOOD(state)) {
  278. return SDL_SetError("Could not connect to PulseAudio");
  279. }
  280. } while (state != PA_CONTEXT_READY);
  281. return 0; /* connected and ready! */
  282. }
  283. static int
  284. ConnectToPulseServer(pa_mainloop **_mainloop, pa_context **_context)
  285. {
  286. const int retval = ConnectToPulseServer_Internal(_mainloop, _context);
  287. if (retval < 0) {
  288. DisconnectFromPulseServer(*_mainloop, *_context);
  289. }
  290. return retval;
  291. }
  292. /* This function waits until it is possible to write a full sound buffer */
  293. static void
  294. PULSEAUDIO_WaitDevice(_THIS)
  295. {
  296. struct SDL_PrivateAudioData *h = this->hidden;
  297. while (SDL_AtomicGet(&this->enabled)) {
  298. if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
  299. PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
  300. PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  301. SDL_OpenedAudioDeviceDisconnected(this);
  302. return;
  303. }
  304. if (PULSEAUDIO_pa_stream_writable_size(h->stream) >= h->mixlen) {
  305. return;
  306. }
  307. }
  308. }
  309. static void
  310. PULSEAUDIO_PlayDevice(_THIS)
  311. {
  312. /* Write the audio data */
  313. struct SDL_PrivateAudioData *h = this->hidden;
  314. if (SDL_AtomicGet(&this->enabled)) {
  315. if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL, PA_SEEK_RELATIVE) < 0) {
  316. SDL_OpenedAudioDeviceDisconnected(this);
  317. }
  318. }
  319. }
  320. static Uint8 *
  321. PULSEAUDIO_GetDeviceBuf(_THIS)
  322. {
  323. return (this->hidden->mixbuf);
  324. }
  325. static int
  326. PULSEAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
  327. {
  328. struct SDL_PrivateAudioData *h = this->hidden;
  329. const void *data = NULL;
  330. size_t nbytes = 0;
  331. while (SDL_AtomicGet(&this->enabled)) {
  332. if (h->capturebuf != NULL) {
  333. const int cpy = SDL_min(buflen, h->capturelen);
  334. SDL_memcpy(buffer, h->capturebuf, cpy);
  335. /*printf("PULSEAUDIO: fed %d captured bytes\n", cpy);*/
  336. h->capturebuf += cpy;
  337. h->capturelen -= cpy;
  338. if (h->capturelen == 0) {
  339. h->capturebuf = NULL;
  340. PULSEAUDIO_pa_stream_drop(h->stream); /* done with this fragment. */
  341. }
  342. return cpy; /* new data, return it. */
  343. }
  344. if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
  345. PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
  346. PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  347. SDL_OpenedAudioDeviceDisconnected(this);
  348. return -1; /* uhoh, pulse failed! */
  349. }
  350. if (PULSEAUDIO_pa_stream_readable_size(h->stream) == 0) {
  351. continue; /* no data available yet. */
  352. }
  353. /* a new fragment is available! */
  354. PULSEAUDIO_pa_stream_peek(h->stream, &data, &nbytes);
  355. SDL_assert(nbytes > 0);
  356. if (data == NULL) { /* NULL==buffer had a hole. Ignore that. */
  357. PULSEAUDIO_pa_stream_drop(h->stream); /* drop this fragment. */
  358. } else {
  359. /* store this fragment's data, start feeding it to SDL. */
  360. /*printf("PULSEAUDIO: captured %d new bytes\n", (int) nbytes);*/
  361. h->capturebuf = (const Uint8 *) data;
  362. h->capturelen = nbytes;
  363. }
  364. }
  365. return -1; /* not enabled? */
  366. }
  367. static void
  368. PULSEAUDIO_FlushCapture(_THIS)
  369. {
  370. struct SDL_PrivateAudioData *h = this->hidden;
  371. if (h->capturebuf != NULL) {
  372. PULSEAUDIO_pa_stream_drop(h->stream);
  373. h->capturebuf = NULL;
  374. h->capturelen = 0;
  375. }
  376. WaitForPulseOperation(h->mainloop, PULSEAUDIO_pa_stream_flush(h->stream, stream_operation_complete_no_op, NULL));
  377. }
  378. static void
  379. PULSEAUDIO_CloseDevice(_THIS)
  380. {
  381. if (this->hidden->stream) {
  382. if (this->hidden->capturebuf != NULL) {
  383. PULSEAUDIO_pa_stream_drop(this->hidden->stream);
  384. }
  385. PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
  386. PULSEAUDIO_pa_stream_unref(this->hidden->stream);
  387. }
  388. DisconnectFromPulseServer(this->hidden->mainloop, this->hidden->context);
  389. SDL_free(this->hidden->mixbuf);
  390. SDL_free(this->hidden->device_name);
  391. SDL_free(this->hidden);
  392. }
  393. static void
  394. SinkDeviceNameCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
  395. {
  396. if (i) {
  397. char **devname = (char **) data;
  398. *devname = SDL_strdup(i->name);
  399. }
  400. }
  401. static void
  402. SourceDeviceNameCallback(pa_context *c, const pa_source_info *i, int is_last, void *data)
  403. {
  404. if (i) {
  405. char **devname = (char **) data;
  406. *devname = SDL_strdup(i->name);
  407. }
  408. }
  409. static SDL_bool
  410. FindDeviceName(struct SDL_PrivateAudioData *h, const int iscapture, void *handle)
  411. {
  412. const uint32_t idx = ((uint32_t) ((size_t) handle)) - 1;
  413. if (handle == NULL) { /* NULL == default device. */
  414. return SDL_TRUE;
  415. }
  416. if (iscapture) {
  417. WaitForPulseOperation(h->mainloop,
  418. PULSEAUDIO_pa_context_get_source_info_by_index(h->context, idx,
  419. SourceDeviceNameCallback, &h->device_name));
  420. } else {
  421. WaitForPulseOperation(h->mainloop,
  422. PULSEAUDIO_pa_context_get_sink_info_by_index(h->context, idx,
  423. SinkDeviceNameCallback, &h->device_name));
  424. }
  425. return (h->device_name != NULL);
  426. }
  427. static int
  428. PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
  429. {
  430. struct SDL_PrivateAudioData *h = NULL;
  431. Uint16 test_format = 0;
  432. pa_sample_spec paspec;
  433. pa_buffer_attr paattr;
  434. pa_channel_map pacmap;
  435. pa_stream_flags_t flags = 0;
  436. int state = 0;
  437. int rc = 0;
  438. /* Initialize all variables that we clean on shutdown */
  439. h = this->hidden = (struct SDL_PrivateAudioData *)
  440. SDL_malloc((sizeof *this->hidden));
  441. if (this->hidden == NULL) {
  442. return SDL_OutOfMemory();
  443. }
  444. SDL_zerop(this->hidden);
  445. paspec.format = PA_SAMPLE_INVALID;
  446. /* Try for a closest match on audio format */
  447. for (test_format = SDL_FirstAudioFormat(this->spec.format);
  448. (paspec.format == PA_SAMPLE_INVALID) && test_format;) {
  449. #ifdef DEBUG_AUDIO
  450. fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
  451. #endif
  452. switch (test_format) {
  453. case AUDIO_U8:
  454. paspec.format = PA_SAMPLE_U8;
  455. break;
  456. case AUDIO_S16LSB:
  457. paspec.format = PA_SAMPLE_S16LE;
  458. break;
  459. case AUDIO_S16MSB:
  460. paspec.format = PA_SAMPLE_S16BE;
  461. break;
  462. case AUDIO_S32LSB:
  463. paspec.format = PA_SAMPLE_S32LE;
  464. break;
  465. case AUDIO_S32MSB:
  466. paspec.format = PA_SAMPLE_S32BE;
  467. break;
  468. case AUDIO_F32LSB:
  469. paspec.format = PA_SAMPLE_FLOAT32LE;
  470. break;
  471. case AUDIO_F32MSB:
  472. paspec.format = PA_SAMPLE_FLOAT32BE;
  473. break;
  474. default:
  475. paspec.format = PA_SAMPLE_INVALID;
  476. break;
  477. }
  478. if (paspec.format == PA_SAMPLE_INVALID) {
  479. test_format = SDL_NextAudioFormat();
  480. }
  481. }
  482. if (paspec.format == PA_SAMPLE_INVALID) {
  483. return SDL_SetError("Couldn't find any hardware audio formats");
  484. }
  485. this->spec.format = test_format;
  486. /* Calculate the final parameters for this audio specification */
  487. #ifdef PA_STREAM_ADJUST_LATENCY
  488. this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */
  489. #endif
  490. SDL_CalculateAudioSpec(&this->spec);
  491. /* Allocate mixing buffer */
  492. if (!iscapture) {
  493. h->mixlen = this->spec.size;
  494. h->mixbuf = (Uint8 *) SDL_malloc(h->mixlen);
  495. if (h->mixbuf == NULL) {
  496. return SDL_OutOfMemory();
  497. }
  498. SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
  499. }
  500. paspec.channels = this->spec.channels;
  501. paspec.rate = this->spec.freq;
  502. /* Reduced prebuffering compared to the defaults. */
  503. #ifdef PA_STREAM_ADJUST_LATENCY
  504. /* 2x original requested bufsize */
  505. paattr.tlength = h->mixlen * 4;
  506. paattr.prebuf = -1;
  507. paattr.maxlength = -1;
  508. /* -1 can lead to pa_stream_writable_size() >= mixlen never being true */
  509. paattr.minreq = h->mixlen;
  510. flags = PA_STREAM_ADJUST_LATENCY;
  511. #else
  512. paattr.tlength = h->mixlen*2;
  513. paattr.prebuf = h->mixlen*2;
  514. paattr.maxlength = h->mixlen*2;
  515. paattr.minreq = h->mixlen;
  516. #endif
  517. if (ConnectToPulseServer(&h->mainloop, &h->context) < 0) {
  518. return SDL_SetError("Could not connect to PulseAudio server");
  519. }
  520. if (!FindDeviceName(h, iscapture, handle)) {
  521. return SDL_SetError("Requested PulseAudio sink/source missing?");
  522. }
  523. /* The SDL ALSA output hints us that we use Windows' channel mapping */
  524. /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
  525. PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels,
  526. PA_CHANNEL_MAP_WAVEEX);
  527. h->stream = PULSEAUDIO_pa_stream_new(
  528. h->context,
  529. "Simple DirectMedia Layer", /* stream description */
  530. &paspec, /* sample format spec */
  531. &pacmap /* channel map */
  532. );
  533. if (h->stream == NULL) {
  534. return SDL_SetError("Could not set up PulseAudio stream");
  535. }
  536. /* now that we have multi-device support, don't move a stream from
  537. a device that was unplugged to something else, unless we're default. */
  538. if (h->device_name != NULL) {
  539. flags |= PA_STREAM_DONT_MOVE;
  540. }
  541. if (iscapture) {
  542. rc = PULSEAUDIO_pa_stream_connect_record(h->stream, h->device_name, &paattr, flags);
  543. } else {
  544. rc = PULSEAUDIO_pa_stream_connect_playback(h->stream, h->device_name, &paattr, flags, NULL, NULL);
  545. }
  546. if (rc < 0) {
  547. return SDL_SetError("Could not connect PulseAudio stream");
  548. }
  549. do {
  550. if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  551. return SDL_SetError("pa_mainloop_iterate() failed");
  552. }
  553. state = PULSEAUDIO_pa_stream_get_state(h->stream);
  554. if (!PA_STREAM_IS_GOOD(state)) {
  555. return SDL_SetError("Could not connect PulseAudio stream");
  556. }
  557. } while (state != PA_STREAM_READY);
  558. /* We're ready to rock and roll. :-) */
  559. return 0;
  560. }
  561. static pa_mainloop *hotplug_mainloop = NULL;
  562. static pa_context *hotplug_context = NULL;
  563. static SDL_Thread *hotplug_thread = NULL;
  564. /* device handles are device index + 1, cast to void*, so we never pass a NULL. */
  565. /* This is called when PulseAudio adds an output ("sink") device. */
  566. static void
  567. SinkInfoCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
  568. {
  569. if (i) {
  570. SDL_AddAudioDevice(SDL_FALSE, i->description, (void *) ((size_t) i->index+1));
  571. }
  572. }
  573. /* This is called when PulseAudio adds a capture ("source") device. */
  574. static void
  575. SourceInfoCallback(pa_context *c, const pa_source_info *i, int is_last, void *data)
  576. {
  577. if (i) {
  578. /* Skip "monitor" sources. These are just output from other sinks. */
  579. if (i->monitor_of_sink == PA_INVALID_INDEX) {
  580. SDL_AddAudioDevice(SDL_TRUE, i->description, (void *) ((size_t) i->index+1));
  581. }
  582. }
  583. }
  584. /* This is called when PulseAudio has a device connected/removed/changed. */
  585. static void
  586. HotplugCallback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *data)
  587. {
  588. const SDL_bool added = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW);
  589. const SDL_bool removed = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE);
  590. if (added || removed) { /* we only care about add/remove events. */
  591. const SDL_bool sink = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK);
  592. const SDL_bool source = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
  593. /* adds need sink details from the PulseAudio server. Another callback... */
  594. if (added && sink) {
  595. PULSEAUDIO_pa_context_get_sink_info_by_index(hotplug_context, idx, SinkInfoCallback, NULL);
  596. } else if (added && source) {
  597. PULSEAUDIO_pa_context_get_source_info_by_index(hotplug_context, idx, SourceInfoCallback, NULL);
  598. } else if (removed && (sink || source)) {
  599. /* removes we can handle just with the device index. */
  600. SDL_RemoveAudioDevice(source != 0, (void *) ((size_t) idx+1));
  601. }
  602. }
  603. }
  604. /* this runs as a thread while the Pulse target is initialized to catch hotplug events. */
  605. static int SDLCALL
  606. HotplugThread(void *data)
  607. {
  608. pa_operation *o;
  609. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
  610. PULSEAUDIO_pa_context_set_subscribe_callback(hotplug_context, HotplugCallback, NULL);
  611. o = PULSEAUDIO_pa_context_subscribe(hotplug_context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, NULL, NULL);
  612. PULSEAUDIO_pa_operation_unref(o); /* don't wait for it, just do our thing. */
  613. PULSEAUDIO_pa_mainloop_run(hotplug_mainloop, NULL);
  614. return 0;
  615. }
  616. static void
  617. PULSEAUDIO_DetectDevices()
  618. {
  619. WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_sink_info_list(hotplug_context, SinkInfoCallback, NULL));
  620. WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_source_info_list(hotplug_context, SourceInfoCallback, NULL));
  621. /* ok, we have a sane list, let's set up hotplug notifications now... */
  622. hotplug_thread = SDL_CreateThreadInternal(HotplugThread, "PulseHotplug", 256 * 1024, NULL);
  623. }
  624. static void
  625. PULSEAUDIO_Deinitialize(void)
  626. {
  627. if (hotplug_thread) {
  628. PULSEAUDIO_pa_mainloop_quit(hotplug_mainloop, 0);
  629. SDL_WaitThread(hotplug_thread, NULL);
  630. hotplug_thread = NULL;
  631. }
  632. DisconnectFromPulseServer(hotplug_mainloop, hotplug_context);
  633. hotplug_mainloop = NULL;
  634. hotplug_context = NULL;
  635. UnloadPulseAudioLibrary();
  636. }
  637. static int
  638. PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
  639. {
  640. if (LoadPulseAudioLibrary() < 0) {
  641. return 0;
  642. }
  643. if (ConnectToPulseServer(&hotplug_mainloop, &hotplug_context) < 0) {
  644. UnloadPulseAudioLibrary();
  645. return 0;
  646. }
  647. /* Set the function pointers */
  648. impl->DetectDevices = PULSEAUDIO_DetectDevices;
  649. impl->OpenDevice = PULSEAUDIO_OpenDevice;
  650. impl->PlayDevice = PULSEAUDIO_PlayDevice;
  651. impl->WaitDevice = PULSEAUDIO_WaitDevice;
  652. impl->GetDeviceBuf = PULSEAUDIO_GetDeviceBuf;
  653. impl->CloseDevice = PULSEAUDIO_CloseDevice;
  654. impl->Deinitialize = PULSEAUDIO_Deinitialize;
  655. impl->CaptureFromDevice = PULSEAUDIO_CaptureFromDevice;
  656. impl->FlushCapture = PULSEAUDIO_FlushCapture;
  657. impl->HasCaptureSupport = SDL_TRUE;
  658. return 1; /* this audio target is available. */
  659. }
  660. AudioBootStrap PULSEAUDIO_bootstrap = {
  661. "pulseaudio", "PulseAudio", PULSEAUDIO_Init, 0
  662. };
  663. #endif /* SDL_AUDIO_DRIVER_PULSEAUDIO */
  664. /* vi: set ts=4 sw=4 expandtab: */