blob: 40fc1ddad30a09aab0be016c1c1e29e443870339 [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
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) {
364 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
365 // offer_to_receive_audio is implemented.
366 if (IsUnifiedPlan()) {
367 return;
368 }
369
Steve Anton8d3444d2017-10-20 15:30:51 -0700370 auto caller = CreatePeerConnection();
371 auto callee = CreatePeerConnectionWithAudioVideo();
372
373 RTCOfferAnswerOptions options;
374 options.offer_to_receive_audio =
375 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
376 options.offer_to_receive_video =
377 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
378
379 ASSERT_TRUE(
380 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
381 ASSERT_TRUE(
382 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
383
384 // Add second set of tracks to the callee.
385 callee->AddAudioTrack("a2");
386 callee->AddVideoTrack("v2");
387
388 ASSERT_TRUE(
389 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
390 ASSERT_TRUE(
391 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
392
393 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
394 EXPECT_EQ(2u, callee_voice->send_streams().size());
395 auto callee_video = callee->media_engine()->GetVideoChannel(0);
396 EXPECT_EQ(2u, callee_video->send_streams().size());
397}
398
399// A PeerConnection with no local streams and no explicit answer constraints
400// should not reject any offered media sections.
Steve Antonad7bffc2018-01-22 10:21:56 -0800401TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700402 CreateAnswerWithNoStreamsAndDefaultOptionsDoesNotReject) {
403 auto caller = CreatePeerConnectionWithAudioVideo();
404 auto callee = CreatePeerConnection();
405 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
406 auto answer = callee->CreateAnswer();
407
408 const auto* audio_content =
409 cricket::GetFirstAudioContent(answer->description());
410 ASSERT_TRUE(audio_content);
411 EXPECT_FALSE(audio_content->rejected);
412
413 const auto* video_content =
414 cricket::GetFirstVideoContent(answer->description());
415 ASSERT_TRUE(video_content);
416 EXPECT_FALSE(video_content->rejected);
417}
418
419class PeerConnectionMediaOfferDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800420 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700421 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800422 std::tuple<SdpSemantics,
423 std::tuple<bool, int, RtpTransceiverDirection>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700424 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800425 PeerConnectionMediaOfferDirectionTest()
426 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
427 auto param = std::get<1>(GetParam());
428 send_media_ = std::get<0>(param);
429 offer_to_receive_ = std::get<1>(param);
430 expected_direction_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700431 }
432
433 bool send_media_;
434 int offer_to_receive_;
Steve Anton4e70a722017-11-28 14:57:10 -0800435 RtpTransceiverDirection expected_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700436};
437
438// Tests that the correct direction is set on the media description according
439// to the presence of a local media track and the offer_to_receive setting.
440TEST_P(PeerConnectionMediaOfferDirectionTest, VerifyDirection) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800441 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
442 // offer_to_receive_audio is implemented.
443 if (IsUnifiedPlan()) {
444 return;
445 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700446 auto caller = CreatePeerConnection();
447 if (send_media_) {
448 caller->AddAudioTrack("a");
449 }
450
451 RTCOfferAnswerOptions options;
452 options.offer_to_receive_audio = offer_to_receive_;
453 auto offer = caller->CreateOffer(options);
454
Steve Antonad7bffc2018-01-22 10:21:56 -0800455 auto* content = cricket::GetFirstMediaContent(offer->description(),
456 cricket::MEDIA_TYPE_AUDIO);
Steve Anton4e70a722017-11-28 14:57:10 -0800457 if (expected_direction_ == RtpTransceiverDirection::kInactive) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800458 EXPECT_FALSE(content);
Steve Anton8d3444d2017-10-20 15:30:51 -0700459 } else {
Steve Antonad7bffc2018-01-22 10:21:56 -0800460 EXPECT_EQ(expected_direction_, content->media_description()->direction());
Steve Anton8d3444d2017-10-20 15:30:51 -0700461 }
462}
463
464// Note that in these tests, MD_INACTIVE indicates that no media section is
465// included in the offer, not that the media direction is inactive.
Steve Anton4e70a722017-11-28 14:57:10 -0800466INSTANTIATE_TEST_CASE_P(
467 PeerConnectionMediaTest,
468 PeerConnectionMediaOfferDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800469 Combine(
470 Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
471 Values(std::make_tuple(false, -1, RtpTransceiverDirection::kInactive),
472 std::make_tuple(false, 0, RtpTransceiverDirection::kInactive),
473 std::make_tuple(false, 1, RtpTransceiverDirection::kRecvOnly),
474 std::make_tuple(true, -1, RtpTransceiverDirection::kSendRecv),
475 std::make_tuple(true, 0, RtpTransceiverDirection::kSendOnly),
476 std::make_tuple(true, 1, RtpTransceiverDirection::kSendRecv))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700477
478class PeerConnectionMediaAnswerDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800479 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700480 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800481 std::tuple<SdpSemantics, RtpTransceiverDirection, bool, int>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700482 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800483 PeerConnectionMediaAnswerDirectionTest()
484 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
485 offer_direction_ = std::get<1>(GetParam());
486 send_media_ = std::get<2>(GetParam());
487 offer_to_receive_ = std::get<3>(GetParam());
Steve Anton8d3444d2017-10-20 15:30:51 -0700488 }
489
Steve Anton4e70a722017-11-28 14:57:10 -0800490 RtpTransceiverDirection offer_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700491 bool send_media_;
492 int offer_to_receive_;
493};
494
495// Tests that the direction in an answer is correct according to direction sent
496// in the offer, the presence of a local media track on the receive side and the
497// offer_to_receive setting.
498TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800499 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
500 // offer_to_receive_audio is implemented.
501 if (IsUnifiedPlan()) {
502 return;
503 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700504 auto caller = CreatePeerConnection();
505 caller->AddAudioTrack("a");
506
507 // Create the offer with an audio section and set its direction.
508 auto offer = caller->CreateOffer();
509 cricket::GetFirstAudioContentDescription(offer->description())
510 ->set_direction(offer_direction_);
511
512 auto callee = CreatePeerConnection();
513 if (send_media_) {
514 callee->AddAudioTrack("a");
515 }
516 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
517
518 // Create the answer according to the test parameters.
519 RTCOfferAnswerOptions options;
520 options.offer_to_receive_audio = offer_to_receive_;
521 auto answer = callee->CreateAnswer(options);
522
523 // The expected direction in the answer is the intersection of each side's
524 // capability to send/recv media.
525 // For the offerer, the direction is given in the offer (offer_direction_).
526 // For the answerer, the direction has two components:
527 // 1. Send if the answerer has a local track to send.
528 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
529 // if it has been left as default.
Steve Anton4e70a722017-11-28 14:57:10 -0800530 bool offer_send = RtpTransceiverDirectionHasSend(offer_direction_);
531 bool offer_recv = RtpTransceiverDirectionHasRecv(offer_direction_);
Steve Anton8d3444d2017-10-20 15:30:51 -0700532
533 // The negotiated components determine the direction set in the answer.
Steve Anton1d03a752017-11-27 14:30:09 -0800534 bool negotiate_send = (send_media_ && offer_recv);
535 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_send);
Steve Anton8d3444d2017-10-20 15:30:51 -0700536
537 auto expected_direction =
Steve Anton4e70a722017-11-28 14:57:10 -0800538 RtpTransceiverDirectionFromSendRecv(negotiate_send, negotiate_recv);
Steve Anton8d3444d2017-10-20 15:30:51 -0700539 EXPECT_EQ(expected_direction,
Steve Antonad7bffc2018-01-22 10:21:56 -0800540 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700541}
542
543// Tests that the media section is rejected if and only if the callee has no
544// local media track and has set offer_to_receive to 0, no matter which
545// direction the caller indicated in the offer.
546TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyRejected) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800547 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
548 // offer_to_receive_audio is implemented.
549 if (IsUnifiedPlan()) {
550 return;
551 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700552 auto caller = CreatePeerConnection();
553 caller->AddAudioTrack("a");
554
555 // Create the offer with an audio section and set its direction.
556 auto offer = caller->CreateOffer();
557 cricket::GetFirstAudioContentDescription(offer->description())
558 ->set_direction(offer_direction_);
559
560 auto callee = CreatePeerConnection();
561 if (send_media_) {
562 callee->AddAudioTrack("a");
563 }
564 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
565
566 // Create the answer according to the test parameters.
567 RTCOfferAnswerOptions options;
568 options.offer_to_receive_audio = offer_to_receive_;
569 auto answer = callee->CreateAnswer(options);
570
571 // The media section is rejected if and only if offer_to_receive is explicitly
572 // set to 0 and there is no media to send.
573 auto* audio_content = cricket::GetFirstAudioContent(answer->description());
574 ASSERT_TRUE(audio_content);
575 EXPECT_EQ((offer_to_receive_ == 0 && !send_media_), audio_content->rejected);
576}
577
578INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
579 PeerConnectionMediaAnswerDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800580 Combine(Values(SdpSemantics::kPlanB,
581 SdpSemantics::kUnifiedPlan),
582 Values(RtpTransceiverDirection::kInactive,
Steve Anton4e70a722017-11-28 14:57:10 -0800583 RtpTransceiverDirection::kSendOnly,
584 RtpTransceiverDirection::kRecvOnly,
585 RtpTransceiverDirection::kSendRecv),
Steve Anton8d3444d2017-10-20 15:30:51 -0700586 Bool(),
587 Values(-1, 0, 1)));
588
Steve Antonad7bffc2018-01-22 10:21:56 -0800589TEST_P(PeerConnectionMediaTest, OfferHasDifferentDirectionForAudioVideo) {
590 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
591 // offer_to_receive_audio is implemented.
592 if (IsUnifiedPlan()) {
593 return;
594 }
595
Steve Anton8d3444d2017-10-20 15:30:51 -0700596 auto caller = CreatePeerConnection();
597 caller->AddVideoTrack("v");
598
599 RTCOfferAnswerOptions options;
600 options.offer_to_receive_audio = 1;
601 options.offer_to_receive_video = 0;
602 auto offer = caller->CreateOffer(options);
603
Steve Anton4e70a722017-11-28 14:57:10 -0800604 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800605 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800606 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800607 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700608}
609
Steve Antonad7bffc2018-01-22 10:21:56 -0800610TEST_P(PeerConnectionMediaTest, AnswerHasDifferentDirectionsForAudioVideo) {
611 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
612 // offer_to_receive_audio is implemented.
613 if (IsUnifiedPlan()) {
614 return;
615 }
616
Steve Anton8d3444d2017-10-20 15:30:51 -0700617 auto caller = CreatePeerConnectionWithAudioVideo();
618 auto callee = CreatePeerConnection();
619 callee->AddVideoTrack("v");
620
621 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
622
623 RTCOfferAnswerOptions options;
624 options.offer_to_receive_audio = 1;
625 options.offer_to_receive_video = 0;
626 auto answer = callee->CreateAnswer(options);
627
Steve Anton4e70a722017-11-28 14:57:10 -0800628 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800629 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800630 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800631 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700632}
633
634void AddComfortNoiseCodecsToSend(cricket::FakeMediaEngine* media_engine) {
635 const cricket::AudioCodec kComfortNoiseCodec8k(102, "CN", 8000, 0, 1);
636 const cricket::AudioCodec kComfortNoiseCodec16k(103, "CN", 16000, 0, 1);
637
638 auto codecs = media_engine->audio_send_codecs();
639 codecs.push_back(kComfortNoiseCodec8k);
640 codecs.push_back(kComfortNoiseCodec16k);
641 media_engine->SetAudioCodecs(codecs);
642}
643
644bool HasAnyComfortNoiseCodecs(const cricket::SessionDescription* desc) {
645 const auto* audio_desc = cricket::GetFirstAudioContentDescription(desc);
646 for (const auto& codec : audio_desc->codecs()) {
647 if (codec.name == "CN") {
648 return true;
649 }
650 }
651 return false;
652}
653
Steve Antonad7bffc2018-01-22 10:21:56 -0800654TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700655 CreateOfferWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
656 auto caller = CreatePeerConnectionWithAudioVideo();
657 AddComfortNoiseCodecsToSend(caller->media_engine());
658
659 RTCOfferAnswerOptions options;
660 options.voice_activity_detection = false;
661 auto offer = caller->CreateOffer(options);
662
663 EXPECT_FALSE(HasAnyComfortNoiseCodecs(offer->description()));
664}
665
Steve Antonad7bffc2018-01-22 10:21:56 -0800666TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700667 CreateAnswerWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
668 auto caller = CreatePeerConnectionWithAudioVideo();
669 AddComfortNoiseCodecsToSend(caller->media_engine());
670 auto callee = CreatePeerConnectionWithAudioVideo();
671 AddComfortNoiseCodecsToSend(callee->media_engine());
672
673 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
674
675 RTCOfferAnswerOptions options;
676 options.voice_activity_detection = false;
677 auto answer = callee->CreateAnswer(options);
678
679 EXPECT_FALSE(HasAnyComfortNoiseCodecs(answer->description()));
680}
681
682// The following test group verifies that we reject answers with invalid media
683// sections as per RFC 3264.
684
685class PeerConnectionMediaInvalidMediaTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800686 : public PeerConnectionMediaBaseTest,
687 public ::testing::WithParamInterface<std::tuple<
688 SdpSemantics,
Steve Anton8d3444d2017-10-20 15:30:51 -0700689 std::tuple<std::string,
690 std::function<void(cricket::SessionDescription*)>,
Steve Antonad7bffc2018-01-22 10:21:56 -0800691 std::string>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700692 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800693 PeerConnectionMediaInvalidMediaTest()
694 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
695 auto param = std::get<1>(GetParam());
696 mutator_ = std::get<1>(param);
697 expected_error_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700698 }
699
700 std::function<void(cricket::SessionDescription*)> mutator_;
701 std::string expected_error_;
702};
703
704TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetRemoteAnswer) {
705 auto caller = CreatePeerConnectionWithAudioVideo();
706 auto callee = CreatePeerConnectionWithAudioVideo();
707
708 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
709
710 auto answer = callee->CreateAnswer();
711 mutator_(answer->description());
712
713 std::string error;
714 ASSERT_FALSE(caller->SetRemoteDescription(std::move(answer), &error));
715 EXPECT_EQ("Failed to set remote answer sdp: " + expected_error_, error);
716}
717
718TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetLocalAnswer) {
719 auto caller = CreatePeerConnectionWithAudioVideo();
720 auto callee = CreatePeerConnectionWithAudioVideo();
721
722 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
723
724 auto answer = callee->CreateAnswer();
725 mutator_(answer->description());
726
727 std::string error;
728 ASSERT_FALSE(callee->SetLocalDescription(std::move(answer), &error));
729 EXPECT_EQ("Failed to set local answer sdp: " + expected_error_, error);
730}
731
732void RemoveVideoContent(cricket::SessionDescription* desc) {
733 auto content_name = cricket::GetFirstVideoContent(desc)->name;
734 desc->RemoveContentByName(content_name);
735 desc->RemoveTransportInfoByName(content_name);
736}
737
738void RenameVideoContent(cricket::SessionDescription* desc) {
739 auto* video_content = cricket::GetFirstVideoContent(desc);
740 auto* transport_info = desc->GetTransportInfoByName(video_content->name);
741 video_content->name = "video_renamed";
742 transport_info->content_name = video_content->name;
743}
744
745void ReverseMediaContent(cricket::SessionDescription* desc) {
746 std::reverse(desc->contents().begin(), desc->contents().end());
747 std::reverse(desc->transport_infos().begin(), desc->transport_infos().end());
748}
749
750void ChangeMediaTypeAudioToVideo(cricket::SessionDescription* desc) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800751 std::string audio_mid = cricket::GetFirstAudioContent(desc)->name;
752 desc->RemoveContentByName(audio_mid);
753 auto* video_content = cricket::GetFirstVideoContent(desc);
754 desc->AddContent(audio_mid, video_content->type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800755 video_content->media_description()->Copy());
Steve Anton8d3444d2017-10-20 15:30:51 -0700756}
757
758constexpr char kMLinesOutOfOrder[] =
759 "The order of m-lines in answer doesn't match order in offer. Rejecting "
760 "answer.";
761
762INSTANTIATE_TEST_CASE_P(
763 PeerConnectionMediaTest,
764 PeerConnectionMediaInvalidMediaTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800765 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
766 Values(std::make_tuple("remove video",
767 RemoveVideoContent,
768 kMLinesOutOfOrder),
769 std::make_tuple("rename video",
770 RenameVideoContent,
771 kMLinesOutOfOrder),
772 std::make_tuple("reverse media sections",
773 ReverseMediaContent,
774 kMLinesOutOfOrder),
775 std::make_tuple("change audio type to video type",
776 ChangeMediaTypeAudioToVideo,
777 kMLinesOutOfOrder))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700778
779// Test that the correct media engine send/recv streams are created when doing
780// a series of offer/answers where audio/video are both sent, then audio is
781// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800782TEST_P(PeerConnectionMediaTest, TestAVOfferWithAudioOnlyAnswer) {
783 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
784 // offer_to_receive_audio is implemented.
785 if (IsUnifiedPlan()) {
786 return;
787 }
788
Steve Anton8d3444d2017-10-20 15:30:51 -0700789 RTCOfferAnswerOptions options_reject_video;
790 options_reject_video.offer_to_receive_audio =
791 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
792 options_reject_video.offer_to_receive_video = 0;
793
794 auto caller = CreatePeerConnection();
795 caller->AddAudioTrack("a");
796 caller->AddVideoTrack("v");
797 auto callee = CreatePeerConnection();
798
799 // Caller initially offers to send/recv audio and video.
800 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
801 // Callee accepts the audio as recv only but rejects the video.
802 ASSERT_TRUE(caller->SetRemoteDescription(
803 callee->CreateAnswerAndSetAsLocal(options_reject_video)));
804
805 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
806 ASSERT_TRUE(caller_voice);
807 EXPECT_EQ(0u, caller_voice->recv_streams().size());
808 EXPECT_EQ(1u, caller_voice->send_streams().size());
809 auto caller_video = caller->media_engine()->GetVideoChannel(0);
810 EXPECT_FALSE(caller_video);
811
812 // Callee adds its own audio/video stream and offers to receive audio/video
813 // too.
814 callee->AddAudioTrack("a");
815 auto callee_video_track = callee->AddVideoTrack("v");
816 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
817 ASSERT_TRUE(
818 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
819
820 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
821 ASSERT_TRUE(callee_voice);
822 EXPECT_EQ(1u, callee_voice->recv_streams().size());
823 EXPECT_EQ(1u, callee_voice->send_streams().size());
824 auto callee_video = callee->media_engine()->GetVideoChannel(0);
825 ASSERT_TRUE(callee_video);
826 EXPECT_EQ(1u, callee_video->recv_streams().size());
827 EXPECT_EQ(1u, callee_video->send_streams().size());
828
829 // Callee removes video but keeps audio and rejects the video once again.
830 callee->pc()->RemoveTrack(callee_video_track);
831 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
832 ASSERT_TRUE(
833 callee->SetLocalDescription(callee->CreateAnswer(options_reject_video)));
834
835 callee_voice = callee->media_engine()->GetVoiceChannel(0);
836 ASSERT_TRUE(callee_voice);
837 EXPECT_EQ(1u, callee_voice->recv_streams().size());
838 EXPECT_EQ(1u, callee_voice->send_streams().size());
839 callee_video = callee->media_engine()->GetVideoChannel(0);
840 EXPECT_FALSE(callee_video);
841}
842
843// Test that the correct media engine send/recv streams are created when doing
844// a series of offer/answers where audio/video are both sent, then video is
845// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800846TEST_P(PeerConnectionMediaTest, TestAVOfferWithVideoOnlyAnswer) {
847 // TODO(bugs.webrtc.org/8765): Enable this test under Unified Plan once
848 // offer_to_receive_audio is implemented.
849 if (IsUnifiedPlan()) {
850 return;
851 }
852
Steve Anton8d3444d2017-10-20 15:30:51 -0700853 // Disable the bundling here. If the media is bundled on audio
854 // transport, then we can't reject the audio because switching the bundled
855 // transport is not currently supported.
856 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6704)
857 RTCOfferAnswerOptions options_no_bundle;
858 options_no_bundle.use_rtp_mux = false;
859 RTCOfferAnswerOptions options_reject_audio = options_no_bundle;
860 options_reject_audio.offer_to_receive_audio = 0;
861 options_reject_audio.offer_to_receive_video =
862 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia;
863
864 auto caller = CreatePeerConnection();
865 caller->AddAudioTrack("a");
866 caller->AddVideoTrack("v");
867 auto callee = CreatePeerConnection();
868
869 // Caller initially offers to send/recv audio and video.
870 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
871 // Callee accepts the video as recv only but rejects the audio.
872 ASSERT_TRUE(caller->SetRemoteDescription(
873 callee->CreateAnswerAndSetAsLocal(options_reject_audio)));
874
875 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
876 EXPECT_FALSE(caller_voice);
877 auto caller_video = caller->media_engine()->GetVideoChannel(0);
878 ASSERT_TRUE(caller_video);
879 EXPECT_EQ(0u, caller_video->recv_streams().size());
880 EXPECT_EQ(1u, caller_video->send_streams().size());
881
882 // Callee adds its own audio/video stream and offers to receive audio/video
883 // too.
884 auto callee_audio_track = callee->AddAudioTrack("a");
885 callee->AddVideoTrack("v");
886 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
887 ASSERT_TRUE(caller->SetRemoteDescription(
888 callee->CreateAnswerAndSetAsLocal(options_no_bundle)));
889
890 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
891 ASSERT_TRUE(callee_voice);
892 EXPECT_EQ(1u, callee_voice->recv_streams().size());
893 EXPECT_EQ(1u, callee_voice->send_streams().size());
894 auto callee_video = callee->media_engine()->GetVideoChannel(0);
895 ASSERT_TRUE(callee_video);
896 EXPECT_EQ(1u, callee_video->recv_streams().size());
897 EXPECT_EQ(1u, callee_video->send_streams().size());
898
899 // Callee removes audio but keeps video and rejects the audio once again.
900 callee->pc()->RemoveTrack(callee_audio_track);
901 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
902 ASSERT_TRUE(
903 callee->SetLocalDescription(callee->CreateAnswer(options_reject_audio)));
904
905 callee_voice = callee->media_engine()->GetVoiceChannel(0);
906 EXPECT_FALSE(callee_voice);
907 callee_video = callee->media_engine()->GetVideoChannel(0);
908 ASSERT_TRUE(callee_video);
909 EXPECT_EQ(1u, callee_video->recv_streams().size());
910 EXPECT_EQ(1u, callee_video->send_streams().size());
911}
912
913// Tests that if the underlying video encoder fails to be initialized (signaled
914// by failing to set send codecs), the PeerConnection signals the error to the
915// client.
Steve Antonad7bffc2018-01-22 10:21:56 -0800916TEST_P(PeerConnectionMediaTest, MediaEngineErrorPropagatedToClients) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700917 auto caller = CreatePeerConnectionWithAudioVideo();
918 auto callee = CreatePeerConnectionWithAudioVideo();
919
920 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
921
922 auto video_channel = caller->media_engine()->GetVideoChannel(0);
923 video_channel->set_fail_set_send_codecs(true);
924
925 std::string error;
926 ASSERT_FALSE(caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(),
927 &error));
928 EXPECT_EQ(
929 "Failed to set remote answer sdp: Session error code: ERROR_CONTENT. "
930 "Session error description: Failed to set remote video description send "
931 "parameters..",
932 error);
933}
934
935// Tests that if the underlying video encoder fails once then subsequent
936// attempts at setting the local/remote description will also fail, even if
937// SetSendCodecs no longer fails.
Steve Antonad7bffc2018-01-22 10:21:56 -0800938TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700939 FailToApplyDescriptionIfVideoEncoderHasEverFailed) {
940 auto caller = CreatePeerConnectionWithAudioVideo();
941 auto callee = CreatePeerConnectionWithAudioVideo();
942
943 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
944
945 auto video_channel = caller->media_engine()->GetVideoChannel(0);
946 video_channel->set_fail_set_send_codecs(true);
947
948 EXPECT_FALSE(
949 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
950
951 video_channel->set_fail_set_send_codecs(false);
952
953 EXPECT_FALSE(caller->SetRemoteDescription(callee->CreateAnswer()));
954 EXPECT_FALSE(caller->SetLocalDescription(caller->CreateOffer()));
955}
956
957void RenameContent(cricket::SessionDescription* desc,
Steve Antonad7bffc2018-01-22 10:21:56 -0800958 cricket::MediaType media_type,
Steve Anton8d3444d2017-10-20 15:30:51 -0700959 const std::string& new_name) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800960 auto* content = cricket::GetFirstMediaContent(desc, media_type);
Steve Anton8d3444d2017-10-20 15:30:51 -0700961 RTC_DCHECK(content);
Steve Antonad7bffc2018-01-22 10:21:56 -0800962 std::string old_name = content->name;
Steve Anton8d3444d2017-10-20 15:30:51 -0700963 content->name = new_name;
964 auto* transport = desc->GetTransportInfoByName(old_name);
965 RTC_DCHECK(transport);
966 transport->content_name = new_name;
967}
968
969// Tests that an answer responds with the same MIDs as the offer.
Steve Antonad7bffc2018-01-22 10:21:56 -0800970TEST_P(PeerConnectionMediaTest, AnswerHasSameMidsAsOffer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700971 const std::string kAudioMid = "not default1";
972 const std::string kVideoMid = "not default2";
973
974 auto caller = CreatePeerConnectionWithAudioVideo();
975 auto callee = CreatePeerConnectionWithAudioVideo();
976
977 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -0800978 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
979 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -0700980 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
981
982 auto answer = callee->CreateAnswer();
983 EXPECT_EQ(kAudioMid,
984 cricket::GetFirstAudioContent(answer->description())->name);
985 EXPECT_EQ(kVideoMid,
986 cricket::GetFirstVideoContent(answer->description())->name);
987}
988
989// Test that if the callee creates a re-offer, the MIDs are the same as the
990// original offer.
Steve Antonad7bffc2018-01-22 10:21:56 -0800991TEST_P(PeerConnectionMediaTest, ReOfferHasSameMidsAsFirstOffer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700992 const std::string kAudioMid = "not default1";
993 const std::string kVideoMid = "not default2";
994
995 auto caller = CreatePeerConnectionWithAudioVideo();
996 auto callee = CreatePeerConnectionWithAudioVideo();
997
998 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -0800999 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
1000 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -07001001 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1002 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1003
1004 auto reoffer = callee->CreateOffer();
1005 EXPECT_EQ(kAudioMid,
1006 cricket::GetFirstAudioContent(reoffer->description())->name);
1007 EXPECT_EQ(kVideoMid,
1008 cricket::GetFirstVideoContent(reoffer->description())->name);
1009}
1010
Steve Antonad7bffc2018-01-22 10:21:56 -08001011TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -07001012 CombinedAudioVideoBweConfigPropagatedToMediaEngine) {
1013 RTCConfiguration config;
1014 config.combined_audio_video_bwe.emplace(true);
1015 auto caller = CreatePeerConnectionWithAudioVideo(config);
1016
1017 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1018
1019 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1020 ASSERT_TRUE(caller_voice);
1021 const cricket::AudioOptions& audio_options = caller_voice->options();
1022 EXPECT_EQ(config.combined_audio_video_bwe,
1023 audio_options.combined_audio_video_bwe);
1024}
1025
Steve Antonad7bffc2018-01-22 10:21:56 -08001026INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
1027 PeerConnectionMediaTest,
1028 Values(SdpSemantics::kPlanB,
1029 SdpSemantics::kUnifiedPlan));
1030
Steve Anton8d3444d2017-10-20 15:30:51 -07001031} // namespace webrtc