OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <cmath> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/win/scoped_com_initializer.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" | |
13 | |
14 using base::win::ScopedCOMInitializer; | |
15 using media::AudioDeviceNames; | |
16 | |
17 class AudioInputVolumeTest : public ::testing::Test { | |
18 protected: | |
19 AudioInputVolumeTest() | |
20 : audio_manager_(AudioManager::Create()), | |
21 com_init_(ScopedCOMInitializer::kMTA) { | |
22 } | |
23 | |
24 bool CanRunAudioTests() { | |
25 if (!audio_manager_.get()) | |
26 return false; | |
27 | |
28 return audio_manager_->HasAudioInputDevices(); | |
29 } | |
30 | |
31 // Helper method which checks if the stream has volume support. | |
32 bool HasDeviceVolumeControl(AudioInputStream* stream) { | |
33 if (!stream) | |
34 return false; | |
35 | |
36 return (stream->GetMaxVolume() != 0.0); | |
37 } | |
38 | |
39 AudioInputStream* CreateAndOpenStream(const std::string& device_id) { | |
40 AudioParameters::Format format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
41 // TODO(xians): Implement a generic HardwareChannelCount API to query | |
42 // the number of channel for all the devices. | |
43 ChannelLayout channel_layout = | |
44 (media::GetAudioInputHardwareChannelCount() == 1) ? | |
45 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
46 int bits_per_sample = 16; | |
47 int sample_rate = | |
48 static_cast<int>(media::GetAudioInputHardwareSampleRate()); | |
49 int samples_per_packet = 0; | |
50 #if defined(OS_MACOSX) | |
51 samples_per_packet = (sample_rate / 100); | |
52 #elif defined(OS_LINUX) || defined(OS_OPENBSD) | |
53 samples_per_packet = (sample_rate / 100); | |
54 #elif defined(OS_WIN) | |
55 if (media::IsWASAPISupported()) { | |
56 if (sample_rate == 44100) | |
57 samples_per_packet = 448; | |
58 else | |
59 samples_per_packet = (sample_rate / 100); | |
60 } else { | |
61 samples_per_packet = 3 * (sample_rate / 100); | |
62 } | |
63 #else | |
64 #error Unsupported platform | |
nilesh
2012/02/28 19:24:24
I am going to exclude this test from the android b
| |
65 #endif | |
66 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( | |
67 AudioParameters(format, channel_layout, sample_rate, bits_per_sample, | |
68 samples_per_packet), | |
69 device_id); | |
70 EXPECT_TRUE(NULL != ais); | |
71 | |
72 #if defined(OS_LINUX) || defined(OS_OPENBSD) | |
73 // Some linux devices do not support our settings, we may fail to open | |
74 // those devices. | |
75 if (!ais->Open()) { | |
76 // Default device should always be able to be opened. | |
77 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id); | |
78 ais->Close(); | |
79 ais = NULL; | |
80 } | |
81 #elif defined(OS_WIN) || defined(OS_MACOSX) | |
82 EXPECT_TRUE(ais->Open()); | |
83 #endif | |
84 | |
85 return ais; | |
86 } | |
87 | |
88 scoped_ptr<AudioManager> audio_manager_; | |
89 ScopedCOMInitializer com_init_; | |
90 }; | |
91 | |
92 TEST_F(AudioInputVolumeTest, InputVolumeTest) { | |
93 if (!CanRunAudioTests()) | |
94 return; | |
95 | |
96 AudioDeviceNames device_names; | |
97 audio_manager_->GetAudioInputDeviceNames(&device_names); | |
98 DCHECK(!device_names.empty()); | |
99 | |
100 for (AudioDeviceNames::const_iterator it = device_names.begin(); | |
101 it != device_names.end(); | |
102 ++it) { | |
103 AudioInputStream* ais = CreateAndOpenStream(it->unique_id); | |
104 if (!ais) { | |
105 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id; | |
106 continue; | |
107 } | |
108 | |
109 if ( !HasDeviceVolumeControl(ais)) { | |
110 DLOG(WARNING) << "Device: " << it->unique_id | |
111 << ", does not have volume control"; | |
112 ais->Close(); | |
113 continue; | |
114 } | |
115 | |
116 double max_volume = ais->GetMaxVolume(); | |
117 EXPECT_GT(max_volume, 0.0); | |
118 | |
119 // Notes that |original_volume| can be higher than |max_volume| on Linux. | |
120 double original_volume = ais->GetVolume(); | |
121 EXPECT_GE(original_volume, 0.0); | |
122 #if defined(OS_WIN) || defined(OS_MACOSX) | |
123 EXPECT_LE(original_volume, max_volume); | |
124 #endif | |
125 | |
126 // Tries to set the volume to |max_volume|. | |
127 ais->SetVolume(max_volume); | |
128 double current_volume = ais->GetVolume(); | |
129 EXPECT_EQ(max_volume, current_volume); | |
130 | |
131 // Tries to set the volume to zero. | |
132 double new_volume = 0.0; | |
133 ais->SetVolume(new_volume); | |
134 current_volume = ais->GetVolume(); | |
135 EXPECT_EQ(new_volume, current_volume); | |
136 | |
137 // Tries to set the volume to the middle. | |
138 new_volume = max_volume / 2; | |
139 ais->SetVolume(new_volume); | |
140 current_volume = ais->GetVolume(); | |
141 EXPECT_LT(current_volume, max_volume); | |
142 EXPECT_GT(current_volume, 0); | |
143 | |
144 // Restores the volume to the original value. | |
145 ais->SetVolume(original_volume); | |
146 current_volume = ais->GetVolume(); | |
147 EXPECT_EQ(original_volume, current_volume); | |
148 | |
149 ais->Close(); | |
150 } | |
151 } | |
OLD | NEW |