Skip to content

Commit

Permalink
Get the default WASAPI device on UWP
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Oct 1, 2023
1 parent 014fb03 commit cd09bd9
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions alc/backends/wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ using std::chrono::nanoseconds;
using std::chrono::milliseconds;
using std::chrono::seconds;

using ReferenceTime = std::chrono::duration<REFERENCE_TIME,std::ratio<1,10000000>>;
using ReferenceTime = std::chrono::duration<REFERENCE_TIME,std::ratio<1,10'000'000>>;

inline constexpr ReferenceTime operator "" _reftime(unsigned long long int n) noexcept
constexpr ReferenceTime operator "" _reftime(unsigned long long int n) noexcept
{ return ReferenceTime{static_cast<REFERENCE_TIME>(n)}; }


Expand Down Expand Up @@ -185,7 +185,7 @@ overloaded(Ts...) -> overloaded<Ts...>;


template<typename T>
auto as_unsigned(T value) noexcept
constexpr auto as_unsigned(T value) noexcept
{
using UT = std::make_unsigned_t<T>;
return static_cast<UT>(value);
Expand Down Expand Up @@ -248,9 +248,9 @@ struct DevMap {
};
DevMap::~DevMap() = default;

bool checkName(const al::span<DevMap> list, const std::string &name)
bool checkName(const al::span<DevMap> list, const std::string_view name)
{
auto match_name = [&name](const DevMap &entry) -> bool { return entry.name == name; };
auto match_name = [name](const DevMap &entry) -> bool { return entry.name == name; };
return std::find_if(list.cbegin(), list.cend(), match_name) != list.cend();
}

Expand Down Expand Up @@ -309,7 +309,7 @@ static NameGUIDPair GetDeviceNameAndGuid(const DeviceHandle &device)
std::string name, guid;

ComPtr<IPropertyStore> ps;
HRESULT hr = device->OpenPropertyStore(STGM_READ, al::out_ptr(ps));
HRESULT hr{device->OpenPropertyStore(STGM_READ, al::out_ptr(ps))};
if(FAILED(hr))
{
WARN("OpenPropertyStore failed: 0x%08lx\n", hr);
Expand All @@ -319,32 +319,20 @@ static NameGUIDPair GetDeviceNameAndGuid(const DeviceHandle &device)
PropVariant pvprop;
hr = ps->GetValue(al::bit_cast<PROPERTYKEY>(DEVPKEY_Device_FriendlyName), pvprop.get());
if(FAILED(hr))
{
WARN("GetValue Device_FriendlyName failed: 0x%08lx\n", hr);
name = UnknownName;
}
else if(pvprop->vt == VT_LPWSTR)
name = wstr_to_utf8(pvprop->pwszVal);
else
{
WARN("Unexpected Device_FriendlyName PROPVARIANT type: 0x%04x\n", pvprop->vt);
name = UnknownName;
}

pvprop.clear();
hr = ps->GetValue(al::bit_cast<PROPERTYKEY>(PKEY_AudioEndpoint_GUID), pvprop.get());
if(FAILED(hr))
{
WARN("GetValue AudioEndpoint_GUID failed: 0x%08lx\n", hr);
guid = UnknownGuid;
}
else if(pvprop->vt == VT_LPWSTR)
guid = wstr_to_utf8(pvprop->pwszVal);
else
{
WARN("Unexpected AudioEndpoint_GUID PROPVARIANT type: 0x%04x\n", pvprop->vt);
guid = UnknownGuid;
}
#else
std::string name{wstr_to_utf8(device.Name())};
std::string guid;
Expand All @@ -361,9 +349,9 @@ static NameGUIDPair GetDeviceNameAndGuid(const DeviceHandle &device)
[](char ch) { return static_cast<char>(std::toupper(ch)); });
}
}
#endif
if(name.empty()) name = UnknownName;
if(guid.empty()) guid = UnknownGuid;
#endif
return std::make_pair(std::move(name), std::move(guid));
}
#if !defined(ALSOFT_UWP)
Expand Down Expand Up @@ -722,12 +710,13 @@ struct DeviceHelper final : private IMMNotificationClient
const auto deviceRole = Windows::Media::Devices::AudioDeviceRole::Default;
auto DefaultAudioId = flowdir == eRender ? MediaDevice::GetDefaultAudioRenderId(deviceRole)
: MediaDevice::GetDefaultAudioCaptureId(deviceRole);
if (DefaultAudioId.empty())
return defaultId;

auto deviceInfo = DeviceInformation::CreateFromIdAsync(DefaultAudioId, nullptr, DeviceInformationKind::DeviceInterface).get();
if(!deviceInfo)
return defaultId;
if(!DefaultAudioId.empty())
{
auto deviceInfo = DeviceInformation::CreateFromIdAsync(DefaultAudioId, nullptr,
DeviceInformationKind::DeviceInterface).get();
if(deviceInfo)
defaultId = deviceInfo.Id().data();
}

// Get the string identifier of the audio renderer
auto AudioSelector = flowdir == eRender ? MediaDevice::GetAudioRenderSelector() : MediaDevice::GetAudioCaptureSelector();
Expand Down

0 comments on commit cd09bd9

Please sign in to comment.