| 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/audio_stream_registry.h" |
| 21 #include "content/browser/renderer_host/media/media_stream_manager.h" | 22 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 22 #include "content/common/media/audio_messages.h" | 23 #include "content/common/media/audio_messages.h" |
| 23 #include "content/public/browser/media_device_id.h" | 24 #include "content/public/browser/media_device_id.h" |
| 24 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
| 25 #include "content/public/test/mock_render_process_host.h" | 26 #include "content/public/test/mock_render_process_host.h" |
| 26 #include "content/public/test/test_browser_context.h" | 27 #include "content/public/test/test_browser_context.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.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" |
| (...skipping 25 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 10 matching lines...) Expand all Loading... |
| 87 | 87 |
| 88 void ShutdownForBadMessage(CrashReportMode crash_report_mode) override { | 88 void ShutdownForBadMessage(CrashReportMode crash_report_mode) override { |
| 89 MockRenderProcessHost::ShutdownForBadMessage(crash_report_mode); | 89 MockRenderProcessHost::ShutdownForBadMessage(crash_report_mode); |
| 90 auth_run_loop_->Quit(); | 90 auth_run_loop_->Quit(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 base::RunLoop* auth_run_loop_; | 94 base::RunLoop* auth_run_loop_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 class MockAudioStreamRegistry : public AudioStreamRegistry { |
| 98 public: |
| 99 explicit MockAudioStreamRegistry(int process_id) {} |
| 100 MOCK_METHOD0(RegisterStream, void()); |
| 101 MOCK_METHOD0(DeregisterStream, void()); |
| 102 MOCK_METHOD1(StreamStateChanged, void(bool)); |
| 103 }; |
| 104 |
| 97 class FakeAudioManagerWithAssociations : public media::FakeAudioManager { | 105 class FakeAudioManagerWithAssociations : public media::FakeAudioManager { |
| 98 public: | 106 public: |
| 99 FakeAudioManagerWithAssociations( | 107 FakeAudioManagerWithAssociations( |
| 100 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 108 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 101 media::AudioLogFactory* factory) | 109 media::AudioLogFactory* factory) |
| 102 : FakeAudioManager(task_runner, task_runner, factory) {} | 110 : FakeAudioManager(task_runner, task_runner, factory) {} |
| 103 | 111 |
| 104 void CreateDeviceAssociation(const std::string& input_device_id, | 112 void CreateDeviceAssociation(const std::string& input_device_id, |
| 105 const std::string& output_device_id) { | 113 const std::string& output_device_id) { |
| 106 // We shouldn't accidentally add hashed ids, since the audio manager | 114 // We shouldn't accidentally add hashed ids, since the audio manager |
| (...skipping 13 matching lines...) Expand all Loading... |
| 120 private: | 128 private: |
| 121 std::map<std::string, std::string> associations_; | 129 std::map<std::string, std::string> associations_; |
| 122 }; | 130 }; |
| 123 | 131 |
| 124 } // namespace | 132 } // namespace |
| 125 | 133 |
| 126 class MockAudioRendererHost : public AudioRendererHost { | 134 class MockAudioRendererHost : public AudioRendererHost { |
| 127 public: | 135 public: |
| 128 MockAudioRendererHost(base::RunLoop* auth_run_loop, | 136 MockAudioRendererHost(base::RunLoop* auth_run_loop, |
| 129 int render_process_id, | 137 int render_process_id, |
| 138 AudioStreamRegistry* stream_registry, |
| 130 media::AudioManager* audio_manager, | 139 media::AudioManager* audio_manager, |
| 131 AudioMirroringManager* mirroring_manager, | 140 AudioMirroringManager* mirroring_manager, |
| 132 MediaStreamManager* media_stream_manager, | 141 MediaStreamManager* media_stream_manager, |
| 133 const std::string& salt) | 142 const std::string& salt) |
| 134 : AudioRendererHost(render_process_id, | 143 : AudioRendererHost(render_process_id, |
| 144 stream_registry, |
| 135 audio_manager, | 145 audio_manager, |
| 136 mirroring_manager, | 146 mirroring_manager, |
| 137 media_stream_manager, | 147 media_stream_manager, |
| 138 salt), | 148 salt), |
| 139 shared_memory_length_(0), | 149 shared_memory_length_(0), |
| 140 auth_run_loop_(auth_run_loop) { | 150 auth_run_loop_(auth_run_loop) { |
| 141 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); | 151 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); |
| 142 } | 152 } |
| 143 | 153 |
| 144 // A list of mock methods. | 154 // A list of mock methods. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | 238 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); |
| 229 }; | 239 }; |
| 230 | 240 |
| 231 class AudioRendererHostTest : public testing::Test { | 241 class AudioRendererHostTest : public testing::Test { |
| 232 public: | 242 public: |
| 233 AudioRendererHostTest() | 243 AudioRendererHostTest() |
| 234 : log_factory(base::MakeUnique<media::FakeAudioLogFactory>()), | 244 : log_factory(base::MakeUnique<media::FakeAudioLogFactory>()), |
| 235 audio_manager_(base::MakeUnique<FakeAudioManagerWithAssociations>( | 245 audio_manager_(base::MakeUnique<FakeAudioManagerWithAssociations>( |
| 236 base::ThreadTaskRunnerHandle::Get(), | 246 base::ThreadTaskRunnerHandle::Get(), |
| 237 log_factory.get())), | 247 log_factory.get())), |
| 238 render_process_host_(&browser_context_, &auth_run_loop_) { | 248 render_process_host_(&browser_context_, &auth_run_loop_), |
| 249 stream_registry_(render_process_host_.GetID()) { |
| 239 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 250 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 240 switches::kUseFakeDeviceForMediaStream); | 251 switches::kUseFakeDeviceForMediaStream); |
| 241 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | 252 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 242 host_ = new MockAudioRendererHost( | 253 host_ = new MockAudioRendererHost( |
| 243 &auth_run_loop_, render_process_host_.GetID(), audio_manager_.get(), | 254 &auth_run_loop_, render_process_host_.GetID(), &stream_registry_, |
| 244 &mirroring_manager_, media_stream_manager_.get(), kSalt); | 255 audio_manager_.get(), &mirroring_manager_, media_stream_manager_.get(), |
| 256 kSalt); |
| 245 | 257 |
| 246 // Simulate IPC channel connected. | 258 // Simulate IPC channel connected. |
| 247 host_->set_peer_process_for_testing(base::Process::Current()); | 259 host_->set_peer_process_for_testing(base::Process::Current()); |
| 248 } | 260 } |
| 249 | 261 |
| 250 ~AudioRendererHostTest() override { | 262 ~AudioRendererHostTest() override { |
| 251 // Simulate closing the IPC channel and give the audio thread time to close | 263 // Simulate closing the IPC channel and give the audio thread time to close |
| 252 // the underlying streams. | 264 // the underlying streams. |
| 253 host_->OnChannelClosing(); | 265 host_->OnChannelClosing(); |
| 254 SyncWithAudioThread(); | 266 SyncWithAudioThread(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 ? media::OUTPUT_DEVICE_STATUS_OK | 345 ? media::OUTPUT_DEVICE_STATUS_OK |
| 334 : device_id == kBadDeviceId | 346 : device_id == kBadDeviceId |
| 335 ? media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 347 ? media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
| 336 : media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND; | 348 : media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND; |
| 337 | 349 |
| 338 if (expect_onauthorized) | 350 if (expect_onauthorized) |
| 339 EXPECT_CALL(*host_.get(), | 351 EXPECT_CALL(*host_.get(), |
| 340 OnDeviceAuthorized(kStreamId, expected_device_status, _, _)); | 352 OnDeviceAuthorized(kStreamId, expected_device_status, _, _)); |
| 341 | 353 |
| 342 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { | 354 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { |
| 355 EXPECT_CALL(stream_registry_, RegisterStream()); |
| 343 EXPECT_CALL(*host_.get(), WasNotifiedOfCreation(kStreamId, _)); | 356 EXPECT_CALL(*host_.get(), WasNotifiedOfCreation(kStreamId, _)); |
| 344 EXPECT_CALL(mirroring_manager_, AddDiverter(render_process_host_.GetID(), | 357 EXPECT_CALL(mirroring_manager_, AddDiverter(render_process_host_.GetID(), |
| 345 kRenderFrameId, NotNull())) | 358 kRenderFrameId, NotNull())) |
| 346 .RetiresOnSaturation(); | 359 .RetiresOnSaturation(); |
| 347 } | 360 } |
| 348 | 361 |
| 349 // Send a create stream message to the audio output stream and wait until | 362 // Send a create stream message to the audio output stream and wait until |
| 350 // we receive the created message. | 363 // we receive the created message. |
| 351 media::AudioParameters params( | 364 media::AudioParameters params( |
| 352 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, | 365 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, |
| 353 media::AudioParameters::kAudioCDSampleRate, 16, | 366 media::AudioParameters::kAudioCDSampleRate, 16, |
| 354 media::AudioParameters::kAudioCDSampleRate / 10); | 367 media::AudioParameters::kAudioCDSampleRate / 10); |
| 355 int session_id = 0; | 368 int session_id = 0; |
| 356 | 369 |
| 357 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, | 370 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, |
| 358 device_id, security_origin); | 371 device_id, security_origin); |
| 359 if (wait_for_auth) | 372 if (wait_for_auth) |
| 360 auth_run_loop_.Run(); | 373 auth_run_loop_.Run(); |
| 361 | 374 |
| 362 if (!wait_for_auth || | 375 if (!wait_for_auth || |
| 363 expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) | 376 expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) |
| 364 host_->OnCreateStream(kStreamId, kRenderFrameId, params); | 377 host_->OnCreateStream(kStreamId, kRenderFrameId, params); |
| 365 | 378 |
| 366 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) | 379 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { |
| 367 // At some point in the future, a corresponding RemoveDiverter() call must | 380 // At some point in the future, a corresponding RemoveDiverter() call must |
| 368 // be made. | 381 // be made. |
| 382 EXPECT_CALL(stream_registry_, DeregisterStream()); |
| 369 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) | 383 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) |
| 370 .RetiresOnSaturation(); | 384 .RetiresOnSaturation(); |
| 385 } |
| 371 SyncWithAudioThread(); | 386 SyncWithAudioThread(); |
| 372 } | 387 } |
| 373 | 388 |
| 374 void RequestDeviceAuthorizationWithBadOrigin(const std::string& device_id) { | 389 void RequestDeviceAuthorizationWithBadOrigin(const std::string& device_id) { |
| 375 int session_id = 0; | 390 int session_id = 0; |
| 376 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, | 391 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, |
| 377 device_id, | 392 device_id, |
| 378 url::Origin(GURL(kBadSecurityOrigin))); | 393 url::Origin(GURL(kBadSecurityOrigin))); |
| 379 SyncWithAudioThread(); | 394 SyncWithAudioThread(); |
| 380 } | 395 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // MediaStreamManager uses a DestructionObserver, so it must outlive the | 522 // MediaStreamManager uses a DestructionObserver, so it must outlive the |
| 508 // TestBrowserThreadBundle. | 523 // TestBrowserThreadBundle. |
| 509 std::unique_ptr<MediaStreamManager> media_stream_manager_; | 524 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 510 TestBrowserThreadBundle thread_bundle_; | 525 TestBrowserThreadBundle thread_bundle_; |
| 511 TestBrowserContext browser_context_; | 526 TestBrowserContext browser_context_; |
| 512 std::unique_ptr<media::FakeAudioLogFactory> log_factory; | 527 std::unique_ptr<media::FakeAudioLogFactory> log_factory; |
| 513 std::unique_ptr<FakeAudioManagerWithAssociations> audio_manager_; | 528 std::unique_ptr<FakeAudioManagerWithAssociations> audio_manager_; |
| 514 MockAudioMirroringManager mirroring_manager_; | 529 MockAudioMirroringManager mirroring_manager_; |
| 515 base::RunLoop auth_run_loop_; | 530 base::RunLoop auth_run_loop_; |
| 516 MockRenderProcessHostWithSignaling render_process_host_; | 531 MockRenderProcessHostWithSignaling render_process_host_; |
| 532 MockAudioStreamRegistry stream_registry_; |
| 517 scoped_refptr<MockAudioRendererHost> host_; | 533 scoped_refptr<MockAudioRendererHost> host_; |
| 518 | 534 |
| 519 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); | 535 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); |
| 520 }; | 536 }; |
| 521 | 537 |
| 522 TEST_F(AudioRendererHostTest, CreateAndClose) { | 538 TEST_F(AudioRendererHostTest, CreateAndClose) { |
| 523 Create(); | 539 Create(); |
| 524 Close(); | 540 Close(); |
| 525 } | 541 } |
| 526 | 542 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 } | 632 } |
| 617 | 633 |
| 618 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { | 634 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { |
| 619 CreateWithInvalidRenderFrameId(); | 635 CreateWithInvalidRenderFrameId(); |
| 620 Close(); | 636 Close(); |
| 621 } | 637 } |
| 622 | 638 |
| 623 // TODO(hclam): Add tests for data conversation in low latency mode. | 639 // TODO(hclam): Add tests for data conversation in low latency mode. |
| 624 | 640 |
| 625 } // namespace content | 641 } // namespace content |
| OLD | NEW |