PulseAudio: Hotplug support!
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 33b4c6e..adcbdee 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -72,6 +72,8 @@ static const char * (*PULSEAUDIO_pa_strerror) (int);
static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (void);
static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int, int *);
+static int (*PULSEAUDIO_pa_mainloop_run) (pa_mainloop *, int *);
+static void (*PULSEAUDIO_pa_mainloop_quit) (pa_mainloop *, int);
static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
@@ -83,9 +85,13 @@ 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_operation * (*PULSEAUDIO_pa_context_get_source_info_list)(pa_context *, pa_source_info_cb_t, void *);
+static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_list) (pa_context *, pa_sink_info_cb_t, void *);
+static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_list) (pa_context *, pa_source_info_cb_t, void *);
+static pa_operation * (*PULSEAUDIO_pa_context_get_sink_info_by_index) (pa_context *, uint32_t, pa_sink_info_cb_t, void *);
+static pa_operation * (*PULSEAUDIO_pa_context_get_source_info_by_index) (pa_context *, uint32_t, pa_source_info_cb_t, void *);
static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
+static pa_operation * (*PULSEAUDIO_pa_context_subscribe) (pa_context *, pa_subscription_mask_t, pa_context_success_cb_t, void *);
+static void (*PULSEAUDIO_pa_context_set_subscribe_callback) (pa_context *, pa_context_subscribe_cb_t, void *);
static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
@@ -180,6 +186,8 @@ load_pulseaudio_syms(void)
SDL_PULSEAUDIO_SYM(pa_mainloop_new);
SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
+ SDL_PULSEAUDIO_SYM(pa_mainloop_run);
+ SDL_PULSEAUDIO_SYM(pa_mainloop_quit);
SDL_PULSEAUDIO_SYM(pa_mainloop_free);
SDL_PULSEAUDIO_SYM(pa_operation_get_state);
SDL_PULSEAUDIO_SYM(pa_operation_cancel);
@@ -188,7 +196,11 @@ load_pulseaudio_syms(void)
SDL_PULSEAUDIO_SYM(pa_context_connect);
SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_list);
SDL_PULSEAUDIO_SYM(pa_context_get_source_info_list);
+ SDL_PULSEAUDIO_SYM(pa_context_get_sink_info_by_index);
+ SDL_PULSEAUDIO_SYM(pa_context_get_source_info_by_index);
SDL_PULSEAUDIO_SYM(pa_context_get_state);
+ SDL_PULSEAUDIO_SYM(pa_context_subscribe);
+ SDL_PULSEAUDIO_SYM(pa_context_set_subscribe_callback);
SDL_PULSEAUDIO_SYM(pa_context_disconnect);
SDL_PULSEAUDIO_SYM(pa_context_unref);
SDL_PULSEAUDIO_SYM(pa_stream_new);
@@ -227,6 +239,19 @@ getAppName(void)
}
static void
+WaitForPulseOperation(pa_mainloop *mainloop, pa_operation *o)
+{
+ /* This checks for NO errors currently. Either fix that, check results elsewhere, or do things you don't care about. */
+ if (mainloop && o) {
+ SDL_bool okay = SDL_TRUE;
+ while (okay && (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_RUNNING)) {
+ okay = (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) >= 0);
+ }
+ PULSEAUDIO_pa_operation_unref(o);
+ }
+}
+
+static void
DisconnectFromPulseServer(pa_mainloop *mainloop, pa_context *context)
{
if (context) {
@@ -300,7 +325,7 @@ PULSEAUDIO_WaitDevice(_THIS)
{
struct SDL_PrivateAudioData *h = this->hidden;
- while(1) {
+ while (this->enabled) {
if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
@@ -318,9 +343,10 @@ PULSEAUDIO_PlayDevice(_THIS)
{
/* Write the audio data */
struct SDL_PrivateAudioData *h = this->hidden;
- if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL,
- PA_SEEK_RELATIVE) < 0) {
- SDL_OpenedAudioDeviceDisconnected(this);
+ if (this->enabled) {
+ if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL, PA_SEEK_RELATIVE) < 0) {
+ SDL_OpenedAudioDeviceDisconnected(this);
+ }
}
}
@@ -333,24 +359,21 @@ stream_drain_complete(pa_stream *s, int success, void *userdata)
static void
PULSEAUDIO_WaitDone(_THIS)
{
- struct SDL_PrivateAudioData *h = this->hidden;
- pa_operation *o;
-
- o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
- if (!o) {
- return;
- }
-
- while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
- if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
- PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
- PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
- PULSEAUDIO_pa_operation_cancel(o);
- break;
+ if (this->enabled) {
+ struct SDL_PrivateAudioData *h = this->hidden;
+ pa_operation *o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
+ if (o) {
+ while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
+ if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
+ PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
+ PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
+ PULSEAUDIO_pa_operation_cancel(o);
+ break;
+ }
+ }
+ PULSEAUDIO_pa_operation_unref(o);
}
}
-
- PULSEAUDIO_pa_operation_unref(o);
}
@@ -367,11 +390,10 @@ PULSEAUDIO_CloseDevice(_THIS)
{
if (this->hidden != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
- this->hidden->mixbuf = NULL;
+ SDL_free(this->hidden->device_name);
if (this->hidden->stream) {
PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
PULSEAUDIO_pa_stream_unref(this->hidden->stream);
- this->hidden->stream = NULL;
}
DisconnectFromPulseServer(this->hidden->mainloop, this->hidden->context);
SDL_free(this->hidden);
@@ -379,10 +401,31 @@ PULSEAUDIO_CloseDevice(_THIS)
}
}
+static void
+DeviceNameCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
+{
+ if (i) {
+ char **devname = (char **) data;
+ *devname = SDL_strdup(i->name);
+ }
+}
+
+static SDL_bool
+FindDeviceName(struct SDL_PrivateAudioData *h, void *handle)
+{
+ const uint32_t idx = ((uint32_t) ((size_t) handle)) - 1;
+
+ if (handle == NULL) { /* NULL == default device. */
+ return SDL_TRUE;
+ }
+
+ WaitForPulseOperation(h->mainloop, PULSEAUDIO_pa_context_get_sink_info_by_index(h->context, idx, DeviceNameCallback, &h->device_name));
+ return (h->device_name != NULL);
+}
+
static int
PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
- const char *devstr = (const char *) handle; /* NULL==default in Pulse. */
struct SDL_PrivateAudioData *h = NULL;
Uint16 test_format = 0;
pa_sample_spec paspec;
@@ -483,6 +526,11 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
return SDL_SetError("Could not connect to PulseAudio server");
}
+ if (!FindDeviceName(h, handle)) {
+ PULSEAUDIO_CloseDevice(this);
+ return SDL_SetError("Requested PulseAudio sink missing?");
+ }
+
/* 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,
@@ -500,7 +548,13 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
return SDL_SetError("Could not set up PulseAudio stream");
}
- if (PULSEAUDIO_pa_stream_connect_playback(h->stream, devstr, &paattr, flags,
+ /* now that we have multi-device support, don't move a stream from
+ a device that was unplugged to something else, unless we're default. */
+ if (h->device_name != NULL) {
+ flags |= PA_STREAM_DONT_MOVE;
+ }
+
+ if (PULSEAUDIO_pa_stream_connect_playback(h->stream, h->device_name, &paattr, flags,
NULL, NULL) < 0) {
PULSEAUDIO_CloseDevice(this);
return SDL_SetError("Could not connect PulseAudio stream");
@@ -514,7 +568,7 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
state = PULSEAUDIO_pa_stream_get_state(h->stream);
if (!PA_STREAM_IS_GOOD(state)) {
PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("Could not create to PulseAudio stream");
+ return SDL_SetError("Could not connect PulseAudio stream");
}
} while (state != PA_STREAM_READY);
@@ -522,95 +576,107 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
return 0;
}
+static pa_mainloop *hotplug_mainloop = NULL;
+static pa_context *hotplug_context = NULL;
+static SDL_Thread *hotplug_thread = NULL;
+
+/* device handles are device index + 1, cast to void*, so we never pass a NULL. */
+
+/* This is called when PulseAudio adds an output ("sink") device. */
static void
-get_sinks_cb(pa_context *c, const pa_sink_info *i, int is_last, void *data)
+SinkInfoCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
{
- SDL_bool *done = (SDL_bool *) data;
-
- *done = (is_list != 0);
if (i) {
- char *handle = SDL_strdup(i->name);
- if (handle != NULL) {
- SDL_AddAudioDevice(SDL_FALSE, i->description, handle);
- }
+ SDL_AddAudioDevice(SDL_FALSE, i->description, (void *) ((size_t) i->index+1));
}
}
+/* This is called when PulseAudio adds a capture ("source") device. */
static void
-get_sources_cb(pa_context *c, const pa_sink_info *i, int is_last, void *data)
+SourceInfoCallback(pa_context *c, const pa_source_info *i, int is_last, void *data)
{
- SDL_bool *done = (SDL_bool *) data;
-
- *done = (is_list != 0);
if (i) {
- char *handle = SDL_strdup(i->name);
- if (handle != NULL) {
- SDL_AddAudioDevice(SDL_TRUE, i->description, handle);
+ /* Skip "monitor" sources. These are just output from other sinks. */
+ if (i->monitor_of_sink == PA_INVALID_INDEX) {
+ SDL_AddAudioDevice(SDL_TRUE, i->description, (void *) ((size_t) i->index+1));
}
}
}
+/* This is called when PulseAudio has a device connected/removed/changed. */
static void
-RunPulseDetectCallback(pa_mainloop *mainloop, pa_operation *o, SDL_bool *done)
+HotplugCallback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *data)
{
- do {
- if (PULSEAUDIO_pa_operation_get_state(o) == PA_OPERATION_CANCELLED) {
- break;
- } else if (PULSEAUDIO_pa_mainloop_iterate(mainloop, 1, NULL) < 0) {
- break;
+ const SDL_bool added = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW);
+ const SDL_bool removed = ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE);
+
+ if (added || removed) { /* we only care about add/remove events. */
+ const SDL_bool sink = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK);
+ const SDL_bool source = ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
+
+ /* adds need sink details from the PulseAudio server. Another callback... */
+ if (added && sink) {
+ PULSEAUDIO_pa_context_get_sink_info_by_index(hotplug_context, idx, SinkInfoCallback, NULL);
+ } else if (added && source) {
+ PULSEAUDIO_pa_context_get_source_info_by_index(hotplug_context, idx, SourceInfoCallback, NULL);
+ } else if (removed && (sink || source)) {
+ /* removes we can handle just with the device index. */
+ SDL_RemoveAudioDevice(source != 0, (void *) ((size_t) idx+1));
}
- } while (*done == SDL_FALSE);
+ }
}
-static void
-PULSEAUDIO_DetectDevices()
+/* this runs as a thread while the Pulse target is initialized to catch hotplug events. */
+static int SDLCALL
+HotplugThread(void *data)
{
- pa_mainloop *mainloop = NULL;
- pa_context *context = NULL;
- pa_operation *o = NULL;
- SDL_bool done;
-
- if (ConnectToPulseServer(&mainloop, &context) < 0) {
- return;
- }
-
- done = SDL_FALSE;
- RunPulseDetectCallback(mainloop, PULSEAUDIO_pa_context_get_sink_info_list(context, get_sinks_cb, &done), &done);
- done = SDL_FALSE;
- RunPulseDetectCallback(mainloop, PULSEAUDIO_pa_context_get_source_info_list(context, get_sources_cb, &done), &done);
-
- DisconnectFromPulseServer(mainloop, context);
+ pa_operation *o;
+ SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
+ PULSEAUDIO_pa_context_set_subscribe_callback(hotplug_context, HotplugCallback, NULL);
+ o = PULSEAUDIO_pa_context_subscribe(hotplug_context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, NULL, NULL);
+ PULSEAUDIO_pa_operation_unref(o); /* don't wait for it, just do our thing. */
+ PULSEAUDIO_pa_mainloop_run(hotplug_mainloop, NULL);
+ return 0;
}
static void
-PULSEAUDIO_FreeDeviceHandle(void *handle)
+PULSEAUDIO_DetectDevices()
{
- SDL_free(handle); /* just a string we copied. */
+ WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_sink_info_list(hotplug_context, SinkInfoCallback, NULL));
+ WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_source_info_list(hotplug_context, SourceInfoCallback, NULL));
+
+ /* ok, we have a sane list, let's set up hotplug notifications now... */
+ hotplug_thread = SDL_CreateThread(HotplugThread, "PulseHotplug", NULL);
}
static void
PULSEAUDIO_Deinitialize(void)
{
+ if (hotplug_thread) {
+ PULSEAUDIO_pa_mainloop_quit(hotplug_mainloop, 0);
+ SDL_WaitThread(hotplug_thread, NULL);
+ hotplug_thread = NULL;
+ }
+
+ DisconnectFromPulseServer(hotplug_mainloop, hotplug_context);
+ hotplug_mainloop = NULL;
+ hotplug_context = NULL;
+
UnloadPulseAudioLibrary();
}
static int
PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
{
- pa_mainloop *mainloop = NULL;
- pa_context *context = NULL;
-
if (LoadPulseAudioLibrary() < 0) {
return 0;
}
- if (ConnectToPulseServer(&mainloop, &context) < 0) {
+ if (ConnectToPulseServer(&hotplug_mainloop, &hotplug_context) < 0) {
UnloadPulseAudioLibrary();
return 0;
}
- DisconnectFromPulseServer(mainloop, context);
-
/* Set the function pointers */
impl->DetectDevices = PULSEAUDIO_DetectDevices;
impl->OpenDevice = PULSEAUDIO_OpenDevice;
@@ -619,7 +685,6 @@ PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
impl->GetDeviceBuf = PULSEAUDIO_GetDeviceBuf;
impl->CloseDevice = PULSEAUDIO_CloseDevice;
impl->WaitDone = PULSEAUDIO_WaitDone;
- impl->FreeDeviceHandle = PULSEAUDIO_FreeDeviceHandle;
impl->Deinitialize = PULSEAUDIO_Deinitialize;
return 1; /* this audio target is available. */
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.h b/src/audio/pulseaudio/SDL_pulseaudio.h
index d587318..10568ff 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.h
+++ b/src/audio/pulseaudio/SDL_pulseaudio.h
@@ -32,6 +32,8 @@
struct SDL_PrivateAudioData
{
+ char *device_name;
+
/* pulseaudio structures */
pa_mainloop *mainloop;
pa_context *context;