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