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 <windows.h> | 5 #include <windows.h> |
6 #include <audioclient.h> | 6 #include <audioclient.h> |
7 #include <avrt.h> | 7 #include <avrt.h> |
8 #include <mmdeviceapi.h> | 8 #include <mmdeviceapi.h> |
9 #include <mmreg.h> | 9 #include <mmreg.h> |
10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "base/win/scoped_co_mem.h" | 17 #include "base/win/scoped_co_mem.h" |
18 #include "base/win/scoped_com_initializer.h" | 18 #include "base/win/scoped_com_initializer.h" |
19 #include "base/win/scoped_comptr.h" | 19 #include "base/win/scoped_comptr.h" |
20 #include "remoting/host/audio_capturer.h" | 20 #include "remoting/host/audio_capturer.h" |
| 21 #include "remoting/host/audio_capturer_util.h" |
21 #include "remoting/proto/audio.pb.h" | 22 #include "remoting/proto/audio.pb.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 const int kChannels = 2; | 25 const int kChannels = 2; |
25 const int kBitsPerSample = 16; | 26 const int kBitsPerSample = 16; |
26 const int kBitsPerByte = 8; | 27 const int kBitsPerByte = 8; |
27 // Conversion factor from 100ns to 1ms. | 28 // Conversion factor from 100ns to 1ms. |
28 const int kHnsToMs = 10000; | 29 const int kHnsToMs = 10000; |
29 } // namespace | 30 } // namespace |
30 | 31 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 if (FAILED(hr)) { | 127 if (FAILED(hr)) { |
127 LOG(ERROR) << "Failed to get WAVEFORMATEX. Error " << hr; | 128 LOG(ERROR) << "Failed to get WAVEFORMATEX. Error " << hr; |
128 return false; | 129 return false; |
129 } | 130 } |
130 | 131 |
131 // Set the wave format | 132 // Set the wave format |
132 switch (wave_format_ex_->wFormatTag) { | 133 switch (wave_format_ex_->wFormatTag) { |
133 case WAVE_FORMAT_IEEE_FLOAT: | 134 case WAVE_FORMAT_IEEE_FLOAT: |
134 // Intentional fall-through. | 135 // Intentional fall-through. |
135 case WAVE_FORMAT_PCM: | 136 case WAVE_FORMAT_PCM: |
136 if (!AudioCapturer::IsValidSampleRate(wave_format_ex_->nSamplesPerSec)) { | 137 if (!IsValidSampleRate(wave_format_ex_->nSamplesPerSec)) { |
137 LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz."; | 138 LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz."; |
138 return false; | 139 return false; |
139 } | 140 } |
140 sampling_rate_ = static_cast<AudioPacket::SamplingRate>( | 141 sampling_rate_ = static_cast<AudioPacket::SamplingRate>( |
141 wave_format_ex_->nSamplesPerSec); | 142 wave_format_ex_->nSamplesPerSec); |
142 | 143 |
143 wave_format_ex_->wFormatTag = WAVE_FORMAT_PCM; | 144 wave_format_ex_->wFormatTag = WAVE_FORMAT_PCM; |
144 wave_format_ex_->nChannels = kChannels; | 145 wave_format_ex_->nChannels = kChannels; |
145 wave_format_ex_->wBitsPerSample = kBitsPerSample; | 146 wave_format_ex_->wBitsPerSample = kBitsPerSample; |
146 wave_format_ex_->nBlockAlign = kChannels * kBitsPerSample / kBitsPerByte; | 147 wave_format_ex_->nBlockAlign = kChannels * kBitsPerSample / kBitsPerByte; |
147 wave_format_ex_->nAvgBytesPerSec = | 148 wave_format_ex_->nAvgBytesPerSec = |
148 sampling_rate_ * kChannels * kBitsPerSample / kBitsPerByte; | 149 sampling_rate_ * kChannels * kBitsPerSample / kBitsPerByte; |
149 break; | 150 break; |
150 case WAVE_FORMAT_EXTENSIBLE: { | 151 case WAVE_FORMAT_EXTENSIBLE: { |
151 PWAVEFORMATEXTENSIBLE wave_format_extensible = | 152 PWAVEFORMATEXTENSIBLE wave_format_extensible = |
152 reinterpret_cast<WAVEFORMATEXTENSIBLE*>( | 153 reinterpret_cast<WAVEFORMATEXTENSIBLE*>( |
153 static_cast<WAVEFORMATEX*>(wave_format_ex_)); | 154 static_cast<WAVEFORMATEX*>(wave_format_ex_)); |
154 if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, | 155 if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, |
155 wave_format_extensible->SubFormat)) { | 156 wave_format_extensible->SubFormat)) { |
156 if (!AudioCapturer::IsValidSampleRate( | 157 if (!IsValidSampleRate(wave_format_extensible->Format.nSamplesPerSec)) { |
157 wave_format_extensible->Format.nSamplesPerSec)) { | |
158 LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz."; | 158 LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz."; |
159 return false; | 159 return false; |
160 } | 160 } |
161 sampling_rate_ = static_cast<AudioPacket::SamplingRate>( | 161 sampling_rate_ = static_cast<AudioPacket::SamplingRate>( |
162 wave_format_extensible->Format.nSamplesPerSec); | 162 wave_format_extensible->Format.nSamplesPerSec); |
163 | 163 |
164 wave_format_extensible->SubFormat = KSDATAFORMAT_SUBTYPE_PCM; | 164 wave_format_extensible->SubFormat = KSDATAFORMAT_SUBTYPE_PCM; |
165 wave_format_extensible->Samples.wValidBitsPerSample = kBitsPerSample; | 165 wave_format_extensible->Samples.wValidBitsPerSample = kBitsPerSample; |
166 | 166 |
167 wave_format_extensible->Format.nChannels = kChannels; | 167 wave_format_extensible->Format.nChannels = kChannels; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 230 |
231 thread_checker_.DetachFromThread(); | 231 thread_checker_.DetachFromThread(); |
232 } | 232 } |
233 | 233 |
234 bool AudioCapturerWin::IsRunning() { | 234 bool AudioCapturerWin::IsRunning() { |
235 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
236 return capture_timer_.get() != NULL; | 236 return capture_timer_.get() != NULL; |
237 } | 237 } |
238 | 238 |
239 void AudioCapturerWin::DoCapture() { | 239 void AudioCapturerWin::DoCapture() { |
240 DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_)); | 240 DCHECK(IsValidSampleRate(sampling_rate_)); |
241 DCHECK(thread_checker_.CalledOnValidThread()); | 241 DCHECK(thread_checker_.CalledOnValidThread()); |
242 DCHECK(IsRunning()); | 242 DCHECK(IsRunning()); |
243 | 243 |
244 // Fetch all packets from the audio capture endpoint buffer. | 244 // Fetch all packets from the audio capture endpoint buffer. |
245 while (true) { | 245 while (true) { |
246 UINT32 next_packet_size; | 246 UINT32 next_packet_size; |
247 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); | 247 HRESULT hr = audio_capture_client_->GetNextPacketSize(&next_packet_size); |
248 if (FAILED(hr)) { | 248 if (FAILED(hr)) { |
249 LOG(ERROR) << "Failed to GetNextPacketSize. Error " << hr; | 249 LOG(ERROR) << "Failed to GetNextPacketSize. Error " << hr; |
250 return; | 250 return; |
(...skipping 10 matching lines...) Expand all Loading... |
261 &data, &frames, &flags, NULL, NULL); | 261 &data, &frames, &flags, NULL, NULL); |
262 if (FAILED(hr)) { | 262 if (FAILED(hr)) { |
263 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; | 263 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; |
264 return; | 264 return; |
265 } | 265 } |
266 | 266 |
267 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); | 267 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); |
268 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); | 268 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); |
269 packet->set_sampling_rate(sampling_rate_); | 269 packet->set_sampling_rate(sampling_rate_); |
270 | 270 |
271 callback_.Run(packet.Pass()); | 271 if (!IsPacketOfSilence(packet.get())) |
| 272 callback_.Run(packet.Pass()); |
272 | 273 |
273 hr = audio_capture_client_->ReleaseBuffer(frames); | 274 hr = audio_capture_client_->ReleaseBuffer(frames); |
274 if (FAILED(hr)) { | 275 if (FAILED(hr)) { |
275 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 276 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
276 return; | 277 return; |
277 } | 278 } |
278 } | 279 } |
279 } | 280 } |
280 | 281 |
281 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 282 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
282 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 283 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
283 } | 284 } |
284 | 285 |
285 } // namespace remoting | 286 } // namespace remoting |
OLD | NEW |