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 <windows.h> | 7 #include <windows.h> |
8 #include <avrt.h> | 8 #include <avrt.h> |
9 #include <mmreg.h> | 9 #include <mmreg.h> |
10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 thread_checker_.DetachFromThread(); | 44 thread_checker_.DetachFromThread(); |
45 } | 45 } |
46 | 46 |
47 AudioCapturerWin::~AudioCapturerWin() { | 47 AudioCapturerWin::~AudioCapturerWin() { |
48 } | 48 } |
49 | 49 |
50 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { | 50 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
51 DCHECK(!audio_capture_client_.get()); | 51 DCHECK(!audio_capture_client_.get()); |
52 DCHECK(!audio_client_.get()); | 52 DCHECK(!audio_client_.get()); |
53 DCHECK(!mm_device_.get()); | 53 DCHECK(!mm_device_.get()); |
54 DCHECK(static_cast<PWAVEFORMATEX>(wave_format_ex_) == NULL); | 54 DCHECK(static_cast<PWAVEFORMATEX>(wave_format_ex_) == nullptr); |
55 DCHECK(thread_checker_.CalledOnValidThread()); | 55 DCHECK(thread_checker_.CalledOnValidThread()); |
56 | 56 |
57 callback_ = callback; | 57 callback_ = callback; |
58 | 58 |
59 // Initialize the capture timer. | 59 // Initialize the capture timer. |
60 capture_timer_.reset(new base::RepeatingTimer<AudioCapturerWin>()); | 60 capture_timer_.reset(new base::RepeatingTimer<AudioCapturerWin>()); |
61 | 61 |
62 HRESULT hr = S_OK; | 62 HRESULT hr = S_OK; |
63 | 63 |
64 base::win::ScopedComPtr<IMMDeviceEnumerator> mm_device_enumerator; | 64 base::win::ScopedComPtr<IMMDeviceEnumerator> mm_device_enumerator; |
65 hr = mm_device_enumerator.CreateInstance(__uuidof(MMDeviceEnumerator)); | 65 hr = mm_device_enumerator.CreateInstance(__uuidof(MMDeviceEnumerator)); |
66 if (FAILED(hr)) { | 66 if (FAILED(hr)) { |
67 LOG(ERROR) << "Failed to create IMMDeviceEnumerator. Error " << hr; | 67 LOG(ERROR) << "Failed to create IMMDeviceEnumerator. Error " << hr; |
68 return false; | 68 return false; |
69 } | 69 } |
70 | 70 |
71 // Get the audio endpoint. | 71 // Get the audio endpoint. |
72 hr = mm_device_enumerator->GetDefaultAudioEndpoint(eRender, | 72 hr = mm_device_enumerator->GetDefaultAudioEndpoint(eRender, |
73 eConsole, | 73 eConsole, |
74 mm_device_.Receive()); | 74 mm_device_.Receive()); |
75 if (FAILED(hr)) { | 75 if (FAILED(hr)) { |
76 LOG(ERROR) << "Failed to get IMMDevice. Error " << hr; | 76 LOG(ERROR) << "Failed to get IMMDevice. Error " << hr; |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 // Get an audio client. | 80 // Get an audio client. |
81 hr = mm_device_->Activate(__uuidof(IAudioClient), | 81 hr = mm_device_->Activate(__uuidof(IAudioClient), |
82 CLSCTX_ALL, | 82 CLSCTX_ALL, |
83 NULL, | 83 nullptr, |
84 audio_client_.ReceiveVoid()); | 84 audio_client_.ReceiveVoid()); |
85 if (FAILED(hr)) { | 85 if (FAILED(hr)) { |
86 LOG(ERROR) << "Failed to get an IAudioClient. Error " << hr; | 86 LOG(ERROR) << "Failed to get an IAudioClient. Error " << hr; |
87 return false; | 87 return false; |
88 } | 88 } |
89 | 89 |
90 REFERENCE_TIME device_period; | 90 REFERENCE_TIME device_period; |
91 hr = audio_client_->GetDevicePeriod(&device_period, NULL); | 91 hr = audio_client_->GetDevicePeriod(&device_period, nullptr); |
92 if (FAILED(hr)) { | 92 if (FAILED(hr)) { |
93 LOG(ERROR) << "IAudioClient::GetDevicePeriod failed. Error " << hr; | 93 LOG(ERROR) << "IAudioClient::GetDevicePeriod failed. Error " << hr; |
94 return false; | 94 return false; |
95 } | 95 } |
96 // We round up, if |device_period| / |k100nsPerMillisecond| | 96 // We round up, if |device_period| / |k100nsPerMillisecond| |
97 // is not a whole number. | 97 // is not a whole number. |
98 int device_period_in_milliseconds = | 98 int device_period_in_milliseconds = |
99 1 + ((device_period - 1) / k100nsPerMillisecond); | 99 1 + ((device_period - 1) / k100nsPerMillisecond); |
100 audio_device_period_ = base::TimeDelta::FromMilliseconds( | 100 audio_device_period_ = base::TimeDelta::FromMilliseconds( |
101 std::max(device_period_in_milliseconds, kMinTimerInterval)); | 101 std::max(device_period_in_milliseconds, kMinTimerInterval)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 162 } |
163 | 163 |
164 // Initialize the IAudioClient. | 164 // Initialize the IAudioClient. |
165 hr = audio_client_->Initialize( | 165 hr = audio_client_->Initialize( |
166 AUDCLNT_SHAREMODE_SHARED, | 166 AUDCLNT_SHAREMODE_SHARED, |
167 AUDCLNT_STREAMFLAGS_LOOPBACK, | 167 AUDCLNT_STREAMFLAGS_LOOPBACK, |
168 (kMaxExpectedTimerLag + audio_device_period_.InMilliseconds()) * | 168 (kMaxExpectedTimerLag + audio_device_period_.InMilliseconds()) * |
169 k100nsPerMillisecond, | 169 k100nsPerMillisecond, |
170 0, | 170 0, |
171 wave_format_ex_, | 171 wave_format_ex_, |
172 NULL); | 172 nullptr); |
173 if (FAILED(hr)) { | 173 if (FAILED(hr)) { |
174 LOG(ERROR) << "Failed to initialize IAudioClient. Error " << hr; | 174 LOG(ERROR) << "Failed to initialize IAudioClient. Error " << hr; |
175 return false; | 175 return false; |
176 } | 176 } |
177 | 177 |
178 // Get an IAudioCaptureClient. | 178 // Get an IAudioCaptureClient. |
179 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient), | 179 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient), |
180 audio_capture_client_.ReceiveVoid()); | 180 audio_capture_client_.ReceiveVoid()); |
181 if (FAILED(hr)) { | 181 if (FAILED(hr)) { |
182 LOG(ERROR) << "Failed to get an IAudioCaptureClient. Error " << hr; | 182 LOG(ERROR) << "Failed to get an IAudioCaptureClient. Error " << hr; |
(...skipping 18 matching lines...) Expand all Loading... |
201 } | 201 } |
202 | 202 |
203 void AudioCapturerWin::Stop() { | 203 void AudioCapturerWin::Stop() { |
204 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
205 DCHECK(IsStarted()); | 205 DCHECK(IsStarted()); |
206 | 206 |
207 capture_timer_.reset(); | 207 capture_timer_.reset(); |
208 mm_device_.Release(); | 208 mm_device_.Release(); |
209 audio_client_.Release(); | 209 audio_client_.Release(); |
210 audio_capture_client_.Release(); | 210 audio_capture_client_.Release(); |
211 wave_format_ex_.Reset(NULL); | 211 wave_format_ex_.Reset(nullptr); |
212 | 212 |
213 thread_checker_.DetachFromThread(); | 213 thread_checker_.DetachFromThread(); |
214 } | 214 } |
215 | 215 |
216 bool AudioCapturerWin::IsStarted() { | 216 bool AudioCapturerWin::IsStarted() { |
217 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
218 return capture_timer_.get() != NULL; | 218 return capture_timer_.get() != nullptr; |
219 } | 219 } |
220 | 220 |
221 void AudioCapturerWin::DoCapture() { | 221 void AudioCapturerWin::DoCapture() { |
222 DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_)); | 222 DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_)); |
223 DCHECK(thread_checker_.CalledOnValidThread()); | 223 DCHECK(thread_checker_.CalledOnValidThread()); |
224 DCHECK(IsStarted()); | 224 DCHECK(IsStarted()); |
225 | 225 |
226 // Fetch all packets from the audio capture endpoint buffer. | 226 // Fetch all packets from the audio capture endpoint buffer. |
227 HRESULT hr = S_OK; | 227 HRESULT hr = S_OK; |
228 while (true) { | 228 while (true) { |
229 UINT32 next_packet_size; | 229 UINT32 next_packet_size; |
230 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); | 230 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); |
231 if (FAILED(hr)) | 231 if (FAILED(hr)) |
232 break; | 232 break; |
233 | 233 |
234 if (next_packet_size <= 0) { | 234 if (next_packet_size <= 0) { |
235 return; | 235 return; |
236 } | 236 } |
237 | 237 |
238 BYTE* data; | 238 BYTE* data; |
239 UINT32 frames; | 239 UINT32 frames; |
240 DWORD flags; | 240 DWORD flags; |
241 hr = audio_capture_client_->GetBuffer(&data, &frames, &flags, NULL, NULL); | 241 hr = audio_capture_client_->GetBuffer(&data, &frames, &flags, nullptr, |
| 242 nullptr); |
242 if (FAILED(hr)) | 243 if (FAILED(hr)) |
243 break; | 244 break; |
244 | 245 |
245 if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0 && | 246 if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0 && |
246 !silence_detector_.IsSilence( | 247 !silence_detector_.IsSilence( |
247 reinterpret_cast<const int16*>(data), frames * kChannels)) { | 248 reinterpret_cast<const int16*>(data), frames * kChannels)) { |
248 scoped_ptr<AudioPacket> packet(new AudioPacket()); | 249 scoped_ptr<AudioPacket> packet(new AudioPacket()); |
249 packet->add_data(data, frames * wave_format_ex_->nBlockAlign); | 250 packet->add_data(data, frames * wave_format_ex_->nBlockAlign); |
250 packet->set_encoding(AudioPacket::ENCODING_RAW); | 251 packet->set_encoding(AudioPacket::ENCODING_RAW); |
251 packet->set_sampling_rate(sampling_rate_); | 252 packet->set_sampling_rate(sampling_rate_); |
(...skipping 23 matching lines...) Expand all Loading... |
275 | 276 |
276 bool AudioCapturer::IsSupported() { | 277 bool AudioCapturer::IsSupported() { |
277 return true; | 278 return true; |
278 } | 279 } |
279 | 280 |
280 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 281 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
281 return make_scoped_ptr(new AudioCapturerWin()); | 282 return make_scoped_ptr(new AudioCapturerWin()); |
282 } | 283 } |
283 | 284 |
284 } // namespace remoting | 285 } // namespace remoting |
OLD | NEW |