Skip to content

Commit

Permalink
Added support for multiple sampling rates on Windows.
Browse files Browse the repository at this point in the history
Review URL: https://chromiumcodereview.appspot.com/10818010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148766 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kxing@chromium.org committed Jul 27, 2012
1 parent 09771af commit ca14889
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
21 changes: 21 additions & 0 deletions remoting/host/audio_capturer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/audio_capturer.h"

#include "remoting/proto/audio.pb.h"

namespace remoting {

bool AudioCapturer::IsValidSampleRate(int sample_rate) {
switch (sample_rate) {
case AudioPacket::SAMPLING_RATE_44100:
case AudioPacket::SAMPLING_RATE_48000:
return true;
default:
return false;
}
}

} // namespace remoting
9 changes: 6 additions & 3 deletions remoting/host/audio_capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "remoting/proto/audio.pb.h"

namespace remoting {

class AudioPacket;

class AudioCapturer {
public:
typedef base::Callback<void(scoped_ptr<AudioPacket> packet)>
Expand All @@ -20,15 +21,17 @@ class AudioCapturer {

static scoped_ptr<AudioCapturer> Create();

// Capturers should sample at a 44.1 kHz sampling rate, in uncompressed PCM
// stereo format. Capturers may choose the number of frames per packet.
// Capturers should sample at a 44.1 or 48 kHz sampling rate, in uncompressed
// PCM stereo format. Capturers may choose the number of frames per packet.
// Returns true on success.
virtual bool Start(const PacketCapturedCallback& callback) = 0;
// Stops the audio capturer, and frees the OS-specific audio capture
// resources.
virtual void Stop() = 0;
// Returns true if the audio capturer is running.
virtual bool IsRunning() = 0;

static bool IsValidSampleRate(int sample_rate);
};

} // namespace remoting
Expand Down
34 changes: 26 additions & 8 deletions remoting/host/audio_capturer_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
namespace {
const int kChannels = 2;
const int kBitsPerSample = 16;
const int kSamplesPerSecond = 44100;
const int kBitsPerByte = 8;
// Conversion factor from 100ns to 1ms.
const int kHnsToMs = 10000;
Expand All @@ -48,6 +47,8 @@ class AudioCapturerWin : public AudioCapturer {

PacketCapturedCallback callback_;

AudioPacket::SamplingRate sampling_rate_;

scoped_ptr<base::RepeatingTimer<AudioCapturerWin> > capture_timer_;
base::TimeDelta audio_device_period_;

Expand All @@ -62,8 +63,9 @@ class AudioCapturerWin : public AudioCapturer {
DISALLOW_COPY_AND_ASSIGN(AudioCapturerWin);
};

AudioCapturerWin::AudioCapturerWin() {
thread_checker_.DetachFromThread();
AudioCapturerWin::AudioCapturerWin()
: sampling_rate_(AudioPacket::SAMPLING_RATE_INVALID) {
thread_checker_.DetachFromThread();
}

AudioCapturerWin::~AudioCapturerWin() {
Expand Down Expand Up @@ -131,38 +133,52 @@ bool AudioCapturerWin::Start(const PacketCapturedCallback& callback) {
case WAVE_FORMAT_IEEE_FLOAT:
// Intentional fall-through.
case WAVE_FORMAT_PCM:
if (!AudioCapturer::IsValidSampleRate(wave_format_ex_->nSamplesPerSec)) {
LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz.";
return false;
}
sampling_rate_ = static_cast<AudioPacket::SamplingRate>(
wave_format_ex_->nSamplesPerSec);

wave_format_ex_->wFormatTag = WAVE_FORMAT_PCM;
wave_format_ex_->nChannels = kChannels;
wave_format_ex_->nSamplesPerSec = kSamplesPerSecond;
wave_format_ex_->wBitsPerSample = kBitsPerSample;
wave_format_ex_->nBlockAlign = kChannels * kBitsPerSample / kBitsPerByte;
wave_format_ex_->nAvgBytesPerSec =
kSamplesPerSecond * kChannels * kBitsPerSample / kBitsPerByte;
sampling_rate_ * kChannels * kBitsPerSample / kBitsPerByte;
break;
case WAVE_FORMAT_EXTENSIBLE: {
PWAVEFORMATEXTENSIBLE wave_format_extensible =
reinterpret_cast<WAVEFORMATEXTENSIBLE*>(
static_cast<WAVEFORMATEX*>(wave_format_ex_));
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
wave_format_extensible->SubFormat)) {
if (!AudioCapturer::IsValidSampleRate(
wave_format_extensible->Format.nSamplesPerSec)) {
LOG(ERROR) << "Host sampling rate is neither 44.1 kHz nor 48 kHz.";
return false;
}
sampling_rate_ = static_cast<AudioPacket::SamplingRate>(
wave_format_extensible->Format.nSamplesPerSec);

wave_format_extensible->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wave_format_extensible->Samples.wValidBitsPerSample = kBitsPerSample;

wave_format_extensible->Format.nChannels = kChannels;
wave_format_extensible->Format.nSamplesPerSec = kSamplesPerSecond;
wave_format_extensible->Format.nSamplesPerSec = sampling_rate_;
wave_format_extensible->Format.wBitsPerSample = kBitsPerSample;
wave_format_extensible->Format.nBlockAlign =
kChannels * kBitsPerSample / kBitsPerByte;
wave_format_extensible->Format.nAvgBytesPerSec =
kSamplesPerSecond * kChannels * kBitsPerSample / kBitsPerByte;
sampling_rate_ * kChannels * kBitsPerSample / kBitsPerByte;
} else {
LOG(ERROR) << "Failed to force 16-bit samples";
return false;
}
break;
}
default:
LOG(ERROR) << "Failed to force 16-bit samples";
LOG(ERROR) << "Failed to force 16-bit PCM";
return false;
}

Expand Down Expand Up @@ -221,6 +237,7 @@ bool AudioCapturerWin::IsRunning() {
}

void AudioCapturerWin::DoCapture() {
DCHECK(AudioCapturer::IsValidSampleRate(sampling_rate_));
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(IsRunning());

Expand Down Expand Up @@ -249,6 +266,7 @@ 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_);

callback_.Run(packet.Pass());

Expand Down
1 change: 1 addition & 0 deletions remoting/remoting.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@
'../crypto/crypto.gyp:crypto',
],
'sources': [
'host/audio_capturer.cc',
'host/audio_capturer.h',
'host/audio_capturer_linux.cc',
'host/audio_capturer_mac.cc',
Expand Down

0 comments on commit ca14889

Please sign in to comment.