blob: 6f4fe1e3f489e9892d6492168b37ebdd5030e5bf [file] [log] [blame]
Steve Anton8d3444d2017-10-20 22:30:511/*
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 11:20:1317#include "api/call/callfactoryinterface.h"
Steve Anton8d3444d2017-10-20 22:30:5118#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 22:30:0923#include "pc/rtpmediautils.h"
Steve Anton8d3444d2017-10-20 22:30:5124#include "pc/sdputils.h"
25#ifdef WEBRTC_ANDROID
26#include "pc/test/androidtestinitializer.h"
27#endif
Karl Wiberg918f50c2018-07-05 09:40:3328#include "absl/memory/memory.h"
Steve Anton8d3444d2017-10-20 22:30:5129#include "pc/test/fakertccertificategenerator.h"
30#include "rtc_base/gunit.h"
Steve Anton8d3444d2017-10-20 22:30:5131#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 18:21:5657class PeerConnectionMediaBaseTest : public ::testing::Test {
Steve Anton8d3444d2017-10-20 22:30:5158 protected:
59 typedef std::unique_ptr<PeerConnectionWrapperForMediaTest> WrapperPtr;
60
Steve Antonad7bffc2018-01-22 18:21:5661 explicit PeerConnectionMediaBaseTest(SdpSemantics sdp_semantics)
62 : vss_(new rtc::VirtualSocketServer()),
63 main_(vss_.get()),
64 sdp_semantics_(sdp_semantics) {
Steve Anton8d3444d2017-10-20 22:30:5165#ifdef WEBRTC_ANDROID
66 InitializeAndroidObjects();
67#endif
68 }
69
70 WrapperPtr CreatePeerConnection() {
71 return CreatePeerConnection(RTCConfiguration());
72 }
73
74 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
Karl Wiberg918f50c2018-07-05 09:40:3375 auto media_engine = absl::make_unique<FakeMediaEngine>();
Steve Anton8d3444d2017-10-20 22:30:5176 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
Karl Wiberg918f50c2018-07-05 09:40:3382 auto fake_port_allocator = absl::make_unique<cricket::FakePortAllocator>(
Steve Anton8d3444d2017-10-20 22:30:5183 rtc::Thread::Current(), nullptr);
Karl Wiberg918f50c2018-07-05 09:40:3384 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Steve Antonad7bffc2018-01-22 18:21:5685 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 22:30:5190 if (!pc) {
91 return nullptr;
92 }
93
Karl Wiberg918f50c2018-07-05 09:40:3394 auto wrapper = absl::make_unique<PeerConnectionWrapperForMediaTest>(
Steve Anton8d3444d2017-10-20 22:30:5195 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 22:57:10113 RtpTransceiverDirection GetMediaContentDirection(
Steve Anton8d3444d2017-10-20 22:30:51114 const SessionDescriptionInterface* sdesc,
Steve Antonad7bffc2018-01-22 18:21:56115 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 22:30:51124 }
125
126 std::unique_ptr<rtc::VirtualSocketServer> vss_;
127 rtc::AutoSocketServerThread main_;
Steve Antonad7bffc2018-01-22 18:21:56128 const SdpSemantics sdp_semantics_;
Steve Anton8d3444d2017-10-20 22:30:51129};
130
Steve Antonad7bffc2018-01-22 18:21:56131class 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 22:30:51151 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 18:21:56158 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
159 "Failed to set remote offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 22:30:51160}
161
Steve Antonad7bffc2018-01-22 18:21:56162TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:51163 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 18:21:56169 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
170 "Failed to set local offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 22:30:51171}
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 18:21:56185TEST_P(PeerConnectionMediaTest, AudioVideoOfferAnswerCreateSendRecvStreams) {
Steve Anton8d3444d2017-10-20 22:30:51186 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 18:21:56228// 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 22:30:51251// Test that removing streams from a subsequent offer causes the receive streams
252// on the callee to be removed.
Steve Antonad7bffc2018-01-22 18:21:56253// See previous test for equivalent behavior with Unified Plan semantics.
254TEST_F(PeerConnectionMediaTestPlanB, EmptyRemoteOfferRemovesRecvStreams) {
Steve Anton8d3444d2017-10-20 22:30:51255 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 18:21:56260 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51261
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 18:21:56266 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51267
268 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 18:21:56269 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 22:30:51270 EXPECT_EQ(1u, callee_voice->send_streams().size());
271 EXPECT_EQ(0u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 22:30:51272 EXPECT_EQ(1u, callee_video->send_streams().size());
273 EXPECT_EQ(0u, callee_video->recv_streams().size());
274}
275
Jonas Orelandfc1acd22018-08-24 08:58:37276// Test enabling of simulcast with Plan B semantics.
277// This test creating an offer.
278TEST_F(PeerConnectionMediaTestPlanB, SimulcastOffer) {
279 auto caller = CreatePeerConnection();
280 auto caller_video_track = caller->AddVideoTrack("v");
281 RTCOfferAnswerOptions options;
282 options.num_simulcast_layers = 3;
283 auto offer = caller->CreateOffer(options);
284 auto* description = cricket::GetFirstMediaContent(
285 offer->description(),
286 cricket::MEDIA_TYPE_VIDEO)->media_description();
287 ASSERT_EQ(1u, description->streams().size());
288 ASSERT_TRUE(description->streams()[0].get_ssrc_group("SIM"));
289 EXPECT_EQ(3u, description->streams()[0].get_ssrc_group("SIM")->ssrcs.size());
290
291 // Check that it actually creates simulcast aswell.
292 caller->SetLocalDescription(std::move(offer));
293 auto senders = caller->pc()->GetSenders();
294 ASSERT_EQ(1u, senders.size());
295 EXPECT_EQ(cricket::MediaType::MEDIA_TYPE_VIDEO, senders[0]->media_type());
296 EXPECT_EQ(3u, senders[0]->GetParameters().encodings.size());
297}
298
299// Test enabling of simulcast with Plan B semantics.
300// This test creating an answer.
301TEST_F(PeerConnectionMediaTestPlanB, SimulcastAnswer) {
302 auto caller = CreatePeerConnection();
303 caller->AddVideoTrack("v0");
304 auto offer = caller->CreateOffer();
305 auto callee = CreatePeerConnection();
306 auto callee_video_track = callee->AddVideoTrack("v1");
307 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
308 RTCOfferAnswerOptions options;
309 options.num_simulcast_layers = 3;
310 auto answer = callee->CreateAnswer(options);
311 auto* description = cricket::GetFirstMediaContent(
312 answer->description(),
313 cricket::MEDIA_TYPE_VIDEO)->media_description();
314 ASSERT_EQ(1u, description->streams().size());
315 ASSERT_TRUE(description->streams()[0].get_ssrc_group("SIM"));
316 EXPECT_EQ(3u, description->streams()[0].get_ssrc_group("SIM")->ssrcs.size());
317
318 // Check that it actually creates simulcast aswell.
319 callee->SetLocalDescription(std::move(answer));
320 auto senders = callee->pc()->GetSenders();
321 ASSERT_EQ(1u, senders.size());
322 EXPECT_EQ(cricket::MediaType::MEDIA_TYPE_VIDEO, senders[0]->media_type());
323 EXPECT_EQ(3u, senders[0]->GetParameters().encodings.size());
324}
325
Steve Antonad7bffc2018-01-22 18:21:56326// Test that stopping the callee transceivers causes the media channels to be
327// destroyed on the callee after calling SetLocalDescription on the local
328// answer.
329// See next test for equivalent behavior with Plan B semantics.
330TEST_F(PeerConnectionMediaTestUnifiedPlan,
331 StoppedLocalTransceiversRemovesMediaChannels) {
332 auto caller = CreatePeerConnectionWithAudioVideo();
333 auto callee = CreatePeerConnectionWithAudioVideo();
334
335 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
336
337 // Stop both audio and video transceivers on the callee.
338 auto transceivers = callee->pc()->GetTransceivers();
339 ASSERT_EQ(2u, transceivers.size());
340 transceivers[0]->Stop();
341 transceivers[1]->Stop();
342
343 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
344
345 EXPECT_FALSE(callee->media_engine()->GetVoiceChannel(0));
346 EXPECT_FALSE(callee->media_engine()->GetVideoChannel(0));
347}
348
Steve Anton8d3444d2017-10-20 22:30:51349// Test that removing streams from a subsequent answer causes the send streams
350// on the callee to be removed when applied locally.
Steve Antonad7bffc2018-01-22 18:21:56351// See previous test for equivalent behavior with Unified Plan semantics.
352TEST_F(PeerConnectionMediaTestPlanB, EmptyLocalAnswerRemovesSendStreams) {
Steve Anton8d3444d2017-10-20 22:30:51353 auto caller = CreatePeerConnectionWithAudioVideo();
354 auto callee = CreatePeerConnection();
355 auto callee_audio_track = callee->AddAudioTrack("a");
356 auto callee_video_track = callee->AddVideoTrack("v");
357
Steve Antonad7bffc2018-01-22 18:21:56358 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51359
360 // Remove both tracks from callee.
361 callee->pc()->RemoveTrack(callee_audio_track);
362 callee->pc()->RemoveTrack(callee_video_track);
363
Steve Antonad7bffc2018-01-22 18:21:56364 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51365
366 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 18:21:56367 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 22:30:51368 EXPECT_EQ(0u, callee_voice->send_streams().size());
369 EXPECT_EQ(1u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 22:30:51370 EXPECT_EQ(0u, callee_video->send_streams().size());
371 EXPECT_EQ(1u, callee_video->recv_streams().size());
372}
373
374// Test that a new stream in a subsequent offer causes a new receive stream to
375// be created on the callee.
Steve Antonad7bffc2018-01-22 18:21:56376TEST_P(PeerConnectionMediaTest, NewStreamInRemoteOfferAddsRecvStreams) {
Steve Anton8d3444d2017-10-20 22:30:51377 auto caller = CreatePeerConnectionWithAudioVideo();
378 auto callee = CreatePeerConnection();
379
Steve Antonad7bffc2018-01-22 18:21:56380 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51381
382 // Add second set of tracks to the caller.
383 caller->AddAudioTrack("a2");
384 caller->AddVideoTrack("v2");
385
Steve Antonad7bffc2018-01-22 18:21:56386 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 22:30:51387
Steve Antonad7bffc2018-01-22 18:21:56388 auto a1 = callee->media_engine()->GetVoiceChannel(0);
389 auto a2 = callee->media_engine()->GetVoiceChannel(1);
390 auto v1 = callee->media_engine()->GetVideoChannel(0);
391 auto v2 = callee->media_engine()->GetVideoChannel(1);
392 if (IsUnifiedPlan()) {
393 ASSERT_TRUE(a1);
394 EXPECT_EQ(1u, a1->recv_streams().size());
395 ASSERT_TRUE(a2);
396 EXPECT_EQ(1u, a2->recv_streams().size());
397 ASSERT_TRUE(v1);
398 EXPECT_EQ(1u, v1->recv_streams().size());
399 ASSERT_TRUE(v2);
400 EXPECT_EQ(1u, v2->recv_streams().size());
401 } else {
402 ASSERT_TRUE(a1);
403 EXPECT_EQ(2u, a1->recv_streams().size());
404 ASSERT_FALSE(a2);
405 ASSERT_TRUE(v1);
406 EXPECT_EQ(2u, v1->recv_streams().size());
407 ASSERT_FALSE(v2);
408 }
Steve Anton8d3444d2017-10-20 22:30:51409}
410
411// Test that a new stream in a subsequent answer causes a new send stream to be
412// created on the callee when added locally.
Steve Antonad7bffc2018-01-22 18:21:56413TEST_P(PeerConnectionMediaTest, NewStreamInLocalAnswerAddsSendStreams) {
Steve Anton8d3444d2017-10-20 22:30:51414 auto caller = CreatePeerConnection();
415 auto callee = CreatePeerConnectionWithAudioVideo();
416
Steve Anton22da89f2018-01-25 21:58:07417 RTCOfferAnswerOptions offer_options;
418 offer_options.offer_to_receive_audio =
Steve Anton8d3444d2017-10-20 22:30:51419 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 21:58:07420 offer_options.offer_to_receive_video =
Steve Anton8d3444d2017-10-20 22:30:51421 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 21:58:07422 RTCOfferAnswerOptions answer_options;
Steve Anton8d3444d2017-10-20 22:30:51423
Steve Anton22da89f2018-01-25 21:58:07424 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
425 answer_options));
Steve Anton8d3444d2017-10-20 22:30:51426
427 // Add second set of tracks to the callee.
428 callee->AddAudioTrack("a2");
429 callee->AddVideoTrack("v2");
430
Steve Anton22da89f2018-01-25 21:58:07431 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
432 answer_options));
Steve Anton8d3444d2017-10-20 22:30:51433
434 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Anton22da89f2018-01-25 21:58:07435 ASSERT_TRUE(callee_voice);
Steve Anton8d3444d2017-10-20 22:30:51436 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton22da89f2018-01-25 21:58:07437 ASSERT_TRUE(callee_video);
438
439 if (IsUnifiedPlan()) {
440 EXPECT_EQ(1u, callee_voice->send_streams().size());
441 EXPECT_EQ(1u, callee_video->send_streams().size());
442 } else {
443 EXPECT_EQ(2u, callee_voice->send_streams().size());
444 EXPECT_EQ(2u, callee_video->send_streams().size());
445 }
Steve Anton8d3444d2017-10-20 22:30:51446}
447
448// A PeerConnection with no local streams and no explicit answer constraints
449// should not reject any offered media sections.
Steve Antonad7bffc2018-01-22 18:21:56450TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:51451 CreateAnswerWithNoStreamsAndDefaultOptionsDoesNotReject) {
452 auto caller = CreatePeerConnectionWithAudioVideo();
453 auto callee = CreatePeerConnection();
454 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
455 auto answer = callee->CreateAnswer();
456
457 const auto* audio_content =
458 cricket::GetFirstAudioContent(answer->description());
459 ASSERT_TRUE(audio_content);
460 EXPECT_FALSE(audio_content->rejected);
461
462 const auto* video_content =
463 cricket::GetFirstVideoContent(answer->description());
464 ASSERT_TRUE(video_content);
465 EXPECT_FALSE(video_content->rejected);
466}
467
468class PeerConnectionMediaOfferDirectionTest
Steve Antonad7bffc2018-01-22 18:21:56469 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 22:30:51470 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 18:21:56471 std::tuple<SdpSemantics,
472 std::tuple<bool, int, RtpTransceiverDirection>>> {
Steve Anton8d3444d2017-10-20 22:30:51473 protected:
Steve Antonad7bffc2018-01-22 18:21:56474 PeerConnectionMediaOfferDirectionTest()
475 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
476 auto param = std::get<1>(GetParam());
477 send_media_ = std::get<0>(param);
478 offer_to_receive_ = std::get<1>(param);
479 expected_direction_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 22:30:51480 }
481
482 bool send_media_;
483 int offer_to_receive_;
Steve Anton4e70a722017-11-28 22:57:10484 RtpTransceiverDirection expected_direction_;
Steve Anton8d3444d2017-10-20 22:30:51485};
486
487// Tests that the correct direction is set on the media description according
488// to the presence of a local media track and the offer_to_receive setting.
489TEST_P(PeerConnectionMediaOfferDirectionTest, VerifyDirection) {
490 auto caller = CreatePeerConnection();
491 if (send_media_) {
492 caller->AddAudioTrack("a");
493 }
494
495 RTCOfferAnswerOptions options;
496 options.offer_to_receive_audio = offer_to_receive_;
497 auto offer = caller->CreateOffer(options);
498
Steve Antonad7bffc2018-01-22 18:21:56499 auto* content = cricket::GetFirstMediaContent(offer->description(),
500 cricket::MEDIA_TYPE_AUDIO);
Steve Anton4e70a722017-11-28 22:57:10501 if (expected_direction_ == RtpTransceiverDirection::kInactive) {
Steve Antonad7bffc2018-01-22 18:21:56502 EXPECT_FALSE(content);
Steve Anton8d3444d2017-10-20 22:30:51503 } else {
Steve Antonad7bffc2018-01-22 18:21:56504 EXPECT_EQ(expected_direction_, content->media_description()->direction());
Steve Anton8d3444d2017-10-20 22:30:51505 }
506}
507
508// Note that in these tests, MD_INACTIVE indicates that no media section is
509// included in the offer, not that the media direction is inactive.
Steve Anton4e70a722017-11-28 22:57:10510INSTANTIATE_TEST_CASE_P(
511 PeerConnectionMediaTest,
512 PeerConnectionMediaOfferDirectionTest,
Steve Antonad7bffc2018-01-22 18:21:56513 Combine(
514 Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
515 Values(std::make_tuple(false, -1, RtpTransceiverDirection::kInactive),
516 std::make_tuple(false, 0, RtpTransceiverDirection::kInactive),
517 std::make_tuple(false, 1, RtpTransceiverDirection::kRecvOnly),
518 std::make_tuple(true, -1, RtpTransceiverDirection::kSendRecv),
519 std::make_tuple(true, 0, RtpTransceiverDirection::kSendOnly),
520 std::make_tuple(true, 1, RtpTransceiverDirection::kSendRecv))));
Steve Anton8d3444d2017-10-20 22:30:51521
522class PeerConnectionMediaAnswerDirectionTest
Steve Antonad7bffc2018-01-22 18:21:56523 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 22:30:51524 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 18:21:56525 std::tuple<SdpSemantics, RtpTransceiverDirection, bool, int>> {
Steve Anton8d3444d2017-10-20 22:30:51526 protected:
Steve Antonad7bffc2018-01-22 18:21:56527 PeerConnectionMediaAnswerDirectionTest()
528 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
529 offer_direction_ = std::get<1>(GetParam());
530 send_media_ = std::get<2>(GetParam());
531 offer_to_receive_ = std::get<3>(GetParam());
Steve Anton8d3444d2017-10-20 22:30:51532 }
533
Steve Anton4e70a722017-11-28 22:57:10534 RtpTransceiverDirection offer_direction_;
Steve Anton8d3444d2017-10-20 22:30:51535 bool send_media_;
536 int offer_to_receive_;
537};
538
539// Tests that the direction in an answer is correct according to direction sent
540// in the offer, the presence of a local media track on the receive side and the
541// offer_to_receive setting.
542TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
Steve Anton22da89f2018-01-25 21:58:07543 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 18:21:56547 return;
548 }
Steve Anton22da89f2018-01-25 21:58:07549
Steve Anton8d3444d2017-10-20 22:30:51550 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 expected direction in the answer is the intersection of each side's
570 // capability to send/recv media.
571 // For the offerer, the direction is given in the offer (offer_direction_).
572 // For the answerer, the direction has two components:
573 // 1. Send if the answerer has a local track to send.
574 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
575 // if it has been left as default.
Steve Anton4e70a722017-11-28 22:57:10576 bool offer_send = RtpTransceiverDirectionHasSend(offer_direction_);
577 bool offer_recv = RtpTransceiverDirectionHasRecv(offer_direction_);
Steve Anton8d3444d2017-10-20 22:30:51578
579 // The negotiated components determine the direction set in the answer.
Steve Anton1d03a752017-11-27 22:30:09580 bool negotiate_send = (send_media_ && offer_recv);
581 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_send);
Steve Anton8d3444d2017-10-20 22:30:51582
583 auto expected_direction =
Steve Anton4e70a722017-11-28 22:57:10584 RtpTransceiverDirectionFromSendRecv(negotiate_send, negotiate_recv);
Steve Anton8d3444d2017-10-20 22:30:51585 EXPECT_EQ(expected_direction,
Steve Antonad7bffc2018-01-22 18:21:56586 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton8d3444d2017-10-20 22:30:51587}
588
589// Tests that the media section is rejected if and only if the callee has no
590// local media track and has set offer_to_receive to 0, no matter which
591// direction the caller indicated in the offer.
592TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyRejected) {
Steve Anton22da89f2018-01-25 21:58:07593 if (IsUnifiedPlan() &&
594 offer_to_receive_ != RTCOfferAnswerOptions::kUndefined) {
595 // offer_to_receive_ is not implemented when creating answers with Unified
596 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 18:21:56597 return;
598 }
Steve Anton22da89f2018-01-25 21:58:07599
Steve Anton8d3444d2017-10-20 22:30:51600 auto caller = CreatePeerConnection();
601 caller->AddAudioTrack("a");
602
603 // Create the offer with an audio section and set its direction.
604 auto offer = caller->CreateOffer();
605 cricket::GetFirstAudioContentDescription(offer->description())
606 ->set_direction(offer_direction_);
607
608 auto callee = CreatePeerConnection();
609 if (send_media_) {
610 callee->AddAudioTrack("a");
611 }
612 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
613
614 // Create the answer according to the test parameters.
615 RTCOfferAnswerOptions options;
616 options.offer_to_receive_audio = offer_to_receive_;
617 auto answer = callee->CreateAnswer(options);
618
619 // The media section is rejected if and only if offer_to_receive is explicitly
620 // set to 0 and there is no media to send.
621 auto* audio_content = cricket::GetFirstAudioContent(answer->description());
622 ASSERT_TRUE(audio_content);
623 EXPECT_EQ((offer_to_receive_ == 0 && !send_media_), audio_content->rejected);
624}
625
626INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
627 PeerConnectionMediaAnswerDirectionTest,
Steve Antonad7bffc2018-01-22 18:21:56628 Combine(Values(SdpSemantics::kPlanB,
629 SdpSemantics::kUnifiedPlan),
630 Values(RtpTransceiverDirection::kInactive,
Steve Anton4e70a722017-11-28 22:57:10631 RtpTransceiverDirection::kSendOnly,
632 RtpTransceiverDirection::kRecvOnly,
633 RtpTransceiverDirection::kSendRecv),
Steve Anton8d3444d2017-10-20 22:30:51634 Bool(),
635 Values(-1, 0, 1)));
636
Steve Antonad7bffc2018-01-22 18:21:56637TEST_P(PeerConnectionMediaTest, OfferHasDifferentDirectionForAudioVideo) {
Steve Anton8d3444d2017-10-20 22:30:51638 auto caller = CreatePeerConnection();
639 caller->AddVideoTrack("v");
640
641 RTCOfferAnswerOptions options;
642 options.offer_to_receive_audio = 1;
643 options.offer_to_receive_video = 0;
644 auto offer = caller->CreateOffer(options);
645
Steve Anton4e70a722017-11-28 22:57:10646 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 18:21:56647 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 22:57:10648 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 18:21:56649 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 22:30:51650}
651
Steve Antonad7bffc2018-01-22 18:21:56652TEST_P(PeerConnectionMediaTest, AnswerHasDifferentDirectionsForAudioVideo) {
Steve Antonad7bffc2018-01-22 18:21:56653 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 21:58:07654 // offer_to_receive_ is not implemented when creating answers with Unified
655 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 18:21:56656 return;
657 }
658
Steve Anton8d3444d2017-10-20 22:30:51659 auto caller = CreatePeerConnectionWithAudioVideo();
660 auto callee = CreatePeerConnection();
661 callee->AddVideoTrack("v");
662
663 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
664
665 RTCOfferAnswerOptions options;
666 options.offer_to_receive_audio = 1;
667 options.offer_to_receive_video = 0;
668 auto answer = callee->CreateAnswer(options);
669
Steve Anton4e70a722017-11-28 22:57:10670 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 18:21:56671 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 22:57:10672 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 18:21:56673 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 22:30:51674}
675
676void AddComfortNoiseCodecsToSend(cricket::FakeMediaEngine* media_engine) {
677 const cricket::AudioCodec kComfortNoiseCodec8k(102, "CN", 8000, 0, 1);
678 const cricket::AudioCodec kComfortNoiseCodec16k(103, "CN", 16000, 0, 1);
679
680 auto codecs = media_engine->audio_send_codecs();
681 codecs.push_back(kComfortNoiseCodec8k);
682 codecs.push_back(kComfortNoiseCodec16k);
683 media_engine->SetAudioCodecs(codecs);
684}
685
686bool HasAnyComfortNoiseCodecs(const cricket::SessionDescription* desc) {
687 const auto* audio_desc = cricket::GetFirstAudioContentDescription(desc);
688 for (const auto& codec : audio_desc->codecs()) {
689 if (codec.name == "CN") {
690 return true;
691 }
692 }
693 return false;
694}
695
Steve Antonad7bffc2018-01-22 18:21:56696TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:51697 CreateOfferWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
698 auto caller = CreatePeerConnectionWithAudioVideo();
699 AddComfortNoiseCodecsToSend(caller->media_engine());
700
701 RTCOfferAnswerOptions options;
702 options.voice_activity_detection = false;
703 auto offer = caller->CreateOffer(options);
704
705 EXPECT_FALSE(HasAnyComfortNoiseCodecs(offer->description()));
706}
707
Steve Antonad7bffc2018-01-22 18:21:56708TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:51709 CreateAnswerWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
710 auto caller = CreatePeerConnectionWithAudioVideo();
711 AddComfortNoiseCodecsToSend(caller->media_engine());
712 auto callee = CreatePeerConnectionWithAudioVideo();
713 AddComfortNoiseCodecsToSend(callee->media_engine());
714
715 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
716
717 RTCOfferAnswerOptions options;
718 options.voice_activity_detection = false;
719 auto answer = callee->CreateAnswer(options);
720
721 EXPECT_FALSE(HasAnyComfortNoiseCodecs(answer->description()));
722}
723
724// The following test group verifies that we reject answers with invalid media
725// sections as per RFC 3264.
726
727class PeerConnectionMediaInvalidMediaTest
Steve Antonad7bffc2018-01-22 18:21:56728 : public PeerConnectionMediaBaseTest,
729 public ::testing::WithParamInterface<std::tuple<
730 SdpSemantics,
Steve Anton8d3444d2017-10-20 22:30:51731 std::tuple<std::string,
732 std::function<void(cricket::SessionDescription*)>,
Steve Antonad7bffc2018-01-22 18:21:56733 std::string>>> {
Steve Anton8d3444d2017-10-20 22:30:51734 protected:
Steve Antonad7bffc2018-01-22 18:21:56735 PeerConnectionMediaInvalidMediaTest()
736 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
737 auto param = std::get<1>(GetParam());
738 mutator_ = std::get<1>(param);
739 expected_error_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 22:30:51740 }
741
742 std::function<void(cricket::SessionDescription*)> mutator_;
743 std::string expected_error_;
744};
745
746TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetRemoteAnswer) {
747 auto caller = CreatePeerConnectionWithAudioVideo();
748 auto callee = CreatePeerConnectionWithAudioVideo();
749
750 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
751
752 auto answer = callee->CreateAnswer();
753 mutator_(answer->description());
754
755 std::string error;
756 ASSERT_FALSE(caller->SetRemoteDescription(std::move(answer), &error));
757 EXPECT_EQ("Failed to set remote answer sdp: " + expected_error_, error);
758}
759
760TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetLocalAnswer) {
761 auto caller = CreatePeerConnectionWithAudioVideo();
762 auto callee = CreatePeerConnectionWithAudioVideo();
763
764 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
765
766 auto answer = callee->CreateAnswer();
767 mutator_(answer->description());
768
769 std::string error;
770 ASSERT_FALSE(callee->SetLocalDescription(std::move(answer), &error));
771 EXPECT_EQ("Failed to set local answer sdp: " + expected_error_, error);
772}
773
774void RemoveVideoContent(cricket::SessionDescription* desc) {
775 auto content_name = cricket::GetFirstVideoContent(desc)->name;
776 desc->RemoveContentByName(content_name);
777 desc->RemoveTransportInfoByName(content_name);
778}
779
780void RenameVideoContent(cricket::SessionDescription* desc) {
781 auto* video_content = cricket::GetFirstVideoContent(desc);
782 auto* transport_info = desc->GetTransportInfoByName(video_content->name);
783 video_content->name = "video_renamed";
784 transport_info->content_name = video_content->name;
785}
786
787void ReverseMediaContent(cricket::SessionDescription* desc) {
788 std::reverse(desc->contents().begin(), desc->contents().end());
789 std::reverse(desc->transport_infos().begin(), desc->transport_infos().end());
790}
791
792void ChangeMediaTypeAudioToVideo(cricket::SessionDescription* desc) {
Steve Antonad7bffc2018-01-22 18:21:56793 std::string audio_mid = cricket::GetFirstAudioContent(desc)->name;
794 desc->RemoveContentByName(audio_mid);
795 auto* video_content = cricket::GetFirstVideoContent(desc);
796 desc->AddContent(audio_mid, video_content->type,
Steve Antonb1c1de12017-12-21 23:14:30797 video_content->media_description()->Copy());
Steve Anton8d3444d2017-10-20 22:30:51798}
799
800constexpr char kMLinesOutOfOrder[] =
801 "The order of m-lines in answer doesn't match order in offer. Rejecting "
802 "answer.";
803
804INSTANTIATE_TEST_CASE_P(
805 PeerConnectionMediaTest,
806 PeerConnectionMediaInvalidMediaTest,
Steve Antonad7bffc2018-01-22 18:21:56807 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
808 Values(std::make_tuple("remove video",
809 RemoveVideoContent,
810 kMLinesOutOfOrder),
811 std::make_tuple("rename video",
812 RenameVideoContent,
813 kMLinesOutOfOrder),
814 std::make_tuple("reverse media sections",
815 ReverseMediaContent,
816 kMLinesOutOfOrder),
817 std::make_tuple("change audio type to video type",
818 ChangeMediaTypeAudioToVideo,
819 kMLinesOutOfOrder))));
Steve Anton8d3444d2017-10-20 22:30:51820
821// Test that the correct media engine send/recv streams are created when doing
822// a series of offer/answers where audio/video are both sent, then audio is
823// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 18:21:56824TEST_P(PeerConnectionMediaTest, TestAVOfferWithAudioOnlyAnswer) {
Steve Antonad7bffc2018-01-22 18:21:56825 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 21:58:07826 // offer_to_receive_ is not implemented when creating answers with Unified
827 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 18:21:56828 return;
829 }
830
Steve Anton8d3444d2017-10-20 22:30:51831 RTCOfferAnswerOptions options_reject_video;
832 options_reject_video.offer_to_receive_audio =
833 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
834 options_reject_video.offer_to_receive_video = 0;
835
836 auto caller = CreatePeerConnection();
837 caller->AddAudioTrack("a");
838 caller->AddVideoTrack("v");
839 auto callee = CreatePeerConnection();
840
841 // Caller initially offers to send/recv audio and video.
842 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
843 // Callee accepts the audio as recv only but rejects the video.
844 ASSERT_TRUE(caller->SetRemoteDescription(
845 callee->CreateAnswerAndSetAsLocal(options_reject_video)));
846
847 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
848 ASSERT_TRUE(caller_voice);
849 EXPECT_EQ(0u, caller_voice->recv_streams().size());
850 EXPECT_EQ(1u, caller_voice->send_streams().size());
851 auto caller_video = caller->media_engine()->GetVideoChannel(0);
852 EXPECT_FALSE(caller_video);
853
854 // Callee adds its own audio/video stream and offers to receive audio/video
855 // too.
856 callee->AddAudioTrack("a");
857 auto callee_video_track = callee->AddVideoTrack("v");
858 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
859 ASSERT_TRUE(
860 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
861
862 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
863 ASSERT_TRUE(callee_voice);
864 EXPECT_EQ(1u, callee_voice->recv_streams().size());
865 EXPECT_EQ(1u, callee_voice->send_streams().size());
866 auto callee_video = callee->media_engine()->GetVideoChannel(0);
867 ASSERT_TRUE(callee_video);
868 EXPECT_EQ(1u, callee_video->recv_streams().size());
869 EXPECT_EQ(1u, callee_video->send_streams().size());
870
871 // Callee removes video but keeps audio and rejects the video once again.
872 callee->pc()->RemoveTrack(callee_video_track);
873 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
874 ASSERT_TRUE(
875 callee->SetLocalDescription(callee->CreateAnswer(options_reject_video)));
876
877 callee_voice = callee->media_engine()->GetVoiceChannel(0);
878 ASSERT_TRUE(callee_voice);
879 EXPECT_EQ(1u, callee_voice->recv_streams().size());
880 EXPECT_EQ(1u, callee_voice->send_streams().size());
881 callee_video = callee->media_engine()->GetVideoChannel(0);
882 EXPECT_FALSE(callee_video);
883}
884
885// Test that the correct media engine send/recv streams are created when doing
886// a series of offer/answers where audio/video are both sent, then video is
887// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 18:21:56888TEST_P(PeerConnectionMediaTest, TestAVOfferWithVideoOnlyAnswer) {
Steve Antonad7bffc2018-01-22 18:21:56889 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 21:58:07890 // offer_to_receive_ is not implemented when creating answers with Unified
891 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 18:21:56892 return;
893 }
894
Steve Anton8d3444d2017-10-20 22:30:51895 // Disable the bundling here. If the media is bundled on audio
896 // transport, then we can't reject the audio because switching the bundled
897 // transport is not currently supported.
898 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6704)
899 RTCOfferAnswerOptions options_no_bundle;
900 options_no_bundle.use_rtp_mux = false;
901 RTCOfferAnswerOptions options_reject_audio = options_no_bundle;
902 options_reject_audio.offer_to_receive_audio = 0;
903 options_reject_audio.offer_to_receive_video =
904 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia;
905
906 auto caller = CreatePeerConnection();
907 caller->AddAudioTrack("a");
908 caller->AddVideoTrack("v");
909 auto callee = CreatePeerConnection();
910
911 // Caller initially offers to send/recv audio and video.
912 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
913 // Callee accepts the video as recv only but rejects the audio.
914 ASSERT_TRUE(caller->SetRemoteDescription(
915 callee->CreateAnswerAndSetAsLocal(options_reject_audio)));
916
917 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
918 EXPECT_FALSE(caller_voice);
919 auto caller_video = caller->media_engine()->GetVideoChannel(0);
920 ASSERT_TRUE(caller_video);
921 EXPECT_EQ(0u, caller_video->recv_streams().size());
922 EXPECT_EQ(1u, caller_video->send_streams().size());
923
924 // Callee adds its own audio/video stream and offers to receive audio/video
925 // too.
926 auto callee_audio_track = callee->AddAudioTrack("a");
927 callee->AddVideoTrack("v");
928 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
929 ASSERT_TRUE(caller->SetRemoteDescription(
930 callee->CreateAnswerAndSetAsLocal(options_no_bundle)));
931
932 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
933 ASSERT_TRUE(callee_voice);
934 EXPECT_EQ(1u, callee_voice->recv_streams().size());
935 EXPECT_EQ(1u, callee_voice->send_streams().size());
936 auto callee_video = callee->media_engine()->GetVideoChannel(0);
937 ASSERT_TRUE(callee_video);
938 EXPECT_EQ(1u, callee_video->recv_streams().size());
939 EXPECT_EQ(1u, callee_video->send_streams().size());
940
941 // Callee removes audio but keeps video and rejects the audio once again.
942 callee->pc()->RemoveTrack(callee_audio_track);
943 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
944 ASSERT_TRUE(
945 callee->SetLocalDescription(callee->CreateAnswer(options_reject_audio)));
946
947 callee_voice = callee->media_engine()->GetVoiceChannel(0);
948 EXPECT_FALSE(callee_voice);
949 callee_video = callee->media_engine()->GetVideoChannel(0);
950 ASSERT_TRUE(callee_video);
951 EXPECT_EQ(1u, callee_video->recv_streams().size());
952 EXPECT_EQ(1u, callee_video->send_streams().size());
953}
954
955// Tests that if the underlying video encoder fails to be initialized (signaled
956// by failing to set send codecs), the PeerConnection signals the error to the
957// client.
Steve Antonad7bffc2018-01-22 18:21:56958TEST_P(PeerConnectionMediaTest, MediaEngineErrorPropagatedToClients) {
Steve Anton8d3444d2017-10-20 22:30:51959 auto caller = CreatePeerConnectionWithAudioVideo();
960 auto callee = CreatePeerConnectionWithAudioVideo();
961
962 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
963
964 auto video_channel = caller->media_engine()->GetVideoChannel(0);
965 video_channel->set_fail_set_send_codecs(true);
966
967 std::string error;
968 ASSERT_FALSE(caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(),
969 &error));
970 EXPECT_EQ(
Steve Anton80dd7b52018-02-17 01:08:42971 "Failed to set remote answer sdp: Failed to set remote video description "
972 "send parameters.",
Steve Anton8d3444d2017-10-20 22:30:51973 error);
974}
975
976// Tests that if the underlying video encoder fails once then subsequent
977// attempts at setting the local/remote description will also fail, even if
978// SetSendCodecs no longer fails.
Steve Antonad7bffc2018-01-22 18:21:56979TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:51980 FailToApplyDescriptionIfVideoEncoderHasEverFailed) {
981 auto caller = CreatePeerConnectionWithAudioVideo();
982 auto callee = CreatePeerConnectionWithAudioVideo();
983
984 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
985
986 auto video_channel = caller->media_engine()->GetVideoChannel(0);
987 video_channel->set_fail_set_send_codecs(true);
988
989 EXPECT_FALSE(
990 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
991
992 video_channel->set_fail_set_send_codecs(false);
993
994 EXPECT_FALSE(caller->SetRemoteDescription(callee->CreateAnswer()));
995 EXPECT_FALSE(caller->SetLocalDescription(caller->CreateOffer()));
996}
997
998void RenameContent(cricket::SessionDescription* desc,
Steve Antonad7bffc2018-01-22 18:21:56999 cricket::MediaType media_type,
Steve Anton8d3444d2017-10-20 22:30:511000 const std::string& new_name) {
Steve Antonad7bffc2018-01-22 18:21:561001 auto* content = cricket::GetFirstMediaContent(desc, media_type);
Steve Anton8d3444d2017-10-20 22:30:511002 RTC_DCHECK(content);
Steve Antonad7bffc2018-01-22 18:21:561003 std::string old_name = content->name;
Steve Anton8d3444d2017-10-20 22:30:511004 content->name = new_name;
1005 auto* transport = desc->GetTransportInfoByName(old_name);
1006 RTC_DCHECK(transport);
1007 transport->content_name = new_name;
Zhi Huangd2248f82018-04-10 21:41:031008
1009 // Rename the content name in the BUNDLE group.
1010 cricket::ContentGroup new_bundle_group =
1011 *desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1012 new_bundle_group.RemoveContentName(old_name);
1013 new_bundle_group.AddContentName(new_name);
1014 desc->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
1015 desc->AddGroup(new_bundle_group);
Steve Anton8d3444d2017-10-20 22:30:511016}
1017
1018// Tests that an answer responds with the same MIDs as the offer.
Steve Antonad7bffc2018-01-22 18:21:561019TEST_P(PeerConnectionMediaTest, AnswerHasSameMidsAsOffer) {
Zhi Huang365381f2018-04-13 23:44:341020 const std::string kAudioMid = "notdefault1";
1021 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 22:30:511022
1023 auto caller = CreatePeerConnectionWithAudioVideo();
1024 auto callee = CreatePeerConnectionWithAudioVideo();
1025
1026 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 18:21:561027 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
1028 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 22:30:511029 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1030
1031 auto answer = callee->CreateAnswer();
1032 EXPECT_EQ(kAudioMid,
1033 cricket::GetFirstAudioContent(answer->description())->name);
1034 EXPECT_EQ(kVideoMid,
1035 cricket::GetFirstVideoContent(answer->description())->name);
1036}
1037
1038// Test that if the callee creates a re-offer, the MIDs are the same as the
1039// original offer.
Steve Antonad7bffc2018-01-22 18:21:561040TEST_P(PeerConnectionMediaTest, ReOfferHasSameMidsAsFirstOffer) {
Zhi Huang365381f2018-04-13 23:44:341041 const std::string kAudioMid = "notdefault1";
1042 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 22:30:511043
1044 auto caller = CreatePeerConnectionWithAudioVideo();
1045 auto callee = CreatePeerConnectionWithAudioVideo();
1046
1047 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 18:21:561048 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
1049 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 22:30:511050 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1051 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1052
1053 auto reoffer = callee->CreateOffer();
1054 EXPECT_EQ(kAudioMid,
1055 cricket::GetFirstAudioContent(reoffer->description())->name);
1056 EXPECT_EQ(kVideoMid,
1057 cricket::GetFirstVideoContent(reoffer->description())->name);
1058}
1059
Steve Antonad7bffc2018-01-22 18:21:561060TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 22:30:511061 CombinedAudioVideoBweConfigPropagatedToMediaEngine) {
1062 RTCConfiguration config;
1063 config.combined_audio_video_bwe.emplace(true);
1064 auto caller = CreatePeerConnectionWithAudioVideo(config);
1065
1066 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1067
1068 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1069 ASSERT_TRUE(caller_voice);
1070 const cricket::AudioOptions& audio_options = caller_voice->options();
1071 EXPECT_EQ(config.combined_audio_video_bwe,
1072 audio_options.combined_audio_video_bwe);
1073}
1074
Steve Antonad7bffc2018-01-22 18:21:561075INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
1076 PeerConnectionMediaTest,
1077 Values(SdpSemantics::kPlanB,
1078 SdpSemantics::kUnifiedPlan));
1079
Steve Anton8d3444d2017-10-20 22:30:511080} // namespace webrtc