blob: f59d6aa1bc529a359e0ae82c0bcce1c44a32d941 [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
Niels Möller8366e172018-02-14 12:20:13 +010017#include "api/call/callfactoryinterface.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070018#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
Steve Antonad7bffc2018-01-22 10:21:56 -080057class PeerConnectionMediaBaseTest : public ::testing::Test {
Steve Anton8d3444d2017-10-20 15:30:51 -070058 protected:
59 typedef std::unique_ptr<PeerConnectionWrapperForMediaTest> WrapperPtr;
60
Steve Antonad7bffc2018-01-22 10:21:56 -080061 explicit PeerConnectionMediaBaseTest(SdpSemantics sdp_semantics)
62 : vss_(new rtc::VirtualSocketServer()),
63 main_(vss_.get()),
64 sdp_semantics_(sdp_semantics) {
Steve Anton8d3444d2017-10-20 15:30:51 -070065#ifdef WEBRTC_ANDROID
66 InitializeAndroidObjects();
67#endif
68 }
69
70 WrapperPtr CreatePeerConnection() {
71 return CreatePeerConnection(RTCConfiguration());
72 }
73
74 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
75 auto media_engine = rtc::MakeUnique<FakeMediaEngine>();
76 auto* media_engine_ptr = media_engine.get();
77 auto pc_factory = CreateModularPeerConnectionFactory(
78 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
79 std::move(media_engine), CreateCallFactory(),
80 CreateRtcEventLogFactory());
81
82 auto fake_port_allocator = rtc::MakeUnique<cricket::FakePortAllocator>(
83 rtc::Thread::Current(), nullptr);
84 auto observer = rtc::MakeUnique<MockPeerConnectionObserver>();
Steve Antonad7bffc2018-01-22 10:21:56 -080085 auto modified_config = config;
86 modified_config.sdp_semantics = sdp_semantics_;
87 auto pc = pc_factory->CreatePeerConnection(modified_config,
88 std::move(fake_port_allocator),
89 nullptr, observer.get());
Steve Anton8d3444d2017-10-20 15:30:51 -070090 if (!pc) {
91 return nullptr;
92 }
93
94 auto wrapper = rtc::MakeUnique<PeerConnectionWrapperForMediaTest>(
95 pc_factory, pc, std::move(observer));
96 wrapper->set_media_engine(media_engine_ptr);
97 return wrapper;
98 }
99
100 // Accepts the same arguments as CreatePeerConnection and adds default audio
101 // and video tracks.
102 template <typename... Args>
103 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
104 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
105 if (!wrapper) {
106 return nullptr;
107 }
108 wrapper->AddAudioTrack("a");
109 wrapper->AddVideoTrack("v");
110 return wrapper;
111 }
112
Steve Anton4e70a722017-11-28 14:57:10 -0800113 RtpTransceiverDirection GetMediaContentDirection(
Steve Anton8d3444d2017-10-20 15:30:51 -0700114 const SessionDescriptionInterface* sdesc,
Steve Antonad7bffc2018-01-22 10:21:56 -0800115 cricket::MediaType media_type) {
116 auto* content =
117 cricket::GetFirstMediaContent(sdesc->description(), media_type);
118 RTC_DCHECK(content);
119 return content->media_description()->direction();
120 }
121
122 bool IsUnifiedPlan() const {
123 return sdp_semantics_ == SdpSemantics::kUnifiedPlan;
Steve Anton8d3444d2017-10-20 15:30:51 -0700124 }
125
126 std::unique_ptr<rtc::VirtualSocketServer> vss_;
127 rtc::AutoSocketServerThread main_;
Steve Antonad7bffc2018-01-22 10:21:56 -0800128 const SdpSemantics sdp_semantics_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700129};
130
Steve Antonad7bffc2018-01-22 10:21:56 -0800131class PeerConnectionMediaTest
132 : public PeerConnectionMediaBaseTest,
133 public ::testing::WithParamInterface<SdpSemantics> {
134 protected:
135 PeerConnectionMediaTest() : PeerConnectionMediaBaseTest(GetParam()) {}
136};
137
138class PeerConnectionMediaTestUnifiedPlan : public PeerConnectionMediaBaseTest {
139 protected:
140 PeerConnectionMediaTestUnifiedPlan()
141 : PeerConnectionMediaBaseTest(SdpSemantics::kUnifiedPlan) {}
142};
143
144class PeerConnectionMediaTestPlanB : public PeerConnectionMediaBaseTest {
145 protected:
146 PeerConnectionMediaTestPlanB()
147 : PeerConnectionMediaBaseTest(SdpSemantics::kPlanB) {}
148};
149
150TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700151 FailToSetRemoteDescriptionIfCreateMediaChannelFails) {
152 auto caller = CreatePeerConnectionWithAudioVideo();
153 auto callee = CreatePeerConnectionWithAudioVideo();
154 callee->media_engine()->set_fail_create_channel(true);
155
156 std::string error;
157 ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
Steve Antonad7bffc2018-01-22 10:21:56 -0800158 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
159 "Failed to set remote offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 15:30:51 -0700160}
161
Steve Antonad7bffc2018-01-22 10:21:56 -0800162TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700163 FailToSetLocalDescriptionIfCreateMediaChannelFails) {
164 auto caller = CreatePeerConnectionWithAudioVideo();
165 caller->media_engine()->set_fail_create_channel(true);
166
167 std::string error;
168 ASSERT_FALSE(caller->SetLocalDescription(caller->CreateOffer(), &error));
Steve Antonad7bffc2018-01-22 10:21:56 -0800169 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
170 "Failed to set local offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 15:30:51 -0700171}
172
173std::vector<std::string> GetIds(
174 const std::vector<cricket::StreamParams>& streams) {
175 std::vector<std::string> ids;
176 for (const auto& stream : streams) {
177 ids.push_back(stream.id);
178 }
179 return ids;
180}
181
182// Test that exchanging an offer and answer with each side having an audio and
183// video stream creates the appropriate send/recv streams in the underlying
184// media engine on both sides.
Steve Antonad7bffc2018-01-22 10:21:56 -0800185TEST_P(PeerConnectionMediaTest, AudioVideoOfferAnswerCreateSendRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700186 const std::string kCallerAudioId = "caller_a";
187 const std::string kCallerVideoId = "caller_v";
188 const std::string kCalleeAudioId = "callee_a";
189 const std::string kCalleeVideoId = "callee_v";
190
191 auto caller = CreatePeerConnection();
192 caller->AddAudioTrack(kCallerAudioId);
193 caller->AddVideoTrack(kCallerVideoId);
194
195 auto callee = CreatePeerConnection();
196 callee->AddAudioTrack(kCalleeAudioId);
197 callee->AddVideoTrack(kCalleeVideoId);
198
199 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
200 ASSERT_TRUE(
201 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
202
203 auto* caller_voice = caller->media_engine()->GetVoiceChannel(0);
204 EXPECT_THAT(GetIds(caller_voice->recv_streams()),
205 ElementsAre(kCalleeAudioId));
206 EXPECT_THAT(GetIds(caller_voice->send_streams()),
207 ElementsAre(kCallerAudioId));
208
209 auto* caller_video = caller->media_engine()->GetVideoChannel(0);
210 EXPECT_THAT(GetIds(caller_video->recv_streams()),
211 ElementsAre(kCalleeVideoId));
212 EXPECT_THAT(GetIds(caller_video->send_streams()),
213 ElementsAre(kCallerVideoId));
214
215 auto* callee_voice = callee->media_engine()->GetVoiceChannel(0);
216 EXPECT_THAT(GetIds(callee_voice->recv_streams()),
217 ElementsAre(kCallerAudioId));
218 EXPECT_THAT(GetIds(callee_voice->send_streams()),
219 ElementsAre(kCalleeAudioId));
220
221 auto* callee_video = callee->media_engine()->GetVideoChannel(0);
222 EXPECT_THAT(GetIds(callee_video->recv_streams()),
223 ElementsAre(kCallerVideoId));
224 EXPECT_THAT(GetIds(callee_video->send_streams()),
225 ElementsAre(kCalleeVideoId));
226}
227
Steve Antonad7bffc2018-01-22 10:21:56 -0800228// Test that stopping the caller transceivers causes the media channels on the
229// callee to be destroyed after calling SetRemoteDescription on the generated
230// offer.
231// See next test for equivalent behavior with Plan B semantics.
232TEST_F(PeerConnectionMediaTestUnifiedPlan,
233 StoppedRemoteTransceiversRemovesMediaChannels) {
234 auto caller = CreatePeerConnectionWithAudioVideo();
235 auto callee = CreatePeerConnection();
236
237 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
238
239 // Stop both audio and video transceivers on the caller.
240 auto transceivers = caller->pc()->GetTransceivers();
241 ASSERT_EQ(2u, transceivers.size());
242 transceivers[0]->Stop();
243 transceivers[1]->Stop();
244
245 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
246
247 ASSERT_FALSE(callee->media_engine()->GetVoiceChannel(0));
248 ASSERT_FALSE(callee->media_engine()->GetVideoChannel(0));
249}
250
Steve Anton8d3444d2017-10-20 15:30:51 -0700251// Test that removing streams from a subsequent offer causes the receive streams
252// on the callee to be removed.
Steve Antonad7bffc2018-01-22 10:21:56 -0800253// See previous test for equivalent behavior with Unified Plan semantics.
254TEST_F(PeerConnectionMediaTestPlanB, EmptyRemoteOfferRemovesRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700255 auto caller = CreatePeerConnection();
256 auto caller_audio_track = caller->AddAudioTrack("a");
257 auto caller_video_track = caller->AddVideoTrack("v");
258 auto callee = CreatePeerConnectionWithAudioVideo();
259
Steve Antonad7bffc2018-01-22 10:21:56 -0800260 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700261
262 // Remove both tracks from caller.
263 caller->pc()->RemoveTrack(caller_audio_track);
264 caller->pc()->RemoveTrack(caller_video_track);
265
Steve Antonad7bffc2018-01-22 10:21:56 -0800266 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700267
268 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 10:21:56 -0800269 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 15:30:51 -0700270 EXPECT_EQ(1u, callee_voice->send_streams().size());
271 EXPECT_EQ(0u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 15:30:51 -0700272 EXPECT_EQ(1u, callee_video->send_streams().size());
273 EXPECT_EQ(0u, callee_video->recv_streams().size());
274}
275
Steve Antonad7bffc2018-01-22 10:21:56 -0800276// Test that stopping the callee transceivers causes the media channels to be
277// destroyed on the callee after calling SetLocalDescription on the local
278// answer.
279// See next test for equivalent behavior with Plan B semantics.
280TEST_F(PeerConnectionMediaTestUnifiedPlan,
281 StoppedLocalTransceiversRemovesMediaChannels) {
282 auto caller = CreatePeerConnectionWithAudioVideo();
283 auto callee = CreatePeerConnectionWithAudioVideo();
284
285 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
286
287 // Stop both audio and video transceivers on the callee.
288 auto transceivers = callee->pc()->GetTransceivers();
289 ASSERT_EQ(2u, transceivers.size());
290 transceivers[0]->Stop();
291 transceivers[1]->Stop();
292
293 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
294
295 EXPECT_FALSE(callee->media_engine()->GetVoiceChannel(0));
296 EXPECT_FALSE(callee->media_engine()->GetVideoChannel(0));
297}
298
Steve Anton8d3444d2017-10-20 15:30:51 -0700299// Test that removing streams from a subsequent answer causes the send streams
300// on the callee to be removed when applied locally.
Steve Antonad7bffc2018-01-22 10:21:56 -0800301// See previous test for equivalent behavior with Unified Plan semantics.
302TEST_F(PeerConnectionMediaTestPlanB, EmptyLocalAnswerRemovesSendStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700303 auto caller = CreatePeerConnectionWithAudioVideo();
304 auto callee = CreatePeerConnection();
305 auto callee_audio_track = callee->AddAudioTrack("a");
306 auto callee_video_track = callee->AddVideoTrack("v");
307
Steve Antonad7bffc2018-01-22 10:21:56 -0800308 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700309
310 // Remove both tracks from callee.
311 callee->pc()->RemoveTrack(callee_audio_track);
312 callee->pc()->RemoveTrack(callee_video_track);
313
Steve Antonad7bffc2018-01-22 10:21:56 -0800314 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700315
316 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 10:21:56 -0800317 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 15:30:51 -0700318 EXPECT_EQ(0u, callee_voice->send_streams().size());
319 EXPECT_EQ(1u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 15:30:51 -0700320 EXPECT_EQ(0u, callee_video->send_streams().size());
321 EXPECT_EQ(1u, callee_video->recv_streams().size());
322}
323
324// Test that a new stream in a subsequent offer causes a new receive stream to
325// be created on the callee.
Steve Antonad7bffc2018-01-22 10:21:56 -0800326TEST_P(PeerConnectionMediaTest, NewStreamInRemoteOfferAddsRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700327 auto caller = CreatePeerConnectionWithAudioVideo();
328 auto callee = CreatePeerConnection();
329
Steve Antonad7bffc2018-01-22 10:21:56 -0800330 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700331
332 // Add second set of tracks to the caller.
333 caller->AddAudioTrack("a2");
334 caller->AddVideoTrack("v2");
335
Steve Antonad7bffc2018-01-22 10:21:56 -0800336 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700337
Steve Antonad7bffc2018-01-22 10:21:56 -0800338 auto a1 = callee->media_engine()->GetVoiceChannel(0);
339 auto a2 = callee->media_engine()->GetVoiceChannel(1);
340 auto v1 = callee->media_engine()->GetVideoChannel(0);
341 auto v2 = callee->media_engine()->GetVideoChannel(1);
342 if (IsUnifiedPlan()) {
343 ASSERT_TRUE(a1);
344 EXPECT_EQ(1u, a1->recv_streams().size());
345 ASSERT_TRUE(a2);
346 EXPECT_EQ(1u, a2->recv_streams().size());
347 ASSERT_TRUE(v1);
348 EXPECT_EQ(1u, v1->recv_streams().size());
349 ASSERT_TRUE(v2);
350 EXPECT_EQ(1u, v2->recv_streams().size());
351 } else {
352 ASSERT_TRUE(a1);
353 EXPECT_EQ(2u, a1->recv_streams().size());
354 ASSERT_FALSE(a2);
355 ASSERT_TRUE(v1);
356 EXPECT_EQ(2u, v1->recv_streams().size());
357 ASSERT_FALSE(v2);
358 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700359}
360
361// Test that a new stream in a subsequent answer causes a new send stream to be
362// created on the callee when added locally.
Steve Antonad7bffc2018-01-22 10:21:56 -0800363TEST_P(PeerConnectionMediaTest, NewStreamInLocalAnswerAddsSendStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700364 auto caller = CreatePeerConnection();
365 auto callee = CreatePeerConnectionWithAudioVideo();
366
Steve Anton22da89f2018-01-25 13:58:07 -0800367 RTCOfferAnswerOptions offer_options;
368 offer_options.offer_to_receive_audio =
Steve Anton8d3444d2017-10-20 15:30:51 -0700369 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 13:58:07 -0800370 offer_options.offer_to_receive_video =
Steve Anton8d3444d2017-10-20 15:30:51 -0700371 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 13:58:07 -0800372 RTCOfferAnswerOptions answer_options;
Steve Anton8d3444d2017-10-20 15:30:51 -0700373
Steve Anton22da89f2018-01-25 13:58:07 -0800374 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
375 answer_options));
Steve Anton8d3444d2017-10-20 15:30:51 -0700376
377 // Add second set of tracks to the callee.
378 callee->AddAudioTrack("a2");
379 callee->AddVideoTrack("v2");
380
Steve Anton22da89f2018-01-25 13:58:07 -0800381 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
382 answer_options));
Steve Anton8d3444d2017-10-20 15:30:51 -0700383
384 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Anton22da89f2018-01-25 13:58:07 -0800385 ASSERT_TRUE(callee_voice);
Steve Anton8d3444d2017-10-20 15:30:51 -0700386 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton22da89f2018-01-25 13:58:07 -0800387 ASSERT_TRUE(callee_video);
388
389 if (IsUnifiedPlan()) {
390 EXPECT_EQ(1u, callee_voice->send_streams().size());
391 EXPECT_EQ(1u, callee_video->send_streams().size());
392 } else {
393 EXPECT_EQ(2u, callee_voice->send_streams().size());
394 EXPECT_EQ(2u, callee_video->send_streams().size());
395 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700396}
397
398// A PeerConnection with no local streams and no explicit answer constraints
399// should not reject any offered media sections.
Steve Antonad7bffc2018-01-22 10:21:56 -0800400TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700401 CreateAnswerWithNoStreamsAndDefaultOptionsDoesNotReject) {
402 auto caller = CreatePeerConnectionWithAudioVideo();
403 auto callee = CreatePeerConnection();
404 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
405 auto answer = callee->CreateAnswer();
406
407 const auto* audio_content =
408 cricket::GetFirstAudioContent(answer->description());
409 ASSERT_TRUE(audio_content);
410 EXPECT_FALSE(audio_content->rejected);
411
412 const auto* video_content =
413 cricket::GetFirstVideoContent(answer->description());
414 ASSERT_TRUE(video_content);
415 EXPECT_FALSE(video_content->rejected);
416}
417
418class PeerConnectionMediaOfferDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800419 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700420 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800421 std::tuple<SdpSemantics,
422 std::tuple<bool, int, RtpTransceiverDirection>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700423 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800424 PeerConnectionMediaOfferDirectionTest()
425 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
426 auto param = std::get<1>(GetParam());
427 send_media_ = std::get<0>(param);
428 offer_to_receive_ = std::get<1>(param);
429 expected_direction_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700430 }
431
432 bool send_media_;
433 int offer_to_receive_;
Steve Anton4e70a722017-11-28 14:57:10 -0800434 RtpTransceiverDirection expected_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700435};
436
437// Tests that the correct direction is set on the media description according
438// to the presence of a local media track and the offer_to_receive setting.
439TEST_P(PeerConnectionMediaOfferDirectionTest, VerifyDirection) {
440 auto caller = CreatePeerConnection();
441 if (send_media_) {
442 caller->AddAudioTrack("a");
443 }
444
445 RTCOfferAnswerOptions options;
446 options.offer_to_receive_audio = offer_to_receive_;
447 auto offer = caller->CreateOffer(options);
448
Steve Antonad7bffc2018-01-22 10:21:56 -0800449 auto* content = cricket::GetFirstMediaContent(offer->description(),
450 cricket::MEDIA_TYPE_AUDIO);
Steve Anton4e70a722017-11-28 14:57:10 -0800451 if (expected_direction_ == RtpTransceiverDirection::kInactive) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800452 EXPECT_FALSE(content);
Steve Anton8d3444d2017-10-20 15:30:51 -0700453 } else {
Steve Antonad7bffc2018-01-22 10:21:56 -0800454 EXPECT_EQ(expected_direction_, content->media_description()->direction());
Steve Anton8d3444d2017-10-20 15:30:51 -0700455 }
456}
457
458// Note that in these tests, MD_INACTIVE indicates that no media section is
459// included in the offer, not that the media direction is inactive.
Steve Anton4e70a722017-11-28 14:57:10 -0800460INSTANTIATE_TEST_CASE_P(
461 PeerConnectionMediaTest,
462 PeerConnectionMediaOfferDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800463 Combine(
464 Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
465 Values(std::make_tuple(false, -1, RtpTransceiverDirection::kInactive),
466 std::make_tuple(false, 0, RtpTransceiverDirection::kInactive),
467 std::make_tuple(false, 1, RtpTransceiverDirection::kRecvOnly),
468 std::make_tuple(true, -1, RtpTransceiverDirection::kSendRecv),
469 std::make_tuple(true, 0, RtpTransceiverDirection::kSendOnly),
470 std::make_tuple(true, 1, RtpTransceiverDirection::kSendRecv))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700471
472class PeerConnectionMediaAnswerDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800473 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700474 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800475 std::tuple<SdpSemantics, RtpTransceiverDirection, bool, int>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700476 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800477 PeerConnectionMediaAnswerDirectionTest()
478 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
479 offer_direction_ = std::get<1>(GetParam());
480 send_media_ = std::get<2>(GetParam());
481 offer_to_receive_ = std::get<3>(GetParam());
Steve Anton8d3444d2017-10-20 15:30:51 -0700482 }
483
Steve Anton4e70a722017-11-28 14:57:10 -0800484 RtpTransceiverDirection offer_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700485 bool send_media_;
486 int offer_to_receive_;
487};
488
489// Tests that the direction in an answer is correct according to direction sent
490// in the offer, the presence of a local media track on the receive side and the
491// offer_to_receive setting.
492TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
Steve Anton22da89f2018-01-25 13:58:07 -0800493 if (IsUnifiedPlan() &&
494 offer_to_receive_ != RTCOfferAnswerOptions::kUndefined) {
495 // offer_to_receive_ is not implemented when creating answers with Unified
496 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800497 return;
498 }
Steve Anton22da89f2018-01-25 13:58:07 -0800499
Steve Anton8d3444d2017-10-20 15:30:51 -0700500 auto caller = CreatePeerConnection();
501 caller->AddAudioTrack("a");
502
503 // Create the offer with an audio section and set its direction.
504 auto offer = caller->CreateOffer();
505 cricket::GetFirstAudioContentDescription(offer->description())
506 ->set_direction(offer_direction_);
507
508 auto callee = CreatePeerConnection();
509 if (send_media_) {
510 callee->AddAudioTrack("a");
511 }
512 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
513
514 // Create the answer according to the test parameters.
515 RTCOfferAnswerOptions options;
516 options.offer_to_receive_audio = offer_to_receive_;
517 auto answer = callee->CreateAnswer(options);
518
519 // The expected direction in the answer is the intersection of each side's
520 // capability to send/recv media.
521 // For the offerer, the direction is given in the offer (offer_direction_).
522 // For the answerer, the direction has two components:
523 // 1. Send if the answerer has a local track to send.
524 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
525 // if it has been left as default.
Steve Anton4e70a722017-11-28 14:57:10 -0800526 bool offer_send = RtpTransceiverDirectionHasSend(offer_direction_);
527 bool offer_recv = RtpTransceiverDirectionHasRecv(offer_direction_);
Steve Anton8d3444d2017-10-20 15:30:51 -0700528
529 // The negotiated components determine the direction set in the answer.
Steve Anton1d03a752017-11-27 14:30:09 -0800530 bool negotiate_send = (send_media_ && offer_recv);
531 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_send);
Steve Anton8d3444d2017-10-20 15:30:51 -0700532
533 auto expected_direction =
Steve Anton4e70a722017-11-28 14:57:10 -0800534 RtpTransceiverDirectionFromSendRecv(negotiate_send, negotiate_recv);
Steve Anton8d3444d2017-10-20 15:30:51 -0700535 EXPECT_EQ(expected_direction,
Steve Antonad7bffc2018-01-22 10:21:56 -0800536 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700537}
538
539// Tests that the media section is rejected if and only if the callee has no
540// local media track and has set offer_to_receive to 0, no matter which
541// direction the caller indicated in the offer.
542TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyRejected) {
Steve Anton22da89f2018-01-25 13:58:07 -0800543 if (IsUnifiedPlan() &&
544 offer_to_receive_ != RTCOfferAnswerOptions::kUndefined) {
545 // offer_to_receive_ is not implemented when creating answers with Unified
546 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800547 return;
548 }
Steve Anton22da89f2018-01-25 13:58:07 -0800549
Steve Anton8d3444d2017-10-20 15:30:51 -0700550 auto caller = CreatePeerConnection();
551 caller->AddAudioTrack("a");
552
553 // Create the offer with an audio section and set its direction.
554 auto offer = caller->CreateOffer();
555 cricket::GetFirstAudioContentDescription(offer->description())
556 ->set_direction(offer_direction_);
557
558 auto callee = CreatePeerConnection();
559 if (send_media_) {
560 callee->AddAudioTrack("a");
561 }
562 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
563
564 // Create the answer according to the test parameters.
565 RTCOfferAnswerOptions options;
566 options.offer_to_receive_audio = offer_to_receive_;
567 auto answer = callee->CreateAnswer(options);
568
569 // The media section is rejected if and only if offer_to_receive is explicitly
570 // set to 0 and there is no media to send.
571 auto* audio_content = cricket::GetFirstAudioContent(answer->description());
572 ASSERT_TRUE(audio_content);
573 EXPECT_EQ((offer_to_receive_ == 0 && !send_media_), audio_content->rejected);
574}
575
576INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
577 PeerConnectionMediaAnswerDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800578 Combine(Values(SdpSemantics::kPlanB,
579 SdpSemantics::kUnifiedPlan),
580 Values(RtpTransceiverDirection::kInactive,
Steve Anton4e70a722017-11-28 14:57:10 -0800581 RtpTransceiverDirection::kSendOnly,
582 RtpTransceiverDirection::kRecvOnly,
583 RtpTransceiverDirection::kSendRecv),
Steve Anton8d3444d2017-10-20 15:30:51 -0700584 Bool(),
585 Values(-1, 0, 1)));
586
Steve Antonad7bffc2018-01-22 10:21:56 -0800587TEST_P(PeerConnectionMediaTest, OfferHasDifferentDirectionForAudioVideo) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700588 auto caller = CreatePeerConnection();
589 caller->AddVideoTrack("v");
590
591 RTCOfferAnswerOptions options;
592 options.offer_to_receive_audio = 1;
593 options.offer_to_receive_video = 0;
594 auto offer = caller->CreateOffer(options);
595
Steve Anton4e70a722017-11-28 14:57:10 -0800596 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800597 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800598 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800599 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700600}
601
Steve Antonad7bffc2018-01-22 10:21:56 -0800602TEST_P(PeerConnectionMediaTest, AnswerHasDifferentDirectionsForAudioVideo) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800603 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800604 // offer_to_receive_ is not implemented when creating answers with Unified
605 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800606 return;
607 }
608
Steve Anton8d3444d2017-10-20 15:30:51 -0700609 auto caller = CreatePeerConnectionWithAudioVideo();
610 auto callee = CreatePeerConnection();
611 callee->AddVideoTrack("v");
612
613 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
614
615 RTCOfferAnswerOptions options;
616 options.offer_to_receive_audio = 1;
617 options.offer_to_receive_video = 0;
618 auto answer = callee->CreateAnswer(options);
619
Steve Anton4e70a722017-11-28 14:57:10 -0800620 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800621 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800622 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800623 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700624}
625
626void AddComfortNoiseCodecsToSend(cricket::FakeMediaEngine* media_engine) {
627 const cricket::AudioCodec kComfortNoiseCodec8k(102, "CN", 8000, 0, 1);
628 const cricket::AudioCodec kComfortNoiseCodec16k(103, "CN", 16000, 0, 1);
629
630 auto codecs = media_engine->audio_send_codecs();
631 codecs.push_back(kComfortNoiseCodec8k);
632 codecs.push_back(kComfortNoiseCodec16k);
633 media_engine->SetAudioCodecs(codecs);
634}
635
636bool HasAnyComfortNoiseCodecs(const cricket::SessionDescription* desc) {
637 const auto* audio_desc = cricket::GetFirstAudioContentDescription(desc);
638 for (const auto& codec : audio_desc->codecs()) {
639 if (codec.name == "CN") {
640 return true;
641 }
642 }
643 return false;
644}
645
Steve Antonad7bffc2018-01-22 10:21:56 -0800646TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700647 CreateOfferWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
648 auto caller = CreatePeerConnectionWithAudioVideo();
649 AddComfortNoiseCodecsToSend(caller->media_engine());
650
651 RTCOfferAnswerOptions options;
652 options.voice_activity_detection = false;
653 auto offer = caller->CreateOffer(options);
654
655 EXPECT_FALSE(HasAnyComfortNoiseCodecs(offer->description()));
656}
657
Steve Antonad7bffc2018-01-22 10:21:56 -0800658TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700659 CreateAnswerWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
660 auto caller = CreatePeerConnectionWithAudioVideo();
661 AddComfortNoiseCodecsToSend(caller->media_engine());
662 auto callee = CreatePeerConnectionWithAudioVideo();
663 AddComfortNoiseCodecsToSend(callee->media_engine());
664
665 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
666
667 RTCOfferAnswerOptions options;
668 options.voice_activity_detection = false;
669 auto answer = callee->CreateAnswer(options);
670
671 EXPECT_FALSE(HasAnyComfortNoiseCodecs(answer->description()));
672}
673
674// The following test group verifies that we reject answers with invalid media
675// sections as per RFC 3264.
676
677class PeerConnectionMediaInvalidMediaTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800678 : public PeerConnectionMediaBaseTest,
679 public ::testing::WithParamInterface<std::tuple<
680 SdpSemantics,
Steve Anton8d3444d2017-10-20 15:30:51 -0700681 std::tuple<std::string,
682 std::function<void(cricket::SessionDescription*)>,
Steve Antonad7bffc2018-01-22 10:21:56 -0800683 std::string>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700684 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800685 PeerConnectionMediaInvalidMediaTest()
686 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
687 auto param = std::get<1>(GetParam());
688 mutator_ = std::get<1>(param);
689 expected_error_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700690 }
691
692 std::function<void(cricket::SessionDescription*)> mutator_;
693 std::string expected_error_;
694};
695
696TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetRemoteAnswer) {
697 auto caller = CreatePeerConnectionWithAudioVideo();
698 auto callee = CreatePeerConnectionWithAudioVideo();
699
700 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
701
702 auto answer = callee->CreateAnswer();
703 mutator_(answer->description());
704
705 std::string error;
706 ASSERT_FALSE(caller->SetRemoteDescription(std::move(answer), &error));
707 EXPECT_EQ("Failed to set remote answer sdp: " + expected_error_, error);
708}
709
710TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetLocalAnswer) {
711 auto caller = CreatePeerConnectionWithAudioVideo();
712 auto callee = CreatePeerConnectionWithAudioVideo();
713
714 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
715
716 auto answer = callee->CreateAnswer();
717 mutator_(answer->description());
718
719 std::string error;
720 ASSERT_FALSE(callee->SetLocalDescription(std::move(answer), &error));
721 EXPECT_EQ("Failed to set local answer sdp: " + expected_error_, error);
722}
723
724void RemoveVideoContent(cricket::SessionDescription* desc) {
725 auto content_name = cricket::GetFirstVideoContent(desc)->name;
726 desc->RemoveContentByName(content_name);
727 desc->RemoveTransportInfoByName(content_name);
728}
729
730void RenameVideoContent(cricket::SessionDescription* desc) {
731 auto* video_content = cricket::GetFirstVideoContent(desc);
732 auto* transport_info = desc->GetTransportInfoByName(video_content->name);
733 video_content->name = "video_renamed";
734 transport_info->content_name = video_content->name;
735}
736
737void ReverseMediaContent(cricket::SessionDescription* desc) {
738 std::reverse(desc->contents().begin(), desc->contents().end());
739 std::reverse(desc->transport_infos().begin(), desc->transport_infos().end());
740}
741
742void ChangeMediaTypeAudioToVideo(cricket::SessionDescription* desc) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800743 std::string audio_mid = cricket::GetFirstAudioContent(desc)->name;
744 desc->RemoveContentByName(audio_mid);
745 auto* video_content = cricket::GetFirstVideoContent(desc);
746 desc->AddContent(audio_mid, video_content->type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800747 video_content->media_description()->Copy());
Steve Anton8d3444d2017-10-20 15:30:51 -0700748}
749
750constexpr char kMLinesOutOfOrder[] =
751 "The order of m-lines in answer doesn't match order in offer. Rejecting "
752 "answer.";
753
754INSTANTIATE_TEST_CASE_P(
755 PeerConnectionMediaTest,
756 PeerConnectionMediaInvalidMediaTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800757 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
758 Values(std::make_tuple("remove video",
759 RemoveVideoContent,
760 kMLinesOutOfOrder),
761 std::make_tuple("rename video",
762 RenameVideoContent,
763 kMLinesOutOfOrder),
764 std::make_tuple("reverse media sections",
765 ReverseMediaContent,
766 kMLinesOutOfOrder),
767 std::make_tuple("change audio type to video type",
768 ChangeMediaTypeAudioToVideo,
769 kMLinesOutOfOrder))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700770
771// Test that the correct media engine send/recv streams are created when doing
772// a series of offer/answers where audio/video are both sent, then audio is
773// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800774TEST_P(PeerConnectionMediaTest, TestAVOfferWithAudioOnlyAnswer) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800775 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800776 // offer_to_receive_ is not implemented when creating answers with Unified
777 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800778 return;
779 }
780
Steve Anton8d3444d2017-10-20 15:30:51 -0700781 RTCOfferAnswerOptions options_reject_video;
782 options_reject_video.offer_to_receive_audio =
783 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
784 options_reject_video.offer_to_receive_video = 0;
785
786 auto caller = CreatePeerConnection();
787 caller->AddAudioTrack("a");
788 caller->AddVideoTrack("v");
789 auto callee = CreatePeerConnection();
790
791 // Caller initially offers to send/recv audio and video.
792 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
793 // Callee accepts the audio as recv only but rejects the video.
794 ASSERT_TRUE(caller->SetRemoteDescription(
795 callee->CreateAnswerAndSetAsLocal(options_reject_video)));
796
797 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
798 ASSERT_TRUE(caller_voice);
799 EXPECT_EQ(0u, caller_voice->recv_streams().size());
800 EXPECT_EQ(1u, caller_voice->send_streams().size());
801 auto caller_video = caller->media_engine()->GetVideoChannel(0);
802 EXPECT_FALSE(caller_video);
803
804 // Callee adds its own audio/video stream and offers to receive audio/video
805 // too.
806 callee->AddAudioTrack("a");
807 auto callee_video_track = callee->AddVideoTrack("v");
808 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
809 ASSERT_TRUE(
810 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
811
812 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
813 ASSERT_TRUE(callee_voice);
814 EXPECT_EQ(1u, callee_voice->recv_streams().size());
815 EXPECT_EQ(1u, callee_voice->send_streams().size());
816 auto callee_video = callee->media_engine()->GetVideoChannel(0);
817 ASSERT_TRUE(callee_video);
818 EXPECT_EQ(1u, callee_video->recv_streams().size());
819 EXPECT_EQ(1u, callee_video->send_streams().size());
820
821 // Callee removes video but keeps audio and rejects the video once again.
822 callee->pc()->RemoveTrack(callee_video_track);
823 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
824 ASSERT_TRUE(
825 callee->SetLocalDescription(callee->CreateAnswer(options_reject_video)));
826
827 callee_voice = callee->media_engine()->GetVoiceChannel(0);
828 ASSERT_TRUE(callee_voice);
829 EXPECT_EQ(1u, callee_voice->recv_streams().size());
830 EXPECT_EQ(1u, callee_voice->send_streams().size());
831 callee_video = callee->media_engine()->GetVideoChannel(0);
832 EXPECT_FALSE(callee_video);
833}
834
835// Test that the correct media engine send/recv streams are created when doing
836// a series of offer/answers where audio/video are both sent, then video is
837// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800838TEST_P(PeerConnectionMediaTest, TestAVOfferWithVideoOnlyAnswer) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800839 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800840 // offer_to_receive_ is not implemented when creating answers with Unified
841 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800842 return;
843 }
844
Steve Anton8d3444d2017-10-20 15:30:51 -0700845 // Disable the bundling here. If the media is bundled on audio
846 // transport, then we can't reject the audio because switching the bundled
847 // transport is not currently supported.
848 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6704)
849 RTCOfferAnswerOptions options_no_bundle;
850 options_no_bundle.use_rtp_mux = false;
851 RTCOfferAnswerOptions options_reject_audio = options_no_bundle;
852 options_reject_audio.offer_to_receive_audio = 0;
853 options_reject_audio.offer_to_receive_video =
854 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia;
855
856 auto caller = CreatePeerConnection();
857 caller->AddAudioTrack("a");
858 caller->AddVideoTrack("v");
859 auto callee = CreatePeerConnection();
860
861 // Caller initially offers to send/recv audio and video.
862 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
863 // Callee accepts the video as recv only but rejects the audio.
864 ASSERT_TRUE(caller->SetRemoteDescription(
865 callee->CreateAnswerAndSetAsLocal(options_reject_audio)));
866
867 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
868 EXPECT_FALSE(caller_voice);
869 auto caller_video = caller->media_engine()->GetVideoChannel(0);
870 ASSERT_TRUE(caller_video);
871 EXPECT_EQ(0u, caller_video->recv_streams().size());
872 EXPECT_EQ(1u, caller_video->send_streams().size());
873
874 // Callee adds its own audio/video stream and offers to receive audio/video
875 // too.
876 auto callee_audio_track = callee->AddAudioTrack("a");
877 callee->AddVideoTrack("v");
878 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
879 ASSERT_TRUE(caller->SetRemoteDescription(
880 callee->CreateAnswerAndSetAsLocal(options_no_bundle)));
881
882 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
883 ASSERT_TRUE(callee_voice);
884 EXPECT_EQ(1u, callee_voice->recv_streams().size());
885 EXPECT_EQ(1u, callee_voice->send_streams().size());
886 auto callee_video = callee->media_engine()->GetVideoChannel(0);
887 ASSERT_TRUE(callee_video);
888 EXPECT_EQ(1u, callee_video->recv_streams().size());
889 EXPECT_EQ(1u, callee_video->send_streams().size());
890
891 // Callee removes audio but keeps video and rejects the audio once again.
892 callee->pc()->RemoveTrack(callee_audio_track);
893 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
894 ASSERT_TRUE(
895 callee->SetLocalDescription(callee->CreateAnswer(options_reject_audio)));
896
897 callee_voice = callee->media_engine()->GetVoiceChannel(0);
898 EXPECT_FALSE(callee_voice);
899 callee_video = callee->media_engine()->GetVideoChannel(0);
900 ASSERT_TRUE(callee_video);
901 EXPECT_EQ(1u, callee_video->recv_streams().size());
902 EXPECT_EQ(1u, callee_video->send_streams().size());
903}
904
905// Tests that if the underlying video encoder fails to be initialized (signaled
906// by failing to set send codecs), the PeerConnection signals the error to the
907// client.
Steve Antonad7bffc2018-01-22 10:21:56 -0800908TEST_P(PeerConnectionMediaTest, MediaEngineErrorPropagatedToClients) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700909 auto caller = CreatePeerConnectionWithAudioVideo();
910 auto callee = CreatePeerConnectionWithAudioVideo();
911
912 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
913
914 auto video_channel = caller->media_engine()->GetVideoChannel(0);
915 video_channel->set_fail_set_send_codecs(true);
916
917 std::string error;
918 ASSERT_FALSE(caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(),
919 &error));
920 EXPECT_EQ(
Steve Anton80dd7b52018-02-16 17:08:42 -0800921 "Failed to set remote answer sdp: Failed to set remote video description "
922 "send parameters.",
Steve Anton8d3444d2017-10-20 15:30:51 -0700923 error);
924}
925
926// Tests that if the underlying video encoder fails once then subsequent
927// attempts at setting the local/remote description will also fail, even if
928// SetSendCodecs no longer fails.
Steve Antonad7bffc2018-01-22 10:21:56 -0800929TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700930 FailToApplyDescriptionIfVideoEncoderHasEverFailed) {
931 auto caller = CreatePeerConnectionWithAudioVideo();
932 auto callee = CreatePeerConnectionWithAudioVideo();
933
934 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
935
936 auto video_channel = caller->media_engine()->GetVideoChannel(0);
937 video_channel->set_fail_set_send_codecs(true);
938
939 EXPECT_FALSE(
940 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
941
942 video_channel->set_fail_set_send_codecs(false);
943
944 EXPECT_FALSE(caller->SetRemoteDescription(callee->CreateAnswer()));
945 EXPECT_FALSE(caller->SetLocalDescription(caller->CreateOffer()));
946}
947
948void RenameContent(cricket::SessionDescription* desc,
Steve Antonad7bffc2018-01-22 10:21:56 -0800949 cricket::MediaType media_type,
Steve Anton8d3444d2017-10-20 15:30:51 -0700950 const std::string& new_name) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800951 auto* content = cricket::GetFirstMediaContent(desc, media_type);
Steve Anton8d3444d2017-10-20 15:30:51 -0700952 RTC_DCHECK(content);
Steve Antonad7bffc2018-01-22 10:21:56 -0800953 std::string old_name = content->name;
Steve Anton8d3444d2017-10-20 15:30:51 -0700954 content->name = new_name;
955 auto* transport = desc->GetTransportInfoByName(old_name);
956 RTC_DCHECK(transport);
957 transport->content_name = new_name;
Zhi Huangd2248f82018-04-10 14:41:03 -0700958
959 // Rename the content name in the BUNDLE group.
960 cricket::ContentGroup new_bundle_group =
961 *desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
962 new_bundle_group.RemoveContentName(old_name);
963 new_bundle_group.AddContentName(new_name);
964 desc->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
965 desc->AddGroup(new_bundle_group);
Steve Anton8d3444d2017-10-20 15:30:51 -0700966}
967
968// Tests that an answer responds with the same MIDs as the offer.
Steve Antonad7bffc2018-01-22 10:21:56 -0800969TEST_P(PeerConnectionMediaTest, AnswerHasSameMidsAsOffer) {
Zhi Huang365381f2018-04-13 16:44:34 -0700970 const std::string kAudioMid = "notdefault1";
971 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 15:30:51 -0700972
973 auto caller = CreatePeerConnectionWithAudioVideo();
974 auto callee = CreatePeerConnectionWithAudioVideo();
975
976 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -0800977 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
978 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -0700979 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
980
981 auto answer = callee->CreateAnswer();
982 EXPECT_EQ(kAudioMid,
983 cricket::GetFirstAudioContent(answer->description())->name);
984 EXPECT_EQ(kVideoMid,
985 cricket::GetFirstVideoContent(answer->description())->name);
986}
987
988// Test that if the callee creates a re-offer, the MIDs are the same as the
989// original offer.
Steve Antonad7bffc2018-01-22 10:21:56 -0800990TEST_P(PeerConnectionMediaTest, ReOfferHasSameMidsAsFirstOffer) {
Zhi Huang365381f2018-04-13 16:44:34 -0700991 const std::string kAudioMid = "notdefault1";
992 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 15:30:51 -0700993
994 auto caller = CreatePeerConnectionWithAudioVideo();
995 auto callee = CreatePeerConnectionWithAudioVideo();
996
997 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -0800998 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
999 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -07001000 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1001 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1002
1003 auto reoffer = callee->CreateOffer();
1004 EXPECT_EQ(kAudioMid,
1005 cricket::GetFirstAudioContent(reoffer->description())->name);
1006 EXPECT_EQ(kVideoMid,
1007 cricket::GetFirstVideoContent(reoffer->description())->name);
1008}
1009
Steve Antonad7bffc2018-01-22 10:21:56 -08001010TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -07001011 CombinedAudioVideoBweConfigPropagatedToMediaEngine) {
1012 RTCConfiguration config;
1013 config.combined_audio_video_bwe.emplace(true);
1014 auto caller = CreatePeerConnectionWithAudioVideo(config);
1015
1016 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1017
1018 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1019 ASSERT_TRUE(caller_voice);
1020 const cricket::AudioOptions& audio_options = caller_voice->options();
1021 EXPECT_EQ(config.combined_audio_video_bwe,
1022 audio_options.combined_audio_video_bwe);
1023}
1024
Steve Antonad7bffc2018-01-22 10:21:56 -08001025INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
1026 PeerConnectionMediaTest,
1027 Values(SdpSemantics::kPlanB,
1028 SdpSemantics::kUnifiedPlan));
1029
Steve Anton8d3444d2017-10-20 15:30:51 -07001030} // namespace webrtc