testaudiohotplug.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Program to test hotplugging of audio devices */
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #include <SDL3/SDL_test.h>
  14. #include "testutils.h"
  15. #ifdef SDL_PLATFORM_EMSCRIPTEN
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <stdlib.h>
  19. #ifdef HAVE_SIGNAL_H
  20. #include <signal.h>
  21. #endif
  22. static SDL_AudioSpec spec;
  23. static Uint8 *sound = NULL; /* Pointer to wave data */
  24. static Uint32 soundlen = 0; /* Length of wave data */
  25. static SDLTest_CommonState *state;
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDL_Quit();
  31. SDLTest_CommonDestroyState(state);
  32. /* Let 'main()' return normally */
  33. if (rc != 0) {
  34. exit(rc);
  35. }
  36. }
  37. static int done = 0;
  38. static void poked(int sig)
  39. {
  40. done = 1;
  41. }
  42. static const char *devtypestr(int recording)
  43. {
  44. return recording ? "recording" : "playback";
  45. }
  46. static void iteration(void)
  47. {
  48. SDL_Event e;
  49. SDL_AudioDeviceID dev;
  50. while (SDL_PollEvent(&e)) {
  51. if (e.type == SDL_EVENT_QUIT) {
  52. done = 1;
  53. } else if (e.type == SDL_EVENT_KEY_UP) {
  54. if (e.key.key == SDLK_ESCAPE) {
  55. done = 1;
  56. }
  57. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_ADDED) {
  58. const SDL_AudioDeviceID which = (SDL_AudioDeviceID) e.adevice.which;
  59. const bool recording = e.adevice.recording ? true : false;
  60. const char *name = SDL_GetAudioDeviceName(which);
  61. if (name) {
  62. SDL_Log("New %s audio device at id %u: %s", devtypestr(recording), (unsigned int)which, name);
  63. } else {
  64. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device, id %u, but failed to get the name: %s",
  65. devtypestr(recording), (unsigned int)which, SDL_GetError());
  66. continue;
  67. }
  68. if (!recording) {
  69. SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(which, &spec, NULL, NULL);
  70. if (!stream) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create/bind an audio stream to %u ('%s'): %s", (unsigned int) which, name, SDL_GetError());
  72. } else {
  73. SDL_Log("Opened '%s' as %u\n", name, (unsigned int) which);
  74. /* !!! FIXME: laziness, this used to loop the audio, but we'll just play it once for now on each connect. */
  75. SDL_PutAudioStreamData(stream, sound, soundlen);
  76. SDL_FlushAudioStream(stream);
  77. SDL_ResumeAudioStreamDevice(stream);
  78. /* !!! FIXME: this is leaking the stream for now. We'll wire it up to a dictionary or whatever later. */
  79. }
  80. }
  81. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_REMOVED) {
  82. dev = (SDL_AudioDeviceID)e.adevice.which;
  83. SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.recording), (unsigned int)dev);
  84. /* !!! FIXME: we need to keep track of our streams and destroy them here. */
  85. }
  86. }
  87. }
  88. #ifdef SDL_PLATFORM_EMSCRIPTEN
  89. static void loop(void)
  90. {
  91. if (done)
  92. emscripten_cancel_main_loop();
  93. else
  94. iteration();
  95. }
  96. #endif
  97. int main(int argc, char *argv[])
  98. {
  99. int i;
  100. char *filename = NULL;
  101. SDL_Window *window;
  102. /* Initialize test framework */
  103. state = SDLTest_CommonCreateState(argv, 0);
  104. if (!state) {
  105. return 1;
  106. }
  107. /* Parse commandline */
  108. for (i = 1; i < argc;) {
  109. int consumed;
  110. consumed = SDLTest_CommonArg(state, i);
  111. if (!consumed) {
  112. if (!filename) {
  113. filename = argv[i];
  114. consumed = 1;
  115. }
  116. }
  117. if (consumed <= 0) {
  118. static const char *options[] = { "[sample.wav]", NULL };
  119. SDLTest_CommonLogUsage(state, argv[0], options);
  120. exit(1);
  121. }
  122. i += consumed;
  123. }
  124. /* Load the SDL library */
  125. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  127. return 1;
  128. }
  129. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  130. window = SDL_CreateWindow("testaudiohotplug", 640, 480, 0);
  131. if (!window) {
  132. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow failed: %s\n", SDL_GetError());
  133. quit(1);
  134. }
  135. SDL_MinimizeWindow(window);
  136. filename = GetResourceFilename(filename, "sample.wav");
  137. if (!filename) {
  138. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  139. quit(1);
  140. }
  141. /* Load the wave file into memory */
  142. if (!SDL_LoadWAV(filename, &spec, &sound, &soundlen)) {
  143. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  144. quit(1);
  145. }
  146. #ifdef HAVE_SIGNAL_H
  147. /* Set the signals */
  148. #ifdef SIGHUP
  149. (void)signal(SIGHUP, poked);
  150. #endif
  151. (void)signal(SIGINT, poked);
  152. #ifdef SIGQUIT
  153. (void)signal(SIGQUIT, poked);
  154. #endif
  155. (void)signal(SIGTERM, poked);
  156. #endif /* HAVE_SIGNAL_H */
  157. /* Show the list of available drivers */
  158. SDL_Log("Available audio drivers:");
  159. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  160. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  161. }
  162. SDL_Log("Select a driver with the SDL_AUDIO_DRIVER environment variable.\n");
  163. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  164. #ifdef SDL_PLATFORM_EMSCRIPTEN
  165. emscripten_set_main_loop(loop, 0, 1);
  166. #else
  167. while (!done) {
  168. SDL_Delay(100);
  169. iteration();
  170. }
  171. #endif
  172. /* Clean up on signal */
  173. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  174. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  175. SDL_free(sound);
  176. SDL_free(filename);
  177. quit(0);
  178. return 0;
  179. }