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

Side by Side Diff: content/renderer/media/rtc_peer_connection_handler_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: Reworked unit tests around structural changes, and added exhaustive media_stream_audio_unittest.cc. 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/renderer/media/rtc_peer_connection_handler.h" 5 #include "content/renderer/media/rtc_peer_connection_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "content/child/child_process.h" 19 #include "content/child/child_process.h"
20 #include "content/renderer/media/media_stream.h" 20 #include "content/renderer/media/media_stream.h"
21 #include "content/renderer/media/media_stream_audio_source.h" 21 #include "content/renderer/media/media_stream_audio_source.h"
22 #include "content/renderer/media/media_stream_audio_track.h"
22 #include "content/renderer/media/media_stream_source.h" 23 #include "content/renderer/media/media_stream_source.h"
23 #include "content/renderer/media/media_stream_video_track.h" 24 #include "content/renderer/media/media_stream_video_track.h"
25 #include "content/renderer/media/mock_audio_device_factory.h"
26 #include "content/renderer/media/mock_constraint_factory.h"
24 #include "content/renderer/media/mock_data_channel_impl.h" 27 #include "content/renderer/media/mock_data_channel_impl.h"
25 #include "content/renderer/media/mock_media_stream_video_source.h" 28 #include "content/renderer/media/mock_media_stream_video_source.h"
26 #include "content/renderer/media/mock_peer_connection_impl.h" 29 #include "content/renderer/media/mock_peer_connection_impl.h"
27 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h" 30 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h"
28 #include "content/renderer/media/peer_connection_tracker.h" 31 #include "content/renderer/media/peer_connection_tracker.h"
29 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h" 32 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h"
30 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" 33 #include "content/renderer/media/webrtc/processed_local_audio_source.h"
31 #include "content/renderer/media/webrtc_local_audio_track.h"
32 #include "testing/gmock/include/gmock/gmock.h" 34 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
34 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 36 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
35 #include "third_party/WebKit/public/platform/WebMediaStream.h" 37 #include "third_party/WebKit/public/platform/WebMediaStream.h"
36 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" 38 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
37 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" 39 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
38 #include "third_party/WebKit/public/platform/WebRTCConfiguration.h" 40 #include "third_party/WebKit/public/platform/WebRTCConfiguration.h"
39 #include "third_party/WebKit/public/platform/WebRTCDTMFSenderHandler.h" 41 #include "third_party/WebKit/public/platform/WebRTCDTMFSenderHandler.h"
40 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandler.h" 42 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandler.h"
41 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h" 43 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 mock_dependency_factory_.reset(); 243 mock_dependency_factory_.reset();
242 mock_client_.reset(); 244 mock_client_.reset();
243 blink::WebHeap::collectAllGarbageForTesting(); 245 blink::WebHeap::collectAllGarbageForTesting();
244 } 246 }
245 247
246 // Creates a WebKit local MediaStream. 248 // Creates a WebKit local MediaStream.
247 blink::WebMediaStream CreateLocalMediaStream( 249 blink::WebMediaStream CreateLocalMediaStream(
248 const std::string& stream_label) { 250 const std::string& stream_label) {
249 std::string video_track_label("video-label"); 251 std::string video_track_label("video-label");
250 std::string audio_track_label("audio-label"); 252 std::string audio_track_label("audio-label");
251 blink::WebMediaStreamSource audio_source; 253 blink::WebMediaStreamSource blink_audio_source;
252 audio_source.initialize(blink::WebString::fromUTF8(audio_track_label), 254 blink_audio_source.initialize(blink::WebString::fromUTF8(audio_track_label),
253 blink::WebMediaStreamSource::TypeAudio, 255 blink::WebMediaStreamSource::TypeAudio,
254 blink::WebString::fromUTF8("audio_track"), 256 blink::WebString::fromUTF8("audio_track"),
255 false /* remote */, true /* readonly */); 257 false /* remote */, true /* readonly */);
256 audio_source.setExtraData(new MediaStreamAudioSource()); 258 ProcessedLocalAudioSource* const audio_source =
259 new ProcessedLocalAudioSource(
260 -1 /* consumer_render_frame_id is N/A for non-browser tests */,
261 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "Mock device",
262 "mock_device_id",
263 media::AudioParameters::kAudioCDSampleRate,
264 media::CHANNEL_LAYOUT_STEREO,
265 media::AudioParameters::kAudioCDSampleRate / 100),
266 mock_dependency_factory_.get());
267 audio_source->SetAllowInvalidRenderFrameIdForTesting(true);
268 audio_source->SetSourceConstraints(
269 MockConstraintFactory().CreateWebMediaConstraints());
270 blink_audio_source.setExtraData(audio_source); // Takes ownership.
271
257 blink::WebMediaStreamSource video_source; 272 blink::WebMediaStreamSource video_source;
258 video_source.initialize(blink::WebString::fromUTF8(video_track_label), 273 video_source.initialize(blink::WebString::fromUTF8(video_track_label),
259 blink::WebMediaStreamSource::TypeVideo, 274 blink::WebMediaStreamSource::TypeVideo,
260 blink::WebString::fromUTF8("video_track"), 275 blink::WebString::fromUTF8("video_track"),
261 false /* remote */, true /* readonly */); 276 false /* remote */, true /* readonly */);
262 MockMediaStreamVideoSource* native_video_source = 277 MockMediaStreamVideoSource* native_video_source =
263 new MockMediaStreamVideoSource(false); 278 new MockMediaStreamVideoSource(false);
264 video_source.setExtraData(native_video_source); 279 video_source.setExtraData(native_video_source);
265 280
266 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks( 281 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks(
267 static_cast<size_t>(1)); 282 static_cast<size_t>(1));
268 audio_tracks[0].initialize(audio_source.id(), audio_source); 283 audio_tracks[0].initialize(blink_audio_source.id(), blink_audio_source);
269 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 284 EXPECT_CALL(*mock_audio_device_factory_.mock_capturer_source(),
270 WebRtcLocalAudioTrackAdapter::Create(audio_track_label, nullptr)); 285 Initialize(_, _, -1));
271 std::unique_ptr<WebRtcLocalAudioTrack> native_track( 286 EXPECT_CALL(*mock_audio_device_factory_.mock_capturer_source(),
272 new WebRtcLocalAudioTrack(adapter.get())); 287 SetAutomaticGainControl(true));
273 audio_tracks[0].setExtraData(native_track.release()); 288 EXPECT_CALL(*mock_audio_device_factory_.mock_capturer_source(), Start());
289 EXPECT_CALL(*mock_audio_device_factory_.mock_capturer_source(), Stop());
290 CHECK(audio_source->ConnectToTrack(audio_tracks[0]));
274 blink::WebVector<blink::WebMediaStreamTrack> video_tracks( 291 blink::WebVector<blink::WebMediaStreamTrack> video_tracks(
275 static_cast<size_t>(1)); 292 static_cast<size_t>(1));
276 blink::WebMediaConstraints video_constraints; 293 blink::WebMediaConstraints video_constraints;
277 video_constraints.initialize(); 294 video_constraints.initialize();
278 video_tracks[0] = MediaStreamVideoTrack::CreateVideoTrack( 295 video_tracks[0] = MediaStreamVideoTrack::CreateVideoTrack(
279 native_video_source, video_constraints, 296 native_video_source, video_constraints,
280 MediaStreamVideoSource::ConstraintsCallback(), true); 297 MediaStreamVideoSource::ConstraintsCallback(), true);
281 298
282 blink::WebMediaStream local_stream; 299 blink::WebMediaStream local_stream;
283 local_stream.initialize(base::UTF8ToUTF16(stream_label), audio_tracks, 300 local_stream.initialize(base::UTF8ToUTF16(stream_label), audio_tracks,
(...skipping 13 matching lines...) Expand all
297 if (!video_track_label.empty()) { 314 if (!video_track_label.empty()) {
298 stream->AddTrack(MockWebRtcVideoTrack::Create(video_track_label).get()); 315 stream->AddTrack(MockWebRtcVideoTrack::Create(video_track_label).get());
299 } 316 }
300 if (!audio_track_label.empty()) { 317 if (!audio_track_label.empty()) {
301 stream->AddTrack(MockWebRtcAudioTrack::Create(audio_track_label).get()); 318 stream->AddTrack(MockWebRtcAudioTrack::Create(audio_track_label).get());
302 } 319 }
303 mock_peer_connection_->AddRemoteStream(stream.get()); 320 mock_peer_connection_->AddRemoteStream(stream.get());
304 return stream; 321 return stream;
305 } 322 }
306 323
324 void StopAllTracks(const blink::WebMediaStream& stream) {
325 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
326 stream.audioTracks(audio_tracks);
327 for (const auto& track : audio_tracks)
328 MediaStreamAudioTrack::From(track)->Stop();
329
330 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
331 stream.videoTracks(video_tracks);
332 for (const auto& track : video_tracks)
333 MediaStreamVideoTrack::GetVideoTrack(track)->Stop();
334 }
335
307 base::MessageLoop message_loop_; 336 base::MessageLoop message_loop_;
308 std::unique_ptr<ChildProcess> child_process_; 337 std::unique_ptr<ChildProcess> child_process_;
309 std::unique_ptr<MockWebRTCPeerConnectionHandlerClient> mock_client_; 338 std::unique_ptr<MockWebRTCPeerConnectionHandlerClient> mock_client_;
310 std::unique_ptr<MockPeerConnectionDependencyFactory> mock_dependency_factory_; 339 std::unique_ptr<MockPeerConnectionDependencyFactory> mock_dependency_factory_;
311 std::unique_ptr<NiceMock<MockPeerConnectionTracker>> mock_tracker_; 340 std::unique_ptr<NiceMock<MockPeerConnectionTracker>> mock_tracker_;
312 std::unique_ptr<RTCPeerConnectionHandlerUnderTest> pc_handler_; 341 std::unique_ptr<RTCPeerConnectionHandlerUnderTest> pc_handler_;
342 MockAudioDeviceFactory mock_audio_device_factory_;
313 343
314 // Weak reference to the mocked native peer connection implementation. 344 // Weak reference to the mocked native peer connection implementation.
315 MockPeerConnectionImpl* mock_peer_connection_; 345 MockPeerConnectionImpl* mock_peer_connection_;
316 }; 346 };
317 347
318 TEST_F(RTCPeerConnectionHandlerTest, Destruct) { 348 TEST_F(RTCPeerConnectionHandlerTest, Destruct) {
319 EXPECT_CALL(*mock_tracker_.get(), UnregisterPeerConnection(pc_handler_.get())) 349 EXPECT_CALL(*mock_tracker_.get(), UnregisterPeerConnection(pc_handler_.get()))
320 .Times(1); 350 .Times(1);
321 pc_handler_.reset(NULL); 351 pc_handler_.reset(NULL);
322 } 352 }
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label()); 517 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label());
488 EXPECT_EQ(1u, 518 EXPECT_EQ(1u,
489 mock_peer_connection_->local_streams()->at(0)->GetAudioTracks().size()); 519 mock_peer_connection_->local_streams()->at(0)->GetAudioTracks().size());
490 EXPECT_EQ(1u, 520 EXPECT_EQ(1u,
491 mock_peer_connection_->local_streams()->at(0)->GetVideoTracks().size()); 521 mock_peer_connection_->local_streams()->at(0)->GetVideoTracks().size());
492 522
493 EXPECT_FALSE(pc_handler_->addStream(local_stream, constraints)); 523 EXPECT_FALSE(pc_handler_->addStream(local_stream, constraints));
494 524
495 pc_handler_->removeStream(local_stream); 525 pc_handler_->removeStream(local_stream);
496 EXPECT_EQ(0u, mock_peer_connection_->local_streams()->count()); 526 EXPECT_EQ(0u, mock_peer_connection_->local_streams()->count());
527
528 StopAllTracks(local_stream);
497 } 529 }
498 530
499 TEST_F(RTCPeerConnectionHandlerTest, addStreamWithStoppedAudioAndVideoTrack) { 531 TEST_F(RTCPeerConnectionHandlerTest, addStreamWithStoppedAudioAndVideoTrack) {
500 std::string stream_label = "local_stream"; 532 std::string stream_label = "local_stream";
501 blink::WebMediaStream local_stream( 533 blink::WebMediaStream local_stream(
502 CreateLocalMediaStream(stream_label)); 534 CreateLocalMediaStream(stream_label));
503 blink::WebMediaConstraints constraints; 535 blink::WebMediaConstraints constraints;
504 536
505 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; 537 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
506 local_stream.audioTracks(audio_tracks); 538 local_stream.audioTracks(audio_tracks);
507 MediaStreamAudioSource* native_audio_source = 539 MediaStreamAudioSource* native_audio_source =
508 MediaStreamAudioSource::From(audio_tracks[0].source()); 540 MediaStreamAudioSource::From(audio_tracks[0].source());
509 native_audio_source->StopSource(); 541 native_audio_source->StopSource();
510 542
511 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; 543 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
512 local_stream.videoTracks(video_tracks); 544 local_stream.videoTracks(video_tracks);
513 MediaStreamVideoSource* native_video_source = 545 MediaStreamVideoSource* native_video_source =
514 static_cast<MediaStreamVideoSource*>( 546 static_cast<MediaStreamVideoSource*>(
515 video_tracks[0].source().getExtraData()); 547 video_tracks[0].source().getExtraData());
516 native_video_source->StopSource(); 548 native_video_source->StopSource();
517 549
518 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints)); 550 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints));
519 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label()); 551 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label());
520 EXPECT_EQ( 552 EXPECT_EQ(
521 1u, 553 1u,
522 mock_peer_connection_->local_streams()->at(0)->GetAudioTracks().size()); 554 mock_peer_connection_->local_streams()->at(0)->GetAudioTracks().size());
523 EXPECT_EQ( 555 EXPECT_EQ(
524 1u, 556 1u,
525 mock_peer_connection_->local_streams()->at(0)->GetVideoTracks().size()); 557 mock_peer_connection_->local_streams()->at(0)->GetVideoTracks().size());
558
559 StopAllTracks(local_stream);
526 } 560 }
527 561
528 TEST_F(RTCPeerConnectionHandlerTest, GetStatsNoSelector) { 562 TEST_F(RTCPeerConnectionHandlerTest, GetStatsNoSelector) {
529 scoped_refptr<MockRTCStatsRequest> request( 563 scoped_refptr<MockRTCStatsRequest> request(
530 new rtc::RefCountedObject<MockRTCStatsRequest>()); 564 new rtc::RefCountedObject<MockRTCStatsRequest>());
531 pc_handler_->getStats(request.get()); 565 pc_handler_->getStats(request.get());
532 base::RunLoop().RunUntilIdle(); 566 base::RunLoop().RunUntilIdle();
533 ASSERT_TRUE(request->result()); 567 ASSERT_TRUE(request->result());
534 EXPECT_LT(1, request->result()->report_count()); 568 EXPECT_LT(1, request->result()->report_count());
535 } 569 }
(...skipping 17 matching lines...) Expand all
553 blink::WebVector<blink::WebMediaStreamTrack> tracks; 587 blink::WebVector<blink::WebMediaStreamTrack> tracks;
554 local_stream.audioTracks(tracks); 588 local_stream.audioTracks(tracks);
555 ASSERT_LE(1ul, tracks.size()); 589 ASSERT_LE(1ul, tracks.size());
556 590
557 scoped_refptr<MockRTCStatsRequest> request( 591 scoped_refptr<MockRTCStatsRequest> request(
558 new rtc::RefCountedObject<MockRTCStatsRequest>()); 592 new rtc::RefCountedObject<MockRTCStatsRequest>());
559 request->setSelector(tracks[0]); 593 request->setSelector(tracks[0]);
560 pc_handler_->getStats(request.get()); 594 pc_handler_->getStats(request.get());
561 base::RunLoop().RunUntilIdle(); 595 base::RunLoop().RunUntilIdle();
562 EXPECT_EQ(1, request->result()->report_count()); 596 EXPECT_EQ(1, request->result()->report_count());
597
598 StopAllTracks(local_stream);
563 } 599 }
564 600
565 TEST_F(RTCPeerConnectionHandlerTest, GetStatsWithRemoteSelector) { 601 TEST_F(RTCPeerConnectionHandlerTest, GetStatsWithRemoteSelector) {
566 scoped_refptr<webrtc::MediaStreamInterface> stream( 602 scoped_refptr<webrtc::MediaStreamInterface> stream(
567 AddRemoteMockMediaStream("remote_stream", "video", "audio")); 603 AddRemoteMockMediaStream("remote_stream", "video", "audio"));
568 pc_handler_->observer()->OnAddStream(stream.get()); 604 pc_handler_->observer()->OnAddStream(stream.get());
569 base::RunLoop().RunUntilIdle(); 605 base::RunLoop().RunUntilIdle();
570 const blink::WebMediaStream& remote_stream = mock_client_->remote_stream(); 606 const blink::WebMediaStream& remote_stream = mock_client_->remote_stream();
571 607
572 blink::WebVector<blink::WebMediaStreamTrack> tracks; 608 blink::WebVector<blink::WebMediaStreamTrack> tracks;
(...skipping 19 matching lines...) Expand all
592 local_stream.audioTracks(tracks); 628 local_stream.audioTracks(tracks);
593 blink::WebMediaStreamTrack component = tracks[0]; 629 blink::WebMediaStreamTrack component = tracks[0];
594 mock_peer_connection_->SetGetStatsResult(false); 630 mock_peer_connection_->SetGetStatsResult(false);
595 631
596 scoped_refptr<MockRTCStatsRequest> request( 632 scoped_refptr<MockRTCStatsRequest> request(
597 new rtc::RefCountedObject<MockRTCStatsRequest>()); 633 new rtc::RefCountedObject<MockRTCStatsRequest>());
598 request->setSelector(component); 634 request->setSelector(component);
599 pc_handler_->getStats(request.get()); 635 pc_handler_->getStats(request.get());
600 base::RunLoop().RunUntilIdle(); 636 base::RunLoop().RunUntilIdle();
601 EXPECT_EQ(0, request->result()->report_count()); 637 EXPECT_EQ(0, request->result()->report_count());
638
639 StopAllTracks(local_stream);
602 } 640 }
603 641
604 TEST_F(RTCPeerConnectionHandlerTest, OnSignalingChange) { 642 TEST_F(RTCPeerConnectionHandlerTest, OnSignalingChange) {
605 testing::InSequence sequence; 643 testing::InSequence sequence;
606 644
607 webrtc::PeerConnectionInterface::SignalingState new_state = 645 webrtc::PeerConnectionInterface::SignalingState new_state =
608 webrtc::PeerConnectionInterface::kHaveRemoteOffer; 646 webrtc::PeerConnectionInterface::kHaveRemoteOffer;
609 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange( 647 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
610 pc_handler_.get(), 648 pc_handler_.get(),
611 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer)); 649 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer));
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 local_stream.audioTracks(tracks); 1056 local_stream.audioTracks(tracks);
1019 ASSERT_LE(1ul, tracks.size()); 1057 ASSERT_LE(1ul, tracks.size());
1020 1058
1021 EXPECT_CALL(*mock_tracker_.get(), 1059 EXPECT_CALL(*mock_tracker_.get(),
1022 TrackCreateDTMFSender(pc_handler_.get(), 1060 TrackCreateDTMFSender(pc_handler_.get(),
1023 testing::Ref(tracks[0]))); 1061 testing::Ref(tracks[0])));
1024 1062
1025 std::unique_ptr<blink::WebRTCDTMFSenderHandler> sender( 1063 std::unique_ptr<blink::WebRTCDTMFSenderHandler> sender(
1026 pc_handler_->createDTMFSender(tracks[0])); 1064 pc_handler_->createDTMFSender(tracks[0]));
1027 EXPECT_TRUE(sender.get()); 1065 EXPECT_TRUE(sender.get());
1066
1067 StopAllTracks(local_stream);
1028 } 1068 }
1029 1069
1030 } // namespace content 1070 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698