Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_source_provider_unittest.cc
blob82ff8f5f02e5006c92f2a050fc0b3f869d47c29f
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 "base/strings/utf_string_conversions.h"
7 #include "content/renderer/media/mock_media_constraint_factory.h"
8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
9 #include "content/renderer/media/webrtc_audio_capturer.h"
10 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
11 #include "content/renderer/media/webrtc_local_audio_track.h"
12 #include "media/audio/audio_parameters.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
16 #include "third_party/WebKit/public/web/WebHeap.h"
18 namespace content {
20 class WebRtcLocalAudioSourceProviderTest : public testing::Test {
21 protected:
22 void SetUp() override {
23 source_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
24 media::CHANNEL_LAYOUT_MONO, 1, 48000, 16, 480);
25 sink_params_.Reset(
26 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
27 media::CHANNEL_LAYOUT_STEREO, 2, 44100, 16,
28 WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize);
29 sink_bus_ = media::AudioBus::Create(sink_params_);
30 MockMediaConstraintFactory constraint_factory;
31 scoped_refptr<WebRtcAudioCapturer> capturer(
32 WebRtcAudioCapturer::CreateCapturer(
33 -1, StreamDeviceInfo(),
34 constraint_factory.CreateWebMediaConstraints(), NULL, NULL));
35 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
36 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
37 scoped_ptr<WebRtcLocalAudioTrack> native_track(
38 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL));
39 blink::WebMediaStreamSource audio_source;
40 audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"),
41 blink::WebMediaStreamSource::TypeAudio,
42 base::UTF8ToUTF16("dummy_source_name"),
43 false /* remote */, true /* readonly */);
44 blink_track_.initialize(blink::WebString::fromUTF8("audio_track"),
45 audio_source);
46 blink_track_.setExtraData(native_track.release());
47 source_provider_.reset(new WebRtcLocalAudioSourceProvider(blink_track_));
48 source_provider_->SetSinkParamsForTesting(sink_params_);
49 source_provider_->OnSetFormat(source_params_);
52 void TearDown() override {
53 source_provider_.reset();
54 blink_track_.reset();
55 blink::WebHeap::collectAllGarbageForTesting();
58 media::AudioParameters source_params_;
59 media::AudioParameters sink_params_;
60 scoped_ptr<media::AudioBus> sink_bus_;
61 blink::WebMediaStreamTrack blink_track_;
62 scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
65 TEST_F(WebRtcLocalAudioSourceProviderTest, VerifyDataFlow) {
66 // Point the WebVector into memory owned by |sink_bus_|.
67 blink::WebVector<float*> audio_data(
68 static_cast<size_t>(sink_bus_->channels()));
69 for (size_t i = 0; i < audio_data.size(); ++i)
70 audio_data[i] = sink_bus_->channel(i);
72 // Enable the |source_provider_| by asking for data. This will inject
73 // source_params_.frames_per_buffer() of zero into the resampler since there
74 // no available data in the FIFO.
75 source_provider_->provideInput(audio_data, sink_params_.frames_per_buffer());
76 EXPECT_TRUE(sink_bus_->channel(0)[0] == 0);
78 // Create a source AudioBus with channel data filled with non-zero values.
79 const scoped_ptr<media::AudioBus> source_bus =
80 media::AudioBus::Create(source_params_);
81 std::fill(source_bus->channel(0),
82 source_bus->channel(0) + source_bus->frames(),
83 0.5f);
85 // Deliver data to |source_provider_|.
86 base::TimeTicks estimated_capture_time = base::TimeTicks::Now();
87 source_provider_->OnData(*source_bus, estimated_capture_time);
89 // Consume the first packet in the resampler, which contains only zeros.
90 // And the consumption of the data will trigger pulling the real packet from
91 // the source provider FIFO into the resampler.
92 // Note that we need to count in the provideInput() call a few lines above.
93 for (int i = sink_params_.frames_per_buffer();
94 i < source_params_.frames_per_buffer();
95 i += sink_params_.frames_per_buffer()) {
96 sink_bus_->Zero();
97 source_provider_->provideInput(audio_data,
98 sink_params_.frames_per_buffer());
99 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(0)[0]);
100 EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(1)[0]);
103 // Make a second data delivery.
104 estimated_capture_time +=
105 source_bus->frames() * base::TimeDelta::FromSeconds(1) /
106 source_params_.sample_rate();
107 source_provider_->OnData(*source_bus, estimated_capture_time);
109 // Verify that non-zero data samples are present in the results of the
110 // following calls to provideInput().
111 for (int i = 0; i < source_params_.frames_per_buffer();
112 i += sink_params_.frames_per_buffer()) {
113 sink_bus_->Zero();
114 source_provider_->provideInput(audio_data,
115 sink_params_.frames_per_buffer());
116 EXPECT_NEAR(0.5f, sink_bus_->channel(0)[0], 0.001f);
117 EXPECT_NEAR(0.5f, sink_bus_->channel(1)[0], 0.001f);
118 EXPECT_DOUBLE_EQ(sink_bus_->channel(0)[0], sink_bus_->channel(1)[0]);
122 TEST_F(WebRtcLocalAudioSourceProviderTest,
123 DeleteSourceProviderBeforeStoppingTrack) {
124 source_provider_.reset();
126 // Stop the audio track.
127 WebRtcLocalAudioTrack* native_track = static_cast<WebRtcLocalAudioTrack*>(
128 MediaStreamTrack::GetTrack(blink_track_));
129 native_track->Stop();
132 TEST_F(WebRtcLocalAudioSourceProviderTest,
133 StopTrackBeforeDeletingSourceProvider) {
134 // Stop the audio track.
135 WebRtcLocalAudioTrack* native_track = static_cast<WebRtcLocalAudioTrack*>(
136 MediaStreamTrack::GetTrack(blink_track_));
137 native_track->Stop();
139 // Delete the source provider.
140 source_provider_.reset();
143 } // namespace content