This source file includes following definitions.
- com_init_
- audio_manager
- SetMMDeviceEnumeration
- SetWaveEnumeration
- GetDeviceIdFromPCMWaveInAudioInputStream
- CheckDeviceNames
- CanRunInputTest
- CanRunOutputTest
- CreateAudioManagerForTesting
- RunOnAudioThread
- RunOnAudioThreadImpl
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/environment.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/waitable_event.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/fake_audio_log_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(USE_ALSA)
#include "media/audio/alsa/audio_manager_alsa.h"
#endif
#if defined(OS_WIN)
#include "base/win/scoped_com_initializer.h"
#include "media/audio/win/audio_manager_win.h"
#include "media/audio/win/wavein_input_win.h"
#endif
#if defined(USE_PULSEAUDIO)
#include "media/audio/pulse/audio_manager_pulse.h"
#endif
namespace media {
class AudioManagerTest : public ::testing::Test {
protected:
AudioManagerTest()
: audio_manager_(AudioManager::CreateForTesting())
#if defined(OS_WIN)
, com_init_(base::win::ScopedCOMInitializer::kMTA)
#endif
{
base::WaitableEvent event(false, false);
audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind(
&base::WaitableEvent::Signal, base::Unretained(&event)));
event.Wait();
}
AudioManager* audio_manager() { return audio_manager_.get(); };
#if defined(OS_WIN)
bool SetMMDeviceEnumeration() {
AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get());
if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration)
return false;
amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration);
return true;
}
void SetWaveEnumeration() {
AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get());
amw->SetEnumerationType(AudioManagerWin::kWaveEnumeration);
}
std::string GetDeviceIdFromPCMWaveInAudioInputStream(
const std::string& device_id) {
AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get());
AudioParameters parameters(
AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO,
AudioParameters::kAudioCDSampleRate, 16,
1024);
scoped_ptr<PCMWaveInAudioInputStream> stream(
static_cast<PCMWaveInAudioInputStream*>(
amw->CreatePCMWaveInAudioInputStream(parameters, device_id)));
return stream.get() ? stream->device_id_ : std::string();
}
#endif
static void CheckDeviceNames(const AudioDeviceNames& device_names) {
VLOG(2) << "Got " << device_names.size() << " audio devices.";
if (!device_names.empty()) {
AudioDeviceNames::const_iterator it = device_names.begin();
EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName),
it->device_name);
EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id);
++it;
while (it != device_names.end()) {
EXPECT_FALSE(it->device_name.empty());
EXPECT_FALSE(it->unique_id.empty());
VLOG(2) << "Device ID(" << it->unique_id
<< "), label: " << it->device_name;
EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName),
it->device_name);
EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId),
it->unique_id);
++it;
}
} else {
LOG(WARNING) << "No input devices detected";
}
}
bool CanRunInputTest() {
return audio_manager_->HasAudioInputDevices();
}
bool CanRunOutputTest() {
return audio_manager_->HasAudioOutputDevices();
}
#if defined(USE_ALSA) || defined(USE_PULSEAUDIO)
template <class T>
void CreateAudioManagerForTesting() {
audio_manager_.reset();
audio_manager_.reset(T::Create(&fake_audio_log_factory_));
}
#endif
void RunOnAudioThread(const base::Closure& closure) {
if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
base::WaitableEvent event(false, false);
audio_manager_->GetTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&AudioManagerTest::RunOnAudioThreadImpl,
base::Unretained(this),
closure,
&event));
event.Wait();
} else {
closure.Run();
}
}
void RunOnAudioThreadImpl(const base::Closure& closure,
base::WaitableEvent* event) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
closure.Run();
event->Signal();
}
FakeAudioLogFactory fake_audio_log_factory_;
scoped_ptr<AudioManager> audio_manager_;
#if defined(OS_WIN)
base::win::ScopedCOMInitializer com_init_;
#endif
};
TEST_F(AudioManagerTest, EnumerateInputDevices) {
if (!CanRunInputTest())
return;
AudioDeviceNames device_names;
RunOnAudioThread(
base::Bind(&AudioManager::GetAudioInputDeviceNames,
base::Unretained(audio_manager()),
&device_names));
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, EnumerateOutputDevices) {
if (!CanRunOutputTest())
return;
AudioDeviceNames device_names;
RunOnAudioThread(
base::Bind(&AudioManager::GetAudioOutputDeviceNames,
base::Unretained(audio_manager()),
&device_names));
CheckDeviceNames(device_names);
}
#if defined(OS_WIN)
TEST_F(AudioManagerTest, EnumerateInputDevicesWinMMDevice) {
if (!CanRunInputTest())
return;
AudioDeviceNames device_names;
if (!SetMMDeviceEnumeration()) {
LOG(WARNING) << "MM device enumeration is not supported.";
return;
}
audio_manager_->GetAudioInputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, EnumerateOutputDevicesWinMMDevice) {
if (!CanRunOutputTest())
return;
AudioDeviceNames device_names;
if (!SetMMDeviceEnumeration()) {
LOG(WARNING) << "MM device enumeration is not supported.";
return;
}
audio_manager_->GetAudioOutputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, EnumerateInputDevicesWinWave) {
if (!CanRunInputTest())
return;
AudioDeviceNames device_names;
SetWaveEnumeration();
audio_manager_->GetAudioInputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, EnumerateOutputDevicesWinWave) {
if (!CanRunOutputTest())
return;
AudioDeviceNames device_names;
SetWaveEnumeration();
audio_manager_->GetAudioOutputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, WinXPDeviceIdUnchanged) {
if (!CanRunInputTest())
return;
AudioDeviceNames xp_device_names;
SetWaveEnumeration();
audio_manager_->GetAudioInputDeviceNames(&xp_device_names);
CheckDeviceNames(xp_device_names);
for (AudioDeviceNames::iterator i = xp_device_names.begin();
i != xp_device_names.end(); ++i) {
EXPECT_EQ(i->unique_id,
GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id));
}
}
TEST_F(AudioManagerTest, ConvertToWinXPInputDeviceId) {
if (!CanRunInputTest())
return;
if (!SetMMDeviceEnumeration()) {
LOG(WARNING) << "MM device enumeration is not supported.";
return;
}
AudioDeviceNames device_names;
audio_manager_->GetAudioInputDeviceNames(&device_names);
CheckDeviceNames(device_names);
for (AudioDeviceNames::iterator i = device_names.begin();
i != device_names.end(); ++i) {
std::string converted_id =
GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id);
if (i == device_names.begin()) {
EXPECT_EQ(i->unique_id, converted_id);
} else {
EXPECT_NE(i->unique_id, converted_id);
}
}
}
#endif
#if defined(USE_PULSEAUDIO)
TEST_F(AudioManagerTest, EnumerateInputDevicesPulseaudio) {
if (!CanRunInputTest())
return;
CreateAudioManagerForTesting<AudioManagerPulse>();
if (audio_manager_.get()) {
AudioDeviceNames device_names;
audio_manager_->GetAudioInputDeviceNames(&device_names);
CheckDeviceNames(device_names);
} else {
LOG(WARNING) << "No pulseaudio on this system.";
}
}
TEST_F(AudioManagerTest, EnumerateOutputDevicesPulseaudio) {
if (!CanRunOutputTest())
return;
CreateAudioManagerForTesting<AudioManagerPulse>();
if (audio_manager_.get()) {
AudioDeviceNames device_names;
audio_manager_->GetAudioOutputDeviceNames(&device_names);
CheckDeviceNames(device_names);
} else {
LOG(WARNING) << "No pulseaudio on this system.";
}
}
#endif
#if defined(USE_ALSA)
TEST_F(AudioManagerTest, EnumerateInputDevicesAlsa) {
if (!CanRunInputTest())
return;
VLOG(2) << "Testing AudioManagerAlsa.";
CreateAudioManagerForTesting<AudioManagerAlsa>();
AudioDeviceNames device_names;
audio_manager_->GetAudioInputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
TEST_F(AudioManagerTest, EnumerateOutputDevicesAlsa) {
if (!CanRunOutputTest())
return;
VLOG(2) << "Testing AudioManagerAlsa.";
CreateAudioManagerForTesting<AudioManagerAlsa>();
AudioDeviceNames device_names;
audio_manager_->GetAudioOutputDeviceNames(&device_names);
CheckDeviceNames(device_names);
}
#endif
TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) {
#if defined(OS_WIN) || defined(OS_MACOSX)
if (!CanRunInputTest())
return;
AudioParameters params = audio_manager_->GetDefaultOutputStreamParameters();
EXPECT_TRUE(params.IsValid());
#endif
}
TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) {
#if defined(OS_WIN) || defined(OS_MACOSX)
if (!CanRunInputTest() || !CanRunOutputTest())
return;
AudioDeviceNames device_names;
audio_manager_->GetAudioInputDeviceNames(&device_names);
bool found_an_associated_device = false;
for (AudioDeviceNames::iterator it = device_names.begin();
it != device_names.end();
++it) {
EXPECT_FALSE(it->unique_id.empty());
EXPECT_FALSE(it->device_name.empty());
std::string output_device_id(
audio_manager_->GetAssociatedOutputDeviceID(it->unique_id));
if (!output_device_id.empty()) {
VLOG(2) << it->unique_id << " matches with " << output_device_id;
found_an_associated_device = true;
}
}
EXPECT_TRUE(found_an_associated_device);
#endif
}
}