alsa: Make hotplug thread optional. Even without the thread, it'll do an initial hardware detection at startup, but there won't be any further hotplug events after that. But for many cases, that is likely complete sufficient. In either case, this cleaned up the code to no longer need a semaphore at startup. Fixes #4862.
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
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index 1ab7299..b9062e2 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -26,6 +26,11 @@
#define SDL_ALSA_NON_BLOCKING 0
#endif
+/* without the thread, you will detect devices on startup, but will not get futher hotplug events. But that might be okay. */
+#ifndef SDL_ALSA_HOTPLUG_THREAD
+#define SDL_ALSA_HOTPLUG_THREAD 1
+#endif
+
/* Allow access to a raw mixing buffer */
#include <sys/types.h>
@@ -802,191 +807,190 @@ add_device(const int iscapture, const char *name, void *hint, ALSA_Device **pSee
}
-static SDL_atomic_t ALSA_hotplug_shutdown;
-static SDL_Thread *ALSA_hotplug_thread;
+static ALSA_Device *hotplug_devices = NULL;
-static int SDLCALL
-ALSA_HotplugThread(void *arg)
+static void
+ALSA_HotplugIteration(void)
{
- SDL_sem *first_run_semaphore = (SDL_sem *) arg;
- ALSA_Device *devices = NULL;
- ALSA_Device *next;
+ void **hints = NULL;
ALSA_Device *dev;
- Uint32 ticks;
+ ALSA_Device *unseen;
+ ALSA_Device *seen;
+ ALSA_Device *next;
+ ALSA_Device *prev;
+
+ if (ALSA_snd_device_name_hint(-1, "pcm", &hints) == 0) {
+ int i, j;
+ const char *match = NULL;
+ int bestmatch = 0xFFFF;
+ size_t match_len = 0;
+ int defaultdev = -1;
+ static const char * const prefixes[] = {
+ "hw:", "sysdefault:", "default:", NULL
+ };
+
+ unseen = hotplug_devices;
+ seen = NULL;
+
+ /* Apparently there are several different ways that ALSA lists
+ actual hardware. It could be prefixed with "hw:" or "default:"
+ or "sysdefault:" and maybe others. Go through the list and see
+ if we can find a preferred prefix for the system. */
+ for (i = 0; hints[i]; i++) {
+ char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
+ if (!name) {
+ continue;
+ }
- SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
+ /* full name, not a prefix */
+ if ((defaultdev == -1) && (SDL_strcmp(name, "default") == 0)) {
+ defaultdev = i;
+ }
- while (!SDL_AtomicGet(&ALSA_hotplug_shutdown)) {
- void **hints = NULL;
- ALSA_Device *unseen;
- ALSA_Device *seen;
- ALSA_Device *prev;
-
- if (ALSA_snd_device_name_hint(-1, "pcm", &hints) == 0) {
- int i, j;
- const char *match = NULL;
- int bestmatch = 0xFFFF;
- size_t match_len = 0;
- int defaultdev = -1;
- static const char * const prefixes[] = {
- "hw:", "sysdefault:", "default:", NULL
- };
-
- unseen = devices;
- seen = NULL;
- /* Apparently there are several different ways that ALSA lists
- actual hardware. It could be prefixed with "hw:" or "default:"
- or "sysdefault:" and maybe others. Go through the list and see
- if we can find a preferred prefix for the system. */
- for (i = 0; hints[i]; i++) {
- char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
- if (!name) {
- continue;
+ for (j = 0; prefixes[j]; j++) {
+ const char *prefix = prefixes[j];
+ const size_t prefixlen = SDL_strlen(prefix);
+ if (SDL_strncmp(name, prefix, prefixlen) == 0) {
+ if (j < bestmatch) {
+ bestmatch = j;
+ match = prefix;
+ match_len = prefixlen;
+ }
}
+ }
- /* full name, not a prefix */
- if ((defaultdev == -1) && (SDL_strcmp(name, "default") == 0)) {
- defaultdev = i;
- }
+ free(name);
+ }
- for (j = 0; prefixes[j]; j++) {
- const char *prefix = prefixes[j];
- const size_t prefixlen = SDL_strlen(prefix);
- if (SDL_strncmp(name, prefix, prefixlen) == 0) {
- if (j < bestmatch) {
- bestmatch = j;
- match = prefix;
- match_len = prefixlen;
- }
- }
- }
+ /* look through the list of device names to find matches */
+ for (i = 0; hints[i]; i++) {
+ char *name;
- free(name);
+ /* if we didn't find a device name prefix we like at all... */
+ if ((!match) && (defaultdev != i)) {
+ continue; /* ...skip anything that isn't the default device. */
}
- /* look through the list of device names to find matches */
- for (i = 0; hints[i]; i++) {
- char *name;
+ name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
+ if (!name) {
+ continue;
+ }
- /* if we didn't find a device name prefix we like at all... */
- if ((!match) && (defaultdev != i)) {
- continue; /* ...skip anything that isn't the default device. */
- }
+ /* only want physical hardware interfaces */
+ if (!match || (SDL_strncmp(name, match, match_len) == 0)) {
+ char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID");
+ const SDL_bool isoutput = (ioid == NULL) || (SDL_strcmp(ioid, "Output") == 0);
+ const SDL_bool isinput = (ioid == NULL) || (SDL_strcmp(ioid, "Input") == 0);
+ SDL_bool have_output = SDL_FALSE;
+ SDL_bool have_input = SDL_FALSE;
+
+ free(ioid);
- name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
- if (!name) {
+ if (!isoutput && !isinput) {
+ free(name);
continue;
}
- /* only want physical hardware interfaces */
- if (!match || (SDL_strncmp(name, match, match_len) == 0)) {
- char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID");
- const SDL_bool isoutput = (ioid == NULL) || (SDL_strcmp(ioid, "Output") == 0);
- const SDL_bool isinput = (ioid == NULL) || (SDL_strcmp(ioid, "Input") == 0);
- SDL_bool have_output = SDL_FALSE;
- SDL_bool have_input = SDL_FALSE;
-
- free(ioid);
-
- if (!isoutput && !isinput) {
- free(name);
- continue;
- }
-
- prev = NULL;
- for (dev = unseen; dev; dev = next) {
- next = dev->next;
- if ( (SDL_strcmp(dev->name, name) == 0) && (((isinput) && dev->iscapture) || ((isoutput) && !dev->iscapture)) ) {
- if (prev) {
- prev->next = next;
- } else {
- unseen = next;
- }
- dev->next = seen;
- seen = dev;
- if (isinput) have_input = SDL_TRUE;
- if (isoutput) have_output = SDL_TRUE;
+ prev = NULL;
+ for (dev = unseen; dev; dev = next) {
+ next = dev->next;
+ if ( (SDL_strcmp(dev->name, name) == 0) && (((isinput) && dev->iscapture) || ((isoutput) && !dev->iscapture)) ) {
+ if (prev) {
+ prev->next = next;
} else {
- prev = dev;
+ unseen = next;
}
- }
-
- if (isinput && !have_input) {
- add_device(SDL_TRUE, name, hints[i], &seen);
- }
- if (isoutput && !have_output) {
- add_device(SDL_FALSE, name, hints[i], &seen);
+ dev->next = seen;
+ seen = dev;
+ if (isinput) have_input = SDL_TRUE;
+ if (isoutput) have_output = SDL_TRUE;
+ } else {
+ prev = dev;
}
}
- free(name);
+ if (isinput && !have_input) {
+ add_device(SDL_TRUE, name, hints[i], &seen);
+ }
+ if (isoutput && !have_output) {
+ add_device(SDL_FALSE, name, hints[i], &seen);
+ }
}
- ALSA_snd_device_name_free_hint(hints);
+ free(name);
+ }
- devices = seen; /* now we have a known-good list of attached devices. */
+ ALSA_snd_device_name_free_hint(hints);
- /* report anything still in unseen as removed. */
- for (dev = unseen; dev; dev = next) {
- /*printf("ALSA: removing usb %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
- next = dev->next;
- SDL_RemoveAudioDevice(dev->iscapture, dev->name);
- SDL_free(dev->name);
- SDL_free(dev);
- }
- }
+ hotplug_devices = seen; /* now we have a known-good list of attached devices. */
- /* On first run, tell ALSA_DetectDevices() that we have a complete device list so it can return. */
- if (first_run_semaphore) {
- SDL_SemPost(first_run_semaphore);
- first_run_semaphore = NULL; /* let other thread clean it up. */
+ /* report anything still in unseen as removed. */
+ for (dev = unseen; dev; dev = next) {
+ /*printf("ALSA: removing usb %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
+ next = dev->next;
+ SDL_RemoveAudioDevice(dev->iscapture, dev->name);
+ SDL_free(dev->name);
+ SDL_free(dev);
}
+ }
+}
+
+#if SDL_ALSA_HOTPLUG_THREAD
+static SDL_atomic_t ALSA_hotplug_shutdown;
+static SDL_Thread *ALSA_hotplug_thread;
+
+static int SDLCALL
+ALSA_HotplugThread(void *arg)
+{
+ SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW);
+ while (!SDL_AtomicGet(&ALSA_hotplug_shutdown)) {
/* Block awhile before checking again, unless we're told to stop. */
- ticks = SDL_GetTicks() + 5000;
+ const Uint32 ticks = SDL_GetTicks() + 5000;
while (!SDL_AtomicGet(&ALSA_hotplug_shutdown) && !SDL_TICKS_PASSED(SDL_GetTicks(), ticks)) {
SDL_Delay(100);
}
- }
- /* Shutting down! Clean up any data we've gathered. */
- for (dev = devices; dev; dev = next) {
- /*printf("ALSA: at shutdown, removing %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
- next = dev->next;
- SDL_free(dev->name);
- SDL_free(dev);
+ ALSA_HotplugIteration(); /* run the check. */
}
return 0;
}
+#endif
static void
ALSA_DetectDevices(void)
{
- /* Start the device detection thread here, wait for an initial iteration to complete. */
- SDL_sem *semaphore = SDL_CreateSemaphore(0);
- if (!semaphore) {
- return; /* oh well. */
- }
+ ALSA_HotplugIteration(); /* run once now before a thread continues to check. */
+#if SDL_ALSA_HOTPLUG_THREAD
SDL_AtomicSet(&ALSA_hotplug_shutdown, 0);
-
- ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", semaphore);
- if (ALSA_hotplug_thread) {
- SDL_SemWait(semaphore); /* wait for the first iteration to finish. */
- }
-
- SDL_DestroySemaphore(semaphore);
+ ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", NULL);
+ /* if the thread doesn't spin, oh well, you just don't get further hotplug events. */
+#endif
}
static void
ALSA_Deinitialize(void)
{
+ ALSA_Device *dev;
+ ALSA_Device *next;
+
+#if SDL_ALSA_HOTPLUG_THREAD
if (ALSA_hotplug_thread != NULL) {
SDL_AtomicSet(&ALSA_hotplug_shutdown, 1);
SDL_WaitThread(ALSA_hotplug_thread, NULL);
ALSA_hotplug_thread = NULL;
}
+#endif
+
+ /* Shutting down! Clean up any data we've gathered. */
+ for (dev = hotplug_devices; dev; dev = next) {
+ /*printf("ALSA: at shutdown, removing %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
+ next = dev->next;
+ SDL_free(dev->name);
+ SDL_free(dev);
+ }
UnloadALSALibrary();
}