| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "content/renderer/media/webrtc_local_audio_source_provider.h" | 6 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| 7 #include "media/audio/audio_parameters.h" | 7 #include "media/audio/audio_parameters.h" |
| 8 #include "media/base/audio_bus.h" | 8 #include "media/base/audio_bus.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 | 12 |
| 13 class WebRtcLocalAudioSourceProviderTest : public testing::Test { | 13 class WebRtcLocalAudioSourceProviderTest : public testing::Test { |
| 14 protected: | 14 protected: |
| 15 virtual void SetUp() OVERRIDE { | 15 virtual void SetUp() OVERRIDE { |
| 16 source_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 16 source_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 17 media::CHANNEL_LAYOUT_MONO, 1, 0, 48000, 16, 480); | 17 media::CHANNEL_LAYOUT_MONO, 1, 0, 48000, 16, 480); |
| 18 sink_params_.Reset( | 18 sink_params_.Reset( |
| 19 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 19 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 20 media::CHANNEL_LAYOUT_STEREO, 2, 0, 44100, 16, | 20 media::CHANNEL_LAYOUT_STEREO, 2, 0, 44100, 16, |
| 21 WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize); | 21 WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize); |
| 22 const int length = | 22 const int length = |
| 23 source_params_.frames_per_buffer() * source_params_.channels(); | 23 source_params_.frames_per_buffer() * source_params_.channels(); |
| 24 source_data_.reset(new int16[length]); | 24 source_data_.reset(new int16[length]); |
| 25 sink_bus_ = media::AudioBus::Create(sink_params_); | 25 sink_bus_ = media::AudioBus::Create(sink_params_); |
| 26 source_provider_.reset(new WebRtcLocalAudioSourceProvider()); | 26 source_provider_.reset(new WebRtcLocalAudioSourceProvider()); |
| 27 source_provider_->SetSinkParamsForTesting(sink_params_); | 27 source_provider_->SetSinkParamsForTesting(sink_params_); |
| 28 source_provider_->SetCaptureFormat(source_params_); | 28 source_provider_->OnSetFormat(source_params_); |
| 29 } | 29 } |
| 30 | 30 |
| 31 media::AudioParameters source_params_; | 31 media::AudioParameters source_params_; |
| 32 scoped_ptr<int16[]> source_data_; | 32 scoped_ptr<int16[]> source_data_; |
| 33 media::AudioParameters sink_params_; | 33 media::AudioParameters sink_params_; |
| 34 scoped_ptr<media::AudioBus> sink_bus_; | 34 scoped_ptr<media::AudioBus> sink_bus_; |
| 35 scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_; | 35 scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 TEST_F(WebRtcLocalAudioSourceProviderTest, VerifyDataFlow) { | 38 TEST_F(WebRtcLocalAudioSourceProviderTest, VerifyDataFlow) { |
| 39 // Point the WebVector into memory owned by |sink_bus_|. | 39 // Point the WebVector into memory owned by |sink_bus_|. |
| 40 blink::WebVector<float*> audio_data( | 40 blink::WebVector<float*> audio_data( |
| 41 static_cast<size_t>(sink_bus_->channels())); | 41 static_cast<size_t>(sink_bus_->channels())); |
| 42 for (size_t i = 0; i < audio_data.size(); ++i) | 42 for (size_t i = 0; i < audio_data.size(); ++i) |
| 43 audio_data[i] = sink_bus_->channel(i); | 43 audio_data[i] = sink_bus_->channel(i); |
| 44 | 44 |
| 45 // Enable the |source_provider_| by asking for data. This will inject | 45 // Enable the |source_provider_| by asking for data. This will inject |
| 46 // source_params_.frames_per_buffer() of zero into the resampler since there | 46 // source_params_.frames_per_buffer() of zero into the resampler since there |
| 47 // no available data in the FIFO. | 47 // no available data in the FIFO. |
| 48 source_provider_->provideInput(audio_data, sink_params_.frames_per_buffer()); | 48 source_provider_->provideInput(audio_data, sink_params_.frames_per_buffer()); |
| 49 EXPECT_TRUE(sink_bus_->channel(0)[0] == 0); | 49 EXPECT_TRUE(sink_bus_->channel(0)[0] == 0); |
| 50 | 50 |
| 51 // Set the value of source data to be 1. | 51 // Set the value of source data to be 1. |
| 52 const int length = | 52 const int length = |
| 53 source_params_.frames_per_buffer() * source_params_.channels(); | 53 source_params_.frames_per_buffer() * source_params_.channels(); |
| 54 std::fill(source_data_.get(), source_data_.get() + length, 1); | 54 std::fill(source_data_.get(), source_data_.get() + length, 1); |
| 55 | 55 |
| 56 // Deliver data to |source_provider_|. | 56 // Deliver data to |source_provider_|. |
| 57 std::vector<int> voe_channels; | 57 source_provider_->OnData(source_data_.get(), |
| 58 source_provider_->CaptureData(voe_channels, | 58 source_params_.sample_rate(), |
| 59 source_data_.get(), | 59 source_params_.channels(), |
| 60 source_params_.sample_rate(), | 60 source_params_.frames_per_buffer()); |
| 61 source_params_.channels(), | |
| 62 source_params_.frames_per_buffer(), | |
| 63 0, 0, false, false); | |
| 64 | 61 |
| 65 // Consume the first packet in the resampler, which contains only zero. | 62 // Consume the first packet in the resampler, which contains only zero. |
| 66 // And the consumption of the data will trigger pulling the real packet from | 63 // And the consumption of the data will trigger pulling the real packet from |
| 67 // the source provider FIFO into the resampler. | 64 // the source provider FIFO into the resampler. |
| 68 // Note that we need to count in the provideInput() call a few lines above. | 65 // Note that we need to count in the provideInput() call a few lines above. |
| 69 for (int i = sink_params_.frames_per_buffer(); | 66 for (int i = sink_params_.frames_per_buffer(); |
| 70 i < source_params_.frames_per_buffer(); | 67 i < source_params_.frames_per_buffer(); |
| 71 i += sink_params_.frames_per_buffer()) { | 68 i += sink_params_.frames_per_buffer()) { |
| 72 sink_bus_->Zero(); | 69 sink_bus_->Zero(); |
| 73 source_provider_->provideInput(audio_data, | 70 source_provider_->provideInput(audio_data, |
| 74 sink_params_.frames_per_buffer()); | 71 sink_params_.frames_per_buffer()); |
| 75 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(0)[0]); | 72 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(0)[0]); |
| 76 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(1)[0]); | 73 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(1)[0]); |
| 77 } | 74 } |
| 78 | 75 |
| 79 // Prepare the second packet for featching. | 76 // Prepare the second packet for featching. |
| 80 source_provider_->CaptureData(voe_channels, | 77 source_provider_->OnData(source_data_.get(), |
| 81 source_data_.get(), | 78 source_params_.sample_rate(), |
| 82 source_params_.sample_rate(), | 79 source_params_.channels(), |
| 83 source_params_.channels(), | 80 source_params_.frames_per_buffer()); |
| 84 source_params_.frames_per_buffer(), | |
| 85 0, 0, false, false); | |
| 86 | 81 |
| 87 // Verify the packets. | 82 // Verify the packets. |
| 88 for (int i = 0; i < source_params_.frames_per_buffer(); | 83 for (int i = 0; i < source_params_.frames_per_buffer(); |
| 89 i += sink_params_.frames_per_buffer()) { | 84 i += sink_params_.frames_per_buffer()) { |
| 90 sink_bus_->Zero(); | 85 sink_bus_->Zero(); |
| 91 source_provider_->provideInput(audio_data, | 86 source_provider_->provideInput(audio_data, |
| 92 sink_params_.frames_per_buffer()); | 87 sink_params_.frames_per_buffer()); |
| 93 EXPECT_GT(sink_bus_->channel(0)[0], 0); | 88 EXPECT_GT(sink_bus_->channel(0)[0], 0); |
| 94 EXPECT_GT(sink_bus_->channel(1)[0], 0); | 89 EXPECT_GT(sink_bus_->channel(1)[0], 0); |
| 95 EXPECT_DOUBLE_EQ(sink_bus_->channel(0)[0], sink_bus_->channel(1)[0]); | 90 EXPECT_DOUBLE_EQ(sink_bus_->channel(0)[0], sink_bus_->channel(1)[0]); |
| 96 } | 91 } |
| 97 } | 92 } |
| 98 | 93 |
| 99 } // namespace content | 94 } // namespace content |
| OLD | NEW |