| 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 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int render_frame_id, | 67 int render_frame_id, |
| 68 Diverter* diverter)); | 68 Diverter* diverter)); |
| 69 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); | 69 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager); | 72 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 class MockAudioRendererHost : public AudioRendererHost { | 75 class MockAudioRendererHost : public AudioRendererHost { |
| 76 public: | 76 public: |
| 77 MockAudioRendererHost(media::AudioManager* audio_manager, | 77 MockAudioRendererHost(base::RunLoop* auth_run_loop, |
| 78 media::AudioManager* audio_manager, |
| 78 AudioMirroringManager* mirroring_manager, | 79 AudioMirroringManager* mirroring_manager, |
| 79 MediaInternals* media_internals, | 80 MediaInternals* media_internals, |
| 80 MediaStreamManager* media_stream_manager, | 81 MediaStreamManager* media_stream_manager, |
| 81 const std::string& salt) | 82 const std::string& salt) |
| 82 : AudioRendererHost(kRenderProcessId, | 83 : AudioRendererHost(kRenderProcessId, |
| 83 audio_manager, | 84 audio_manager, |
| 84 mirroring_manager, | 85 mirroring_manager, |
| 85 media_internals, | 86 media_internals, |
| 86 media_stream_manager, | 87 media_stream_manager, |
| 87 salt), | 88 salt), |
| 88 shared_memory_length_(0) { | 89 shared_memory_length_(0), |
| 90 auth_run_loop_(auth_run_loop) { |
| 89 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); | 91 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // A list of mock methods. | 94 // A list of mock methods. |
| 93 MOCK_METHOD4(OnDeviceAuthorized, | 95 MOCK_METHOD4(OnDeviceAuthorized, |
| 94 void(int stream_id, | 96 void(int stream_id, |
| 95 media::OutputDeviceStatus device_status, | 97 media::OutputDeviceStatus device_status, |
| 96 const media::AudioParameters& output_params, | 98 const media::AudioParameters& output_params, |
| 97 const std::string& matched_device_id)); | 99 const std::string& matched_device_id)); |
| 98 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); | 100 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 delete message; | 134 delete message; |
| 133 return true; | 135 return true; |
| 134 } | 136 } |
| 135 | 137 |
| 136 void OnNotifyDeviceAuthorized(int stream_id, | 138 void OnNotifyDeviceAuthorized(int stream_id, |
| 137 media::OutputDeviceStatus device_status, | 139 media::OutputDeviceStatus device_status, |
| 138 const media::AudioParameters& output_params, | 140 const media::AudioParameters& output_params, |
| 139 const std::string& matched_device_id) { | 141 const std::string& matched_device_id) { |
| 140 OnDeviceAuthorized(stream_id, device_status, output_params, | 142 OnDeviceAuthorized(stream_id, device_status, output_params, |
| 141 matched_device_id); | 143 matched_device_id); |
| 144 auth_run_loop_->Quit(); |
| 142 } | 145 } |
| 143 | 146 |
| 144 void OnNotifyStreamCreated( | 147 void OnNotifyStreamCreated( |
| 145 int stream_id, | 148 int stream_id, |
| 146 base::SharedMemoryHandle handle, | 149 base::SharedMemoryHandle handle, |
| 147 base::SyncSocket::TransitDescriptor socket_descriptor, | 150 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 148 uint32_t length) { | 151 uint32_t length) { |
| 149 // Maps the shared memory. | 152 // Maps the shared memory. |
| 150 shared_memory_.reset(new base::SharedMemory(handle, false)); | 153 shared_memory_.reset(new base::SharedMemory(handle, false)); |
| 151 CHECK(shared_memory_->Map(length)); | 154 CHECK(shared_memory_->Map(length)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 175 break; | 178 break; |
| 176 default: | 179 default: |
| 177 FAIL() << "Unknown stream state"; | 180 FAIL() << "Unknown stream state"; |
| 178 break; | 181 break; |
| 179 } | 182 } |
| 180 } | 183 } |
| 181 | 184 |
| 182 std::unique_ptr<base::SharedMemory> shared_memory_; | 185 std::unique_ptr<base::SharedMemory> shared_memory_; |
| 183 std::unique_ptr<base::SyncSocket> sync_socket_; | 186 std::unique_ptr<base::SyncSocket> sync_socket_; |
| 184 uint32_t shared_memory_length_; | 187 uint32_t shared_memory_length_; |
| 188 base::RunLoop* auth_run_loop_; // Used to wait for authorization. |
| 185 | 189 |
| 186 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); | 190 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); |
| 187 }; | 191 }; |
| 188 | 192 |
| 189 namespace { | |
| 190 | |
| 191 void WaitForEnumeration(base::RunLoop* loop, | |
| 192 const AudioOutputDeviceEnumeration& e) { | |
| 193 loop->Quit(); | |
| 194 } | |
| 195 | |
| 196 } // namespace | |
| 197 | |
| 198 class AudioRendererHostTest : public testing::Test { | 193 class AudioRendererHostTest : public testing::Test { |
| 199 public: | 194 public: |
| 200 AudioRendererHostTest() { | 195 AudioRendererHostTest() { |
| 201 audio_manager_ = media::AudioManager::CreateForTesting( | 196 audio_manager_ = media::AudioManager::CreateForTesting( |
| 202 base::ThreadTaskRunnerHandle::Get()); | 197 base::ThreadTaskRunnerHandle::Get()); |
| 203 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 198 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 204 switches::kUseFakeDeviceForMediaStream); | 199 switches::kUseFakeDeviceForMediaStream); |
| 205 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | 200 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 206 | 201 host_ = new MockAudioRendererHost( |
| 207 // Enable caching to make enumerations run in a single thread | 202 &auth_run_loop_, audio_manager_.get(), &mirroring_manager_, |
| 208 media_stream_manager_->audio_output_device_enumerator()->SetCachePolicy( | 203 MediaInternals::GetInstance(), media_stream_manager_.get(), |
| 209 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION); | 204 std::string()); |
| 210 base::RunLoop().RunUntilIdle(); | |
| 211 base::RunLoop run_loop; | |
| 212 media_stream_manager_->audio_output_device_enumerator()->Enumerate( | |
| 213 base::Bind(&WaitForEnumeration, &run_loop)); | |
| 214 run_loop.Run(); | |
| 215 | |
| 216 host_ = | |
| 217 new MockAudioRendererHost(audio_manager_.get(), &mirroring_manager_, | |
| 218 MediaInternals::GetInstance(), | |
| 219 media_stream_manager_.get(), std::string()); | |
| 220 | 205 |
| 221 // Simulate IPC channel connected. | 206 // Simulate IPC channel connected. |
| 222 host_->set_peer_process_for_testing(base::Process::Current()); | 207 host_->set_peer_process_for_testing(base::Process::Current()); |
| 223 } | 208 } |
| 224 | 209 |
| 225 ~AudioRendererHostTest() override { | 210 ~AudioRendererHostTest() override { |
| 226 // Simulate closing the IPC channel and give the audio thread time to close | 211 // Simulate closing the IPC channel and give the audio thread time to close |
| 227 // the underlying streams. | 212 // the underlying streams. |
| 228 host_->OnChannelClosing(); | 213 host_->OnChannelClosing(); |
| 229 SyncWithAudioThread(); | 214 SyncWithAudioThread(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 media::AudioParameters::kAudioCDSampleRate, 16, | 250 media::AudioParameters::kAudioCDSampleRate, 16, |
| 266 media::AudioParameters::kAudioCDSampleRate / 10); | 251 media::AudioParameters::kAudioCDSampleRate / 10); |
| 267 int session_id = 0; | 252 int session_id = 0; |
| 268 if (unified_stream) { | 253 if (unified_stream) { |
| 269 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to | 254 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to |
| 270 // pass the permission check. | 255 // pass the permission check. |
| 271 session_id = AudioInputDeviceManager::kFakeOpenSessionId; | 256 session_id = AudioInputDeviceManager::kFakeOpenSessionId; |
| 272 } | 257 } |
| 273 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, | 258 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id, |
| 274 device_id, security_origin); | 259 device_id, security_origin); |
| 260 auth_run_loop_.Run(); |
| 275 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { | 261 if (expected_device_status == media::OUTPUT_DEVICE_STATUS_OK) { |
| 276 host_->OnCreateStream(kStreamId, kRenderFrameId, params); | 262 host_->OnCreateStream(kStreamId, kRenderFrameId, params); |
| 277 | 263 |
| 278 // At some point in the future, a corresponding RemoveDiverter() call must | 264 // At some point in the future, a corresponding RemoveDiverter() call must |
| 279 // be made. | 265 // be made. |
| 280 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) | 266 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) |
| 281 .RetiresOnSaturation(); | 267 .RetiresOnSaturation(); |
| 282 } | 268 } |
| 283 SyncWithAudioThread(); | 269 SyncWithAudioThread(); |
| 284 } | 270 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 } | 344 } |
| 359 | 345 |
| 360 private: | 346 private: |
| 361 // MediaStreamManager uses a DestructionObserver, so it must outlive the | 347 // MediaStreamManager uses a DestructionObserver, so it must outlive the |
| 362 // TestBrowserThreadBundle. | 348 // TestBrowserThreadBundle. |
| 363 std::unique_ptr<MediaStreamManager> media_stream_manager_; | 349 std::unique_ptr<MediaStreamManager> media_stream_manager_; |
| 364 TestBrowserThreadBundle thread_bundle_; | 350 TestBrowserThreadBundle thread_bundle_; |
| 365 media::ScopedAudioManagerPtr audio_manager_; | 351 media::ScopedAudioManagerPtr audio_manager_; |
| 366 MockAudioMirroringManager mirroring_manager_; | 352 MockAudioMirroringManager mirroring_manager_; |
| 367 scoped_refptr<MockAudioRendererHost> host_; | 353 scoped_refptr<MockAudioRendererHost> host_; |
| 354 base::RunLoop auth_run_loop_; |
| 368 | 355 |
| 369 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); | 356 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); |
| 370 }; | 357 }; |
| 371 | 358 |
| 372 TEST_F(AudioRendererHostTest, CreateAndClose) { | 359 TEST_F(AudioRendererHostTest, CreateAndClose) { |
| 373 Create(); | 360 Create(); |
| 374 Close(); | 361 Close(); |
| 375 } | 362 } |
| 376 | 363 |
| 377 // Simulate the case where a stream is not properly closed. | 364 // Simulate the case where a stream is not properly closed. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 } | 432 } |
| 446 | 433 |
| 447 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { | 434 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { |
| 448 CreateWithInvalidRenderFrameId(); | 435 CreateWithInvalidRenderFrameId(); |
| 449 Close(); | 436 Close(); |
| 450 } | 437 } |
| 451 | 438 |
| 452 // TODO(hclam): Add tests for data conversation in low latency mode. | 439 // TODO(hclam): Add tests for data conversation in low latency mode. |
| 453 | 440 |
| 454 } // namespace content | 441 } // namespace content |
| OLD | NEW |