Chromium Code Reviews| Index: remoting/host/audio_capturer_win.cc |
| diff --git a/remoting/host/audio_capturer_win.cc b/remoting/host/audio_capturer_win.cc |
| index c648a0ab0727c5c776e51a9bf66db3c4100bc8e4..43fdfd8182ab702b919b35d24fcb344e641764d8 100644 |
| --- a/remoting/host/audio_capturer_win.cc |
| +++ b/remoting/host/audio_capturer_win.cc |
| @@ -9,6 +9,7 @@ |
| #include <mmreg.h> |
| #include <mmsystem.h> |
| +#include <algorithm> |
| #include <stdlib.h> |
| #include "base/basictypes.h" |
| @@ -27,13 +28,20 @@ const int kChannels = 2; |
| const int kBitsPerSample = 16; |
| const int kBitsPerByte = 8; |
| // Conversion factor from 100ns to 1ms. |
| -const int kHnsToMs = 10000; |
| +const int k100nsInMillisecond = 10000; |
|
Wez
2012/08/16 20:20:20
nit: k100nsPerMillisecond, to avoid ambiguity over
kxing
2012/08/16 20:38:46
Done.
|
| // Tolerance for catching packets of silence. If all samples have absolute |
| // value less than this threshold, the packet will be counted as a packet of |
| // silence. A value of 2 was chosen, because Windows can give samples of 1 and |
| // -1, even when no audio is playing. |
| const int kSilenceThreshold = 2; |
| + |
| +// Lower bound for timer intervals, in milliseconds. |
| +const int kMinTimerInterval = 30; |
| + |
| +// Upper bound for the timer precision error, in milliseconds. |
| +// Timers are supposed to be accurate to 20ms, so we use 30ms to be safe. |
| +const int kMaxExpectedTimerLag = 30; |
| } // namespace |
| namespace remoting { |
| @@ -128,8 +136,12 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
| LOG(ERROR) << "IAudioClient::GetDevicePeriod failed. Error " << hr; |
| return false; |
| } |
| + // We round up, if |device_period| / |k100nsInMillisecond| |
| + // is not a whole number. |
| + int device_period_in_milliseconds = |
| + 1 + (device_period - 1) / k100nsInMillisecond; |
|
Wez
2012/08/16 20:20:20
nit: Bracket the division to make it clear to the
kxing
2012/08/16 20:38:46
Done.
|
| audio_device_period_ = base::TimeDelta::FromMilliseconds( |
| - device_period / kChannels / kHnsToMs); |
| + std::max(device_period_in_milliseconds, kMinTimerInterval)); |
| // Get the wave format. |
| hr = audio_client_->GetMixFormat(&wave_format_ex_); |
| @@ -193,12 +205,14 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) { |
| } |
| // Initialize the IAudioClient. |
| - hr = audio_client_->Initialize(AUDCLNT_SHAREMODE_SHARED, |
| - AUDCLNT_STREAMFLAGS_LOOPBACK, |
| - 0, |
| - 0, |
| - wave_format_ex_, |
| - NULL); |
| + hr = audio_client_->Initialize( |
| + AUDCLNT_SHAREMODE_SHARED, |
| + AUDCLNT_STREAMFLAGS_LOOPBACK, |
| + (kMaxExpectedTimerLag + audio_device_period_.InMilliseconds()) * |
| + k100nsInMillisecond, |
| + 0, |
| + wave_format_ex_, |
| + NULL); |
| if (FAILED(hr)) { |
| LOG(ERROR) << "Failed to initialize IAudioClient. Error " << hr; |
| return false; |
| @@ -275,8 +289,8 @@ void AudioCapturerWin::DoCapture() { |
| } |
| if (!IsPacketOfSilence( |
| - reinterpret_cast<const int16*>(data), |
| - frames * kChannels)) { |
| + reinterpret_cast<const int16*>(data), |
| + frames * kChannels)) { |
| scoped_ptr<AudioPacket> packet = |
| scoped_ptr<AudioPacket>(new AudioPacket()); |
| packet->add_data(data, frames * wave_format_ex_->nBlockAlign); |