blob: 843a312292cb5fcfaab6cabca4b64fcff65ee798 [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"
Henrik Boströmd79599d2016-06-01 13:58:50 +020029#include "webrtc/api/test/fakertccertificategenerator.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 Brandstettera1c30352016-05-13 08:15:11 -070042#include "webrtc/p2p/base/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) {
deadbeefa601f5c2016-06-06 14:27:39 -0700336 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700337 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_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700476 void OnAddStream(rtc::scoped_refptr<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 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700480 void OnRemoveStream(
481 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700483 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 }
perkjdfb769d2016-02-09 03:09:43 -0800485 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700486 void OnDataChannel(
487 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 last_datachannel_ = data_channel;
489 }
490
perkjdfb769d2016-02-09 03:09:43 -0800491 void OnIceConnectionChange(
492 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 EXPECT_EQ(pc_->ice_connection_state(), new_state);
494 }
perkjdfb769d2016-02-09 03:09:43 -0800495 void OnIceGatheringChange(
496 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800498 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
perkjdfb769d2016-02-09 03:09:43 -0800500 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
502 pc_->ice_gathering_state());
503
504 std::string sdp;
505 EXPECT_TRUE(candidate->ToString(&sdp));
506 EXPECT_LT(0u, sdp.size());
507 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
508 candidate->sdp_mline_index(), sdp, NULL));
509 EXPECT_TRUE(last_candidate_.get() != NULL);
510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 // Returns the label of the last added stream.
513 // Empty string if no stream have been added.
514 std::string GetLastAddedStreamLabel() {
515 if (last_added_stream_.get())
516 return last_added_stream_->label();
517 return "";
518 }
519 std::string GetLastRemovedStreamLabel() {
520 if (last_removed_stream_.get())
521 return last_removed_stream_->label();
522 return "";
523 }
524
525 scoped_refptr<PeerConnectionInterface> pc_;
526 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700527 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700529 rtc::scoped_refptr<StreamCollection> remote_streams_;
530 bool renegotiation_needed_ = false;
531 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
533 private:
534 scoped_refptr<MediaStreamInterface> last_added_stream_;
535 scoped_refptr<MediaStreamInterface> last_removed_stream_;
536};
537
538} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540class PeerConnectionInterfaceTest : public testing::Test {
541 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800542 PeerConnectionInterfaceTest() {
543#ifdef WEBRTC_ANDROID
544 webrtc::InitializeAndroidObjects();
545#endif
546 }
547
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 virtual void SetUp() {
549 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700550 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
551 nullptr, nullptr, nullptr);
552 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
554
555 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700556 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
558
559 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700560 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
561 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
563
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700564 void CreatePeerConnectionWithIceTransportsType(
565 PeerConnectionInterface::IceTransportsType type) {
566 PeerConnectionInterface::RTCConfiguration config;
567 config.type = type;
568 return CreatePeerConnection(config, nullptr);
569 }
570
571 void CreatePeerConnectionWithIceServer(const std::string& uri,
572 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800573 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700575 server.uri = uri;
576 server.password = password;
577 config.servers.push_back(server);
578 CreatePeerConnection(config, nullptr);
579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700581 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
582 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700583 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800584 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
585 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000586
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000587 // DTLS does not work in a loopback call, so is disabled for most of the
588 // tests in this file. We only create a FakeIdentityService if the test
589 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000590 FakeConstraints default_constraints;
591 if (!constraints) {
592 constraints = &default_constraints;
593
594 default_constraints.AddMandatory(
595 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
596 }
597
Henrik Boströmd79599d2016-06-01 13:58:50 +0200598 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000599 bool dtls;
600 if (FindConstraint(constraints,
601 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
602 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200603 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200604 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000605 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200606 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800607 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200608 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 ASSERT_TRUE(pc_.get() != NULL);
610 observer_.SetPeerConnectionInterface(pc_.get());
611 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
612 }
613
deadbeef0a6c4ca2015-10-06 11:38:28 -0700614 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800615 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700616 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700617 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800618 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700619
deadbeef0a6c4ca2015-10-06 11:38:28 -0700620 scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700621 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
622 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800623 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700627 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800628 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
629 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
630 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800632 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
deadbeef0a6c4ca2015-10-06 11:38:28 -0700634 CreatePeerConnectionExpectFail(kStunInvalidPort);
635 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
636 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700638 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800639 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
640 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800646 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648
649 void ReleasePeerConnection() {
650 pc_ = NULL;
651 observer_.SetPeerConnectionInterface(NULL);
652 }
653
deadbeefab9b2d12015-10-14 11:33:11 -0700654 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 // Create a local stream.
656 scoped_refptr<MediaStreamInterface> stream(
657 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100658 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
660 scoped_refptr<VideoTrackInterface> video_track(
661 pc_factory_->CreateVideoTrack(label + "v0", video_source));
662 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000663 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
665 observer_.renegotiation_needed_ = false;
666 }
667
668 void AddVoiceStream(const std::string& label) {
669 // Create a local stream.
670 scoped_refptr<MediaStreamInterface> stream(
671 pc_factory_->CreateLocalMediaStream(label));
672 scoped_refptr<AudioTrackInterface> audio_track(
673 pc_factory_->CreateAudioTrack(label + "a0", NULL));
674 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000675 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
677 observer_.renegotiation_needed_ = false;
678 }
679
680 void AddAudioVideoStream(const std::string& stream_label,
681 const std::string& audio_track_label,
682 const std::string& video_track_label) {
683 // Create a local stream.
684 scoped_refptr<MediaStreamInterface> stream(
685 pc_factory_->CreateLocalMediaStream(stream_label));
686 scoped_refptr<AudioTrackInterface> audio_track(
687 pc_factory_->CreateAudioTrack(
688 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
689 stream->AddTrack(audio_track.get());
690 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700691 pc_factory_->CreateVideoTrack(
692 video_track_label,
693 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000695 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
697 observer_.renegotiation_needed_ = false;
698 }
699
kwibergd1fe2812016-04-27 06:47:29 -0700700 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700701 bool offer,
702 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000703 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
704 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 MockCreateSessionDescriptionObserver>());
706 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700707 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700709 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 }
711 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700712 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 return observer->result();
714 }
715
kwibergd1fe2812016-04-27 06:47:29 -0700716 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700717 MediaConstraintsInterface* constraints) {
718 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
kwibergd1fe2812016-04-27 06:47:29 -0700721 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700722 MediaConstraintsInterface* constraints) {
723 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000727 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
728 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 MockSetSessionDescriptionObserver>());
730 if (local) {
731 pc_->SetLocalDescription(observer, desc);
732 } else {
733 pc_->SetRemoteDescription(observer, desc);
734 }
735 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
736 return observer->result();
737 }
738
739 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
740 return DoSetSessionDescription(desc, true);
741 }
742
743 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
744 return DoSetSessionDescription(desc, false);
745 }
746
747 // Calls PeerConnection::GetStats and check the return value.
748 // It does not verify the values in the StatReports since a RTCP packet might
749 // be required.
750 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000751 rtc::scoped_refptr<MockStatsObserver> observer(
752 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000753 if (!pc_->GetStats(
754 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return false;
756 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
757 return observer->called();
758 }
759
760 void InitiateCall() {
761 CreatePeerConnection();
762 // Create a local stream with audio&video tracks.
763 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
764 CreateOfferReceiveAnswer();
765 }
766
767 // Verify that RTP Header extensions has been negotiated for audio and video.
768 void VerifyRemoteRtpHeaderExtensions() {
769 const cricket::MediaContentDescription* desc =
770 cricket::GetFirstAudioContentDescription(
771 pc_->remote_description()->description());
772 ASSERT_TRUE(desc != NULL);
773 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
774
775 desc = cricket::GetFirstVideoContentDescription(
776 pc_->remote_description()->description());
777 ASSERT_TRUE(desc != NULL);
778 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
779 }
780
781 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700782 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700783 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 std::string sdp;
785 EXPECT_TRUE(offer->ToString(&sdp));
786 SessionDescriptionInterface* remote_offer =
787 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
788 sdp, NULL);
789 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
790 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
791 }
792
deadbeefab9b2d12015-10-14 11:33:11 -0700793 void CreateAndSetRemoteOffer(const std::string& sdp) {
794 SessionDescriptionInterface* remote_offer =
795 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
796 sdp, nullptr);
797 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
798 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
799 }
800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700802 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700803 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
805 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
806 // audio codec change, even if the parameter has nothing to do with
807 // receiving. Not all parameters are serialized to SDP.
808 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
809 // the SessionDescription, it is necessary to do that here to in order to
810 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
811 // https://code.google.com/p/webrtc/issues/detail?id=1356
812 std::string sdp;
813 EXPECT_TRUE(answer->ToString(&sdp));
814 SessionDescriptionInterface* new_answer =
815 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
816 sdp, NULL);
817 EXPECT_TRUE(DoSetLocalDescription(new_answer));
818 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
819 }
820
821 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700822 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700823 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
825 std::string sdp;
826 EXPECT_TRUE(answer->ToString(&sdp));
827 SessionDescriptionInterface* pr_answer =
828 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
829 sdp, NULL);
830 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
831 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
832 }
833
834 void CreateOfferReceiveAnswer() {
835 CreateOfferAsLocalDescription();
836 std::string sdp;
837 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
838 CreateAnswerAsRemoteDescription(sdp);
839 }
840
841 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700842 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700843 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
845 // audio codec change, even if the parameter has nothing to do with
846 // receiving. Not all parameters are serialized to SDP.
847 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
848 // the SessionDescription, it is necessary to do that here to in order to
849 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
850 // https://code.google.com/p/webrtc/issues/detail?id=1356
851 std::string sdp;
852 EXPECT_TRUE(offer->ToString(&sdp));
853 SessionDescriptionInterface* new_offer =
854 webrtc::CreateSessionDescription(
855 SessionDescriptionInterface::kOffer,
856 sdp, NULL);
857
858 EXPECT_TRUE(DoSetLocalDescription(new_offer));
859 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000860 // Wait for the ice_complete message, so that SDP will have candidates.
861 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863
deadbeefab9b2d12015-10-14 11:33:11 -0700864 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
866 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700867 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 EXPECT_TRUE(DoSetRemoteDescription(answer));
869 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
870 }
871
deadbeefab9b2d12015-10-14 11:33:11 -0700872 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 webrtc::JsepSessionDescription* pr_answer =
874 new webrtc::JsepSessionDescription(
875 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700876 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
878 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
879 webrtc::JsepSessionDescription* answer =
880 new webrtc::JsepSessionDescription(
881 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700882 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 EXPECT_TRUE(DoSetRemoteDescription(answer));
884 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
885 }
886
887 // Help function used for waiting until a the last signaled remote stream has
888 // the same label as |stream_label|. In a few of the tests in this file we
889 // answer with the same session description as we offer and thus we can
890 // check if OnAddStream have been called with the same stream as we offer to
891 // send.
892 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
893 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
894 }
895
896 // Creates an offer and applies it as a local session description.
897 // Creates an answer with the same SDP an the offer but removes all lines
898 // that start with a:ssrc"
899 void CreateOfferReceiveAnswerWithoutSsrc() {
900 CreateOfferAsLocalDescription();
901 std::string sdp;
902 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
903 SetSsrcToZero(&sdp);
904 CreateAnswerAsRemoteDescription(sdp);
905 }
906
deadbeefab9b2d12015-10-14 11:33:11 -0700907 // This function creates a MediaStream with label kStreams[0] and
908 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
909 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700910 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700911 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700912 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700913 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
914 size_t number_of_video_tracks) {
915 EXPECT_LE(number_of_audio_tracks, 2u);
916 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700917
918 reference_collection_ = StreamCollection::Create();
919 std::string sdp_ms1 = std::string(kSdpStringInit);
920
921 std::string mediastream_label = kStreams[0];
922
923 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
924 webrtc::MediaStream::Create(mediastream_label));
925 reference_collection_->AddStream(stream);
926
927 if (number_of_audio_tracks > 0) {
928 sdp_ms1 += std::string(kSdpStringAudio);
929 sdp_ms1 += std::string(kSdpStringMs1Audio0);
930 AddAudioTrack(kAudioTracks[0], stream);
931 }
932 if (number_of_audio_tracks > 1) {
933 sdp_ms1 += kSdpStringMs1Audio1;
934 AddAudioTrack(kAudioTracks[1], stream);
935 }
936
937 if (number_of_video_tracks > 0) {
938 sdp_ms1 += std::string(kSdpStringVideo);
939 sdp_ms1 += std::string(kSdpStringMs1Video0);
940 AddVideoTrack(kVideoTracks[0], stream);
941 }
942 if (number_of_video_tracks > 1) {
943 sdp_ms1 += kSdpStringMs1Video1;
944 AddVideoTrack(kVideoTracks[1], stream);
945 }
946
kwibergd1fe2812016-04-27 06:47:29 -0700947 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700948 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
949 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700950 }
951
952 void AddAudioTrack(const std::string& track_id,
953 MediaStreamInterface* stream) {
954 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
955 webrtc::AudioTrack::Create(track_id, nullptr));
956 ASSERT_TRUE(stream->AddTrack(audio_track));
957 }
958
959 void AddVideoTrack(const std::string& track_id,
960 MediaStreamInterface* stream) {
961 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700962 webrtc::VideoTrack::Create(track_id,
963 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700964 ASSERT_TRUE(stream->AddTrack(video_track));
965 }
966
kwibergfd8be342016-05-14 19:44:11 -0700967 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700968 CreatePeerConnection();
969 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700970 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700971 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
972 return offer;
973 }
974
kwibergfd8be342016-05-14 19:44:11 -0700975 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700976 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700977 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700978 CreateOfferWithOneAudioStream();
979 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700980 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700981 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
982 return answer;
983 }
984
985 const std::string& GetFirstAudioStreamCname(
986 const SessionDescriptionInterface* desc) {
987 const cricket::ContentInfo* audio_content =
988 cricket::GetFirstAudioContent(desc->description());
989 const cricket::AudioContentDescription* audio_desc =
990 static_cast<const cricket::AudioContentDescription*>(
991 audio_content->description);
992 return audio_desc->streams()[0].cname;
993 }
994
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800995 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
997 scoped_refptr<PeerConnectionInterface> pc_;
998 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700999 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000};
1001
zhihuang8f65cdf2016-05-06 18:40:30 -07001002// Generate different CNAMEs when PeerConnections are created.
1003// The CNAMEs are expected to be generated randomly. It is possible
1004// that the test fails, though the possibility is very low.
1005TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001006 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001008 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001009 CreateOfferWithOneAudioStream();
1010 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1011 GetFirstAudioStreamCname(offer2.get()));
1012}
1013
1014TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001017 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001018 CreateAnswerWithOneAudioStream();
1019 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1020 GetFirstAudioStreamCname(answer2.get()));
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023TEST_F(PeerConnectionInterfaceTest,
1024 CreatePeerConnectionWithDifferentConfigurations) {
1025 CreatePeerConnectionWithDifferentConfigurations();
1026}
1027
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028TEST_F(PeerConnectionInterfaceTest,
1029 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1030 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1031 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1033 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1034 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1035 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1036 port_allocator_->candidate_filter());
1037 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1038 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1039}
1040
1041// Test that when a PeerConnection is created with a nonzero candidate pool
1042// size, the pooled PortAllocatorSession is created with all the attributes
1043// in the RTCConfiguration.
1044TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1045 PeerConnectionInterface::RTCConfiguration config;
1046 PeerConnectionInterface::IceServer server;
1047 server.uri = kStunAddressOnly;
1048 config.servers.push_back(server);
1049 config.type = PeerConnectionInterface::kRelay;
1050 config.disable_ipv6 = true;
1051 config.tcp_candidate_policy =
1052 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001053 config.candidate_network_policy =
1054 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001055 config.ice_candidate_pool_size = 1;
1056 CreatePeerConnection(config, nullptr);
1057
1058 const cricket::FakePortAllocatorSession* session =
1059 static_cast<const cricket::FakePortAllocatorSession*>(
1060 port_allocator_->GetPooledSession());
1061 ASSERT_NE(nullptr, session);
1062 EXPECT_EQ(1UL, session->stun_servers().size());
1063 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1064 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001065 EXPECT_LT(0U,
1066 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001067}
1068
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001069// Test that the PeerConnection initializes the port allocator passed into it,
1070// and on the correct thread.
1071TEST_F(PeerConnectionInterfaceTest,
1072 CreatePeerConnectionInitializesPortAllocator) {
1073 rtc::Thread network_thread;
1074 network_thread.Start();
1075 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1076 webrtc::CreatePeerConnectionFactory(
1077 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1078 nullptr, nullptr, nullptr));
1079 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1080 new cricket::FakePortAllocator(&network_thread, nullptr));
1081 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1082 PeerConnectionInterface::RTCConfiguration config;
1083 rtc::scoped_refptr<PeerConnectionInterface> pc(
1084 pc_factory->CreatePeerConnection(
1085 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1086 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1087 // so all we have to do here is check that it's initialized.
1088 EXPECT_TRUE(raw_port_allocator->initialized());
1089}
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1092 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001093 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 AddVoiceStream(kStreamLabel2);
1095 ASSERT_EQ(2u, pc_->local_streams()->count());
1096
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001097 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 scoped_refptr<MediaStreamInterface> stream(
1099 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1100 scoped_refptr<AudioTrackInterface> audio_track(
1101 pc_factory_->CreateAudioTrack(
1102 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1103 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001104 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001105 EXPECT_EQ(3u, pc_->local_streams()->count());
1106
1107 // Remove the third stream.
1108 pc_->RemoveStream(pc_->local_streams()->at(2));
1109 EXPECT_EQ(2u, pc_->local_streams()->count());
1110
1111 // Remove the second stream.
1112 pc_->RemoveStream(pc_->local_streams()->at(1));
1113 EXPECT_EQ(1u, pc_->local_streams()->count());
1114
1115 // Remove the first stream.
1116 pc_->RemoveStream(pc_->local_streams()->at(0));
1117 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118}
1119
deadbeefab9b2d12015-10-14 11:33:11 -07001120// Test that the created offer includes streams we added.
1121TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1122 CreatePeerConnection();
1123 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001124 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001125 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001126
1127 const cricket::ContentInfo* audio_content =
1128 cricket::GetFirstAudioContent(offer->description());
1129 const cricket::AudioContentDescription* audio_desc =
1130 static_cast<const cricket::AudioContentDescription*>(
1131 audio_content->description);
1132 EXPECT_TRUE(
1133 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1134
1135 const cricket::ContentInfo* video_content =
1136 cricket::GetFirstVideoContent(offer->description());
1137 const cricket::VideoContentDescription* video_desc =
1138 static_cast<const cricket::VideoContentDescription*>(
1139 video_content->description);
1140 EXPECT_TRUE(
1141 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1142
1143 // Add another stream and ensure the offer includes both the old and new
1144 // streams.
1145 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001146 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001147
1148 audio_content = cricket::GetFirstAudioContent(offer->description());
1149 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1150 audio_content->description);
1151 EXPECT_TRUE(
1152 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1153 EXPECT_TRUE(
1154 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1155
1156 video_content = cricket::GetFirstVideoContent(offer->description());
1157 video_desc = static_cast<const cricket::VideoContentDescription*>(
1158 video_content->description);
1159 EXPECT_TRUE(
1160 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1161 EXPECT_TRUE(
1162 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1163}
1164
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1166 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001167 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 ASSERT_EQ(1u, pc_->local_streams()->count());
1169 pc_->RemoveStream(pc_->local_streams()->at(0));
1170 EXPECT_EQ(0u, pc_->local_streams()->count());
1171}
1172
deadbeefe1f9d832016-01-14 15:35:42 -08001173// Test for AddTrack and RemoveTrack methods.
1174// Tests that the created offer includes tracks we added,
1175// and that the RtpSenders are created correctly.
1176// Also tests that RemoveTrack removes the tracks from subsequent offers.
1177TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1178 CreatePeerConnection();
1179 // Create a dummy stream, so tracks share a stream label.
1180 scoped_refptr<MediaStreamInterface> stream(
1181 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1182 std::vector<MediaStreamInterface*> stream_list;
1183 stream_list.push_back(stream.get());
1184 scoped_refptr<AudioTrackInterface> audio_track(
1185 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001186 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1187 "video_track",
1188 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001189 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1190 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001191 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1192 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001193 EXPECT_EQ("audio_track", audio_sender->id());
1194 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001195 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1196 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001197 EXPECT_EQ("video_track", video_sender->id());
1198 EXPECT_EQ(video_track, video_sender->track());
1199
1200 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001201 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001202 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001203
1204 const cricket::ContentInfo* audio_content =
1205 cricket::GetFirstAudioContent(offer->description());
1206 const cricket::AudioContentDescription* audio_desc =
1207 static_cast<const cricket::AudioContentDescription*>(
1208 audio_content->description);
1209 EXPECT_TRUE(
1210 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1211
1212 const cricket::ContentInfo* video_content =
1213 cricket::GetFirstVideoContent(offer->description());
1214 const cricket::VideoContentDescription* video_desc =
1215 static_cast<const cricket::VideoContentDescription*>(
1216 video_content->description);
1217 EXPECT_TRUE(
1218 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1219
1220 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1221
1222 // Now try removing the tracks.
1223 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1224 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1225
1226 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001227 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001228
1229 audio_content = cricket::GetFirstAudioContent(offer->description());
1230 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1231 audio_content->description);
1232 EXPECT_FALSE(
1233 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1234
1235 video_content = cricket::GetFirstVideoContent(offer->description());
1236 video_desc = static_cast<const cricket::VideoContentDescription*>(
1237 video_content->description);
1238 EXPECT_FALSE(
1239 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1240
1241 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1242
1243 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1244 // should return false.
1245 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1246 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1247}
1248
1249// Test creating senders without a stream specified,
1250// expecting a random stream ID to be generated.
1251TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1252 CreatePeerConnection();
1253 // Create a dummy stream, so tracks share a stream label.
1254 scoped_refptr<AudioTrackInterface> audio_track(
1255 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001256 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1257 "video_track",
1258 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001259 auto audio_sender =
1260 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1261 auto video_sender =
1262 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1263 EXPECT_EQ("audio_track", audio_sender->id());
1264 EXPECT_EQ(audio_track, audio_sender->track());
1265 EXPECT_EQ("video_track", video_sender->id());
1266 EXPECT_EQ(video_track, video_sender->track());
1267 // If the ID is truly a random GUID, it should be infinitely unlikely they
1268 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001269 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001270}
1271
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1273 InitiateCall();
1274 WaitAndVerifyOnAddStream(kStreamLabel1);
1275 VerifyRemoteRtpHeaderExtensions();
1276}
1277
1278TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1279 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001280 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 CreateOfferAsLocalDescription();
1282 std::string offer;
1283 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1284 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1285 WaitAndVerifyOnAddStream(kStreamLabel1);
1286}
1287
1288TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1289 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001290 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291
1292 CreateOfferAsRemoteDescription();
1293 CreateAnswerAsLocalDescription();
1294
1295 WaitAndVerifyOnAddStream(kStreamLabel1);
1296}
1297
1298TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1299 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001300 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301
1302 CreateOfferAsRemoteDescription();
1303 CreatePrAnswerAsLocalDescription();
1304 CreateAnswerAsLocalDescription();
1305
1306 WaitAndVerifyOnAddStream(kStreamLabel1);
1307}
1308
1309TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1310 InitiateCall();
1311 ASSERT_EQ(1u, pc_->remote_streams()->count());
1312 pc_->RemoveStream(pc_->local_streams()->at(0));
1313 CreateOfferReceiveAnswer();
1314 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001315 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 CreateOfferReceiveAnswer();
1317}
1318
1319// Tests that after negotiating an audio only call, the respondent can perform a
1320// renegotiation that removes the audio stream.
1321TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1322 CreatePeerConnection();
1323 AddVoiceStream(kStreamLabel1);
1324 CreateOfferAsRemoteDescription();
1325 CreateAnswerAsLocalDescription();
1326
1327 ASSERT_EQ(1u, pc_->remote_streams()->count());
1328 pc_->RemoveStream(pc_->local_streams()->at(0));
1329 CreateOfferReceiveAnswer();
1330 EXPECT_EQ(0u, pc_->remote_streams()->count());
1331}
1332
1333// Test that candidates are generated and that we can parse our own candidates.
1334TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1335 CreatePeerConnection();
1336
1337 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1338 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001339 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001340 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001341 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001342 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343
1344 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001345 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001346 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001347 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
1349 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1350 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1351
1352 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1353}
1354
deadbeefab9b2d12015-10-14 11:33:11 -07001355// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356// not unique.
1357TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1358 CreatePeerConnection();
1359 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001360 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001361 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001362 EXPECT_TRUE(offer);
1363 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364
1365 // Create a local stream with audio&video tracks having same label.
1366 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1367
1368 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001369 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
1371 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001372 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001373 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374}
1375
1376// Test that we will get different SSRCs for each tracks in the offer and answer
1377// we created.
1378TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1379 CreatePeerConnection();
1380 // Create a local stream with audio&video tracks having different labels.
1381 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1382
1383 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001384 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001385 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 int audio_ssrc = 0;
1387 int video_ssrc = 0;
1388 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1389 &audio_ssrc));
1390 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1391 &video_ssrc));
1392 EXPECT_NE(audio_ssrc, video_ssrc);
1393
1394 // Test CreateAnswer
1395 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001396 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001397 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398 audio_ssrc = 0;
1399 video_ssrc = 0;
1400 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1401 &audio_ssrc));
1402 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1403 &video_ssrc));
1404 EXPECT_NE(audio_ssrc, video_ssrc);
1405}
1406
deadbeefeb459812015-12-15 19:24:43 -08001407// Test that it's possible to call AddTrack on a MediaStream after adding
1408// the stream to a PeerConnection.
1409// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1410TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1411 CreatePeerConnection();
1412 // Create audio stream and add to PeerConnection.
1413 AddVoiceStream(kStreamLabel1);
1414 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1415
1416 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001417 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1418 "video_label",
1419 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001420 stream->AddTrack(video_track.get());
1421
kwibergd1fe2812016-04-27 06:47:29 -07001422 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001423 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001424
1425 const cricket::MediaContentDescription* video_desc =
1426 cricket::GetFirstVideoContentDescription(offer->description());
1427 EXPECT_TRUE(video_desc != nullptr);
1428}
1429
1430// Test that it's possible to call RemoveTrack on a MediaStream after adding
1431// the stream to a PeerConnection.
1432// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1433TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1434 CreatePeerConnection();
1435 // Create audio/video stream and add to PeerConnection.
1436 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1437 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1438
1439 // Remove the video track.
1440 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1441
kwibergd1fe2812016-04-27 06:47:29 -07001442 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001443 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001444
1445 const cricket::MediaContentDescription* video_desc =
1446 cricket::GetFirstVideoContentDescription(offer->description());
1447 EXPECT_TRUE(video_desc == nullptr);
1448}
1449
deadbeefbd7d8f72015-12-18 16:58:44 -08001450// Test creating a sender with a stream ID, and ensure the ID is populated
1451// in the offer.
1452TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1453 CreatePeerConnection();
1454 pc_->CreateSender("video", kStreamLabel1);
1455
kwibergd1fe2812016-04-27 06:47:29 -07001456 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001457 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001458
1459 const cricket::MediaContentDescription* video_desc =
1460 cricket::GetFirstVideoContentDescription(offer->description());
1461 ASSERT_TRUE(video_desc != nullptr);
1462 ASSERT_EQ(1u, video_desc->streams().size());
1463 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1464}
1465
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466// Test that we can specify a certain track that we want statistics about.
1467TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1468 InitiateCall();
1469 ASSERT_LT(0u, pc_->remote_streams()->count());
1470 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1471 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1472 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1473 EXPECT_TRUE(DoGetStats(remote_audio));
1474
1475 // Remove the stream. Since we are sending to our selves the local
1476 // and the remote stream is the same.
1477 pc_->RemoveStream(pc_->local_streams()->at(0));
1478 // Do a re-negotiation.
1479 CreateOfferReceiveAnswer();
1480
1481 ASSERT_EQ(0u, pc_->remote_streams()->count());
1482
1483 // Test that we still can get statistics for the old track. Even if it is not
1484 // sent any longer.
1485 EXPECT_TRUE(DoGetStats(remote_audio));
1486}
1487
1488// Test that we can get stats on a video track.
1489TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1490 InitiateCall();
1491 ASSERT_LT(0u, pc_->remote_streams()->count());
1492 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1493 scoped_refptr<MediaStreamTrackInterface> remote_video =
1494 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1495 EXPECT_TRUE(DoGetStats(remote_video));
1496}
1497
1498// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001499// TODO(tommi): Fix this test. DoGetStats will return true
1500// for the unknown track (since GetStats is async), but no
1501// data is returned for the track.
1502TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 InitiateCall();
1504 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1505 pc_factory_->CreateAudioTrack("unknown track", NULL));
1506 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1507}
1508
1509// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 FakeConstraints constraints;
1512 constraints.SetAllowRtpDataChannels();
1513 CreatePeerConnection(&constraints);
1514 scoped_refptr<DataChannelInterface> data1 =
1515 pc_->CreateDataChannel("test1", NULL);
1516 scoped_refptr<DataChannelInterface> data2 =
1517 pc_->CreateDataChannel("test2", NULL);
1518 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001519 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001521 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 new MockDataChannelObserver(data2));
1523
1524 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1525 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1526 std::string data_to_send1 = "testing testing";
1527 std::string data_to_send2 = "testing something else";
1528 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1529
1530 CreateOfferReceiveAnswer();
1531 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1532 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1533
1534 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1535 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1536 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1537 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1538
1539 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1540 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1541
1542 data1->Close();
1543 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1544 CreateOfferReceiveAnswer();
1545 EXPECT_FALSE(observer1->IsOpen());
1546 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1547 EXPECT_TRUE(observer2->IsOpen());
1548
1549 data_to_send2 = "testing something else again";
1550 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1551
1552 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1553}
1554
1555// This test verifies that sendnig binary data over RTP data channels should
1556// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 FakeConstraints constraints;
1559 constraints.SetAllowRtpDataChannels();
1560 CreatePeerConnection(&constraints);
1561 scoped_refptr<DataChannelInterface> data1 =
1562 pc_->CreateDataChannel("test1", NULL);
1563 scoped_refptr<DataChannelInterface> data2 =
1564 pc_->CreateDataChannel("test2", NULL);
1565 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001566 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001568 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 new MockDataChannelObserver(data2));
1570
1571 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1572 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1573
1574 CreateOfferReceiveAnswer();
1575 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1576 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1577
1578 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1579 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1580
jbaucheec21bd2016-03-20 06:15:43 -07001581 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1583}
1584
1585// This test setup a RTP data channels in loop back and test that a channel is
1586// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 FakeConstraints constraints;
1589 constraints.SetAllowRtpDataChannels();
1590 CreatePeerConnection(&constraints);
1591 scoped_refptr<DataChannelInterface> data1 =
1592 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001593 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 new MockDataChannelObserver(data1));
1595
1596 CreateOfferReceiveAnswerWithoutSsrc();
1597
1598 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1599
1600 data1->Close();
1601 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1602 CreateOfferReceiveAnswerWithoutSsrc();
1603 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1604 EXPECT_FALSE(observer1->IsOpen());
1605}
1606
1607// This test that if a data channel is added in an answer a receive only channel
1608// channel is created.
1609TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1610 FakeConstraints constraints;
1611 constraints.SetAllowRtpDataChannels();
1612 CreatePeerConnection(&constraints);
1613
1614 std::string offer_label = "offer_channel";
1615 scoped_refptr<DataChannelInterface> offer_channel =
1616 pc_->CreateDataChannel(offer_label, NULL);
1617
1618 CreateOfferAsLocalDescription();
1619
1620 // Replace the data channel label in the offer and apply it as an answer.
1621 std::string receive_label = "answer_channel";
1622 std::string sdp;
1623 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001624 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 receive_label.c_str(), receive_label.length(),
1626 &sdp);
1627 CreateAnswerAsRemoteDescription(sdp);
1628
1629 // Verify that a new incoming data channel has been created and that
1630 // it is open but can't we written to.
1631 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1632 DataChannelInterface* received_channel = observer_.last_datachannel_;
1633 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1634 EXPECT_EQ(receive_label, received_channel->label());
1635 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1636
1637 // Verify that the channel we initially offered has been rejected.
1638 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1639
1640 // Do another offer / answer exchange and verify that the data channel is
1641 // opened.
1642 CreateOfferReceiveAnswer();
1643 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1644 kTimeout);
1645}
1646
1647// This test that no data channel is returned if a reliable channel is
1648// requested.
1649// TODO(perkj): Remove this test once reliable channels are implemented.
1650TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1651 FakeConstraints constraints;
1652 constraints.SetAllowRtpDataChannels();
1653 CreatePeerConnection(&constraints);
1654
1655 std::string label = "test";
1656 webrtc::DataChannelInit config;
1657 config.reliable = true;
1658 scoped_refptr<DataChannelInterface> channel =
1659 pc_->CreateDataChannel(label, &config);
1660 EXPECT_TRUE(channel == NULL);
1661}
1662
deadbeefab9b2d12015-10-14 11:33:11 -07001663// Verifies that duplicated label is not allowed for RTP data channel.
1664TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1665 FakeConstraints constraints;
1666 constraints.SetAllowRtpDataChannels();
1667 CreatePeerConnection(&constraints);
1668
1669 std::string label = "test";
1670 scoped_refptr<DataChannelInterface> channel =
1671 pc_->CreateDataChannel(label, nullptr);
1672 EXPECT_NE(channel, nullptr);
1673
1674 scoped_refptr<DataChannelInterface> dup_channel =
1675 pc_->CreateDataChannel(label, nullptr);
1676 EXPECT_EQ(dup_channel, nullptr);
1677}
1678
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679// This tests that a SCTP data channel is returned using different
1680// DataChannelInit configurations.
1681TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1682 FakeConstraints constraints;
1683 constraints.SetAllowDtlsSctpDataChannels();
1684 CreatePeerConnection(&constraints);
1685
1686 webrtc::DataChannelInit config;
1687
1688 scoped_refptr<DataChannelInterface> channel =
1689 pc_->CreateDataChannel("1", &config);
1690 EXPECT_TRUE(channel != NULL);
1691 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001692 EXPECT_TRUE(observer_.renegotiation_needed_);
1693 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694
1695 config.ordered = false;
1696 channel = pc_->CreateDataChannel("2", &config);
1697 EXPECT_TRUE(channel != NULL);
1698 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001699 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700
1701 config.ordered = true;
1702 config.maxRetransmits = 0;
1703 channel = pc_->CreateDataChannel("3", &config);
1704 EXPECT_TRUE(channel != NULL);
1705 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001706 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707
1708 config.maxRetransmits = -1;
1709 config.maxRetransmitTime = 0;
1710 channel = pc_->CreateDataChannel("4", &config);
1711 EXPECT_TRUE(channel != NULL);
1712 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001713 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714}
1715
1716// This tests that no data channel is returned if both maxRetransmits and
1717// maxRetransmitTime are set for SCTP data channels.
1718TEST_F(PeerConnectionInterfaceTest,
1719 CreateSctpDataChannelShouldFailForInvalidConfig) {
1720 FakeConstraints constraints;
1721 constraints.SetAllowDtlsSctpDataChannels();
1722 CreatePeerConnection(&constraints);
1723
1724 std::string label = "test";
1725 webrtc::DataChannelInit config;
1726 config.maxRetransmits = 0;
1727 config.maxRetransmitTime = 0;
1728
1729 scoped_refptr<DataChannelInterface> channel =
1730 pc_->CreateDataChannel(label, &config);
1731 EXPECT_TRUE(channel == NULL);
1732}
1733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734// The test verifies that creating a SCTP data channel with an id already in use
1735// or out of range should fail.
1736TEST_F(PeerConnectionInterfaceTest,
1737 CreateSctpDataChannelWithInvalidIdShouldFail) {
1738 FakeConstraints constraints;
1739 constraints.SetAllowDtlsSctpDataChannels();
1740 CreatePeerConnection(&constraints);
1741
1742 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001743 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001745 config.id = 1;
1746 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 EXPECT_TRUE(channel != NULL);
1748 EXPECT_EQ(1, channel->id());
1749
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 channel = pc_->CreateDataChannel("x", &config);
1751 EXPECT_TRUE(channel == NULL);
1752
1753 config.id = cricket::kMaxSctpSid;
1754 channel = pc_->CreateDataChannel("max", &config);
1755 EXPECT_TRUE(channel != NULL);
1756 EXPECT_EQ(config.id, channel->id());
1757
1758 config.id = cricket::kMaxSctpSid + 1;
1759 channel = pc_->CreateDataChannel("x", &config);
1760 EXPECT_TRUE(channel == NULL);
1761}
1762
deadbeefab9b2d12015-10-14 11:33:11 -07001763// Verifies that duplicated label is allowed for SCTP data channel.
1764TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1765 FakeConstraints constraints;
1766 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1767 true);
1768 CreatePeerConnection(&constraints);
1769
1770 std::string label = "test";
1771 scoped_refptr<DataChannelInterface> channel =
1772 pc_->CreateDataChannel(label, nullptr);
1773 EXPECT_NE(channel, nullptr);
1774
1775 scoped_refptr<DataChannelInterface> dup_channel =
1776 pc_->CreateDataChannel(label, nullptr);
1777 EXPECT_NE(dup_channel, nullptr);
1778}
1779
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001780// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1781// DataChannel.
1782TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1783 FakeConstraints constraints;
1784 constraints.SetAllowRtpDataChannels();
1785 CreatePeerConnection(&constraints);
1786
1787 scoped_refptr<DataChannelInterface> dc1 =
1788 pc_->CreateDataChannel("test1", NULL);
1789 EXPECT_TRUE(observer_.renegotiation_needed_);
1790 observer_.renegotiation_needed_ = false;
1791
1792 scoped_refptr<DataChannelInterface> dc2 =
1793 pc_->CreateDataChannel("test2", NULL);
1794 EXPECT_TRUE(observer_.renegotiation_needed_);
1795}
1796
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 FakeConstraints constraints;
1800 constraints.SetAllowRtpDataChannels();
1801 CreatePeerConnection(&constraints);
1802
1803 scoped_refptr<DataChannelInterface> data1 =
1804 pc_->CreateDataChannel("test1", NULL);
1805 scoped_refptr<DataChannelInterface> data2 =
1806 pc_->CreateDataChannel("test2", NULL);
1807 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001808 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001810 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 new MockDataChannelObserver(data2));
1812
1813 CreateOfferReceiveAnswer();
1814 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1815 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1816
1817 ReleasePeerConnection();
1818 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1819 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1820}
1821
1822// This test that data channels can be rejected in an answer.
1823TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1824 FakeConstraints constraints;
1825 constraints.SetAllowRtpDataChannels();
1826 CreatePeerConnection(&constraints);
1827
1828 scoped_refptr<DataChannelInterface> offer_channel(
1829 pc_->CreateDataChannel("offer_channel", NULL));
1830
1831 CreateOfferAsLocalDescription();
1832
1833 // Create an answer where the m-line for data channels are rejected.
1834 std::string sdp;
1835 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1836 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1837 SessionDescriptionInterface::kAnswer);
1838 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1839 cricket::ContentInfo* data_info =
1840 answer->description()->GetContentByName("data");
1841 data_info->rejected = true;
1842
1843 DoSetRemoteDescription(answer);
1844 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1845}
1846
1847// Test that we can create a session description from an SDP string from
1848// FireFox, use it as a remote session description, generate an answer and use
1849// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001850TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001851 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 FakeConstraints constraints;
1853 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1854 true);
1855 CreatePeerConnection(&constraints);
1856 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1857 SessionDescriptionInterface* desc =
1858 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001859 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1861 CreateAnswerAsLocalDescription();
1862 ASSERT_TRUE(pc_->local_description() != NULL);
1863 ASSERT_TRUE(pc_->remote_description() != NULL);
1864
1865 const cricket::ContentInfo* content =
1866 cricket::GetFirstAudioContent(pc_->local_description()->description());
1867 ASSERT_TRUE(content != NULL);
1868 EXPECT_FALSE(content->rejected);
1869
1870 content =
1871 cricket::GetFirstVideoContent(pc_->local_description()->description());
1872 ASSERT_TRUE(content != NULL);
1873 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001874#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 content =
1876 cricket::GetFirstDataContent(pc_->local_description()->description());
1877 ASSERT_TRUE(content != NULL);
1878 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001879#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880}
1881
1882// Test that we can create an audio only offer and receive an answer with a
1883// limited set of audio codecs and receive an updated offer with more audio
1884// codecs, where the added codecs are not supported.
1885TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1886 CreatePeerConnection();
1887 AddVoiceStream("audio_label");
1888 CreateOfferAsLocalDescription();
1889
1890 SessionDescriptionInterface* answer =
1891 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001892 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1894
1895 SessionDescriptionInterface* updated_offer =
1896 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001897 webrtc::kAudioSdpWithUnsupportedCodecs,
1898 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1900 CreateAnswerAsLocalDescription();
1901}
1902
deadbeefc80741f2015-10-22 13:14:45 -07001903// Test that if we're receiving (but not sending) a track, subsequent offers
1904// will have m-lines with a=recvonly.
1905TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1906 FakeConstraints constraints;
1907 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1908 true);
1909 CreatePeerConnection(&constraints);
1910 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1911 CreateAnswerAsLocalDescription();
1912
1913 // At this point we should be receiving stream 1, but not sending anything.
1914 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001915 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001916 DoCreateOffer(&offer, nullptr);
1917
1918 const cricket::ContentInfo* video_content =
1919 cricket::GetFirstVideoContent(offer->description());
1920 const cricket::VideoContentDescription* video_desc =
1921 static_cast<const cricket::VideoContentDescription*>(
1922 video_content->description);
1923 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1924
1925 const cricket::ContentInfo* audio_content =
1926 cricket::GetFirstAudioContent(offer->description());
1927 const cricket::AudioContentDescription* audio_desc =
1928 static_cast<const cricket::AudioContentDescription*>(
1929 audio_content->description);
1930 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1931}
1932
1933// Test that if we're receiving (but not sending) a track, and the
1934// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1935// false, the generated m-lines will be a=inactive.
1936TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1937 FakeConstraints constraints;
1938 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1939 true);
1940 CreatePeerConnection(&constraints);
1941 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1942 CreateAnswerAsLocalDescription();
1943
1944 // At this point we should be receiving stream 1, but not sending anything.
1945 // A new offer would be recvonly, but we'll set the "no receive" constraints
1946 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001947 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001948 FakeConstraints offer_constraints;
1949 offer_constraints.AddMandatory(
1950 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1951 offer_constraints.AddMandatory(
1952 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1953 DoCreateOffer(&offer, &offer_constraints);
1954
1955 const cricket::ContentInfo* video_content =
1956 cricket::GetFirstVideoContent(offer->description());
1957 const cricket::VideoContentDescription* video_desc =
1958 static_cast<const cricket::VideoContentDescription*>(
1959 video_content->description);
1960 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1961
1962 const cricket::ContentInfo* audio_content =
1963 cricket::GetFirstAudioContent(offer->description());
1964 const cricket::AudioContentDescription* audio_desc =
1965 static_cast<const cricket::AudioContentDescription*>(
1966 audio_content->description);
1967 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1968}
1969
deadbeef653b8e02015-11-11 12:55:10 -08001970// Test that we can use SetConfiguration to change the ICE servers of the
1971// PortAllocator.
1972TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1973 CreatePeerConnection();
1974
1975 PeerConnectionInterface::RTCConfiguration config;
1976 PeerConnectionInterface::IceServer server;
1977 server.uri = "stun:test_hostname";
1978 config.servers.push_back(server);
1979 EXPECT_TRUE(pc_->SetConfiguration(config));
1980
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001981 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1982 EXPECT_EQ("test_hostname",
1983 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001984}
1985
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001986TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1987 CreatePeerConnection();
1988 PeerConnectionInterface::RTCConfiguration config;
1989 config.type = PeerConnectionInterface::kRelay;
1990 EXPECT_TRUE(pc_->SetConfiguration(config));
1991 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1992}
1993
1994// Test that when SetConfiguration changes both the pool size and other
1995// attributes, the pooled session is created with the updated attributes.
1996TEST_F(PeerConnectionInterfaceTest,
1997 SetConfigurationCreatesPooledSessionCorrectly) {
1998 CreatePeerConnection();
1999 PeerConnectionInterface::RTCConfiguration config;
2000 config.ice_candidate_pool_size = 1;
2001 PeerConnectionInterface::IceServer server;
2002 server.uri = kStunAddressOnly;
2003 config.servers.push_back(server);
2004 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002005 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002006
2007 const cricket::FakePortAllocatorSession* session =
2008 static_cast<const cricket::FakePortAllocatorSession*>(
2009 port_allocator_->GetPooledSession());
2010 ASSERT_NE(nullptr, session);
2011 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002012}
2013
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014// Test that PeerConnection::Close changes the states to closed and all remote
2015// tracks change state to ended.
2016TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2017 // Initialize a PeerConnection and negotiate local and remote session
2018 // description.
2019 InitiateCall();
2020 ASSERT_EQ(1u, pc_->local_streams()->count());
2021 ASSERT_EQ(1u, pc_->remote_streams()->count());
2022
2023 pc_->Close();
2024
2025 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2026 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2027 pc_->ice_connection_state());
2028 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2029 pc_->ice_gathering_state());
2030
2031 EXPECT_EQ(1u, pc_->local_streams()->count());
2032 EXPECT_EQ(1u, pc_->remote_streams()->count());
2033
2034 scoped_refptr<MediaStreamInterface> remote_stream =
2035 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002036 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002037 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002038 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2039 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2040 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041}
2042
2043// Test that PeerConnection methods fails gracefully after
2044// PeerConnection::Close has been called.
2045TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2046 CreatePeerConnection();
2047 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2048 CreateOfferAsRemoteDescription();
2049 CreateAnswerAsLocalDescription();
2050
2051 ASSERT_EQ(1u, pc_->local_streams()->count());
2052 scoped_refptr<MediaStreamInterface> local_stream =
2053 pc_->local_streams()->at(0);
2054
2055 pc_->Close();
2056
2057 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002058 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059
2060 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002061 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002063 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064
2065 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2066
2067 EXPECT_TRUE(pc_->local_description() != NULL);
2068 EXPECT_TRUE(pc_->remote_description() != NULL);
2069
kwibergd1fe2812016-04-27 06:47:29 -07002070 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002071 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002072 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002073 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074
2075 std::string sdp;
2076 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2077 SessionDescriptionInterface* remote_offer =
2078 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2079 sdp, NULL);
2080 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2081
2082 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2083 SessionDescriptionInterface* local_offer =
2084 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2085 sdp, NULL);
2086 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2087}
2088
2089// Test that GetStats can still be called after PeerConnection::Close.
2090TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2091 InitiateCall();
2092 pc_->Close();
2093 DoGetStats(NULL);
2094}
deadbeefab9b2d12015-10-14 11:33:11 -07002095
2096// NOTE: The series of tests below come from what used to be
2097// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2098// setting a remote or local description has the expected effects.
2099
2100// This test verifies that the remote MediaStreams corresponding to a received
2101// SDP string is created. In this test the two separate MediaStreams are
2102// signaled.
2103TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2104 FakeConstraints constraints;
2105 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2106 true);
2107 CreatePeerConnection(&constraints);
2108 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2109
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002110 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002111 EXPECT_TRUE(
2112 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2113 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2114 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2115
2116 // Create a session description based on another SDP with another
2117 // MediaStream.
2118 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2119
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002120 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002121 EXPECT_TRUE(
2122 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2123}
2124
2125// This test verifies that when remote tracks are added/removed from SDP, the
2126// created remote streams are updated appropriately.
2127TEST_F(PeerConnectionInterfaceTest,
2128 AddRemoveTrackFromExistingRemoteMediaStream) {
2129 FakeConstraints constraints;
2130 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2131 true);
2132 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002133 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002134 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002135 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2136 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2137 reference_collection_));
2138
2139 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002140 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002141 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002142 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2143 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2144 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002145 scoped_refptr<AudioTrackInterface> audio_track2 =
2146 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2147 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2148 scoped_refptr<VideoTrackInterface> video_track2 =
2149 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2150 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002151
2152 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002153 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002154 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002155 MockTrackObserver audio_track_observer(audio_track2);
2156 MockTrackObserver video_track_observer(video_track2);
2157
2158 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2159 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002160 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2161 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2162 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002163 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002164 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002165 audio_track2->state(), kTimeout);
2166 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2167 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002168}
2169
2170// This tests that remote tracks are ended if a local session description is set
2171// that rejects the media content type.
2172TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2173 FakeConstraints constraints;
2174 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2175 true);
2176 CreatePeerConnection(&constraints);
2177 // First create and set a remote offer, then reject its video content in our
2178 // answer.
2179 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2180 ASSERT_EQ(1u, observer_.remote_streams()->count());
2181 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2182 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2183 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2184
2185 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2186 remote_stream->GetVideoTracks()[0];
2187 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2188 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2189 remote_stream->GetAudioTracks()[0];
2190 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2191
kwibergd1fe2812016-04-27 06:47:29 -07002192 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002193 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002194 cricket::ContentInfo* video_info =
2195 local_answer->description()->GetContentByName("video");
2196 video_info->rejected = true;
2197 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2198 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2199 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2200
2201 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002202 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002203 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002204 video_info = local_offer->description()->GetContentByName("video");
2205 ASSERT_TRUE(video_info != nullptr);
2206 video_info->rejected = true;
2207 cricket::ContentInfo* audio_info =
2208 local_offer->description()->GetContentByName("audio");
2209 ASSERT_TRUE(audio_info != nullptr);
2210 audio_info->rejected = true;
2211 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002212 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002213 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002214 remote_audio->state(), kTimeout);
2215 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2216 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002217}
2218
2219// This tests that we won't crash if the remote track has been removed outside
2220// of PeerConnection and then PeerConnection tries to reject the track.
2221TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2222 FakeConstraints constraints;
2223 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2224 true);
2225 CreatePeerConnection(&constraints);
2226 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2227 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2228 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2229 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2230
kwibergd1fe2812016-04-27 06:47:29 -07002231 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002232 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2233 kSdpStringWithStream1, nullptr));
2234 cricket::ContentInfo* video_info =
2235 local_answer->description()->GetContentByName("video");
2236 video_info->rejected = true;
2237 cricket::ContentInfo* audio_info =
2238 local_answer->description()->GetContentByName("audio");
2239 audio_info->rejected = true;
2240 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2241
2242 // No crash is a pass.
2243}
2244
deadbeef5e97fb52015-10-15 12:49:08 -07002245// This tests that if a recvonly remote description is set, no remote streams
2246// will be created, even if the description contains SSRCs/MSIDs.
2247// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2248TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2249 FakeConstraints constraints;
2250 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2251 true);
2252 CreatePeerConnection(&constraints);
2253
2254 std::string recvonly_offer = kSdpStringWithStream1;
2255 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2256 strlen(kRecvonly), &recvonly_offer);
2257 CreateAndSetRemoteOffer(recvonly_offer);
2258
2259 EXPECT_EQ(0u, observer_.remote_streams()->count());
2260}
2261
deadbeefab9b2d12015-10-14 11:33:11 -07002262// This tests that a default MediaStream is created if a remote session
2263// description doesn't contain any streams and no MSID support.
2264// It also tests that the default stream is updated if a video m-line is added
2265// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002266TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002267 FakeConstraints constraints;
2268 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2269 true);
2270 CreatePeerConnection(&constraints);
2271 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2272
2273 ASSERT_EQ(1u, observer_.remote_streams()->count());
2274 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2275
2276 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2277 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2278 EXPECT_EQ("default", remote_stream->label());
2279
2280 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2281 ASSERT_EQ(1u, observer_.remote_streams()->count());
2282 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2283 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002284 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2285 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002286 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2287 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002288 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2289 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002290}
2291
2292// This tests that a default MediaStream is created if a remote session
2293// description doesn't contain any streams and media direction is send only.
2294TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002295 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002296 FakeConstraints constraints;
2297 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2298 true);
2299 CreatePeerConnection(&constraints);
2300 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2301
2302 ASSERT_EQ(1u, observer_.remote_streams()->count());
2303 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2304
2305 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2306 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2307 EXPECT_EQ("default", remote_stream->label());
2308}
2309
2310// This tests that it won't crash when PeerConnection tries to remove
2311// a remote track that as already been removed from the MediaStream.
2312TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2313 FakeConstraints constraints;
2314 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2315 true);
2316 CreatePeerConnection(&constraints);
2317 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2318 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2319 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2320 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2321
2322 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2323
2324 // No crash is a pass.
2325}
2326
2327// This tests that a default MediaStream is created if the remote session
2328// description doesn't contain any streams and don't contain an indication if
2329// MSID is supported.
2330TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002331 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002332 FakeConstraints constraints;
2333 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2334 true);
2335 CreatePeerConnection(&constraints);
2336 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2337
2338 ASSERT_EQ(1u, observer_.remote_streams()->count());
2339 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2340 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2341 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2342}
2343
2344// This tests that a default MediaStream is not created if the remote session
2345// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002346TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002347 FakeConstraints constraints;
2348 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2349 true);
2350 CreatePeerConnection(&constraints);
2351 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2352 EXPECT_EQ(0u, observer_.remote_streams()->count());
2353}
2354
deadbeefbda7e0b2015-12-08 17:13:40 -08002355// This tests that when setting a new description, the old default tracks are
2356// not destroyed and recreated.
2357// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002358TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002359 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002360 FakeConstraints constraints;
2361 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2362 true);
2363 CreatePeerConnection(&constraints);
2364 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2365
2366 ASSERT_EQ(1u, observer_.remote_streams()->count());
2367 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2368 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2369
2370 // Set the track to "disabled", then set a new description and ensure the
2371 // track is still disabled, which ensures it hasn't been recreated.
2372 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2373 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2374 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2375 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2376}
2377
deadbeefab9b2d12015-10-14 11:33:11 -07002378// This tests that a default MediaStream is not created if a remote session
2379// description is updated to not have any MediaStreams.
2380TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2381 FakeConstraints constraints;
2382 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2383 true);
2384 CreatePeerConnection(&constraints);
2385 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002386 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002387 EXPECT_TRUE(
2388 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2389
2390 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2391 EXPECT_EQ(0u, observer_.remote_streams()->count());
2392}
2393
2394// This tests that an RtpSender is created when the local description is set
2395// after adding a local stream.
2396// TODO(deadbeef): This test and the one below it need to be updated when
2397// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002398TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002399 FakeConstraints constraints;
2400 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2401 true);
2402 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002403
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002404 // Create an offer with 1 stream with 2 tracks of each type.
2405 rtc::scoped_refptr<StreamCollection> stream_collection =
2406 CreateStreamCollection(1, 2);
2407 pc_->AddStream(stream_collection->at(0));
2408 std::unique_ptr<SessionDescriptionInterface> offer;
2409 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2410 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002411
deadbeefab9b2d12015-10-14 11:33:11 -07002412 auto senders = pc_->GetSenders();
2413 EXPECT_EQ(4u, senders.size());
2414 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2415 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2416 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2417 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2418
2419 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002420 pc_->RemoveStream(stream_collection->at(0));
2421 stream_collection = CreateStreamCollection(1, 1);
2422 pc_->AddStream(stream_collection->at(0));
2423 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2424 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2425
deadbeefab9b2d12015-10-14 11:33:11 -07002426 senders = pc_->GetSenders();
2427 EXPECT_EQ(2u, senders.size());
2428 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2429 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2430 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2431 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2432}
2433
2434// This tests that an RtpSender is created when the local description is set
2435// before adding a local stream.
2436TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002437 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002438 FakeConstraints constraints;
2439 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2440 true);
2441 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002442
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002443 rtc::scoped_refptr<StreamCollection> stream_collection =
2444 CreateStreamCollection(1, 2);
2445 // Add a stream to create the offer, but remove it afterwards.
2446 pc_->AddStream(stream_collection->at(0));
2447 std::unique_ptr<SessionDescriptionInterface> offer;
2448 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2449 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002450
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002451 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002452 auto senders = pc_->GetSenders();
2453 EXPECT_EQ(0u, senders.size());
2454
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002455 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002456 senders = pc_->GetSenders();
2457 EXPECT_EQ(4u, senders.size());
2458 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2459 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2460 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2461 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2462}
2463
2464// This tests that the expected behavior occurs if the SSRC on a local track is
2465// changed when SetLocalDescription is called.
2466TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002467 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002468 FakeConstraints constraints;
2469 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2470 true);
2471 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002472
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002473 rtc::scoped_refptr<StreamCollection> stream_collection =
2474 CreateStreamCollection(2, 1);
2475 pc_->AddStream(stream_collection->at(0));
2476 std::unique_ptr<SessionDescriptionInterface> offer;
2477 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2478 // Grab a copy of the offer before it gets passed into the PC.
2479 std::unique_ptr<JsepSessionDescription> modified_offer(
2480 new JsepSessionDescription(JsepSessionDescription::kOffer));
2481 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2482 offer->session_version());
2483 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002484
deadbeefab9b2d12015-10-14 11:33:11 -07002485 auto senders = pc_->GetSenders();
2486 EXPECT_EQ(2u, senders.size());
2487 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2488 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2489
2490 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002491 cricket::MediaContentDescription* desc =
2492 cricket::GetFirstAudioContentDescription(modified_offer->description());
2493 ASSERT_TRUE(desc != NULL);
2494 for (StreamParams& stream : desc->mutable_streams()) {
2495 for (unsigned int& ssrc : stream.ssrcs) {
2496 ++ssrc;
2497 }
2498 }
deadbeefab9b2d12015-10-14 11:33:11 -07002499
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002500 desc =
2501 cricket::GetFirstVideoContentDescription(modified_offer->description());
2502 ASSERT_TRUE(desc != NULL);
2503 for (StreamParams& stream : desc->mutable_streams()) {
2504 for (unsigned int& ssrc : stream.ssrcs) {
2505 ++ssrc;
2506 }
2507 }
2508
2509 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002510 senders = pc_->GetSenders();
2511 EXPECT_EQ(2u, senders.size());
2512 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2513 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2514 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2515 // changed.
2516}
2517
2518// This tests that the expected behavior occurs if a new session description is
2519// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002520TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002521 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002522 FakeConstraints constraints;
2523 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2524 true);
2525 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002526
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002527 rtc::scoped_refptr<StreamCollection> stream_collection =
2528 CreateStreamCollection(2, 1);
2529 pc_->AddStream(stream_collection->at(0));
2530 std::unique_ptr<SessionDescriptionInterface> offer;
2531 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2532 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002533
deadbeefab9b2d12015-10-14 11:33:11 -07002534 auto senders = pc_->GetSenders();
2535 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002536 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2537 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002538
2539 // Add a new MediaStream but with the same tracks as in the first stream.
2540 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2541 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002542 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2543 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002544 pc_->AddStream(stream_1);
2545
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002546 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2547 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002548
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002549 auto new_senders = pc_->GetSenders();
2550 // Should be the same senders as before, but with updated stream id.
2551 // Note that this behavior is subject to change in the future.
2552 // We may decide the PC should ignore existing tracks in AddStream.
2553 EXPECT_EQ(senders, new_senders);
2554 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2555 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002556}
2557
nisse51542be2016-02-12 02:27:06 -08002558// The PeerConnectionMediaConfig tests below verify that configuration
2559// and constraints are propagated into the MediaConfig passed to
2560// CreateMediaController. These settings are intended for MediaChannel
2561// constructors, but that is not exercised by these unittest.
2562class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2563 public:
2564 webrtc::MediaControllerInterface* CreateMediaController(
2565 const cricket::MediaConfig& config) const override {
2566 create_media_controller_called_ = true;
2567 create_media_controller_config_ = config;
2568
2569 webrtc::MediaControllerInterface* mc =
2570 PeerConnectionFactory::CreateMediaController(config);
2571 EXPECT_TRUE(mc != nullptr);
2572 return mc;
2573 }
2574
2575 // Mutable, so they can be modified in the above const-declared method.
2576 mutable bool create_media_controller_called_ = false;
2577 mutable cricket::MediaConfig create_media_controller_config_;
2578};
2579
2580class PeerConnectionMediaConfigTest : public testing::Test {
2581 protected:
2582 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002583 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002584 pcf_->Initialize();
2585 }
2586 const cricket::MediaConfig& TestCreatePeerConnection(
2587 const PeerConnectionInterface::RTCConfiguration& config,
2588 const MediaConstraintsInterface *constraints) {
2589 pcf_->create_media_controller_called_ = false;
2590
2591 scoped_refptr<PeerConnectionInterface> pc(
2592 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2593 &observer_));
2594 EXPECT_TRUE(pc.get());
2595 EXPECT_TRUE(pcf_->create_media_controller_called_);
2596 return pcf_->create_media_controller_config_;
2597 }
2598
2599 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2600 MockPeerConnectionObserver observer_;
2601};
2602
2603// This test verifies the default behaviour with no constraints and a
2604// default RTCConfiguration.
2605TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2606 PeerConnectionInterface::RTCConfiguration config;
2607 FakeConstraints constraints;
2608
2609 const cricket::MediaConfig& media_config =
2610 TestCreatePeerConnection(config, &constraints);
2611
2612 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002613 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2614 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2615 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002616}
2617
2618// This test verifies the DSCP constraint is recognized and passed to
2619// the CreateMediaController call.
2620TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2621 PeerConnectionInterface::RTCConfiguration config;
2622 FakeConstraints constraints;
2623
2624 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2625 const cricket::MediaConfig& media_config =
2626 TestCreatePeerConnection(config, &constraints);
2627
2628 EXPECT_TRUE(media_config.enable_dscp);
2629}
2630
2631// This test verifies the cpu overuse detection constraint is
2632// recognized and passed to the CreateMediaController call.
2633TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2634 PeerConnectionInterface::RTCConfiguration config;
2635 FakeConstraints constraints;
2636
2637 constraints.AddOptional(
2638 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2639 const cricket::MediaConfig media_config =
2640 TestCreatePeerConnection(config, &constraints);
2641
nisse0db023a2016-03-01 04:29:59 -08002642 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002643}
2644
2645// This test verifies that the disable_prerenderer_smoothing flag is
2646// propagated from RTCConfiguration to the CreateMediaController call.
2647TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2648 PeerConnectionInterface::RTCConfiguration config;
2649 FakeConstraints constraints;
2650
Niels Möller71bdda02016-03-31 12:59:59 +02002651 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002652 const cricket::MediaConfig& media_config =
2653 TestCreatePeerConnection(config, &constraints);
2654
nisse0db023a2016-03-01 04:29:59 -08002655 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2656}
2657
2658// This test verifies the suspend below min bitrate constraint is
2659// recognized and passed to the CreateMediaController call.
2660TEST_F(PeerConnectionMediaConfigTest,
2661 TestSuspendBelowMinBitrateConstraintTrue) {
2662 PeerConnectionInterface::RTCConfiguration config;
2663 FakeConstraints constraints;
2664
2665 constraints.AddOptional(
2666 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2667 true);
2668 const cricket::MediaConfig media_config =
2669 TestCreatePeerConnection(config, &constraints);
2670
2671 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002672}
2673
deadbeefab9b2d12015-10-14 11:33:11 -07002674// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002675// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2676// "verify options are converted correctly", should be "pass options into
2677// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002678
2679TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2680 RTCOfferAnswerOptions rtc_options;
2681 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2682
2683 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002684 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002685
2686 rtc_options.offer_to_receive_audio =
2687 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002688 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002689}
2690
2691TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2692 RTCOfferAnswerOptions rtc_options;
2693 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2694
2695 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002696 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002697
2698 rtc_options.offer_to_receive_video =
2699 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002700 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002701}
2702
2703// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002704// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002705TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2706 RTCOfferAnswerOptions rtc_options;
2707 rtc_options.offer_to_receive_audio = 1;
2708 rtc_options.offer_to_receive_video = 1;
2709
2710 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002711 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002712 EXPECT_TRUE(options.has_audio());
2713 EXPECT_TRUE(options.has_video());
2714 EXPECT_TRUE(options.bundle_enabled);
2715}
2716
2717// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002718// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002719TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2720 RTCOfferAnswerOptions rtc_options;
2721 rtc_options.offer_to_receive_audio = 1;
2722
2723 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002724 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002725 EXPECT_TRUE(options.has_audio());
2726 EXPECT_FALSE(options.has_video());
2727 EXPECT_TRUE(options.bundle_enabled);
2728}
2729
2730// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002731// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002732TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2733 RTCOfferAnswerOptions rtc_options;
2734
2735 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002736 options.transport_options["audio"] = cricket::TransportOptions();
2737 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002738 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002739 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002740 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002741 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002742 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002743 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2744 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002745}
2746
2747// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002748// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002749TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2750 RTCOfferAnswerOptions rtc_options;
2751 rtc_options.offer_to_receive_audio = 0;
2752 rtc_options.offer_to_receive_video = 1;
2753
2754 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002755 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002756 EXPECT_FALSE(options.has_audio());
2757 EXPECT_TRUE(options.has_video());
2758 EXPECT_TRUE(options.bundle_enabled);
2759}
2760
2761// Test that a correct MediaSessionOptions is created for an offer if
2762// UseRtpMux is set to false.
2763TEST(CreateSessionOptionsTest,
2764 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2765 RTCOfferAnswerOptions rtc_options;
2766 rtc_options.offer_to_receive_audio = 1;
2767 rtc_options.offer_to_receive_video = 1;
2768 rtc_options.use_rtp_mux = false;
2769
2770 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002771 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002772 EXPECT_TRUE(options.has_audio());
2773 EXPECT_TRUE(options.has_video());
2774 EXPECT_FALSE(options.bundle_enabled);
2775}
2776
2777// Test that a correct MediaSessionOptions is created to restart ice if
2778// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002779// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002780TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2781 RTCOfferAnswerOptions rtc_options;
2782 rtc_options.ice_restart = true;
2783
2784 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002785 options.transport_options["audio"] = cricket::TransportOptions();
2786 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002787 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002788 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2789 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002790
2791 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002792 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002793 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2794 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002795}
2796
2797// Test that the MediaConstraints in an answer don't affect if audio and video
2798// is offered in an offer but that if kOfferToReceiveAudio or
2799// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2800// included in subsequent answers.
2801TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2802 FakeConstraints answer_c;
2803 answer_c.SetMandatoryReceiveAudio(true);
2804 answer_c.SetMandatoryReceiveVideo(true);
2805
2806 cricket::MediaSessionOptions answer_options;
2807 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2808 EXPECT_TRUE(answer_options.has_audio());
2809 EXPECT_TRUE(answer_options.has_video());
2810
deadbeefc80741f2015-10-22 13:14:45 -07002811 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002812
2813 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002814 EXPECT_TRUE(
2815 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002816 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002817 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002818
deadbeefc80741f2015-10-22 13:14:45 -07002819 RTCOfferAnswerOptions updated_rtc_offer_options;
2820 updated_rtc_offer_options.offer_to_receive_audio = 1;
2821 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002822
2823 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002824 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002825 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002826 EXPECT_TRUE(updated_offer_options.has_audio());
2827 EXPECT_TRUE(updated_offer_options.has_video());
2828
2829 // Since an offer has been created with both audio and video, subsequent
2830 // offers and answers should contain both audio and video.
2831 // Answers will only contain the media types that exist in the offer
2832 // regardless of the value of |updated_answer_options.has_audio| and
2833 // |updated_answer_options.has_video|.
2834 FakeConstraints updated_answer_c;
2835 answer_c.SetMandatoryReceiveAudio(false);
2836 answer_c.SetMandatoryReceiveVideo(false);
2837
2838 cricket::MediaSessionOptions updated_answer_options;
2839 EXPECT_TRUE(
2840 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2841 EXPECT_TRUE(updated_answer_options.has_audio());
2842 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002843}