blob: 419f69c5636705843bee89cf7078af3a51c79cb1 [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;
deadbeef907abe42016-08-04 12:22:18 -0700245using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700247using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248using webrtc::AudioTrackInterface;
249using webrtc::DataBuffer;
250using webrtc::DataChannelInterface;
251using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700253using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700254using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700255using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256using webrtc::MediaStreamInterface;
257using webrtc::MediaStreamTrackInterface;
258using webrtc::MockCreateSessionDescriptionObserver;
259using webrtc::MockDataChannelObserver;
260using webrtc::MockSetSessionDescriptionObserver;
261using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700262using webrtc::NotifierInterface;
263using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264using webrtc::PeerConnectionInterface;
265using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700266using webrtc::RtpReceiverInterface;
267using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268using webrtc::SdpParseError;
269using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700270using webrtc::StreamCollection;
271using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100272using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700273using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274using webrtc::VideoTrackInterface;
275
deadbeefab9b2d12015-10-14 11:33:11 -0700276typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278namespace {
279
280// Gets the first ssrc of given content type from the ContentInfo.
281bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
282 if (!content_info || !ssrc) {
283 return false;
284 }
285 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000286 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 content_info->description);
288 if (!media_desc || media_desc->streams().empty()) {
289 return false;
290 }
291 *ssrc = media_desc->streams().begin()->first_ssrc();
292 return true;
293}
294
295void SetSsrcToZero(std::string* sdp) {
296 const char kSdpSsrcAtribute[] = "a=ssrc:";
297 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
298 size_t ssrc_pos = 0;
299 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
300 std::string::npos) {
301 size_t end_ssrc = sdp->find(" ", ssrc_pos);
302 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
303 ssrc_pos = end_ssrc;
304 }
305}
306
deadbeefab9b2d12015-10-14 11:33:11 -0700307// Check if |streams| contains the specified track.
308bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
309 const std::string& stream_label,
310 const std::string& track_id) {
311 for (const cricket::StreamParams& params : streams) {
312 if (params.sync_label == stream_label && params.id == track_id) {
313 return true;
314 }
315 }
316 return false;
317}
318
319// Check if |senders| contains the specified sender, by id.
320bool ContainsSender(
321 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
322 const std::string& id) {
323 for (const auto& sender : senders) {
324 if (sender->id() == id) {
325 return true;
326 }
327 }
328 return false;
329}
330
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700331// Check if |senders| contains the specified sender, by id and stream id.
332bool ContainsSender(
333 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
334 const std::string& id,
335 const std::string& stream_id) {
336 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700337 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700338 return true;
339 }
340 }
341 return false;
342}
343
deadbeefab9b2d12015-10-14 11:33:11 -0700344// Create a collection of streams.
345// CreateStreamCollection(1) creates a collection that
346// correspond to kSdpStringWithStream1.
347// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
348rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700349 int number_of_streams,
350 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700351 rtc::scoped_refptr<StreamCollection> local_collection(
352 StreamCollection::Create());
353
354 for (int i = 0; i < number_of_streams; ++i) {
355 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
356 webrtc::MediaStream::Create(kStreams[i]));
357
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700358 for (int j = 0; j < tracks_per_stream; ++j) {
359 // Add a local audio track.
360 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
361 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
362 nullptr));
363 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700364
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700365 // Add a local video track.
366 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
367 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
368 webrtc::FakeVideoTrackSource::Create()));
369 stream->AddTrack(video_track);
370 }
deadbeefab9b2d12015-10-14 11:33:11 -0700371
372 local_collection->AddStream(stream);
373 }
374 return local_collection;
375}
376
377// Check equality of StreamCollections.
378bool CompareStreamCollections(StreamCollectionInterface* s1,
379 StreamCollectionInterface* s2) {
380 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
381 return false;
382 }
383
384 for (size_t i = 0; i != s1->count(); ++i) {
385 if (s1->at(i)->label() != s2->at(i)->label()) {
386 return false;
387 }
388 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
389 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
390 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
391 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
392
393 if (audio_tracks1.size() != audio_tracks2.size()) {
394 return false;
395 }
396 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
397 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
398 return false;
399 }
400 }
401 if (video_tracks1.size() != video_tracks2.size()) {
402 return false;
403 }
404 for (size_t j = 0; j != video_tracks1.size(); ++j) {
405 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
406 return false;
407 }
408 }
409 }
410 return true;
411}
412
perkjd61bf802016-03-24 03:16:19 -0700413// Helper class to test Observer.
414class MockTrackObserver : public ObserverInterface {
415 public:
416 explicit MockTrackObserver(NotifierInterface* notifier)
417 : notifier_(notifier) {
418 notifier_->RegisterObserver(this);
419 }
420
421 ~MockTrackObserver() { Unregister(); }
422
423 void Unregister() {
424 if (notifier_) {
425 notifier_->UnregisterObserver(this);
426 notifier_ = nullptr;
427 }
428 }
429
430 MOCK_METHOD0(OnChanged, void());
431
432 private:
433 NotifierInterface* notifier_;
434};
435
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436class MockPeerConnectionObserver : public PeerConnectionObserver {
437 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700438 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200439 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 }
441 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
442 pc_ = pc;
443 if (pc) {
444 state_ = pc_->signaling_state();
445 }
446 }
nisseef8b61e2016-04-29 06:09:15 -0700447 void OnSignalingChange(
448 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 EXPECT_EQ(pc_->signaling_state(), new_state);
450 state_ = new_state;
451 }
452 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
453 virtual void OnStateChange(StateType state_changed) {
454 if (pc_.get() == NULL)
455 return;
456 switch (state_changed) {
457 case kSignalingState:
458 // OnSignalingChange and OnStateChange(kSignalingState) should always
459 // be called approximately simultaneously. To ease testing, we require
460 // that they always be called in that order. This check verifies
461 // that OnSignalingChange has just been called.
462 EXPECT_EQ(pc_->signaling_state(), state_);
463 break;
464 case kIceState:
465 ADD_FAILURE();
466 break;
467 default:
468 ADD_FAILURE();
469 break;
470 }
471 }
deadbeefab9b2d12015-10-14 11:33:11 -0700472
473 MediaStreamInterface* RemoteStream(const std::string& label) {
474 return remote_streams_->find(label);
475 }
476 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700477 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700479 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700481 void OnRemoveStream(
482 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700484 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 }
perkjdfb769d2016-02-09 03:09:43 -0800486 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700487 void OnDataChannel(
488 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 last_datachannel_ = data_channel;
490 }
491
perkjdfb769d2016-02-09 03:09:43 -0800492 void OnIceConnectionChange(
493 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang29ff8442016-07-27 11:07:25 -0700495 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
perkjdfb769d2016-02-09 03:09:43 -0800497 void OnIceGatheringChange(
498 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800500 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang29ff8442016-07-27 11:07:25 -0700501 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
perkjdfb769d2016-02-09 03:09:43 -0800503 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
505 pc_->ice_gathering_state());
506
507 std::string sdp;
508 EXPECT_TRUE(candidate->ToString(&sdp));
509 EXPECT_LT(0u, sdp.size());
510 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
511 candidate->sdp_mline_index(), sdp, NULL));
512 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang29ff8442016-07-27 11:07:25 -0700513 callback_triggered = true;
514 }
515
516 void OnIceCandidatesRemoved(
517 const std::vector<cricket::Candidate>& candidates) override {
518 callback_triggered = true;
519 }
520
521 void OnIceConnectionReceivingChange(bool receiving) override {
522 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
525 // Returns the label of the last added stream.
526 // Empty string if no stream have been added.
527 std::string GetLastAddedStreamLabel() {
528 if (last_added_stream_.get())
529 return last_added_stream_->label();
530 return "";
531 }
532 std::string GetLastRemovedStreamLabel() {
533 if (last_removed_stream_.get())
534 return last_removed_stream_->label();
535 return "";
536 }
537
deadbeef907abe42016-08-04 12:22:18 -0700538 scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700540 std::unique_ptr<IceCandidateInterface> last_candidate_;
deadbeef907abe42016-08-04 12:22:18 -0700541 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700542 rtc::scoped_refptr<StreamCollection> remote_streams_;
543 bool renegotiation_needed_ = false;
544 bool ice_complete_ = false;
zhihuang29ff8442016-07-27 11:07:25 -0700545 bool callback_triggered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
547 private:
deadbeef907abe42016-08-04 12:22:18 -0700548 scoped_refptr<MediaStreamInterface> last_added_stream_;
549 scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550};
551
552} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700553
zhihuang29ff8442016-07-27 11:07:25 -0700554// The PeerConnectionMediaConfig tests below verify that configuration
555// and constraints are propagated into the MediaConfig passed to
556// CreateMediaController. These settings are intended for MediaChannel
557// constructors, but that is not exercised by these unittest.
558class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
559 public:
560 webrtc::MediaControllerInterface* CreateMediaController(
561 const cricket::MediaConfig& config) const override {
562 create_media_controller_called_ = true;
563 create_media_controller_config_ = config;
564
565 webrtc::MediaControllerInterface* mc =
566 PeerConnectionFactory::CreateMediaController(config);
567 EXPECT_TRUE(mc != nullptr);
568 return mc;
569 }
570
571 cricket::TransportController* CreateTransportController(
572 cricket::PortAllocator* port_allocator) override {
573 transport_controller = new cricket::TransportController(
574 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator);
575 return transport_controller;
576 }
577
578 cricket::TransportController* transport_controller;
579 // Mutable, so they can be modified in the above const-declared method.
580 mutable bool create_media_controller_called_ = false;
581 mutable cricket::MediaConfig create_media_controller_config_;
582};
583
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584class PeerConnectionInterfaceTest : public testing::Test {
585 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800586 PeerConnectionInterfaceTest() {
587#ifdef WEBRTC_ANDROID
588 webrtc::InitializeAndroidObjects();
589#endif
590 }
591
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 virtual void SetUp() {
593 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700594 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
595 nullptr, nullptr, nullptr);
596 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700597 pc_factory_for_test_ =
598 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
599 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 }
601
602 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700603 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
605
606 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700607 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
608 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 }
610
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700611 void CreatePeerConnectionWithIceTransportsType(
612 PeerConnectionInterface::IceTransportsType type) {
613 PeerConnectionInterface::RTCConfiguration config;
614 config.type = type;
615 return CreatePeerConnection(config, nullptr);
616 }
617
618 void CreatePeerConnectionWithIceServer(const std::string& uri,
619 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800620 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700622 server.uri = uri;
623 server.password = password;
624 config.servers.push_back(server);
625 CreatePeerConnection(config, nullptr);
626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700628 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
629 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700630 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800631 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
632 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000633
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000634 // DTLS does not work in a loopback call, so is disabled for most of the
635 // tests in this file. We only create a FakeIdentityService if the test
636 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000637 FakeConstraints default_constraints;
638 if (!constraints) {
639 constraints = &default_constraints;
640
641 default_constraints.AddMandatory(
642 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
643 }
644
Henrik Boströmd79599d2016-06-01 13:58:50 +0200645 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000646 bool dtls;
647 if (FindConstraint(constraints,
648 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
649 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200650 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200651 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000652 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200653 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800654 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200655 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 ASSERT_TRUE(pc_.get() != NULL);
657 observer_.SetPeerConnectionInterface(pc_.get());
658 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
659 }
660
deadbeef0a6c4ca2015-10-06 11:38:28 -0700661 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800662 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700663 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700664 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800665 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700666
deadbeef907abe42016-08-04 12:22:18 -0700667 scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700668 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
669 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800670 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700671 }
672
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700674 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
676 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
677 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800679 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680
deadbeef0a6c4ca2015-10-06 11:38:28 -0700681 CreatePeerConnectionExpectFail(kStunInvalidPort);
682 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
683 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700685 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800686 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
687 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800689 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800693 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
695
696 void ReleasePeerConnection() {
697 pc_ = NULL;
698 observer_.SetPeerConnectionInterface(NULL);
699 }
700
deadbeefab9b2d12015-10-14 11:33:11 -0700701 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 // Create a local stream.
deadbeef907abe42016-08-04 12:22:18 -0700703 scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 pc_factory_->CreateLocalMediaStream(label));
deadbeef907abe42016-08-04 12:22:18 -0700705 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
deadbeef907abe42016-08-04 12:22:18 -0700707 scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 pc_factory_->CreateVideoTrack(label + "v0", video_source));
709 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000710 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
712 observer_.renegotiation_needed_ = false;
713 }
714
715 void AddVoiceStream(const std::string& label) {
716 // Create a local stream.
deadbeef907abe42016-08-04 12:22:18 -0700717 scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 pc_factory_->CreateLocalMediaStream(label));
deadbeef907abe42016-08-04 12:22:18 -0700719 scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 pc_factory_->CreateAudioTrack(label + "a0", NULL));
721 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000722 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
724 observer_.renegotiation_needed_ = false;
725 }
726
727 void AddAudioVideoStream(const std::string& stream_label,
728 const std::string& audio_track_label,
729 const std::string& video_track_label) {
730 // Create a local stream.
deadbeef907abe42016-08-04 12:22:18 -0700731 scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 pc_factory_->CreateLocalMediaStream(stream_label));
deadbeef907abe42016-08-04 12:22:18 -0700733 scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 pc_factory_->CreateAudioTrack(
735 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
736 stream->AddTrack(audio_track.get());
deadbeef907abe42016-08-04 12:22:18 -0700737 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700738 pc_factory_->CreateVideoTrack(
739 video_track_label,
740 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000742 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
744 observer_.renegotiation_needed_ = false;
745 }
746
kwibergd1fe2812016-04-27 06:47:29 -0700747 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700748 bool offer,
749 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000750 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
751 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 MockCreateSessionDescriptionObserver>());
753 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700754 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700756 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700759 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 return observer->result();
761 }
762
kwibergd1fe2812016-04-27 06:47:29 -0700763 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700764 MediaConstraintsInterface* constraints) {
765 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 }
767
kwibergd1fe2812016-04-27 06:47:29 -0700768 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700769 MediaConstraintsInterface* constraints) {
770 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 }
772
773 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000774 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
775 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 MockSetSessionDescriptionObserver>());
777 if (local) {
778 pc_->SetLocalDescription(observer, desc);
779 } else {
780 pc_->SetRemoteDescription(observer, desc);
781 }
zhihuang29ff8442016-07-27 11:07:25 -0700782 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
783 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
784 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 return observer->result();
786 }
787
788 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
789 return DoSetSessionDescription(desc, true);
790 }
791
792 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
793 return DoSetSessionDescription(desc, false);
794 }
795
796 // Calls PeerConnection::GetStats and check the return value.
797 // It does not verify the values in the StatReports since a RTCP packet might
798 // be required.
799 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000800 rtc::scoped_refptr<MockStatsObserver> observer(
801 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000802 if (!pc_->GetStats(
803 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 return false;
805 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
806 return observer->called();
807 }
808
809 void InitiateCall() {
810 CreatePeerConnection();
811 // Create a local stream with audio&video tracks.
812 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
813 CreateOfferReceiveAnswer();
814 }
815
816 // Verify that RTP Header extensions has been negotiated for audio and video.
817 void VerifyRemoteRtpHeaderExtensions() {
818 const cricket::MediaContentDescription* desc =
819 cricket::GetFirstAudioContentDescription(
820 pc_->remote_description()->description());
821 ASSERT_TRUE(desc != NULL);
822 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
823
824 desc = cricket::GetFirstVideoContentDescription(
825 pc_->remote_description()->description());
826 ASSERT_TRUE(desc != NULL);
827 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
828 }
829
830 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700831 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700832 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 std::string sdp;
834 EXPECT_TRUE(offer->ToString(&sdp));
835 SessionDescriptionInterface* remote_offer =
836 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
837 sdp, NULL);
838 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
839 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
840 }
841
deadbeefab9b2d12015-10-14 11:33:11 -0700842 void CreateAndSetRemoteOffer(const std::string& sdp) {
843 SessionDescriptionInterface* remote_offer =
844 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
845 sdp, nullptr);
846 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
847 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
848 }
849
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700851 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700852 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853
854 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
855 // audio codec change, even if the parameter has nothing to do with
856 // receiving. Not all parameters are serialized to SDP.
857 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
858 // the SessionDescription, it is necessary to do that here to in order to
859 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
860 // https://code.google.com/p/webrtc/issues/detail?id=1356
861 std::string sdp;
862 EXPECT_TRUE(answer->ToString(&sdp));
863 SessionDescriptionInterface* new_answer =
864 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
865 sdp, NULL);
866 EXPECT_TRUE(DoSetLocalDescription(new_answer));
867 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
868 }
869
870 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700871 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700872 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873
874 std::string sdp;
875 EXPECT_TRUE(answer->ToString(&sdp));
876 SessionDescriptionInterface* pr_answer =
877 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
878 sdp, NULL);
879 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
880 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
881 }
882
883 void CreateOfferReceiveAnswer() {
884 CreateOfferAsLocalDescription();
885 std::string sdp;
886 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
887 CreateAnswerAsRemoteDescription(sdp);
888 }
889
890 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700891 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700892 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
894 // audio codec change, even if the parameter has nothing to do with
895 // receiving. Not all parameters are serialized to SDP.
896 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
897 // the SessionDescription, it is necessary to do that here to in order to
898 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
899 // https://code.google.com/p/webrtc/issues/detail?id=1356
900 std::string sdp;
901 EXPECT_TRUE(offer->ToString(&sdp));
902 SessionDescriptionInterface* new_offer =
903 webrtc::CreateSessionDescription(
904 SessionDescriptionInterface::kOffer,
905 sdp, NULL);
906
907 EXPECT_TRUE(DoSetLocalDescription(new_offer));
908 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000909 // Wait for the ice_complete message, so that SDP will have candidates.
910 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 }
912
deadbeefab9b2d12015-10-14 11:33:11 -0700913 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
915 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700916 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 EXPECT_TRUE(DoSetRemoteDescription(answer));
918 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
919 }
920
deadbeefab9b2d12015-10-14 11:33:11 -0700921 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 webrtc::JsepSessionDescription* pr_answer =
923 new webrtc::JsepSessionDescription(
924 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700925 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
927 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
928 webrtc::JsepSessionDescription* answer =
929 new webrtc::JsepSessionDescription(
930 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700931 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 EXPECT_TRUE(DoSetRemoteDescription(answer));
933 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
934 }
935
936 // Help function used for waiting until a the last signaled remote stream has
937 // the same label as |stream_label|. In a few of the tests in this file we
938 // answer with the same session description as we offer and thus we can
939 // check if OnAddStream have been called with the same stream as we offer to
940 // send.
941 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
942 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
943 }
944
945 // Creates an offer and applies it as a local session description.
946 // Creates an answer with the same SDP an the offer but removes all lines
947 // that start with a:ssrc"
948 void CreateOfferReceiveAnswerWithoutSsrc() {
949 CreateOfferAsLocalDescription();
950 std::string sdp;
951 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
952 SetSsrcToZero(&sdp);
953 CreateAnswerAsRemoteDescription(sdp);
954 }
955
deadbeefab9b2d12015-10-14 11:33:11 -0700956 // This function creates a MediaStream with label kStreams[0] and
957 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
958 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700959 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700960 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700961 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700962 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
963 size_t number_of_video_tracks) {
964 EXPECT_LE(number_of_audio_tracks, 2u);
965 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700966
967 reference_collection_ = StreamCollection::Create();
968 std::string sdp_ms1 = std::string(kSdpStringInit);
969
970 std::string mediastream_label = kStreams[0];
971
972 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
973 webrtc::MediaStream::Create(mediastream_label));
974 reference_collection_->AddStream(stream);
975
976 if (number_of_audio_tracks > 0) {
977 sdp_ms1 += std::string(kSdpStringAudio);
978 sdp_ms1 += std::string(kSdpStringMs1Audio0);
979 AddAudioTrack(kAudioTracks[0], stream);
980 }
981 if (number_of_audio_tracks > 1) {
982 sdp_ms1 += kSdpStringMs1Audio1;
983 AddAudioTrack(kAudioTracks[1], stream);
984 }
985
986 if (number_of_video_tracks > 0) {
987 sdp_ms1 += std::string(kSdpStringVideo);
988 sdp_ms1 += std::string(kSdpStringMs1Video0);
989 AddVideoTrack(kVideoTracks[0], stream);
990 }
991 if (number_of_video_tracks > 1) {
992 sdp_ms1 += kSdpStringMs1Video1;
993 AddVideoTrack(kVideoTracks[1], stream);
994 }
995
kwibergd1fe2812016-04-27 06:47:29 -0700996 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700997 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
998 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700999 }
1000
1001 void AddAudioTrack(const std::string& track_id,
1002 MediaStreamInterface* stream) {
1003 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1004 webrtc::AudioTrack::Create(track_id, nullptr));
1005 ASSERT_TRUE(stream->AddTrack(audio_track));
1006 }
1007
1008 void AddVideoTrack(const std::string& track_id,
1009 MediaStreamInterface* stream) {
1010 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001011 webrtc::VideoTrack::Create(track_id,
1012 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001013 ASSERT_TRUE(stream->AddTrack(video_track));
1014 }
1015
kwibergfd8be342016-05-14 19:44:11 -07001016 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001017 CreatePeerConnection();
1018 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001019 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001020 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1021 return offer;
1022 }
1023
kwibergfd8be342016-05-14 19:44:11 -07001024 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001025 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001026 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001027 CreateOfferWithOneAudioStream();
1028 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001029 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001030 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1031 return answer;
1032 }
1033
1034 const std::string& GetFirstAudioStreamCname(
1035 const SessionDescriptionInterface* desc) {
1036 const cricket::ContentInfo* audio_content =
1037 cricket::GetFirstAudioContent(desc->description());
1038 const cricket::AudioContentDescription* audio_desc =
1039 static_cast<const cricket::AudioContentDescription*>(
1040 audio_content->description);
1041 return audio_desc->streams()[0].cname;
1042 }
1043
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001044 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef907abe42016-08-04 12:22:18 -07001045 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1046 scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1047 scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001049 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050};
1051
zhihuang29ff8442016-07-27 11:07:25 -07001052// Test that no callbacks on the PeerConnectionObserver are called after the
1053// PeerConnection is closed.
1054TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
deadbeef907abe42016-08-04 12:22:18 -07001055 scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001056 pc_factory_for_test_->CreatePeerConnection(
1057 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1058 nullptr, &observer_));
1059 observer_.SetPeerConnectionInterface(pc.get());
1060 pc->Close();
1061
1062 // No callbacks is expected to be called.
1063 observer_.callback_triggered = false;
1064 std::vector<cricket::Candidate> candidates;
1065 pc_factory_for_test_->transport_controller->SignalGatheringState(
1066 cricket::IceGatheringState{});
1067 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1068 "", candidates);
1069 pc_factory_for_test_->transport_controller->SignalConnectionState(
1070 cricket::IceConnectionState{});
1071 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1072 candidates);
1073 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1074 EXPECT_FALSE(observer_.callback_triggered);
1075}
1076
zhihuang8f65cdf2016-05-06 18:40:30 -07001077// Generate different CNAMEs when PeerConnections are created.
1078// The CNAMEs are expected to be generated randomly. It is possible
1079// that the test fails, though the possibility is very low.
1080TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001081 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001082 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001083 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001084 CreateOfferWithOneAudioStream();
1085 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1086 GetFirstAudioStreamCname(offer2.get()));
1087}
1088
1089TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001090 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001091 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001092 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001093 CreateAnswerWithOneAudioStream();
1094 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1095 GetFirstAudioStreamCname(answer2.get()));
1096}
1097
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098TEST_F(PeerConnectionInterfaceTest,
1099 CreatePeerConnectionWithDifferentConfigurations) {
1100 CreatePeerConnectionWithDifferentConfigurations();
1101}
1102
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001103TEST_F(PeerConnectionInterfaceTest,
1104 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1105 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1106 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1107 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1108 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1109 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1110 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1111 port_allocator_->candidate_filter());
1112 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1113 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1114}
1115
1116// Test that when a PeerConnection is created with a nonzero candidate pool
1117// size, the pooled PortAllocatorSession is created with all the attributes
1118// in the RTCConfiguration.
1119TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1120 PeerConnectionInterface::RTCConfiguration config;
1121 PeerConnectionInterface::IceServer server;
1122 server.uri = kStunAddressOnly;
1123 config.servers.push_back(server);
1124 config.type = PeerConnectionInterface::kRelay;
1125 config.disable_ipv6 = true;
1126 config.tcp_candidate_policy =
1127 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001128 config.candidate_network_policy =
1129 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001130 config.ice_candidate_pool_size = 1;
1131 CreatePeerConnection(config, nullptr);
1132
1133 const cricket::FakePortAllocatorSession* session =
1134 static_cast<const cricket::FakePortAllocatorSession*>(
1135 port_allocator_->GetPooledSession());
1136 ASSERT_NE(nullptr, session);
1137 EXPECT_EQ(1UL, session->stun_servers().size());
1138 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1139 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001140 EXPECT_LT(0U,
1141 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001142}
1143
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001144// Test that the PeerConnection initializes the port allocator passed into it,
1145// and on the correct thread.
1146TEST_F(PeerConnectionInterfaceTest,
1147 CreatePeerConnectionInitializesPortAllocator) {
1148 rtc::Thread network_thread;
1149 network_thread.Start();
1150 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1151 webrtc::CreatePeerConnectionFactory(
1152 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1153 nullptr, nullptr, nullptr));
1154 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1155 new cricket::FakePortAllocator(&network_thread, nullptr));
1156 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1157 PeerConnectionInterface::RTCConfiguration config;
1158 rtc::scoped_refptr<PeerConnectionInterface> pc(
1159 pc_factory->CreatePeerConnection(
1160 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1161 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1162 // so all we have to do here is check that it's initialized.
1163 EXPECT_TRUE(raw_port_allocator->initialized());
1164}
1165
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1167 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001168 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 AddVoiceStream(kStreamLabel2);
1170 ASSERT_EQ(2u, pc_->local_streams()->count());
1171
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001172 // Test we can add multiple local streams to one peerconnection.
deadbeef907abe42016-08-04 12:22:18 -07001173 scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
deadbeef907abe42016-08-04 12:22:18 -07001175 scoped_refptr<AudioTrackInterface> audio_track(
1176 pc_factory_->CreateAudioTrack(
1177 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001179 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001180 EXPECT_EQ(3u, pc_->local_streams()->count());
1181
1182 // Remove the third stream.
1183 pc_->RemoveStream(pc_->local_streams()->at(2));
1184 EXPECT_EQ(2u, pc_->local_streams()->count());
1185
1186 // Remove the second stream.
1187 pc_->RemoveStream(pc_->local_streams()->at(1));
1188 EXPECT_EQ(1u, pc_->local_streams()->count());
1189
1190 // Remove the first stream.
1191 pc_->RemoveStream(pc_->local_streams()->at(0));
1192 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193}
1194
deadbeefab9b2d12015-10-14 11:33:11 -07001195// Test that the created offer includes streams we added.
1196TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1197 CreatePeerConnection();
1198 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001199 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001200 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001201
1202 const cricket::ContentInfo* audio_content =
1203 cricket::GetFirstAudioContent(offer->description());
1204 const cricket::AudioContentDescription* audio_desc =
1205 static_cast<const cricket::AudioContentDescription*>(
1206 audio_content->description);
1207 EXPECT_TRUE(
1208 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1209
1210 const cricket::ContentInfo* video_content =
1211 cricket::GetFirstVideoContent(offer->description());
1212 const cricket::VideoContentDescription* video_desc =
1213 static_cast<const cricket::VideoContentDescription*>(
1214 video_content->description);
1215 EXPECT_TRUE(
1216 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1217
1218 // Add another stream and ensure the offer includes both the old and new
1219 // streams.
1220 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001221 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001222
1223 audio_content = cricket::GetFirstAudioContent(offer->description());
1224 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1225 audio_content->description);
1226 EXPECT_TRUE(
1227 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1228 EXPECT_TRUE(
1229 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1230
1231 video_content = cricket::GetFirstVideoContent(offer->description());
1232 video_desc = static_cast<const cricket::VideoContentDescription*>(
1233 video_content->description);
1234 EXPECT_TRUE(
1235 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1236 EXPECT_TRUE(
1237 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1238}
1239
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1241 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001242 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 ASSERT_EQ(1u, pc_->local_streams()->count());
1244 pc_->RemoveStream(pc_->local_streams()->at(0));
1245 EXPECT_EQ(0u, pc_->local_streams()->count());
1246}
1247
deadbeefe1f9d832016-01-14 15:35:42 -08001248// Test for AddTrack and RemoveTrack methods.
1249// Tests that the created offer includes tracks we added,
1250// and that the RtpSenders are created correctly.
1251// Also tests that RemoveTrack removes the tracks from subsequent offers.
1252TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1253 CreatePeerConnection();
1254 // Create a dummy stream, so tracks share a stream label.
deadbeef907abe42016-08-04 12:22:18 -07001255 scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001256 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1257 std::vector<MediaStreamInterface*> stream_list;
1258 stream_list.push_back(stream.get());
deadbeef907abe42016-08-04 12:22:18 -07001259 scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001260 pc_factory_->CreateAudioTrack("audio_track", nullptr));
deadbeef907abe42016-08-04 12:22:18 -07001261 scoped_refptr<VideoTrackInterface> video_track(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.
deadbeef907abe42016-08-04 12:22:18 -07001329 scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001330 pc_factory_->CreateAudioTrack("audio_track", nullptr));
deadbeef907abe42016-08-04 12:22:18 -07001331 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1332 "video_track",
1333 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001334 auto audio_sender =
1335 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1336 auto video_sender =
1337 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1338 EXPECT_EQ("audio_track", audio_sender->id());
1339 EXPECT_EQ(audio_track, audio_sender->track());
1340 EXPECT_EQ("video_track", video_sender->id());
1341 EXPECT_EQ(video_track, video_sender->track());
1342 // If the ID is truly a random GUID, it should be infinitely unlikely they
1343 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001344 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001345}
1346
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1348 InitiateCall();
1349 WaitAndVerifyOnAddStream(kStreamLabel1);
1350 VerifyRemoteRtpHeaderExtensions();
1351}
1352
1353TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1354 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001355 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 CreateOfferAsLocalDescription();
1357 std::string offer;
1358 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1359 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1360 WaitAndVerifyOnAddStream(kStreamLabel1);
1361}
1362
1363TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1364 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001365 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366
1367 CreateOfferAsRemoteDescription();
1368 CreateAnswerAsLocalDescription();
1369
1370 WaitAndVerifyOnAddStream(kStreamLabel1);
1371}
1372
1373TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1374 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001375 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376
1377 CreateOfferAsRemoteDescription();
1378 CreatePrAnswerAsLocalDescription();
1379 CreateAnswerAsLocalDescription();
1380
1381 WaitAndVerifyOnAddStream(kStreamLabel1);
1382}
1383
1384TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1385 InitiateCall();
1386 ASSERT_EQ(1u, pc_->remote_streams()->count());
1387 pc_->RemoveStream(pc_->local_streams()->at(0));
1388 CreateOfferReceiveAnswer();
1389 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001390 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 CreateOfferReceiveAnswer();
1392}
1393
1394// Tests that after negotiating an audio only call, the respondent can perform a
1395// renegotiation that removes the audio stream.
1396TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1397 CreatePeerConnection();
1398 AddVoiceStream(kStreamLabel1);
1399 CreateOfferAsRemoteDescription();
1400 CreateAnswerAsLocalDescription();
1401
1402 ASSERT_EQ(1u, pc_->remote_streams()->count());
1403 pc_->RemoveStream(pc_->local_streams()->at(0));
1404 CreateOfferReceiveAnswer();
1405 EXPECT_EQ(0u, pc_->remote_streams()->count());
1406}
1407
1408// Test that candidates are generated and that we can parse our own candidates.
1409TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1410 CreatePeerConnection();
1411
1412 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1413 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001414 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001415 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001416 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001417 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418
1419 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001420 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001421 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001422 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423
1424 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1425 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1426
1427 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1428}
1429
deadbeefab9b2d12015-10-14 11:33:11 -07001430// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001431// not unique.
1432TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1433 CreatePeerConnection();
1434 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001435 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001436 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001437 EXPECT_TRUE(offer);
1438 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439
1440 // Create a local stream with audio&video tracks having same label.
1441 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1442
1443 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001444 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445
1446 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001447 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001448 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449}
1450
1451// Test that we will get different SSRCs for each tracks in the offer and answer
1452// we created.
1453TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1454 CreatePeerConnection();
1455 // Create a local stream with audio&video tracks having different labels.
1456 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1457
1458 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001459 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001460 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 int audio_ssrc = 0;
1462 int video_ssrc = 0;
1463 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1464 &audio_ssrc));
1465 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1466 &video_ssrc));
1467 EXPECT_NE(audio_ssrc, video_ssrc);
1468
1469 // Test CreateAnswer
1470 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001471 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001472 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 audio_ssrc = 0;
1474 video_ssrc = 0;
1475 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1476 &audio_ssrc));
1477 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1478 &video_ssrc));
1479 EXPECT_NE(audio_ssrc, video_ssrc);
1480}
1481
deadbeefeb459812015-12-15 19:24:43 -08001482// Test that it's possible to call AddTrack on a MediaStream after adding
1483// the stream to a PeerConnection.
1484// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1485TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1486 CreatePeerConnection();
1487 // Create audio stream and add to PeerConnection.
1488 AddVoiceStream(kStreamLabel1);
1489 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1490
1491 // Add video track to the audio-only stream.
deadbeef907abe42016-08-04 12:22:18 -07001492 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1493 "video_label",
1494 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001495 stream->AddTrack(video_track.get());
1496
kwibergd1fe2812016-04-27 06:47:29 -07001497 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001498 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001499
1500 const cricket::MediaContentDescription* video_desc =
1501 cricket::GetFirstVideoContentDescription(offer->description());
1502 EXPECT_TRUE(video_desc != nullptr);
1503}
1504
1505// Test that it's possible to call RemoveTrack on a MediaStream after adding
1506// the stream to a PeerConnection.
1507// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1508TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1509 CreatePeerConnection();
1510 // Create audio/video stream and add to PeerConnection.
1511 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1512 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1513
1514 // Remove the video track.
1515 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1516
kwibergd1fe2812016-04-27 06:47:29 -07001517 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001518 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001519
1520 const cricket::MediaContentDescription* video_desc =
1521 cricket::GetFirstVideoContentDescription(offer->description());
1522 EXPECT_TRUE(video_desc == nullptr);
1523}
1524
deadbeefbd7d8f72015-12-18 16:58:44 -08001525// Test creating a sender with a stream ID, and ensure the ID is populated
1526// in the offer.
1527TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1528 CreatePeerConnection();
1529 pc_->CreateSender("video", kStreamLabel1);
1530
kwibergd1fe2812016-04-27 06:47:29 -07001531 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001532 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001533
1534 const cricket::MediaContentDescription* video_desc =
1535 cricket::GetFirstVideoContentDescription(offer->description());
1536 ASSERT_TRUE(video_desc != nullptr);
1537 ASSERT_EQ(1u, video_desc->streams().size());
1538 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1539}
1540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541// Test that we can specify a certain track that we want statistics about.
1542TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1543 InitiateCall();
1544 ASSERT_LT(0u, pc_->remote_streams()->count());
1545 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
deadbeef907abe42016-08-04 12:22:18 -07001546 scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1548 EXPECT_TRUE(DoGetStats(remote_audio));
1549
1550 // Remove the stream. Since we are sending to our selves the local
1551 // and the remote stream is the same.
1552 pc_->RemoveStream(pc_->local_streams()->at(0));
1553 // Do a re-negotiation.
1554 CreateOfferReceiveAnswer();
1555
1556 ASSERT_EQ(0u, pc_->remote_streams()->count());
1557
1558 // Test that we still can get statistics for the old track. Even if it is not
1559 // sent any longer.
1560 EXPECT_TRUE(DoGetStats(remote_audio));
1561}
1562
1563// Test that we can get stats on a video track.
1564TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1565 InitiateCall();
1566 ASSERT_LT(0u, pc_->remote_streams()->count());
1567 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
deadbeef907abe42016-08-04 12:22:18 -07001568 scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1570 EXPECT_TRUE(DoGetStats(remote_video));
1571}
1572
1573// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001574// TODO(tommi): Fix this test. DoGetStats will return true
1575// for the unknown track (since GetStats is async), but no
1576// data is returned for the track.
1577TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 InitiateCall();
deadbeef907abe42016-08-04 12:22:18 -07001579 scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 pc_factory_->CreateAudioTrack("unknown track", NULL));
1581 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1582}
1583
1584// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 FakeConstraints constraints;
1587 constraints.SetAllowRtpDataChannels();
1588 CreatePeerConnection(&constraints);
deadbeef907abe42016-08-04 12:22:18 -07001589 scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 pc_->CreateDataChannel("test1", NULL);
deadbeef907abe42016-08-04 12:22:18 -07001591 scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 pc_->CreateDataChannel("test2", NULL);
1593 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001594 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001596 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 new MockDataChannelObserver(data2));
1598
1599 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1600 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1601 std::string data_to_send1 = "testing testing";
1602 std::string data_to_send2 = "testing something else";
1603 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1604
1605 CreateOfferReceiveAnswer();
1606 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1607 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1608
1609 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1610 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1611 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1612 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1613
1614 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1615 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1616
1617 data1->Close();
1618 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1619 CreateOfferReceiveAnswer();
1620 EXPECT_FALSE(observer1->IsOpen());
1621 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1622 EXPECT_TRUE(observer2->IsOpen());
1623
1624 data_to_send2 = "testing something else again";
1625 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1626
1627 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1628}
1629
1630// This test verifies that sendnig binary data over RTP data channels should
1631// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 FakeConstraints constraints;
1634 constraints.SetAllowRtpDataChannels();
1635 CreatePeerConnection(&constraints);
deadbeef907abe42016-08-04 12:22:18 -07001636 scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 pc_->CreateDataChannel("test1", NULL);
deadbeef907abe42016-08-04 12:22:18 -07001638 scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 pc_->CreateDataChannel("test2", NULL);
1640 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001641 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001643 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 new MockDataChannelObserver(data2));
1645
1646 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1647 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1648
1649 CreateOfferReceiveAnswer();
1650 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1651 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1652
1653 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1654 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1655
jbaucheec21bd2016-03-20 06:15:43 -07001656 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1658}
1659
1660// This test setup a RTP data channels in loop back and test that a channel is
1661// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 FakeConstraints constraints;
1664 constraints.SetAllowRtpDataChannels();
1665 CreatePeerConnection(&constraints);
deadbeef907abe42016-08-04 12:22:18 -07001666 scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001668 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 new MockDataChannelObserver(data1));
1670
1671 CreateOfferReceiveAnswerWithoutSsrc();
1672
1673 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1674
1675 data1->Close();
1676 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1677 CreateOfferReceiveAnswerWithoutSsrc();
1678 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1679 EXPECT_FALSE(observer1->IsOpen());
1680}
1681
1682// This test that if a data channel is added in an answer a receive only channel
1683// channel is created.
1684TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1685 FakeConstraints constraints;
1686 constraints.SetAllowRtpDataChannels();
1687 CreatePeerConnection(&constraints);
1688
1689 std::string offer_label = "offer_channel";
deadbeef907abe42016-08-04 12:22:18 -07001690 scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 pc_->CreateDataChannel(offer_label, NULL);
1692
1693 CreateOfferAsLocalDescription();
1694
1695 // Replace the data channel label in the offer and apply it as an answer.
1696 std::string receive_label = "answer_channel";
1697 std::string sdp;
1698 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001699 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 receive_label.c_str(), receive_label.length(),
1701 &sdp);
1702 CreateAnswerAsRemoteDescription(sdp);
1703
1704 // Verify that a new incoming data channel has been created and that
1705 // it is open but can't we written to.
1706 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1707 DataChannelInterface* received_channel = observer_.last_datachannel_;
1708 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1709 EXPECT_EQ(receive_label, received_channel->label());
1710 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1711
1712 // Verify that the channel we initially offered has been rejected.
1713 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1714
1715 // Do another offer / answer exchange and verify that the data channel is
1716 // opened.
1717 CreateOfferReceiveAnswer();
1718 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1719 kTimeout);
1720}
1721
1722// This test that no data channel is returned if a reliable channel is
1723// requested.
1724// TODO(perkj): Remove this test once reliable channels are implemented.
1725TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1726 FakeConstraints constraints;
1727 constraints.SetAllowRtpDataChannels();
1728 CreatePeerConnection(&constraints);
1729
1730 std::string label = "test";
1731 webrtc::DataChannelInit config;
1732 config.reliable = true;
deadbeef907abe42016-08-04 12:22:18 -07001733 scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 pc_->CreateDataChannel(label, &config);
1735 EXPECT_TRUE(channel == NULL);
1736}
1737
deadbeefab9b2d12015-10-14 11:33:11 -07001738// Verifies that duplicated label is not allowed for RTP data channel.
1739TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1740 FakeConstraints constraints;
1741 constraints.SetAllowRtpDataChannels();
1742 CreatePeerConnection(&constraints);
1743
1744 std::string label = "test";
deadbeef907abe42016-08-04 12:22:18 -07001745 scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001746 pc_->CreateDataChannel(label, nullptr);
1747 EXPECT_NE(channel, nullptr);
1748
deadbeef907abe42016-08-04 12:22:18 -07001749 scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001750 pc_->CreateDataChannel(label, nullptr);
1751 EXPECT_EQ(dup_channel, nullptr);
1752}
1753
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754// This tests that a SCTP data channel is returned using different
1755// DataChannelInit configurations.
1756TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1757 FakeConstraints constraints;
1758 constraints.SetAllowDtlsSctpDataChannels();
1759 CreatePeerConnection(&constraints);
1760
1761 webrtc::DataChannelInit config;
1762
deadbeef907abe42016-08-04 12:22:18 -07001763 scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 pc_->CreateDataChannel("1", &config);
1765 EXPECT_TRUE(channel != NULL);
1766 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001767 EXPECT_TRUE(observer_.renegotiation_needed_);
1768 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769
1770 config.ordered = false;
1771 channel = pc_->CreateDataChannel("2", &config);
1772 EXPECT_TRUE(channel != NULL);
1773 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001774 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
1776 config.ordered = true;
1777 config.maxRetransmits = 0;
1778 channel = pc_->CreateDataChannel("3", &config);
1779 EXPECT_TRUE(channel != NULL);
1780 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001781 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782
1783 config.maxRetransmits = -1;
1784 config.maxRetransmitTime = 0;
1785 channel = pc_->CreateDataChannel("4", &config);
1786 EXPECT_TRUE(channel != NULL);
1787 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001788 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789}
1790
1791// This tests that no data channel is returned if both maxRetransmits and
1792// maxRetransmitTime are set for SCTP data channels.
1793TEST_F(PeerConnectionInterfaceTest,
1794 CreateSctpDataChannelShouldFailForInvalidConfig) {
1795 FakeConstraints constraints;
1796 constraints.SetAllowDtlsSctpDataChannels();
1797 CreatePeerConnection(&constraints);
1798
1799 std::string label = "test";
1800 webrtc::DataChannelInit config;
1801 config.maxRetransmits = 0;
1802 config.maxRetransmitTime = 0;
1803
deadbeef907abe42016-08-04 12:22:18 -07001804 scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 pc_->CreateDataChannel(label, &config);
1806 EXPECT_TRUE(channel == NULL);
1807}
1808
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809// The test verifies that creating a SCTP data channel with an id already in use
1810// or out of range should fail.
1811TEST_F(PeerConnectionInterfaceTest,
1812 CreateSctpDataChannelWithInvalidIdShouldFail) {
1813 FakeConstraints constraints;
1814 constraints.SetAllowDtlsSctpDataChannels();
1815 CreatePeerConnection(&constraints);
1816
1817 webrtc::DataChannelInit config;
deadbeef907abe42016-08-04 12:22:18 -07001818 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001820 config.id = 1;
1821 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 EXPECT_TRUE(channel != NULL);
1823 EXPECT_EQ(1, channel->id());
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 channel = pc_->CreateDataChannel("x", &config);
1826 EXPECT_TRUE(channel == NULL);
1827
1828 config.id = cricket::kMaxSctpSid;
1829 channel = pc_->CreateDataChannel("max", &config);
1830 EXPECT_TRUE(channel != NULL);
1831 EXPECT_EQ(config.id, channel->id());
1832
1833 config.id = cricket::kMaxSctpSid + 1;
1834 channel = pc_->CreateDataChannel("x", &config);
1835 EXPECT_TRUE(channel == NULL);
1836}
1837
deadbeefab9b2d12015-10-14 11:33:11 -07001838// Verifies that duplicated label is allowed for SCTP data channel.
1839TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1840 FakeConstraints constraints;
1841 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1842 true);
1843 CreatePeerConnection(&constraints);
1844
1845 std::string label = "test";
deadbeef907abe42016-08-04 12:22:18 -07001846 scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001847 pc_->CreateDataChannel(label, nullptr);
1848 EXPECT_NE(channel, nullptr);
1849
deadbeef907abe42016-08-04 12:22:18 -07001850 scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001851 pc_->CreateDataChannel(label, nullptr);
1852 EXPECT_NE(dup_channel, nullptr);
1853}
1854
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001855// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1856// DataChannel.
1857TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1858 FakeConstraints constraints;
1859 constraints.SetAllowRtpDataChannels();
1860 CreatePeerConnection(&constraints);
1861
deadbeef907abe42016-08-04 12:22:18 -07001862 scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001863 pc_->CreateDataChannel("test1", NULL);
1864 EXPECT_TRUE(observer_.renegotiation_needed_);
1865 observer_.renegotiation_needed_ = false;
1866
deadbeef907abe42016-08-04 12:22:18 -07001867 scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001868 pc_->CreateDataChannel("test2", NULL);
1869 EXPECT_TRUE(observer_.renegotiation_needed_);
1870}
1871
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 FakeConstraints constraints;
1875 constraints.SetAllowRtpDataChannels();
1876 CreatePeerConnection(&constraints);
1877
deadbeef907abe42016-08-04 12:22:18 -07001878 scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 pc_->CreateDataChannel("test1", NULL);
deadbeef907abe42016-08-04 12:22:18 -07001880 scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 pc_->CreateDataChannel("test2", NULL);
1882 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001883 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001885 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 new MockDataChannelObserver(data2));
1887
1888 CreateOfferReceiveAnswer();
1889 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1890 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1891
1892 ReleasePeerConnection();
1893 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1894 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1895}
1896
1897// This test that data channels can be rejected in an answer.
1898TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1899 FakeConstraints constraints;
1900 constraints.SetAllowRtpDataChannels();
1901 CreatePeerConnection(&constraints);
1902
deadbeef907abe42016-08-04 12:22:18 -07001903 scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 pc_->CreateDataChannel("offer_channel", NULL));
1905
1906 CreateOfferAsLocalDescription();
1907
1908 // Create an answer where the m-line for data channels are rejected.
1909 std::string sdp;
1910 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1911 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1912 SessionDescriptionInterface::kAnswer);
1913 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1914 cricket::ContentInfo* data_info =
1915 answer->description()->GetContentByName("data");
1916 data_info->rejected = true;
1917
1918 DoSetRemoteDescription(answer);
1919 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1920}
1921
1922// Test that we can create a session description from an SDP string from
1923// FireFox, use it as a remote session description, generate an answer and use
1924// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001925TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001926 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 FakeConstraints constraints;
1928 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1929 true);
1930 CreatePeerConnection(&constraints);
1931 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1932 SessionDescriptionInterface* desc =
1933 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001934 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1936 CreateAnswerAsLocalDescription();
1937 ASSERT_TRUE(pc_->local_description() != NULL);
1938 ASSERT_TRUE(pc_->remote_description() != NULL);
1939
1940 const cricket::ContentInfo* content =
1941 cricket::GetFirstAudioContent(pc_->local_description()->description());
1942 ASSERT_TRUE(content != NULL);
1943 EXPECT_FALSE(content->rejected);
1944
1945 content =
1946 cricket::GetFirstVideoContent(pc_->local_description()->description());
1947 ASSERT_TRUE(content != NULL);
1948 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001949#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 content =
1951 cricket::GetFirstDataContent(pc_->local_description()->description());
1952 ASSERT_TRUE(content != NULL);
1953 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001954#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955}
1956
1957// Test that we can create an audio only offer and receive an answer with a
1958// limited set of audio codecs and receive an updated offer with more audio
1959// codecs, where the added codecs are not supported.
1960TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1961 CreatePeerConnection();
1962 AddVoiceStream("audio_label");
1963 CreateOfferAsLocalDescription();
1964
1965 SessionDescriptionInterface* answer =
1966 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001967 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1969
1970 SessionDescriptionInterface* updated_offer =
1971 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001972 webrtc::kAudioSdpWithUnsupportedCodecs,
1973 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1975 CreateAnswerAsLocalDescription();
1976}
1977
deadbeefc80741f2015-10-22 13:14:45 -07001978// Test that if we're receiving (but not sending) a track, subsequent offers
1979// will have m-lines with a=recvonly.
1980TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1981 FakeConstraints constraints;
1982 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1983 true);
1984 CreatePeerConnection(&constraints);
1985 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1986 CreateAnswerAsLocalDescription();
1987
1988 // At this point we should be receiving stream 1, but not sending anything.
1989 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001990 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001991 DoCreateOffer(&offer, nullptr);
1992
1993 const cricket::ContentInfo* video_content =
1994 cricket::GetFirstVideoContent(offer->description());
1995 const cricket::VideoContentDescription* video_desc =
1996 static_cast<const cricket::VideoContentDescription*>(
1997 video_content->description);
1998 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1999
2000 const cricket::ContentInfo* audio_content =
2001 cricket::GetFirstAudioContent(offer->description());
2002 const cricket::AudioContentDescription* audio_desc =
2003 static_cast<const cricket::AudioContentDescription*>(
2004 audio_content->description);
2005 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2006}
2007
2008// Test that if we're receiving (but not sending) a track, and the
2009// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2010// false, the generated m-lines will be a=inactive.
2011TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2012 FakeConstraints constraints;
2013 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2014 true);
2015 CreatePeerConnection(&constraints);
2016 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2017 CreateAnswerAsLocalDescription();
2018
2019 // At this point we should be receiving stream 1, but not sending anything.
2020 // A new offer would be recvonly, but we'll set the "no receive" constraints
2021 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002022 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002023 FakeConstraints offer_constraints;
2024 offer_constraints.AddMandatory(
2025 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2026 offer_constraints.AddMandatory(
2027 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2028 DoCreateOffer(&offer, &offer_constraints);
2029
2030 const cricket::ContentInfo* video_content =
2031 cricket::GetFirstVideoContent(offer->description());
2032 const cricket::VideoContentDescription* video_desc =
2033 static_cast<const cricket::VideoContentDescription*>(
2034 video_content->description);
2035 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2036
2037 const cricket::ContentInfo* audio_content =
2038 cricket::GetFirstAudioContent(offer->description());
2039 const cricket::AudioContentDescription* audio_desc =
2040 static_cast<const cricket::AudioContentDescription*>(
2041 audio_content->description);
2042 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2043}
2044
deadbeef653b8e02015-11-11 12:55:10 -08002045// Test that we can use SetConfiguration to change the ICE servers of the
2046// PortAllocator.
2047TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2048 CreatePeerConnection();
2049
2050 PeerConnectionInterface::RTCConfiguration config;
2051 PeerConnectionInterface::IceServer server;
2052 server.uri = "stun:test_hostname";
2053 config.servers.push_back(server);
2054 EXPECT_TRUE(pc_->SetConfiguration(config));
2055
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002056 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2057 EXPECT_EQ("test_hostname",
2058 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002059}
2060
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002061TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2062 CreatePeerConnection();
2063 PeerConnectionInterface::RTCConfiguration config;
2064 config.type = PeerConnectionInterface::kRelay;
2065 EXPECT_TRUE(pc_->SetConfiguration(config));
2066 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2067}
2068
2069// Test that when SetConfiguration changes both the pool size and other
2070// attributes, the pooled session is created with the updated attributes.
2071TEST_F(PeerConnectionInterfaceTest,
2072 SetConfigurationCreatesPooledSessionCorrectly) {
2073 CreatePeerConnection();
2074 PeerConnectionInterface::RTCConfiguration config;
2075 config.ice_candidate_pool_size = 1;
2076 PeerConnectionInterface::IceServer server;
2077 server.uri = kStunAddressOnly;
2078 config.servers.push_back(server);
2079 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002080 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002081
2082 const cricket::FakePortAllocatorSession* session =
2083 static_cast<const cricket::FakePortAllocatorSession*>(
2084 port_allocator_->GetPooledSession());
2085 ASSERT_NE(nullptr, session);
2086 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002087}
2088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089// Test that PeerConnection::Close changes the states to closed and all remote
2090// tracks change state to ended.
2091TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2092 // Initialize a PeerConnection and negotiate local and remote session
2093 // description.
2094 InitiateCall();
2095 ASSERT_EQ(1u, pc_->local_streams()->count());
2096 ASSERT_EQ(1u, pc_->remote_streams()->count());
2097
2098 pc_->Close();
2099
2100 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2101 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2102 pc_->ice_connection_state());
2103 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2104 pc_->ice_gathering_state());
2105
2106 EXPECT_EQ(1u, pc_->local_streams()->count());
2107 EXPECT_EQ(1u, pc_->remote_streams()->count());
2108
deadbeef907abe42016-08-04 12:22:18 -07002109 scoped_refptr<MediaStreamInterface> remote_stream =
2110 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002111 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002112 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002113 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2114 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2115 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116}
2117
2118// Test that PeerConnection methods fails gracefully after
2119// PeerConnection::Close has been called.
2120TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2121 CreatePeerConnection();
2122 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2123 CreateOfferAsRemoteDescription();
2124 CreateAnswerAsLocalDescription();
2125
2126 ASSERT_EQ(1u, pc_->local_streams()->count());
deadbeef907abe42016-08-04 12:22:18 -07002127 scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 pc_->local_streams()->at(0);
2129
2130 pc_->Close();
2131
2132 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002133 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134
2135 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002136 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002138 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
2140 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2141
2142 EXPECT_TRUE(pc_->local_description() != NULL);
2143 EXPECT_TRUE(pc_->remote_description() != NULL);
2144
kwibergd1fe2812016-04-27 06:47:29 -07002145 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002146 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002147 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002148 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149
2150 std::string sdp;
2151 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2152 SessionDescriptionInterface* remote_offer =
2153 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2154 sdp, NULL);
2155 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2156
2157 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2158 SessionDescriptionInterface* local_offer =
2159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2160 sdp, NULL);
2161 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2162}
2163
2164// Test that GetStats can still be called after PeerConnection::Close.
2165TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2166 InitiateCall();
2167 pc_->Close();
2168 DoGetStats(NULL);
2169}
deadbeefab9b2d12015-10-14 11:33:11 -07002170
2171// NOTE: The series of tests below come from what used to be
2172// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2173// setting a remote or local description has the expected effects.
2174
2175// This test verifies that the remote MediaStreams corresponding to a received
2176// SDP string is created. In this test the two separate MediaStreams are
2177// signaled.
2178TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2179 FakeConstraints constraints;
2180 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2181 true);
2182 CreatePeerConnection(&constraints);
2183 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2184
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002185 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002186 EXPECT_TRUE(
2187 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2188 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2189 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2190
2191 // Create a session description based on another SDP with another
2192 // MediaStream.
2193 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2194
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002195 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002196 EXPECT_TRUE(
2197 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2198}
2199
2200// This test verifies that when remote tracks are added/removed from SDP, the
2201// created remote streams are updated appropriately.
2202TEST_F(PeerConnectionInterfaceTest,
2203 AddRemoveTrackFromExistingRemoteMediaStream) {
2204 FakeConstraints constraints;
2205 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2206 true);
2207 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002208 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002209 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002210 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2211 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2212 reference_collection_));
2213
2214 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002215 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002216 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002217 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2218 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2219 reference_collection_));
deadbeef907abe42016-08-04 12:22:18 -07002220 scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002221 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2222 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
deadbeef907abe42016-08-04 12:22:18 -07002223 scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002224 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2225 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002226
2227 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002228 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002229 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002230 MockTrackObserver audio_track_observer(audio_track2);
2231 MockTrackObserver video_track_observer(video_track2);
2232
2233 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2234 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002235 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2236 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2237 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002238 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002239 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002240 audio_track2->state(), kTimeout);
2241 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2242 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002243}
2244
2245// This tests that remote tracks are ended if a local session description is set
2246// that rejects the media content type.
2247TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2248 FakeConstraints constraints;
2249 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2250 true);
2251 CreatePeerConnection(&constraints);
2252 // First create and set a remote offer, then reject its video content in our
2253 // answer.
2254 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2255 ASSERT_EQ(1u, observer_.remote_streams()->count());
2256 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2257 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2258 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2259
2260 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2261 remote_stream->GetVideoTracks()[0];
2262 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2263 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2264 remote_stream->GetAudioTracks()[0];
2265 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2266
kwibergd1fe2812016-04-27 06:47:29 -07002267 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002268 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002269 cricket::ContentInfo* video_info =
2270 local_answer->description()->GetContentByName("video");
2271 video_info->rejected = true;
2272 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2273 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2274 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2275
2276 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002277 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002278 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002279 video_info = local_offer->description()->GetContentByName("video");
2280 ASSERT_TRUE(video_info != nullptr);
2281 video_info->rejected = true;
2282 cricket::ContentInfo* audio_info =
2283 local_offer->description()->GetContentByName("audio");
2284 ASSERT_TRUE(audio_info != nullptr);
2285 audio_info->rejected = true;
2286 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002287 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002288 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002289 remote_audio->state(), kTimeout);
2290 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2291 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002292}
2293
2294// This tests that we won't crash if the remote track has been removed outside
2295// of PeerConnection and then PeerConnection tries to reject the track.
2296TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2297 FakeConstraints constraints;
2298 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2299 true);
2300 CreatePeerConnection(&constraints);
2301 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2302 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2303 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2304 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2305
kwibergd1fe2812016-04-27 06:47:29 -07002306 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002307 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2308 kSdpStringWithStream1, nullptr));
2309 cricket::ContentInfo* video_info =
2310 local_answer->description()->GetContentByName("video");
2311 video_info->rejected = true;
2312 cricket::ContentInfo* audio_info =
2313 local_answer->description()->GetContentByName("audio");
2314 audio_info->rejected = true;
2315 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2316
2317 // No crash is a pass.
2318}
2319
deadbeef5e97fb52015-10-15 12:49:08 -07002320// This tests that if a recvonly remote description is set, no remote streams
2321// will be created, even if the description contains SSRCs/MSIDs.
2322// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2323TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2324 FakeConstraints constraints;
2325 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2326 true);
2327 CreatePeerConnection(&constraints);
2328
2329 std::string recvonly_offer = kSdpStringWithStream1;
2330 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2331 strlen(kRecvonly), &recvonly_offer);
2332 CreateAndSetRemoteOffer(recvonly_offer);
2333
2334 EXPECT_EQ(0u, observer_.remote_streams()->count());
2335}
2336
deadbeefab9b2d12015-10-14 11:33:11 -07002337// This tests that a default MediaStream is created if a remote session
2338// description doesn't contain any streams and no MSID support.
2339// It also tests that the default stream is updated if a video m-line is added
2340// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002341TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002342 FakeConstraints constraints;
2343 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2344 true);
2345 CreatePeerConnection(&constraints);
2346 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2347
2348 ASSERT_EQ(1u, observer_.remote_streams()->count());
2349 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2350
2351 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2352 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2353 EXPECT_EQ("default", remote_stream->label());
2354
2355 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2356 ASSERT_EQ(1u, observer_.remote_streams()->count());
2357 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2358 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002359 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2360 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002361 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2362 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002363 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2364 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002365}
2366
2367// This tests that a default MediaStream is created if a remote session
2368// description doesn't contain any streams and media direction is send only.
2369TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002370 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002371 FakeConstraints constraints;
2372 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2373 true);
2374 CreatePeerConnection(&constraints);
2375 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2376
2377 ASSERT_EQ(1u, observer_.remote_streams()->count());
2378 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2379
2380 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2381 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2382 EXPECT_EQ("default", remote_stream->label());
2383}
2384
2385// This tests that it won't crash when PeerConnection tries to remove
2386// a remote track that as already been removed from the MediaStream.
2387TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2388 FakeConstraints constraints;
2389 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2390 true);
2391 CreatePeerConnection(&constraints);
2392 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2393 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2394 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2395 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2396
2397 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2398
2399 // No crash is a pass.
2400}
2401
2402// This tests that a default MediaStream is created if the remote session
2403// description doesn't contain any streams and don't contain an indication if
2404// MSID is supported.
2405TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002406 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002407 FakeConstraints constraints;
2408 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2409 true);
2410 CreatePeerConnection(&constraints);
2411 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2412
2413 ASSERT_EQ(1u, observer_.remote_streams()->count());
2414 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2415 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2416 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2417}
2418
2419// This tests that a default MediaStream is not created if the remote session
2420// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002421TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002422 FakeConstraints constraints;
2423 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2424 true);
2425 CreatePeerConnection(&constraints);
2426 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2427 EXPECT_EQ(0u, observer_.remote_streams()->count());
2428}
2429
deadbeefbda7e0b2015-12-08 17:13:40 -08002430// This tests that when setting a new description, the old default tracks are
2431// not destroyed and recreated.
2432// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002433TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002434 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002435 FakeConstraints constraints;
2436 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2437 true);
2438 CreatePeerConnection(&constraints);
2439 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2440
2441 ASSERT_EQ(1u, observer_.remote_streams()->count());
2442 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2443 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2444
2445 // Set the track to "disabled", then set a new description and ensure the
2446 // track is still disabled, which ensures it hasn't been recreated.
2447 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2448 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2449 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2450 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2451}
2452
deadbeefab9b2d12015-10-14 11:33:11 -07002453// This tests that a default MediaStream is not created if a remote session
2454// description is updated to not have any MediaStreams.
2455TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2456 FakeConstraints constraints;
2457 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2458 true);
2459 CreatePeerConnection(&constraints);
2460 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002461 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002462 EXPECT_TRUE(
2463 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2464
2465 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2466 EXPECT_EQ(0u, observer_.remote_streams()->count());
2467}
2468
2469// This tests that an RtpSender is created when the local description is set
2470// after adding a local stream.
2471// TODO(deadbeef): This test and the one below it need to be updated when
2472// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002473TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002474 FakeConstraints constraints;
2475 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2476 true);
2477 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002478
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002479 // Create an offer with 1 stream with 2 tracks of each type.
2480 rtc::scoped_refptr<StreamCollection> stream_collection =
2481 CreateStreamCollection(1, 2);
2482 pc_->AddStream(stream_collection->at(0));
2483 std::unique_ptr<SessionDescriptionInterface> offer;
2484 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2485 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002486
deadbeefab9b2d12015-10-14 11:33:11 -07002487 auto senders = pc_->GetSenders();
2488 EXPECT_EQ(4u, senders.size());
2489 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2490 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2491 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2492 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2493
2494 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002495 pc_->RemoveStream(stream_collection->at(0));
2496 stream_collection = CreateStreamCollection(1, 1);
2497 pc_->AddStream(stream_collection->at(0));
2498 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2499 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2500
deadbeefab9b2d12015-10-14 11:33:11 -07002501 senders = pc_->GetSenders();
2502 EXPECT_EQ(2u, senders.size());
2503 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2504 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2505 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2506 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2507}
2508
2509// This tests that an RtpSender is created when the local description is set
2510// before adding a local stream.
2511TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002512 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002513 FakeConstraints constraints;
2514 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2515 true);
2516 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002517
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002518 rtc::scoped_refptr<StreamCollection> stream_collection =
2519 CreateStreamCollection(1, 2);
2520 // Add a stream to create the offer, but remove it afterwards.
2521 pc_->AddStream(stream_collection->at(0));
2522 std::unique_ptr<SessionDescriptionInterface> offer;
2523 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2524 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002525
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002526 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002527 auto senders = pc_->GetSenders();
2528 EXPECT_EQ(0u, senders.size());
2529
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002530 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002531 senders = pc_->GetSenders();
2532 EXPECT_EQ(4u, senders.size());
2533 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2534 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2535 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2536 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2537}
2538
2539// This tests that the expected behavior occurs if the SSRC on a local track is
2540// changed when SetLocalDescription is called.
2541TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002542 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002543 FakeConstraints constraints;
2544 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2545 true);
2546 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002547
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002548 rtc::scoped_refptr<StreamCollection> stream_collection =
2549 CreateStreamCollection(2, 1);
2550 pc_->AddStream(stream_collection->at(0));
2551 std::unique_ptr<SessionDescriptionInterface> offer;
2552 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2553 // Grab a copy of the offer before it gets passed into the PC.
2554 std::unique_ptr<JsepSessionDescription> modified_offer(
2555 new JsepSessionDescription(JsepSessionDescription::kOffer));
2556 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2557 offer->session_version());
2558 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002559
deadbeefab9b2d12015-10-14 11:33:11 -07002560 auto senders = pc_->GetSenders();
2561 EXPECT_EQ(2u, senders.size());
2562 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2563 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2564
2565 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002566 cricket::MediaContentDescription* desc =
2567 cricket::GetFirstAudioContentDescription(modified_offer->description());
2568 ASSERT_TRUE(desc != NULL);
2569 for (StreamParams& stream : desc->mutable_streams()) {
2570 for (unsigned int& ssrc : stream.ssrcs) {
2571 ++ssrc;
2572 }
2573 }
deadbeefab9b2d12015-10-14 11:33:11 -07002574
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002575 desc =
2576 cricket::GetFirstVideoContentDescription(modified_offer->description());
2577 ASSERT_TRUE(desc != NULL);
2578 for (StreamParams& stream : desc->mutable_streams()) {
2579 for (unsigned int& ssrc : stream.ssrcs) {
2580 ++ssrc;
2581 }
2582 }
2583
2584 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002585 senders = pc_->GetSenders();
2586 EXPECT_EQ(2u, senders.size());
2587 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2588 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2589 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2590 // changed.
2591}
2592
2593// This tests that the expected behavior occurs if a new session description is
2594// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002595TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002596 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002597 FakeConstraints constraints;
2598 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2599 true);
2600 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002601
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002602 rtc::scoped_refptr<StreamCollection> stream_collection =
2603 CreateStreamCollection(2, 1);
2604 pc_->AddStream(stream_collection->at(0));
2605 std::unique_ptr<SessionDescriptionInterface> offer;
2606 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2607 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002608
deadbeefab9b2d12015-10-14 11:33:11 -07002609 auto senders = pc_->GetSenders();
2610 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002611 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2612 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002613
2614 // Add a new MediaStream but with the same tracks as in the first stream.
2615 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2616 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002617 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2618 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002619 pc_->AddStream(stream_1);
2620
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002621 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2622 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002623
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002624 auto new_senders = pc_->GetSenders();
2625 // Should be the same senders as before, but with updated stream id.
2626 // Note that this behavior is subject to change in the future.
2627 // We may decide the PC should ignore existing tracks in AddStream.
2628 EXPECT_EQ(senders, new_senders);
2629 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2630 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002631}
2632
nisse51542be2016-02-12 02:27:06 -08002633class PeerConnectionMediaConfigTest : public testing::Test {
2634 protected:
2635 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002636 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002637 pcf_->Initialize();
2638 }
2639 const cricket::MediaConfig& TestCreatePeerConnection(
2640 const PeerConnectionInterface::RTCConfiguration& config,
2641 const MediaConstraintsInterface *constraints) {
2642 pcf_->create_media_controller_called_ = false;
2643
deadbeef907abe42016-08-04 12:22:18 -07002644 scoped_refptr<PeerConnectionInterface> pc(
2645 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2646 &observer_));
nisse51542be2016-02-12 02:27:06 -08002647 EXPECT_TRUE(pc.get());
2648 EXPECT_TRUE(pcf_->create_media_controller_called_);
2649 return pcf_->create_media_controller_config_;
2650 }
2651
deadbeef907abe42016-08-04 12:22:18 -07002652 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002653 MockPeerConnectionObserver observer_;
2654};
2655
2656// This test verifies the default behaviour with no constraints and a
2657// default RTCConfiguration.
2658TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2659 PeerConnectionInterface::RTCConfiguration config;
2660 FakeConstraints constraints;
2661
2662 const cricket::MediaConfig& media_config =
2663 TestCreatePeerConnection(config, &constraints);
2664
2665 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002666 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2667 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2668 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002669}
2670
2671// This test verifies the DSCP constraint is recognized and passed to
2672// the CreateMediaController call.
2673TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2674 PeerConnectionInterface::RTCConfiguration config;
2675 FakeConstraints constraints;
2676
2677 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2678 const cricket::MediaConfig& media_config =
2679 TestCreatePeerConnection(config, &constraints);
2680
2681 EXPECT_TRUE(media_config.enable_dscp);
2682}
2683
2684// This test verifies the cpu overuse detection constraint is
2685// recognized and passed to the CreateMediaController call.
2686TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2687 PeerConnectionInterface::RTCConfiguration config;
2688 FakeConstraints constraints;
2689
2690 constraints.AddOptional(
2691 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2692 const cricket::MediaConfig media_config =
2693 TestCreatePeerConnection(config, &constraints);
2694
nisse0db023a2016-03-01 04:29:59 -08002695 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002696}
2697
2698// This test verifies that the disable_prerenderer_smoothing flag is
2699// propagated from RTCConfiguration to the CreateMediaController call.
2700TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2701 PeerConnectionInterface::RTCConfiguration config;
2702 FakeConstraints constraints;
2703
Niels Möller71bdda02016-03-31 12:59:59 +02002704 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002705 const cricket::MediaConfig& media_config =
2706 TestCreatePeerConnection(config, &constraints);
2707
nisse0db023a2016-03-01 04:29:59 -08002708 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2709}
2710
2711// This test verifies the suspend below min bitrate constraint is
2712// recognized and passed to the CreateMediaController call.
2713TEST_F(PeerConnectionMediaConfigTest,
2714 TestSuspendBelowMinBitrateConstraintTrue) {
2715 PeerConnectionInterface::RTCConfiguration config;
2716 FakeConstraints constraints;
2717
2718 constraints.AddOptional(
2719 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2720 true);
2721 const cricket::MediaConfig media_config =
2722 TestCreatePeerConnection(config, &constraints);
2723
2724 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002725}
2726
deadbeefab9b2d12015-10-14 11:33:11 -07002727// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002728// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2729// "verify options are converted correctly", should be "pass options into
2730// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002731
2732TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2733 RTCOfferAnswerOptions rtc_options;
2734 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2735
2736 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002737 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002738
2739 rtc_options.offer_to_receive_audio =
2740 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002741 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002742}
2743
2744TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2745 RTCOfferAnswerOptions rtc_options;
2746 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2747
2748 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002749 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002750
2751 rtc_options.offer_to_receive_video =
2752 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002753 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002754}
2755
2756// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002757// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002758TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2759 RTCOfferAnswerOptions rtc_options;
2760 rtc_options.offer_to_receive_audio = 1;
2761 rtc_options.offer_to_receive_video = 1;
2762
2763 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002764 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002765 EXPECT_TRUE(options.has_audio());
2766 EXPECT_TRUE(options.has_video());
2767 EXPECT_TRUE(options.bundle_enabled);
2768}
2769
2770// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002771// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002772TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2773 RTCOfferAnswerOptions rtc_options;
2774 rtc_options.offer_to_receive_audio = 1;
2775
2776 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002777 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002778 EXPECT_TRUE(options.has_audio());
2779 EXPECT_FALSE(options.has_video());
2780 EXPECT_TRUE(options.bundle_enabled);
2781}
2782
2783// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002784// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002785TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2786 RTCOfferAnswerOptions rtc_options;
2787
2788 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002789 options.transport_options["audio"] = cricket::TransportOptions();
2790 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002791 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002792 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002793 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002794 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002795 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002796 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2797 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002798}
2799
2800// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002801// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002802TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2803 RTCOfferAnswerOptions rtc_options;
2804 rtc_options.offer_to_receive_audio = 0;
2805 rtc_options.offer_to_receive_video = 1;
2806
2807 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002808 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002809 EXPECT_FALSE(options.has_audio());
2810 EXPECT_TRUE(options.has_video());
2811 EXPECT_TRUE(options.bundle_enabled);
2812}
2813
2814// Test that a correct MediaSessionOptions is created for an offer if
2815// UseRtpMux is set to false.
2816TEST(CreateSessionOptionsTest,
2817 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2818 RTCOfferAnswerOptions rtc_options;
2819 rtc_options.offer_to_receive_audio = 1;
2820 rtc_options.offer_to_receive_video = 1;
2821 rtc_options.use_rtp_mux = false;
2822
2823 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002824 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002825 EXPECT_TRUE(options.has_audio());
2826 EXPECT_TRUE(options.has_video());
2827 EXPECT_FALSE(options.bundle_enabled);
2828}
2829
2830// Test that a correct MediaSessionOptions is created to restart ice if
2831// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002832// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002833TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2834 RTCOfferAnswerOptions rtc_options;
2835 rtc_options.ice_restart = true;
2836
2837 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002838 options.transport_options["audio"] = cricket::TransportOptions();
2839 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002840 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002841 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2842 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002843
2844 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002845 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002846 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2847 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002848}
2849
2850// Test that the MediaConstraints in an answer don't affect if audio and video
2851// is offered in an offer but that if kOfferToReceiveAudio or
2852// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2853// included in subsequent answers.
2854TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2855 FakeConstraints answer_c;
2856 answer_c.SetMandatoryReceiveAudio(true);
2857 answer_c.SetMandatoryReceiveVideo(true);
2858
2859 cricket::MediaSessionOptions answer_options;
2860 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2861 EXPECT_TRUE(answer_options.has_audio());
2862 EXPECT_TRUE(answer_options.has_video());
2863
deadbeefc80741f2015-10-22 13:14:45 -07002864 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002865
2866 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002867 EXPECT_TRUE(
2868 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002869 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002870 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002871
deadbeefc80741f2015-10-22 13:14:45 -07002872 RTCOfferAnswerOptions updated_rtc_offer_options;
2873 updated_rtc_offer_options.offer_to_receive_audio = 1;
2874 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002875
2876 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002877 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002878 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002879 EXPECT_TRUE(updated_offer_options.has_audio());
2880 EXPECT_TRUE(updated_offer_options.has_video());
2881
2882 // Since an offer has been created with both audio and video, subsequent
2883 // offers and answers should contain both audio and video.
2884 // Answers will only contain the media types that exist in the offer
2885 // regardless of the value of |updated_answer_options.has_audio| and
2886 // |updated_answer_options.has_video|.
2887 FakeConstraints updated_answer_c;
2888 answer_c.SetMandatoryReceiveAudio(false);
2889 answer_c.SetMandatoryReceiveVideo(false);
2890
2891 cricket::MediaSessionOptions updated_answer_options;
2892 EXPECT_TRUE(
2893 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2894 EXPECT_TRUE(updated_answer_options.has_audio());
2895 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002896}