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