mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

[WASAPI] ma_device_start does not return when ma_device_start__wasapi failed #399

Closed anhnht3 closed 2 years ago

anhnht3 commented 2 years ago

We faced a critical bug that happened occasionally when user plug in a microphone device and we need to switch to that device.

In those cases, ma_device_init were ok, but ma_device_start never returned, . Log callback showed "[WASAPI] Failed to start internal capture device."

Our lead engineer found that:

  1. ma_device_start sends pDevice->wakeupEvent and then wait for pDevice->startEvent
  2. ma_worker_thread calls ma_device_start__wasapi via callbacks.onDeviceStart
  3. if ma_device_start__wasapi failed, ma_worker_thread will store the result to pDevice->workResult loop back to the start and wait for pDevice->wakeupEvent again.

In this case, ma_device_start will wait forever. We temporarily patched ma_worker_thread to notify pDevice->startEvent so that ma_device_start will return when internal start function fails.

      if (startResult != MA_SUCCESS) {
          pDevice->workResult = startResult;
          ma_event_signal(&pDevice->startEvent);
          continue;   /* Failed to start. Loop back to the start and wait for something to happen (pDevice->wakeupEvent). */
      }

Please let me know if what we are doing are correct or not.

Thank you very much for miniaudio.

Best,

mackron commented 2 years ago

Good catch! Your proposed fix is spot on. I've pushed the same fix to the dev branch and will be released soon. Thanks for reporting.

mackron commented 2 years ago

This has been released. Thanks for the report. Closing.