Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: content/renderer/media/webrtc/processed_local_audio_source_unittest.cc

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE + Workaround to ensure MediaStreamAudioProcessor is destroyed on the main thread. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "build/build_config.h" 6 #include "build/build_config.h"
7 #include "content/public/renderer/media_stream_audio_sink.h" 7 #include "content/public/renderer/media_stream_audio_sink.h"
8 #include "content/renderer/media/media_stream_audio_track.h"
9 #include "content/renderer/media/mock_audio_device_factory.h"
8 #include "content/renderer/media/mock_constraint_factory.h" 10 #include "content/renderer/media/mock_constraint_factory.h"
9 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" 11 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h"
10 #include "content/renderer/media/webrtc_audio_capturer.h" 12 #include "content/renderer/media/webrtc/processed_local_audio_source.h"
11 #include "content/renderer/media/webrtc_local_audio_track.h"
12 #include "media/base/audio_bus.h" 13 #include "media/base/audio_bus.h"
13 #include "media/base/audio_parameters.h" 14 #include "media/base/audio_parameters.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 17 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
18 #include "third_party/WebKit/public/web/WebHeap.h"
17 19
18 using ::testing::_; 20 using ::testing::_;
19 using ::testing::AtLeast; 21 using ::testing::AtLeast;
22 using ::testing::Invoke;
23 using ::testing::WithArg;
20 24
21 namespace content { 25 namespace content {
22 26
23 namespace { 27 namespace {
24 28
25 class MockCapturerSource : public media::AudioCapturerSource { 29 // Audio parameters for the VerifyAudioFlowWithoutAudioProcessing test.
26 public: 30 constexpr int kSampleRate = 48000;
27 MockCapturerSource() {} 31 constexpr media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO;
28 MOCK_METHOD3(Initialize, void(const media::AudioParameters& params, 32 constexpr int kRequestedBufferSize = 512;
29 CaptureCallback* callback,
30 int session_id));
31 MOCK_METHOD0(Start, void());
32 MOCK_METHOD0(Stop, void());
33 MOCK_METHOD1(SetVolume, void(double volume));
34 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable));
35 33
36 protected: 34 // On Android, ProcessedLocalAudioSource forces a 20ms buffer size from the
37 ~MockCapturerSource() override {} 35 // input device.
38 }; 36 #if defined(OS_ANDROID)
37 constexpr int kExpectedSourceBufferSize = kSampleRate / 50;
38 #else
39 constexpr int kExpectedSourceBufferSize = kRequestedBufferSize;
40 #endif
41
42 // On both platforms, even though audio processing is turned off, the
43 // MediaStreamAudioProcessor will force the use of 10ms buffer sizes on the
44 // output end of its FIFO.
45 constexpr int kExpectedOutputBufferSize = kSampleRate / 100;
39 46
40 class MockMediaStreamAudioSink : public MediaStreamAudioSink { 47 class MockMediaStreamAudioSink : public MediaStreamAudioSink {
41 public: 48 public:
42 MockMediaStreamAudioSink() {} 49 MockMediaStreamAudioSink() {}
43 ~MockMediaStreamAudioSink() override {} 50 ~MockMediaStreamAudioSink() override {}
51
44 void OnData(const media::AudioBus& audio_bus, 52 void OnData(const media::AudioBus& audio_bus,
45 base::TimeTicks estimated_capture_time) override { 53 base::TimeTicks estimated_capture_time) override {
46 EXPECT_EQ(audio_bus.channels(), params_.channels()); 54 EXPECT_EQ(audio_bus.channels(), params_.channels());
47 EXPECT_EQ(audio_bus.frames(), params_.frames_per_buffer()); 55 EXPECT_EQ(audio_bus.frames(), params_.frames_per_buffer());
48 EXPECT_FALSE(estimated_capture_time.is_null()); 56 EXPECT_FALSE(estimated_capture_time.is_null());
49 OnDataCallback(); 57 OnDataCallback();
50 } 58 }
51 MOCK_METHOD0(OnDataCallback, void()); 59 MOCK_METHOD0(OnDataCallback, void());
60
52 void OnSetFormat(const media::AudioParameters& params) override { 61 void OnSetFormat(const media::AudioParameters& params) override {
53 params_ = params; 62 params_ = params;
54 FormatIsSet(); 63 FormatIsSet(params_);
55 } 64 }
56 MOCK_METHOD0(FormatIsSet, void()); 65 MOCK_METHOD1(FormatIsSet, void(const media::AudioParameters& params));
57 66
58 private: 67 private:
59 media::AudioParameters params_; 68 media::AudioParameters params_;
60 }; 69 };
61 70
62 } // namespace 71 } // namespace
63 72
64 class WebRtcAudioCapturerTest : public testing::Test { 73 class ProcessedLocalAudioSourceTest : public testing::Test {
65 protected: 74 protected:
66 WebRtcAudioCapturerTest() 75 ProcessedLocalAudioSourceTest() {}
67 #if defined(OS_ANDROID) 76
68 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 77 ~ProcessedLocalAudioSourceTest() override {}
69 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 960) { 78
70 // Android works with a buffer size bigger than 20ms. 79 void SetUp() override {
71 #else 80 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_label"),
72 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 81 blink::WebMediaStreamSource::TypeAudio,
73 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128) { 82 blink::WebString::fromUTF8("audio_track"),
74 #endif 83 false /* remote */);
84 blink_audio_track_.initialize(blink_audio_source_.id(),
85 blink_audio_source_);
75 } 86 }
76 87
77 void VerifyAudioParams(const blink::WebMediaConstraints& constraints, 88 void TearDown() override {
78 bool need_audio_processing) { 89 blink_audio_track_.reset();
79 const std::unique_ptr<WebRtcAudioCapturer> capturer = 90 blink_audio_source_.reset();
80 WebRtcAudioCapturer::CreateCapturer( 91 blink::WebHeap::collectAllGarbageForTesting();
81 -1, StreamDeviceInfo(
82 MEDIA_DEVICE_AUDIO_CAPTURE, "", "", params_.sample_rate(),
83 params_.channel_layout(), params_.frames_per_buffer()),
84 constraints, nullptr, nullptr);
85 const scoped_refptr<MockCapturerSource> capturer_source(
86 new MockCapturerSource());
87 EXPECT_CALL(*capturer_source.get(), Initialize(_, capturer.get(), -1));
88 EXPECT_CALL(*capturer_source.get(), SetAutomaticGainControl(true));
89 EXPECT_CALL(*capturer_source.get(), Start());
90 capturer->SetCapturerSource(capturer_source, params_);
91
92 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
93 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
94 const std::unique_ptr<WebRtcLocalAudioTrack> track(
95 new WebRtcLocalAudioTrack(adapter.get()));
96 capturer->AddTrack(track.get());
97
98 // Connect a mock sink to the track.
99 std::unique_ptr<MockMediaStreamAudioSink> sink(
100 new MockMediaStreamAudioSink());
101 track->AddSink(sink.get());
102
103 int delay_ms = 65;
104 bool key_pressed = true;
105 double volume = 0.9;
106
107 std::unique_ptr<media::AudioBus> audio_bus =
108 media::AudioBus::Create(params_);
109 audio_bus->Zero();
110
111 media::AudioCapturerSource::CaptureCallback* callback =
112 static_cast<media::AudioCapturerSource::CaptureCallback*>(
113 capturer.get());
114
115 // Verify the sink is getting the correct values.
116 EXPECT_CALL(*sink, FormatIsSet());
117 EXPECT_CALL(*sink, OnDataCallback()).Times(AtLeast(1));
118 callback->Capture(audio_bus.get(), delay_ms, volume, key_pressed);
119
120 track->RemoveSink(sink.get());
121 EXPECT_CALL(*capturer_source.get(), Stop());
122 capturer->Stop();
123 } 92 }
124 93
125 media::AudioParameters params_; 94 void CreateProcessedLocalAudioSource(
95 const blink::WebMediaConstraints& constraints) {
96 ProcessedLocalAudioSource* const source =
97 new ProcessedLocalAudioSource(
98 -1 /* consumer_render_frame_id is N/A for non-browser tests */,
99 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "Mock audio device",
100 "mock_audio_device_id", kSampleRate,
101 kChannelLayout, kRequestedBufferSize),
102 &mock_dependency_factory_);
103 source->SetAllowInvalidRenderFrameIdForTesting(true);
104 source->SetSourceConstraints(constraints);
105 blink_audio_source_.setExtraData(source); // Takes ownership.
106 }
107
108 void CheckSourceFormatMatches(const media::AudioParameters& params) {
109 EXPECT_EQ(kSampleRate, params.sample_rate());
110 EXPECT_EQ(kChannelLayout, params.channel_layout());
111 EXPECT_EQ(kExpectedSourceBufferSize, params.frames_per_buffer());
112 }
113
114 void CheckOutputFormatMatches(const media::AudioParameters& params) {
115 EXPECT_EQ(kSampleRate, params.sample_rate());
116 EXPECT_EQ(kChannelLayout, params.channel_layout());
117 EXPECT_EQ(kExpectedOutputBufferSize, params.frames_per_buffer());
118 }
119
120 MockAudioDeviceFactory* mock_audio_device_factory() {
121 return &mock_audio_device_factory_;
122 }
123
124 media::AudioCapturerSource::CaptureCallback* capture_source_callback() const {
125 return static_cast<media::AudioCapturerSource::CaptureCallback*>(
126 ProcessedLocalAudioSource::From(audio_source()));
127 }
128
129 MediaStreamAudioSource* audio_source() const {
130 return MediaStreamAudioSource::From(blink_audio_source_);
131 }
132
133 const blink::WebMediaStreamTrack& blink_audio_track() {
134 return blink_audio_track_;
135 }
136
137 private:
138 MockAudioDeviceFactory mock_audio_device_factory_;
139 MockPeerConnectionDependencyFactory mock_dependency_factory_;
140 blink::WebMediaStreamSource blink_audio_source_;
141 blink::WebMediaStreamTrack blink_audio_track_;
126 }; 142 };
127 143
128 TEST_F(WebRtcAudioCapturerTest, VerifyAudioParamsWithAudioProcessing) { 144 // Tests a basic end-to-end start-up, track+sink connections, audio flow, and
129 // Turn off the default constraints to verify that the sink will get packets 145 // shut-down. The unit tests in media_stream_audio_unittest.cc provide more
130 // with a buffer size smaller than 10ms. 146 // comprehensive testing of the object graph connections and multi-threading
147 // concerns.
148 TEST_F(ProcessedLocalAudioSourceTest, VerifyAudioFlowWithoutAudioProcessing) {
149 using ThisTest =
150 ProcessedLocalAudioSourceTest_VerifyAudioFlowWithoutAudioProcessing_Test;
151
152 // Turn off the default constraints so the sink will get audio in chunks of
153 // the native buffer size.
131 MockConstraintFactory constraint_factory; 154 MockConstraintFactory constraint_factory;
132 constraint_factory.DisableDefaultAudioConstraints(); 155 constraint_factory.DisableDefaultAudioConstraints();
133 VerifyAudioParams(constraint_factory.CreateWebMediaConstraints(), false); 156
157 CreateProcessedLocalAudioSource(
158 constraint_factory.CreateWebMediaConstraints());
159
160 // Connect the track, and expect the MockCapturerSource to be initialized and
161 // started by ProcessedLocalAudioSource.
162 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(),
163 Initialize(_, capture_source_callback(), -1))
164 .WillOnce(WithArg<0>(Invoke(this, &ThisTest::CheckSourceFormatMatches)));
165 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(),
166 SetAutomaticGainControl(true));
167 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(), Start());
168 ASSERT_TRUE(audio_source()->ConnectToTrack(blink_audio_track()));
169 CheckOutputFormatMatches(audio_source()->GetAudioParameters());
170
171 // Connect a sink to the track.
172 std::unique_ptr<MockMediaStreamAudioSink> sink(
173 new MockMediaStreamAudioSink());
174 EXPECT_CALL(*sink, FormatIsSet(_))
175 .WillOnce(Invoke(this, &ThisTest::CheckOutputFormatMatches));
176 MediaStreamAudioTrack::From(blink_audio_track())->AddSink(sink.get());
177
178 // Feed audio data into the ProcessedLocalAudioSource and expect it to reach
179 // the sink.
180 int delay_ms = 65;
181 bool key_pressed = true;
182 double volume = 0.9;
183 std::unique_ptr<media::AudioBus> audio_bus =
184 media::AudioBus::Create(2, kExpectedSourceBufferSize);
185 audio_bus->Zero();
186 EXPECT_CALL(*sink, OnDataCallback()).Times(AtLeast(1));
187 capture_source_callback()->Capture(audio_bus.get(), delay_ms, volume,
188 key_pressed);
189
190 // Expect the ProcessedLocalAudioSource to auto-stop the MockCapturerSource
191 // when the track is stopped.
192 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(), Stop());
193 MediaStreamAudioTrack::From(blink_audio_track())->Stop();
134 } 194 }
135 195
136 TEST_F(WebRtcAudioCapturerTest, FailToCreateCapturerWithWrongConstraints) { 196 // Tests that the source is not started when invalid audio constraints are
197 // present.
198 TEST_F(ProcessedLocalAudioSourceTest, FailToStartWithWrongConstraints) {
137 MockConstraintFactory constraint_factory; 199 MockConstraintFactory constraint_factory;
138 const std::string dummy_constraint = "dummy"; 200 const std::string dummy_constraint = "dummy";
139 // Set a non-audio constraint. 201 // Set a non-audio constraint.
140 constraint_factory.basic().width.setExact(240); 202 constraint_factory.basic().width.setExact(240);
141 203
142 std::unique_ptr<WebRtcAudioCapturer> capturer( 204 CreateProcessedLocalAudioSource(
143 WebRtcAudioCapturer::CreateCapturer( 205 constraint_factory.CreateWebMediaConstraints());
144 0, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "", 206
145 params_.sample_rate(), params_.channel_layout(), 207 // Expect the MockCapturerSource is never initialized/started and the
146 params_.frames_per_buffer()), 208 // ConnectToTrack() operation fails due to the invalid constraint.
147 constraint_factory.CreateWebMediaConstraints(), NULL, NULL)); 209 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(),
148 EXPECT_TRUE(capturer.get() == NULL); 210 Initialize(_, capture_source_callback(), -1))
211 .Times(0);
212 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(),
213 SetAutomaticGainControl(true)).Times(0);
214 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(), Start())
215 .Times(0);
216 EXPECT_FALSE(audio_source()->ConnectToTrack(blink_audio_track()));
217
218 // Even though ConnectToTrack() failed, there should still have been a new
219 // MediaStreamAudioTrack instance created, owned by the
220 // blink::WebMediaStreamTrack.
221 EXPECT_TRUE(MediaStreamAudioTrack::From(blink_audio_track()));
149 } 222 }
150 223
224 // TODO(miu): There's a lot of logic in ProcessedLocalAudioSource around
225 // constraints processing and validation that should have unit testing.
151 226
152 } // namespace content 227 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/webrtc/processed_local_audio_source.cc ('k') | content/renderer/media/webrtc/webrtc_audio_sink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698