添加音频管理
This commit is contained in:
		
							
								
								
									
										7
									
								
								WinDevice/Audio/AudioEnum.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								WinDevice/Audio/AudioEnum.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| #pragma once | ||||
|  | ||||
| enum AudioDeviceType | ||||
| { | ||||
|     Render, | ||||
|     Capture | ||||
| }; | ||||
							
								
								
									
										110
									
								
								WinDevice/Audio/AudioManager.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								WinDevice/Audio/AudioManager.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,110 @@ | ||||
| #include "stdafx.h" | ||||
|  | ||||
| #include "AudioManager.h" | ||||
| #include <iostream> | ||||
|  | ||||
|  | ||||
| AudioManager::AudioManager() | ||||
| { | ||||
|     // 创建设备枚举器 | ||||
| } | ||||
|  | ||||
| AudioManager::~AudioManager() | ||||
| { | ||||
| } | ||||
|  | ||||
| HRESULT 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 AudioManager::Uninit() | ||||
| { | ||||
|     pEnumerator = nullptr; | ||||
|     pCaptureCollection = nullptr; | ||||
|     pRenderCollection = nullptr; | ||||
|     pDefaultCaptureEndpoint = nullptr; | ||||
|     pDefaultRenderEndpoint = nullptr; | ||||
|     return S_OK; | ||||
| } | ||||
|  | ||||
| IMMDeviceCollection* 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* AudioManager::GetDefaultDevice(EDataFlow flow) | ||||
| { | ||||
|     if (flow == eCapture) | ||||
|     { | ||||
|         return pDefaultCaptureEndpoint; | ||||
|     } | ||||
|     if (flow == eRender) | ||||
|     { | ||||
|         return pDefaultRenderEndpoint; | ||||
|     } | ||||
|     return nullptr; | ||||
| } | ||||
|  | ||||
| HRESULT 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 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; | ||||
| } | ||||
							
								
								
									
										26
									
								
								WinDevice/Audio/AudioManager.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								WinDevice/Audio/AudioManager.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <Mmdeviceapi.h> | ||||
|  | ||||
| class AudioManager | ||||
| { | ||||
| public: | ||||
|     AudioManager(); | ||||
|     ~AudioManager(); | ||||
|  | ||||
|     HRESULT Init(); | ||||
|     HRESULT Uninit(); | ||||
|  | ||||
|     IMMDeviceCollection* GetDeviceList(EDataFlow flow); | ||||
|     IMMDevice* GetDefaultDevice(EDataFlow flow); | ||||
|  | ||||
| private: | ||||
|     IMMDeviceEnumerator* pEnumerator = NULL; | ||||
|     IMMDeviceCollection* pRenderCollection = NULL; | ||||
|     IMMDeviceCollection* pCaptureCollection = NULL; | ||||
|     IMMDevice* pDefaultRenderEndpoint = NULL; | ||||
|     IMMDevice* pDefaultCaptureEndpoint = NULL; | ||||
|  | ||||
|     HRESULT _UpdateDeviceList(EDataFlow flow); | ||||
|     HRESULT _UpdateDefaultDevice(EDataFlow flow); | ||||
| }; | ||||
							
								
								
									
										41
									
								
								WinDevice/Utils/SysInfoUtil.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								WinDevice/Utils/SysInfoUtil.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| #include <stdafx.h> | ||||
| #include <cstdio> | ||||
| #include <iphlpapi.h> | ||||
|  | ||||
| #include "SysInfoUtil.h" | ||||
|  | ||||
| void SysInfoUtil::GetMacByGetAdaptersInfo(char* outMAC) const | ||||
| { | ||||
|     ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); | ||||
|     PIP_ADAPTER_INFO pAdapter = NULL; | ||||
|     PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); | ||||
|  | ||||
|     // Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable | ||||
|     if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS) | ||||
|     { | ||||
|         free(pAdapterInfo); | ||||
|         pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); | ||||
|     } | ||||
|  | ||||
|     //char MACAddress[32]; | ||||
|     if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) | ||||
|     { | ||||
|         pAdapter = pAdapterInfo; | ||||
|         while (pAdapter) | ||||
|         { | ||||
|             if (pAdapter->AddressLength == 6 && pAdapter->Type == MIB_IF_TYPE_ETHERNET) | ||||
|             { | ||||
|                 sprintf_s(outMAC, 18, "%02X-%02X-%02X-%02X-%02X-%02X", | ||||
|                     int(pAdapter->Address[0]), | ||||
|                     int(pAdapter->Address[1]), | ||||
|                     int(pAdapter->Address[2]), | ||||
|                     int(pAdapter->Address[3]), | ||||
|                     int(pAdapter->Address[4]), | ||||
|                     int(pAdapter->Address[5])); | ||||
|                 break; | ||||
|             } | ||||
|             pAdapter = pAdapter->Next; | ||||
|         } | ||||
|     } | ||||
|     free(pAdapterInfo); | ||||
| } | ||||
							
								
								
									
										9
									
								
								WinDevice/Utils/SysInfoUtil.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								WinDevice/Utils/SysInfoUtil.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| #pragma once | ||||
|  | ||||
| #pragma comment(lib, "IPHLPAPI.lib") | ||||
|  | ||||
| class SysInfoUtil | ||||
| { | ||||
| public: | ||||
|     void GetMacByGetAdaptersInfo(char* outMAC) const; | ||||
| }; | ||||
| @@ -1,11 +1,26 @@ | ||||
| // WinDevice.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 | ||||
| // | ||||
|  | ||||
| #include <chrono> | ||||
| #include <iostream> | ||||
|  | ||||
| #include "Utils/SysInfoUtil.h" | ||||
|  | ||||
| using namespace std; | ||||
| int main() | ||||
| { | ||||
|     std::cout << "Hello World!\n"; | ||||
|     SysInfoUtil util; | ||||
|     char mac[18]; | ||||
|  | ||||
|     // 测量函数调用的耗时 | ||||
|     auto start = chrono::high_resolution_clock::now(); | ||||
|     util.GetMacByGetAdaptersInfo(mac); | ||||
|     auto end = chrono::high_resolution_clock::now(); | ||||
|     auto duration = chrono::duration_cast<chrono::milliseconds>(end - start); | ||||
|     cout << "Function call duration: " << duration.count() << " ms" << endl; | ||||
|  | ||||
|     // 输出日志信息 | ||||
|     cout << "MAC address: " << mac << endl; | ||||
| } | ||||
|  | ||||
| // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 | ||||
|   | ||||
| @@ -88,6 +88,7 @@ | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <ConformanceMode>true</ConformanceMode> | ||||
|       <AdditionalIncludeDirectories>.;</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <SubSystem>Console</SubSystem> | ||||
| @@ -102,6 +103,7 @@ | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <ConformanceMode>true</ConformanceMode> | ||||
|       <AdditionalIncludeDirectories>.;</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <SubSystem>Console</SubSystem> | ||||
| @@ -116,6 +118,7 @@ | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <ConformanceMode>true</ConformanceMode> | ||||
|       <AdditionalIncludeDirectories>.;</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <SubSystem>Console</SubSystem> | ||||
| @@ -130,6 +133,7 @@ | ||||
|       <SDLCheck>true</SDLCheck> | ||||
|       <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
|       <ConformanceMode>true</ConformanceMode> | ||||
|       <AdditionalIncludeDirectories>.;</AdditionalIncludeDirectories> | ||||
|     </ClCompile> | ||||
|     <Link> | ||||
|       <SubSystem>Console</SubSystem> | ||||
| @@ -139,8 +143,16 @@ | ||||
|     </Link> | ||||
|   </ItemDefinitionGroup> | ||||
|   <ItemGroup> | ||||
|     <ClCompile Include="Audio\AudioManager.cpp" /> | ||||
|     <ClCompile Include="Utils\SysInfoUtil.cpp" /> | ||||
|     <ClCompile Include="WinDevice.cpp" /> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="Audio\AudioEnum.h" /> | ||||
|     <ClInclude Include="Audio\AudioManager.h" /> | ||||
|     <ClInclude Include="stdafx.h" /> | ||||
|     <ClInclude Include="Utils\SysInfoUtil.h" /> | ||||
|   </ItemGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
|   <ImportGroup Label="ExtensionTargets"> | ||||
|   </ImportGroup> | ||||
|   | ||||
							
								
								
									
										8
									
								
								WinDevice/stdafx.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								WinDevice/stdafx.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <windows.h> | ||||
| #include <winerror.h> | ||||
| #include <shlwapi.h> | ||||
| #include <Psapi.h> | ||||
| #include <Shellapi.h> | ||||
		Reference in New Issue
	
	Block a user