blob: 1e8b2962d656b2ca4e0d0302c8a56c447d32da3f [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));
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<
Steve Anton4e70a722017-11-28 14:57:10 -0800342 std::tuple<bool, int, RtpTransceiverDirection>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700343 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_;
Steve Anton4e70a722017-11-28 14:57:10 -0800352 RtpTransceiverDirection expected_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700353};
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);
Steve Anton4e70a722017-11-28 14:57:10 -0800368 if (expected_direction_ == RtpTransceiverDirection::kInactive) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700369 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.
Steve Anton4e70a722017-11-28 14:57:10 -0800377INSTANTIATE_TEST_CASE_P(
378 PeerConnectionMediaTest,
379 PeerConnectionMediaOfferDirectionTest,
380 Values(std::make_tuple(false, -1, RtpTransceiverDirection::kInactive),
381 std::make_tuple(false, 0, RtpTransceiverDirection::kInactive),
382 std::make_tuple(false, 1, RtpTransceiverDirection::kRecvOnly),
383 std::make_tuple(true, -1, RtpTransceiverDirection::kSendRecv),
384 std::make_tuple(true, 0, RtpTransceiverDirection::kSendOnly),
385 std::make_tuple(true, 1, RtpTransceiverDirection::kSendRecv)));
Steve Anton8d3444d2017-10-20 15:30:51 -0700386
387class PeerConnectionMediaAnswerDirectionTest
388 : public PeerConnectionMediaTest,
389 public ::testing::WithParamInterface<
Steve Anton4e70a722017-11-28 14:57:10 -0800390 std::tuple<RtpTransceiverDirection, bool, int>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700391 protected:
392 PeerConnectionMediaAnswerDirectionTest() {
393 offer_direction_ = std::get<0>(GetParam());
394 send_media_ = std::get<1>(GetParam());
395 offer_to_receive_ = std::get<2>(GetParam());
396 }
397
Steve Anton4e70a722017-11-28 14:57:10 -0800398 RtpTransceiverDirection offer_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700399 bool send_media_;
400 int offer_to_receive_;
401};
402
403// Tests that the direction in an answer is correct according to direction sent
404// in the offer, the presence of a local media track on the receive side and the
405// offer_to_receive setting.
406TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
407 auto caller = CreatePeerConnection();
408 caller->AddAudioTrack("a");
409
410 // Create the offer with an audio section and set its direction.
411 auto offer = caller->CreateOffer();
412 cricket::GetFirstAudioContentDescription(offer->description())
413 ->set_direction(offer_direction_);
414
415 auto callee = CreatePeerConnection();
416 if (send_media_) {
417 callee->AddAudioTrack("a");
418 }
419 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
420
421 // Create the answer according to the test parameters.
422 RTCOfferAnswerOptions options;
423 options.offer_to_receive_audio = offer_to_receive_;
424 auto answer = callee->CreateAnswer(options);
425
426 // The expected direction in the answer is the intersection of each side's
427 // capability to send/recv media.
428 // For the offerer, the direction is given in the offer (offer_direction_).
429 // For the answerer, the direction has two components:
430 // 1. Send if the answerer has a local track to send.
431 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
432 // if it has been left as default.
Steve Anton4e70a722017-11-28 14:57:10 -0800433 bool offer_send = RtpTransceiverDirectionHasSend(offer_direction_);
434 bool offer_recv = RtpTransceiverDirectionHasRecv(offer_direction_);
Steve Anton8d3444d2017-10-20 15:30:51 -0700435
436 // The negotiated components determine the direction set in the answer.
Steve Anton1d03a752017-11-27 14:30:09 -0800437 bool negotiate_send = (send_media_ && offer_recv);
438 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_send);
Steve Anton8d3444d2017-10-20 15:30:51 -0700439
440 auto expected_direction =
Steve Anton4e70a722017-11-28 14:57:10 -0800441 RtpTransceiverDirectionFromSendRecv(negotiate_send, negotiate_recv);
Steve Anton8d3444d2017-10-20 15:30:51 -0700442 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,
Steve Anton4e70a722017-11-28 14:57:10 -0800478 Combine(Values(RtpTransceiverDirection::kInactive,
479 RtpTransceiverDirection::kSendOnly,
480 RtpTransceiverDirection::kRecvOnly,
481 RtpTransceiverDirection::kSendRecv),
Steve Anton8d3444d2017-10-20 15:30:51 -0700482 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
Steve Anton4e70a722017-11-28 14:57:10 -0800494 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Anton8d3444d2017-10-20 15:30:51 -0700495 GetMediaContentDirection(offer.get(), cricket::CN_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800496 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Anton8d3444d2017-10-20 15:30:51 -0700497 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
Steve Anton4e70a722017-11-28 14:57:10 -0800512 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Anton8d3444d2017-10-20 15:30:51 -0700513 GetMediaContentDirection(answer.get(), cricket::CN_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800514 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Anton8d3444d2017-10-20 15:30:51 -0700515 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