| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_input_renderer_host.h" |
| 6 | 6 |
| 7 #include <utility> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "content/browser/bad_message.h" | 15 #include "content/browser/bad_message.h" |
| 15 #include "content/browser/media/capture/audio_mirroring_manager.h" | 16 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 16 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 17 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 72 } |
| 72 | 73 |
| 73 class MockRenderer { | 74 class MockRenderer { |
| 74 public: | 75 public: |
| 75 MOCK_METHOD5(NotifyStreamCreated, | 76 MOCK_METHOD5(NotifyStreamCreated, |
| 76 void(int /*stream_id*/, | 77 void(int /*stream_id*/, |
| 77 base::SharedMemoryHandle /*handle*/, | 78 base::SharedMemoryHandle /*handle*/, |
| 78 base::SyncSocket::TransitDescriptor /*socket_desriptor*/, | 79 base::SyncSocket::TransitDescriptor /*socket_desriptor*/, |
| 79 uint32_t /*length*/, | 80 uint32_t /*length*/, |
| 80 uint32_t /*total_segments*/)); | 81 uint32_t /*total_segments*/)); |
| 81 MOCK_METHOD2(NotifyStreamVolume, void(int /*stream_id*/, double /*volume*/)); | 82 MOCK_METHOD1(NotifyStreamError, void(int /*stream_id*/)); |
| 82 MOCK_METHOD2(NotifyStreamStateChanged, | |
| 83 void(int /*stream_id*/, | |
| 84 media::AudioInputIPCDelegateState /*state*/)); | |
| 85 MOCK_METHOD0(WasShutDown, void()); | 83 MOCK_METHOD0(WasShutDown, void()); |
| 86 }; | 84 }; |
| 87 | 85 |
| 88 // This class overrides Send to intercept the messages that the | 86 // This class overrides Send to intercept the messages that the |
| 89 // AudioInputRendererHost sends to the renderer. They are sent to | 87 // AudioInputRendererHost sends to the renderer. They are sent to |
| 90 // the provided MockRenderer instead. | 88 // the provided MockRenderer instead. |
| 91 class AudioInputRendererHostWithInterception : public AudioInputRendererHost { | 89 class AudioInputRendererHostWithInterception : public AudioInputRendererHost { |
| 92 public: | 90 public: |
| 93 AudioInputRendererHostWithInterception( | 91 AudioInputRendererHostWithInterception( |
| 94 int render_process_id, | 92 int render_process_id, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 112 ~AudioInputRendererHostWithInterception() override = default; | 110 ~AudioInputRendererHostWithInterception() override = default; |
| 113 | 111 |
| 114 private: | 112 private: |
| 115 bool Send(IPC::Message* message) override { | 113 bool Send(IPC::Message* message) override { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 117 bool handled = true; | 115 bool handled = true; |
| 118 | 116 |
| 119 IPC_BEGIN_MESSAGE_MAP(AudioInputRendererHostWithInterception, *message) | 117 IPC_BEGIN_MESSAGE_MAP(AudioInputRendererHostWithInterception, *message) |
| 120 IPC_MESSAGE_HANDLER(AudioInputMsg_NotifyStreamCreated, | 118 IPC_MESSAGE_HANDLER(AudioInputMsg_NotifyStreamCreated, |
| 121 NotifyRendererStreamCreated) | 119 NotifyRendererStreamCreated) |
| 122 IPC_MESSAGE_HANDLER(AudioInputMsg_NotifyStreamVolume, | 120 IPC_MESSAGE_HANDLER(AudioInputMsg_NotifyStreamError, |
| 123 NotifyRendererStreamVolume) | 121 NotifyRendererStreamError) |
| 124 IPC_MESSAGE_HANDLER(AudioInputMsg_NotifyStreamStateChanged, | |
| 125 NotifyRendererStreamStateChanged) | |
| 126 IPC_MESSAGE_UNHANDLED(handled = false) | 122 IPC_MESSAGE_UNHANDLED(handled = false) |
| 127 IPC_END_MESSAGE_MAP() | 123 IPC_END_MESSAGE_MAP() |
| 128 | 124 |
| 129 EXPECT_TRUE(handled); | 125 EXPECT_TRUE(handled); |
| 130 delete message; | 126 delete message; |
| 131 return true; | 127 return true; |
| 132 } | 128 } |
| 133 | 129 |
| 134 void ShutdownForBadMessage() override { renderer_->WasShutDown(); } | 130 void ShutdownForBadMessage() override { renderer_->WasShutDown(); } |
| 135 | 131 |
| 136 void NotifyRendererStreamCreated( | 132 void NotifyRendererStreamCreated( |
| 137 int stream_id, | 133 int stream_id, |
| 138 base::SharedMemoryHandle handle, | 134 base::SharedMemoryHandle handle, |
| 139 base::SyncSocket::TransitDescriptor socket_descriptor, | 135 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 140 uint32_t length, | 136 uint32_t length, |
| 141 uint32_t total_segments) { | 137 uint32_t total_segments) { |
| 142 // It's difficult to check that the sync socket and shared memory is | 138 // It's difficult to check that the sync socket and shared memory is |
| 143 // valid in the gmock macros, so we check them here. | 139 // valid in the gmock macros, so we check them here. |
| 144 EXPECT_NE(base::SyncSocket::UnwrapHandle(socket_descriptor), | 140 EXPECT_NE(base::SyncSocket::UnwrapHandle(socket_descriptor), |
| 145 base::SyncSocket::kInvalidHandle); | 141 base::SyncSocket::kInvalidHandle); |
| 146 base::SharedMemory memory(handle, /*read_only*/ true); | 142 base::SharedMemory memory(handle, /*read_only*/ true); |
| 147 EXPECT_TRUE(memory.Map(length)); | 143 EXPECT_TRUE(memory.Map(length)); |
| 148 renderer_->NotifyStreamCreated(stream_id, handle, socket_descriptor, length, | 144 renderer_->NotifyStreamCreated(stream_id, handle, socket_descriptor, length, |
| 149 total_segments); | 145 total_segments); |
| 150 EXPECT_TRUE(memory.Unmap()); | 146 EXPECT_TRUE(memory.Unmap()); |
| 151 memory.Close(); | 147 memory.Close(); |
| 152 } | 148 } |
| 153 | 149 |
| 154 void NotifyRendererStreamVolume(int stream_id, double volume) { | 150 void NotifyRendererStreamError(int stream_id) { |
| 155 renderer_->NotifyStreamVolume(stream_id, volume); | 151 renderer_->NotifyStreamError(stream_id); |
| 156 } | |
| 157 | |
| 158 void NotifyRendererStreamStateChanged( | |
| 159 int stream_id, | |
| 160 media::AudioInputIPCDelegateState state) { | |
| 161 renderer_->NotifyStreamStateChanged(stream_id, state); | |
| 162 } | 152 } |
| 163 | 153 |
| 164 MockRenderer* renderer_; | 154 MockRenderer* renderer_; |
| 165 }; | 155 }; |
| 166 | 156 |
| 167 class MockAudioInputController : public AudioInputController { | 157 class MockAudioInputController : public AudioInputController { |
| 168 public: | 158 public: |
| 169 MockAudioInputController( | 159 MockAudioInputController( |
| 170 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 160 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 171 AudioInputController::SyncWriter* writer, | 161 AudioInputController::SyncWriter* writer, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 328 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 339 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 329 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 340 | 330 |
| 341 base::RunLoop().RunUntilIdle(); | 331 base::RunLoop().RunUntilIdle(); |
| 342 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); | 332 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); |
| 343 } | 333 } |
| 344 | 334 |
| 345 // If authorization hasn't been granted, only reply with and error and do | 335 // If authorization hasn't been granted, only reply with and error and do |
| 346 // nothing else. | 336 // nothing else. |
| 347 TEST_F(AudioInputRendererHostTest, CreateWithoutAuthorization_Error) { | 337 TEST_F(AudioInputRendererHostTest, CreateWithoutAuthorization_Error) { |
| 348 EXPECT_CALL(renderer_, | 338 EXPECT_CALL(renderer_, NotifyStreamError(kStreamId)); |
| 349 NotifyStreamStateChanged( | |
| 350 kStreamId, media::AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR)); | |
| 351 | 339 |
| 352 int session_id = 0; | 340 int session_id = 0; |
| 353 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 341 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 354 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 342 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 355 base::RunLoop().RunUntilIdle(); | 343 base::RunLoop().RunUntilIdle(); |
| 356 } | 344 } |
| 357 | 345 |
| 358 // Like CreateWithDefaultDevice but with a nondefault device. | 346 // Like CreateWithDefaultDevice but with a nondefault device. |
| 359 TEST_F(AudioInputRendererHostTest, CreateWithNonDefaultDevice) { | 347 TEST_F(AudioInputRendererHostTest, CreateWithNonDefaultDevice) { |
| 360 int session_id = Open("Nondefault device", GetRawNondefaultId()); | 348 int session_id = Open("Nondefault device", GetRawNondefaultId()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 base::RunLoop().RunUntilIdle(); | 448 base::RunLoop().RunUntilIdle(); |
| 461 } | 449 } |
| 462 | 450 |
| 463 // Checks that a stream_id cannot be reused. | 451 // Checks that a stream_id cannot be reused. |
| 464 TEST_F(AudioInputRendererHostTest, CreateTwice_Error) { | 452 TEST_F(AudioInputRendererHostTest, CreateTwice_Error) { |
| 465 int session_id = | 453 int session_id = |
| 466 Open("Default device", media::AudioDeviceDescription::kDefaultDeviceId); | 454 Open("Default device", media::AudioDeviceDescription::kDefaultDeviceId); |
| 467 | 455 |
| 468 EXPECT_CALL(renderer_, | 456 EXPECT_CALL(renderer_, |
| 469 NotifyStreamCreated(kStreamId, _, _, _, kSharedMemoryCount)); | 457 NotifyStreamCreated(kStreamId, _, _, _, kSharedMemoryCount)); |
| 470 EXPECT_CALL(renderer_, | 458 EXPECT_CALL(renderer_, NotifyStreamError(kStreamId)); |
| 471 NotifyStreamStateChanged( | |
| 472 kStreamId, media::AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR)); | |
| 473 EXPECT_CALL(controller_factory_, ControllerCreated()); | 459 EXPECT_CALL(controller_factory_, ControllerCreated()); |
| 474 | 460 |
| 475 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 461 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 476 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 462 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 477 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 463 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 478 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 464 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 479 base::RunLoop().RunUntilIdle(); | 465 base::RunLoop().RunUntilIdle(); |
| 480 | 466 |
| 481 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); | 467 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); |
| 482 } | 468 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 | 513 |
| 528 EXPECT_CALL(renderer_, | 514 EXPECT_CALL(renderer_, |
| 529 NotifyStreamCreated(kStreamId, _, _, _, kSharedMemoryCount)); | 515 NotifyStreamCreated(kStreamId, _, _, _, kSharedMemoryCount)); |
| 530 EXPECT_CALL(controller_factory_, ControllerCreated()); | 516 EXPECT_CALL(controller_factory_, ControllerCreated()); |
| 531 | 517 |
| 532 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 518 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 533 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 519 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 534 | 520 |
| 535 base::RunLoop().RunUntilIdle(); | 521 base::RunLoop().RunUntilIdle(); |
| 536 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); | 522 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); |
| 537 EXPECT_CALL(renderer_, | 523 EXPECT_CALL(renderer_, NotifyStreamError(kStreamId)); |
| 538 NotifyStreamStateChanged( | |
| 539 kStreamId, media::AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR)); | |
| 540 | 524 |
| 541 controller_factory_.controller(0)->handler()->OnError( | 525 controller_factory_.controller(0)->handler()->OnError( |
| 542 controller_factory_.controller(0), AudioInputController::UNKNOWN_ERROR); | 526 controller_factory_.controller(0), AudioInputController::UNKNOWN_ERROR); |
| 543 | 527 |
| 544 // Check Close expectation before the destructor. | 528 // Check Close expectation before the destructor. |
| 545 base::RunLoop().RunUntilIdle(); | 529 base::RunLoop().RunUntilIdle(); |
| 546 Mock::VerifyAndClear(controller_factory_.controller(0)); | 530 Mock::VerifyAndClear(controller_factory_.controller(0)); |
| 547 } | 531 } |
| 548 | 532 |
| 549 // Checks that tab capture streams can be created. | 533 // Checks that tab capture streams can be created. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 564 EXPECT_CALL(controller_factory_, ControllerCreated()); | 548 EXPECT_CALL(controller_factory_, ControllerCreated()); |
| 565 | 549 |
| 566 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( | 550 airh_->OnMessageReceived(AudioInputHostMsg_CreateStream( |
| 567 kStreamId, kRenderFrameId, session_id, DefaultConfig())); | 551 kStreamId, kRenderFrameId, session_id, DefaultConfig())); |
| 568 base::RunLoop().RunUntilIdle(); | 552 base::RunLoop().RunUntilIdle(); |
| 569 | 553 |
| 570 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); | 554 EXPECT_CALL(*controller_factory_.controller(0), Close(_)); |
| 571 } | 555 } |
| 572 | 556 |
| 573 } // namespace content | 557 } // namespace content |
| OLD | NEW |