OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/audio_capturer_win.h" | 5 #include "remoting/host/audio_capturer_win.h" |
6 | 6 |
7 #include <avrt.h> | 7 #include <avrt.h> |
8 #include <mmreg.h> | 8 #include <mmreg.h> |
9 #include <mmsystem.h> | 9 #include <mmsystem.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <windows.h> | 12 #include <windows.h> |
13 | 13 |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/synchronization/lock.h" | |
19 | 20 |
20 namespace { | 21 namespace { |
21 const int kChannels = 2; | 22 const int kChannels = 2; |
22 const int kBytesPerSample = 2; | 23 const int kBytesPerSample = 2; |
23 const int kBitsPerSample = kBytesPerSample * 8; | 24 const int kBitsPerSample = kBytesPerSample * 8; |
24 // Conversion factor from 100ns to 1ms. | 25 // Conversion factor from 100ns to 1ms. |
25 const int k100nsPerMillisecond = 10000; | 26 const int k100nsPerMillisecond = 10000; |
26 | 27 |
27 // Tolerance for catching packets of silence. If all samples have absolute | 28 // Tolerance for catching packets of silence. If all samples have absolute |
28 // value less than this threshold, the packet will be counted as a packet of | 29 // value less than this threshold, the packet will be counted as a packet of |
29 // silence. A value of 2 was chosen, because Windows can give samples of 1 and | 30 // silence. A value of 2 was chosen, because Windows can give samples of 1 and |
30 // -1, even when no audio is playing. | 31 // -1, even when no audio is playing. |
31 const int kSilenceThreshold = 2; | 32 const int kSilenceThreshold = 2; |
32 | 33 |
33 // Lower bound for timer intervals, in milliseconds. | 34 // Lower bound for timer intervals, in milliseconds. |
34 const int kMinTimerInterval = 30; | 35 const int kMinTimerInterval = 30; |
35 | 36 |
36 // Upper bound for the timer precision error, in milliseconds. | 37 // Upper bound for the timer precision error, in milliseconds. |
37 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. | 38 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. |
38 const int kMaxExpectedTimerLag = 30; | 39 const int kMaxExpectedTimerLag = 30; |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 namespace remoting { | 42 namespace remoting { |
42 | 43 |
44 class AudioCapturerWin::MMNotificationClient : public IMMNotificationClient { | |
45 public: | |
46 HRESULT __stdcall OnDefaultDeviceChanged( | |
47 EDataFlow flow, | |
48 ERole role, | |
49 LPCWSTR pwstrDefaultDevice) override { | |
50 base::AutoLock lock(lock_); | |
51 default_audio_device_changed_ = true; | |
52 return S_OK; | |
53 } | |
54 | |
55 HRESULT __stdcall QueryInterface(REFIID iid, void** object) override { | |
56 if (iid == IID_IUnknown || iid == __uuidof(IMMNotificationClient)) { | |
57 *object = static_cast<IMMNotificationClient*>(this); | |
58 return S_OK; | |
59 } | |
60 *object = nullptr; | |
61 return E_NOINTERFACE; | |
62 } | |
63 | |
64 // No Ops overrides. | |
65 HRESULT __stdcall OnDeviceAdded(LPCWSTR pwstrDeviceId) override { | |
66 return S_OK; | |
67 } | |
68 HRESULT __stdcall OnDeviceRemoved(LPCWSTR pwstrDeviceId) override { | |
69 return S_OK; | |
70 } | |
71 HRESULT __stdcall OnDeviceStateChanged(LPCWSTR pwstrDeviceId, | |
72 DWORD dwNewState) override { | |
73 return S_OK; | |
74 } | |
75 HRESULT __stdcall OnPropertyValueChanged(LPCWSTR pwstrDeviceId, | |
76 const PROPERTYKEY key) override { | |
77 return S_OK; | |
78 } | |
79 ULONG __stdcall AddRef() override { return 1; } | |
80 ULONG __stdcall Release() override { return 1; } | |
81 | |
82 bool GetAndResetDefaultAudioDeviceChanged() { | |
83 base::AutoLock lock(lock_); | |
84 if (default_audio_device_changed_) { | |
85 default_audio_device_changed_ = false; | |
86 return true; | |
87 } | |
88 return false; | |
89 } | |
90 | |
91 private: | |
92 // |lock_| musted be locked when accessing |default_audio_device_changed_|. | |
93 bool default_audio_device_changed_ = false; | |
94 base::Lock lock_; | |
95 }; | |
96 | |
43 AudioCapturerWin::AudioCapturerWin() | 97 AudioCapturerWin::AudioCapturerWin() |
44 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), | 98 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID), |
45 silence_detector_(kSilenceThreshold), | 99 silence_detector_(kSilenceThreshold), |
100 mm_notification_client_(new MMNotificationClient()), | |
46 last_capture_error_(S_OK) { | 101 last_capture_error_(S_OK) { |
47 thread_checker_.DetachFromThread(); | 102 thread_checker_.DetachFromThread(); |
48 } | 103 } |
49 | 104 |
50 AudioCapturerWin::~AudioCapturerWin() { | 105 AudioCapturerWin::~AudioCapturerWin() { |
51 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
52 if (audio_client_) { | 107 if (audio_client_) { |
53 audio_client_->Stop(); | 108 audio_client_->Stop(); |
54 } | 109 } |
55 } | 110 } |
56 | 111 |
57 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { | 112 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
58 DCHECK(!audio_capture_client_.get()); | 113 DCHECK(!audio_capture_client_.get()); |
59 DCHECK(!audio_client_.get()); | 114 DCHECK(!audio_client_.get()); |
60 DCHECK(!mm_device_.get()); | 115 DCHECK(!mm_device_.get()); |
61 DCHECK(!audio_volume_.get()); | 116 DCHECK(!audio_volume_.get()); |
62 DCHECK(static_cast<PWAVEFORMATEX>(wave_format_ex_) == nullptr); | 117 DCHECK(static_cast<PWAVEFORMATEX>(wave_format_ex_) == nullptr); |
63 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
64 | 119 |
65 callback_ = callback; | 120 callback_ = callback; |
66 | 121 |
67 // Initialize the capture timer. | 122 if (!ResetAndInitialize()) { |
123 return false; | |
124 } | |
125 | |
126 // Initialize the capture timer and start capturing. Note, this timer won't | |
127 // be reset or restarted in ResetAndInitialize() function. Which means we | |
128 // expect the audio_device_period_ is a system wide configuration, it would | |
129 // not be changed with the default audio device. | |
68 capture_timer_.reset(new base::RepeatingTimer()); | 130 capture_timer_.reset(new base::RepeatingTimer()); |
131 capture_timer_->Start(FROM_HERE, audio_device_period_, this, | |
132 &AudioCapturerWin::DoCapture); | |
133 return true; | |
134 } | |
135 | |
136 bool AudioCapturerWin::ResetAndInitialize() { | |
137 wave_format_ex_.Reset(nullptr); | |
138 mm_device_enumerator_.Release(); | |
139 audio_capture_client_.Release(); | |
140 audio_client_.Release(); | |
141 mm_device_.Release(); | |
142 audio_volume_.Release(); | |
69 | 143 |
70 HRESULT hr = S_OK; | 144 HRESULT hr = S_OK; |
71 | 145 hr = mm_device_enumerator_.CreateInstance(__uuidof(MMDeviceEnumerator)); |
72 base::win::ScopedComPtr<IMMDeviceEnumerator> mm_device_enumerator; | |
73 hr = mm_device_enumerator.CreateInstance(__uuidof(MMDeviceEnumerator)); | |
74 if (FAILED(hr)) { | 146 if (FAILED(hr)) { |
75 LOG(ERROR) << "Failed to create IMMDeviceEnumerator. Error " << hr; | 147 LOG(ERROR) << "Failed to create IMMDeviceEnumerator. Error " << hr; |
76 return false; | 148 return false; |
77 } | 149 } |
78 | 150 |
151 hr = mm_device_enumerator_->RegisterEndpointNotificationCallback( | |
152 mm_notification_client_.get()); | |
153 if (FAILED(hr)) { | |
154 // We cannot predict which kind of error the API may return, but this is | |
155 // not a fatal error. | |
156 LOG(ERROR) << "Failed to register IMMNotificationClient. Error " << hr; | |
157 } | |
158 | |
79 // Get the audio endpoint. | 159 // Get the audio endpoint. |
80 hr = mm_device_enumerator->GetDefaultAudioEndpoint(eRender, | 160 hr = mm_device_enumerator_->GetDefaultAudioEndpoint(eRender, eConsole, |
81 eConsole, | 161 mm_device_.Receive()); |
82 mm_device_.Receive()); | |
83 if (FAILED(hr)) { | 162 if (FAILED(hr)) { |
84 LOG(ERROR) << "Failed to get IMMDevice. Error " << hr; | 163 LOG(ERROR) << "Failed to get IMMDevice. Error " << hr; |
85 return false; | 164 return false; |
86 } | 165 } |
87 | 166 |
88 // Get an audio client. | 167 // Get an audio client. |
89 hr = mm_device_->Activate(__uuidof(IAudioClient), | 168 hr = mm_device_->Activate(__uuidof(IAudioClient), |
90 CLSCTX_ALL, | 169 CLSCTX_ALL, |
91 nullptr, | 170 nullptr, |
92 audio_client_.ReceiveVoid()); | 171 audio_client_.ReceiveVoid()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 // TODO(zijiehe): Do we need to control per process volume? | 281 // TODO(zijiehe): Do we need to control per process volume? |
203 hr = mm_device_->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, | 282 hr = mm_device_->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, |
204 audio_volume_.ReceiveVoid()); | 283 audio_volume_.ReceiveVoid()); |
205 if (FAILED(hr)) { | 284 if (FAILED(hr)) { |
206 LOG(ERROR) << "Failed to get an IAudioEndpointVolume. Error " << hr; | 285 LOG(ERROR) << "Failed to get an IAudioEndpointVolume. Error " << hr; |
207 return false; | 286 return false; |
208 } | 287 } |
209 | 288 |
210 silence_detector_.Reset(sampling_rate_, kChannels); | 289 silence_detector_.Reset(sampling_rate_, kChannels); |
211 | 290 |
212 // Start capturing. | |
213 capture_timer_->Start(FROM_HERE, | |
214 audio_device_period_, | |
215 this, | |
216 &AudioCapturerWin::DoCapture); | |
217 return true; | 291 return true; |
218 } | 292 } |
219 | 293 |
294 bool AudioCapturerWin::Initialized() const { | |
Sergey Ulanov
2016/07/22 23:38:05
nit: call it is_initialized()?
Hzj_jie
2016/07/23 00:24:18
Done.
| |
295 return wave_format_ex_.get() && | |
296 audio_capture_client_ && | |
297 audio_client_ && | |
298 mm_device_ && | |
299 audio_volume_; | |
Sergey Ulanov
2016/07/22 23:38:05
The way ResetAndInitialize() is implemented I'm no
Hzj_jie
2016/07/23 00:24:18
Indeed, no, the last statement mm_device->Activate
| |
300 } | |
301 | |
220 float AudioCapturerWin::GetAudioLevel() { | 302 float AudioCapturerWin::GetAudioLevel() { |
221 BOOL mute; | 303 BOOL mute; |
222 HRESULT hr = audio_volume_->GetMute(&mute); | 304 HRESULT hr = audio_volume_->GetMute(&mute); |
223 if (FAILED(hr)) { | 305 if (FAILED(hr)) { |
224 LOG(ERROR) << "Failed to get mute status from IAudioEndpointVolume, error " | 306 LOG(ERROR) << "Failed to get mute status from IAudioEndpointVolume, error " |
225 << hr; | 307 << hr; |
226 return 1; | 308 return 1; |
227 } | 309 } |
228 if (mute) { | 310 if (mute) { |
229 return 0; | 311 return 0; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); | 359 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
278 packet->set_channels(AudioPacket::CHANNELS_STEREO); | 360 packet->set_channels(AudioPacket::CHANNELS_STEREO); |
279 | 361 |
280 callback_.Run(std::move(packet)); | 362 callback_.Run(std::move(packet)); |
281 } | 363 } |
282 | 364 |
283 void AudioCapturerWin::DoCapture() { | 365 void AudioCapturerWin::DoCapture() { |
284 DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_)); | 366 DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_)); |
285 DCHECK(thread_checker_.CalledOnValidThread()); | 367 DCHECK(thread_checker_.CalledOnValidThread()); |
286 | 368 |
369 if (!Initialized() || | |
370 mm_notification_client_->GetAndResetDefaultAudioDeviceChanged()) { | |
371 if (!ResetAndInitialize()) { | |
372 // Initialization failed, we should wait for next DoCapture call. | |
373 return; | |
374 } | |
375 } | |
376 | |
287 // Fetch all packets from the audio capture endpoint buffer. | 377 // Fetch all packets from the audio capture endpoint buffer. |
288 HRESULT hr = S_OK; | 378 HRESULT hr = S_OK; |
289 while (true) { | 379 while (true) { |
290 UINT32 next_packet_size; | 380 UINT32 next_packet_size; |
291 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); | 381 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); |
292 if (FAILED(hr)) | 382 if (FAILED(hr)) |
293 break; | 383 break; |
294 | 384 |
295 if (next_packet_size <= 0) { | 385 if (next_packet_size <= 0) { |
296 return; | 386 return; |
(...skipping 29 matching lines...) Expand all Loading... | |
326 | 416 |
327 bool AudioCapturer::IsSupported() { | 417 bool AudioCapturer::IsSupported() { |
328 return true; | 418 return true; |
329 } | 419 } |
330 | 420 |
331 std::unique_ptr<AudioCapturer> AudioCapturer::Create() { | 421 std::unique_ptr<AudioCapturer> AudioCapturer::Create() { |
332 return base::WrapUnique(new AudioCapturerWin()); | 422 return base::WrapUnique(new AudioCapturerWin()); |
333 } | 423 } |
334 | 424 |
335 } // namespace remoting | 425 } // namespace remoting |
OLD | NEW |