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/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "content/public/common/media_stream_request.h" | 21 #include "content/public/common/media_stream_request.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "media/audio/audio_manager_base.h" | 23 #include "media/audio/audio_manager_base.h" |
24 #include "media/audio/audio_system_impl.h" | |
24 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
26 #include "media/base/test_helpers.h" | |
25 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
27 | 29 |
28 using testing::InSequence; | 30 using testing::InSequence; |
29 | 31 |
30 namespace content { | 32 namespace content { |
31 | 33 |
32 class MockAudioInputDeviceManagerListener | 34 class MockAudioInputDeviceManagerListener |
33 : public MediaStreamProviderListener { | 35 : public MediaStreamProviderListener { |
34 public: | 36 public: |
(...skipping 16 matching lines...) Expand all Loading... | |
51 // AudioInputDeviceManager::Open() must be called on the audio thread. | 53 // AudioInputDeviceManager::Open() must be called on the audio thread. |
52 // This test suite must be modified to run on Android. | 54 // This test suite must be modified to run on Android. |
53 #if defined(OS_ANDROID) | 55 #if defined(OS_ANDROID) |
54 #define MAYBE_AudioInputDeviceManagerTest DISABLED_AudioInputDeviceManagerTest | 56 #define MAYBE_AudioInputDeviceManagerTest DISABLED_AudioInputDeviceManagerTest |
55 #else | 57 #else |
56 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest | 58 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest |
57 #endif | 59 #endif |
58 | 60 |
59 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { | 61 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { |
60 public: | 62 public: |
61 MAYBE_AudioInputDeviceManagerTest() {} | 63 MAYBE_AudioInputDeviceManagerTest() : audio_thread_("AudioSystemThread") { |
64 audio_thread_.StartAndWaitForTesting(); | |
65 } | |
62 | 66 |
63 protected: | 67 protected: |
64 void SetUp() override { | 68 void SetUp() override { |
65 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 69 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
66 switches::kUseFakeDeviceForMediaStream); | 70 switches::kUseFakeDeviceForMediaStream); |
67 audio_manager_ = media::AudioManager::CreateForTesting( | 71 // AudioInputDeviceManager accesses AudioSystem from IO thread, so it never |
68 base::ThreadTaskRunnerHandle::Get()); | 72 // runs on the same thread with it, even on Mac. |
73 audio_manager_ = | |
74 media::AudioManager::CreateForTesting(audio_thread_.task_runner()); | |
69 // Flush the message loop to ensure proper initialization of AudioManager. | 75 // Flush the message loop to ensure proper initialization of AudioManager. |
70 base::RunLoop().RunUntilIdle(); | 76 base::RunLoop().RunUntilIdle(); |
71 | 77 |
72 manager_ = new AudioInputDeviceManager(audio_manager_.get()); | 78 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
79 manager_ = new AudioInputDeviceManager(audio_system_.get()); | |
73 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); | 80 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
74 manager_->RegisterListener(audio_input_listener_.get()); | 81 manager_->RegisterListener(audio_input_listener_.get()); |
75 | 82 |
76 // Use fake devices. | 83 // Use fake devices. |
77 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 1", | 84 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 1", |
78 "fake_device_1"); | 85 "fake_device_1"); |
79 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 2", | 86 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 2", |
80 "fake_device_2"); | 87 "fake_device_2"); |
81 | 88 |
82 // Wait until we get the list. | 89 // Wait until we get the list. |
83 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
84 } | 91 } |
85 | 92 |
86 void TearDown() override { | 93 void TearDown() override { |
87 manager_->UnregisterListener(audio_input_listener_.get()); | 94 manager_->UnregisterListener(audio_input_listener_.get()); |
95 audio_system_.reset(); | |
96 audio_manager_.reset(); | |
97 audio_thread_.Stop(); | |
98 } | |
99 | |
100 void WaitForOpenCompletion() { | |
101 media::WaitableMessageLoopEvent event; | |
102 audio_thread_.task_runner()->PostTaskAndReply( | |
103 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | |
104 // Runs the loop and waits for the |audio_thread_| to call event's | |
105 // closure. | |
106 event.RunAndWait(); | |
107 base::RunLoop().RunUntilIdle(); | |
88 } | 108 } |
89 | 109 |
90 TestBrowserThreadBundle thread_bundle_; | 110 TestBrowserThreadBundle thread_bundle_; |
111 base::Thread audio_thread_; | |
91 scoped_refptr<AudioInputDeviceManager> manager_; | 112 scoped_refptr<AudioInputDeviceManager> manager_; |
92 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 113 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
93 media::ScopedAudioManagerPtr audio_manager_; | 114 media::ScopedAudioManagerPtr audio_manager_; |
115 std::unique_ptr<media::AudioSystem> audio_system_; | |
94 StreamDeviceInfoArray devices_; | 116 StreamDeviceInfoArray devices_; |
95 | 117 |
96 private: | 118 private: |
97 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); | 119 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); |
98 }; | 120 }; |
99 | 121 |
100 // Opens and closes the devices. | 122 // Opens and closes the devices. |
101 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 123 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
102 ASSERT_FALSE(devices_.empty()); | 124 ASSERT_FALSE(devices_.empty()); |
103 | 125 |
104 InSequence s; | 126 InSequence s; |
105 | 127 |
106 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 128 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
107 iter != devices_.end(); ++iter) { | 129 iter != devices_.end(); ++iter) { |
108 // Opens/closes the devices. | 130 // Opens/closes the devices. |
109 int session_id = manager_->Open(iter->device); | 131 int session_id = manager_->Open(iter->device); |
110 | 132 |
111 // Expected mock call with expected return value. | 133 // Expected mock call with expected return value. |
112 EXPECT_CALL(*audio_input_listener_, | 134 EXPECT_CALL(*audio_input_listener_, |
113 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 135 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
114 .Times(1); | 136 .Times(1); |
115 // Waits for the callback. | 137 // Waits for the callback. |
116 base::RunLoop().RunUntilIdle(); | 138 WaitForOpenCompletion(); |
117 | 139 |
118 manager_->Close(session_id); | 140 manager_->Close(session_id); |
119 EXPECT_CALL(*audio_input_listener_, | 141 EXPECT_CALL(*audio_input_listener_, |
120 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 142 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
121 .Times(1); | 143 .Times(1); |
122 | 144 |
123 // Waits for the callback. | 145 // Waits for the callback. |
124 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
125 } | 147 } |
126 } | 148 } |
(...skipping 12 matching lines...) Expand all Loading... | |
139 iter != devices_.end(); ++iter, ++index) { | 161 iter != devices_.end(); ++iter, ++index) { |
140 // Opens the devices. | 162 // Opens the devices. |
141 session_id[index] = manager_->Open(iter->device); | 163 session_id[index] = manager_->Open(iter->device); |
142 | 164 |
143 // Expected mock call with expected returned value. | 165 // Expected mock call with expected returned value. |
144 EXPECT_CALL(*audio_input_listener_, | 166 EXPECT_CALL(*audio_input_listener_, |
145 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 167 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
146 .Times(1); | 168 .Times(1); |
147 | 169 |
148 // Waits for the callback. | 170 // Waits for the callback. |
149 base::RunLoop().RunUntilIdle(); | 171 WaitForOpenCompletion(); |
150 } | 172 } |
151 | 173 |
152 // Checks if the session_ids are unique. | 174 // Checks if the session_ids are unique. |
153 for (size_t i = 0; i < devices_.size() - 1; ++i) { | 175 for (size_t i = 0; i < devices_.size() - 1; ++i) { |
154 for (size_t k = i + 1; k < devices_.size(); ++k) { | 176 for (size_t k = i + 1; k < devices_.size(); ++k) { |
155 EXPECT_TRUE(session_id[i] != session_id[k]); | 177 EXPECT_TRUE(session_id[i] != session_id[k]); |
156 } | 178 } |
157 } | 179 } |
158 | 180 |
159 for (size_t i = 0; i < devices_.size(); ++i) { | 181 for (size_t i = 0; i < devices_.size(); ++i) { |
(...skipping 16 matching lines...) Expand all Loading... | |
176 std::string device_name("device_doesnt_exist"); | 198 std::string device_name("device_doesnt_exist"); |
177 std::string device_id("id_doesnt_exist"); | 199 std::string device_id("id_doesnt_exist"); |
178 MediaStreamDevice dummy_device(stream_type, device_id, device_name); | 200 MediaStreamDevice dummy_device(stream_type, device_id, device_name); |
179 | 201 |
180 int session_id = manager_->Open(dummy_device); | 202 int session_id = manager_->Open(dummy_device); |
181 EXPECT_CALL(*audio_input_listener_, | 203 EXPECT_CALL(*audio_input_listener_, |
182 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 204 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
183 .Times(1); | 205 .Times(1); |
184 | 206 |
185 // Waits for the callback. | 207 // Waits for the callback. |
186 base::RunLoop().RunUntilIdle(); | 208 WaitForOpenCompletion(); |
187 } | 209 } |
188 | 210 |
189 // Opens default device twice. | 211 // Opens default device twice. |
190 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { | 212 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { |
191 ASSERT_FALSE(devices_.empty()); | 213 ASSERT_FALSE(devices_.empty()); |
192 | 214 |
193 InSequence s; | 215 InSequence s; |
194 | 216 |
195 // Opens and closes the default device twice. | 217 // Opens and closes the default device twice. |
196 int first_session_id = manager_->Open(devices_.front().device); | 218 int first_session_id = manager_->Open(devices_.front().device); |
197 int second_session_id = manager_->Open(devices_.front().device); | 219 int second_session_id = manager_->Open(devices_.front().device); |
198 | 220 |
199 // Expected mock calls with expected returned values. | 221 // Expected mock calls with expected returned values. |
200 EXPECT_NE(first_session_id, second_session_id); | 222 EXPECT_NE(first_session_id, second_session_id); |
201 EXPECT_CALL(*audio_input_listener_, | 223 EXPECT_CALL(*audio_input_listener_, |
202 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 224 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
203 .Times(1); | 225 .Times(1); |
204 EXPECT_CALL(*audio_input_listener_, | 226 EXPECT_CALL(*audio_input_listener_, |
205 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 227 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
206 .Times(1); | 228 .Times(1); |
207 // Waits for the callback. | 229 // Waits for the callback. |
208 base::RunLoop().RunUntilIdle(); | 230 WaitForOpenCompletion(); |
209 | 231 |
210 manager_->Close(first_session_id); | 232 manager_->Close(first_session_id); |
211 manager_->Close(second_session_id); | 233 manager_->Close(second_session_id); |
212 EXPECT_CALL(*audio_input_listener_, | 234 EXPECT_CALL(*audio_input_listener_, |
213 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 235 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
214 .Times(1); | 236 .Times(1); |
215 EXPECT_CALL(*audio_input_listener_, | 237 EXPECT_CALL(*audio_input_listener_, |
216 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 238 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
217 .Times(1); | 239 .Times(1); |
218 // Waits for the callback. | 240 // Waits for the callback. |
(...skipping 12 matching lines...) Expand all Loading... | |
231 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById | 253 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById |
232 // for each device. | 254 // for each device. |
233 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 255 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
234 iter != devices_.end(); ++iter, ++index) { | 256 iter != devices_.end(); ++iter, ++index) { |
235 // Note that no DeviceStopped() notification for Event Handler as we have | 257 // Note that no DeviceStopped() notification for Event Handler as we have |
236 // stopped the device before calling close. | 258 // stopped the device before calling close. |
237 session_id[index] = manager_->Open(iter->device); | 259 session_id[index] = manager_->Open(iter->device); |
238 EXPECT_CALL(*audio_input_listener_, | 260 EXPECT_CALL(*audio_input_listener_, |
239 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 261 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
240 .Times(1); | 262 .Times(1); |
241 base::RunLoop().RunUntilIdle(); | 263 WaitForOpenCompletion(); |
242 | 264 |
243 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( | 265 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( |
244 session_id[index]); | 266 session_id[index]); |
245 DCHECK(info); | 267 DCHECK(info); |
246 EXPECT_EQ(iter->device.id, info->device.id); | 268 EXPECT_EQ(iter->device.id, info->device.id); |
247 manager_->Close(session_id[index]); | 269 manager_->Close(session_id[index]); |
248 EXPECT_CALL(*audio_input_listener_, | 270 EXPECT_CALL(*audio_input_listener_, |
249 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 271 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
250 .Times(1); | 272 .Times(1); |
251 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
252 } | 274 } |
253 } | 275 } |
254 | 276 |
255 // Access an invalid session. | 277 // Access an invalid session. |
256 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { | 278 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { |
257 InSequence s; | 279 InSequence s; |
258 | 280 |
259 // Opens the first device. | 281 // Opens the first device. |
260 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 282 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
261 int session_id = manager_->Open(iter->device); | 283 int session_id = manager_->Open(iter->device); |
262 EXPECT_CALL(*audio_input_listener_, | 284 EXPECT_CALL(*audio_input_listener_, |
263 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 285 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
264 .Times(1); | 286 .Times(1); |
265 base::RunLoop().RunUntilIdle(); | 287 WaitForOpenCompletion(); |
266 | 288 |
267 // Access a non-opened device. | 289 // Access a non-opened device. |
268 // This should fail and return an empty StreamDeviceInfo. | 290 // This should fail and return an empty StreamDeviceInfo. |
269 int invalid_session_id = session_id + 1; | 291 int invalid_session_id = session_id + 1; |
270 const StreamDeviceInfo* info = | 292 const StreamDeviceInfo* info = |
271 manager_->GetOpenedDeviceInfoById(invalid_session_id); | 293 manager_->GetOpenedDeviceInfoById(invalid_session_id); |
272 DCHECK(!info); | 294 DCHECK(!info); |
273 | 295 |
274 manager_->Close(session_id); | 296 manager_->Close(session_id); |
275 EXPECT_CALL(*audio_input_listener_, | 297 EXPECT_CALL(*audio_input_listener_, |
276 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 298 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
277 .Times(1); | 299 .Times(1); |
278 base::RunLoop().RunUntilIdle(); | 300 base::RunLoop().RunUntilIdle(); |
279 } | 301 } |
280 | 302 |
303 // No crash during shutdown while Open() is in progress (i.e there are tasks | |
304 // queued on audio thread). | |
305 TEST_F(MAYBE_AudioInputDeviceManagerTest, NoCrashOnShutdown) { | |
o1ka
2017/04/05 14:21:22
Does not make sense now when there are no weak poi
| |
306 // Loops through the devices and calls Open() for each device. | |
307 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | |
308 iter != devices_.end(); ++iter) { | |
309 manager_->Open(iter->device); | |
310 } | |
311 } | |
312 | |
281 } // namespace content | 313 } // namespace content |
OLD | NEW |