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> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 // AudioCapturer interface. | 46 // AudioCapturer interface. |
47 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; | 47 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; |
48 virtual void Stop() OVERRIDE; | 48 virtual void Stop() OVERRIDE; |
49 virtual bool IsRunning() OVERRIDE; | 49 virtual bool IsRunning() OVERRIDE; |
50 | 50 |
51 private: | 51 private: |
52 // Receives all packets from the audio capture endpoint buffer and pushes them | 52 // Receives all packets from the audio capture endpoint buffer and pushes them |
53 // to the network. | 53 // to the network. |
54 void DoCapture(); | 54 void DoCapture(); |
55 | 55 |
56 static bool IsPacketOfSilence(const AudioPacket* packet); | 56 static bool IsPacketOfSilence(const int16* samples, int number_of_samples); |
57 | 57 |
58 PacketCapturedCallback callback_; | 58 PacketCapturedCallback callback_; |
59 | 59 |
60 AudioPacket::SamplingRate sampling_rate_; | 60 AudioPacket::SamplingRate sampling_rate_; |
61 | 61 |
62 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; | 62 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; |
63 base::TimeDelta audio_device_period_; | 63 base::TimeDelta audio_device_period_; |
64 | 64 |
65 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; | 65 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; |
66 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; | 66 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 UINT32 frames; | 268 UINT32 frames; |
269 DWORD flags; | 269 DWORD flags; |
270 hr = audio_capture_client_->GetBuffer( | 270 hr = audio_capture_client_->GetBuffer( |
271 &data, &frames, &flags, NULL, NULL); | 271 &data, &frames, &flags, NULL, NULL); |
272 if (FAILED(hr)) { | 272 if (FAILED(hr)) { |
273 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; | 273 LOG(ERROR) << "Failed to GetBuffer. Error " << hr; |
274 return; | 274 return; |
275 } | 275 } |
276 | 276 |
277 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); | 277 scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); |
278 packet->set_data(data, frames * wave_format_ex_->nBlockAlign); | 278 packet->add_data(data, frames * wave_format_ex_->nBlockAlign); |
279 packet->set_sampling_rate(sampling_rate_); | 279 packet->set_sampling_rate(sampling_rate_); |
280 packet->set_bytes_per_sample( | 280 packet->set_bytes_per_sample( |
281 static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); | 281 static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); |
282 packet->set_encoding(AudioPacket::ENCODING_RAW); | 282 packet->set_encoding(AudioPacket::ENCODING_RAW); |
283 | 283 |
284 if (!IsPacketOfSilence(packet.get())) | 284 if (!IsPacketOfSilence( |
Sergey Ulanov
2012/08/15 00:47:20
Now you can move this above, before |packet| is cr
kxing
2012/08/15 16:00:05
Done.
| |
285 reinterpret_cast<const int16*>(packet->data(0).data()), | |
286 packet->data(0).size() * kBitsPerByte / kBitsPerSample)) { | |
285 callback_.Run(packet.Pass()); | 287 callback_.Run(packet.Pass()); |
288 } | |
286 | 289 |
287 hr = audio_capture_client_->ReleaseBuffer(frames); | 290 hr = audio_capture_client_->ReleaseBuffer(frames); |
288 if (FAILED(hr)) { | 291 if (FAILED(hr)) { |
289 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 292 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
290 return; | 293 return; |
291 } | 294 } |
292 } | 295 } |
293 } | 296 } |
294 | 297 |
295 // Detects whether there is audio playing in a packet of samples. | 298 // Detects whether there is audio playing in a packet of samples. |
296 // Windows can give nonzero samples, even when there is no audio playing, so | 299 // Windows can give nonzero samples, even when there is no audio playing, so |
297 // extremely low amplitude samples are counted as silence. | 300 // extremely low amplitude samples are counted as silence. |
298 bool AudioCapturerWin::IsPacketOfSilence(const AudioPacket* packet) { | 301 bool AudioCapturerWin::IsPacketOfSilence( |
299 DCHECK_EQ(static_cast<AudioPacket::BytesPerSample>(sizeof(int16)), | 302 const int16* samples, |
300 packet->bytes_per_sample()); | 303 int number_of_samples) { |
301 const int16* data = reinterpret_cast<const int16*>(packet->data().data()); | |
302 int number_of_samples = packet->data().size() * kBitsPerByte / kBitsPerSample; | |
303 | |
304 for (int i = 0; i < number_of_samples; i++) { | 304 for (int i = 0; i < number_of_samples; i++) { |
305 if (abs(data[i]) > kSilenceThreshold) | 305 if (abs(samples[i]) > kSilenceThreshold) |
306 return false; | 306 return false; |
307 } | 307 } |
308 return true; | 308 return true; |
309 } | 309 } |
310 | 310 |
311 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 311 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
312 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 312 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
313 } | 313 } |
314 | 314 |
315 } // namespace remoting | 315 } // namespace remoting |
OLD | NEW |