This source file includes following definitions.
- MATCHER_P
- ACTION_P
- elements_to_write_
- OnMoreData
- OnMoreIOData
- OnError
- file_size
- ExclusiveModeIsEnabled
- CanRunAudioTests
- bits_per_sample_
- Create
- Create
- Create
- format
- channels
- bits_per_sample
- sample_rate
- samples_per_packet
- CreateOutputStream
- CreateDefaultAudioOutputStream
- TEST
- TEST
- TEST
- TEST
- 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/time/time.h"
#include "base/win/scoped_com_initializer.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager.h"
#include "media/audio/mock_audio_source_callback.h"
#include "media/audio/win/audio_low_latency_output_win.h"
#include "media/audio/win/core_audio_util_win.h"
#include "media/base/decoder_buffer.h"
#include "media/base/seekable_buffer.h"
#include "media/base/test_data_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gmock_mutant.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Between;
using ::testing::CreateFunctor;
using ::testing::DoAll;
using ::testing::Gt;
using ::testing::InvokeWithoutArgs;
using ::testing::NotNull;
using ::testing::Return;
using base::win::ScopedCOMInitializer;
namespace media {
static const char kSpeechFile_16b_s_48k[] = "speech_16b_stereo_48kHz.raw";
static const char kSpeechFile_16b_s_44k[] = "speech_16b_stereo_44kHz.raw";
static const size_t kFileDurationMs = 20000;
static const size_t kNumFileSegments = 2;
static const int kBitsPerSample = 16;
static const size_t kMaxDeltaSamples = 1000;
static const char kDeltaTimeMsFileName[] = "delta_times_ms.txt";
MATCHER_P(HasValidDelay, value, "") {
return arg.hardware_delay_bytes >= value.hardware_delay_bytes;
}
ACTION_P(QuitLoop, loop) {
loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
}
class ReadFromFileAudioSource : public AudioOutputStream::AudioSourceCallback {
public:
explicit ReadFromFileAudioSource(const std::string& name)
: pos_(0),
previous_call_time_(base::TimeTicks::Now()),
text_file_(NULL),
elements_to_write_(0) {
file_ = ReadTestDataFile(name);
delta_times_.reset(new int[kMaxDeltaSamples]);
}
virtual ~ReadFromFileAudioSource() {
base::FilePath file_name;
EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_name));
file_name = file_name.AppendASCII(kDeltaTimeMsFileName);
EXPECT_TRUE(!text_file_);
text_file_ = base::OpenFile(file_name, "wt");
DLOG_IF(ERROR, !text_file_) << "Failed to open log file.";
size_t elements_written = 0;
while (elements_written < elements_to_write_) {
fprintf(text_file_, "%d\n", delta_times_[elements_written]);
++elements_written;
}
base::CloseFile(text_file_);
}
virtual int OnMoreData(AudioBus* audio_bus,
AudioBuffersState buffers_state) {
const base::TimeTicks now_time = base::TimeTicks::Now();
const int diff = (now_time - previous_call_time_).InMilliseconds();
previous_call_time_ = now_time;
if (elements_to_write_ < kMaxDeltaSamples) {
delta_times_[elements_to_write_] = diff;
++elements_to_write_;
}
int max_size =
audio_bus->frames() * audio_bus->channels() * kBitsPerSample / 8;
if (pos_ + static_cast<int>(max_size) > file_size())
max_size = file_size() - pos_;
int frames = max_size / (audio_bus->channels() * kBitsPerSample / 8);
if (max_size) {
audio_bus->FromInterleaved(
file_->data() + pos_, frames, kBitsPerSample / 8);
pos_ += max_size;
}
return frames;
}
virtual int OnMoreIOData(AudioBus* source,
AudioBus* dest,
AudioBuffersState buffers_state) OVERRIDE {
NOTREACHED();
return 0;
}
virtual void OnError(AudioOutputStream* stream) {}
int file_size() { return file_->data_size(); }
private:
scoped_refptr<DecoderBuffer> file_;
scoped_ptr<int[]> delta_times_;
int pos_;
base::TimeTicks previous_call_time_;
FILE* text_file_;
size_t elements_to_write_;
};
static bool ExclusiveModeIsEnabled() {
return (WASAPIAudioOutputStream::GetShareMode() ==
AUDCLNT_SHAREMODE_EXCLUSIVE);
}
static bool CanRunAudioTests(AudioManager* audio_man) {
if (!CoreAudioUtil::IsSupported()) {
LOG(WARNING) << "This test requires Windows Vista or higher.";
return false;
}
if (!audio_man->HasAudioOutputDevices()) {
LOG(WARNING) << "No output devices detected.";
return false;
}
return true;
}
class AudioOutputStreamWrapper {
public:
explicit AudioOutputStreamWrapper(AudioManager* audio_manager)
: audio_man_(audio_manager),
format_(AudioParameters::AUDIO_PCM_LOW_LATENCY),
bits_per_sample_(kBitsPerSample) {
AudioParameters preferred_params;
EXPECT_TRUE(SUCCEEDED(CoreAudioUtil::GetPreferredAudioParameters(
eRender, eConsole, &preferred_params)));
channel_layout_ = preferred_params.channel_layout();
sample_rate_ = preferred_params.sample_rate();
samples_per_packet_ = preferred_params.frames_per_buffer();
}
~AudioOutputStreamWrapper() {}
AudioOutputStream* Create() {
return CreateOutputStream();
}
AudioOutputStream* Create(int samples_per_packet) {
samples_per_packet_ = samples_per_packet;
return CreateOutputStream();
}
AudioOutputStream* Create(int sample_rate, int samples_per_packet) {
sample_rate_ = sample_rate;
samples_per_packet_ = samples_per_packet;
return CreateOutputStream();
}
AudioParameters::Format format() const { return format_; }
int channels() const { return ChannelLayoutToChannelCount(channel_layout_); }
int bits_per_sample() const { return bits_per_sample_; }
int sample_rate() const { return sample_rate_; }
int samples_per_packet() const { return samples_per_packet_; }
private:
AudioOutputStream* CreateOutputStream() {
AudioOutputStream* aos = audio_man_->MakeAudioOutputStream(
AudioParameters(format_, channel_layout_, sample_rate_,
bits_per_sample_, samples_per_packet_),
std::string());
EXPECT_TRUE(aos);
return aos;
}
AudioManager* audio_man_;
AudioParameters::Format format_;
ChannelLayout channel_layout_;
int bits_per_sample_;
int sample_rate_;
int samples_per_packet_;
};
static AudioOutputStream* CreateDefaultAudioOutputStream(
AudioManager* audio_manager) {
AudioOutputStreamWrapper aosw(audio_manager);
AudioOutputStream* aos = aosw.Create();
return aos;
}
TEST(WASAPIAudioOutputStreamTest, HardwareSampleRate) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()) || ExclusiveModeIsEnabled())
return;
int fs = static_cast<int>(
WASAPIAudioOutputStream::HardwareSampleRate(std::string()));
EXPECT_GE(fs, 0);
}
TEST(WASAPIAudioOutputStreamTest, CreateAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, OpenAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
EXPECT_TRUE(aos->Open());
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, OpenStartAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
EXPECT_TRUE(aos->Open());
MockAudioSourceCallback source;
EXPECT_CALL(source, OnError(aos))
.Times(0);
aos->Start(&source);
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, OpenStartStopAndClose) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
EXPECT_TRUE(aos->Open());
MockAudioSourceCallback source;
EXPECT_CALL(source, OnError(aos))
.Times(0);
aos->Start(&source);
aos->Stop();
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, Volume) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
double volume = 0.0;
aos->GetVolume(&volume);
EXPECT_EQ(1.0, volume);
aos->SetVolume(0.0);
aos->GetVolume(&volume);
EXPECT_EQ(0.0, volume);
aos->SetVolume(0.5);
aos->GetVolume(&volume);
EXPECT_EQ(0.5, volume);
aos->SetVolume(1.0);
aos->GetVolume(&volume);
EXPECT_EQ(1.0, volume);
aos->SetVolume(1.5);
aos->GetVolume(&volume);
EXPECT_EQ(1.0, volume);
aos->SetVolume(-0.5);
aos->GetVolume(&volume);
EXPECT_EQ(1.0, volume);
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, MiscCallingSequences) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStream* aos = CreateDefaultAudioOutputStream(audio_manager.get());
WASAPIAudioOutputStream* waos = static_cast<WASAPIAudioOutputStream*>(aos);
EXPECT_TRUE(aos->Open());
EXPECT_TRUE(aos->Open());
MockAudioSourceCallback source;
aos->Start(&source);
EXPECT_TRUE(waos->started());
aos->Start(&source);
EXPECT_TRUE(waos->started());
aos->Stop();
EXPECT_FALSE(waos->started());
aos->Stop();
EXPECT_FALSE(waos->started());
aos->Start(&source);
EXPECT_TRUE(waos->started());
aos->Stop();
EXPECT_FALSE(waos->started());
aos->Start(&source);
EXPECT_TRUE(waos->started());
aos->Stop();
EXPECT_FALSE(waos->started());
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, ValidPacketSize) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
base::MessageLoopForUI loop;
MockAudioSourceCallback source;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create();
EXPECT_TRUE(aos->Open());
uint32 bytes_per_packet = aosw.channels() * aosw.samples_per_packet() *
(aosw.bits_per_sample() / 8);
AudioBuffersState state(0, bytes_per_packet);
EXPECT_CALL(source, OnMoreData(NotNull(), HasValidDelay(state)))
.WillOnce(DoAll(
QuitLoop(loop.message_loop_proxy()),
Return(aosw.samples_per_packet())));
aos->Start(&source);
loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(),
TestTimeouts::action_timeout());
loop.Run();
aos->Stop();
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, DISABLED_ReadFromStereoFile) {
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create();
EXPECT_TRUE(aos->Open());
std::string file_name;
if (aosw.sample_rate() == 48000) {
file_name = kSpeechFile_16b_s_48k;
} else if (aosw.sample_rate() == 44100) {
file_name = kSpeechFile_16b_s_44k;
} else if (aosw.sample_rate() == 96000) {
file_name = kSpeechFile_16b_s_48k;
} else {
FAIL() << "This test supports 44.1, 48kHz and 96kHz only.";
return;
}
ReadFromFileAudioSource file_source(file_name);
VLOG(0) << "File name : " << file_name.c_str();
VLOG(0) << "Sample rate : " << aosw.sample_rate();
VLOG(0) << "Bits per sample: " << aosw.bits_per_sample();
VLOG(0) << "#channels : " << aosw.channels();
VLOG(0) << "File size : " << file_source.file_size();
VLOG(0) << "#file segments : " << kNumFileSegments;
VLOG(0) << ">> Listen to the stereo file while playing...";
for (int i = 0; i < kNumFileSegments; i++) {
aos->Start(&file_source);
base::PlatformThread::Sleep(
base::TimeDelta::FromMilliseconds(kFileDurationMs / kNumFileSegments));
aos->Stop();
}
VLOG(0) << ">> Stereo file playout has stopped.";
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, ExclusiveModeBufferSizesAt48kHz) {
if (!ExclusiveModeIsEnabled())
return;
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create(48000, 480);
EXPECT_TRUE(aos->Open());
aos->Close();
aos = aosw.Create(48000, 240);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(48000, 256);
EXPECT_TRUE(aos->Open());
aos->Close();
aos = aosw.Create(48000, 128);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(48000, 144);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(48000, 160);
EXPECT_TRUE(aos->Open());
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, ExclusiveModeBufferSizesAt44kHz) {
if (!ExclusiveModeIsEnabled())
return;
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create(44100, 441);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 448);
EXPECT_TRUE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 256);
EXPECT_TRUE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 220);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 224);
EXPECT_TRUE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 132);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 133);
EXPECT_FALSE(aos->Open());
aos->Close();
aos = aosw.Create(44100, 160);
EXPECT_TRUE(aos->Open());
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, ExclusiveModeMinBufferSizeAt48kHz) {
if (!ExclusiveModeIsEnabled())
return;
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
base::MessageLoopForUI loop;
MockAudioSourceCallback source;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create(48000, 160);
EXPECT_TRUE(aos->Open());
uint32 bytes_per_packet = aosw.channels() * aosw.samples_per_packet() *
(aosw.bits_per_sample() / 8);
AudioBuffersState state(0, bytes_per_packet);
EXPECT_CALL(source, OnMoreData(NotNull(), HasValidDelay(state)))
.WillOnce(DoAll(
QuitLoop(loop.message_loop_proxy()),
Return(aosw.samples_per_packet())))
.WillRepeatedly(Return(aosw.samples_per_packet()));
aos->Start(&source);
loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(),
TestTimeouts::action_timeout());
loop.Run();
aos->Stop();
aos->Close();
}
TEST(WASAPIAudioOutputStreamTest, ExclusiveModeMinBufferSizeAt44kHz) {
if (!ExclusiveModeIsEnabled())
return;
scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
if (!CanRunAudioTests(audio_manager.get()))
return;
base::MessageLoopForUI loop;
MockAudioSourceCallback source;
AudioOutputStreamWrapper aosw(audio_manager.get());
AudioOutputStream* aos = aosw.Create(44100, 160);
EXPECT_TRUE(aos->Open());
uint32 bytes_per_packet = aosw.channels() * aosw.samples_per_packet() *
(aosw.bits_per_sample() / 8);
AudioBuffersState state(0, bytes_per_packet);
EXPECT_CALL(source, OnMoreData(NotNull(), HasValidDelay(state)))
.WillOnce(DoAll(
QuitLoop(loop.message_loop_proxy()),
Return(aosw.samples_per_packet())))
.WillRepeatedly(Return(aosw.samples_per_packet()));
aos->Start(&source);
loop.PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(),
TestTimeouts::action_timeout());
loop.Run();
aos->Stop();
aos->Close();
}
}