blob: 0b24fcb7cfc4d119af78dacd4258d84f6188aa76 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020029#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070042#include "webrtc/p2p/base/fakeportallocator.h"
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
zhihuang34b54c32016-08-04 11:06: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_;
zhihuang34b54c32016-08-04 11:06: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:
zhihuang34b54c32016-08-04 11:06: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(
571 cricket::PortAllocator* port_allocator) override {
572 transport_controller = new cricket::TransportController(
573 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator);
574 return transport_controller;
575 }
576
577 cricket::TransportController* transport_controller;
578 // Mutable, so they can be modified in the above const-declared method.
579 mutable bool create_media_controller_called_ = false;
580 mutable cricket::MediaConfig create_media_controller_config_;
581};
582
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583class PeerConnectionInterfaceTest : public testing::Test {
584 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800585 PeerConnectionInterfaceTest() {
586#ifdef WEBRTC_ANDROID
587 webrtc::InitializeAndroidObjects();
588#endif
589 }
590
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 virtual void SetUp() {
592 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700593 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
594 nullptr, nullptr, nullptr);
595 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700596 pc_factory_for_test_ =
597 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
598 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 }
600
601 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700602 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 }
604
605 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700606 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
607 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
609
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700610 void CreatePeerConnectionWithIceTransportsType(
611 PeerConnectionInterface::IceTransportsType type) {
612 PeerConnectionInterface::RTCConfiguration config;
613 config.type = type;
614 return CreatePeerConnection(config, nullptr);
615 }
616
617 void CreatePeerConnectionWithIceServer(const std::string& uri,
618 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800619 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621 server.uri = uri;
622 server.password = password;
623 config.servers.push_back(server);
624 CreatePeerConnection(config, nullptr);
625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700627 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
628 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700629 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800630 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
631 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000632
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000633 // DTLS does not work in a loopback call, so is disabled for most of the
634 // tests in this file. We only create a FakeIdentityService if the test
635 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000636 FakeConstraints default_constraints;
637 if (!constraints) {
638 constraints = &default_constraints;
639
640 default_constraints.AddMandatory(
641 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
642 }
643
Henrik Boströmd79599d2016-06-01 13:58:50 +0200644 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000645 bool dtls;
646 if (FindConstraint(constraints,
647 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
648 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200649 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200650 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000651 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200652 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800653 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200654 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 ASSERT_TRUE(pc_.get() != NULL);
656 observer_.SetPeerConnectionInterface(pc_.get());
657 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
658 }
659
deadbeef0a6c4ca2015-10-06 11:38:28 -0700660 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800661 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700662 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700663 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800664 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700665
zhihuang34b54c32016-08-04 11:06:50 -0700666 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700667 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
668 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800669 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700670 }
671
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700673 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800674 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
675 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
676 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800678 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679
deadbeef0a6c4ca2015-10-06 11:38:28 -0700680 CreatePeerConnectionExpectFail(kStunInvalidPort);
681 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
682 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700684 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800685 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
686 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800688 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800690 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 }
694
695 void ReleasePeerConnection() {
696 pc_ = NULL;
697 observer_.SetPeerConnectionInterface(NULL);
698 }
699
deadbeefab9b2d12015-10-14 11:33:11 -0700700 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 // Create a local stream.
zhihuang34b54c32016-08-04 11:06:50 -0700702 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 pc_factory_->CreateLocalMediaStream(label));
zhihuang34b54c32016-08-04 11:06:50 -0700704 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang34b54c32016-08-04 11:06:50 -0700706 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 pc_factory_->CreateVideoTrack(label + "v0", video_source));
708 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000709 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
711 observer_.renegotiation_needed_ = false;
712 }
713
714 void AddVoiceStream(const std::string& label) {
715 // Create a local stream.
zhihuang34b54c32016-08-04 11:06:50 -0700716 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 pc_factory_->CreateLocalMediaStream(label));
zhihuang34b54c32016-08-04 11:06:50 -0700718 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 pc_factory_->CreateAudioTrack(label + "a0", NULL));
720 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000721 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
723 observer_.renegotiation_needed_ = false;
724 }
725
726 void AddAudioVideoStream(const std::string& stream_label,
727 const std::string& audio_track_label,
728 const std::string& video_track_label) {
729 // Create a local stream.
zhihuang34b54c32016-08-04 11:06:50 -0700730 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang34b54c32016-08-04 11:06:50 -0700732 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 pc_factory_->CreateAudioTrack(
734 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
735 stream->AddTrack(audio_track.get());
zhihuang34b54c32016-08-04 11:06:50 -0700736 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700737 pc_factory_->CreateVideoTrack(
738 video_track_label,
739 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000741 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
743 observer_.renegotiation_needed_ = false;
744 }
745
kwibergd1fe2812016-04-27 06:47:29 -0700746 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700747 bool offer,
748 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000749 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
750 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 MockCreateSessionDescriptionObserver>());
752 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700753 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700755 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 }
757 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700758 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 return observer->result();
760 }
761
kwibergd1fe2812016-04-27 06:47:29 -0700762 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700763 MediaConstraintsInterface* constraints) {
764 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
766
kwibergd1fe2812016-04-27 06:47:29 -0700767 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700768 MediaConstraintsInterface* constraints) {
769 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
771
772 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000773 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
774 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 MockSetSessionDescriptionObserver>());
776 if (local) {
777 pc_->SetLocalDescription(observer, desc);
778 } else {
779 pc_->SetRemoteDescription(observer, desc);
780 }
zhihuang29ff8442016-07-27 11:07:25 -0700781 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
782 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
783 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 return observer->result();
785 }
786
787 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
788 return DoSetSessionDescription(desc, true);
789 }
790
791 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
792 return DoSetSessionDescription(desc, false);
793 }
794
795 // Calls PeerConnection::GetStats and check the return value.
796 // It does not verify the values in the StatReports since a RTCP packet might
797 // be required.
798 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000799 rtc::scoped_refptr<MockStatsObserver> observer(
800 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000801 if (!pc_->GetStats(
802 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 return false;
804 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
805 return observer->called();
806 }
807
808 void InitiateCall() {
809 CreatePeerConnection();
810 // Create a local stream with audio&video tracks.
811 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
812 CreateOfferReceiveAnswer();
813 }
814
815 // Verify that RTP Header extensions has been negotiated for audio and video.
816 void VerifyRemoteRtpHeaderExtensions() {
817 const cricket::MediaContentDescription* desc =
818 cricket::GetFirstAudioContentDescription(
819 pc_->remote_description()->description());
820 ASSERT_TRUE(desc != NULL);
821 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
822
823 desc = cricket::GetFirstVideoContentDescription(
824 pc_->remote_description()->description());
825 ASSERT_TRUE(desc != NULL);
826 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
827 }
828
829 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700830 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700831 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 std::string sdp;
833 EXPECT_TRUE(offer->ToString(&sdp));
834 SessionDescriptionInterface* remote_offer =
835 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
836 sdp, NULL);
837 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
838 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
839 }
840
deadbeefab9b2d12015-10-14 11:33:11 -0700841 void CreateAndSetRemoteOffer(const std::string& sdp) {
842 SessionDescriptionInterface* remote_offer =
843 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
844 sdp, nullptr);
845 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
846 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
847 }
848
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700850 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700851 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852
853 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
854 // audio codec change, even if the parameter has nothing to do with
855 // receiving. Not all parameters are serialized to SDP.
856 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
857 // the SessionDescription, it is necessary to do that here to in order to
858 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
859 // https://code.google.com/p/webrtc/issues/detail?id=1356
860 std::string sdp;
861 EXPECT_TRUE(answer->ToString(&sdp));
862 SessionDescriptionInterface* new_answer =
863 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
864 sdp, NULL);
865 EXPECT_TRUE(DoSetLocalDescription(new_answer));
866 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
867 }
868
869 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700870 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700871 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
873 std::string sdp;
874 EXPECT_TRUE(answer->ToString(&sdp));
875 SessionDescriptionInterface* pr_answer =
876 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
877 sdp, NULL);
878 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
879 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
880 }
881
882 void CreateOfferReceiveAnswer() {
883 CreateOfferAsLocalDescription();
884 std::string sdp;
885 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
886 CreateAnswerAsRemoteDescription(sdp);
887 }
888
889 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700890 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700891 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
893 // audio codec change, even if the parameter has nothing to do with
894 // receiving. Not all parameters are serialized to SDP.
895 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
896 // the SessionDescription, it is necessary to do that here to in order to
897 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
898 // https://code.google.com/p/webrtc/issues/detail?id=1356
899 std::string sdp;
900 EXPECT_TRUE(offer->ToString(&sdp));
901 SessionDescriptionInterface* new_offer =
902 webrtc::CreateSessionDescription(
903 SessionDescriptionInterface::kOffer,
904 sdp, NULL);
905
906 EXPECT_TRUE(DoSetLocalDescription(new_offer));
907 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000908 // Wait for the ice_complete message, so that SDP will have candidates.
909 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911
deadbeefab9b2d12015-10-14 11:33:11 -0700912 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
914 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700915 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 EXPECT_TRUE(DoSetRemoteDescription(answer));
917 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
918 }
919
deadbeefab9b2d12015-10-14 11:33:11 -0700920 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 webrtc::JsepSessionDescription* pr_answer =
922 new webrtc::JsepSessionDescription(
923 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700924 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
926 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
927 webrtc::JsepSessionDescription* answer =
928 new webrtc::JsepSessionDescription(
929 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700930 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 EXPECT_TRUE(DoSetRemoteDescription(answer));
932 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
933 }
934
935 // Help function used for waiting until a the last signaled remote stream has
936 // the same label as |stream_label|. In a few of the tests in this file we
937 // answer with the same session description as we offer and thus we can
938 // check if OnAddStream have been called with the same stream as we offer to
939 // send.
940 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
941 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
942 }
943
944 // Creates an offer and applies it as a local session description.
945 // Creates an answer with the same SDP an the offer but removes all lines
946 // that start with a:ssrc"
947 void CreateOfferReceiveAnswerWithoutSsrc() {
948 CreateOfferAsLocalDescription();
949 std::string sdp;
950 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
951 SetSsrcToZero(&sdp);
952 CreateAnswerAsRemoteDescription(sdp);
953 }
954
deadbeefab9b2d12015-10-14 11:33:11 -0700955 // This function creates a MediaStream with label kStreams[0] and
956 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
957 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700958 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700959 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700960 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700961 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
962 size_t number_of_video_tracks) {
963 EXPECT_LE(number_of_audio_tracks, 2u);
964 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700965
966 reference_collection_ = StreamCollection::Create();
967 std::string sdp_ms1 = std::string(kSdpStringInit);
968
969 std::string mediastream_label = kStreams[0];
970
971 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
972 webrtc::MediaStream::Create(mediastream_label));
973 reference_collection_->AddStream(stream);
974
975 if (number_of_audio_tracks > 0) {
976 sdp_ms1 += std::string(kSdpStringAudio);
977 sdp_ms1 += std::string(kSdpStringMs1Audio0);
978 AddAudioTrack(kAudioTracks[0], stream);
979 }
980 if (number_of_audio_tracks > 1) {
981 sdp_ms1 += kSdpStringMs1Audio1;
982 AddAudioTrack(kAudioTracks[1], stream);
983 }
984
985 if (number_of_video_tracks > 0) {
986 sdp_ms1 += std::string(kSdpStringVideo);
987 sdp_ms1 += std::string(kSdpStringMs1Video0);
988 AddVideoTrack(kVideoTracks[0], stream);
989 }
990 if (number_of_video_tracks > 1) {
991 sdp_ms1 += kSdpStringMs1Video1;
992 AddVideoTrack(kVideoTracks[1], stream);
993 }
994
kwibergd1fe2812016-04-27 06:47:29 -0700995 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700996 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
997 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700998 }
999
1000 void AddAudioTrack(const std::string& track_id,
1001 MediaStreamInterface* stream) {
1002 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1003 webrtc::AudioTrack::Create(track_id, nullptr));
1004 ASSERT_TRUE(stream->AddTrack(audio_track));
1005 }
1006
1007 void AddVideoTrack(const std::string& track_id,
1008 MediaStreamInterface* stream) {
1009 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001010 webrtc::VideoTrack::Create(track_id,
1011 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001012 ASSERT_TRUE(stream->AddTrack(video_track));
1013 }
1014
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreatePeerConnection();
1017 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001018 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001019 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1020 return offer;
1021 }
1022
kwibergfd8be342016-05-14 19:44:11 -07001023 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001024 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001025 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001026 CreateOfferWithOneAudioStream();
1027 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001028 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001029 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1030 return answer;
1031 }
1032
1033 const std::string& GetFirstAudioStreamCname(
1034 const SessionDescriptionInterface* desc) {
1035 const cricket::ContentInfo* audio_content =
1036 cricket::GetFirstAudioContent(desc->description());
1037 const cricket::AudioContentDescription* audio_desc =
1038 static_cast<const cricket::AudioContentDescription*>(
1039 audio_content->description);
1040 return audio_desc->streams()[0].cname;
1041 }
1042
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001043 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang34b54c32016-08-04 11:06:50 -07001044 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1045 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1046 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001048 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049};
1050
zhihuang29ff8442016-07-27 11:07:25 -07001051// Test that no callbacks on the PeerConnectionObserver are called after the
1052// PeerConnection is closed.
1053TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang34b54c32016-08-04 11:06:50 -07001054 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001055 pc_factory_for_test_->CreatePeerConnection(
1056 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1057 nullptr, &observer_));
1058 observer_.SetPeerConnectionInterface(pc.get());
1059 pc->Close();
1060
1061 // No callbacks is expected to be called.
1062 observer_.callback_triggered = false;
1063 std::vector<cricket::Candidate> candidates;
1064 pc_factory_for_test_->transport_controller->SignalGatheringState(
1065 cricket::IceGatheringState{});
1066 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1067 "", candidates);
1068 pc_factory_for_test_->transport_controller->SignalConnectionState(
1069 cricket::IceConnectionState{});
1070 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1071 candidates);
1072 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1073 EXPECT_FALSE(observer_.callback_triggered);
1074}
1075
zhihuang8f65cdf2016-05-06 18:40:30 -07001076// Generate different CNAMEs when PeerConnections are created.
1077// The CNAMEs are expected to be generated randomly. It is possible
1078// that the test fails, though the possibility is very low.
1079TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001080 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001081 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001082 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001083 CreateOfferWithOneAudioStream();
1084 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1085 GetFirstAudioStreamCname(offer2.get()));
1086}
1087
1088TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001089 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001090 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001091 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001092 CreateAnswerWithOneAudioStream();
1093 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1094 GetFirstAudioStreamCname(answer2.get()));
1095}
1096
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097TEST_F(PeerConnectionInterfaceTest,
1098 CreatePeerConnectionWithDifferentConfigurations) {
1099 CreatePeerConnectionWithDifferentConfigurations();
1100}
1101
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001102TEST_F(PeerConnectionInterfaceTest,
1103 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1104 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1105 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1106 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1107 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1108 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1109 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1110 port_allocator_->candidate_filter());
1111 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1112 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1113}
1114
1115// Test that when a PeerConnection is created with a nonzero candidate pool
1116// size, the pooled PortAllocatorSession is created with all the attributes
1117// in the RTCConfiguration.
1118TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1119 PeerConnectionInterface::RTCConfiguration config;
1120 PeerConnectionInterface::IceServer server;
1121 server.uri = kStunAddressOnly;
1122 config.servers.push_back(server);
1123 config.type = PeerConnectionInterface::kRelay;
1124 config.disable_ipv6 = true;
1125 config.tcp_candidate_policy =
1126 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001127 config.candidate_network_policy =
1128 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001129 config.ice_candidate_pool_size = 1;
1130 CreatePeerConnection(config, nullptr);
1131
1132 const cricket::FakePortAllocatorSession* session =
1133 static_cast<const cricket::FakePortAllocatorSession*>(
1134 port_allocator_->GetPooledSession());
1135 ASSERT_NE(nullptr, session);
1136 EXPECT_EQ(1UL, session->stun_servers().size());
1137 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1138 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001139 EXPECT_LT(0U,
1140 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001141}
1142
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001143// Test that the PeerConnection initializes the port allocator passed into it,
1144// and on the correct thread.
1145TEST_F(PeerConnectionInterfaceTest,
1146 CreatePeerConnectionInitializesPortAllocator) {
1147 rtc::Thread network_thread;
1148 network_thread.Start();
1149 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1150 webrtc::CreatePeerConnectionFactory(
1151 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1152 nullptr, nullptr, nullptr));
1153 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1154 new cricket::FakePortAllocator(&network_thread, nullptr));
1155 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1156 PeerConnectionInterface::RTCConfiguration config;
1157 rtc::scoped_refptr<PeerConnectionInterface> pc(
1158 pc_factory->CreatePeerConnection(
1159 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1160 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1161 // so all we have to do here is check that it's initialized.
1162 EXPECT_TRUE(raw_port_allocator->initialized());
1163}
1164
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1166 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001167 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 AddVoiceStream(kStreamLabel2);
1169 ASSERT_EQ(2u, pc_->local_streams()->count());
1170
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001171 // Test we can add multiple local streams to one peerconnection.
zhihuang34b54c32016-08-04 11:06:50 -07001172 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang34b54c32016-08-04 11:06:50 -07001174 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1175 pc_factory_->CreateAudioTrack(kStreamLabel3,
1176 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001178 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001179 EXPECT_EQ(3u, pc_->local_streams()->count());
1180
1181 // Remove the third stream.
1182 pc_->RemoveStream(pc_->local_streams()->at(2));
1183 EXPECT_EQ(2u, pc_->local_streams()->count());
1184
1185 // Remove the second stream.
1186 pc_->RemoveStream(pc_->local_streams()->at(1));
1187 EXPECT_EQ(1u, pc_->local_streams()->count());
1188
1189 // Remove the first stream.
1190 pc_->RemoveStream(pc_->local_streams()->at(0));
1191 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192}
1193
deadbeefab9b2d12015-10-14 11:33:11 -07001194// Test that the created offer includes streams we added.
1195TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1196 CreatePeerConnection();
1197 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001198 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001199 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001200
1201 const cricket::ContentInfo* audio_content =
1202 cricket::GetFirstAudioContent(offer->description());
1203 const cricket::AudioContentDescription* audio_desc =
1204 static_cast<const cricket::AudioContentDescription*>(
1205 audio_content->description);
1206 EXPECT_TRUE(
1207 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1208
1209 const cricket::ContentInfo* video_content =
1210 cricket::GetFirstVideoContent(offer->description());
1211 const cricket::VideoContentDescription* video_desc =
1212 static_cast<const cricket::VideoContentDescription*>(
1213 video_content->description);
1214 EXPECT_TRUE(
1215 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1216
1217 // Add another stream and ensure the offer includes both the old and new
1218 // streams.
1219 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001220 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001221
1222 audio_content = cricket::GetFirstAudioContent(offer->description());
1223 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1224 audio_content->description);
1225 EXPECT_TRUE(
1226 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1227 EXPECT_TRUE(
1228 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1229
1230 video_content = cricket::GetFirstVideoContent(offer->description());
1231 video_desc = static_cast<const cricket::VideoContentDescription*>(
1232 video_content->description);
1233 EXPECT_TRUE(
1234 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1235 EXPECT_TRUE(
1236 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1237}
1238
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1240 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001241 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 ASSERT_EQ(1u, pc_->local_streams()->count());
1243 pc_->RemoveStream(pc_->local_streams()->at(0));
1244 EXPECT_EQ(0u, pc_->local_streams()->count());
1245}
1246
deadbeefe1f9d832016-01-14 15:35:42 -08001247// Test for AddTrack and RemoveTrack methods.
1248// Tests that the created offer includes tracks we added,
1249// and that the RtpSenders are created correctly.
1250// Also tests that RemoveTrack removes the tracks from subsequent offers.
1251TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1252 CreatePeerConnection();
1253 // Create a dummy stream, so tracks share a stream label.
zhihuang34b54c32016-08-04 11:06:50 -07001254 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001255 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1256 std::vector<MediaStreamInterface*> stream_list;
1257 stream_list.push_back(stream.get());
zhihuang34b54c32016-08-04 11:06:50 -07001258 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001259 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang34b54c32016-08-04 11:06:50 -07001260 rtc::scoped_refptr<VideoTrackInterface> video_track(
1261 pc_factory_->CreateVideoTrack(
1262 "video_track",
1263 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001264 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1265 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001266 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1267 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001268 EXPECT_EQ("audio_track", audio_sender->id());
1269 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001270 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1271 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001272 EXPECT_EQ("video_track", video_sender->id());
1273 EXPECT_EQ(video_track, video_sender->track());
1274
1275 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001276 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001277 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001278
1279 const cricket::ContentInfo* audio_content =
1280 cricket::GetFirstAudioContent(offer->description());
1281 const cricket::AudioContentDescription* audio_desc =
1282 static_cast<const cricket::AudioContentDescription*>(
1283 audio_content->description);
1284 EXPECT_TRUE(
1285 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1286
1287 const cricket::ContentInfo* video_content =
1288 cricket::GetFirstVideoContent(offer->description());
1289 const cricket::VideoContentDescription* video_desc =
1290 static_cast<const cricket::VideoContentDescription*>(
1291 video_content->description);
1292 EXPECT_TRUE(
1293 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1294
1295 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1296
1297 // Now try removing the tracks.
1298 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1299 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1300
1301 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001302 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001303
1304 audio_content = cricket::GetFirstAudioContent(offer->description());
1305 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1306 audio_content->description);
1307 EXPECT_FALSE(
1308 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1309
1310 video_content = cricket::GetFirstVideoContent(offer->description());
1311 video_desc = static_cast<const cricket::VideoContentDescription*>(
1312 video_content->description);
1313 EXPECT_FALSE(
1314 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1315
1316 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1317
1318 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1319 // should return false.
1320 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1321 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1322}
1323
1324// Test creating senders without a stream specified,
1325// expecting a random stream ID to be generated.
1326TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1327 CreatePeerConnection();
1328 // Create a dummy stream, so tracks share a stream label.
zhihuang34b54c32016-08-04 11:06:50 -07001329 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001330 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang34b54c32016-08-04 11:06:50 -07001331 rtc::scoped_refptr<VideoTrackInterface> video_track(
1332 pc_factory_->CreateVideoTrack(
1333 "video_track",
1334 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001335 auto audio_sender =
1336 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1337 auto video_sender =
1338 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1339 EXPECT_EQ("audio_track", audio_sender->id());
1340 EXPECT_EQ(audio_track, audio_sender->track());
1341 EXPECT_EQ("video_track", video_sender->id());
1342 EXPECT_EQ(video_track, video_sender->track());
1343 // If the ID is truly a random GUID, it should be infinitely unlikely they
1344 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001345 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001346}
1347
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1349 InitiateCall();
1350 WaitAndVerifyOnAddStream(kStreamLabel1);
1351 VerifyRemoteRtpHeaderExtensions();
1352}
1353
1354TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1355 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001356 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 CreateOfferAsLocalDescription();
1358 std::string offer;
1359 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1360 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1361 WaitAndVerifyOnAddStream(kStreamLabel1);
1362}
1363
1364TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1365 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001366 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367
1368 CreateOfferAsRemoteDescription();
1369 CreateAnswerAsLocalDescription();
1370
1371 WaitAndVerifyOnAddStream(kStreamLabel1);
1372}
1373
1374TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1375 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001376 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377
1378 CreateOfferAsRemoteDescription();
1379 CreatePrAnswerAsLocalDescription();
1380 CreateAnswerAsLocalDescription();
1381
1382 WaitAndVerifyOnAddStream(kStreamLabel1);
1383}
1384
1385TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1386 InitiateCall();
1387 ASSERT_EQ(1u, pc_->remote_streams()->count());
1388 pc_->RemoveStream(pc_->local_streams()->at(0));
1389 CreateOfferReceiveAnswer();
1390 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001391 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 CreateOfferReceiveAnswer();
1393}
1394
1395// Tests that after negotiating an audio only call, the respondent can perform a
1396// renegotiation that removes the audio stream.
1397TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1398 CreatePeerConnection();
1399 AddVoiceStream(kStreamLabel1);
1400 CreateOfferAsRemoteDescription();
1401 CreateAnswerAsLocalDescription();
1402
1403 ASSERT_EQ(1u, pc_->remote_streams()->count());
1404 pc_->RemoveStream(pc_->local_streams()->at(0));
1405 CreateOfferReceiveAnswer();
1406 EXPECT_EQ(0u, pc_->remote_streams()->count());
1407}
1408
1409// Test that candidates are generated and that we can parse our own candidates.
1410TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1411 CreatePeerConnection();
1412
1413 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1414 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001415 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001416 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001417 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001418 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419
1420 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001421 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001422 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001423 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424
1425 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1426 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1427
1428 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1429}
1430
deadbeefab9b2d12015-10-14 11:33:11 -07001431// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432// not unique.
1433TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1434 CreatePeerConnection();
1435 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001436 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001437 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001438 EXPECT_TRUE(offer);
1439 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440
1441 // Create a local stream with audio&video tracks having same label.
1442 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1443
1444 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001445 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446
1447 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001448 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001449 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450}
1451
1452// Test that we will get different SSRCs for each tracks in the offer and answer
1453// we created.
1454TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1455 CreatePeerConnection();
1456 // Create a local stream with audio&video tracks having different labels.
1457 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1458
1459 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001460 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001461 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 int audio_ssrc = 0;
1463 int video_ssrc = 0;
1464 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1465 &audio_ssrc));
1466 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1467 &video_ssrc));
1468 EXPECT_NE(audio_ssrc, video_ssrc);
1469
1470 // Test CreateAnswer
1471 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001472 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001473 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 audio_ssrc = 0;
1475 video_ssrc = 0;
1476 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1477 &audio_ssrc));
1478 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1479 &video_ssrc));
1480 EXPECT_NE(audio_ssrc, video_ssrc);
1481}
1482
deadbeefeb459812015-12-15 19:24:43 -08001483// Test that it's possible to call AddTrack on a MediaStream after adding
1484// the stream to a PeerConnection.
1485// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1486TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1487 CreatePeerConnection();
1488 // Create audio stream and add to PeerConnection.
1489 AddVoiceStream(kStreamLabel1);
1490 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1491
1492 // Add video track to the audio-only stream.
zhihuang34b54c32016-08-04 11:06:50 -07001493 rtc::scoped_refptr<VideoTrackInterface> video_track(
1494 pc_factory_->CreateVideoTrack(
1495 "video_label",
1496 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001497 stream->AddTrack(video_track.get());
1498
kwibergd1fe2812016-04-27 06:47:29 -07001499 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001500 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001501
1502 const cricket::MediaContentDescription* video_desc =
1503 cricket::GetFirstVideoContentDescription(offer->description());
1504 EXPECT_TRUE(video_desc != nullptr);
1505}
1506
1507// Test that it's possible to call RemoveTrack on a MediaStream after adding
1508// the stream to a PeerConnection.
1509// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1510TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1511 CreatePeerConnection();
1512 // Create audio/video stream and add to PeerConnection.
1513 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1514 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1515
1516 // Remove the video track.
1517 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1518
kwibergd1fe2812016-04-27 06:47:29 -07001519 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001520 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001521
1522 const cricket::MediaContentDescription* video_desc =
1523 cricket::GetFirstVideoContentDescription(offer->description());
1524 EXPECT_TRUE(video_desc == nullptr);
1525}
1526
deadbeefbd7d8f72015-12-18 16:58:44 -08001527// Test creating a sender with a stream ID, and ensure the ID is populated
1528// in the offer.
1529TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1530 CreatePeerConnection();
1531 pc_->CreateSender("video", kStreamLabel1);
1532
kwibergd1fe2812016-04-27 06:47:29 -07001533 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001534 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001535
1536 const cricket::MediaContentDescription* video_desc =
1537 cricket::GetFirstVideoContentDescription(offer->description());
1538 ASSERT_TRUE(video_desc != nullptr);
1539 ASSERT_EQ(1u, video_desc->streams().size());
1540 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1541}
1542
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543// Test that we can specify a certain track that we want statistics about.
1544TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1545 InitiateCall();
1546 ASSERT_LT(0u, pc_->remote_streams()->count());
1547 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang34b54c32016-08-04 11:06:50 -07001548 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1550 EXPECT_TRUE(DoGetStats(remote_audio));
1551
1552 // Remove the stream. Since we are sending to our selves the local
1553 // and the remote stream is the same.
1554 pc_->RemoveStream(pc_->local_streams()->at(0));
1555 // Do a re-negotiation.
1556 CreateOfferReceiveAnswer();
1557
1558 ASSERT_EQ(0u, pc_->remote_streams()->count());
1559
1560 // Test that we still can get statistics for the old track. Even if it is not
1561 // sent any longer.
1562 EXPECT_TRUE(DoGetStats(remote_audio));
1563}
1564
1565// Test that we can get stats on a video track.
1566TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1567 InitiateCall();
1568 ASSERT_LT(0u, pc_->remote_streams()->count());
1569 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang34b54c32016-08-04 11:06:50 -07001570 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1572 EXPECT_TRUE(DoGetStats(remote_video));
1573}
1574
1575// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001576// TODO(tommi): Fix this test. DoGetStats will return true
1577// for the unknown track (since GetStats is async), but no
1578// data is returned for the track.
1579TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 InitiateCall();
zhihuang34b54c32016-08-04 11:06:50 -07001581 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 pc_factory_->CreateAudioTrack("unknown track", NULL));
1583 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1584}
1585
1586// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 FakeConstraints constraints;
1589 constraints.SetAllowRtpDataChannels();
1590 CreatePeerConnection(&constraints);
zhihuang34b54c32016-08-04 11:06:50 -07001591 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 pc_->CreateDataChannel("test1", NULL);
zhihuang34b54c32016-08-04 11:06:50 -07001593 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 pc_->CreateDataChannel("test2", NULL);
1595 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001596 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 new MockDataChannelObserver(data2));
1600
1601 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1602 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1603 std::string data_to_send1 = "testing testing";
1604 std::string data_to_send2 = "testing something else";
1605 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1606
1607 CreateOfferReceiveAnswer();
1608 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1609 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1610
1611 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1612 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1613 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1614 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1615
1616 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1617 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1618
1619 data1->Close();
1620 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1621 CreateOfferReceiveAnswer();
1622 EXPECT_FALSE(observer1->IsOpen());
1623 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1624 EXPECT_TRUE(observer2->IsOpen());
1625
1626 data_to_send2 = "testing something else again";
1627 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1628
1629 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1630}
1631
1632// This test verifies that sendnig binary data over RTP data channels should
1633// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 FakeConstraints constraints;
1636 constraints.SetAllowRtpDataChannels();
1637 CreatePeerConnection(&constraints);
zhihuang34b54c32016-08-04 11:06:50 -07001638 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 pc_->CreateDataChannel("test1", NULL);
zhihuang34b54c32016-08-04 11:06:50 -07001640 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 pc_->CreateDataChannel("test2", NULL);
1642 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001643 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001645 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 new MockDataChannelObserver(data2));
1647
1648 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1649 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1650
1651 CreateOfferReceiveAnswer();
1652 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1653 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1654
1655 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1656 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1657
jbaucheec21bd2016-03-20 06:15:43 -07001658 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1660}
1661
1662// This test setup a RTP data channels in loop back and test that a channel is
1663// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 FakeConstraints constraints;
1666 constraints.SetAllowRtpDataChannels();
1667 CreatePeerConnection(&constraints);
zhihuang34b54c32016-08-04 11:06:50 -07001668 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001670 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 new MockDataChannelObserver(data1));
1672
1673 CreateOfferReceiveAnswerWithoutSsrc();
1674
1675 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1676
1677 data1->Close();
1678 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1679 CreateOfferReceiveAnswerWithoutSsrc();
1680 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1681 EXPECT_FALSE(observer1->IsOpen());
1682}
1683
1684// This test that if a data channel is added in an answer a receive only channel
1685// channel is created.
1686TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1687 FakeConstraints constraints;
1688 constraints.SetAllowRtpDataChannels();
1689 CreatePeerConnection(&constraints);
1690
1691 std::string offer_label = "offer_channel";
zhihuang34b54c32016-08-04 11:06:50 -07001692 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 pc_->CreateDataChannel(offer_label, NULL);
1694
1695 CreateOfferAsLocalDescription();
1696
1697 // Replace the data channel label in the offer and apply it as an answer.
1698 std::string receive_label = "answer_channel";
1699 std::string sdp;
1700 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001701 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 receive_label.c_str(), receive_label.length(),
1703 &sdp);
1704 CreateAnswerAsRemoteDescription(sdp);
1705
1706 // Verify that a new incoming data channel has been created and that
1707 // it is open but can't we written to.
1708 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1709 DataChannelInterface* received_channel = observer_.last_datachannel_;
1710 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1711 EXPECT_EQ(receive_label, received_channel->label());
1712 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1713
1714 // Verify that the channel we initially offered has been rejected.
1715 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1716
1717 // Do another offer / answer exchange and verify that the data channel is
1718 // opened.
1719 CreateOfferReceiveAnswer();
1720 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1721 kTimeout);
1722}
1723
1724// This test that no data channel is returned if a reliable channel is
1725// requested.
1726// TODO(perkj): Remove this test once reliable channels are implemented.
1727TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1728 FakeConstraints constraints;
1729 constraints.SetAllowRtpDataChannels();
1730 CreatePeerConnection(&constraints);
1731
1732 std::string label = "test";
1733 webrtc::DataChannelInit config;
1734 config.reliable = true;
zhihuang34b54c32016-08-04 11:06:50 -07001735 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 pc_->CreateDataChannel(label, &config);
1737 EXPECT_TRUE(channel == NULL);
1738}
1739
deadbeefab9b2d12015-10-14 11:33:11 -07001740// Verifies that duplicated label is not allowed for RTP data channel.
1741TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1742 FakeConstraints constraints;
1743 constraints.SetAllowRtpDataChannels();
1744 CreatePeerConnection(&constraints);
1745
1746 std::string label = "test";
zhihuang34b54c32016-08-04 11:06:50 -07001747 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001748 pc_->CreateDataChannel(label, nullptr);
1749 EXPECT_NE(channel, nullptr);
1750
zhihuang34b54c32016-08-04 11:06:50 -07001751 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001752 pc_->CreateDataChannel(label, nullptr);
1753 EXPECT_EQ(dup_channel, nullptr);
1754}
1755
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756// This tests that a SCTP data channel is returned using different
1757// DataChannelInit configurations.
1758TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1759 FakeConstraints constraints;
1760 constraints.SetAllowDtlsSctpDataChannels();
1761 CreatePeerConnection(&constraints);
1762
1763 webrtc::DataChannelInit config;
1764
zhihuang34b54c32016-08-04 11:06:50 -07001765 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 pc_->CreateDataChannel("1", &config);
1767 EXPECT_TRUE(channel != NULL);
1768 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001769 EXPECT_TRUE(observer_.renegotiation_needed_);
1770 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771
1772 config.ordered = false;
1773 channel = pc_->CreateDataChannel("2", &config);
1774 EXPECT_TRUE(channel != NULL);
1775 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001776 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777
1778 config.ordered = true;
1779 config.maxRetransmits = 0;
1780 channel = pc_->CreateDataChannel("3", &config);
1781 EXPECT_TRUE(channel != NULL);
1782 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001783 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784
1785 config.maxRetransmits = -1;
1786 config.maxRetransmitTime = 0;
1787 channel = pc_->CreateDataChannel("4", &config);
1788 EXPECT_TRUE(channel != NULL);
1789 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001790 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
1793// This tests that no data channel is returned if both maxRetransmits and
1794// maxRetransmitTime are set for SCTP data channels.
1795TEST_F(PeerConnectionInterfaceTest,
1796 CreateSctpDataChannelShouldFailForInvalidConfig) {
1797 FakeConstraints constraints;
1798 constraints.SetAllowDtlsSctpDataChannels();
1799 CreatePeerConnection(&constraints);
1800
1801 std::string label = "test";
1802 webrtc::DataChannelInit config;
1803 config.maxRetransmits = 0;
1804 config.maxRetransmitTime = 0;
1805
zhihuang34b54c32016-08-04 11:06:50 -07001806 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 pc_->CreateDataChannel(label, &config);
1808 EXPECT_TRUE(channel == NULL);
1809}
1810
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811// The test verifies that creating a SCTP data channel with an id already in use
1812// or out of range should fail.
1813TEST_F(PeerConnectionInterfaceTest,
1814 CreateSctpDataChannelWithInvalidIdShouldFail) {
1815 FakeConstraints constraints;
1816 constraints.SetAllowDtlsSctpDataChannels();
1817 CreatePeerConnection(&constraints);
1818
1819 webrtc::DataChannelInit config;
zhihuang34b54c32016-08-04 11:06:50 -07001820 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001822 config.id = 1;
1823 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 EXPECT_TRUE(channel != NULL);
1825 EXPECT_EQ(1, channel->id());
1826
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 channel = pc_->CreateDataChannel("x", &config);
1828 EXPECT_TRUE(channel == NULL);
1829
1830 config.id = cricket::kMaxSctpSid;
1831 channel = pc_->CreateDataChannel("max", &config);
1832 EXPECT_TRUE(channel != NULL);
1833 EXPECT_EQ(config.id, channel->id());
1834
1835 config.id = cricket::kMaxSctpSid + 1;
1836 channel = pc_->CreateDataChannel("x", &config);
1837 EXPECT_TRUE(channel == NULL);
1838}
1839
deadbeefab9b2d12015-10-14 11:33:11 -07001840// Verifies that duplicated label is allowed for SCTP data channel.
1841TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1842 FakeConstraints constraints;
1843 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1844 true);
1845 CreatePeerConnection(&constraints);
1846
1847 std::string label = "test";
zhihuang34b54c32016-08-04 11:06:50 -07001848 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001849 pc_->CreateDataChannel(label, nullptr);
1850 EXPECT_NE(channel, nullptr);
1851
zhihuang34b54c32016-08-04 11:06:50 -07001852 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001853 pc_->CreateDataChannel(label, nullptr);
1854 EXPECT_NE(dup_channel, nullptr);
1855}
1856
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001857// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1858// DataChannel.
1859TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1860 FakeConstraints constraints;
1861 constraints.SetAllowRtpDataChannels();
1862 CreatePeerConnection(&constraints);
1863
zhihuang34b54c32016-08-04 11:06:50 -07001864 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001865 pc_->CreateDataChannel("test1", NULL);
1866 EXPECT_TRUE(observer_.renegotiation_needed_);
1867 observer_.renegotiation_needed_ = false;
1868
zhihuang34b54c32016-08-04 11:06:50 -07001869 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001870 pc_->CreateDataChannel("test2", NULL);
1871 EXPECT_TRUE(observer_.renegotiation_needed_);
1872}
1873
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 FakeConstraints constraints;
1877 constraints.SetAllowRtpDataChannels();
1878 CreatePeerConnection(&constraints);
1879
zhihuang34b54c32016-08-04 11:06:50 -07001880 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 pc_->CreateDataChannel("test1", NULL);
zhihuang34b54c32016-08-04 11:06:50 -07001882 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 pc_->CreateDataChannel("test2", NULL);
1884 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001885 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001887 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 new MockDataChannelObserver(data2));
1889
1890 CreateOfferReceiveAnswer();
1891 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1892 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1893
1894 ReleasePeerConnection();
1895 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1896 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1897}
1898
1899// This test that data channels can be rejected in an answer.
1900TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1901 FakeConstraints constraints;
1902 constraints.SetAllowRtpDataChannels();
1903 CreatePeerConnection(&constraints);
1904
zhihuang34b54c32016-08-04 11:06:50 -07001905 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 pc_->CreateDataChannel("offer_channel", NULL));
1907
1908 CreateOfferAsLocalDescription();
1909
1910 // Create an answer where the m-line for data channels are rejected.
1911 std::string sdp;
1912 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1913 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1914 SessionDescriptionInterface::kAnswer);
1915 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1916 cricket::ContentInfo* data_info =
1917 answer->description()->GetContentByName("data");
1918 data_info->rejected = true;
1919
1920 DoSetRemoteDescription(answer);
1921 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1922}
1923
1924// Test that we can create a session description from an SDP string from
1925// FireFox, use it as a remote session description, generate an answer and use
1926// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001927TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001928 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 FakeConstraints constraints;
1930 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1931 true);
1932 CreatePeerConnection(&constraints);
1933 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1934 SessionDescriptionInterface* desc =
1935 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001936 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1938 CreateAnswerAsLocalDescription();
1939 ASSERT_TRUE(pc_->local_description() != NULL);
1940 ASSERT_TRUE(pc_->remote_description() != NULL);
1941
1942 const cricket::ContentInfo* content =
1943 cricket::GetFirstAudioContent(pc_->local_description()->description());
1944 ASSERT_TRUE(content != NULL);
1945 EXPECT_FALSE(content->rejected);
1946
1947 content =
1948 cricket::GetFirstVideoContent(pc_->local_description()->description());
1949 ASSERT_TRUE(content != NULL);
1950 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001951#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 content =
1953 cricket::GetFirstDataContent(pc_->local_description()->description());
1954 ASSERT_TRUE(content != NULL);
1955 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001956#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957}
1958
1959// Test that we can create an audio only offer and receive an answer with a
1960// limited set of audio codecs and receive an updated offer with more audio
1961// codecs, where the added codecs are not supported.
1962TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1963 CreatePeerConnection();
1964 AddVoiceStream("audio_label");
1965 CreateOfferAsLocalDescription();
1966
1967 SessionDescriptionInterface* answer =
1968 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001969 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1971
1972 SessionDescriptionInterface* updated_offer =
1973 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001974 webrtc::kAudioSdpWithUnsupportedCodecs,
1975 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1977 CreateAnswerAsLocalDescription();
1978}
1979
deadbeefc80741f2015-10-22 13:14:45 -07001980// Test that if we're receiving (but not sending) a track, subsequent offers
1981// will have m-lines with a=recvonly.
1982TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1983 FakeConstraints constraints;
1984 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1985 true);
1986 CreatePeerConnection(&constraints);
1987 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1988 CreateAnswerAsLocalDescription();
1989
1990 // At this point we should be receiving stream 1, but not sending anything.
1991 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001992 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001993 DoCreateOffer(&offer, nullptr);
1994
1995 const cricket::ContentInfo* video_content =
1996 cricket::GetFirstVideoContent(offer->description());
1997 const cricket::VideoContentDescription* video_desc =
1998 static_cast<const cricket::VideoContentDescription*>(
1999 video_content->description);
2000 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2001
2002 const cricket::ContentInfo* audio_content =
2003 cricket::GetFirstAudioContent(offer->description());
2004 const cricket::AudioContentDescription* audio_desc =
2005 static_cast<const cricket::AudioContentDescription*>(
2006 audio_content->description);
2007 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2008}
2009
2010// Test that if we're receiving (but not sending) a track, and the
2011// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2012// false, the generated m-lines will be a=inactive.
2013TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2014 FakeConstraints constraints;
2015 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2016 true);
2017 CreatePeerConnection(&constraints);
2018 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2019 CreateAnswerAsLocalDescription();
2020
2021 // At this point we should be receiving stream 1, but not sending anything.
2022 // A new offer would be recvonly, but we'll set the "no receive" constraints
2023 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002024 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002025 FakeConstraints offer_constraints;
2026 offer_constraints.AddMandatory(
2027 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2028 offer_constraints.AddMandatory(
2029 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2030 DoCreateOffer(&offer, &offer_constraints);
2031
2032 const cricket::ContentInfo* video_content =
2033 cricket::GetFirstVideoContent(offer->description());
2034 const cricket::VideoContentDescription* video_desc =
2035 static_cast<const cricket::VideoContentDescription*>(
2036 video_content->description);
2037 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2038
2039 const cricket::ContentInfo* audio_content =
2040 cricket::GetFirstAudioContent(offer->description());
2041 const cricket::AudioContentDescription* audio_desc =
2042 static_cast<const cricket::AudioContentDescription*>(
2043 audio_content->description);
2044 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2045}
2046
deadbeef653b8e02015-11-11 12:55:10 -08002047// Test that we can use SetConfiguration to change the ICE servers of the
2048// PortAllocator.
2049TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2050 CreatePeerConnection();
2051
2052 PeerConnectionInterface::RTCConfiguration config;
2053 PeerConnectionInterface::IceServer server;
2054 server.uri = "stun:test_hostname";
2055 config.servers.push_back(server);
2056 EXPECT_TRUE(pc_->SetConfiguration(config));
2057
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002058 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2059 EXPECT_EQ("test_hostname",
2060 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002061}
2062
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002063TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2064 CreatePeerConnection();
2065 PeerConnectionInterface::RTCConfiguration config;
2066 config.type = PeerConnectionInterface::kRelay;
2067 EXPECT_TRUE(pc_->SetConfiguration(config));
2068 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2069}
2070
2071// Test that when SetConfiguration changes both the pool size and other
2072// attributes, the pooled session is created with the updated attributes.
2073TEST_F(PeerConnectionInterfaceTest,
2074 SetConfigurationCreatesPooledSessionCorrectly) {
2075 CreatePeerConnection();
2076 PeerConnectionInterface::RTCConfiguration config;
2077 config.ice_candidate_pool_size = 1;
2078 PeerConnectionInterface::IceServer server;
2079 server.uri = kStunAddressOnly;
2080 config.servers.push_back(server);
2081 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002082 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002083
2084 const cricket::FakePortAllocatorSession* session =
2085 static_cast<const cricket::FakePortAllocatorSession*>(
2086 port_allocator_->GetPooledSession());
2087 ASSERT_NE(nullptr, session);
2088 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002089}
2090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091// Test that PeerConnection::Close changes the states to closed and all remote
2092// tracks change state to ended.
2093TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2094 // Initialize a PeerConnection and negotiate local and remote session
2095 // description.
2096 InitiateCall();
2097 ASSERT_EQ(1u, pc_->local_streams()->count());
2098 ASSERT_EQ(1u, pc_->remote_streams()->count());
2099
2100 pc_->Close();
2101
2102 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2103 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2104 pc_->ice_connection_state());
2105 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2106 pc_->ice_gathering_state());
2107
2108 EXPECT_EQ(1u, pc_->local_streams()->count());
2109 EXPECT_EQ(1u, pc_->remote_streams()->count());
2110
zhihuang34b54c32016-08-04 11:06:50 -07002111 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2112 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002113 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002114 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002115 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2116 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2117 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118}
2119
2120// Test that PeerConnection methods fails gracefully after
2121// PeerConnection::Close has been called.
2122TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2123 CreatePeerConnection();
2124 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2125 CreateOfferAsRemoteDescription();
2126 CreateAnswerAsLocalDescription();
2127
2128 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang34b54c32016-08-04 11:06:50 -07002129 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 pc_->local_streams()->at(0);
2131
2132 pc_->Close();
2133
2134 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002135 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136
2137 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002138 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002140 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141
2142 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2143
2144 EXPECT_TRUE(pc_->local_description() != NULL);
2145 EXPECT_TRUE(pc_->remote_description() != NULL);
2146
kwibergd1fe2812016-04-27 06:47:29 -07002147 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002148 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002149 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002150 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151
2152 std::string sdp;
2153 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2154 SessionDescriptionInterface* remote_offer =
2155 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2156 sdp, NULL);
2157 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2158
2159 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2160 SessionDescriptionInterface* local_offer =
2161 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2162 sdp, NULL);
2163 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2164}
2165
2166// Test that GetStats can still be called after PeerConnection::Close.
2167TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2168 InitiateCall();
2169 pc_->Close();
2170 DoGetStats(NULL);
2171}
deadbeefab9b2d12015-10-14 11:33:11 -07002172
2173// NOTE: The series of tests below come from what used to be
2174// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2175// setting a remote or local description has the expected effects.
2176
2177// This test verifies that the remote MediaStreams corresponding to a received
2178// SDP string is created. In this test the two separate MediaStreams are
2179// signaled.
2180TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2181 FakeConstraints constraints;
2182 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2183 true);
2184 CreatePeerConnection(&constraints);
2185 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2186
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002187 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002188 EXPECT_TRUE(
2189 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2190 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2191 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2192
2193 // Create a session description based on another SDP with another
2194 // MediaStream.
2195 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2196
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002197 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002198 EXPECT_TRUE(
2199 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2200}
2201
2202// This test verifies that when remote tracks are added/removed from SDP, the
2203// created remote streams are updated appropriately.
2204TEST_F(PeerConnectionInterfaceTest,
2205 AddRemoveTrackFromExistingRemoteMediaStream) {
2206 FakeConstraints constraints;
2207 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2208 true);
2209 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002210 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002211 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002212 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2213 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2214 reference_collection_));
2215
2216 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002217 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002218 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002219 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2220 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2221 reference_collection_));
zhihuang34b54c32016-08-04 11:06:50 -07002222 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002223 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2224 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang34b54c32016-08-04 11:06:50 -07002225 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002226 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2227 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002228
2229 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002230 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002231 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002232 MockTrackObserver audio_track_observer(audio_track2);
2233 MockTrackObserver video_track_observer(video_track2);
2234
2235 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2236 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002237 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2238 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2239 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002240 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002241 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002242 audio_track2->state(), kTimeout);
2243 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2244 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002245}
2246
2247// This tests that remote tracks are ended if a local session description is set
2248// that rejects the media content type.
2249TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2250 FakeConstraints constraints;
2251 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2252 true);
2253 CreatePeerConnection(&constraints);
2254 // First create and set a remote offer, then reject its video content in our
2255 // answer.
2256 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2257 ASSERT_EQ(1u, observer_.remote_streams()->count());
2258 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2259 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2260 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2261
2262 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2263 remote_stream->GetVideoTracks()[0];
2264 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2265 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2266 remote_stream->GetAudioTracks()[0];
2267 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2268
kwibergd1fe2812016-04-27 06:47:29 -07002269 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002270 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002271 cricket::ContentInfo* video_info =
2272 local_answer->description()->GetContentByName("video");
2273 video_info->rejected = true;
2274 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2275 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2276 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2277
2278 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002279 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002280 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002281 video_info = local_offer->description()->GetContentByName("video");
2282 ASSERT_TRUE(video_info != nullptr);
2283 video_info->rejected = true;
2284 cricket::ContentInfo* audio_info =
2285 local_offer->description()->GetContentByName("audio");
2286 ASSERT_TRUE(audio_info != nullptr);
2287 audio_info->rejected = true;
2288 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002289 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002290 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002291 remote_audio->state(), kTimeout);
2292 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2293 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002294}
2295
2296// This tests that we won't crash if the remote track has been removed outside
2297// of PeerConnection and then PeerConnection tries to reject the track.
2298TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2299 FakeConstraints constraints;
2300 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2301 true);
2302 CreatePeerConnection(&constraints);
2303 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2304 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2305 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2306 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2307
kwibergd1fe2812016-04-27 06:47:29 -07002308 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002309 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2310 kSdpStringWithStream1, nullptr));
2311 cricket::ContentInfo* video_info =
2312 local_answer->description()->GetContentByName("video");
2313 video_info->rejected = true;
2314 cricket::ContentInfo* audio_info =
2315 local_answer->description()->GetContentByName("audio");
2316 audio_info->rejected = true;
2317 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2318
2319 // No crash is a pass.
2320}
2321
deadbeef5e97fb52015-10-15 12:49:08 -07002322// This tests that if a recvonly remote description is set, no remote streams
2323// will be created, even if the description contains SSRCs/MSIDs.
2324// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2325TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2326 FakeConstraints constraints;
2327 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2328 true);
2329 CreatePeerConnection(&constraints);
2330
2331 std::string recvonly_offer = kSdpStringWithStream1;
2332 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2333 strlen(kRecvonly), &recvonly_offer);
2334 CreateAndSetRemoteOffer(recvonly_offer);
2335
2336 EXPECT_EQ(0u, observer_.remote_streams()->count());
2337}
2338
deadbeefab9b2d12015-10-14 11:33:11 -07002339// This tests that a default MediaStream is created if a remote session
2340// description doesn't contain any streams and no MSID support.
2341// It also tests that the default stream is updated if a video m-line is added
2342// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002343TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002344 FakeConstraints constraints;
2345 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2346 true);
2347 CreatePeerConnection(&constraints);
2348 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2349
2350 ASSERT_EQ(1u, observer_.remote_streams()->count());
2351 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2352
2353 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2354 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2355 EXPECT_EQ("default", remote_stream->label());
2356
2357 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2358 ASSERT_EQ(1u, observer_.remote_streams()->count());
2359 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2360 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002361 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2362 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002363 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2364 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002365 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2366 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002367}
2368
2369// This tests that a default MediaStream is created if a remote session
2370// description doesn't contain any streams and media direction is send only.
2371TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002372 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002373 FakeConstraints constraints;
2374 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2375 true);
2376 CreatePeerConnection(&constraints);
2377 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2378
2379 ASSERT_EQ(1u, observer_.remote_streams()->count());
2380 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2381
2382 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2383 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2384 EXPECT_EQ("default", remote_stream->label());
2385}
2386
2387// This tests that it won't crash when PeerConnection tries to remove
2388// a remote track that as already been removed from the MediaStream.
2389TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2390 FakeConstraints constraints;
2391 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2392 true);
2393 CreatePeerConnection(&constraints);
2394 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2395 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2396 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2397 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2398
2399 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2400
2401 // No crash is a pass.
2402}
2403
2404// This tests that a default MediaStream is created if the remote session
2405// description doesn't contain any streams and don't contain an indication if
2406// MSID is supported.
2407TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002408 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002409 FakeConstraints constraints;
2410 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2411 true);
2412 CreatePeerConnection(&constraints);
2413 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2414
2415 ASSERT_EQ(1u, observer_.remote_streams()->count());
2416 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2417 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2418 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2419}
2420
2421// This tests that a default MediaStream is not created if the remote session
2422// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002423TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002424 FakeConstraints constraints;
2425 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2426 true);
2427 CreatePeerConnection(&constraints);
2428 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2429 EXPECT_EQ(0u, observer_.remote_streams()->count());
2430}
2431
deadbeefbda7e0b2015-12-08 17:13:40 -08002432// This tests that when setting a new description, the old default tracks are
2433// not destroyed and recreated.
2434// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002435TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002436 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002437 FakeConstraints constraints;
2438 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2439 true);
2440 CreatePeerConnection(&constraints);
2441 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2442
2443 ASSERT_EQ(1u, observer_.remote_streams()->count());
2444 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2445 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2446
2447 // Set the track to "disabled", then set a new description and ensure the
2448 // track is still disabled, which ensures it hasn't been recreated.
2449 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2450 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2451 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2452 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2453}
2454
deadbeefab9b2d12015-10-14 11:33:11 -07002455// This tests that a default MediaStream is not created if a remote session
2456// description is updated to not have any MediaStreams.
2457TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2458 FakeConstraints constraints;
2459 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2460 true);
2461 CreatePeerConnection(&constraints);
2462 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002463 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002464 EXPECT_TRUE(
2465 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2466
2467 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2468 EXPECT_EQ(0u, observer_.remote_streams()->count());
2469}
2470
2471// This tests that an RtpSender is created when the local description is set
2472// after adding a local stream.
2473// TODO(deadbeef): This test and the one below it need to be updated when
2474// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002475TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002476 FakeConstraints constraints;
2477 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2478 true);
2479 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002480
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002481 // Create an offer with 1 stream with 2 tracks of each type.
2482 rtc::scoped_refptr<StreamCollection> stream_collection =
2483 CreateStreamCollection(1, 2);
2484 pc_->AddStream(stream_collection->at(0));
2485 std::unique_ptr<SessionDescriptionInterface> offer;
2486 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2487 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002488
deadbeefab9b2d12015-10-14 11:33:11 -07002489 auto senders = pc_->GetSenders();
2490 EXPECT_EQ(4u, senders.size());
2491 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2492 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2493 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2494 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2495
2496 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002497 pc_->RemoveStream(stream_collection->at(0));
2498 stream_collection = CreateStreamCollection(1, 1);
2499 pc_->AddStream(stream_collection->at(0));
2500 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2501 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2502
deadbeefab9b2d12015-10-14 11:33:11 -07002503 senders = pc_->GetSenders();
2504 EXPECT_EQ(2u, senders.size());
2505 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2506 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2507 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2508 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2509}
2510
2511// This tests that an RtpSender is created when the local description is set
2512// before adding a local stream.
2513TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002514 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002515 FakeConstraints constraints;
2516 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2517 true);
2518 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002519
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002520 rtc::scoped_refptr<StreamCollection> stream_collection =
2521 CreateStreamCollection(1, 2);
2522 // Add a stream to create the offer, but remove it afterwards.
2523 pc_->AddStream(stream_collection->at(0));
2524 std::unique_ptr<SessionDescriptionInterface> offer;
2525 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2526 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002527
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002528 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002529 auto senders = pc_->GetSenders();
2530 EXPECT_EQ(0u, senders.size());
2531
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002532 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002533 senders = pc_->GetSenders();
2534 EXPECT_EQ(4u, senders.size());
2535 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2536 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2537 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2538 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2539}
2540
2541// This tests that the expected behavior occurs if the SSRC on a local track is
2542// changed when SetLocalDescription is called.
2543TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002544 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002545 FakeConstraints constraints;
2546 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2547 true);
2548 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002549
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002550 rtc::scoped_refptr<StreamCollection> stream_collection =
2551 CreateStreamCollection(2, 1);
2552 pc_->AddStream(stream_collection->at(0));
2553 std::unique_ptr<SessionDescriptionInterface> offer;
2554 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2555 // Grab a copy of the offer before it gets passed into the PC.
2556 std::unique_ptr<JsepSessionDescription> modified_offer(
2557 new JsepSessionDescription(JsepSessionDescription::kOffer));
2558 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2559 offer->session_version());
2560 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002561
deadbeefab9b2d12015-10-14 11:33:11 -07002562 auto senders = pc_->GetSenders();
2563 EXPECT_EQ(2u, senders.size());
2564 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2565 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2566
2567 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002568 cricket::MediaContentDescription* desc =
2569 cricket::GetFirstAudioContentDescription(modified_offer->description());
2570 ASSERT_TRUE(desc != NULL);
2571 for (StreamParams& stream : desc->mutable_streams()) {
2572 for (unsigned int& ssrc : stream.ssrcs) {
2573 ++ssrc;
2574 }
2575 }
deadbeefab9b2d12015-10-14 11:33:11 -07002576
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002577 desc =
2578 cricket::GetFirstVideoContentDescription(modified_offer->description());
2579 ASSERT_TRUE(desc != NULL);
2580 for (StreamParams& stream : desc->mutable_streams()) {
2581 for (unsigned int& ssrc : stream.ssrcs) {
2582 ++ssrc;
2583 }
2584 }
2585
2586 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002587 senders = pc_->GetSenders();
2588 EXPECT_EQ(2u, senders.size());
2589 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2590 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2591 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2592 // changed.
2593}
2594
2595// This tests that the expected behavior occurs if a new session description is
2596// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002597TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002598 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002599 FakeConstraints constraints;
2600 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2601 true);
2602 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002603
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002604 rtc::scoped_refptr<StreamCollection> stream_collection =
2605 CreateStreamCollection(2, 1);
2606 pc_->AddStream(stream_collection->at(0));
2607 std::unique_ptr<SessionDescriptionInterface> offer;
2608 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2609 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002610
deadbeefab9b2d12015-10-14 11:33:11 -07002611 auto senders = pc_->GetSenders();
2612 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002613 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2614 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002615
2616 // Add a new MediaStream but with the same tracks as in the first stream.
2617 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2618 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002619 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2620 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002621 pc_->AddStream(stream_1);
2622
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002623 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2624 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002625
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002626 auto new_senders = pc_->GetSenders();
2627 // Should be the same senders as before, but with updated stream id.
2628 // Note that this behavior is subject to change in the future.
2629 // We may decide the PC should ignore existing tracks in AddStream.
2630 EXPECT_EQ(senders, new_senders);
2631 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2632 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002633}
2634
nisse51542be2016-02-12 02:27:06 -08002635class PeerConnectionMediaConfigTest : public testing::Test {
2636 protected:
2637 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002638 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002639 pcf_->Initialize();
2640 }
2641 const cricket::MediaConfig& TestCreatePeerConnection(
2642 const PeerConnectionInterface::RTCConfiguration& config,
2643 const MediaConstraintsInterface *constraints) {
2644 pcf_->create_media_controller_called_ = false;
2645
zhihuang34b54c32016-08-04 11:06:50 -07002646 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2647 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002648 EXPECT_TRUE(pc.get());
2649 EXPECT_TRUE(pcf_->create_media_controller_called_);
2650 return pcf_->create_media_controller_config_;
2651 }
2652
zhihuang34b54c32016-08-04 11:06:50 -07002653 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002654 MockPeerConnectionObserver observer_;
2655};
2656
2657// This test verifies the default behaviour with no constraints and a
2658// default RTCConfiguration.
2659TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2660 PeerConnectionInterface::RTCConfiguration config;
2661 FakeConstraints constraints;
2662
2663 const cricket::MediaConfig& media_config =
2664 TestCreatePeerConnection(config, &constraints);
2665
2666 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002667 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2668 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2669 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002670}
2671
2672// This test verifies the DSCP constraint is recognized and passed to
2673// the CreateMediaController call.
2674TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2675 PeerConnectionInterface::RTCConfiguration config;
2676 FakeConstraints constraints;
2677
2678 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2679 const cricket::MediaConfig& media_config =
2680 TestCreatePeerConnection(config, &constraints);
2681
2682 EXPECT_TRUE(media_config.enable_dscp);
2683}
2684
2685// This test verifies the cpu overuse detection constraint is
2686// recognized and passed to the CreateMediaController call.
2687TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2688 PeerConnectionInterface::RTCConfiguration config;
2689 FakeConstraints constraints;
2690
2691 constraints.AddOptional(
2692 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2693 const cricket::MediaConfig media_config =
2694 TestCreatePeerConnection(config, &constraints);
2695
nisse0db023a2016-03-01 04:29:59 -08002696 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002697}
2698
2699// This test verifies that the disable_prerenderer_smoothing flag is
2700// propagated from RTCConfiguration to the CreateMediaController call.
2701TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2702 PeerConnectionInterface::RTCConfiguration config;
2703 FakeConstraints constraints;
2704
Niels Möller71bdda02016-03-31 12:59:59 +02002705 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002706 const cricket::MediaConfig& media_config =
2707 TestCreatePeerConnection(config, &constraints);
2708
nisse0db023a2016-03-01 04:29:59 -08002709 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2710}
2711
2712// This test verifies the suspend below min bitrate constraint is
2713// recognized and passed to the CreateMediaController call.
2714TEST_F(PeerConnectionMediaConfigTest,
2715 TestSuspendBelowMinBitrateConstraintTrue) {
2716 PeerConnectionInterface::RTCConfiguration config;
2717 FakeConstraints constraints;
2718
2719 constraints.AddOptional(
2720 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2721 true);
2722 const cricket::MediaConfig media_config =
2723 TestCreatePeerConnection(config, &constraints);
2724
2725 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002726}
2727
deadbeefab9b2d12015-10-14 11:33:11 -07002728// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002729// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2730// "verify options are converted correctly", should be "pass options into
2731// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002732
2733TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2734 RTCOfferAnswerOptions rtc_options;
2735 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2736
2737 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002738 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002739
2740 rtc_options.offer_to_receive_audio =
2741 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002742 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002743}
2744
2745TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2746 RTCOfferAnswerOptions rtc_options;
2747 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2748
2749 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002750 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002751
2752 rtc_options.offer_to_receive_video =
2753 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002754 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002755}
2756
2757// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002758// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002759TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2760 RTCOfferAnswerOptions rtc_options;
2761 rtc_options.offer_to_receive_audio = 1;
2762 rtc_options.offer_to_receive_video = 1;
2763
2764 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002765 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002766 EXPECT_TRUE(options.has_audio());
2767 EXPECT_TRUE(options.has_video());
2768 EXPECT_TRUE(options.bundle_enabled);
2769}
2770
2771// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002772// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002773TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2774 RTCOfferAnswerOptions rtc_options;
2775 rtc_options.offer_to_receive_audio = 1;
2776
2777 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002778 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002779 EXPECT_TRUE(options.has_audio());
2780 EXPECT_FALSE(options.has_video());
2781 EXPECT_TRUE(options.bundle_enabled);
2782}
2783
2784// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002785// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002786TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2787 RTCOfferAnswerOptions rtc_options;
2788
2789 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002790 options.transport_options["audio"] = cricket::TransportOptions();
2791 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002792 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002793 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002794 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002795 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002796 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002797 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2798 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002799}
2800
2801// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002802// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002803TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2804 RTCOfferAnswerOptions rtc_options;
2805 rtc_options.offer_to_receive_audio = 0;
2806 rtc_options.offer_to_receive_video = 1;
2807
2808 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002809 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002810 EXPECT_FALSE(options.has_audio());
2811 EXPECT_TRUE(options.has_video());
2812 EXPECT_TRUE(options.bundle_enabled);
2813}
2814
2815// Test that a correct MediaSessionOptions is created for an offer if
2816// UseRtpMux is set to false.
2817TEST(CreateSessionOptionsTest,
2818 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2819 RTCOfferAnswerOptions rtc_options;
2820 rtc_options.offer_to_receive_audio = 1;
2821 rtc_options.offer_to_receive_video = 1;
2822 rtc_options.use_rtp_mux = false;
2823
2824 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002825 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002826 EXPECT_TRUE(options.has_audio());
2827 EXPECT_TRUE(options.has_video());
2828 EXPECT_FALSE(options.bundle_enabled);
2829}
2830
2831// Test that a correct MediaSessionOptions is created to restart ice if
2832// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002833// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002834TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2835 RTCOfferAnswerOptions rtc_options;
2836 rtc_options.ice_restart = true;
2837
2838 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002839 options.transport_options["audio"] = cricket::TransportOptions();
2840 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002841 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002842 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2843 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002844
2845 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002846 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002847 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2848 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002849}
2850
2851// Test that the MediaConstraints in an answer don't affect if audio and video
2852// is offered in an offer but that if kOfferToReceiveAudio or
2853// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2854// included in subsequent answers.
2855TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2856 FakeConstraints answer_c;
2857 answer_c.SetMandatoryReceiveAudio(true);
2858 answer_c.SetMandatoryReceiveVideo(true);
2859
2860 cricket::MediaSessionOptions answer_options;
2861 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2862 EXPECT_TRUE(answer_options.has_audio());
2863 EXPECT_TRUE(answer_options.has_video());
2864
deadbeefc80741f2015-10-22 13:14:45 -07002865 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002866
2867 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002868 EXPECT_TRUE(
2869 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002870 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002871 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002872
deadbeefc80741f2015-10-22 13:14:45 -07002873 RTCOfferAnswerOptions updated_rtc_offer_options;
2874 updated_rtc_offer_options.offer_to_receive_audio = 1;
2875 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002876
2877 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002878 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002879 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002880 EXPECT_TRUE(updated_offer_options.has_audio());
2881 EXPECT_TRUE(updated_offer_options.has_video());
2882
2883 // Since an offer has been created with both audio and video, subsequent
2884 // offers and answers should contain both audio and video.
2885 // Answers will only contain the media types that exist in the offer
2886 // regardless of the value of |updated_answer_options.has_audio| and
2887 // |updated_answer_options.has_video|.
2888 FakeConstraints updated_answer_c;
2889 answer_c.SetMandatoryReceiveAudio(false);
2890 answer_c.SetMandatoryReceiveVideo(false);
2891
2892 cricket::MediaSessionOptions updated_answer_options;
2893 EXPECT_TRUE(
2894 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2895 EXPECT_TRUE(updated_answer_options.has_audio());
2896 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002897}