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; |
| 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) ? |
| 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"; |
| 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 ais->GetMaxMicVolume(&max_volume); |
| 121 EXPECT_GT(max_volume, 0.0); |
| 122 |
| 123 // Notes that |original_volume| can be higher than |max_volume| on Linux. |
| 124 ais->GetMicVolume(&original_volume); |
| 125 EXPECT_GE(original_volume, 0.0); |
| 126 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 127 EXPECT_LE(original_volume, max_volume); |
| 128 #endif |
| 129 |
| 130 // Tries to set the volume to the |max_volume|. |
| 131 ais->SetMicVolume(max_volume); |
| 132 ais->GetMicVolume(¤t_volume); |
| 133 EXPECT_EQ(max_volume, current_volume); |
| 134 |
| 135 // Tries to set the volume to 0. |
| 136 new_volume = 0.0; |
| 137 ais->SetMicVolume(new_volume); |
| 138 ais->GetMicVolume(¤t_volume); |
| 139 EXPECT_EQ(new_volume, current_volume); |
| 140 |
| 141 // Depending on the step size of the volume control, the value that we get |
| 142 // can be different from what we set. |
| 143 new_volume = max_volume / 2; |
| 144 ais->SetMicVolume(new_volume); |
| 145 ais->GetMicVolume(¤t_volume); |
| 146 EXPECT_LT(current_volume, max_volume); |
| 147 EXPECT_GT(current_volume, 0); |
| 148 |
| 149 // Restores the volume to the original value. |
| 150 ais->SetMicVolume(original_volume); |
| 151 ais->GetMicVolume(¤t_volume); |
| 152 EXPECT_EQ(original_volume, current_volume); |
| 153 |
| 154 ais->Close(); |
| 155 } |
| 156 } |
OLD | NEW |