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 double max_volume = 0.0; | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
We could use bool return value as indicator here a
no longer working on chromium
2012/02/21 18:01:38
Sure. Another alternative is to have one more API
| |
37 stream->GetMaxMicVolume(&max_volume); | |
38 return (max_volume != 0.0); | |
39 } | |
40 | |
41 AudioInputStream* CreateAndOpenStream(const std::string& device_id) { | |
42 AudioParameters::Format format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
43 // TODO(xians): Implement a generic HardwareChannelCount API to query | |
44 // the number of channel for all the devices. | |
45 ChannelLayout channel_layout = | |
46 (media::GetAudioInputHardwareChannelCount() == 1)? | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
add space after )
no longer working on chromium
2012/02/21 18:01:38
Done.
| |
47 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
48 int bits_per_sample = 16; | |
49 int sample_rate = | |
50 static_cast<int>(media::GetAudioInputHardwareSampleRate()); | |
51 int samples_per_packet = 0; | |
52 #if defined(OS_MACOSX) | |
53 samples_per_packet = (sample_rate / 100); | |
54 #elif defined(OS_LINUX) || defined(OS_OPENBSD) | |
55 samples_per_packet = (sample_rate / 100); | |
56 #elif defined(OS_WIN) | |
57 if (media::IsWASAPISupported()) { | |
58 if (sample_rate == 44100) | |
59 samples_per_packet = 448; | |
60 else | |
61 samples_per_packet = (sample_rate / 100); | |
62 } else { | |
63 samples_per_packet = 3 * (sample_rate / 100); | |
64 } | |
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 return; | |
107 } | |
108 | |
109 if ( !HasDeviceVolumeControl(ais)) { | |
110 DLOG(WARNING) << "Device: " << it->unique_id | |
111 << ", does not have volume control"; | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
End with period I guess.
no longer working on chromium
2012/02/21 18:01:38
I searched the code, and found out that we don't d
| |
112 break; | |
113 } | |
114 | |
115 double max_volume = 0.0; | |
116 double original_volume = 0.0; | |
117 double new_volume = 0.0; | |
118 double current_volume = 0.0; | |
119 | |
120 // Gets the max volume. | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Redundant comment.
no longer working on chromium
2012/02/21 18:01:38
Done.
| |
121 ais->GetMaxMicVolume(&max_volume); | |
122 EXPECT_GT(max_volume, 0.0); | |
123 | |
124 // Notes that |original_volume| can be higher than |max_volume| on Linux. | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
Not sure if I understand this comment.
no longer working on chromium
2012/02/21 18:01:38
I try to explain why I put EXPECT_LE(original_volu
| |
125 ais->GetMicVolume(&original_volume); | |
126 EXPECT_GE(original_volume, 0.0); | |
127 #if defined(OS_WIN) || defined(OS_MACOSX) | |
128 EXPECT_LE(original_volume, max_volume); | |
129 #endif | |
130 | |
131 // Tries to set the volume to the |max_volume|. | |
132 ais->SetMicVolume(max_volume); | |
133 ais->GetMicVolume(¤t_volume); | |
134 EXPECT_EQ(max_volume, current_volume); | |
135 | |
136 // Tries to set the volume to 0. | |
137 new_volume = 0.0; | |
138 ais->SetMicVolume(new_volume); | |
139 ais->GetMicVolume(¤t_volume); | |
140 EXPECT_EQ(new_volume, current_volume); | |
141 | |
142 // Depending on the step size of the volume control, the value that we get | |
143 // can be different from what we set, the variation should be less than | |
144 // the step size. | |
145 double max_step_size = max_volume / 8; | |
henrika (OOO until Aug 14)
2012/02/21 14:03:47
We discussed this part and the code is not OK here
no longer working on chromium
2012/02/21 18:01:38
Done.
| |
146 new_volume = (max_volume <= 1)? | |
147 (max_volume / 2) : std::ceil(max_volume / 2); | |
148 ais->SetMicVolume(new_volume); | |
149 ais->GetMicVolume(¤t_volume); | |
150 EXPECT_LT(current_volume, new_volume + max_step_size); | |
151 EXPECT_GT(current_volume, new_volume - max_step_size); | |
152 | |
153 // Restores the volume to the original value. | |
154 ais->SetMicVolume(original_volume); | |
155 ais->GetMicVolume(¤t_volume); | |
156 EXPECT_EQ(original_volume, current_volume); | |
157 | |
158 ais->Close(); | |
159 } | |
160 } | |
OLD | NEW |