blob: 20690d24874db977b91652b59b56483931650dc7 [file] [log] [blame]
Steve Anton8d3444d2017-10-20 15:30:51 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// This file contains tests that check the interaction between the
12// PeerConnection and the underlying media engine, as well as tests that check
13// the media-related aspects of SDP.
14
15#include <tuple>
16
17#include "call/callfactoryinterface.h"
18#include "logging/rtc_event_log/rtc_event_log_factory.h"
19#include "media/base/fakemediaengine.h"
20#include "p2p/base/fakeportallocator.h"
21#include "pc/mediasession.h"
22#include "pc/peerconnectionwrapper.h"
23#include "pc/sdputils.h"
24#ifdef WEBRTC_ANDROID
25#include "pc/test/androidtestinitializer.h"
26#endif
27#include "pc/test/fakertccertificategenerator.h"
28#include "rtc_base/gunit.h"
29#include "rtc_base/ptr_util.h"
30#include "rtc_base/virtualsocketserver.h"
31#include "test/gmock.h"
32
33namespace webrtc {
34
35using cricket::FakeMediaEngine;
36using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
37using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
38using ::testing::Bool;
39using ::testing::Combine;
40using ::testing::Values;
41using ::testing::ElementsAre;
42
43class PeerConnectionWrapperForMediaTest : public PeerConnectionWrapper {
44 public:
45 using PeerConnectionWrapper::PeerConnectionWrapper;
46
47 FakeMediaEngine* media_engine() { return media_engine_; }
48 void set_media_engine(FakeMediaEngine* media_engine) {
49 media_engine_ = media_engine;
50 }
51
52 private:
53 FakeMediaEngine* media_engine_;
54};
55
56class PeerConnectionMediaTest : public ::testing::Test {
57 protected:
58 typedef std::unique_ptr<PeerConnectionWrapperForMediaTest> WrapperPtr;
59
60 PeerConnectionMediaTest()
61 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
62#ifdef WEBRTC_ANDROID
63 InitializeAndroidObjects();
64#endif
65 }
66
67 WrapperPtr CreatePeerConnection() {
68 return CreatePeerConnection(RTCConfiguration());
69 }
70
71 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
72 auto media_engine = rtc::MakeUnique<FakeMediaEngine>();
73 auto* media_engine_ptr = media_engine.get();
74 auto pc_factory = CreateModularPeerConnectionFactory(
75 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
76 std::move(media_engine), CreateCallFactory(),
77 CreateRtcEventLogFactory());
78
79 auto fake_port_allocator = rtc::MakeUnique<cricket::FakePortAllocator>(
80 rtc::Thread::Current(), nullptr);
81 auto observer = rtc::MakeUnique<MockPeerConnectionObserver>();
82 auto pc = pc_factory->CreatePeerConnection(
83 config, std::move(fake_port_allocator), nullptr, observer.get());
84 if (!pc) {
85 return nullptr;
86 }
87
88 auto wrapper = rtc::MakeUnique<PeerConnectionWrapperForMediaTest>(
89 pc_factory, pc, std::move(observer));
90 wrapper->set_media_engine(media_engine_ptr);
91 return wrapper;
92 }
93
94 // Accepts the same arguments as CreatePeerConnection and adds default audio
95 // and video tracks.
96 template <typename... Args>
97 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
98 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
99 if (!wrapper) {
100 return nullptr;
101 }
102 wrapper->AddAudioTrack("a");
103 wrapper->AddVideoTrack("v");
104 return wrapper;
105 }
106
107 const cricket::MediaContentDescription* GetMediaContent(
108 const SessionDescriptionInterface* sdesc,
109 const std::string& mid) {
110 const auto* content_desc =
111 sdesc->description()->GetContentDescriptionByName(mid);
112 return static_cast<const cricket::MediaContentDescription*>(content_desc);
113 }
114
115 cricket::MediaContentDirection GetMediaContentDirection(
116 const SessionDescriptionInterface* sdesc,
117 const std::string& mid) {
118 auto* media_content = GetMediaContent(sdesc, mid);
119 RTC_DCHECK(media_content);
120 return media_content->direction();
121 }
122
123 std::unique_ptr<rtc::VirtualSocketServer> vss_;
124 rtc::AutoSocketServerThread main_;
125};
126
127TEST_F(PeerConnectionMediaTest,
128 FailToSetRemoteDescriptionIfCreateMediaChannelFails) {
129 auto caller = CreatePeerConnectionWithAudioVideo();
130 auto callee = CreatePeerConnectionWithAudioVideo();
131 callee->media_engine()->set_fail_create_channel(true);
132
133 std::string error;
134 ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
135 EXPECT_EQ("Failed to set remote offer sdp: Failed to create channels.",
136 error);
137}
138
139TEST_F(PeerConnectionMediaTest,
140 FailToSetLocalDescriptionIfCreateMediaChannelFails) {
141 auto caller = CreatePeerConnectionWithAudioVideo();
142 caller->media_engine()->set_fail_create_channel(true);
143
144 std::string error;
145 ASSERT_FALSE(caller->SetLocalDescription(caller->CreateOffer(), &error));
146 EXPECT_EQ("Failed to set local offer sdp: Failed to create channels.", error);
147}
148
149std::vector<std::string> GetIds(
150 const std::vector<cricket::StreamParams>& streams) {
151 std::vector<std::string> ids;
152 for (const auto& stream : streams) {
153 ids.push_back(stream.id);
154 }
155 return ids;
156}
157
158// Test that exchanging an offer and answer with each side having an audio and
159// video stream creates the appropriate send/recv streams in the underlying
160// media engine on both sides.
161TEST_F(PeerConnectionMediaTest, AudioVideoOfferAnswerCreateSendRecvStreams) {
162 const std::string kCallerAudioId = "caller_a";
163 const std::string kCallerVideoId = "caller_v";
164 const std::string kCalleeAudioId = "callee_a";
165 const std::string kCalleeVideoId = "callee_v";
166
167 auto caller = CreatePeerConnection();
168 caller->AddAudioTrack(kCallerAudioId);
169 caller->AddVideoTrack(kCallerVideoId);
170
171 auto callee = CreatePeerConnection();
172 callee->AddAudioTrack(kCalleeAudioId);
173 callee->AddVideoTrack(kCalleeVideoId);
174
175 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
176 ASSERT_TRUE(
177 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
178
179 auto* caller_voice = caller->media_engine()->GetVoiceChannel(0);
180 EXPECT_THAT(GetIds(caller_voice->recv_streams()),
181 ElementsAre(kCalleeAudioId));
182 EXPECT_THAT(GetIds(caller_voice->send_streams()),
183 ElementsAre(kCallerAudioId));
184
185 auto* caller_video = caller->media_engine()->GetVideoChannel(0);
186 EXPECT_THAT(GetIds(caller_video->recv_streams()),
187 ElementsAre(kCalleeVideoId));
188 EXPECT_THAT(GetIds(caller_video->send_streams()),
189 ElementsAre(kCallerVideoId));
190
191 auto* callee_voice = callee->media_engine()->GetVoiceChannel(0);
192 EXPECT_THAT(GetIds(callee_voice->recv_streams()),
193 ElementsAre(kCallerAudioId));
194 EXPECT_THAT(GetIds(callee_voice->send_streams()),
195 ElementsAre(kCalleeAudioId));
196
197 auto* callee_video = callee->media_engine()->GetVideoChannel(0);
198 EXPECT_THAT(GetIds(callee_video->recv_streams()),
199 ElementsAre(kCallerVideoId));
200 EXPECT_THAT(GetIds(callee_video->send_streams()),
201 ElementsAre(kCalleeVideoId));
202}
203
204// Test that removing streams from a subsequent offer causes the receive streams
205// on the callee to be removed.
206TEST_F(PeerConnectionMediaTest, EmptyRemoteOfferRemovesRecvStreams) {
207 auto caller = CreatePeerConnection();
208 auto caller_audio_track = caller->AddAudioTrack("a");
209 auto caller_video_track = caller->AddVideoTrack("v");
210 auto callee = CreatePeerConnectionWithAudioVideo();
211
212 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
213 ASSERT_TRUE(
214 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
215
216 // Remove both tracks from caller.
217 caller->pc()->RemoveTrack(caller_audio_track);
218 caller->pc()->RemoveTrack(caller_video_track);
219
220 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
221 ASSERT_TRUE(
222 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
223
224 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
225 EXPECT_EQ(1u, callee_voice->send_streams().size());
226 EXPECT_EQ(0u, callee_voice->recv_streams().size());
227
228 auto callee_video = callee->media_engine()->GetVideoChannel(0);
229 EXPECT_EQ(1u, callee_video->send_streams().size());
230 EXPECT_EQ(0u, callee_video->recv_streams().size());
231}
232
233// Test that removing streams from a subsequent answer causes the send streams
234// on the callee to be removed when applied locally.
235TEST_F(PeerConnectionMediaTest, EmptyLocalAnswerRemovesSendStreams) {
236 auto caller = CreatePeerConnectionWithAudioVideo();
237 auto callee = CreatePeerConnection();
238 auto callee_audio_track = callee->AddAudioTrack("a");
239 auto callee_video_track = callee->AddVideoTrack("v");
240
241 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
242 ASSERT_TRUE(
243 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
244
245 // Remove both tracks from callee.
246 callee->pc()->RemoveTrack(callee_audio_track);
247 callee->pc()->RemoveTrack(callee_video_track);
248
249 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
250 ASSERT_TRUE(
251 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
252
253 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
254 EXPECT_EQ(0u, callee_voice->send_streams().size());
255 EXPECT_EQ(1u, callee_voice->recv_streams().size());
256
257 auto callee_video = callee->media_engine()->GetVideoChannel(0);
258 EXPECT_EQ(0u, callee_video->send_streams().size());
259 EXPECT_EQ(1u, callee_video->recv_streams().size());
260}
261
262// Test that a new stream in a subsequent offer causes a new receive stream to
263// be created on the callee.
264TEST_F(PeerConnectionMediaTest, NewStreamInRemoteOfferAddsRecvStreams) {
265 auto caller = CreatePeerConnectionWithAudioVideo();
266 auto callee = CreatePeerConnection();
267
268 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
269 ASSERT_TRUE(
270 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
271
272 // Add second set of tracks to the caller.
273 caller->AddAudioTrack("a2");
274 caller->AddVideoTrack("v2");
275
276 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
277 ASSERT_TRUE(
278 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
279
280 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
281 EXPECT_EQ(2u, callee_voice->recv_streams().size());
282 auto callee_video = callee->media_engine()->GetVideoChannel(0);
283 EXPECT_EQ(2u, callee_video->recv_streams().size());
284}
285
286// Test that a new stream in a subsequent answer causes a new send stream to be
287// created on the callee when added locally.
288TEST_F(PeerConnectionMediaTest, NewStreamInLocalAnswerAddsSendStreams) {
289 auto caller = CreatePeerConnection();
290 auto callee = CreatePeerConnectionWithAudioVideo();
291
292 RTCOfferAnswerOptions options;
293 options.offer_to_receive_audio =
294 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
295 options.offer_to_receive_video =
296 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
297
298 ASSERT_TRUE(
299 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
300 ASSERT_TRUE(
301 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
302
303 // Add second set of tracks to the callee.
304 callee->AddAudioTrack("a2");
305 callee->AddVideoTrack("v2");
306
307 ASSERT_TRUE(
308 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
309 ASSERT_TRUE(
310 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
311
312 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
313 EXPECT_EQ(2u, callee_voice->send_streams().size());
314 auto callee_video = callee->media_engine()->GetVideoChannel(0);
315 EXPECT_EQ(2u, callee_video->send_streams().size());
316}
317
318// A PeerConnection with no local streams and no explicit answer constraints
319// should not reject any offered media sections.
320TEST_F(PeerConnectionMediaTest,
321 CreateAnswerWithNoStreamsAndDefaultOptionsDoesNotReject) {
322 auto caller = CreatePeerConnectionWithAudioVideo();
323 auto callee = CreatePeerConnection();
324 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
325 auto answer = callee->CreateAnswer();
326
327 const auto* audio_content =
328 cricket::GetFirstAudioContent(answer->description());
329 ASSERT_TRUE(audio_content);
330 EXPECT_FALSE(audio_content->rejected);
331
332 const auto* video_content =
333 cricket::GetFirstVideoContent(answer->description());
334 ASSERT_TRUE(video_content);
335 EXPECT_FALSE(video_content->rejected);
336}
337
338class PeerConnectionMediaOfferDirectionTest
339 : public PeerConnectionMediaTest,
340 public ::testing::WithParamInterface<
341 std::tuple<bool, int, cricket::MediaContentDirection>> {
342 protected:
343 PeerConnectionMediaOfferDirectionTest() {
344 send_media_ = std::get<0>(GetParam());
345 offer_to_receive_ = std::get<1>(GetParam());
346 expected_direction_ = std::get<2>(GetParam());
347 }
348
349 bool send_media_;
350 int offer_to_receive_;
351 cricket::MediaContentDirection expected_direction_;
352};
353
354// Tests that the correct direction is set on the media description according
355// to the presence of a local media track and the offer_to_receive setting.
356TEST_P(PeerConnectionMediaOfferDirectionTest, VerifyDirection) {
357 auto caller = CreatePeerConnection();
358 if (send_media_) {
359 caller->AddAudioTrack("a");
360 }
361
362 RTCOfferAnswerOptions options;
363 options.offer_to_receive_audio = offer_to_receive_;
364 auto offer = caller->CreateOffer(options);
365
366 auto* media_content = GetMediaContent(offer.get(), cricket::CN_AUDIO);
367 if (expected_direction_ == cricket::MD_INACTIVE) {
368 EXPECT_FALSE(media_content);
369 } else {
370 EXPECT_EQ(expected_direction_, media_content->direction());
371 }
372}
373
374// Note that in these tests, MD_INACTIVE indicates that no media section is
375// included in the offer, not that the media direction is inactive.
376INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
377 PeerConnectionMediaOfferDirectionTest,
378 Values(std::make_tuple(false, -1, cricket::MD_INACTIVE),
379 std::make_tuple(false, 0, cricket::MD_INACTIVE),
380 std::make_tuple(false, 1, cricket::MD_RECVONLY),
381 std::make_tuple(true, -1, cricket::MD_SENDRECV),
382 std::make_tuple(true, 0, cricket::MD_SENDONLY),
383 std::make_tuple(true, 1, cricket::MD_SENDRECV)));
384
385class PeerConnectionMediaAnswerDirectionTest
386 : public PeerConnectionMediaTest,
387 public ::testing::WithParamInterface<
388 std::tuple<cricket::MediaContentDirection, bool, int>> {
389 protected:
390 PeerConnectionMediaAnswerDirectionTest() {
391 offer_direction_ = std::get<0>(GetParam());
392 send_media_ = std::get<1>(GetParam());
393 offer_to_receive_ = std::get<2>(GetParam());
394 }
395
396 cricket::MediaContentDirection offer_direction_;
397 bool send_media_;
398 int offer_to_receive_;
399};
400
401// Tests that the direction in an answer is correct according to direction sent
402// in the offer, the presence of a local media track on the receive side and the
403// offer_to_receive setting.
404TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
405 auto caller = CreatePeerConnection();
406 caller->AddAudioTrack("a");
407
408 // Create the offer with an audio section and set its direction.
409 auto offer = caller->CreateOffer();
410 cricket::GetFirstAudioContentDescription(offer->description())
411 ->set_direction(offer_direction_);
412
413 auto callee = CreatePeerConnection();
414 if (send_media_) {
415 callee->AddAudioTrack("a");
416 }
417 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
418
419 // Create the answer according to the test parameters.
420 RTCOfferAnswerOptions options;
421 options.offer_to_receive_audio = offer_to_receive_;
422 auto answer = callee->CreateAnswer(options);
423
424 // The expected direction in the answer is the intersection of each side's
425 // capability to send/recv media.
426 // For the offerer, the direction is given in the offer (offer_direction_).
427 // For the answerer, the direction has two components:
428 // 1. Send if the answerer has a local track to send.
429 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
430 // if it has been left as default.
431 auto offer_direction =
432 cricket::RtpTransceiverDirection::FromMediaContentDirection(
433 offer_direction_);
434
435 // The negotiated components determine the direction set in the answer.
436 bool negotiate_send = (send_media_ && offer_direction.recv);
437 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_direction.send);
438
439 auto expected_direction =
440 cricket::RtpTransceiverDirection(negotiate_send, negotiate_recv)
441 .ToMediaContentDirection();
442 EXPECT_EQ(expected_direction,
443 GetMediaContentDirection(answer.get(), cricket::CN_AUDIO));
444}
445
446// Tests that the media section is rejected if and only if the callee has no
447// local media track and has set offer_to_receive to 0, no matter which
448// direction the caller indicated in the offer.
449TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyRejected) {
450 auto caller = CreatePeerConnection();
451 caller->AddAudioTrack("a");
452
453 // Create the offer with an audio section and set its direction.
454 auto offer = caller->CreateOffer();
455 cricket::GetFirstAudioContentDescription(offer->description())
456 ->set_direction(offer_direction_);
457
458 auto callee = CreatePeerConnection();
459 if (send_media_) {
460 callee->AddAudioTrack("a");
461 }
462 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
463
464 // Create the answer according to the test parameters.
465 RTCOfferAnswerOptions options;
466 options.offer_to_receive_audio = offer_to_receive_;
467 auto answer = callee->CreateAnswer(options);
468
469 // The media section is rejected if and only if offer_to_receive is explicitly
470 // set to 0 and there is no media to send.
471 auto* audio_content = cricket::GetFirstAudioContent(answer->description());
472 ASSERT_TRUE(audio_content);
473 EXPECT_EQ((offer_to_receive_ == 0 && !send_media_), audio_content->rejected);
474}
475
476INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
477 PeerConnectionMediaAnswerDirectionTest,
478 Combine(Values(cricket::MD_INACTIVE,
479 cricket::MD_SENDONLY,
480 cricket::MD_RECVONLY,
481 cricket::MD_SENDRECV),
482 Bool(),
483 Values(-1, 0, 1)));
484
485TEST_F(PeerConnectionMediaTest, OfferHasDifferentDirectionForAudioVideo) {
486 auto caller = CreatePeerConnection();
487 caller->AddVideoTrack("v");
488
489 RTCOfferAnswerOptions options;
490 options.offer_to_receive_audio = 1;
491 options.offer_to_receive_video = 0;
492 auto offer = caller->CreateOffer(options);
493
494 EXPECT_EQ(cricket::MD_RECVONLY,
495 GetMediaContentDirection(offer.get(), cricket::CN_AUDIO));
496 EXPECT_EQ(cricket::MD_SENDONLY,
497 GetMediaContentDirection(offer.get(), cricket::CN_VIDEO));
498}
499
500TEST_F(PeerConnectionMediaTest, AnswerHasDifferentDirectionsForAudioVideo) {
501 auto caller = CreatePeerConnectionWithAudioVideo();
502 auto callee = CreatePeerConnection();
503 callee->AddVideoTrack("v");
504
505 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
506
507 RTCOfferAnswerOptions options;
508 options.offer_to_receive_audio = 1;
509 options.offer_to_receive_video = 0;
510 auto answer = callee->CreateAnswer(options);
511
512 EXPECT_EQ(cricket::MD_RECVONLY,
513 GetMediaContentDirection(answer.get(), cricket::CN_AUDIO));
514 EXPECT_EQ(cricket::MD_SENDONLY,
515 GetMediaContentDirection(answer.get(), cricket::CN_VIDEO));
516}
517
518void AddComfortNoiseCodecsToSend(cricket::FakeMediaEngine* media_engine) {
519 const cricket::AudioCodec kComfortNoiseCodec8k(102, "CN", 8000, 0, 1);
520 const cricket::AudioCodec kComfortNoiseCodec16k(103, "CN", 16000, 0, 1);
521
522 auto codecs = media_engine->audio_send_codecs();
523 codecs.push_back(kComfortNoiseCodec8k);
524 codecs.push_back(kComfortNoiseCodec16k);
525 media_engine->SetAudioCodecs(codecs);
526}
527
528bool HasAnyComfortNoiseCodecs(const cricket::SessionDescription* desc) {
529 const auto* audio_desc = cricket::GetFirstAudioContentDescription(desc);
530 for (const auto& codec : audio_desc->codecs()) {
531 if (codec.name == "CN") {
532 return true;
533 }
534 }
535 return false;
536}
537
538TEST_F(PeerConnectionMediaTest,
539 CreateOfferWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
540 auto caller = CreatePeerConnectionWithAudioVideo();
541 AddComfortNoiseCodecsToSend(caller->media_engine());
542
543 RTCOfferAnswerOptions options;
544 options.voice_activity_detection = false;
545 auto offer = caller->CreateOffer(options);
546
547 EXPECT_FALSE(HasAnyComfortNoiseCodecs(offer->description()));
548}
549
550TEST_F(PeerConnectionMediaTest,
551 CreateAnswerWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
552 auto caller = CreatePeerConnectionWithAudioVideo();
553 AddComfortNoiseCodecsToSend(caller->media_engine());
554 auto callee = CreatePeerConnectionWithAudioVideo();
555 AddComfortNoiseCodecsToSend(callee->media_engine());
556
557 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
558
559 RTCOfferAnswerOptions options;
560 options.voice_activity_detection = false;
561 auto answer = callee->CreateAnswer(options);
562
563 EXPECT_FALSE(HasAnyComfortNoiseCodecs(answer->description()));
564}
565
566// The following test group verifies that we reject answers with invalid media
567// sections as per RFC 3264.
568
569class PeerConnectionMediaInvalidMediaTest
570 : public PeerConnectionMediaTest,
571 public ::testing::WithParamInterface<
572 std::tuple<std::string,
573 std::function<void(cricket::SessionDescription*)>,
574 std::string>> {
575 protected:
576 PeerConnectionMediaInvalidMediaTest() {
577 mutator_ = std::get<1>(GetParam());
578 expected_error_ = std::get<2>(GetParam());
579 }
580
581 std::function<void(cricket::SessionDescription*)> mutator_;
582 std::string expected_error_;
583};
584
585TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetRemoteAnswer) {
586 auto caller = CreatePeerConnectionWithAudioVideo();
587 auto callee = CreatePeerConnectionWithAudioVideo();
588
589 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
590
591 auto answer = callee->CreateAnswer();
592 mutator_(answer->description());
593
594 std::string error;
595 ASSERT_FALSE(caller->SetRemoteDescription(std::move(answer), &error));
596 EXPECT_EQ("Failed to set remote answer sdp: " + expected_error_, error);
597}
598
599TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetLocalAnswer) {
600 auto caller = CreatePeerConnectionWithAudioVideo();
601 auto callee = CreatePeerConnectionWithAudioVideo();
602
603 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
604
605 auto answer = callee->CreateAnswer();
606 mutator_(answer->description());
607
608 std::string error;
609 ASSERT_FALSE(callee->SetLocalDescription(std::move(answer), &error));
610 EXPECT_EQ("Failed to set local answer sdp: " + expected_error_, error);
611}
612
613void RemoveVideoContent(cricket::SessionDescription* desc) {
614 auto content_name = cricket::GetFirstVideoContent(desc)->name;
615 desc->RemoveContentByName(content_name);
616 desc->RemoveTransportInfoByName(content_name);
617}
618
619void RenameVideoContent(cricket::SessionDescription* desc) {
620 auto* video_content = cricket::GetFirstVideoContent(desc);
621 auto* transport_info = desc->GetTransportInfoByName(video_content->name);
622 video_content->name = "video_renamed";
623 transport_info->content_name = video_content->name;
624}
625
626void ReverseMediaContent(cricket::SessionDescription* desc) {
627 std::reverse(desc->contents().begin(), desc->contents().end());
628 std::reverse(desc->transport_infos().begin(), desc->transport_infos().end());
629}
630
631void ChangeMediaTypeAudioToVideo(cricket::SessionDescription* desc) {
632 desc->RemoveContentByName(cricket::CN_AUDIO);
633 auto* video_content = desc->GetContentByName(cricket::CN_VIDEO);
634 desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
635 video_content->description->Copy());
636}
637
638constexpr char kMLinesOutOfOrder[] =
639 "The order of m-lines in answer doesn't match order in offer. Rejecting "
640 "answer.";
641
642INSTANTIATE_TEST_CASE_P(
643 PeerConnectionMediaTest,
644 PeerConnectionMediaInvalidMediaTest,
645 Values(
646 std::make_tuple("remove video", RemoveVideoContent, kMLinesOutOfOrder),
647 std::make_tuple("rename video", RenameVideoContent, kMLinesOutOfOrder),
648 std::make_tuple("reverse media sections",
649 ReverseMediaContent,
650 kMLinesOutOfOrder),
651 std::make_tuple("change audio type to video type",
652 ChangeMediaTypeAudioToVideo,
653 kMLinesOutOfOrder)));
654
655// Test that the correct media engine send/recv streams are created when doing
656// a series of offer/answers where audio/video are both sent, then audio is
657// rejected, then both audio/video sent again.
658TEST_F(PeerConnectionMediaTest, TestAVOfferWithAudioOnlyAnswer) {
659 RTCOfferAnswerOptions options_reject_video;
660 options_reject_video.offer_to_receive_audio =
661 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
662 options_reject_video.offer_to_receive_video = 0;
663
664 auto caller = CreatePeerConnection();
665 caller->AddAudioTrack("a");
666 caller->AddVideoTrack("v");
667 auto callee = CreatePeerConnection();
668
669 // Caller initially offers to send/recv audio and video.
670 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
671 // Callee accepts the audio as recv only but rejects the video.
672 ASSERT_TRUE(caller->SetRemoteDescription(
673 callee->CreateAnswerAndSetAsLocal(options_reject_video)));
674
675 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
676 ASSERT_TRUE(caller_voice);
677 EXPECT_EQ(0u, caller_voice->recv_streams().size());
678 EXPECT_EQ(1u, caller_voice->send_streams().size());
679 auto caller_video = caller->media_engine()->GetVideoChannel(0);
680 EXPECT_FALSE(caller_video);
681
682 // Callee adds its own audio/video stream and offers to receive audio/video
683 // too.
684 callee->AddAudioTrack("a");
685 auto callee_video_track = callee->AddVideoTrack("v");
686 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
687 ASSERT_TRUE(
688 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
689
690 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
691 ASSERT_TRUE(callee_voice);
692 EXPECT_EQ(1u, callee_voice->recv_streams().size());
693 EXPECT_EQ(1u, callee_voice->send_streams().size());
694 auto callee_video = callee->media_engine()->GetVideoChannel(0);
695 ASSERT_TRUE(callee_video);
696 EXPECT_EQ(1u, callee_video->recv_streams().size());
697 EXPECT_EQ(1u, callee_video->send_streams().size());
698
699 // Callee removes video but keeps audio and rejects the video once again.
700 callee->pc()->RemoveTrack(callee_video_track);
701 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
702 ASSERT_TRUE(
703 callee->SetLocalDescription(callee->CreateAnswer(options_reject_video)));
704
705 callee_voice = callee->media_engine()->GetVoiceChannel(0);
706 ASSERT_TRUE(callee_voice);
707 EXPECT_EQ(1u, callee_voice->recv_streams().size());
708 EXPECT_EQ(1u, callee_voice->send_streams().size());
709 callee_video = callee->media_engine()->GetVideoChannel(0);
710 EXPECT_FALSE(callee_video);
711}
712
713// Test that the correct media engine send/recv streams are created when doing
714// a series of offer/answers where audio/video are both sent, then video is
715// rejected, then both audio/video sent again.
716TEST_F(PeerConnectionMediaTest, TestAVOfferWithVideoOnlyAnswer) {
717 // Disable the bundling here. If the media is bundled on audio
718 // transport, then we can't reject the audio because switching the bundled
719 // transport is not currently supported.
720 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6704)
721 RTCOfferAnswerOptions options_no_bundle;
722 options_no_bundle.use_rtp_mux = false;
723 RTCOfferAnswerOptions options_reject_audio = options_no_bundle;
724 options_reject_audio.offer_to_receive_audio = 0;
725 options_reject_audio.offer_to_receive_video =
726 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia;
727
728 auto caller = CreatePeerConnection();
729 caller->AddAudioTrack("a");
730 caller->AddVideoTrack("v");
731 auto callee = CreatePeerConnection();
732
733 // Caller initially offers to send/recv audio and video.
734 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
735 // Callee accepts the video as recv only but rejects the audio.
736 ASSERT_TRUE(caller->SetRemoteDescription(
737 callee->CreateAnswerAndSetAsLocal(options_reject_audio)));
738
739 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
740 EXPECT_FALSE(caller_voice);
741 auto caller_video = caller->media_engine()->GetVideoChannel(0);
742 ASSERT_TRUE(caller_video);
743 EXPECT_EQ(0u, caller_video->recv_streams().size());
744 EXPECT_EQ(1u, caller_video->send_streams().size());
745
746 // Callee adds its own audio/video stream and offers to receive audio/video
747 // too.
748 auto callee_audio_track = callee->AddAudioTrack("a");
749 callee->AddVideoTrack("v");
750 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
751 ASSERT_TRUE(caller->SetRemoteDescription(
752 callee->CreateAnswerAndSetAsLocal(options_no_bundle)));
753
754 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
755 ASSERT_TRUE(callee_voice);
756 EXPECT_EQ(1u, callee_voice->recv_streams().size());
757 EXPECT_EQ(1u, callee_voice->send_streams().size());
758 auto callee_video = callee->media_engine()->GetVideoChannel(0);
759 ASSERT_TRUE(callee_video);
760 EXPECT_EQ(1u, callee_video->recv_streams().size());
761 EXPECT_EQ(1u, callee_video->send_streams().size());
762
763 // Callee removes audio but keeps video and rejects the audio once again.
764 callee->pc()->RemoveTrack(callee_audio_track);
765 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
766 ASSERT_TRUE(
767 callee->SetLocalDescription(callee->CreateAnswer(options_reject_audio)));
768
769 callee_voice = callee->media_engine()->GetVoiceChannel(0);
770 EXPECT_FALSE(callee_voice);
771 callee_video = callee->media_engine()->GetVideoChannel(0);
772 ASSERT_TRUE(callee_video);
773 EXPECT_EQ(1u, callee_video->recv_streams().size());
774 EXPECT_EQ(1u, callee_video->send_streams().size());
775}
776
777// Tests that if the underlying video encoder fails to be initialized (signaled
778// by failing to set send codecs), the PeerConnection signals the error to the
779// client.
780TEST_F(PeerConnectionMediaTest, MediaEngineErrorPropagatedToClients) {
781 auto caller = CreatePeerConnectionWithAudioVideo();
782 auto callee = CreatePeerConnectionWithAudioVideo();
783
784 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
785
786 auto video_channel = caller->media_engine()->GetVideoChannel(0);
787 video_channel->set_fail_set_send_codecs(true);
788
789 std::string error;
790 ASSERT_FALSE(caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(),
791 &error));
792 EXPECT_EQ(
793 "Failed to set remote answer sdp: Session error code: ERROR_CONTENT. "
794 "Session error description: Failed to set remote video description send "
795 "parameters..",
796 error);
797}
798
799// Tests that if the underlying video encoder fails once then subsequent
800// attempts at setting the local/remote description will also fail, even if
801// SetSendCodecs no longer fails.
802TEST_F(PeerConnectionMediaTest,
803 FailToApplyDescriptionIfVideoEncoderHasEverFailed) {
804 auto caller = CreatePeerConnectionWithAudioVideo();
805 auto callee = CreatePeerConnectionWithAudioVideo();
806
807 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
808
809 auto video_channel = caller->media_engine()->GetVideoChannel(0);
810 video_channel->set_fail_set_send_codecs(true);
811
812 EXPECT_FALSE(
813 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
814
815 video_channel->set_fail_set_send_codecs(false);
816
817 EXPECT_FALSE(caller->SetRemoteDescription(callee->CreateAnswer()));
818 EXPECT_FALSE(caller->SetLocalDescription(caller->CreateOffer()));
819}
820
821void RenameContent(cricket::SessionDescription* desc,
822 const std::string& old_name,
823 const std::string& new_name) {
824 auto* content = desc->GetContentByName(old_name);
825 RTC_DCHECK(content);
826 content->name = new_name;
827 auto* transport = desc->GetTransportInfoByName(old_name);
828 RTC_DCHECK(transport);
829 transport->content_name = new_name;
830}
831
832// Tests that an answer responds with the same MIDs as the offer.
833TEST_F(PeerConnectionMediaTest, AnswerHasSameMidsAsOffer) {
834 const std::string kAudioMid = "not default1";
835 const std::string kVideoMid = "not default2";
836
837 auto caller = CreatePeerConnectionWithAudioVideo();
838 auto callee = CreatePeerConnectionWithAudioVideo();
839
840 auto offer = caller->CreateOffer();
841 RenameContent(offer->description(), cricket::CN_AUDIO, kAudioMid);
842 RenameContent(offer->description(), cricket::CN_VIDEO, kVideoMid);
843 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
844
845 auto answer = callee->CreateAnswer();
846 EXPECT_EQ(kAudioMid,
847 cricket::GetFirstAudioContent(answer->description())->name);
848 EXPECT_EQ(kVideoMid,
849 cricket::GetFirstVideoContent(answer->description())->name);
850}
851
852// Test that if the callee creates a re-offer, the MIDs are the same as the
853// original offer.
854TEST_F(PeerConnectionMediaTest, ReOfferHasSameMidsAsFirstOffer) {
855 const std::string kAudioMid = "not default1";
856 const std::string kVideoMid = "not default2";
857
858 auto caller = CreatePeerConnectionWithAudioVideo();
859 auto callee = CreatePeerConnectionWithAudioVideo();
860
861 auto offer = caller->CreateOffer();
862 RenameContent(offer->description(), cricket::CN_AUDIO, kAudioMid);
863 RenameContent(offer->description(), cricket::CN_VIDEO, kVideoMid);
864 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
865 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
866
867 auto reoffer = callee->CreateOffer();
868 EXPECT_EQ(kAudioMid,
869 cricket::GetFirstAudioContent(reoffer->description())->name);
870 EXPECT_EQ(kVideoMid,
871 cricket::GetFirstVideoContent(reoffer->description())->name);
872}
873
874TEST_F(PeerConnectionMediaTest,
875 CombinedAudioVideoBweConfigPropagatedToMediaEngine) {
876 RTCConfiguration config;
877 config.combined_audio_video_bwe.emplace(true);
878 auto caller = CreatePeerConnectionWithAudioVideo(config);
879
880 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
881
882 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
883 ASSERT_TRUE(caller_voice);
884 const cricket::AudioOptions& audio_options = caller_voice->options();
885 EXPECT_EQ(config.combined_audio_video_bwe,
886 audio_options.combined_audio_video_bwe);
887}
888
889} // namespace webrtc