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