Commit 07cb35f8 authored by kxing@chromium.org's avatar kxing@chromium.org

Not streaming packets of silence.


Review URL: https://chromiumcodereview.appspot.com/10820059

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149309 0039d316-1c4b-4281-b951-d872f2087c98
parent d62c2ff1
......@@ -4,10 +4,12 @@
#include "remoting/host/audio_capturer.h"
#include "base/basictypes.h"
#include "remoting/proto/audio.pb.h"
namespace remoting {
// Returns true if the sampling rate is supported by Pepper.
bool AudioCapturer::IsValidSampleRate(int sample_rate) {
switch (sample_rate) {
case AudioPacket::SAMPLING_RATE_44100:
......
......@@ -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.
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,7 +277,10 @@ 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)));
if (!IsPacketOfSilence(packet.get()))
callback_.Run(packet.Pass());
hr = audio_capture_client_->ReleaseBuffer(frames);
......@@ -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());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment