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