| 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_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/sync_socket.h" | 16 #include "base/sync_socket.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/media/capture/audio_mirroring_manager.h" | 18 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 19 #include "content/browser/media/media_internals.h" | 19 #include "content/browser/media/media_internals.h" |
| 20 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 20 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 21 #include "content/browser/renderer_host/media/media_stream_manager.h" | 21 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 22 #include "content/common/media/audio_messages.h" | 22 #include "content/common/media/audio_messages.h" |
| 23 #include "content/public/browser/media_device_id.h" | 23 #include "content/public/browser/media_device_id.h" |
| 24 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 25 #include "content/public/test/mock_render_process_host.h" | 25 #include "content/public/test/mock_render_process_host.h" |
| 26 #include "content/public/test/test_browser_context.h" | 26 #include "content/public/test/test_browser_context.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "content/test/mock_audio_stream_registry.h" |
| 28 #include "ipc/ipc_message_utils.h" | 29 #include "ipc/ipc_message_utils.h" |
| 29 #include "media/audio/fake_audio_log_factory.h" | 30 #include "media/audio/fake_audio_log_factory.h" |
| 30 #include "media/audio/fake_audio_manager.h" | 31 #include "media/audio/fake_audio_manager.h" |
| 31 #include "media/base/bind_to_current_loop.h" | 32 #include "media/base/bind_to_current_loop.h" |
| 32 #include "media/base/media_switches.h" | 33 #include "media/base/media_switches.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 36 |
| 36 using ::testing::_; | 37 using ::testing::_; |
| 37 using ::testing::Assign; | 38 using ::testing::Assign; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 56 | 57 |
| 57 void ValidateRenderFrameId(int render_process_id, | 58 void ValidateRenderFrameId(int render_process_id, |
| 58 int render_frame_id, | 59 int render_frame_id, |
| 59 const base::Callback<void(bool)>& callback) { | 60 const base::Callback<void(bool)>& callback) { |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 61 const bool frame_exists = (render_frame_id == kRenderFrameId); | 62 const bool frame_exists = (render_frame_id == kRenderFrameId); |
| 62 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 63 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 63 base::Bind(callback, frame_exists)); | 64 base::Bind(callback, frame_exists)); |
| 64 } | 65 } |
| 65 | 66 |
| 66 | |
| 67 class MockAudioMirroringManager : public AudioMirroringManager { | 67 class MockAudioMirroringManager : public AudioMirroringManager { |
| 68 public: | 68 public: |
| 69 MockAudioMirroringManager() {} | 69 MockAudioMirroringManager() {} |
| 70 virtual ~MockAudioMirroringManager() {} | 70 virtual ~MockAudioMirroringManager() {} |
| 71 | 71 |
| 72 MOCK_METHOD3(AddDiverter, | 72 MOCK_METHOD3(AddDiverter, |
| 73 void(int render_process_id, | 73 void(int render_process_id, |
| 74 int render_frame_id, | 74 int render_frame_id, |
| 75 Diverter* diverter)); | 75 Diverter* diverter)); |
| 76 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); | 76 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 private: | 120 private: |
| 121 std::map<std::string, std::string> associations_; | 121 std::map<std::string, std::string> associations_; |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 } // namespace | 124 } // namespace |
| 125 | 125 |
| 126 class MockAudioRendererHost : public AudioRendererHost { | 126 class MockAudioRendererHost : public AudioRendererHost { |
| 127 public: | 127 public: |
| 128 MockAudioRendererHost(base::RunLoop* auth_run_loop, | 128 MockAudioRendererHost(base::RunLoop* auth_run_loop, |
| 129 int render_process_id, | 129 int render_process_id, |
| 130 AudioStreamRegistry* stream_registry, |
| 130 media::AudioManager* audio_manager, | 131 media::AudioManager* audio_manager, |
| 131 AudioMirroringManager* mirroring_manager, | 132 AudioMirroringManager* mirroring_manager, |
| 132 MediaStreamManager* media_stream_manager, | 133 MediaStreamManager* media_stream_manager, |
| 133 const std::string& salt) | 134 const std::string& salt) |
| 134 : AudioRendererHost(render_process_id, | 135 : AudioRendererHost(render_process_id, |
| 136 stream_registry, |
| 135 audio_manager, | 137 audio_manager, |
| 136 mirroring_manager, | 138 mirroring_manager, |
| 137 media_stream_manager, | 139 media_stream_manager, |
| 138 salt), | 140 salt), |
| 139 shared_memory_length_(0), | 141 shared_memory_length_(0), |
| 140 auth_run_loop_(auth_run_loop) { | 142 auth_run_loop_(auth_run_loop) { |
| 141 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); | 143 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); |
| 142 } | 144 } |
| 143 | 145 |
| 144 // A list of mock methods. | 146 // A list of mock methods. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | 230 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); |
| 229 }; | 231 }; |
| 230 | 232 |
| 231 class AudioRendererHostTest : public testing::Test { | 233 class AudioRendererHostTest : public testing::Test { |
| 232 public: | 234 public: |
| 233 AudioRendererHostTest() | 235 AudioRendererHostTest() |
| 234 : log_factory(base::MakeUnique<media::FakeAudioLogFactory>()), | 236 : log_factory(base::MakeUnique<media::FakeAudioLogFactory>()), |
| 235 audio_manager_(base::MakeUnique<FakeAudioManagerWithAssociations>( | 237 audio_manager_(base::MakeUnique<FakeAudioManagerWithAssociations>( |
| 236 base::ThreadTaskRunnerHandle::Get(), | 238 base::ThreadTaskRunnerHandle::Get(), |
| 237 log_factory.get())), | 239 log_factory.get())), |
| 238 render_process_host_(&browser_context_, &auth_run_loop_) { | 240 render_process_host_(&browser_context_, &auth_run_loop_), |
| 241 stream_registry_(render_process_host_.GetID()) { |
| 239 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 242 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 240 switches::kUseFakeDeviceForMediaStream); | 243 switches::kUseFakeDeviceForMediaStream); |
| 241 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | 244 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 242 host_ = new MockAudioRendererHost( | 245 host_ = new MockAudioRendererHost( |
| 243 &auth_run_loop_, render_process_host_.GetID(), audio_manager_.get(), | 246 &auth_run_loop_, render_process_host_.GetID(), &stream_registry_, |
| 244 &mirroring_manager_, media_stream_manager_.get(), kSalt); | 247 audio_manager_.get(), &mirroring_manager_, media_stream_manager_.get(), |
| 248 kSalt); |
| 245 | 249 |
| 246 // Simulate IPC channel connected. | 250 // Simulate IPC channel connected. |
| 247 host_->set_peer_process_for_testing(base::Process::Current()); | 251 host_->set_peer_process_for_testing(base::Process::Current()); |
| 248 } | 252 } |
| 249 | 253 |
| 250 ~AudioRendererHostTest() override { | 254 ~AudioRendererHostTest() override { |
| 251 // Simulate closing the IPC channel and give the audio thread time to close | 255 // Simulate closing the IPC channel and give the audio thread time to close |
| 252 // the underlying streams. | 256 // the underlying streams. |
| 253 host_->OnChannelClosing(); | 257 host_->OnChannelClosing(); |
| 254 SyncWithAudioThread(); | 258 SyncWithAudioThread(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 ? media::OUTPUT_DEVICE_STATUS_OK | 337 ? media::OUTPUT_DEVICE_STATUS_OK |
| 334 : device_id == kBadDeviceId | 338 : device_id == kBadDeviceId |
| 335 ? media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 339 ? media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
| 336 : media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND; | 340 : media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND; |
| 337 | 341 |
| 338 if (expect_onauthorized) | 342 if (expect_onauthorized) |
| 339 EXPECT_CALL(*host_.get(), | 343 EXPECT_CALL(*host_.get(), |
| 340 OnDeviceAuthorized(kStreamId, expected_device_status, _, _)); | 344 OnDeviceAuthorized(kStreamId, expected_device_status, _, _)); |
| 341 | 345 |
| 342 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { | 346 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { |
| 347 EXPECT_CALL(stream_registry_, RegisterOutputStream(NotNull())); |
| 343 EXPECT_CALL(*host_.get(), WasNotifiedOfCreation(kStreamId, _)); | 348 EXPECT_CALL(*host_.get(), WasNotifiedOfCreation(kStreamId, _)); |
| 344 EXPECT_CALL(mirroring_manager_, AddDiverter(render_process_host_.GetID(), | 349 EXPECT_CALL(mirroring_manager_, AddDiverter(render_process_host_.GetID(), |
| 345 kRenderFrameId, NotNull())) | 350 kRenderFrameId, NotNull())) |
| 346 .RetiresOnSaturation(); | 351 .RetiresOnSaturation(); |
| 347 } | 352 } |
| 348 | 353 |
| 349 // Send a create stream message to the audio output stream and wait until | 354 // Send a create stream message to the audio output stream and wait until |
| 350 // we receive the created message. | 355 // we receive the created message. |
| 351 media::AudioParameters params( | 356 media::AudioParameters params( |
| 352 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, | 357 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, |
| 353 media::AudioParameters::kAudioCDSampleRate, 16, | 358 media::AudioParameters::kAudioCDSampleRate, 16, |
| 354 media::AudioParameters::kAudioCDSampleRate / 10); | 359 media::AudioParameters::kAudioCDSampleRate / 10); |
| 355 int session_id = 0; | 360 int session_id = 0; |
| 356 | 361 |
| 357 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, | 362 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, |
| 358 device_id, security_origin); | 363 device_id, security_origin); |
| 359 if (wait_for_auth) | 364 if (wait_for_auth) |
| 360 auth_run_loop_.Run(); | 365 auth_run_loop_.Run(); |
| 361 | 366 |
| 362 if (!wait_for_auth || | 367 if (!wait_for_auth || |
| 363 expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) | 368 expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) |
| 364 host_->OnCreateStream(kStreamId, kRenderFrameId, params); | 369 host_->OnCreateStream(kStreamId, kRenderFrameId, params); |
| 365 | 370 |
| 366 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) | 371 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { |
| 367 // At some point in the future, a corresponding RemoveDiverter() call must | 372 // At some point in the future, a corresponding RemoveDiverter() call must |
| 368 // be made. | 373 // be made. |
| 374 EXPECT_CALL(stream_registry_, DeregisterOutputStream(NotNull())); |
| 369 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) | 375 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) |
| 370 .RetiresOnSaturation(); | 376 .RetiresOnSaturation(); |
| 377 } |
| 371 SyncWithAudioThread(); | 378 SyncWithAudioThread(); |
| 372 } | 379 } |
| 373 | 380 |
| 374 void RequestDeviceAuthorizationWithBadOrigin(const std::string& device_id) { | 381 void RequestDeviceAuthorizationWithBadOrigin(const std::string& device_id) { |
| 375 int session_id = 0; | 382 int session_id = 0; |
| 376 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, | 383 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, |
| 377 device_id, | 384 device_id, |
| 378 url::Origin(GURL(kBadSecurityOrigin))); | 385 url::Origin(GURL(kBadSecurityOrigin))); |
| 379 SyncWithAudioThread(); | 386 SyncWithAudioThread(); |
| 380 } | 387 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // MediaStreamManager uses a DestructionObserver, so it must outlive the | 514 // MediaStreamManager uses a DestructionObserver, so it must outlive the |
| 508 // TestBrowserThreadBundle. | 515 // TestBrowserThreadBundle. |
| 509 std::unique_ptr<MediaStreamManager> media_stream_manager_; | 516 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 510 TestBrowserThreadBundle thread_bundle_; | 517 TestBrowserThreadBundle thread_bundle_; |
| 511 TestBrowserContext browser_context_; | 518 TestBrowserContext browser_context_; |
| 512 std::unique_ptr<media::FakeAudioLogFactory> log_factory; | 519 std::unique_ptr<media::FakeAudioLogFactory> log_factory; |
| 513 std::unique_ptr<FakeAudioManagerWithAssociations> audio_manager_; | 520 std::unique_ptr<FakeAudioManagerWithAssociations> audio_manager_; |
| 514 MockAudioMirroringManager mirroring_manager_; | 521 MockAudioMirroringManager mirroring_manager_; |
| 515 base::RunLoop auth_run_loop_; | 522 base::RunLoop auth_run_loop_; |
| 516 MockRenderProcessHostWithSignaling render_process_host_; | 523 MockRenderProcessHostWithSignaling render_process_host_; |
| 524 MockAudioStreamRegistry stream_registry_; |
| 517 scoped_refptr<MockAudioRendererHost> host_; | 525 scoped_refptr<MockAudioRendererHost> host_; |
| 518 | 526 |
| 519 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); | 527 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); |
| 520 }; | 528 }; |
| 521 | 529 |
| 522 TEST_F(AudioRendererHostTest, CreateAndClose) { | 530 TEST_F(AudioRendererHostTest, CreateAndClose) { |
| 523 Create(); | 531 Create(); |
| 524 Close(); | 532 Close(); |
| 525 } | 533 } |
| 526 | 534 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 } | 624 } |
| 617 | 625 |
| 618 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { | 626 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { |
| 619 CreateWithInvalidRenderFrameId(); | 627 CreateWithInvalidRenderFrameId(); |
| 620 Close(); | 628 Close(); |
| 621 } | 629 } |
| 622 | 630 |
| 623 // TODO(hclam): Add tests for data conversation in low latency mode. | 631 // TODO(hclam): Add tests for data conversation in low latency mode. |
| 624 | 632 |
| 625 } // namespace content | 633 } // namespace content |
| OLD | NEW |