| 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 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return mixer_.get(); | 70 return mixer_.get(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 MOCK_METHOD1(RemoveMixer, void(const AudioParameters&)); | 73 MOCK_METHOD1(RemoveMixer, void(const AudioParameters&)); |
| 74 | 74 |
| 75 void InitializeInputs(int count) { | 75 void InitializeInputs(int count) { |
| 76 mixer_inputs_.reserve(count); | 76 mixer_inputs_.reserve(count); |
| 77 fake_callbacks_.reserve(count); | 77 fake_callbacks_.reserve(count); |
| 78 | 78 |
| 79 // Setup FakeAudioRenderCallback step to compensate for resampling. | 79 // Setup FakeAudioRenderCallback step to compensate for resampling. |
| 80 double scale_factor = input_parameters_.sample_rate() | 80 double scale_factor = input_parameters_.sample_rate() / |
| 81 / static_cast<double>(output_parameters_.sample_rate()); | 81 static_cast<double>(output_parameters_.sample_rate()); |
| 82 double step = kSineCycles / (scale_factor * | 82 double step = kSineCycles / (scale_factor * |
| 83 static_cast<double>(output_parameters_.frames_per_buffer())); | 83 static_cast<double>(output_parameters_.frames_per_buffer())); |
| 84 | 84 |
| 85 for (int i = 0; i < count; ++i) { | 85 for (int i = 0; i < count; ++i) { |
| 86 fake_callbacks_.push_back(new FakeAudioRenderCallback(step)); | 86 fake_callbacks_.push_back(new FakeAudioRenderCallback(step)); |
| 87 mixer_inputs_.push_back(new AudioRendererMixerInput( | 87 mixer_inputs_.push_back(new AudioRendererMixerInput( |
| 88 base::Bind(&AudioRendererMixerTest::GetMixer, | 88 base::Bind(&AudioRendererMixerTest::GetMixer, |
| 89 base::Unretained(this)), | 89 base::Unretained(this)), |
| 90 base::Bind(&AudioRendererMixerTest::RemoveMixer, | 90 base::Bind(&AudioRendererMixerTest::RemoveMixer, |
| 91 base::Unretained(this)))); | 91 base::Unretained(this)))); |
| 92 mixer_inputs_[i]->Initialize(input_parameters_, fake_callbacks_[i]); | 92 mixer_inputs_[i]->Initialize(input_parameters_, fake_callbacks_[i]); |
| 93 mixer_inputs_[i]->SetVolume(1.0f); | 93 mixer_inputs_[i]->SetVolume(1.0f); |
| 94 } | 94 } |
| 95 EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(count); | 95 EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(count); |
| 96 } | 96 } |
| 97 | 97 |
| 98 bool ValidateAudioData(int index, int frames, float scale) { | 98 bool ValidateAudioData(int index, int frames, float scale, double epsilon) { |
| 99 for (int i = 0; i < audio_bus_->channels(); ++i) { | 99 for (int i = 0; i < audio_bus_->channels(); ++i) { |
| 100 for (int j = index; j < frames; j++) { | 100 for (int j = index; j < frames; j++) { |
| 101 double error = fabs(audio_bus_->channel(i)[j] - | 101 double error = fabs(audio_bus_->channel(i)[j] - |
| 102 expected_audio_bus_->channel(i)[j] * scale); | 102 expected_audio_bus_->channel(i)[j] * scale); |
| 103 if (error > epsilon_) { | 103 if (error > epsilon) { |
| 104 EXPECT_NEAR(expected_audio_bus_->channel(i)[j] * scale, | 104 EXPECT_NEAR(expected_audio_bus_->channel(i)[j] * scale, |
| 105 audio_bus_->channel(i)[j], epsilon_) | 105 audio_bus_->channel(i)[j], epsilon) |
| 106 << " i=" << i << ", j=" << j; | 106 << " i=" << i << ", j=" << j; |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 return true; | 111 return true; |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool ValidateAudioData(int index, int frames, float scale) { |
| 115 return ValidateAudioData(index, frames, scale, epsilon_); |
| 116 } |
| 117 |
| 114 bool RenderAndValidateAudioData(float scale) { | 118 bool RenderAndValidateAudioData(float scale) { |
| 115 // Half fill won't be exactly half when resampling since the resampler | |
| 116 // will have enough data to fill out more of the buffer based on its | |
| 117 // internal buffer and kernel size. So special case some of the checks. | |
| 118 bool resampling = input_parameters_.sample_rate() | |
| 119 != output_parameters_.sample_rate(); | |
| 120 | |
| 121 if (half_fill_) { | 119 if (half_fill_) { |
| 122 for (size_t i = 0; i < fake_callbacks_.size(); ++i) | 120 for (size_t i = 0; i < fake_callbacks_.size(); ++i) |
| 123 fake_callbacks_[i]->set_half_fill(true); | 121 fake_callbacks_[i]->set_half_fill(true); |
| 124 expected_callback_->set_half_fill(true); | 122 expected_callback_->set_half_fill(true); |
| 125 expected_audio_bus_->Zero(); | |
| 126 } | 123 } |
| 127 | 124 |
| 128 // Render actual audio data. | 125 // Render actual audio data. |
| 129 int frames = mixer_callback_->Render(audio_bus_.get(), 0); | 126 int frames = mixer_callback_->Render(audio_bus_.get(), 0); |
| 130 if (frames != audio_bus_->frames()) | 127 if (frames != audio_bus_->frames()) |
| 131 return false; | 128 return false; |
| 132 | 129 |
| 133 // Render expected audio data (without scaling). | 130 // Render expected audio data (without scaling). |
| 134 expected_callback_->Render(expected_audio_bus_.get(), 0); | 131 expected_callback_->Render(expected_audio_bus_.get(), 0); |
| 135 | 132 |
| 136 if (half_fill_) { | 133 if (half_fill_) { |
| 137 // Verify first half of audio data for both resampling and non-resampling. | 134 // In this case, just verify that every frame was initialized, this will |
| 138 if (!ValidateAudioData(0, frames / 2, scale)) | 135 // only fail under tooling such as valgrind. |
| 139 return false; | 136 return ValidateAudioData( |
| 140 // Verify silence in the second half if we're not resampling. | 137 0, frames, 0, std::numeric_limits<double>::max()); |
| 141 if (!resampling) | |
| 142 return ValidateAudioData(frames / 2, frames, 0); | |
| 143 return true; | |
| 144 } else { | 138 } else { |
| 145 return ValidateAudioData(0, frames, scale); | 139 return ValidateAudioData(0, frames, scale); |
| 146 } | 140 } |
| 147 } | 141 } |
| 148 | 142 |
| 149 // Fill |audio_bus_| fully with |value|. | 143 // Fill |audio_bus_| fully with |value|. |
| 150 void FillAudioData(float value) { | 144 void FillAudioData(float value) { |
| 151 for (int i = 0; i < audio_bus_->channels(); ++i) { | 145 for (int i = 0; i < audio_bus_->channels(); ++i) { |
| 152 std::fill(audio_bus_->channel(i), | 146 std::fill(audio_bus_->channel(i), |
| 153 audio_bus_->channel(i) + audio_bus_->frames(), value); | 147 audio_bus_->channel(i) + audio_bus_->frames(), value); |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 for (size_t i = 0; i < mixer_inputs_.size(); ++i) { | 375 for (size_t i = 0; i < mixer_inputs_.size(); ++i) { |
| 382 mixer_inputs_[i]->Start(); | 376 mixer_inputs_[i]->Start(); |
| 383 EXPECT_CALL(*fake_callbacks_[i], OnRenderError()).Times(1); | 377 EXPECT_CALL(*fake_callbacks_[i], OnRenderError()).Times(1); |
| 384 } | 378 } |
| 385 | 379 |
| 386 mixer_callback_->OnRenderError(); | 380 mixer_callback_->OnRenderError(); |
| 387 for (size_t i = 0; i < mixer_inputs_.size(); ++i) | 381 for (size_t i = 0; i < mixer_inputs_.size(); ++i) |
| 388 mixer_inputs_[i]->Stop(); | 382 mixer_inputs_[i]->Stop(); |
| 389 } | 383 } |
| 390 | 384 |
| 391 // Verify that audio delay information is scaled to the input parameters. | |
| 392 TEST_P(AudioRendererMixerTest, DelayTest) { | |
| 393 InitializeInputs(1); | |
| 394 static const int kAudioDelayMilliseconds = 100; | |
| 395 ASSERT_EQ(mixer_inputs_.size(), 1u); | |
| 396 | |
| 397 // Start the input and issue a single render callback. | |
| 398 mixer_inputs_[0]->Start(); | |
| 399 mixer_inputs_[0]->Play(); | |
| 400 mixer_callback_->Render(audio_bus_.get(), kAudioDelayMilliseconds); | |
| 401 | |
| 402 // The input to output ratio should only include the sample rate difference. | |
| 403 double io_ratio = input_parameters_.sample_rate() / | |
| 404 static_cast<double>(output_parameters_.sample_rate()); | |
| 405 | |
| 406 EXPECT_EQ(static_cast<int>(kAudioDelayMilliseconds / io_ratio), | |
| 407 fake_callbacks_[0]->last_audio_delay_milliseconds()); | |
| 408 mixer_inputs_[0]->Stop(); | |
| 409 } | |
| 410 | |
| 411 // Ensure constructing an AudioRendererMixerInput, but not initializing it does | 385 // Ensure constructing an AudioRendererMixerInput, but not initializing it does |
| 412 // not call RemoveMixer(). | 386 // not call RemoveMixer(). |
| 413 TEST_P(AudioRendererMixerTest, NoInitialize) { | 387 TEST_P(AudioRendererMixerTest, NoInitialize) { |
| 414 EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(0); | 388 EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(0); |
| 415 scoped_refptr<AudioRendererMixerInput> audio_renderer_mixer = | 389 scoped_refptr<AudioRendererMixerInput> audio_renderer_mixer = |
| 416 new AudioRendererMixerInput( | 390 new AudioRendererMixerInput( |
| 417 base::Bind(&AudioRendererMixerTest::GetMixer, | 391 base::Bind(&AudioRendererMixerTest::GetMixer, |
| 418 base::Unretained(this)), | 392 base::Unretained(this)), |
| 419 base::Bind(&AudioRendererMixerTest::RemoveMixer, | 393 base::Bind(&AudioRendererMixerTest::RemoveMixer, |
| 420 base::Unretained(this))); | 394 base::Unretained(this))); |
| 421 } | 395 } |
| 422 | 396 |
| 423 INSTANTIATE_TEST_CASE_P( | 397 INSTANTIATE_TEST_CASE_P( |
| 424 AudioRendererMixerTest, AudioRendererMixerTest, testing::Values( | 398 AudioRendererMixerTest, AudioRendererMixerTest, testing::Values( |
| 425 // No resampling. | 399 // No resampling. |
| 426 std::tr1::make_tuple(44100, 44100, 0.00000048), | 400 std::tr1::make_tuple(44100, 44100, 0.00000048), |
| 427 | 401 |
| 428 // Upsampling. | 402 // Upsampling. |
| 429 std::tr1::make_tuple(44100, 48000, 0.033), | 403 std::tr1::make_tuple(44100, 48000, 0.033), |
| 430 | 404 |
| 431 // Downsampling. | 405 // Downsampling. |
| 432 std::tr1::make_tuple(48000, 41000, 0.042))); | 406 std::tr1::make_tuple(48000, 41000, 0.042))); |
| 433 | 407 |
| 434 } // namespace media | 408 } // namespace media |
| OLD | NEW |