HRESULT CMpcAudioRenderer::GetAudioDevice(IMMDevice **ppMMDevice) { TRACE(_T("CMpcAudioRenderer::GetAudioDevice\n")); CComPtr<IMMDeviceEnumerator> enumerator; IMMDeviceCollection* devices; IPropertyStore* pProps = NULL; HRESULT hr = enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator)); if (hr != S_OK) { TRACE(_T("CMpcAudioRenderer::GetAudioDevice - failed to create MMDeviceEnumerator!\n")); return hr; } TRACE(_T("CMpcAudioRenderer::GetAudioDevice - Target end point: %s\n"), m_csSound_Device); if (GetAvailableAudioDevices(&devices) == S_OK && devices) { UINT count(0); hr = devices->GetCount(&count); if (hr != S_OK) { TRACE(_T("CMpcAudioRenderer::GetAudioDevice - devices->GetCount failed: (0x%08x)\n"), hr); return hr; } for (ULONG i = 0 ; i < count ; i++) { LPWSTR pwszID = NULL; IMMDevice *endpoint = NULL; hr = devices->Item(i, &endpoint); if (hr == S_OK) { hr = endpoint->GetId(&pwszID); if (hr == S_OK) { if (endpoint->OpenPropertyStore(STGM_READ, &pProps) == S_OK) { PROPVARIANT varName; PropVariantInit(&varName); // Found the configured audio endpoint if ((pProps->GetValue(PKEY_Device_FriendlyName, &varName) == S_OK) && (m_csSound_Device == varName.pwszVal)) { TRACE(_T("CMpcAudioRenderer::GetAudioDevice - devices->GetId OK, num: (%d), pwszVal: %s, pwszID: %s\n"), i, varName.pwszVal, pwszID); enumerator->GetDevice(pwszID, ppMMDevice); SAFE_RELEASE(devices); *(ppMMDevice) = endpoint; CoTaskMemFree(pwszID); pwszID = NULL; PropVariantClear(&varName); SAFE_RELEASE(pProps); return S_OK; } else { PropVariantClear(&varName); SAFE_RELEASE(pProps); SAFE_RELEASE(endpoint); CoTaskMemFree(pwszID); pwszID = NULL; } } } else { TRACE(_T("CMpcAudioRenderer::GetAudioDevice - devices->GetId failed: (0x%08x)\n"), hr); } } else { TRACE(_T("CMpcAudioRenderer::GetAudioDevice - devices->Item failed: (0x%08x)\n"), hr); } CoTaskMemFree(pwszID); pwszID = NULL; } } TRACE(_T("CMpcAudioRenderer::GetAudioDevice - Unable to find selected audio device, using the default end point!\n")); hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, ppMMDevice); SAFE_RELEASE(devices); return hr; }
HRESULT AudioRendererSettings::GetAudioDevice(IMMDevice** ppMMDevice) { Log("CWASAPIRenderFilter::GetAudioDevice"); CComPtr<IMMDeviceEnumerator> enumerator; IMMDeviceCollection* devices; HRESULT hr = enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator)); if (FAILED(hr)) { Log(" failed to create MMDeviceEnumerator!"); return hr; } Log("Target end point: %S", m_wWASAPIPreferredDeviceId); if (GetAvailableAudioDevices(&devices, NULL, false) == S_OK) { UINT count = 0; hr = devices->GetCount(&count); if (FAILED(hr)) { Log(" devices->GetCount failed: (0x%08x)", hr); return hr; } for (UINT i = 0; i < count; i++) { LPWSTR pwszID = NULL; IMMDevice* endpoint = NULL; hr = devices->Item(i, &endpoint); if (SUCCEEDED(hr)) { hr = endpoint->GetId(&pwszID); if (SUCCEEDED(hr)) { // Found the configured audio endpoint if (wcscmp(pwszID, m_wWASAPIPreferredDeviceId) == 0) { enumerator->GetDevice(m_wWASAPIPreferredDeviceId, ppMMDevice); SAFE_RELEASE(devices); *ppMMDevice = endpoint; CoTaskMemFree(pwszID); pwszID = NULL; return S_OK; } else { SAFE_RELEASE(endpoint); CoTaskMemFree(pwszID); pwszID = NULL; } } else Log(" devices->GetId failed: (0x%08x)", hr); } else Log(" devices->Item failed: (0x%08x)", hr); CoTaskMemFree(pwszID); pwszID = NULL; } } Log("Unable to find selected audio device, using the default end point!"); hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, ppMMDevice); IPropertyStore* pProps = NULL; if (SUCCEEDED((*ppMMDevice)->OpenPropertyStore(STGM_READ, &pProps))) { LPWSTR pwszID = NULL; PROPVARIANT varName; PropVariantInit(&varName); PROPVARIANT eventDriven; PropVariantInit(&eventDriven); PROPVARIANT speakerMask; PropVariantInit(&speakerMask); if (SUCCEEDED(pProps->GetValue(PKEY_Device_FriendlyName, &varName)) && SUCCEEDED(pProps->GetValue(PKEY_AudioEndpoint_Supports_EventDriven_Mode, &eventDriven)) && SUCCEEDED((*ppMMDevice)->GetId(&pwszID))) { pProps->GetValue(PKEY_AudioEndpoint_PhysicalSpeakers, &speakerMask); Log("Default audio endpoint: \"%S\" (%S) - pull mode: %d sprk mask: %d" ,varName.pwszVal, pwszID, eventDriven.intVal, speakerMask.uintVal); } CoTaskMemFree(pwszID); pwszID = NULL; PropVariantClear(&varName); PropVariantClear(&eventDriven); PropVariantClear(&speakerMask); SAFE_RELEASE(pProps) }