blob: 71233e06081fc5d703712aaf3b6b2bcdb3be61c9 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1070 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001071 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 AddVoiceStream(kStreamLabel2);
1073 ASSERT_EQ(2u, pc_->local_streams()->count());
1074
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001075 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 scoped_refptr<MediaStreamInterface> stream(
1077 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1078 scoped_refptr<AudioTrackInterface> audio_track(
1079 pc_factory_->CreateAudioTrack(
1080 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1081 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001082 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001083 EXPECT_EQ(3u, pc_->local_streams()->count());
1084
1085 // Remove the third stream.
1086 pc_->RemoveStream(pc_->local_streams()->at(2));
1087 EXPECT_EQ(2u, pc_->local_streams()->count());
1088
1089 // Remove the second stream.
1090 pc_->RemoveStream(pc_->local_streams()->at(1));
1091 EXPECT_EQ(1u, pc_->local_streams()->count());
1092
1093 // Remove the first stream.
1094 pc_->RemoveStream(pc_->local_streams()->at(0));
1095 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096}
1097
deadbeefab9b2d12015-10-14 11:33:11 -07001098// Test that the created offer includes streams we added.
1099TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1100 CreatePeerConnection();
1101 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001102 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001103 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001104
1105 const cricket::ContentInfo* audio_content =
1106 cricket::GetFirstAudioContent(offer->description());
1107 const cricket::AudioContentDescription* audio_desc =
1108 static_cast<const cricket::AudioContentDescription*>(
1109 audio_content->description);
1110 EXPECT_TRUE(
1111 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1112
1113 const cricket::ContentInfo* video_content =
1114 cricket::GetFirstVideoContent(offer->description());
1115 const cricket::VideoContentDescription* video_desc =
1116 static_cast<const cricket::VideoContentDescription*>(
1117 video_content->description);
1118 EXPECT_TRUE(
1119 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1120
1121 // Add another stream and ensure the offer includes both the old and new
1122 // streams.
1123 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001124 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001125
1126 audio_content = cricket::GetFirstAudioContent(offer->description());
1127 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1128 audio_content->description);
1129 EXPECT_TRUE(
1130 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1131 EXPECT_TRUE(
1132 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1133
1134 video_content = cricket::GetFirstVideoContent(offer->description());
1135 video_desc = static_cast<const cricket::VideoContentDescription*>(
1136 video_content->description);
1137 EXPECT_TRUE(
1138 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1139 EXPECT_TRUE(
1140 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1141}
1142
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1144 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001145 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 ASSERT_EQ(1u, pc_->local_streams()->count());
1147 pc_->RemoveStream(pc_->local_streams()->at(0));
1148 EXPECT_EQ(0u, pc_->local_streams()->count());
1149}
1150
deadbeefe1f9d832016-01-14 15:35:42 -08001151// Test for AddTrack and RemoveTrack methods.
1152// Tests that the created offer includes tracks we added,
1153// and that the RtpSenders are created correctly.
1154// Also tests that RemoveTrack removes the tracks from subsequent offers.
1155TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1156 CreatePeerConnection();
1157 // Create a dummy stream, so tracks share a stream label.
1158 scoped_refptr<MediaStreamInterface> stream(
1159 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1160 std::vector<MediaStreamInterface*> stream_list;
1161 stream_list.push_back(stream.get());
1162 scoped_refptr<AudioTrackInterface> audio_track(
1163 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001164 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1165 "video_track",
1166 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001167 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1168 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001169 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1170 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001171 EXPECT_EQ("audio_track", audio_sender->id());
1172 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001173 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1174 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001175 EXPECT_EQ("video_track", video_sender->id());
1176 EXPECT_EQ(video_track, video_sender->track());
1177
1178 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001179 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001180 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001181
1182 const cricket::ContentInfo* audio_content =
1183 cricket::GetFirstAudioContent(offer->description());
1184 const cricket::AudioContentDescription* audio_desc =
1185 static_cast<const cricket::AudioContentDescription*>(
1186 audio_content->description);
1187 EXPECT_TRUE(
1188 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1189
1190 const cricket::ContentInfo* video_content =
1191 cricket::GetFirstVideoContent(offer->description());
1192 const cricket::VideoContentDescription* video_desc =
1193 static_cast<const cricket::VideoContentDescription*>(
1194 video_content->description);
1195 EXPECT_TRUE(
1196 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1197
1198 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1199
1200 // Now try removing the tracks.
1201 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1202 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1203
1204 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001205 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001206
1207 audio_content = cricket::GetFirstAudioContent(offer->description());
1208 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1209 audio_content->description);
1210 EXPECT_FALSE(
1211 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1212
1213 video_content = cricket::GetFirstVideoContent(offer->description());
1214 video_desc = static_cast<const cricket::VideoContentDescription*>(
1215 video_content->description);
1216 EXPECT_FALSE(
1217 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1218
1219 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1220
1221 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1222 // should return false.
1223 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1224 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1225}
1226
1227// Test creating senders without a stream specified,
1228// expecting a random stream ID to be generated.
1229TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1230 CreatePeerConnection();
1231 // Create a dummy stream, so tracks share a stream label.
1232 scoped_refptr<AudioTrackInterface> audio_track(
1233 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001234 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1235 "video_track",
1236 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001237 auto audio_sender =
1238 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1239 auto video_sender =
1240 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1241 EXPECT_EQ("audio_track", audio_sender->id());
1242 EXPECT_EQ(audio_track, audio_sender->track());
1243 EXPECT_EQ("video_track", video_sender->id());
1244 EXPECT_EQ(video_track, video_sender->track());
1245 // If the ID is truly a random GUID, it should be infinitely unlikely they
1246 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001247 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001248}
1249
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1251 InitiateCall();
1252 WaitAndVerifyOnAddStream(kStreamLabel1);
1253 VerifyRemoteRtpHeaderExtensions();
1254}
1255
1256TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1257 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001258 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 CreateOfferAsLocalDescription();
1260 std::string offer;
1261 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1262 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1263 WaitAndVerifyOnAddStream(kStreamLabel1);
1264}
1265
1266TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1267 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001268 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269
1270 CreateOfferAsRemoteDescription();
1271 CreateAnswerAsLocalDescription();
1272
1273 WaitAndVerifyOnAddStream(kStreamLabel1);
1274}
1275
1276TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1277 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001278 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279
1280 CreateOfferAsRemoteDescription();
1281 CreatePrAnswerAsLocalDescription();
1282 CreateAnswerAsLocalDescription();
1283
1284 WaitAndVerifyOnAddStream(kStreamLabel1);
1285}
1286
1287TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1288 InitiateCall();
1289 ASSERT_EQ(1u, pc_->remote_streams()->count());
1290 pc_->RemoveStream(pc_->local_streams()->at(0));
1291 CreateOfferReceiveAnswer();
1292 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001293 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 CreateOfferReceiveAnswer();
1295}
1296
1297// Tests that after negotiating an audio only call, the respondent can perform a
1298// renegotiation that removes the audio stream.
1299TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1300 CreatePeerConnection();
1301 AddVoiceStream(kStreamLabel1);
1302 CreateOfferAsRemoteDescription();
1303 CreateAnswerAsLocalDescription();
1304
1305 ASSERT_EQ(1u, pc_->remote_streams()->count());
1306 pc_->RemoveStream(pc_->local_streams()->at(0));
1307 CreateOfferReceiveAnswer();
1308 EXPECT_EQ(0u, pc_->remote_streams()->count());
1309}
1310
1311// Test that candidates are generated and that we can parse our own candidates.
1312TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1313 CreatePeerConnection();
1314
1315 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1316 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001317 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001318 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001319 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001320 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321
1322 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001323 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001324 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001325 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326
1327 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1328 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1329
1330 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1331}
1332
deadbeefab9b2d12015-10-14 11:33:11 -07001333// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334// not unique.
1335TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1336 CreatePeerConnection();
1337 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001338 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001339 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001340 EXPECT_TRUE(offer);
1341 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342
1343 // Create a local stream with audio&video tracks having same label.
1344 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1345
1346 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001347 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
1349 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001350 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001351 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
1353
1354// Test that we will get different SSRCs for each tracks in the offer and answer
1355// we created.
1356TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1357 CreatePeerConnection();
1358 // Create a local stream with audio&video tracks having different labels.
1359 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1360
1361 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001362 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001363 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 int audio_ssrc = 0;
1365 int video_ssrc = 0;
1366 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1367 &audio_ssrc));
1368 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1369 &video_ssrc));
1370 EXPECT_NE(audio_ssrc, video_ssrc);
1371
1372 // Test CreateAnswer
1373 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001374 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001375 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 audio_ssrc = 0;
1377 video_ssrc = 0;
1378 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1379 &audio_ssrc));
1380 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1381 &video_ssrc));
1382 EXPECT_NE(audio_ssrc, video_ssrc);
1383}
1384
deadbeefeb459812015-12-15 19:24:43 -08001385// Test that it's possible to call AddTrack on a MediaStream after adding
1386// the stream to a PeerConnection.
1387// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1388TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1389 CreatePeerConnection();
1390 // Create audio stream and add to PeerConnection.
1391 AddVoiceStream(kStreamLabel1);
1392 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1393
1394 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001395 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1396 "video_label",
1397 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001398 stream->AddTrack(video_track.get());
1399
kwibergd1fe2812016-04-27 06:47:29 -07001400 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001401 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001402
1403 const cricket::MediaContentDescription* video_desc =
1404 cricket::GetFirstVideoContentDescription(offer->description());
1405 EXPECT_TRUE(video_desc != nullptr);
1406}
1407
1408// Test that it's possible to call RemoveTrack on a MediaStream after adding
1409// the stream to a PeerConnection.
1410// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1411TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1412 CreatePeerConnection();
1413 // Create audio/video stream and add to PeerConnection.
1414 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1415 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1416
1417 // Remove the video track.
1418 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1419
kwibergd1fe2812016-04-27 06:47:29 -07001420 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001421 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001422
1423 const cricket::MediaContentDescription* video_desc =
1424 cricket::GetFirstVideoContentDescription(offer->description());
1425 EXPECT_TRUE(video_desc == nullptr);
1426}
1427
deadbeefbd7d8f72015-12-18 16:58:44 -08001428// Test creating a sender with a stream ID, and ensure the ID is populated
1429// in the offer.
1430TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1431 CreatePeerConnection();
1432 pc_->CreateSender("video", kStreamLabel1);
1433
kwibergd1fe2812016-04-27 06:47:29 -07001434 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001435 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001436
1437 const cricket::MediaContentDescription* video_desc =
1438 cricket::GetFirstVideoContentDescription(offer->description());
1439 ASSERT_TRUE(video_desc != nullptr);
1440 ASSERT_EQ(1u, video_desc->streams().size());
1441 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1442}
1443
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444// Test that we can specify a certain track that we want statistics about.
1445TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1446 InitiateCall();
1447 ASSERT_LT(0u, pc_->remote_streams()->count());
1448 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1449 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1450 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1451 EXPECT_TRUE(DoGetStats(remote_audio));
1452
1453 // Remove the stream. Since we are sending to our selves the local
1454 // and the remote stream is the same.
1455 pc_->RemoveStream(pc_->local_streams()->at(0));
1456 // Do a re-negotiation.
1457 CreateOfferReceiveAnswer();
1458
1459 ASSERT_EQ(0u, pc_->remote_streams()->count());
1460
1461 // Test that we still can get statistics for the old track. Even if it is not
1462 // sent any longer.
1463 EXPECT_TRUE(DoGetStats(remote_audio));
1464}
1465
1466// Test that we can get stats on a video track.
1467TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1468 InitiateCall();
1469 ASSERT_LT(0u, pc_->remote_streams()->count());
1470 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1471 scoped_refptr<MediaStreamTrackInterface> remote_video =
1472 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1473 EXPECT_TRUE(DoGetStats(remote_video));
1474}
1475
1476// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001477// TODO(tommi): Fix this test. DoGetStats will return true
1478// for the unknown track (since GetStats is async), but no
1479// data is returned for the track.
1480TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 InitiateCall();
1482 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1483 pc_factory_->CreateAudioTrack("unknown track", NULL));
1484 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1485}
1486
1487// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 FakeConstraints constraints;
1490 constraints.SetAllowRtpDataChannels();
1491 CreatePeerConnection(&constraints);
1492 scoped_refptr<DataChannelInterface> data1 =
1493 pc_->CreateDataChannel("test1", NULL);
1494 scoped_refptr<DataChannelInterface> data2 =
1495 pc_->CreateDataChannel("test2", NULL);
1496 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001497 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001499 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 new MockDataChannelObserver(data2));
1501
1502 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1503 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1504 std::string data_to_send1 = "testing testing";
1505 std::string data_to_send2 = "testing something else";
1506 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1507
1508 CreateOfferReceiveAnswer();
1509 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1510 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1511
1512 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1513 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1514 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1515 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1516
1517 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1518 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1519
1520 data1->Close();
1521 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1522 CreateOfferReceiveAnswer();
1523 EXPECT_FALSE(observer1->IsOpen());
1524 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1525 EXPECT_TRUE(observer2->IsOpen());
1526
1527 data_to_send2 = "testing something else again";
1528 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1529
1530 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1531}
1532
1533// This test verifies that sendnig binary data over RTP data channels should
1534// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 FakeConstraints constraints;
1537 constraints.SetAllowRtpDataChannels();
1538 CreatePeerConnection(&constraints);
1539 scoped_refptr<DataChannelInterface> data1 =
1540 pc_->CreateDataChannel("test1", NULL);
1541 scoped_refptr<DataChannelInterface> data2 =
1542 pc_->CreateDataChannel("test2", NULL);
1543 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001544 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001546 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 new MockDataChannelObserver(data2));
1548
1549 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1550 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1551
1552 CreateOfferReceiveAnswer();
1553 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1554 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1555
1556 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1557 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1558
jbaucheec21bd2016-03-20 06:15:43 -07001559 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1561}
1562
1563// This test setup a RTP data channels in loop back and test that a channel is
1564// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 FakeConstraints constraints;
1567 constraints.SetAllowRtpDataChannels();
1568 CreatePeerConnection(&constraints);
1569 scoped_refptr<DataChannelInterface> data1 =
1570 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001571 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 new MockDataChannelObserver(data1));
1573
1574 CreateOfferReceiveAnswerWithoutSsrc();
1575
1576 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1577
1578 data1->Close();
1579 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1580 CreateOfferReceiveAnswerWithoutSsrc();
1581 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1582 EXPECT_FALSE(observer1->IsOpen());
1583}
1584
1585// This test that if a data channel is added in an answer a receive only channel
1586// channel is created.
1587TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1588 FakeConstraints constraints;
1589 constraints.SetAllowRtpDataChannels();
1590 CreatePeerConnection(&constraints);
1591
1592 std::string offer_label = "offer_channel";
1593 scoped_refptr<DataChannelInterface> offer_channel =
1594 pc_->CreateDataChannel(offer_label, NULL);
1595
1596 CreateOfferAsLocalDescription();
1597
1598 // Replace the data channel label in the offer and apply it as an answer.
1599 std::string receive_label = "answer_channel";
1600 std::string sdp;
1601 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001602 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 receive_label.c_str(), receive_label.length(),
1604 &sdp);
1605 CreateAnswerAsRemoteDescription(sdp);
1606
1607 // Verify that a new incoming data channel has been created and that
1608 // it is open but can't we written to.
1609 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1610 DataChannelInterface* received_channel = observer_.last_datachannel_;
1611 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1612 EXPECT_EQ(receive_label, received_channel->label());
1613 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1614
1615 // Verify that the channel we initially offered has been rejected.
1616 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1617
1618 // Do another offer / answer exchange and verify that the data channel is
1619 // opened.
1620 CreateOfferReceiveAnswer();
1621 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1622 kTimeout);
1623}
1624
1625// This test that no data channel is returned if a reliable channel is
1626// requested.
1627// TODO(perkj): Remove this test once reliable channels are implemented.
1628TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1629 FakeConstraints constraints;
1630 constraints.SetAllowRtpDataChannels();
1631 CreatePeerConnection(&constraints);
1632
1633 std::string label = "test";
1634 webrtc::DataChannelInit config;
1635 config.reliable = true;
1636 scoped_refptr<DataChannelInterface> channel =
1637 pc_->CreateDataChannel(label, &config);
1638 EXPECT_TRUE(channel == NULL);
1639}
1640
deadbeefab9b2d12015-10-14 11:33:11 -07001641// Verifies that duplicated label is not allowed for RTP data channel.
1642TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1643 FakeConstraints constraints;
1644 constraints.SetAllowRtpDataChannels();
1645 CreatePeerConnection(&constraints);
1646
1647 std::string label = "test";
1648 scoped_refptr<DataChannelInterface> channel =
1649 pc_->CreateDataChannel(label, nullptr);
1650 EXPECT_NE(channel, nullptr);
1651
1652 scoped_refptr<DataChannelInterface> dup_channel =
1653 pc_->CreateDataChannel(label, nullptr);
1654 EXPECT_EQ(dup_channel, nullptr);
1655}
1656
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657// This tests that a SCTP data channel is returned using different
1658// DataChannelInit configurations.
1659TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1660 FakeConstraints constraints;
1661 constraints.SetAllowDtlsSctpDataChannels();
1662 CreatePeerConnection(&constraints);
1663
1664 webrtc::DataChannelInit config;
1665
1666 scoped_refptr<DataChannelInterface> channel =
1667 pc_->CreateDataChannel("1", &config);
1668 EXPECT_TRUE(channel != NULL);
1669 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001670 EXPECT_TRUE(observer_.renegotiation_needed_);
1671 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672
1673 config.ordered = false;
1674 channel = pc_->CreateDataChannel("2", &config);
1675 EXPECT_TRUE(channel != NULL);
1676 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001677 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678
1679 config.ordered = true;
1680 config.maxRetransmits = 0;
1681 channel = pc_->CreateDataChannel("3", &config);
1682 EXPECT_TRUE(channel != NULL);
1683 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001684 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685
1686 config.maxRetransmits = -1;
1687 config.maxRetransmitTime = 0;
1688 channel = pc_->CreateDataChannel("4", &config);
1689 EXPECT_TRUE(channel != NULL);
1690 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001691 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692}
1693
1694// This tests that no data channel is returned if both maxRetransmits and
1695// maxRetransmitTime are set for SCTP data channels.
1696TEST_F(PeerConnectionInterfaceTest,
1697 CreateSctpDataChannelShouldFailForInvalidConfig) {
1698 FakeConstraints constraints;
1699 constraints.SetAllowDtlsSctpDataChannels();
1700 CreatePeerConnection(&constraints);
1701
1702 std::string label = "test";
1703 webrtc::DataChannelInit config;
1704 config.maxRetransmits = 0;
1705 config.maxRetransmitTime = 0;
1706
1707 scoped_refptr<DataChannelInterface> channel =
1708 pc_->CreateDataChannel(label, &config);
1709 EXPECT_TRUE(channel == NULL);
1710}
1711
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712// The test verifies that creating a SCTP data channel with an id already in use
1713// or out of range should fail.
1714TEST_F(PeerConnectionInterfaceTest,
1715 CreateSctpDataChannelWithInvalidIdShouldFail) {
1716 FakeConstraints constraints;
1717 constraints.SetAllowDtlsSctpDataChannels();
1718 CreatePeerConnection(&constraints);
1719
1720 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001721 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001723 config.id = 1;
1724 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 EXPECT_TRUE(channel != NULL);
1726 EXPECT_EQ(1, channel->id());
1727
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 channel = pc_->CreateDataChannel("x", &config);
1729 EXPECT_TRUE(channel == NULL);
1730
1731 config.id = cricket::kMaxSctpSid;
1732 channel = pc_->CreateDataChannel("max", &config);
1733 EXPECT_TRUE(channel != NULL);
1734 EXPECT_EQ(config.id, channel->id());
1735
1736 config.id = cricket::kMaxSctpSid + 1;
1737 channel = pc_->CreateDataChannel("x", &config);
1738 EXPECT_TRUE(channel == NULL);
1739}
1740
deadbeefab9b2d12015-10-14 11:33:11 -07001741// Verifies that duplicated label is allowed for SCTP data channel.
1742TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1743 FakeConstraints constraints;
1744 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1745 true);
1746 CreatePeerConnection(&constraints);
1747
1748 std::string label = "test";
1749 scoped_refptr<DataChannelInterface> channel =
1750 pc_->CreateDataChannel(label, nullptr);
1751 EXPECT_NE(channel, nullptr);
1752
1753 scoped_refptr<DataChannelInterface> dup_channel =
1754 pc_->CreateDataChannel(label, nullptr);
1755 EXPECT_NE(dup_channel, nullptr);
1756}
1757
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001758// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1759// DataChannel.
1760TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1761 FakeConstraints constraints;
1762 constraints.SetAllowRtpDataChannels();
1763 CreatePeerConnection(&constraints);
1764
1765 scoped_refptr<DataChannelInterface> dc1 =
1766 pc_->CreateDataChannel("test1", NULL);
1767 EXPECT_TRUE(observer_.renegotiation_needed_);
1768 observer_.renegotiation_needed_ = false;
1769
1770 scoped_refptr<DataChannelInterface> dc2 =
1771 pc_->CreateDataChannel("test2", NULL);
1772 EXPECT_TRUE(observer_.renegotiation_needed_);
1773}
1774
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 FakeConstraints constraints;
1778 constraints.SetAllowRtpDataChannels();
1779 CreatePeerConnection(&constraints);
1780
1781 scoped_refptr<DataChannelInterface> data1 =
1782 pc_->CreateDataChannel("test1", NULL);
1783 scoped_refptr<DataChannelInterface> data2 =
1784 pc_->CreateDataChannel("test2", NULL);
1785 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001786 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001788 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 new MockDataChannelObserver(data2));
1790
1791 CreateOfferReceiveAnswer();
1792 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1793 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1794
1795 ReleasePeerConnection();
1796 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1797 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1798}
1799
1800// This test that data channels can be rejected in an answer.
1801TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1802 FakeConstraints constraints;
1803 constraints.SetAllowRtpDataChannels();
1804 CreatePeerConnection(&constraints);
1805
1806 scoped_refptr<DataChannelInterface> offer_channel(
1807 pc_->CreateDataChannel("offer_channel", NULL));
1808
1809 CreateOfferAsLocalDescription();
1810
1811 // Create an answer where the m-line for data channels are rejected.
1812 std::string sdp;
1813 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1814 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1815 SessionDescriptionInterface::kAnswer);
1816 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1817 cricket::ContentInfo* data_info =
1818 answer->description()->GetContentByName("data");
1819 data_info->rejected = true;
1820
1821 DoSetRemoteDescription(answer);
1822 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1823}
1824
1825// Test that we can create a session description from an SDP string from
1826// FireFox, use it as a remote session description, generate an answer and use
1827// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001828TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001829 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 FakeConstraints constraints;
1831 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1832 true);
1833 CreatePeerConnection(&constraints);
1834 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1835 SessionDescriptionInterface* desc =
1836 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001837 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1839 CreateAnswerAsLocalDescription();
1840 ASSERT_TRUE(pc_->local_description() != NULL);
1841 ASSERT_TRUE(pc_->remote_description() != NULL);
1842
1843 const cricket::ContentInfo* content =
1844 cricket::GetFirstAudioContent(pc_->local_description()->description());
1845 ASSERT_TRUE(content != NULL);
1846 EXPECT_FALSE(content->rejected);
1847
1848 content =
1849 cricket::GetFirstVideoContent(pc_->local_description()->description());
1850 ASSERT_TRUE(content != NULL);
1851 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001852#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 content =
1854 cricket::GetFirstDataContent(pc_->local_description()->description());
1855 ASSERT_TRUE(content != NULL);
1856 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001857#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858}
1859
1860// Test that we can create an audio only offer and receive an answer with a
1861// limited set of audio codecs and receive an updated offer with more audio
1862// codecs, where the added codecs are not supported.
1863TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1864 CreatePeerConnection();
1865 AddVoiceStream("audio_label");
1866 CreateOfferAsLocalDescription();
1867
1868 SessionDescriptionInterface* answer =
1869 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001870 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1872
1873 SessionDescriptionInterface* updated_offer =
1874 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001875 webrtc::kAudioSdpWithUnsupportedCodecs,
1876 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1878 CreateAnswerAsLocalDescription();
1879}
1880
deadbeefc80741f2015-10-22 13:14:45 -07001881// Test that if we're receiving (but not sending) a track, subsequent offers
1882// will have m-lines with a=recvonly.
1883TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1884 FakeConstraints constraints;
1885 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1886 true);
1887 CreatePeerConnection(&constraints);
1888 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1889 CreateAnswerAsLocalDescription();
1890
1891 // At this point we should be receiving stream 1, but not sending anything.
1892 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001893 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001894 DoCreateOffer(&offer, nullptr);
1895
1896 const cricket::ContentInfo* video_content =
1897 cricket::GetFirstVideoContent(offer->description());
1898 const cricket::VideoContentDescription* video_desc =
1899 static_cast<const cricket::VideoContentDescription*>(
1900 video_content->description);
1901 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1902
1903 const cricket::ContentInfo* audio_content =
1904 cricket::GetFirstAudioContent(offer->description());
1905 const cricket::AudioContentDescription* audio_desc =
1906 static_cast<const cricket::AudioContentDescription*>(
1907 audio_content->description);
1908 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1909}
1910
1911// Test that if we're receiving (but not sending) a track, and the
1912// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1913// false, the generated m-lines will be a=inactive.
1914TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1915 FakeConstraints constraints;
1916 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1917 true);
1918 CreatePeerConnection(&constraints);
1919 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1920 CreateAnswerAsLocalDescription();
1921
1922 // At this point we should be receiving stream 1, but not sending anything.
1923 // A new offer would be recvonly, but we'll set the "no receive" constraints
1924 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001925 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001926 FakeConstraints offer_constraints;
1927 offer_constraints.AddMandatory(
1928 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1929 offer_constraints.AddMandatory(
1930 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1931 DoCreateOffer(&offer, &offer_constraints);
1932
1933 const cricket::ContentInfo* video_content =
1934 cricket::GetFirstVideoContent(offer->description());
1935 const cricket::VideoContentDescription* video_desc =
1936 static_cast<const cricket::VideoContentDescription*>(
1937 video_content->description);
1938 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1939
1940 const cricket::ContentInfo* audio_content =
1941 cricket::GetFirstAudioContent(offer->description());
1942 const cricket::AudioContentDescription* audio_desc =
1943 static_cast<const cricket::AudioContentDescription*>(
1944 audio_content->description);
1945 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1946}
1947
deadbeef653b8e02015-11-11 12:55:10 -08001948// Test that we can use SetConfiguration to change the ICE servers of the
1949// PortAllocator.
1950TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1951 CreatePeerConnection();
1952
1953 PeerConnectionInterface::RTCConfiguration config;
1954 PeerConnectionInterface::IceServer server;
1955 server.uri = "stun:test_hostname";
1956 config.servers.push_back(server);
1957 EXPECT_TRUE(pc_->SetConfiguration(config));
1958
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001959 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1960 EXPECT_EQ("test_hostname",
1961 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001962}
1963
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001964TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1965 CreatePeerConnection();
1966 PeerConnectionInterface::RTCConfiguration config;
1967 config.type = PeerConnectionInterface::kRelay;
1968 EXPECT_TRUE(pc_->SetConfiguration(config));
1969 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1970}
1971
1972// Test that when SetConfiguration changes both the pool size and other
1973// attributes, the pooled session is created with the updated attributes.
1974TEST_F(PeerConnectionInterfaceTest,
1975 SetConfigurationCreatesPooledSessionCorrectly) {
1976 CreatePeerConnection();
1977 PeerConnectionInterface::RTCConfiguration config;
1978 config.ice_candidate_pool_size = 1;
1979 PeerConnectionInterface::IceServer server;
1980 server.uri = kStunAddressOnly;
1981 config.servers.push_back(server);
1982 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001983 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001984
1985 const cricket::FakePortAllocatorSession* session =
1986 static_cast<const cricket::FakePortAllocatorSession*>(
1987 port_allocator_->GetPooledSession());
1988 ASSERT_NE(nullptr, session);
1989 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001990}
1991
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992// Test that PeerConnection::Close changes the states to closed and all remote
1993// tracks change state to ended.
1994TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1995 // Initialize a PeerConnection and negotiate local and remote session
1996 // description.
1997 InitiateCall();
1998 ASSERT_EQ(1u, pc_->local_streams()->count());
1999 ASSERT_EQ(1u, pc_->remote_streams()->count());
2000
2001 pc_->Close();
2002
2003 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2004 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2005 pc_->ice_connection_state());
2006 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2007 pc_->ice_gathering_state());
2008
2009 EXPECT_EQ(1u, pc_->local_streams()->count());
2010 EXPECT_EQ(1u, pc_->remote_streams()->count());
2011
2012 scoped_refptr<MediaStreamInterface> remote_stream =
2013 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002014 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002015 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002016 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2017 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2018 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019}
2020
2021// Test that PeerConnection methods fails gracefully after
2022// PeerConnection::Close has been called.
2023TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2024 CreatePeerConnection();
2025 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2026 CreateOfferAsRemoteDescription();
2027 CreateAnswerAsLocalDescription();
2028
2029 ASSERT_EQ(1u, pc_->local_streams()->count());
2030 scoped_refptr<MediaStreamInterface> local_stream =
2031 pc_->local_streams()->at(0);
2032
2033 pc_->Close();
2034
2035 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002036 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037
2038 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002039 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002041 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042
2043 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2044
2045 EXPECT_TRUE(pc_->local_description() != NULL);
2046 EXPECT_TRUE(pc_->remote_description() != NULL);
2047
kwibergd1fe2812016-04-27 06:47:29 -07002048 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002049 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002050 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002051 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052
2053 std::string sdp;
2054 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2055 SessionDescriptionInterface* remote_offer =
2056 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2057 sdp, NULL);
2058 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2059
2060 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2061 SessionDescriptionInterface* local_offer =
2062 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2063 sdp, NULL);
2064 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2065}
2066
2067// Test that GetStats can still be called after PeerConnection::Close.
2068TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2069 InitiateCall();
2070 pc_->Close();
2071 DoGetStats(NULL);
2072}
deadbeefab9b2d12015-10-14 11:33:11 -07002073
2074// NOTE: The series of tests below come from what used to be
2075// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2076// setting a remote or local description has the expected effects.
2077
2078// This test verifies that the remote MediaStreams corresponding to a received
2079// SDP string is created. In this test the two separate MediaStreams are
2080// signaled.
2081TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2082 FakeConstraints constraints;
2083 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2084 true);
2085 CreatePeerConnection(&constraints);
2086 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2087
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002088 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002089 EXPECT_TRUE(
2090 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2091 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2092 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2093
2094 // Create a session description based on another SDP with another
2095 // MediaStream.
2096 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2097
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002098 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002099 EXPECT_TRUE(
2100 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2101}
2102
2103// This test verifies that when remote tracks are added/removed from SDP, the
2104// created remote streams are updated appropriately.
2105TEST_F(PeerConnectionInterfaceTest,
2106 AddRemoveTrackFromExistingRemoteMediaStream) {
2107 FakeConstraints constraints;
2108 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2109 true);
2110 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002111 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002112 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002113 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2114 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2115 reference_collection_));
2116
2117 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002118 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002119 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002120 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2121 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2122 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002123 scoped_refptr<AudioTrackInterface> audio_track2 =
2124 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2125 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2126 scoped_refptr<VideoTrackInterface> video_track2 =
2127 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2128 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002129
2130 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002131 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002132 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002133 MockTrackObserver audio_track_observer(audio_track2);
2134 MockTrackObserver video_track_observer(video_track2);
2135
2136 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2137 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002138 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2139 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2140 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002141 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002142 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002143 audio_track2->state(), kTimeout);
2144 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2145 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002146}
2147
2148// This tests that remote tracks are ended if a local session description is set
2149// that rejects the media content type.
2150TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2151 FakeConstraints constraints;
2152 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2153 true);
2154 CreatePeerConnection(&constraints);
2155 // First create and set a remote offer, then reject its video content in our
2156 // answer.
2157 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2158 ASSERT_EQ(1u, observer_.remote_streams()->count());
2159 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2160 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2161 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2162
2163 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2164 remote_stream->GetVideoTracks()[0];
2165 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2166 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2167 remote_stream->GetAudioTracks()[0];
2168 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2169
kwibergd1fe2812016-04-27 06:47:29 -07002170 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002171 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002172 cricket::ContentInfo* video_info =
2173 local_answer->description()->GetContentByName("video");
2174 video_info->rejected = true;
2175 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2176 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2177 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2178
2179 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002181 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002182 video_info = local_offer->description()->GetContentByName("video");
2183 ASSERT_TRUE(video_info != nullptr);
2184 video_info->rejected = true;
2185 cricket::ContentInfo* audio_info =
2186 local_offer->description()->GetContentByName("audio");
2187 ASSERT_TRUE(audio_info != nullptr);
2188 audio_info->rejected = true;
2189 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002190 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002191 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002192 remote_audio->state(), kTimeout);
2193 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2194 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002195}
2196
2197// This tests that we won't crash if the remote track has been removed outside
2198// of PeerConnection and then PeerConnection tries to reject the track.
2199TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2200 FakeConstraints constraints;
2201 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2202 true);
2203 CreatePeerConnection(&constraints);
2204 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2205 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2206 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2207 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2208
kwibergd1fe2812016-04-27 06:47:29 -07002209 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002210 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2211 kSdpStringWithStream1, nullptr));
2212 cricket::ContentInfo* video_info =
2213 local_answer->description()->GetContentByName("video");
2214 video_info->rejected = true;
2215 cricket::ContentInfo* audio_info =
2216 local_answer->description()->GetContentByName("audio");
2217 audio_info->rejected = true;
2218 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2219
2220 // No crash is a pass.
2221}
2222
deadbeef5e97fb52015-10-15 12:49:08 -07002223// This tests that if a recvonly remote description is set, no remote streams
2224// will be created, even if the description contains SSRCs/MSIDs.
2225// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2226TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2227 FakeConstraints constraints;
2228 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2229 true);
2230 CreatePeerConnection(&constraints);
2231
2232 std::string recvonly_offer = kSdpStringWithStream1;
2233 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2234 strlen(kRecvonly), &recvonly_offer);
2235 CreateAndSetRemoteOffer(recvonly_offer);
2236
2237 EXPECT_EQ(0u, observer_.remote_streams()->count());
2238}
2239
deadbeefab9b2d12015-10-14 11:33:11 -07002240// This tests that a default MediaStream is created if a remote session
2241// description doesn't contain any streams and no MSID support.
2242// It also tests that the default stream is updated if a video m-line is added
2243// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002244TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002245 FakeConstraints constraints;
2246 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2247 true);
2248 CreatePeerConnection(&constraints);
2249 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2250
2251 ASSERT_EQ(1u, observer_.remote_streams()->count());
2252 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2253
2254 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2255 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2256 EXPECT_EQ("default", remote_stream->label());
2257
2258 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2259 ASSERT_EQ(1u, observer_.remote_streams()->count());
2260 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2261 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002262 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2263 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002264 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2265 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002266 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2267 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002268}
2269
2270// This tests that a default MediaStream is created if a remote session
2271// description doesn't contain any streams and media direction is send only.
2272TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002273 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002274 FakeConstraints constraints;
2275 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2276 true);
2277 CreatePeerConnection(&constraints);
2278 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2279
2280 ASSERT_EQ(1u, observer_.remote_streams()->count());
2281 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2282
2283 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2284 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2285 EXPECT_EQ("default", remote_stream->label());
2286}
2287
2288// This tests that it won't crash when PeerConnection tries to remove
2289// a remote track that as already been removed from the MediaStream.
2290TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2291 FakeConstraints constraints;
2292 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2293 true);
2294 CreatePeerConnection(&constraints);
2295 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2296 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2297 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2298 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2299
2300 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2301
2302 // No crash is a pass.
2303}
2304
2305// This tests that a default MediaStream is created if the remote session
2306// description doesn't contain any streams and don't contain an indication if
2307// MSID is supported.
2308TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002309 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002310 FakeConstraints constraints;
2311 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2312 true);
2313 CreatePeerConnection(&constraints);
2314 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2315
2316 ASSERT_EQ(1u, observer_.remote_streams()->count());
2317 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2318 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2319 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2320}
2321
2322// This tests that a default MediaStream is not created if the remote session
2323// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002324TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002325 FakeConstraints constraints;
2326 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2327 true);
2328 CreatePeerConnection(&constraints);
2329 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2330 EXPECT_EQ(0u, observer_.remote_streams()->count());
2331}
2332
deadbeefbda7e0b2015-12-08 17:13:40 -08002333// This tests that when setting a new description, the old default tracks are
2334// not destroyed and recreated.
2335// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002336TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002337 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002338 FakeConstraints constraints;
2339 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2340 true);
2341 CreatePeerConnection(&constraints);
2342 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2343
2344 ASSERT_EQ(1u, observer_.remote_streams()->count());
2345 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2346 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2347
2348 // Set the track to "disabled", then set a new description and ensure the
2349 // track is still disabled, which ensures it hasn't been recreated.
2350 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2351 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2352 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2353 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2354}
2355
deadbeefab9b2d12015-10-14 11:33:11 -07002356// This tests that a default MediaStream is not created if a remote session
2357// description is updated to not have any MediaStreams.
2358TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2359 FakeConstraints constraints;
2360 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2361 true);
2362 CreatePeerConnection(&constraints);
2363 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002364 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002365 EXPECT_TRUE(
2366 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2367
2368 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2369 EXPECT_EQ(0u, observer_.remote_streams()->count());
2370}
2371
2372// This tests that an RtpSender is created when the local description is set
2373// after adding a local stream.
2374// TODO(deadbeef): This test and the one below it need to be updated when
2375// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002376TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002377 FakeConstraints constraints;
2378 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2379 true);
2380 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002381
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002382 // Create an offer with 1 stream with 2 tracks of each type.
2383 rtc::scoped_refptr<StreamCollection> stream_collection =
2384 CreateStreamCollection(1, 2);
2385 pc_->AddStream(stream_collection->at(0));
2386 std::unique_ptr<SessionDescriptionInterface> offer;
2387 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2388 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002389
deadbeefab9b2d12015-10-14 11:33:11 -07002390 auto senders = pc_->GetSenders();
2391 EXPECT_EQ(4u, senders.size());
2392 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2393 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2394 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2395 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2396
2397 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002398 pc_->RemoveStream(stream_collection->at(0));
2399 stream_collection = CreateStreamCollection(1, 1);
2400 pc_->AddStream(stream_collection->at(0));
2401 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2402 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2403
deadbeefab9b2d12015-10-14 11:33:11 -07002404 senders = pc_->GetSenders();
2405 EXPECT_EQ(2u, senders.size());
2406 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2407 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2408 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2409 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2410}
2411
2412// This tests that an RtpSender is created when the local description is set
2413// before adding a local stream.
2414TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002415 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002416 FakeConstraints constraints;
2417 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2418 true);
2419 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002420
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002421 rtc::scoped_refptr<StreamCollection> stream_collection =
2422 CreateStreamCollection(1, 2);
2423 // Add a stream to create the offer, but remove it afterwards.
2424 pc_->AddStream(stream_collection->at(0));
2425 std::unique_ptr<SessionDescriptionInterface> offer;
2426 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2427 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002428
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002429 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002430 auto senders = pc_->GetSenders();
2431 EXPECT_EQ(0u, senders.size());
2432
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002433 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002434 senders = pc_->GetSenders();
2435 EXPECT_EQ(4u, senders.size());
2436 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2437 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2438 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2439 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2440}
2441
2442// This tests that the expected behavior occurs if the SSRC on a local track is
2443// changed when SetLocalDescription is called.
2444TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002445 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002446 FakeConstraints constraints;
2447 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2448 true);
2449 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002450
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002451 rtc::scoped_refptr<StreamCollection> stream_collection =
2452 CreateStreamCollection(2, 1);
2453 pc_->AddStream(stream_collection->at(0));
2454 std::unique_ptr<SessionDescriptionInterface> offer;
2455 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2456 // Grab a copy of the offer before it gets passed into the PC.
2457 std::unique_ptr<JsepSessionDescription> modified_offer(
2458 new JsepSessionDescription(JsepSessionDescription::kOffer));
2459 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2460 offer->session_version());
2461 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002462
deadbeefab9b2d12015-10-14 11:33:11 -07002463 auto senders = pc_->GetSenders();
2464 EXPECT_EQ(2u, senders.size());
2465 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2466 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2467
2468 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002469 cricket::MediaContentDescription* desc =
2470 cricket::GetFirstAudioContentDescription(modified_offer->description());
2471 ASSERT_TRUE(desc != NULL);
2472 for (StreamParams& stream : desc->mutable_streams()) {
2473 for (unsigned int& ssrc : stream.ssrcs) {
2474 ++ssrc;
2475 }
2476 }
deadbeefab9b2d12015-10-14 11:33:11 -07002477
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002478 desc =
2479 cricket::GetFirstVideoContentDescription(modified_offer->description());
2480 ASSERT_TRUE(desc != NULL);
2481 for (StreamParams& stream : desc->mutable_streams()) {
2482 for (unsigned int& ssrc : stream.ssrcs) {
2483 ++ssrc;
2484 }
2485 }
2486
2487 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002488 senders = pc_->GetSenders();
2489 EXPECT_EQ(2u, senders.size());
2490 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2491 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2492 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2493 // changed.
2494}
2495
2496// This tests that the expected behavior occurs if a new session description is
2497// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002498TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002499 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002500 FakeConstraints constraints;
2501 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2502 true);
2503 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002504
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002505 rtc::scoped_refptr<StreamCollection> stream_collection =
2506 CreateStreamCollection(2, 1);
2507 pc_->AddStream(stream_collection->at(0));
2508 std::unique_ptr<SessionDescriptionInterface> offer;
2509 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2510 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002511
deadbeefab9b2d12015-10-14 11:33:11 -07002512 auto senders = pc_->GetSenders();
2513 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002514 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2515 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002516
2517 // Add a new MediaStream but with the same tracks as in the first stream.
2518 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2519 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002520 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2521 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002522 pc_->AddStream(stream_1);
2523
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002524 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2525 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002526
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002527 auto new_senders = pc_->GetSenders();
2528 // Should be the same senders as before, but with updated stream id.
2529 // Note that this behavior is subject to change in the future.
2530 // We may decide the PC should ignore existing tracks in AddStream.
2531 EXPECT_EQ(senders, new_senders);
2532 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2533 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002534}
2535
nisse51542be2016-02-12 02:27:06 -08002536// The PeerConnectionMediaConfig tests below verify that configuration
2537// and constraints are propagated into the MediaConfig passed to
2538// CreateMediaController. These settings are intended for MediaChannel
2539// constructors, but that is not exercised by these unittest.
2540class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2541 public:
2542 webrtc::MediaControllerInterface* CreateMediaController(
2543 const cricket::MediaConfig& config) const override {
2544 create_media_controller_called_ = true;
2545 create_media_controller_config_ = config;
2546
2547 webrtc::MediaControllerInterface* mc =
2548 PeerConnectionFactory::CreateMediaController(config);
2549 EXPECT_TRUE(mc != nullptr);
2550 return mc;
2551 }
2552
2553 // Mutable, so they can be modified in the above const-declared method.
2554 mutable bool create_media_controller_called_ = false;
2555 mutable cricket::MediaConfig create_media_controller_config_;
2556};
2557
2558class PeerConnectionMediaConfigTest : public testing::Test {
2559 protected:
2560 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002561 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002562 pcf_->Initialize();
2563 }
2564 const cricket::MediaConfig& TestCreatePeerConnection(
2565 const PeerConnectionInterface::RTCConfiguration& config,
2566 const MediaConstraintsInterface *constraints) {
2567 pcf_->create_media_controller_called_ = false;
2568
2569 scoped_refptr<PeerConnectionInterface> pc(
2570 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2571 &observer_));
2572 EXPECT_TRUE(pc.get());
2573 EXPECT_TRUE(pcf_->create_media_controller_called_);
2574 return pcf_->create_media_controller_config_;
2575 }
2576
2577 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2578 MockPeerConnectionObserver observer_;
2579};
2580
2581// This test verifies the default behaviour with no constraints and a
2582// default RTCConfiguration.
2583TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2584 PeerConnectionInterface::RTCConfiguration config;
2585 FakeConstraints constraints;
2586
2587 const cricket::MediaConfig& media_config =
2588 TestCreatePeerConnection(config, &constraints);
2589
2590 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002591 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2592 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2593 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002594}
2595
2596// This test verifies the DSCP constraint is recognized and passed to
2597// the CreateMediaController call.
2598TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2599 PeerConnectionInterface::RTCConfiguration config;
2600 FakeConstraints constraints;
2601
2602 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2603 const cricket::MediaConfig& media_config =
2604 TestCreatePeerConnection(config, &constraints);
2605
2606 EXPECT_TRUE(media_config.enable_dscp);
2607}
2608
2609// This test verifies the cpu overuse detection constraint is
2610// recognized and passed to the CreateMediaController call.
2611TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2612 PeerConnectionInterface::RTCConfiguration config;
2613 FakeConstraints constraints;
2614
2615 constraints.AddOptional(
2616 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2617 const cricket::MediaConfig media_config =
2618 TestCreatePeerConnection(config, &constraints);
2619
nisse0db023a2016-03-01 04:29:59 -08002620 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002621}
2622
2623// This test verifies that the disable_prerenderer_smoothing flag is
2624// propagated from RTCConfiguration to the CreateMediaController call.
2625TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2626 PeerConnectionInterface::RTCConfiguration config;
2627 FakeConstraints constraints;
2628
Niels Möller71bdda02016-03-31 12:59:59 +02002629 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002630 const cricket::MediaConfig& media_config =
2631 TestCreatePeerConnection(config, &constraints);
2632
nisse0db023a2016-03-01 04:29:59 -08002633 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2634}
2635
2636// This test verifies the suspend below min bitrate constraint is
2637// recognized and passed to the CreateMediaController call.
2638TEST_F(PeerConnectionMediaConfigTest,
2639 TestSuspendBelowMinBitrateConstraintTrue) {
2640 PeerConnectionInterface::RTCConfiguration config;
2641 FakeConstraints constraints;
2642
2643 constraints.AddOptional(
2644 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2645 true);
2646 const cricket::MediaConfig media_config =
2647 TestCreatePeerConnection(config, &constraints);
2648
2649 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002650}
2651
deadbeefab9b2d12015-10-14 11:33:11 -07002652// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002653// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2654// "verify options are converted correctly", should be "pass options into
2655// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002656
2657TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2658 RTCOfferAnswerOptions rtc_options;
2659 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2660
2661 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002662 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002663
2664 rtc_options.offer_to_receive_audio =
2665 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002666 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002667}
2668
2669TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2670 RTCOfferAnswerOptions rtc_options;
2671 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2672
2673 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002674 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002675
2676 rtc_options.offer_to_receive_video =
2677 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002678 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002679}
2680
2681// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002682// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002683TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2684 RTCOfferAnswerOptions rtc_options;
2685 rtc_options.offer_to_receive_audio = 1;
2686 rtc_options.offer_to_receive_video = 1;
2687
2688 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002689 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002690 EXPECT_TRUE(options.has_audio());
2691 EXPECT_TRUE(options.has_video());
2692 EXPECT_TRUE(options.bundle_enabled);
2693}
2694
2695// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002696// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002697TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2698 RTCOfferAnswerOptions rtc_options;
2699 rtc_options.offer_to_receive_audio = 1;
2700
2701 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002702 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002703 EXPECT_TRUE(options.has_audio());
2704 EXPECT_FALSE(options.has_video());
2705 EXPECT_TRUE(options.bundle_enabled);
2706}
2707
2708// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002709// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002710TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2711 RTCOfferAnswerOptions rtc_options;
2712
2713 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002714 options.transport_options["audio"] = cricket::TransportOptions();
2715 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002716 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002717 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002718 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002719 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002720 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002721 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2722 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002723}
2724
2725// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002726// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002727TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2728 RTCOfferAnswerOptions rtc_options;
2729 rtc_options.offer_to_receive_audio = 0;
2730 rtc_options.offer_to_receive_video = 1;
2731
2732 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002733 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002734 EXPECT_FALSE(options.has_audio());
2735 EXPECT_TRUE(options.has_video());
2736 EXPECT_TRUE(options.bundle_enabled);
2737}
2738
2739// Test that a correct MediaSessionOptions is created for an offer if
2740// UseRtpMux is set to false.
2741TEST(CreateSessionOptionsTest,
2742 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2743 RTCOfferAnswerOptions rtc_options;
2744 rtc_options.offer_to_receive_audio = 1;
2745 rtc_options.offer_to_receive_video = 1;
2746 rtc_options.use_rtp_mux = false;
2747
2748 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002749 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002750 EXPECT_TRUE(options.has_audio());
2751 EXPECT_TRUE(options.has_video());
2752 EXPECT_FALSE(options.bundle_enabled);
2753}
2754
2755// Test that a correct MediaSessionOptions is created to restart ice if
2756// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002757// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002758TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2759 RTCOfferAnswerOptions rtc_options;
2760 rtc_options.ice_restart = true;
2761
2762 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002763 options.transport_options["audio"] = cricket::TransportOptions();
2764 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002765 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002766 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2767 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002768
2769 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002770 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002771 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2772 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002773}
2774
2775// Test that the MediaConstraints in an answer don't affect if audio and video
2776// is offered in an offer but that if kOfferToReceiveAudio or
2777// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2778// included in subsequent answers.
2779TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2780 FakeConstraints answer_c;
2781 answer_c.SetMandatoryReceiveAudio(true);
2782 answer_c.SetMandatoryReceiveVideo(true);
2783
2784 cricket::MediaSessionOptions answer_options;
2785 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2786 EXPECT_TRUE(answer_options.has_audio());
2787 EXPECT_TRUE(answer_options.has_video());
2788
deadbeefc80741f2015-10-22 13:14:45 -07002789 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002790
2791 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002792 EXPECT_TRUE(
2793 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002794 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002795 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002796
deadbeefc80741f2015-10-22 13:14:45 -07002797 RTCOfferAnswerOptions updated_rtc_offer_options;
2798 updated_rtc_offer_options.offer_to_receive_audio = 1;
2799 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002800
2801 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002802 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002803 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002804 EXPECT_TRUE(updated_offer_options.has_audio());
2805 EXPECT_TRUE(updated_offer_options.has_video());
2806
2807 // Since an offer has been created with both audio and video, subsequent
2808 // offers and answers should contain both audio and video.
2809 // Answers will only contain the media types that exist in the offer
2810 // regardless of the value of |updated_answer_options.has_audio| and
2811 // |updated_answer_options.has_video|.
2812 FakeConstraints updated_answer_c;
2813 answer_c.SetMandatoryReceiveAudio(false);
2814 answer_c.SetMandatoryReceiveVideo(false);
2815
2816 cricket::MediaSessionOptions updated_answer_options;
2817 EXPECT_TRUE(
2818 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2819 EXPECT_TRUE(updated_answer_options.has_audio());
2820 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002821}