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 "base/memory/scoped_ptr.h" |
| 6 #include "base/synchronization/waitable_event.h" |
| 7 #include "base/win/scoped_com_initializer.h" |
| 8 #include "media/audio/win/core_audio_util_win.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using base::win::ScopedCOMInitializer; |
| 13 |
| 14 namespace media { |
| 15 namespace win { |
| 16 |
| 17 class CoreAudioUtilWinTest : public ::testing::Test { |
| 18 protected: |
| 19 // The test runs on a COM thread in a multithread apartment (MTA). |
| 20 // If we don't initialize the COM library on a thread before using COM, |
| 21 // all function calls will return CO_E_NOTINITIALIZED. |
| 22 CoreAudioUtilWinTest() |
| 23 : com_init_(ScopedCOMInitializer::kMTA) {} |
| 24 virtual ~CoreAudioUtilWinTest() {} |
| 25 |
| 26 bool CanRunAudioTest() { |
| 27 bool core_audio = CoreAudioIsSupported(); |
| 28 int capture_devices = NumberOfActiveAudioDevices(eCapture); |
| 29 int render_devices = NumberOfActiveAudioDevices(eRender); |
| 30 return (core_audio && (capture_devices > 0) && (render_devices > 0)); |
| 31 } |
| 32 |
| 33 ScopedCOMInitializer com_init_; |
| 34 }; |
| 35 |
| 36 // --- MMdevice |
| 37 |
| 38 TEST_F(CoreAudioUtilWinTest, NumberOfActiveAudioDevices) { |
| 39 if (!CanRunAudioTest()) |
| 40 return; |
| 41 |
| 42 int render_devices = NumberOfActiveAudioDevices(eRender); |
| 43 EXPECT_GT(render_devices, 0); |
| 44 int capture_devices = NumberOfActiveAudioDevices(eCapture); |
| 45 EXPECT_GT(capture_devices, 0); |
| 46 int total_devices = NumberOfActiveAudioDevices(eAll); |
| 47 EXPECT_EQ(total_devices, render_devices + capture_devices); |
| 48 } |
| 49 |
| 50 TEST_F(CoreAudioUtilWinTest, CreateDeviceEnumerator) { |
| 51 if (!CanRunAudioTest()) |
| 52 return; |
| 53 |
| 54 ScopedComPtr<IMMDeviceEnumerator> enumerator = CreateDeviceEnumerator(); |
| 55 EXPECT_TRUE(enumerator); |
| 56 enumerator = CreateDeviceEnumerator(); |
| 57 EXPECT_TRUE(enumerator); |
| 58 enumerator.Release(); |
| 59 EXPECT_FALSE(enumerator); |
| 60 enumerator = CreateDeviceEnumerator(); |
| 61 EXPECT_TRUE(enumerator); |
| 62 } |
| 63 |
| 64 TEST_F(CoreAudioUtilWinTest, CreateDefaultAudioDevice) { |
| 65 if (!CanRunAudioTest()) |
| 66 return; |
| 67 |
| 68 // Verify all device roles for playback endpoints. |
| 69 ScopedComPtr<IMMDevice> audio_device = |
| 70 CreateDefaultAudioDevice(eRender, eConsole); |
| 71 EXPECT_TRUE(audio_device); |
| 72 EXPECT_EQ(eRender, GetDataFlow(audio_device)); |
| 73 CreateDefaultAudioDevice(eRender, eCommunications); |
| 74 EXPECT_TRUE(audio_device); |
| 75 CreateDefaultAudioDevice(eRender, eMultimedia); |
| 76 EXPECT_TRUE(audio_device); |
| 77 CreateDefaultAudioDevice(eRender, eCommunications); |
| 78 |
| 79 // Verify all device roles for recording endpoints. |
| 80 audio_device = CreateDefaultAudioDevice(eCapture, eConsole); |
| 81 EXPECT_TRUE(audio_device); |
| 82 EXPECT_EQ(eCapture, GetDataFlow(audio_device)); |
| 83 audio_device = CreateDefaultAudioDevice(eCapture, eCommunications); |
| 84 EXPECT_TRUE(audio_device); |
| 85 EXPECT_EQ(eCapture, GetDataFlow(audio_device)); |
| 86 audio_device = CreateDefaultAudioDevice(eCapture, eMultimedia); |
| 87 EXPECT_TRUE(audio_device); |
| 88 EXPECT_EQ(eCapture, GetDataFlow(audio_device)); |
| 89 |
| 90 // Only eRender and eCapture are allowed as flow parameter. |
| 91 audio_device = CreateDefaultAudioDevice(eAll, eConsole); |
| 92 EXPECT_FALSE(audio_device); |
| 93 EXPECT_EQ(eAll, GetDataFlow(audio_device)); |
| 94 } |
| 95 |
| 96 TEST_F(CoreAudioUtilWinTest, CreateAudioDevice) { |
| 97 if (!CanRunAudioTest()) |
| 98 return; |
| 99 |
| 100 // Get name and ID of default device used for playback. |
| 101 ScopedComPtr<IMMDevice> default_render_device = |
| 102 CreateDefaultAudioDevice(eRender, eConsole); |
| 103 AudioDeviceName default_render_name; |
| 104 HRESULT hr = GetAudioDeviceName(default_render_device, &default_render_name); |
| 105 EXPECT_TRUE(SUCCEEDED(hr)); |
| 106 |
| 107 // Use the uniqe ID as input to CreateAudioDevice() and create a |
| 108 // corresponding IMMDevice. |
| 109 ScopedComPtr<IMMDevice> audio_device = |
| 110 CreateAudioDevice(default_render_name.unique_id); |
| 111 EXPECT_TRUE(audio_device); |
| 112 |
| 113 // Verify that the two IMMDevice interfaces represents the same endpoint |
| 114 // by comparing their unique IDs. |
| 115 AudioDeviceName device_name; |
| 116 hr = GetAudioDeviceName(audio_device, &device_name); |
| 117 EXPECT_TRUE(SUCCEEDED(hr)); |
| 118 EXPECT_STREQ(default_render_name.unique_id.c_str(), |
| 119 device_name.unique_id.c_str()); |
| 120 } |
| 121 |
| 122 TEST_F(CoreAudioUtilWinTest, GetDefaultAudioDeviceName) { |
| 123 if (!CanRunAudioTest()) |
| 124 return; |
| 125 |
| 126 // Get name and ID of default device used for playback. |
| 127 ScopedComPtr<IMMDevice> audio_device = |
| 128 CreateDefaultAudioDevice(eRender, eConsole); |
| 129 AudioDeviceName device_name; |
| 130 HRESULT hr = GetAudioDeviceName(audio_device, &device_name); |
| 131 EXPECT_TRUE(SUCCEEDED(hr)); |
| 132 EXPECT_FALSE(device_name.device_name.empty()); |
| 133 EXPECT_FALSE(device_name.unique_id.empty()); |
| 134 |
| 135 |
| 136 // Get name and ID of default communication device used for playback. |
| 137 audio_device = CreateDefaultAudioDevice(eRender, eCommunications); |
| 138 hr = GetAudioDeviceName(audio_device, &device_name); |
| 139 EXPECT_TRUE(SUCCEEDED(hr)); |
| 140 EXPECT_FALSE(device_name.device_name.empty()); |
| 141 EXPECT_FALSE(device_name.unique_id.empty()); |
| 142 |
| 143 // Get name and ID of default device used for recording. |
| 144 audio_device = CreateDefaultAudioDevice(eCapture, eConsole); |
| 145 hr = GetAudioDeviceName(audio_device, &device_name); |
| 146 EXPECT_TRUE(SUCCEEDED(hr)); |
| 147 EXPECT_FALSE(device_name.device_name.empty()); |
| 148 EXPECT_FALSE(device_name.unique_id.empty()); |
| 149 |
| 150 // Get name and ID of default communication device used for recording. |
| 151 audio_device = CreateDefaultAudioDevice(eCapture, eCommunications); |
| 152 hr = GetAudioDeviceName(audio_device, &device_name); |
| 153 EXPECT_TRUE(SUCCEEDED(hr)); |
| 154 EXPECT_FALSE(device_name.device_name.empty()); |
| 155 EXPECT_FALSE(device_name.unique_id.empty()); |
| 156 } |
| 157 |
| 158 TEST_F(CoreAudioUtilWinTest, GetFriendlyName) { |
| 159 if (!CanRunAudioTest()) |
| 160 return; |
| 161 |
| 162 // Get name and ID of default device used for recording. |
| 163 ScopedComPtr<IMMDevice> audio_device = |
| 164 CreateDefaultAudioDevice(eCapture, eConsole); |
| 165 AudioDeviceName device_name; |
| 166 HRESULT hr = GetAudioDeviceName(audio_device, &device_name); |
| 167 EXPECT_TRUE(SUCCEEDED(hr)); |
| 168 |
| 169 // Use unique ID as input to GetFriendlyName() and compare the result |
| 170 // with the already obtained friendly name for the default capture device. |
| 171 std::string friendly_name = GetFriendlyName(device_name.unique_id); |
| 172 EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str()); |
| 173 |
| 174 // Same test as above but for playback. |
| 175 audio_device = CreateDefaultAudioDevice(eRender, eConsole); |
| 176 hr = GetAudioDeviceName(audio_device, &device_name); |
| 177 EXPECT_TRUE(SUCCEEDED(hr)); |
| 178 friendly_name = GetFriendlyName(device_name.unique_id); |
| 179 EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str()); |
| 180 } |
| 181 |
| 182 TEST_F(CoreAudioUtilWinTest, IsDeviceDefault) { |
| 183 if (!CanRunAudioTest()) |
| 184 return; |
| 185 |
| 186 // Verify that the default render device is correctly identified as a |
| 187 // default device. |
| 188 ScopedComPtr<IMMDevice> audio_device = |
| 189 CreateDefaultAudioDevice(eRender, eConsole); |
| 190 AudioDeviceName name; |
| 191 HRESULT hr = GetAudioDeviceName(audio_device, &name); |
| 192 EXPECT_TRUE(SUCCEEDED(hr)); |
| 193 EXPECT_TRUE(IsDeviceDefault(eRender, eConsole, name.unique_id)); |
| 194 EXPECT_FALSE(IsDeviceDefault(eCapture, eConsole, name.unique_id)); |
| 195 |
| 196 // If more than one active render device exists, one must be default and |
| 197 // the other default communication. |
| 198 if (NumberOfActiveAudioDevices(eRender) > 1) { |
| 199 EXPECT_FALSE(IsDeviceDefault(eRender, eCommunications, name.unique_id)); |
| 200 } |
| 201 } |
| 202 |
| 203 // --- WASAPI |
| 204 |
| 205 TEST_F(CoreAudioUtilWinTest, CreateAudioClient) { |
| 206 if (!CanRunAudioTest()) |
| 207 return; |
| 208 |
| 209 // Create an audio client for the default render device. |
| 210 ScopedComPtr<IMMDevice> device = CreateDefaultAudioDevice(eRender, eConsole); |
| 211 EXPECT_EQ(eRender, GetDataFlow(device)); |
| 212 ScopedComPtr<IAudioClient> client = CreateAudioClient(device); |
| 213 EXPECT_TRUE(client); |
| 214 |
| 215 // Create an audio client for the default capture device. |
| 216 device = CreateDefaultAudioDevice(eCapture, eConsole); |
| 217 EXPECT_EQ(eCapture, GetDataFlow(device)); |
| 218 client = CreateAudioClient(device); |
| 219 EXPECT_TRUE(client); |
| 220 } |
| 221 |
| 222 } // namespace win |
| 223 } // namespace media |
OLD | NEW |