testaudiohotplug.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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 <stdlib.h>
  12. #ifdef HAVE_SIGNAL_H
  13. #include <signal.h>
  14. #endif
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <SDL3/SDL.h>
  19. #include <SDL3/SDL_main.h>
  20. #include <SDL3/SDL_test.h>
  21. #include "testutils.h"
  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 int posindex = 0;
  26. static Uint32 positions[64];
  27. static SDLTest_CommonState *state;
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. SDL_Quit();
  33. SDLTest_CommonDestroyState(state);
  34. /* Let 'main()' return normally */
  35. if (rc != 0) {
  36. exit(rc);
  37. }
  38. }
  39. static void SDLCALL
  40. fillerup(void *_pos, Uint8 *stream, int len)
  41. {
  42. Uint32 pos = *((Uint32 *)_pos);
  43. Uint8 *waveptr;
  44. int waveleft;
  45. /* Set up the pointers */
  46. waveptr = sound + pos;
  47. waveleft = soundlen - pos;
  48. /* Go! */
  49. while (waveleft <= len) {
  50. SDL_memcpy(stream, waveptr, waveleft);
  51. stream += waveleft;
  52. len -= waveleft;
  53. waveptr = sound;
  54. waveleft = soundlen;
  55. pos = 0;
  56. }
  57. SDL_memcpy(stream, waveptr, len);
  58. pos += len;
  59. *((Uint32 *)_pos) = pos;
  60. }
  61. static int done = 0;
  62. static void poked(int sig)
  63. {
  64. done = 1;
  65. }
  66. static const char *devtypestr(int iscapture)
  67. {
  68. return iscapture ? "capture" : "output";
  69. }
  70. static void iteration(void)
  71. {
  72. SDL_Event e;
  73. SDL_AudioDeviceID dev;
  74. while (SDL_PollEvent(&e)) {
  75. if (e.type == SDL_EVENT_QUIT) {
  76. done = 1;
  77. } else if (e.type == SDL_EVENT_KEY_UP) {
  78. if (e.key.keysym.sym == SDLK_ESCAPE) {
  79. done = 1;
  80. }
  81. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_ADDED) {
  82. int index = e.adevice.which;
  83. int iscapture = e.adevice.iscapture;
  84. const char *name = SDL_GetAudioDeviceName(index, iscapture);
  85. if (name != NULL) {
  86. SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int)index, name);
  87. } else {
  88. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
  89. devtypestr(iscapture), (unsigned int)index, SDL_GetError());
  90. continue;
  91. }
  92. if (!iscapture) {
  93. positions[posindex] = 0;
  94. spec.userdata = &positions[posindex++];
  95. spec.callback = fillerup;
  96. dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
  97. if (!dev) {
  98. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
  99. } else {
  100. SDL_Log("Opened '%s' as %u\n", name, (unsigned int)dev);
  101. SDL_PlayAudioDevice(dev);
  102. }
  103. }
  104. } else if (e.type == SDL_EVENT_AUDIO_DEVICE_REMOVED) {
  105. dev = (SDL_AudioDeviceID)e.adevice.which;
  106. SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int)dev);
  107. SDL_CloseAudioDevice(dev);
  108. }
  109. }
  110. }
  111. #ifdef __EMSCRIPTEN__
  112. static void loop(void)
  113. {
  114. if (done)
  115. emscripten_cancel_main_loop();
  116. else
  117. iteration();
  118. }
  119. #endif
  120. int main(int argc, char *argv[])
  121. {
  122. int i;
  123. char *filename = NULL;
  124. SDL_Window *window;
  125. /* Initialize test framework */
  126. state = SDLTest_CommonCreateState(argv, 0);
  127. if (state == NULL) {
  128. return 1;
  129. }
  130. /* Enable standard application logging */
  131. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  132. /* Parse commandline */
  133. for (i = 1; i < argc;) {
  134. int consumed;
  135. consumed = SDLTest_CommonArg(state, i);
  136. if (!consumed) {
  137. if (!filename) {
  138. filename = argv[i];
  139. consumed = 1;
  140. }
  141. }
  142. if (consumed <= 0) {
  143. static const char *options[] = { "[sample.wav]", NULL };
  144. SDLTest_CommonLogUsage(state, argv[0], options);
  145. exit(1);
  146. }
  147. i += consumed;
  148. }
  149. /* Load the SDL library */
  150. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
  151. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  152. return 1;
  153. }
  154. /* Some targets (Mac CoreAudio) need an event queue for audio hotplug, so make and immediately hide a window. */
  155. window = SDL_CreateWindow("testaudiohotplug", 640, 480, 0);
  156. if (window == NULL) {
  157. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow failed: %s\n", SDL_GetError());
  158. quit(1);
  159. }
  160. SDL_MinimizeWindow(window);
  161. filename = GetResourceFilename(filename, "sample.wav");
  162. if (filename == NULL) {
  163. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  164. quit(1);
  165. }
  166. /* Load the wave file into memory */
  167. if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
  168. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  169. quit(1);
  170. }
  171. #ifdef HAVE_SIGNAL_H
  172. /* Set the signals */
  173. #ifdef SIGHUP
  174. (void)signal(SIGHUP, poked);
  175. #endif
  176. (void)signal(SIGINT, poked);
  177. #ifdef SIGQUIT
  178. (void)signal(SIGQUIT, poked);
  179. #endif
  180. (void)signal(SIGTERM, poked);
  181. #endif /* HAVE_SIGNAL_H */
  182. /* Show the list of available drivers */
  183. SDL_Log("Available audio drivers:");
  184. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  185. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  186. }
  187. SDL_Log("Select a driver with the SDL_AUDIO_DRIVER environment variable.\n");
  188. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  189. #ifdef __EMSCRIPTEN__
  190. emscripten_set_main_loop(loop, 0, 1);
  191. #else
  192. while (!done) {
  193. SDL_Delay(100);
  194. iteration();
  195. }
  196. #endif
  197. /* Clean up on signal */
  198. /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
  199. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  200. SDL_free(sound);
  201. SDL_free(filename);
  202. quit(0);
  203. return 0;
  204. }