| 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 14 matching lines...) Expand all Loading... |
| 25 #include "media/base/bind_to_current_loop.h" | 25 #include "media/base/bind_to_current_loop.h" |
| 26 #include "media/base/media_switches.h" | 26 #include "media/base/media_switches.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 using ::testing::_; | 30 using ::testing::_; |
| 31 using ::testing::Assign; | 31 using ::testing::Assign; |
| 32 using ::testing::DoAll; | 32 using ::testing::DoAll; |
| 33 using ::testing::NotNull; | 33 using ::testing::NotNull; |
| 34 | 34 |
| 35 namespace content { |
| 36 |
| 35 namespace { | 37 namespace { |
| 36 const int kRenderProcessId = 1; | 38 const int kRenderProcessId = 1; |
| 37 const int kRenderFrameId = 5; | 39 const int kRenderFrameId = 5; |
| 38 const int kStreamId = 50; | 40 const int kStreamId = 50; |
| 39 const char kSecurityOrigin[] = "http://localhost"; | 41 const char kSecurityOrigin[] = "http://localhost"; |
| 40 const char kDefaultDeviceId[] = ""; | 42 const char kDefaultDeviceId[] = ""; |
| 41 const char kBadDeviceId[] = | 43 const char kBadDeviceId[] = |
| 42 "badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbad1"; | 44 "badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbad1"; |
| 43 const char kInvalidDeviceId[] = "invalid-device-id"; | 45 const char kInvalidDeviceId[] = "invalid-device-id"; |
| 46 |
| 47 #if DCHECK_IS_ON() |
| 48 void ValidateRenderFrameId(int render_process_id, |
| 49 int render_frame_id, |
| 50 const base::Callback<void(bool)>& callback) { |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 const bool frame_exists = (render_process_id == kRenderProcessId && |
| 53 render_frame_id == kRenderFrameId); |
| 54 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 55 base::Bind(callback, frame_exists)); |
| 56 } |
| 57 #endif // DCHECK_IS_ON() |
| 58 |
| 44 } // namespace | 59 } // namespace |
| 45 | 60 |
| 46 namespace content { | |
| 47 | |
| 48 class MockAudioMirroringManager : public AudioMirroringManager { | 61 class MockAudioMirroringManager : public AudioMirroringManager { |
| 49 public: | 62 public: |
| 50 MockAudioMirroringManager() {} | 63 MockAudioMirroringManager() {} |
| 51 virtual ~MockAudioMirroringManager() {} | 64 virtual ~MockAudioMirroringManager() {} |
| 52 | 65 |
| 53 MOCK_METHOD3(AddDiverter, | 66 MOCK_METHOD3(AddDiverter, |
| 54 void(int render_process_id, | 67 void(int render_process_id, |
| 55 int render_frame_id, | 68 int render_frame_id, |
| 56 Diverter* diverter)); | 69 Diverter* diverter)); |
| 57 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); | 70 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter)); |
| 58 | 71 |
| 59 private: | 72 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager); | 73 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager); |
| 61 }; | 74 }; |
| 62 | 75 |
| 63 class MockAudioRendererHost : public AudioRendererHost { | 76 class MockAudioRendererHost : public AudioRendererHost { |
| 64 public: | 77 public: |
| 65 MockAudioRendererHost(media::AudioManager* audio_manager, | 78 MockAudioRendererHost(media::AudioManager* audio_manager, |
| 66 AudioMirroringManager* mirroring_manager, | 79 AudioMirroringManager* mirroring_manager, |
| 67 MediaInternals* media_internals, | 80 MediaInternals* media_internals, |
| 68 MediaStreamManager* media_stream_manager, | 81 MediaStreamManager* media_stream_manager, |
| 69 const std::string& salt) | 82 const std::string& salt) |
| 70 : AudioRendererHost(kRenderProcessId, | 83 : AudioRendererHost(kRenderProcessId, |
| 71 audio_manager, | 84 audio_manager, |
| 72 mirroring_manager, | 85 mirroring_manager, |
| 73 media_internals, | 86 media_internals, |
| 74 media_stream_manager, | 87 media_stream_manager, |
| 75 salt), | 88 salt), |
| 76 shared_memory_length_(0) {} | 89 shared_memory_length_(0) { |
| 90 #if DCHECK_IS_ON() |
| 91 set_render_frame_id_validate_function_for_testing(&ValidateRenderFrameId); |
| 92 #endif // DCHECK_IS_ON() |
| 93 } |
| 77 | 94 |
| 78 // A list of mock methods. | 95 // A list of mock methods. |
| 79 MOCK_METHOD4(OnDeviceAuthorized, | 96 MOCK_METHOD4(OnDeviceAuthorized, |
| 80 void(int stream_id, | 97 void(int stream_id, |
| 81 media::OutputDeviceStatus device_status, | 98 media::OutputDeviceStatus device_status, |
| 82 const media::AudioParameters& output_params, | 99 const media::AudioParameters& output_params, |
| 83 const std::string& matched_device_id)); | 100 const std::string& matched_device_id)); |
| 84 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); | 101 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); |
| 85 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); | 102 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); |
| 86 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); | 103 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 host_->OnCreateStream(kStreamId, kRenderFrameId, params); | 276 host_->OnCreateStream(kStreamId, kRenderFrameId, params); |
| 260 | 277 |
| 261 // At some point in the future, a corresponding RemoveDiverter() call must | 278 // At some point in the future, a corresponding RemoveDiverter() call must |
| 262 // be made. | 279 // be made. |
| 263 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) | 280 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) |
| 264 .RetiresOnSaturation(); | 281 .RetiresOnSaturation(); |
| 265 } | 282 } |
| 266 SyncWithAudioThread(); | 283 SyncWithAudioThread(); |
| 267 } | 284 } |
| 268 | 285 |
| 286 void CreateWithInvalidRenderFrameId() { |
| 287 // Because the render frame is invalid, the host should only reply with a |
| 288 // stream error message. |
| 289 EXPECT_CALL(*host_, OnStreamError(kStreamId)); |
| 290 |
| 291 // When DCHECKs are on, provide a seemingly-valid render frame ID; and it |
| 292 // should be rejected when AudioRendererHost calls |
| 293 // ValidateRenderFrameId(). When DCHECKs are off, AudioRendererHost won't |
| 294 // call the validation function, but can still reject a render frame ID when |
| 295 // it is obviously bogus. |
| 296 #if DCHECK_IS_ON() |
| 297 const int kInvalidRenderFrameId = kRenderFrameId + 1; |
| 298 #else |
| 299 const int kInvalidRenderFrameId = MSG_ROUTING_NONE; |
| 300 #endif // DCHECK_IS_ON() |
| 301 |
| 302 const media::AudioParameters params( |
| 303 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, |
| 304 media::AudioParameters::kAudioCDSampleRate, 16, |
| 305 media::AudioParameters::kAudioCDSampleRate / 10); |
| 306 host_->OnCreateStream(kStreamId, kInvalidRenderFrameId, params); |
| 307 } |
| 308 |
| 269 void Close() { | 309 void Close() { |
| 270 // Send a message to AudioRendererHost to tell it we want to close the | 310 // Send a message to AudioRendererHost to tell it we want to close the |
| 271 // stream. | 311 // stream. |
| 272 host_->OnCloseStream(kStreamId); | 312 host_->OnCloseStream(kStreamId); |
| 273 SyncWithAudioThread(); | 313 SyncWithAudioThread(); |
| 274 } | 314 } |
| 275 | 315 |
| 276 void Play() { | 316 void Play() { |
| 277 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId)); | 317 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId)); |
| 278 host_->OnPlayStream(kStreamId); | 318 host_->OnPlayStream(kStreamId); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 TEST_F(AudioRendererHostTest, CreateUnauthorizedDevice) { | 438 TEST_F(AudioRendererHostTest, CreateUnauthorizedDevice) { |
| 399 Create(false, kBadDeviceId, url::Origin(GURL(kSecurityOrigin))); | 439 Create(false, kBadDeviceId, url::Origin(GURL(kSecurityOrigin))); |
| 400 Close(); | 440 Close(); |
| 401 } | 441 } |
| 402 | 442 |
| 403 TEST_F(AudioRendererHostTest, CreateInvalidDevice) { | 443 TEST_F(AudioRendererHostTest, CreateInvalidDevice) { |
| 404 Create(false, kInvalidDeviceId, url::Origin(GURL(kSecurityOrigin))); | 444 Create(false, kInvalidDeviceId, url::Origin(GURL(kSecurityOrigin))); |
| 405 Close(); | 445 Close(); |
| 406 } | 446 } |
| 407 | 447 |
| 448 TEST_F(AudioRendererHostTest, CreateFailsForInvalidRenderFrame) { |
| 449 CreateWithInvalidRenderFrameId(); |
| 450 Close(); |
| 451 } |
| 452 |
| 408 // TODO(hclam): Add tests for data conversation in low latency mode. | 453 // TODO(hclam): Add tests for data conversation in low latency mode. |
| 409 | 454 |
| 410 } // namespace content | 455 } // namespace content |
| OLD | NEW |