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