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 audio_manager_ = |
68 base::ThreadTaskRunnerHandle::Get()); | 72 media::AudioManager::CreateForTesting(audio_thread_.task_runner()); |
69 // Flush the message loop to ensure proper initialization of AudioManager. | 73 // Flush the message loop to ensure proper initialization of AudioManager. |
70 base::RunLoop().RunUntilIdle(); | 74 base::RunLoop().RunUntilIdle(); |
71 | 75 |
72 manager_ = new AudioInputDeviceManager(audio_manager_.get()); | 76 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
| 77 manager_ = new AudioInputDeviceManager(audio_system_.get()); |
73 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); | 78 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
74 manager_->RegisterListener(audio_input_listener_.get()); | 79 manager_->RegisterListener(audio_input_listener_.get()); |
75 | 80 |
76 // Use fake devices. | 81 // Use fake devices. |
77 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 1", | 82 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 1", |
78 "fake_device_1"); | 83 "fake_device_1"); |
79 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 2", | 84 devices_.emplace_back(MEDIA_DEVICE_AUDIO_CAPTURE, "Fake Device 2", |
80 "fake_device_2"); | 85 "fake_device_2"); |
81 | 86 |
82 // Wait until we get the list. | 87 // Wait until we get the list. |
83 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
84 } | 89 } |
85 | 90 |
86 void TearDown() override { | 91 void TearDown() override { |
87 manager_->UnregisterListener(audio_input_listener_.get()); | 92 manager_->UnregisterListener(audio_input_listener_.get()); |
88 } | 93 } |
89 | 94 |
| 95 void WaitForOpenCompletion() { |
| 96 media::WaitableMessageLoopEvent event; |
| 97 audio_thread_.task_runner()->PostTaskAndReply( |
| 98 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
| 99 // Runs the loop and waits for the |audio_thread_| to call event's |
| 100 // closure. |
| 101 event.RunAndWait(); |
| 102 base::RunLoop().RunUntilIdle(); |
| 103 } |
| 104 |
90 TestBrowserThreadBundle thread_bundle_; | 105 TestBrowserThreadBundle thread_bundle_; |
| 106 base::Thread audio_thread_; |
91 scoped_refptr<AudioInputDeviceManager> manager_; | 107 scoped_refptr<AudioInputDeviceManager> manager_; |
92 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 108 std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
93 media::ScopedAudioManagerPtr audio_manager_; | 109 media::ScopedAudioManagerPtr audio_manager_; |
| 110 std::unique_ptr<media::AudioSystem> audio_system_; |
94 StreamDeviceInfoArray devices_; | 111 StreamDeviceInfoArray devices_; |
95 | 112 |
96 private: | 113 private: |
97 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); | 114 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); |
98 }; | 115 }; |
99 | 116 |
100 // Opens and closes the devices. | 117 // Opens and closes the devices. |
101 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 118 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
102 ASSERT_FALSE(devices_.empty()); | 119 ASSERT_FALSE(devices_.empty()); |
103 | 120 |
104 InSequence s; | 121 InSequence s; |
105 | 122 |
106 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 123 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
107 iter != devices_.end(); ++iter) { | 124 iter != devices_.end(); ++iter) { |
108 // Opens/closes the devices. | 125 // Opens/closes the devices. |
109 int session_id = manager_->Open(iter->device); | 126 int session_id = manager_->Open(iter->device); |
110 | 127 |
111 // Expected mock call with expected return value. | 128 // Expected mock call with expected return value. |
112 EXPECT_CALL(*audio_input_listener_, | 129 EXPECT_CALL(*audio_input_listener_, |
113 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 130 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
114 .Times(1); | 131 .Times(1); |
115 // Waits for the callback. | 132 // Waits for the callback. |
116 base::RunLoop().RunUntilIdle(); | 133 WaitForOpenCompletion(); |
117 | 134 |
118 manager_->Close(session_id); | 135 manager_->Close(session_id); |
119 EXPECT_CALL(*audio_input_listener_, | 136 EXPECT_CALL(*audio_input_listener_, |
120 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 137 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
121 .Times(1); | 138 .Times(1); |
122 | 139 |
123 // Waits for the callback. | 140 // Waits for the callback. |
124 base::RunLoop().RunUntilIdle(); | 141 base::RunLoop().RunUntilIdle(); |
125 } | 142 } |
126 } | 143 } |
(...skipping 12 matching lines...) Expand all Loading... |
139 iter != devices_.end(); ++iter, ++index) { | 156 iter != devices_.end(); ++iter, ++index) { |
140 // Opens the devices. | 157 // Opens the devices. |
141 session_id[index] = manager_->Open(iter->device); | 158 session_id[index] = manager_->Open(iter->device); |
142 | 159 |
143 // Expected mock call with expected returned value. | 160 // Expected mock call with expected returned value. |
144 EXPECT_CALL(*audio_input_listener_, | 161 EXPECT_CALL(*audio_input_listener_, |
145 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 162 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
146 .Times(1); | 163 .Times(1); |
147 | 164 |
148 // Waits for the callback. | 165 // Waits for the callback. |
149 base::RunLoop().RunUntilIdle(); | 166 WaitForOpenCompletion(); |
150 } | 167 } |
151 | 168 |
152 // Checks if the session_ids are unique. | 169 // Checks if the session_ids are unique. |
153 for (size_t i = 0; i < devices_.size() - 1; ++i) { | 170 for (size_t i = 0; i < devices_.size() - 1; ++i) { |
154 for (size_t k = i + 1; k < devices_.size(); ++k) { | 171 for (size_t k = i + 1; k < devices_.size(); ++k) { |
155 EXPECT_TRUE(session_id[i] != session_id[k]); | 172 EXPECT_TRUE(session_id[i] != session_id[k]); |
156 } | 173 } |
157 } | 174 } |
158 | 175 |
159 for (size_t i = 0; i < devices_.size(); ++i) { | 176 for (size_t i = 0; i < devices_.size(); ++i) { |
(...skipping 16 matching lines...) Expand all Loading... |
176 std::string device_name("device_doesnt_exist"); | 193 std::string device_name("device_doesnt_exist"); |
177 std::string device_id("id_doesnt_exist"); | 194 std::string device_id("id_doesnt_exist"); |
178 MediaStreamDevice dummy_device(stream_type, device_id, device_name); | 195 MediaStreamDevice dummy_device(stream_type, device_id, device_name); |
179 | 196 |
180 int session_id = manager_->Open(dummy_device); | 197 int session_id = manager_->Open(dummy_device); |
181 EXPECT_CALL(*audio_input_listener_, | 198 EXPECT_CALL(*audio_input_listener_, |
182 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 199 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
183 .Times(1); | 200 .Times(1); |
184 | 201 |
185 // Waits for the callback. | 202 // Waits for the callback. |
186 base::RunLoop().RunUntilIdle(); | 203 WaitForOpenCompletion(); |
187 } | 204 } |
188 | 205 |
189 // Opens default device twice. | 206 // Opens default device twice. |
190 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { | 207 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { |
191 ASSERT_FALSE(devices_.empty()); | 208 ASSERT_FALSE(devices_.empty()); |
192 | 209 |
193 InSequence s; | 210 InSequence s; |
194 | 211 |
195 // Opens and closes the default device twice. | 212 // Opens and closes the default device twice. |
196 int first_session_id = manager_->Open(devices_.front().device); | 213 int first_session_id = manager_->Open(devices_.front().device); |
197 int second_session_id = manager_->Open(devices_.front().device); | 214 int second_session_id = manager_->Open(devices_.front().device); |
198 | 215 |
199 // Expected mock calls with expected returned values. | 216 // Expected mock calls with expected returned values. |
200 EXPECT_NE(first_session_id, second_session_id); | 217 EXPECT_NE(first_session_id, second_session_id); |
201 EXPECT_CALL(*audio_input_listener_, | 218 EXPECT_CALL(*audio_input_listener_, |
202 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 219 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
203 .Times(1); | 220 .Times(1); |
204 EXPECT_CALL(*audio_input_listener_, | 221 EXPECT_CALL(*audio_input_listener_, |
205 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 222 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
206 .Times(1); | 223 .Times(1); |
207 // Waits for the callback. | 224 // Waits for the callback. |
208 base::RunLoop().RunUntilIdle(); | 225 WaitForOpenCompletion(); |
209 | 226 |
210 manager_->Close(first_session_id); | 227 manager_->Close(first_session_id); |
211 manager_->Close(second_session_id); | 228 manager_->Close(second_session_id); |
212 EXPECT_CALL(*audio_input_listener_, | 229 EXPECT_CALL(*audio_input_listener_, |
213 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) | 230 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) |
214 .Times(1); | 231 .Times(1); |
215 EXPECT_CALL(*audio_input_listener_, | 232 EXPECT_CALL(*audio_input_listener_, |
216 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 233 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
217 .Times(1); | 234 .Times(1); |
218 // Waits for the callback. | 235 // Waits for the callback. |
(...skipping 12 matching lines...) Expand all Loading... |
231 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById | 248 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById |
232 // for each device. | 249 // for each device. |
233 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 250 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
234 iter != devices_.end(); ++iter, ++index) { | 251 iter != devices_.end(); ++iter, ++index) { |
235 // Note that no DeviceStopped() notification for Event Handler as we have | 252 // Note that no DeviceStopped() notification for Event Handler as we have |
236 // stopped the device before calling close. | 253 // stopped the device before calling close. |
237 session_id[index] = manager_->Open(iter->device); | 254 session_id[index] = manager_->Open(iter->device); |
238 EXPECT_CALL(*audio_input_listener_, | 255 EXPECT_CALL(*audio_input_listener_, |
239 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 256 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
240 .Times(1); | 257 .Times(1); |
241 base::RunLoop().RunUntilIdle(); | 258 WaitForOpenCompletion(); |
242 | 259 |
243 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( | 260 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( |
244 session_id[index]); | 261 session_id[index]); |
245 DCHECK(info); | 262 DCHECK(info); |
246 EXPECT_EQ(iter->device.id, info->device.id); | 263 EXPECT_EQ(iter->device.id, info->device.id); |
247 manager_->Close(session_id[index]); | 264 manager_->Close(session_id[index]); |
248 EXPECT_CALL(*audio_input_listener_, | 265 EXPECT_CALL(*audio_input_listener_, |
249 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 266 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
250 .Times(1); | 267 .Times(1); |
251 base::RunLoop().RunUntilIdle(); | 268 base::RunLoop().RunUntilIdle(); |
252 } | 269 } |
253 } | 270 } |
254 | 271 |
255 // Access an invalid session. | 272 // Access an invalid session. |
256 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { | 273 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { |
257 InSequence s; | 274 InSequence s; |
258 | 275 |
259 // Opens the first device. | 276 // Opens the first device. |
260 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 277 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
261 int session_id = manager_->Open(iter->device); | 278 int session_id = manager_->Open(iter->device); |
262 EXPECT_CALL(*audio_input_listener_, | 279 EXPECT_CALL(*audio_input_listener_, |
263 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 280 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
264 .Times(1); | 281 .Times(1); |
265 base::RunLoop().RunUntilIdle(); | 282 WaitForOpenCompletion(); |
266 | 283 |
267 // Access a non-opened device. | 284 // Access a non-opened device. |
268 // This should fail and return an empty StreamDeviceInfo. | 285 // This should fail and return an empty StreamDeviceInfo. |
269 int invalid_session_id = session_id + 1; | 286 int invalid_session_id = session_id + 1; |
270 const StreamDeviceInfo* info = | 287 const StreamDeviceInfo* info = |
271 manager_->GetOpenedDeviceInfoById(invalid_session_id); | 288 manager_->GetOpenedDeviceInfoById(invalid_session_id); |
272 DCHECK(!info); | 289 DCHECK(!info); |
273 | 290 |
274 manager_->Close(session_id); | 291 manager_->Close(session_id); |
275 EXPECT_CALL(*audio_input_listener_, | 292 EXPECT_CALL(*audio_input_listener_, |
276 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 293 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
277 .Times(1); | 294 .Times(1); |
278 base::RunLoop().RunUntilIdle(); | 295 base::RunLoop().RunUntilIdle(); |
279 } | 296 } |
280 | 297 |
| 298 // No crash during shutdown while Open() is in progress (i.e there are tasks |
| 299 // queued on audio thread). |
| 300 TEST_F(MAYBE_AudioInputDeviceManagerTest, NoCrashOnShutdown) { |
| 301 // Loops through the devices and calls Open() for each device. |
| 302 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
| 303 iter != devices_.end(); ++iter) { |
| 304 manager_->Open(iter->device); |
| 305 } |
| 306 } |
| 307 |
281 } // namespace content | 308 } // namespace content |
OLD | NEW |