blob: 795a66d7f61607b4135d7a90ec0f0a51834240a8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kStreamLabel1[] = "local_stream_1";
46static const char kStreamLabel2[] = "local_stream_2";
47static const char kStreamLabel3[] = "local_stream_3";
48static const int kDefaultStunPort = 3478;
49static const char kStunAddressOnly[] = "stun:address";
50static const char kStunInvalidPort[] = "stun:address:-1";
51static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
52static const char kStunAddressPortAndMore2[] = "stun:address:port more";
53static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
54static const char kTurnUsername[] = "user";
55static const char kTurnPassword[] = "password";
56static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020057static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
deadbeefab9b2d12015-10-14 11:33:11 -070059static const char kStreams[][8] = {"stream1", "stream2"};
60static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
61static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
62
deadbeef5e97fb52015-10-15 12:49:08 -070063static const char kRecvonly[] = "recvonly";
64static const char kSendrecv[] = "sendrecv";
65
deadbeefab9b2d12015-10-14 11:33:11 -070066// Reference SDP with a MediaStream with label "stream1" and audio track with
67// id "audio_1" and a video track with id "video_1;
68static const char kSdpStringWithStream1[] =
69 "v=0\r\n"
70 "o=- 0 0 IN IP4 127.0.0.1\r\n"
71 "s=-\r\n"
72 "t=0 0\r\n"
73 "a=ice-ufrag:e5785931\r\n"
74 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
75 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
76 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
77 "m=audio 1 RTP/AVPF 103\r\n"
78 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070079 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=rtpmap:103 ISAC/16000\r\n"
81 "a=ssrc:1 cname:stream1\r\n"
82 "a=ssrc:1 mslabel:stream1\r\n"
83 "a=ssrc:1 label:audiotrack0\r\n"
84 "m=video 1 RTP/AVPF 120\r\n"
85 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:120 VP8/90000\r\n"
88 "a=ssrc:2 cname:stream1\r\n"
89 "a=ssrc:2 mslabel:stream1\r\n"
90 "a=ssrc:2 label:videotrack0\r\n";
91
92// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
93// MediaStreams have one audio track and one video track.
94// This uses MSID.
95static const char kSdpStringWithStream1And2[] =
96 "v=0\r\n"
97 "o=- 0 0 IN IP4 127.0.0.1\r\n"
98 "s=-\r\n"
99 "t=0 0\r\n"
100 "a=ice-ufrag:e5785931\r\n"
101 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
102 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
103 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
104 "a=msid-semantic: WMS stream1 stream2\r\n"
105 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700107 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
111 "a=ssrc:3 cname:stream2\r\n"
112 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
113 "m=video 1 RTP/AVPF 120\r\n"
114 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700115 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700116 "a=rtpmap:120 VP8/0\r\n"
117 "a=ssrc:2 cname:stream1\r\n"
118 "a=ssrc:2 msid:stream1 videotrack0\r\n"
119 "a=ssrc:4 cname:stream2\r\n"
120 "a=ssrc:4 msid:stream2 videotrack1\r\n";
121
122// Reference SDP without MediaStreams. Msid is not supported.
123static const char kSdpStringWithoutStreams[] =
124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "m=video 1 RTP/AVPF 120\r\n"
137 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:120 VP8/90000\r\n";
140
141// Reference SDP without MediaStreams. Msid is supported.
142static const char kSdpStringWithMsidWithoutStreams[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
151 "a=msid-semantic: WMS\r\n"
152 "m=audio 1 RTP/AVPF 103\r\n"
153 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:103 ISAC/16000\r\n"
156 "m=video 1 RTP/AVPF 120\r\n"
157 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700158 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n";
175
176// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
177static const char kSdpStringSendOnlyWithoutStreams[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
182 "a=ice-ufrag:e5785931\r\n"
183 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
184 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
185 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
186 "m=audio 1 RTP/AVPF 103\r\n"
187 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=sendonly\r\n"
190 "a=rtpmap:103 ISAC/16000\r\n"
191 "m=video 1 RTP/AVPF 120\r\n"
192 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700193 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=sendonly\r\n"
195 "a=rtpmap:120 VP8/90000\r\n";
196
197static const char kSdpStringInit[] =
198 "v=0\r\n"
199 "o=- 0 0 IN IP4 127.0.0.1\r\n"
200 "s=-\r\n"
201 "t=0 0\r\n"
202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
206 "a=msid-semantic: WMS\r\n";
207
208static const char kSdpStringAudio[] =
209 "m=audio 1 RTP/AVPF 103\r\n"
210 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n";
213
214static const char kSdpStringVideo[] =
215 "m=video 1 RTP/AVPF 120\r\n"
216 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700217 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=rtpmap:120 VP8/90000\r\n";
219
220static const char kSdpStringMs1Audio0[] =
221 "a=ssrc:1 cname:stream1\r\n"
222 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
223
224static const char kSdpStringMs1Video0[] =
225 "a=ssrc:2 cname:stream1\r\n"
226 "a=ssrc:2 msid:stream1 videotrack0\r\n";
227
228static const char kSdpStringMs1Audio1[] =
229 "a=ssrc:3 cname:stream1\r\n"
230 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
231
232static const char kSdpStringMs1Video1[] =
233 "a=ssrc:4 cname:stream1\r\n"
234 "a=ssrc:4 msid:stream1 videotrack1\r\n";
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236#define MAYBE_SKIP_TEST(feature) \
237 if (!(feature())) { \
238 LOG(LS_INFO) << "Feature disabled... skipping"; \
239 return; \
240 }
241
perkjd61bf802016-03-24 03:16:19 -0700242using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700243using cricket::StreamParams;
244using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700246using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247using webrtc::AudioTrackInterface;
248using webrtc::DataBuffer;
249using webrtc::DataChannelInterface;
250using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700252using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700253using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700254using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255using webrtc::MediaStreamInterface;
256using webrtc::MediaStreamTrackInterface;
257using webrtc::MockCreateSessionDescriptionObserver;
258using webrtc::MockDataChannelObserver;
259using webrtc::MockSetSessionDescriptionObserver;
260using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700261using webrtc::NotifierInterface;
262using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263using webrtc::PeerConnectionInterface;
264using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700265using webrtc::RtpReceiverInterface;
266using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::SdpParseError;
268using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700269using webrtc::StreamCollection;
270using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100271using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700272using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273using webrtc::VideoTrackInterface;
274
deadbeefab9b2d12015-10-14 11:33:11 -0700275typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277namespace {
278
279// Gets the first ssrc of given content type from the ContentInfo.
280bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
281 if (!content_info || !ssrc) {
282 return false;
283 }
284 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000285 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 content_info->description);
287 if (!media_desc || media_desc->streams().empty()) {
288 return false;
289 }
290 *ssrc = media_desc->streams().begin()->first_ssrc();
291 return true;
292}
293
294void SetSsrcToZero(std::string* sdp) {
295 const char kSdpSsrcAtribute[] = "a=ssrc:";
296 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
297 size_t ssrc_pos = 0;
298 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
299 std::string::npos) {
300 size_t end_ssrc = sdp->find(" ", ssrc_pos);
301 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
302 ssrc_pos = end_ssrc;
303 }
304}
305
deadbeefab9b2d12015-10-14 11:33:11 -0700306// Check if |streams| contains the specified track.
307bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
308 const std::string& stream_label,
309 const std::string& track_id) {
310 for (const cricket::StreamParams& params : streams) {
311 if (params.sync_label == stream_label && params.id == track_id) {
312 return true;
313 }
314 }
315 return false;
316}
317
318// Check if |senders| contains the specified sender, by id.
319bool ContainsSender(
320 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
321 const std::string& id) {
322 for (const auto& sender : senders) {
323 if (sender->id() == id) {
324 return true;
325 }
326 }
327 return false;
328}
329
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700330// Check if |senders| contains the specified sender, by id and stream id.
331bool ContainsSender(
332 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
333 const std::string& id,
334 const std::string& stream_id) {
335 for (const auto& sender : senders) {
336 if (sender->id() == id && sender->stream_id() == stream_id) {
337 return true;
338 }
339 }
340 return false;
341}
342
deadbeefab9b2d12015-10-14 11:33:11 -0700343// Create a collection of streams.
344// CreateStreamCollection(1) creates a collection that
345// correspond to kSdpStringWithStream1.
346// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
347rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700348 int number_of_streams,
349 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700350 rtc::scoped_refptr<StreamCollection> local_collection(
351 StreamCollection::Create());
352
353 for (int i = 0; i < number_of_streams; ++i) {
354 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
355 webrtc::MediaStream::Create(kStreams[i]));
356
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700357 for (int j = 0; j < tracks_per_stream; ++j) {
358 // Add a local audio track.
359 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
360 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
361 nullptr));
362 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364 // Add a local video track.
365 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
366 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
367 webrtc::FakeVideoTrackSource::Create()));
368 stream->AddTrack(video_track);
369 }
deadbeefab9b2d12015-10-14 11:33:11 -0700370
371 local_collection->AddStream(stream);
372 }
373 return local_collection;
374}
375
376// Check equality of StreamCollections.
377bool CompareStreamCollections(StreamCollectionInterface* s1,
378 StreamCollectionInterface* s2) {
379 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
380 return false;
381 }
382
383 for (size_t i = 0; i != s1->count(); ++i) {
384 if (s1->at(i)->label() != s2->at(i)->label()) {
385 return false;
386 }
387 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
388 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
389 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
390 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
391
392 if (audio_tracks1.size() != audio_tracks2.size()) {
393 return false;
394 }
395 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
396 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
397 return false;
398 }
399 }
400 if (video_tracks1.size() != video_tracks2.size()) {
401 return false;
402 }
403 for (size_t j = 0; j != video_tracks1.size(); ++j) {
404 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
405 return false;
406 }
407 }
408 }
409 return true;
410}
411
perkjd61bf802016-03-24 03:16:19 -0700412// Helper class to test Observer.
413class MockTrackObserver : public ObserverInterface {
414 public:
415 explicit MockTrackObserver(NotifierInterface* notifier)
416 : notifier_(notifier) {
417 notifier_->RegisterObserver(this);
418 }
419
420 ~MockTrackObserver() { Unregister(); }
421
422 void Unregister() {
423 if (notifier_) {
424 notifier_->UnregisterObserver(this);
425 notifier_ = nullptr;
426 }
427 }
428
429 MOCK_METHOD0(OnChanged, void());
430
431 private:
432 NotifierInterface* notifier_;
433};
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435class MockPeerConnectionObserver : public PeerConnectionObserver {
436 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700437 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200438 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
440 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
441 pc_ = pc;
442 if (pc) {
443 state_ = pc_->signaling_state();
444 }
445 }
nisseef8b61e2016-04-29 06:09:15 -0700446 void OnSignalingChange(
447 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 EXPECT_EQ(pc_->signaling_state(), new_state);
449 state_ = new_state;
450 }
451 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
452 virtual void OnStateChange(StateType state_changed) {
453 if (pc_.get() == NULL)
454 return;
455 switch (state_changed) {
456 case kSignalingState:
457 // OnSignalingChange and OnStateChange(kSignalingState) should always
458 // be called approximately simultaneously. To ease testing, we require
459 // that they always be called in that order. This check verifies
460 // that OnSignalingChange has just been called.
461 EXPECT_EQ(pc_->signaling_state(), state_);
462 break;
463 case kIceState:
464 ADD_FAILURE();
465 break;
466 default:
467 ADD_FAILURE();
468 break;
469 }
470 }
deadbeefab9b2d12015-10-14 11:33:11 -0700471
472 MediaStreamInterface* RemoteStream(const std::string& label) {
473 return remote_streams_->find(label);
474 }
475 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800476 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700478 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 }
perkjdfb769d2016-02-09 03:09:43 -0800480 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700482 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
perkjdfb769d2016-02-09 03:09:43 -0800484 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
485 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 last_datachannel_ = data_channel;
487 }
488
perkjdfb769d2016-02-09 03:09:43 -0800489 void OnIceConnectionChange(
490 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 EXPECT_EQ(pc_->ice_connection_state(), new_state);
492 }
perkjdfb769d2016-02-09 03:09:43 -0800493 void OnIceGatheringChange(
494 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800496 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
perkjdfb769d2016-02-09 03:09:43 -0800498 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
500 pc_->ice_gathering_state());
501
502 std::string sdp;
503 EXPECT_TRUE(candidate->ToString(&sdp));
504 EXPECT_LT(0u, sdp.size());
505 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
506 candidate->sdp_mline_index(), sdp, NULL));
507 EXPECT_TRUE(last_candidate_.get() != NULL);
508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
510 // Returns the label of the last added stream.
511 // Empty string if no stream have been added.
512 std::string GetLastAddedStreamLabel() {
513 if (last_added_stream_.get())
514 return last_added_stream_->label();
515 return "";
516 }
517 std::string GetLastRemovedStreamLabel() {
518 if (last_removed_stream_.get())
519 return last_removed_stream_->label();
520 return "";
521 }
522
523 scoped_refptr<PeerConnectionInterface> pc_;
524 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700525 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700527 rtc::scoped_refptr<StreamCollection> remote_streams_;
528 bool renegotiation_needed_ = false;
529 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
531 private:
532 scoped_refptr<MediaStreamInterface> last_added_stream_;
533 scoped_refptr<MediaStreamInterface> last_removed_stream_;
534};
535
536} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538class PeerConnectionInterfaceTest : public testing::Test {
539 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800540 PeerConnectionInterfaceTest() {
541#ifdef WEBRTC_ANDROID
542 webrtc::InitializeAndroidObjects();
543#endif
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 virtual void SetUp() {
547 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 NULL);
550 ASSERT_TRUE(pc_factory_.get() != NULL);
551 }
552
553 void CreatePeerConnection() {
554 CreatePeerConnection("", "", NULL);
555 }
556
557 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
558 CreatePeerConnection("", "", constraints);
559 }
560
561 void CreatePeerConnection(const std::string& uri,
562 const std::string& password,
563 webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800564 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700566 if (!uri.empty()) {
567 server.uri = uri;
568 server.password = password;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800569 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700570 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571
kwibergd1fe2812016-04-27 06:47:29 -0700572 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800573 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
574 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000575
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000576 // DTLS does not work in a loopback call, so is disabled for most of the
577 // tests in this file. We only create a FakeIdentityService if the test
578 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000579 FakeConstraints default_constraints;
580 if (!constraints) {
581 constraints = &default_constraints;
582
583 default_constraints.AddMandatory(
584 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
585 }
586
kwibergd1fe2812016-04-27 06:47:29 -0700587 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000588 bool dtls;
589 if (FindConstraint(constraints,
590 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
591 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200592 nullptr) && dtls) {
593 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000594 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800595 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800596 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800597 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 ASSERT_TRUE(pc_.get() != NULL);
599 observer_.SetPeerConnectionInterface(pc_.get());
600 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
601 }
602
deadbeef0a6c4ca2015-10-06 11:38:28 -0700603 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800604 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700605 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700606 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800607 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700608
deadbeef0a6c4ca2015-10-06 11:38:28 -0700609 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800610 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
611 &observer_);
612 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700613 }
614
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 void CreatePeerConnectionWithDifferentConfigurations() {
616 CreatePeerConnection(kStunAddressOnly, "", NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800617 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
618 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
619 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800621 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622
deadbeef0a6c4ca2015-10-06 11:38:28 -0700623 CreatePeerConnectionExpectFail(kStunInvalidPort);
624 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
625 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
627 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800628 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
629 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800631 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800633 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800635 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 }
637
638 void ReleasePeerConnection() {
639 pc_ = NULL;
640 observer_.SetPeerConnectionInterface(NULL);
641 }
642
deadbeefab9b2d12015-10-14 11:33:11 -0700643 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 // Create a local stream.
645 scoped_refptr<MediaStreamInterface> stream(
646 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100647 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
649 scoped_refptr<VideoTrackInterface> video_track(
650 pc_factory_->CreateVideoTrack(label + "v0", video_source));
651 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000652 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
654 observer_.renegotiation_needed_ = false;
655 }
656
657 void AddVoiceStream(const std::string& label) {
658 // Create a local stream.
659 scoped_refptr<MediaStreamInterface> stream(
660 pc_factory_->CreateLocalMediaStream(label));
661 scoped_refptr<AudioTrackInterface> audio_track(
662 pc_factory_->CreateAudioTrack(label + "a0", NULL));
663 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000664 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
666 observer_.renegotiation_needed_ = false;
667 }
668
669 void AddAudioVideoStream(const std::string& stream_label,
670 const std::string& audio_track_label,
671 const std::string& video_track_label) {
672 // Create a local stream.
673 scoped_refptr<MediaStreamInterface> stream(
674 pc_factory_->CreateLocalMediaStream(stream_label));
675 scoped_refptr<AudioTrackInterface> audio_track(
676 pc_factory_->CreateAudioTrack(
677 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
678 stream->AddTrack(audio_track.get());
679 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700680 pc_factory_->CreateVideoTrack(
681 video_track_label,
682 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000684 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
686 observer_.renegotiation_needed_ = false;
687 }
688
kwibergd1fe2812016-04-27 06:47:29 -0700689 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700690 bool offer,
691 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
693 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 MockCreateSessionDescriptionObserver>());
695 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700696 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700698 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 }
700 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700701 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 return observer->result();
703 }
704
kwibergd1fe2812016-04-27 06:47:29 -0700705 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700706 MediaConstraintsInterface* constraints) {
707 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
709
kwibergd1fe2812016-04-27 06:47:29 -0700710 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700711 MediaConstraintsInterface* constraints) {
712 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
715 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000716 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
717 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 MockSetSessionDescriptionObserver>());
719 if (local) {
720 pc_->SetLocalDescription(observer, desc);
721 } else {
722 pc_->SetRemoteDescription(observer, desc);
723 }
724 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
725 return observer->result();
726 }
727
728 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
729 return DoSetSessionDescription(desc, true);
730 }
731
732 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
733 return DoSetSessionDescription(desc, false);
734 }
735
736 // Calls PeerConnection::GetStats and check the return value.
737 // It does not verify the values in the StatReports since a RTCP packet might
738 // be required.
739 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000740 rtc::scoped_refptr<MockStatsObserver> observer(
741 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000742 if (!pc_->GetStats(
743 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 return false;
745 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
746 return observer->called();
747 }
748
749 void InitiateCall() {
750 CreatePeerConnection();
751 // Create a local stream with audio&video tracks.
752 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
753 CreateOfferReceiveAnswer();
754 }
755
756 // Verify that RTP Header extensions has been negotiated for audio and video.
757 void VerifyRemoteRtpHeaderExtensions() {
758 const cricket::MediaContentDescription* desc =
759 cricket::GetFirstAudioContentDescription(
760 pc_->remote_description()->description());
761 ASSERT_TRUE(desc != NULL);
762 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
763
764 desc = cricket::GetFirstVideoContentDescription(
765 pc_->remote_description()->description());
766 ASSERT_TRUE(desc != NULL);
767 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
768 }
769
770 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700771 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700772 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 std::string sdp;
774 EXPECT_TRUE(offer->ToString(&sdp));
775 SessionDescriptionInterface* remote_offer =
776 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
777 sdp, NULL);
778 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
779 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
780 }
781
deadbeefab9b2d12015-10-14 11:33:11 -0700782 void CreateAndSetRemoteOffer(const std::string& sdp) {
783 SessionDescriptionInterface* remote_offer =
784 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
785 sdp, nullptr);
786 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
787 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
788 }
789
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700791 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700792 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
794 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
795 // audio codec change, even if the parameter has nothing to do with
796 // receiving. Not all parameters are serialized to SDP.
797 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
798 // the SessionDescription, it is necessary to do that here to in order to
799 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
800 // https://code.google.com/p/webrtc/issues/detail?id=1356
801 std::string sdp;
802 EXPECT_TRUE(answer->ToString(&sdp));
803 SessionDescriptionInterface* new_answer =
804 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
805 sdp, NULL);
806 EXPECT_TRUE(DoSetLocalDescription(new_answer));
807 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
808 }
809
810 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700811 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700812 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813
814 std::string sdp;
815 EXPECT_TRUE(answer->ToString(&sdp));
816 SessionDescriptionInterface* pr_answer =
817 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
818 sdp, NULL);
819 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
820 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
821 }
822
823 void CreateOfferReceiveAnswer() {
824 CreateOfferAsLocalDescription();
825 std::string sdp;
826 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
827 CreateAnswerAsRemoteDescription(sdp);
828 }
829
830 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700831 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700832 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
834 // audio codec change, even if the parameter has nothing to do with
835 // receiving. Not all parameters are serialized to SDP.
836 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
837 // the SessionDescription, it is necessary to do that here to in order to
838 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
839 // https://code.google.com/p/webrtc/issues/detail?id=1356
840 std::string sdp;
841 EXPECT_TRUE(offer->ToString(&sdp));
842 SessionDescriptionInterface* new_offer =
843 webrtc::CreateSessionDescription(
844 SessionDescriptionInterface::kOffer,
845 sdp, NULL);
846
847 EXPECT_TRUE(DoSetLocalDescription(new_offer));
848 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000849 // Wait for the ice_complete message, so that SDP will have candidates.
850 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
852
deadbeefab9b2d12015-10-14 11:33:11 -0700853 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
855 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700856 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 EXPECT_TRUE(DoSetRemoteDescription(answer));
858 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
859 }
860
deadbeefab9b2d12015-10-14 11:33:11 -0700861 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 webrtc::JsepSessionDescription* pr_answer =
863 new webrtc::JsepSessionDescription(
864 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700865 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
867 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
868 webrtc::JsepSessionDescription* answer =
869 new webrtc::JsepSessionDescription(
870 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700871 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 EXPECT_TRUE(DoSetRemoteDescription(answer));
873 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
874 }
875
876 // Help function used for waiting until a the last signaled remote stream has
877 // the same label as |stream_label|. In a few of the tests in this file we
878 // answer with the same session description as we offer and thus we can
879 // check if OnAddStream have been called with the same stream as we offer to
880 // send.
881 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
882 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
883 }
884
885 // Creates an offer and applies it as a local session description.
886 // Creates an answer with the same SDP an the offer but removes all lines
887 // that start with a:ssrc"
888 void CreateOfferReceiveAnswerWithoutSsrc() {
889 CreateOfferAsLocalDescription();
890 std::string sdp;
891 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
892 SetSsrcToZero(&sdp);
893 CreateAnswerAsRemoteDescription(sdp);
894 }
895
deadbeefab9b2d12015-10-14 11:33:11 -0700896 // This function creates a MediaStream with label kStreams[0] and
897 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
898 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700899 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700900 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700901 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700902 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
903 size_t number_of_video_tracks) {
904 EXPECT_LE(number_of_audio_tracks, 2u);
905 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700906
907 reference_collection_ = StreamCollection::Create();
908 std::string sdp_ms1 = std::string(kSdpStringInit);
909
910 std::string mediastream_label = kStreams[0];
911
912 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
913 webrtc::MediaStream::Create(mediastream_label));
914 reference_collection_->AddStream(stream);
915
916 if (number_of_audio_tracks > 0) {
917 sdp_ms1 += std::string(kSdpStringAudio);
918 sdp_ms1 += std::string(kSdpStringMs1Audio0);
919 AddAudioTrack(kAudioTracks[0], stream);
920 }
921 if (number_of_audio_tracks > 1) {
922 sdp_ms1 += kSdpStringMs1Audio1;
923 AddAudioTrack(kAudioTracks[1], stream);
924 }
925
926 if (number_of_video_tracks > 0) {
927 sdp_ms1 += std::string(kSdpStringVideo);
928 sdp_ms1 += std::string(kSdpStringMs1Video0);
929 AddVideoTrack(kVideoTracks[0], stream);
930 }
931 if (number_of_video_tracks > 1) {
932 sdp_ms1 += kSdpStringMs1Video1;
933 AddVideoTrack(kVideoTracks[1], stream);
934 }
935
kwibergd1fe2812016-04-27 06:47:29 -0700936 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700937 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
938 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700939 }
940
941 void AddAudioTrack(const std::string& track_id,
942 MediaStreamInterface* stream) {
943 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
944 webrtc::AudioTrack::Create(track_id, nullptr));
945 ASSERT_TRUE(stream->AddTrack(audio_track));
946 }
947
948 void AddVideoTrack(const std::string& track_id,
949 MediaStreamInterface* stream) {
950 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700951 webrtc::VideoTrack::Create(track_id,
952 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700953 ASSERT_TRUE(stream->AddTrack(video_track));
954 }
955
kwiberg6ab3db22016-05-11 05:07:26 -0700956 rtc::scoped_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700957 CreatePeerConnection();
958 AddVoiceStream(kStreamLabel1);
kwiberg6ab3db22016-05-11 05:07:26 -0700959 rtc::scoped_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700960 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
961 return offer;
962 }
963
kwiberg6ab3db22016-05-11 05:07:26 -0700964 rtc::scoped_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700965 CreateAnswerWithOneAudioStream() {
kwiberg6ab3db22016-05-11 05:07:26 -0700966 rtc::scoped_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700967 CreateOfferWithOneAudioStream();
968 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwiberg6ab3db22016-05-11 05:07:26 -0700969 rtc::scoped_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700970 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
971 return answer;
972 }
973
974 const std::string& GetFirstAudioStreamCname(
975 const SessionDescriptionInterface* desc) {
976 const cricket::ContentInfo* audio_content =
977 cricket::GetFirstAudioContent(desc->description());
978 const cricket::AudioContentDescription* audio_desc =
979 static_cast<const cricket::AudioContentDescription*>(
980 audio_content->description);
981 return audio_desc->streams()[0].cname;
982 }
983
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800984 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
986 scoped_refptr<PeerConnectionInterface> pc_;
987 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700988 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989};
990
zhihuang8f65cdf2016-05-06 18:40:30 -0700991// Generate different CNAMEs when PeerConnections are created.
992// The CNAMEs are expected to be generated randomly. It is possible
993// that the test fails, though the possibility is very low.
994TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwiberg6ab3db22016-05-11 05:07:26 -0700995 rtc::scoped_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700996 CreateOfferWithOneAudioStream();
kwiberg6ab3db22016-05-11 05:07:26 -0700997 rtc::scoped_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700998 CreateOfferWithOneAudioStream();
999 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1000 GetFirstAudioStreamCname(offer2.get()));
1001}
1002
1003TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwiberg6ab3db22016-05-11 05:07:26 -07001004 rtc::scoped_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001005 CreateAnswerWithOneAudioStream();
kwiberg6ab3db22016-05-11 05:07:26 -07001006 rtc::scoped_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateAnswerWithOneAudioStream();
1008 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1009 GetFirstAudioStreamCname(answer2.get()));
1010}
1011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012TEST_F(PeerConnectionInterfaceTest,
1013 CreatePeerConnectionWithDifferentConfigurations) {
1014 CreatePeerConnectionWithDifferentConfigurations();
1015}
1016
1017TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1018 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001019 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 AddVoiceStream(kStreamLabel2);
1021 ASSERT_EQ(2u, pc_->local_streams()->count());
1022
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001023 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 scoped_refptr<MediaStreamInterface> stream(
1025 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1026 scoped_refptr<AudioTrackInterface> audio_track(
1027 pc_factory_->CreateAudioTrack(
1028 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1029 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001030 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001031 EXPECT_EQ(3u, pc_->local_streams()->count());
1032
1033 // Remove the third stream.
1034 pc_->RemoveStream(pc_->local_streams()->at(2));
1035 EXPECT_EQ(2u, pc_->local_streams()->count());
1036
1037 // Remove the second stream.
1038 pc_->RemoveStream(pc_->local_streams()->at(1));
1039 EXPECT_EQ(1u, pc_->local_streams()->count());
1040
1041 // Remove the first stream.
1042 pc_->RemoveStream(pc_->local_streams()->at(0));
1043 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044}
1045
deadbeefab9b2d12015-10-14 11:33:11 -07001046// Test that the created offer includes streams we added.
1047TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1048 CreatePeerConnection();
1049 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001050 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001051 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001052
1053 const cricket::ContentInfo* audio_content =
1054 cricket::GetFirstAudioContent(offer->description());
1055 const cricket::AudioContentDescription* audio_desc =
1056 static_cast<const cricket::AudioContentDescription*>(
1057 audio_content->description);
1058 EXPECT_TRUE(
1059 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1060
1061 const cricket::ContentInfo* video_content =
1062 cricket::GetFirstVideoContent(offer->description());
1063 const cricket::VideoContentDescription* video_desc =
1064 static_cast<const cricket::VideoContentDescription*>(
1065 video_content->description);
1066 EXPECT_TRUE(
1067 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1068
1069 // Add another stream and ensure the offer includes both the old and new
1070 // streams.
1071 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001072 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001073
1074 audio_content = cricket::GetFirstAudioContent(offer->description());
1075 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1076 audio_content->description);
1077 EXPECT_TRUE(
1078 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1079 EXPECT_TRUE(
1080 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1081
1082 video_content = cricket::GetFirstVideoContent(offer->description());
1083 video_desc = static_cast<const cricket::VideoContentDescription*>(
1084 video_content->description);
1085 EXPECT_TRUE(
1086 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1087 EXPECT_TRUE(
1088 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1089}
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1092 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001093 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 ASSERT_EQ(1u, pc_->local_streams()->count());
1095 pc_->RemoveStream(pc_->local_streams()->at(0));
1096 EXPECT_EQ(0u, pc_->local_streams()->count());
1097}
1098
deadbeefe1f9d832016-01-14 15:35:42 -08001099// Test for AddTrack and RemoveTrack methods.
1100// Tests that the created offer includes tracks we added,
1101// and that the RtpSenders are created correctly.
1102// Also tests that RemoveTrack removes the tracks from subsequent offers.
1103TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1104 CreatePeerConnection();
1105 // Create a dummy stream, so tracks share a stream label.
1106 scoped_refptr<MediaStreamInterface> stream(
1107 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1108 std::vector<MediaStreamInterface*> stream_list;
1109 stream_list.push_back(stream.get());
1110 scoped_refptr<AudioTrackInterface> audio_track(
1111 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001112 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1113 "video_track",
1114 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001115 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1116 auto video_sender = pc_->AddTrack(video_track, stream_list);
1117 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1118 EXPECT_EQ("audio_track", audio_sender->id());
1119 EXPECT_EQ(audio_track, audio_sender->track());
1120 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1121 EXPECT_EQ("video_track", video_sender->id());
1122 EXPECT_EQ(video_track, video_sender->track());
1123
1124 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001125 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001126 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001127
1128 const cricket::ContentInfo* audio_content =
1129 cricket::GetFirstAudioContent(offer->description());
1130 const cricket::AudioContentDescription* audio_desc =
1131 static_cast<const cricket::AudioContentDescription*>(
1132 audio_content->description);
1133 EXPECT_TRUE(
1134 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1135
1136 const cricket::ContentInfo* video_content =
1137 cricket::GetFirstVideoContent(offer->description());
1138 const cricket::VideoContentDescription* video_desc =
1139 static_cast<const cricket::VideoContentDescription*>(
1140 video_content->description);
1141 EXPECT_TRUE(
1142 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1143
1144 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1145
1146 // Now try removing the tracks.
1147 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1148 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1149
1150 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001151 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001152
1153 audio_content = cricket::GetFirstAudioContent(offer->description());
1154 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1155 audio_content->description);
1156 EXPECT_FALSE(
1157 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1158
1159 video_content = cricket::GetFirstVideoContent(offer->description());
1160 video_desc = static_cast<const cricket::VideoContentDescription*>(
1161 video_content->description);
1162 EXPECT_FALSE(
1163 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1164
1165 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1166
1167 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1168 // should return false.
1169 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1170 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1171}
1172
1173// Test creating senders without a stream specified,
1174// expecting a random stream ID to be generated.
1175TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1176 CreatePeerConnection();
1177 // Create a dummy stream, so tracks share a stream label.
1178 scoped_refptr<AudioTrackInterface> audio_track(
1179 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001180 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1181 "video_track",
1182 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001183 auto audio_sender =
1184 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1185 auto video_sender =
1186 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1187 EXPECT_EQ("audio_track", audio_sender->id());
1188 EXPECT_EQ(audio_track, audio_sender->track());
1189 EXPECT_EQ("video_track", video_sender->id());
1190 EXPECT_EQ(video_track, video_sender->track());
1191 // If the ID is truly a random GUID, it should be infinitely unlikely they
1192 // will be the same.
1193 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1194}
1195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1197 InitiateCall();
1198 WaitAndVerifyOnAddStream(kStreamLabel1);
1199 VerifyRemoteRtpHeaderExtensions();
1200}
1201
1202TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1203 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001204 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 CreateOfferAsLocalDescription();
1206 std::string offer;
1207 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1208 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1209 WaitAndVerifyOnAddStream(kStreamLabel1);
1210}
1211
1212TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1213 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001214 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215
1216 CreateOfferAsRemoteDescription();
1217 CreateAnswerAsLocalDescription();
1218
1219 WaitAndVerifyOnAddStream(kStreamLabel1);
1220}
1221
1222TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1223 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001224 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225
1226 CreateOfferAsRemoteDescription();
1227 CreatePrAnswerAsLocalDescription();
1228 CreateAnswerAsLocalDescription();
1229
1230 WaitAndVerifyOnAddStream(kStreamLabel1);
1231}
1232
1233TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1234 InitiateCall();
1235 ASSERT_EQ(1u, pc_->remote_streams()->count());
1236 pc_->RemoveStream(pc_->local_streams()->at(0));
1237 CreateOfferReceiveAnswer();
1238 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001239 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 CreateOfferReceiveAnswer();
1241}
1242
1243// Tests that after negotiating an audio only call, the respondent can perform a
1244// renegotiation that removes the audio stream.
1245TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1246 CreatePeerConnection();
1247 AddVoiceStream(kStreamLabel1);
1248 CreateOfferAsRemoteDescription();
1249 CreateAnswerAsLocalDescription();
1250
1251 ASSERT_EQ(1u, pc_->remote_streams()->count());
1252 pc_->RemoveStream(pc_->local_streams()->at(0));
1253 CreateOfferReceiveAnswer();
1254 EXPECT_EQ(0u, pc_->remote_streams()->count());
1255}
1256
1257// Test that candidates are generated and that we can parse our own candidates.
1258TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1259 CreatePeerConnection();
1260
1261 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1262 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001263 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001264 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001265 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001266 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267
1268 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001269 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001270 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001271 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272
1273 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1274 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1275
1276 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1277}
1278
deadbeefab9b2d12015-10-14 11:33:11 -07001279// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280// not unique.
1281TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1282 CreatePeerConnection();
1283 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001284 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001285 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001286 EXPECT_TRUE(offer);
1287 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288
1289 // Create a local stream with audio&video tracks having same label.
1290 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1291
1292 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001293 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294
1295 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001296 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001297 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298}
1299
1300// Test that we will get different SSRCs for each tracks in the offer and answer
1301// we created.
1302TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1303 CreatePeerConnection();
1304 // Create a local stream with audio&video tracks having different labels.
1305 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1306
1307 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001308 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001309 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 int audio_ssrc = 0;
1311 int video_ssrc = 0;
1312 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1313 &audio_ssrc));
1314 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1315 &video_ssrc));
1316 EXPECT_NE(audio_ssrc, video_ssrc);
1317
1318 // Test CreateAnswer
1319 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001320 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001321 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 audio_ssrc = 0;
1323 video_ssrc = 0;
1324 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1325 &audio_ssrc));
1326 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1327 &video_ssrc));
1328 EXPECT_NE(audio_ssrc, video_ssrc);
1329}
1330
deadbeefeb459812015-12-15 19:24:43 -08001331// Test that it's possible to call AddTrack on a MediaStream after adding
1332// the stream to a PeerConnection.
1333// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1334TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1335 CreatePeerConnection();
1336 // Create audio stream and add to PeerConnection.
1337 AddVoiceStream(kStreamLabel1);
1338 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1339
1340 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001341 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1342 "video_label",
1343 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001344 stream->AddTrack(video_track.get());
1345
kwibergd1fe2812016-04-27 06:47:29 -07001346 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001347 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001348
1349 const cricket::MediaContentDescription* video_desc =
1350 cricket::GetFirstVideoContentDescription(offer->description());
1351 EXPECT_TRUE(video_desc != nullptr);
1352}
1353
1354// Test that it's possible to call RemoveTrack on a MediaStream after adding
1355// the stream to a PeerConnection.
1356// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1357TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1358 CreatePeerConnection();
1359 // Create audio/video stream and add to PeerConnection.
1360 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1361 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1362
1363 // Remove the video track.
1364 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1365
kwibergd1fe2812016-04-27 06:47:29 -07001366 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001367 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001368
1369 const cricket::MediaContentDescription* video_desc =
1370 cricket::GetFirstVideoContentDescription(offer->description());
1371 EXPECT_TRUE(video_desc == nullptr);
1372}
1373
deadbeefbd7d8f72015-12-18 16:58:44 -08001374// Test creating a sender with a stream ID, and ensure the ID is populated
1375// in the offer.
1376TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1377 CreatePeerConnection();
1378 pc_->CreateSender("video", kStreamLabel1);
1379
kwibergd1fe2812016-04-27 06:47:29 -07001380 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001381 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001382
1383 const cricket::MediaContentDescription* video_desc =
1384 cricket::GetFirstVideoContentDescription(offer->description());
1385 ASSERT_TRUE(video_desc != nullptr);
1386 ASSERT_EQ(1u, video_desc->streams().size());
1387 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1388}
1389
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390// Test that we can specify a certain track that we want statistics about.
1391TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1392 InitiateCall();
1393 ASSERT_LT(0u, pc_->remote_streams()->count());
1394 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1395 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1396 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1397 EXPECT_TRUE(DoGetStats(remote_audio));
1398
1399 // Remove the stream. Since we are sending to our selves the local
1400 // and the remote stream is the same.
1401 pc_->RemoveStream(pc_->local_streams()->at(0));
1402 // Do a re-negotiation.
1403 CreateOfferReceiveAnswer();
1404
1405 ASSERT_EQ(0u, pc_->remote_streams()->count());
1406
1407 // Test that we still can get statistics for the old track. Even if it is not
1408 // sent any longer.
1409 EXPECT_TRUE(DoGetStats(remote_audio));
1410}
1411
1412// Test that we can get stats on a video track.
1413TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1414 InitiateCall();
1415 ASSERT_LT(0u, pc_->remote_streams()->count());
1416 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1417 scoped_refptr<MediaStreamTrackInterface> remote_video =
1418 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1419 EXPECT_TRUE(DoGetStats(remote_video));
1420}
1421
1422// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001423// TODO(tommi): Fix this test. DoGetStats will return true
1424// for the unknown track (since GetStats is async), but no
1425// data is returned for the track.
1426TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 InitiateCall();
1428 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1429 pc_factory_->CreateAudioTrack("unknown track", NULL));
1430 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1431}
1432
1433// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 FakeConstraints constraints;
1436 constraints.SetAllowRtpDataChannels();
1437 CreatePeerConnection(&constraints);
1438 scoped_refptr<DataChannelInterface> data1 =
1439 pc_->CreateDataChannel("test1", NULL);
1440 scoped_refptr<DataChannelInterface> data2 =
1441 pc_->CreateDataChannel("test2", NULL);
1442 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001443 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001445 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 new MockDataChannelObserver(data2));
1447
1448 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1449 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1450 std::string data_to_send1 = "testing testing";
1451 std::string data_to_send2 = "testing something else";
1452 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1453
1454 CreateOfferReceiveAnswer();
1455 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1456 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1457
1458 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1459 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1460 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1461 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1462
1463 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1464 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1465
1466 data1->Close();
1467 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1468 CreateOfferReceiveAnswer();
1469 EXPECT_FALSE(observer1->IsOpen());
1470 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1471 EXPECT_TRUE(observer2->IsOpen());
1472
1473 data_to_send2 = "testing something else again";
1474 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1475
1476 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1477}
1478
1479// This test verifies that sendnig binary data over RTP data channels should
1480// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 FakeConstraints constraints;
1483 constraints.SetAllowRtpDataChannels();
1484 CreatePeerConnection(&constraints);
1485 scoped_refptr<DataChannelInterface> data1 =
1486 pc_->CreateDataChannel("test1", NULL);
1487 scoped_refptr<DataChannelInterface> data2 =
1488 pc_->CreateDataChannel("test2", NULL);
1489 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001490 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001492 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 new MockDataChannelObserver(data2));
1494
1495 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1496 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1497
1498 CreateOfferReceiveAnswer();
1499 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1500 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1501
1502 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1503 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1504
jbaucheec21bd2016-03-20 06:15:43 -07001505 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1507}
1508
1509// This test setup a RTP data channels in loop back and test that a channel is
1510// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 FakeConstraints constraints;
1513 constraints.SetAllowRtpDataChannels();
1514 CreatePeerConnection(&constraints);
1515 scoped_refptr<DataChannelInterface> data1 =
1516 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001517 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 new MockDataChannelObserver(data1));
1519
1520 CreateOfferReceiveAnswerWithoutSsrc();
1521
1522 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1523
1524 data1->Close();
1525 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1526 CreateOfferReceiveAnswerWithoutSsrc();
1527 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1528 EXPECT_FALSE(observer1->IsOpen());
1529}
1530
1531// This test that if a data channel is added in an answer a receive only channel
1532// channel is created.
1533TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1534 FakeConstraints constraints;
1535 constraints.SetAllowRtpDataChannels();
1536 CreatePeerConnection(&constraints);
1537
1538 std::string offer_label = "offer_channel";
1539 scoped_refptr<DataChannelInterface> offer_channel =
1540 pc_->CreateDataChannel(offer_label, NULL);
1541
1542 CreateOfferAsLocalDescription();
1543
1544 // Replace the data channel label in the offer and apply it as an answer.
1545 std::string receive_label = "answer_channel";
1546 std::string sdp;
1547 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001548 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 receive_label.c_str(), receive_label.length(),
1550 &sdp);
1551 CreateAnswerAsRemoteDescription(sdp);
1552
1553 // Verify that a new incoming data channel has been created and that
1554 // it is open but can't we written to.
1555 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1556 DataChannelInterface* received_channel = observer_.last_datachannel_;
1557 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1558 EXPECT_EQ(receive_label, received_channel->label());
1559 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1560
1561 // Verify that the channel we initially offered has been rejected.
1562 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1563
1564 // Do another offer / answer exchange and verify that the data channel is
1565 // opened.
1566 CreateOfferReceiveAnswer();
1567 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1568 kTimeout);
1569}
1570
1571// This test that no data channel is returned if a reliable channel is
1572// requested.
1573// TODO(perkj): Remove this test once reliable channels are implemented.
1574TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1575 FakeConstraints constraints;
1576 constraints.SetAllowRtpDataChannels();
1577 CreatePeerConnection(&constraints);
1578
1579 std::string label = "test";
1580 webrtc::DataChannelInit config;
1581 config.reliable = true;
1582 scoped_refptr<DataChannelInterface> channel =
1583 pc_->CreateDataChannel(label, &config);
1584 EXPECT_TRUE(channel == NULL);
1585}
1586
deadbeefab9b2d12015-10-14 11:33:11 -07001587// Verifies that duplicated label is not allowed for RTP data channel.
1588TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1589 FakeConstraints constraints;
1590 constraints.SetAllowRtpDataChannels();
1591 CreatePeerConnection(&constraints);
1592
1593 std::string label = "test";
1594 scoped_refptr<DataChannelInterface> channel =
1595 pc_->CreateDataChannel(label, nullptr);
1596 EXPECT_NE(channel, nullptr);
1597
1598 scoped_refptr<DataChannelInterface> dup_channel =
1599 pc_->CreateDataChannel(label, nullptr);
1600 EXPECT_EQ(dup_channel, nullptr);
1601}
1602
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603// This tests that a SCTP data channel is returned using different
1604// DataChannelInit configurations.
1605TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1606 FakeConstraints constraints;
1607 constraints.SetAllowDtlsSctpDataChannels();
1608 CreatePeerConnection(&constraints);
1609
1610 webrtc::DataChannelInit config;
1611
1612 scoped_refptr<DataChannelInterface> channel =
1613 pc_->CreateDataChannel("1", &config);
1614 EXPECT_TRUE(channel != NULL);
1615 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001616 EXPECT_TRUE(observer_.renegotiation_needed_);
1617 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618
1619 config.ordered = false;
1620 channel = pc_->CreateDataChannel("2", &config);
1621 EXPECT_TRUE(channel != NULL);
1622 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001623 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624
1625 config.ordered = true;
1626 config.maxRetransmits = 0;
1627 channel = pc_->CreateDataChannel("3", &config);
1628 EXPECT_TRUE(channel != NULL);
1629 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001630 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631
1632 config.maxRetransmits = -1;
1633 config.maxRetransmitTime = 0;
1634 channel = pc_->CreateDataChannel("4", &config);
1635 EXPECT_TRUE(channel != NULL);
1636 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001637 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638}
1639
1640// This tests that no data channel is returned if both maxRetransmits and
1641// maxRetransmitTime are set for SCTP data channels.
1642TEST_F(PeerConnectionInterfaceTest,
1643 CreateSctpDataChannelShouldFailForInvalidConfig) {
1644 FakeConstraints constraints;
1645 constraints.SetAllowDtlsSctpDataChannels();
1646 CreatePeerConnection(&constraints);
1647
1648 std::string label = "test";
1649 webrtc::DataChannelInit config;
1650 config.maxRetransmits = 0;
1651 config.maxRetransmitTime = 0;
1652
1653 scoped_refptr<DataChannelInterface> channel =
1654 pc_->CreateDataChannel(label, &config);
1655 EXPECT_TRUE(channel == NULL);
1656}
1657
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658// The test verifies that creating a SCTP data channel with an id already in use
1659// or out of range should fail.
1660TEST_F(PeerConnectionInterfaceTest,
1661 CreateSctpDataChannelWithInvalidIdShouldFail) {
1662 FakeConstraints constraints;
1663 constraints.SetAllowDtlsSctpDataChannels();
1664 CreatePeerConnection(&constraints);
1665
1666 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001667 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001669 config.id = 1;
1670 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 EXPECT_TRUE(channel != NULL);
1672 EXPECT_EQ(1, channel->id());
1673
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 channel = pc_->CreateDataChannel("x", &config);
1675 EXPECT_TRUE(channel == NULL);
1676
1677 config.id = cricket::kMaxSctpSid;
1678 channel = pc_->CreateDataChannel("max", &config);
1679 EXPECT_TRUE(channel != NULL);
1680 EXPECT_EQ(config.id, channel->id());
1681
1682 config.id = cricket::kMaxSctpSid + 1;
1683 channel = pc_->CreateDataChannel("x", &config);
1684 EXPECT_TRUE(channel == NULL);
1685}
1686
deadbeefab9b2d12015-10-14 11:33:11 -07001687// Verifies that duplicated label is allowed for SCTP data channel.
1688TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1689 FakeConstraints constraints;
1690 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1691 true);
1692 CreatePeerConnection(&constraints);
1693
1694 std::string label = "test";
1695 scoped_refptr<DataChannelInterface> channel =
1696 pc_->CreateDataChannel(label, nullptr);
1697 EXPECT_NE(channel, nullptr);
1698
1699 scoped_refptr<DataChannelInterface> dup_channel =
1700 pc_->CreateDataChannel(label, nullptr);
1701 EXPECT_NE(dup_channel, nullptr);
1702}
1703
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001704// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1705// DataChannel.
1706TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1707 FakeConstraints constraints;
1708 constraints.SetAllowRtpDataChannels();
1709 CreatePeerConnection(&constraints);
1710
1711 scoped_refptr<DataChannelInterface> dc1 =
1712 pc_->CreateDataChannel("test1", NULL);
1713 EXPECT_TRUE(observer_.renegotiation_needed_);
1714 observer_.renegotiation_needed_ = false;
1715
1716 scoped_refptr<DataChannelInterface> dc2 =
1717 pc_->CreateDataChannel("test2", NULL);
1718 EXPECT_TRUE(observer_.renegotiation_needed_);
1719}
1720
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 FakeConstraints constraints;
1724 constraints.SetAllowRtpDataChannels();
1725 CreatePeerConnection(&constraints);
1726
1727 scoped_refptr<DataChannelInterface> data1 =
1728 pc_->CreateDataChannel("test1", NULL);
1729 scoped_refptr<DataChannelInterface> data2 =
1730 pc_->CreateDataChannel("test2", NULL);
1731 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001732 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001734 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 new MockDataChannelObserver(data2));
1736
1737 CreateOfferReceiveAnswer();
1738 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1739 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1740
1741 ReleasePeerConnection();
1742 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1743 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1744}
1745
1746// This test that data channels can be rejected in an answer.
1747TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1748 FakeConstraints constraints;
1749 constraints.SetAllowRtpDataChannels();
1750 CreatePeerConnection(&constraints);
1751
1752 scoped_refptr<DataChannelInterface> offer_channel(
1753 pc_->CreateDataChannel("offer_channel", NULL));
1754
1755 CreateOfferAsLocalDescription();
1756
1757 // Create an answer where the m-line for data channels are rejected.
1758 std::string sdp;
1759 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1760 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1761 SessionDescriptionInterface::kAnswer);
1762 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1763 cricket::ContentInfo* data_info =
1764 answer->description()->GetContentByName("data");
1765 data_info->rejected = true;
1766
1767 DoSetRemoteDescription(answer);
1768 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1769}
1770
1771// Test that we can create a session description from an SDP string from
1772// FireFox, use it as a remote session description, generate an answer and use
1773// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001774TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001775 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 FakeConstraints constraints;
1777 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1778 true);
1779 CreatePeerConnection(&constraints);
1780 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1781 SessionDescriptionInterface* desc =
1782 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001783 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1785 CreateAnswerAsLocalDescription();
1786 ASSERT_TRUE(pc_->local_description() != NULL);
1787 ASSERT_TRUE(pc_->remote_description() != NULL);
1788
1789 const cricket::ContentInfo* content =
1790 cricket::GetFirstAudioContent(pc_->local_description()->description());
1791 ASSERT_TRUE(content != NULL);
1792 EXPECT_FALSE(content->rejected);
1793
1794 content =
1795 cricket::GetFirstVideoContent(pc_->local_description()->description());
1796 ASSERT_TRUE(content != NULL);
1797 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001798#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 content =
1800 cricket::GetFirstDataContent(pc_->local_description()->description());
1801 ASSERT_TRUE(content != NULL);
1802 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001803#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804}
1805
1806// Test that we can create an audio only offer and receive an answer with a
1807// limited set of audio codecs and receive an updated offer with more audio
1808// codecs, where the added codecs are not supported.
1809TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1810 CreatePeerConnection();
1811 AddVoiceStream("audio_label");
1812 CreateOfferAsLocalDescription();
1813
1814 SessionDescriptionInterface* answer =
1815 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001816 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1818
1819 SessionDescriptionInterface* updated_offer =
1820 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001821 webrtc::kAudioSdpWithUnsupportedCodecs,
1822 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1824 CreateAnswerAsLocalDescription();
1825}
1826
deadbeefc80741f2015-10-22 13:14:45 -07001827// Test that if we're receiving (but not sending) a track, subsequent offers
1828// will have m-lines with a=recvonly.
1829TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1830 FakeConstraints constraints;
1831 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1832 true);
1833 CreatePeerConnection(&constraints);
1834 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1835 CreateAnswerAsLocalDescription();
1836
1837 // At this point we should be receiving stream 1, but not sending anything.
1838 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001839 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001840 DoCreateOffer(&offer, nullptr);
1841
1842 const cricket::ContentInfo* video_content =
1843 cricket::GetFirstVideoContent(offer->description());
1844 const cricket::VideoContentDescription* video_desc =
1845 static_cast<const cricket::VideoContentDescription*>(
1846 video_content->description);
1847 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1848
1849 const cricket::ContentInfo* audio_content =
1850 cricket::GetFirstAudioContent(offer->description());
1851 const cricket::AudioContentDescription* audio_desc =
1852 static_cast<const cricket::AudioContentDescription*>(
1853 audio_content->description);
1854 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1855}
1856
1857// Test that if we're receiving (but not sending) a track, and the
1858// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1859// false, the generated m-lines will be a=inactive.
1860TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1861 FakeConstraints constraints;
1862 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1863 true);
1864 CreatePeerConnection(&constraints);
1865 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1866 CreateAnswerAsLocalDescription();
1867
1868 // At this point we should be receiving stream 1, but not sending anything.
1869 // A new offer would be recvonly, but we'll set the "no receive" constraints
1870 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001871 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001872 FakeConstraints offer_constraints;
1873 offer_constraints.AddMandatory(
1874 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1875 offer_constraints.AddMandatory(
1876 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1877 DoCreateOffer(&offer, &offer_constraints);
1878
1879 const cricket::ContentInfo* video_content =
1880 cricket::GetFirstVideoContent(offer->description());
1881 const cricket::VideoContentDescription* video_desc =
1882 static_cast<const cricket::VideoContentDescription*>(
1883 video_content->description);
1884 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1885
1886 const cricket::ContentInfo* audio_content =
1887 cricket::GetFirstAudioContent(offer->description());
1888 const cricket::AudioContentDescription* audio_desc =
1889 static_cast<const cricket::AudioContentDescription*>(
1890 audio_content->description);
1891 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1892}
1893
deadbeef653b8e02015-11-11 12:55:10 -08001894// Test that we can use SetConfiguration to change the ICE servers of the
1895// PortAllocator.
1896TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1897 CreatePeerConnection();
1898
1899 PeerConnectionInterface::RTCConfiguration config;
1900 PeerConnectionInterface::IceServer server;
1901 server.uri = "stun:test_hostname";
1902 config.servers.push_back(server);
1903 EXPECT_TRUE(pc_->SetConfiguration(config));
1904
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001905 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1906 EXPECT_EQ("test_hostname",
1907 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001908}
1909
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910// Test that PeerConnection::Close changes the states to closed and all remote
1911// tracks change state to ended.
1912TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1913 // Initialize a PeerConnection and negotiate local and remote session
1914 // description.
1915 InitiateCall();
1916 ASSERT_EQ(1u, pc_->local_streams()->count());
1917 ASSERT_EQ(1u, pc_->remote_streams()->count());
1918
1919 pc_->Close();
1920
1921 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1922 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1923 pc_->ice_connection_state());
1924 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1925 pc_->ice_gathering_state());
1926
1927 EXPECT_EQ(1u, pc_->local_streams()->count());
1928 EXPECT_EQ(1u, pc_->remote_streams()->count());
1929
1930 scoped_refptr<MediaStreamInterface> remote_stream =
1931 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001932 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001933 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001934 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
1935 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
1936 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937}
1938
1939// Test that PeerConnection methods fails gracefully after
1940// PeerConnection::Close has been called.
1941TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1942 CreatePeerConnection();
1943 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1944 CreateOfferAsRemoteDescription();
1945 CreateAnswerAsLocalDescription();
1946
1947 ASSERT_EQ(1u, pc_->local_streams()->count());
1948 scoped_refptr<MediaStreamInterface> local_stream =
1949 pc_->local_streams()->at(0);
1950
1951 pc_->Close();
1952
1953 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001954 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955
1956 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001957 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001959 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960
1961 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1962
1963 EXPECT_TRUE(pc_->local_description() != NULL);
1964 EXPECT_TRUE(pc_->remote_description() != NULL);
1965
kwibergd1fe2812016-04-27 06:47:29 -07001966 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001967 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07001968 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001969 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
1971 std::string sdp;
1972 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1973 SessionDescriptionInterface* remote_offer =
1974 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1975 sdp, NULL);
1976 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1977
1978 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1979 SessionDescriptionInterface* local_offer =
1980 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1981 sdp, NULL);
1982 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1983}
1984
1985// Test that GetStats can still be called after PeerConnection::Close.
1986TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1987 InitiateCall();
1988 pc_->Close();
1989 DoGetStats(NULL);
1990}
deadbeefab9b2d12015-10-14 11:33:11 -07001991
1992// NOTE: The series of tests below come from what used to be
1993// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1994// setting a remote or local description has the expected effects.
1995
1996// This test verifies that the remote MediaStreams corresponding to a received
1997// SDP string is created. In this test the two separate MediaStreams are
1998// signaled.
1999TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2000 FakeConstraints constraints;
2001 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2002 true);
2003 CreatePeerConnection(&constraints);
2004 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2005
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002006 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002007 EXPECT_TRUE(
2008 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2009 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2010 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2011
2012 // Create a session description based on another SDP with another
2013 // MediaStream.
2014 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2015
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002016 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002017 EXPECT_TRUE(
2018 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2019}
2020
2021// This test verifies that when remote tracks are added/removed from SDP, the
2022// created remote streams are updated appropriately.
2023TEST_F(PeerConnectionInterfaceTest,
2024 AddRemoveTrackFromExistingRemoteMediaStream) {
2025 FakeConstraints constraints;
2026 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2027 true);
2028 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002029 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002030 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002031 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2032 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2033 reference_collection_));
2034
2035 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002036 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002037 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002038 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2039 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2040 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002041 scoped_refptr<AudioTrackInterface> audio_track2 =
2042 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2043 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2044 scoped_refptr<VideoTrackInterface> video_track2 =
2045 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2046 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002047
2048 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002049 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002050 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002051 MockTrackObserver audio_track_observer(audio_track2);
2052 MockTrackObserver video_track_observer(video_track2);
2053
2054 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2055 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002056 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2057 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2058 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002059 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002060 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002061 audio_track2->state(), kTimeout);
2062 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2063 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002064}
2065
2066// This tests that remote tracks are ended if a local session description is set
2067// that rejects the media content type.
2068TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2069 FakeConstraints constraints;
2070 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2071 true);
2072 CreatePeerConnection(&constraints);
2073 // First create and set a remote offer, then reject its video content in our
2074 // answer.
2075 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2076 ASSERT_EQ(1u, observer_.remote_streams()->count());
2077 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2078 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2079 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2080
2081 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2082 remote_stream->GetVideoTracks()[0];
2083 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2084 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2085 remote_stream->GetAudioTracks()[0];
2086 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2087
kwibergd1fe2812016-04-27 06:47:29 -07002088 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002089 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002090 cricket::ContentInfo* video_info =
2091 local_answer->description()->GetContentByName("video");
2092 video_info->rejected = true;
2093 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2094 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2095 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2096
2097 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002098 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002099 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002100 video_info = local_offer->description()->GetContentByName("video");
2101 ASSERT_TRUE(video_info != nullptr);
2102 video_info->rejected = true;
2103 cricket::ContentInfo* audio_info =
2104 local_offer->description()->GetContentByName("audio");
2105 ASSERT_TRUE(audio_info != nullptr);
2106 audio_info->rejected = true;
2107 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002108 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002109 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002110 remote_audio->state(), kTimeout);
2111 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2112 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002113}
2114
2115// This tests that we won't crash if the remote track has been removed outside
2116// of PeerConnection and then PeerConnection tries to reject the track.
2117TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2118 FakeConstraints constraints;
2119 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2120 true);
2121 CreatePeerConnection(&constraints);
2122 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2123 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2124 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2125 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2126
kwibergd1fe2812016-04-27 06:47:29 -07002127 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002128 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2129 kSdpStringWithStream1, nullptr));
2130 cricket::ContentInfo* video_info =
2131 local_answer->description()->GetContentByName("video");
2132 video_info->rejected = true;
2133 cricket::ContentInfo* audio_info =
2134 local_answer->description()->GetContentByName("audio");
2135 audio_info->rejected = true;
2136 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2137
2138 // No crash is a pass.
2139}
2140
deadbeef5e97fb52015-10-15 12:49:08 -07002141// This tests that if a recvonly remote description is set, no remote streams
2142// will be created, even if the description contains SSRCs/MSIDs.
2143// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2144TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2145 FakeConstraints constraints;
2146 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2147 true);
2148 CreatePeerConnection(&constraints);
2149
2150 std::string recvonly_offer = kSdpStringWithStream1;
2151 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2152 strlen(kRecvonly), &recvonly_offer);
2153 CreateAndSetRemoteOffer(recvonly_offer);
2154
2155 EXPECT_EQ(0u, observer_.remote_streams()->count());
2156}
2157
deadbeefab9b2d12015-10-14 11:33:11 -07002158// This tests that a default MediaStream is created if a remote session
2159// description doesn't contain any streams and no MSID support.
2160// It also tests that the default stream is updated if a video m-line is added
2161// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002162TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002163 FakeConstraints constraints;
2164 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2165 true);
2166 CreatePeerConnection(&constraints);
2167 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2168
2169 ASSERT_EQ(1u, observer_.remote_streams()->count());
2170 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2171
2172 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2173 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2174 EXPECT_EQ("default", remote_stream->label());
2175
2176 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2177 ASSERT_EQ(1u, observer_.remote_streams()->count());
2178 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2179 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002180 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2181 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002182 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2183 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002184 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2185 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002186}
2187
2188// This tests that a default MediaStream is created if a remote session
2189// description doesn't contain any streams and media direction is send only.
2190TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002191 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002192 FakeConstraints constraints;
2193 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2194 true);
2195 CreatePeerConnection(&constraints);
2196 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2197
2198 ASSERT_EQ(1u, observer_.remote_streams()->count());
2199 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2200
2201 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2202 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2203 EXPECT_EQ("default", remote_stream->label());
2204}
2205
2206// This tests that it won't crash when PeerConnection tries to remove
2207// a remote track that as already been removed from the MediaStream.
2208TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
2213 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2214 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2215 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2216 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2217
2218 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2219
2220 // No crash is a pass.
2221}
2222
2223// This tests that a default MediaStream is created if the remote session
2224// description doesn't contain any streams and don't contain an indication if
2225// MSID is supported.
2226TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002227 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002228 FakeConstraints constraints;
2229 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2230 true);
2231 CreatePeerConnection(&constraints);
2232 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2233
2234 ASSERT_EQ(1u, observer_.remote_streams()->count());
2235 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2236 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2237 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2238}
2239
2240// This tests that a default MediaStream is not created if the remote session
2241// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002242TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002243 FakeConstraints constraints;
2244 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2245 true);
2246 CreatePeerConnection(&constraints);
2247 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2248 EXPECT_EQ(0u, observer_.remote_streams()->count());
2249}
2250
deadbeefbda7e0b2015-12-08 17:13:40 -08002251// This tests that when setting a new description, the old default tracks are
2252// not destroyed and recreated.
2253// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002254TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002255 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002256 FakeConstraints constraints;
2257 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2258 true);
2259 CreatePeerConnection(&constraints);
2260 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2261
2262 ASSERT_EQ(1u, observer_.remote_streams()->count());
2263 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2264 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2265
2266 // Set the track to "disabled", then set a new description and ensure the
2267 // track is still disabled, which ensures it hasn't been recreated.
2268 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2269 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2270 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2271 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2272}
2273
deadbeefab9b2d12015-10-14 11:33:11 -07002274// This tests that a default MediaStream is not created if a remote session
2275// description is updated to not have any MediaStreams.
2276TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2277 FakeConstraints constraints;
2278 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2279 true);
2280 CreatePeerConnection(&constraints);
2281 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002282 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002283 EXPECT_TRUE(
2284 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2285
2286 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2287 EXPECT_EQ(0u, observer_.remote_streams()->count());
2288}
2289
2290// This tests that an RtpSender is created when the local description is set
2291// after adding a local stream.
2292// TODO(deadbeef): This test and the one below it need to be updated when
2293// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002294TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002295 FakeConstraints constraints;
2296 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2297 true);
2298 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002299
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002300 // Create an offer with 1 stream with 2 tracks of each type.
2301 rtc::scoped_refptr<StreamCollection> stream_collection =
2302 CreateStreamCollection(1, 2);
2303 pc_->AddStream(stream_collection->at(0));
2304 std::unique_ptr<SessionDescriptionInterface> offer;
2305 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2306 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002307
deadbeefab9b2d12015-10-14 11:33:11 -07002308 auto senders = pc_->GetSenders();
2309 EXPECT_EQ(4u, senders.size());
2310 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2311 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2312 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2313 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2314
2315 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002316 pc_->RemoveStream(stream_collection->at(0));
2317 stream_collection = CreateStreamCollection(1, 1);
2318 pc_->AddStream(stream_collection->at(0));
2319 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2320 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2321
deadbeefab9b2d12015-10-14 11:33:11 -07002322 senders = pc_->GetSenders();
2323 EXPECT_EQ(2u, senders.size());
2324 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2325 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2326 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2327 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2328}
2329
2330// This tests that an RtpSender is created when the local description is set
2331// before adding a local stream.
2332TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002333 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002334 FakeConstraints constraints;
2335 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2336 true);
2337 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002338
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002339 rtc::scoped_refptr<StreamCollection> stream_collection =
2340 CreateStreamCollection(1, 2);
2341 // Add a stream to create the offer, but remove it afterwards.
2342 pc_->AddStream(stream_collection->at(0));
2343 std::unique_ptr<SessionDescriptionInterface> offer;
2344 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2345 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002346
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002347 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002348 auto senders = pc_->GetSenders();
2349 EXPECT_EQ(0u, senders.size());
2350
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002351 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002352 senders = pc_->GetSenders();
2353 EXPECT_EQ(4u, senders.size());
2354 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2355 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2356 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2357 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2358}
2359
2360// This tests that the expected behavior occurs if the SSRC on a local track is
2361// changed when SetLocalDescription is called.
2362TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002363 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002364 FakeConstraints constraints;
2365 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2366 true);
2367 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002368
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002369 rtc::scoped_refptr<StreamCollection> stream_collection =
2370 CreateStreamCollection(2, 1);
2371 pc_->AddStream(stream_collection->at(0));
2372 std::unique_ptr<SessionDescriptionInterface> offer;
2373 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2374 // Grab a copy of the offer before it gets passed into the PC.
2375 std::unique_ptr<JsepSessionDescription> modified_offer(
2376 new JsepSessionDescription(JsepSessionDescription::kOffer));
2377 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2378 offer->session_version());
2379 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002380
deadbeefab9b2d12015-10-14 11:33:11 -07002381 auto senders = pc_->GetSenders();
2382 EXPECT_EQ(2u, senders.size());
2383 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2384 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2385
2386 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002387 cricket::MediaContentDescription* desc =
2388 cricket::GetFirstAudioContentDescription(modified_offer->description());
2389 ASSERT_TRUE(desc != NULL);
2390 for (StreamParams& stream : desc->mutable_streams()) {
2391 for (unsigned int& ssrc : stream.ssrcs) {
2392 ++ssrc;
2393 }
2394 }
deadbeefab9b2d12015-10-14 11:33:11 -07002395
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002396 desc =
2397 cricket::GetFirstVideoContentDescription(modified_offer->description());
2398 ASSERT_TRUE(desc != NULL);
2399 for (StreamParams& stream : desc->mutable_streams()) {
2400 for (unsigned int& ssrc : stream.ssrcs) {
2401 ++ssrc;
2402 }
2403 }
2404
2405 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002406 senders = pc_->GetSenders();
2407 EXPECT_EQ(2u, senders.size());
2408 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2409 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2410 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2411 // changed.
2412}
2413
2414// This tests that the expected behavior occurs if a new session description is
2415// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002416TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002417 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002418 FakeConstraints constraints;
2419 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2420 true);
2421 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002422
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002423 rtc::scoped_refptr<StreamCollection> stream_collection =
2424 CreateStreamCollection(2, 1);
2425 pc_->AddStream(stream_collection->at(0));
2426 std::unique_ptr<SessionDescriptionInterface> offer;
2427 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2428 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002429
deadbeefab9b2d12015-10-14 11:33:11 -07002430 auto senders = pc_->GetSenders();
2431 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002432 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2433 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002434
2435 // Add a new MediaStream but with the same tracks as in the first stream.
2436 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2437 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002438 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2439 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002440 pc_->AddStream(stream_1);
2441
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002442 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2443 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002444
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002445 auto new_senders = pc_->GetSenders();
2446 // Should be the same senders as before, but with updated stream id.
2447 // Note that this behavior is subject to change in the future.
2448 // We may decide the PC should ignore existing tracks in AddStream.
2449 EXPECT_EQ(senders, new_senders);
2450 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2451 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002452}
2453
nisse51542be2016-02-12 02:27:06 -08002454// The PeerConnectionMediaConfig tests below verify that configuration
2455// and constraints are propagated into the MediaConfig passed to
2456// CreateMediaController. These settings are intended for MediaChannel
2457// constructors, but that is not exercised by these unittest.
2458class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2459 public:
2460 webrtc::MediaControllerInterface* CreateMediaController(
2461 const cricket::MediaConfig& config) const override {
2462 create_media_controller_called_ = true;
2463 create_media_controller_config_ = config;
2464
2465 webrtc::MediaControllerInterface* mc =
2466 PeerConnectionFactory::CreateMediaController(config);
2467 EXPECT_TRUE(mc != nullptr);
2468 return mc;
2469 }
2470
2471 // Mutable, so they can be modified in the above const-declared method.
2472 mutable bool create_media_controller_called_ = false;
2473 mutable cricket::MediaConfig create_media_controller_config_;
2474};
2475
2476class PeerConnectionMediaConfigTest : public testing::Test {
2477 protected:
2478 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002479 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002480 pcf_->Initialize();
2481 }
2482 const cricket::MediaConfig& TestCreatePeerConnection(
2483 const PeerConnectionInterface::RTCConfiguration& config,
2484 const MediaConstraintsInterface *constraints) {
2485 pcf_->create_media_controller_called_ = false;
2486
2487 scoped_refptr<PeerConnectionInterface> pc(
2488 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2489 &observer_));
2490 EXPECT_TRUE(pc.get());
2491 EXPECT_TRUE(pcf_->create_media_controller_called_);
2492 return pcf_->create_media_controller_config_;
2493 }
2494
2495 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2496 MockPeerConnectionObserver observer_;
2497};
2498
2499// This test verifies the default behaviour with no constraints and a
2500// default RTCConfiguration.
2501TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2502 PeerConnectionInterface::RTCConfiguration config;
2503 FakeConstraints constraints;
2504
2505 const cricket::MediaConfig& media_config =
2506 TestCreatePeerConnection(config, &constraints);
2507
2508 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002509 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2510 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2511 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002512}
2513
2514// This test verifies the DSCP constraint is recognized and passed to
2515// the CreateMediaController call.
2516TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2517 PeerConnectionInterface::RTCConfiguration config;
2518 FakeConstraints constraints;
2519
2520 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2521 const cricket::MediaConfig& media_config =
2522 TestCreatePeerConnection(config, &constraints);
2523
2524 EXPECT_TRUE(media_config.enable_dscp);
2525}
2526
2527// This test verifies the cpu overuse detection constraint is
2528// recognized and passed to the CreateMediaController call.
2529TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2530 PeerConnectionInterface::RTCConfiguration config;
2531 FakeConstraints constraints;
2532
2533 constraints.AddOptional(
2534 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2535 const cricket::MediaConfig media_config =
2536 TestCreatePeerConnection(config, &constraints);
2537
nisse0db023a2016-03-01 04:29:59 -08002538 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002539}
2540
2541// This test verifies that the disable_prerenderer_smoothing flag is
2542// propagated from RTCConfiguration to the CreateMediaController call.
2543TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2544 PeerConnectionInterface::RTCConfiguration config;
2545 FakeConstraints constraints;
2546
Niels Möller71bdda02016-03-31 12:59:59 +02002547 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002548 const cricket::MediaConfig& media_config =
2549 TestCreatePeerConnection(config, &constraints);
2550
nisse0db023a2016-03-01 04:29:59 -08002551 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2552}
2553
2554// This test verifies the suspend below min bitrate constraint is
2555// recognized and passed to the CreateMediaController call.
2556TEST_F(PeerConnectionMediaConfigTest,
2557 TestSuspendBelowMinBitrateConstraintTrue) {
2558 PeerConnectionInterface::RTCConfiguration config;
2559 FakeConstraints constraints;
2560
2561 constraints.AddOptional(
2562 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2563 true);
2564 const cricket::MediaConfig media_config =
2565 TestCreatePeerConnection(config, &constraints);
2566
2567 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002568}
2569
deadbeefab9b2d12015-10-14 11:33:11 -07002570// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002571// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2572// "verify options are converted correctly", should be "pass options into
2573// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002574
2575TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2576 RTCOfferAnswerOptions rtc_options;
2577 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2578
2579 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002580 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002581
2582 rtc_options.offer_to_receive_audio =
2583 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002584 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002585}
2586
2587TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2588 RTCOfferAnswerOptions rtc_options;
2589 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2590
2591 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002592 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002593
2594 rtc_options.offer_to_receive_video =
2595 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002596 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002597}
2598
2599// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002600// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002601TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2602 RTCOfferAnswerOptions rtc_options;
2603 rtc_options.offer_to_receive_audio = 1;
2604 rtc_options.offer_to_receive_video = 1;
2605
2606 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002607 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002608 EXPECT_TRUE(options.has_audio());
2609 EXPECT_TRUE(options.has_video());
2610 EXPECT_TRUE(options.bundle_enabled);
2611}
2612
2613// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002614// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002615TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2616 RTCOfferAnswerOptions rtc_options;
2617 rtc_options.offer_to_receive_audio = 1;
2618
2619 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002620 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002621 EXPECT_TRUE(options.has_audio());
2622 EXPECT_FALSE(options.has_video());
2623 EXPECT_TRUE(options.bundle_enabled);
2624}
2625
2626// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002627// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002628TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2629 RTCOfferAnswerOptions rtc_options;
2630
2631 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002632 options.transport_options["audio"] = cricket::TransportOptions();
2633 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002634 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002635 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002636 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002637 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002638 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002639 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2640 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002641}
2642
2643// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002644// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002645TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2646 RTCOfferAnswerOptions rtc_options;
2647 rtc_options.offer_to_receive_audio = 0;
2648 rtc_options.offer_to_receive_video = 1;
2649
2650 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002651 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002652 EXPECT_FALSE(options.has_audio());
2653 EXPECT_TRUE(options.has_video());
2654 EXPECT_TRUE(options.bundle_enabled);
2655}
2656
2657// Test that a correct MediaSessionOptions is created for an offer if
2658// UseRtpMux is set to false.
2659TEST(CreateSessionOptionsTest,
2660 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2661 RTCOfferAnswerOptions rtc_options;
2662 rtc_options.offer_to_receive_audio = 1;
2663 rtc_options.offer_to_receive_video = 1;
2664 rtc_options.use_rtp_mux = false;
2665
2666 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002667 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002668 EXPECT_TRUE(options.has_audio());
2669 EXPECT_TRUE(options.has_video());
2670 EXPECT_FALSE(options.bundle_enabled);
2671}
2672
2673// Test that a correct MediaSessionOptions is created to restart ice if
2674// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002675// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002676TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2677 RTCOfferAnswerOptions rtc_options;
2678 rtc_options.ice_restart = true;
2679
2680 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002681 options.transport_options["audio"] = cricket::TransportOptions();
2682 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002683 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002684 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2685 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002686
2687 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002688 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002689 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2690 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002691}
2692
2693// Test that the MediaConstraints in an answer don't affect if audio and video
2694// is offered in an offer but that if kOfferToReceiveAudio or
2695// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2696// included in subsequent answers.
2697TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2698 FakeConstraints answer_c;
2699 answer_c.SetMandatoryReceiveAudio(true);
2700 answer_c.SetMandatoryReceiveVideo(true);
2701
2702 cricket::MediaSessionOptions answer_options;
2703 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2704 EXPECT_TRUE(answer_options.has_audio());
2705 EXPECT_TRUE(answer_options.has_video());
2706
deadbeefc80741f2015-10-22 13:14:45 -07002707 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002708
2709 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002710 EXPECT_TRUE(
2711 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002712 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002713 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002714
deadbeefc80741f2015-10-22 13:14:45 -07002715 RTCOfferAnswerOptions updated_rtc_offer_options;
2716 updated_rtc_offer_options.offer_to_receive_audio = 1;
2717 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002718
2719 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002720 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002721 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002722 EXPECT_TRUE(updated_offer_options.has_audio());
2723 EXPECT_TRUE(updated_offer_options.has_video());
2724
2725 // Since an offer has been created with both audio and video, subsequent
2726 // offers and answers should contain both audio and video.
2727 // Answers will only contain the media types that exist in the offer
2728 // regardless of the value of |updated_answer_options.has_audio| and
2729 // |updated_answer_options.has_video|.
2730 FakeConstraints updated_answer_c;
2731 answer_c.SetMandatoryReceiveAudio(false);
2732 answer_c.SetMandatoryReceiveVideo(false);
2733
2734 cricket::MediaSessionOptions updated_answer_options;
2735 EXPECT_TRUE(
2736 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2737 EXPECT_TRUE(updated_answer_options.has_audio());
2738 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002739}