Demonstrates recovering from a backend disconnecting.
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
__attribute__ ((cold))
__attribute__ ((noreturn))
__attribute__ ((format (printf, 1, 2)))
static void panic(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
abort();
}
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [options]\n"
"Options:\n"
" [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n"
" [--timeout seconds]\n", exe);
return 1;
}
static bool severed = false;
static void on_backend_disconnect(
struct SoundIo *soundio,
int err) {
severed = true;
}
int main(int argc, char **argv) {
char *exe = argv[0];
int timeout = 0;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '-') {
i += 1;
if (i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--timeout") == 0) {
timeout = atoi(argv[i]);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp("-dummy", argv[i]) == 0) {
} else if (strcmp("alsa", argv[i]) == 0) {
} else if (strcmp("pulseaudio", argv[i]) == 0) {
} else if (strcmp("jack", argv[i]) == 0) {
} else if (strcmp("coreaudio", argv[i]) == 0) {
} else if (strcmp("wasapi", argv[i]) == 0) {
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return 1;
}
} else {
return usage(exe);
}
} else {
return usage(exe);
}
}
panic("out of memory");
if (err)
fprintf(stderr, "OK connected to %s. Now cause the backend to disconnect.\n",
while (!severed)
if (timeout > 0) {
fprintf(stderr, "OK sleeping for %d seconds\n", timeout);
sleep(timeout);
}
fprintf(stderr, "OK cleaned up. Reconnecting...\n");
if (err)
fprintf(stderr, "OK test passed\n");
return 0;
}