This source file includes following definitions.
- GetTaskRunner
- audio_manager
- message_loop
- CanRunAudioTests
- previous_write_time_
- OnData
- OnError
- OnMoreData
- OnMoreIOData
- OnError
- BytesToMilliseconds
- GetDefaultAudioStreamParameters
- CreateStream
- GetDefaultAudioStreamParameters
- CreateStream
- bits_per_sample_
- Create
- channels
- bits_per_sample
- sample_rate
- samples_per_packet
- CreateStream
- TEST_F
#include "base/basictypes.h"
#include "base/environment.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/synchronization/lock.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/fake_audio_log_factory.h"
#include "media/base/seekable_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(USE_ALSA)
#include "media/audio/alsa/audio_manager_alsa.h"
#elif defined(OS_MACOSX)
#include "media/audio/mac/audio_manager_mac.h"
#elif defined(OS_WIN)
#include "media/audio/win/audio_manager_win.h"
#include "media/audio/win/core_audio_util_win.h"
#elif defined(OS_ANDROID)
#include "media/audio/android/audio_manager_android.h"
#else
#include "media/audio/fake_audio_manager.h"
#endif
namespace media {
#if defined(USE_ALSA)
typedef AudioManagerAlsa AudioManagerAnyPlatform;
#elif defined(OS_MACOSX)
typedef AudioManagerMac AudioManagerAnyPlatform;
#elif defined(OS_WIN)
typedef AudioManagerWin AudioManagerAnyPlatform;
#elif defined(OS_ANDROID)
typedef AudioManagerAndroid AudioManagerAnyPlatform;
#else
typedef FakeAudioManager AudioManagerAnyPlatform;
#endif
static const size_t kMaxDelayMeasurements = 1000;
static const char kDelayValuesFileName[] = "audio_delay_values_ms.txt";
struct AudioDelayState {
AudioDelayState()
: delta_time_ms(0),
buffer_delay_ms(0),
input_delay_ms(0),
output_delay_ms(0) {
}
int delta_time_ms;
int buffer_delay_ms;
int input_delay_ms;
int output_delay_ms;
};
class MockAudioManager : public AudioManagerAnyPlatform {
public:
MockAudioManager() : AudioManagerAnyPlatform(&fake_audio_log_factory_) {}
virtual ~MockAudioManager() {}
virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() OVERRIDE {
return base::MessageLoop::current()->message_loop_proxy();
}
private:
FakeAudioLogFactory fake_audio_log_factory_;
DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
};
class AudioLowLatencyInputOutputTest : public testing::Test {
protected:
AudioLowLatencyInputOutputTest() {}
virtual ~AudioLowLatencyInputOutputTest() {}
AudioManager* audio_manager() { return &mock_audio_manager_; }
base::MessageLoopForUI* message_loop() { return &message_loop_; }
bool CanRunAudioTests() {
bool input = audio_manager()->HasAudioInputDevices();
bool output = audio_manager()->HasAudioOutputDevices();
LOG_IF(WARNING, !input) << "No input device detected.";
LOG_IF(WARNING, !output) << "No output device detected.";
return input && output;
}
private:
base::MessageLoopForUI message_loop_;
MockAudioManager mock_audio_manager_;
DISALLOW_COPY_AND_ASSIGN(AudioLowLatencyInputOutputTest);
};
class FullDuplexAudioSinkSource
: public AudioInputStream::AudioInputCallback,
public AudioOutputStream::AudioSourceCallback {
public:
FullDuplexAudioSinkSource(int sample_rate,
int samples_per_packet,
int channels)
: sample_rate_(sample_rate),
samples_per_packet_(samples_per_packet),
channels_(channels),
input_elements_to_write_(0),
output_elements_to_write_(0),
previous_write_time_(base::TimeTicks::Now()) {
frame_size_ = (16 / 8) * channels_;
buffer_.reset(
new media::SeekableBuffer(0, samples_per_packet_ * frame_size_));
frames_to_ms_ = static_cast<double>(1000.0 / sample_rate_);
delay_states_.reset(new AudioDelayState[kMaxDelayMeasurements]);
}
virtual ~FullDuplexAudioSinkSource() {
base::FilePath file_name;
EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_name));
file_name = file_name.AppendASCII(kDelayValuesFileName);
FILE* text_file = base::OpenFile(file_name, "wt");
DLOG_IF(ERROR, !text_file) << "Failed to open log file.";
VLOG(0) << ">> Output file " << file_name.value() << " has been created.";
size_t elements_written = 0;
while (elements_written <
std::min(input_elements_to_write_, output_elements_to_write_)) {
const AudioDelayState state = delay_states_[elements_written];
fprintf(text_file, "%d %d %d %d\n",
state.delta_time_ms,
state.buffer_delay_ms,
state.input_delay_ms,
state.output_delay_ms);
++elements_written;
}
base::CloseFile(text_file);
}
virtual void OnData(AudioInputStream* stream,
const uint8* src, uint32 size,
uint32 hardware_delay_bytes,
double volume) OVERRIDE {
base::AutoLock lock(lock_);
const base::TimeTicks now_time = base::TimeTicks::Now();
const int diff = (now_time - previous_write_time_).InMilliseconds();
previous_write_time_ = now_time;
if (input_elements_to_write_ < kMaxDelayMeasurements) {
delay_states_[input_elements_to_write_].delta_time_ms = diff;
delay_states_[input_elements_to_write_].buffer_delay_ms =
BytesToMilliseconds(buffer_->forward_bytes());
delay_states_[input_elements_to_write_].input_delay_ms =
BytesToMilliseconds(hardware_delay_bytes);
++input_elements_to_write_;
}
if (!buffer_->Append(src, size)) {
buffer_->set_forward_capacity(2 * buffer_->forward_capacity());
buffer_->Clear();
}
}
virtual void OnError(AudioInputStream* stream) OVERRIDE {}
virtual int OnMoreData(AudioBus* audio_bus,
AudioBuffersState buffers_state) OVERRIDE {
base::AutoLock lock(lock_);
if (output_elements_to_write_ < kMaxDelayMeasurements) {
int output_delay_bytes = buffers_state.hardware_delay_bytes;
#if defined(OS_WIN)
if (!CoreAudioUtil::IsSupported()) {
output_delay_bytes = buffers_state.pending_bytes;
}
#endif
delay_states_[output_elements_to_write_].output_delay_ms =
BytesToMilliseconds(output_delay_bytes);
++output_elements_to_write_;
}
int size;
const uint8* source;
if (buffer_->GetCurrentChunk(&source, &size) && size > 0) {
EXPECT_EQ(channels_, audio_bus->channels());
size = std::min(audio_bus->frames() * frame_size_, size);
EXPECT_EQ(static_cast<size_t>(size) % sizeof(*audio_bus->channel(0)), 0U);
audio_bus->FromInterleaved(
source, size / frame_size_, frame_size_ / channels_);
buffer_->Seek(size);
return size / frame_size_;
}
return 0;
}
virtual int OnMoreIOData(AudioBus* source,
AudioBus* dest,
AudioBuffersState buffers_state) OVERRIDE {
NOTREACHED();
return 0;
}
virtual void OnError(AudioOutputStream* stream) OVERRIDE {}
protected:
int BytesToMilliseconds(uint32 delay_bytes) const {
return static_cast<int>((delay_bytes / frame_size_) * frames_to_ms_ + 0.5);
}
private:
base::Lock lock_;
scoped_ptr<media::SeekableBuffer> buffer_;
int sample_rate_;
int samples_per_packet_;
int channels_;
int frame_size_;
double frames_to_ms_;
scoped_ptr<AudioDelayState[]> delay_states_;
size_t input_elements_to_write_;
size_t output_elements_to_write_;
base::TimeTicks previous_write_time_;
};
class AudioInputStreamTraits {
public:
typedef AudioInputStream StreamType;
static AudioParameters GetDefaultAudioStreamParameters(
AudioManager* audio_manager) {
return audio_manager->GetInputStreamParameters(
AudioManagerBase::kDefaultDeviceId);
}
static StreamType* CreateStream(AudioManager* audio_manager,
const AudioParameters& params) {
return audio_manager->MakeAudioInputStream(params,
AudioManagerBase::kDefaultDeviceId);
}
};
class AudioOutputStreamTraits {
public:
typedef AudioOutputStream StreamType;
static AudioParameters GetDefaultAudioStreamParameters(
AudioManager* audio_manager) {
return audio_manager->GetDefaultOutputStreamParameters();
}
static StreamType* CreateStream(AudioManager* audio_manager,
const AudioParameters& params) {
return audio_manager->MakeAudioOutputStream(params, std::string());
}
};
template <typename StreamTraits>
class StreamWrapper {
public:
typedef typename StreamTraits::StreamType StreamType;
explicit StreamWrapper(AudioManager* audio_manager)
:
audio_manager_(audio_manager),
format_(AudioParameters::AUDIO_PCM_LOW_LATENCY),
#if defined(OS_ANDROID)
channel_layout_(CHANNEL_LAYOUT_MONO),
#else
channel_layout_(CHANNEL_LAYOUT_STEREO),
#endif
bits_per_sample_(16) {
const AudioParameters& params =
StreamTraits::GetDefaultAudioStreamParameters(audio_manager_);
sample_rate_ = params.sample_rate();
samples_per_packet_ = params.frames_per_buffer();
}
virtual ~StreamWrapper() {}
StreamType* Create() {
return CreateStream();
}
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:
StreamType* CreateStream() {
StreamType* stream = StreamTraits::CreateStream(audio_manager_,
AudioParameters(format_, channel_layout_, sample_rate_,
bits_per_sample_, samples_per_packet_));
EXPECT_TRUE(stream);
return stream;
}
AudioManager* audio_manager_;
AudioParameters::Format format_;
ChannelLayout channel_layout_;
int bits_per_sample_;
int sample_rate_;
int samples_per_packet_;
};
typedef StreamWrapper<AudioInputStreamTraits> AudioInputStreamWrapper;
typedef StreamWrapper<AudioOutputStreamTraits> AudioOutputStreamWrapper;
TEST_F(AudioLowLatencyInputOutputTest, DISABLED_FullDuplexDelayMeasurement) {
if (!CanRunAudioTests())
return;
AudioInputStreamWrapper aisw(audio_manager());
AudioInputStream* ais = aisw.Create();
EXPECT_TRUE(ais);
AudioOutputStreamWrapper aosw(audio_manager());
AudioOutputStream* aos = aosw.Create();
EXPECT_TRUE(aos);
if (aisw.sample_rate() != aosw.sample_rate() ||
aisw.samples_per_packet() != aosw.samples_per_packet() ||
aisw.channels()!= aosw.channels() ||
aisw.bits_per_sample() != aosw.bits_per_sample()) {
LOG(ERROR) << "This test requires symmetric input and output parameters. "
"Ensure that sample rate and number of channels are identical in "
"both directions";
aos->Close();
ais->Close();
return;
}
EXPECT_TRUE(ais->Open());
EXPECT_TRUE(aos->Open());
FullDuplexAudioSinkSource full_duplex(
aisw.sample_rate(), aisw.samples_per_packet(), aisw.channels());
VLOG(0) << ">> You should now be able to hear yourself in loopback...";
DVLOG(0) << " sample_rate : " << aisw.sample_rate();
DVLOG(0) << " samples_per_packet: " << aisw.samples_per_packet();
DVLOG(0) << " channels : " << aisw.channels();
ais->Start(&full_duplex);
aos->Start(&full_duplex);
message_loop()->PostDelayedTask(FROM_HERE,
base::MessageLoop::QuitClosure(), TestTimeouts::action_timeout());
message_loop()->Run();
aos->Stop();
ais->Stop();
aos->Close();
ais->Close();
}
}