output/wasapi: SearchDevice() returns IMMDevice

This commit is contained in:
Max Kellermann 2021-03-05 19:01:45 +01:00
parent 2ff6a9ad2b
commit 2f2b3f1cdc
1 changed files with 11 additions and 10 deletions

View File

@ -261,7 +261,7 @@ private:
void FindSharedFormatSupported(AudioFormat &audio_format); void FindSharedFormatSupported(AudioFormat &audio_format);
void EnumerateDevices(); void EnumerateDevices();
ComPtr<IMMDevice> GetDevice(unsigned int index); ComPtr<IMMDevice> GetDevice(unsigned int index);
unsigned int SearchDevice(std::string_view name); ComPtr<IMMDevice> SearchDevice(std::string_view name);
}; };
WasapiOutput &wasapi_output_downcast(AudioOutput &output) noexcept { WasapiOutput &wasapi_output_downcast(AudioOutput &output) noexcept {
@ -616,13 +616,12 @@ void WasapiOutput::OpenDevice() {
unsigned int id = kErrorId; unsigned int id = kErrorId;
if (!device_config.empty()) { if (!device_config.empty()) {
if (!SafeSilenceTry([this, &id]() { id = std::stoul(device_config); })) { if (!SafeSilenceTry([this, &id]() { id = std::stoul(device_config); })) {
id = SearchDevice(device_config); device = SearchDevice(device_config);
if (id == kErrorId) if (!device)
throw FormatRuntimeError("Device '%s' not found", throw FormatRuntimeError("Device '%s' not found",
device_config.c_str()); device_config.c_str());
} } else
device = GetDevice(id);
device = GetDevice(id);
} else { } else {
device = GetDefaultAudioEndpoint(*enumerator); device = GetDefaultAudioEndpoint(*enumerator);
} }
@ -836,9 +835,11 @@ WasapiOutput::GetDevice(unsigned int index)
} }
/// run inside COMWorkerThread /// run inside COMWorkerThread
unsigned int WasapiOutput::SearchDevice(std::string_view name) { ComPtr<IMMDevice>
WasapiOutput::SearchDevice(std::string_view name)
{
if (!SafeTry([this]() { EnumerateDevices(); })) { if (!SafeTry([this]() { EnumerateDevices(); })) {
return kErrorId; return nullptr;
} }
auto iter = auto iter =
std::find_if(device_desc.cbegin(), device_desc.cend(), std::find_if(device_desc.cbegin(), device_desc.cend(),
@ -846,11 +847,11 @@ unsigned int WasapiOutput::SearchDevice(std::string_view name) {
if (iter == device_desc.cend()) { if (iter == device_desc.cend()) {
FormatError(wasapi_output_domain, "Device %.*s not founded.", FormatError(wasapi_output_domain, "Device %.*s not founded.",
int(name.size()), name.data()); int(name.size()), name.data());
return kErrorId; return nullptr;
} }
FormatInfo(wasapi_output_domain, "Select device \"%u\" \"%s\"", iter->first, FormatInfo(wasapi_output_domain, "Select device \"%u\" \"%s\"", iter->first,
iter->second.c_str()); iter->second.c_str());
return iter->first; return GetDevice(iter->first);
} }
static bool wasapi_output_test_default_device() { return true; } static bool wasapi_output_test_default_device() { return true; }