This source file includes following definitions.
- ACTION_P3
- data_event_
- received_data
- error
- WaitForData
- OnData
- OnError
- bytes_to_write_
- OnData
- OnError
- CanRunAudioTests
- default_params_
- Create
- Create
- format
- channels
- bits_per_sample
- sample_rate
- frames_per_buffer
- CreateInputStream
- CreateDefaultAudioInputStream
- Close
- get
- Reset
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include <windows.h>
#include <mmsystem.h>
#include "base/basictypes.h"
#include "base/environment.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/test/test_timeouts.h"
#include "base/win/scoped_com_initializer.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/win/audio_low_latency_input_win.h"
#include "media/audio/win/core_audio_util_win.h"
#include "media/base/seekable_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::win::ScopedCOMInitializer;
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Gt;
using ::testing::NotNull;
namespace media {
ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) {
if (++*count >= limit) {
loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
}
}
class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
public:
MOCK_METHOD5(OnData, void(AudioInputStream* stream,
const uint8* src, uint32 size,
uint32 hardware_delay_bytes, double volume));
MOCK_METHOD1(OnError, void(AudioInputStream* stream));
};
class FakeAudioInputCallback : public AudioInputStream::AudioInputCallback {
public:
FakeAudioInputCallback()
: error_(false),
data_event_(false, false) {
}
const std::vector<uint8>& received_data() const { return received_data_; }
bool error() const { return error_; }
void WaitForData() {
data_event_.Wait();
}
virtual void OnData(AudioInputStream* stream,
const uint8* src, uint32 size,
uint32 hardware_delay_bytes, double volume) OVERRIDE {
received_data_.insert(received_data_.end(), src, src + size);
data_event_.Signal();
}
virtual void OnError(AudioInputStream* stream) OVERRIDE {
error_ = true;
}
private:
std::vector<uint8> received_data_;
base::WaitableEvent data_event_;
bool error_;
DISALLOW_COPY_AND_ASSIGN(FakeAudioInputCallback);
};
class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback {
public:
static const size_t kMaxBufferSize = 2 * 2 * 480 * 100 * 10;
explicit WriteToFileAudioSink(const char* file_name)
: buffer_(0, kMaxBufferSize),
bytes_to_write_(0) {
base::FilePath file_path;
EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_path));
file_path = file_path.AppendASCII(file_name);
binary_file_ = base::OpenFile(file_path, "wb");
DLOG_IF(ERROR, !binary_file_) << "Failed to open binary PCM data file.";
VLOG(0) << ">> Output file: " << file_path.value() << " has been created.";
}
virtual ~WriteToFileAudioSink() {
size_t bytes_written = 0;
while (bytes_written < bytes_to_write_) {
const uint8* chunk;
int chunk_size;
if (!buffer_.GetCurrentChunk(&chunk, &chunk_size))
break;
fwrite(chunk, 1, chunk_size, binary_file_);
buffer_.Seek(chunk_size);
bytes_written += chunk_size;
}
base::CloseFile(binary_file_);
}
virtual void OnData(AudioInputStream* stream,
const uint8* src,
uint32 size,
uint32 hardware_delay_bytes,
double volume) {
if (buffer_.Append(src, size)) {
bytes_to_write_ += size;
}
}
virtual void OnError(AudioInputStream* stream) {}
private:
media::SeekableBuffer buffer_;
FILE* binary_file_;
size_t bytes_to_write_;
};
static bool CanRunAudioTests(AudioManager* audio_man) {
if (!CoreAudioUtil::IsSupported()) {
LOG(WARNING) << "This tests requires Windows Vista or higher.";
return false;
}
bool input = audio_man->HasAudioInputDevices();
LOG_IF(WARNING, !input) << "No input device detected.";
return input;
}
class AudioInputStreamWrapper {
public:
explicit AudioInputStreamWrapper(AudioManager* audio_manager)
: com_init_(ScopedCOMInitializer::kMTA),
audio_man_(audio_manager),
default_params_(
audio_manager->GetInputStreamParameters(
AudioManagerBase::kDefaultDeviceId)) {
EXPECT_EQ(format(), AudioParameters::AUDIO_PCM_LOW_LATENCY);
frames_per_buffer_ = default_params_.frames_per_buffer();
EXPECT_EQ(frames_per_buffer_, sample_rate() / 100);
}
~AudioInputStreamWrapper() {}
AudioInputStream* Create() {
return CreateInputStream();
}
AudioInputStream* Create(int frames_per_buffer) {
frames_per_buffer_ = frames_per_buffer;
return CreateInputStream();
}
AudioParameters::Format format() const { return default_params_.format(); }
int channels() const {
return ChannelLayoutToChannelCount(default_params_.channel_layout());
}
int bits_per_sample() const { return default_params_.bits_per_sample(); }
int sample_rate() const { return default_params_.sample_rate(); }
int frames_per_buffer() const { return frames_per_buffer_; }
private:
AudioInputStream* CreateInputStream() {
AudioInputStream* ais = audio_man_->MakeAudioInputStream(
AudioParameters(format(), default_params_.channel_layout(),
default_params_.input_channels(),
sample_rate(), bits_per_sample(), frames_per_buffer_,
default_params_.effects()),
AudioManagerBase::kDefaultDeviceId);
EXPECT_TRUE(ais);
return ais;
}
ScopedCOMInitializer com_init_;
AudioManager* audio_man_;
const AudioParameters default_params_;
int frames_per_buffer_;
};
static AudioInputStream* CreateDefaultAudioInputStream(
AudioManager* audio_manager) {
AudioInputStreamWrapper aisw(audio_manager);
AudioInputStream* ais = aisw.Create();
return ais;
}
class ScopedAudioInputStream {
public:
explicit ScopedAudioInputStream(AudioInputStream* stream)
: stream_(stream) {}
~ScopedAudioInputStream() {
if (stream_)
stream_->Close();
}
void Close() {
if (stream_)
stream_->Close();
stream_ = NULL;
}
AudioInputStream* operator->() {
return stream_;
}
AudioInputStream* get() const { return stream_; }
void Reset(AudioInputStream* new_stream) {
Close();
stream_ = new_stream;
}
private:
AudioInputStream* stream_;
DISALLOW_COPY_AND_ASSIGN(ScopedAudioInputStream);
};
TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
media::AudioDeviceNames device_names;
audio_manager->GetAudioInputDeviceNames(&device_names);
for (media::AudioDeviceNames::const_iterator it = device_names.begin();
it != device_names.end(); ++it) {
AudioParameters params = WASAPIAudioInputStream::GetInputStreamParameters(
it->unique_id);
EXPECT_GE(params.sample_rate(), 0);
}
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedAudioInputStream ais(
CreateDefaultAudioInputStream(audio_manager.get()));
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedAudioInputStream ais(
CreateDefaultAudioInputStream(audio_manager.get()));
EXPECT_TRUE(ais->Open());
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedAudioInputStream ais(
CreateDefaultAudioInputStream(audio_manager.get()));
EXPECT_TRUE(ais->Open());
MockAudioInputCallback sink;
ais->Start(&sink);
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedAudioInputStream ais(
CreateDefaultAudioInputStream(audio_manager.get()));
EXPECT_TRUE(ais->Open());
MockAudioInputCallback sink;
ais->Start(&sink);
ais->Stop();
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
ScopedAudioInputStream ais(
CreateDefaultAudioInputStream(audio_manager.get()));
WASAPIAudioInputStream* wais =
static_cast<WASAPIAudioInputStream*>(ais.get());
EXPECT_TRUE(ais->Open());
EXPECT_FALSE(ais->Open());
MockAudioInputCallback sink;
ais->Start(&sink);
EXPECT_TRUE(wais->started());
ais->Start(&sink);
EXPECT_TRUE(wais->started());
ais->Stop();
EXPECT_FALSE(wais->started());
ais->Stop();
EXPECT_FALSE(wais->started());
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
int count = 0;
base::MessageLoopForUI loop;
AudioInputStreamWrapper aisw(audio_manager.get());
ScopedAudioInputStream ais(aisw.Create());
EXPECT_TRUE(ais->Open());
MockAudioInputCallback sink;
uint32 bytes_per_packet = aisw.channels() * aisw.frames_per_buffer() *
(aisw.bits_per_sample() / 8);
EXPECT_CALL(sink, OnData(
ais.get(), NotNull(), bytes_per_packet, Gt(bytes_per_packet), _))
.Times(AtLeast(10))
.WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop));
ais->Start(&sink);
loop.Run();
ais->Stop();
int frames_per_buffer_10ms = aisw.frames_per_buffer();
ais.Close();
count = 0;
ais.Reset(aisw.Create(2 * frames_per_buffer_10ms));
EXPECT_TRUE(ais->Open());
bytes_per_packet = aisw.channels() * aisw.frames_per_buffer() *
(aisw.bits_per_sample() / 8);
EXPECT_CALL(sink, OnData(
ais.get(), NotNull(), bytes_per_packet, Gt(bytes_per_packet), _))
.Times(AtLeast(10))
.WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop));
ais->Start(&sink);
loop.Run();
ais->Stop();
ais.Close();
count = 0;
ais.Reset(aisw.Create(frames_per_buffer_10ms / 2));
EXPECT_TRUE(ais->Open());
bytes_per_packet = aisw.channels() * aisw.frames_per_buffer() *
(aisw.bits_per_sample() / 8);
EXPECT_CALL(sink, OnData(
ais.get(), NotNull(), bytes_per_packet, Gt(bytes_per_packet), _))
.Times(AtLeast(10))
.WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop));
ais->Start(&sink);
loop.Run();
ais->Stop();
ais.Close();
}
TEST(WinAudioInputTest, WASAPIAudioInputStreamLoopback) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!audio_manager->HasAudioOutputDevices() || !CoreAudioUtil::IsSupported())
return;
AudioParameters params = audio_manager->GetInputStreamParameters(
AudioManagerBase::kLoopbackInputDeviceId);
EXPECT_EQ(params.effects(), 0);
AudioParameters output_params =
audio_manager->GetOutputStreamParameters(std::string());
EXPECT_EQ(params.sample_rate(), output_params.sample_rate());
EXPECT_EQ(params.channel_layout(), output_params.channel_layout());
ScopedAudioInputStream stream(audio_manager->MakeAudioInputStream(
params, AudioManagerBase::kLoopbackInputDeviceId));
ASSERT_TRUE(stream->Open());
FakeAudioInputCallback sink;
stream->Start(&sink);
ASSERT_FALSE(sink.error());
sink.WaitForData();
stream.Close();
EXPECT_FALSE(sink.received_data().empty());
EXPECT_FALSE(sink.error());
}
TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
const char* file_name = "out_stereo_10sec.pcm";
AudioInputStreamWrapper aisw(audio_manager.get());
ScopedAudioInputStream ais(aisw.Create());
EXPECT_TRUE(ais->Open());
VLOG(0) << ">> Sample rate: " << aisw.sample_rate() << " [Hz]";
WriteToFileAudioSink file_sink(file_name);
VLOG(0) << ">> Speak into the default microphone while recording.";
ais->Start(&file_sink);
base::PlatformThread::Sleep(TestTimeouts::action_timeout());
ais->Stop();
VLOG(0) << ">> Recording has stopped.";
ais.Close();
}
}