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

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

Issue 1647773002: MediaStream audio sourcing: Bypass audio processing for non-WebRTC cases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: NOT FOR REVIEW -- This will be broken-up across multiple CLs. Created 4 years, 10 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 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/macros.h" 5 #include "base/macros.h"
6 #include "base/synchronization/waitable_event.h" 6 #include "base/synchronization/waitable_event.h"
7 #include "base/test/test_timeouts.h" 7 #include "base/test/test_timeouts.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "content/public/renderer/media_stream_audio_sink.h" 9 #include "content/public/renderer/media_stream_audio_sink.h"
10 #include "content/renderer/media/media_stream_audio_source.h"
11 #include "content/renderer/media/mock_media_constraint_factory.h" 10 #include "content/renderer/media/mock_media_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"
12 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" 13 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
13 #include "content/renderer/media/webrtc_audio_capturer.h" 14 #include "content/renderer/media/webrtc_audio_capturer.h"
14 #include "content/renderer/media/webrtc_local_audio_track.h" 15 #include "content/renderer/media/webrtc_local_audio_track.h"
15 #include "media/audio/audio_parameters.h" 16 #include "media/audio/audio_parameters.h"
16 #include "media/base/audio_bus.h" 17 #include "media/base/audio_bus.h"
17 #include "media/base/audio_capturer_source.h" 18 #include "media/base/audio_capturer_source.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 21 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
21 #include "third_party/WebKit/public/web/WebHeap.h" 22 #include "third_party/WebKit/public/web/WebHeap.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 89
89 class MockCapturerSource : public media::AudioCapturerSource { 90 class MockCapturerSource : public media::AudioCapturerSource {
90 public: 91 public:
91 explicit MockCapturerSource(WebRtcAudioCapturer* capturer) 92 explicit MockCapturerSource(WebRtcAudioCapturer* capturer)
92 : capturer_(capturer) {} 93 : capturer_(capturer) {}
93 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params, 94 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params,
94 CaptureCallback* callback, 95 CaptureCallback* callback,
95 int session_id)); 96 int session_id));
96 MOCK_METHOD0(OnStart, void()); 97 MOCK_METHOD0(OnStart, void());
97 MOCK_METHOD0(OnStop, void()); 98 MOCK_METHOD0(OnStop, void());
98 MOCK_METHOD1(SetVolume, void(double volume)); 99 void SetVolume(double volume) final {} // Ignored.
99 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); 100 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable));
100 101
101 void Initialize(const media::AudioParameters& params, 102 void Initialize(const media::AudioParameters& params,
102 CaptureCallback* callback, 103 CaptureCallback* callback,
103 int session_id) override { 104 int session_id) override {
104 DCHECK(params.IsValid()); 105 DCHECK(params.IsValid());
105 params_ = params; 106 params_ = params;
106 OnInitialize(params, callback, session_id); 107 OnInitialize(params, callback, session_id);
107 } 108 }
108 void Start() override { 109 void Start() override {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 148
148 private: 149 private:
149 media::AudioParameters params_; 150 media::AudioParameters params_;
150 }; 151 };
151 152
152 } // namespace 153 } // namespace
153 154
154 class WebRtcLocalAudioTrackTest : public ::testing::Test { 155 class WebRtcLocalAudioTrackTest : public ::testing::Test {
155 protected: 156 protected:
156 void SetUp() override { 157 void SetUp() override {
157 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 158 CreateSourceWithParams(media::AudioParameters(
158 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480); 159 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
159 MockMediaConstraintFactory constraint_factory; 160 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480));
160 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio,
161 "dummy",
162 false /* remote */, true /* readonly */);
163 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource();
164 blink_source_.setExtraData(audio_source);
165
166 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE,
167 std::string(), std::string());
168 capturer_ = WebRtcAudioCapturer::CreateCapturer(
169 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL,
170 audio_source);
171 audio_source->SetAudioCapturer(capturer_.get());
172 capturer_source_ = new MockCapturerSource(capturer_.get());
173 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1))
174 .WillOnce(Return());
175 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true));
176 EXPECT_CALL(*capturer_source_.get(), OnStart());
177 capturer_->SetCapturerSource(capturer_source_, params_);
178 } 161 }
179 162
180 void TearDown() override { 163 void TearDown() override {
181 blink_source_.reset(); 164 blink_source_.reset();
182 blink::WebHeap::collectAllGarbageForTesting(); 165 blink::WebHeap::collectAllGarbageForTesting();
183 } 166 }
184 167
168 void CreateSourceWithParams(const media::AudioParameters& params) {
169 params_ = params;
170 MockMediaConstraintFactory constraint_factory;
171 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio,
172 "dummy",
173 false /* remote */, true /* readonly */);
174 ProcessedLocalAudioSource* const audio_source =
175 new ProcessedLocalAudioSource(
176 -1,
177 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, std::string(),
178 std::string(), params_.sample_rate(),
179 params_.channel_layout(),
180 params_.frames_per_buffer()),
181 &mock_dependency_factory_);
182 audio_source->SetAllowInvalidRenderFrameIdForTesting(true);
183 audio_source->SetSourceConstraints(
184 MockMediaConstraintFactory().CreateWebMediaConstraints());
185 blink_source_.setExtraData(audio_source);
186
187 audio_source->StartSourceForTesting();
188 capturer_ = audio_source->audio_capturer();
189 capturer_source_ = new MockCapturerSource(capturer_);
190 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_, -1))
191 .WillOnce(Return());
192 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true));
193 EXPECT_CALL(*capturer_source_.get(), OnStart());
194 capturer_->SetCapturerSource(capturer_source_, params_);
195 }
196
197 MockPeerConnectionDependencyFactory mock_dependency_factory_;
185 media::AudioParameters params_; 198 media::AudioParameters params_;
186 blink::WebMediaStreamSource blink_source_; 199 blink::WebMediaStreamSource blink_source_;
187 scoped_refptr<MockCapturerSource> capturer_source_; 200 scoped_refptr<MockCapturerSource> capturer_source_;
188 scoped_refptr<WebRtcAudioCapturer> capturer_; 201 WebRtcAudioCapturer* capturer_; // Owned by ProcessedLocalAudioSource.
189 }; 202 };
190 203
191 // Creates a capturer and audio track, fakes its audio thread, and 204 // Creates a capturer and audio track, fakes its audio thread, and
192 // connect/disconnect the sink to the audio track on the fly, the sink should 205 // connect/disconnect the sink to the audio track on the fly, the sink should
193 // get data callback when the track is connected to the capturer but not when 206 // get data callback when the track is connected to the capturer but not when
194 // the track is disconnected from the capturer. 207 // the track is disconnected from the capturer.
195 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { 208 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) {
196 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 209 blink::WebMediaStreamTrack blink_track;
197 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 210 blink_track.initialize(blink::WebString::fromUTF8("dummy_track"),
198 scoped_ptr<WebRtcLocalAudioTrack> track( 211 blink_source_);
199 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); 212 ASSERT_TRUE(
200 track->Start(); 213 MediaStreamAudioSource::Get(blink_source_)->ConnectToTrack(blink_track));
201 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); 214
215 WebRtcLocalAudioTrack* const track = WebRtcLocalAudioTrack::From(
216 MediaStreamAudioTrack::Get(blink_track));
217 EXPECT_TRUE(track->adapter()->enabled());
202 218
203 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 219 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
204 base::WaitableEvent event(false, false); 220 base::WaitableEvent event(false, false);
205 EXPECT_CALL(*sink, FormatIsSet()); 221 EXPECT_CALL(*sink, FormatIsSet());
206 EXPECT_CALL(*sink, 222 EXPECT_CALL(*sink,
207 CaptureData()).Times(AtLeast(1)) 223 CaptureData()).Times(AtLeast(1))
208 .WillRepeatedly(SignalEvent(&event)); 224 .WillRepeatedly(SignalEvent(&event));
209 track->AddSink(sink.get()); 225 track->AddSink(sink.get());
210 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 226 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
211 track->RemoveSink(sink.get()); 227 track->RemoveSink(sink.get());
212 228
213 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 229 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return());
214 capturer_->Stop(); 230 capturer_->Stop();
215 } 231 }
216 232
217 // The same setup as ConnectAndDisconnectOneSink, but enable and disable the 233 // The same setup as ConnectAndDisconnectOneSink, but enable and disable the
218 // audio track on the fly. When the audio track is disabled, there is no data 234 // audio track on the fly. When the audio track is disabled, there is no data
219 // callback to the sink; when the audio track is enabled, there comes data 235 // callback to the sink; when the audio track is enabled, there comes data
220 // callback. 236 // callback.
221 // TODO(xians): Enable this test after resolving the racing issue that TSAN 237 // TODO(xians): Enable this test after resolving the racing issue that TSAN
222 // reports on MediaStreamTrack::enabled(); 238 // reports on MediaStreamTrack::enabled();
223 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_DisableEnableAudioTrack) { 239 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_DisableEnableAudioTrack) {
224 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 240 blink::WebMediaStreamTrack blink_track;
225 EXPECT_CALL(*capturer_source_.get(), OnStart()); 241 blink_track.initialize(blink::WebString::fromUTF8("dummy_track"),
226 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 242 blink_source_);
227 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 243 ASSERT_TRUE(
228 scoped_ptr<WebRtcLocalAudioTrack> track( 244 MediaStreamAudioSource::Get(blink_source_)->ConnectToTrack(blink_track));
229 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); 245
230 track->Start(); 246 WebRtcLocalAudioTrack* const track = WebRtcLocalAudioTrack::From(
231 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); 247 MediaStreamAudioTrack::Get(blink_track));
232 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(false)); 248 EXPECT_TRUE(track->adapter()->enabled());
249
233 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 250 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
234 const media::AudioParameters params = capturer_->source_audio_parameters(); 251 const media::AudioParameters params = capturer_->source_audio_parameters();
235 base::WaitableEvent event(false, false); 252 base::WaitableEvent event(false, false);
236 EXPECT_CALL(*sink, FormatIsSet()).Times(1); 253 EXPECT_CALL(*sink, FormatIsSet()).Times(1);
237 EXPECT_CALL(*sink, CaptureData()).Times(0); 254 EXPECT_CALL(*sink, CaptureData()).Times(0);
238 EXPECT_EQ(sink->audio_params().frames_per_buffer(), 255 EXPECT_EQ(sink->audio_params().frames_per_buffer(),
239 params.sample_rate() / 100); 256 params.sample_rate() / 100);
240 track->AddSink(sink.get()); 257 track->AddSink(sink.get());
241 EXPECT_FALSE(event.TimedWait(TestTimeouts::tiny_timeout())); 258 EXPECT_FALSE(event.TimedWait(TestTimeouts::tiny_timeout()));
242 259
243 event.Reset(); 260 event.Reset();
244 EXPECT_CALL(*sink, CaptureData()).Times(AtLeast(1)) 261 EXPECT_CALL(*sink, CaptureData()).Times(AtLeast(1))
245 .WillRepeatedly(SignalEvent(&event)); 262 .WillRepeatedly(SignalEvent(&event));
246 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(true)); 263 EXPECT_TRUE(track->adapter()->set_enabled(true));
247 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 264 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
248 track->RemoveSink(sink.get()); 265 track->RemoveSink(sink.get());
249 266
250 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 267 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return());
251 capturer_->Stop(); 268 capturer_->Stop();
252 track.reset();
253 } 269 }
254 270
255 // Create multiple audio tracks and enable/disable them, verify that the audio 271 // Create multiple audio tracks and enable/disable them, verify that the audio
256 // callbacks appear/disappear. 272 // callbacks appear/disappear.
257 // Flaky due to a data race, see http://crbug.com/295418 273 // Flaky due to a data race, see http://crbug.com/295418
258 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) { 274 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) {
259 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( 275 blink::WebMediaStreamTrack blink_track_1;
260 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 276 blink_track_1.initialize(blink::WebString::fromUTF8("dummy_track"),
261 scoped_ptr<WebRtcLocalAudioTrack> track_1( 277 blink_source_);
262 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL)); 278 ASSERT_TRUE(MediaStreamAudioSource::Get(blink_source_)
263 track_1->Start(); 279 ->ConnectToTrack(blink_track_1));
264 EXPECT_TRUE(track_1->GetAudioAdapter()->enabled()); 280
281 WebRtcLocalAudioTrack* const track_1 = WebRtcLocalAudioTrack::From(
282 MediaStreamAudioTrack::Get(blink_track_1));
283 EXPECT_TRUE(track_1->adapter()->enabled());
284
265 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); 285 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink());
266 const media::AudioParameters params = capturer_->source_audio_parameters(); 286 const media::AudioParameters params = capturer_->source_audio_parameters();
267 base::WaitableEvent event_1(false, false); 287 base::WaitableEvent event_1(false, false);
268 EXPECT_CALL(*sink_1, FormatIsSet()).WillOnce(Return()); 288 EXPECT_CALL(*sink_1, FormatIsSet()).WillOnce(Return());
269 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1)) 289 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1))
270 .WillRepeatedly(SignalEvent(&event_1)); 290 .WillRepeatedly(SignalEvent(&event_1));
271 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(), 291 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(),
272 params.sample_rate() / 100); 292 params.sample_rate() / 100);
273 track_1->AddSink(sink_1.get()); 293 track_1->AddSink(sink_1.get());
274 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); 294 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout()));
275 295
276 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( 296 blink::WebMediaStreamTrack blink_track_2;
277 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 297 blink_track_2.initialize(blink::WebString::fromUTF8("dummy_track"),
278 scoped_ptr<WebRtcLocalAudioTrack> track_2( 298 blink_source_);
279 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL)); 299 ASSERT_TRUE(MediaStreamAudioSource::Get(blink_source_)
280 track_2->Start(); 300 ->ConnectToTrack(blink_track_2));
281 EXPECT_TRUE(track_2->GetAudioAdapter()->enabled()); 301
302 WebRtcLocalAudioTrack* const track_2 = WebRtcLocalAudioTrack::From(
303 MediaStreamAudioTrack::Get(blink_track_2));
304 EXPECT_TRUE(track_2->adapter()->enabled());
282 305
283 // Verify both |sink_1| and |sink_2| get data. 306 // Verify both |sink_1| and |sink_2| get data.
284 event_1.Reset(); 307 event_1.Reset();
285 base::WaitableEvent event_2(false, false); 308 base::WaitableEvent event_2(false, false);
286 309
287 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); 310 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink());
288 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(Return()); 311 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(Return());
289 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1)) 312 EXPECT_CALL(*sink_1, CaptureData()).Times(AtLeast(1))
290 .WillRepeatedly(SignalEvent(&event_1)); 313 .WillRepeatedly(SignalEvent(&event_1));
291 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(), 314 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(),
292 params.sample_rate() / 100); 315 params.sample_rate() / 100);
293 EXPECT_CALL(*sink_2, CaptureData()).Times(AtLeast(1)) 316 EXPECT_CALL(*sink_2, CaptureData()).Times(AtLeast(1))
294 .WillRepeatedly(SignalEvent(&event_2)); 317 .WillRepeatedly(SignalEvent(&event_2));
295 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(), 318 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(),
296 params.sample_rate() / 100); 319 params.sample_rate() / 100);
297 track_2->AddSink(sink_2.get()); 320 track_2->AddSink(sink_2.get());
298 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); 321 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout()));
299 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); 322 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout()));
300 323
301 track_1->RemoveSink(sink_1.get()); 324 track_1->RemoveSink(sink_1.get());
302 track_1->Stop(); 325 track_1->Stop();
303 track_1.reset(); 326 blink_track_1.reset();
304 327
305 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 328 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return());
306 track_2->RemoveSink(sink_2.get()); 329 track_2->RemoveSink(sink_2.get());
307 track_2->Stop(); 330 track_2->Stop();
308 track_2.reset(); 331 blink_track_2.reset();
309 } 332 }
310 333
311 334
312 // Start one track and verify the capturer is correctly starting its source. 335 // Start one track and verify the capturer is correctly starting its source.
313 // And it should be fine to not to call Stop() explicitly. 336 // And it should be fine to not to call Stop() explicitly.
314 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { 337 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) {
315 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 338 blink::WebMediaStreamTrack blink_track;
316 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 339 blink_track.initialize(blink::WebString::fromUTF8("dummy_track"),
317 scoped_ptr<WebRtcLocalAudioTrack> track( 340 blink_source_);
318 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL)); 341 ASSERT_TRUE(
319 track->Start(); 342 MediaStreamAudioSource::Get(blink_source_)->ConnectToTrack(blink_track));
343
344 WebRtcLocalAudioTrack* const track = WebRtcLocalAudioTrack::From(
345 MediaStreamAudioTrack::Get(blink_track));
346 EXPECT_TRUE(track->adapter()->enabled());
320 347
321 // When the track goes away, it will automatically stop the 348 // When the track goes away, it will automatically stop the
322 // |capturer_source_|. 349 // |capturer_source_|.
323 EXPECT_CALL(*capturer_source_.get(), OnStop()); 350 EXPECT_CALL(*capturer_source_.get(), OnStop());
324 track.reset();
325 } 351 }
326 352
327 // Start two tracks and verify the capturer is correctly starting its source. 353 // Start two tracks and verify the capturer is correctly starting its source.
328 // When the last track connected to the capturer is stopped, the source is 354 // When the last track connected to the capturer is stopped, the source is
329 // stopped. 355 // stopped.
330 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) { 356 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) {
331 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter1( 357 blink::WebMediaStreamTrack blink_track_1;
332 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 358 blink_track_1.initialize(blink::WebString::fromUTF8("dummy_track"),
333 scoped_ptr<WebRtcLocalAudioTrack> track1( 359 blink_source_);
334 new WebRtcLocalAudioTrack(adapter1.get(), capturer_, NULL)); 360 ASSERT_TRUE(MediaStreamAudioSource::Get(blink_source_)
335 track1->Start(); 361 ->ConnectToTrack(blink_track_1));
336 362
337 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter2( 363 blink::WebMediaStreamTrack blink_track_2;
338 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 364 blink_track_2.initialize(blink::WebString::fromUTF8("dummy_track"),
339 scoped_ptr<WebRtcLocalAudioTrack> track2( 365 blink_source_);
340 new WebRtcLocalAudioTrack(adapter2.get(), capturer_, NULL)); 366 // Starting the second audio track will not start the |capturer_source_|
341 track2->Start(); 367 // since it has been started.
368 EXPECT_CALL(*capturer_source_.get(), OnStart()).Times(0);
369 ASSERT_TRUE(MediaStreamAudioSource::Get(blink_source_)
370 ->ConnectToTrack(blink_track_2));
342 371
343 track1->Stop(); 372 WebRtcLocalAudioTrack::From(MediaStreamAudioTrack::Get(blink_track_1))
373 ->Stop();
374
344 // When the last track is stopped, it will automatically stop the 375 // When the last track is stopped, it will automatically stop the
345 // |capturer_source_|. 376 // |capturer_source_|.
346 EXPECT_CALL(*capturer_source_.get(), OnStop()); 377 EXPECT_CALL(*capturer_source_.get(), OnStop());
347 track2->Stop(); 378 WebRtcLocalAudioTrack::From(MediaStreamAudioTrack::Get(blink_track_2))
348 } 379 ->Stop();
349
350 // Start/Stop tracks and verify the capturer is correctly starting/stopping
351 // its source.
352 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) {
353 base::WaitableEvent event(false, false);
354 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1(
355 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
356 scoped_ptr<WebRtcLocalAudioTrack> track_1(
357 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL));
358 track_1->Start();
359
360 // Verify the data flow by connecting the sink to |track_1|.
361 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
362 event.Reset();
363 EXPECT_CALL(*sink, FormatIsSet()).WillOnce(SignalEvent(&event));
364 EXPECT_CALL(*sink, CaptureData())
365 .Times(AnyNumber()).WillRepeatedly(Return());
366 track_1->AddSink(sink.get());
367 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
368
369 // Start the second audio track will not start the |capturer_source_|
370 // since it has been started.
371 EXPECT_CALL(*capturer_source_.get(), OnStart()).Times(0);
372 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2(
373 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
374 scoped_ptr<WebRtcLocalAudioTrack> track_2(
375 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL));
376 track_2->Start();
377
378 // Stop the capturer will clear up the track lists in the capturer.
379 EXPECT_CALL(*capturer_source_.get(), OnStop());
380 capturer_->Stop();
381
382 // Adding a new track to the capturer.
383 track_2->AddSink(sink.get());
384 EXPECT_CALL(*sink, FormatIsSet()).Times(0);
385
386 // Stop the capturer again will not trigger stopping the source of the
387 // capturer again..
388 event.Reset();
389 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0);
390 capturer_->Stop();
391 }
392
393 // Create a new capturer with new source, connect it to a new audio track.
394 #if defined(THREAD_SANITIZER)
395 // Fails under TSan, see https://crbug.com/576634.
396 #define MAYBE_ConnectTracksToDifferentCapturers \
397 DISABLED_ConnectTracksToDifferentCapturers
398 #else
399 #define MAYBE_ConnectTracksToDifferentCapturers \
400 ConnectTracksToDifferentCapturers
401 #endif
402 TEST_F(WebRtcLocalAudioTrackTest, MAYBE_ConnectTracksToDifferentCapturers) {
403 // Setup the first audio track and start it.
404 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1(
405 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
406 scoped_ptr<WebRtcLocalAudioTrack> track_1(
407 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL));
408 track_1->Start();
409
410 // Verify the data flow by connecting the |sink_1| to |track_1|.
411 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink());
412 EXPECT_CALL(*sink_1.get(), CaptureData())
413 .Times(AnyNumber()).WillRepeatedly(Return());
414 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber());
415 track_1->AddSink(sink_1.get());
416
417 // Create a new capturer with new source with different audio format.
418 MockMediaConstraintFactory constraint_factory;
419 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE,
420 std::string(), std::string());
421 scoped_refptr<WebRtcAudioCapturer> new_capturer(
422 WebRtcAudioCapturer::CreateCapturer(
423 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL,
424 NULL));
425 scoped_refptr<MockCapturerSource> new_source(
426 new MockCapturerSource(new_capturer.get()));
427 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1));
428 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true));
429 EXPECT_CALL(*new_source.get(), OnStart());
430
431 media::AudioParameters new_param(
432 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
433 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441);
434 new_capturer->SetCapturerSource(new_source, new_param);
435
436 // Setup the second audio track, connect it to the new capturer and start it.
437 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2(
438 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
439 scoped_ptr<WebRtcLocalAudioTrack> track_2(
440 new WebRtcLocalAudioTrack(adapter_2.get(), new_capturer, NULL));
441 track_2->Start();
442
443 // Verify the data flow by connecting the |sink_2| to |track_2|.
444 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink());
445 base::WaitableEvent event(false, false);
446 EXPECT_CALL(*sink_2, CaptureData())
447 .Times(AnyNumber()).WillRepeatedly(Return());
448 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(SignalEvent(&event));
449 track_2->AddSink(sink_2.get());
450 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
451
452 // Stopping the new source will stop the second track.
453 event.Reset();
454 EXPECT_CALL(*new_source.get(), OnStop())
455 .Times(1).WillOnce(SignalEvent(&event));
456 new_capturer->Stop();
457 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
458
459 // Stop the capturer of the first audio track.
460 EXPECT_CALL(*capturer_source_.get(), OnStop());
461 capturer_->Stop();
462 } 380 }
463 381
464 // Make sure a audio track can deliver packets with a buffer size smaller than 382 // Make sure a audio track can deliver packets with a buffer size smaller than
465 // 10ms when it is not connected with a peer connection. 383 // 10ms when it is not connected with a peer connection.
466 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { 384 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) {
467 // Setup a capturer which works with a buffer size smaller than 10ms. 385 // Setup a capturer which works with a buffer size smaller than 10ms.
468 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 386 CreateSourceWithParams(media::AudioParameters(
469 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); 387 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
470 388 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128));
471 // Create a capturer with new source which works with the format above.
472 MockMediaConstraintFactory factory;
473 factory.DisableDefaultAudioConstraints();
474 scoped_refptr<WebRtcAudioCapturer> capturer(
475 WebRtcAudioCapturer::CreateCapturer(
476 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", "",
477 params.sample_rate(), params.channel_layout(),
478 params.frames_per_buffer()),
479 factory.CreateWebMediaConstraints(), NULL, NULL));
480 scoped_refptr<MockCapturerSource> source(
481 new MockCapturerSource(capturer.get()));
482 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1));
483 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true));
484 EXPECT_CALL(*source.get(), OnStart());
485 capturer->SetCapturerSource(source, params);
486 389
487 // Setup a audio track, connect it to the capturer and start it. 390 // Setup a audio track, connect it to the capturer and start it.
488 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 391 blink::WebMediaStreamTrack blink_track;
489 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 392 blink_track.initialize(blink::WebString::fromUTF8("dummy_track"),
490 scoped_ptr<WebRtcLocalAudioTrack> track( 393 blink_source_);
491 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL)); 394 ASSERT_TRUE(
492 track->Start(); 395 MediaStreamAudioSource::Get(blink_source_)->ConnectToTrack(blink_track));
396 WebRtcLocalAudioTrack* const track = WebRtcLocalAudioTrack::From(
397 MediaStreamAudioTrack::Get(blink_track));
398 EXPECT_TRUE(track->adapter()->enabled());
493 399
494 // Verify the data flow by connecting the |sink| to |track|. 400 // Verify the data flow by connecting the |sink| to |track|.
495 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 401 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
496 base::WaitableEvent event(false, false); 402 base::WaitableEvent event(false, false);
497 EXPECT_CALL(*sink, FormatIsSet()).Times(1); 403 EXPECT_CALL(*sink, FormatIsSet()).Times(1);
498 // Verify the sinks are getting the packets with an expecting buffer size. 404 // Verify the sinks are getting the packets with an expecting buffer size.
499 #if defined(OS_ANDROID) 405 const int expected_buffer_size = params_.sample_rate() / 100;
500 const int expected_buffer_size = params.sample_rate() / 100;
501 #else
502 const int expected_buffer_size = params.frames_per_buffer();
503 #endif
504 EXPECT_CALL(*sink, CaptureData()) 406 EXPECT_CALL(*sink, CaptureData())
505 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); 407 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event));
506 track->AddSink(sink.get()); 408 track->AddSink(sink.get());
507 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 409 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
508 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); 410 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer());
509 411
510 // Stopping the new source will stop the second track. 412 // Stopping the new source will stop the second track.
511 EXPECT_CALL(*source.get(), OnStop()).Times(1); 413 EXPECT_CALL(*capturer_source_, OnStop()).Times(AtLeast(1));
512 capturer->Stop(); 414 capturer_->Stop();
513
514 // Even though this test don't use |capturer_source_| it will be stopped
515 // during teardown of the test harness.
516 EXPECT_CALL(*capturer_source_.get(), OnStop());
517 } 415 }
518 416
519 } // namespace content 417 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698