SDL_pulseaudio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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 <errno.h>
  34. #include <pulse/pulseaudio.h>
  35. #include "SDL_timer.h"
  36. #include "SDL_audio.h"
  37. #include "../SDL_audiomem.h"
  38. #include "../SDL_audio_c.h"
  39. #include "SDL_pulseaudio.h"
  40. #include "SDL_loadso.h"
  41. #if (PA_API_VERSION < 12)
  42. /** Return non-zero if the passed state is one of the connected states */
  43. static SDL_INLINE int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
  44. return
  45. x == PA_CONTEXT_CONNECTING ||
  46. x == PA_CONTEXT_AUTHORIZING ||
  47. x == PA_CONTEXT_SETTING_NAME ||
  48. x == PA_CONTEXT_READY;
  49. }
  50. /** Return non-zero if the passed state is one of the connected states */
  51. static SDL_INLINE int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
  52. return
  53. x == PA_STREAM_CREATING ||
  54. x == PA_STREAM_READY;
  55. }
  56. #endif /* pulseaudio <= 0.9.10 */
  57. static const char *(*PULSEAUDIO_pa_get_library_version) (void);
  58. static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
  59. pa_channel_map *, unsigned, pa_channel_map_def_t);
  60. static const char * (*PULSEAUDIO_pa_strerror) (int);
  61. static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (void);
  62. static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
  63. static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int, int *);
  64. static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
  65. static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
  66. pa_operation *);
  67. static void (*PULSEAUDIO_pa_operation_cancel) (pa_operation *);
  68. static void (*PULSEAUDIO_pa_operation_unref) (pa_operation *);
  69. static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
  70. const char *);
  71. static int (*PULSEAUDIO_pa_context_connect) (pa_context *, const char *,
  72. pa_context_flags_t, const pa_spawn_api *);
  73. static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_list)(pa_context *, pa_sink_info_cb_t, void *);
  74. static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
  75. static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
  76. static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
  77. static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *, const char *,
  78. const pa_sample_spec *, const pa_channel_map *);
  79. static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *, const char *,
  80. const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
  81. static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
  82. static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
  83. static int (*PULSEAUDIO_pa_stream_write) (pa_stream *, const void *, size_t,
  84. pa_free_cb_t, int64_t, pa_seek_mode_t);
  85. static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
  86. pa_stream_success_cb_t, void *);
  87. static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
  88. static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
  89. static int load_pulseaudio_syms(void);
  90. #ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
  91. static const char *pulseaudio_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
  92. static void *pulseaudio_handle = NULL;
  93. static int
  94. load_pulseaudio_sym(const char *fn, void **addr)
  95. {
  96. *addr = SDL_LoadFunction(pulseaudio_handle, fn);
  97. if (*addr == NULL) {
  98. /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. /* cast funcs to char* first, to please GCC's strict aliasing rules. */
  104. #define SDL_PULSEAUDIO_SYM(x) \
  105. if (!load_pulseaudio_sym(#x, (void **) (char *) &PULSEAUDIO_##x)) return -1
  106. static void
  107. UnloadPulseAudioLibrary(void)
  108. {
  109. if (pulseaudio_handle != NULL) {
  110. SDL_UnloadObject(pulseaudio_handle);
  111. pulseaudio_handle = NULL;
  112. }
  113. }
  114. static int
  115. LoadPulseAudioLibrary(void)
  116. {
  117. int retval = 0;
  118. if (pulseaudio_handle == NULL) {
  119. pulseaudio_handle = SDL_LoadObject(pulseaudio_library);
  120. if (pulseaudio_handle == NULL) {
  121. retval = -1;
  122. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  123. } else {
  124. retval = load_pulseaudio_syms();
  125. if (retval < 0) {
  126. UnloadPulseAudioLibrary();
  127. }
  128. }
  129. }
  130. return retval;
  131. }
  132. #else
  133. #define SDL_PULSEAUDIO_SYM(x) PULSEAUDIO_##x = x
  134. static void
  135. UnloadPulseAudioLibrary(void)
  136. {
  137. }
  138. static int
  139. LoadPulseAudioLibrary(void)
  140. {
  141. load_pulseaudio_syms();
  142. return 0;
  143. }
  144. #endif /* SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC */
  145. static int
  146. load_pulseaudio_syms(void)
  147. {
  148. SDL_PULSEAUDIO_SYM(pa_get_library_version);
  149. SDL_PULSEAUDIO_SYM(pa_mainloop_new);
  150. SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
  151. SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
  152. SDL_PULSEAUDIO_SYM(pa_mainloop_free);
  153. SDL_PULSEAUDIO_SYM(pa_operation_get_state);
  154. SDL_PULSEAUDIO_SYM(pa_operation_cancel);
  155. SDL_PULSEAUDIO_SYM(pa_operation_unref);
  156. SDL_PULSEAUDIO_SYM(pa_context_new);
  157. SDL_PULSEAUDIO_SYM(pa_context_connect);
  158. SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_list);
  159. SDL_PULSEAUDIO_SYM(pa_context_get_state);
  160. SDL_PULSEAUDIO_SYM(pa_context_disconnect);
  161. SDL_PULSEAUDIO_SYM(pa_context_unref);
  162. SDL_PULSEAUDIO_SYM(pa_stream_new);
  163. SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
  164. SDL_PULSEAUDIO_SYM(pa_stream_get_state);
  165. SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
  166. SDL_PULSEAUDIO_SYM(pa_stream_write);
  167. SDL_PULSEAUDIO_SYM(pa_stream_drain);
  168. SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
  169. SDL_PULSEAUDIO_SYM(pa_stream_unref);
  170. SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
  171. SDL_PULSEAUDIO_SYM(pa_strerror);
  172. return 0;
  173. }
  174. static SDL_INLINE int
  175. squashVersion(const int major, const int minor, const int patch)
  176. {
  177. return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
  178. }
  179. /* Workaround for older pulse: pa_context_new() must have non-NULL appname */
  180. static const char *
  181. getAppName(void)
  182. {
  183. const char *verstr = PULSEAUDIO_pa_get_library_version();
  184. if (verstr != NULL) {
  185. int maj, min, patch;
  186. if (SDL_sscanf(verstr, "%d.%d.%d", &maj, &min, &patch) == 3) {
  187. if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
  188. return NULL; /* 0.9.15+ handles NULL correctly. */
  189. }
  190. }
  191. }
  192. return "SDL Application"; /* oh well. */
  193. }
  194. static void
  195. DisconnectFromPulseServer(pa_mainloop *mainloop, pa_context *context)
  196. {
  197. if (context) {
  198. PULSEAUDIO_pa_context_disconnect(context);
  199. PULSEAUDIO_pa_context_unref(context);
  200. }
  201. if (mainloop != NULL) {
  202. PULSEAUDIO_pa_mainloop_free(mainloop);
  203. }
  204. }
  205. static int
  206. ConnectToPulseServer_Internal(pa_mainloop **_mainloop, pa_context **_context)
  207. {
  208. pa_mainloop *mainloop = NULL;
  209. pa_context *context = NULL;
  210. pa_mainloop_api *mainloop_api = NULL;
  211. int state = 0;
  212. *_mainloop = NULL;
  213. *_context = NULL;
  214. /* Set up a new main loop */
  215. if (!(mainloop = PULSEAUDIO_pa_mainloop_new())) {
  216. return SDL_SetError("pa_mainloop_new() failed");
  217. }
  218. *_mainloop = mainloop;
  219. mainloop_api = PULSEAUDIO_pa_mainloop_get_api(mainloop);
  220. SDL_assert(mainloop_api); /* this never fails, right? */
  221. context = PULSEAUDIO_pa_context_new(mainloop_api, getAppName());
  222. if (!context) {
  223. return SDL_SetError("pa_context_new() failed");
  224. }
  225. *_context = context;
  226. /* Connect to the PulseAudio server */
  227. if (PULSEAUDIO_pa_context_connect(context, NULL, 0, NULL) < 0) {
  228. return SDL_SetError("Could not setup connection to PulseAudio");
  229. }
  230. do {
  231. if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
  232. return SDL_SetError("pa_mainloop_iterate() failed");
  233. }
  234. state = PULSEAUDIO_pa_context_get_state(context);
  235. if (!PA_CONTEXT_IS_GOOD(state)) {
  236. return SDL_SetError("Could not connect to PulseAudio");
  237. }
  238. } while (state != PA_CONTEXT_READY);
  239. return 0; /* connected and ready! */
  240. }
  241. static int
  242. ConnectToPulseServer(pa_mainloop **_mainloop, pa_context **_context)
  243. {
  244. const int retval = ConnectToPulseServer_Internal(_mainloop, _context);
  245. if (retval < 0) {
  246. DisconnectFromPulseServer(*_mainloop, *_context);
  247. }
  248. return retval;
  249. }
  250. /* This function waits until it is possible to write a full sound buffer */
  251. static void
  252. PULSEAUDIO_WaitDevice(_THIS)
  253. {
  254. struct SDL_PrivateAudioData *h = this->hidden;
  255. while(1) {
  256. if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
  257. PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
  258. PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  259. this->enabled = 0;
  260. return;
  261. }
  262. if (PULSEAUDIO_pa_stream_writable_size(h->stream) >= h->mixlen) {
  263. return;
  264. }
  265. }
  266. }
  267. static void
  268. PULSEAUDIO_PlayDevice(_THIS)
  269. {
  270. /* Write the audio data */
  271. struct SDL_PrivateAudioData *h = this->hidden;
  272. if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL,
  273. PA_SEEK_RELATIVE) < 0) {
  274. this->enabled = 0;
  275. }
  276. }
  277. static void
  278. stream_drain_complete(pa_stream *s, int success, void *userdata)
  279. {
  280. /* no-op for pa_stream_drain() to use for callback. */
  281. }
  282. static void
  283. PULSEAUDIO_WaitDone(_THIS)
  284. {
  285. struct SDL_PrivateAudioData *h = this->hidden;
  286. pa_operation *o;
  287. o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
  288. if (!o) {
  289. return;
  290. }
  291. while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
  292. if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
  293. PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
  294. PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  295. PULSEAUDIO_pa_operation_cancel(o);
  296. break;
  297. }
  298. }
  299. PULSEAUDIO_pa_operation_unref(o);
  300. }
  301. static Uint8 *
  302. PULSEAUDIO_GetDeviceBuf(_THIS)
  303. {
  304. return (this->hidden->mixbuf);
  305. }
  306. static void
  307. PULSEAUDIO_CloseDevice(_THIS)
  308. {
  309. if (this->hidden != NULL) {
  310. SDL_FreeAudioMem(this->hidden->mixbuf);
  311. this->hidden->mixbuf = NULL;
  312. if (this->hidden->stream) {
  313. PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
  314. PULSEAUDIO_pa_stream_unref(this->hidden->stream);
  315. this->hidden->stream = NULL;
  316. }
  317. DisconnectFromPulseServer(this->hidden->mainloop, this->hidden->context);
  318. SDL_free(this->hidden);
  319. this->hidden = NULL;
  320. }
  321. }
  322. static int
  323. PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
  324. {
  325. struct SDL_PrivateAudioData *h = NULL;
  326. Uint16 test_format = 0;
  327. pa_sample_spec paspec;
  328. pa_buffer_attr paattr;
  329. pa_channel_map pacmap;
  330. pa_stream_flags_t flags = 0;
  331. int state = 0;
  332. /* Initialize all variables that we clean on shutdown */
  333. this->hidden = (struct SDL_PrivateAudioData *)
  334. SDL_malloc((sizeof *this->hidden));
  335. if (this->hidden == NULL) {
  336. return SDL_OutOfMemory();
  337. }
  338. SDL_memset(this->hidden, 0, (sizeof *this->hidden));
  339. h = this->hidden;
  340. paspec.format = PA_SAMPLE_INVALID;
  341. /* Try for a closest match on audio format */
  342. for (test_format = SDL_FirstAudioFormat(this->spec.format);
  343. (paspec.format == PA_SAMPLE_INVALID) && test_format;) {
  344. #ifdef DEBUG_AUDIO
  345. fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
  346. #endif
  347. switch (test_format) {
  348. case AUDIO_U8:
  349. paspec.format = PA_SAMPLE_U8;
  350. break;
  351. case AUDIO_S16LSB:
  352. paspec.format = PA_SAMPLE_S16LE;
  353. break;
  354. case AUDIO_S16MSB:
  355. paspec.format = PA_SAMPLE_S16BE;
  356. break;
  357. case AUDIO_S32LSB:
  358. paspec.format = PA_SAMPLE_S32LE;
  359. break;
  360. case AUDIO_S32MSB:
  361. paspec.format = PA_SAMPLE_S32BE;
  362. break;
  363. case AUDIO_F32LSB:
  364. paspec.format = PA_SAMPLE_FLOAT32LE;
  365. break;
  366. case AUDIO_F32MSB:
  367. paspec.format = PA_SAMPLE_FLOAT32BE;
  368. break;
  369. default:
  370. paspec.format = PA_SAMPLE_INVALID;
  371. break;
  372. }
  373. if (paspec.format == PA_SAMPLE_INVALID) {
  374. test_format = SDL_NextAudioFormat();
  375. }
  376. }
  377. if (paspec.format == PA_SAMPLE_INVALID) {
  378. PULSEAUDIO_CloseDevice(this);
  379. return SDL_SetError("Couldn't find any hardware audio formats");
  380. }
  381. this->spec.format = test_format;
  382. /* Calculate the final parameters for this audio specification */
  383. #ifdef PA_STREAM_ADJUST_LATENCY
  384. this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */
  385. #endif
  386. SDL_CalculateAudioSpec(&this->spec);
  387. /* Allocate mixing buffer */
  388. h->mixlen = this->spec.size;
  389. h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen);
  390. if (h->mixbuf == NULL) {
  391. PULSEAUDIO_CloseDevice(this);
  392. return SDL_OutOfMemory();
  393. }
  394. SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
  395. paspec.channels = this->spec.channels;
  396. paspec.rate = this->spec.freq;
  397. /* Reduced prebuffering compared to the defaults. */
  398. #ifdef PA_STREAM_ADJUST_LATENCY
  399. /* 2x original requested bufsize */
  400. paattr.tlength = h->mixlen * 4;
  401. paattr.prebuf = -1;
  402. paattr.maxlength = -1;
  403. /* -1 can lead to pa_stream_writable_size() >= mixlen never being true */
  404. paattr.minreq = h->mixlen;
  405. flags = PA_STREAM_ADJUST_LATENCY;
  406. #else
  407. paattr.tlength = h->mixlen*2;
  408. paattr.prebuf = h->mixlen*2;
  409. paattr.maxlength = h->mixlen*2;
  410. paattr.minreq = h->mixlen;
  411. #endif
  412. if (ConnectToPulseServer(&h->mainloop, &h->context) < 0) {
  413. PULSEAUDIO_CloseDevice(this);
  414. return SDL_SetError("Could not connect to PulseAudio server");
  415. }
  416. /* The SDL ALSA output hints us that we use Windows' channel mapping */
  417. /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
  418. PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels,
  419. PA_CHANNEL_MAP_WAVEEX);
  420. h->stream = PULSEAUDIO_pa_stream_new(
  421. h->context,
  422. "Simple DirectMedia Layer", /* stream description */
  423. &paspec, /* sample format spec */
  424. &pacmap /* channel map */
  425. );
  426. if (h->stream == NULL) {
  427. PULSEAUDIO_CloseDevice(this);
  428. return SDL_SetError("Could not set up PulseAudio stream");
  429. }
  430. if (PULSEAUDIO_pa_stream_connect_playback(h->stream, devname, &paattr, flags,
  431. NULL, NULL) < 0) {
  432. PULSEAUDIO_CloseDevice(this);
  433. return SDL_SetError("Could not connect PulseAudio stream");
  434. }
  435. do {
  436. if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
  437. PULSEAUDIO_CloseDevice(this);
  438. return SDL_SetError("pa_mainloop_iterate() failed");
  439. }
  440. state = PULSEAUDIO_pa_stream_get_state(h->stream);
  441. if (!PA_STREAM_IS_GOOD(state)) {
  442. PULSEAUDIO_CloseDevice(this);
  443. return SDL_SetError("Could not create to PulseAudio stream");
  444. }
  445. } while (state != PA_STREAM_READY);
  446. /* We're ready to rock and roll. :-) */
  447. return 0;
  448. }
  449. typedef struct
  450. {
  451. uint8_t last;
  452. SDL_AddAudioDevice addfn;
  453. } sink_struct;
  454. static void
  455. get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata)
  456. {
  457. sink_struct *a = (sink_struct *) userdata;
  458. a->last = is_last;
  459. if (i) {
  460. a->addfn(i->name);
  461. }
  462. }
  463. static void
  464. PULSEAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
  465. {
  466. pa_mainloop *mainloop = NULL;
  467. pa_context *context = NULL;
  468. if (ConnectToPulseServer(&mainloop, &context) < 0) {
  469. return;
  470. }
  471. if (!iscapture) {
  472. sink_struct a = { 0, addfn };
  473. pa_operation *o = PULSEAUDIO_pa_context_get_sink_info_list(context,
  474. get_sink_info_callback, &a);
  475. while (!a.last) {
  476. if (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_CANCELLED) {
  477. break;
  478. }
  479. if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
  480. break;
  481. }
  482. }
  483. }
  484. DisconnectFromPulseServer(mainloop, context);
  485. }
  486. static void
  487. PULSEAUDIO_Deinitialize(void)
  488. {
  489. UnloadPulseAudioLibrary();
  490. }
  491. static int
  492. PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
  493. {
  494. pa_mainloop *mainloop = NULL;
  495. pa_context *context = NULL;
  496. if (LoadPulseAudioLibrary() < 0) {
  497. return 0;
  498. }
  499. if (ConnectToPulseServer(&mainloop, &context) < 0) {
  500. UnloadPulseAudioLibrary();
  501. return 0;
  502. }
  503. DisconnectFromPulseServer(mainloop, context);
  504. /* Set the function pointers */
  505. impl->DetectDevices = PULSEAUDIO_DetectDevices;
  506. impl->OpenDevice = PULSEAUDIO_OpenDevice;
  507. impl->PlayDevice = PULSEAUDIO_PlayDevice;
  508. impl->WaitDevice = PULSEAUDIO_WaitDevice;
  509. impl->GetDeviceBuf = PULSEAUDIO_GetDeviceBuf;
  510. impl->CloseDevice = PULSEAUDIO_CloseDevice;
  511. impl->WaitDone = PULSEAUDIO_WaitDone;
  512. impl->Deinitialize = PULSEAUDIO_Deinitialize;
  513. return 1; /* this audio target is available. */
  514. }
  515. AudioBootStrap PULSEAUDIO_bootstrap = {
  516. "pulseaudio", "PulseAudio", PULSEAUDIO_Init, 0
  517. };
  518. #endif /* SDL_AUDIO_DRIVER_PULSEAUDIO */
  519. /* vi: set ts=4 sw=4 expandtab: */