blob: caa0ea0587eadd291c9a6f1a8d5648de650509af [file] [log] [blame]
Steve Antondcc3c022017-12-22 16:02:54 -08001/*
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#include "api/audio_codecs/builtin_audio_decoder_factory.h"
12#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Antonfa2260d2017-12-28 16:38:23 -080013#include "media/engine/webrtcmediaengine.h"
14#include "modules/audio_processing/include/audio_processing.h"
Steve Antondcc3c022017-12-22 16:02:54 -080015#include "pc/mediasession.h"
Steve Antonfa2260d2017-12-28 16:38:23 -080016#include "pc/peerconnectionfactory.h"
Steve Antondcc3c022017-12-22 16:02:54 -080017#include "pc/peerconnectionwrapper.h"
18#include "pc/sdputils.h"
19#ifdef WEBRTC_ANDROID
20#include "pc/test/androidtestinitializer.h"
21#endif
22#include "pc/test/fakeaudiocapturemodule.h"
Steve Antonfa2260d2017-12-28 16:38:23 -080023#include "pc/test/fakesctptransport.h"
Steve Antondcc3c022017-12-22 16:02:54 -080024#include "rtc_base/gunit.h"
25#include "rtc_base/ptr_util.h"
26#include "rtc_base/virtualsocketserver.h"
27#include "test/gmock.h"
28
29// This file contains tests that ensure the PeerConnection's implementation of
30// CreateOffer/CreateAnswer/SetLocalDescription/SetRemoteDescription conform
31// to the JavaScript Session Establishment Protocol (JSEP).
32// For now these semantics are only available when configuring the
33// PeerConnection with Unified Plan, but eventually that will be the default.
34
35namespace webrtc {
36
37using cricket::MediaContentDescription;
38using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
39using ::testing::Values;
40using ::testing::Combine;
41using ::testing::ElementsAre;
Steve Antonef65ef12018-01-10 17:15:20 -080042using ::testing::UnorderedElementsAre;
Steve Antondcc3c022017-12-22 16:02:54 -080043
Steve Antonfa2260d2017-12-28 16:38:23 -080044class PeerConnectionFactoryForJsepTest : public PeerConnectionFactory {
45 public:
46 PeerConnectionFactoryForJsepTest()
47 : PeerConnectionFactory(
48 rtc::Thread::Current(),
49 rtc::Thread::Current(),
50 rtc::Thread::Current(),
51 rtc::WrapUnique(cricket::WebRtcMediaEngineFactory::Create(
52 FakeAudioCaptureModule::Create(),
53 CreateBuiltinAudioEncoderFactory(),
54 CreateBuiltinAudioDecoderFactory(),
55 nullptr,
56 nullptr,
57 nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +010058 AudioProcessingBuilder().Create())),
Steve Antonfa2260d2017-12-28 16:38:23 -080059 CreateCallFactory(),
60 nullptr) {}
61
62 std::unique_ptr<cricket::SctpTransportInternalFactory>
63 CreateSctpTransportInternalFactory() {
64 return rtc::MakeUnique<FakeSctpTransportFactory>();
65 }
66};
67
Steve Antondcc3c022017-12-22 16:02:54 -080068class PeerConnectionJsepTest : public ::testing::Test {
69 protected:
70 typedef std::unique_ptr<PeerConnectionWrapper> WrapperPtr;
71
72 PeerConnectionJsepTest()
73 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
74#ifdef WEBRTC_ANDROID
75 InitializeAndroidObjects();
76#endif
Steve Antondcc3c022017-12-22 16:02:54 -080077 }
78
79 WrapperPtr CreatePeerConnection() {
80 RTCConfiguration config;
81 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
82 return CreatePeerConnection(config);
83 }
84
85 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
Steve Antonfa2260d2017-12-28 16:38:23 -080086 rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
87 new rtc::RefCountedObject<PeerConnectionFactoryForJsepTest>());
88 RTC_CHECK(pc_factory->Initialize());
Steve Antondcc3c022017-12-22 16:02:54 -080089 auto observer = rtc::MakeUnique<MockPeerConnectionObserver>();
Steve Antonfa2260d2017-12-28 16:38:23 -080090 auto pc = pc_factory->CreatePeerConnection(config, nullptr, nullptr,
91 observer.get());
Steve Antondcc3c022017-12-22 16:02:54 -080092 if (!pc) {
93 return nullptr;
94 }
95
Steve Antonfa2260d2017-12-28 16:38:23 -080096 return rtc::MakeUnique<PeerConnectionWrapper>(pc_factory, pc,
Steve Antondcc3c022017-12-22 16:02:54 -080097 std::move(observer));
98 }
99
100 std::unique_ptr<rtc::VirtualSocketServer> vss_;
101 rtc::AutoSocketServerThread main_;
Steve Antondcc3c022017-12-22 16:02:54 -0800102};
103
104// Tests for JSEP initial offer generation.
105
106// Test that an offer created by a PeerConnection with no transceivers generates
107// no media sections.
108TEST_F(PeerConnectionJsepTest, EmptyInitialOffer) {
109 auto caller = CreatePeerConnection();
Steve Antonfa2260d2017-12-28 16:38:23 -0800110
Steve Antondcc3c022017-12-22 16:02:54 -0800111 auto offer = caller->CreateOffer();
Steve Antonfa2260d2017-12-28 16:38:23 -0800112 ASSERT_EQ(0u, offer->description()->contents().size());
Steve Antondcc3c022017-12-22 16:02:54 -0800113}
114
115// Test that an initial offer with one audio track generates one audio media
116// section.
117TEST_F(PeerConnectionJsepTest, AudioOnlyInitialOffer) {
118 auto caller = CreatePeerConnection();
119 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
Steve Antondcc3c022017-12-22 16:02:54 -0800120
Steve Antonfa2260d2017-12-28 16:38:23 -0800121 auto offer = caller->CreateOffer();
Steve Antondcc3c022017-12-22 16:02:54 -0800122 auto contents = offer->description()->contents();
123 ASSERT_EQ(1u, contents.size());
124 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, contents[0].media_description()->type());
125}
126
127// Test than an initial offer with one video track generates one video media
128// section
129TEST_F(PeerConnectionJsepTest, VideoOnlyInitialOffer) {
130 auto caller = CreatePeerConnection();
131 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
Steve Antondcc3c022017-12-22 16:02:54 -0800132
Steve Antonfa2260d2017-12-28 16:38:23 -0800133 auto offer = caller->CreateOffer();
Steve Antondcc3c022017-12-22 16:02:54 -0800134 auto contents = offer->description()->contents();
135 ASSERT_EQ(1u, contents.size());
136 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, contents[0].media_description()->type());
137}
138
Steve Antonfa2260d2017-12-28 16:38:23 -0800139// Test that an initial offer with one data channel generates one data media
140// section.
141TEST_F(PeerConnectionJsepTest, DataOnlyInitialOffer) {
142 auto caller = CreatePeerConnection();
143 caller->CreateDataChannel("dc");
144
145 auto offer = caller->CreateOffer();
146 auto contents = offer->description()->contents();
147 ASSERT_EQ(1u, contents.size());
148 EXPECT_EQ(cricket::MEDIA_TYPE_DATA, contents[0].media_description()->type());
149}
150
151// Test that creating multiple data channels only results in one data section
152// generated in the offer.
153TEST_F(PeerConnectionJsepTest, MultipleDataChannelsCreateOnlyOneDataSection) {
154 auto caller = CreatePeerConnection();
155 caller->CreateDataChannel("first");
156 caller->CreateDataChannel("second");
157 caller->CreateDataChannel("third");
158
159 auto offer = caller->CreateOffer();
160 ASSERT_EQ(1u, offer->description()->contents().size());
161}
162
Steve Antondcc3c022017-12-22 16:02:54 -0800163// Test that multiple media sections in the initial offer are ordered in the
164// order the transceivers were added to the PeerConnection. This is required by
165// JSEP section 5.2.1.
166TEST_F(PeerConnectionJsepTest, MediaSectionsInInitialOfferOrderedCorrectly) {
167 auto caller = CreatePeerConnection();
168 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
169 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
170 RtpTransceiverInit init;
171 init.direction = RtpTransceiverDirection::kSendOnly;
172 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init);
Steve Antondcc3c022017-12-22 16:02:54 -0800173
Steve Antonfa2260d2017-12-28 16:38:23 -0800174 auto offer = caller->CreateOffer();
Steve Antondcc3c022017-12-22 16:02:54 -0800175 auto contents = offer->description()->contents();
176 ASSERT_EQ(3u, contents.size());
177
178 const MediaContentDescription* media_description1 =
179 contents[0].media_description();
180 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, media_description1->type());
181 EXPECT_EQ(RtpTransceiverDirection::kSendRecv,
182 media_description1->direction());
183
184 const MediaContentDescription* media_description2 =
185 contents[1].media_description();
186 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, media_description2->type());
187 EXPECT_EQ(RtpTransceiverDirection::kSendRecv,
188 media_description2->direction());
189
190 const MediaContentDescription* media_description3 =
191 contents[2].media_description();
192 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, media_description3->type());
193 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
194 media_description3->direction());
195}
196
197// Test that media sections in the initial offer have different mids.
198TEST_F(PeerConnectionJsepTest, MediaSectionsInInitialOfferHaveDifferentMids) {
199 auto caller = CreatePeerConnection();
200 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
201 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
Steve Antonfa2260d2017-12-28 16:38:23 -0800202
Steve Antondcc3c022017-12-22 16:02:54 -0800203 auto offer = caller->CreateOffer();
Steve Antondcc3c022017-12-22 16:02:54 -0800204 auto contents = offer->description()->contents();
205 ASSERT_EQ(2u, contents.size());
206 EXPECT_NE(contents[0].name, contents[1].name);
207}
208
209TEST_F(PeerConnectionJsepTest,
210 StoppedTransceiverHasNoMediaSectionInInitialOffer) {
211 auto caller = CreatePeerConnection();
212 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
213 transceiver->Stop();
214
215 auto offer = caller->CreateOffer();
216 EXPECT_EQ(0u, offer->description()->contents().size());
217}
218
219// Tests for JSEP SetLocalDescription with a local offer.
220
221TEST_F(PeerConnectionJsepTest, SetLocalEmptyOfferCreatesNoTransceivers) {
222 auto caller = CreatePeerConnection();
Steve Antonfa2260d2017-12-28 16:38:23 -0800223
Steve Antondcc3c022017-12-22 16:02:54 -0800224 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
225
226 EXPECT_THAT(caller->pc()->GetTransceivers(), ElementsAre());
227 EXPECT_THAT(caller->pc()->GetSenders(), ElementsAre());
228 EXPECT_THAT(caller->pc()->GetReceivers(), ElementsAre());
229}
230
231TEST_F(PeerConnectionJsepTest, SetLocalOfferSetsTransceiverMid) {
232 auto caller = CreatePeerConnection();
233 auto audio_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
234 auto video_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
235
236 auto offer = caller->CreateOffer();
237 std::string audio_mid = offer->description()->contents()[0].name;
238 std::string video_mid = offer->description()->contents()[1].name;
239
240 ASSERT_TRUE(caller->SetLocalDescription(std::move(offer)));
241
242 EXPECT_EQ(audio_mid, audio_transceiver->mid());
243 EXPECT_EQ(video_mid, video_transceiver->mid());
244}
245
246// Tests for JSEP SetRemoteDescription with a remote offer.
247
248// Test that setting a remote offer with sendrecv audio and video creates two
249// transceivers, one for receiving audio and one for receiving video.
250TEST_F(PeerConnectionJsepTest, SetRemoteOfferCreatesTransceivers) {
251 auto caller = CreatePeerConnection();
252 auto caller_audio = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
253 auto caller_video = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
254 auto callee = CreatePeerConnection();
255
256 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
257
258 auto transceivers = callee->pc()->GetTransceivers();
259 ASSERT_EQ(2u, transceivers.size());
Steve Anton69470252018-02-09 11:43:08 -0800260 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, transceivers[0]->media_type());
Steve Antondcc3c022017-12-22 16:02:54 -0800261 EXPECT_EQ(caller_audio->mid(), transceivers[0]->mid());
262 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly, transceivers[0]->direction());
Steve Anton69470252018-02-09 11:43:08 -0800263 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, transceivers[1]->media_type());
Steve Antondcc3c022017-12-22 16:02:54 -0800264 EXPECT_EQ(caller_video->mid(), transceivers[1]->mid());
265 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly, transceivers[1]->direction());
266}
267
268// Test that setting a remote offer with an audio track will reuse the
269// transceiver created for a local audio track added by AddTrack.
270// This is specified in JSEP section 5.10 (Applying a Remote Description). The
271// intent is to preserve backwards compatibility with clients who only use the
272// AddTrack API.
273TEST_F(PeerConnectionJsepTest, SetRemoteOfferReusesTransceiverFromAddTrack) {
274 auto caller = CreatePeerConnection();
275 caller->AddAudioTrack("a");
276 auto caller_audio = caller->pc()->GetTransceivers()[0];
277 auto callee = CreatePeerConnection();
278 callee->AddAudioTrack("a");
279
280 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
281
282 auto transceivers = callee->pc()->GetTransceivers();
283 ASSERT_EQ(1u, transceivers.size());
284 EXPECT_EQ(MediaStreamTrackInterface::kAudioKind,
285 transceivers[0]->receiver()->track()->kind());
286 EXPECT_EQ(caller_audio->mid(), transceivers[0]->mid());
287}
288
289// Test that setting a remote offer with an audio track marked sendonly will not
290// reuse a transceiver created by AddTrack. JSEP only allows the transceiver to
291// be reused if the offer direction is sendrecv or recvonly.
292TEST_F(PeerConnectionJsepTest,
293 SetRemoteOfferDoesNotReuseTransceiverIfDirectionSendOnly) {
294 auto caller = CreatePeerConnection();
295 caller->AddAudioTrack("a");
296 auto caller_audio = caller->pc()->GetTransceivers()[0];
297 caller_audio->SetDirection(RtpTransceiverDirection::kSendOnly);
298 auto callee = CreatePeerConnection();
299 callee->AddAudioTrack("a");
300
301 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
302
303 auto transceivers = callee->pc()->GetTransceivers();
304 ASSERT_EQ(2u, transceivers.size());
305 EXPECT_EQ(rtc::nullopt, transceivers[0]->mid());
306 EXPECT_EQ(caller_audio->mid(), transceivers[1]->mid());
307}
308
309// Test that setting a remote offer with an audio track will not reuse a
310// transceiver added by AddTransceiver. The logic for reusing a transceiver is
311// specific to those added by AddTrack and is tested above.
312TEST_F(PeerConnectionJsepTest,
313 SetRemoteOfferDoesNotReuseTransceiverFromAddTransceiver) {
314 auto caller = CreatePeerConnection();
315 caller->AddAudioTrack("a");
316 auto callee = CreatePeerConnection();
317 auto transceiver = callee->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
318
319 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
320
321 auto transceivers = callee->pc()->GetTransceivers();
322 ASSERT_EQ(2u, transceivers.size());
323 EXPECT_EQ(rtc::nullopt, transceivers[0]->mid());
324 EXPECT_EQ(caller->pc()->GetTransceivers()[0]->mid(), transceivers[1]->mid());
325 EXPECT_EQ(MediaStreamTrackInterface::kAudioKind,
326 transceivers[1]->receiver()->track()->kind());
327}
328
329// Test that setting a remote offer with an audio track will not reuse a
330// transceiver created for a local video track added by AddTrack.
331TEST_F(PeerConnectionJsepTest,
332 SetRemoteOfferDoesNotReuseTransceiverOfWrongType) {
333 auto caller = CreatePeerConnection();
334 caller->AddAudioTrack("a");
335 auto callee = CreatePeerConnection();
336 auto video_sender = callee->AddVideoTrack("v");
337
338 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
339
340 auto transceivers = callee->pc()->GetTransceivers();
341 ASSERT_EQ(2u, transceivers.size());
342 EXPECT_EQ(rtc::nullopt, transceivers[0]->mid());
343 EXPECT_EQ(caller->pc()->GetTransceivers()[0]->mid(), transceivers[1]->mid());
344 EXPECT_EQ(MediaStreamTrackInterface::kAudioKind,
345 transceivers[1]->receiver()->track()->kind());
346}
347
348// Test that setting a remote offer with an audio track will not reuse a
349// stopped transceiver.
350TEST_F(PeerConnectionJsepTest, SetRemoteOfferDoesNotReuseStoppedTransceiver) {
351 auto caller = CreatePeerConnection();
352 caller->AddAudioTrack("a");
353 auto callee = CreatePeerConnection();
354 callee->AddAudioTrack("a");
355 callee->pc()->GetTransceivers()[0]->Stop();
356
357 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
358
359 auto transceivers = callee->pc()->GetTransceivers();
360 ASSERT_EQ(2u, transceivers.size());
361 EXPECT_EQ(rtc::nullopt, transceivers[0]->mid());
362 EXPECT_TRUE(transceivers[0]->stopped());
363 EXPECT_EQ(caller->pc()->GetTransceivers()[0]->mid(), transceivers[1]->mid());
364 EXPECT_FALSE(transceivers[1]->stopped());
365}
366
367// Test that audio and video transceivers created on the remote side with
368// AddTrack will all be reused if there is the same number of audio/video tracks
369// in the remote offer. Additionally, this tests that transceivers are
370// successfully matched even if they are in a different order on the remote
371// side.
372TEST_F(PeerConnectionJsepTest, SetRemoteOfferReusesTransceiversOfBothTypes) {
373 auto caller = CreatePeerConnection();
374 caller->AddVideoTrack("v");
375 caller->AddAudioTrack("a");
376 auto callee = CreatePeerConnection();
377 callee->AddAudioTrack("a");
378 callee->AddVideoTrack("v");
379
380 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
381
382 auto caller_transceivers = caller->pc()->GetTransceivers();
383 auto callee_transceivers = callee->pc()->GetTransceivers();
384 ASSERT_EQ(2u, callee_transceivers.size());
385 EXPECT_EQ(caller_transceivers[0]->mid(), callee_transceivers[1]->mid());
386 EXPECT_EQ(caller_transceivers[1]->mid(), callee_transceivers[0]->mid());
387}
388
389// Tests for JSEP initial CreateAnswer.
390
391// Test that the answer to a remote offer creates media sections for each
392// offered media in the same order and with the same mids.
393TEST_F(PeerConnectionJsepTest, CreateAnswerHasSameMidsAsOffer) {
394 auto caller = CreatePeerConnection();
395 auto first_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
396 auto second_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
397 auto third_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
Steve Antonfa2260d2017-12-28 16:38:23 -0800398 caller->CreateDataChannel("dc");
Steve Antondcc3c022017-12-22 16:02:54 -0800399 auto callee = CreatePeerConnection();
400
Steve Antonfa2260d2017-12-28 16:38:23 -0800401 auto offer = caller->CreateOffer();
402 const auto* offer_data = cricket::GetFirstDataContent(offer->description());
403 ASSERT_TRUE(
404 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
405 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Steve Antondcc3c022017-12-22 16:02:54 -0800406
407 auto answer = callee->CreateAnswer();
408 auto contents = answer->description()->contents();
Steve Antonfa2260d2017-12-28 16:38:23 -0800409 ASSERT_EQ(4u, contents.size());
Steve Antondcc3c022017-12-22 16:02:54 -0800410 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, contents[0].media_description()->type());
Steve Antonfa2260d2017-12-28 16:38:23 -0800411 EXPECT_EQ(first_transceiver->mid(), contents[0].name);
Steve Antondcc3c022017-12-22 16:02:54 -0800412 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, contents[1].media_description()->type());
Steve Antonfa2260d2017-12-28 16:38:23 -0800413 EXPECT_EQ(second_transceiver->mid(), contents[1].name);
Steve Antondcc3c022017-12-22 16:02:54 -0800414 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, contents[2].media_description()->type());
Steve Antonfa2260d2017-12-28 16:38:23 -0800415 EXPECT_EQ(third_transceiver->mid(), contents[2].name);
416 EXPECT_EQ(cricket::MEDIA_TYPE_DATA, contents[3].media_description()->type());
417 EXPECT_EQ(offer_data->name, contents[3].name);
Steve Antondcc3c022017-12-22 16:02:54 -0800418}
419
420// Test that an answering media section is marked as rejected if the underlying
421// transceiver has been stopped.
422TEST_F(PeerConnectionJsepTest, CreateAnswerRejectsStoppedTransceiver) {
423 auto caller = CreatePeerConnection();
424 caller->AddAudioTrack("a");
425 auto callee = CreatePeerConnection();
426
427 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
428
429 callee->pc()->GetTransceivers()[0]->Stop();
430
431 auto answer = callee->CreateAnswer();
432 auto contents = answer->description()->contents();
433 ASSERT_EQ(1u, contents.size());
434 EXPECT_TRUE(contents[0].rejected);
435}
436
437// Test that CreateAnswer will generate media sections which will only send or
438// receive if the offer indicates it can do the reciprocating direction.
439// The full matrix is tested more extensively in MediaSession.
440TEST_F(PeerConnectionJsepTest, CreateAnswerNegotiatesDirection) {
441 auto caller = CreatePeerConnection();
442 RtpTransceiverInit init;
443 init.direction = RtpTransceiverDirection::kSendOnly;
444 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init);
445 auto callee = CreatePeerConnection();
446 callee->AddAudioTrack("a");
447
448 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
449
450 auto answer = callee->CreateAnswer();
451 auto contents = answer->description()->contents();
452 ASSERT_EQ(1u, contents.size());
453 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
454 contents[0].media_description()->direction());
455}
456
457// Tests for JSEP SetLocalDescription with a local answer.
458// Note that these test only the additional behaviors not covered by
459// SetLocalDescription with a local offer.
460
461// Test that SetLocalDescription with an answer sets the current_direction
462// property of the transceivers mentioned in the session description.
463TEST_F(PeerConnectionJsepTest, SetLocalAnswerUpdatesCurrentDirection) {
464 auto caller = CreatePeerConnection();
465 auto caller_audio = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
466 caller_audio->SetDirection(RtpTransceiverDirection::kRecvOnly);
467 auto callee = CreatePeerConnection();
468 callee->AddAudioTrack("a");
469
470 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
471 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
472
473 auto transceivers = callee->pc()->GetTransceivers();
474 ASSERT_EQ(1u, transceivers.size());
475 // Since the offer was recvonly and the transceiver direction is sendrecv,
476 // the negotiated direction will be sendonly.
477 EXPECT_EQ(RtpTransceiverDirection::kSendOnly,
478 transceivers[0]->current_direction());
479}
480
481// Tests for JSEP SetRemoteDescription with a remote answer.
482// Note that these test only the additional behaviors not covered by
483// SetRemoteDescription with a remote offer.
484
485TEST_F(PeerConnectionJsepTest, SetRemoteAnswerUpdatesCurrentDirection) {
486 auto caller = CreatePeerConnection();
487 caller->AddAudioTrack("a");
488 auto callee = CreatePeerConnection();
489 callee->AddAudioTrack("a");
490 auto callee_audio = callee->pc()->GetTransceivers()[0];
491 callee_audio->SetDirection(RtpTransceiverDirection::kSendOnly);
492
493 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
494 ASSERT_TRUE(
495 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
496
497 auto transceivers = caller->pc()->GetTransceivers();
498 ASSERT_EQ(1u, transceivers.size());
499 // Since the remote transceiver was set to sendonly, the negotiated direction
500 // in the answer would be sendonly which we apply as recvonly to the local
501 // transceiver.
502 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly,
503 transceivers[0]->current_direction());
504}
505
506// Tests for multiple round trips.
507
508// Test that setting a transceiver with the inactive direction does not stop it
509// on either the caller or the callee.
510TEST_F(PeerConnectionJsepTest, SettingTransceiverInactiveDoesNotStopIt) {
511 auto caller = CreatePeerConnection();
512 caller->AddAudioTrack("a");
513 auto callee = CreatePeerConnection();
514 callee->AddAudioTrack("a");
515 callee->pc()->GetTransceivers()[0]->SetDirection(
516 RtpTransceiverDirection::kInactive);
517
518 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
519 ASSERT_TRUE(
520 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
521
522 EXPECT_FALSE(caller->pc()->GetTransceivers()[0]->stopped());
523 EXPECT_FALSE(callee->pc()->GetTransceivers()[0]->stopped());
524}
525
526// Test that if a transceiver had been associated and later stopped, then a
527// media section is still generated for it and the media section is marked as
528// rejected.
529TEST_F(PeerConnectionJsepTest,
530 ReOfferMediaSectionForAssociatedStoppedTransceiverIsRejected) {
531 auto caller = CreatePeerConnection();
532 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
533 auto callee = CreatePeerConnection();
534
535 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
536 ASSERT_TRUE(
537 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
538
539 ASSERT_TRUE(transceiver->mid());
540 transceiver->Stop();
541
542 auto reoffer = caller->CreateOffer();
543 auto contents = reoffer->description()->contents();
544 ASSERT_EQ(1u, contents.size());
545 EXPECT_TRUE(contents[0].rejected);
546}
547
548// Test that stopping an associated transceiver on the caller side will stop the
549// corresponding transceiver on the remote side when the remote offer is
550// applied.
551TEST_F(PeerConnectionJsepTest,
552 StoppingTransceiverInOfferStopsTransceiverOnRemoteSide) {
553 auto caller = CreatePeerConnection();
554 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
555 auto callee = CreatePeerConnection();
556
557 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
558 ASSERT_TRUE(
559 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
560
561 transceiver->Stop();
562
563 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
564
565 auto transceivers = callee->pc()->GetTransceivers();
566 EXPECT_TRUE(transceivers[0]->stopped());
567 EXPECT_TRUE(transceivers[0]->mid());
568}
569
570// Test that CreateOffer will only generate a recycled media section if the
571// transceiver to be recycled has been seen stopped by the other side first.
572TEST_F(PeerConnectionJsepTest,
573 CreateOfferDoesNotRecycleMediaSectionIfFirstStopped) {
574 auto caller = CreatePeerConnection();
575 auto first_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
576 auto callee = CreatePeerConnection();
577
578 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
579 ASSERT_TRUE(
580 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
581
582 auto second_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
583 first_transceiver->Stop();
584
585 auto reoffer = caller->CreateOffer();
586 auto contents = reoffer->description()->contents();
587 ASSERT_EQ(2u, contents.size());
588 EXPECT_TRUE(contents[0].rejected);
589 EXPECT_FALSE(contents[1].rejected);
590}
591
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800592// Test that the offer/answer and the transceivers are correctly generated and
593// updated when the media section is recycled after the callee stops a
594// transceiver and sends an answer with a 0 port.
595TEST_F(PeerConnectionJsepTest,
596 RecycleMediaSectionWhenStoppingTransceiverOnAnswerer) {
597 auto caller = CreatePeerConnection();
598 auto first_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
599 auto callee = CreatePeerConnection();
600
601 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
602 callee->pc()->GetTransceivers()[0]->Stop();
603 ASSERT_TRUE(
604 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
605 EXPECT_TRUE(first_transceiver->stopped());
606 // First transceivers aren't dissociated yet.
607 ASSERT_NE(rtc::nullopt, first_transceiver->mid());
608 std::string first_mid = *first_transceiver->mid();
609 EXPECT_EQ(first_mid, callee->pc()->GetTransceivers()[0]->mid());
610
611 // New offer exchange with new transceivers that recycles the m section
612 // correctly.
613 caller->AddAudioTrack("audio2");
614 callee->AddAudioTrack("audio2");
615 auto offer = caller->CreateOffer();
616 auto offer_contents = offer->description()->contents();
617 std::string second_mid = offer_contents[0].name;
618 ASSERT_EQ(1u, offer_contents.size());
619 EXPECT_FALSE(offer_contents[0].rejected);
620 EXPECT_NE(first_mid, second_mid);
621
622 // Setting the offer on each side will dissociate the first transceivers and
623 // associate the new transceivers.
624 ASSERT_TRUE(
625 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
626 EXPECT_EQ(rtc::nullopt, first_transceiver->mid());
627 EXPECT_EQ(second_mid, caller->pc()->GetTransceivers()[1]->mid());
628 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
629 EXPECT_EQ(rtc::nullopt, callee->pc()->GetTransceivers()[0]->mid());
630 EXPECT_EQ(second_mid, callee->pc()->GetTransceivers()[1]->mid());
631
632 // The new answer should also recycle the m section correctly.
633 auto answer = callee->CreateAnswer();
634 auto answer_contents = answer->description()->contents();
635 ASSERT_EQ(1u, answer_contents.size());
636 EXPECT_FALSE(answer_contents[0].rejected);
637 EXPECT_EQ(second_mid, answer_contents[0].name);
638
639 // Finishing the negotiation shouldn't add or dissociate any transceivers.
640 ASSERT_TRUE(
641 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
642 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
643 auto caller_transceivers = caller->pc()->GetTransceivers();
644 ASSERT_EQ(2u, caller_transceivers.size());
645 EXPECT_EQ(rtc::nullopt, caller_transceivers[0]->mid());
646 EXPECT_EQ(second_mid, caller_transceivers[1]->mid());
647 auto callee_transceivers = callee->pc()->GetTransceivers();
648 ASSERT_EQ(2u, callee_transceivers.size());
649 EXPECT_EQ(rtc::nullopt, callee_transceivers[0]->mid());
650 EXPECT_EQ(second_mid, callee_transceivers[1]->mid());
651}
652
653// Test that creating/setting a local offer that recycles an m= section is
654// idempotent.
655TEST_F(PeerConnectionJsepTest, CreateOfferRecyclesWhenOfferingTwice) {
656 // Do a negotiation with a port 0 for the media section.
657 auto caller = CreatePeerConnection();
658 auto first_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
659 auto callee = CreatePeerConnection();
660 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
661 first_transceiver->Stop();
662 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
663 caller->AddAudioTrack("audio2");
664
665 // Create a new offer that recycles the media section and set it as a local
666 // description.
667 auto offer = caller->CreateOffer();
668 auto offer_contents = offer->description()->contents();
669 ASSERT_EQ(1u, offer_contents.size());
670 EXPECT_FALSE(offer_contents[0].rejected);
671 ASSERT_TRUE(caller->SetLocalDescription(std::move(offer)));
672 EXPECT_FALSE(caller->pc()->GetTransceivers()[1]->stopped());
673 std::string second_mid = offer_contents[0].name;
674
675 // Create another new offer and set the local description again without the
676 // rest of any negotation ocurring.
677 auto second_offer = caller->CreateOffer();
678 auto second_offer_contents = second_offer->description()->contents();
679 ASSERT_EQ(1u, second_offer_contents.size());
680 EXPECT_FALSE(second_offer_contents[0].rejected);
681 // The mid shouldn't change.
682 EXPECT_EQ(second_mid, second_offer_contents[0].name);
683
684 ASSERT_TRUE(caller->SetLocalDescription(std::move(second_offer)));
685 // Make sure that the caller's transceivers are associated correctly.
686 auto caller_transceivers = caller->pc()->GetTransceivers();
687 ASSERT_EQ(2u, caller_transceivers.size());
688 EXPECT_EQ(rtc::nullopt, caller_transceivers[0]->mid());
689 EXPECT_EQ(second_mid, caller_transceivers[1]->mid());
690 EXPECT_FALSE(caller_transceivers[1]->stopped());
691}
692
Steve Antondcc3c022017-12-22 16:02:54 -0800693// Test that the offer/answer and transceivers for both the caller and callee
694// side are generated/updated correctly when recycling an audio/video media
695// section as a media section of either the same or opposite type.
696class RecycleMediaSectionTest
697 : public PeerConnectionJsepTest,
698 public testing::WithParamInterface<
699 std::tuple<cricket::MediaType, cricket::MediaType>> {
700 protected:
701 RecycleMediaSectionTest() {
702 first_type_ = std::get<0>(GetParam());
703 second_type_ = std::get<1>(GetParam());
704 }
705
706 cricket::MediaType first_type_;
707 cricket::MediaType second_type_;
708};
709
710TEST_P(RecycleMediaSectionTest, VerifyOfferAnswerAndTransceivers) {
711 auto caller = CreatePeerConnection();
712 auto first_transceiver = caller->AddTransceiver(first_type_);
713 auto callee = CreatePeerConnection();
714
715 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
716
717 std::string first_mid = *first_transceiver->mid();
718 first_transceiver->Stop();
719
720 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
721
722 auto second_transceiver = caller->AddTransceiver(second_type_);
723
724 // The offer should reuse the previous media section but allocate a new MID
725 // and change the media type.
726 auto offer = caller->CreateOffer();
727 auto offer_contents = offer->description()->contents();
728 ASSERT_EQ(1u, offer_contents.size());
729 EXPECT_FALSE(offer_contents[0].rejected);
730 EXPECT_EQ(second_type_, offer_contents[0].media_description()->type());
731 std::string second_mid = offer_contents[0].name;
732 EXPECT_NE(first_mid, second_mid);
733
734 // Setting the local offer will dissociate the previous transceiver and set
735 // the MID for the new transceiver.
736 ASSERT_TRUE(
737 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
738 EXPECT_EQ(rtc::nullopt, first_transceiver->mid());
739 EXPECT_EQ(second_mid, second_transceiver->mid());
740
741 // Setting the remote offer will dissociate the previous transceiver and
742 // create a new transceiver for the media section.
743 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
744 auto callee_transceivers = callee->pc()->GetTransceivers();
745 ASSERT_EQ(2u, callee_transceivers.size());
746 EXPECT_EQ(rtc::nullopt, callee_transceivers[0]->mid());
Steve Anton69470252018-02-09 11:43:08 -0800747 EXPECT_EQ(first_type_, callee_transceivers[0]->media_type());
Steve Antondcc3c022017-12-22 16:02:54 -0800748 EXPECT_EQ(second_mid, callee_transceivers[1]->mid());
Steve Anton69470252018-02-09 11:43:08 -0800749 EXPECT_EQ(second_type_, callee_transceivers[1]->media_type());
Steve Antondcc3c022017-12-22 16:02:54 -0800750
751 // The answer should have only one media section for the new transceiver.
752 auto answer = callee->CreateAnswer();
753 auto answer_contents = answer->description()->contents();
754 ASSERT_EQ(1u, answer_contents.size());
755 EXPECT_FALSE(answer_contents[0].rejected);
756 EXPECT_EQ(second_mid, answer_contents[0].name);
757 EXPECT_EQ(second_type_, answer_contents[0].media_description()->type());
758
759 // Setting the local answer should succeed.
760 ASSERT_TRUE(
761 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
762
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800763 // Setting the remote answer should succeed and not create any new
764 // transceivers.
Steve Antondcc3c022017-12-22 16:02:54 -0800765 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800766 ASSERT_EQ(2u, caller->pc()->GetTransceivers().size());
767 ASSERT_EQ(2u, callee->pc()->GetTransceivers().size());
Steve Antondcc3c022017-12-22 16:02:54 -0800768}
769
770// Test all combinations of audio and video as the first and second media type
771// for the media section. This is needed for full test coverage because
772// MediaSession has separate functions for processing audio and video media
773// sections.
774INSTANTIATE_TEST_CASE_P(
775 PeerConnectionJsepTest,
776 RecycleMediaSectionTest,
777 Combine(Values(cricket::MEDIA_TYPE_AUDIO, cricket::MEDIA_TYPE_VIDEO),
778 Values(cricket::MEDIA_TYPE_AUDIO, cricket::MEDIA_TYPE_VIDEO)));
779
Steve Antonfa2260d2017-12-28 16:38:23 -0800780// Test that a new data channel section will not reuse a recycleable audio or
781// video media section. Additionally, tests that the new section is added to the
782// end of the session description.
783TEST_F(PeerConnectionJsepTest, DataChannelDoesNotRecycleMediaSection) {
784 auto caller = CreatePeerConnection();
785 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
786 auto callee = CreatePeerConnection();
787
788 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
789
790 transceiver->Stop();
791
792 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
793
794 caller->CreateDataChannel("dc");
795
796 auto offer = caller->CreateOffer();
797 auto offer_contents = offer->description()->contents();
798 ASSERT_EQ(2u, offer_contents.size());
799 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO,
800 offer_contents[0].media_description()->type());
801 EXPECT_EQ(cricket::MEDIA_TYPE_DATA,
802 offer_contents[1].media_description()->type());
803
804 ASSERT_TRUE(
805 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
806 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
807
808 auto answer = callee->CreateAnswer();
809 auto answer_contents = answer->description()->contents();
810 ASSERT_EQ(2u, answer_contents.size());
811 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO,
812 answer_contents[0].media_description()->type());
813 EXPECT_EQ(cricket::MEDIA_TYPE_DATA,
814 answer_contents[1].media_description()->type());
815}
816
817// Test that if a new track is added to an existing session that has a data,
818// the new section comes at the end of the new offer, after the existing data
819// section.
820TEST_F(PeerConnectionJsepTest, AudioTrackAddedAfterDataSectionInReoffer) {
821 auto caller = CreatePeerConnection();
822 caller->CreateDataChannel("dc");
823 auto callee = CreatePeerConnection();
824
825 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
826
827 caller->AddAudioTrack("a");
828
829 auto offer = caller->CreateOffer();
830 auto contents = offer->description()->contents();
831 ASSERT_EQ(2u, contents.size());
832 EXPECT_EQ(cricket::MEDIA_TYPE_DATA, contents[0].media_description()->type());
833 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, contents[1].media_description()->type());
834}
835
Steve Antondcc3c022017-12-22 16:02:54 -0800836// Tests for MID properties.
837
838static void RenameSection(size_t mline_index,
839 const std::string& new_mid,
840 SessionDescriptionInterface* sdesc) {
841 cricket::SessionDescription* desc = sdesc->description();
842 std::string old_mid = desc->contents()[mline_index].name;
843 desc->contents()[mline_index].name = new_mid;
844 desc->transport_infos()[mline_index].content_name = new_mid;
845 const cricket::ContentGroup* bundle =
846 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
847 if (bundle) {
848 cricket::ContentGroup new_bundle = *bundle;
849 if (new_bundle.RemoveContentName(old_mid)) {
850 new_bundle.AddContentName(new_mid);
851 }
852 desc->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
853 desc->AddGroup(new_bundle);
854 }
855}
856
857// Test that two PeerConnections can have a successful offer/answer exchange if
858// the MIDs are changed from the defaults.
859TEST_F(PeerConnectionJsepTest, OfferAnswerWithChangedMids) {
860 constexpr char kFirstMid[] = "nondefaultmid";
861 constexpr char kSecondMid[] = "randommid";
862
863 auto caller = CreatePeerConnection();
864 caller->AddAudioTrack("a");
865 caller->AddAudioTrack("b");
866 auto callee = CreatePeerConnection();
867
868 auto offer = caller->CreateOffer();
869 RenameSection(0, kFirstMid, offer.get());
870 RenameSection(1, kSecondMid, offer.get());
871
872 ASSERT_TRUE(
873 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
874 auto caller_transceivers = caller->pc()->GetTransceivers();
875 EXPECT_EQ(kFirstMid, caller_transceivers[0]->mid());
876 EXPECT_EQ(kSecondMid, caller_transceivers[1]->mid());
877
878 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
879 auto callee_transceivers = callee->pc()->GetTransceivers();
880 EXPECT_EQ(kFirstMid, callee_transceivers[0]->mid());
881 EXPECT_EQ(kSecondMid, callee_transceivers[1]->mid());
882
883 auto answer = callee->CreateAnswer();
884 auto answer_contents = answer->description()->contents();
885 EXPECT_EQ(kFirstMid, answer_contents[0].name);
886 EXPECT_EQ(kSecondMid, answer_contents[1].name);
887
888 ASSERT_TRUE(
889 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
890 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
891}
892
893// Test that CreateOffer will generate a MID that is not already used if the
894// default it would have picked is already taken. This is tested by using a
895// third PeerConnection to determine what the default would be for the second
896// media section then setting that as the first media section's MID.
897TEST_F(PeerConnectionJsepTest, CreateOfferGeneratesUniqueMidIfAlreadyTaken) {
898 // First, find what the default MID is for the second media section.
899 auto pc = CreatePeerConnection();
900 pc->AddAudioTrack("a");
901 pc->AddAudioTrack("b");
902 auto default_offer = pc->CreateOffer();
903 std::string default_second_mid =
904 default_offer->description()->contents()[1].name;
905
906 // Now, do an offer/answer with one track which has the MID set to the default
907 // second MID.
908 auto caller = CreatePeerConnection();
909 caller->AddAudioTrack("a");
910 auto callee = CreatePeerConnection();
911
912 auto offer = caller->CreateOffer();
913 RenameSection(0, default_second_mid, offer.get());
914
915 ASSERT_TRUE(
916 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
917 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
918 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
919
920 // Add a second track and ensure that the MID is different.
921 caller->AddAudioTrack("b");
922
923 auto reoffer = caller->CreateOffer();
924 auto reoffer_contents = reoffer->description()->contents();
925 EXPECT_EQ(default_second_mid, reoffer_contents[0].name);
926 EXPECT_NE(reoffer_contents[0].name, reoffer_contents[1].name);
927}
928
Steve Antonfa2260d2017-12-28 16:38:23 -0800929// Test that if an audio or video section has the default data section MID, then
930// CreateOffer will generate a unique MID for the newly added data section.
931TEST_F(PeerConnectionJsepTest,
932 CreateOfferGeneratesUniqueMidForDataSectionIfAlreadyTaken) {
933 // First, find what the default MID is for the data channel.
934 auto pc = CreatePeerConnection();
935 pc->CreateDataChannel("dc");
936 auto default_offer = pc->CreateOffer();
937 std::string default_data_mid =
938 default_offer->description()->contents()[0].name;
939
940 // Now do an offer/answer with one audio track which has a MID set to the
941 // default data MID.
942 auto caller = CreatePeerConnection();
943 caller->AddAudioTrack("a");
944 auto callee = CreatePeerConnection();
945
946 auto offer = caller->CreateOffer();
947 RenameSection(0, default_data_mid, offer.get());
948
949 ASSERT_TRUE(
950 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
951 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
952 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
953
954 // Add a data channel and ensure that the MID is different.
955 caller->CreateDataChannel("dc");
956
957 auto reoffer = caller->CreateOffer();
958 auto reoffer_contents = reoffer->description()->contents();
959 EXPECT_EQ(default_data_mid, reoffer_contents[0].name);
960 EXPECT_NE(reoffer_contents[0].name, reoffer_contents[1].name);
961}
962
Steve Antondcc3c022017-12-22 16:02:54 -0800963// Test that a reoffer initiated by the callee adds a new track to the caller.
964TEST_F(PeerConnectionJsepTest, CalleeDoesReoffer) {
965 auto caller = CreatePeerConnection();
966 caller->AddAudioTrack("a");
967 auto callee = CreatePeerConnection();
968 callee->AddAudioTrack("a");
969 callee->AddVideoTrack("v");
970
971 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
972
973 EXPECT_EQ(1u, caller->pc()->GetTransceivers().size());
974 EXPECT_EQ(2u, callee->pc()->GetTransceivers().size());
975
976 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
977
978 EXPECT_EQ(2u, caller->pc()->GetTransceivers().size());
979 EXPECT_EQ(2u, callee->pc()->GetTransceivers().size());
980}
981
Steve Anton02ee47c2018-01-10 16:26:06 -0800982// Tests for MSID properties.
983
984// Test that adding a track with AddTrack results in an offer that signals the
985// track's ID.
986TEST_F(PeerConnectionJsepTest, AddingTrackWithAddTrackSpecifiesTrackId) {
987 const std::string kTrackId = "audio_track";
988
989 auto caller = CreatePeerConnection();
990 caller->AddAudioTrack(kTrackId);
991
992 auto offer = caller->CreateOffer();
993 auto contents = offer->description()->contents();
994 ASSERT_EQ(1u, contents.size());
995 auto streams = contents[0].media_description()->streams();
996 ASSERT_EQ(1u, streams.size());
997 EXPECT_EQ(kTrackId, streams[0].id);
998}
999
1000// Test that adding a track by calling AddTransceiver then SetTrack results in
1001// an offer that does not signal the track's ID and signals a random ID.
1002TEST_F(PeerConnectionJsepTest,
1003 AddingTrackWithAddTransceiverSpecifiesRandomTrackId) {
1004 const std::string kTrackId = "audio_track";
1005
1006 auto caller = CreatePeerConnection();
1007 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1008 transceiver->sender()->SetTrack(caller->CreateAudioTrack(kTrackId));
1009
1010 auto offer = caller->CreateOffer();
1011 auto contents = offer->description()->contents();
1012 ASSERT_EQ(1u, contents.size());
1013 auto streams = contents[0].media_description()->streams();
1014 ASSERT_EQ(1u, streams.size());
1015 EXPECT_NE(kTrackId, streams[0].id);
1016}
1017
Steve Anton5f94aa22018-02-01 10:58:30 -08001018// Test that if the transceiver is recvonly or inactive, then no MSID
1019// information is included in the offer.
1020TEST_F(PeerConnectionJsepTest, NoMsidInOfferIfTransceiverDirectionHasNoSend) {
1021 auto caller = CreatePeerConnection();
1022
1023 RtpTransceiverInit init_recvonly;
1024 init_recvonly.direction = RtpTransceiverDirection::kRecvOnly;
1025 ASSERT_TRUE(caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init_recvonly));
1026
1027 RtpTransceiverInit init_inactive;
1028 init_inactive.direction = RtpTransceiverDirection::kInactive;
1029 ASSERT_TRUE(caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init_inactive));
1030
1031 auto offer = caller->CreateOffer();
1032 auto contents = offer->description()->contents();
1033 ASSERT_EQ(2u, contents.size());
1034 // MSID is specified in the first stream, so no streams means no MSID.
1035 EXPECT_EQ(0u, contents[0].media_description()->streams().size());
1036 EXPECT_EQ(0u, contents[1].media_description()->streams().size());
1037}
1038
1039// Test that if an answer negotiates transceiver directions of recvonly or
1040// inactive, then no MSID information is included in the answer.
1041TEST_F(PeerConnectionJsepTest, NoMsidInAnswerIfNoRespondingTracks) {
1042 auto caller = CreatePeerConnection();
1043 auto callee = CreatePeerConnection();
1044
1045 // recvonly transceiver will get negotiated to inactive since the callee has
1046 // no tracks to send in response.
1047 RtpTransceiverInit init_recvonly;
1048 init_recvonly.direction = RtpTransceiverDirection::kRecvOnly;
1049 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init_recvonly);
1050
1051 // sendrecv transceiver will get negotiated to recvonly since the callee has
1052 // no tracks to send in response.
1053 RtpTransceiverInit init_sendrecv;
1054 init_sendrecv.direction = RtpTransceiverDirection::kSendRecv;
1055 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init_sendrecv);
1056
1057 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1058
1059 auto answer = callee->CreateAnswer();
1060 auto contents = answer->description()->contents();
1061 ASSERT_EQ(2u, contents.size());
1062 // MSID is specified in the first stream, so no streams means no MSID.
1063 EXPECT_EQ(0u, contents[0].media_description()->streams().size());
1064 EXPECT_EQ(0u, contents[1].media_description()->streams().size());
1065}
1066
1067// Test that the MSID is included even if the transceiver direction has changed
1068// to inactive if the transceiver had previously sent media.
1069TEST_F(PeerConnectionJsepTest, IncludeMsidEvenIfDirectionHasChanged) {
1070 auto caller = CreatePeerConnection();
1071 caller->AddAudioTrack("audio");
1072 auto callee = CreatePeerConnection();
1073 callee->AddAudioTrack("audio");
1074
1075 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1076
1077 caller->pc()->GetTransceivers()[0]->SetDirection(
1078 RtpTransceiverDirection::kInactive);
1079
1080 // The transceiver direction on both sides will turn to inactive.
1081 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1082
1083 auto* offer = callee->pc()->remote_description();
1084 auto offer_contents = offer->description()->contents();
1085 ASSERT_EQ(1u, offer_contents.size());
1086 // MSID is specified in the first stream. If it is present, assume that MSID
1087 // is there.
1088 EXPECT_EQ(1u, offer_contents[0].media_description()->streams().size());
1089
1090 auto* answer = caller->pc()->remote_description();
1091 auto answer_contents = answer->description()->contents();
1092 ASSERT_EQ(1u, answer_contents.size());
1093 EXPECT_EQ(1u, answer_contents[0].media_description()->streams().size());
1094}
1095
1096// Test that stopping a RtpTransceiver will cause future offers to not include
1097// any MSID information for that section.
1098TEST_F(PeerConnectionJsepTest, RemoveMsidIfTransceiverStopped) {
1099 auto caller = CreatePeerConnection();
1100 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1101 auto callee = CreatePeerConnection();
1102
1103 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1104
1105 transceiver->Stop();
1106
1107 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1108
1109 auto* offer = callee->pc()->remote_description();
1110 auto offer_contents = offer->description()->contents();
1111 ASSERT_EQ(1u, offer_contents.size());
1112 // MSID is specified in the first stream, so no streams means no MSID.
1113 EXPECT_EQ(0u, offer_contents[0].media_description()->streams().size());
1114}
1115
Steve Anton02ee47c2018-01-10 16:26:06 -08001116// Test that the callee RtpReceiver created by a call to SetRemoteDescription
1117// has its ID set to the signaled track ID.
1118TEST_F(PeerConnectionJsepTest,
1119 RtpReceiverCreatedBySetRemoteDescriptionHasSignaledTrackId) {
1120 const std::string kTrackId = "audio_track";
1121
1122 auto caller = CreatePeerConnection();
1123 auto callee = CreatePeerConnection();
1124 caller->AddAudioTrack(kTrackId);
1125
1126 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1127
1128 ASSERT_EQ(1u, callee->pc()->GetReceivers().size());
1129 auto receiver = callee->pc()->GetReceivers()[0];
1130 EXPECT_EQ(kTrackId, receiver->id());
1131}
1132
1133// Test that if the callee RtpReceiver is reused by a call to
1134// SetRemoteDescription, its ID does not change.
1135TEST_F(PeerConnectionJsepTest,
1136 RtpReceiverCreatedBeforeSetRemoteDescriptionKeepsId) {
1137 const std::string kTrackId = "audio_track";
1138
1139 auto caller = CreatePeerConnection();
1140 auto callee = CreatePeerConnection();
1141 caller->AddAudioTrack(kTrackId);
1142 callee->AddAudioTrack("dummy_track");
1143
1144 ASSERT_EQ(1u, callee->pc()->GetReceivers().size());
1145 auto receiver = callee->pc()->GetReceivers()[0];
1146 std::string receiver_id = receiver->id();
1147
1148 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1149
1150 EXPECT_EQ(receiver_id, receiver->id());
1151}
1152
Steve Antonef65ef12018-01-10 17:15:20 -08001153// Test that setting a remote offer with one track that has no streams fires off
1154// the correct OnAddTrack event.
1155TEST_F(PeerConnectionJsepTest,
1156 SetRemoteOfferWithOneTrackNoStreamFiresOnAddTrack) {
1157 const std::string kTrackLabel = "audio_track";
1158
1159 auto caller = CreatePeerConnection();
1160 auto callee = CreatePeerConnection();
1161 ASSERT_TRUE(caller->AddAudioTrack(kTrackLabel));
1162
1163 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1164
Seth Hampson5b4f0752018-04-02 16:31:36 -07001165 const auto& track_events = callee->observer()->add_track_events_;
1166 ASSERT_EQ(1u, track_events.size());
1167 const auto& event = track_events[0];
1168 EXPECT_EQ(kTrackLabel, event.receiver->track()->id());
1169 EXPECT_EQ(0u, event.streams.size());
Steve Antonef65ef12018-01-10 17:15:20 -08001170}
1171
1172// Test that setting a remote offer with one track that has one stream fires off
1173// the correct OnAddTrack event.
1174TEST_F(PeerConnectionJsepTest,
1175 SetRemoteOfferWithOneTrackOneStreamFiresOnAddTrack) {
1176 const std::string kTrackLabel = "audio_track";
Seth Hampson845e8782018-03-02 11:34:10 -08001177 const std::string kStreamId = "audio_stream";
Steve Antonef65ef12018-01-10 17:15:20 -08001178
1179 auto caller = CreatePeerConnection();
1180 auto callee = CreatePeerConnection();
Seth Hampson845e8782018-03-02 11:34:10 -08001181 ASSERT_TRUE(caller->AddAudioTrack(kTrackLabel, {kStreamId}));
Steve Antonef65ef12018-01-10 17:15:20 -08001182
1183 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1184
1185 const auto& track_events = callee->observer()->add_track_events_;
1186 ASSERT_EQ(1u, track_events.size());
1187 const auto& event = track_events[0];
1188 ASSERT_EQ(1u, event.streams.size());
1189 auto stream = event.streams[0];
Seth Hampson13b8bad2018-03-13 16:05:28 -07001190 EXPECT_EQ(kStreamId, stream->id());
Steve Antonef65ef12018-01-10 17:15:20 -08001191 EXPECT_THAT(track_events[0].snapshotted_stream_tracks.at(stream),
1192 ElementsAre(event.receiver->track()));
1193 EXPECT_EQ(event.receiver->streams(), track_events[0].streams);
1194}
1195
1196// Test that setting a remote offer with two tracks that share the same stream
1197// fires off two OnAddTrack events, both with the same stream that has both
1198// tracks present at the time of firing. This is to ensure that track events are
1199// not fired until SetRemoteDescription has finished processing all the media
1200// sections.
1201TEST_F(PeerConnectionJsepTest,
1202 SetRemoteOfferWithTwoTracksSameStreamFiresOnAddTrack) {
1203 const std::string kTrack1Label = "audio_track1";
1204 const std::string kTrack2Label = "audio_track2";
Seth Hampson845e8782018-03-02 11:34:10 -08001205 const std::string kSharedStreamId = "stream";
Steve Antonef65ef12018-01-10 17:15:20 -08001206
1207 auto caller = CreatePeerConnection();
1208 auto callee = CreatePeerConnection();
Seth Hampson845e8782018-03-02 11:34:10 -08001209 ASSERT_TRUE(caller->AddAudioTrack(kTrack1Label, {kSharedStreamId}));
1210 ASSERT_TRUE(caller->AddAudioTrack(kTrack2Label, {kSharedStreamId}));
Steve Antonef65ef12018-01-10 17:15:20 -08001211
1212 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1213
1214 const auto& track_events = callee->observer()->add_track_events_;
1215 ASSERT_EQ(2u, track_events.size());
1216 const auto& event1 = track_events[0];
1217 const auto& event2 = track_events[1];
1218 ASSERT_EQ(1u, event1.streams.size());
1219 auto stream = event1.streams[0];
1220 ASSERT_THAT(event2.streams, ElementsAre(stream));
1221 auto track1 = event1.receiver->track();
1222 auto track2 = event2.receiver->track();
1223 EXPECT_THAT(event1.snapshotted_stream_tracks.at(stream),
1224 UnorderedElementsAre(track1, track2));
1225 EXPECT_THAT(event2.snapshotted_stream_tracks.at(stream),
1226 UnorderedElementsAre(track1, track2));
1227}
1228
Seth Hampson5b4f0752018-04-02 16:31:36 -07001229// Test that setting a remote offer with one track that has two streams fires
1230// off the correct OnAddTrack event.
1231TEST_F(PeerConnectionJsepTest,
1232 SetRemoteOfferWithOneTrackTwoStreamFiresOnAddTrack) {
1233 const std::string kTrackLabel = "audio_track";
1234 const std::string kStreamId1 = "audio_stream1";
1235 const std::string kStreamId2 = "audio_stream2";
1236
1237 auto caller = CreatePeerConnection();
1238 auto callee = CreatePeerConnection();
1239 ASSERT_TRUE(caller->AddAudioTrack(kTrackLabel, {kStreamId1, kStreamId2}));
1240
1241 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1242
1243 const auto& track_events = callee->observer()->add_track_events_;
1244 ASSERT_EQ(1u, track_events.size());
1245 const auto& event = track_events[0];
1246 ASSERT_EQ(2u, event.streams.size());
1247 EXPECT_EQ(kStreamId1, event.streams[0]->id());
1248 EXPECT_EQ(kStreamId2, event.streams[1]->id());
1249}
Steve Antonef65ef12018-01-10 17:15:20 -08001250
Steve Anton54b84072018-02-20 15:19:52 -08001251// Test that if an RtpTransceiver with a current_direction set is stopped, then
1252// current_direction is changed to null.
1253TEST_F(PeerConnectionJsepTest, CurrentDirectionResetWhenRtpTransceiverStopped) {
1254 auto caller = CreatePeerConnection();
1255 auto callee = CreatePeerConnection();
1256
1257 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1258
1259 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1260
1261 ASSERT_TRUE(transceiver->current_direction());
1262 transceiver->Stop();
1263 EXPECT_FALSE(transceiver->current_direction());
1264}
1265
Harald Alvestrand5081c0c2018-03-09 15:18:03 +01001266// Tests that you can't set an answer on a PeerConnection before setting
1267// the offer.
1268TEST_F(PeerConnectionJsepTest, AnswerBeforeOfferFails) {
1269 auto caller = CreatePeerConnection();
1270 auto callee = CreatePeerConnection();
1271 RTCError error_out;
1272 caller->AddAudioTrack("audio");
1273 auto offer = caller->CreateOffer();
1274 callee->SetRemoteDescription(std::move(offer), &error_out);
1275 auto answer = callee->CreateAnswer();
1276 EXPECT_FALSE(caller->SetRemoteDescription(std::move(answer), &error_out));
1277 EXPECT_EQ(RTCErrorType::INVALID_STATE, error_out.type());
1278}
1279
Steve Antondcc3c022017-12-22 16:02:54 -08001280} // namespace webrtc