OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/logging.h" | |
6 #include "build/build_config.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" | |
10 #include "content/renderer/media/mock_constraint_factory.h" | |
11 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.
h" | |
12 #include "content/renderer/media/webrtc/processed_local_audio_source.h" | |
13 #include "media/base/audio_bus.h" | |
14 #include "media/base/audio_parameters.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
18 #include "third_party/WebKit/public/web/WebHeap.h" | |
19 | |
20 using ::testing::_; | |
21 using ::testing::AtLeast; | |
22 using ::testing::Invoke; | |
23 using ::testing::WithArg; | |
24 | |
25 namespace content { | |
26 | |
27 namespace { | |
28 | |
29 // Audio parameters for the VerifyAudioFlowWithoutAudioProcessing test. | |
30 constexpr int kSampleRate = 48000; | |
31 constexpr media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO; | |
32 constexpr int kRequestedBufferSize = 512; | |
33 | |
34 // On Android, ProcessedLocalAudioSource forces a 20ms buffer size from the | |
35 // input device. | |
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; | |
46 | |
47 class MockMediaStreamAudioSink : public MediaStreamAudioSink { | |
48 public: | |
49 MockMediaStreamAudioSink() {} | |
50 ~MockMediaStreamAudioSink() override {} | |
51 | |
52 void OnData(const media::AudioBus& audio_bus, | |
53 base::TimeTicks estimated_capture_time) override { | |
54 EXPECT_EQ(audio_bus.channels(), params_.channels()); | |
55 EXPECT_EQ(audio_bus.frames(), params_.frames_per_buffer()); | |
56 EXPECT_FALSE(estimated_capture_time.is_null()); | |
57 OnDataCallback(); | |
58 } | |
59 MOCK_METHOD0(OnDataCallback, void()); | |
60 | |
61 void OnSetFormat(const media::AudioParameters& params) override { | |
62 params_ = params; | |
63 FormatIsSet(params_); | |
64 } | |
65 MOCK_METHOD1(FormatIsSet, void(const media::AudioParameters& params)); | |
66 | |
67 private: | |
68 media::AudioParameters params_; | |
69 }; | |
70 | |
71 } // namespace | |
72 | |
73 class ProcessedLocalAudioSourceTest : public testing::Test { | |
74 protected: | |
75 ProcessedLocalAudioSourceTest() {} | |
76 | |
77 ~ProcessedLocalAudioSourceTest() override {} | |
78 | |
79 void SetUp() override { | |
80 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_label"), | |
81 blink::WebMediaStreamSource::TypeAudio, | |
82 blink::WebString::fromUTF8("audio_track"), | |
83 false /* remote */); | |
84 blink_audio_track_.initialize(blink_audio_source_.id(), | |
85 blink_audio_source_); | |
86 } | |
87 | |
88 void TearDown() override { | |
89 blink_audio_track_.reset(); | |
90 blink_audio_source_.reset(); | |
91 blink::WebHeap::collectAllGarbageForTesting(); | |
92 } | |
93 | |
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_; | |
142 }; | |
143 | |
144 // Tests a basic end-to-end start-up, track+sink connections, audio flow, and | |
145 // shut-down. The unit tests in media_stream_audio_unittest.cc provide more | |
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. | |
154 MockConstraintFactory constraint_factory; | |
155 constraint_factory.DisableDefaultAudioConstraints(); | |
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(); | |
194 } | |
195 | |
196 // Tests that the source is not started when invalid audio constraints are | |
197 // present. | |
198 TEST_F(ProcessedLocalAudioSourceTest, FailToStartWithWrongConstraints) { | |
199 MockConstraintFactory constraint_factory; | |
200 const std::string dummy_constraint = "dummy"; | |
201 // Set a non-audio constraint. | |
202 constraint_factory.basic().width.setExact(240); | |
203 | |
204 CreateProcessedLocalAudioSource( | |
205 constraint_factory.CreateWebMediaConstraints()); | |
206 | |
207 // Expect the MockCapturerSource is never initialized/started and the | |
208 // ConnectToTrack() operation fails due to the invalid constraint. | |
209 EXPECT_CALL(*mock_audio_device_factory()->mock_capturer_source(), | |
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())); | |
222 } | |
223 | |
224 // TODO(miu): There's a lot of logic in ProcessedLocalAudioSource around | |
225 // constraints processing and validation that should have unit testing. | |
226 | |
227 } // namespace content | |
OLD | NEW |