stuff-from-scratch/apps/audio-win.cpp

83 lines
2.3 KiB
C++
Raw Normal View History

2020-07-04 18:43:08 +00:00
// LowLevelAudio.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "Mmdeviceapi.h"
#include "Functiondiscoverykeys_devpkey.h"
#include <iostream>
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
int main()
{
2021-03-29 20:31:24 +00:00
CoInitialize(nullptr);
IMMDeviceEnumerator* pEnumerator = nullptr;
IMMDeviceCollection* pDeviceCollection = nullptr;
HRESULT hr = S_OK;
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
IID_IMMDeviceEnumerator, (void**)& pEnumerator);
if (!pEnumerator)
{
std::cout << "Failed to populate enumerator" << std::endl;
return 0;
}
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDeviceCollection);
UINT count;
hr = pDeviceCollection->GetCount(&count);
if (count == 0)
{
std::cout << "No devices found\n";
}
// Each loop prints the name of an endpoint device.
IMMDevice* pEndpoint = nullptr;
IPropertyStore* pProps = nullptr;
LPWSTR pwszID = nullptr;
for (ULONG i = 0; i < count; i++)
{
// Get pointer to endpoint number i.
hr = pDeviceCollection->Item(i, &pEndpoint);
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
// Get the endpoint ID string.
hr = pEndpoint->GetId(&pwszID);
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
PROPVARIANT varName;
// Initialize container for property value.
PropVariantInit(&varName);
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
// Get the endpoint's friendly-name property.
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
// Print endpoint friendly name and endpoint ID.
std::cout << "Endpoint: " << i << " " << varName.pwszVal << " " << pwszID << std::endl;
CoTaskMemFree(pwszID);
pwszID = nullptr;
2020-07-04 18:43:08 +00:00
2021-03-29 20:31:24 +00:00
PropVariantClear(&varName);
if (pProps)
{
pProps->Release();
pProps = nullptr;
}
if (pEndpoint)
{
pEndpoint->Release();
pEndpoint = nullptr;
}
}
if (pEnumerator)
{
pEnumerator->Release();
pEnumerator = nullptr;
}
if (pDeviceCollection)
{
pDeviceCollection->Release();
pDeviceCollection = nullptr;
}
return 0;
2020-07-04 18:43:08 +00:00
}