| 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 | 7 |
| 8 #include "media/base/audio_renderer_mixer.h" | 8 #include "media/base/audio_renderer_mixer.h" |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 | 11 |
| 12 #include <cmath> | 12 #include <cmath> |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
| 19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 21 #include "media/base/audio_renderer_mixer_input.h" | 21 #include "media/base/audio_renderer_mixer_input.h" |
| 22 #include "media/base/audio_renderer_mixer_pool.h" |
| 22 #include "media/base/fake_audio_render_callback.h" | 23 #include "media/base/fake_audio_render_callback.h" |
| 23 #include "media/base/mock_audio_renderer_sink.h" | 24 #include "media/base/mock_audio_renderer_sink.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 27 |
| 27 namespace media { | 28 namespace media { |
| 28 | 29 |
| 29 // Parameters which control the many input case tests. | 30 // Parameters which control the many input case tests. |
| 30 const int kMixerInputs = 8; | 31 const int kMixerInputs = 8; |
| 31 const int kOddMixerInputs = 7; | 32 const int kOddMixerInputs = 7; |
| 32 const int kMixerCycles = 3; | 33 const int kMixerCycles = 3; |
| 33 | 34 |
| 34 // Parameters used for testing. | 35 // Parameters used for testing. |
| 35 const int kBitsPerChannel = 32; | 36 const int kBitsPerChannel = 32; |
| 36 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 37 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 37 const int kHighLatencyBufferSize = 8192; | 38 const int kHighLatencyBufferSize = 8192; |
| 38 const int kLowLatencyBufferSize = 256; | 39 const int kLowLatencyBufferSize = 256; |
| 39 | 40 |
| 40 // Number of full sine wave cycles for each Render() call. | 41 // Number of full sine wave cycles for each Render() call. |
| 41 const int kSineCycles = 4; | 42 const int kSineCycles = 4; |
| 42 | 43 |
| 43 // Input sample frequencies for testing. | 44 // Input sample frequencies for testing. |
| 44 const int kTestInputLower = 44100; | 45 const int kTestInputLower = 44100; |
| 45 const int kTestInputHigher = 48000; | 46 const int kTestInputHigher = 48000; |
| 46 const int kTestInput3Rates[] = {22050, 44100, 48000}; | 47 const int kTestInput3Rates[] = {22050, 44100, 48000}; |
| 47 | 48 |
| 48 // Tuple of <input sampling rates, number of input sample rates, | 49 // Tuple of <input sampling rates, number of input sample rates, |
| 49 // output sampling rate, epsilon>. | 50 // output sampling rate, epsilon>. |
| 50 typedef std::tr1::tuple<const int* const, size_t, int, double> | 51 using AudioRendererMixerTestData = |
| 51 AudioRendererMixerTestData; | 52 std::tr1::tuple<const int* const, size_t, int, double>; |
| 52 | 53 |
| 53 class AudioRendererMixerTest | 54 class AudioRendererMixerTest |
| 54 : public testing::TestWithParam<AudioRendererMixerTestData> { | 55 : public testing::TestWithParam<AudioRendererMixerTestData>, |
| 56 AudioRendererMixerPool { |
| 55 public: | 57 public: |
| 56 AudioRendererMixerTest() | 58 AudioRendererMixerTest() |
| 57 : epsilon_(std::tr1::get<3>(GetParam())), half_fill_(false) { | 59 : epsilon_(std::tr1::get<3>(GetParam())), half_fill_(false) { |
| 58 // Create input parameters based on test parameters. | 60 // Create input parameters based on test parameters. |
| 59 const int* const sample_rates = std::tr1::get<0>(GetParam()); | 61 const int* const sample_rates = std::tr1::get<0>(GetParam()); |
| 60 size_t sample_rates_count = std::tr1::get<1>(GetParam()); | 62 size_t sample_rates_count = std::tr1::get<1>(GetParam()); |
| 61 for (size_t i = 0; i < sample_rates_count; ++i) | 63 for (size_t i = 0; i < sample_rates_count; ++i) |
| 62 input_parameters_.push_back(AudioParameters( | 64 input_parameters_.push_back(AudioParameters( |
| 63 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, sample_rates[i], | 65 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, sample_rates[i], |
| 64 kBitsPerChannel, kHighLatencyBufferSize)); | 66 kBitsPerChannel, kHighLatencyBufferSize)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 77 | 79 |
| 78 audio_bus_ = AudioBus::Create(output_parameters_); | 80 audio_bus_ = AudioBus::Create(output_parameters_); |
| 79 expected_audio_bus_ = AudioBus::Create(output_parameters_); | 81 expected_audio_bus_ = AudioBus::Create(output_parameters_); |
| 80 | 82 |
| 81 // Allocate one callback for generating expected results. | 83 // Allocate one callback for generating expected results. |
| 82 double step = kSineCycles / static_cast<double>( | 84 double step = kSineCycles / static_cast<double>( |
| 83 output_parameters_.frames_per_buffer()); | 85 output_parameters_.frames_per_buffer()); |
| 84 expected_callback_.reset(new FakeAudioRenderCallback(step)); | 86 expected_callback_.reset(new FakeAudioRenderCallback(step)); |
| 85 } | 87 } |
| 86 | 88 |
| 87 AudioRendererMixer* GetMixer(const AudioParameters& params, | 89 AudioRendererMixer* GetMixer(int owner_id, |
| 90 const AudioParameters& params, |
| 88 const std::string& device_id, | 91 const std::string& device_id, |
| 89 const url::Origin& security_origin, | 92 const url::Origin& security_origin, |
| 90 OutputDeviceStatus* device_status) { | 93 OutputDeviceStatus* device_status) final { |
| 91 return mixer_.get(); | 94 return mixer_.get(); |
| 92 } | 95 }; |
| 93 | 96 |
| 94 MOCK_METHOD3(RemoveMixer, | 97 MOCK_METHOD4(ReturnMixer, |
| 95 void(const AudioParameters&, | 98 void(int, |
| 99 const AudioParameters&, |
| 96 const std::string&, | 100 const std::string&, |
| 97 const url::Origin&)); | 101 const url::Origin&)); |
| 98 | 102 |
| 103 MOCK_METHOD4( |
| 104 GetOutputDeviceInfo, |
| 105 OutputDeviceInfo(int, int, const std::string&, const url::Origin&)); |
| 106 |
| 99 void InitializeInputs(int inputs_per_sample_rate) { | 107 void InitializeInputs(int inputs_per_sample_rate) { |
| 100 mixer_inputs_.reserve(inputs_per_sample_rate * input_parameters_.size()); | 108 mixer_inputs_.reserve(inputs_per_sample_rate * input_parameters_.size()); |
| 101 fake_callbacks_.reserve(inputs_per_sample_rate * input_parameters_.size()); | 109 fake_callbacks_.reserve(inputs_per_sample_rate * input_parameters_.size()); |
| 102 | 110 |
| 103 for (size_t i = 0, input = 0; i < input_parameters_.size(); ++i) { | 111 for (size_t i = 0, input = 0; i < input_parameters_.size(); ++i) { |
| 104 // Setup FakeAudioRenderCallback step to compensate for resampling. | 112 // Setup FakeAudioRenderCallback step to compensate for resampling. |
| 105 double scale_factor = | 113 double scale_factor = |
| 106 input_parameters_[i].sample_rate() / | 114 input_parameters_[i].sample_rate() / |
| 107 static_cast<double>(output_parameters_.sample_rate()); | 115 static_cast<double>(output_parameters_.sample_rate()); |
| 108 double step = | 116 double step = |
| 109 kSineCycles / | 117 kSineCycles / |
| 110 (scale_factor * | 118 (scale_factor * |
| 111 static_cast<double>(output_parameters_.frames_per_buffer())); | 119 static_cast<double>(output_parameters_.frames_per_buffer())); |
| 112 | 120 |
| 113 for (int j = 0; j < inputs_per_sample_rate; ++j, ++input) { | 121 for (int j = 0; j < inputs_per_sample_rate; ++j, ++input) { |
| 114 fake_callbacks_.push_back(new FakeAudioRenderCallback(step)); | 122 fake_callbacks_.push_back(new FakeAudioRenderCallback(step)); |
| 115 mixer_inputs_.push_back(new AudioRendererMixerInput( | 123 mixer_inputs_.push_back(CreateMixerInput()); |
| 116 base::Bind(&AudioRendererMixerTest::GetMixer, | |
| 117 base::Unretained(this)), | |
| 118 base::Bind(&AudioRendererMixerTest::RemoveMixer, | |
| 119 base::Unretained(this)), | |
| 120 // Default device ID and security origin. | |
| 121 std::string(), url::Origin())); | |
| 122 mixer_inputs_[input]->Initialize(input_parameters_[i], | 124 mixer_inputs_[input]->Initialize(input_parameters_[i], |
| 123 fake_callbacks_[input]); | 125 fake_callbacks_[input]); |
| 124 mixer_inputs_[input]->SetVolume(1.0f); | 126 mixer_inputs_[input]->SetVolume(1.0f); |
| 125 } | 127 } |
| 126 } | 128 } |
| 127 EXPECT_CALL(*this, RemoveMixer(testing::_, testing::_, testing::_)) | |
| 128 .Times(mixer_inputs_.size()); | |
| 129 } | 129 } |
| 130 | 130 |
| 131 bool ValidateAudioData(int index, int frames, float scale, double epsilon) { | 131 bool ValidateAudioData(int index, int frames, float scale, double epsilon) { |
| 132 for (int i = 0; i < audio_bus_->channels(); ++i) { | 132 for (int i = 0; i < audio_bus_->channels(); ++i) { |
| 133 for (int j = index; j < frames; j++) { | 133 for (int j = index; j < frames; j++) { |
| 134 double error = fabs(audio_bus_->channel(i)[j] - | 134 double error = fabs(audio_bus_->channel(i)[j] - |
| 135 expected_audio_bus_->channel(i)[j] * scale); | 135 expected_audio_bus_->channel(i)[j] * scale); |
| 136 // The second comparison is for the case when scale is set to 0 | 136 // The second comparison is for the case when scale is set to 0 |
| 137 // (and less that 1 in general) | 137 // (and less that 1 in general) |
| 138 if ((error > epsilon * scale) && (error > epsilon)) { | 138 if ((error > epsilon * scale) && (error > epsilon)) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 if (mixer_inputs_.size() % 2) | 322 if (mixer_inputs_.size() % 2) |
| 323 mixer_inputs_[mixer_inputs_.size() - 1]->Stop(); | 323 mixer_inputs_[mixer_inputs_.size() - 1]->Stop(); |
| 324 | 324 |
| 325 ASSERT_TRUE(RenderAndValidateAudioData( | 325 ASSERT_TRUE(RenderAndValidateAudioData( |
| 326 std::max(1.f, static_cast<float>(floor(mixer_inputs_.size() / 2.f))))); | 326 std::max(1.f, static_cast<float>(floor(mixer_inputs_.size() / 2.f))))); |
| 327 | 327 |
| 328 for (size_t i = 1; i < mixer_inputs_.size(); i += 2) | 328 for (size_t i = 1; i < mixer_inputs_.size(); i += 2) |
| 329 mixer_inputs_[i]->Stop(); | 329 mixer_inputs_[i]->Stop(); |
| 330 } | 330 } |
| 331 | 331 |
| 332 scoped_refptr<AudioRendererMixerInput> CreateMixerInput() { |
| 333 return new AudioRendererMixerInput( |
| 334 this, |
| 335 // Zero frame id, default device ID and security origin. |
| 336 0, std::string(), url::Origin()); |
| 337 } |
| 338 |
| 332 protected: | 339 protected: |
| 333 virtual ~AudioRendererMixerTest() {} | 340 virtual ~AudioRendererMixerTest() {} |
| 334 | 341 |
| 335 scoped_refptr<MockAudioRendererSink> sink_; | 342 scoped_refptr<MockAudioRendererSink> sink_; |
| 336 std::unique_ptr<AudioRendererMixer> mixer_; | 343 std::unique_ptr<AudioRendererMixer> mixer_; |
| 337 AudioRendererSink::RenderCallback* mixer_callback_; | 344 AudioRendererSink::RenderCallback* mixer_callback_; |
| 338 std::vector<AudioParameters> input_parameters_; | 345 std::vector<AudioParameters> input_parameters_; |
| 339 AudioParameters output_parameters_; | 346 AudioParameters output_parameters_; |
| 340 std::unique_ptr<AudioBus> audio_bus_; | 347 std::unique_ptr<AudioBus> audio_bus_; |
| 341 std::unique_ptr<AudioBus> expected_audio_bus_; | 348 std::unique_ptr<AudioBus> expected_audio_bus_; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 } | 461 } |
| 455 | 462 |
| 456 // Fire the error before attaching any inputs. Ensure an error is recieved | 463 // Fire the error before attaching any inputs. Ensure an error is recieved |
| 457 // even if the input is not connected. | 464 // even if the input is not connected. |
| 458 mixer_callback_->OnRenderError(); | 465 mixer_callback_->OnRenderError(); |
| 459 | 466 |
| 460 for (size_t i = 0; i < mixer_inputs_.size(); ++i) | 467 for (size_t i = 0; i < mixer_inputs_.size(); ++i) |
| 461 mixer_inputs_[i]->Stop(); | 468 mixer_inputs_[i]->Stop(); |
| 462 } | 469 } |
| 463 | 470 |
| 464 // Ensure constructing an AudioRendererMixerInput, but not initializing it does | |
| 465 // not call RemoveMixer(). | |
| 466 TEST_P(AudioRendererMixerBehavioralTest, NoInitialize) { | |
| 467 EXPECT_CALL(*this, RemoveMixer(testing::_, testing::_, testing::_)).Times(0); | |
| 468 scoped_refptr<AudioRendererMixerInput> audio_renderer_mixer_input = | |
| 469 new AudioRendererMixerInput( | |
| 470 base::Bind(&AudioRendererMixerTest::GetMixer, base::Unretained(this)), | |
| 471 base::Bind(&AudioRendererMixerTest::RemoveMixer, | |
| 472 base::Unretained(this)), | |
| 473 // Default device ID and security origin. | |
| 474 std::string(), url::Origin()); | |
| 475 } | |
| 476 | |
| 477 // Ensure the physical stream is paused after a certain amount of time with no | 471 // Ensure the physical stream is paused after a certain amount of time with no |
| 478 // inputs playing. The test will hang if the behavior is incorrect. | 472 // inputs playing. The test will hang if the behavior is incorrect. |
| 479 TEST_P(AudioRendererMixerBehavioralTest, MixerPausesStream) { | 473 TEST_P(AudioRendererMixerBehavioralTest, MixerPausesStream) { |
| 480 const base::TimeDelta kPauseTime = base::TimeDelta::FromMilliseconds(500); | 474 const base::TimeDelta kPauseTime = base::TimeDelta::FromMilliseconds(500); |
| 481 // This value can't be too low or valgrind, tsan will timeout on the bots. | 475 // This value can't be too low or valgrind, tsan will timeout on the bots. |
| 482 const base::TimeDelta kTestTimeout = 10 * kPauseTime; | 476 const base::TimeDelta kTestTimeout = 10 * kPauseTime; |
| 483 mixer_->set_pause_delay_for_testing(kPauseTime); | 477 mixer_->set_pause_delay_for_testing(kPauseTime); |
| 484 | 478 |
| 485 base::WaitableEvent pause_event(true, false); | 479 base::WaitableEvent pause_event(true, false); |
| 486 EXPECT_CALL(*sink_.get(), Pause()).Times(2) | 480 EXPECT_CALL(*sink_.get(), Pause()).Times(2) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 INSTANTIATE_TEST_CASE_P( | 545 INSTANTIATE_TEST_CASE_P( |
| 552 AudioRendererMixerBehavioralTest, | 546 AudioRendererMixerBehavioralTest, |
| 553 AudioRendererMixerBehavioralTest, | 547 AudioRendererMixerBehavioralTest, |
| 554 testing::ValuesIn(std::vector<AudioRendererMixerTestData>( | 548 testing::ValuesIn(std::vector<AudioRendererMixerTestData>( |
| 555 1, | 549 1, |
| 556 std::tr1::make_tuple(&kTestInputLower, | 550 std::tr1::make_tuple(&kTestInputLower, |
| 557 1, | 551 1, |
| 558 kTestInputLower, | 552 kTestInputLower, |
| 559 0.00000048)))); | 553 0.00000048)))); |
| 560 } // namespace media | 554 } // namespace media |
| OLD | NEW |