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

Side by Side Diff: content/renderer/media/webrtc_local_audio_track_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698