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

Side by Side Diff: media/audio/win/core_audio_util_win_unittest.cc

Issue 11226057: Adds Core Audio Utility methods for Windows (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Improved comments Created 8 years, 1 month 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
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 "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
16 class CoreAudioUtilWinTest : public ::testing::Test {
17 protected:
18 // The test runs on a COM thread in a multithread apartment (MTA).
tommi (sloooow) - chröme 2012/10/25 08:42:11 nit: s/a multithread/the multithreaded (there is a
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
19 // If we don't initialize the COM library on a thread before using COM,
20 // all function calls will return CO_E_NOTINITIALIZED.
21 CoreAudioUtilWinTest()
22 : com_init_(ScopedCOMInitializer::kMTA) {}
tommi (sloooow) - chröme 2012/10/25 08:42:11 maybe DCHECK(com_init_.succeeded())?
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
23 virtual ~CoreAudioUtilWinTest() {}
24
25 bool CanRunAudioTest() {
26 bool core_audio = CoreAudioIsSupported();
27 int capture_devices = CoreAudioNumberOfActiveDevices(eCapture);
tommi (sloooow) - chröme 2012/10/25 08:42:11 shouldn't you only call these methods if core_audi
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Thanks.
28 int render_devices = CoreAudioNumberOfActiveDevices(eRender);
29 return (core_audio && (capture_devices > 0) && (render_devices > 0));
30 }
31
32 ScopedCOMInitializer com_init_;
33 };
34
35 // --- MMdevice
tommi (sloooow) - chröme 2012/10/25 08:42:11 remove?
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
36
37 TEST_F(CoreAudioUtilWinTest, NumberOfActiveDevices) {
38 if (!CanRunAudioTest())
39 return;
40
41 int render_devices = CoreAudioNumberOfActiveDevices(eRender);
42 EXPECT_GT(render_devices, 0);
43 int capture_devices = CoreAudioNumberOfActiveDevices(eCapture);
44 EXPECT_GT(capture_devices, 0);
45 int total_devices = CoreAudioNumberOfActiveDevices(eAll);
46 EXPECT_EQ(total_devices, render_devices + capture_devices);
47 }
48
49 TEST_F(CoreAudioUtilWinTest, CreateDeviceEnumerator) {
50 if (!CanRunAudioTest())
51 return;
52
53 ScopedComPtr<IMMDeviceEnumerator> enumerator =
54 CoreAudioCreateDeviceEnumerator();
55 EXPECT_TRUE(enumerator);
56 enumerator = CoreAudioCreateDeviceEnumerator();
57 EXPECT_TRUE(enumerator);
58 enumerator.Release();
59 EXPECT_FALSE(enumerator);
tommi (sloooow) - chröme 2012/10/25 08:42:11 this isn't needed since you're not testing ScopedC
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Good point.
60 enumerator = CoreAudioCreateDeviceEnumerator();
61 EXPECT_TRUE(enumerator);
tommi (sloooow) - chröme 2012/10/25 08:42:11 I must be missing something - is the intention of
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Agree. I was trying to test different use cases bu
62 }
63
64 TEST_F(CoreAudioUtilWinTest, CreateDefaultDevice) {
65 if (!CanRunAudioTest())
66 return;
67
68 // Verify all device roles for playback endpoints.
69 ScopedComPtr<IMMDevice> audio_device =
70 CoreAudioCreateDefaultDevice(eRender, eConsole);
71 EXPECT_TRUE(audio_device);
72 EXPECT_EQ(eRender, CoreAudioGetDataFlow(audio_device));
73 CoreAudioCreateDefaultDevice(eRender, eCommunications);
tommi (sloooow) - chröme 2012/10/25 08:42:11 Forgot to assign to audio_device. All the checks
henrika (OOO until Aug 14) 2012/10/25 15:14:08 LOL. Will fix.
74 EXPECT_TRUE(audio_device);
75 CoreAudioCreateDefaultDevice(eRender, eMultimedia);
tommi (sloooow) - chröme 2012/10/25 08:42:11 forgot to assign.
henrika (OOO until Aug 14) 2012/10/25 15:14:08 dito
76 EXPECT_TRUE(audio_device);
77 CoreAudioCreateDefaultDevice(eRender, eCommunications);
tommi (sloooow) - chröme 2012/10/25 08:42:11 forgot to assign... but then again, you've already
henrika (OOO until Aug 14) 2012/10/25 15:14:08 see comment above about being stupid ;-)
78
79 // Verify all device roles for recording endpoints.
80 audio_device = CoreAudioCreateDefaultDevice(eCapture, eConsole);
81 EXPECT_TRUE(audio_device);
82 EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device));
83 audio_device = CoreAudioCreateDefaultDevice(eCapture, eCommunications);
84 EXPECT_TRUE(audio_device);
85 EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device));
86 audio_device = CoreAudioCreateDefaultDevice(eCapture, eMultimedia);
87 EXPECT_TRUE(audio_device);
88 EXPECT_EQ(eCapture, CoreAudioGetDataFlow(audio_device));
89
90 // Only eRender and eCapture are allowed as flow parameter.
91 audio_device = CoreAudioCreateDefaultDevice(eAll, eConsole);
92 EXPECT_FALSE(audio_device);
93 EXPECT_EQ(eAll, CoreAudioGetDataFlow(audio_device));
tommi (sloooow) - chröme 2012/10/25 08:42:11 As a suggestion for writing code like this, I pers
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Cool ;-) Will fix last test as well. Something t
94 }
95
96 TEST_F(CoreAudioUtilWinTest, CreateDevice) {
97 if (!CanRunAudioTest())
98 return;
99
100 // Get name and ID of default device used for playback.
101 ScopedComPtr<IMMDevice> default_render_device =
102 CoreAudioCreateDefaultDevice(eRender, eConsole);
103 AudioDeviceName default_render_name;
104 HRESULT hr = CoreAudioGetDeviceName(default_render_device,
105 &default_render_name);
106 EXPECT_TRUE(SUCCEEDED(hr));
tommi (sloooow) - chröme 2012/10/25 08:42:11 nit: just pass the entire function call to EXPECT_
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Nice.
107
108 // Use the uniqe ID as input to CoreAudioCreateDevice() and create a
109 // corresponding IMMDevice.
110 ScopedComPtr<IMMDevice> audio_device =
111 CoreAudioCreateDevice(default_render_name.unique_id);
112 EXPECT_TRUE(audio_device);
113
114 // Verify that the two IMMDevice interfaces represents the same endpoint
115 // by comparing their unique IDs.
116 AudioDeviceName device_name;
117 hr = CoreAudioGetDeviceName(audio_device, &device_name);
118 EXPECT_TRUE(SUCCEEDED(hr));
tommi (sloooow) - chröme 2012/10/25 08:42:11 same here and elsewhere.
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
119 EXPECT_STREQ(default_render_name.unique_id.c_str(),
120 device_name.unique_id.c_str());
tommi (sloooow) - chröme 2012/10/25 08:42:11 doesn't this work? EXPECT_EQ(default_render_name.u
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Yes. In fact it does ;-)
121 }
122
123 TEST_F(CoreAudioUtilWinTest, GetDefaultDeviceName) {
124 if (!CanRunAudioTest())
125 return;
126
127 // Get name and ID of default device used for playback.
128 ScopedComPtr<IMMDevice> audio_device =
129 CoreAudioCreateDefaultDevice(eRender, eConsole);
130 AudioDeviceName device_name;
131 HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name);
132 EXPECT_TRUE(SUCCEEDED(hr));
133 EXPECT_FALSE(device_name.device_name.empty());
134 EXPECT_FALSE(device_name.unique_id.empty());
135
tommi (sloooow) - chröme 2012/10/25 08:42:11 remove
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
136
137 // Get name and ID of default communication device used for playback.
138 audio_device = CoreAudioCreateDefaultDevice(eRender, eCommunications);
139 hr = CoreAudioGetDeviceName(audio_device, &device_name);
140 EXPECT_TRUE(SUCCEEDED(hr));
141 EXPECT_FALSE(device_name.device_name.empty());
142 EXPECT_FALSE(device_name.unique_id.empty());
143
144 // Get name and ID of default device used for recording.
145 audio_device = CoreAudioCreateDefaultDevice(eCapture, eConsole);
146 hr = CoreAudioGetDeviceName(audio_device, &device_name);
147 EXPECT_TRUE(SUCCEEDED(hr));
148 EXPECT_FALSE(device_name.device_name.empty());
149 EXPECT_FALSE(device_name.unique_id.empty());
150
151 // Get name and ID of default communication device used for recording.
152 audio_device = CoreAudioCreateDefaultDevice(eCapture, eCommunications);
tommi (sloooow) - chröme 2012/10/25 08:42:11 you could use the same data driven approach in thi
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
153 hr = CoreAudioGetDeviceName(audio_device, &device_name);
154 EXPECT_TRUE(SUCCEEDED(hr));
155 EXPECT_FALSE(device_name.device_name.empty());
156 EXPECT_FALSE(device_name.unique_id.empty());
157 }
158
159 TEST_F(CoreAudioUtilWinTest, GetFriendlyName) {
160 if (!CanRunAudioTest())
161 return;
162
163 // Get name and ID of default device used for recording.
164 ScopedComPtr<IMMDevice> audio_device =
165 CoreAudioCreateDefaultDevice(eCapture, eConsole);
166 AudioDeviceName device_name;
167 HRESULT hr = CoreAudioGetDeviceName(audio_device, &device_name);
168 EXPECT_TRUE(SUCCEEDED(hr));
169
170 // Use unique ID as input to CoreAudioGetFriendlyName() and compare the
171 // result with the already obtained friendly name for the default capture
172 // device.
173 std::string friendly_name = CoreAudioGetFriendlyName(device_name.unique_id);
174 EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str());
tommi (sloooow) - chröme 2012/10/25 08:42:11 EXPECT_EQ should work for std::string (and no .c_s
henrika (OOO until Aug 14) 2012/10/25 15:14:08 Done.
175
176 // Same test as above but for playback.
177 audio_device = CoreAudioCreateDefaultDevice(eRender, eConsole);
178 hr = CoreAudioGetDeviceName(audio_device, &device_name);
179 EXPECT_TRUE(SUCCEEDED(hr));
180 friendly_name = CoreAudioGetFriendlyName(device_name.unique_id);
181 EXPECT_STREQ(friendly_name.c_str(), device_name.device_name.c_str());
182 }
183
184 TEST_F(CoreAudioUtilWinTest, DeviceIsDefault) {
185 if (!CanRunAudioTest())
186 return;
187
188 // Verify that the default render device is correctly identified as a
189 // default device.
190 ScopedComPtr<IMMDevice> audio_device =
191 CoreAudioCreateDefaultDevice(eRender, eConsole);
192 AudioDeviceName name;
193 HRESULT hr = CoreAudioGetDeviceName(audio_device, &name);
194 EXPECT_TRUE(SUCCEEDED(hr));
195 const std::string id = name.unique_id;
tommi (sloooow) - chröme 2012/10/25 08:42:11 nit: const std::string& (assuming you're only addi
henrika (OOO until Aug 14) 2012/10/25 15:14:08 less line breaking as well
196 EXPECT_TRUE(CoreAudioDeviceIsDefault(eRender, eConsole, id));
197 EXPECT_FALSE(CoreAudioDeviceIsDefault(eCapture, eConsole, id));
198
199 // If more than one active render device exists, one must be default and
200 // the other default communication.
201 if (CoreAudioNumberOfActiveDevices(eRender) > 1) {
202 EXPECT_FALSE(CoreAudioDeviceIsDefault(eRender, eCommunications, id));
203 }
204 }
205
206 // --- WASAPI
207
208 TEST_F(CoreAudioUtilWinTest, CreateClient) {
209 if (!CanRunAudioTest())
210 return;
211
212 // Create an audio client for the default render device.
213 ScopedComPtr<IMMDevice> device =
214 CoreAudioCreateDefaultDevice(eRender, eConsole);
215 EXPECT_EQ(eRender, CoreAudioGetDataFlow(device));
216 ScopedComPtr<IAudioClient> client = CoreAudioCreateClient(device);
217 EXPECT_TRUE(client);
tommi (sloooow) - chröme 2012/10/25 08:42:11 nit: EXPECT_TRUE(CoreAudioCreateClient(device));
218
219 // Create an audio client for the default capture device.
220 device = CoreAudioCreateDefaultDevice(eCapture, eConsole);
tommi (sloooow) - chröme 2012/10/25 08:42:11 this looks like a copy/paste of the block above, s
henrika (OOO until Aug 14) 2012/10/25 15:14:08 OK ;-)
221 EXPECT_EQ(eCapture, CoreAudioGetDataFlow(device));
222 client = CoreAudioCreateClient(device);
223 EXPECT_TRUE(client);
224 }
225
226 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698