| 1 | //////////////////////////////////////////////////////////// |
|---|
| 2 | // Copyright (C) Roman Ryltsov, 2012-2013 |
|---|
| 3 | // Created by Roman Ryltsov roman@alax.info |
|---|
| 4 | |
|---|
| 5 | #include "stdafx.h" |
|---|
| 6 | #include <windows.h> |
|---|
| 7 | #include <mmdeviceapi.h> |
|---|
| 8 | #include <audiopolicy.h> |
|---|
| 9 | #include <endpointvolume.h> |
|---|
| 10 | |
|---|
| 11 | #define __C ATLENSURE_SUCCEEDED |
|---|
| 12 | |
|---|
| 13 | //class CNotification : |
|---|
| 14 | // public IAudioSessionEvents |
|---|
| 15 | //{ |
|---|
| 16 | //private: |
|---|
| 17 | // INT m_nSessionIndex; |
|---|
| 18 | // |
|---|
| 19 | //public: |
|---|
| 20 | //// CNotification |
|---|
| 21 | // CNotification(INT nSessionIndex) : |
|---|
| 22 | // m_nSessionIndex(nSessionIndex) |
|---|
| 23 | // { |
|---|
| 24 | // } |
|---|
| 25 | // |
|---|
| 26 | //// IUnknown |
|---|
| 27 | // STDMETHOD(QueryInterface)(REFIID, VOID** ppvObject) |
|---|
| 28 | // { |
|---|
| 29 | // _tprintf(_T("m_nSessionIndex %d, QueryInterface\n")); |
|---|
| 30 | // return E_NOINTERFACE; |
|---|
| 31 | // } |
|---|
| 32 | // STDMETHOD_(ULONG, AddRef)() |
|---|
| 33 | // { |
|---|
| 34 | // return 2; |
|---|
| 35 | // } |
|---|
| 36 | // STDMETHOD_(ULONG, Release)() |
|---|
| 37 | // { |
|---|
| 38 | // return 1; |
|---|
| 39 | // } |
|---|
| 40 | // |
|---|
| 41 | //// IAudioSessionEvents |
|---|
| 42 | // STDMETHOD(OnDisplayNameChanged)(LPCWSTR NewDisplayName, LPCGUID EventContext) |
|---|
| 43 | // { |
|---|
| 44 | // return S_OK; |
|---|
| 45 | // } |
|---|
| 46 | // STDMETHOD(OnIconPathChanged)(LPCWSTR NewIconPath, LPCGUID EventContext) |
|---|
| 47 | // { |
|---|
| 48 | // return S_OK; |
|---|
| 49 | // } |
|---|
| 50 | // STDMETHOD(OnSimpleVolumeChanged)(float NewVolume, BOOL NewMute, LPCGUID EventContext) |
|---|
| 51 | // { |
|---|
| 52 | // _tprintf(_T("m_nSessionIndex %d, NewVolume %.2f, NewMute %d\n"), m_nSessionIndex, NewVolume, NewMute); |
|---|
| 53 | // return S_OK; |
|---|
| 54 | // } |
|---|
| 55 | // STDMETHOD(OnChannelVolumeChanged)(DWORD ChannelCount, float NewChannelVolumeArray[], DWORD ChangedChannel, LPCGUID EventContext) |
|---|
| 56 | // { |
|---|
| 57 | // _tprintf(_T("m_nSessionIndex %d, ChannelCount %d, NewChannelVolumeArray][ { %.2f, ... }, ChangedChannel %d\n"), m_nSessionIndex, ChannelCount, NewChannelVolumeArray[0], ChangedChannel); |
|---|
| 58 | // return S_OK; |
|---|
| 59 | // } |
|---|
| 60 | // STDMETHOD(OnGroupingParamChanged)(LPCGUID NewGroupingParam, LPCGUID EventContext) |
|---|
| 61 | // { |
|---|
| 62 | // return S_OK; |
|---|
| 63 | // } |
|---|
| 64 | // STDMETHOD(OnStateChanged)(AudioSessionState NewState) |
|---|
| 65 | // { |
|---|
| 66 | // return S_OK; |
|---|
| 67 | // } |
|---|
| 68 | // STDMETHOD(OnSessionDisconnected)(AudioSessionDisconnectReason DisconnectReason) |
|---|
| 69 | // { |
|---|
| 70 | // return S_OK; |
|---|
| 71 | // } |
|---|
| 72 | //}; |
|---|
| 73 | |
|---|
| 74 | int _tmain(int argc, _TCHAR* argv[]) |
|---|
| 75 | { |
|---|
| 76 | __C(CoInitializeEx(NULL, COINIT_MULTITHREADED)); |
|---|
| 77 | CComPtr<IMMDeviceEnumerator> pDeviceEnumerator; |
|---|
| 78 | __C(pDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator))); |
|---|
| 79 | CComPtr<IMMDevice> pDevice; |
|---|
| 80 | __C(pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice)); |
|---|
| 81 | CComPtr<IAudioSessionManager2> pAudioSessionManager2; |
|---|
| 82 | __C(pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (VOID**) &pAudioSessionManager2)); |
|---|
| 83 | CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator; |
|---|
| 84 | __C(pAudioSessionManager2->GetSessionEnumerator(&pAudioSessionEnumerator)); |
|---|
| 85 | INT nSessionCount; |
|---|
| 86 | __C(pAudioSessionEnumerator->GetCount(&nSessionCount)); |
|---|
| 87 | _tprintf(_T("nSessionCount %d\n"), nSessionCount); |
|---|
| 88 | //for(INT nSessionIndex = 0; nSessionIndex < nSessionCount; nSessionIndex++) |
|---|
| 89 | //{ |
|---|
| 90 | // CComPtr<IAudioSessionControl> pSessionControl; |
|---|
| 91 | // if(FAILED(pAudioSessionEnumerator->GetSession(nSessionIndex, &pSessionControl))) |
|---|
| 92 | // continue; |
|---|
| 93 | // CComHeapPtr<WCHAR> pszDisplayName; |
|---|
| 94 | // __C(pSessionControl->GetDisplayName(&pszDisplayName)); |
|---|
| 95 | // _tprintf(_T("nSessionIndex %d, pszDisplayName \"%s\"\n"), nSessionIndex, CString(pszDisplayName)); |
|---|
| 96 | // CNotification* pNotification = new CNotification(nSessionIndex); |
|---|
| 97 | // __C(pSessionControl->RegisterAudioSessionNotification(pNotification)); |
|---|
| 98 | //} |
|---|
| 99 | for(; ; ) |
|---|
| 100 | { |
|---|
| 101 | for(INT nSessionIndex = 0; nSessionIndex < nSessionCount; nSessionIndex++) |
|---|
| 102 | { |
|---|
| 103 | CComPtr<IAudioSessionControl> pSessionControl; |
|---|
| 104 | if(FAILED(pAudioSessionEnumerator->GetSession(nSessionIndex, &pSessionControl))) |
|---|
| 105 | continue; |
|---|
| 106 | CComQIPtr<IAudioMeterInformation> pMeterInformation = pSessionControl; |
|---|
| 107 | FLOAT fPeakValue; |
|---|
| 108 | __C(pMeterInformation->GetPeakValue(&fPeakValue)); |
|---|
| 109 | _tprintf(_T("nSessionIndex %d, fPeakValue %.2f\n"), nSessionIndex, fPeakValue); |
|---|
| 110 | } |
|---|
| 111 | Sleep(1000); |
|---|
| 112 | } |
|---|
| 113 | //Sleep(INFINITE); |
|---|
| 114 | CoUninitialize(); |
|---|
| 115 | return 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|