28 #include "../../SDL_internal.h"
31 #if SDL_AUDIO_DRIVER_PULSEAUDIO
39 #include <sys/types.h>
40 #include <pulse/pulseaudio.h>
44 #include "../SDL_audio_c.h"
47 #include "../../thread/SDL_systhread.h"
49 #if (PA_API_VERSION < 12)
51 static SDL_INLINE int PA_CONTEXT_IS_GOOD(pa_context_state_t
x) {
53 x == PA_CONTEXT_CONNECTING ||
54 x == PA_CONTEXT_AUTHORIZING ||
55 x == PA_CONTEXT_SETTING_NAME ||
56 x == PA_CONTEXT_READY;
59 static SDL_INLINE int PA_STREAM_IS_GOOD(pa_stream_state_t
x) {
61 x == PA_STREAM_CREATING ||
67 static const char *(*PULSEAUDIO_pa_get_library_version) (
void);
68 static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
69 pa_channel_map *, unsigned, pa_channel_map_def_t);
70 static const char * (*PULSEAUDIO_pa_strerror) (int);
71 static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (
void);
72 static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
73 static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int,
int *);
74 static int (*PULSEAUDIO_pa_mainloop_run) (pa_mainloop *,
int *);
75 static void (*PULSEAUDIO_pa_mainloop_quit) (pa_mainloop *, int);
76 static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
78 static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
80 static void (*PULSEAUDIO_pa_operation_cancel) (pa_operation *);
81 static void (*PULSEAUDIO_pa_operation_unref) (pa_operation *);
83 static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
85 static int (*PULSEAUDIO_pa_context_connect) (pa_context *,
const char *,
86 pa_context_flags_t,
const pa_spawn_api *);
87 static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_list) (pa_context *, pa_sink_info_cb_t,
void *);
88 static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_list) (pa_context *, pa_source_info_cb_t,
void *);
89 static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_by_index) (pa_context *,
uint32_t, pa_sink_info_cb_t,
void *);
90 static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_by_index) (pa_context *,
uint32_t, pa_source_info_cb_t,
void *);
91 static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
92 static pa_operation * (*PULSEAUDIO_pa_context_subscribe) (pa_context *, pa_subscription_mask_t, pa_context_success_cb_t,
void *);
93 static void (*PULSEAUDIO_pa_context_set_subscribe_callback) (pa_context *, pa_context_subscribe_cb_t,
void *);
94 static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
95 static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
97 static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *,
const char *,
98 const pa_sample_spec *,
const pa_channel_map *);
99 static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *,
const char *,
100 const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
101 static int (*PULSEAUDIO_pa_stream_connect_record) (pa_stream *,
const char *,
102 const pa_buffer_attr *, pa_stream_flags_t);
103 static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
104 static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
105 static size_t (*PULSEAUDIO_pa_stream_readable_size) (pa_stream *);
106 static int (*PULSEAUDIO_pa_stream_write) (pa_stream *,
const void *,
size_t,
107 pa_free_cb_t,
int64_t, pa_seek_mode_t);
108 static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
109 pa_stream_success_cb_t,
void *);
110 static int (*PULSEAUDIO_pa_stream_peek) (pa_stream *,
const void **,
size_t *);
111 static int (*PULSEAUDIO_pa_stream_drop) (pa_stream *);
112 static pa_operation * (*PULSEAUDIO_pa_stream_flush) (pa_stream *,
113 pa_stream_success_cb_t,
void *);
114 static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
115 static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
117 static int load_pulseaudio_syms(
void);
120 #ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
122 static const char *pulseaudio_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
123 static void *pulseaudio_handle =
NULL;
126 load_pulseaudio_sym(
const char *fn,
void **
addr)
138 #define SDL_PULSEAUDIO_SYM(x) \
139 if (!load_pulseaudio_sym(#x, (void **) (char *) &PULSEAUDIO_##x)) return -1
142 UnloadPulseAudioLibrary(
void)
144 if (pulseaudio_handle !=
NULL) {
146 pulseaudio_handle =
NULL;
151 LoadPulseAudioLibrary(
void)
154 if (pulseaudio_handle ==
NULL) {
156 if (pulseaudio_handle ==
NULL) {
160 retval = load_pulseaudio_syms();
162 UnloadPulseAudioLibrary();
171 #define SDL_PULSEAUDIO_SYM(x) PULSEAUDIO_##x = x
174 UnloadPulseAudioLibrary(
void)
179 LoadPulseAudioLibrary(
void)
181 load_pulseaudio_syms();
189 load_pulseaudio_syms(
void)
191 SDL_PULSEAUDIO_SYM(pa_get_library_version);
192 SDL_PULSEAUDIO_SYM(pa_mainloop_new);
193 SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
194 SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
195 SDL_PULSEAUDIO_SYM(pa_mainloop_run);
196 SDL_PULSEAUDIO_SYM(pa_mainloop_quit);
197 SDL_PULSEAUDIO_SYM(pa_mainloop_free);
198 SDL_PULSEAUDIO_SYM(pa_operation_get_state);
199 SDL_PULSEAUDIO_SYM(pa_operation_cancel);
200 SDL_PULSEAUDIO_SYM(pa_operation_unref);
201 SDL_PULSEAUDIO_SYM(pa_context_new);
202 SDL_PULSEAUDIO_SYM(pa_context_connect);
203 SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_list);
204 SDL_PULSEAUDIO_SYM(pa_context_get_source_info_list);
205 SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_by_index);
206 SDL_PULSEAUDIO_SYM(pa_context_get_source_info_by_index);
207 SDL_PULSEAUDIO_SYM(pa_context_get_state);
208 SDL_PULSEAUDIO_SYM(pa_context_subscribe);
209 SDL_PULSEAUDIO_SYM(pa_context_set_subscribe_callback);
210 SDL_PULSEAUDIO_SYM(pa_context_disconnect);
211 SDL_PULSEAUDIO_SYM(pa_context_unref);
212 SDL_PULSEAUDIO_SYM(pa_stream_new);
213 SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
214 SDL_PULSEAUDIO_SYM(pa_stream_connect_record);
215 SDL_PULSEAUDIO_SYM(pa_stream_get_state);
216 SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
217 SDL_PULSEAUDIO_SYM(pa_stream_readable_size);
218 SDL_PULSEAUDIO_SYM(pa_stream_write);
219 SDL_PULSEAUDIO_SYM(pa_stream_drain);
220 SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
221 SDL_PULSEAUDIO_SYM(pa_stream_peek);
222 SDL_PULSEAUDIO_SYM(pa_stream_drop);
223 SDL_PULSEAUDIO_SYM(pa_stream_flush);
224 SDL_PULSEAUDIO_SYM(pa_stream_unref);
225 SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
226 SDL_PULSEAUDIO_SYM(pa_strerror);
231 squashVersion(
const int major,
const int minor,
const int patch)
233 return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
240 const char *verstr = PULSEAUDIO_pa_get_library_version();
241 if (verstr !=
NULL) {
243 if (
SDL_sscanf(verstr,
"%d.%d.%d", &maj, &min, &patch) == 3) {
244 if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
249 return "SDL Application";
253 stream_operation_complete_no_op(pa_stream *
s,
int success,
void *userdata)
259 WaitForPulseOperation(pa_mainloop *mainloop, pa_operation *o)
264 while (okay && (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_RUNNING)) {
265 okay = (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1,
NULL) >= 0);
267 PULSEAUDIO_pa_operation_unref(o);
272 DisconnectFromPulseServer(pa_mainloop *mainloop, pa_context *
context)
275 PULSEAUDIO_pa_context_disconnect(context);
276 PULSEAUDIO_pa_context_unref(context);
278 if (mainloop !=
NULL) {
279 PULSEAUDIO_pa_mainloop_free(mainloop);
284 ConnectToPulseServer_Internal(pa_mainloop **_mainloop, pa_context **_context)
286 pa_mainloop *mainloop =
NULL;
287 pa_context *context =
NULL;
288 pa_mainloop_api *mainloop_api =
NULL;
295 if (!(mainloop = PULSEAUDIO_pa_mainloop_new())) {
299 *_mainloop = mainloop;
301 mainloop_api = PULSEAUDIO_pa_mainloop_get_api(mainloop);
304 context = PULSEAUDIO_pa_context_new(mainloop_api, getAppName());
311 if (PULSEAUDIO_pa_context_connect(context,
NULL, 0,
NULL) < 0) {
312 return SDL_SetError(
"Could not setup connection to PulseAudio");
316 if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1,
NULL) < 0) {
319 state = PULSEAUDIO_pa_context_get_state(context);
320 if (!PA_CONTEXT_IS_GOOD(state)) {
323 }
while (state != PA_CONTEXT_READY);
329 ConnectToPulseServer(pa_mainloop **_mainloop, pa_context **_context)
331 const int retval = ConnectToPulseServer_Internal(_mainloop, _context);
333 DisconnectFromPulseServer(*_mainloop, *_context);
341 PULSEAUDIO_WaitDevice(
_THIS)
346 if (PULSEAUDIO_pa_context_get_state(h->
context) != PA_CONTEXT_READY ||
347 PULSEAUDIO_pa_stream_get_state(h->
stream) != PA_STREAM_READY ||
348 PULSEAUDIO_pa_mainloop_iterate(h->
mainloop, 1,
NULL) < 0) {
352 if (PULSEAUDIO_pa_stream_writable_size(h->
stream) >= h->
mixlen) {
359 PULSEAUDIO_PlayDevice(
_THIS)
371 PULSEAUDIO_GetDeviceBuf(
_THIS)
373 return (this->hidden->mixbuf);
378 PULSEAUDIO_CaptureFromDevice(
_THIS,
void *
buffer,
int buflen)
393 PULSEAUDIO_pa_stream_drop(h->
stream);
398 if (PULSEAUDIO_pa_context_get_state(h->
context) != PA_CONTEXT_READY ||
399 PULSEAUDIO_pa_stream_get_state(h->
stream) != PA_STREAM_READY ||
400 PULSEAUDIO_pa_mainloop_iterate(h->
mainloop, 1,
NULL) < 0) {
405 if (PULSEAUDIO_pa_stream_readable_size(h->
stream) == 0) {
410 PULSEAUDIO_pa_stream_peek(h->
stream, &data, &nbytes);
413 PULSEAUDIO_pa_stream_drop(h->
stream);
426 PULSEAUDIO_FlushCapture(
_THIS)
431 PULSEAUDIO_pa_stream_drop(h->
stream);
436 WaitForPulseOperation(h->
mainloop, PULSEAUDIO_pa_stream_flush(h->
stream, stream_operation_complete_no_op,
NULL));
440 PULSEAUDIO_CloseDevice(
_THIS)
442 if (this->hidden->stream) {
443 if (this->hidden->capturebuf !=
NULL) {
444 PULSEAUDIO_pa_stream_drop(this->hidden->stream);
446 PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
447 PULSEAUDIO_pa_stream_unref(this->hidden->stream);
450 DisconnectFromPulseServer(this->hidden->mainloop, this->hidden->context);
452 SDL_free(this->hidden->device_name);
457 SinkDeviceNameCallback(pa_context *
c,
const pa_sink_info *
i,
int is_last,
void *data)
460 char **devname = (
char **) data;
466 SourceDeviceNameCallback(pa_context *c,
const pa_source_info *i,
int is_last,
void *data)
469 char **devname = (
char **) data;
479 if (handle ==
NULL) {
485 PULSEAUDIO_pa_context_get_source_info_by_index(h->
context, idx,
489 PULSEAUDIO_pa_context_get_sink_info_by_index(h->
context, idx,
497 PULSEAUDIO_OpenDevice(
_THIS,
void *handle,
const char *devname,
int iscapture)
501 pa_sample_spec paspec;
502 pa_buffer_attr paattr;
503 pa_channel_map pacmap;
504 pa_stream_flags_t
flags = 0;
511 if (this->hidden ==
NULL) {
516 paspec.format = PA_SAMPLE_INVALID;
520 (paspec.format == PA_SAMPLE_INVALID) && test_format;) {
522 fprintf(stderr,
"Trying format 0x%4.4x\n", test_format);
524 switch (test_format) {
526 paspec.format = PA_SAMPLE_U8;
529 paspec.format = PA_SAMPLE_S16LE;
532 paspec.format = PA_SAMPLE_S16BE;
535 paspec.format = PA_SAMPLE_S32LE;
538 paspec.format = PA_SAMPLE_S32BE;
541 paspec.format = PA_SAMPLE_FLOAT32LE;
544 paspec.format = PA_SAMPLE_FLOAT32BE;
547 paspec.format = PA_SAMPLE_INVALID;
550 if (paspec.format == PA_SAMPLE_INVALID) {
554 if (paspec.format == PA_SAMPLE_INVALID) {
555 return SDL_SetError(
"Couldn't find any hardware audio formats");
560 #ifdef PA_STREAM_ADJUST_LATENCY
579 #ifdef PA_STREAM_ADJUST_LATENCY
581 paattr.tlength = h->
mixlen * 4;
583 paattr.maxlength = -1;
585 paattr.minreq = h->
mixlen;
586 flags = PA_STREAM_ADJUST_LATENCY;
588 paattr.tlength = h->
mixlen*2;
589 paattr.prebuf = h->
mixlen*2;
590 paattr.maxlength = h->
mixlen*2;
591 paattr.minreq = h->
mixlen;
595 return SDL_SetError(
"Could not connect to PulseAudio server");
598 if (!FindDeviceName(h, iscapture, handle)) {
599 return SDL_SetError(
"Requested PulseAudio sink/source missing?");
604 PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->
spec.
channels,
605 PA_CHANNEL_MAP_WAVEEX);
607 h->
stream = PULSEAUDIO_pa_stream_new(
609 "Simple DirectMedia Layer",
615 return SDL_SetError(
"Could not set up PulseAudio stream");
621 flags |= PA_STREAM_DONT_MOVE;
625 rc = PULSEAUDIO_pa_stream_connect_record(h->
stream, h->
device_name, &paattr, flags);
631 return SDL_SetError(
"Could not connect PulseAudio stream");
635 if (PULSEAUDIO_pa_mainloop_iterate(h->
mainloop, 1,
NULL) < 0) {
638 state = PULSEAUDIO_pa_stream_get_state(h->
stream);
639 if (!PA_STREAM_IS_GOOD(state)) {
640 return SDL_SetError(
"Could not connect PulseAudio stream");
642 }
while (state != PA_STREAM_READY);
648 static pa_mainloop *hotplug_mainloop =
NULL;
649 static pa_context *hotplug_context =
NULL;
656 SinkInfoCallback(pa_context *c,
const pa_sink_info *i,
int is_last,
void *data)
665 SourceInfoCallback(pa_context *c,
const pa_source_info *i,
int is_last,
void *data)
669 if (i->monitor_of_sink == PA_INVALID_INDEX) {
677 HotplugCallback(pa_context *c, pa_subscription_event_type_t
t,
uint32_t idx,
void *data)
679 const SDL_bool added = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW);
680 const SDL_bool removed = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE);
682 if (added || removed) {
683 const SDL_bool sink = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK);
684 const SDL_bool source = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
688 PULSEAUDIO_pa_context_get_sink_info_by_index(hotplug_context, idx, SinkInfoCallback,
NULL);
689 }
else if (added && source) {
690 PULSEAUDIO_pa_context_get_source_info_by_index(hotplug_context, idx, SourceInfoCallback,
NULL);
691 }
else if (removed && (sink || source)) {
700 HotplugThread(
void *data)
704 PULSEAUDIO_pa_context_set_subscribe_callback(hotplug_context, HotplugCallback,
NULL);
705 o = PULSEAUDIO_pa_context_subscribe(hotplug_context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE,
NULL,
NULL);
706 PULSEAUDIO_pa_operation_unref(o);
707 PULSEAUDIO_pa_mainloop_run(hotplug_mainloop,
NULL);
712 PULSEAUDIO_DetectDevices()
714 WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_sink_info_list(hotplug_context, SinkInfoCallback,
NULL));
715 WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_source_info_list(hotplug_context, SourceInfoCallback,
NULL));
722 PULSEAUDIO_Deinitialize(
void)
724 if (hotplug_thread) {
725 PULSEAUDIO_pa_mainloop_quit(hotplug_mainloop, 0);
727 hotplug_thread =
NULL;
730 DisconnectFromPulseServer(hotplug_mainloop, hotplug_context);
731 hotplug_mainloop =
NULL;
732 hotplug_context =
NULL;
734 UnloadPulseAudioLibrary();
740 if (LoadPulseAudioLibrary() < 0) {
744 if (ConnectToPulseServer(&hotplug_mainloop, &hotplug_context) < 0) {
745 UnloadPulseAudioLibrary();
766 "pulseaudio",
"PulseAudio", PULSEAUDIO_Init, 0
void(* CloseDevice)(_THIS)
GLsizei GLenum GLboolean sink
SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format)
void(* WaitDevice)(_THIS)
GLint GLint GLint GLint GLint x
LPDIRECTSOUNDCAPTUREBUFFER capturebuf
GLfloat GLfloat GLfloat GLfloat h
Uint8 *(* GetDeviceBuf)(_THIS)
void(* DetectDevices)(void)
static screen_context_t context
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
SDL_AudioFormat SDL_NextAudioFormat(void)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
void(* PlayDevice)(_THIS)
SDL_Thread * SDL_CreateThreadInternal(int(*fn)(void *), const char *name, const size_t stacksize, void *data)
EGLImageKHR EGLint EGLint * handle
void SDL_RemoveAudioDevice(const int iscapture, void *handle)
int(* CaptureFromDevice)(_THIS, void *buffer, int buflen)
GLsizei GLsizei GLchar * source
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
void(* Deinitialize)(void)
GLenum GLenum GLsizei const GLuint GLboolean enabled
#define SDL_assert(condition)
#define SDL_OutOfMemory()
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
AudioBootStrap PULSEAUDIO_bootstrap
void * SDL_LoadFunction(void *handle, const char *name)
void(* FlushCapture)(_THIS)
#define SDL_SetThreadPriority
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)