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 #endif | |
64 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( | |
65 AudioParameters(format, channel_layout, sample_rate, bits_per_sample, | |
66 samples_per_packet), | |
67 device_id); | |
68 EXPECT_TRUE(NULL != ais); | |
69 | |
70 #if defined(OS_LINUX) || defined(OS_OPENBSD) | |
71 // Some linux devices do not support our settings, we may fail to open | |
72 // those devices. | |
73 if (!ais->Open()) { | |
74 // Default device should always be able to be opened. | |
75 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id); | |
76 ais->Close(); | |
77 ais = NULL; | |
78 } | |
79 #elif defined(OS_WIN) || defined(OS_MACOSX) | |
80 EXPECT_TRUE(ais->Open()); | |
81 #endif | |
82 | |
83 return ais; | |
84 } | |
85 | |
86 scoped_ptr<AudioManager> audio_manager_; | |
87 ScopedCOMInitializer com_init_; | |
88 }; | |
89 | |
90 TEST_F(AudioInputVolumeTest, InputVolumeTest) { | |
91 if (!CanRunAudioTests()) | |
92 return; | |
93 | |
94 AudioDeviceNames device_names; | |
95 audio_manager_->GetAudioInputDeviceNames(&device_names); | |
96 DCHECK(!device_names.empty()); | |
97 | |
98 for (AudioDeviceNames::const_iterator it = device_names.begin(); | |
99 it != device_names.end(); | |
100 ++it) { | |
101 AudioInputStream* ais = CreateAndOpenStream(it->unique_id); | |
102 if (!ais) { | |
103 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id; | |
104 return; | |
105 } | |
106 | |
107 if ( !HasDeviceVolumeControl(ais)) { | |
108 DLOG(WARNING) << "Device: " << it->unique_id | |
109 << ", does not have volume control"; | |
110 break; | |
111 } | |
112 | |
113 double max_volume = ais->GetMaxVolume(); | |
114 EXPECT_GT(max_volume, 0.0); | |
115 | |
116 // Notes that |original_volume| can be higher than |max_volume| on Linux. | |
117 double original_volume = ais->GetVolume(); | |
118 EXPECT_GE(original_volume, 0.0); | |
119 #if defined(OS_WIN) || defined(OS_MACOSX) | |
120 EXPECT_LE(original_volume, max_volume); | |
121 #endif | |
122 | |
123 // Tries to set the volume to the |max_volume|. | |
henrika (OOO until Aug 14)
2012/02/23 11:50:41
nit, I would skip second "the"
no longer working on chromium
2012/02/24 09:27:48
Done.
| |
124 ais->SetVolume(max_volume); | |
125 double current_volume = ais->GetVolume(); | |
126 EXPECT_EQ(max_volume, current_volume); | |
127 | |
128 // Tries to set the volume to 0. | |
henrika (OOO until Aug 14)
2012/02/23 11:50:41
I would use zero or mute instead of 0.
no longer working on chromium
2012/02/24 09:27:48
Done.
| |
129 double new_volume = 0.0; | |
130 ais->SetVolume(new_volume); | |
131 current_volume = ais->GetVolume(); | |
132 EXPECT_EQ(new_volume, current_volume); | |
133 | |
134 // Depending on the step size of the volume control, the value that we get | |
henrika (OOO until Aug 14)
2012/02/23 11:50:41
This comment doesn't really describe the test.
no longer working on chromium
2012/02/24 09:27:48
Done.
| |
135 // can be different from what we set. | |
136 new_volume = max_volume / 2; | |
137 ais->SetVolume(new_volume); | |
138 current_volume = ais->GetVolume(); | |
139 EXPECT_LT(current_volume, max_volume); | |
140 EXPECT_GT(current_volume, 0); | |
141 | |
142 // Restores the volume to the original value. | |
143 ais->SetVolume(original_volume); | |
144 current_volume = ais->GetVolume(); | |
145 EXPECT_EQ(original_volume, current_volume); | |
146 | |
147 ais->Close(); | |
148 } | |
149 } | |
OLD | NEW |