Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: media/audio/audio_input_volume_unittest.cc

Issue 9418042: Adding microphone volume support to chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed henrik's comments Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | media/audio/audio_io.h » ('j') | media/audio/linux/alsa_util.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
tommi (sloooow) - chröme 2012/02/24 12:07:34 we also have code for bsd although we don't mainta
no longer working on chromium 2012/02/24 15:05:55 I have || defined(OS_OPENBSD) above, but I agree t
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 continue;
105 }
106
107 if ( !HasDeviceVolumeControl(ais)) {
108 DLOG(WARNING) << "Device: " << it->unique_id
109 << ", does not have volume control";
110 ais->Close();
111 continue;
112 }
113
114 double max_volume = ais->GetMaxVolume();
115 EXPECT_GT(max_volume, 0.0);
116
117 // Notes that |original_volume| can be higher than |max_volume| on Linux.
118 double original_volume = ais->GetVolume();
119 EXPECT_GE(original_volume, 0.0);
120 #if defined(OS_WIN) || defined(OS_MACOSX)
121 EXPECT_LE(original_volume, max_volume);
122 #endif
123
124 // Tries to set the volume to |max_volume|.
125 ais->SetVolume(max_volume);
126 double current_volume = ais->GetVolume();
127 EXPECT_EQ(max_volume, current_volume);
128
129 // Tries to set the volume to zero.
130 double new_volume = 0.0;
131 ais->SetVolume(new_volume);
132 current_volume = ais->GetVolume();
133 EXPECT_EQ(new_volume, current_volume);
134
135 // Tries to set the volume to the middle.
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 }
OLDNEW
« no previous file with comments | « no previous file | media/audio/audio_io.h » ('j') | media/audio/linux/alsa_util.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698