blob: ca036568ffdf9d1c8a02ec46f2b610a6174842c2 [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
Steve Anton64b626b2019-01-28 17:25:26 -080017#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/call/call_factory_interface.h"
Anton Sukhanov98a462c2018-10-17 13:15:42 -070019#include "api/test/fake_media_transport.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070020#include "logging/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/base/fake_media_engine.h"
22#include "p2p/base/fake_port_allocator.h"
23#include "pc/media_session.h"
24#include "pc/peer_connection_wrapper.h"
25#include "pc/rtp_media_utils.h"
26#include "pc/sdp_utils.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070027#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080028#include "pc/test/android_test_initializer.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070029#endif
Karl Wiberg918f50c2018-07-05 11:40:33 +020030#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/test/fake_rtc_certificate_generator.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070032#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/virtual_socket_server.h"
Steve Anton8d3444d2017-10-20 15:30:51 -070034#include "test/gmock.h"
35
36namespace webrtc {
37
38using cricket::FakeMediaEngine;
39using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
40using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
41using ::testing::Bool;
42using ::testing::Combine;
43using ::testing::Values;
44using ::testing::ElementsAre;
45
46class PeerConnectionWrapperForMediaTest : public PeerConnectionWrapper {
47 public:
48 using PeerConnectionWrapper::PeerConnectionWrapper;
49
50 FakeMediaEngine* media_engine() { return media_engine_; }
51 void set_media_engine(FakeMediaEngine* media_engine) {
52 media_engine_ = media_engine;
53 }
54
55 private:
56 FakeMediaEngine* media_engine_;
57};
58
Steve Antonad7bffc2018-01-22 10:21:56 -080059class PeerConnectionMediaBaseTest : public ::testing::Test {
Steve Anton8d3444d2017-10-20 15:30:51 -070060 protected:
61 typedef std::unique_ptr<PeerConnectionWrapperForMediaTest> WrapperPtr;
62
Steve Antonad7bffc2018-01-22 10:21:56 -080063 explicit PeerConnectionMediaBaseTest(SdpSemantics sdp_semantics)
64 : vss_(new rtc::VirtualSocketServer()),
65 main_(vss_.get()),
66 sdp_semantics_(sdp_semantics) {
Steve Anton8d3444d2017-10-20 15:30:51 -070067#ifdef WEBRTC_ANDROID
68 InitializeAndroidObjects();
69#endif
70 }
71
72 WrapperPtr CreatePeerConnection() {
73 return CreatePeerConnection(RTCConfiguration());
74 }
75
Anton Sukhanov98a462c2018-10-17 13:15:42 -070076 // Creates PeerConnectionFactory and PeerConnection for given configuration.
77 // Note that PeerConnectionFactory is created with MediaTransportFactory,
78 // because some tests pass config.use_media_transport = true.
Steve Anton8d3444d2017-10-20 15:30:51 -070079 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020080 auto media_engine = absl::make_unique<FakeMediaEngine>();
Steve Anton8d3444d2017-10-20 15:30:51 -070081 auto* media_engine_ptr = media_engine.get();
Anton Sukhanov98a462c2018-10-17 13:15:42 -070082
83 PeerConnectionFactoryDependencies factory_dependencies;
84
85 factory_dependencies.network_thread = rtc::Thread::Current();
86 factory_dependencies.worker_thread = rtc::Thread::Current();
87 factory_dependencies.signaling_thread = rtc::Thread::Current();
88 factory_dependencies.media_engine = std::move(media_engine);
89 factory_dependencies.call_factory = CreateCallFactory();
90 factory_dependencies.event_log_factory = CreateRtcEventLogFactory();
91 factory_dependencies.media_transport_factory =
92 absl::make_unique<FakeMediaTransportFactory>();
93
94 auto pc_factory =
95 CreateModularPeerConnectionFactory(std::move(factory_dependencies));
Steve Anton8d3444d2017-10-20 15:30:51 -070096
Karl Wiberg918f50c2018-07-05 11:40:33 +020097 auto fake_port_allocator = absl::make_unique<cricket::FakePortAllocator>(
Steve Anton8d3444d2017-10-20 15:30:51 -070098 rtc::Thread::Current(), nullptr);
Karl Wiberg918f50c2018-07-05 11:40:33 +020099 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Steve Antonad7bffc2018-01-22 10:21:56 -0800100 auto modified_config = config;
101 modified_config.sdp_semantics = sdp_semantics_;
102 auto pc = pc_factory->CreatePeerConnection(modified_config,
103 std::move(fake_port_allocator),
104 nullptr, observer.get());
Steve Anton8d3444d2017-10-20 15:30:51 -0700105 if (!pc) {
106 return nullptr;
107 }
108
Yves Gerey4e933292018-10-31 15:36:05 +0100109 observer->SetPeerConnectionInterface(pc.get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200110 auto wrapper = absl::make_unique<PeerConnectionWrapperForMediaTest>(
Steve Anton8d3444d2017-10-20 15:30:51 -0700111 pc_factory, pc, std::move(observer));
112 wrapper->set_media_engine(media_engine_ptr);
113 return wrapper;
114 }
115
116 // Accepts the same arguments as CreatePeerConnection and adds default audio
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -0800117 // track (but no video).
118 template <typename... Args>
119 WrapperPtr CreatePeerConnectionWithAudio(Args&&... args) {
120 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
121 if (!wrapper) {
122 return nullptr;
123 }
124 wrapper->AddAudioTrack("a");
125 return wrapper;
126 }
127
128 // Accepts the same arguments as CreatePeerConnection and adds default audio
Steve Anton8d3444d2017-10-20 15:30:51 -0700129 // and video tracks.
130 template <typename... Args>
131 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
132 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
133 if (!wrapper) {
134 return nullptr;
135 }
136 wrapper->AddAudioTrack("a");
137 wrapper->AddVideoTrack("v");
138 return wrapper;
139 }
140
Steve Anton4e70a722017-11-28 14:57:10 -0800141 RtpTransceiverDirection GetMediaContentDirection(
Steve Anton8d3444d2017-10-20 15:30:51 -0700142 const SessionDescriptionInterface* sdesc,
Steve Antonad7bffc2018-01-22 10:21:56 -0800143 cricket::MediaType media_type) {
144 auto* content =
145 cricket::GetFirstMediaContent(sdesc->description(), media_type);
146 RTC_DCHECK(content);
147 return content->media_description()->direction();
148 }
149
150 bool IsUnifiedPlan() const {
151 return sdp_semantics_ == SdpSemantics::kUnifiedPlan;
Steve Anton8d3444d2017-10-20 15:30:51 -0700152 }
153
154 std::unique_ptr<rtc::VirtualSocketServer> vss_;
155 rtc::AutoSocketServerThread main_;
Steve Antonad7bffc2018-01-22 10:21:56 -0800156 const SdpSemantics sdp_semantics_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700157};
158
Steve Antonad7bffc2018-01-22 10:21:56 -0800159class PeerConnectionMediaTest
160 : public PeerConnectionMediaBaseTest,
161 public ::testing::WithParamInterface<SdpSemantics> {
162 protected:
163 PeerConnectionMediaTest() : PeerConnectionMediaBaseTest(GetParam()) {}
164};
165
166class PeerConnectionMediaTestUnifiedPlan : public PeerConnectionMediaBaseTest {
167 protected:
168 PeerConnectionMediaTestUnifiedPlan()
169 : PeerConnectionMediaBaseTest(SdpSemantics::kUnifiedPlan) {}
170};
171
172class PeerConnectionMediaTestPlanB : public PeerConnectionMediaBaseTest {
173 protected:
174 PeerConnectionMediaTestPlanB()
175 : PeerConnectionMediaBaseTest(SdpSemantics::kPlanB) {}
176};
177
178TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700179 FailToSetRemoteDescriptionIfCreateMediaChannelFails) {
180 auto caller = CreatePeerConnectionWithAudioVideo();
181 auto callee = CreatePeerConnectionWithAudioVideo();
182 callee->media_engine()->set_fail_create_channel(true);
183
184 std::string error;
185 ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
Steve Antonad7bffc2018-01-22 10:21:56 -0800186 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
187 "Failed to set remote offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 15:30:51 -0700188}
189
Steve Antonad7bffc2018-01-22 10:21:56 -0800190TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700191 FailToSetLocalDescriptionIfCreateMediaChannelFails) {
192 auto caller = CreatePeerConnectionWithAudioVideo();
193 caller->media_engine()->set_fail_create_channel(true);
194
195 std::string error;
196 ASSERT_FALSE(caller->SetLocalDescription(caller->CreateOffer(), &error));
Steve Antonad7bffc2018-01-22 10:21:56 -0800197 EXPECT_PRED_FORMAT2(AssertStartsWith, error,
198 "Failed to set local offer sdp: Failed to create");
Steve Anton8d3444d2017-10-20 15:30:51 -0700199}
200
201std::vector<std::string> GetIds(
202 const std::vector<cricket::StreamParams>& streams) {
203 std::vector<std::string> ids;
204 for (const auto& stream : streams) {
205 ids.push_back(stream.id);
206 }
207 return ids;
208}
209
210// Test that exchanging an offer and answer with each side having an audio and
211// video stream creates the appropriate send/recv streams in the underlying
212// media engine on both sides.
Steve Antonad7bffc2018-01-22 10:21:56 -0800213TEST_P(PeerConnectionMediaTest, AudioVideoOfferAnswerCreateSendRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700214 const std::string kCallerAudioId = "caller_a";
215 const std::string kCallerVideoId = "caller_v";
216 const std::string kCalleeAudioId = "callee_a";
217 const std::string kCalleeVideoId = "callee_v";
218
219 auto caller = CreatePeerConnection();
220 caller->AddAudioTrack(kCallerAudioId);
221 caller->AddVideoTrack(kCallerVideoId);
222
223 auto callee = CreatePeerConnection();
224 callee->AddAudioTrack(kCalleeAudioId);
225 callee->AddVideoTrack(kCalleeVideoId);
226
227 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
228 ASSERT_TRUE(
229 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
230
231 auto* caller_voice = caller->media_engine()->GetVoiceChannel(0);
232 EXPECT_THAT(GetIds(caller_voice->recv_streams()),
233 ElementsAre(kCalleeAudioId));
234 EXPECT_THAT(GetIds(caller_voice->send_streams()),
235 ElementsAre(kCallerAudioId));
236
237 auto* caller_video = caller->media_engine()->GetVideoChannel(0);
238 EXPECT_THAT(GetIds(caller_video->recv_streams()),
239 ElementsAre(kCalleeVideoId));
240 EXPECT_THAT(GetIds(caller_video->send_streams()),
241 ElementsAre(kCallerVideoId));
242
243 auto* callee_voice = callee->media_engine()->GetVoiceChannel(0);
244 EXPECT_THAT(GetIds(callee_voice->recv_streams()),
245 ElementsAre(kCallerAudioId));
246 EXPECT_THAT(GetIds(callee_voice->send_streams()),
247 ElementsAre(kCalleeAudioId));
248
249 auto* callee_video = callee->media_engine()->GetVideoChannel(0);
250 EXPECT_THAT(GetIds(callee_video->recv_streams()),
251 ElementsAre(kCallerVideoId));
252 EXPECT_THAT(GetIds(callee_video->send_streams()),
253 ElementsAre(kCalleeVideoId));
254}
255
Steve Antonad7bffc2018-01-22 10:21:56 -0800256// Test that stopping the caller transceivers causes the media channels on the
257// callee to be destroyed after calling SetRemoteDescription on the generated
258// offer.
259// See next test for equivalent behavior with Plan B semantics.
260TEST_F(PeerConnectionMediaTestUnifiedPlan,
261 StoppedRemoteTransceiversRemovesMediaChannels) {
262 auto caller = CreatePeerConnectionWithAudioVideo();
263 auto callee = CreatePeerConnection();
264
265 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
266
267 // Stop both audio and video transceivers on the caller.
268 auto transceivers = caller->pc()->GetTransceivers();
269 ASSERT_EQ(2u, transceivers.size());
270 transceivers[0]->Stop();
271 transceivers[1]->Stop();
272
273 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
274
275 ASSERT_FALSE(callee->media_engine()->GetVoiceChannel(0));
276 ASSERT_FALSE(callee->media_engine()->GetVideoChannel(0));
277}
278
Steve Anton8d3444d2017-10-20 15:30:51 -0700279// Test that removing streams from a subsequent offer causes the receive streams
280// on the callee to be removed.
Steve Antonad7bffc2018-01-22 10:21:56 -0800281// See previous test for equivalent behavior with Unified Plan semantics.
282TEST_F(PeerConnectionMediaTestPlanB, EmptyRemoteOfferRemovesRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700283 auto caller = CreatePeerConnection();
284 auto caller_audio_track = caller->AddAudioTrack("a");
285 auto caller_video_track = caller->AddVideoTrack("v");
286 auto callee = CreatePeerConnectionWithAudioVideo();
287
Steve Antonad7bffc2018-01-22 10:21:56 -0800288 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700289
290 // Remove both tracks from caller.
291 caller->pc()->RemoveTrack(caller_audio_track);
292 caller->pc()->RemoveTrack(caller_video_track);
293
Steve Antonad7bffc2018-01-22 10:21:56 -0800294 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700295
296 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 10:21:56 -0800297 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 15:30:51 -0700298 EXPECT_EQ(1u, callee_voice->send_streams().size());
299 EXPECT_EQ(0u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 15:30:51 -0700300 EXPECT_EQ(1u, callee_video->send_streams().size());
301 EXPECT_EQ(0u, callee_video->recv_streams().size());
302}
303
Jonas Orelandfc1acd22018-08-24 10:58:37 +0200304// Test enabling of simulcast with Plan B semantics.
305// This test creating an offer.
306TEST_F(PeerConnectionMediaTestPlanB, SimulcastOffer) {
307 auto caller = CreatePeerConnection();
308 auto caller_video_track = caller->AddVideoTrack("v");
309 RTCOfferAnswerOptions options;
310 options.num_simulcast_layers = 3;
311 auto offer = caller->CreateOffer(options);
312 auto* description = cricket::GetFirstMediaContent(
313 offer->description(),
314 cricket::MEDIA_TYPE_VIDEO)->media_description();
315 ASSERT_EQ(1u, description->streams().size());
316 ASSERT_TRUE(description->streams()[0].get_ssrc_group("SIM"));
317 EXPECT_EQ(3u, description->streams()[0].get_ssrc_group("SIM")->ssrcs.size());
318
319 // Check that it actually creates simulcast aswell.
320 caller->SetLocalDescription(std::move(offer));
321 auto senders = caller->pc()->GetSenders();
322 ASSERT_EQ(1u, senders.size());
323 EXPECT_EQ(cricket::MediaType::MEDIA_TYPE_VIDEO, senders[0]->media_type());
324 EXPECT_EQ(3u, senders[0]->GetParameters().encodings.size());
325}
326
327// Test enabling of simulcast with Plan B semantics.
328// This test creating an answer.
329TEST_F(PeerConnectionMediaTestPlanB, SimulcastAnswer) {
330 auto caller = CreatePeerConnection();
331 caller->AddVideoTrack("v0");
332 auto offer = caller->CreateOffer();
333 auto callee = CreatePeerConnection();
334 auto callee_video_track = callee->AddVideoTrack("v1");
335 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
336 RTCOfferAnswerOptions options;
337 options.num_simulcast_layers = 3;
338 auto answer = callee->CreateAnswer(options);
339 auto* description = cricket::GetFirstMediaContent(
340 answer->description(),
341 cricket::MEDIA_TYPE_VIDEO)->media_description();
342 ASSERT_EQ(1u, description->streams().size());
343 ASSERT_TRUE(description->streams()[0].get_ssrc_group("SIM"));
344 EXPECT_EQ(3u, description->streams()[0].get_ssrc_group("SIM")->ssrcs.size());
345
346 // Check that it actually creates simulcast aswell.
347 callee->SetLocalDescription(std::move(answer));
348 auto senders = callee->pc()->GetSenders();
349 ASSERT_EQ(1u, senders.size());
350 EXPECT_EQ(cricket::MediaType::MEDIA_TYPE_VIDEO, senders[0]->media_type());
351 EXPECT_EQ(3u, senders[0]->GetParameters().encodings.size());
352}
353
Steve Antonad7bffc2018-01-22 10:21:56 -0800354// Test that stopping the callee transceivers causes the media channels to be
355// destroyed on the callee after calling SetLocalDescription on the local
356// answer.
357// See next test for equivalent behavior with Plan B semantics.
358TEST_F(PeerConnectionMediaTestUnifiedPlan,
359 StoppedLocalTransceiversRemovesMediaChannels) {
360 auto caller = CreatePeerConnectionWithAudioVideo();
361 auto callee = CreatePeerConnectionWithAudioVideo();
362
363 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
364
365 // Stop both audio and video transceivers on the callee.
366 auto transceivers = callee->pc()->GetTransceivers();
367 ASSERT_EQ(2u, transceivers.size());
368 transceivers[0]->Stop();
369 transceivers[1]->Stop();
370
371 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
372
373 EXPECT_FALSE(callee->media_engine()->GetVoiceChannel(0));
374 EXPECT_FALSE(callee->media_engine()->GetVideoChannel(0));
375}
376
Steve Anton8d3444d2017-10-20 15:30:51 -0700377// Test that removing streams from a subsequent answer causes the send streams
378// on the callee to be removed when applied locally.
Steve Antonad7bffc2018-01-22 10:21:56 -0800379// See previous test for equivalent behavior with Unified Plan semantics.
380TEST_F(PeerConnectionMediaTestPlanB, EmptyLocalAnswerRemovesSendStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700381 auto caller = CreatePeerConnectionWithAudioVideo();
382 auto callee = CreatePeerConnection();
383 auto callee_audio_track = callee->AddAudioTrack("a");
384 auto callee_video_track = callee->AddVideoTrack("v");
385
Steve Antonad7bffc2018-01-22 10:21:56 -0800386 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700387
388 // Remove both tracks from callee.
389 callee->pc()->RemoveTrack(callee_audio_track);
390 callee->pc()->RemoveTrack(callee_video_track);
391
Steve Antonad7bffc2018-01-22 10:21:56 -0800392 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700393
394 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Antonad7bffc2018-01-22 10:21:56 -0800395 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton8d3444d2017-10-20 15:30:51 -0700396 EXPECT_EQ(0u, callee_voice->send_streams().size());
397 EXPECT_EQ(1u, callee_voice->recv_streams().size());
Steve Anton8d3444d2017-10-20 15:30:51 -0700398 EXPECT_EQ(0u, callee_video->send_streams().size());
399 EXPECT_EQ(1u, callee_video->recv_streams().size());
400}
401
402// Test that a new stream in a subsequent offer causes a new receive stream to
403// be created on the callee.
Steve Antonad7bffc2018-01-22 10:21:56 -0800404TEST_P(PeerConnectionMediaTest, NewStreamInRemoteOfferAddsRecvStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700405 auto caller = CreatePeerConnectionWithAudioVideo();
406 auto callee = CreatePeerConnection();
407
Steve Antonad7bffc2018-01-22 10:21:56 -0800408 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700409
410 // Add second set of tracks to the caller.
411 caller->AddAudioTrack("a2");
412 caller->AddVideoTrack("v2");
413
Steve Antonad7bffc2018-01-22 10:21:56 -0800414 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Steve Anton8d3444d2017-10-20 15:30:51 -0700415
Steve Antonad7bffc2018-01-22 10:21:56 -0800416 auto a1 = callee->media_engine()->GetVoiceChannel(0);
417 auto a2 = callee->media_engine()->GetVoiceChannel(1);
418 auto v1 = callee->media_engine()->GetVideoChannel(0);
419 auto v2 = callee->media_engine()->GetVideoChannel(1);
420 if (IsUnifiedPlan()) {
421 ASSERT_TRUE(a1);
422 EXPECT_EQ(1u, a1->recv_streams().size());
423 ASSERT_TRUE(a2);
424 EXPECT_EQ(1u, a2->recv_streams().size());
425 ASSERT_TRUE(v1);
426 EXPECT_EQ(1u, v1->recv_streams().size());
427 ASSERT_TRUE(v2);
428 EXPECT_EQ(1u, v2->recv_streams().size());
429 } else {
430 ASSERT_TRUE(a1);
431 EXPECT_EQ(2u, a1->recv_streams().size());
432 ASSERT_FALSE(a2);
433 ASSERT_TRUE(v1);
434 EXPECT_EQ(2u, v1->recv_streams().size());
435 ASSERT_FALSE(v2);
436 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700437}
438
439// Test that a new stream in a subsequent answer causes a new send stream to be
440// created on the callee when added locally.
Steve Antonad7bffc2018-01-22 10:21:56 -0800441TEST_P(PeerConnectionMediaTest, NewStreamInLocalAnswerAddsSendStreams) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700442 auto caller = CreatePeerConnection();
443 auto callee = CreatePeerConnectionWithAudioVideo();
444
Steve Anton22da89f2018-01-25 13:58:07 -0800445 RTCOfferAnswerOptions offer_options;
446 offer_options.offer_to_receive_audio =
Steve Anton8d3444d2017-10-20 15:30:51 -0700447 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 13:58:07 -0800448 offer_options.offer_to_receive_video =
Steve Anton8d3444d2017-10-20 15:30:51 -0700449 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
Steve Anton22da89f2018-01-25 13:58:07 -0800450 RTCOfferAnswerOptions answer_options;
Steve Anton8d3444d2017-10-20 15:30:51 -0700451
Steve Anton22da89f2018-01-25 13:58:07 -0800452 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
453 answer_options));
Steve Anton8d3444d2017-10-20 15:30:51 -0700454
455 // Add second set of tracks to the callee.
456 callee->AddAudioTrack("a2");
457 callee->AddVideoTrack("v2");
458
Steve Anton22da89f2018-01-25 13:58:07 -0800459 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get(), offer_options,
460 answer_options));
Steve Anton8d3444d2017-10-20 15:30:51 -0700461
462 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
Steve Anton22da89f2018-01-25 13:58:07 -0800463 ASSERT_TRUE(callee_voice);
Steve Anton8d3444d2017-10-20 15:30:51 -0700464 auto callee_video = callee->media_engine()->GetVideoChannel(0);
Steve Anton22da89f2018-01-25 13:58:07 -0800465 ASSERT_TRUE(callee_video);
466
467 if (IsUnifiedPlan()) {
468 EXPECT_EQ(1u, callee_voice->send_streams().size());
469 EXPECT_EQ(1u, callee_video->send_streams().size());
470 } else {
471 EXPECT_EQ(2u, callee_voice->send_streams().size());
472 EXPECT_EQ(2u, callee_video->send_streams().size());
473 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700474}
475
476// A PeerConnection with no local streams and no explicit answer constraints
477// should not reject any offered media sections.
Steve Antonad7bffc2018-01-22 10:21:56 -0800478TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700479 CreateAnswerWithNoStreamsAndDefaultOptionsDoesNotReject) {
480 auto caller = CreatePeerConnectionWithAudioVideo();
481 auto callee = CreatePeerConnection();
482 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
483 auto answer = callee->CreateAnswer();
484
485 const auto* audio_content =
486 cricket::GetFirstAudioContent(answer->description());
487 ASSERT_TRUE(audio_content);
488 EXPECT_FALSE(audio_content->rejected);
489
490 const auto* video_content =
491 cricket::GetFirstVideoContent(answer->description());
492 ASSERT_TRUE(video_content);
493 EXPECT_FALSE(video_content->rejected);
494}
495
496class PeerConnectionMediaOfferDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800497 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700498 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800499 std::tuple<SdpSemantics,
500 std::tuple<bool, int, RtpTransceiverDirection>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700501 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800502 PeerConnectionMediaOfferDirectionTest()
503 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
504 auto param = std::get<1>(GetParam());
505 send_media_ = std::get<0>(param);
506 offer_to_receive_ = std::get<1>(param);
507 expected_direction_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700508 }
509
510 bool send_media_;
511 int offer_to_receive_;
Steve Anton4e70a722017-11-28 14:57:10 -0800512 RtpTransceiverDirection expected_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700513};
514
515// Tests that the correct direction is set on the media description according
516// to the presence of a local media track and the offer_to_receive setting.
517TEST_P(PeerConnectionMediaOfferDirectionTest, VerifyDirection) {
518 auto caller = CreatePeerConnection();
519 if (send_media_) {
520 caller->AddAudioTrack("a");
521 }
522
523 RTCOfferAnswerOptions options;
524 options.offer_to_receive_audio = offer_to_receive_;
525 auto offer = caller->CreateOffer(options);
526
Steve Antonad7bffc2018-01-22 10:21:56 -0800527 auto* content = cricket::GetFirstMediaContent(offer->description(),
528 cricket::MEDIA_TYPE_AUDIO);
Steve Anton4e70a722017-11-28 14:57:10 -0800529 if (expected_direction_ == RtpTransceiverDirection::kInactive) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800530 EXPECT_FALSE(content);
Steve Anton8d3444d2017-10-20 15:30:51 -0700531 } else {
Steve Antonad7bffc2018-01-22 10:21:56 -0800532 EXPECT_EQ(expected_direction_, content->media_description()->direction());
Steve Anton8d3444d2017-10-20 15:30:51 -0700533 }
534}
535
536// Note that in these tests, MD_INACTIVE indicates that no media section is
537// included in the offer, not that the media direction is inactive.
Steve Anton4e70a722017-11-28 14:57:10 -0800538INSTANTIATE_TEST_CASE_P(
539 PeerConnectionMediaTest,
540 PeerConnectionMediaOfferDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800541 Combine(
542 Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
543 Values(std::make_tuple(false, -1, RtpTransceiverDirection::kInactive),
544 std::make_tuple(false, 0, RtpTransceiverDirection::kInactive),
545 std::make_tuple(false, 1, RtpTransceiverDirection::kRecvOnly),
546 std::make_tuple(true, -1, RtpTransceiverDirection::kSendRecv),
547 std::make_tuple(true, 0, RtpTransceiverDirection::kSendOnly),
548 std::make_tuple(true, 1, RtpTransceiverDirection::kSendRecv))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700549
550class PeerConnectionMediaAnswerDirectionTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800551 : public PeerConnectionMediaBaseTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700552 public ::testing::WithParamInterface<
Steve Antonad7bffc2018-01-22 10:21:56 -0800553 std::tuple<SdpSemantics, RtpTransceiverDirection, bool, int>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700554 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800555 PeerConnectionMediaAnswerDirectionTest()
556 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
557 offer_direction_ = std::get<1>(GetParam());
558 send_media_ = std::get<2>(GetParam());
559 offer_to_receive_ = std::get<3>(GetParam());
Steve Anton8d3444d2017-10-20 15:30:51 -0700560 }
561
Steve Anton4e70a722017-11-28 14:57:10 -0800562 RtpTransceiverDirection offer_direction_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700563 bool send_media_;
564 int offer_to_receive_;
565};
566
567// Tests that the direction in an answer is correct according to direction sent
568// in the offer, the presence of a local media track on the receive side and the
569// offer_to_receive setting.
570TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyDirection) {
Steve Anton22da89f2018-01-25 13:58:07 -0800571 if (IsUnifiedPlan() &&
572 offer_to_receive_ != RTCOfferAnswerOptions::kUndefined) {
573 // offer_to_receive_ is not implemented when creating answers with Unified
574 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800575 return;
576 }
Steve Anton22da89f2018-01-25 13:58:07 -0800577
Steve Anton8d3444d2017-10-20 15:30:51 -0700578 auto caller = CreatePeerConnection();
579 caller->AddAudioTrack("a");
580
581 // Create the offer with an audio section and set its direction.
582 auto offer = caller->CreateOffer();
583 cricket::GetFirstAudioContentDescription(offer->description())
584 ->set_direction(offer_direction_);
585
586 auto callee = CreatePeerConnection();
587 if (send_media_) {
588 callee->AddAudioTrack("a");
589 }
590 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
591
592 // Create the answer according to the test parameters.
593 RTCOfferAnswerOptions options;
594 options.offer_to_receive_audio = offer_to_receive_;
595 auto answer = callee->CreateAnswer(options);
596
597 // The expected direction in the answer is the intersection of each side's
598 // capability to send/recv media.
599 // For the offerer, the direction is given in the offer (offer_direction_).
600 // For the answerer, the direction has two components:
601 // 1. Send if the answerer has a local track to send.
602 // 2. Receive if the answerer has explicitly set the offer_to_receive to 1 or
603 // if it has been left as default.
Steve Anton4e70a722017-11-28 14:57:10 -0800604 bool offer_send = RtpTransceiverDirectionHasSend(offer_direction_);
605 bool offer_recv = RtpTransceiverDirectionHasRecv(offer_direction_);
Steve Anton8d3444d2017-10-20 15:30:51 -0700606
607 // The negotiated components determine the direction set in the answer.
Steve Anton1d03a752017-11-27 14:30:09 -0800608 bool negotiate_send = (send_media_ && offer_recv);
609 bool negotiate_recv = ((offer_to_receive_ != 0) && offer_send);
Steve Anton8d3444d2017-10-20 15:30:51 -0700610
611 auto expected_direction =
Steve Anton4e70a722017-11-28 14:57:10 -0800612 RtpTransceiverDirectionFromSendRecv(negotiate_send, negotiate_recv);
Steve Anton8d3444d2017-10-20 15:30:51 -0700613 EXPECT_EQ(expected_direction,
Steve Antonad7bffc2018-01-22 10:21:56 -0800614 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700615}
616
617// Tests that the media section is rejected if and only if the callee has no
618// local media track and has set offer_to_receive to 0, no matter which
619// direction the caller indicated in the offer.
620TEST_P(PeerConnectionMediaAnswerDirectionTest, VerifyRejected) {
Steve Anton22da89f2018-01-25 13:58:07 -0800621 if (IsUnifiedPlan() &&
622 offer_to_receive_ != RTCOfferAnswerOptions::kUndefined) {
623 // offer_to_receive_ is not implemented when creating answers with Unified
624 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800625 return;
626 }
Steve Anton22da89f2018-01-25 13:58:07 -0800627
Steve Anton8d3444d2017-10-20 15:30:51 -0700628 auto caller = CreatePeerConnection();
629 caller->AddAudioTrack("a");
630
631 // Create the offer with an audio section and set its direction.
632 auto offer = caller->CreateOffer();
633 cricket::GetFirstAudioContentDescription(offer->description())
634 ->set_direction(offer_direction_);
635
636 auto callee = CreatePeerConnection();
637 if (send_media_) {
638 callee->AddAudioTrack("a");
639 }
640 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
641
642 // Create the answer according to the test parameters.
643 RTCOfferAnswerOptions options;
644 options.offer_to_receive_audio = offer_to_receive_;
645 auto answer = callee->CreateAnswer(options);
646
647 // The media section is rejected if and only if offer_to_receive is explicitly
648 // set to 0 and there is no media to send.
649 auto* audio_content = cricket::GetFirstAudioContent(answer->description());
650 ASSERT_TRUE(audio_content);
651 EXPECT_EQ((offer_to_receive_ == 0 && !send_media_), audio_content->rejected);
652}
653
654INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
655 PeerConnectionMediaAnswerDirectionTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800656 Combine(Values(SdpSemantics::kPlanB,
657 SdpSemantics::kUnifiedPlan),
658 Values(RtpTransceiverDirection::kInactive,
Steve Anton4e70a722017-11-28 14:57:10 -0800659 RtpTransceiverDirection::kSendOnly,
660 RtpTransceiverDirection::kRecvOnly,
661 RtpTransceiverDirection::kSendRecv),
Steve Anton8d3444d2017-10-20 15:30:51 -0700662 Bool(),
663 Values(-1, 0, 1)));
664
Steve Antonad7bffc2018-01-22 10:21:56 -0800665TEST_P(PeerConnectionMediaTest, OfferHasDifferentDirectionForAudioVideo) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700666 auto caller = CreatePeerConnection();
667 caller->AddVideoTrack("v");
668
669 RTCOfferAnswerOptions options;
670 options.offer_to_receive_audio = 1;
671 options.offer_to_receive_video = 0;
672 auto offer = caller->CreateOffer(options);
673
Steve Anton4e70a722017-11-28 14:57:10 -0800674 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800675 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800676 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800677 GetMediaContentDirection(offer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700678}
679
Steve Antonad7bffc2018-01-22 10:21:56 -0800680TEST_P(PeerConnectionMediaTest, AnswerHasDifferentDirectionsForAudioVideo) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800681 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800682 // offer_to_receive_ is not implemented when creating answers with Unified
683 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800684 return;
685 }
686
Steve Anton8d3444d2017-10-20 15:30:51 -0700687 auto caller = CreatePeerConnectionWithAudioVideo();
688 auto callee = CreatePeerConnection();
689 callee->AddVideoTrack("v");
690
691 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
692
693 RTCOfferAnswerOptions options;
694 options.offer_to_receive_audio = 1;
695 options.offer_to_receive_video = 0;
696 auto answer = callee->CreateAnswer(options);
697
Steve Anton4e70a722017-11-28 14:57:10 -0800698 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800699 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_AUDIO));
Steve Anton4e70a722017-11-28 14:57:10 -0800700 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
Steve Antonad7bffc2018-01-22 10:21:56 -0800701 GetMediaContentDirection(answer.get(), cricket::MEDIA_TYPE_VIDEO));
Steve Anton8d3444d2017-10-20 15:30:51 -0700702}
703
704void AddComfortNoiseCodecsToSend(cricket::FakeMediaEngine* media_engine) {
705 const cricket::AudioCodec kComfortNoiseCodec8k(102, "CN", 8000, 0, 1);
706 const cricket::AudioCodec kComfortNoiseCodec16k(103, "CN", 16000, 0, 1);
707
Sebastian Jansson6eb8a162018-11-16 11:29:55 +0100708 auto codecs = media_engine->voice().send_codecs();
Steve Anton8d3444d2017-10-20 15:30:51 -0700709 codecs.push_back(kComfortNoiseCodec8k);
710 codecs.push_back(kComfortNoiseCodec16k);
711 media_engine->SetAudioCodecs(codecs);
712}
713
714bool HasAnyComfortNoiseCodecs(const cricket::SessionDescription* desc) {
715 const auto* audio_desc = cricket::GetFirstAudioContentDescription(desc);
716 for (const auto& codec : audio_desc->codecs()) {
717 if (codec.name == "CN") {
718 return true;
719 }
720 }
721 return false;
722}
723
Steve Antonad7bffc2018-01-22 10:21:56 -0800724TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700725 CreateOfferWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
726 auto caller = CreatePeerConnectionWithAudioVideo();
727 AddComfortNoiseCodecsToSend(caller->media_engine());
728
729 RTCOfferAnswerOptions options;
730 options.voice_activity_detection = false;
731 auto offer = caller->CreateOffer(options);
732
733 EXPECT_FALSE(HasAnyComfortNoiseCodecs(offer->description()));
734}
735
Steve Antonad7bffc2018-01-22 10:21:56 -0800736TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -0700737 CreateAnswerWithNoVoiceActivityDetectionIncludesNoComfortNoiseCodecs) {
738 auto caller = CreatePeerConnectionWithAudioVideo();
739 AddComfortNoiseCodecsToSend(caller->media_engine());
740 auto callee = CreatePeerConnectionWithAudioVideo();
741 AddComfortNoiseCodecsToSend(callee->media_engine());
742
743 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
744
745 RTCOfferAnswerOptions options;
746 options.voice_activity_detection = false;
747 auto answer = callee->CreateAnswer(options);
748
749 EXPECT_FALSE(HasAnyComfortNoiseCodecs(answer->description()));
750}
751
752// The following test group verifies that we reject answers with invalid media
753// sections as per RFC 3264.
754
755class PeerConnectionMediaInvalidMediaTest
Steve Antonad7bffc2018-01-22 10:21:56 -0800756 : public PeerConnectionMediaBaseTest,
757 public ::testing::WithParamInterface<std::tuple<
758 SdpSemantics,
Steve Anton8d3444d2017-10-20 15:30:51 -0700759 std::tuple<std::string,
760 std::function<void(cricket::SessionDescription*)>,
Steve Antonad7bffc2018-01-22 10:21:56 -0800761 std::string>>> {
Steve Anton8d3444d2017-10-20 15:30:51 -0700762 protected:
Steve Antonad7bffc2018-01-22 10:21:56 -0800763 PeerConnectionMediaInvalidMediaTest()
764 : PeerConnectionMediaBaseTest(std::get<0>(GetParam())) {
765 auto param = std::get<1>(GetParam());
766 mutator_ = std::get<1>(param);
767 expected_error_ = std::get<2>(param);
Steve Anton8d3444d2017-10-20 15:30:51 -0700768 }
769
770 std::function<void(cricket::SessionDescription*)> mutator_;
771 std::string expected_error_;
772};
773
774TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetRemoteAnswer) {
775 auto caller = CreatePeerConnectionWithAudioVideo();
776 auto callee = CreatePeerConnectionWithAudioVideo();
777
778 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
779
780 auto answer = callee->CreateAnswer();
781 mutator_(answer->description());
782
783 std::string error;
784 ASSERT_FALSE(caller->SetRemoteDescription(std::move(answer), &error));
785 EXPECT_EQ("Failed to set remote answer sdp: " + expected_error_, error);
786}
787
788TEST_P(PeerConnectionMediaInvalidMediaTest, FailToSetLocalAnswer) {
789 auto caller = CreatePeerConnectionWithAudioVideo();
790 auto callee = CreatePeerConnectionWithAudioVideo();
791
792 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
793
794 auto answer = callee->CreateAnswer();
795 mutator_(answer->description());
796
797 std::string error;
798 ASSERT_FALSE(callee->SetLocalDescription(std::move(answer), &error));
799 EXPECT_EQ("Failed to set local answer sdp: " + expected_error_, error);
800}
801
802void RemoveVideoContent(cricket::SessionDescription* desc) {
803 auto content_name = cricket::GetFirstVideoContent(desc)->name;
804 desc->RemoveContentByName(content_name);
805 desc->RemoveTransportInfoByName(content_name);
806}
807
808void RenameVideoContent(cricket::SessionDescription* desc) {
809 auto* video_content = cricket::GetFirstVideoContent(desc);
810 auto* transport_info = desc->GetTransportInfoByName(video_content->name);
811 video_content->name = "video_renamed";
812 transport_info->content_name = video_content->name;
813}
814
815void ReverseMediaContent(cricket::SessionDescription* desc) {
Steve Anton64b626b2019-01-28 17:25:26 -0800816 absl::c_reverse(desc->contents());
817 absl::c_reverse(desc->transport_infos());
Steve Anton8d3444d2017-10-20 15:30:51 -0700818}
819
820void ChangeMediaTypeAudioToVideo(cricket::SessionDescription* desc) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800821 std::string audio_mid = cricket::GetFirstAudioContent(desc)->name;
822 desc->RemoveContentByName(audio_mid);
823 auto* video_content = cricket::GetFirstVideoContent(desc);
824 desc->AddContent(audio_mid, video_content->type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800825 video_content->media_description()->Copy());
Steve Anton8d3444d2017-10-20 15:30:51 -0700826}
827
828constexpr char kMLinesOutOfOrder[] =
829 "The order of m-lines in answer doesn't match order in offer. Rejecting "
830 "answer.";
831
832INSTANTIATE_TEST_CASE_P(
833 PeerConnectionMediaTest,
834 PeerConnectionMediaInvalidMediaTest,
Steve Antonad7bffc2018-01-22 10:21:56 -0800835 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
836 Values(std::make_tuple("remove video",
837 RemoveVideoContent,
838 kMLinesOutOfOrder),
839 std::make_tuple("rename video",
840 RenameVideoContent,
841 kMLinesOutOfOrder),
842 std::make_tuple("reverse media sections",
843 ReverseMediaContent,
844 kMLinesOutOfOrder),
845 std::make_tuple("change audio type to video type",
846 ChangeMediaTypeAudioToVideo,
847 kMLinesOutOfOrder))));
Steve Anton8d3444d2017-10-20 15:30:51 -0700848
849// Test that the correct media engine send/recv streams are created when doing
850// a series of offer/answers where audio/video are both sent, then audio is
851// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800852TEST_P(PeerConnectionMediaTest, TestAVOfferWithAudioOnlyAnswer) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800853 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800854 // offer_to_receive_ is not implemented when creating answers with Unified
855 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800856 return;
857 }
858
Steve Anton8d3444d2017-10-20 15:30:51 -0700859 RTCOfferAnswerOptions options_reject_video;
860 options_reject_video.offer_to_receive_audio =
861 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
862 options_reject_video.offer_to_receive_video = 0;
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 audio as recv only but rejects the video.
872 ASSERT_TRUE(caller->SetRemoteDescription(
873 callee->CreateAnswerAndSetAsLocal(options_reject_video)));
874
875 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
876 ASSERT_TRUE(caller_voice);
877 EXPECT_EQ(0u, caller_voice->recv_streams().size());
878 EXPECT_EQ(1u, caller_voice->send_streams().size());
879 auto caller_video = caller->media_engine()->GetVideoChannel(0);
880 EXPECT_FALSE(caller_video);
881
882 // Callee adds its own audio/video stream and offers to receive audio/video
883 // too.
884 callee->AddAudioTrack("a");
885 auto callee_video_track = callee->AddVideoTrack("v");
886 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
887 ASSERT_TRUE(
888 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
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 video but keeps audio and rejects the video once again.
900 callee->pc()->RemoveTrack(callee_video_track);
901 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
902 ASSERT_TRUE(
903 callee->SetLocalDescription(callee->CreateAnswer(options_reject_video)));
904
905 callee_voice = callee->media_engine()->GetVoiceChannel(0);
906 ASSERT_TRUE(callee_voice);
907 EXPECT_EQ(1u, callee_voice->recv_streams().size());
908 EXPECT_EQ(1u, callee_voice->send_streams().size());
909 callee_video = callee->media_engine()->GetVideoChannel(0);
910 EXPECT_FALSE(callee_video);
911}
912
913// Test that the correct media engine send/recv streams are created when doing
914// a series of offer/answers where audio/video are both sent, then video is
915// rejected, then both audio/video sent again.
Steve Antonad7bffc2018-01-22 10:21:56 -0800916TEST_P(PeerConnectionMediaTest, TestAVOfferWithVideoOnlyAnswer) {
Steve Antonad7bffc2018-01-22 10:21:56 -0800917 if (IsUnifiedPlan()) {
Steve Anton22da89f2018-01-25 13:58:07 -0800918 // offer_to_receive_ is not implemented when creating answers with Unified
919 // Plan semantics specified.
Steve Antonad7bffc2018-01-22 10:21:56 -0800920 return;
921 }
922
Steve Anton8d3444d2017-10-20 15:30:51 -0700923 // Disable the bundling here. If the media is bundled on audio
924 // transport, then we can't reject the audio because switching the bundled
925 // transport is not currently supported.
926 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6704)
927 RTCOfferAnswerOptions options_no_bundle;
928 options_no_bundle.use_rtp_mux = false;
929 RTCOfferAnswerOptions options_reject_audio = options_no_bundle;
930 options_reject_audio.offer_to_receive_audio = 0;
931 options_reject_audio.offer_to_receive_video =
932 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia;
933
934 auto caller = CreatePeerConnection();
935 caller->AddAudioTrack("a");
936 caller->AddVideoTrack("v");
937 auto callee = CreatePeerConnection();
938
939 // Caller initially offers to send/recv audio and video.
940 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
941 // Callee accepts the video as recv only but rejects the audio.
942 ASSERT_TRUE(caller->SetRemoteDescription(
943 callee->CreateAnswerAndSetAsLocal(options_reject_audio)));
944
945 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
946 EXPECT_FALSE(caller_voice);
947 auto caller_video = caller->media_engine()->GetVideoChannel(0);
948 ASSERT_TRUE(caller_video);
949 EXPECT_EQ(0u, caller_video->recv_streams().size());
950 EXPECT_EQ(1u, caller_video->send_streams().size());
951
952 // Callee adds its own audio/video stream and offers to receive audio/video
953 // too.
954 auto callee_audio_track = callee->AddAudioTrack("a");
955 callee->AddVideoTrack("v");
956 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
957 ASSERT_TRUE(caller->SetRemoteDescription(
958 callee->CreateAnswerAndSetAsLocal(options_no_bundle)));
959
960 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
961 ASSERT_TRUE(callee_voice);
962 EXPECT_EQ(1u, callee_voice->recv_streams().size());
963 EXPECT_EQ(1u, callee_voice->send_streams().size());
964 auto callee_video = callee->media_engine()->GetVideoChannel(0);
965 ASSERT_TRUE(callee_video);
966 EXPECT_EQ(1u, callee_video->recv_streams().size());
967 EXPECT_EQ(1u, callee_video->send_streams().size());
968
969 // Callee removes audio but keeps video and rejects the audio once again.
970 callee->pc()->RemoveTrack(callee_audio_track);
971 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
972 ASSERT_TRUE(
973 callee->SetLocalDescription(callee->CreateAnswer(options_reject_audio)));
974
975 callee_voice = callee->media_engine()->GetVoiceChannel(0);
976 EXPECT_FALSE(callee_voice);
977 callee_video = callee->media_engine()->GetVideoChannel(0);
978 ASSERT_TRUE(callee_video);
979 EXPECT_EQ(1u, callee_video->recv_streams().size());
980 EXPECT_EQ(1u, callee_video->send_streams().size());
981}
982
983// Tests that if the underlying video encoder fails to be initialized (signaled
984// by failing to set send codecs), the PeerConnection signals the error to the
985// client.
Steve Antonad7bffc2018-01-22 10:21:56 -0800986TEST_P(PeerConnectionMediaTest, MediaEngineErrorPropagatedToClients) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700987 auto caller = CreatePeerConnectionWithAudioVideo();
988 auto callee = CreatePeerConnectionWithAudioVideo();
989
990 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
991
992 auto video_channel = caller->media_engine()->GetVideoChannel(0);
993 video_channel->set_fail_set_send_codecs(true);
994
995 std::string error;
996 ASSERT_FALSE(caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(),
997 &error));
998 EXPECT_EQ(
Steve Anton80dd7b52018-02-16 17:08:42 -0800999 "Failed to set remote answer sdp: Failed to set remote video description "
1000 "send parameters.",
Steve Anton8d3444d2017-10-20 15:30:51 -07001001 error);
1002}
1003
1004// Tests that if the underlying video encoder fails once then subsequent
1005// attempts at setting the local/remote description will also fail, even if
1006// SetSendCodecs no longer fails.
Steve Antonad7bffc2018-01-22 10:21:56 -08001007TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -07001008 FailToApplyDescriptionIfVideoEncoderHasEverFailed) {
1009 auto caller = CreatePeerConnectionWithAudioVideo();
1010 auto callee = CreatePeerConnectionWithAudioVideo();
1011
1012 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1013
1014 auto video_channel = caller->media_engine()->GetVideoChannel(0);
1015 video_channel->set_fail_set_send_codecs(true);
1016
1017 EXPECT_FALSE(
1018 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1019
1020 video_channel->set_fail_set_send_codecs(false);
1021
1022 EXPECT_FALSE(caller->SetRemoteDescription(callee->CreateAnswer()));
1023 EXPECT_FALSE(caller->SetLocalDescription(caller->CreateOffer()));
1024}
1025
1026void RenameContent(cricket::SessionDescription* desc,
Steve Antonad7bffc2018-01-22 10:21:56 -08001027 cricket::MediaType media_type,
Steve Anton8d3444d2017-10-20 15:30:51 -07001028 const std::string& new_name) {
Steve Antonad7bffc2018-01-22 10:21:56 -08001029 auto* content = cricket::GetFirstMediaContent(desc, media_type);
Steve Anton8d3444d2017-10-20 15:30:51 -07001030 RTC_DCHECK(content);
Steve Antonad7bffc2018-01-22 10:21:56 -08001031 std::string old_name = content->name;
Steve Anton8d3444d2017-10-20 15:30:51 -07001032 content->name = new_name;
1033 auto* transport = desc->GetTransportInfoByName(old_name);
1034 RTC_DCHECK(transport);
1035 transport->content_name = new_name;
Zhi Huangd2248f82018-04-10 14:41:03 -07001036
1037 // Rename the content name in the BUNDLE group.
1038 cricket::ContentGroup new_bundle_group =
1039 *desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1040 new_bundle_group.RemoveContentName(old_name);
1041 new_bundle_group.AddContentName(new_name);
1042 desc->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
1043 desc->AddGroup(new_bundle_group);
Steve Anton8d3444d2017-10-20 15:30:51 -07001044}
1045
1046// Tests that an answer responds with the same MIDs as the offer.
Steve Antonad7bffc2018-01-22 10:21:56 -08001047TEST_P(PeerConnectionMediaTest, AnswerHasSameMidsAsOffer) {
Zhi Huang365381f2018-04-13 16:44:34 -07001048 const std::string kAudioMid = "notdefault1";
1049 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 15:30:51 -07001050
1051 auto caller = CreatePeerConnectionWithAudioVideo();
1052 auto callee = CreatePeerConnectionWithAudioVideo();
1053
1054 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -08001055 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
1056 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -07001057 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1058
1059 auto answer = callee->CreateAnswer();
1060 EXPECT_EQ(kAudioMid,
1061 cricket::GetFirstAudioContent(answer->description())->name);
1062 EXPECT_EQ(kVideoMid,
1063 cricket::GetFirstVideoContent(answer->description())->name);
1064}
1065
1066// Test that if the callee creates a re-offer, the MIDs are the same as the
1067// original offer.
Steve Antonad7bffc2018-01-22 10:21:56 -08001068TEST_P(PeerConnectionMediaTest, ReOfferHasSameMidsAsFirstOffer) {
Zhi Huang365381f2018-04-13 16:44:34 -07001069 const std::string kAudioMid = "notdefault1";
1070 const std::string kVideoMid = "notdefault2";
Steve Anton8d3444d2017-10-20 15:30:51 -07001071
1072 auto caller = CreatePeerConnectionWithAudioVideo();
1073 auto callee = CreatePeerConnectionWithAudioVideo();
1074
1075 auto offer = caller->CreateOffer();
Steve Antonad7bffc2018-01-22 10:21:56 -08001076 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, kAudioMid);
1077 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, kVideoMid);
Steve Anton8d3444d2017-10-20 15:30:51 -07001078 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1079 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1080
1081 auto reoffer = callee->CreateOffer();
1082 EXPECT_EQ(kAudioMid,
1083 cricket::GetFirstAudioContent(reoffer->description())->name);
1084 EXPECT_EQ(kVideoMid,
1085 cricket::GetFirstVideoContent(reoffer->description())->name);
1086}
1087
Steve Anton06817cd2018-12-18 15:55:30 -08001088// Test that SetRemoteDescription returns an error if there are two m= sections
1089// with the same MID value.
1090TEST_P(PeerConnectionMediaTest, SetRemoteDescriptionFailsWithDuplicateMids) {
1091 auto caller = CreatePeerConnectionWithAudioVideo();
1092 auto callee = CreatePeerConnectionWithAudioVideo();
1093
1094 auto offer = caller->CreateOffer();
1095 RenameContent(offer->description(), cricket::MEDIA_TYPE_AUDIO, "same");
1096 RenameContent(offer->description(), cricket::MEDIA_TYPE_VIDEO, "same");
1097
1098 std::string error;
1099 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer), &error));
1100 EXPECT_EQ(error,
1101 "Failed to set remote offer sdp: Duplicate a=mid value 'same'.");
1102}
1103
Steve Antonad7bffc2018-01-22 10:21:56 -08001104TEST_P(PeerConnectionMediaTest,
Steve Anton8d3444d2017-10-20 15:30:51 -07001105 CombinedAudioVideoBweConfigPropagatedToMediaEngine) {
1106 RTCConfiguration config;
1107 config.combined_audio_video_bwe.emplace(true);
1108 auto caller = CreatePeerConnectionWithAudioVideo(config);
1109
1110 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1111
1112 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1113 ASSERT_TRUE(caller_voice);
1114 const cricket::AudioOptions& audio_options = caller_voice->options();
1115 EXPECT_EQ(config.combined_audio_video_bwe,
1116 audio_options.combined_audio_video_bwe);
1117}
1118
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001119TEST_P(PeerConnectionMediaTest, MediaTransportPropagatedToVoiceEngine) {
1120 RTCConfiguration config;
1121
1122 // Setup PeerConnection to use media transport.
1123 config.use_media_transport = true;
1124
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001125 // Force SDES.
1126 config.enable_dtls_srtp = false;
1127
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -08001128 auto caller = CreatePeerConnectionWithAudio(config);
1129 auto callee = CreatePeerConnectionWithAudio(config);
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001130
1131 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1132 auto answer = callee->CreateAnswer();
1133 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
1134
1135 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1136 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
1137 ASSERT_TRUE(caller_voice);
1138 ASSERT_TRUE(callee_voice);
1139
1140 // Make sure media transport is propagated to voice channel.
1141 FakeMediaTransport* caller_voice_media_transport =
1142 static_cast<FakeMediaTransport*>(caller_voice->media_transport());
1143 FakeMediaTransport* callee_voice_media_transport =
1144 static_cast<FakeMediaTransport*>(callee_voice->media_transport());
1145 ASSERT_NE(nullptr, caller_voice_media_transport);
1146 ASSERT_NE(nullptr, callee_voice_media_transport);
1147
1148 // Make sure media transport is created with correct is_caller.
1149 EXPECT_TRUE(caller_voice_media_transport->is_caller());
1150 EXPECT_FALSE(callee_voice_media_transport->is_caller());
1151
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -08001152 // TODO(sukhanov): Propagate media transport to video channel.
1153 // This test does NOT set up video channels, because currently it causes
1154 // us to create two media transports.
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001155}
1156
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001157TEST_P(PeerConnectionMediaTest, MediaTransportOnlyForDataChannels) {
1158 RTCConfiguration config;
1159
1160 // Setup PeerConnection to use media transport for data channels.
1161 config.use_media_transport_for_data_channels = true;
1162
1163 // Force SDES.
1164 config.enable_dtls_srtp = false;
1165
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -08001166 auto caller = CreatePeerConnectionWithAudio(config);
1167 auto callee = CreatePeerConnectionWithAudio(config);
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001168
1169 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1170 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1171
1172 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1173 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
1174 ASSERT_TRUE(caller_voice);
1175 ASSERT_TRUE(callee_voice);
1176
1177 // Make sure media transport is not propagated to voice channel.
1178 EXPECT_EQ(nullptr, caller_voice->media_transport());
1179 EXPECT_EQ(nullptr, callee_voice->media_transport());
1180}
1181
1182TEST_P(PeerConnectionMediaTest, MediaTransportForMediaAndDataChannels) {
1183 RTCConfiguration config;
1184
1185 // Setup PeerConnection to use media transport for both media and data
1186 // channels.
1187 config.use_media_transport = true;
1188 config.use_media_transport_for_data_channels = true;
1189
1190 // Force SDES.
1191 config.enable_dtls_srtp = false;
1192
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -08001193 auto caller = CreatePeerConnectionWithAudio(config);
1194 auto callee = CreatePeerConnectionWithAudio(config);
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001195
1196 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1197 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
1198
1199 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1200 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
1201 ASSERT_TRUE(caller_voice);
1202 ASSERT_TRUE(callee_voice);
1203
1204 // Make sure media transport is propagated to voice channel.
1205 FakeMediaTransport* caller_voice_media_transport =
1206 static_cast<FakeMediaTransport*>(caller_voice->media_transport());
1207 FakeMediaTransport* callee_voice_media_transport =
1208 static_cast<FakeMediaTransport*>(callee_voice->media_transport());
1209 ASSERT_NE(nullptr, caller_voice_media_transport);
1210 ASSERT_NE(nullptr, callee_voice_media_transport);
1211
1212 // Make sure media transport is created with correct is_caller.
1213 EXPECT_TRUE(caller_voice_media_transport->is_caller());
1214 EXPECT_FALSE(callee_voice_media_transport->is_caller());
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001215}
1216
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001217TEST_P(PeerConnectionMediaTest, MediaTransportNotPropagatedToVoiceEngine) {
1218 auto caller = CreatePeerConnectionWithAudioVideo();
1219 auto callee = CreatePeerConnectionWithAudioVideo();
1220
1221 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1222 auto answer = callee->CreateAnswer();
1223 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
1224
1225 auto caller_voice = caller->media_engine()->GetVoiceChannel(0);
1226 auto callee_voice = callee->media_engine()->GetVoiceChannel(0);
1227 ASSERT_TRUE(caller_voice);
1228 ASSERT_TRUE(callee_voice);
1229
1230 // Since we did not setup PeerConnection to use media transport, media
1231 // transport should not be created / propagated to the voice engine.
1232 ASSERT_EQ(nullptr, caller_voice->media_transport());
1233 ASSERT_EQ(nullptr, callee_voice->media_transport());
1234
1235 auto caller_video = caller->media_engine()->GetVideoChannel(0);
1236 auto callee_video = callee->media_engine()->GetVideoChannel(0);
1237 ASSERT_EQ(nullptr, caller_video->media_transport());
1238 ASSERT_EQ(nullptr, callee_video->media_transport());
1239}
1240
Steve Antonad7bffc2018-01-22 10:21:56 -08001241INSTANTIATE_TEST_CASE_P(PeerConnectionMediaTest,
1242 PeerConnectionMediaTest,
1243 Values(SdpSemantics::kPlanB,
1244 SdpSemantics::kUnifiedPlan));
1245
Steve Anton8d3444d2017-10-20 15:30:51 -07001246} // namespace webrtc