test/testaudiohotplug.c
changeset 10413 636499ff46f8
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
     1.1 --- a/test/testaudiohotplug.c	Sat Oct 01 12:28:05 2016 -0700
     1.2 +++ b/test/testaudiohotplug.c	Sat Oct 01 12:29:55 2016 -0700
     1.3 @@ -74,6 +74,12 @@
     1.4      done = 1;
     1.5  }
     1.6  
     1.7 +static const char*
     1.8 +devtypestr(int iscapture)
     1.9 +{
    1.10 +    return iscapture ? "capture" : "output";
    1.11 +}
    1.12 +
    1.13  static void
    1.14  iteration()
    1.15  {
    1.16 @@ -82,10 +88,21 @@
    1.17      while (SDL_PollEvent(&e)) {
    1.18          if (e.type == SDL_QUIT) {
    1.19              done = 1;
    1.20 +        } else if (e.type == SDL_KEYUP) {
    1.21 +            if (e.key.keysym.sym == SDLK_ESCAPE)
    1.22 +                done = 1;
    1.23          } else if (e.type == SDL_AUDIODEVICEADDED) {
    1.24 -            const char *name = SDL_GetAudioDeviceName(e.adevice.which, 0);
    1.25 -            SDL_Log("New %s audio device: %s\n", e.adevice.iscapture ? "capture" : "output", name);
    1.26 -            if (!e.adevice.iscapture) {
    1.27 +            int index = e.adevice.which;
    1.28 +            int iscapture = e.adevice.iscapture;
    1.29 +            const char *name = SDL_GetAudioDeviceName(index, iscapture);
    1.30 +            if (name != NULL)
    1.31 +                SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int) index, name);
    1.32 +            else {
    1.33 +                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
    1.34 +                    devtypestr(iscapture), (unsigned int) index, SDL_GetError());
    1.35 +                continue;
    1.36 +            }
    1.37 +            if (!iscapture) {
    1.38                  positions[posindex] = 0;
    1.39                  spec.userdata = &positions[posindex++];
    1.40                  spec.callback = fillerup;
    1.41 @@ -99,7 +116,7 @@
    1.42              }
    1.43          } else if (e.type == SDL_AUDIODEVICEREMOVED) {
    1.44              dev = (SDL_AudioDeviceID) e.adevice.which;
    1.45 -            SDL_Log("%s device %u removed.\n", e.adevice.iscapture ? "capture" : "output", (unsigned int) dev);
    1.46 +            SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int) dev);
    1.47              SDL_CloseAudioDevice(dev);
    1.48          }
    1.49      }
    1.50 @@ -163,6 +180,7 @@
    1.51          SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
    1.52      }
    1.53  
    1.54 +    SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n");
    1.55      SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
    1.56  
    1.57  #ifdef __EMSCRIPTEN__
    1.58 @@ -175,6 +193,8 @@
    1.59  #endif
    1.60  
    1.61      /* Clean up on signal */
    1.62 +    /* Quit audio first, then free WAV. This prevents access violations in the audio threads. */
    1.63 +    SDL_QuitSubSystem(SDL_INIT_AUDIO);
    1.64      SDL_FreeWAV(sound);
    1.65      SDL_Quit();
    1.66      return (0);