Chromium Code Reviews| 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" | |
| 6 | |
| 5 #include <windows.h> | 7 #include <windows.h> |
| 6 #include <audioclient.h> | 8 #include <audioclient.h> |
|
Sergey Ulanov
2012/08/21 21:34:39
Don't need to include files that are already inclu
kxing
2012/08/21 21:55:53
Done.
| |
| 7 #include <avrt.h> | 9 #include <avrt.h> |
| 8 #include <mmdeviceapi.h> | 10 #include <mmdeviceapi.h> |
| 9 #include <mmreg.h> | 11 #include <mmreg.h> |
| 10 #include <mmsystem.h> | 12 #include <mmsystem.h> |
| 11 | 13 |
| 12 #include <algorithm> | 14 #include <algorithm> |
| 13 #include <stdlib.h> | 15 #include <stdlib.h> |
| 14 | 16 |
| 15 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 16 #include "base/logging.h" | 18 #include "base/logging.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 39 // Lower bound for timer intervals, in milliseconds. | 41 // Lower bound for timer intervals, in milliseconds. |
| 40 const int kMinTimerInterval = 30; | 42 const int kMinTimerInterval = 30; |
| 41 | 43 |
| 42 // Upper bound for the timer precision error, in milliseconds. | 44 // Upper bound for the timer precision error, in milliseconds. |
| 43 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. | 45 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. |
| 44 const int kMaxExpectedTimerLag = 30; | 46 const int kMaxExpectedTimerLag = 30; |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 namespace remoting { | 49 namespace remoting { |
| 48 | 50 |
| 49 class AudioCapturerWin : public AudioCapturer { | |
| 50 public: | |
| 51 AudioCapturerWin(); | |
| 52 virtual ~AudioCapturerWin(); | |
| 53 | |
| 54 // AudioCapturer interface. | |
| 55 virtual bool Start(const PacketCapturedCallback& callback) OVERRIDE; | |
| 56 virtual void Stop() OVERRIDE; | |
| 57 virtual bool IsRunning() OVERRIDE; | |
| 58 | |
| 59 private: | |
| 60 // Receives all packets from the audio capture endpoint buffer and pushes them | |
| 61 // to the network. | |
| 62 void DoCapture(); | |
| 63 | |
| 64 static bool IsPacketOfSilence(const int16* samples, int number_of_samples); | |
| 65 | |
| 66 PacketCapturedCallback callback_; | |
| 67 | |
| 68 AudioPacket::SamplingRate sampling_rate_; | |
| 69 | |
| 70 scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_; | |
| 71 base::TimeDelta audio_device_period_; | |
| 72 | |
| 73 base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_; | |
| 74 base::win::ScopedComPtr<IAudioCaptureClient> audio_capture_client_; | |
| 75 base::win::ScopedComPtr<IAudioClient> audio_client_; | |
| 76 base::win::ScopedComPtr<IMMDevice> mm_device_; | |
| 77 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_; | |
| 78 | |
| 79 base::ThreadChecker thread_checker_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(AudioCapturerWin); | |
| 82 }; | |
| 83 | |
| 84 AudioCapturerWin::AudioCapturerWin() | 51 AudioCapturerWin::AudioCapturerWin() |
| 85 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) { | 52 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) { |
| 86 thread_checker_.DetachFromThread(); | 53 thread_checker_.DetachFromThread(); |
| 87 } | 54 } |
| 88 | 55 |
| 89 AudioCapturerWin::~AudioCapturerWin() { | 56 AudioCapturerWin::~AudioCapturerWin() { |
| 90 } | 57 } |
| 91 | 58 |
| 92 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { | 59 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
| 93 DCHECK(!audio_capture_client_.get()); | 60 DCHECK(!audio_capture_client_.get()); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 if (FAILED(hr)) { | 273 if (FAILED(hr)) { |
| 307 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 274 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
| 308 return; | 275 return; |
| 309 } | 276 } |
| 310 } | 277 } |
| 311 } | 278 } |
| 312 | 279 |
| 313 // Detects whether there is audio playing in a packet of samples. | 280 // Detects whether there is audio playing in a packet of samples. |
| 314 // Windows can give nonzero samples, even when there is no audio playing, so | 281 // Windows can give nonzero samples, even when there is no audio playing, so |
| 315 // extremely low amplitude samples are counted as silence. | 282 // extremely low amplitude samples are counted as silence. |
| 283 // static | |
| 316 bool AudioCapturerWin::IsPacketOfSilence( | 284 bool AudioCapturerWin::IsPacketOfSilence( |
| 317 const int16* samples, int number_of_samples) { | 285 const int16* samples, int number_of_samples) { |
| 318 for (int i = 0; i < number_of_samples; i++) { | 286 for (int i = 0; i < number_of_samples; i++) { |
| 319 if (abs(samples[i]) > kSilenceThreshold) | 287 if (abs(samples[i]) > kSilenceThreshold) |
| 320 return false; | 288 return false; |
| 321 } | 289 } |
| 322 return true; | 290 return true; |
| 323 } | 291 } |
| 324 | 292 |
| 325 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 293 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
| 326 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 294 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
| 327 } | 295 } |
| 328 | 296 |
| 329 } // namespace remoting | 297 } // namespace remoting |
| OLD | NEW |