// SPDX-FileCopyrightText: Copyright 2026 Gloria Goertz, Mireille Meyer // SPDX-License-Identifier: BSD-2-Clause #include #include #include "../../StringTools.hh" namespace atom::Audio { Result> Manager::Create(const std::string& name) { return Implementation::WASAPManager::Create(name); } namespace Implementation { Result> WASAPManager::Create(const std::string& name) { IMMDeviceEnumerator* enumerator = nullptr; const HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), reinterpret_cast(&enumerator)); switch (result) { case S_OK: { break; } case CO_E_NOTINITIALIZED: case REGDB_E_CLASSNOTREG: case E_NOINTERFACE: { return Status::Unavailable; } default: { ATOM_PANIC("CoCreateInstance failed with {}", result); } } return std::unique_ptr(new Implementation::WASAPManager(enumerator, name)); } WASAPManager::~WASAPManager() { m_notificationClient->Release(); m_enumerator->Release(); } Result> WASAPManager::GetAvailableRenderers() { IMMDeviceCollection* collection = nullptr; HRESULT result = m_enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &collection); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IMMDeviceEnumerator::EnumAudioEndpoints failed with {}", result); } } u32 count = 0; result = collection->GetCount(&count); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IMMDeviceCollection::GetCount failed with {}", result); } } if (count == 0) { return Status::Empty; } std::vector infos(count); for (usize i = 0; i < count; i++) { IMMDevice* device = nullptr; result = collection->Item(i, &device); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IMMDeviceCollection::GetCount failed with {}", result); } } LPWSTR id = nullptr; result = device->GetId(&id); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IMMDevice::GetId failed with {}", result); } } IPropertyStore* properties = nullptr; result = device->OpenPropertyStore(STGM_READ, &properties); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IMMDevice::OpenPropertyStore failed with {}", result); } } PROPVARIANT property = { }; result = properties->GetValue(PKEY_Device_FriendlyName, &property); switch (result) { case S_OK: { break; } default: { ATOM_PANIC("IPropertyStore::GetValue failed with {}", result); } } const usize idSize = wcslen(id) * sizeof(wchar_t); infos[i].m_id = std::shared_ptr(new u8[idSize + 1]); std::memcpy(infos[i].m_id.get(), reinterpret_cast(id), idSize); infos[i].m_name = WCHARToStdString(property.pwszVal).Unwrap(); properties->Release(); CoTaskMemFree(id); device->Release(); } collection->Release(); return infos; } } }