This source file includes following definitions.
- GetVolumeAfterSetVolumeOnLinux
- com_init_
- CanRunAudioTests
- HasDeviceVolumeControl
- CreateAndOpenStream
- TEST_F
#include <cmath>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include "base/win/scoped_com_initializer.h"
#include "media/audio/win/core_audio_util_win.h"
#endif
namespace media {
double GetVolumeAfterSetVolumeOnLinux(AudioInputStream* ais,
double target_volume) {
static const int kTimesToRun = 10;
double volume = 0.0;
for (int i = 0; i < kTimesToRun; ++i) {
volume = ais->GetVolume();
if (volume == target_volume)
break;
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
}
return volume;
}
class AudioInputVolumeTest : public ::testing::Test {
protected:
AudioInputVolumeTest()
: audio_manager_(AudioManager::CreateForTesting())
#if defined(OS_WIN)
, com_init_(base::win::ScopedCOMInitializer::kMTA)
#endif
{
}
bool CanRunAudioTests() {
#if defined(OS_WIN)
if (!CoreAudioUtil::IsSupported())
return false;
#endif
if (!audio_manager_)
return false;
return audio_manager_->HasAudioInputDevices();
}
bool HasDeviceVolumeControl(AudioInputStream* stream) {
if (!stream)
return false;
return (stream->GetMaxVolume() != 0.0);
}
AudioInputStream* CreateAndOpenStream(const std::string& device_id) {
const AudioParameters& params =
audio_manager_->GetInputStreamParameters(device_id);
AudioInputStream* ais = audio_manager_->MakeAudioInputStream(
params, device_id);
EXPECT_TRUE(NULL != ais);
#if defined(OS_LINUX) || defined(OS_OPENBSD)
if (!ais->Open()) {
EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id);
ais->Close();
ais = NULL;
}
#elif defined(OS_WIN) || defined(OS_MACOSX)
EXPECT_TRUE(ais->Open());
#endif
return ais;
}
scoped_ptr<AudioManager> audio_manager_;
#if defined(OS_WIN)
base::win::ScopedCOMInitializer com_init_;
#endif
};
#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
#define MAYBE_InputVolumeTest DISABLED_InputVolumeTest
#else
#define MAYBE_InputVolumeTest InputVolumeTest
#endif
TEST_F(AudioInputVolumeTest, MAYBE_InputVolumeTest) {
if (!CanRunAudioTests())
return;
AudioDeviceNames device_names;
audio_manager_->GetAudioInputDeviceNames(&device_names);
if (device_names.empty()) {
LOG(WARNING) << "Could not find any available input device";
return;
}
for (AudioDeviceNames::const_iterator it = device_names.begin();
it != device_names.end();
++it) {
AudioInputStream* ais = CreateAndOpenStream(it->unique_id);
if (!ais) {
DLOG(WARNING) << "Failed to open stream for device " << it->unique_id;
continue;
}
if (!HasDeviceVolumeControl(ais)) {
DLOG(WARNING) << "Device: " << it->unique_id
<< ", does not have volume control.";
ais->Close();
continue;
}
double max_volume = ais->GetMaxVolume();
EXPECT_GT(max_volume, 0.0);
double original_volume = ais->GetVolume();
EXPECT_GE(original_volume, 0.0);
#if defined(OS_WIN) || defined(OS_MACOSX)
EXPECT_LE(original_volume, max_volume);
#endif
ais->SetVolume(max_volume);
double current_volume = ais->GetVolume();
EXPECT_EQ(max_volume, current_volume);
double new_volume = 0.0;
ais->SetVolume(new_volume);
#if defined(OS_LINUX)
current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
#else
current_volume = ais->GetVolume();
#endif
EXPECT_EQ(new_volume, current_volume);
new_volume = max_volume / 2;
ais->SetVolume(new_volume);
#if defined(OS_LINUX)
current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
#else
current_volume = ais->GetVolume();
#endif
EXPECT_LT(current_volume, max_volume);
EXPECT_GT(current_volume, 0);
EXPECT_NEAR(current_volume, new_volume, 0.25 * max_volume);
ais->SetVolume(original_volume);
current_volume = ais->GetVolume();
EXPECT_EQ(original_volume, current_volume);
ais->Close();
}
}
}