-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVolumeImpl.cpp
More file actions
149 lines (127 loc) · 3.85 KB
/
VolumeImpl.cpp
File metadata and controls
149 lines (127 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "pch.h"
#include "VolumeImpl.h"
#include <functional>
using namespace UWPGlobalVolume;
using namespace Microsoft::WRL;
using namespace Windows::Media::Devices;
using namespace std::placeholders;
VolumeImpl::VolumeImpl()
{
m_event = CreateEvent(nullptr, false, false, nullptr);
// register for changes in Default audio device
m_token = MediaDevice::DefaultAudioRenderDeviceChanged += ref new Windows::Foundation::TypedEventHandler<Platform::Object^, DefaultAudioRenderDeviceChangedEventArgs^>(std::bind(&VolumeImpl::OnDefaultAudioCaptureDeviceChanged, this, _1, _2));
}
VolumeImpl::~VolumeImpl()
{
UnregisterControlChangeNotify();
CloseHandle(m_event);
MediaDevice::DefaultAudioCaptureDeviceChanged -= m_token;
}
HRESULT VolumeImpl::InitializeVolumeInterface()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_volumeInterface != nullptr)
{
return S_OK;
}
ComPtr<IActivateAudioInterfaceAsyncOperation> asyncOp;
m_result = S_OK;
auto id = MediaDevice::GetDefaultAudioRenderId(AudioDeviceRole::Default);
HRESULT hr = ActivateAudioInterfaceAsync(id->Data(), __uuidof(IAudioEndpointVolume), nullptr, this, &asyncOp);
if (SUCCEEDED(hr))
{
WaitForSingleObject(m_event, 1000);
}
return m_result;
}
void VolumeImpl::OnDefaultAudioCaptureDeviceChanged(Platform::Object^ sender, DefaultAudioRenderDeviceChangedEventArgs^ args)
{
// force next Set/GetVolume call to reinitialize the IAudioEndpointInterface
std::lock_guard<std::mutex> lock(m_mutex);
m_volumeInterface.Reset();
}
bool VolumeImpl::SetVolume(float volume)
{
HRESULT hr = InitializeVolumeInterface();
if (SUCCEEDED(hr) && m_volumeInterface != nullptr)
{
hr = m_volumeInterface->SetMasterVolumeLevelScalar(volume, NULL);
}
return SUCCEEDED(hr);
}
float VolumeImpl::GetVolume()
{
float volume = 0.0f;
HRESULT hr = InitializeVolumeInterface();
if (SUCCEEDED(hr) && m_volumeInterface != nullptr)
{
hr = m_volumeInterface->GetMasterVolumeLevelScalar(&volume);
}
return volume;
}
bool VolumeImpl::SetMute(bool isMuted)
{
HRESULT hr = InitializeVolumeInterface();
if (SUCCEEDED(hr) && m_volumeInterface != nullptr)
{
hr = m_volumeInterface->SetMute(isMuted, NULL);
}
return SUCCEEDED(hr);
}
bool VolumeImpl::GetMute()
{
BOOL isMuted = false;
HRESULT hr = InitializeVolumeInterface();
if (SUCCEEDED(hr) && m_volumeInterface != nullptr)
{
hr = m_volumeInterface->GetMute(&isMuted);
}
return isMuted;
}
void VolumeImpl::UnregisterControlChangeNotify()
{
HRESULT hr = InitializeVolumeInterface();
if (SUCCEEDED(hr) && m_volumeInterface != nullptr)
{
hr = m_volumeInterface->UnregisterControlChangeNotify(this);
}
}
//
// ActivateCompleted()
//
// Callback implementation of ActivateAudioInterfaceAsync function. This will be called on MTA thread
// when results of the activation are available.
//
HRESULT VolumeImpl::ActivateCompleted(IActivateAudioInterfaceAsyncOperation* operation)
{
HRESULT hr = S_OK;
HRESULT hrActivateResult = S_OK;
ComPtr<IUnknown> punkVolumeInterface;
// Check for a successful activation result
hr = operation->GetActivateResult(&hrActivateResult, &punkVolumeInterface);
if (FAILED(hr))
{
if (FAILED(hrActivateResult))
{
hr = hrActivateResult;
}
goto exit;
}
punkVolumeInterface.CopyTo(&m_volumeInterface);
if (nullptr == m_volumeInterface)
{
hr = E_NOINTERFACE;
goto exit;
}
m_volumeInterface->RegisterControlChangeNotify(this);
exit:
SetEvent(m_event);
m_result = hr;
// Need to return S_OK
return S_OK;
}
HRESULT VolumeImpl::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify)
{
this->VolumeChangedAction(pNotify->bMuted, pNotify->fMasterVolume);
return S_OK;
}