| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/sync_socket.h" | 10 #include "base/sync_socket.h" |
| 11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 12 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 12 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 13 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" | 13 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" |
| 14 #include "content/browser/renderer_host/media/audio_renderer_host.h" | 14 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 15 #include "content/browser/renderer_host/media/media_stream_manager.h" | 15 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 16 #include "content/browser/renderer_host/media/mock_media_observer.h" | 16 #include "content/browser/renderer_host/media/mock_media_observer.h" |
| 17 #include "content/common/media/audio_messages.h" | 17 #include "content/common/media/audio_messages.h" |
| 18 #include "content/common/media/media_stream_options.h" | 18 #include "content/common/media/media_stream_options.h" |
| 19 #include "ipc/ipc_message_utils.h" | 19 #include "ipc/ipc_message_utils.h" |
| 20 #include "media/audio/audio_manager.h" | 20 #include "media/audio/audio_manager.h" |
| 21 #include "media/audio/audio_manager_base.h" | 21 #include "media/audio/audio_manager_base.h" |
| 22 #include "media/audio/fake_audio_output_stream.h" | 22 #include "media/audio/fake_audio_output_stream.h" |
| 23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 using ::testing::_; | 27 using ::testing::_; |
| 28 using ::testing::AtLeast; | 28 using ::testing::Assign; |
| 29 using ::testing::DoAll; | 29 using ::testing::DoAll; |
| 30 using ::testing::InSequence; | |
| 31 using ::testing::NotNull; | 30 using ::testing::NotNull; |
| 32 using ::testing::Return; | |
| 33 using ::testing::SaveArg; | |
| 34 using ::testing::SetArgumentPointee; | |
| 35 | 31 |
| 36 namespace content { | 32 namespace content { |
| 37 | 33 |
| 38 static const int kRenderProcessId = 1; | 34 static const int kRenderProcessId = 1; |
| 39 static const int kRenderViewId = 4; | 35 static const int kRenderViewId = 4; |
| 40 static const int kStreamId = 50; | 36 static const int kStreamId = 50; |
| 41 | 37 |
| 42 class MockAudioMirroringManager : public AudioMirroringManager { | 38 class MockAudioMirroringManager : public AudioMirroringManager { |
| 43 public: | 39 public: |
| 44 MockAudioMirroringManager() {} | 40 MockAudioMirroringManager() {} |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 152 |
| 157 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | 153 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); |
| 158 }; | 154 }; |
| 159 | 155 |
| 160 ACTION_P(QuitMessageLoop, message_loop) { | 156 ACTION_P(QuitMessageLoop, message_loop) { |
| 161 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 157 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 162 } | 158 } |
| 163 | 159 |
| 164 class AudioRendererHostTest : public testing::Test { | 160 class AudioRendererHostTest : public testing::Test { |
| 165 public: | 161 public: |
| 166 AudioRendererHostTest() {} | 162 AudioRendererHostTest() : is_stream_active_(false) {} |
| 167 | 163 |
| 168 protected: | 164 protected: |
| 169 virtual void SetUp() { | 165 virtual void SetUp() { |
| 170 // Create a message loop so AudioRendererHost can use it. | 166 // Create a message loop so AudioRendererHost can use it. |
| 171 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); | 167 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); |
| 172 | 168 |
| 173 // Claim to be on both the UI and IO threads to pass all the DCHECKS. | 169 // Claim to be on both the UI and IO threads to pass all the DCHECKS. |
| 174 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 170 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 175 message_loop_.get())); | 171 message_loop_.get())); |
| 176 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, | 172 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 192 host_->OnChannelClosing(); | 188 host_->OnChannelClosing(); |
| 193 | 189 |
| 194 // Release the reference to the mock object. The object will be destructed | 190 // Release the reference to the mock object. The object will be destructed |
| 195 // on message_loop_. | 191 // on message_loop_. |
| 196 host_ = NULL; | 192 host_ = NULL; |
| 197 | 193 |
| 198 // We need to continue running message_loop_ to complete all destructions. | 194 // We need to continue running message_loop_ to complete all destructions. |
| 199 SyncWithAudioThread(); | 195 SyncWithAudioThread(); |
| 200 audio_manager_.reset(); | 196 audio_manager_.reset(); |
| 201 | 197 |
| 198 // Make sure the stream has been deleted before continuing. |
| 199 while (is_stream_active_) |
| 200 message_loop_->Run(); |
| 201 |
| 202 io_thread_.reset(); | 202 io_thread_.reset(); |
| 203 ui_thread_.reset(); | 203 ui_thread_.reset(); |
| 204 | 204 |
| 205 // Delete the IO message loop. This will cause the MediaStreamManager to be | 205 // Delete the IO message loop. This will cause the MediaStreamManager to be |
| 206 // notified so it will stop its device thread and device managers. | 206 // notified so it will stop its device thread and device managers. |
| 207 message_loop_.reset(); | 207 message_loop_.reset(); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void Create() { | 210 void Create(bool unified_stream) { |
| 211 EXPECT_CALL(*observer_, | 211 EXPECT_CALL(*observer_, |
| 212 OnSetAudioStreamStatus(_, kStreamId, "created")); | 212 OnSetAudioStreamStatus(_, kStreamId, "created")); |
| 213 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _)) | 213 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _)) |
| 214 .WillOnce(QuitMessageLoop(message_loop_.get())); | 214 .WillOnce(DoAll(Assign(&is_stream_active_, true), |
| 215 QuitMessageLoop(message_loop_.get()))); |
| 215 EXPECT_CALL(mirroring_manager_, | 216 EXPECT_CALL(mirroring_manager_, |
| 216 AddDiverter(kRenderProcessId, kRenderViewId, NotNull())) | 217 AddDiverter(kRenderProcessId, kRenderViewId, NotNull())) |
| 217 .RetiresOnSaturation(); | 218 .RetiresOnSaturation(); |
| 218 | 219 |
| 219 // Send a create stream message to the audio output stream and wait until | 220 // Send a create stream message to the audio output stream and wait until |
| 220 // we receive the created message. | 221 // we receive the created message. |
| 221 host_->OnCreateStream(kStreamId, | 222 int session_id; |
| 222 kRenderViewId, | 223 media::AudioParameters params; |
| 223 0, | 224 if (unified_stream) { |
| 224 media::AudioParameters( | 225 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to |
| 225 media::AudioParameters::AUDIO_FAKE, | 226 // pass the permission check. |
| 226 media::CHANNEL_LAYOUT_STEREO, | 227 session_id = AudioInputDeviceManager::kFakeOpenSessionId; |
| 227 media::AudioParameters::kAudioCDSampleRate, 16, | 228 params = media::AudioParameters( |
| 228 media::AudioParameters::kAudioCDSampleRate / 10)); | 229 media::AudioParameters::AUDIO_FAKE, |
| 230 media::CHANNEL_LAYOUT_STEREO, |
| 231 2, |
| 232 media::AudioParameters::kAudioCDSampleRate, 16, |
| 233 media::AudioParameters::kAudioCDSampleRate / 10); |
| 234 } else { |
| 235 session_id = 0; |
| 236 params = media::AudioParameters( |
| 237 media::AudioParameters::AUDIO_FAKE, |
| 238 media::CHANNEL_LAYOUT_STEREO, |
| 239 media::AudioParameters::kAudioCDSampleRate, 16, |
| 240 media::AudioParameters::kAudioCDSampleRate / 10); |
| 241 } |
| 242 host_->OnCreateStream(kStreamId, kRenderViewId, session_id, params); |
| 229 message_loop_->Run(); | 243 message_loop_->Run(); |
| 230 | 244 |
| 231 // At some point in the future, a corresponding RemoveDiverter() call must | 245 // At some point in the future, a corresponding RemoveDiverter() call must |
| 232 // be made. | |
| 233 EXPECT_CALL(mirroring_manager_, | |
| 234 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull())) | |
| 235 .RetiresOnSaturation(); | |
| 236 | |
| 237 // All created streams should ultimately be closed. | |
| 238 EXPECT_CALL(*observer_, | |
| 239 OnSetAudioStreamStatus(_, kStreamId, "closed")); | |
| 240 | |
| 241 // Expect the audio stream will be deleted at some later point. | |
| 242 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId)); | |
| 243 } | |
| 244 | |
| 245 void CreateUnifiedStream() { | |
| 246 EXPECT_CALL(*observer_, | |
| 247 OnSetAudioStreamStatus(_, kStreamId, "created")); | |
| 248 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _)) | |
| 249 .WillOnce(QuitMessageLoop(message_loop_.get())); | |
| 250 EXPECT_CALL(mirroring_manager_, | |
| 251 AddDiverter(kRenderProcessId, kRenderViewId, NotNull())) | |
| 252 .RetiresOnSaturation(); | |
| 253 // Send a create stream message to the audio output stream and wait until | |
| 254 // we receive the created message. | |
| 255 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to | |
| 256 // pass the permission check. | |
| 257 host_->OnCreateStream(kStreamId, | |
| 258 kRenderViewId, | |
| 259 AudioInputDeviceManager::kFakeOpenSessionId, | |
| 260 media::AudioParameters( | |
| 261 media::AudioParameters::AUDIO_FAKE, | |
| 262 media::CHANNEL_LAYOUT_STEREO, | |
| 263 2, | |
| 264 media::AudioParameters::kAudioCDSampleRate, 16, | |
| 265 media::AudioParameters::kAudioCDSampleRate / 10)); | |
| 266 message_loop_->Run(); | |
| 267 | |
| 268 // At some point in the future, a corresponding RemoveDiverter() call must | |
| 269 // be made. | 246 // be made. |
| 270 EXPECT_CALL(mirroring_manager_, | 247 EXPECT_CALL(mirroring_manager_, |
| 271 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull())) | 248 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull())) |
| 272 .RetiresOnSaturation(); | 249 .RetiresOnSaturation(); |
| 273 | 250 |
| 274 // All created streams should ultimately be closed. | 251 // All created streams should ultimately be closed. |
| 275 EXPECT_CALL(*observer_, | 252 EXPECT_CALL(*observer_, |
| 276 OnSetAudioStreamStatus(_, kStreamId, "closed")); | 253 OnSetAudioStreamStatus(_, kStreamId, "closed")); |
| 277 | 254 |
| 278 // Expect the audio stream will be deleted at some later point. | 255 // Expect the audio stream will be deleted at some later point. |
| 279 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId)); | 256 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId)) |
| 257 .WillOnce(DoAll(Assign(&is_stream_active_, false), |
| 258 QuitMessageLoop(message_loop_.get()))); |
| 280 } | 259 } |
| 281 | 260 |
| 282 void Close() { | 261 void Close() { |
| 283 // Send a message to AudioRendererHost to tell it we want to close the | 262 // Send a message to AudioRendererHost to tell it we want to close the |
| 284 // stream. | 263 // stream. |
| 285 host_->OnCloseStream(kStreamId); | 264 host_->OnCloseStream(kStreamId); |
| 286 message_loop_->RunUntilIdle(); | 265 message_loop_->Run(); |
| 287 } | 266 } |
| 288 | 267 |
| 289 void Play() { | 268 void Play() { |
| 290 EXPECT_CALL(*observer_, | 269 EXPECT_CALL(*observer_, |
| 291 OnSetAudioStreamPlaying(_, kStreamId, true)); | 270 OnSetAudioStreamPlaying(_, kStreamId, true)); |
| 292 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId)) | 271 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId)) |
| 293 .WillOnce(QuitMessageLoop(message_loop_.get())); | 272 .WillOnce(QuitMessageLoop(message_loop_.get())); |
| 294 | 273 |
| 295 host_->OnPlayStream(kStreamId); | 274 host_->OnPlayStream(kStreamId); |
| 296 message_loop_->Run(); | 275 message_loop_->Run(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 private: | 339 private: |
| 361 scoped_ptr<MockMediaInternals> observer_; | 340 scoped_ptr<MockMediaInternals> observer_; |
| 362 MockAudioMirroringManager mirroring_manager_; | 341 MockAudioMirroringManager mirroring_manager_; |
| 363 scoped_refptr<MockAudioRendererHost> host_; | 342 scoped_refptr<MockAudioRendererHost> host_; |
| 364 scoped_ptr<base::MessageLoop> message_loop_; | 343 scoped_ptr<base::MessageLoop> message_loop_; |
| 365 scoped_ptr<BrowserThreadImpl> io_thread_; | 344 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 366 scoped_ptr<BrowserThreadImpl> ui_thread_; | 345 scoped_ptr<BrowserThreadImpl> ui_thread_; |
| 367 scoped_ptr<media::AudioManager> audio_manager_; | 346 scoped_ptr<media::AudioManager> audio_manager_; |
| 368 scoped_ptr<MediaStreamManager> media_stream_manager_; | 347 scoped_ptr<MediaStreamManager> media_stream_manager_; |
| 369 | 348 |
| 349 bool is_stream_active_; |
| 350 |
| 370 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); | 351 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); |
| 371 }; | 352 }; |
| 372 | 353 |
| 373 TEST_F(AudioRendererHostTest, CreateAndClose) { | 354 TEST_F(AudioRendererHostTest, CreateAndClose) { |
| 374 Create(); | 355 Create(false); |
| 375 Close(); | 356 Close(); |
| 376 } | 357 } |
| 377 | 358 |
| 378 // Simulate the case where a stream is not properly closed. | 359 // Simulate the case where a stream is not properly closed. |
| 379 TEST_F(AudioRendererHostTest, CreateAndShutdown) { | 360 TEST_F(AudioRendererHostTest, CreateAndShutdown) { |
| 380 Create(); | 361 Create(false); |
| 381 } | 362 } |
| 382 | 363 |
| 383 TEST_F(AudioRendererHostTest, CreatePlayAndClose) { | 364 TEST_F(AudioRendererHostTest, CreatePlayAndClose) { |
| 384 Create(); | 365 Create(false); |
| 385 Play(); | 366 Play(); |
| 386 Close(); | 367 Close(); |
| 387 } | 368 } |
| 388 | 369 |
| 389 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) { | 370 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) { |
| 390 Create(); | 371 Create(false); |
| 391 Play(); | 372 Play(); |
| 392 Pause(); | 373 Pause(); |
| 393 Close(); | 374 Close(); |
| 394 } | 375 } |
| 395 | 376 |
| 396 TEST_F(AudioRendererHostTest, SetVolume) { | 377 TEST_F(AudioRendererHostTest, SetVolume) { |
| 397 Create(); | 378 Create(false); |
| 398 SetVolume(0.5); | 379 SetVolume(0.5); |
| 399 Play(); | 380 Play(); |
| 400 Pause(); | 381 Pause(); |
| 401 Close(); | 382 Close(); |
| 402 } | 383 } |
| 403 | 384 |
| 404 // Simulate the case where a stream is not properly closed. | 385 // Simulate the case where a stream is not properly closed. |
| 405 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { | 386 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { |
| 406 Create(); | 387 Create(false); |
| 407 Play(); | 388 Play(); |
| 408 } | 389 } |
| 409 | 390 |
| 410 // Simulate the case where a stream is not properly closed. | 391 // Simulate the case where a stream is not properly closed. |
| 411 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { | 392 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { |
| 412 Create(); | 393 Create(false); |
| 413 Play(); | 394 Play(); |
| 414 Pause(); | 395 Pause(); |
| 415 } | 396 } |
| 416 | 397 |
| 417 TEST_F(AudioRendererHostTest, SimulateError) { | 398 TEST_F(AudioRendererHostTest, SimulateError) { |
| 418 Create(); | 399 Create(false); |
| 419 Play(); | 400 Play(); |
| 420 SimulateError(); | 401 SimulateError(); |
| 421 } | 402 } |
| 422 | 403 |
| 423 // Simulate the case when an error is generated on the browser process, | 404 // Simulate the case when an error is generated on the browser process, |
| 424 // the audio device is closed but the render process try to close the | 405 // the audio device is closed but the render process try to close the |
| 425 // audio stream again. | 406 // audio stream again. |
| 426 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) { | 407 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) { |
| 427 Create(); | 408 Create(false); |
| 428 Play(); | 409 Play(); |
| 429 SimulateError(); | 410 SimulateError(); |
| 430 Close(); | 411 Close(); |
| 431 } | 412 } |
| 432 | 413 |
| 433 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) { | 414 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) { |
| 434 CreateUnifiedStream(); | 415 Create(true); |
| 435 Close(); | 416 Close(); |
| 436 } | 417 } |
| 437 | 418 |
| 438 // TODO(hclam): Add tests for data conversation in low latency mode. | 419 // TODO(hclam): Add tests for data conversation in low latency mode. |
| 439 | 420 |
| 440 } // namespace content | 421 } // namespace content |
| OLD | NEW |