Added support to loopwave for hotplugging audio devices
authorSam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2017 14:50:23 -0800
changeset 1093298b94b9e3e8c
parent 10931 a364af650bf1
child 10933 a9ce2bbd76e1
Added support to loopwave for hotplugging audio devices
test/loopwave.c
     1.1 --- a/test/loopwave.c	Mon Mar 06 21:25:06 2017 -0800
     1.2 +++ b/test/loopwave.c	Thu Mar 09 14:50:23 2017 -0800
     1.3 @@ -38,6 +38,7 @@
     1.4      int soundpos;               /* Current play position */
     1.5  } wave;
     1.6  
     1.7 +static SDL_AudioDeviceID device;
     1.8  
     1.9  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.10  static void
    1.11 @@ -47,6 +48,37 @@
    1.12      exit(rc);
    1.13  }
    1.14  
    1.15 +static void
    1.16 +close_audio()
    1.17 +{
    1.18 +	if (device != 0) {
    1.19 +		SDL_CloseAudioDevice(device);
    1.20 +		device = 0;
    1.21 +	}
    1.22 +}
    1.23 +
    1.24 +static void
    1.25 +open_audio()
    1.26 +{
    1.27 +	/* Initialize fillerup() variables */
    1.28 +	device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
    1.29 +	if (!device) {
    1.30 +		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    1.31 +		SDL_FreeWAV(wave.sound);
    1.32 +		quit(2);
    1.33 +	}
    1.34 +
    1.35 +
    1.36 +	/* Let the audio run */
    1.37 +	SDL_PauseAudioDevice(device, SDL_FALSE);
    1.38 +}
    1.39 +
    1.40 +static void reopen_audio()
    1.41 +{
    1.42 +	close_audio();
    1.43 +	open_audio();
    1.44 +}
    1.45 +
    1.46  
    1.47  void SDLCALL
    1.48  fillerup(void *unused, Uint8 * stream, int len)
    1.49 @@ -82,7 +114,7 @@
    1.50  void
    1.51  loop()
    1.52  {
    1.53 -    if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
    1.54 +    if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
    1.55          emscripten_cancel_main_loop();
    1.56  }
    1.57  #endif
    1.58 @@ -97,7 +129,7 @@
    1.59      SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    1.60  
    1.61      /* Load the SDL library */
    1.62 -    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    1.63 +    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
    1.64          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.65          return (1);
    1.66      }
    1.67 @@ -114,45 +146,40 @@
    1.68      }
    1.69  
    1.70      wave.spec.callback = fillerup;
    1.71 -#if HAVE_SIGNAL_H
    1.72 -    /* Set the signals */
    1.73 -#ifdef SIGHUP
    1.74 -    signal(SIGHUP, poked);
    1.75 -#endif
    1.76 -    signal(SIGINT, poked);
    1.77 -#ifdef SIGQUIT
    1.78 -    signal(SIGQUIT, poked);
    1.79 -#endif
    1.80 -    signal(SIGTERM, poked);
    1.81 -#endif /* HAVE_SIGNAL_H */
    1.82  
    1.83      /* Show the list of available drivers */
    1.84      SDL_Log("Available audio drivers:");
    1.85      for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
    1.86          SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
    1.87 -    }
    1.88 +	}
    1.89 +
    1.90 +	SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
    1.91  
    1.92 -    /* Initialize fillerup() variables */
    1.93 -    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
    1.94 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    1.95 -        SDL_FreeWAV(wave.sound);
    1.96 -        quit(2);
    1.97 -    }
    1.98 +	open_audio();
    1.99  
   1.100 -    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.101 -
   1.102 -    /* Let the audio run */
   1.103 -    SDL_PauseAudio(0);
   1.104 +	SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
   1.105  
   1.106  #ifdef __EMSCRIPTEN__
   1.107      emscripten_set_main_loop(loop, 0, 1);
   1.108  #else
   1.109 -    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.110 -        SDL_Delay(1000);
   1.111 +	while (!done) {
   1.112 +		SDL_Event event;
   1.113 +
   1.114 +		while (SDL_PollEvent(&event) > 0) {
   1.115 +			if (event.type == SDL_QUIT) {
   1.116 +				done = 1;
   1.117 +			}
   1.118 +			if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
   1.119 +				(event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
   1.120 +				reopen_audio();
   1.121 +			}
   1.122 +		}
   1.123 +		SDL_Delay(100);
   1.124 +	}
   1.125  #endif
   1.126  
   1.127      /* Clean up on signal */
   1.128 -    SDL_CloseAudio();
   1.129 +	close_audio();
   1.130      SDL_FreeWAV(wave.sound);
   1.131      SDL_Quit();
   1.132      return (0);