IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_source_provider_unittest.cc
blob5b7e8526898f93cd6287148093c658bda7e3ec80
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
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
7 #include "media/audio/audio_parameters.h"
8 #include "media/base/audio_bus.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
13 class WebRtcLocalAudioSourceProviderTest : public testing::Test {
14 protected:
15 virtual void SetUp() OVERRIDE {
16 source_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
17 media::CHANNEL_LAYOUT_MONO, 1, 0, 48000, 16, 480);
18 sink_params_.Reset(
19 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
20 media::CHANNEL_LAYOUT_STEREO, 2, 0, 44100, 16,
21 WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize);
22 const int length =
23 source_params_.frames_per_buffer() * source_params_.channels();
24 source_data_.reset(new int16[length]);
25 sink_bus_ = media::AudioBus::Create(sink_params_);
26 source_provider_.reset(new WebRtcLocalAudioSourceProvider());
27 source_provider_->SetSinkParamsForTesting(sink_params_);
28 source_provider_->OnSetFormat(source_params_);
31 media::AudioParameters source_params_;
32 scoped_ptr<int16[]> source_data_;
33 media::AudioParameters sink_params_;
34 scoped_ptr<media::AudioBus> sink_bus_;
35 scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
38 TEST_F(WebRtcLocalAudioSourceProviderTest, VerifyDataFlow) {
39 // Point the WebVector into memory owned by |sink_bus_|.
40 blink::WebVector<float*> audio_data(
41 static_cast<size_t>(sink_bus_->channels()));
42 for (size_t i = 0; i < audio_data.size(); ++i)
43 audio_data[i] = sink_bus_->channel(i);
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
47 // no available data in the FIFO.
48 source_provider_->provideInput(audio_data, sink_params_.frames_per_buffer());
49 EXPECT_TRUE(sink_bus_->channel(0)[0] == 0);
51 // Set the value of source data to be 1.
52 const int length =
53 source_params_.frames_per_buffer() * source_params_.channels();
54 std::fill(source_data_.get(), source_data_.get() + length, 1);
56 // Deliver data to |source_provider_|.
57 source_provider_->OnData(source_data_.get(),
58 source_params_.sample_rate(),
59 source_params_.channels(),
60 source_params_.frames_per_buffer());
62 // Consume the first packet in the resampler, which contains only zero.
63 // And the consumption of the data will trigger pulling the real packet from
64 // the source provider FIFO into the resampler.
65 // Note that we need to count in the provideInput() call a few lines above.
66 for (int i = sink_params_.frames_per_buffer();
67 i < source_params_.frames_per_buffer();
68 i += sink_params_.frames_per_buffer()) {
69 sink_bus_->Zero();
70 source_provider_->provideInput(audio_data,
71 sink_params_.frames_per_buffer());
72 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(0)[0]);
73 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(1)[0]);
76 // Prepare the second packet for featching.
77 source_provider_->OnData(source_data_.get(),
78 source_params_.sample_rate(),
79 source_params_.channels(),
80 source_params_.frames_per_buffer());
82 // Verify the packets.
83 for (int i = 0; i < source_params_.frames_per_buffer();
84 i += sink_params_.frames_per_buffer()) {
85 sink_bus_->Zero();
86 source_provider_->provideInput(audio_data,
87 sink_params_.frames_per_buffer());
88 EXPECT_GT(sink_bus_->channel(0)[0], 0);
89 EXPECT_GT(sink_bus_->channel(1)[0], 0);
90 EXPECT_DOUBLE_EQ(sink_bus_->channel(0)[0], sink_bus_->channel(1)[0]);
94 } // namespace content