Files
DeviceManager/WinDevice/src/Audio/AudioManager.cpp
2024-09-29 10:18:35 +08:00

109 lines
2.5 KiB
C++

#include "AudioManager.h"
#include <iostream>
WinDevice::AudioManager::AudioManager()
{
// 创建设备枚举器
}
WinDevice::AudioManager::~AudioManager()
{
}
HRESULT WinDevice::AudioManager::Init()
{
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&pEnumerator));
if (FAILED(hr))
{
std::cerr << "Error: CoCreateInstance failed with hr = " << hr << std::endl;
}
hr = _UpdateDeviceList(eCapture);
if (FAILED(hr))
{
std::cerr << "Error: Update Capture Device failed with hr = " << hr << std::endl;
}
hr = _UpdateDeviceList(eRender);
if (FAILED(hr))
{
std::cerr << "Error: Update Render Device failed with hr = " << hr << std::endl;
}
return hr;
}
HRESULT WinDevice::AudioManager::Uninit()
{
pEnumerator = nullptr;
pCaptureCollection = nullptr;
pRenderCollection = nullptr;
pDefaultCaptureEndpoint = nullptr;
pDefaultRenderEndpoint = nullptr;
return S_OK;
}
IMMDeviceCollection* WinDevice::AudioManager::GetDeviceList(EDataFlow flow)
{
if (flow == eCapture)
{
UINT count;
pCaptureCollection->GetCount(&count);
if (count == 0)
{
_UpdateDeviceList(eCapture);
}
return pCaptureCollection;
}
if (flow == eRender)
{
UINT count;
pRenderCollection->GetCount(&count);
if (count == 0)
{
_UpdateDeviceList(eRender);
}
return pRenderCollection;
}
return nullptr;
}
IMMDevice* WinDevice::AudioManager::GetDefaultDevice(EDataFlow flow)
{
if (flow == eCapture)
{
return pDefaultCaptureEndpoint;
}
if (flow == eRender)
{
return pDefaultRenderEndpoint;
}
return nullptr;
}
HRESULT WinDevice::AudioManager::_UpdateDeviceList(EDataFlow flow)
{
HRESULT hr = S_OK;
if (flow == eCapture)
{
hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pCaptureCollection);
}
if (flow == eRender)
{
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pRenderCollection);
}
return hr;
}
HRESULT WinDevice::AudioManager::_UpdateDefaultDevice(EDataFlow flow)
{
HRESULT hr = S_OK;
if (flow == eCapture)
{
hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDefaultCaptureEndpoint);
}
if (flow == eRender)
{
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultRenderEndpoint);
}
return hr;
}