OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 5 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/thread_task_runner_handle.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "content/browser/browser_thread_impl.h" | |
20 #include "content/public/common/media_stream_request.h" | 20 #include "content/public/common/media_stream_request.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" |
21 #include "media/audio/audio_manager_base.h" | 22 #include "media/audio/audio_manager_base.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 using testing::_; | 26 using testing::_; |
26 using testing::InSequence; | 27 using testing::InSequence; |
27 using testing::SaveArg; | 28 using testing::SaveArg; |
28 using testing::Return; | 29 using testing::Return; |
29 | 30 |
30 namespace content { | 31 namespace content { |
(...skipping 24 matching lines...) Expand all Loading... |
55 #else | 56 #else |
56 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest | 57 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest |
57 #endif | 58 #endif |
58 | 59 |
59 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { | 60 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { |
60 public: | 61 public: |
61 MAYBE_AudioInputDeviceManagerTest() {} | 62 MAYBE_AudioInputDeviceManagerTest() {} |
62 | 63 |
63 protected: | 64 protected: |
64 void SetUp() override { | 65 void SetUp() override { |
65 // The test must run on Browser::IO. | 66 audio_manager_ = media::AudioManager::CreateForTesting( |
66 message_loop_.reset(new base::MessageLoopForIO); | 67 base::ThreadTaskRunnerHandle::Get()); |
67 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 68 // Flush the message loop to ensure proper initialization of AudioManager. |
68 message_loop_.get())); | 69 base::RunLoop().RunUntilIdle(); |
69 audio_manager_.reset(media::AudioManager::CreateForTesting()); | 70 |
70 // Wait for audio thread initialization to complete. Otherwise the | |
71 // enumeration type may not have been set yet. | |
72 base::WaitableEvent event(false, false); | |
73 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( | |
74 &base::WaitableEvent::Signal, base::Unretained(&event))); | |
75 event.Wait(); | |
76 manager_ = new AudioInputDeviceManager(audio_manager_.get()); | 71 manager_ = new AudioInputDeviceManager(audio_manager_.get()); |
77 manager_->UseFakeDevice(); | 72 manager_->UseFakeDevice(); |
78 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); | 73 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
79 manager_->Register(audio_input_listener_.get(), | 74 manager_->Register(audio_input_listener_.get(), |
80 message_loop_->task_runner().get()); | 75 audio_manager_->GetTaskRunner()); |
81 | 76 |
82 // Gets the enumerated device list from the AudioInputDeviceManager. | 77 // Gets the enumerated device list from the AudioInputDeviceManager. |
83 manager_->EnumerateDevices(MEDIA_DEVICE_AUDIO_CAPTURE); | 78 manager_->EnumerateDevices(MEDIA_DEVICE_AUDIO_CAPTURE); |
84 EXPECT_CALL(*audio_input_listener_, | 79 EXPECT_CALL(*audio_input_listener_, |
85 DevicesEnumerated(MEDIA_DEVICE_AUDIO_CAPTURE, _)) | 80 DevicesEnumerated(MEDIA_DEVICE_AUDIO_CAPTURE, _)) |
86 .Times(1) | 81 .Times(1) |
87 .WillOnce(SaveArg<1>(&devices_)); | 82 .WillOnce(SaveArg<1>(&devices_)); |
88 | 83 |
89 // Wait until we get the list. | 84 // Wait until we get the list. |
90 message_loop_->RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
91 } | 86 } |
92 | 87 |
93 void TearDown() override { | 88 void TearDown() override { |
94 manager_->Unregister(); | 89 manager_->Unregister(); |
95 io_thread_.reset(); | |
96 } | 90 } |
97 | 91 |
98 std::unique_ptr<base::MessageLoop> message_loop_; | 92 TestBrowserThreadBundle thread_bundle_; |
99 std::unique_ptr<BrowserThreadImpl> io_thread_; | |
100 scoped_refptr<AudioInputDeviceManager> manager_; | 93 scoped_refptr<AudioInputDeviceManager> manager_; |
101 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 94 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
102 std::unique_ptr<media::AudioManager> audio_manager_; | 95 media::ScopedAudioManagerPtr audio_manager_; |
103 StreamDeviceInfoArray devices_; | 96 StreamDeviceInfoArray devices_; |
104 | 97 |
105 private: | 98 private: |
106 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); | 99 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); |
107 }; | 100 }; |
108 | 101 |
109 // Opens and closes the devices. | 102 // Opens and closes the devices. |
110 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 103 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
111 | 104 |
112 ASSERT_FALSE(devices_.empty()); | 105 ASSERT_FALSE(devices_.empty()); |
113 | 106 |
114 InSequence s; | 107 InSequence s; |
115 | 108 |
116 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 109 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
117 iter != devices_.end(); ++iter) { | 110 iter != devices_.end(); ++iter) { |
118 // Opens/closes the devices. | 111 // Opens/closes the devices. |
119 int session_id = manager_->Open(*iter); | 112 int session_id = manager_->Open(*iter); |
120 | 113 |
121 // Expected mock call with expected return value. | 114 // Expected mock call with expected return value. |
122 EXPECT_CALL(*audio_input_listener_, | 115 EXPECT_CALL(*audio_input_listener_, |
123 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 116 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
124 .Times(1); | 117 .Times(1); |
125 // Waits for the callback. | 118 // Waits for the callback. |
126 message_loop_->RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
127 | 120 |
128 manager_->Close(session_id); | 121 manager_->Close(session_id); |
129 EXPECT_CALL(*audio_input_listener_, | 122 EXPECT_CALL(*audio_input_listener_, |
130 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 123 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
131 .Times(1); | 124 .Times(1); |
132 | 125 |
133 // Waits for the callback. | 126 // Waits for the callback. |
134 message_loop_->RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
135 } | 128 } |
136 } | 129 } |
137 | 130 |
138 // Opens multiple devices at one time and closes them later. | 131 // Opens multiple devices at one time and closes them later. |
139 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenMultipleDevices) { | 132 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenMultipleDevices) { |
140 ASSERT_FALSE(devices_.empty()); | 133 ASSERT_FALSE(devices_.empty()); |
141 | 134 |
142 InSequence s; | 135 InSequence s; |
143 | 136 |
144 int index = 0; | 137 int index = 0; |
145 std::unique_ptr<int[]> session_id(new int[devices_.size()]); | 138 std::unique_ptr<int[]> session_id(new int[devices_.size()]); |
146 | 139 |
147 // Opens the devices in a loop. | 140 // Opens the devices in a loop. |
148 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 141 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
149 iter != devices_.end(); ++iter, ++index) { | 142 iter != devices_.end(); ++iter, ++index) { |
150 // Opens the devices. | 143 // Opens the devices. |
151 session_id[index] = manager_->Open(*iter); | 144 session_id[index] = manager_->Open(*iter); |
152 | 145 |
153 // Expected mock call with expected returned value. | 146 // Expected mock call with expected returned value. |
154 EXPECT_CALL(*audio_input_listener_, | 147 EXPECT_CALL(*audio_input_listener_, |
155 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 148 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
156 .Times(1); | 149 .Times(1); |
157 | 150 |
158 // Waits for the callback. | 151 // Waits for the callback. |
159 message_loop_->RunUntilIdle(); | 152 base::RunLoop().RunUntilIdle(); |
160 } | 153 } |
161 | 154 |
162 // Checks if the session_ids are unique. | 155 // Checks if the session_ids are unique. |
163 for (size_t i = 0; i < devices_.size() - 1; ++i) { | 156 for (size_t i = 0; i < devices_.size() - 1; ++i) { |
164 for (size_t k = i + 1; k < devices_.size(); ++k) { | 157 for (size_t k = i + 1; k < devices_.size(); ++k) { |
165 EXPECT_TRUE(session_id[i] != session_id[k]); | 158 EXPECT_TRUE(session_id[i] != session_id[k]); |
166 } | 159 } |
167 } | 160 } |
168 | 161 |
169 for (size_t i = 0; i < devices_.size(); ++i) { | 162 for (size_t i = 0; i < devices_.size(); ++i) { |
170 // Closes the devices. | 163 // Closes the devices. |
171 manager_->Close(session_id[i]); | 164 manager_->Close(session_id[i]); |
172 EXPECT_CALL(*audio_input_listener_, | 165 EXPECT_CALL(*audio_input_listener_, |
173 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[i])) | 166 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[i])) |
174 .Times(1); | 167 .Times(1); |
175 | 168 |
176 // Waits for the callback. | 169 // Waits for the callback. |
177 message_loop_->RunUntilIdle(); | 170 base::RunLoop().RunUntilIdle(); |
178 } | 171 } |
179 } | 172 } |
180 | 173 |
181 // Opens a non-existing device. | 174 // Opens a non-existing device. |
182 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenNotExistingDevice) { | 175 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenNotExistingDevice) { |
183 InSequence s; | 176 InSequence s; |
184 | 177 |
185 MediaStreamType stream_type = MEDIA_DEVICE_AUDIO_CAPTURE; | 178 MediaStreamType stream_type = MEDIA_DEVICE_AUDIO_CAPTURE; |
186 std::string device_name("device_doesnt_exist"); | 179 std::string device_name("device_doesnt_exist"); |
187 std::string device_id("id_doesnt_exist"); | 180 std::string device_id("id_doesnt_exist"); |
188 int sample_rate(0); | 181 int sample_rate(0); |
189 int channel_config(0); | 182 int channel_config(0); |
190 StreamDeviceInfo dummy_device( | 183 StreamDeviceInfo dummy_device( |
191 stream_type, device_name, device_id, sample_rate, channel_config, 2048); | 184 stream_type, device_name, device_id, sample_rate, channel_config, 2048); |
192 | 185 |
193 int session_id = manager_->Open(dummy_device); | 186 int session_id = manager_->Open(dummy_device); |
194 EXPECT_CALL(*audio_input_listener_, | 187 EXPECT_CALL(*audio_input_listener_, |
195 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 188 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
196 .Times(1); | 189 .Times(1); |
197 | 190 |
198 // Waits for the callback. | 191 // Waits for the callback. |
199 message_loop_->RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
200 } | 193 } |
201 | 194 |
202 // Opens default device twice. | 195 // Opens default device twice. |
203 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { | 196 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { |
204 ASSERT_FALSE(devices_.empty()); | 197 ASSERT_FALSE(devices_.empty()); |
205 | 198 |
206 InSequence s; | 199 InSequence s; |
207 | 200 |
208 // Opens and closes the default device twice. | 201 // Opens and closes the default device twice. |
209 int first_session_id = manager_->Open(devices_.front()); | 202 int first_session_id = manager_->Open(devices_.front()); |
210 int second_session_id = manager_->Open(devices_.front()); | 203 int second_session_id = manager_->Open(devices_.front()); |
211 | 204 |
212 // Expected mock calls with expected returned values. | 205 // Expected mock calls with expected returned values. |
213 EXPECT_NE(first_session_id, second_session_id); | 206 EXPECT_NE(first_session_id, second_session_id); |
214 EXPECT_CALL(*audio_input_listener_, | 207 EXPECT_CALL(*audio_input_listener_, |
215 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 208 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
216 .Times(1); | 209 .Times(1); |
217 EXPECT_CALL(*audio_input_listener_, | 210 EXPECT_CALL(*audio_input_listener_, |
218 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 211 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
219 .Times(1); | 212 .Times(1); |
220 // Waits for the callback. | 213 // Waits for the callback. |
221 message_loop_->RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
222 | 215 |
223 manager_->Close(first_session_id); | 216 manager_->Close(first_session_id); |
224 manager_->Close(second_session_id); | 217 manager_->Close(second_session_id); |
225 EXPECT_CALL(*audio_input_listener_, | 218 EXPECT_CALL(*audio_input_listener_, |
226 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 219 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
227 .Times(1); | 220 .Times(1); |
228 EXPECT_CALL(*audio_input_listener_, | 221 EXPECT_CALL(*audio_input_listener_, |
229 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 222 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
230 .Times(1); | 223 .Times(1); |
231 // Waits for the callback. | 224 // Waits for the callback. |
232 message_loop_->RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
233 } | 226 } |
234 | 227 |
235 // Accesses then closes the sessions after opening the devices. | 228 // Accesses then closes the sessions after opening the devices. |
236 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessAndCloseSession) { | 229 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessAndCloseSession) { |
237 ASSERT_FALSE(devices_.empty()); | 230 ASSERT_FALSE(devices_.empty()); |
238 | 231 |
239 InSequence s; | 232 InSequence s; |
240 | 233 |
241 int index = 0; | 234 int index = 0; |
242 std::unique_ptr<int[]> session_id(new int[devices_.size()]); | 235 std::unique_ptr<int[]> session_id(new int[devices_.size()]); |
243 | 236 |
244 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById | 237 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById |
245 // for each device. | 238 // for each device. |
246 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 239 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
247 iter != devices_.end(); ++iter, ++index) { | 240 iter != devices_.end(); ++iter, ++index) { |
248 // Note that no DeviceStopped() notification for Event Handler as we have | 241 // Note that no DeviceStopped() notification for Event Handler as we have |
249 // stopped the device before calling close. | 242 // stopped the device before calling close. |
250 session_id[index] = manager_->Open(*iter); | 243 session_id[index] = manager_->Open(*iter); |
251 EXPECT_CALL(*audio_input_listener_, | 244 EXPECT_CALL(*audio_input_listener_, |
252 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 245 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
253 .Times(1); | 246 .Times(1); |
254 message_loop_->RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
255 | 248 |
256 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( | 249 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( |
257 session_id[index]); | 250 session_id[index]); |
258 DCHECK(info); | 251 DCHECK(info); |
259 EXPECT_EQ(iter->device.id, info->device.id); | 252 EXPECT_EQ(iter->device.id, info->device.id); |
260 manager_->Close(session_id[index]); | 253 manager_->Close(session_id[index]); |
261 EXPECT_CALL(*audio_input_listener_, | 254 EXPECT_CALL(*audio_input_listener_, |
262 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 255 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
263 .Times(1); | 256 .Times(1); |
264 message_loop_->RunUntilIdle(); | 257 base::RunLoop().RunUntilIdle(); |
265 } | 258 } |
266 } | 259 } |
267 | 260 |
268 // Access an invalid session. | 261 // Access an invalid session. |
269 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { | 262 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { |
270 InSequence s; | 263 InSequence s; |
271 | 264 |
272 // Opens the first device. | 265 // Opens the first device. |
273 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 266 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
274 int session_id = manager_->Open(*iter); | 267 int session_id = manager_->Open(*iter); |
275 EXPECT_CALL(*audio_input_listener_, | 268 EXPECT_CALL(*audio_input_listener_, |
276 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 269 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
277 .Times(1); | 270 .Times(1); |
278 message_loop_->RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
279 | 272 |
280 // Access a non-opened device. | 273 // Access a non-opened device. |
281 // This should fail and return an empty StreamDeviceInfo. | 274 // This should fail and return an empty StreamDeviceInfo. |
282 int invalid_session_id = session_id + 1; | 275 int invalid_session_id = session_id + 1; |
283 const StreamDeviceInfo* info = | 276 const StreamDeviceInfo* info = |
284 manager_->GetOpenedDeviceInfoById(invalid_session_id); | 277 manager_->GetOpenedDeviceInfoById(invalid_session_id); |
285 DCHECK(!info); | 278 DCHECK(!info); |
286 | 279 |
287 manager_->Close(session_id); | 280 manager_->Close(session_id); |
288 EXPECT_CALL(*audio_input_listener_, | 281 EXPECT_CALL(*audio_input_listener_, |
289 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 282 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
290 .Times(1); | 283 .Times(1); |
291 message_loop_->RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
292 } | 285 } |
293 | 286 |
294 } // namespace content | 287 } // namespace content |
OLD | NEW |