blob: c639f98b4f05f5f6463ef07d5b3789144e4e0c83 [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
kwiberg77eab702016-09-28 17:42:01 -070015#include "webrtc/test/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"
zhihuang29ff8442016-07-27 11:07:25 -070043#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46static const char kStreamLabel1[] = "local_stream_1";
47static const char kStreamLabel2[] = "local_stream_2";
48static const char kStreamLabel3[] = "local_stream_3";
49static const int kDefaultStunPort = 3478;
50static const char kStunAddressOnly[] = "stun:address";
51static const char kStunInvalidPort[] = "stun:address:-1";
52static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
53static const char kStunAddressPortAndMore2[] = "stun:address:port more";
54static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
55static const char kTurnUsername[] = "user";
56static const char kTurnPassword[] = "password";
57static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020058static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
deadbeefab9b2d12015-10-14 11:33:11 -070060static const char kStreams[][8] = {"stream1", "stream2"};
61static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
62static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
63
deadbeef5e97fb52015-10-15 12:49:08 -070064static const char kRecvonly[] = "recvonly";
65static const char kSendrecv[] = "sendrecv";
66
deadbeefab9b2d12015-10-14 11:33:11 -070067// Reference SDP with a MediaStream with label "stream1" and audio track with
68// id "audio_1" and a video track with id "video_1;
69static const char kSdpStringWithStream1[] =
70 "v=0\r\n"
71 "o=- 0 0 IN IP4 127.0.0.1\r\n"
72 "s=-\r\n"
73 "t=0 0\r\n"
74 "a=ice-ufrag:e5785931\r\n"
75 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
76 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
77 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
78 "m=audio 1 RTP/AVPF 103\r\n"
79 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070080 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070081 "a=rtpmap:103 ISAC/16000\r\n"
82 "a=ssrc:1 cname:stream1\r\n"
83 "a=ssrc:1 mslabel:stream1\r\n"
84 "a=ssrc:1 label:audiotrack0\r\n"
85 "m=video 1 RTP/AVPF 120\r\n"
86 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070087 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070088 "a=rtpmap:120 VP8/90000\r\n"
89 "a=ssrc:2 cname:stream1\r\n"
90 "a=ssrc:2 mslabel:stream1\r\n"
91 "a=ssrc:2 label:videotrack0\r\n";
92
93// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
94// MediaStreams have one audio track and one video track.
95// This uses MSID.
96static const char kSdpStringWithStream1And2[] =
97 "v=0\r\n"
98 "o=- 0 0 IN IP4 127.0.0.1\r\n"
99 "s=-\r\n"
100 "t=0 0\r\n"
101 "a=ice-ufrag:e5785931\r\n"
102 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
103 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
104 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
105 "a=msid-semantic: WMS stream1 stream2\r\n"
106 "m=audio 1 RTP/AVPF 103\r\n"
107 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700108 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700109 "a=rtpmap:103 ISAC/16000\r\n"
110 "a=ssrc:1 cname:stream1\r\n"
111 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
112 "a=ssrc:3 cname:stream2\r\n"
113 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
114 "m=video 1 RTP/AVPF 120\r\n"
115 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700116 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700117 "a=rtpmap:120 VP8/0\r\n"
118 "a=ssrc:2 cname:stream1\r\n"
119 "a=ssrc:2 msid:stream1 videotrack0\r\n"
120 "a=ssrc:4 cname:stream2\r\n"
121 "a=ssrc:4 msid:stream2 videotrack1\r\n";
122
123// Reference SDP without MediaStreams. Msid is not supported.
124static const char kSdpStringWithoutStreams[] =
125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
129 "a=ice-ufrag:e5785931\r\n"
130 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
131 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
132 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
133 "m=audio 1 RTP/AVPF 103\r\n"
134 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700135 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700136 "a=rtpmap:103 ISAC/16000\r\n"
137 "m=video 1 RTP/AVPF 120\r\n"
138 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700139 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700140 "a=rtpmap:120 VP8/90000\r\n";
141
142// Reference SDP without MediaStreams. Msid is supported.
143static const char kSdpStringWithMsidWithoutStreams[] =
144 "v=0\r\n"
145 "o=- 0 0 IN IP4 127.0.0.1\r\n"
146 "s=-\r\n"
147 "t=0 0\r\n"
148 "a=ice-ufrag:e5785931\r\n"
149 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
150 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
151 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
152 "a=msid-semantic: WMS\r\n"
153 "m=audio 1 RTP/AVPF 103\r\n"
154 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700155 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700156 "a=rtpmap:103 ISAC/16000\r\n"
157 "m=video 1 RTP/AVPF 120\r\n"
158 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700159 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700160 "a=rtpmap:120 VP8/90000\r\n";
161
162// Reference SDP without MediaStreams and audio only.
163static const char kSdpStringWithoutStreamsAudioOnly[] =
164 "v=0\r\n"
165 "o=- 0 0 IN IP4 127.0.0.1\r\n"
166 "s=-\r\n"
167 "t=0 0\r\n"
168 "a=ice-ufrag:e5785931\r\n"
169 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
170 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
171 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
172 "m=audio 1 RTP/AVPF 103\r\n"
173 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700174 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700175 "a=rtpmap:103 ISAC/16000\r\n";
176
177// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
178static const char kSdpStringSendOnlyWithoutStreams[] =
179 "v=0\r\n"
180 "o=- 0 0 IN IP4 127.0.0.1\r\n"
181 "s=-\r\n"
182 "t=0 0\r\n"
183 "a=ice-ufrag:e5785931\r\n"
184 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
185 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
186 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
187 "m=audio 1 RTP/AVPF 103\r\n"
188 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700189 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700190 "a=sendonly\r\n"
191 "a=rtpmap:103 ISAC/16000\r\n"
192 "m=video 1 RTP/AVPF 120\r\n"
193 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700194 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700195 "a=sendonly\r\n"
196 "a=rtpmap:120 VP8/90000\r\n";
197
198static const char kSdpStringInit[] =
199 "v=0\r\n"
200 "o=- 0 0 IN IP4 127.0.0.1\r\n"
201 "s=-\r\n"
202 "t=0 0\r\n"
203 "a=ice-ufrag:e5785931\r\n"
204 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
205 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
206 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
207 "a=msid-semantic: WMS\r\n";
208
209static const char kSdpStringAudio[] =
210 "m=audio 1 RTP/AVPF 103\r\n"
211 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700212 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700213 "a=rtpmap:103 ISAC/16000\r\n";
214
215static const char kSdpStringVideo[] =
216 "m=video 1 RTP/AVPF 120\r\n"
217 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700218 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700219 "a=rtpmap:120 VP8/90000\r\n";
220
221static const char kSdpStringMs1Audio0[] =
222 "a=ssrc:1 cname:stream1\r\n"
223 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
224
225static const char kSdpStringMs1Video0[] =
226 "a=ssrc:2 cname:stream1\r\n"
227 "a=ssrc:2 msid:stream1 videotrack0\r\n";
228
229static const char kSdpStringMs1Audio1[] =
230 "a=ssrc:3 cname:stream1\r\n"
231 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
232
233static const char kSdpStringMs1Video1[] =
234 "a=ssrc:4 cname:stream1\r\n"
235 "a=ssrc:4 msid:stream1 videotrack1\r\n";
236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237#define MAYBE_SKIP_TEST(feature) \
238 if (!(feature())) { \
239 LOG(LS_INFO) << "Feature disabled... skipping"; \
240 return; \
241 }
242
perkjd61bf802016-03-24 03:16:19 -0700243using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700244using cricket::StreamParams;
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);
zhihuang29ff8442016-07-27 11:07:25 -0700494 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 }
perkjdfb769d2016-02-09 03:09:43 -0800496 void OnIceGatheringChange(
497 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800499 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang29ff8442016-07-27 11:07:25 -0700500 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 }
perkjdfb769d2016-02-09 03:09:43 -0800502 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
504 pc_->ice_gathering_state());
505
506 std::string sdp;
507 EXPECT_TRUE(candidate->ToString(&sdp));
508 EXPECT_LT(0u, sdp.size());
509 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
510 candidate->sdp_mline_index(), sdp, NULL));
511 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang29ff8442016-07-27 11:07:25 -0700512 callback_triggered = true;
513 }
514
515 void OnIceCandidatesRemoved(
516 const std::vector<cricket::Candidate>& candidates) override {
517 callback_triggered = true;
518 }
519
520 void OnIceConnectionReceivingChange(bool receiving) override {
521 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
524 // Returns the label of the last added stream.
525 // Empty string if no stream have been added.
526 std::string GetLastAddedStreamLabel() {
527 if (last_added_stream_.get())
528 return last_added_stream_->label();
529 return "";
530 }
531 std::string GetLastRemovedStreamLabel() {
532 if (last_removed_stream_.get())
533 return last_removed_stream_->label();
534 return "";
535 }
536
zhihuang9763d562016-08-05 11:14:50 -0700537 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700539 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700540 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700541 rtc::scoped_refptr<StreamCollection> remote_streams_;
542 bool renegotiation_needed_ = false;
543 bool ice_complete_ = false;
zhihuang29ff8442016-07-27 11:07:25 -0700544 bool callback_triggered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
546 private:
zhihuang9763d562016-08-05 11:14:50 -0700547 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
548 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549};
550
551} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700552
zhihuang29ff8442016-07-27 11:07:25 -0700553// The PeerConnectionMediaConfig tests below verify that configuration
554// and constraints are propagated into the MediaConfig passed to
555// CreateMediaController. These settings are intended for MediaChannel
556// constructors, but that is not exercised by these unittest.
557class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
558 public:
559 webrtc::MediaControllerInterface* CreateMediaController(
560 const cricket::MediaConfig& config) const override {
561 create_media_controller_called_ = true;
562 create_media_controller_config_ = config;
563
564 webrtc::MediaControllerInterface* mc =
565 PeerConnectionFactory::CreateMediaController(config);
566 EXPECT_TRUE(mc != nullptr);
567 return mc;
568 }
569
570 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700571 cricket::PortAllocator* port_allocator,
572 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700573 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700574 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
575 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700576 return transport_controller;
577 }
578
579 cricket::TransportController* transport_controller;
580 // Mutable, so they can be modified in the above const-declared method.
581 mutable bool create_media_controller_called_ = false;
582 mutable cricket::MediaConfig create_media_controller_config_;
583};
584
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585class PeerConnectionInterfaceTest : public testing::Test {
586 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800587 PeerConnectionInterfaceTest() {
588#ifdef WEBRTC_ANDROID
589 webrtc::InitializeAndroidObjects();
590#endif
591 }
592
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 virtual void SetUp() {
594 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700595 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
596 nullptr, nullptr, nullptr);
597 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700598 pc_factory_for_test_ =
599 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
600 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 }
602
603 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700604 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606
607 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700608 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
609 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
611
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700612 void CreatePeerConnectionWithIceTransportsType(
613 PeerConnectionInterface::IceTransportsType type) {
614 PeerConnectionInterface::RTCConfiguration config;
615 config.type = type;
616 return CreatePeerConnection(config, nullptr);
617 }
618
619 void CreatePeerConnectionWithIceServer(const std::string& uri,
620 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800621 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700623 server.uri = uri;
624 server.password = password;
625 config.servers.push_back(server);
626 CreatePeerConnection(config, nullptr);
627 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700629 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
630 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700631 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800632 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
633 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000634
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000635 // DTLS does not work in a loopback call, so is disabled for most of the
636 // tests in this file. We only create a FakeIdentityService if the test
637 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000638 FakeConstraints default_constraints;
639 if (!constraints) {
640 constraints = &default_constraints;
641
642 default_constraints.AddMandatory(
643 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
644 }
645
Henrik Boströmd79599d2016-06-01 13:58:50 +0200646 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000647 bool dtls;
648 if (FindConstraint(constraints,
649 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
650 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200651 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200652 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000653 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200654 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800655 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200656 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 ASSERT_TRUE(pc_.get() != NULL);
658 observer_.SetPeerConnectionInterface(pc_.get());
659 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
660 }
661
deadbeef0a6c4ca2015-10-06 11:38:28 -0700662 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800663 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700664 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700665 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800666 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700667
zhihuang9763d562016-08-05 11:14:50 -0700668 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700669 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
670 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800671 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700672 }
673
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700675 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800676 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
677 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
678 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800680 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
deadbeef0a6c4ca2015-10-06 11:38:28 -0700682 CreatePeerConnectionExpectFail(kStunInvalidPort);
683 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
684 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700686 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800687 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
688 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800690 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800694 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696
697 void ReleasePeerConnection() {
698 pc_ = NULL;
699 observer_.SetPeerConnectionInterface(NULL);
700 }
701
deadbeefab9b2d12015-10-14 11:33:11 -0700702 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700704 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700706 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700708 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 pc_factory_->CreateVideoTrack(label + "v0", video_source));
710 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000711 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
713 observer_.renegotiation_needed_ = false;
714 }
715
716 void AddVoiceStream(const std::string& label) {
717 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700718 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700720 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 pc_factory_->CreateAudioTrack(label + "a0", NULL));
722 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000723 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
725 observer_.renegotiation_needed_ = false;
726 }
727
728 void AddAudioVideoStream(const std::string& stream_label,
729 const std::string& audio_track_label,
730 const std::string& video_track_label) {
731 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700732 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700734 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 pc_factory_->CreateAudioTrack(
736 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
737 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700738 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700739 pc_factory_->CreateVideoTrack(
740 video_track_label,
741 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000743 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
745 observer_.renegotiation_needed_ = false;
746 }
747
kwibergd1fe2812016-04-27 06:47:29 -0700748 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700749 bool offer,
750 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000751 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
752 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 MockCreateSessionDescriptionObserver>());
754 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700755 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700757 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700760 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 return observer->result();
762 }
763
kwibergd1fe2812016-04-27 06:47:29 -0700764 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700765 MediaConstraintsInterface* constraints) {
766 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768
kwibergd1fe2812016-04-27 06:47:29 -0700769 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700770 MediaConstraintsInterface* constraints) {
771 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 }
773
774 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
776 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 MockSetSessionDescriptionObserver>());
778 if (local) {
779 pc_->SetLocalDescription(observer, desc);
780 } else {
781 pc_->SetRemoteDescription(observer, desc);
782 }
zhihuang29ff8442016-07-27 11:07:25 -0700783 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
784 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
785 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 return observer->result();
787 }
788
789 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
790 return DoSetSessionDescription(desc, true);
791 }
792
793 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
794 return DoSetSessionDescription(desc, false);
795 }
796
797 // Calls PeerConnection::GetStats and check the return value.
798 // It does not verify the values in the StatReports since a RTCP packet might
799 // be required.
800 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000801 rtc::scoped_refptr<MockStatsObserver> observer(
802 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000803 if (!pc_->GetStats(
804 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 return false;
806 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
807 return observer->called();
808 }
809
810 void InitiateCall() {
811 CreatePeerConnection();
812 // Create a local stream with audio&video tracks.
813 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
814 CreateOfferReceiveAnswer();
815 }
816
817 // Verify that RTP Header extensions has been negotiated for audio and video.
818 void VerifyRemoteRtpHeaderExtensions() {
819 const cricket::MediaContentDescription* desc =
820 cricket::GetFirstAudioContentDescription(
821 pc_->remote_description()->description());
822 ASSERT_TRUE(desc != NULL);
823 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
824
825 desc = cricket::GetFirstVideoContentDescription(
826 pc_->remote_description()->description());
827 ASSERT_TRUE(desc != NULL);
828 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
829 }
830
831 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700832 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700833 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 std::string sdp;
835 EXPECT_TRUE(offer->ToString(&sdp));
836 SessionDescriptionInterface* remote_offer =
837 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
838 sdp, NULL);
839 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
840 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
841 }
842
deadbeefab9b2d12015-10-14 11:33:11 -0700843 void CreateAndSetRemoteOffer(const std::string& sdp) {
844 SessionDescriptionInterface* remote_offer =
845 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
846 sdp, nullptr);
847 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
848 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
849 }
850
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700852 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700853 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854
855 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
856 // audio codec change, even if the parameter has nothing to do with
857 // receiving. Not all parameters are serialized to SDP.
858 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
859 // the SessionDescription, it is necessary to do that here to in order to
860 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
861 // https://code.google.com/p/webrtc/issues/detail?id=1356
862 std::string sdp;
863 EXPECT_TRUE(answer->ToString(&sdp));
864 SessionDescriptionInterface* new_answer =
865 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
866 sdp, NULL);
867 EXPECT_TRUE(DoSetLocalDescription(new_answer));
868 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
869 }
870
871 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700872 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700873 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874
875 std::string sdp;
876 EXPECT_TRUE(answer->ToString(&sdp));
877 SessionDescriptionInterface* pr_answer =
878 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
879 sdp, NULL);
880 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
881 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
882 }
883
884 void CreateOfferReceiveAnswer() {
885 CreateOfferAsLocalDescription();
886 std::string sdp;
887 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
888 CreateAnswerAsRemoteDescription(sdp);
889 }
890
891 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700892 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700893 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
895 // audio codec change, even if the parameter has nothing to do with
896 // receiving. Not all parameters are serialized to SDP.
897 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
898 // the SessionDescription, it is necessary to do that here to in order to
899 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
900 // https://code.google.com/p/webrtc/issues/detail?id=1356
901 std::string sdp;
902 EXPECT_TRUE(offer->ToString(&sdp));
903 SessionDescriptionInterface* new_offer =
904 webrtc::CreateSessionDescription(
905 SessionDescriptionInterface::kOffer,
906 sdp, NULL);
907
908 EXPECT_TRUE(DoSetLocalDescription(new_offer));
909 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000910 // Wait for the ice_complete message, so that SDP will have candidates.
911 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
913
deadbeefab9b2d12015-10-14 11:33:11 -0700914 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
916 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700917 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 EXPECT_TRUE(DoSetRemoteDescription(answer));
919 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
920 }
921
deadbeefab9b2d12015-10-14 11:33:11 -0700922 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 webrtc::JsepSessionDescription* pr_answer =
924 new webrtc::JsepSessionDescription(
925 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700926 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
928 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
929 webrtc::JsepSessionDescription* answer =
930 new webrtc::JsepSessionDescription(
931 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700932 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 EXPECT_TRUE(DoSetRemoteDescription(answer));
934 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
935 }
936
937 // Help function used for waiting until a the last signaled remote stream has
938 // the same label as |stream_label|. In a few of the tests in this file we
939 // answer with the same session description as we offer and thus we can
940 // check if OnAddStream have been called with the same stream as we offer to
941 // send.
942 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
943 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
944 }
945
946 // Creates an offer and applies it as a local session description.
947 // Creates an answer with the same SDP an the offer but removes all lines
948 // that start with a:ssrc"
949 void CreateOfferReceiveAnswerWithoutSsrc() {
950 CreateOfferAsLocalDescription();
951 std::string sdp;
952 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
953 SetSsrcToZero(&sdp);
954 CreateAnswerAsRemoteDescription(sdp);
955 }
956
deadbeefab9b2d12015-10-14 11:33:11 -0700957 // This function creates a MediaStream with label kStreams[0] and
958 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
959 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700960 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700961 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700962 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700963 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
964 size_t number_of_video_tracks) {
965 EXPECT_LE(number_of_audio_tracks, 2u);
966 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700967
968 reference_collection_ = StreamCollection::Create();
969 std::string sdp_ms1 = std::string(kSdpStringInit);
970
971 std::string mediastream_label = kStreams[0];
972
973 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
974 webrtc::MediaStream::Create(mediastream_label));
975 reference_collection_->AddStream(stream);
976
977 if (number_of_audio_tracks > 0) {
978 sdp_ms1 += std::string(kSdpStringAudio);
979 sdp_ms1 += std::string(kSdpStringMs1Audio0);
980 AddAudioTrack(kAudioTracks[0], stream);
981 }
982 if (number_of_audio_tracks > 1) {
983 sdp_ms1 += kSdpStringMs1Audio1;
984 AddAudioTrack(kAudioTracks[1], stream);
985 }
986
987 if (number_of_video_tracks > 0) {
988 sdp_ms1 += std::string(kSdpStringVideo);
989 sdp_ms1 += std::string(kSdpStringMs1Video0);
990 AddVideoTrack(kVideoTracks[0], stream);
991 }
992 if (number_of_video_tracks > 1) {
993 sdp_ms1 += kSdpStringMs1Video1;
994 AddVideoTrack(kVideoTracks[1], stream);
995 }
996
kwibergd1fe2812016-04-27 06:47:29 -0700997 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700998 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
999 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001000 }
1001
1002 void AddAudioTrack(const std::string& track_id,
1003 MediaStreamInterface* stream) {
1004 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1005 webrtc::AudioTrack::Create(track_id, nullptr));
1006 ASSERT_TRUE(stream->AddTrack(audio_track));
1007 }
1008
1009 void AddVideoTrack(const std::string& track_id,
1010 MediaStreamInterface* stream) {
1011 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001012 webrtc::VideoTrack::Create(track_id,
1013 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001014 ASSERT_TRUE(stream->AddTrack(video_track));
1015 }
1016
kwibergfd8be342016-05-14 19:44:11 -07001017 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001018 CreatePeerConnection();
1019 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001020 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001021 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1022 return offer;
1023 }
1024
kwibergfd8be342016-05-14 19:44:11 -07001025 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001026 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001027 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001028 CreateOfferWithOneAudioStream();
1029 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001030 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001031 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1032 return answer;
1033 }
1034
1035 const std::string& GetFirstAudioStreamCname(
1036 const SessionDescriptionInterface* desc) {
1037 const cricket::ContentInfo* audio_content =
1038 cricket::GetFirstAudioContent(desc->description());
1039 const cricket::AudioContentDescription* audio_desc =
1040 static_cast<const cricket::AudioContentDescription*>(
1041 audio_content->description);
1042 return audio_desc->streams()[0].cname;
1043 }
1044
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001045 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001046 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1047 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1048 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001050 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051};
1052
zhihuang29ff8442016-07-27 11:07:25 -07001053// Test that no callbacks on the PeerConnectionObserver are called after the
1054// PeerConnection is closed.
1055TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001056 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001057 pc_factory_for_test_->CreatePeerConnection(
1058 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1059 nullptr, &observer_));
1060 observer_.SetPeerConnectionInterface(pc.get());
1061 pc->Close();
1062
1063 // No callbacks is expected to be called.
1064 observer_.callback_triggered = false;
1065 std::vector<cricket::Candidate> candidates;
1066 pc_factory_for_test_->transport_controller->SignalGatheringState(
1067 cricket::IceGatheringState{});
1068 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1069 "", candidates);
1070 pc_factory_for_test_->transport_controller->SignalConnectionState(
1071 cricket::IceConnectionState{});
1072 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1073 candidates);
1074 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1075 EXPECT_FALSE(observer_.callback_triggered);
1076}
1077
zhihuang8f65cdf2016-05-06 18:40:30 -07001078// Generate different CNAMEs when PeerConnections are created.
1079// The CNAMEs are expected to be generated randomly. It is possible
1080// that the test fails, though the possibility is very low.
1081TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001082 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001083 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001084 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001085 CreateOfferWithOneAudioStream();
1086 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1087 GetFirstAudioStreamCname(offer2.get()));
1088}
1089
1090TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001091 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001092 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001093 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001094 CreateAnswerWithOneAudioStream();
1095 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1096 GetFirstAudioStreamCname(answer2.get()));
1097}
1098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099TEST_F(PeerConnectionInterfaceTest,
1100 CreatePeerConnectionWithDifferentConfigurations) {
1101 CreatePeerConnectionWithDifferentConfigurations();
1102}
1103
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001104TEST_F(PeerConnectionInterfaceTest,
1105 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1106 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1107 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1108 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1109 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1110 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1111 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1112 port_allocator_->candidate_filter());
1113 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1114 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1115}
1116
1117// Test that when a PeerConnection is created with a nonzero candidate pool
1118// size, the pooled PortAllocatorSession is created with all the attributes
1119// in the RTCConfiguration.
1120TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1121 PeerConnectionInterface::RTCConfiguration config;
1122 PeerConnectionInterface::IceServer server;
1123 server.uri = kStunAddressOnly;
1124 config.servers.push_back(server);
1125 config.type = PeerConnectionInterface::kRelay;
1126 config.disable_ipv6 = true;
1127 config.tcp_candidate_policy =
1128 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001129 config.candidate_network_policy =
1130 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001131 config.ice_candidate_pool_size = 1;
1132 CreatePeerConnection(config, nullptr);
1133
1134 const cricket::FakePortAllocatorSession* session =
1135 static_cast<const cricket::FakePortAllocatorSession*>(
1136 port_allocator_->GetPooledSession());
1137 ASSERT_NE(nullptr, session);
1138 EXPECT_EQ(1UL, session->stun_servers().size());
1139 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1140 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001141 EXPECT_LT(0U,
1142 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001143}
1144
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001145// Test that the PeerConnection initializes the port allocator passed into it,
1146// and on the correct thread.
1147TEST_F(PeerConnectionInterfaceTest,
1148 CreatePeerConnectionInitializesPortAllocator) {
1149 rtc::Thread network_thread;
1150 network_thread.Start();
1151 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1152 webrtc::CreatePeerConnectionFactory(
1153 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1154 nullptr, nullptr, nullptr));
1155 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1156 new cricket::FakePortAllocator(&network_thread, nullptr));
1157 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1158 PeerConnectionInterface::RTCConfiguration config;
1159 rtc::scoped_refptr<PeerConnectionInterface> pc(
1160 pc_factory->CreatePeerConnection(
1161 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1162 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1163 // so all we have to do here is check that it's initialized.
1164 EXPECT_TRUE(raw_port_allocator->initialized());
1165}
1166
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1168 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001169 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 AddVoiceStream(kStreamLabel2);
1171 ASSERT_EQ(2u, pc_->local_streams()->count());
1172
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001173 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001174 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001176 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1177 pc_factory_->CreateAudioTrack(kStreamLabel3,
1178 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001180 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001181 EXPECT_EQ(3u, pc_->local_streams()->count());
1182
1183 // Remove the third stream.
1184 pc_->RemoveStream(pc_->local_streams()->at(2));
1185 EXPECT_EQ(2u, pc_->local_streams()->count());
1186
1187 // Remove the second stream.
1188 pc_->RemoveStream(pc_->local_streams()->at(1));
1189 EXPECT_EQ(1u, pc_->local_streams()->count());
1190
1191 // Remove the first stream.
1192 pc_->RemoveStream(pc_->local_streams()->at(0));
1193 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194}
1195
deadbeefab9b2d12015-10-14 11:33:11 -07001196// Test that the created offer includes streams we added.
1197TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1198 CreatePeerConnection();
1199 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001200 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001201 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001202
1203 const cricket::ContentInfo* audio_content =
1204 cricket::GetFirstAudioContent(offer->description());
1205 const cricket::AudioContentDescription* audio_desc =
1206 static_cast<const cricket::AudioContentDescription*>(
1207 audio_content->description);
1208 EXPECT_TRUE(
1209 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1210
1211 const cricket::ContentInfo* video_content =
1212 cricket::GetFirstVideoContent(offer->description());
1213 const cricket::VideoContentDescription* video_desc =
1214 static_cast<const cricket::VideoContentDescription*>(
1215 video_content->description);
1216 EXPECT_TRUE(
1217 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1218
1219 // Add another stream and ensure the offer includes both the old and new
1220 // streams.
1221 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001222 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001223
1224 audio_content = cricket::GetFirstAudioContent(offer->description());
1225 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1226 audio_content->description);
1227 EXPECT_TRUE(
1228 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1229 EXPECT_TRUE(
1230 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1231
1232 video_content = cricket::GetFirstVideoContent(offer->description());
1233 video_desc = static_cast<const cricket::VideoContentDescription*>(
1234 video_content->description);
1235 EXPECT_TRUE(
1236 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1237 EXPECT_TRUE(
1238 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1239}
1240
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1242 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001243 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 ASSERT_EQ(1u, pc_->local_streams()->count());
1245 pc_->RemoveStream(pc_->local_streams()->at(0));
1246 EXPECT_EQ(0u, pc_->local_streams()->count());
1247}
1248
deadbeefe1f9d832016-01-14 15:35:42 -08001249// Test for AddTrack and RemoveTrack methods.
1250// Tests that the created offer includes tracks we added,
1251// and that the RtpSenders are created correctly.
1252// Also tests that RemoveTrack removes the tracks from subsequent offers.
1253TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1254 CreatePeerConnection();
1255 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001256 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001257 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1258 std::vector<MediaStreamInterface*> stream_list;
1259 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001260 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001261 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001262 rtc::scoped_refptr<VideoTrackInterface> video_track(
1263 pc_factory_->CreateVideoTrack(
1264 "video_track",
1265 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001266 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1267 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001268 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1269 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001270 EXPECT_EQ("audio_track", audio_sender->id());
1271 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001272 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1273 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001274 EXPECT_EQ("video_track", video_sender->id());
1275 EXPECT_EQ(video_track, video_sender->track());
1276
1277 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001278 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001279 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001280
1281 const cricket::ContentInfo* audio_content =
1282 cricket::GetFirstAudioContent(offer->description());
1283 const cricket::AudioContentDescription* audio_desc =
1284 static_cast<const cricket::AudioContentDescription*>(
1285 audio_content->description);
1286 EXPECT_TRUE(
1287 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1288
1289 const cricket::ContentInfo* video_content =
1290 cricket::GetFirstVideoContent(offer->description());
1291 const cricket::VideoContentDescription* video_desc =
1292 static_cast<const cricket::VideoContentDescription*>(
1293 video_content->description);
1294 EXPECT_TRUE(
1295 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1296
1297 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1298
1299 // Now try removing the tracks.
1300 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1301 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1302
1303 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001304 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001305
1306 audio_content = cricket::GetFirstAudioContent(offer->description());
1307 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1308 audio_content->description);
1309 EXPECT_FALSE(
1310 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1311
1312 video_content = cricket::GetFirstVideoContent(offer->description());
1313 video_desc = static_cast<const cricket::VideoContentDescription*>(
1314 video_content->description);
1315 EXPECT_FALSE(
1316 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1317
1318 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1319
1320 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1321 // should return false.
1322 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1323 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1324}
1325
1326// Test creating senders without a stream specified,
1327// expecting a random stream ID to be generated.
1328TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1329 CreatePeerConnection();
1330 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001331 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001332 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001333 rtc::scoped_refptr<VideoTrackInterface> video_track(
1334 pc_factory_->CreateVideoTrack(
1335 "video_track",
1336 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001337 auto audio_sender =
1338 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1339 auto video_sender =
1340 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1341 EXPECT_EQ("audio_track", audio_sender->id());
1342 EXPECT_EQ(audio_track, audio_sender->track());
1343 EXPECT_EQ("video_track", video_sender->id());
1344 EXPECT_EQ(video_track, video_sender->track());
1345 // If the ID is truly a random GUID, it should be infinitely unlikely they
1346 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001347 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001348}
1349
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1351 InitiateCall();
1352 WaitAndVerifyOnAddStream(kStreamLabel1);
1353 VerifyRemoteRtpHeaderExtensions();
1354}
1355
1356TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1357 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001358 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 CreateOfferAsLocalDescription();
1360 std::string offer;
1361 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1362 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1363 WaitAndVerifyOnAddStream(kStreamLabel1);
1364}
1365
1366TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1367 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001368 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369
1370 CreateOfferAsRemoteDescription();
1371 CreateAnswerAsLocalDescription();
1372
1373 WaitAndVerifyOnAddStream(kStreamLabel1);
1374}
1375
1376TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1377 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001378 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379
1380 CreateOfferAsRemoteDescription();
1381 CreatePrAnswerAsLocalDescription();
1382 CreateAnswerAsLocalDescription();
1383
1384 WaitAndVerifyOnAddStream(kStreamLabel1);
1385}
1386
1387TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1388 InitiateCall();
1389 ASSERT_EQ(1u, pc_->remote_streams()->count());
1390 pc_->RemoveStream(pc_->local_streams()->at(0));
1391 CreateOfferReceiveAnswer();
1392 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001393 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 CreateOfferReceiveAnswer();
1395}
1396
1397// Tests that after negotiating an audio only call, the respondent can perform a
1398// renegotiation that removes the audio stream.
1399TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1400 CreatePeerConnection();
1401 AddVoiceStream(kStreamLabel1);
1402 CreateOfferAsRemoteDescription();
1403 CreateAnswerAsLocalDescription();
1404
1405 ASSERT_EQ(1u, pc_->remote_streams()->count());
1406 pc_->RemoveStream(pc_->local_streams()->at(0));
1407 CreateOfferReceiveAnswer();
1408 EXPECT_EQ(0u, pc_->remote_streams()->count());
1409}
1410
1411// Test that candidates are generated and that we can parse our own candidates.
1412TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1413 CreatePeerConnection();
1414
1415 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1416 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001417 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001418 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001419 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001420 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421
1422 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001423 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001424 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001425 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426
1427 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1428 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1429
1430 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1431}
1432
deadbeefab9b2d12015-10-14 11:33:11 -07001433// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434// not unique.
1435TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1436 CreatePeerConnection();
1437 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001438 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001439 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001440 EXPECT_TRUE(offer);
1441 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442
1443 // Create a local stream with audio&video tracks having same label.
1444 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1445
1446 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001447 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448
1449 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001450 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001451 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452}
1453
1454// Test that we will get different SSRCs for each tracks in the offer and answer
1455// we created.
1456TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1457 CreatePeerConnection();
1458 // Create a local stream with audio&video tracks having different labels.
1459 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1460
1461 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001462 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001463 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 int audio_ssrc = 0;
1465 int video_ssrc = 0;
1466 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1467 &audio_ssrc));
1468 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1469 &video_ssrc));
1470 EXPECT_NE(audio_ssrc, video_ssrc);
1471
1472 // Test CreateAnswer
1473 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001474 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001475 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 audio_ssrc = 0;
1477 video_ssrc = 0;
1478 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1479 &audio_ssrc));
1480 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1481 &video_ssrc));
1482 EXPECT_NE(audio_ssrc, video_ssrc);
1483}
1484
deadbeefeb459812015-12-15 19:24:43 -08001485// Test that it's possible to call AddTrack on a MediaStream after adding
1486// the stream to a PeerConnection.
1487// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1488TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1489 CreatePeerConnection();
1490 // Create audio stream and add to PeerConnection.
1491 AddVoiceStream(kStreamLabel1);
1492 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1493
1494 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001495 rtc::scoped_refptr<VideoTrackInterface> video_track(
1496 pc_factory_->CreateVideoTrack(
1497 "video_label",
1498 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001499 stream->AddTrack(video_track.get());
1500
kwibergd1fe2812016-04-27 06:47:29 -07001501 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001502 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001503
1504 const cricket::MediaContentDescription* video_desc =
1505 cricket::GetFirstVideoContentDescription(offer->description());
1506 EXPECT_TRUE(video_desc != nullptr);
1507}
1508
1509// Test that it's possible to call RemoveTrack on a MediaStream after adding
1510// the stream to a PeerConnection.
1511// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1512TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1513 CreatePeerConnection();
1514 // Create audio/video stream and add to PeerConnection.
1515 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1516 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1517
1518 // Remove the video track.
1519 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1520
kwibergd1fe2812016-04-27 06:47:29 -07001521 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001522 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001523
1524 const cricket::MediaContentDescription* video_desc =
1525 cricket::GetFirstVideoContentDescription(offer->description());
1526 EXPECT_TRUE(video_desc == nullptr);
1527}
1528
deadbeefbd7d8f72015-12-18 16:58:44 -08001529// Test creating a sender with a stream ID, and ensure the ID is populated
1530// in the offer.
1531TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1532 CreatePeerConnection();
1533 pc_->CreateSender("video", kStreamLabel1);
1534
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001536 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001537
1538 const cricket::MediaContentDescription* video_desc =
1539 cricket::GetFirstVideoContentDescription(offer->description());
1540 ASSERT_TRUE(video_desc != nullptr);
1541 ASSERT_EQ(1u, video_desc->streams().size());
1542 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1543}
1544
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545// Test that we can specify a certain track that we want statistics about.
1546TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1547 InitiateCall();
1548 ASSERT_LT(0u, pc_->remote_streams()->count());
1549 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001550 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1552 EXPECT_TRUE(DoGetStats(remote_audio));
1553
1554 // Remove the stream. Since we are sending to our selves the local
1555 // and the remote stream is the same.
1556 pc_->RemoveStream(pc_->local_streams()->at(0));
1557 // Do a re-negotiation.
1558 CreateOfferReceiveAnswer();
1559
1560 ASSERT_EQ(0u, pc_->remote_streams()->count());
1561
1562 // Test that we still can get statistics for the old track. Even if it is not
1563 // sent any longer.
1564 EXPECT_TRUE(DoGetStats(remote_audio));
1565}
1566
1567// Test that we can get stats on a video track.
1568TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1569 InitiateCall();
1570 ASSERT_LT(0u, pc_->remote_streams()->count());
1571 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001572 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1574 EXPECT_TRUE(DoGetStats(remote_video));
1575}
1576
1577// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001578// TODO(tommi): Fix this test. DoGetStats will return true
1579// for the unknown track (since GetStats is async), but no
1580// data is returned for the track.
1581TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001583 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 pc_factory_->CreateAudioTrack("unknown track", NULL));
1585 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1586}
1587
1588// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 FakeConstraints constraints;
1591 constraints.SetAllowRtpDataChannels();
1592 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001593 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001595 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 pc_->CreateDataChannel("test2", NULL);
1597 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001600 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 new MockDataChannelObserver(data2));
1602
1603 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1604 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1605 std::string data_to_send1 = "testing testing";
1606 std::string data_to_send2 = "testing something else";
1607 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1608
1609 CreateOfferReceiveAnswer();
1610 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1611 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1612
1613 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1614 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1615 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1616 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1617
1618 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1619 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1620
1621 data1->Close();
1622 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1623 CreateOfferReceiveAnswer();
1624 EXPECT_FALSE(observer1->IsOpen());
1625 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1626 EXPECT_TRUE(observer2->IsOpen());
1627
1628 data_to_send2 = "testing something else again";
1629 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1630
1631 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1632}
1633
1634// This test verifies that sendnig binary data over RTP data channels should
1635// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 FakeConstraints constraints;
1638 constraints.SetAllowRtpDataChannels();
1639 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001640 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001642 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 pc_->CreateDataChannel("test2", NULL);
1644 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001645 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 new MockDataChannelObserver(data2));
1649
1650 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1651 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1652
1653 CreateOfferReceiveAnswer();
1654 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1655 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1656
1657 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1658 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1659
jbaucheec21bd2016-03-20 06:15:43 -07001660 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1662}
1663
1664// This test setup a RTP data channels in loop back and test that a channel is
1665// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 FakeConstraints constraints;
1668 constraints.SetAllowRtpDataChannels();
1669 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001670 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001672 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 new MockDataChannelObserver(data1));
1674
1675 CreateOfferReceiveAnswerWithoutSsrc();
1676
1677 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1678
1679 data1->Close();
1680 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1681 CreateOfferReceiveAnswerWithoutSsrc();
1682 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1683 EXPECT_FALSE(observer1->IsOpen());
1684}
1685
1686// This test that if a data channel is added in an answer a receive only channel
1687// channel is created.
1688TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1689 FakeConstraints constraints;
1690 constraints.SetAllowRtpDataChannels();
1691 CreatePeerConnection(&constraints);
1692
1693 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001694 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 pc_->CreateDataChannel(offer_label, NULL);
1696
1697 CreateOfferAsLocalDescription();
1698
1699 // Replace the data channel label in the offer and apply it as an answer.
1700 std::string receive_label = "answer_channel";
1701 std::string sdp;
1702 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001703 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 receive_label.c_str(), receive_label.length(),
1705 &sdp);
1706 CreateAnswerAsRemoteDescription(sdp);
1707
1708 // Verify that a new incoming data channel has been created and that
1709 // it is open but can't we written to.
1710 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1711 DataChannelInterface* received_channel = observer_.last_datachannel_;
1712 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1713 EXPECT_EQ(receive_label, received_channel->label());
1714 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1715
1716 // Verify that the channel we initially offered has been rejected.
1717 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1718
1719 // Do another offer / answer exchange and verify that the data channel is
1720 // opened.
1721 CreateOfferReceiveAnswer();
1722 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1723 kTimeout);
1724}
1725
1726// This test that no data channel is returned if a reliable channel is
1727// requested.
1728// TODO(perkj): Remove this test once reliable channels are implemented.
1729TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1730 FakeConstraints constraints;
1731 constraints.SetAllowRtpDataChannels();
1732 CreatePeerConnection(&constraints);
1733
1734 std::string label = "test";
1735 webrtc::DataChannelInit config;
1736 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001737 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 pc_->CreateDataChannel(label, &config);
1739 EXPECT_TRUE(channel == NULL);
1740}
1741
deadbeefab9b2d12015-10-14 11:33:11 -07001742// Verifies that duplicated label is not allowed for RTP data channel.
1743TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1744 FakeConstraints constraints;
1745 constraints.SetAllowRtpDataChannels();
1746 CreatePeerConnection(&constraints);
1747
1748 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001749 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001750 pc_->CreateDataChannel(label, nullptr);
1751 EXPECT_NE(channel, nullptr);
1752
zhihuang9763d562016-08-05 11:14:50 -07001753 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001754 pc_->CreateDataChannel(label, nullptr);
1755 EXPECT_EQ(dup_channel, nullptr);
1756}
1757
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758// This tests that a SCTP data channel is returned using different
1759// DataChannelInit configurations.
1760TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1761 FakeConstraints constraints;
1762 constraints.SetAllowDtlsSctpDataChannels();
1763 CreatePeerConnection(&constraints);
1764
1765 webrtc::DataChannelInit config;
1766
zhihuang9763d562016-08-05 11:14:50 -07001767 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 pc_->CreateDataChannel("1", &config);
1769 EXPECT_TRUE(channel != NULL);
1770 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001771 EXPECT_TRUE(observer_.renegotiation_needed_);
1772 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773
1774 config.ordered = false;
1775 channel = pc_->CreateDataChannel("2", &config);
1776 EXPECT_TRUE(channel != NULL);
1777 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001778 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779
1780 config.ordered = true;
1781 config.maxRetransmits = 0;
1782 channel = pc_->CreateDataChannel("3", &config);
1783 EXPECT_TRUE(channel != NULL);
1784 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001785 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786
1787 config.maxRetransmits = -1;
1788 config.maxRetransmitTime = 0;
1789 channel = pc_->CreateDataChannel("4", &config);
1790 EXPECT_TRUE(channel != NULL);
1791 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001792 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793}
1794
1795// This tests that no data channel is returned if both maxRetransmits and
1796// maxRetransmitTime are set for SCTP data channels.
1797TEST_F(PeerConnectionInterfaceTest,
1798 CreateSctpDataChannelShouldFailForInvalidConfig) {
1799 FakeConstraints constraints;
1800 constraints.SetAllowDtlsSctpDataChannels();
1801 CreatePeerConnection(&constraints);
1802
1803 std::string label = "test";
1804 webrtc::DataChannelInit config;
1805 config.maxRetransmits = 0;
1806 config.maxRetransmitTime = 0;
1807
zhihuang9763d562016-08-05 11:14:50 -07001808 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 pc_->CreateDataChannel(label, &config);
1810 EXPECT_TRUE(channel == NULL);
1811}
1812
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813// The test verifies that creating a SCTP data channel with an id already in use
1814// or out of range should fail.
1815TEST_F(PeerConnectionInterfaceTest,
1816 CreateSctpDataChannelWithInvalidIdShouldFail) {
1817 FakeConstraints constraints;
1818 constraints.SetAllowDtlsSctpDataChannels();
1819 CreatePeerConnection(&constraints);
1820
1821 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001822 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001824 config.id = 1;
1825 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 EXPECT_TRUE(channel != NULL);
1827 EXPECT_EQ(1, channel->id());
1828
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 channel = pc_->CreateDataChannel("x", &config);
1830 EXPECT_TRUE(channel == NULL);
1831
1832 config.id = cricket::kMaxSctpSid;
1833 channel = pc_->CreateDataChannel("max", &config);
1834 EXPECT_TRUE(channel != NULL);
1835 EXPECT_EQ(config.id, channel->id());
1836
1837 config.id = cricket::kMaxSctpSid + 1;
1838 channel = pc_->CreateDataChannel("x", &config);
1839 EXPECT_TRUE(channel == NULL);
1840}
1841
deadbeefab9b2d12015-10-14 11:33:11 -07001842// Verifies that duplicated label is allowed for SCTP data channel.
1843TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1844 FakeConstraints constraints;
1845 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1846 true);
1847 CreatePeerConnection(&constraints);
1848
1849 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001850 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001851 pc_->CreateDataChannel(label, nullptr);
1852 EXPECT_NE(channel, nullptr);
1853
zhihuang9763d562016-08-05 11:14:50 -07001854 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001855 pc_->CreateDataChannel(label, nullptr);
1856 EXPECT_NE(dup_channel, nullptr);
1857}
1858
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001859// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1860// DataChannel.
1861TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1862 FakeConstraints constraints;
1863 constraints.SetAllowRtpDataChannels();
1864 CreatePeerConnection(&constraints);
1865
zhihuang9763d562016-08-05 11:14:50 -07001866 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001867 pc_->CreateDataChannel("test1", NULL);
1868 EXPECT_TRUE(observer_.renegotiation_needed_);
1869 observer_.renegotiation_needed_ = false;
1870
zhihuang9763d562016-08-05 11:14:50 -07001871 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001872 pc_->CreateDataChannel("test2", NULL);
1873 EXPECT_TRUE(observer_.renegotiation_needed_);
1874}
1875
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 FakeConstraints constraints;
1879 constraints.SetAllowRtpDataChannels();
1880 CreatePeerConnection(&constraints);
1881
zhihuang9763d562016-08-05 11:14:50 -07001882 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 pc_->CreateDataChannel("test2", NULL);
1886 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001887 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001889 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 new MockDataChannelObserver(data2));
1891
1892 CreateOfferReceiveAnswer();
1893 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1894 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1895
1896 ReleasePeerConnection();
1897 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1898 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1899}
1900
1901// This test that data channels can be rejected in an answer.
1902TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1903 FakeConstraints constraints;
1904 constraints.SetAllowRtpDataChannels();
1905 CreatePeerConnection(&constraints);
1906
zhihuang9763d562016-08-05 11:14:50 -07001907 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 pc_->CreateDataChannel("offer_channel", NULL));
1909
1910 CreateOfferAsLocalDescription();
1911
1912 // Create an answer where the m-line for data channels are rejected.
1913 std::string sdp;
1914 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1915 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1916 SessionDescriptionInterface::kAnswer);
1917 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1918 cricket::ContentInfo* data_info =
1919 answer->description()->GetContentByName("data");
1920 data_info->rejected = true;
1921
1922 DoSetRemoteDescription(answer);
1923 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1924}
1925
1926// Test that we can create a session description from an SDP string from
1927// FireFox, use it as a remote session description, generate an answer and use
1928// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001929TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001930 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 FakeConstraints constraints;
1932 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1933 true);
1934 CreatePeerConnection(&constraints);
1935 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1936 SessionDescriptionInterface* desc =
1937 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001938 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1940 CreateAnswerAsLocalDescription();
1941 ASSERT_TRUE(pc_->local_description() != NULL);
1942 ASSERT_TRUE(pc_->remote_description() != NULL);
1943
1944 const cricket::ContentInfo* content =
1945 cricket::GetFirstAudioContent(pc_->local_description()->description());
1946 ASSERT_TRUE(content != NULL);
1947 EXPECT_FALSE(content->rejected);
1948
1949 content =
1950 cricket::GetFirstVideoContent(pc_->local_description()->description());
1951 ASSERT_TRUE(content != NULL);
1952 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001953#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 content =
1955 cricket::GetFirstDataContent(pc_->local_description()->description());
1956 ASSERT_TRUE(content != NULL);
1957 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001958#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959}
1960
1961// Test that we can create an audio only offer and receive an answer with a
1962// limited set of audio codecs and receive an updated offer with more audio
1963// codecs, where the added codecs are not supported.
1964TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1965 CreatePeerConnection();
1966 AddVoiceStream("audio_label");
1967 CreateOfferAsLocalDescription();
1968
1969 SessionDescriptionInterface* answer =
1970 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001971 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1973
1974 SessionDescriptionInterface* updated_offer =
1975 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001976 webrtc::kAudioSdpWithUnsupportedCodecs,
1977 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1979 CreateAnswerAsLocalDescription();
1980}
1981
deadbeefc80741f2015-10-22 13:14:45 -07001982// Test that if we're receiving (but not sending) a track, subsequent offers
1983// will have m-lines with a=recvonly.
1984TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1985 FakeConstraints constraints;
1986 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1987 true);
1988 CreatePeerConnection(&constraints);
1989 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1990 CreateAnswerAsLocalDescription();
1991
1992 // At this point we should be receiving stream 1, but not sending anything.
1993 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001994 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001995 DoCreateOffer(&offer, nullptr);
1996
1997 const cricket::ContentInfo* video_content =
1998 cricket::GetFirstVideoContent(offer->description());
1999 const cricket::VideoContentDescription* video_desc =
2000 static_cast<const cricket::VideoContentDescription*>(
2001 video_content->description);
2002 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2003
2004 const cricket::ContentInfo* audio_content =
2005 cricket::GetFirstAudioContent(offer->description());
2006 const cricket::AudioContentDescription* audio_desc =
2007 static_cast<const cricket::AudioContentDescription*>(
2008 audio_content->description);
2009 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2010}
2011
2012// Test that if we're receiving (but not sending) a track, and the
2013// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2014// false, the generated m-lines will be a=inactive.
2015TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2016 FakeConstraints constraints;
2017 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2018 true);
2019 CreatePeerConnection(&constraints);
2020 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2021 CreateAnswerAsLocalDescription();
2022
2023 // At this point we should be receiving stream 1, but not sending anything.
2024 // A new offer would be recvonly, but we'll set the "no receive" constraints
2025 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002026 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002027 FakeConstraints offer_constraints;
2028 offer_constraints.AddMandatory(
2029 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2030 offer_constraints.AddMandatory(
2031 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2032 DoCreateOffer(&offer, &offer_constraints);
2033
2034 const cricket::ContentInfo* video_content =
2035 cricket::GetFirstVideoContent(offer->description());
2036 const cricket::VideoContentDescription* video_desc =
2037 static_cast<const cricket::VideoContentDescription*>(
2038 video_content->description);
2039 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2040
2041 const cricket::ContentInfo* audio_content =
2042 cricket::GetFirstAudioContent(offer->description());
2043 const cricket::AudioContentDescription* audio_desc =
2044 static_cast<const cricket::AudioContentDescription*>(
2045 audio_content->description);
2046 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2047}
2048
deadbeef653b8e02015-11-11 12:55:10 -08002049// Test that we can use SetConfiguration to change the ICE servers of the
2050// PortAllocator.
2051TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2052 CreatePeerConnection();
2053
2054 PeerConnectionInterface::RTCConfiguration config;
2055 PeerConnectionInterface::IceServer server;
2056 server.uri = "stun:test_hostname";
2057 config.servers.push_back(server);
2058 EXPECT_TRUE(pc_->SetConfiguration(config));
2059
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002060 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2061 EXPECT_EQ("test_hostname",
2062 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002063}
2064
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002065TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2066 CreatePeerConnection();
2067 PeerConnectionInterface::RTCConfiguration config;
2068 config.type = PeerConnectionInterface::kRelay;
2069 EXPECT_TRUE(pc_->SetConfiguration(config));
2070 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2071}
2072
2073// Test that when SetConfiguration changes both the pool size and other
2074// attributes, the pooled session is created with the updated attributes.
2075TEST_F(PeerConnectionInterfaceTest,
2076 SetConfigurationCreatesPooledSessionCorrectly) {
2077 CreatePeerConnection();
2078 PeerConnectionInterface::RTCConfiguration config;
2079 config.ice_candidate_pool_size = 1;
2080 PeerConnectionInterface::IceServer server;
2081 server.uri = kStunAddressOnly;
2082 config.servers.push_back(server);
2083 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002084 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002085
2086 const cricket::FakePortAllocatorSession* session =
2087 static_cast<const cricket::FakePortAllocatorSession*>(
2088 port_allocator_->GetPooledSession());
2089 ASSERT_NE(nullptr, session);
2090 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002091}
2092
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093// Test that PeerConnection::Close changes the states to closed and all remote
2094// tracks change state to ended.
2095TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2096 // Initialize a PeerConnection and negotiate local and remote session
2097 // description.
2098 InitiateCall();
2099 ASSERT_EQ(1u, pc_->local_streams()->count());
2100 ASSERT_EQ(1u, pc_->remote_streams()->count());
2101
2102 pc_->Close();
2103
2104 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2105 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2106 pc_->ice_connection_state());
2107 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2108 pc_->ice_gathering_state());
2109
2110 EXPECT_EQ(1u, pc_->local_streams()->count());
2111 EXPECT_EQ(1u, pc_->remote_streams()->count());
2112
zhihuang9763d562016-08-05 11:14:50 -07002113 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2114 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002115 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002116 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002117 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2118 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2119 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120}
2121
2122// Test that PeerConnection methods fails gracefully after
2123// PeerConnection::Close has been called.
2124TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2125 CreatePeerConnection();
2126 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2127 CreateOfferAsRemoteDescription();
2128 CreateAnswerAsLocalDescription();
2129
2130 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002131 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 pc_->local_streams()->at(0);
2133
2134 pc_->Close();
2135
2136 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002137 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138
2139 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002140 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002142 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143
2144 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2145
2146 EXPECT_TRUE(pc_->local_description() != NULL);
2147 EXPECT_TRUE(pc_->remote_description() != NULL);
2148
kwibergd1fe2812016-04-27 06:47:29 -07002149 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002150 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002151 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002152 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153
2154 std::string sdp;
2155 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2156 SessionDescriptionInterface* remote_offer =
2157 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2158 sdp, NULL);
2159 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2160
2161 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2162 SessionDescriptionInterface* local_offer =
2163 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2164 sdp, NULL);
2165 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2166}
2167
2168// Test that GetStats can still be called after PeerConnection::Close.
2169TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2170 InitiateCall();
2171 pc_->Close();
2172 DoGetStats(NULL);
2173}
deadbeefab9b2d12015-10-14 11:33:11 -07002174
2175// NOTE: The series of tests below come from what used to be
2176// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2177// setting a remote or local description has the expected effects.
2178
2179// This test verifies that the remote MediaStreams corresponding to a received
2180// SDP string is created. In this test the two separate MediaStreams are
2181// signaled.
2182TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2183 FakeConstraints constraints;
2184 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2185 true);
2186 CreatePeerConnection(&constraints);
2187 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2188
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002189 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002190 EXPECT_TRUE(
2191 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2192 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2193 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2194
2195 // Create a session description based on another SDP with another
2196 // MediaStream.
2197 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2198
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002199 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002200 EXPECT_TRUE(
2201 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2202}
2203
2204// This test verifies that when remote tracks are added/removed from SDP, the
2205// created remote streams are updated appropriately.
2206TEST_F(PeerConnectionInterfaceTest,
2207 AddRemoveTrackFromExistingRemoteMediaStream) {
2208 FakeConstraints constraints;
2209 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2210 true);
2211 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002212 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002213 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002214 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2215 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2216 reference_collection_));
2217
2218 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002219 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002220 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002221 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2222 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2223 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002224 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002225 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2226 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002227 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002228 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2229 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002230
2231 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002232 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002233 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002234 MockTrackObserver audio_track_observer(audio_track2);
2235 MockTrackObserver video_track_observer(video_track2);
2236
2237 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2238 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002239 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2240 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2241 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002242 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002243 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002244 audio_track2->state(), kTimeout);
2245 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2246 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002247}
2248
2249// This tests that remote tracks are ended if a local session description is set
2250// that rejects the media content type.
2251TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2252 FakeConstraints constraints;
2253 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2254 true);
2255 CreatePeerConnection(&constraints);
2256 // First create and set a remote offer, then reject its video content in our
2257 // answer.
2258 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2259 ASSERT_EQ(1u, observer_.remote_streams()->count());
2260 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2261 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2262 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2263
2264 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2265 remote_stream->GetVideoTracks()[0];
2266 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2267 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2268 remote_stream->GetAudioTracks()[0];
2269 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2270
kwibergd1fe2812016-04-27 06:47:29 -07002271 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002272 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002273 cricket::ContentInfo* video_info =
2274 local_answer->description()->GetContentByName("video");
2275 video_info->rejected = true;
2276 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2277 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2278 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2279
2280 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002281 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002282 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002283 video_info = local_offer->description()->GetContentByName("video");
2284 ASSERT_TRUE(video_info != nullptr);
2285 video_info->rejected = true;
2286 cricket::ContentInfo* audio_info =
2287 local_offer->description()->GetContentByName("audio");
2288 ASSERT_TRUE(audio_info != nullptr);
2289 audio_info->rejected = true;
2290 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002291 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002292 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002293 remote_audio->state(), kTimeout);
2294 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2295 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002296}
2297
2298// This tests that we won't crash if the remote track has been removed outside
2299// of PeerConnection and then PeerConnection tries to reject the track.
2300TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2301 FakeConstraints constraints;
2302 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2303 true);
2304 CreatePeerConnection(&constraints);
2305 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2306 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2307 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2308 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2309
kwibergd1fe2812016-04-27 06:47:29 -07002310 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002311 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2312 kSdpStringWithStream1, nullptr));
2313 cricket::ContentInfo* video_info =
2314 local_answer->description()->GetContentByName("video");
2315 video_info->rejected = true;
2316 cricket::ContentInfo* audio_info =
2317 local_answer->description()->GetContentByName("audio");
2318 audio_info->rejected = true;
2319 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2320
2321 // No crash is a pass.
2322}
2323
deadbeef5e97fb52015-10-15 12:49:08 -07002324// This tests that if a recvonly remote description is set, no remote streams
2325// will be created, even if the description contains SSRCs/MSIDs.
2326// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2327TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2328 FakeConstraints constraints;
2329 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2330 true);
2331 CreatePeerConnection(&constraints);
2332
2333 std::string recvonly_offer = kSdpStringWithStream1;
2334 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2335 strlen(kRecvonly), &recvonly_offer);
2336 CreateAndSetRemoteOffer(recvonly_offer);
2337
2338 EXPECT_EQ(0u, observer_.remote_streams()->count());
2339}
2340
deadbeefab9b2d12015-10-14 11:33:11 -07002341// This tests that a default MediaStream is created if a remote session
2342// description doesn't contain any streams and no MSID support.
2343// It also tests that the default stream is updated if a video m-line is added
2344// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002345TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002346 FakeConstraints constraints;
2347 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2348 true);
2349 CreatePeerConnection(&constraints);
2350 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2351
2352 ASSERT_EQ(1u, observer_.remote_streams()->count());
2353 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2354
2355 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2356 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2357 EXPECT_EQ("default", remote_stream->label());
2358
2359 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2360 ASSERT_EQ(1u, observer_.remote_streams()->count());
2361 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2362 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002363 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2364 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002365 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2366 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002367 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2368 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002369}
2370
2371// This tests that a default MediaStream is created if a remote session
2372// description doesn't contain any streams and media direction is send only.
2373TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002374 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002375 FakeConstraints constraints;
2376 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2377 true);
2378 CreatePeerConnection(&constraints);
2379 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2380
2381 ASSERT_EQ(1u, observer_.remote_streams()->count());
2382 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2383
2384 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2385 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2386 EXPECT_EQ("default", remote_stream->label());
2387}
2388
2389// This tests that it won't crash when PeerConnection tries to remove
2390// a remote track that as already been removed from the MediaStream.
2391TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2392 FakeConstraints constraints;
2393 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2394 true);
2395 CreatePeerConnection(&constraints);
2396 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2397 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2398 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2399 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2400
2401 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2402
2403 // No crash is a pass.
2404}
2405
2406// This tests that a default MediaStream is created if the remote session
2407// description doesn't contain any streams and don't contain an indication if
2408// MSID is supported.
2409TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002410 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002411 FakeConstraints constraints;
2412 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2413 true);
2414 CreatePeerConnection(&constraints);
2415 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2416
2417 ASSERT_EQ(1u, observer_.remote_streams()->count());
2418 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2419 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2420 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2421}
2422
2423// This tests that a default MediaStream is not created if the remote session
2424// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002425TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002426 FakeConstraints constraints;
2427 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2428 true);
2429 CreatePeerConnection(&constraints);
2430 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2431 EXPECT_EQ(0u, observer_.remote_streams()->count());
2432}
2433
deadbeefbda7e0b2015-12-08 17:13:40 -08002434// This tests that when setting a new description, the old default tracks are
2435// not destroyed and recreated.
2436// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002437TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002438 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002439 FakeConstraints constraints;
2440 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2441 true);
2442 CreatePeerConnection(&constraints);
2443 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2444
2445 ASSERT_EQ(1u, observer_.remote_streams()->count());
2446 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2447 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2448
2449 // Set the track to "disabled", then set a new description and ensure the
2450 // track is still disabled, which ensures it hasn't been recreated.
2451 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2452 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2453 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2454 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2455}
2456
deadbeefab9b2d12015-10-14 11:33:11 -07002457// This tests that a default MediaStream is not created if a remote session
2458// description is updated to not have any MediaStreams.
2459TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2460 FakeConstraints constraints;
2461 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2462 true);
2463 CreatePeerConnection(&constraints);
2464 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002465 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002466 EXPECT_TRUE(
2467 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2468
2469 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2470 EXPECT_EQ(0u, observer_.remote_streams()->count());
2471}
2472
2473// This tests that an RtpSender is created when the local description is set
2474// after adding a local stream.
2475// TODO(deadbeef): This test and the one below it need to be updated when
2476// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002477TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002478 FakeConstraints constraints;
2479 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2480 true);
2481 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002482
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002483 // Create an offer with 1 stream with 2 tracks of each type.
2484 rtc::scoped_refptr<StreamCollection> stream_collection =
2485 CreateStreamCollection(1, 2);
2486 pc_->AddStream(stream_collection->at(0));
2487 std::unique_ptr<SessionDescriptionInterface> offer;
2488 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2489 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002490
deadbeefab9b2d12015-10-14 11:33:11 -07002491 auto senders = pc_->GetSenders();
2492 EXPECT_EQ(4u, senders.size());
2493 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2494 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2495 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2496 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2497
2498 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002499 pc_->RemoveStream(stream_collection->at(0));
2500 stream_collection = CreateStreamCollection(1, 1);
2501 pc_->AddStream(stream_collection->at(0));
2502 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2503 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2504
deadbeefab9b2d12015-10-14 11:33:11 -07002505 senders = pc_->GetSenders();
2506 EXPECT_EQ(2u, senders.size());
2507 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2508 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2509 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2510 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2511}
2512
2513// This tests that an RtpSender is created when the local description is set
2514// before adding a local stream.
2515TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002516 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002517 FakeConstraints constraints;
2518 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2519 true);
2520 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002521
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002522 rtc::scoped_refptr<StreamCollection> stream_collection =
2523 CreateStreamCollection(1, 2);
2524 // Add a stream to create the offer, but remove it afterwards.
2525 pc_->AddStream(stream_collection->at(0));
2526 std::unique_ptr<SessionDescriptionInterface> offer;
2527 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2528 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002529
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002530 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002531 auto senders = pc_->GetSenders();
2532 EXPECT_EQ(0u, senders.size());
2533
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002534 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002535 senders = pc_->GetSenders();
2536 EXPECT_EQ(4u, senders.size());
2537 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2538 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2539 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2540 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2541}
2542
2543// This tests that the expected behavior occurs if the SSRC on a local track is
2544// changed when SetLocalDescription is called.
2545TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002546 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002547 FakeConstraints constraints;
2548 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2549 true);
2550 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002551
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002552 rtc::scoped_refptr<StreamCollection> stream_collection =
2553 CreateStreamCollection(2, 1);
2554 pc_->AddStream(stream_collection->at(0));
2555 std::unique_ptr<SessionDescriptionInterface> offer;
2556 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2557 // Grab a copy of the offer before it gets passed into the PC.
2558 std::unique_ptr<JsepSessionDescription> modified_offer(
2559 new JsepSessionDescription(JsepSessionDescription::kOffer));
2560 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2561 offer->session_version());
2562 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002563
deadbeefab9b2d12015-10-14 11:33:11 -07002564 auto senders = pc_->GetSenders();
2565 EXPECT_EQ(2u, senders.size());
2566 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2567 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2568
2569 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002570 cricket::MediaContentDescription* desc =
2571 cricket::GetFirstAudioContentDescription(modified_offer->description());
2572 ASSERT_TRUE(desc != NULL);
2573 for (StreamParams& stream : desc->mutable_streams()) {
2574 for (unsigned int& ssrc : stream.ssrcs) {
2575 ++ssrc;
2576 }
2577 }
deadbeefab9b2d12015-10-14 11:33:11 -07002578
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002579 desc =
2580 cricket::GetFirstVideoContentDescription(modified_offer->description());
2581 ASSERT_TRUE(desc != NULL);
2582 for (StreamParams& stream : desc->mutable_streams()) {
2583 for (unsigned int& ssrc : stream.ssrcs) {
2584 ++ssrc;
2585 }
2586 }
2587
2588 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002589 senders = pc_->GetSenders();
2590 EXPECT_EQ(2u, senders.size());
2591 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2592 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2593 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2594 // changed.
2595}
2596
2597// This tests that the expected behavior occurs if a new session description is
2598// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002599TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002600 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002601 FakeConstraints constraints;
2602 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2603 true);
2604 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002605
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002606 rtc::scoped_refptr<StreamCollection> stream_collection =
2607 CreateStreamCollection(2, 1);
2608 pc_->AddStream(stream_collection->at(0));
2609 std::unique_ptr<SessionDescriptionInterface> offer;
2610 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2611 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002612
deadbeefab9b2d12015-10-14 11:33:11 -07002613 auto senders = pc_->GetSenders();
2614 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002615 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2616 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002617
2618 // Add a new MediaStream but with the same tracks as in the first stream.
2619 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2620 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002621 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2622 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002623 pc_->AddStream(stream_1);
2624
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002625 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2626 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002627
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002628 auto new_senders = pc_->GetSenders();
2629 // Should be the same senders as before, but with updated stream id.
2630 // Note that this behavior is subject to change in the future.
2631 // We may decide the PC should ignore existing tracks in AddStream.
2632 EXPECT_EQ(senders, new_senders);
2633 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2634 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002635}
2636
nisse51542be2016-02-12 02:27:06 -08002637class PeerConnectionMediaConfigTest : public testing::Test {
2638 protected:
2639 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002640 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002641 pcf_->Initialize();
2642 }
2643 const cricket::MediaConfig& TestCreatePeerConnection(
2644 const PeerConnectionInterface::RTCConfiguration& config,
2645 const MediaConstraintsInterface *constraints) {
2646 pcf_->create_media_controller_called_ = false;
2647
zhihuang9763d562016-08-05 11:14:50 -07002648 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2649 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002650 EXPECT_TRUE(pc.get());
2651 EXPECT_TRUE(pcf_->create_media_controller_called_);
2652 return pcf_->create_media_controller_config_;
2653 }
2654
zhihuang9763d562016-08-05 11:14:50 -07002655 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002656 MockPeerConnectionObserver observer_;
2657};
2658
2659// This test verifies the default behaviour with no constraints and a
2660// default RTCConfiguration.
2661TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2662 PeerConnectionInterface::RTCConfiguration config;
2663 FakeConstraints constraints;
2664
2665 const cricket::MediaConfig& media_config =
2666 TestCreatePeerConnection(config, &constraints);
2667
2668 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002669 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2670 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2671 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002672}
2673
2674// This test verifies the DSCP constraint is recognized and passed to
2675// the CreateMediaController call.
2676TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2677 PeerConnectionInterface::RTCConfiguration config;
2678 FakeConstraints constraints;
2679
2680 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2681 const cricket::MediaConfig& media_config =
2682 TestCreatePeerConnection(config, &constraints);
2683
2684 EXPECT_TRUE(media_config.enable_dscp);
2685}
2686
2687// This test verifies the cpu overuse detection constraint is
2688// recognized and passed to the CreateMediaController call.
2689TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2690 PeerConnectionInterface::RTCConfiguration config;
2691 FakeConstraints constraints;
2692
2693 constraints.AddOptional(
2694 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2695 const cricket::MediaConfig media_config =
2696 TestCreatePeerConnection(config, &constraints);
2697
nisse0db023a2016-03-01 04:29:59 -08002698 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002699}
2700
2701// This test verifies that the disable_prerenderer_smoothing flag is
2702// propagated from RTCConfiguration to the CreateMediaController call.
2703TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2704 PeerConnectionInterface::RTCConfiguration config;
2705 FakeConstraints constraints;
2706
Niels Möller71bdda02016-03-31 12:59:59 +02002707 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002708 const cricket::MediaConfig& media_config =
2709 TestCreatePeerConnection(config, &constraints);
2710
nisse0db023a2016-03-01 04:29:59 -08002711 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2712}
2713
2714// This test verifies the suspend below min bitrate constraint is
2715// recognized and passed to the CreateMediaController call.
2716TEST_F(PeerConnectionMediaConfigTest,
2717 TestSuspendBelowMinBitrateConstraintTrue) {
2718 PeerConnectionInterface::RTCConfiguration config;
2719 FakeConstraints constraints;
2720
2721 constraints.AddOptional(
2722 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2723 true);
2724 const cricket::MediaConfig media_config =
2725 TestCreatePeerConnection(config, &constraints);
2726
2727 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002728}
2729
deadbeefab9b2d12015-10-14 11:33:11 -07002730// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002731// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2732// "verify options are converted correctly", should be "pass options into
2733// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002734
2735TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2736 RTCOfferAnswerOptions rtc_options;
2737 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2738
2739 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002740 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002741
2742 rtc_options.offer_to_receive_audio =
2743 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002744 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002745}
2746
2747TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2748 RTCOfferAnswerOptions rtc_options;
2749 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2750
2751 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002752 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002753
2754 rtc_options.offer_to_receive_video =
2755 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002756 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002757}
2758
2759// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002760// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002761TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2762 RTCOfferAnswerOptions rtc_options;
2763 rtc_options.offer_to_receive_audio = 1;
2764 rtc_options.offer_to_receive_video = 1;
2765
2766 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002767 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002768 EXPECT_TRUE(options.has_audio());
2769 EXPECT_TRUE(options.has_video());
2770 EXPECT_TRUE(options.bundle_enabled);
2771}
2772
2773// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002774// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002775TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2776 RTCOfferAnswerOptions rtc_options;
2777 rtc_options.offer_to_receive_audio = 1;
2778
2779 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002780 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002781 EXPECT_TRUE(options.has_audio());
2782 EXPECT_FALSE(options.has_video());
2783 EXPECT_TRUE(options.bundle_enabled);
2784}
2785
2786// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002787// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002788TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2789 RTCOfferAnswerOptions rtc_options;
2790
2791 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002792 options.transport_options["audio"] = cricket::TransportOptions();
2793 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002794 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002795 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002796 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002797 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002798 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002799 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2800 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002801}
2802
2803// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002804// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002805TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2806 RTCOfferAnswerOptions rtc_options;
2807 rtc_options.offer_to_receive_audio = 0;
2808 rtc_options.offer_to_receive_video = 1;
2809
2810 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002811 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002812 EXPECT_FALSE(options.has_audio());
2813 EXPECT_TRUE(options.has_video());
2814 EXPECT_TRUE(options.bundle_enabled);
2815}
2816
2817// Test that a correct MediaSessionOptions is created for an offer if
2818// UseRtpMux is set to false.
2819TEST(CreateSessionOptionsTest,
2820 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2821 RTCOfferAnswerOptions rtc_options;
2822 rtc_options.offer_to_receive_audio = 1;
2823 rtc_options.offer_to_receive_video = 1;
2824 rtc_options.use_rtp_mux = false;
2825
2826 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002827 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002828 EXPECT_TRUE(options.has_audio());
2829 EXPECT_TRUE(options.has_video());
2830 EXPECT_FALSE(options.bundle_enabled);
2831}
2832
2833// Test that a correct MediaSessionOptions is created to restart ice if
2834// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002835// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002836TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2837 RTCOfferAnswerOptions rtc_options;
2838 rtc_options.ice_restart = true;
2839
2840 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002841 options.transport_options["audio"] = cricket::TransportOptions();
2842 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002843 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002844 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2845 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002846
2847 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002848 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002849 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2850 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002851}
2852
2853// Test that the MediaConstraints in an answer don't affect if audio and video
2854// is offered in an offer but that if kOfferToReceiveAudio or
2855// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2856// included in subsequent answers.
2857TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2858 FakeConstraints answer_c;
2859 answer_c.SetMandatoryReceiveAudio(true);
2860 answer_c.SetMandatoryReceiveVideo(true);
2861
2862 cricket::MediaSessionOptions answer_options;
2863 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2864 EXPECT_TRUE(answer_options.has_audio());
2865 EXPECT_TRUE(answer_options.has_video());
2866
deadbeefc80741f2015-10-22 13:14:45 -07002867 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002868
2869 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002870 EXPECT_TRUE(
2871 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002872 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002873 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002874
deadbeefc80741f2015-10-22 13:14:45 -07002875 RTCOfferAnswerOptions updated_rtc_offer_options;
2876 updated_rtc_offer_options.offer_to_receive_audio = 1;
2877 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002878
2879 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002880 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002881 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002882 EXPECT_TRUE(updated_offer_options.has_audio());
2883 EXPECT_TRUE(updated_offer_options.has_video());
2884
2885 // Since an offer has been created with both audio and video, subsequent
2886 // offers and answers should contain both audio and video.
2887 // Answers will only contain the media types that exist in the offer
2888 // regardless of the value of |updated_answer_options.has_audio| and
2889 // |updated_answer_options.has_video|.
2890 FakeConstraints updated_answer_c;
2891 answer_c.SetMandatoryReceiveAudio(false);
2892 answer_c.SetMandatoryReceiveVideo(false);
2893
2894 cricket::MediaSessionOptions updated_answer_options;
2895 EXPECT_TRUE(
2896 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2897 EXPECT_TRUE(updated_answer_options.has_audio());
2898 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002899}