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 @@ ...@@ -4,10 +4,12 @@
#include "remoting/host/audio_capturer.h" #include "remoting/host/audio_capturer.h"
#include "base/basictypes.h"
#include "remoting/proto/audio.pb.h" #include "remoting/proto/audio.pb.h"
namespace remoting { namespace remoting {
// Returns true if the sampling rate is supported by Pepper.
bool AudioCapturer::IsValidSampleRate(int sample_rate) { bool AudioCapturer::IsValidSampleRate(int sample_rate) {
switch (sample_rate) { switch (sample_rate) {
case AudioPacket::SAMPLING_RATE_44100: case AudioPacket::SAMPLING_RATE_44100:
...@@ -18,4 +20,4 @@ bool AudioCapturer::IsValidSampleRate(int sample_rate) { ...@@ -18,4 +20,4 @@ bool AudioCapturer::IsValidSampleRate(int sample_rate) {
} }
} }
} // namespace remoting } // namespace remoting
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include <mmreg.h> #include <mmreg.h>
#include <mmsystem.h> #include <mmsystem.h>
#include <stdlib.h>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
...@@ -26,6 +28,12 @@ const int kBitsPerSample = 16; ...@@ -26,6 +28,12 @@ const int kBitsPerSample = 16;
const int kBitsPerByte = 8; const int kBitsPerByte = 8;
// Conversion factor from 100ns to 1ms. // Conversion factor from 100ns to 1ms.
const int kHnsToMs = 10000; 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
namespace remoting { namespace remoting {
...@@ -45,6 +53,8 @@ class AudioCapturerWin : public AudioCapturer { ...@@ -45,6 +53,8 @@ class AudioCapturerWin : public AudioCapturer {
// to the network. // to the network.
void DoCapture(); void DoCapture();
static bool IsPacketOfSilence(const AudioPacket* packet);
PacketCapturedCallback callback_; PacketCapturedCallback callback_;
AudioPacket::SamplingRate sampling_rate_; AudioPacket::SamplingRate sampling_rate_;
...@@ -267,8 +277,11 @@ void AudioCapturerWin::DoCapture() { ...@@ -267,8 +277,11 @@ void AudioCapturerWin::DoCapture() {
scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket()); scoped_ptr<AudioPacket> packet = scoped_ptr<AudioPacket>(new AudioPacket());
packet->set_data(data, frames * wave_format_ex_->nBlockAlign); packet->set_data(data, frames * wave_format_ex_->nBlockAlign);
packet->set_sampling_rate(sampling_rate_); 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); hr = audio_capture_client_->ReleaseBuffer(frames);
if (FAILED(hr)) { if (FAILED(hr)) {
...@@ -278,6 +291,22 @@ void AudioCapturerWin::DoCapture() { ...@@ -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() { scoped_ptr<AudioCapturer> AudioCapturer::Create() {
return scoped_ptr<AudioCapturer>(new AudioCapturerWin()); 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