Skip to content

Commit

Permalink
Make sure WASAPI device names are probed before opening devices
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Feb 11, 2025
1 parent 6d7bf0d commit 82c2c74
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions alc/backends/wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ struct DeviceListLock : public std::unique_lock<DeviceList> {
};

DeviceList gDeviceList;
std::condition_variable_any gInitCV;
std::atomic<bool> gInitDone{false};


#ifdef AVRTAPI
Expand Down Expand Up @@ -792,22 +794,34 @@ void DeviceEnumHelper::messageHandler(std::promise<HRESULT> *promise)
return;
}

DeviceEnumHelper helper;
auto hr = helper.init();
promise->set_value(hr);
promise = nullptr;
if(FAILED(hr))
return;

{
auto helper = std::optional<DeviceEnumHelper>{};
try {
auto devlock = DeviceListLock{gDeviceList};
auto defaultId = helper.probeDevices(eRender, devlock.getPlaybackList());

auto hr = helper.emplace().init();
promise->set_value(hr);
promise = nullptr;
if(FAILED(hr))
return;

auto defaultId = helper->probeDevices(eRender, devlock.getPlaybackList());
if(!defaultId.empty()) devlock.setPlaybackDefaultId(defaultId);
defaultId = helper.probeDevices(eCapture, devlock.getCaptureList());

defaultId = helper->probeDevices(eCapture, devlock.getCaptureList());
if(!defaultId.empty()) devlock.setCaptureDefaultId(defaultId);

gInitDone.store(true, std::memory_order_relaxed);
}
catch(std::exception &e) {
ERR("Exception probing devices: {}", e.what());
if(promise)
promise->set_value(E_FAIL);
return;
}

TRACE("Watcher thread started");
gInitCV.notify_all();

while(!quit()) {
/* Do nothing. */
}
Expand Down Expand Up @@ -1483,6 +1497,12 @@ try {
return;
}

if(!gInitDone.load(std::memory_order_relaxed))
{
auto devlock = DeviceListLock{gDeviceList};
gInitCV.wait(devlock, []() -> bool { return gInitDone; });
}

auto helper = DeviceHelper{};
if(HRESULT hr{helper.init()}; FAILED(hr))
{
Expand Down Expand Up @@ -2522,6 +2542,12 @@ try {
return;
}

if(!gInitDone.load(std::memory_order_relaxed))
{
auto devlock = DeviceListLock{gDeviceList};
gInitCV.wait(devlock, []() -> bool { return gInitDone; });
}

auto helper = DeviceHelper{};
if(HRESULT hr{helper.init()}; FAILED(hr))
{
Expand Down

0 comments on commit 82c2c74

Please sign in to comment.