PulseAudio: Added multiple device support, other cleanups. Thanks to Dominik Frizel for most of the effort on this! Fixes Bugzilla #2730.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index c3e1238..30bc228 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -26,6 +26,7 @@
Stéphan Kochen: stephan .a.t. kochen.nl
*/
#include "../../SDL_internal.h"
+#include "SDL_assert.h"
#if SDL_AUDIO_DRIVER_PULSEAUDIO
@@ -38,7 +39,6 @@
#include <sys/types.h>
#include <errno.h>
#include <pulse/pulseaudio.h>
-#include <pulse/simple.h>
#include "SDL_timer.h"
#include "SDL_audio.h"
@@ -66,10 +66,6 @@ static SDL_INLINE int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
static const char *(*PULSEAUDIO_pa_get_library_version) (void);
-static pa_simple *(*PULSEAUDIO_pa_simple_new) (const char *, const char *,
- pa_stream_direction_t, const char *, const char *, const pa_sample_spec *,
- const pa_channel_map *, const pa_buffer_attr *, int *);
-static void (*PULSEAUDIO_pa_simple_free) (pa_simple *);
static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
pa_channel_map *, unsigned, pa_channel_map_def_t);
static const char * (*PULSEAUDIO_pa_strerror) (int);
@@ -87,6 +83,7 @@ static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
const char *);
static int (*PULSEAUDIO_pa_context_connect) (pa_context *, const char *,
pa_context_flags_t, const pa_spawn_api *);
+static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_list)(pa_context *, pa_sink_info_cb_t, void *);
static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
@@ -179,8 +176,6 @@ static int
load_pulseaudio_syms(void)
{
SDL_PULSEAUDIO_SYM(pa_get_library_version);
- SDL_PULSEAUDIO_SYM(pa_simple_new);
- SDL_PULSEAUDIO_SYM(pa_simple_free);
SDL_PULSEAUDIO_SYM(pa_mainloop_new);
SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
@@ -190,6 +185,7 @@ load_pulseaudio_syms(void)
SDL_PULSEAUDIO_SYM(pa_operation_unref);
SDL_PULSEAUDIO_SYM(pa_context_new);
SDL_PULSEAUDIO_SYM(pa_context_connect);
+ SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_list);
SDL_PULSEAUDIO_SYM(pa_context_get_state);
SDL_PULSEAUDIO_SYM(pa_context_disconnect);
SDL_PULSEAUDIO_SYM(pa_context_unref);
@@ -206,28 +202,96 @@ load_pulseaudio_syms(void)
return 0;
}
+static SDL_INLINE int
+squashVersion(const int major, const int minor, const int patch)
+{
+ return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
+}
+
+/* Workaround for older pulse: pa_context_new() must have non-NULL appname */
+static const char *
+getAppName(void)
+{
+ const char *verstr = PULSEAUDIO_pa_get_library_version();
+ if (verstr != NULL) {
+ int maj, min, patch;
+ if (SDL_sscanf(verstr, "%d.%d.%d", &maj, &min, &patch) == 3) {
+ if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
+ return NULL; /* 0.9.15+ handles NULL correctly. */
+ }
+ }
+ }
+ return "SDL Application"; /* oh well. */
+}
-/* Check to see if we can connect to PulseAudio */
-static SDL_bool
-CheckPulseAudioAvailable()
+static void
+DisconnectFromPulseServer(pa_mainloop *mainloop, pa_context *context)
{
- pa_simple *s;
- pa_sample_spec ss;
-
- ss.format = PA_SAMPLE_S16NE;
- ss.channels = 1;
- ss.rate = 22050;
-
- s = PULSEAUDIO_pa_simple_new(NULL, "SDL", PA_STREAM_PLAYBACK, NULL,
- "Test", &ss, NULL, NULL, NULL);
- if (s) {
- PULSEAUDIO_pa_simple_free(s);
- return SDL_TRUE;
- } else {
- return SDL_FALSE;
+ if (context) {
+ PULSEAUDIO_pa_context_disconnect(context);
+ PULSEAUDIO_pa_context_unref(context);
+ }
+ if (mainloop != NULL) {
+ PULSEAUDIO_pa_mainloop_free(mainloop);
}
}
+static int
+ConnectToPulseServer_Internal(pa_mainloop **_mainloop, pa_context **_context)
+{
+ pa_mainloop *mainloop = NULL;
+ pa_context *context = NULL;
+ pa_mainloop_api *mainloop_api = NULL;
+ int state = 0;
+
+ *_mainloop = NULL;
+ *_context = NULL;
+
+ /* Set up a new main loop */
+ if (!(mainloop = PULSEAUDIO_pa_mainloop_new())) {
+ return SDL_SetError("pa_mainloop_new() failed");
+ }
+
+ *_mainloop = mainloop;
+
+ mainloop_api = PULSEAUDIO_pa_mainloop_get_api(mainloop);
+ SDL_assert(mainloop_api); /* this never fails, right? */
+
+ context = PULSEAUDIO_pa_context_new(mainloop_api, getAppName());
+ if (!context) {
+ return SDL_SetError("pa_context_new() failed");
+ }
+ *_context = context;
+
+ /* Connect to the PulseAudio server */
+ if (PULSEAUDIO_pa_context_connect(context, NULL, 0, NULL) < 0) {
+ return SDL_SetError("Could not setup connection to PulseAudio");
+ }
+
+ do {
+ if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
+ return SDL_SetError("pa_mainloop_iterate() failed");
+ }
+ state = PULSEAUDIO_pa_context_get_state(context);
+ if (!PA_CONTEXT_IS_GOOD(state)) {
+ return SDL_SetError("Could not connect to PulseAudio");
+ }
+ } while (state != PA_CONTEXT_READY);
+
+ return 0; /* connected and ready! */
+}
+
+static int
+ConnectToPulseServer(pa_mainloop **_mainloop, pa_context **_context)
+{
+ const int retval = ConnectToPulseServer_Internal(_mainloop, _context);
+ if (retval < 0) {
+ DisconnectFromPulseServer(*_mainloop, *_context);
+ }
+ return retval;
+}
+
+
/* This function waits until it is possible to write a full sound buffer */
static void
PULSEAUDIO_WaitDevice(_THIS)
@@ -307,43 +371,12 @@ PULSEAUDIO_CloseDevice(_THIS)
PULSEAUDIO_pa_stream_unref(this->hidden->stream);
this->hidden->stream = NULL;
}
- if (this->hidden->context != NULL) {
- PULSEAUDIO_pa_context_disconnect(this->hidden->context);
- PULSEAUDIO_pa_context_unref(this->hidden->context);
- this->hidden->context = NULL;
- }
- if (this->hidden->mainloop != NULL) {
- PULSEAUDIO_pa_mainloop_free(this->hidden->mainloop);
- this->hidden->mainloop = NULL;
- }
+ DisconnectFromPulseServer(this->hidden->mainloop, this->hidden->context);
SDL_free(this->hidden);
this->hidden = NULL;
}
}
-
-static SDL_INLINE int
-squashVersion(const int major, const int minor, const int patch)
-{
- return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF);
-}
-
-/* Workaround for older pulse: pa_context_new() must have non-NULL appname */
-static const char *
-getAppName(void)
-{
- const char *verstr = PULSEAUDIO_pa_get_library_version();
- if (verstr != NULL) {
- int maj, min, patch;
- if (SDL_sscanf(verstr, "%d.%d.%d", &maj, &min, &patch) == 3) {
- if (squashVersion(maj, min, patch) >= squashVersion(0, 9, 15)) {
- return NULL; /* 0.9.15+ handles NULL correctly. */
- }
- }
- }
- return "SDL Application"; /* oh well. */
-}
-
static int
PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
{
@@ -442,42 +475,16 @@ PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
paattr.minreq = h->mixlen;
#endif
+ if (ConnectToPulseServer(&h->mainloop, &h->context) < 0) {
+ PULSEAUDIO_CloseDevice(this);
+ return SDL_SetError("Could not connect to PulseAudio server");
+ }
+
/* The SDL ALSA output hints us that we use Windows' channel mapping */
/* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels,
PA_CHANNEL_MAP_WAVEEX);
- /* Set up a new main loop */
- if (!(h->mainloop = PULSEAUDIO_pa_mainloop_new())) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("pa_mainloop_new() failed");
- }
-
- h->mainloop_api = PULSEAUDIO_pa_mainloop_get_api(h->mainloop);
- h->context = PULSEAUDIO_pa_context_new(h->mainloop_api, getAppName());
- if (!h->context) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("pa_context_new() failed");
- }
-
- /* Connect to the PulseAudio server */
- if (PULSEAUDIO_pa_context_connect(h->context, NULL, 0, NULL) < 0) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("Could not setup connection to PulseAudio");
- }
-
- do {
- if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("pa_mainloop_iterate() failed");
- }
- state = PULSEAUDIO_pa_context_get_state(h->context);
- if (!PA_CONTEXT_IS_GOOD(state)) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("Could not connect to PulseAudio");
- }
- } while (state != PA_CONTEXT_READY);
-
h->stream = PULSEAUDIO_pa_stream_new(
h->context,
"Simple DirectMedia Layer", /* stream description */
@@ -490,7 +497,7 @@ PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
return SDL_SetError("Could not set up PulseAudio stream");
}
- if (PULSEAUDIO_pa_stream_connect_playback(h->stream, NULL, &paattr, flags,
+ if (PULSEAUDIO_pa_stream_connect_playback(h->stream, devname, &paattr, flags,
NULL, NULL) < 0) {
PULSEAUDIO_CloseDevice(this);
return SDL_SetError("Could not connect PulseAudio stream");
@@ -512,6 +519,50 @@ PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
return 0;
}
+typedef struct
+{
+ uint8_t last;
+ SDL_AddAudioDevice addfn;
+} sink_struct;
+
+static void
+get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata)
+{
+ sink_struct *a = (sink_struct *) userdata;
+ a->last = is_last;
+ if (i) {
+ a->addfn(i->name);
+ }
+}
+
+static void
+PULSEAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
+{
+ pa_mainloop *mainloop = NULL;
+ pa_context *context = NULL;
+
+ if (ConnectToPulseServer(&mainloop, &context) < 0) {
+ return;
+ }
+
+ if (!iscapture) {
+ sink_struct a;
+ a.last = 0;
+ a.addfn = addfn;
+ pa_operation* o = PULSEAUDIO_pa_context_get_sink_info_list(context,
+ get_sink_info_callback, &a);
+ while (!a.last) {
+ if (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_CANCELLED) {
+ break;
+ }
+ if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
+ break;
+ }
+ }
+ }
+
+ DisconnectFromPulseServer(mainloop, context);
+}
static void
PULSEAUDIO_Deinitialize(void)
@@ -522,16 +573,22 @@ PULSEAUDIO_Deinitialize(void)
static int
PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
{
+ pa_mainloop *mainloop = NULL;
+ pa_context *context = NULL;
+
if (LoadPulseAudioLibrary() < 0) {
return 0;
}
- if (!CheckPulseAudioAvailable()) {
+ if (ConnectToPulseServer(&mainloop, &context) < 0) {
UnloadPulseAudioLibrary();
return 0;
}
+ DisconnectFromPulseServer(mainloop, context);
+
/* Set the function pointers */
+ impl->DetectDevices = PULSEAUDIO_DetectDevices;
impl->OpenDevice = PULSEAUDIO_OpenDevice;
impl->PlayDevice = PULSEAUDIO_PlayDevice;
impl->WaitDevice = PULSEAUDIO_WaitDevice;
@@ -539,12 +596,10 @@ PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
impl->CloseDevice = PULSEAUDIO_CloseDevice;
impl->WaitDone = PULSEAUDIO_WaitDone;
impl->Deinitialize = PULSEAUDIO_Deinitialize;
- impl->OnlyHasDefaultOutputDevice = 1;
return 1; /* this audio target is available. */
}
-
AudioBootStrap PULSEAUDIO_bootstrap = {
"pulseaudio", "PulseAudio", PULSEAUDIO_Init, 0
};
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.h b/src/audio/pulseaudio/SDL_pulseaudio.h
index a75409b..d587318 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.h
+++ b/src/audio/pulseaudio/SDL_pulseaudio.h
@@ -34,7 +34,6 @@ struct SDL_PrivateAudioData
{
/* pulseaudio structures */
pa_mainloop *mainloop;
- pa_mainloop_api *mainloop_api;
pa_context *context;
pa_stream *stream;