| 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> | |
| 7 #include <avrt.h> | 8 #include <avrt.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 <algorithm> | 12 #include <algorithm> |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | |
| 16 #include "base/logging.h" | 15 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/message_loop.h" | |
| 19 #include "base/timer.h" | |
| 20 #include "base/win/scoped_co_mem.h" | |
| 21 #include "base/win/scoped_com_initializer.h" | |
| 22 #include "base/win/scoped_comptr.h" | |
| 23 #include "remoting/host/audio_capturer.h" | |
| 24 #include "remoting/proto/audio.pb.h" | |
| 25 | 16 |
| 26 namespace { | 17 namespace { |
| 27 const int kChannels = 2; | 18 const int kChannels = 2; |
| 28 const int kBitsPerSample = 16; | 19 const int kBitsPerSample = 16; |
| 29 const int kBitsPerByte = 8; | 20 const int kBitsPerByte = 8; |
| 30 // Conversion factor from 100ns to 1ms. | 21 // Conversion factor from 100ns to 1ms. |
| 31 const int k100nsPerMillisecond = 10000; | 22 const int k100nsPerMillisecond = 10000; |
| 32 | 23 |
| 33 // Tolerance for catching packets of silence. If all samples have absolute | 24 // Tolerance for catching packets of silence. If all samples have absolute |
| 34 // value less than this threshold, the packet will be counted as a packet of | 25 // value less than this threshold, the packet will be counted as a packet of |
| 35 // silence. A value of 2 was chosen, because Windows can give samples of 1 and | 26 // silence. A value of 2 was chosen, because Windows can give samples of 1 and |
| 36 // -1, even when no audio is playing. | 27 // -1, even when no audio is playing. |
| 37 const int kSilenceThreshold = 2; | 28 const int kSilenceThreshold = 2; |
| 38 | 29 |
| 39 // Lower bound for timer intervals, in milliseconds. | 30 // Lower bound for timer intervals, in milliseconds. |
| 40 const int kMinTimerInterval = 30; | 31 const int kMinTimerInterval = 30; |
| 41 | 32 |
| 42 // Upper bound for the timer precision error, in milliseconds. | 33 // 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. | 34 // Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. |
| 44 const int kMaxExpectedTimerLag = 30; | 35 const int kMaxExpectedTimerLag = 30; |
| 45 } // namespace | 36 } // namespace |
| 46 | 37 |
| 47 namespace remoting { | 38 namespace remoting { |
| 48 | 39 |
| 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() | 40 AudioCapturerWin::AudioCapturerWin() |
| 85 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) { | 41 : sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) { |
| 86 thread_checker_.DetachFromThread(); | 42 thread_checker_.DetachFromThread(); |
| 87 } | 43 } |
| 88 | 44 |
| 89 AudioCapturerWin::~AudioCapturerWin() { | 45 AudioCapturerWin::~AudioCapturerWin() { |
| 90 } | 46 } |
| 91 | 47 |
| 92 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { | 48 bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
| 93 DCHECK(!audio_capture_client_.get()); | 49 DCHECK(!audio_capture_client_.get()); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 if (FAILED(hr)) { | 262 if (FAILED(hr)) { |
| 307 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; | 263 LOG(ERROR) << "Failed to ReleaseBuffer. Error " << hr; |
| 308 return; | 264 return; |
| 309 } | 265 } |
| 310 } | 266 } |
| 311 } | 267 } |
| 312 | 268 |
| 313 // Detects whether there is audio playing in a packet of samples. | 269 // 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 | 270 // Windows can give nonzero samples, even when there is no audio playing, so |
| 315 // extremely low amplitude samples are counted as silence. | 271 // extremely low amplitude samples are counted as silence. |
| 272 // static |
| 316 bool AudioCapturerWin::IsPacketOfSilence( | 273 bool AudioCapturerWin::IsPacketOfSilence( |
| 317 const int16* samples, int number_of_samples) { | 274 const int16* samples, int number_of_samples) { |
| 318 for (int i = 0; i < number_of_samples; i++) { | 275 for (int i = 0; i < number_of_samples; i++) { |
| 319 if (abs(samples[i]) > kSilenceThreshold) | 276 if (abs(samples[i]) > kSilenceThreshold) |
| 320 return false; | 277 return false; |
| 321 } | 278 } |
| 322 return true; | 279 return true; |
| 323 } | 280 } |
| 324 | 281 |
| 325 scoped_ptr<AudioCapturer> AudioCapturer::Create() { | 282 scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
| 326 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); | 283 return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
| 327 } | 284 } |
| 328 | 285 |
| 329 } // namespace remoting | 286 } // namespace remoting |
| OLD | NEW |