blob: c35141224c058b9ba511a291f359bc4ebc6959fa [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediastream.h"
18#include "webrtc/api/mediastreaminterface.h"
19#include "webrtc/api/peerconnection.h"
20#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/streamcollection.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010024#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020025#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070026#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010027#include "webrtc/api/test/mockpeerconnectionobservers.h"
28#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010029#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/sslstreamadapter.h"
34#include "webrtc/base/stringutils.h"
35#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/fakevideocapturer.h"
37#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070038#include "webrtc/p2p/base/fakeportallocator.h"
zhihuang29ff8442016-07-27 11:07:25 -070039#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
kwibergac9f8762016-09-30 22:29:43 -070041#include "webrtc/test/gmock.h"
42
43#ifdef WEBRTC_ANDROID
44#include "webrtc/api/test/androidtestinitializer.h"
45#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47static const char kStreamLabel1[] = "local_stream_1";
48static const char kStreamLabel2[] = "local_stream_2";
49static const char kStreamLabel3[] = "local_stream_3";
50static const int kDefaultStunPort = 3478;
51static const char kStunAddressOnly[] = "stun:address";
52static const char kStunInvalidPort[] = "stun:address:-1";
53static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
54static const char kStunAddressPortAndMore2[] = "stun:address:port more";
55static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
56static const char kTurnUsername[] = "user";
57static const char kTurnPassword[] = "password";
58static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020059static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
deadbeefab9b2d12015-10-14 11:33:11 -070061static const char kStreams[][8] = {"stream1", "stream2"};
62static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
63static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
64
deadbeef5e97fb52015-10-15 12:49:08 -070065static const char kRecvonly[] = "recvonly";
66static const char kSendrecv[] = "sendrecv";
67
deadbeefab9b2d12015-10-14 11:33:11 -070068// Reference SDP with a MediaStream with label "stream1" and audio track with
69// id "audio_1" and a video track with id "video_1;
70static const char kSdpStringWithStream1[] =
71 "v=0\r\n"
72 "o=- 0 0 IN IP4 127.0.0.1\r\n"
73 "s=-\r\n"
74 "t=0 0\r\n"
75 "a=ice-ufrag:e5785931\r\n"
76 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
77 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
78 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
79 "m=audio 1 RTP/AVPF 103\r\n"
80 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070081 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070082 "a=rtpmap:103 ISAC/16000\r\n"
83 "a=ssrc:1 cname:stream1\r\n"
84 "a=ssrc:1 mslabel:stream1\r\n"
85 "a=ssrc:1 label:audiotrack0\r\n"
86 "m=video 1 RTP/AVPF 120\r\n"
87 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070089 "a=rtpmap:120 VP8/90000\r\n"
90 "a=ssrc:2 cname:stream1\r\n"
91 "a=ssrc:2 mslabel:stream1\r\n"
92 "a=ssrc:2 label:videotrack0\r\n";
93
zhihuang81c3a032016-11-17 12:06:24 -080094// Reference SDP with a MediaStream with label "stream1" and audio track with
95// id "audio_1";
96static const char kSdpStringWithStream1AudioTrackOnly[] =
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 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
107 "a=sendrecv\r\n"
108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 mslabel:stream1\r\n"
111 "a=ssrc:1 label:audiotrack0\r\n";
112
deadbeefab9b2d12015-10-14 11:33:11 -0700113// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
114// MediaStreams have one audio track and one video track.
115// This uses MSID.
116static const char kSdpStringWithStream1And2[] =
117 "v=0\r\n"
118 "o=- 0 0 IN IP4 127.0.0.1\r\n"
119 "s=-\r\n"
120 "t=0 0\r\n"
121 "a=ice-ufrag:e5785931\r\n"
122 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
123 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
124 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
125 "a=msid-semantic: WMS stream1 stream2\r\n"
126 "m=audio 1 RTP/AVPF 103\r\n"
127 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700128 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700129 "a=rtpmap:103 ISAC/16000\r\n"
130 "a=ssrc:1 cname:stream1\r\n"
131 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
132 "a=ssrc:3 cname:stream2\r\n"
133 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
134 "m=video 1 RTP/AVPF 120\r\n"
135 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:120 VP8/0\r\n"
138 "a=ssrc:2 cname:stream1\r\n"
139 "a=ssrc:2 msid:stream1 videotrack0\r\n"
140 "a=ssrc:4 cname:stream2\r\n"
141 "a=ssrc:4 msid:stream2 videotrack1\r\n";
142
143// Reference SDP without MediaStreams. Msid is not supported.
144static const char kSdpStringWithoutStreams[] =
145 "v=0\r\n"
146 "o=- 0 0 IN IP4 127.0.0.1\r\n"
147 "s=-\r\n"
148 "t=0 0\r\n"
149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\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. Msid is supported.
163static const char kSdpStringWithMsidWithoutStreams[] =
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 "a=msid-semantic: WMS\r\n"
173 "m=audio 1 RTP/AVPF 103\r\n"
174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700176 "a=rtpmap:103 ISAC/16000\r\n"
177 "m=video 1 RTP/AVPF 120\r\n"
178 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700179 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=rtpmap:120 VP8/90000\r\n";
181
182// Reference SDP without MediaStreams and audio only.
183static const char kSdpStringWithoutStreamsAudioOnly[] =
184 "v=0\r\n"
185 "o=- 0 0 IN IP4 127.0.0.1\r\n"
186 "s=-\r\n"
187 "t=0 0\r\n"
188 "a=ice-ufrag:e5785931\r\n"
189 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
190 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
191 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
192 "m=audio 1 RTP/AVPF 103\r\n"
193 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700194 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700195 "a=rtpmap:103 ISAC/16000\r\n";
196
197// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
198static const char kSdpStringSendOnlyWithoutStreams[] =
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 "m=audio 1 RTP/AVPF 103\r\n"
208 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700209 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=sendonly\r\n"
211 "a=rtpmap:103 ISAC/16000\r\n"
212 "m=video 1 RTP/AVPF 120\r\n"
213 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700214 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700215 "a=sendonly\r\n"
216 "a=rtpmap:120 VP8/90000\r\n";
217
218static const char kSdpStringInit[] =
219 "v=0\r\n"
220 "o=- 0 0 IN IP4 127.0.0.1\r\n"
221 "s=-\r\n"
222 "t=0 0\r\n"
223 "a=ice-ufrag:e5785931\r\n"
224 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
225 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
226 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
227 "a=msid-semantic: WMS\r\n";
228
229static const char kSdpStringAudio[] =
230 "m=audio 1 RTP/AVPF 103\r\n"
231 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700232 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700233 "a=rtpmap:103 ISAC/16000\r\n";
234
235static const char kSdpStringVideo[] =
236 "m=video 1 RTP/AVPF 120\r\n"
237 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700238 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=rtpmap:120 VP8/90000\r\n";
240
241static const char kSdpStringMs1Audio0[] =
242 "a=ssrc:1 cname:stream1\r\n"
243 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
244
245static const char kSdpStringMs1Video0[] =
246 "a=ssrc:2 cname:stream1\r\n"
247 "a=ssrc:2 msid:stream1 videotrack0\r\n";
248
249static const char kSdpStringMs1Audio1[] =
250 "a=ssrc:3 cname:stream1\r\n"
251 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
252
253static const char kSdpStringMs1Video1[] =
254 "a=ssrc:4 cname:stream1\r\n"
255 "a=ssrc:4 msid:stream1 videotrack1\r\n";
256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257#define MAYBE_SKIP_TEST(feature) \
258 if (!(feature())) { \
259 LOG(LS_INFO) << "Feature disabled... skipping"; \
260 return; \
261 }
262
perkjd61bf802016-03-24 03:16:19 -0700263using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700264using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700266using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::AudioTrackInterface;
268using webrtc::DataBuffer;
269using webrtc::DataChannelInterface;
270using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700272using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700273using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700274using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275using webrtc::MediaStreamInterface;
276using webrtc::MediaStreamTrackInterface;
277using webrtc::MockCreateSessionDescriptionObserver;
278using webrtc::MockDataChannelObserver;
279using webrtc::MockSetSessionDescriptionObserver;
280using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700281using webrtc::NotifierInterface;
282using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283using webrtc::PeerConnectionInterface;
284using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700285using webrtc::RtpReceiverInterface;
286using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287using webrtc::SdpParseError;
288using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700289using webrtc::StreamCollection;
290using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100291using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700292using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293using webrtc::VideoTrackInterface;
294
deadbeefab9b2d12015-10-14 11:33:11 -0700295typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297namespace {
298
299// Gets the first ssrc of given content type from the ContentInfo.
300bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
301 if (!content_info || !ssrc) {
302 return false;
303 }
304 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000305 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 content_info->description);
307 if (!media_desc || media_desc->streams().empty()) {
308 return false;
309 }
310 *ssrc = media_desc->streams().begin()->first_ssrc();
311 return true;
312}
313
314void SetSsrcToZero(std::string* sdp) {
315 const char kSdpSsrcAtribute[] = "a=ssrc:";
316 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
317 size_t ssrc_pos = 0;
318 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
319 std::string::npos) {
320 size_t end_ssrc = sdp->find(" ", ssrc_pos);
321 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
322 ssrc_pos = end_ssrc;
323 }
324}
325
deadbeefab9b2d12015-10-14 11:33:11 -0700326// Check if |streams| contains the specified track.
327bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
328 const std::string& stream_label,
329 const std::string& track_id) {
330 for (const cricket::StreamParams& params : streams) {
331 if (params.sync_label == stream_label && params.id == track_id) {
332 return true;
333 }
334 }
335 return false;
336}
337
338// Check if |senders| contains the specified sender, by id.
339bool ContainsSender(
340 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
341 const std::string& id) {
342 for (const auto& sender : senders) {
343 if (sender->id() == id) {
344 return true;
345 }
346 }
347 return false;
348}
349
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700350// Check if |senders| contains the specified sender, by id and stream id.
351bool ContainsSender(
352 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
353 const std::string& id,
354 const std::string& stream_id) {
355 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700356 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700357 return true;
358 }
359 }
360 return false;
361}
362
deadbeefab9b2d12015-10-14 11:33:11 -0700363// Create a collection of streams.
364// CreateStreamCollection(1) creates a collection that
365// correspond to kSdpStringWithStream1.
366// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
367rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700368 int number_of_streams,
369 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700370 rtc::scoped_refptr<StreamCollection> local_collection(
371 StreamCollection::Create());
372
373 for (int i = 0; i < number_of_streams; ++i) {
374 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
375 webrtc::MediaStream::Create(kStreams[i]));
376
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700377 for (int j = 0; j < tracks_per_stream; ++j) {
378 // Add a local audio track.
379 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
380 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
381 nullptr));
382 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700383
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700384 // Add a local video track.
385 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
386 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
387 webrtc::FakeVideoTrackSource::Create()));
388 stream->AddTrack(video_track);
389 }
deadbeefab9b2d12015-10-14 11:33:11 -0700390
391 local_collection->AddStream(stream);
392 }
393 return local_collection;
394}
395
396// Check equality of StreamCollections.
397bool CompareStreamCollections(StreamCollectionInterface* s1,
398 StreamCollectionInterface* s2) {
399 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
400 return false;
401 }
402
403 for (size_t i = 0; i != s1->count(); ++i) {
404 if (s1->at(i)->label() != s2->at(i)->label()) {
405 return false;
406 }
407 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
408 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
409 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
410 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
411
412 if (audio_tracks1.size() != audio_tracks2.size()) {
413 return false;
414 }
415 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
416 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
417 return false;
418 }
419 }
420 if (video_tracks1.size() != video_tracks2.size()) {
421 return false;
422 }
423 for (size_t j = 0; j != video_tracks1.size(); ++j) {
424 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
425 return false;
426 }
427 }
428 }
429 return true;
430}
431
perkjd61bf802016-03-24 03:16:19 -0700432// Helper class to test Observer.
433class MockTrackObserver : public ObserverInterface {
434 public:
435 explicit MockTrackObserver(NotifierInterface* notifier)
436 : notifier_(notifier) {
437 notifier_->RegisterObserver(this);
438 }
439
440 ~MockTrackObserver() { Unregister(); }
441
442 void Unregister() {
443 if (notifier_) {
444 notifier_->UnregisterObserver(this);
445 notifier_ = nullptr;
446 }
447 }
448
449 MOCK_METHOD0(OnChanged, void());
450
451 private:
452 NotifierInterface* notifier_;
453};
454
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455class MockPeerConnectionObserver : public PeerConnectionObserver {
456 public:
kjellander71a1b612016-11-07 01:18:08 -0800457 // We need these using declarations because there are two versions of each of
458 // the below methods and we only override one of them.
459 // TODO(deadbeef): Remove once there's only one version of the methods.
460 using PeerConnectionObserver::OnAddStream;
461 using PeerConnectionObserver::OnRemoveStream;
462 using PeerConnectionObserver::OnDataChannel;
463
deadbeefab9b2d12015-10-14 11:33:11 -0700464 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200465 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
467 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
468 pc_ = pc;
469 if (pc) {
470 state_ = pc_->signaling_state();
471 }
472 }
nisseef8b61e2016-04-29 06:09:15 -0700473 void OnSignalingChange(
474 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 EXPECT_EQ(pc_->signaling_state(), new_state);
476 state_ = new_state;
477 }
478 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
479 virtual void OnStateChange(StateType state_changed) {
480 if (pc_.get() == NULL)
481 return;
482 switch (state_changed) {
483 case kSignalingState:
484 // OnSignalingChange and OnStateChange(kSignalingState) should always
485 // be called approximately simultaneously. To ease testing, we require
486 // that they always be called in that order. This check verifies
487 // that OnSignalingChange has just been called.
488 EXPECT_EQ(pc_->signaling_state(), state_);
489 break;
490 case kIceState:
491 ADD_FAILURE();
492 break;
493 default:
494 ADD_FAILURE();
495 break;
496 }
497 }
deadbeefab9b2d12015-10-14 11:33:11 -0700498
499 MediaStreamInterface* RemoteStream(const std::string& label) {
500 return remote_streams_->find(label);
501 }
502 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700503 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700505 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700507 void OnRemoveStream(
508 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700510 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
perkjdfb769d2016-02-09 03:09:43 -0800512 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700513 void OnDataChannel(
514 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 last_datachannel_ = data_channel;
516 }
517
perkjdfb769d2016-02-09 03:09:43 -0800518 void OnIceConnectionChange(
519 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800521 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
perkjdfb769d2016-02-09 03:09:43 -0800523 void OnIceGatheringChange(
524 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800526 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800527 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 }
perkjdfb769d2016-02-09 03:09:43 -0800529 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
531 pc_->ice_gathering_state());
532
533 std::string sdp;
534 EXPECT_TRUE(candidate->ToString(&sdp));
535 EXPECT_LT(0u, sdp.size());
536 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
537 candidate->sdp_mline_index(), sdp, NULL));
538 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800539 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700540 }
541
542 void OnIceCandidatesRemoved(
543 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800544 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700545 }
546
547 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800548 callback_triggered_ = true;
549 }
550
551 void OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
552 std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>
553 streams) override {
554 EXPECT_TRUE(receiver != nullptr);
555 num_added_tracks_++;
556 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558
559 // Returns the label of the last added stream.
560 // Empty string if no stream have been added.
561 std::string GetLastAddedStreamLabel() {
562 if (last_added_stream_.get())
563 return last_added_stream_->label();
564 return "";
565 }
566 std::string GetLastRemovedStreamLabel() {
567 if (last_removed_stream_.get())
568 return last_removed_stream_->label();
569 return "";
570 }
571
zhihuang9763d562016-08-05 11:14:50 -0700572 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700574 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700575 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700576 rtc::scoped_refptr<StreamCollection> remote_streams_;
577 bool renegotiation_needed_ = false;
578 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800579 bool callback_triggered_ = false;
580 int num_added_tracks_ = 0;
581 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582
583 private:
zhihuang9763d562016-08-05 11:14:50 -0700584 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
585 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586};
587
588} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700589
zhihuang29ff8442016-07-27 11:07:25 -0700590// The PeerConnectionMediaConfig tests below verify that configuration
591// and constraints are propagated into the MediaConfig passed to
592// CreateMediaController. These settings are intended for MediaChannel
593// constructors, but that is not exercised by these unittest.
594class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
595 public:
596 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700597 const cricket::MediaConfig& config,
598 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700599 create_media_controller_called_ = true;
600 create_media_controller_config_ = config;
601
602 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700603 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700604 EXPECT_TRUE(mc != nullptr);
605 return mc;
606 }
607
608 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700609 cricket::PortAllocator* port_allocator,
610 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700611 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700612 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
613 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700614 return transport_controller;
615 }
616
617 cricket::TransportController* transport_controller;
618 // Mutable, so they can be modified in the above const-declared method.
619 mutable bool create_media_controller_called_ = false;
620 mutable cricket::MediaConfig create_media_controller_config_;
621};
622
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623class PeerConnectionInterfaceTest : public testing::Test {
624 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800625 PeerConnectionInterfaceTest() {
626#ifdef WEBRTC_ANDROID
627 webrtc::InitializeAndroidObjects();
628#endif
629 }
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 virtual void SetUp() {
632 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700633 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
634 nullptr, nullptr, nullptr);
635 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700636 pc_factory_for_test_ =
637 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
638 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 }
640
641 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700642 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 }
644
645 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700646 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
647 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 }
649
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700650 void CreatePeerConnectionWithIceTransportsType(
651 PeerConnectionInterface::IceTransportsType type) {
652 PeerConnectionInterface::RTCConfiguration config;
653 config.type = type;
654 return CreatePeerConnection(config, nullptr);
655 }
656
657 void CreatePeerConnectionWithIceServer(const std::string& uri,
658 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800659 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700661 server.uri = uri;
662 server.password = password;
663 config.servers.push_back(server);
664 CreatePeerConnection(config, nullptr);
665 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700667 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
668 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700669 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800670 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
671 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000672
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000673 // DTLS does not work in a loopback call, so is disabled for most of the
674 // tests in this file. We only create a FakeIdentityService if the test
675 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000676 FakeConstraints default_constraints;
677 if (!constraints) {
678 constraints = &default_constraints;
679
680 default_constraints.AddMandatory(
681 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
682 }
683
Henrik Boströmd79599d2016-06-01 13:58:50 +0200684 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000685 bool dtls;
686 if (FindConstraint(constraints,
687 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
688 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200689 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200690 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000691 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200692 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800693 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200694 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 ASSERT_TRUE(pc_.get() != NULL);
696 observer_.SetPeerConnectionInterface(pc_.get());
697 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
698 }
699
deadbeef0a6c4ca2015-10-06 11:38:28 -0700700 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800701 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700702 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700703 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800704 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700705
zhihuang9763d562016-08-05 11:14:50 -0700706 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700707 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
708 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700710 }
711
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700713 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800714 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
715 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
716 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719
deadbeef0a6c4ca2015-10-06 11:38:28 -0700720 CreatePeerConnectionExpectFail(kStunInvalidPort);
721 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
722 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700724 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800725 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
726 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800730 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800732 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 }
734
735 void ReleasePeerConnection() {
736 pc_ = NULL;
737 observer_.SetPeerConnectionInterface(NULL);
738 }
739
deadbeefab9b2d12015-10-14 11:33:11 -0700740 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700742 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700744 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700746 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 pc_factory_->CreateVideoTrack(label + "v0", video_source));
748 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000749 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
751 observer_.renegotiation_needed_ = false;
752 }
753
754 void AddVoiceStream(const std::string& label) {
755 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700756 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700758 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 pc_factory_->CreateAudioTrack(label + "a0", NULL));
760 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000761 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
763 observer_.renegotiation_needed_ = false;
764 }
765
766 void AddAudioVideoStream(const std::string& stream_label,
767 const std::string& audio_track_label,
768 const std::string& video_track_label) {
769 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700770 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 pc_factory_->CreateAudioTrack(
774 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
775 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700776 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700777 pc_factory_->CreateVideoTrack(
778 video_track_label,
779 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000781 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
783 observer_.renegotiation_needed_ = false;
784 }
785
kwibergd1fe2812016-04-27 06:47:29 -0700786 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700787 bool offer,
788 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000789 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
790 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 MockCreateSessionDescriptionObserver>());
792 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700793 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700795 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 }
797 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700798 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 return observer->result();
800 }
801
kwibergd1fe2812016-04-27 06:47:29 -0700802 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700803 MediaConstraintsInterface* constraints) {
804 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 }
806
kwibergd1fe2812016-04-27 06:47:29 -0700807 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700808 MediaConstraintsInterface* constraints) {
809 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811
812 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
814 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 MockSetSessionDescriptionObserver>());
816 if (local) {
817 pc_->SetLocalDescription(observer, desc);
818 } else {
819 pc_->SetRemoteDescription(observer, desc);
820 }
zhihuang29ff8442016-07-27 11:07:25 -0700821 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
822 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 return observer->result();
825 }
826
827 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
828 return DoSetSessionDescription(desc, true);
829 }
830
831 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
832 return DoSetSessionDescription(desc, false);
833 }
834
835 // Calls PeerConnection::GetStats and check the return value.
836 // It does not verify the values in the StatReports since a RTCP packet might
837 // be required.
838 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000839 rtc::scoped_refptr<MockStatsObserver> observer(
840 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000841 if (!pc_->GetStats(
842 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 return false;
844 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
845 return observer->called();
846 }
847
848 void InitiateCall() {
849 CreatePeerConnection();
850 // Create a local stream with audio&video tracks.
851 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
852 CreateOfferReceiveAnswer();
853 }
854
855 // Verify that RTP Header extensions has been negotiated for audio and video.
856 void VerifyRemoteRtpHeaderExtensions() {
857 const cricket::MediaContentDescription* desc =
858 cricket::GetFirstAudioContentDescription(
859 pc_->remote_description()->description());
860 ASSERT_TRUE(desc != NULL);
861 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
862
863 desc = cricket::GetFirstVideoContentDescription(
864 pc_->remote_description()->description());
865 ASSERT_TRUE(desc != NULL);
866 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
867 }
868
869 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700870 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700871 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 std::string sdp;
873 EXPECT_TRUE(offer->ToString(&sdp));
874 SessionDescriptionInterface* remote_offer =
875 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
876 sdp, NULL);
877 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
878 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
879 }
880
deadbeefab9b2d12015-10-14 11:33:11 -0700881 void CreateAndSetRemoteOffer(const std::string& sdp) {
882 SessionDescriptionInterface* remote_offer =
883 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
884 sdp, nullptr);
885 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
886 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
887 }
888
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700890 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700891 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892
893 // 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(answer->ToString(&sdp));
902 SessionDescriptionInterface* new_answer =
903 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
904 sdp, NULL);
905 EXPECT_TRUE(DoSetLocalDescription(new_answer));
906 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
907 }
908
909 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700910 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700911 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912
913 std::string sdp;
914 EXPECT_TRUE(answer->ToString(&sdp));
915 SessionDescriptionInterface* pr_answer =
916 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
917 sdp, NULL);
918 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
919 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
920 }
921
922 void CreateOfferReceiveAnswer() {
923 CreateOfferAsLocalDescription();
924 std::string sdp;
925 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
926 CreateAnswerAsRemoteDescription(sdp);
927 }
928
929 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700930 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700931 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
933 // audio codec change, even if the parameter has nothing to do with
934 // receiving. Not all parameters are serialized to SDP.
935 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
936 // the SessionDescription, it is necessary to do that here to in order to
937 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
938 // https://code.google.com/p/webrtc/issues/detail?id=1356
939 std::string sdp;
940 EXPECT_TRUE(offer->ToString(&sdp));
941 SessionDescriptionInterface* new_offer =
942 webrtc::CreateSessionDescription(
943 SessionDescriptionInterface::kOffer,
944 sdp, NULL);
945
946 EXPECT_TRUE(DoSetLocalDescription(new_offer));
947 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000948 // Wait for the ice_complete message, so that SDP will have candidates.
949 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 }
951
deadbeefab9b2d12015-10-14 11:33:11 -0700952 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
954 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700955 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 EXPECT_TRUE(DoSetRemoteDescription(answer));
957 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
958 }
959
deadbeefab9b2d12015-10-14 11:33:11 -0700960 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 webrtc::JsepSessionDescription* pr_answer =
962 new webrtc::JsepSessionDescription(
963 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700964 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
966 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
967 webrtc::JsepSessionDescription* answer =
968 new webrtc::JsepSessionDescription(
969 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700970 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 EXPECT_TRUE(DoSetRemoteDescription(answer));
972 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
973 }
974
975 // Help function used for waiting until a the last signaled remote stream has
976 // the same label as |stream_label|. In a few of the tests in this file we
977 // answer with the same session description as we offer and thus we can
978 // check if OnAddStream have been called with the same stream as we offer to
979 // send.
980 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
981 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
982 }
983
984 // Creates an offer and applies it as a local session description.
985 // Creates an answer with the same SDP an the offer but removes all lines
986 // that start with a:ssrc"
987 void CreateOfferReceiveAnswerWithoutSsrc() {
988 CreateOfferAsLocalDescription();
989 std::string sdp;
990 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
991 SetSsrcToZero(&sdp);
992 CreateAnswerAsRemoteDescription(sdp);
993 }
994
deadbeefab9b2d12015-10-14 11:33:11 -0700995 // This function creates a MediaStream with label kStreams[0] and
996 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
997 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700998 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700999 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001001 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1002 size_t number_of_video_tracks) {
1003 EXPECT_LE(number_of_audio_tracks, 2u);
1004 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001005
1006 reference_collection_ = StreamCollection::Create();
1007 std::string sdp_ms1 = std::string(kSdpStringInit);
1008
1009 std::string mediastream_label = kStreams[0];
1010
1011 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1012 webrtc::MediaStream::Create(mediastream_label));
1013 reference_collection_->AddStream(stream);
1014
1015 if (number_of_audio_tracks > 0) {
1016 sdp_ms1 += std::string(kSdpStringAudio);
1017 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1018 AddAudioTrack(kAudioTracks[0], stream);
1019 }
1020 if (number_of_audio_tracks > 1) {
1021 sdp_ms1 += kSdpStringMs1Audio1;
1022 AddAudioTrack(kAudioTracks[1], stream);
1023 }
1024
1025 if (number_of_video_tracks > 0) {
1026 sdp_ms1 += std::string(kSdpStringVideo);
1027 sdp_ms1 += std::string(kSdpStringMs1Video0);
1028 AddVideoTrack(kVideoTracks[0], stream);
1029 }
1030 if (number_of_video_tracks > 1) {
1031 sdp_ms1 += kSdpStringMs1Video1;
1032 AddVideoTrack(kVideoTracks[1], stream);
1033 }
1034
kwibergd1fe2812016-04-27 06:47:29 -07001035 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001036 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1037 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001038 }
1039
1040 void AddAudioTrack(const std::string& track_id,
1041 MediaStreamInterface* stream) {
1042 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1043 webrtc::AudioTrack::Create(track_id, nullptr));
1044 ASSERT_TRUE(stream->AddTrack(audio_track));
1045 }
1046
1047 void AddVideoTrack(const std::string& track_id,
1048 MediaStreamInterface* stream) {
1049 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001050 webrtc::VideoTrack::Create(track_id,
1051 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001052 ASSERT_TRUE(stream->AddTrack(video_track));
1053 }
1054
kwibergfd8be342016-05-14 19:44:11 -07001055 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001056 CreatePeerConnection();
1057 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001058 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001059 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1060 return offer;
1061 }
1062
kwibergfd8be342016-05-14 19:44:11 -07001063 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001064 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001065 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001066 CreateOfferWithOneAudioStream();
1067 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001068 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001069 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1070 return answer;
1071 }
1072
1073 const std::string& GetFirstAudioStreamCname(
1074 const SessionDescriptionInterface* desc) {
1075 const cricket::ContentInfo* audio_content =
1076 cricket::GetFirstAudioContent(desc->description());
1077 const cricket::AudioContentDescription* audio_desc =
1078 static_cast<const cricket::AudioContentDescription*>(
1079 audio_content->description);
1080 return audio_desc->streams()[0].cname;
1081 }
1082
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001083 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001084 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1085 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1086 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001088 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089};
1090
zhihuang29ff8442016-07-27 11:07:25 -07001091// Test that no callbacks on the PeerConnectionObserver are called after the
1092// PeerConnection is closed.
1093TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001094 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001095 pc_factory_for_test_->CreatePeerConnection(
1096 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1097 nullptr, &observer_));
1098 observer_.SetPeerConnectionInterface(pc.get());
1099 pc->Close();
1100
1101 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001102 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001103 std::vector<cricket::Candidate> candidates;
1104 pc_factory_for_test_->transport_controller->SignalGatheringState(
1105 cricket::IceGatheringState{});
1106 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1107 "", candidates);
1108 pc_factory_for_test_->transport_controller->SignalConnectionState(
1109 cricket::IceConnectionState{});
1110 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1111 candidates);
1112 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001113 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001114}
1115
zhihuang8f65cdf2016-05-06 18:40:30 -07001116// Generate different CNAMEs when PeerConnections are created.
1117// The CNAMEs are expected to be generated randomly. It is possible
1118// that the test fails, though the possibility is very low.
1119TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001120 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001121 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001122 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001123 CreateOfferWithOneAudioStream();
1124 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1125 GetFirstAudioStreamCname(offer2.get()));
1126}
1127
1128TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001129 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001130 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001131 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001132 CreateAnswerWithOneAudioStream();
1133 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1134 GetFirstAudioStreamCname(answer2.get()));
1135}
1136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137TEST_F(PeerConnectionInterfaceTest,
1138 CreatePeerConnectionWithDifferentConfigurations) {
1139 CreatePeerConnectionWithDifferentConfigurations();
1140}
1141
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001142TEST_F(PeerConnectionInterfaceTest,
1143 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1144 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1145 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1146 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1147 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1148 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1149 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1150 port_allocator_->candidate_filter());
1151 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1152 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1153}
1154
1155// Test that when a PeerConnection is created with a nonzero candidate pool
1156// size, the pooled PortAllocatorSession is created with all the attributes
1157// in the RTCConfiguration.
1158TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1159 PeerConnectionInterface::RTCConfiguration config;
1160 PeerConnectionInterface::IceServer server;
1161 server.uri = kStunAddressOnly;
1162 config.servers.push_back(server);
1163 config.type = PeerConnectionInterface::kRelay;
1164 config.disable_ipv6 = true;
1165 config.tcp_candidate_policy =
1166 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001167 config.candidate_network_policy =
1168 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001169 config.ice_candidate_pool_size = 1;
1170 CreatePeerConnection(config, nullptr);
1171
1172 const cricket::FakePortAllocatorSession* session =
1173 static_cast<const cricket::FakePortAllocatorSession*>(
1174 port_allocator_->GetPooledSession());
1175 ASSERT_NE(nullptr, session);
1176 EXPECT_EQ(1UL, session->stun_servers().size());
1177 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1178 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001179 EXPECT_LT(0U,
1180 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001181}
1182
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001183// Test that the PeerConnection initializes the port allocator passed into it,
1184// and on the correct thread.
1185TEST_F(PeerConnectionInterfaceTest,
1186 CreatePeerConnectionInitializesPortAllocator) {
1187 rtc::Thread network_thread;
1188 network_thread.Start();
1189 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1190 webrtc::CreatePeerConnectionFactory(
1191 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1192 nullptr, nullptr, nullptr));
1193 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1194 new cricket::FakePortAllocator(&network_thread, nullptr));
1195 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1196 PeerConnectionInterface::RTCConfiguration config;
1197 rtc::scoped_refptr<PeerConnectionInterface> pc(
1198 pc_factory->CreatePeerConnection(
1199 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1200 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1201 // so all we have to do here is check that it's initialized.
1202 EXPECT_TRUE(raw_port_allocator->initialized());
1203}
1204
deadbeef46c73892016-11-16 19:42:04 -08001205// Check that GetConfiguration returns the configuration the PeerConnection was
1206// constructed with, before SetConfiguration is called.
1207TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1208 PeerConnectionInterface::RTCConfiguration config;
1209 config.type = PeerConnectionInterface::kRelay;
1210 CreatePeerConnection(config, nullptr);
1211
1212 PeerConnectionInterface::RTCConfiguration returned_config =
1213 pc_->GetConfiguration();
1214 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1215}
1216
1217// Check that GetConfiguration returns the last configuration passed into
1218// SetConfiguration.
1219TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1220 CreatePeerConnection();
1221
1222 PeerConnectionInterface::RTCConfiguration config;
1223 config.type = PeerConnectionInterface::kRelay;
1224 EXPECT_TRUE(pc_->SetConfiguration(config));
1225
1226 PeerConnectionInterface::RTCConfiguration returned_config =
1227 pc_->GetConfiguration();
1228 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1229}
1230
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1232 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001233 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 AddVoiceStream(kStreamLabel2);
1235 ASSERT_EQ(2u, pc_->local_streams()->count());
1236
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001237 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001238 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001240 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1241 pc_factory_->CreateAudioTrack(kStreamLabel3,
1242 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001244 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001245 EXPECT_EQ(3u, pc_->local_streams()->count());
1246
1247 // Remove the third stream.
1248 pc_->RemoveStream(pc_->local_streams()->at(2));
1249 EXPECT_EQ(2u, pc_->local_streams()->count());
1250
1251 // Remove the second stream.
1252 pc_->RemoveStream(pc_->local_streams()->at(1));
1253 EXPECT_EQ(1u, pc_->local_streams()->count());
1254
1255 // Remove the first stream.
1256 pc_->RemoveStream(pc_->local_streams()->at(0));
1257 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258}
1259
deadbeefab9b2d12015-10-14 11:33:11 -07001260// Test that the created offer includes streams we added.
1261TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1262 CreatePeerConnection();
1263 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001264 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001265 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001266
1267 const cricket::ContentInfo* audio_content =
1268 cricket::GetFirstAudioContent(offer->description());
1269 const cricket::AudioContentDescription* audio_desc =
1270 static_cast<const cricket::AudioContentDescription*>(
1271 audio_content->description);
1272 EXPECT_TRUE(
1273 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1274
1275 const cricket::ContentInfo* video_content =
1276 cricket::GetFirstVideoContent(offer->description());
1277 const cricket::VideoContentDescription* video_desc =
1278 static_cast<const cricket::VideoContentDescription*>(
1279 video_content->description);
1280 EXPECT_TRUE(
1281 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1282
1283 // Add another stream and ensure the offer includes both the old and new
1284 // streams.
1285 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001286 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001287
1288 audio_content = cricket::GetFirstAudioContent(offer->description());
1289 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1290 audio_content->description);
1291 EXPECT_TRUE(
1292 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1293 EXPECT_TRUE(
1294 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1295
1296 video_content = cricket::GetFirstVideoContent(offer->description());
1297 video_desc = static_cast<const cricket::VideoContentDescription*>(
1298 video_content->description);
1299 EXPECT_TRUE(
1300 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1301 EXPECT_TRUE(
1302 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1303}
1304
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1306 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001307 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 ASSERT_EQ(1u, pc_->local_streams()->count());
1309 pc_->RemoveStream(pc_->local_streams()->at(0));
1310 EXPECT_EQ(0u, pc_->local_streams()->count());
1311}
1312
deadbeefe1f9d832016-01-14 15:35:42 -08001313// Test for AddTrack and RemoveTrack methods.
1314// Tests that the created offer includes tracks we added,
1315// and that the RtpSenders are created correctly.
1316// Also tests that RemoveTrack removes the tracks from subsequent offers.
1317TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1318 CreatePeerConnection();
1319 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001320 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001321 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1322 std::vector<MediaStreamInterface*> stream_list;
1323 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001324 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001325 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001326 rtc::scoped_refptr<VideoTrackInterface> video_track(
1327 pc_factory_->CreateVideoTrack(
1328 "video_track",
1329 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001330 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1331 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001332 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1333 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001334 EXPECT_EQ("audio_track", audio_sender->id());
1335 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001336 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1337 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001338 EXPECT_EQ("video_track", video_sender->id());
1339 EXPECT_EQ(video_track, video_sender->track());
1340
1341 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001342 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001343 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001344
1345 const cricket::ContentInfo* audio_content =
1346 cricket::GetFirstAudioContent(offer->description());
1347 const cricket::AudioContentDescription* audio_desc =
1348 static_cast<const cricket::AudioContentDescription*>(
1349 audio_content->description);
1350 EXPECT_TRUE(
1351 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1352
1353 const cricket::ContentInfo* video_content =
1354 cricket::GetFirstVideoContent(offer->description());
1355 const cricket::VideoContentDescription* video_desc =
1356 static_cast<const cricket::VideoContentDescription*>(
1357 video_content->description);
1358 EXPECT_TRUE(
1359 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1360
1361 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1362
1363 // Now try removing the tracks.
1364 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1365 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1366
1367 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001368 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001369
1370 audio_content = cricket::GetFirstAudioContent(offer->description());
1371 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1372 audio_content->description);
1373 EXPECT_FALSE(
1374 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1375
1376 video_content = cricket::GetFirstVideoContent(offer->description());
1377 video_desc = static_cast<const cricket::VideoContentDescription*>(
1378 video_content->description);
1379 EXPECT_FALSE(
1380 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1381
1382 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1383
1384 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1385 // should return false.
1386 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1387 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1388}
1389
1390// Test creating senders without a stream specified,
1391// expecting a random stream ID to be generated.
1392TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1393 CreatePeerConnection();
1394 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001395 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001396 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001397 rtc::scoped_refptr<VideoTrackInterface> video_track(
1398 pc_factory_->CreateVideoTrack(
1399 "video_track",
1400 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001401 auto audio_sender =
1402 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1403 auto video_sender =
1404 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1405 EXPECT_EQ("audio_track", audio_sender->id());
1406 EXPECT_EQ(audio_track, audio_sender->track());
1407 EXPECT_EQ("video_track", video_sender->id());
1408 EXPECT_EQ(video_track, video_sender->track());
1409 // If the ID is truly a random GUID, it should be infinitely unlikely they
1410 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001411 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001412}
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1415 InitiateCall();
1416 WaitAndVerifyOnAddStream(kStreamLabel1);
1417 VerifyRemoteRtpHeaderExtensions();
1418}
1419
1420TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1421 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001422 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 CreateOfferAsLocalDescription();
1424 std::string offer;
1425 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1426 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1427 WaitAndVerifyOnAddStream(kStreamLabel1);
1428}
1429
1430TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1431 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001432 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433
1434 CreateOfferAsRemoteDescription();
1435 CreateAnswerAsLocalDescription();
1436
1437 WaitAndVerifyOnAddStream(kStreamLabel1);
1438}
1439
1440TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1441 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001442 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443
1444 CreateOfferAsRemoteDescription();
1445 CreatePrAnswerAsLocalDescription();
1446 CreateAnswerAsLocalDescription();
1447
1448 WaitAndVerifyOnAddStream(kStreamLabel1);
1449}
1450
1451TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1452 InitiateCall();
1453 ASSERT_EQ(1u, pc_->remote_streams()->count());
1454 pc_->RemoveStream(pc_->local_streams()->at(0));
1455 CreateOfferReceiveAnswer();
1456 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001457 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 CreateOfferReceiveAnswer();
1459}
1460
1461// Tests that after negotiating an audio only call, the respondent can perform a
1462// renegotiation that removes the audio stream.
1463TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1464 CreatePeerConnection();
1465 AddVoiceStream(kStreamLabel1);
1466 CreateOfferAsRemoteDescription();
1467 CreateAnswerAsLocalDescription();
1468
1469 ASSERT_EQ(1u, pc_->remote_streams()->count());
1470 pc_->RemoveStream(pc_->local_streams()->at(0));
1471 CreateOfferReceiveAnswer();
1472 EXPECT_EQ(0u, pc_->remote_streams()->count());
1473}
1474
1475// Test that candidates are generated and that we can parse our own candidates.
1476TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1477 CreatePeerConnection();
1478
1479 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1480 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001481 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001482 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001483 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001484 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485
1486 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001487 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001488 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001489 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490
1491 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1492 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1493
1494 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1495}
1496
deadbeefab9b2d12015-10-14 11:33:11 -07001497// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498// not unique.
1499TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1500 CreatePeerConnection();
1501 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001502 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001503 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001504 EXPECT_TRUE(offer);
1505 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506
1507 // Create a local stream with audio&video tracks having same label.
1508 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1509
1510 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001511 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512
1513 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001514 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001515 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516}
1517
1518// Test that we will get different SSRCs for each tracks in the offer and answer
1519// we created.
1520TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1521 CreatePeerConnection();
1522 // Create a local stream with audio&video tracks having different labels.
1523 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1524
1525 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001526 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001527 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 int audio_ssrc = 0;
1529 int video_ssrc = 0;
1530 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1531 &audio_ssrc));
1532 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1533 &video_ssrc));
1534 EXPECT_NE(audio_ssrc, video_ssrc);
1535
1536 // Test CreateAnswer
1537 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001538 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001539 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 audio_ssrc = 0;
1541 video_ssrc = 0;
1542 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1543 &audio_ssrc));
1544 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1545 &video_ssrc));
1546 EXPECT_NE(audio_ssrc, video_ssrc);
1547}
1548
deadbeefeb459812015-12-15 19:24:43 -08001549// Test that it's possible to call AddTrack on a MediaStream after adding
1550// the stream to a PeerConnection.
1551// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1552TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1553 CreatePeerConnection();
1554 // Create audio stream and add to PeerConnection.
1555 AddVoiceStream(kStreamLabel1);
1556 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1557
1558 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001559 rtc::scoped_refptr<VideoTrackInterface> video_track(
1560 pc_factory_->CreateVideoTrack(
1561 "video_label",
1562 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001563 stream->AddTrack(video_track.get());
1564
kwibergd1fe2812016-04-27 06:47:29 -07001565 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001566 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001567
1568 const cricket::MediaContentDescription* video_desc =
1569 cricket::GetFirstVideoContentDescription(offer->description());
1570 EXPECT_TRUE(video_desc != nullptr);
1571}
1572
1573// Test that it's possible to call RemoveTrack on a MediaStream after adding
1574// the stream to a PeerConnection.
1575// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1576TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1577 CreatePeerConnection();
1578 // Create audio/video stream and add to PeerConnection.
1579 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1580 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1581
1582 // Remove the video track.
1583 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1584
kwibergd1fe2812016-04-27 06:47:29 -07001585 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001586 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001587
1588 const cricket::MediaContentDescription* video_desc =
1589 cricket::GetFirstVideoContentDescription(offer->description());
1590 EXPECT_TRUE(video_desc == nullptr);
1591}
1592
deadbeefbd7d8f72015-12-18 16:58:44 -08001593// Test creating a sender with a stream ID, and ensure the ID is populated
1594// in the offer.
1595TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1596 CreatePeerConnection();
1597 pc_->CreateSender("video", kStreamLabel1);
1598
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001600 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001601
1602 const cricket::MediaContentDescription* video_desc =
1603 cricket::GetFirstVideoContentDescription(offer->description());
1604 ASSERT_TRUE(video_desc != nullptr);
1605 ASSERT_EQ(1u, video_desc->streams().size());
1606 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1607}
1608
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609// Test that we can specify a certain track that we want statistics about.
1610TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1611 InitiateCall();
1612 ASSERT_LT(0u, pc_->remote_streams()->count());
1613 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001614 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1616 EXPECT_TRUE(DoGetStats(remote_audio));
1617
1618 // Remove the stream. Since we are sending to our selves the local
1619 // and the remote stream is the same.
1620 pc_->RemoveStream(pc_->local_streams()->at(0));
1621 // Do a re-negotiation.
1622 CreateOfferReceiveAnswer();
1623
1624 ASSERT_EQ(0u, pc_->remote_streams()->count());
1625
1626 // Test that we still can get statistics for the old track. Even if it is not
1627 // sent any longer.
1628 EXPECT_TRUE(DoGetStats(remote_audio));
1629}
1630
1631// Test that we can get stats on a video track.
1632TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1633 InitiateCall();
1634 ASSERT_LT(0u, pc_->remote_streams()->count());
1635 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001636 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1638 EXPECT_TRUE(DoGetStats(remote_video));
1639}
1640
1641// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001642TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001644 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 pc_factory_->CreateAudioTrack("unknown track", NULL));
1646 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1647}
1648
1649// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 FakeConstraints constraints;
1652 constraints.SetAllowRtpDataChannels();
1653 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001654 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001656 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 pc_->CreateDataChannel("test2", NULL);
1658 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001659 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001661 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 new MockDataChannelObserver(data2));
1663
1664 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1665 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1666 std::string data_to_send1 = "testing testing";
1667 std::string data_to_send2 = "testing something else";
1668 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1669
1670 CreateOfferReceiveAnswer();
1671 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1672 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1673
1674 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1675 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1676 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1677 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1678
1679 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1680 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1681
1682 data1->Close();
1683 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1684 CreateOfferReceiveAnswer();
1685 EXPECT_FALSE(observer1->IsOpen());
1686 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1687 EXPECT_TRUE(observer2->IsOpen());
1688
1689 data_to_send2 = "testing something else again";
1690 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1691
1692 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1693}
1694
1695// This test verifies that sendnig binary data over RTP data channels should
1696// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 FakeConstraints constraints;
1699 constraints.SetAllowRtpDataChannels();
1700 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001701 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001703 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 pc_->CreateDataChannel("test2", NULL);
1705 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001706 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001708 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 new MockDataChannelObserver(data2));
1710
1711 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1712 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1713
1714 CreateOfferReceiveAnswer();
1715 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1716 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1717
1718 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1719 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1720
jbaucheec21bd2016-03-20 06:15:43 -07001721 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1723}
1724
1725// This test setup a RTP data channels in loop back and test that a channel is
1726// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 FakeConstraints constraints;
1729 constraints.SetAllowRtpDataChannels();
1730 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001731 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001733 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 new MockDataChannelObserver(data1));
1735
1736 CreateOfferReceiveAnswerWithoutSsrc();
1737
1738 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1739
1740 data1->Close();
1741 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1742 CreateOfferReceiveAnswerWithoutSsrc();
1743 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1744 EXPECT_FALSE(observer1->IsOpen());
1745}
1746
1747// This test that if a data channel is added in an answer a receive only channel
1748// channel is created.
1749TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1750 FakeConstraints constraints;
1751 constraints.SetAllowRtpDataChannels();
1752 CreatePeerConnection(&constraints);
1753
1754 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001755 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 pc_->CreateDataChannel(offer_label, NULL);
1757
1758 CreateOfferAsLocalDescription();
1759
1760 // Replace the data channel label in the offer and apply it as an answer.
1761 std::string receive_label = "answer_channel";
1762 std::string sdp;
1763 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001764 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 receive_label.c_str(), receive_label.length(),
1766 &sdp);
1767 CreateAnswerAsRemoteDescription(sdp);
1768
1769 // Verify that a new incoming data channel has been created and that
1770 // it is open but can't we written to.
1771 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1772 DataChannelInterface* received_channel = observer_.last_datachannel_;
1773 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1774 EXPECT_EQ(receive_label, received_channel->label());
1775 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1776
1777 // Verify that the channel we initially offered has been rejected.
1778 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1779
1780 // Do another offer / answer exchange and verify that the data channel is
1781 // opened.
1782 CreateOfferReceiveAnswer();
1783 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1784 kTimeout);
1785}
1786
1787// This test that no data channel is returned if a reliable channel is
1788// requested.
1789// TODO(perkj): Remove this test once reliable channels are implemented.
1790TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1791 FakeConstraints constraints;
1792 constraints.SetAllowRtpDataChannels();
1793 CreatePeerConnection(&constraints);
1794
1795 std::string label = "test";
1796 webrtc::DataChannelInit config;
1797 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001798 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 pc_->CreateDataChannel(label, &config);
1800 EXPECT_TRUE(channel == NULL);
1801}
1802
deadbeefab9b2d12015-10-14 11:33:11 -07001803// Verifies that duplicated label is not allowed for RTP data channel.
1804TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1805 FakeConstraints constraints;
1806 constraints.SetAllowRtpDataChannels();
1807 CreatePeerConnection(&constraints);
1808
1809 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001810 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001811 pc_->CreateDataChannel(label, nullptr);
1812 EXPECT_NE(channel, nullptr);
1813
zhihuang9763d562016-08-05 11:14:50 -07001814 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001815 pc_->CreateDataChannel(label, nullptr);
1816 EXPECT_EQ(dup_channel, nullptr);
1817}
1818
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819// This tests that a SCTP data channel is returned using different
1820// DataChannelInit configurations.
1821TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1822 FakeConstraints constraints;
1823 constraints.SetAllowDtlsSctpDataChannels();
1824 CreatePeerConnection(&constraints);
1825
1826 webrtc::DataChannelInit config;
1827
zhihuang9763d562016-08-05 11:14:50 -07001828 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 pc_->CreateDataChannel("1", &config);
1830 EXPECT_TRUE(channel != NULL);
1831 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001832 EXPECT_TRUE(observer_.renegotiation_needed_);
1833 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834
1835 config.ordered = false;
1836 channel = pc_->CreateDataChannel("2", &config);
1837 EXPECT_TRUE(channel != NULL);
1838 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001839 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840
1841 config.ordered = true;
1842 config.maxRetransmits = 0;
1843 channel = pc_->CreateDataChannel("3", &config);
1844 EXPECT_TRUE(channel != NULL);
1845 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001846 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847
1848 config.maxRetransmits = -1;
1849 config.maxRetransmitTime = 0;
1850 channel = pc_->CreateDataChannel("4", &config);
1851 EXPECT_TRUE(channel != NULL);
1852 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001853 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854}
1855
1856// This tests that no data channel is returned if both maxRetransmits and
1857// maxRetransmitTime are set for SCTP data channels.
1858TEST_F(PeerConnectionInterfaceTest,
1859 CreateSctpDataChannelShouldFailForInvalidConfig) {
1860 FakeConstraints constraints;
1861 constraints.SetAllowDtlsSctpDataChannels();
1862 CreatePeerConnection(&constraints);
1863
1864 std::string label = "test";
1865 webrtc::DataChannelInit config;
1866 config.maxRetransmits = 0;
1867 config.maxRetransmitTime = 0;
1868
zhihuang9763d562016-08-05 11:14:50 -07001869 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 pc_->CreateDataChannel(label, &config);
1871 EXPECT_TRUE(channel == NULL);
1872}
1873
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874// The test verifies that creating a SCTP data channel with an id already in use
1875// or out of range should fail.
1876TEST_F(PeerConnectionInterfaceTest,
1877 CreateSctpDataChannelWithInvalidIdShouldFail) {
1878 FakeConstraints constraints;
1879 constraints.SetAllowDtlsSctpDataChannels();
1880 CreatePeerConnection(&constraints);
1881
1882 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001885 config.id = 1;
1886 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 EXPECT_TRUE(channel != NULL);
1888 EXPECT_EQ(1, channel->id());
1889
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 channel = pc_->CreateDataChannel("x", &config);
1891 EXPECT_TRUE(channel == NULL);
1892
1893 config.id = cricket::kMaxSctpSid;
1894 channel = pc_->CreateDataChannel("max", &config);
1895 EXPECT_TRUE(channel != NULL);
1896 EXPECT_EQ(config.id, channel->id());
1897
1898 config.id = cricket::kMaxSctpSid + 1;
1899 channel = pc_->CreateDataChannel("x", &config);
1900 EXPECT_TRUE(channel == NULL);
1901}
1902
deadbeefab9b2d12015-10-14 11:33:11 -07001903// Verifies that duplicated label is allowed for SCTP data channel.
1904TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1905 FakeConstraints constraints;
1906 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1907 true);
1908 CreatePeerConnection(&constraints);
1909
1910 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001911 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001912 pc_->CreateDataChannel(label, nullptr);
1913 EXPECT_NE(channel, nullptr);
1914
zhihuang9763d562016-08-05 11:14:50 -07001915 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001916 pc_->CreateDataChannel(label, nullptr);
1917 EXPECT_NE(dup_channel, nullptr);
1918}
1919
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001920// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1921// DataChannel.
1922TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1923 FakeConstraints constraints;
1924 constraints.SetAllowRtpDataChannels();
1925 CreatePeerConnection(&constraints);
1926
zhihuang9763d562016-08-05 11:14:50 -07001927 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001928 pc_->CreateDataChannel("test1", NULL);
1929 EXPECT_TRUE(observer_.renegotiation_needed_);
1930 observer_.renegotiation_needed_ = false;
1931
zhihuang9763d562016-08-05 11:14:50 -07001932 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001933 pc_->CreateDataChannel("test2", NULL);
1934 EXPECT_TRUE(observer_.renegotiation_needed_);
1935}
1936
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 FakeConstraints constraints;
1940 constraints.SetAllowRtpDataChannels();
1941 CreatePeerConnection(&constraints);
1942
zhihuang9763d562016-08-05 11:14:50 -07001943 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001945 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 pc_->CreateDataChannel("test2", NULL);
1947 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001948 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001950 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 new MockDataChannelObserver(data2));
1952
1953 CreateOfferReceiveAnswer();
1954 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1955 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1956
1957 ReleasePeerConnection();
1958 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1959 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1960}
1961
1962// This test that data channels can be rejected in an answer.
1963TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1964 FakeConstraints constraints;
1965 constraints.SetAllowRtpDataChannels();
1966 CreatePeerConnection(&constraints);
1967
zhihuang9763d562016-08-05 11:14:50 -07001968 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 pc_->CreateDataChannel("offer_channel", NULL));
1970
1971 CreateOfferAsLocalDescription();
1972
1973 // Create an answer where the m-line for data channels are rejected.
1974 std::string sdp;
1975 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1976 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1977 SessionDescriptionInterface::kAnswer);
1978 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1979 cricket::ContentInfo* data_info =
1980 answer->description()->GetContentByName("data");
1981 data_info->rejected = true;
1982
1983 DoSetRemoteDescription(answer);
1984 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1985}
1986
1987// Test that we can create a session description from an SDP string from
1988// FireFox, use it as a remote session description, generate an answer and use
1989// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001990TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001991 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 FakeConstraints constraints;
1993 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1994 true);
1995 CreatePeerConnection(&constraints);
1996 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1997 SessionDescriptionInterface* desc =
1998 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001999 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2001 CreateAnswerAsLocalDescription();
2002 ASSERT_TRUE(pc_->local_description() != NULL);
2003 ASSERT_TRUE(pc_->remote_description() != NULL);
2004
2005 const cricket::ContentInfo* content =
2006 cricket::GetFirstAudioContent(pc_->local_description()->description());
2007 ASSERT_TRUE(content != NULL);
2008 EXPECT_FALSE(content->rejected);
2009
2010 content =
2011 cricket::GetFirstVideoContent(pc_->local_description()->description());
2012 ASSERT_TRUE(content != NULL);
2013 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002014#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 content =
2016 cricket::GetFirstDataContent(pc_->local_description()->description());
2017 ASSERT_TRUE(content != NULL);
2018 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002019#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020}
2021
2022// Test that we can create an audio only offer and receive an answer with a
2023// limited set of audio codecs and receive an updated offer with more audio
2024// codecs, where the added codecs are not supported.
2025TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2026 CreatePeerConnection();
2027 AddVoiceStream("audio_label");
2028 CreateOfferAsLocalDescription();
2029
2030 SessionDescriptionInterface* answer =
2031 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002032 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2034
2035 SessionDescriptionInterface* updated_offer =
2036 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002037 webrtc::kAudioSdpWithUnsupportedCodecs,
2038 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2040 CreateAnswerAsLocalDescription();
2041}
2042
deadbeefc80741f2015-10-22 13:14:45 -07002043// Test that if we're receiving (but not sending) a track, subsequent offers
2044// will have m-lines with a=recvonly.
2045TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2046 FakeConstraints constraints;
2047 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2048 true);
2049 CreatePeerConnection(&constraints);
2050 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2051 CreateAnswerAsLocalDescription();
2052
2053 // At this point we should be receiving stream 1, but not sending anything.
2054 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002055 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002056 DoCreateOffer(&offer, nullptr);
2057
2058 const cricket::ContentInfo* video_content =
2059 cricket::GetFirstVideoContent(offer->description());
2060 const cricket::VideoContentDescription* video_desc =
2061 static_cast<const cricket::VideoContentDescription*>(
2062 video_content->description);
2063 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2064
2065 const cricket::ContentInfo* audio_content =
2066 cricket::GetFirstAudioContent(offer->description());
2067 const cricket::AudioContentDescription* audio_desc =
2068 static_cast<const cricket::AudioContentDescription*>(
2069 audio_content->description);
2070 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2071}
2072
2073// Test that if we're receiving (but not sending) a track, and the
2074// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2075// false, the generated m-lines will be a=inactive.
2076TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2077 FakeConstraints constraints;
2078 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2079 true);
2080 CreatePeerConnection(&constraints);
2081 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2082 CreateAnswerAsLocalDescription();
2083
2084 // At this point we should be receiving stream 1, but not sending anything.
2085 // A new offer would be recvonly, but we'll set the "no receive" constraints
2086 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002087 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002088 FakeConstraints offer_constraints;
2089 offer_constraints.AddMandatory(
2090 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2091 offer_constraints.AddMandatory(
2092 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2093 DoCreateOffer(&offer, &offer_constraints);
2094
2095 const cricket::ContentInfo* video_content =
2096 cricket::GetFirstVideoContent(offer->description());
2097 const cricket::VideoContentDescription* video_desc =
2098 static_cast<const cricket::VideoContentDescription*>(
2099 video_content->description);
2100 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2101
2102 const cricket::ContentInfo* audio_content =
2103 cricket::GetFirstAudioContent(offer->description());
2104 const cricket::AudioContentDescription* audio_desc =
2105 static_cast<const cricket::AudioContentDescription*>(
2106 audio_content->description);
2107 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2108}
2109
deadbeef653b8e02015-11-11 12:55:10 -08002110// Test that we can use SetConfiguration to change the ICE servers of the
2111// PortAllocator.
2112TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2113 CreatePeerConnection();
2114
2115 PeerConnectionInterface::RTCConfiguration config;
2116 PeerConnectionInterface::IceServer server;
2117 server.uri = "stun:test_hostname";
2118 config.servers.push_back(server);
2119 EXPECT_TRUE(pc_->SetConfiguration(config));
2120
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002121 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2122 EXPECT_EQ("test_hostname",
2123 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002124}
2125
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002126TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2127 CreatePeerConnection();
2128 PeerConnectionInterface::RTCConfiguration config;
2129 config.type = PeerConnectionInterface::kRelay;
2130 EXPECT_TRUE(pc_->SetConfiguration(config));
2131 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2132}
2133
2134// Test that when SetConfiguration changes both the pool size and other
2135// attributes, the pooled session is created with the updated attributes.
2136TEST_F(PeerConnectionInterfaceTest,
2137 SetConfigurationCreatesPooledSessionCorrectly) {
2138 CreatePeerConnection();
2139 PeerConnectionInterface::RTCConfiguration config;
2140 config.ice_candidate_pool_size = 1;
2141 PeerConnectionInterface::IceServer server;
2142 server.uri = kStunAddressOnly;
2143 config.servers.push_back(server);
2144 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002145 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002146
2147 const cricket::FakePortAllocatorSession* session =
2148 static_cast<const cricket::FakePortAllocatorSession*>(
2149 port_allocator_->GetPooledSession());
2150 ASSERT_NE(nullptr, session);
2151 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002152}
2153
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154// Test that PeerConnection::Close changes the states to closed and all remote
2155// tracks change state to ended.
2156TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2157 // Initialize a PeerConnection and negotiate local and remote session
2158 // description.
2159 InitiateCall();
2160 ASSERT_EQ(1u, pc_->local_streams()->count());
2161 ASSERT_EQ(1u, pc_->remote_streams()->count());
2162
2163 pc_->Close();
2164
2165 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2166 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2167 pc_->ice_connection_state());
2168 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2169 pc_->ice_gathering_state());
2170
2171 EXPECT_EQ(1u, pc_->local_streams()->count());
2172 EXPECT_EQ(1u, pc_->remote_streams()->count());
2173
zhihuang9763d562016-08-05 11:14:50 -07002174 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2175 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002176 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002177 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002178 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2179 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2180 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181}
2182
2183// Test that PeerConnection methods fails gracefully after
2184// PeerConnection::Close has been called.
2185TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2186 CreatePeerConnection();
2187 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2188 CreateOfferAsRemoteDescription();
2189 CreateAnswerAsLocalDescription();
2190
2191 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002192 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 pc_->local_streams()->at(0);
2194
2195 pc_->Close();
2196
2197 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002198 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199
2200 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002201 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002203 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204
2205 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2206
2207 EXPECT_TRUE(pc_->local_description() != NULL);
2208 EXPECT_TRUE(pc_->remote_description() != NULL);
2209
kwibergd1fe2812016-04-27 06:47:29 -07002210 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002211 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002212 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002213 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214
2215 std::string sdp;
2216 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2217 SessionDescriptionInterface* remote_offer =
2218 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2219 sdp, NULL);
2220 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2221
2222 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2223 SessionDescriptionInterface* local_offer =
2224 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2225 sdp, NULL);
2226 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2227}
2228
2229// Test that GetStats can still be called after PeerConnection::Close.
2230TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2231 InitiateCall();
2232 pc_->Close();
2233 DoGetStats(NULL);
2234}
deadbeefab9b2d12015-10-14 11:33:11 -07002235
2236// NOTE: The series of tests below come from what used to be
2237// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2238// setting a remote or local description has the expected effects.
2239
2240// This test verifies that the remote MediaStreams corresponding to a received
2241// SDP string is created. In this test the two separate MediaStreams are
2242// signaled.
2243TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2244 FakeConstraints constraints;
2245 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2246 true);
2247 CreatePeerConnection(&constraints);
2248 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2249
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002250 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002251 EXPECT_TRUE(
2252 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2253 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2254 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2255
2256 // Create a session description based on another SDP with another
2257 // MediaStream.
2258 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2259
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002260 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002261 EXPECT_TRUE(
2262 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2263}
2264
2265// This test verifies that when remote tracks are added/removed from SDP, the
2266// created remote streams are updated appropriately.
2267TEST_F(PeerConnectionInterfaceTest,
2268 AddRemoveTrackFromExistingRemoteMediaStream) {
2269 FakeConstraints constraints;
2270 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2271 true);
2272 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002273 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002274 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002275 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2276 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2277 reference_collection_));
2278
2279 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002280 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002281 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002282 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2283 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2284 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002285 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002286 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2287 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002288 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002289 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2290 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002291
2292 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002293 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002294 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002295 MockTrackObserver audio_track_observer(audio_track2);
2296 MockTrackObserver video_track_observer(video_track2);
2297
2298 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2299 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002300 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2301 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2302 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002303 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002304 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002305 audio_track2->state(), kTimeout);
2306 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2307 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002308}
2309
2310// This tests that remote tracks are ended if a local session description is set
2311// that rejects the media content type.
2312TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2313 FakeConstraints constraints;
2314 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2315 true);
2316 CreatePeerConnection(&constraints);
2317 // First create and set a remote offer, then reject its video content in our
2318 // answer.
2319 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2320 ASSERT_EQ(1u, observer_.remote_streams()->count());
2321 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2322 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2323 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2324
2325 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2326 remote_stream->GetVideoTracks()[0];
2327 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2328 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2329 remote_stream->GetAudioTracks()[0];
2330 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2331
kwibergd1fe2812016-04-27 06:47:29 -07002332 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002333 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002334 cricket::ContentInfo* video_info =
2335 local_answer->description()->GetContentByName("video");
2336 video_info->rejected = true;
2337 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2338 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2339 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2340
2341 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002342 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002343 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002344 video_info = local_offer->description()->GetContentByName("video");
2345 ASSERT_TRUE(video_info != nullptr);
2346 video_info->rejected = true;
2347 cricket::ContentInfo* audio_info =
2348 local_offer->description()->GetContentByName("audio");
2349 ASSERT_TRUE(audio_info != nullptr);
2350 audio_info->rejected = true;
2351 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002352 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002353 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002354 remote_audio->state(), kTimeout);
2355 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2356 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002357}
2358
2359// This tests that we won't crash if the remote track has been removed outside
2360// of PeerConnection and then PeerConnection tries to reject the track.
2361TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2362 FakeConstraints constraints;
2363 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2364 true);
2365 CreatePeerConnection(&constraints);
2366 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2367 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2368 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2369 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2370
kwibergd1fe2812016-04-27 06:47:29 -07002371 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002372 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2373 kSdpStringWithStream1, nullptr));
2374 cricket::ContentInfo* video_info =
2375 local_answer->description()->GetContentByName("video");
2376 video_info->rejected = true;
2377 cricket::ContentInfo* audio_info =
2378 local_answer->description()->GetContentByName("audio");
2379 audio_info->rejected = true;
2380 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2381
2382 // No crash is a pass.
2383}
2384
deadbeef5e97fb52015-10-15 12:49:08 -07002385// This tests that if a recvonly remote description is set, no remote streams
2386// will be created, even if the description contains SSRCs/MSIDs.
2387// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2388TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2389 FakeConstraints constraints;
2390 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2391 true);
2392 CreatePeerConnection(&constraints);
2393
2394 std::string recvonly_offer = kSdpStringWithStream1;
2395 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2396 strlen(kRecvonly), &recvonly_offer);
2397 CreateAndSetRemoteOffer(recvonly_offer);
2398
2399 EXPECT_EQ(0u, observer_.remote_streams()->count());
2400}
2401
deadbeefab9b2d12015-10-14 11:33:11 -07002402// This tests that a default MediaStream is created if a remote session
2403// description doesn't contain any streams and no MSID support.
2404// It also tests that the default stream is updated if a video m-line is added
2405// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002406TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002407 FakeConstraints constraints;
2408 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2409 true);
2410 CreatePeerConnection(&constraints);
2411 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2412
2413 ASSERT_EQ(1u, observer_.remote_streams()->count());
2414 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2415
2416 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2417 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2418 EXPECT_EQ("default", remote_stream->label());
2419
2420 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2421 ASSERT_EQ(1u, observer_.remote_streams()->count());
2422 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2423 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002424 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2425 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002426 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2427 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002428 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2429 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002430}
2431
2432// This tests that a default MediaStream is created if a remote session
2433// description doesn't contain any streams and media direction is send only.
2434TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002435 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002436 FakeConstraints constraints;
2437 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2438 true);
2439 CreatePeerConnection(&constraints);
2440 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2441
2442 ASSERT_EQ(1u, observer_.remote_streams()->count());
2443 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2444
2445 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2446 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2447 EXPECT_EQ("default", remote_stream->label());
2448}
2449
2450// This tests that it won't crash when PeerConnection tries to remove
2451// a remote track that as already been removed from the MediaStream.
2452TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2453 FakeConstraints constraints;
2454 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2455 true);
2456 CreatePeerConnection(&constraints);
2457 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2458 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2459 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2460 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2461
2462 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2463
2464 // No crash is a pass.
2465}
2466
2467// This tests that a default MediaStream is created if the remote session
2468// description doesn't contain any streams and don't contain an indication if
2469// MSID is supported.
2470TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002471 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002472 FakeConstraints constraints;
2473 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2474 true);
2475 CreatePeerConnection(&constraints);
2476 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2477
2478 ASSERT_EQ(1u, observer_.remote_streams()->count());
2479 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2480 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2481 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2482}
2483
2484// This tests that a default MediaStream is not created if the remote session
2485// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002486TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002487 FakeConstraints constraints;
2488 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2489 true);
2490 CreatePeerConnection(&constraints);
2491 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2492 EXPECT_EQ(0u, observer_.remote_streams()->count());
2493}
2494
deadbeefbda7e0b2015-12-08 17:13:40 -08002495// This tests that when setting a new description, the old default tracks are
2496// not destroyed and recreated.
2497// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002498TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002499 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002500 FakeConstraints constraints;
2501 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2502 true);
2503 CreatePeerConnection(&constraints);
2504 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2505
2506 ASSERT_EQ(1u, observer_.remote_streams()->count());
2507 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2508 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2509
2510 // Set the track to "disabled", then set a new description and ensure the
2511 // track is still disabled, which ensures it hasn't been recreated.
2512 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2513 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2514 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2515 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2516}
2517
deadbeefab9b2d12015-10-14 11:33:11 -07002518// This tests that a default MediaStream is not created if a remote session
2519// description is updated to not have any MediaStreams.
2520TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2521 FakeConstraints constraints;
2522 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2523 true);
2524 CreatePeerConnection(&constraints);
2525 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002526 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002527 EXPECT_TRUE(
2528 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2529
2530 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2531 EXPECT_EQ(0u, observer_.remote_streams()->count());
2532}
2533
2534// This tests that an RtpSender is created when the local description is set
2535// after adding a local stream.
2536// TODO(deadbeef): This test and the one below it need to be updated when
2537// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002538TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002539 FakeConstraints constraints;
2540 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2541 true);
2542 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002543
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002544 // Create an offer with 1 stream with 2 tracks of each type.
2545 rtc::scoped_refptr<StreamCollection> stream_collection =
2546 CreateStreamCollection(1, 2);
2547 pc_->AddStream(stream_collection->at(0));
2548 std::unique_ptr<SessionDescriptionInterface> offer;
2549 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2550 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002551
deadbeefab9b2d12015-10-14 11:33:11 -07002552 auto senders = pc_->GetSenders();
2553 EXPECT_EQ(4u, senders.size());
2554 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2555 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2556 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2557 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2558
2559 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002560 pc_->RemoveStream(stream_collection->at(0));
2561 stream_collection = CreateStreamCollection(1, 1);
2562 pc_->AddStream(stream_collection->at(0));
2563 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2564 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2565
deadbeefab9b2d12015-10-14 11:33:11 -07002566 senders = pc_->GetSenders();
2567 EXPECT_EQ(2u, senders.size());
2568 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2569 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2570 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2571 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2572}
2573
2574// This tests that an RtpSender is created when the local description is set
2575// before adding a local stream.
2576TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002577 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002578 FakeConstraints constraints;
2579 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2580 true);
2581 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002582
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002583 rtc::scoped_refptr<StreamCollection> stream_collection =
2584 CreateStreamCollection(1, 2);
2585 // Add a stream to create the offer, but remove it afterwards.
2586 pc_->AddStream(stream_collection->at(0));
2587 std::unique_ptr<SessionDescriptionInterface> offer;
2588 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2589 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002590
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002591 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002592 auto senders = pc_->GetSenders();
2593 EXPECT_EQ(0u, senders.size());
2594
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002595 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002596 senders = pc_->GetSenders();
2597 EXPECT_EQ(4u, senders.size());
2598 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2599 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2600 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2601 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2602}
2603
2604// This tests that the expected behavior occurs if the SSRC on a local track is
2605// changed when SetLocalDescription is called.
2606TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002607 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002608 FakeConstraints constraints;
2609 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2610 true);
2611 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002612
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002613 rtc::scoped_refptr<StreamCollection> stream_collection =
2614 CreateStreamCollection(2, 1);
2615 pc_->AddStream(stream_collection->at(0));
2616 std::unique_ptr<SessionDescriptionInterface> offer;
2617 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2618 // Grab a copy of the offer before it gets passed into the PC.
2619 std::unique_ptr<JsepSessionDescription> modified_offer(
2620 new JsepSessionDescription(JsepSessionDescription::kOffer));
2621 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2622 offer->session_version());
2623 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002624
deadbeefab9b2d12015-10-14 11:33:11 -07002625 auto senders = pc_->GetSenders();
2626 EXPECT_EQ(2u, senders.size());
2627 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2628 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2629
2630 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002631 cricket::MediaContentDescription* desc =
2632 cricket::GetFirstAudioContentDescription(modified_offer->description());
2633 ASSERT_TRUE(desc != NULL);
2634 for (StreamParams& stream : desc->mutable_streams()) {
2635 for (unsigned int& ssrc : stream.ssrcs) {
2636 ++ssrc;
2637 }
2638 }
deadbeefab9b2d12015-10-14 11:33:11 -07002639
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002640 desc =
2641 cricket::GetFirstVideoContentDescription(modified_offer->description());
2642 ASSERT_TRUE(desc != NULL);
2643 for (StreamParams& stream : desc->mutable_streams()) {
2644 for (unsigned int& ssrc : stream.ssrcs) {
2645 ++ssrc;
2646 }
2647 }
2648
2649 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002650 senders = pc_->GetSenders();
2651 EXPECT_EQ(2u, senders.size());
2652 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2653 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2654 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2655 // changed.
2656}
2657
2658// This tests that the expected behavior occurs if a new session description is
2659// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002660TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002661 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002662 FakeConstraints constraints;
2663 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2664 true);
2665 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002666
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002667 rtc::scoped_refptr<StreamCollection> stream_collection =
2668 CreateStreamCollection(2, 1);
2669 pc_->AddStream(stream_collection->at(0));
2670 std::unique_ptr<SessionDescriptionInterface> offer;
2671 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2672 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002673
deadbeefab9b2d12015-10-14 11:33:11 -07002674 auto senders = pc_->GetSenders();
2675 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002676 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2677 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002678
2679 // Add a new MediaStream but with the same tracks as in the first stream.
2680 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2681 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002682 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2683 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002684 pc_->AddStream(stream_1);
2685
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002686 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2687 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002688
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002689 auto new_senders = pc_->GetSenders();
2690 // Should be the same senders as before, but with updated stream id.
2691 // Note that this behavior is subject to change in the future.
2692 // We may decide the PC should ignore existing tracks in AddStream.
2693 EXPECT_EQ(senders, new_senders);
2694 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2695 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002696}
2697
zhihuang81c3a032016-11-17 12:06:24 -08002698// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2699TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2700 FakeConstraints constraints;
2701 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2702 true);
2703 CreatePeerConnection(&constraints);
2704 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2705 EXPECT_EQ(observer_.num_added_tracks_, 1);
2706 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2707
2708 // Create and set the updated remote SDP.
2709 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2710 EXPECT_EQ(observer_.num_added_tracks_, 2);
2711 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2712}
2713
nisse51542be2016-02-12 02:27:06 -08002714class PeerConnectionMediaConfigTest : public testing::Test {
2715 protected:
2716 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002717 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002718 pcf_->Initialize();
2719 }
2720 const cricket::MediaConfig& TestCreatePeerConnection(
2721 const PeerConnectionInterface::RTCConfiguration& config,
2722 const MediaConstraintsInterface *constraints) {
2723 pcf_->create_media_controller_called_ = false;
2724
zhihuang9763d562016-08-05 11:14:50 -07002725 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2726 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002727 EXPECT_TRUE(pc.get());
2728 EXPECT_TRUE(pcf_->create_media_controller_called_);
2729 return pcf_->create_media_controller_config_;
2730 }
2731
zhihuang9763d562016-08-05 11:14:50 -07002732 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002733 MockPeerConnectionObserver observer_;
2734};
2735
2736// This test verifies the default behaviour with no constraints and a
2737// default RTCConfiguration.
2738TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2739 PeerConnectionInterface::RTCConfiguration config;
2740 FakeConstraints constraints;
2741
2742 const cricket::MediaConfig& media_config =
2743 TestCreatePeerConnection(config, &constraints);
2744
2745 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002746 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2747 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2748 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002749}
2750
2751// This test verifies the DSCP constraint is recognized and passed to
2752// the CreateMediaController call.
2753TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2754 PeerConnectionInterface::RTCConfiguration config;
2755 FakeConstraints constraints;
2756
2757 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2758 const cricket::MediaConfig& media_config =
2759 TestCreatePeerConnection(config, &constraints);
2760
2761 EXPECT_TRUE(media_config.enable_dscp);
2762}
2763
2764// This test verifies the cpu overuse detection constraint is
2765// recognized and passed to the CreateMediaController call.
2766TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2767 PeerConnectionInterface::RTCConfiguration config;
2768 FakeConstraints constraints;
2769
2770 constraints.AddOptional(
2771 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2772 const cricket::MediaConfig media_config =
2773 TestCreatePeerConnection(config, &constraints);
2774
nisse0db023a2016-03-01 04:29:59 -08002775 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002776}
2777
2778// This test verifies that the disable_prerenderer_smoothing flag is
2779// propagated from RTCConfiguration to the CreateMediaController call.
2780TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2781 PeerConnectionInterface::RTCConfiguration config;
2782 FakeConstraints constraints;
2783
Niels Möller71bdda02016-03-31 12:59:59 +02002784 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002785 const cricket::MediaConfig& media_config =
2786 TestCreatePeerConnection(config, &constraints);
2787
nisse0db023a2016-03-01 04:29:59 -08002788 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2789}
2790
2791// This test verifies the suspend below min bitrate constraint is
2792// recognized and passed to the CreateMediaController call.
2793TEST_F(PeerConnectionMediaConfigTest,
2794 TestSuspendBelowMinBitrateConstraintTrue) {
2795 PeerConnectionInterface::RTCConfiguration config;
2796 FakeConstraints constraints;
2797
2798 constraints.AddOptional(
2799 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2800 true);
2801 const cricket::MediaConfig media_config =
2802 TestCreatePeerConnection(config, &constraints);
2803
2804 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002805}
2806
deadbeefab9b2d12015-10-14 11:33:11 -07002807// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002808// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2809// "verify options are converted correctly", should be "pass options into
2810// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002811
2812TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2813 RTCOfferAnswerOptions rtc_options;
2814 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2815
2816 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002817 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002818
2819 rtc_options.offer_to_receive_audio =
2820 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002821 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002822}
2823
2824TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2825 RTCOfferAnswerOptions rtc_options;
2826 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2827
2828 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002829 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002830
2831 rtc_options.offer_to_receive_video =
2832 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002833 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002834}
2835
2836// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002837// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002838TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2839 RTCOfferAnswerOptions rtc_options;
2840 rtc_options.offer_to_receive_audio = 1;
2841 rtc_options.offer_to_receive_video = 1;
2842
2843 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002844 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002845 EXPECT_TRUE(options.has_audio());
2846 EXPECT_TRUE(options.has_video());
2847 EXPECT_TRUE(options.bundle_enabled);
2848}
2849
2850// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002851// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002852TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2853 RTCOfferAnswerOptions rtc_options;
2854 rtc_options.offer_to_receive_audio = 1;
2855
2856 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002857 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002858 EXPECT_TRUE(options.has_audio());
2859 EXPECT_FALSE(options.has_video());
2860 EXPECT_TRUE(options.bundle_enabled);
2861}
2862
2863// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002864// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002865TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2866 RTCOfferAnswerOptions rtc_options;
2867
2868 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002869 options.transport_options["audio"] = cricket::TransportOptions();
2870 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002871 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002872 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002873 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002874 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002875 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002876 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2877 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002878}
2879
2880// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002881// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002882TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2883 RTCOfferAnswerOptions rtc_options;
2884 rtc_options.offer_to_receive_audio = 0;
2885 rtc_options.offer_to_receive_video = 1;
2886
2887 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002888 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002889 EXPECT_FALSE(options.has_audio());
2890 EXPECT_TRUE(options.has_video());
2891 EXPECT_TRUE(options.bundle_enabled);
2892}
2893
2894// Test that a correct MediaSessionOptions is created for an offer if
2895// UseRtpMux is set to false.
2896TEST(CreateSessionOptionsTest,
2897 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2898 RTCOfferAnswerOptions rtc_options;
2899 rtc_options.offer_to_receive_audio = 1;
2900 rtc_options.offer_to_receive_video = 1;
2901 rtc_options.use_rtp_mux = false;
2902
2903 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002904 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002905 EXPECT_TRUE(options.has_audio());
2906 EXPECT_TRUE(options.has_video());
2907 EXPECT_FALSE(options.bundle_enabled);
2908}
2909
2910// Test that a correct MediaSessionOptions is created to restart ice if
2911// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002912// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002913TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2914 RTCOfferAnswerOptions rtc_options;
2915 rtc_options.ice_restart = true;
2916
2917 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002918 options.transport_options["audio"] = cricket::TransportOptions();
2919 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002920 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002921 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2922 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002923
2924 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002925 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002926 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2927 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002928}
2929
2930// Test that the MediaConstraints in an answer don't affect if audio and video
2931// is offered in an offer but that if kOfferToReceiveAudio or
2932// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2933// included in subsequent answers.
2934TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2935 FakeConstraints answer_c;
2936 answer_c.SetMandatoryReceiveAudio(true);
2937 answer_c.SetMandatoryReceiveVideo(true);
2938
2939 cricket::MediaSessionOptions answer_options;
2940 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2941 EXPECT_TRUE(answer_options.has_audio());
2942 EXPECT_TRUE(answer_options.has_video());
2943
deadbeefc80741f2015-10-22 13:14:45 -07002944 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002945
2946 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002947 EXPECT_TRUE(
2948 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002949 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002950 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002951
deadbeefc80741f2015-10-22 13:14:45 -07002952 RTCOfferAnswerOptions updated_rtc_offer_options;
2953 updated_rtc_offer_options.offer_to_receive_audio = 1;
2954 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002955
2956 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002957 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002958 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002959 EXPECT_TRUE(updated_offer_options.has_audio());
2960 EXPECT_TRUE(updated_offer_options.has_video());
2961
2962 // Since an offer has been created with both audio and video, subsequent
2963 // offers and answers should contain both audio and video.
2964 // Answers will only contain the media types that exist in the offer
2965 // regardless of the value of |updated_answer_options.has_audio| and
2966 // |updated_answer_options.has_video|.
2967 FakeConstraints updated_answer_c;
2968 answer_c.SetMandatoryReceiveAudio(false);
2969 answer_c.SetMandatoryReceiveVideo(false);
2970
2971 cricket::MediaSessionOptions updated_answer_options;
2972 EXPECT_TRUE(
2973 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2974 EXPECT_TRUE(updated_answer_options.has_audio());
2975 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002976}