audio_manager_unittest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// found in the LICENSE file. 458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/environment.h" 658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/logging.h" 758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "media/audio/audio_manager.h" 9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "media/audio/audio_manager_base.h" 1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_LINUX) 1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "media/audio/linux/audio_manager_linux.h" 1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // defined(OS_LINUX) 1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_WIN) 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/win/scoped_com_initializer.h" 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "media/audio/win/audio_manager_win.h" 19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "media/audio/win/wavein_input_win.h" 20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif 2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(USE_PULSEAUDIO) 2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "media/audio/pulse/audio_manager_pulse.h" 2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // defined(USE_PULSEAUDIO) 2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace media { 2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Test fixture which allows us to override the default enumeration API on 29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Windows. 30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class AudioManagerTest 31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) : public ::testing::Test { 32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) protected: 33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioManagerTest() 34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) : audio_manager_(AudioManager::Create()) 35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(OS_WIN) 36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) , com_init_(base::win::ScopedCOMInitializer::kMTA) 37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif 38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) { 3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_WIN) 42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool SetMMDeviceEnumeration() { 43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); 44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Windows Wave is used as default if Windows XP was detected => 45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // return false since MMDevice is not supported on XP. 46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) 47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return false; 48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); 50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return true; 51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void SetWaveEnumeration() { 54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); 55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) amw->SetEnumerationType(AudioManagerWin::kWaveEnumeration); 56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) std::string GetDeviceIdFromPCMWaveInAudioInputStream( 59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const std::string& device_id) { 60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); 61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioParameters parameters( 62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioParameters::kAudioCDSampleRate, 16, 64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 1024); 65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<PCMWaveInAudioInputStream> stream( 66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) static_cast<PCMWaveInAudioInputStream*>( 67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) amw->CreatePCMWaveInAudioInputStream(parameters, device_id))); 68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return stream.get() ? stream->device_id_ : std::string(); 69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif 71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Helper method which verifies that the device list starts with a valid 73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // default record followed by non-default device names. 74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) static void CheckDeviceNames(const AudioDeviceNames& device_names) { 75d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VLOG(2) << "Got " << device_names.size() << " audio devices."; 76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!device_names.empty()) { 77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames::const_iterator it = device_names.begin(); 78d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The first device in the list should always be the default device. 80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName), 81d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) it->device_name); 82d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id); 83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) ++it; 84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Other devices should have non-empty name and id and should not contain 86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // default name or id. 87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) while (it != device_names.end()) { 88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_FALSE(it->device_name.empty()); 89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_FALSE(it->unique_id.empty()); 90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VLOG(2) << "Device ID(" << it->unique_id 91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) << "), label: " << it->device_name; 92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName), 93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) it->device_name); 94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId), 95d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) it->unique_id); 96d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) ++it; 97d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } else { 99d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Log a warning so we can see the status on the build bots. No need to 100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // break the test though since this does successfully test the code and 101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // some failure cases. 102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "No input devices detected"; 10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool CanRunInputTest() { 107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return audio_manager_->HasAudioInputDevices(); 10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) bool CanRunOutputTest() { 111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return audio_manager_->HasAudioOutputDevices(); 11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 114d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) scoped_ptr<AudioManager> audio_manager_; 11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_WIN) 117d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The MMDevice API requires COM to be initialized on the current thread. 118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) base::win::ScopedCOMInitializer com_init_; 119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif 120d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}; 121d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 122d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Test that devices can be enumerated. 123d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateInputDevices) { 124d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 125d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 126d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 127d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 128d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 129d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 130d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 131d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 132d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Test that devices can be enumerated. 133d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateOutputDevices) { 134d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunOutputTest()) 135d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 136d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 137d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 138d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioOutputDeviceNames(&device_names); 139d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Run additional tests for Windows since enumeration can be done using 143d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// two different APIs. MMDevice is default for Vista and higher and Wave 144d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// is default for XP and lower. 145d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(OS_WIN) 146d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 147d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Override default enumeration API and force usage of Windows MMDevice. 148d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// This test will only run on Windows Vista and higher. 149d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateInputDevicesWinMMDevice) { 150d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 151d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 152d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 153d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 154d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!SetMMDeviceEnumeration()) { 155d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Usage of MMDevice will fail on XP and lower. 156d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "MM device enumeration is not supported."; 157d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 158d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 159d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 160d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 161d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 162d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 163d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateOutputDevicesWinMMDevice) { 164d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunOutputTest()) 165d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 166d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 167d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 168d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!SetMMDeviceEnumeration()) { 169d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Usage of MMDevice will fail on XP and lower. 170d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "MM device enumeration is not supported."; 171d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 172d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 173d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioOutputDeviceNames(&device_names); 174d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 175d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 176d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 177d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Override default enumeration API and force usage of Windows Wave. 178d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// This test will run on Windows XP, Windows Vista and Windows 7. 179d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateInputDevicesWinWave) { 180d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 181d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 182d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 183d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 184d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) SetWaveEnumeration(); 185d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 186d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 187d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 188d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 189d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateOutputDevicesWinWave) { 190d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunOutputTest()) 191d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 192d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 193d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 194d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) SetWaveEnumeration(); 195d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioOutputDeviceNames(&device_names); 196d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 197d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 198d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 199d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, WinXPDeviceIdUnchanged) { 200d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 201d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 202d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 203d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames xp_device_names; 204d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) SetWaveEnumeration(); 205d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&xp_device_names); 206d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(xp_device_names); 207d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 208d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Device ID should remain unchanged, including the default device ID. 209d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) for (AudioDeviceNames::iterator i = xp_device_names.begin(); 210d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) i != xp_device_names.end(); ++i) { 211d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_EQ(i->unique_id, 212d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id)); 213d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 214d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 215d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 216d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, ConvertToWinXPInputDeviceId) { 217d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 218d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 219d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 220d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!SetMMDeviceEnumeration()) { 221d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Usage of MMDevice will fail on XP and lower. 222d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "MM device enumeration is not supported."; 223d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 224d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 225d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 226d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 227d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 228d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 229d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 230d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) for (AudioDeviceNames::iterator i = device_names.begin(); 231d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) i != device_names.end(); ++i) { 232d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) std::string converted_id = 233d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id); 234d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (i == device_names.begin()) { 235d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // The first in the list is the default device ID, which should not be 236d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // changed when passed to PCMWaveInAudioInputStream. 237d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_EQ(i->unique_id, converted_id); 238d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } else { 239d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // MMDevice-style device IDs should be converted to WaveIn-style device 240d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // IDs. 241d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) EXPECT_NE(i->unique_id, converted_id); 24258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 24358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 244d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 245d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // defined(OS_WIN) 247d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 248d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(USE_PULSEAUDIO) 249d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// On Linux, there are two implementations available and both can 250d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// sometimes be tested on a single system. These tests specifically 251d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// test Pulseaudio. 252d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 253d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateInputDevicesPulseaudio) { 254d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 255d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 256d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 257d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_.reset(AudioManagerPulse::Create()); 258d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (audio_manager_.get()) { 259d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 260d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 261d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 262d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } else { 263d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "No pulseaudio on this system."; 264d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 267d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateOutputDevicesPulseaudio) { 268d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunOutputTest()) 269d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 270d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 271d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_.reset(AudioManagerPulse::Create()); 272d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (audio_manager_.get()) { 273d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 274d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioOutputDeviceNames(&device_names); 275d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 276d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } else { 277d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) LOG(WARNING) << "No pulseaudio on this system."; 278d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 279d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 280d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif // defined(USE_PULSEAUDIO) 281d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 282d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(USE_ALSA) 283d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// On Linux, there are two implementations available and both can 284d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// sometimes be tested on a single system. These tests specifically 285d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// test Alsa. 286d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 287d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateInputDevicesAlsa) { 288d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 289d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 290d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 291d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VLOG(2) << "Testing AudioManagerLinux."; 292d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_.reset(new AudioManagerLinux()); 293d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 294d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 295d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 296d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 297d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 298d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AudioManagerTest, EnumerateOutputDevicesAlsa) { 299d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunOutputTest()) 300d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return; 301d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 302d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) VLOG(2) << "Testing AudioManagerLinux."; 303d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_.reset(new AudioManagerLinux()); 304d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioDeviceNames device_names; 305d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioOutputDeviceNames(&device_names); 306d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) CheckDeviceNames(device_names); 307d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 308d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif // defined(USE_ALSA) 309d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 31058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) { 31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) 312d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest()) 31358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return; 31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 315d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) AudioParameters params = audio_manager_->GetDefaultOutputStreamParameters(); 31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) EXPECT_TRUE(params.IsValid()); 31758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // defined(OS_WIN) || defined(OS_MACOSX) 31858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 31958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) { 32158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) 322d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!CanRunInputTest() || !CanRunOutputTest()) 32358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return; 32458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) AudioDeviceNames device_names; 326d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAudioInputDeviceNames(&device_names); 32758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) bool found_an_associated_device = false; 32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) for (AudioDeviceNames::iterator it = device_names.begin(); 32958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) it != device_names.end(); 33058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ++it) { 33158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) EXPECT_FALSE(it->unique_id.empty()); 33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) EXPECT_FALSE(it->device_name.empty()); 33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) std::string output_device_id( 334d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) audio_manager_->GetAssociatedOutputDeviceID(it->unique_id)); 33558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) if (!output_device_id.empty()) { 33658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) VLOG(2) << it->unique_id << " matches with " << output_device_id; 33758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) found_an_associated_device = true; 33858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 33958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 34058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 34158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) EXPECT_TRUE(found_an_associated_device); 34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // defined(OS_WIN) || defined(OS_MACOSX) 34358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 34458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} // namespace media 346