blob: 48c14e3ab7522a9d7cd8318d42bb74f0cc5893ef [file] [log] [blame]
Henrik Boström933d8b02017-10-10 10:05:16 -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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stdint.h>
Henrik Boström933d8b02017-10-10 10:05:16 -070012#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string>
14#include <utility>
Henrik Boström933d8b02017-10-10 10:05:16 -070015#include <vector>
16
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/types/optional.h"
19#include "api/audio/audio_mixer.h"
20#include "api/audio_codecs/audio_decoder_factory.h"
21#include "api/audio_codecs/audio_encoder_factory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020022#include "api/audio_codecs/builtin_audio_decoder_factory.h"
23#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010024#include "api/create_peerconnection_factory.h"
Henrik Boström933d8b02017-10-10 10:05:16 -070025#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/media_stream_interface.h"
27#include "api/media_types.h"
28#include "api/peer_connection_interface.h"
29#include "api/rtc_error.h"
30#include "api/rtp_parameters.h"
31#include "api/rtp_receiver_interface.h"
32#include "api/rtp_sender_interface.h"
33#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010034#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "api/set_remote_description_observer_interface.h"
36#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020037#include "api/video_codecs/builtin_video_decoder_factory.h"
38#include "api/video_codecs/builtin_video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010039#include "api/video_codecs/video_decoder_factory.h"
40#include "api/video_codecs/video_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/base/stream_params.h"
Yves Gerey3e707812018-11-28 16:47:49 +010042#include "modules/audio_device/include/audio_device.h"
43#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/port_allocator.h"
45#include "pc/media_session.h"
46#include "pc/peer_connection_wrapper.h"
47#include "pc/sdp_utils.h"
48#include "pc/session_description.h"
49#include "pc/test/fake_audio_capture_module.h"
50#include "pc/test/mock_peer_connection_observers.h"
Henrik Boström933d8b02017-10-10 10:05:16 -070051#include "rtc_base/checks.h"
52#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080053#include "rtc_base/ref_counted_object.h"
54#include "rtc_base/rtc_certificate_generator.h"
Henrik Boström933d8b02017-10-10 10:05:16 -070055#include "rtc_base/thread.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020056#include "system_wrappers/include/metrics.h"
Steve Anton9158ef62017-11-27 13:01:52 -080057#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010058#include "test/gtest.h"
Henrik Boström933d8b02017-10-10 10:05:16 -070059
60// This file contains tests for RTP Media API-related behavior of
61// |webrtc::PeerConnection|, see https://w3c.github.io/webrtc-pc/#rtp-media-api.
62
Steve Anton9158ef62017-11-27 13:01:52 -080063namespace webrtc {
64
65using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
66using ::testing::ElementsAre;
Steve Antonc1e6e862019-03-04 14:43:44 -080067using ::testing::Pair;
Steve Anton9158ef62017-11-27 13:01:52 -080068using ::testing::UnorderedElementsAre;
Steve Anton3172c032018-05-03 15:30:18 -070069using ::testing::Values;
Henrik Boström933d8b02017-10-10 10:05:16 -070070
Henrik Boström31638672017-11-23 17:48:32 +010071const uint32_t kDefaultTimeout = 10000u;
72
73template <typename MethodFunctor>
74class OnSuccessObserver : public rtc::RefCountedObject<
75 webrtc::SetRemoteDescriptionObserverInterface> {
76 public:
77 explicit OnSuccessObserver(MethodFunctor on_success)
78 : on_success_(std::move(on_success)) {}
79
80 // webrtc::SetRemoteDescriptionObserverInterface implementation.
81 void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override {
82 RTC_CHECK(error.ok());
83 on_success_();
84 }
85
86 private:
87 MethodFunctor on_success_;
88};
89
Steve Anton3172c032018-05-03 15:30:18 -070090class PeerConnectionRtpBaseTest : public testing::Test {
Henrik Boström933d8b02017-10-10 10:05:16 -070091 public:
Steve Anton3172c032018-05-03 15:30:18 -070092 explicit PeerConnectionRtpBaseTest(SdpSemantics sdp_semantics)
93 : sdp_semantics_(sdp_semantics),
94 pc_factory_(
Steve Anton9158ef62017-11-27 13:01:52 -080095 CreatePeerConnectionFactory(rtc::Thread::Current(),
96 rtc::Thread::Current(),
97 rtc::Thread::Current(),
98 FakeAudioCaptureModule::Create(),
99 CreateBuiltinAudioEncoderFactory(),
100 CreateBuiltinAudioDecoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +0200101 CreateBuiltinVideoEncoderFactory(),
102 CreateBuiltinVideoDecoderFactory(),
103 nullptr /* audio_mixer */,
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700104 nullptr /* audio_processing */)) {
105 webrtc::metrics::Reset();
106 }
Henrik Boström933d8b02017-10-10 10:05:16 -0700107
Steve Anton9158ef62017-11-27 13:01:52 -0800108 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnection() {
109 return CreatePeerConnection(RTCConfiguration());
110 }
111
Steve Antone831b8c2018-02-01 12:22:16 -0800112 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWithPlanB() {
113 RTCConfiguration config;
114 config.sdp_semantics = SdpSemantics::kPlanB;
Steve Anton3172c032018-05-03 15:30:18 -0700115 return CreatePeerConnectionInternal(config);
Steve Antone831b8c2018-02-01 12:22:16 -0800116 }
117
Steve Anton9158ef62017-11-27 13:01:52 -0800118 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWithUnifiedPlan() {
119 RTCConfiguration config;
120 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Steve Anton3172c032018-05-03 15:30:18 -0700121 return CreatePeerConnectionInternal(config);
Steve Anton9158ef62017-11-27 13:01:52 -0800122 }
123
124 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnection(
125 const RTCConfiguration& config) {
Steve Anton3172c032018-05-03 15:30:18 -0700126 RTCConfiguration modified_config = config;
127 modified_config.sdp_semantics = sdp_semantics_;
128 return CreatePeerConnectionInternal(modified_config);
129 }
130
131 protected:
132 const SdpSemantics sdp_semantics_;
133 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
134
135 private:
136 // Private so that tests don't accidentally bypass the SdpSemantics
137 // adjustment.
138 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionInternal(
139 const RTCConfiguration& config) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200140 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Henrik Boström933d8b02017-10-10 10:05:16 -0700141 auto pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr,
142 observer.get());
Yves Gerey4e933292018-10-31 15:36:05 +0100143 EXPECT_TRUE(pc.get());
144 observer->SetPeerConnectionInterface(pc.get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200145 return absl::make_unique<PeerConnectionWrapper>(pc_factory_, pc,
146 std::move(observer));
Henrik Boström933d8b02017-10-10 10:05:16 -0700147 }
Steve Anton3172c032018-05-03 15:30:18 -0700148};
Henrik Boström933d8b02017-10-10 10:05:16 -0700149
Steve Anton3172c032018-05-03 15:30:18 -0700150class PeerConnectionRtpTest
151 : public PeerConnectionRtpBaseTest,
152 public ::testing::WithParamInterface<SdpSemantics> {
Henrik Boström933d8b02017-10-10 10:05:16 -0700153 protected:
Steve Anton3172c032018-05-03 15:30:18 -0700154 PeerConnectionRtpTest() : PeerConnectionRtpBaseTest(GetParam()) {}
155};
156
157class PeerConnectionRtpTestPlanB : public PeerConnectionRtpBaseTest {
158 protected:
159 PeerConnectionRtpTestPlanB()
160 : PeerConnectionRtpBaseTest(SdpSemantics::kPlanB) {}
161};
162
163class PeerConnectionRtpTestUnifiedPlan : public PeerConnectionRtpBaseTest {
164 protected:
165 PeerConnectionRtpTestUnifiedPlan()
166 : PeerConnectionRtpBaseTest(SdpSemantics::kUnifiedPlan) {}
Henrik Boström933d8b02017-10-10 10:05:16 -0700167};
168
Henrik Boström31638672017-11-23 17:48:32 +0100169// These tests cover |webrtc::PeerConnectionObserver| callbacks firing upon
170// setting the remote description.
Henrik Boström31638672017-11-23 17:48:32 +0100171
Steve Anton3172c032018-05-03 15:30:18 -0700172TEST_P(PeerConnectionRtpTest, AddTrackWithoutStreamFiresOnAddTrack) {
Henrik Boström933d8b02017-10-10 10:05:16 -0700173 auto caller = CreatePeerConnection();
174 auto callee = CreatePeerConnection();
175
Steve Anton3172c032018-05-03 15:30:18 -0700176 ASSERT_TRUE(caller->AddAudioTrack("audio_track"));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700177 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700178
Henrik Boström31638672017-11-23 17:48:32 +0100179 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Steve Anton3172c032018-05-03 15:30:18 -0700180 const auto& add_track_event = callee->observer()->add_track_events_[0];
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100181 EXPECT_EQ(add_track_event.streams, add_track_event.receiver->streams());
Steve Anton3172c032018-05-03 15:30:18 -0700182
183 if (sdp_semantics_ == SdpSemantics::kPlanB) {
184 // Since we are not supporting the no stream case with Plan B, there should
185 // be a generated stream, even though we didn't set one with AddTrack.
186 ASSERT_EQ(1u, add_track_event.streams.size());
187 EXPECT_TRUE(add_track_event.streams[0]->FindAudioTrack("audio_track"));
188 } else {
189 EXPECT_EQ(0u, add_track_event.streams.size());
190 }
Henrik Boström933d8b02017-10-10 10:05:16 -0700191}
192
Steve Anton3172c032018-05-03 15:30:18 -0700193TEST_P(PeerConnectionRtpTest, AddTrackWithStreamFiresOnAddTrack) {
Henrik Boström933d8b02017-10-10 10:05:16 -0700194 auto caller = CreatePeerConnection();
195 auto callee = CreatePeerConnection();
196
Steve Anton3172c032018-05-03 15:30:18 -0700197 ASSERT_TRUE(caller->AddAudioTrack("audio_track", {"audio_stream"}));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700198 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700199
Henrik Boström31638672017-11-23 17:48:32 +0100200 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100201 auto& add_track_event = callee->observer()->add_track_events_[0];
Henrik Boström31638672017-11-23 17:48:32 +0100202 ASSERT_EQ(add_track_event.streams.size(), 1u);
Seth Hampson13b8bad2018-03-13 16:05:28 -0700203 EXPECT_EQ("audio_stream", add_track_event.streams[0]->id());
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100204 EXPECT_TRUE(add_track_event.streams[0]->FindAudioTrack("audio_track"));
205 EXPECT_EQ(add_track_event.streams, add_track_event.receiver->streams());
Henrik Boström933d8b02017-10-10 10:05:16 -0700206}
207
Steve Anton3172c032018-05-03 15:30:18 -0700208TEST_P(PeerConnectionRtpTest, RemoveTrackWithoutStreamFiresOnRemoveTrack) {
Henrik Boström933d8b02017-10-10 10:05:16 -0700209 auto caller = CreatePeerConnection();
210 auto callee = CreatePeerConnection();
211
Steve Anton3172c032018-05-03 15:30:18 -0700212 auto sender = caller->AddAudioTrack("audio_track", {});
Seth Hampson5897a6e2018-04-03 11:16:33 -0700213 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100214 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Steve Anton3172c032018-05-03 15:30:18 -0700215 ASSERT_TRUE(
216 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
217
Henrik Boström933d8b02017-10-10 10:05:16 -0700218 EXPECT_TRUE(caller->pc()->RemoveTrack(sender));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700219 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700220
Henrik Boström31638672017-11-23 17:48:32 +0100221 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Henrik Boström933d8b02017-10-10 10:05:16 -0700222 EXPECT_EQ(callee->observer()->GetAddTrackReceivers(),
223 callee->observer()->remove_track_events_);
224}
225
Steve Anton3172c032018-05-03 15:30:18 -0700226TEST_P(PeerConnectionRtpTest, RemoveTrackWithStreamFiresOnRemoveTrack) {
Henrik Boström933d8b02017-10-10 10:05:16 -0700227 auto caller = CreatePeerConnection();
228 auto callee = CreatePeerConnection();
229
Steve Anton3172c032018-05-03 15:30:18 -0700230 auto sender = caller->AddAudioTrack("audio_track", {"audio_stream"});
Seth Hampson5897a6e2018-04-03 11:16:33 -0700231 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100232 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Steve Anton3172c032018-05-03 15:30:18 -0700233 ASSERT_TRUE(
234 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
235
Henrik Boström933d8b02017-10-10 10:05:16 -0700236 EXPECT_TRUE(caller->pc()->RemoveTrack(sender));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700237 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700238
Henrik Boström31638672017-11-23 17:48:32 +0100239 ASSERT_EQ(callee->observer()->add_track_events_.size(), 1u);
Henrik Boström933d8b02017-10-10 10:05:16 -0700240 EXPECT_EQ(callee->observer()->GetAddTrackReceivers(),
241 callee->observer()->remove_track_events_);
Steve Anton3172c032018-05-03 15:30:18 -0700242 EXPECT_EQ(0u, callee->observer()->remote_streams()->count());
Henrik Boström933d8b02017-10-10 10:05:16 -0700243}
244
Steve Anton3172c032018-05-03 15:30:18 -0700245TEST_P(PeerConnectionRtpTest, RemoveTrackWithSharedStreamFiresOnRemoveTrack) {
Henrik Boström933d8b02017-10-10 10:05:16 -0700246 auto caller = CreatePeerConnection();
247 auto callee = CreatePeerConnection();
248
Seth Hampson845e8782018-03-02 11:34:10 -0800249 const char kSharedStreamId[] = "shared_audio_stream";
Steve Anton3172c032018-05-03 15:30:18 -0700250 auto sender1 = caller->AddAudioTrack("audio_track1", {kSharedStreamId});
251 auto sender2 = caller->AddAudioTrack("audio_track2", {kSharedStreamId});
Seth Hampson5897a6e2018-04-03 11:16:33 -0700252 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100253 ASSERT_EQ(callee->observer()->add_track_events_.size(), 2u);
Steve Anton3172c032018-05-03 15:30:18 -0700254 ASSERT_TRUE(
255 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700256
257 // Remove "audio_track1".
258 EXPECT_TRUE(caller->pc()->RemoveTrack(sender1));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700259 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100260 ASSERT_EQ(callee->observer()->add_track_events_.size(), 2u);
Henrik Boström933d8b02017-10-10 10:05:16 -0700261 EXPECT_EQ(
Steve Anton9158ef62017-11-27 13:01:52 -0800262 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>{
Henrik Boström933d8b02017-10-10 10:05:16 -0700263 callee->observer()->add_track_events_[0].receiver},
264 callee->observer()->remove_track_events_);
Steve Anton3172c032018-05-03 15:30:18 -0700265 ASSERT_EQ(1u, callee->observer()->remote_streams()->count());
266 ASSERT_TRUE(
267 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
Henrik Boström933d8b02017-10-10 10:05:16 -0700268
269 // Remove "audio_track2".
270 EXPECT_TRUE(caller->pc()->RemoveTrack(sender2));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700271 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100272 ASSERT_EQ(callee->observer()->add_track_events_.size(), 2u);
Henrik Boström933d8b02017-10-10 10:05:16 -0700273 EXPECT_EQ(callee->observer()->GetAddTrackReceivers(),
274 callee->observer()->remove_track_events_);
Steve Anton3172c032018-05-03 15:30:18 -0700275 EXPECT_EQ(0u, callee->observer()->remote_streams()->count());
Henrik Boström933d8b02017-10-10 10:05:16 -0700276}
277
Seth Hampson5b4f0752018-04-02 16:31:36 -0700278// Tests the edge case that if a stream ID changes for a given track that both
279// OnRemoveTrack and OnAddTrack is fired.
Steve Anton3172c032018-05-03 15:30:18 -0700280TEST_F(PeerConnectionRtpTestPlanB,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700281 RemoteStreamIdChangesFiresOnRemoveAndOnAddTrack) {
282 auto caller = CreatePeerConnection();
283 auto callee = CreatePeerConnection();
284
285 const char kStreamId1[] = "stream1";
286 const char kStreamId2[] = "stream2";
Steve Anton3172c032018-05-03 15:30:18 -0700287 caller->AddAudioTrack("audio_track1", {kStreamId1});
Seth Hampson5897a6e2018-04-03 11:16:33 -0700288 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Seth Hampson5b4f0752018-04-02 16:31:36 -0700289 EXPECT_EQ(callee->observer()->add_track_events_.size(), 1u);
290
291 // Change the stream ID of the sender in the session description.
292 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Anton3172c032018-05-03 15:30:18 -0700293 auto* audio_desc =
294 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson5b4f0752018-04-02 16:31:36 -0700295 ASSERT_EQ(audio_desc->mutable_streams().size(), 1u);
296 audio_desc->mutable_streams()[0].set_stream_ids({kStreamId2});
Steve Anton3172c032018-05-03 15:30:18 -0700297 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Seth Hampson5b4f0752018-04-02 16:31:36 -0700298
299 ASSERT_EQ(callee->observer()->add_track_events_.size(), 2u);
300 EXPECT_EQ(callee->observer()->add_track_events_[1].streams[0]->id(),
301 kStreamId2);
302 ASSERT_EQ(callee->observer()->remove_track_events_.size(), 1u);
303 EXPECT_EQ(callee->observer()->remove_track_events_[0]->streams()[0]->id(),
304 kStreamId1);
305}
306
Steve Anton8b815cd2018-02-16 16:14:42 -0800307// Tests that setting a remote description with sending transceivers will fire
308// the OnTrack callback for each transceiver and setting a remote description
Seth Hampson5b4f0752018-04-02 16:31:36 -0700309// with receive only transceivers will not call OnTrack. One transceiver is
310// created without any stream_ids, while the other is created with multiple
311// stream_ids.
Steve Anton3172c032018-05-03 15:30:18 -0700312TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTransceiverCallsOnTrack) {
Seth Hampson5b4f0752018-04-02 16:31:36 -0700313 const std::string kStreamId1 = "video_stream1";
314 const std::string kStreamId2 = "video_stream2";
Steve Anton3172c032018-05-03 15:30:18 -0700315 auto caller = CreatePeerConnection();
316 auto callee = CreatePeerConnection();
Steve Anton8b815cd2018-02-16 16:14:42 -0800317
318 auto audio_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
Seth Hampson5b4f0752018-04-02 16:31:36 -0700319 RtpTransceiverInit video_transceiver_init;
320 video_transceiver_init.stream_ids = {kStreamId1, kStreamId2};
321 auto video_transceiver =
322 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, video_transceiver_init);
Steve Anton8b815cd2018-02-16 16:14:42 -0800323
324 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
325
326 ASSERT_EQ(0u, caller->observer()->on_track_transceivers_.size());
327 ASSERT_EQ(2u, callee->observer()->on_track_transceivers_.size());
328 EXPECT_EQ(audio_transceiver->mid(),
329 callee->pc()->GetTransceivers()[0]->mid());
330 EXPECT_EQ(video_transceiver->mid(),
331 callee->pc()->GetTransceivers()[1]->mid());
Seth Hampson5b4f0752018-04-02 16:31:36 -0700332 std::vector<rtc::scoped_refptr<MediaStreamInterface>> audio_streams =
333 callee->pc()->GetTransceivers()[0]->receiver()->streams();
334 std::vector<rtc::scoped_refptr<MediaStreamInterface>> video_streams =
335 callee->pc()->GetTransceivers()[1]->receiver()->streams();
336 ASSERT_EQ(0u, audio_streams.size());
337 ASSERT_EQ(2u, video_streams.size());
338 EXPECT_EQ(kStreamId1, video_streams[0]->id());
339 EXPECT_EQ(kStreamId2, video_streams[1]->id());
Steve Anton8b815cd2018-02-16 16:14:42 -0800340}
341
342// Test that doing additional offer/answer exchanges with no changes to tracks
343// will cause no additional OnTrack calls after the tracks have been negotiated.
Steve Anton3172c032018-05-03 15:30:18 -0700344TEST_F(PeerConnectionRtpTestUnifiedPlan, ReofferDoesNotCallOnTrack) {
345 auto caller = CreatePeerConnection();
346 auto callee = CreatePeerConnection();
Steve Anton8b815cd2018-02-16 16:14:42 -0800347
348 caller->AddAudioTrack("audio");
349 callee->AddAudioTrack("audio");
350
351 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
352 EXPECT_EQ(1u, caller->observer()->on_track_transceivers_.size());
353 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
354
355 // If caller reoffers with no changes expect no additional OnTrack calls.
356 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
357 EXPECT_EQ(1u, caller->observer()->on_track_transceivers_.size());
358 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
359
360 // Also if callee reoffers with no changes expect no additional OnTrack calls.
361 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
362 EXPECT_EQ(1u, caller->observer()->on_track_transceivers_.size());
363 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
364}
365
366// Test that OnTrack is called when the transceiver direction changes to send
367// the track.
Steve Anton3172c032018-05-03 15:30:18 -0700368TEST_F(PeerConnectionRtpTestUnifiedPlan, SetDirectionCallsOnTrack) {
369 auto caller = CreatePeerConnection();
370 auto callee = CreatePeerConnection();
Steve Anton8b815cd2018-02-16 16:14:42 -0800371
372 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
373 transceiver->SetDirection(RtpTransceiverDirection::kInactive);
374 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
375 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
376 EXPECT_EQ(0u, callee->observer()->on_track_transceivers_.size());
377
378 transceiver->SetDirection(RtpTransceiverDirection::kSendOnly);
379 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
380 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
381 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
382
383 // If the direction changes but it is still receiving on the remote side, then
384 // OnTrack should not be fired again.
385 transceiver->SetDirection(RtpTransceiverDirection::kSendRecv);
386 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
387 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
388 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
389}
390
391// Test that OnTrack is called twice when a sendrecv call is started, the callee
392// changes the direction to inactive, then changes it back to sendrecv.
Steve Anton3172c032018-05-03 15:30:18 -0700393TEST_F(PeerConnectionRtpTestUnifiedPlan, SetDirectionHoldCallsOnTrackTwice) {
394 auto caller = CreatePeerConnection();
395 auto callee = CreatePeerConnection();
Steve Anton8b815cd2018-02-16 16:14:42 -0800396
397 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
398
399 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
400 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
401 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
402
403 // Put the call on hold by no longer receiving the track.
404 callee->pc()->GetTransceivers()[0]->SetDirection(
405 RtpTransceiverDirection::kInactive);
406
407 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
408 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
409 EXPECT_EQ(1u, callee->observer()->on_track_transceivers_.size());
410
411 // Resume the call by changing the direction to recvonly. This should call
412 // OnTrack again on the callee side.
413 callee->pc()->GetTransceivers()[0]->SetDirection(
414 RtpTransceiverDirection::kRecvOnly);
415
416 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
417 EXPECT_EQ(0u, caller->observer()->on_track_transceivers_.size());
418 EXPECT_EQ(2u, callee->observer()->on_track_transceivers_.size());
419}
420
Steve Anton3172c032018-05-03 15:30:18 -0700421// Test that setting a remote offer twice with no answer in the middle results
Steve Anton0f5400a2018-07-17 14:25:36 -0700422// in OnAddTrack being fired only once.
Steve Anton3172c032018-05-03 15:30:18 -0700423TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton0f5400a2018-07-17 14:25:36 -0700424 ApplyTwoRemoteOffersWithNoAnswerResultsInOneAddTrackEvent) {
Henrik Boström31638672017-11-23 17:48:32 +0100425 auto caller = CreatePeerConnection();
426 auto callee = CreatePeerConnection();
427
Steve Anton3172c032018-05-03 15:30:18 -0700428 caller->AddAudioTrack("audio_track", {});
429
Amit Hilbuchae3df542019-01-07 12:13:08 -0800430 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Steve Anton3172c032018-05-03 15:30:18 -0700431 ASSERT_EQ(1u, callee->observer()->add_track_events_.size());
432
Amit Hilbuchae3df542019-01-07 12:13:08 -0800433 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Steve Anton0f5400a2018-07-17 14:25:36 -0700434 EXPECT_EQ(1u, callee->observer()->add_track_events_.size());
Steve Anton3172c032018-05-03 15:30:18 -0700435}
436
437// Test that setting a remote offer twice with no answer in the middle and the
438// track being removed between the two offers results in OnAddTrack being called
Steve Anton0f5400a2018-07-17 14:25:36 -0700439// once the first time and OnRemoveTrack being called once the second time.
Steve Anton3172c032018-05-03 15:30:18 -0700440TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton0f5400a2018-07-17 14:25:36 -0700441 ApplyRemoteOfferAddThenRemoteOfferRemoveResultsInOneRemoveTrackEvent) {
Steve Anton3172c032018-05-03 15:30:18 -0700442 auto caller = CreatePeerConnection();
443 auto callee = CreatePeerConnection();
444
445 auto sender = caller->AddAudioTrack("audio_track", {});
446
Amit Hilbuchae3df542019-01-07 12:13:08 -0800447 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Steve Anton3172c032018-05-03 15:30:18 -0700448 ASSERT_EQ(1u, callee->observer()->add_track_events_.size());
Steve Anton0f5400a2018-07-17 14:25:36 -0700449 EXPECT_EQ(0u, callee->observer()->remove_track_events_.size());
Steve Anton3172c032018-05-03 15:30:18 -0700450
451 caller->pc()->RemoveTrack(sender);
452
Amit Hilbuchae3df542019-01-07 12:13:08 -0800453 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Steve Anton3172c032018-05-03 15:30:18 -0700454 EXPECT_EQ(1u, callee->observer()->add_track_events_.size());
Steve Anton0f5400a2018-07-17 14:25:36 -0700455 EXPECT_EQ(1u, callee->observer()->remove_track_events_.size());
456}
457
458// Test that changing the direction from receiving to not receiving between
459// setting the remote offer and creating / setting the local answer results in
460// a remove track event when SetLocalDescription is called.
461TEST_F(PeerConnectionRtpTestUnifiedPlan,
462 ChangeDirectionInAnswerResultsInRemoveTrackEvent) {
463 auto caller = CreatePeerConnection();
464 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
465 auto callee = CreatePeerConnection();
466 callee->AddAudioTrack("audio_track", {});
467
468 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
469 EXPECT_EQ(1u, callee->observer()->add_track_events_.size());
Steve Anton3172c032018-05-03 15:30:18 -0700470 EXPECT_EQ(0u, callee->observer()->remove_track_events_.size());
Steve Anton0f5400a2018-07-17 14:25:36 -0700471
472 auto callee_transceiver = callee->pc()->GetTransceivers()[0];
473 callee_transceiver->SetDirection(RtpTransceiverDirection::kSendOnly);
474
475 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
476 EXPECT_EQ(1u, callee->observer()->add_track_events_.size());
477 EXPECT_EQ(1u, callee->observer()->remove_track_events_.size());
Steve Anton3172c032018-05-03 15:30:18 -0700478}
479
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100480TEST_F(PeerConnectionRtpTestUnifiedPlan, ChangeMsidWhileReceiving) {
481 auto caller = CreatePeerConnection();
482 caller->AddAudioTrack("audio_track", {"stream1"});
483 auto callee = CreatePeerConnection();
Amit Hilbuchae3df542019-01-07 12:13:08 -0800484 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100485
486 ASSERT_EQ(1u, callee->observer()->on_track_transceivers_.size());
487 auto transceiver = callee->observer()->on_track_transceivers_[0];
488 ASSERT_EQ(1u, transceiver->receiver()->streams().size());
489 EXPECT_EQ("stream1", transceiver->receiver()->streams()[0]->id());
490
Amit Hilbuchae3df542019-01-07 12:13:08 -0800491 ASSERT_TRUE(callee->CreateAnswerAndSetAsLocal());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100492
493 // Change the stream ID in the offer.
494 // TODO(https://crbug.com/webrtc/10129): When RtpSenderInterface::SetStreams
495 // is supported, this can use that instead of munging the SDP.
496 auto offer = caller->CreateOffer();
497 auto contents = offer->description()->contents();
498 ASSERT_EQ(1u, contents.size());
499 auto& stream_params = contents[0].media_description()->mutable_streams();
500 ASSERT_EQ(1u, stream_params.size());
501 stream_params[0].set_stream_ids({"stream2"});
502 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
503 ASSERT_EQ(1u, transceiver->receiver()->streams().size());
504 EXPECT_EQ("stream2", transceiver->receiver()->streams()[0]->id());
505}
506
Steve Anton3172c032018-05-03 15:30:18 -0700507// These tests examine the state of the peer connection as a result of
508// performing SetRemoteDescription().
509
510TEST_P(PeerConnectionRtpTest, AddTrackWithoutStreamAddsReceiver) {
511 auto caller = CreatePeerConnection();
512 auto callee = CreatePeerConnection();
513
514 ASSERT_TRUE(caller->AddAudioTrack("audio_track", {}));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700515 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100516
517 EXPECT_EQ(callee->pc()->GetReceivers().size(), 1u);
518 auto receiver_added = callee->pc()->GetReceivers()[0];
519 EXPECT_EQ("audio_track", receiver_added->track()->id());
Steve Anton3172c032018-05-03 15:30:18 -0700520
521 if (sdp_semantics_ == SdpSemantics::kPlanB) {
522 // Since we are not supporting the no stream case with Plan B, there should
523 // be a generated stream, even though we didn't set one with AddTrack.
524 ASSERT_EQ(1u, receiver_added->streams().size());
525 EXPECT_TRUE(receiver_added->streams()[0]->FindAudioTrack("audio_track"));
526 } else {
527 EXPECT_EQ(0u, receiver_added->streams().size());
528 }
Henrik Boström31638672017-11-23 17:48:32 +0100529}
530
Steve Anton3172c032018-05-03 15:30:18 -0700531TEST_P(PeerConnectionRtpTest, AddTrackWithStreamAddsReceiver) {
Henrik Boström31638672017-11-23 17:48:32 +0100532 auto caller = CreatePeerConnection();
533 auto callee = CreatePeerConnection();
534
Steve Anton3172c032018-05-03 15:30:18 -0700535 ASSERT_TRUE(caller->AddAudioTrack("audio_track", {"audio_stream"}));
Seth Hampson5897a6e2018-04-03 11:16:33 -0700536 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boström31638672017-11-23 17:48:32 +0100537
538 EXPECT_EQ(callee->pc()->GetReceivers().size(), 1u);
539 auto receiver_added = callee->pc()->GetReceivers()[0];
540 EXPECT_EQ("audio_track", receiver_added->track()->id());
541 EXPECT_EQ(receiver_added->streams().size(), 1u);
Seth Hampson13b8bad2018-03-13 16:05:28 -0700542 EXPECT_EQ("audio_stream", receiver_added->streams()[0]->id());
Henrik Boström31638672017-11-23 17:48:32 +0100543 EXPECT_TRUE(receiver_added->streams()[0]->FindAudioTrack("audio_track"));
544}
545
Steve Anton3172c032018-05-03 15:30:18 -0700546TEST_P(PeerConnectionRtpTest, RemoveTrackWithoutStreamRemovesReceiver) {
Henrik Boström31638672017-11-23 17:48:32 +0100547 auto caller = CreatePeerConnection();
548 auto callee = CreatePeerConnection();
549
Steve Anton3172c032018-05-03 15:30:18 -0700550 auto sender = caller->AddAudioTrack("audio_track", {});
Henrik Boström31638672017-11-23 17:48:32 +0100551 ASSERT_TRUE(sender);
Steve Anton3172c032018-05-03 15:30:18 -0700552 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
553
Henrik Boström31638672017-11-23 17:48:32 +0100554 ASSERT_EQ(callee->pc()->GetReceivers().size(), 1u);
555 auto receiver = callee->pc()->GetReceivers()[0];
556 ASSERT_TRUE(caller->pc()->RemoveTrack(sender));
Steve Anton3172c032018-05-03 15:30:18 -0700557 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boström31638672017-11-23 17:48:32 +0100558
Steve Anton3172c032018-05-03 15:30:18 -0700559 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
560 // With Unified Plan the receiver stays but the transceiver transitions to
561 // inactive.
562 ASSERT_EQ(1u, callee->pc()->GetReceivers().size());
563 EXPECT_EQ(RtpTransceiverDirection::kInactive,
564 callee->pc()->GetTransceivers()[0]->current_direction());
565 } else {
566 // With Plan B the receiver is removed.
567 ASSERT_EQ(0u, callee->pc()->GetReceivers().size());
568 }
Henrik Boström31638672017-11-23 17:48:32 +0100569}
570
Steve Anton3172c032018-05-03 15:30:18 -0700571TEST_P(PeerConnectionRtpTest, RemoveTrackWithStreamRemovesReceiver) {
Henrik Boström31638672017-11-23 17:48:32 +0100572 auto caller = CreatePeerConnection();
573 auto callee = CreatePeerConnection();
574
Steve Anton3172c032018-05-03 15:30:18 -0700575 auto sender = caller->AddAudioTrack("audio_track", {"audio_stream"});
Henrik Boström31638672017-11-23 17:48:32 +0100576 ASSERT_TRUE(sender);
Steve Anton3172c032018-05-03 15:30:18 -0700577 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boström31638672017-11-23 17:48:32 +0100578 ASSERT_EQ(callee->pc()->GetReceivers().size(), 1u);
579 auto receiver = callee->pc()->GetReceivers()[0];
580 ASSERT_TRUE(caller->pc()->RemoveTrack(sender));
Steve Anton3172c032018-05-03 15:30:18 -0700581 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boström31638672017-11-23 17:48:32 +0100582
Steve Anton3172c032018-05-03 15:30:18 -0700583 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
584 // With Unified Plan the receiver stays but the transceiver transitions to
585 // inactive.
586 EXPECT_EQ(1u, callee->pc()->GetReceivers().size());
587 EXPECT_EQ(RtpTransceiverDirection::kInactive,
588 callee->pc()->GetTransceivers()[0]->current_direction());
589 } else {
590 // With Plan B the receiver is removed.
591 EXPECT_EQ(0u, callee->pc()->GetReceivers().size());
592 }
Henrik Boström31638672017-11-23 17:48:32 +0100593}
594
Steve Anton3172c032018-05-03 15:30:18 -0700595TEST_P(PeerConnectionRtpTest, RemoveTrackWithSharedStreamRemovesReceiver) {
Henrik Boström31638672017-11-23 17:48:32 +0100596 auto caller = CreatePeerConnection();
597 auto callee = CreatePeerConnection();
598
Seth Hampson845e8782018-03-02 11:34:10 -0800599 const char kSharedStreamId[] = "shared_audio_stream";
Steve Anton3172c032018-05-03 15:30:18 -0700600 auto sender1 = caller->AddAudioTrack("audio_track1", {kSharedStreamId});
601 auto sender2 = caller->AddAudioTrack("audio_track2", {kSharedStreamId});
602 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
603 ASSERT_EQ(2u, callee->pc()->GetReceivers().size());
Henrik Boström31638672017-11-23 17:48:32 +0100604
605 // Remove "audio_track1".
606 EXPECT_TRUE(caller->pc()->RemoveTrack(sender1));
Steve Anton3172c032018-05-03 15:30:18 -0700607 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
608
609 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
610 // With Unified Plan the receiver stays but the transceiver transitions to
611 // inactive.
612 ASSERT_EQ(2u, callee->pc()->GetReceivers().size());
613 auto transceiver = callee->pc()->GetTransceivers()[0];
614 EXPECT_EQ("audio_track1", transceiver->receiver()->track()->id());
615 EXPECT_EQ(RtpTransceiverDirection::kInactive,
616 transceiver->current_direction());
617 } else {
618 // With Plan B the receiver is removed.
619 ASSERT_EQ(1u, callee->pc()->GetReceivers().size());
620 EXPECT_EQ("audio_track2", callee->pc()->GetReceivers()[0]->track()->id());
621 }
Henrik Boström31638672017-11-23 17:48:32 +0100622
623 // Remove "audio_track2".
624 EXPECT_TRUE(caller->pc()->RemoveTrack(sender2));
Steve Anton3172c032018-05-03 15:30:18 -0700625 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
626
627 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
628 // With Unified Plan the receiver stays but the transceiver transitions to
629 // inactive.
630 ASSERT_EQ(2u, callee->pc()->GetReceivers().size());
631 auto transceiver = callee->pc()->GetTransceivers()[1];
632 EXPECT_EQ("audio_track2", transceiver->receiver()->track()->id());
633 EXPECT_EQ(RtpTransceiverDirection::kInactive,
634 transceiver->current_direction());
635 } else {
636 // With Plan B the receiver is removed.
637 ASSERT_EQ(0u, callee->pc()->GetReceivers().size());
638 }
Henrik Boström31638672017-11-23 17:48:32 +0100639}
640
Florent Castelliabe301f2018-06-12 18:33:49 +0200641TEST_P(PeerConnectionRtpTest, AudioGetParametersHasHeaderExtensions) {
642 auto caller = CreatePeerConnection();
643 auto callee = CreatePeerConnection();
644 auto sender = caller->AddAudioTrack("audio_track");
645 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
646
647 ASSERT_GT(caller->pc()->GetSenders().size(), 0u);
648 EXPECT_GT(sender->GetParameters().header_extensions.size(), 0u);
649
650 ASSERT_GT(callee->pc()->GetReceivers().size(), 0u);
651 auto receiver = callee->pc()->GetReceivers()[0];
652 EXPECT_GT(receiver->GetParameters().header_extensions.size(), 0u);
653}
654
655TEST_P(PeerConnectionRtpTest, VideoGetParametersHasHeaderExtensions) {
656 auto caller = CreatePeerConnection();
657 auto callee = CreatePeerConnection();
658 auto sender = caller->AddVideoTrack("video_track");
659 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
660
661 ASSERT_GT(caller->pc()->GetSenders().size(), 0u);
662 EXPECT_GT(sender->GetParameters().header_extensions.size(), 0u);
663
664 ASSERT_GT(callee->pc()->GetReceivers().size(), 0u);
665 auto receiver = callee->pc()->GetReceivers()[0];
666 EXPECT_GT(receiver->GetParameters().header_extensions.size(), 0u);
667}
668
Henrik Boström31638672017-11-23 17:48:32 +0100669// Invokes SetRemoteDescription() twice in a row without synchronizing the two
670// calls and examine the state of the peer connection inside the callbacks to
671// ensure that the second call does not occur prematurely, contaminating the
672// state of the peer connection of the first callback.
Steve Anton3172c032018-05-03 15:30:18 -0700673TEST_F(PeerConnectionRtpTestPlanB,
Henrik Boström31638672017-11-23 17:48:32 +0100674 StatesCorrelateWithSetRemoteDescriptionCall) {
675 auto caller = CreatePeerConnection();
676 auto callee = CreatePeerConnection();
677
Henrik Boström31638672017-11-23 17:48:32 +0100678 // Create SDP for adding a track and for removing it. This will be used in the
679 // first and second SetRemoteDescription() calls.
Steve Anton3172c032018-05-03 15:30:18 -0700680 auto sender = caller->AddAudioTrack("audio_track", {});
Henrik Boström31638672017-11-23 17:48:32 +0100681 auto srd1_sdp = caller->CreateOfferAndSetAsLocal();
682 EXPECT_TRUE(caller->pc()->RemoveTrack(sender));
683 auto srd2_sdp = caller->CreateOfferAndSetAsLocal();
684
685 // In the first SetRemoteDescription() callback, check that we have a
686 // receiver for the track.
687 auto pc = callee->pc();
688 bool srd1_callback_called = false;
689 auto srd1_callback = [&srd1_callback_called, &pc]() {
690 EXPECT_EQ(pc->GetReceivers().size(), 1u);
691 srd1_callback_called = true;
692 };
693
694 // In the second SetRemoteDescription() callback, check that the receiver has
695 // been removed.
696 // TODO(hbos): When we implement Unified Plan, receivers will not be removed.
697 // Instead, the transceiver owning the receiver will become inactive.
698 // https://crbug.com/webrtc/7600
699 bool srd2_callback_called = false;
700 auto srd2_callback = [&srd2_callback_called, &pc]() {
701 EXPECT_TRUE(pc->GetReceivers().empty());
702 srd2_callback_called = true;
703 };
704
705 // Invoke SetRemoteDescription() twice in a row without synchronizing the two
706 // calls. The callbacks verify that the two calls are synchronized, as in, the
707 // effects of the second SetRemoteDescription() call must not have happened by
708 // the time the first callback is invoked. If it has then the receiver that is
709 // added as a result of the first SetRemoteDescription() call will already
710 // have been removed as a result of the second SetRemoteDescription() call
711 // when the first callback is invoked.
712 callee->pc()->SetRemoteDescription(
713 std::move(srd1_sdp),
714 new OnSuccessObserver<decltype(srd1_callback)>(srd1_callback));
715 callee->pc()->SetRemoteDescription(
716 std::move(srd2_sdp),
717 new OnSuccessObserver<decltype(srd2_callback)>(srd2_callback));
718 EXPECT_TRUE_WAIT(srd1_callback_called, kDefaultTimeout);
719 EXPECT_TRUE_WAIT(srd2_callback_called, kDefaultTimeout);
720}
721
Seth Hampson5897a6e2018-04-03 11:16:33 -0700722// Tests that a remote track is created with the signaled MSIDs when they are
723// communicated with a=msid and no SSRCs are signaled at all (i.e., no a=ssrc
724// lines).
Steve Anton3172c032018-05-03 15:30:18 -0700725TEST_F(PeerConnectionRtpTestUnifiedPlan, UnsignaledSsrcCreatesReceiverStreams) {
726 auto caller = CreatePeerConnection();
727 auto callee = CreatePeerConnection();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700728 const char kStreamId1[] = "stream1";
729 const char kStreamId2[] = "stream2";
730 caller->AddTrack(caller->CreateAudioTrack("audio_track1"),
731 {kStreamId1, kStreamId2});
732
733 auto offer = caller->CreateOfferAndSetAsLocal();
734 // Munge the offer to take out everything but the stream_ids.
735 auto contents = offer->description()->contents();
736 ASSERT_TRUE(!contents.empty());
737 ASSERT_TRUE(!contents[0].media_description()->streams().empty());
738 std::vector<std::string> stream_ids =
739 contents[0].media_description()->streams()[0].stream_ids();
740 contents[0].media_description()->mutable_streams().clear();
741 cricket::StreamParams new_stream;
742 new_stream.set_stream_ids(stream_ids);
743 contents[0].media_description()->AddStream(new_stream);
744
745 // Set the remote description and verify that the streams were added to the
746 // receiver correctly.
747 ASSERT_TRUE(
748 callee->SetRemoteDescription(CloneSessionDescription(offer.get())));
749 auto receivers = callee->pc()->GetReceivers();
750 ASSERT_EQ(receivers.size(), 1u);
751 ASSERT_EQ(receivers[0]->streams().size(), 2u);
752 EXPECT_EQ(receivers[0]->streams()[0]->id(), kStreamId1);
753 EXPECT_EQ(receivers[0]->streams()[1]->id(), kStreamId2);
754}
755
Seth Hampson5b4f0752018-04-02 16:31:36 -0700756// Tests that with Unified Plan if the the stream id changes for a track when
757// when setting a new remote description, that the media stream is updated
758// appropriately for the receiver.
Steve Anton0f5400a2018-07-17 14:25:36 -0700759// TODO(https://github.com/w3c/webrtc-pc/issues/1937): Resolve spec issue or fix
760// test.
761TEST_F(PeerConnectionRtpTestUnifiedPlan,
762 DISABLED_RemoteStreamIdChangesUpdatesReceiver) {
Steve Anton3172c032018-05-03 15:30:18 -0700763 auto caller = CreatePeerConnection();
764 auto callee = CreatePeerConnection();
Seth Hampson5b4f0752018-04-02 16:31:36 -0700765
766 const char kStreamId1[] = "stream1";
767 const char kStreamId2[] = "stream2";
Steve Anton0f5400a2018-07-17 14:25:36 -0700768 caller->AddAudioTrack("audio_track1", {kStreamId1});
Seth Hampson5897a6e2018-04-03 11:16:33 -0700769 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Seth Hampson5b4f0752018-04-02 16:31:36 -0700770 EXPECT_EQ(callee->observer()->add_track_events_.size(), 1u);
771
772 // Change the stream id of the sender in the session description.
773 auto offer = caller->CreateOfferAndSetAsLocal();
774 auto contents = offer->description()->contents();
775 ASSERT_EQ(contents.size(), 1u);
776 ASSERT_EQ(contents[0].media_description()->mutable_streams().size(), 1u);
777 contents[0].media_description()->mutable_streams()[0].set_stream_ids(
778 {kStreamId2});
779
Steve Anton0f5400a2018-07-17 14:25:36 -0700780 // Set the remote description and verify that the stream was updated
781 // properly.
782 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Seth Hampson5b4f0752018-04-02 16:31:36 -0700783 auto receivers = callee->pc()->GetReceivers();
784 ASSERT_EQ(receivers.size(), 1u);
785 ASSERT_EQ(receivers[0]->streams().size(), 1u);
786 EXPECT_EQ(receivers[0]->streams()[0]->id(), kStreamId2);
787}
788
789// This tests a regression caught by a downstream client, that occured when
790// applying a remote description with a SessionDescription object that
791// contained StreamParams that didn't have ids. Although there were multiple
792// remote audio senders, FindSenderInfo didn't find them as unique, because
793// it looked up by StreamParam.id, which none had. This meant only one
794// AudioRtpReceiver was created, as opposed to one for each remote sender.
Steve Anton3172c032018-05-03 15:30:18 -0700795TEST_F(PeerConnectionRtpTestPlanB,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700796 MultipleRemoteSendersWithoutStreamParamIdAddsMultipleReceivers) {
797 auto caller = CreatePeerConnection();
798 auto callee = CreatePeerConnection();
799
800 const char kStreamId1[] = "stream1";
801 const char kStreamId2[] = "stream2";
802 caller->AddAudioTrack("audio_track1", {kStreamId1});
803 caller->AddAudioTrack("audio_track2", {kStreamId2});
804
805 auto offer = caller->CreateOfferAndSetAsLocal();
806 auto mutable_streams =
807 cricket::GetFirstAudioContentDescription(offer->description())
808 ->mutable_streams();
809 ASSERT_EQ(mutable_streams.size(), 2u);
810 // Clear the IDs in the StreamParams.
811 mutable_streams[0].id.clear();
812 mutable_streams[1].id.clear();
813 ASSERT_TRUE(
814 callee->SetRemoteDescription(CloneSessionDescription(offer.get())));
815
816 auto receivers = callee->pc()->GetReceivers();
817 ASSERT_EQ(receivers.size(), 2u);
818 ASSERT_EQ(receivers[0]->streams().size(), 1u);
819 EXPECT_EQ(kStreamId1, receivers[0]->streams()[0]->id());
820 ASSERT_EQ(receivers[1]->streams().size(), 1u);
821 EXPECT_EQ(kStreamId2, receivers[1]->streams()[0]->id());
822}
823
Henrik Boström31638672017-11-23 17:48:32 +0100824// Tests for the legacy SetRemoteDescription() function signature.
Henrik Boström31638672017-11-23 17:48:32 +0100825
826// Sanity test making sure the callback is invoked.
Steve Anton3172c032018-05-03 15:30:18 -0700827TEST_P(PeerConnectionRtpTest, LegacyObserverOnSuccess) {
Henrik Boström31638672017-11-23 17:48:32 +0100828 auto caller = CreatePeerConnection();
829 auto callee = CreatePeerConnection();
830
831 std::string error;
832 ASSERT_TRUE(
833 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(), &error));
834}
835
836// Verifies legacy behavior: The observer is not called if if the peer
837// connection is destroyed because the asynchronous callback is executed in the
838// peer connection's message handler.
Steve Anton3172c032018-05-03 15:30:18 -0700839TEST_P(PeerConnectionRtpTest,
840 LegacyObserverNotCalledIfPeerConnectionDereferenced) {
Henrik Boström31638672017-11-23 17:48:32 +0100841 auto caller = CreatePeerConnection();
842 auto callee = CreatePeerConnection();
843
844 rtc::scoped_refptr<webrtc::MockSetSessionDescriptionObserver> observer =
845 new rtc::RefCountedObject<webrtc::MockSetSessionDescriptionObserver>();
846
847 auto offer = caller->CreateOfferAndSetAsLocal();
848 callee->pc()->SetRemoteDescription(observer, offer.release());
849 callee = nullptr;
850 rtc::Thread::Current()->ProcessMessages(0);
851 EXPECT_FALSE(observer->called());
852}
853
Steve Antonf9381f02017-12-14 10:23:57 -0800854// RtpTransceiver Tests.
Steve Anton9158ef62017-11-27 13:01:52 -0800855
856// Test that by default there are no transceivers with Unified Plan.
Steve Anton3172c032018-05-03 15:30:18 -0700857TEST_F(PeerConnectionRtpTestUnifiedPlan, PeerConnectionHasNoTransceivers) {
858 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800859 EXPECT_THAT(caller->pc()->GetTransceivers(), ElementsAre());
860}
861
862// Test that a transceiver created with the audio kind has the correct initial
863// properties.
Steve Anton3172c032018-05-03 15:30:18 -0700864TEST_F(PeerConnectionRtpTestUnifiedPlan,
865 AddTransceiverHasCorrectInitProperties) {
866 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800867
868 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200869 EXPECT_EQ(absl::nullopt, transceiver->mid());
Steve Anton9158ef62017-11-27 13:01:52 -0800870 EXPECT_FALSE(transceiver->stopped());
871 EXPECT_EQ(RtpTransceiverDirection::kSendRecv, transceiver->direction());
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200872 EXPECT_EQ(absl::nullopt, transceiver->current_direction());
Steve Anton9158ef62017-11-27 13:01:52 -0800873}
874
875// Test that adding a transceiver with the audio kind creates an audio sender
876// and audio receiver with the receiver having a live audio track.
Steve Anton3172c032018-05-03 15:30:18 -0700877TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton9158ef62017-11-27 13:01:52 -0800878 AddAudioTransceiverCreatesAudioSenderAndReceiver) {
Steve Anton3172c032018-05-03 15:30:18 -0700879 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800880
881 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
Steve Anton69470252018-02-09 11:43:08 -0800882 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, transceiver->media_type());
Steve Anton9158ef62017-11-27 13:01:52 -0800883
884 ASSERT_TRUE(transceiver->sender());
885 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, transceiver->sender()->media_type());
886
887 ASSERT_TRUE(transceiver->receiver());
888 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, transceiver->receiver()->media_type());
889
890 auto track = transceiver->receiver()->track();
891 ASSERT_TRUE(track);
892 EXPECT_EQ(MediaStreamTrackInterface::kAudioKind, track->kind());
893 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kLive, track->state());
894}
895
896// Test that adding a transceiver with the video kind creates an video sender
897// and video receiver with the receiver having a live video track.
Steve Anton3172c032018-05-03 15:30:18 -0700898TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton9158ef62017-11-27 13:01:52 -0800899 AddAudioTransceiverCreatesVideoSenderAndReceiver) {
Steve Anton3172c032018-05-03 15:30:18 -0700900 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800901
902 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
Steve Anton69470252018-02-09 11:43:08 -0800903 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
Steve Anton9158ef62017-11-27 13:01:52 -0800904
905 ASSERT_TRUE(transceiver->sender());
906 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->sender()->media_type());
907
908 ASSERT_TRUE(transceiver->receiver());
909 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->receiver()->media_type());
910
911 auto track = transceiver->receiver()->track();
912 ASSERT_TRUE(track);
913 EXPECT_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
914 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kLive, track->state());
915}
916
917// Test that after a call to AddTransceiver, the transceiver shows in
918// GetTransceivers(), the transceiver's sender shows in GetSenders(), and the
919// transceiver's receiver shows in GetReceivers().
Steve Anton3172c032018-05-03 15:30:18 -0700920TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTransceiverShowsInLists) {
921 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800922
923 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
924 EXPECT_EQ(
925 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>{transceiver},
926 caller->pc()->GetTransceivers());
927 EXPECT_EQ(
928 std::vector<rtc::scoped_refptr<RtpSenderInterface>>{
929 transceiver->sender()},
930 caller->pc()->GetSenders());
931 EXPECT_EQ(
932 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>{
933 transceiver->receiver()},
934 caller->pc()->GetReceivers());
935}
936
937// Test that the direction passed in through the AddTransceiver init parameter
938// is set in the returned transceiver.
Steve Anton3172c032018-05-03 15:30:18 -0700939TEST_F(PeerConnectionRtpTestUnifiedPlan,
940 AddTransceiverWithDirectionIsReflected) {
941 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800942
943 RtpTransceiverInit init;
944 init.direction = RtpTransceiverDirection::kSendOnly;
945 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init);
946 EXPECT_EQ(RtpTransceiverDirection::kSendOnly, transceiver->direction());
947}
948
Steve Anton9158ef62017-11-27 13:01:52 -0800949// Test that calling AddTransceiver with a track creates a transceiver which has
950// its sender's track set to the passed-in track.
Steve Anton3172c032018-05-03 15:30:18 -0700951TEST_F(PeerConnectionRtpTestUnifiedPlan,
952 AddTransceiverWithTrackCreatesSenderWithTrack) {
953 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800954
955 auto audio_track = caller->CreateAudioTrack("audio track");
956 auto transceiver = caller->AddTransceiver(audio_track);
957
958 auto sender = transceiver->sender();
959 ASSERT_TRUE(sender->track());
960 EXPECT_EQ(audio_track, sender->track());
961
962 auto receiver = transceiver->receiver();
963 ASSERT_TRUE(receiver->track());
964 EXPECT_EQ(MediaStreamTrackInterface::kAudioKind, receiver->track()->kind());
965 EXPECT_EQ(MediaStreamTrackInterface::TrackState::kLive,
966 receiver->track()->state());
967}
968
969// Test that calling AddTransceiver twice with the same track creates distinct
970// transceivers, senders with the same track.
Steve Anton3172c032018-05-03 15:30:18 -0700971TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton9158ef62017-11-27 13:01:52 -0800972 AddTransceiverTwiceWithSameTrackCreatesMultipleTransceivers) {
Steve Anton3172c032018-05-03 15:30:18 -0700973 auto caller = CreatePeerConnection();
Steve Anton9158ef62017-11-27 13:01:52 -0800974
975 auto audio_track = caller->CreateAudioTrack("audio track");
976
977 auto transceiver1 = caller->AddTransceiver(audio_track);
978 auto transceiver2 = caller->AddTransceiver(audio_track);
979
980 EXPECT_NE(transceiver1, transceiver2);
981
982 auto sender1 = transceiver1->sender();
983 auto sender2 = transceiver2->sender();
984 EXPECT_NE(sender1, sender2);
985 EXPECT_EQ(audio_track, sender1->track());
986 EXPECT_EQ(audio_track, sender2->track());
987
988 EXPECT_THAT(caller->pc()->GetTransceivers(),
989 UnorderedElementsAre(transceiver1, transceiver2));
990 EXPECT_THAT(caller->pc()->GetSenders(),
991 UnorderedElementsAre(sender1, sender2));
992}
993
Steve Anton3fe1b152017-12-12 10:20:08 -0800994// RtpTransceiver error handling tests.
995
Steve Anton3172c032018-05-03 15:30:18 -0700996TEST_F(PeerConnectionRtpTestUnifiedPlan,
997 AddTransceiverWithInvalidKindReturnsError) {
998 auto caller = CreatePeerConnection();
Steve Anton3fe1b152017-12-12 10:20:08 -0800999
1000 auto result = caller->pc()->AddTransceiver(cricket::MEDIA_TYPE_DATA);
1001 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
1002}
1003
Steve Anton3172c032018-05-03 15:30:18 -07001004TEST_F(PeerConnectionRtpTestUnifiedPlan,
1005 CanClosePeerConnectionWithoutCrashing) {
1006 auto caller = CreatePeerConnection();
Steve Anton3fe1b152017-12-12 10:20:08 -08001007
1008 caller->pc()->Close();
1009}
1010
Steve Antonf9381f02017-12-14 10:23:57 -08001011// Unified Plan AddTrack tests.
1012
Steve Antonf9381f02017-12-14 10:23:57 -08001013// Test that adding an audio track creates a new audio RtpSender with the given
1014// track.
Steve Anton3172c032018-05-03 15:30:18 -07001015TEST_F(PeerConnectionRtpTestUnifiedPlan, AddAudioTrackCreatesAudioSender) {
1016 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001017
1018 auto audio_track = caller->CreateAudioTrack("a");
Steve Anton2d6c76a2018-01-05 17:10:52 -08001019 auto sender = caller->AddTrack(audio_track);
Steve Antonf9381f02017-12-14 10:23:57 -08001020 ASSERT_TRUE(sender);
1021
1022 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, sender->media_type());
1023 EXPECT_EQ(audio_track, sender->track());
1024}
1025
1026// Test that adding a video track creates a new video RtpSender with the given
1027// track.
Steve Anton3172c032018-05-03 15:30:18 -07001028TEST_F(PeerConnectionRtpTestUnifiedPlan, AddVideoTrackCreatesVideoSender) {
1029 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001030
1031 auto video_track = caller->CreateVideoTrack("a");
Steve Anton2d6c76a2018-01-05 17:10:52 -08001032 auto sender = caller->AddTrack(video_track);
Steve Antonf9381f02017-12-14 10:23:57 -08001033 ASSERT_TRUE(sender);
1034
1035 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type());
1036 EXPECT_EQ(video_track, sender->track());
1037}
1038
1039// Test that adding a track to a new PeerConnection creates an RtpTransceiver
1040// with the sender that AddTrack returns and in the sendrecv direction.
Steve Anton3172c032018-05-03 15:30:18 -07001041TEST_F(PeerConnectionRtpTestUnifiedPlan, AddFirstTrackCreatesTransceiver) {
1042 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001043
1044 auto sender = caller->AddAudioTrack("a");
1045 ASSERT_TRUE(sender);
1046
1047 auto transceivers = caller->pc()->GetTransceivers();
1048 ASSERT_EQ(1u, transceivers.size());
1049 EXPECT_EQ(sender, transceivers[0]->sender());
1050 EXPECT_EQ(RtpTransceiverDirection::kSendRecv, transceivers[0]->direction());
1051}
1052
1053// Test that if a transceiver of the same type but no track had been added to
1054// the PeerConnection and later a call to AddTrack is made, the resulting sender
1055// is the transceiver's sender and the sender's track is the newly-added track.
Steve Anton3172c032018-05-03 15:30:18 -07001056TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTrackReusesTransceiver) {
1057 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001058
1059 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1060 auto audio_track = caller->CreateAudioTrack("a");
Steve Anton2d6c76a2018-01-05 17:10:52 -08001061 auto sender = caller->AddTrack(audio_track);
Steve Antonf9381f02017-12-14 10:23:57 -08001062 ASSERT_TRUE(sender);
1063
1064 auto transceivers = caller->pc()->GetTransceivers();
1065 ASSERT_EQ(1u, transceivers.size());
1066 EXPECT_EQ(transceiver, transceivers[0]);
1067 EXPECT_EQ(sender, transceiver->sender());
1068 EXPECT_EQ(audio_track, sender->track());
1069}
1070
1071// Test that adding two tracks to a new PeerConnection creates two
1072// RtpTransceivers in the same order.
Steve Anton3172c032018-05-03 15:30:18 -07001073TEST_F(PeerConnectionRtpTestUnifiedPlan, TwoAddTrackCreatesTwoTransceivers) {
1074 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001075
1076 auto sender1 = caller->AddAudioTrack("a");
1077 auto sender2 = caller->AddVideoTrack("v");
1078 ASSERT_TRUE(sender2);
1079
1080 auto transceivers = caller->pc()->GetTransceivers();
1081 ASSERT_EQ(2u, transceivers.size());
1082 EXPECT_EQ(sender1, transceivers[0]->sender());
1083 EXPECT_EQ(sender2, transceivers[1]->sender());
1084}
1085
1086// Test that if there are multiple transceivers with no sending track then a
1087// later call to AddTrack will use the one of the same type as the newly-added
1088// track.
Steve Anton3172c032018-05-03 15:30:18 -07001089TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTrackReusesTransceiverOfType) {
1090 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001091
1092 auto audio_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1093 auto video_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
1094 auto sender = caller->AddVideoTrack("v");
1095
1096 ASSERT_EQ(2u, caller->pc()->GetTransceivers().size());
1097 EXPECT_NE(sender, audio_transceiver->sender());
1098 EXPECT_EQ(sender, video_transceiver->sender());
1099}
1100
1101// Test that if the only transceivers that do not have a sending track have a
1102// different type from the added track, then AddTrack will create a new
1103// transceiver for the track.
Steve Anton3172c032018-05-03 15:30:18 -07001104TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001105 AddTrackDoesNotReuseTransceiverOfWrongType) {
Steve Anton3172c032018-05-03 15:30:18 -07001106 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001107
1108 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1109 auto sender = caller->AddVideoTrack("v");
1110
1111 auto transceivers = caller->pc()->GetTransceivers();
1112 ASSERT_EQ(2u, transceivers.size());
1113 EXPECT_NE(sender, transceivers[0]->sender());
1114 EXPECT_EQ(sender, transceivers[1]->sender());
1115}
1116
1117// Test that the first available transceiver is reused by AddTrack when multiple
1118// are available.
Steve Anton3172c032018-05-03 15:30:18 -07001119TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001120 AddTrackReusesFirstMatchingTransceiver) {
Steve Anton3172c032018-05-03 15:30:18 -07001121 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001122
1123 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1124 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1125 auto sender = caller->AddAudioTrack("a");
1126
1127 auto transceivers = caller->pc()->GetTransceivers();
1128 ASSERT_EQ(2u, transceivers.size());
1129 EXPECT_EQ(sender, transceivers[0]->sender());
1130 EXPECT_NE(sender, transceivers[1]->sender());
1131}
1132
1133// Test that a call to AddTrack that reuses a transceiver will change the
1134// direction from inactive to sendonly.
Steve Anton3172c032018-05-03 15:30:18 -07001135TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001136 AddTrackChangesDirectionFromInactiveToSendOnly) {
Steve Anton3172c032018-05-03 15:30:18 -07001137 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001138
1139 RtpTransceiverInit init;
1140 init.direction = RtpTransceiverDirection::kInactive;
1141 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init);
1142
1143 caller->observer()->clear_negotiation_needed();
1144 ASSERT_TRUE(caller->AddAudioTrack("a"));
1145 EXPECT_TRUE(caller->observer()->negotiation_needed());
1146
1147 EXPECT_EQ(RtpTransceiverDirection::kSendOnly, transceiver->direction());
1148}
1149
1150// Test that a call to AddTrack that reuses a transceiver will change the
1151// direction from recvonly to sendrecv.
Steve Anton3172c032018-05-03 15:30:18 -07001152TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001153 AddTrackChangesDirectionFromRecvOnlyToSendRecv) {
Steve Anton3172c032018-05-03 15:30:18 -07001154 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001155
1156 RtpTransceiverInit init;
1157 init.direction = RtpTransceiverDirection::kRecvOnly;
1158 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init);
1159
1160 caller->observer()->clear_negotiation_needed();
1161 ASSERT_TRUE(caller->AddAudioTrack("a"));
1162 EXPECT_TRUE(caller->observer()->negotiation_needed());
1163
1164 EXPECT_EQ(RtpTransceiverDirection::kSendRecv, transceiver->direction());
1165}
1166
Steve Anton3172c032018-05-03 15:30:18 -07001167TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTrackCreatesSenderWithTrackId) {
Steve Anton02ee47c2018-01-10 16:26:06 -08001168 const std::string kTrackId = "audio_track";
1169
Steve Anton3172c032018-05-03 15:30:18 -07001170 auto caller = CreatePeerConnection();
Steve Anton02ee47c2018-01-10 16:26:06 -08001171
1172 auto audio_track = caller->CreateAudioTrack(kTrackId);
1173 auto sender = caller->AddTrack(audio_track);
1174
1175 EXPECT_EQ(kTrackId, sender->id());
1176}
1177
Steve Antonf9381f02017-12-14 10:23:57 -08001178// Unified Plan AddTrack error handling.
1179
Steve Anton3172c032018-05-03 15:30:18 -07001180TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTrackErrorIfClosed) {
1181 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001182
1183 auto audio_track = caller->CreateAudioTrack("a");
1184 caller->pc()->Close();
1185
1186 caller->observer()->clear_negotiation_needed();
Yves Gerey665174f2018-06-19 15:03:05 +02001187 auto result = caller->pc()->AddTrack(audio_track, std::vector<std::string>());
Steve Anton2d6c76a2018-01-05 17:10:52 -08001188 EXPECT_EQ(RTCErrorType::INVALID_STATE, result.error().type());
Steve Antonf9381f02017-12-14 10:23:57 -08001189 EXPECT_FALSE(caller->observer()->negotiation_needed());
1190}
1191
Steve Anton3172c032018-05-03 15:30:18 -07001192TEST_F(PeerConnectionRtpTestUnifiedPlan, AddTrackErrorIfTrackAlreadyHasSender) {
1193 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001194
1195 auto audio_track = caller->CreateAudioTrack("a");
Steve Anton2d6c76a2018-01-05 17:10:52 -08001196 ASSERT_TRUE(caller->AddTrack(audio_track));
Steve Antonf9381f02017-12-14 10:23:57 -08001197
1198 caller->observer()->clear_negotiation_needed();
Yves Gerey665174f2018-06-19 15:03:05 +02001199 auto result = caller->pc()->AddTrack(audio_track, std::vector<std::string>());
Steve Anton2d6c76a2018-01-05 17:10:52 -08001200 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, result.error().type());
Steve Antonf9381f02017-12-14 10:23:57 -08001201 EXPECT_FALSE(caller->observer()->negotiation_needed());
1202}
1203
1204// Unified Plan RemoveTrack tests.
1205
1206// Test that calling RemoveTrack on a sender with a previously-added track
1207// clears the sender's track.
Steve Anton3172c032018-05-03 15:30:18 -07001208TEST_F(PeerConnectionRtpTestUnifiedPlan, RemoveTrackClearsSenderTrack) {
1209 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001210
1211 auto sender = caller->AddAudioTrack("a");
1212 ASSERT_TRUE(caller->pc()->RemoveTrack(sender));
1213
1214 EXPECT_FALSE(sender->track());
1215}
1216
1217// Test that calling RemoveTrack on a sender where the transceiver is configured
1218// in the sendrecv direction changes the transceiver's direction to recvonly.
Steve Anton3172c032018-05-03 15:30:18 -07001219TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001220 RemoveTrackChangesDirectionFromSendRecvToRecvOnly) {
Steve Anton3172c032018-05-03 15:30:18 -07001221 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001222
1223 RtpTransceiverInit init;
1224 init.direction = RtpTransceiverDirection::kSendRecv;
1225 auto transceiver =
1226 caller->AddTransceiver(caller->CreateAudioTrack("a"), init);
1227
1228 caller->observer()->clear_negotiation_needed();
1229 ASSERT_TRUE(caller->pc()->RemoveTrack(transceiver->sender()));
1230 EXPECT_TRUE(caller->observer()->negotiation_needed());
1231
1232 EXPECT_EQ(RtpTransceiverDirection::kRecvOnly, transceiver->direction());
1233 EXPECT_TRUE(caller->observer()->renegotiation_needed_);
1234}
1235
1236// Test that calling RemoveTrack on a sender where the transceiver is configured
1237// in the sendonly direction changes the transceiver's direction to inactive.
Steve Anton3172c032018-05-03 15:30:18 -07001238TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001239 RemoveTrackChangesDirectionFromSendOnlyToInactive) {
Steve Anton3172c032018-05-03 15:30:18 -07001240 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001241
1242 RtpTransceiverInit init;
1243 init.direction = RtpTransceiverDirection::kSendOnly;
1244 auto transceiver =
1245 caller->AddTransceiver(caller->CreateAudioTrack("a"), init);
1246
1247 caller->observer()->clear_negotiation_needed();
1248 ASSERT_TRUE(caller->pc()->RemoveTrack(transceiver->sender()));
1249 EXPECT_TRUE(caller->observer()->negotiation_needed());
1250
1251 EXPECT_EQ(RtpTransceiverDirection::kInactive, transceiver->direction());
1252}
1253
1254// Test that calling RemoveTrack with a sender that has a null track results in
1255// no change in state.
Steve Anton3172c032018-05-03 15:30:18 -07001256TEST_F(PeerConnectionRtpTestUnifiedPlan, RemoveTrackWithNullSenderTrackIsNoOp) {
1257 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001258
1259 auto sender = caller->AddAudioTrack("a");
1260 auto transceiver = caller->pc()->GetTransceivers()[0];
1261 ASSERT_TRUE(sender->SetTrack(nullptr));
1262
1263 caller->observer()->clear_negotiation_needed();
1264 ASSERT_TRUE(caller->pc()->RemoveTrack(sender));
1265 EXPECT_FALSE(caller->observer()->negotiation_needed());
1266
1267 EXPECT_EQ(RtpTransceiverDirection::kSendRecv, transceiver->direction());
1268}
1269
1270// Unified Plan RemoveTrack error handling.
1271
Steve Anton3172c032018-05-03 15:30:18 -07001272TEST_F(PeerConnectionRtpTestUnifiedPlan, RemoveTrackErrorIfClosed) {
1273 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001274
1275 auto sender = caller->AddAudioTrack("a");
1276 caller->pc()->Close();
1277
1278 caller->observer()->clear_negotiation_needed();
1279 EXPECT_FALSE(caller->pc()->RemoveTrack(sender));
1280 EXPECT_FALSE(caller->observer()->negotiation_needed());
1281}
1282
Steve Anton3172c032018-05-03 15:30:18 -07001283TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Antonf9381f02017-12-14 10:23:57 -08001284 RemoveTrackNoErrorIfTrackAlreadyRemoved) {
Steve Anton3172c032018-05-03 15:30:18 -07001285 auto caller = CreatePeerConnection();
Steve Antonf9381f02017-12-14 10:23:57 -08001286
1287 auto sender = caller->AddAudioTrack("a");
1288 ASSERT_TRUE(caller->pc()->RemoveTrack(sender));
1289
1290 caller->observer()->clear_negotiation_needed();
1291 EXPECT_TRUE(caller->pc()->RemoveTrack(sender));
1292 EXPECT_FALSE(caller->observer()->negotiation_needed());
1293}
1294
Steve Anton60b6c1d2018-06-13 11:32:27 -07001295// Test that setting offers that add/remove/add a track repeatedly without
1296// setting the appropriate answer in between works.
1297// These are regression tests for bugs.webrtc.org/9401
1298TEST_F(PeerConnectionRtpTestUnifiedPlan, AddRemoveAddTrackOffersWorksAudio) {
1299 auto caller = CreatePeerConnection();
1300
1301 auto sender1 = caller->AddAudioTrack("audio1");
1302 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1303
1304 caller->pc()->RemoveTrack(sender1);
1305 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1306
1307 // This will re-use the transceiver created by the first AddTrack.
1308 auto sender2 = caller->AddAudioTrack("audio2");
1309 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1310
1311 EXPECT_EQ(1u, caller->pc()->GetTransceivers().size());
1312 EXPECT_EQ(sender1, sender2);
1313}
1314TEST_F(PeerConnectionRtpTestUnifiedPlan, AddRemoveAddTrackOffersWorksVideo) {
1315 auto caller = CreatePeerConnection();
1316
1317 auto sender1 = caller->AddVideoTrack("video1");
1318 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1319
1320 caller->pc()->RemoveTrack(sender1);
1321 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1322
1323 // This will re-use the transceiver created by the first AddTrack.
1324 auto sender2 = caller->AddVideoTrack("video2");
1325 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1326
1327 EXPECT_EQ(1u, caller->pc()->GetTransceivers().size());
1328 EXPECT_EQ(sender1, sender2);
1329}
1330
Steve Anton07563732018-06-26 11:13:50 -07001331// Test that CreateOffer succeeds if two tracks with the same label are added.
1332TEST_F(PeerConnectionRtpTestUnifiedPlan, CreateOfferSameTrackLabel) {
1333 auto caller = CreatePeerConnection();
1334
1335 auto audio_sender = caller->AddAudioTrack("track", {});
1336 auto video_sender = caller->AddVideoTrack("track", {});
1337
1338 EXPECT_TRUE(caller->CreateOffer());
1339
1340 EXPECT_EQ(audio_sender->track()->id(), video_sender->track()->id());
1341 EXPECT_NE(audio_sender->id(), video_sender->id());
1342}
1343
1344// Test that CreateAnswer succeeds if two tracks with the same label are added.
1345TEST_F(PeerConnectionRtpTestUnifiedPlan, CreateAnswerSameTrackLabel) {
1346 auto caller = CreatePeerConnection();
1347 auto callee = CreatePeerConnection();
1348
1349 RtpTransceiverInit recvonly;
1350 recvonly.direction = RtpTransceiverDirection::kRecvOnly;
1351 caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, recvonly);
1352 caller->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, recvonly);
1353
1354 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
1355
1356 auto audio_sender = callee->AddAudioTrack("track", {});
1357 auto video_sender = callee->AddVideoTrack("track", {});
1358
1359 EXPECT_TRUE(callee->CreateAnswer());
1360
1361 EXPECT_EQ(audio_sender->track()->id(), video_sender->track()->id());
1362 EXPECT_NE(audio_sender->id(), video_sender->id());
1363}
1364
1365// Test that calling AddTrack, RemoveTrack and AddTrack again creates a second
1366// m= section with a random sender id (different from the first, now rejected,
1367// m= section).
1368TEST_F(PeerConnectionRtpTestUnifiedPlan,
1369 AddRemoveAddTrackGeneratesNewSenderId) {
1370 auto caller = CreatePeerConnection();
1371 auto callee = CreatePeerConnection();
1372
1373 auto track = caller->CreateVideoTrack("video");
1374 auto sender1 = caller->AddTrack(track);
1375 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1376
1377 caller->pc()->RemoveTrack(sender1);
1378 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1379
1380 auto sender2 = caller->AddTrack(track);
1381
1382 EXPECT_NE(sender1, sender2);
1383 EXPECT_NE(sender1->id(), sender2->id());
1384 std::string sender2_id = sender2->id();
1385
1386 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1387
1388 // The sender's ID should not change after negotiation.
1389 EXPECT_EQ(sender2_id, sender2->id());
1390}
1391
Steve Anton52d86772018-02-20 15:48:12 -08001392// Test that OnRenegotiationNeeded is fired if SetDirection is called on an
1393// active RtpTransceiver with a new direction.
Steve Anton3172c032018-05-03 15:30:18 -07001394TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton52d86772018-02-20 15:48:12 -08001395 RenegotiationNeededAfterTransceiverSetDirection) {
Steve Anton3172c032018-05-03 15:30:18 -07001396 auto caller = CreatePeerConnection();
Steve Anton52d86772018-02-20 15:48:12 -08001397
1398 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1399
1400 caller->observer()->clear_negotiation_needed();
1401 transceiver->SetDirection(RtpTransceiverDirection::kInactive);
1402 EXPECT_TRUE(caller->observer()->negotiation_needed());
1403}
1404
1405// Test that OnRenegotiationNeeded is not fired if SetDirection is called on an
1406// active RtpTransceiver with current direction.
Steve Anton3172c032018-05-03 15:30:18 -07001407TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton52d86772018-02-20 15:48:12 -08001408 NoRenegotiationNeededAfterTransceiverSetSameDirection) {
Steve Anton3172c032018-05-03 15:30:18 -07001409 auto caller = CreatePeerConnection();
Steve Anton52d86772018-02-20 15:48:12 -08001410
1411 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1412
1413 caller->observer()->clear_negotiation_needed();
1414 transceiver->SetDirection(transceiver->direction());
1415 EXPECT_FALSE(caller->observer()->negotiation_needed());
1416}
1417
1418// Test that OnRenegotiationNeeded is not fired if SetDirection is called on a
1419// stopped RtpTransceiver.
Steve Anton3172c032018-05-03 15:30:18 -07001420TEST_F(PeerConnectionRtpTestUnifiedPlan,
Steve Anton52d86772018-02-20 15:48:12 -08001421 NoRenegotiationNeededAfterSetDirectionOnStoppedTransceiver) {
Steve Anton3172c032018-05-03 15:30:18 -07001422 auto caller = CreatePeerConnection();
Steve Anton52d86772018-02-20 15:48:12 -08001423
1424 auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
1425 transceiver->Stop();
1426
1427 caller->observer()->clear_negotiation_needed();
1428 transceiver->SetDirection(RtpTransceiverDirection::kInactive);
1429 EXPECT_FALSE(caller->observer()->negotiation_needed());
1430}
1431
Florent Castelli892acf02018-10-01 22:47:20 +02001432// Test that AddTransceiver fails if trying to use unimplemented RTP encoding
1433// parameters with the send_encodings parameters.
1434TEST_F(PeerConnectionRtpTestUnifiedPlan,
1435 CheckForUnsupportedEncodingParameters) {
1436 auto caller = CreatePeerConnection();
1437
1438 RtpTransceiverInit init;
1439 init.send_encodings.emplace_back();
1440
1441 auto default_send_encodings = init.send_encodings;
1442
Henrik Grunelle1301a82018-12-13 12:13:22 +00001443 // Unimplemented RtpParameters: ssrc, codec_payload_type, fec, rtx, dtx,
Amit Hilbuchaa584152019-02-06 17:09:52 -08001444 // ptime, scale_framerate_down_by, dependency_rids.
Florent Castelli892acf02018-10-01 22:47:20 +02001445 init.send_encodings[0].ssrc = 1;
1446 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1447 caller->pc()
1448 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1449 .error()
1450 .type());
1451 init.send_encodings = default_send_encodings;
1452
Henrik Grunelle1301a82018-12-13 12:13:22 +00001453 init.send_encodings[0].codec_payload_type = 1;
1454 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1455 caller->pc()
1456 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1457 .error()
1458 .type());
1459 init.send_encodings = default_send_encodings;
1460
Florent Castelli892acf02018-10-01 22:47:20 +02001461 init.send_encodings[0].fec = RtpFecParameters();
1462 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1463 caller->pc()
1464 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1465 .error()
1466 .type());
1467 init.send_encodings = default_send_encodings;
1468
1469 init.send_encodings[0].rtx = RtpRtxParameters();
1470 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1471 caller->pc()
1472 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1473 .error()
1474 .type());
1475 init.send_encodings = default_send_encodings;
1476
1477 init.send_encodings[0].dtx = DtxStatus::ENABLED;
1478 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1479 caller->pc()
1480 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1481 .error()
1482 .type());
1483 init.send_encodings = default_send_encodings;
1484
1485 init.send_encodings[0].ptime = 1;
1486 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1487 caller->pc()
1488 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1489 .error()
1490 .type());
1491 init.send_encodings = default_send_encodings;
1492
Florent Castelli892acf02018-10-01 22:47:20 +02001493 init.send_encodings[0].dependency_rids.push_back("dummy_rid");
1494 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER,
1495 caller->pc()
1496 ->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init)
1497 .error()
1498 .type());
1499}
1500
Florent Castellic1a0bcb2019-01-29 14:26:48 +01001501// Test that AddTransceiver fails if trying to use invalid RTP encoding
1502// parameters with the send_encodings parameters.
1503TEST_F(PeerConnectionRtpTestUnifiedPlan, CheckForInvalidEncodingParameters) {
1504 auto caller = CreatePeerConnection();
1505
1506 RtpTransceiverInit init;
1507 init.send_encodings.emplace_back();
1508
1509 auto default_send_encodings = init.send_encodings;
1510
1511 init.send_encodings[0].scale_resolution_down_by = 0.5;
1512 EXPECT_EQ(RTCErrorType::INVALID_RANGE,
1513 caller->pc()
1514 ->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init)
1515 .error()
1516 .type());
1517 init.send_encodings = default_send_encodings;
1518
1519 init.send_encodings[0].bitrate_priority = 0;
1520 EXPECT_EQ(RTCErrorType::INVALID_RANGE,
1521 caller->pc()
1522 ->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init)
1523 .error()
1524 .type());
1525 init.send_encodings = default_send_encodings;
1526
1527 init.send_encodings[0].min_bitrate_bps = 200000;
1528 init.send_encodings[0].max_bitrate_bps = 100000;
1529 EXPECT_EQ(RTCErrorType::INVALID_RANGE,
1530 caller->pc()
1531 ->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init)
1532 .error()
1533 .type());
1534 init.send_encodings = default_send_encodings;
1535
1536 init.send_encodings[0].num_temporal_layers = 0;
1537 EXPECT_EQ(RTCErrorType::INVALID_RANGE,
1538 caller->pc()
1539 ->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init)
1540 .error()
1541 .type());
1542 init.send_encodings = default_send_encodings;
1543
1544 init.send_encodings[0].num_temporal_layers = 5;
1545 EXPECT_EQ(RTCErrorType::INVALID_RANGE,
1546 caller->pc()
1547 ->AddTransceiver(cricket::MEDIA_TYPE_VIDEO, init)
1548 .error()
1549 .type());
1550 init.send_encodings = default_send_encodings;
1551}
1552
Florent Castelli892acf02018-10-01 22:47:20 +02001553// Test that AddTransceiver transfers the send_encodings to the sender and they
1554// are retained after SetLocalDescription().
1555TEST_F(PeerConnectionRtpTestUnifiedPlan, SendEncodingsPassedToSender) {
1556 auto caller = CreatePeerConnection();
1557
1558 RtpTransceiverInit init;
1559 init.send_encodings.emplace_back();
1560 init.send_encodings[0].active = false;
1561 init.send_encodings[0].max_bitrate_bps = 180000;
1562
1563 auto result = caller->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO, init);
1564 ASSERT_TRUE(result.ok());
1565
1566 auto init_send_encodings = result.value()->sender()->init_send_encodings();
1567 EXPECT_FALSE(init_send_encodings[0].active);
1568 EXPECT_EQ(init_send_encodings[0].max_bitrate_bps, 180000);
1569
1570 auto parameters = result.value()->sender()->GetParameters();
1571 EXPECT_FALSE(parameters.encodings[0].active);
1572 EXPECT_EQ(parameters.encodings[0].max_bitrate_bps, 180000);
1573
1574 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
1575
1576 parameters = result.value()->sender()->GetParameters();
1577 EXPECT_FALSE(parameters.encodings[0].active);
1578 EXPECT_EQ(parameters.encodings[0].max_bitrate_bps, 180000);
1579}
1580
Steve Antone831b8c2018-02-01 12:22:16 -08001581// Test MSID signaling between Unified Plan and Plan B endpoints. There are two
1582// options for this kind of signaling: media section based (a=msid) and ssrc
1583// based (a=ssrc MSID). While JSEP only specifies media section MSID signaling,
1584// we want to ensure compatibility with older Plan B endpoints that might expect
1585// ssrc based MSID signaling. Thus we test here that Unified Plan offers both
1586// types but answers with the same type as the offer.
1587
Steve Anton3172c032018-05-03 15:30:18 -07001588class PeerConnectionMsidSignalingTest
1589 : public PeerConnectionRtpTestUnifiedPlan {};
Steve Antone831b8c2018-02-01 12:22:16 -08001590
1591TEST_F(PeerConnectionMsidSignalingTest, UnifiedPlanTalkingToOurself) {
1592 auto caller = CreatePeerConnectionWithUnifiedPlan();
1593 caller->AddAudioTrack("caller_audio");
1594 auto callee = CreatePeerConnectionWithUnifiedPlan();
1595 callee->AddAudioTrack("callee_audio");
1596
1597 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1598
1599 // Offer should have had both a=msid and a=ssrc MSID lines.
1600 auto* offer = callee->pc()->remote_description();
1601 EXPECT_EQ((cricket::kMsidSignalingMediaSection |
1602 cricket::kMsidSignalingSsrcAttribute),
1603 offer->description()->msid_signaling());
1604
1605 // Answer should have had only a=msid lines.
1606 auto* answer = caller->pc()->remote_description();
1607 EXPECT_EQ(cricket::kMsidSignalingMediaSection,
1608 answer->description()->msid_signaling());
Harald Alvestrand5dbb5862018-02-13 23:48:00 +01001609 // Check that this is counted correctly
Steve Antonc1e6e862019-03-04 14:43:44 -08001610 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpSemanticNegotiated"),
1611 ElementsAre(Pair(kSdpSemanticNegotiatedUnifiedPlan, 2)));
Steve Antone831b8c2018-02-01 12:22:16 -08001612}
1613
1614TEST_F(PeerConnectionMsidSignalingTest, PlanBOfferToUnifiedPlanAnswer) {
1615 auto caller = CreatePeerConnectionWithPlanB();
1616 caller->AddAudioTrack("caller_audio");
1617 auto callee = CreatePeerConnectionWithUnifiedPlan();
1618 callee->AddAudioTrack("callee_audio");
1619
1620 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1621
1622 // Offer should have only a=ssrc MSID lines.
1623 auto* offer = callee->pc()->remote_description();
1624 EXPECT_EQ(cricket::kMsidSignalingSsrcAttribute,
1625 offer->description()->msid_signaling());
1626
1627 // Answer should have only a=ssrc MSID lines to match the offer.
1628 auto* answer = caller->pc()->remote_description();
1629 EXPECT_EQ(cricket::kMsidSignalingSsrcAttribute,
1630 answer->description()->msid_signaling());
1631}
1632
Seth Hampson5b4f0752018-04-02 16:31:36 -07001633// This tests that a Plan B endpoint appropriately sets the remote description
1634// from a Unified Plan offer. When the Unified Plan offer contains a=msid lines
1635// that signal no stream ids or multiple stream ids we expect that the Plan B
1636// endpoint always has exactly one media stream per track.
1637TEST_F(PeerConnectionMsidSignalingTest, UnifiedPlanToPlanBAnswer) {
1638 const std::string kStreamId1 = "audio_stream_1";
1639 const std::string kStreamId2 = "audio_stream_2";
1640
1641 auto caller = CreatePeerConnectionWithUnifiedPlan();
1642 caller->AddAudioTrack("caller_audio", {kStreamId1, kStreamId2});
1643 caller->AddVideoTrack("caller_video", {});
1644 auto callee = CreatePeerConnectionWithPlanB();
1645 callee->AddAudioTrack("callee_audio");
1646 caller->AddVideoTrack("callee_video");
1647
1648 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1649
1650 // Offer should have had both a=msid and a=ssrc MSID lines.
1651 auto* offer = callee->pc()->remote_description();
1652 EXPECT_EQ((cricket::kMsidSignalingMediaSection |
1653 cricket::kMsidSignalingSsrcAttribute),
1654 offer->description()->msid_signaling());
1655
1656 // Callee should always have 1 stream for all of it's receivers.
1657 const auto& track_events = callee->observer()->add_track_events_;
1658 ASSERT_EQ(2u, track_events.size());
1659 ASSERT_EQ(1u, track_events[0].streams.size());
1660 EXPECT_EQ(kStreamId1, track_events[0].streams[0]->id());
1661 ASSERT_EQ(1u, track_events[1].streams.size());
1662 // This autogenerated a stream id for the empty one signalled.
1663 EXPECT_FALSE(track_events[1].streams[0]->id().empty());
1664}
1665
Steve Antone831b8c2018-02-01 12:22:16 -08001666TEST_F(PeerConnectionMsidSignalingTest, PureUnifiedPlanToUs) {
1667 auto caller = CreatePeerConnectionWithUnifiedPlan();
1668 caller->AddAudioTrack("caller_audio");
1669 auto callee = CreatePeerConnectionWithUnifiedPlan();
1670 callee->AddAudioTrack("callee_audio");
1671
1672 auto offer = caller->CreateOffer();
1673 // Simulate a pure Unified Plan offerer by setting the MSID signaling to media
1674 // section only.
1675 offer->description()->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1676
1677 ASSERT_TRUE(
1678 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1679 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1680
1681 // Answer should have only a=msid to match the offer.
1682 auto answer = callee->CreateAnswer();
1683 EXPECT_EQ(cricket::kMsidSignalingMediaSection,
1684 answer->description()->msid_signaling());
1685}
1686
Steve Anton8e20f172018-03-06 10:55:04 -08001687// Test that the correct UMA metrics are reported for simple/complex SDP.
1688
Steve Anton3172c032018-05-03 15:30:18 -07001689class SdpFormatReceivedTest : public PeerConnectionRtpTestUnifiedPlan {};
Steve Anton8e20f172018-03-06 10:55:04 -08001690
1691#ifdef HAVE_SCTP
1692TEST_F(SdpFormatReceivedTest, DataChannelOnlyIsReportedAsNoTracks) {
1693 auto caller = CreatePeerConnectionWithUnifiedPlan();
1694 caller->CreateDataChannel("dc");
1695 auto callee = CreatePeerConnectionWithUnifiedPlan();
Steve Anton8e20f172018-03-06 10:55:04 -08001696
1697 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001698 // Note that only the callee does ReportSdpFormatReceived.
Steve Antonc1e6e862019-03-04 14:43:44 -08001699 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
1700 ElementsAre(Pair(kSdpFormatReceivedNoTracks, 1)));
Steve Anton8e20f172018-03-06 10:55:04 -08001701}
1702#endif // HAVE_SCTP
1703
1704TEST_F(SdpFormatReceivedTest, SimpleUnifiedPlanIsReportedAsSimple) {
1705 auto caller = CreatePeerConnectionWithUnifiedPlan();
1706 caller->AddAudioTrack("audio");
1707 caller->AddVideoTrack("video");
1708 auto callee = CreatePeerConnectionWithPlanB();
Steve Anton8e20f172018-03-06 10:55:04 -08001709
1710 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001711 // Note that only the callee does ReportSdpFormatReceived.
Steve Antonc1e6e862019-03-04 14:43:44 -08001712 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
1713 ElementsAre(Pair(kSdpFormatReceivedSimple, 1)));
Steve Anton8e20f172018-03-06 10:55:04 -08001714}
1715
1716TEST_F(SdpFormatReceivedTest, SimplePlanBIsReportedAsSimple) {
1717 auto caller = CreatePeerConnectionWithPlanB();
1718 caller->AddVideoTrack("video"); // Video only.
1719 auto callee = CreatePeerConnectionWithUnifiedPlan();
Steve Anton8e20f172018-03-06 10:55:04 -08001720
1721 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
Steve Antonc1e6e862019-03-04 14:43:44 -08001722 // Note that only the callee does ReportSdpFormatReceived.
1723 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
1724 ElementsAre(Pair(kSdpFormatReceivedSimple, 1)));
Steve Anton8e20f172018-03-06 10:55:04 -08001725}
1726
1727TEST_F(SdpFormatReceivedTest, ComplexUnifiedIsReportedAsComplexUnifiedPlan) {
1728 auto caller = CreatePeerConnectionWithUnifiedPlan();
1729 caller->AddAudioTrack("audio1");
1730 caller->AddAudioTrack("audio2");
1731 caller->AddVideoTrack("video");
1732 auto callee = CreatePeerConnectionWithPlanB();
Steve Anton8e20f172018-03-06 10:55:04 -08001733
1734 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001735 // Note that only the callee does ReportSdpFormatReceived.
Steve Antonc1e6e862019-03-04 14:43:44 -08001736 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
1737 ElementsAre(Pair(kSdpFormatReceivedComplexUnifiedPlan, 1)));
Steve Anton8e20f172018-03-06 10:55:04 -08001738}
1739
1740TEST_F(SdpFormatReceivedTest, ComplexPlanBIsReportedAsComplexPlanB) {
1741 auto caller = CreatePeerConnectionWithPlanB();
1742 caller->AddVideoTrack("video1");
1743 caller->AddVideoTrack("video2");
1744 auto callee = CreatePeerConnectionWithUnifiedPlan();
Steve Anton8e20f172018-03-06 10:55:04 -08001745
Steve Antonba42e992018-04-09 14:10:01 -07001746 // This fails since Unified Plan cannot set a session description with
1747 // multiple "Plan B tracks" in the same media section. But we still expect the
1748 // SDP Format to be recorded.
1749 ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer()));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001750 // Note that only the callee does ReportSdpFormatReceived.
Steve Antonc1e6e862019-03-04 14:43:44 -08001751 EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
1752 ElementsAre(Pair(kSdpFormatReceivedComplexPlanB, 1)));
Steve Anton8e20f172018-03-06 10:55:04 -08001753}
1754
Henrik Boström91d039b2018-01-11 17:43:30 +01001755// Sender setups in a call.
1756
Steve Anton3172c032018-05-03 15:30:18 -07001757TEST_P(PeerConnectionRtpTest, CreateTwoSendersWithSameTrack) {
Henrik Boström91d039b2018-01-11 17:43:30 +01001758 auto caller = CreatePeerConnection();
1759 auto callee = CreatePeerConnection();
1760
1761 auto track = caller->CreateAudioTrack("audio_track");
1762 auto sender1 = caller->AddTrack(track);
1763 ASSERT_TRUE(sender1);
1764 // We need to temporarily reset the track for the subsequent AddTrack() to
1765 // succeed.
1766 EXPECT_TRUE(sender1->SetTrack(nullptr));
1767 auto sender2 = caller->AddTrack(track);
1768 EXPECT_TRUE(sender2);
1769 EXPECT_TRUE(sender1->SetTrack(track));
1770
Steve Anton3172c032018-05-03 15:30:18 -07001771 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1772 // TODO(hbos): When https://crbug.com/webrtc/8734 is resolved, this should
1773 // return true, and doing |callee->SetRemoteDescription()| should work.
1774 EXPECT_FALSE(caller->CreateOfferAndSetAsLocal());
1775 } else {
1776 EXPECT_TRUE(caller->CreateOfferAndSetAsLocal());
1777 }
Henrik Boström91d039b2018-01-11 17:43:30 +01001778}
1779
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001780INSTANTIATE_TEST_SUITE_P(PeerConnectionRtpTest,
1781 PeerConnectionRtpTest,
1782 Values(SdpSemantics::kPlanB,
1783 SdpSemantics::kUnifiedPlan));
Steve Anton3172c032018-05-03 15:30:18 -07001784
Steve Anton9158ef62017-11-27 13:01:52 -08001785} // namespace webrtc