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 8a256454fac9362d4a5774fb85a00c1cc1cd4328..5ea6bc34ea9e3b0d00d96b0a3e880aba0d49a147 100644 |
| --- a/remoting/host/audio_capturer_win.cc |
| +++ b/remoting/host/audio_capturer_win.cc |
| @@ -9,6 +9,8 @@ |
| #include <mmreg.h> |
| #include <mmsystem.h> |
| +#include <stdlib.h> |
| + |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| @@ -26,6 +28,12 @@ const int kBitsPerSample = 16; |
| const int kBitsPerByte = 8; |
| // Conversion factor from 100ns to 1ms. |
| const int kHnsToMs = 10000; |
| + |
| +// 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. |
|
Wez
2012/08/07 18:01:37
This doesn't sound right; a packet of frames all o
|
| +const int kSilenceThreshold = 2; |
| } // namespace |
| namespace remoting { |
| @@ -45,6 +53,8 @@ class AudioCapturerWin : public AudioCapturer { |
| // to the network. |
| void DoCapture(); |
| + static bool IsPacketOfSilence(const AudioPacket* packet); |
| + |
| PacketCapturedCallback callback_; |
| AudioPacket::SamplingRate sampling_rate_; |
| @@ -267,8 +277,11 @@ void AudioCapturerWin::DoCapture() { |
| scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); |
| packet->set_data(data, frames * wave_format_ex_->nBlockAlign); |
| packet->set_sampling_rate(sampling_rate_); |
| + packet->set_bytes_per_sample( |
| + static_cast<AudioPacket::BytesPerSample>(sizeof(int16))); |
| - callback_.Run(packet.Pass()); |
| + if (!IsPacketOfSilence(packet.get())) |
| + callback_.Run(packet.Pass()); |
| hr = audio_capture_client_->ReleaseBuffer(frames); |
| if (FAILED(hr)) { |
| @@ -278,6 +291,22 @@ void AudioCapturerWin::DoCapture() { |
| } |
| } |
| +// Detects whether there is audio playing in a packet of samples. |
| +// Windows can give nonzero samples, even when there is no audio playing, so |
| +// extremely low amplitude samples are counted as silence. |
| +bool AudioCapturerWin::IsPacketOfSilence(const AudioPacket* packet) { |
| + DCHECK_EQ(static_cast<AudioPacket::BytesPerSample>(sizeof(int16)), |
| + packet->bytes_per_sample()); |
| + const int16* data = reinterpret_cast<const int16*>(packet->data().data()); |
| + int number_of_samples = packet->data().size() * kBitsPerByte / kBitsPerSample; |
| + |
| + for (int i = 0; i < number_of_samples; i++) { |
| + if (abs(data[i]) > kSilenceThreshold) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| scoped_ptr<AudioCapturer> AudioCapturer::Create() { |
| return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); |
| } |