cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / media / audio / audio_input_volume_unittest.cc
blob570c045570edef562130fa0a96e3ffecdcb62b11
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <cmath>
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "media/audio/audio_io.h"
10 #include "media/audio/audio_manager_base.h"
11 #include "media/audio/audio_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(OS_WIN)
15 #include "base/win/scoped_com_initializer.h"
16 #include "media/audio/win/core_audio_util_win.h"
17 #endif
19 namespace media {
21 double GetVolumeAfterSetVolumeOnLinux(AudioInputStream* ais,
22 double target_volume) {
23 // SetVolume() is asynchronous on Linux, we need to keep trying until
24 // the SetVolume() operation is done.
25 static const int kTimesToRun = 10;
26 double volume = 0.0;
27 for (int i = 0; i < kTimesToRun; ++i) {
28 volume = ais->GetVolume();
29 if (volume == target_volume)
30 break;
32 // Sleep 100ms to wait for the operation.
33 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
36 return volume;
39 class AudioInputVolumeTest : public ::testing::Test {
40 protected:
41 AudioInputVolumeTest()
42 : audio_manager_(AudioManager::Create())
43 #if defined(OS_WIN)
44 , com_init_(base::win::ScopedCOMInitializer::kMTA)
45 #endif
49 bool CanRunAudioTests() {
50 #if defined(OS_WIN)
51 // TODO(henrika): add support for volume control on Windows XP as well.
52 // For now, we might as well signal false already here to avoid running
53 // these tests on Windows XP.
54 if (!CoreAudioUtil::IsSupported())
55 return false;
56 #endif
57 if (!audio_manager_)
58 return false;
60 return audio_manager_->HasAudioInputDevices();
63 // Helper method which checks if the stream has volume support.
64 bool HasDeviceVolumeControl(AudioInputStream* stream) {
65 if (!stream)
66 return false;
68 return (stream->GetMaxVolume() != 0.0);
71 AudioInputStream* CreateAndOpenStream(const std::string& device_id) {
72 const AudioParameters& params =
73 audio_manager_->GetInputStreamParameters(device_id);
74 AudioInputStream* ais = audio_manager_->MakeAudioInputStream(
75 params, device_id);
76 EXPECT_TRUE(NULL != ais);
78 #if defined(OS_LINUX) || defined(OS_OPENBSD)
79 // Some linux devices do not support our settings, we may fail to open
80 // those devices.
81 if (!ais->Open()) {
82 // Default device should always be able to be opened.
83 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id);
84 ais->Close();
85 ais = NULL;
87 #elif defined(OS_WIN) || defined(OS_MACOSX)
88 EXPECT_TRUE(ais->Open());
89 #endif
91 return ais;
94 scoped_ptr<AudioManager> audio_manager_;
96 #if defined(OS_WIN)
97 base::win::ScopedCOMInitializer com_init_;
98 #endif
101 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
102 // Currently failing on linux ARM bot: http://crbug/238490
103 #define MAYBE_InputVolumeTest DISABLED_InputVolumeTest
104 #else
105 #define MAYBE_InputVolumeTest InputVolumeTest
106 #endif
108 TEST_F(AudioInputVolumeTest, MAYBE_InputVolumeTest) {
109 if (!CanRunAudioTests())
110 return;
112 // Retrieve a list of all available input devices.
113 AudioDeviceNames device_names;
114 audio_manager_->GetAudioInputDeviceNames(&device_names);
115 if (device_names.empty()) {
116 LOG(WARNING) << "Could not find any available input device";
117 return;
120 // Scan all available input devices and repeat the same test for all of them.
121 for (AudioDeviceNames::const_iterator it = device_names.begin();
122 it != device_names.end();
123 ++it) {
124 AudioInputStream* ais = CreateAndOpenStream(it->unique_id);
125 if (!ais) {
126 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id;
127 continue;
130 if (!HasDeviceVolumeControl(ais)) {
131 DLOG(WARNING) << "Device: " << it->unique_id
132 << ", does not have volume control.";
133 ais->Close();
134 continue;
137 double max_volume = ais->GetMaxVolume();
138 EXPECT_GT(max_volume, 0.0);
140 // Store the current input-device volume level.
141 double original_volume = ais->GetVolume();
142 EXPECT_GE(original_volume, 0.0);
143 #if defined(OS_WIN) || defined(OS_MACOSX)
144 // Note that |original_volume| can be higher than |max_volume| on Linux.
145 EXPECT_LE(original_volume, max_volume);
146 #endif
148 // Set the volume to the maxiumum level..
149 ais->SetVolume(max_volume);
150 double current_volume = ais->GetVolume();
151 EXPECT_EQ(max_volume, current_volume);
153 // Set the volume to the mininum level (=0).
154 double new_volume = 0.0;
155 ais->SetVolume(new_volume);
156 #if defined(OS_LINUX)
157 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
158 #else
159 current_volume = ais->GetVolume();
160 #endif
161 EXPECT_EQ(new_volume, current_volume);
163 // Set the volume to the mid level (50% of max).
164 // Verify that the absolute error is small enough.
165 new_volume = max_volume / 2;
166 ais->SetVolume(new_volume);
167 #if defined(OS_LINUX)
168 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
169 #else
170 current_volume = ais->GetVolume();
171 #endif
172 EXPECT_LT(current_volume, max_volume);
173 EXPECT_GT(current_volume, 0);
174 EXPECT_NEAR(current_volume, new_volume, 0.25 * max_volume);
176 // Restores the volume to the original value.
177 ais->SetVolume(original_volume);
178 current_volume = ais->GetVolume();
179 EXPECT_EQ(original_volume, current_volume);
181 ais->Close();
185 } // namespace media