blob: cfba3e67ec73cff287e139985b8a95ddb2250241 [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>
deadbeef3edec7c2016-12-10 11:44:26 -080012#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080014#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010025#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020026#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070027#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/test/mockpeerconnectionobservers.h"
29#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010030#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/sslstreamadapter.h"
35#include "webrtc/base/stringutils.h"
36#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080037#include "webrtc/media/base/fakevideocapturer.h"
38#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070039#include "webrtc/p2p/base/fakeportallocator.h"
zhihuang29ff8442016-07-27 11:07:25 -070040#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010041#include "webrtc/pc/mediasession.h"
kwibergac9f8762016-09-30 22:29:43 -070042#include "webrtc/test/gmock.h"
43
44#ifdef WEBRTC_ANDROID
45#include "webrtc/api/test/androidtestinitializer.h"
46#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48static const char kStreamLabel1[] = "local_stream_1";
49static const char kStreamLabel2[] = "local_stream_2";
50static const char kStreamLabel3[] = "local_stream_3";
51static const int kDefaultStunPort = 3478;
52static const char kStunAddressOnly[] = "stun:address";
53static const char kStunInvalidPort[] = "stun:address:-1";
54static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
55static const char kStunAddressPortAndMore2[] = "stun:address:port more";
56static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
57static const char kTurnUsername[] = "user";
58static const char kTurnPassword[] = "password";
59static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020060static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
deadbeefab9b2d12015-10-14 11:33:11 -070062static const char kStreams[][8] = {"stream1", "stream2"};
63static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
64static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
65
deadbeef5e97fb52015-10-15 12:49:08 -070066static const char kRecvonly[] = "recvonly";
67static const char kSendrecv[] = "sendrecv";
68
deadbeefab9b2d12015-10-14 11:33:11 -070069// Reference SDP with a MediaStream with label "stream1" and audio track with
70// id "audio_1" and a video track with id "video_1;
71static const char kSdpStringWithStream1[] =
72 "v=0\r\n"
73 "o=- 0 0 IN IP4 127.0.0.1\r\n"
74 "s=-\r\n"
75 "t=0 0\r\n"
76 "a=ice-ufrag:e5785931\r\n"
77 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
78 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
79 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
80 "m=audio 1 RTP/AVPF 103\r\n"
81 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070082 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080083 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=rtpmap:103 ISAC/16000\r\n"
85 "a=ssrc:1 cname:stream1\r\n"
86 "a=ssrc:1 mslabel:stream1\r\n"
87 "a=ssrc:1 label:audiotrack0\r\n"
88 "m=video 1 RTP/AVPF 120\r\n"
89 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070090 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080091 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070092 "a=rtpmap:120 VP8/90000\r\n"
93 "a=ssrc:2 cname:stream1\r\n"
94 "a=ssrc:2 mslabel:stream1\r\n"
95 "a=ssrc:2 label:videotrack0\r\n";
96
zhihuang81c3a032016-11-17 12:06:24 -080097// Reference SDP with a MediaStream with label "stream1" and audio track with
98// id "audio_1";
99static const char kSdpStringWithStream1AudioTrackOnly[] =
100 "v=0\r\n"
101 "o=- 0 0 IN IP4 127.0.0.1\r\n"
102 "s=-\r\n"
103 "t=0 0\r\n"
104 "a=ice-ufrag:e5785931\r\n"
105 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
106 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
107 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
108 "m=audio 1 RTP/AVPF 103\r\n"
109 "a=mid:audio\r\n"
110 "a=sendrecv\r\n"
111 "a=rtpmap:103 ISAC/16000\r\n"
112 "a=ssrc:1 cname:stream1\r\n"
113 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800114 "a=ssrc:1 label:audiotrack0\r\n"
115 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800116
deadbeefab9b2d12015-10-14 11:33:11 -0700117// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
118// MediaStreams have one audio track and one video track.
119// This uses MSID.
120static const char kSdpStringWithStream1And2[] =
121 "v=0\r\n"
122 "o=- 0 0 IN IP4 127.0.0.1\r\n"
123 "s=-\r\n"
124 "t=0 0\r\n"
125 "a=ice-ufrag:e5785931\r\n"
126 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
127 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
128 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
129 "a=msid-semantic: WMS stream1 stream2\r\n"
130 "m=audio 1 RTP/AVPF 103\r\n"
131 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700132 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800133 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=rtpmap:103 ISAC/16000\r\n"
135 "a=ssrc:1 cname:stream1\r\n"
136 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
137 "a=ssrc:3 cname:stream2\r\n"
138 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
139 "m=video 1 RTP/AVPF 120\r\n"
140 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700141 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800142 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700143 "a=rtpmap:120 VP8/0\r\n"
144 "a=ssrc:2 cname:stream1\r\n"
145 "a=ssrc:2 msid:stream1 videotrack0\r\n"
146 "a=ssrc:4 cname:stream2\r\n"
147 "a=ssrc:4 msid:stream2 videotrack1\r\n";
148
149// Reference SDP without MediaStreams. Msid is not supported.
150static const char kSdpStringWithoutStreams[] =
151 "v=0\r\n"
152 "o=- 0 0 IN IP4 127.0.0.1\r\n"
153 "s=-\r\n"
154 "t=0 0\r\n"
155 "a=ice-ufrag:e5785931\r\n"
156 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
157 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
158 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
159 "m=audio 1 RTP/AVPF 103\r\n"
160 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700161 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800162 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700163 "a=rtpmap:103 ISAC/16000\r\n"
164 "m=video 1 RTP/AVPF 120\r\n"
165 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700166 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800167 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700168 "a=rtpmap:120 VP8/90000\r\n";
169
170// Reference SDP without MediaStreams. Msid is supported.
171static const char kSdpStringWithMsidWithoutStreams[] =
172 "v=0\r\n"
173 "o=- 0 0 IN IP4 127.0.0.1\r\n"
174 "s=-\r\n"
175 "t=0 0\r\n"
176 "a=ice-ufrag:e5785931\r\n"
177 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
178 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
179 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
180 "a=msid-semantic: WMS\r\n"
181 "m=audio 1 RTP/AVPF 103\r\n"
182 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700183 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800184 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700185 "a=rtpmap:103 ISAC/16000\r\n"
186 "m=video 1 RTP/AVPF 120\r\n"
187 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800189 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700190 "a=rtpmap:120 VP8/90000\r\n";
191
192// Reference SDP without MediaStreams and audio only.
193static const char kSdpStringWithoutStreamsAudioOnly[] =
194 "v=0\r\n"
195 "o=- 0 0 IN IP4 127.0.0.1\r\n"
196 "s=-\r\n"
197 "t=0 0\r\n"
198 "a=ice-ufrag:e5785931\r\n"
199 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
200 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
201 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
202 "m=audio 1 RTP/AVPF 103\r\n"
203 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700204 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800205 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=rtpmap:103 ISAC/16000\r\n";
207
208// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
209static const char kSdpStringSendOnlyWithoutStreams[] =
210 "v=0\r\n"
211 "o=- 0 0 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
214 "a=ice-ufrag:e5785931\r\n"
215 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
216 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
217 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
218 "m=audio 1 RTP/AVPF 103\r\n"
219 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700220 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800222 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=rtpmap:103 ISAC/16000\r\n"
224 "m=video 1 RTP/AVPF 120\r\n"
225 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700226 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700227 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800228 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700229 "a=rtpmap:120 VP8/90000\r\n";
230
231static const char kSdpStringInit[] =
232 "v=0\r\n"
233 "o=- 0 0 IN IP4 127.0.0.1\r\n"
234 "s=-\r\n"
235 "t=0 0\r\n"
236 "a=ice-ufrag:e5785931\r\n"
237 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
238 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
239 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
240 "a=msid-semantic: WMS\r\n";
241
242static const char kSdpStringAudio[] =
243 "m=audio 1 RTP/AVPF 103\r\n"
244 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700245 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800246 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700247 "a=rtpmap:103 ISAC/16000\r\n";
248
249static const char kSdpStringVideo[] =
250 "m=video 1 RTP/AVPF 120\r\n"
251 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700252 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800253 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700254 "a=rtpmap:120 VP8/90000\r\n";
255
256static const char kSdpStringMs1Audio0[] =
257 "a=ssrc:1 cname:stream1\r\n"
258 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
259
260static const char kSdpStringMs1Video0[] =
261 "a=ssrc:2 cname:stream1\r\n"
262 "a=ssrc:2 msid:stream1 videotrack0\r\n";
263
264static const char kSdpStringMs1Audio1[] =
265 "a=ssrc:3 cname:stream1\r\n"
266 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
267
268static const char kSdpStringMs1Video1[] =
269 "a=ssrc:4 cname:stream1\r\n"
270 "a=ssrc:4 msid:stream1 videotrack1\r\n";
271
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272#define MAYBE_SKIP_TEST(feature) \
273 if (!(feature())) { \
274 LOG(LS_INFO) << "Feature disabled... skipping"; \
275 return; \
276 }
277
perkjd61bf802016-03-24 03:16:19 -0700278using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700279using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700281using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282using webrtc::AudioTrackInterface;
283using webrtc::DataBuffer;
284using webrtc::DataChannelInterface;
285using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700287using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700288using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700289using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290using webrtc::MediaStreamInterface;
291using webrtc::MediaStreamTrackInterface;
292using webrtc::MockCreateSessionDescriptionObserver;
293using webrtc::MockDataChannelObserver;
294using webrtc::MockSetSessionDescriptionObserver;
295using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700296using webrtc::NotifierInterface;
297using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298using webrtc::PeerConnectionInterface;
299using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700300using webrtc::RtpReceiverInterface;
301using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302using webrtc::SdpParseError;
303using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700304using webrtc::StreamCollection;
305using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100306using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700307using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308using webrtc::VideoTrackInterface;
309
deadbeefab9b2d12015-10-14 11:33:11 -0700310typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
311
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312namespace {
313
314// Gets the first ssrc of given content type from the ContentInfo.
315bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
316 if (!content_info || !ssrc) {
317 return false;
318 }
319 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000320 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 content_info->description);
322 if (!media_desc || media_desc->streams().empty()) {
323 return false;
324 }
325 *ssrc = media_desc->streams().begin()->first_ssrc();
326 return true;
327}
328
329void SetSsrcToZero(std::string* sdp) {
330 const char kSdpSsrcAtribute[] = "a=ssrc:";
331 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
332 size_t ssrc_pos = 0;
333 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
334 std::string::npos) {
335 size_t end_ssrc = sdp->find(" ", ssrc_pos);
336 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
337 ssrc_pos = end_ssrc;
338 }
339}
340
deadbeefab9b2d12015-10-14 11:33:11 -0700341// Check if |streams| contains the specified track.
342bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
343 const std::string& stream_label,
344 const std::string& track_id) {
345 for (const cricket::StreamParams& params : streams) {
346 if (params.sync_label == stream_label && params.id == track_id) {
347 return true;
348 }
349 }
350 return false;
351}
352
353// Check if |senders| contains the specified sender, by id.
354bool ContainsSender(
355 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
356 const std::string& id) {
357 for (const auto& sender : senders) {
358 if (sender->id() == id) {
359 return true;
360 }
361 }
362 return false;
363}
364
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700365// Check if |senders| contains the specified sender, by id and stream id.
366bool ContainsSender(
367 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
368 const std::string& id,
369 const std::string& stream_id) {
370 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700371 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700372 return true;
373 }
374 }
375 return false;
376}
377
deadbeefab9b2d12015-10-14 11:33:11 -0700378// Create a collection of streams.
379// CreateStreamCollection(1) creates a collection that
380// correspond to kSdpStringWithStream1.
381// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
382rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700383 int number_of_streams,
384 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700385 rtc::scoped_refptr<StreamCollection> local_collection(
386 StreamCollection::Create());
387
388 for (int i = 0; i < number_of_streams; ++i) {
389 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
390 webrtc::MediaStream::Create(kStreams[i]));
391
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700392 for (int j = 0; j < tracks_per_stream; ++j) {
393 // Add a local audio track.
394 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
395 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
396 nullptr));
397 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700398
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700399 // Add a local video track.
400 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
401 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
402 webrtc::FakeVideoTrackSource::Create()));
403 stream->AddTrack(video_track);
404 }
deadbeefab9b2d12015-10-14 11:33:11 -0700405
406 local_collection->AddStream(stream);
407 }
408 return local_collection;
409}
410
411// Check equality of StreamCollections.
412bool CompareStreamCollections(StreamCollectionInterface* s1,
413 StreamCollectionInterface* s2) {
414 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
415 return false;
416 }
417
418 for (size_t i = 0; i != s1->count(); ++i) {
419 if (s1->at(i)->label() != s2->at(i)->label()) {
420 return false;
421 }
422 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
423 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
424 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
425 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
426
427 if (audio_tracks1.size() != audio_tracks2.size()) {
428 return false;
429 }
430 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
431 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
432 return false;
433 }
434 }
435 if (video_tracks1.size() != video_tracks2.size()) {
436 return false;
437 }
438 for (size_t j = 0; j != video_tracks1.size(); ++j) {
439 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
440 return false;
441 }
442 }
443 }
444 return true;
445}
446
perkjd61bf802016-03-24 03:16:19 -0700447// Helper class to test Observer.
448class MockTrackObserver : public ObserverInterface {
449 public:
450 explicit MockTrackObserver(NotifierInterface* notifier)
451 : notifier_(notifier) {
452 notifier_->RegisterObserver(this);
453 }
454
455 ~MockTrackObserver() { Unregister(); }
456
457 void Unregister() {
458 if (notifier_) {
459 notifier_->UnregisterObserver(this);
460 notifier_ = nullptr;
461 }
462 }
463
464 MOCK_METHOD0(OnChanged, void());
465
466 private:
467 NotifierInterface* notifier_;
468};
469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470class MockPeerConnectionObserver : public PeerConnectionObserver {
471 public:
kjellander71a1b612016-11-07 01:18:08 -0800472 // We need these using declarations because there are two versions of each of
473 // the below methods and we only override one of them.
474 // TODO(deadbeef): Remove once there's only one version of the methods.
475 using PeerConnectionObserver::OnAddStream;
476 using PeerConnectionObserver::OnRemoveStream;
477 using PeerConnectionObserver::OnDataChannel;
478
deadbeefab9b2d12015-10-14 11:33:11 -0700479 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200480 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 }
482 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
483 pc_ = pc;
484 if (pc) {
485 state_ = pc_->signaling_state();
486 }
487 }
nisseef8b61e2016-04-29 06:09:15 -0700488 void OnSignalingChange(
489 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 EXPECT_EQ(pc_->signaling_state(), new_state);
491 state_ = new_state;
492 }
493 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
494 virtual void OnStateChange(StateType state_changed) {
495 if (pc_.get() == NULL)
496 return;
497 switch (state_changed) {
498 case kSignalingState:
499 // OnSignalingChange and OnStateChange(kSignalingState) should always
500 // be called approximately simultaneously. To ease testing, we require
501 // that they always be called in that order. This check verifies
502 // that OnSignalingChange has just been called.
503 EXPECT_EQ(pc_->signaling_state(), state_);
504 break;
505 case kIceState:
506 ADD_FAILURE();
507 break;
508 default:
509 ADD_FAILURE();
510 break;
511 }
512 }
deadbeefab9b2d12015-10-14 11:33:11 -0700513
514 MediaStreamInterface* RemoteStream(const std::string& label) {
515 return remote_streams_->find(label);
516 }
517 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700518 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700520 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700522 void OnRemoveStream(
523 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700525 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
perkjdfb769d2016-02-09 03:09:43 -0800527 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700528 void OnDataChannel(
529 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 last_datachannel_ = data_channel;
531 }
532
perkjdfb769d2016-02-09 03:09:43 -0800533 void OnIceConnectionChange(
534 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800536 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
perkjdfb769d2016-02-09 03:09:43 -0800538 void OnIceGatheringChange(
539 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800541 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800542 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 }
perkjdfb769d2016-02-09 03:09:43 -0800544 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
546 pc_->ice_gathering_state());
547
548 std::string sdp;
549 EXPECT_TRUE(candidate->ToString(&sdp));
550 EXPECT_LT(0u, sdp.size());
551 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
552 candidate->sdp_mline_index(), sdp, NULL));
553 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800554 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700555 }
556
557 void OnIceCandidatesRemoved(
558 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800559 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700560 }
561
562 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800563 callback_triggered_ = true;
564 }
565
zhihuangc63b8942016-12-02 15:41:10 -0800566 void OnAddTrack(
567 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
568 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
569 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800570 EXPECT_TRUE(receiver != nullptr);
571 num_added_tracks_++;
572 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574
575 // Returns the label of the last added stream.
576 // Empty string if no stream have been added.
577 std::string GetLastAddedStreamLabel() {
578 if (last_added_stream_.get())
579 return last_added_stream_->label();
580 return "";
581 }
582 std::string GetLastRemovedStreamLabel() {
583 if (last_removed_stream_.get())
584 return last_removed_stream_->label();
585 return "";
586 }
587
zhihuang9763d562016-08-05 11:14:50 -0700588 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700590 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700591 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700592 rtc::scoped_refptr<StreamCollection> remote_streams_;
593 bool renegotiation_needed_ = false;
594 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800595 bool callback_triggered_ = false;
596 int num_added_tracks_ = 0;
597 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598
599 private:
zhihuang9763d562016-08-05 11:14:50 -0700600 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
601 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602};
603
604} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700605
zhihuang29ff8442016-07-27 11:07:25 -0700606// The PeerConnectionMediaConfig tests below verify that configuration
607// and constraints are propagated into the MediaConfig passed to
608// CreateMediaController. These settings are intended for MediaChannel
609// constructors, but that is not exercised by these unittest.
610class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
611 public:
612 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700613 const cricket::MediaConfig& config,
614 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700615 create_media_controller_called_ = true;
616 create_media_controller_config_ = config;
617
618 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700619 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700620 EXPECT_TRUE(mc != nullptr);
621 return mc;
622 }
623
624 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700625 cricket::PortAllocator* port_allocator,
626 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700627 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700628 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
629 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700630 return transport_controller;
631 }
632
633 cricket::TransportController* transport_controller;
634 // Mutable, so they can be modified in the above const-declared method.
635 mutable bool create_media_controller_called_ = false;
636 mutable cricket::MediaConfig create_media_controller_config_;
637};
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639class PeerConnectionInterfaceTest : public testing::Test {
640 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800641 PeerConnectionInterfaceTest() {
642#ifdef WEBRTC_ANDROID
643 webrtc::InitializeAndroidObjects();
644#endif
645 }
646
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 virtual void SetUp() {
648 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700649 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
650 nullptr, nullptr, nullptr);
651 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700652 pc_factory_for_test_ =
653 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
654 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 }
656
657 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700658 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660
661 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700662 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
663 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 }
665
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700666 void CreatePeerConnectionWithIceTransportsType(
667 PeerConnectionInterface::IceTransportsType type) {
668 PeerConnectionInterface::RTCConfiguration config;
669 config.type = type;
670 return CreatePeerConnection(config, nullptr);
671 }
672
673 void CreatePeerConnectionWithIceServer(const std::string& uri,
674 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700677 server.uri = uri;
678 server.password = password;
679 config.servers.push_back(server);
680 CreatePeerConnection(config, nullptr);
681 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700683 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
684 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700685 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800686 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
687 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000688
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000689 // DTLS does not work in a loopback call, so is disabled for most of the
690 // tests in this file. We only create a FakeIdentityService if the test
691 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000692 FakeConstraints default_constraints;
693 if (!constraints) {
694 constraints = &default_constraints;
695
696 default_constraints.AddMandatory(
697 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
698 }
699
Henrik Boströmd79599d2016-06-01 13:58:50 +0200700 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000701 bool dtls;
702 if (FindConstraint(constraints,
703 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
704 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200705 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200706 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000707 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200708 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200710 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 ASSERT_TRUE(pc_.get() != NULL);
712 observer_.SetPeerConnectionInterface(pc_.get());
713 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
714 }
715
deadbeef0a6c4ca2015-10-06 11:38:28 -0700716 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800717 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700718 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700719 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800720 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700721
zhihuang9763d562016-08-05 11:14:50 -0700722 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700723 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
724 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800725 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700726 }
727
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700729 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800730 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
731 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
732 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800734 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
deadbeef0a6c4ca2015-10-06 11:38:28 -0700736 CreatePeerConnectionExpectFail(kStunInvalidPort);
737 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
738 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700740 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800741 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
742 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800748 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750
751 void ReleasePeerConnection() {
752 pc_ = NULL;
753 observer_.SetPeerConnectionInterface(NULL);
754 }
755
deadbeefab9b2d12015-10-14 11:33:11 -0700756 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700758 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700762 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 pc_factory_->CreateVideoTrack(label + "v0", video_source));
764 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000765 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
767 observer_.renegotiation_needed_ = false;
768 }
769
770 void AddVoiceStream(const std::string& label) {
771 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700774 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 pc_factory_->CreateAudioTrack(label + "a0", NULL));
776 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000777 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
779 observer_.renegotiation_needed_ = false;
780 }
781
782 void AddAudioVideoStream(const std::string& stream_label,
783 const std::string& audio_track_label,
784 const std::string& video_track_label) {
785 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700786 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700788 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 pc_factory_->CreateAudioTrack(
790 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
791 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700792 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700793 pc_factory_->CreateVideoTrack(
794 video_track_label,
795 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000797 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
799 observer_.renegotiation_needed_ = false;
800 }
801
kwibergd1fe2812016-04-27 06:47:29 -0700802 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700803 bool offer,
804 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000805 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
806 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 MockCreateSessionDescriptionObserver>());
808 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700809 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700811 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 }
813 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700814 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 return observer->result();
816 }
817
kwibergd1fe2812016-04-27 06:47:29 -0700818 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700819 MediaConstraintsInterface* constraints) {
820 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
kwibergd1fe2812016-04-27 06:47:29 -0700823 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700824 MediaConstraintsInterface* constraints) {
825 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
828 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
830 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 MockSetSessionDescriptionObserver>());
832 if (local) {
833 pc_->SetLocalDescription(observer, desc);
834 } else {
835 pc_->SetRemoteDescription(observer, desc);
836 }
zhihuang29ff8442016-07-27 11:07:25 -0700837 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
838 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 return observer->result();
841 }
842
843 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
844 return DoSetSessionDescription(desc, true);
845 }
846
847 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
848 return DoSetSessionDescription(desc, false);
849 }
850
851 // Calls PeerConnection::GetStats and check the return value.
852 // It does not verify the values in the StatReports since a RTCP packet might
853 // be required.
854 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000855 rtc::scoped_refptr<MockStatsObserver> observer(
856 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000857 if (!pc_->GetStats(
858 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 return false;
860 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
861 return observer->called();
862 }
863
864 void InitiateCall() {
865 CreatePeerConnection();
866 // Create a local stream with audio&video tracks.
867 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
868 CreateOfferReceiveAnswer();
869 }
870
871 // Verify that RTP Header extensions has been negotiated for audio and video.
872 void VerifyRemoteRtpHeaderExtensions() {
873 const cricket::MediaContentDescription* desc =
874 cricket::GetFirstAudioContentDescription(
875 pc_->remote_description()->description());
876 ASSERT_TRUE(desc != NULL);
877 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
878
879 desc = cricket::GetFirstVideoContentDescription(
880 pc_->remote_description()->description());
881 ASSERT_TRUE(desc != NULL);
882 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
883 }
884
885 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700886 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700887 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 std::string sdp;
889 EXPECT_TRUE(offer->ToString(&sdp));
890 SessionDescriptionInterface* remote_offer =
891 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
892 sdp, NULL);
893 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
894 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
895 }
896
deadbeefab9b2d12015-10-14 11:33:11 -0700897 void CreateAndSetRemoteOffer(const std::string& sdp) {
898 SessionDescriptionInterface* remote_offer =
899 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
900 sdp, nullptr);
901 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
902 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
903 }
904
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700906 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700907 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908
909 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
910 // audio codec change, even if the parameter has nothing to do with
911 // receiving. Not all parameters are serialized to SDP.
912 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
913 // the SessionDescription, it is necessary to do that here to in order to
914 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
915 // https://code.google.com/p/webrtc/issues/detail?id=1356
916 std::string sdp;
917 EXPECT_TRUE(answer->ToString(&sdp));
918 SessionDescriptionInterface* new_answer =
919 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
920 sdp, NULL);
921 EXPECT_TRUE(DoSetLocalDescription(new_answer));
922 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
923 }
924
925 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700926 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700927 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928
929 std::string sdp;
930 EXPECT_TRUE(answer->ToString(&sdp));
931 SessionDescriptionInterface* pr_answer =
932 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
933 sdp, NULL);
934 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
935 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
936 }
937
938 void CreateOfferReceiveAnswer() {
939 CreateOfferAsLocalDescription();
940 std::string sdp;
941 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
942 CreateAnswerAsRemoteDescription(sdp);
943 }
944
945 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700946 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700947 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
949 // audio codec change, even if the parameter has nothing to do with
950 // receiving. Not all parameters are serialized to SDP.
951 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
952 // the SessionDescription, it is necessary to do that here to in order to
953 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
954 // https://code.google.com/p/webrtc/issues/detail?id=1356
955 std::string sdp;
956 EXPECT_TRUE(offer->ToString(&sdp));
957 SessionDescriptionInterface* new_offer =
958 webrtc::CreateSessionDescription(
959 SessionDescriptionInterface::kOffer,
960 sdp, NULL);
961
962 EXPECT_TRUE(DoSetLocalDescription(new_offer));
963 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000964 // Wait for the ice_complete message, so that SDP will have candidates.
965 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 }
967
deadbeefab9b2d12015-10-14 11:33:11 -0700968 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
970 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700971 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 EXPECT_TRUE(DoSetRemoteDescription(answer));
973 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
974 }
975
deadbeefab9b2d12015-10-14 11:33:11 -0700976 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 webrtc::JsepSessionDescription* pr_answer =
978 new webrtc::JsepSessionDescription(
979 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700980 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
982 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
983 webrtc::JsepSessionDescription* answer =
984 new webrtc::JsepSessionDescription(
985 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700986 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 EXPECT_TRUE(DoSetRemoteDescription(answer));
988 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
989 }
990
991 // Help function used for waiting until a the last signaled remote stream has
992 // the same label as |stream_label|. In a few of the tests in this file we
993 // answer with the same session description as we offer and thus we can
994 // check if OnAddStream have been called with the same stream as we offer to
995 // send.
996 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
997 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
998 }
999
1000 // Creates an offer and applies it as a local session description.
1001 // Creates an answer with the same SDP an the offer but removes all lines
1002 // that start with a:ssrc"
1003 void CreateOfferReceiveAnswerWithoutSsrc() {
1004 CreateOfferAsLocalDescription();
1005 std::string sdp;
1006 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1007 SetSsrcToZero(&sdp);
1008 CreateAnswerAsRemoteDescription(sdp);
1009 }
1010
deadbeefab9b2d12015-10-14 11:33:11 -07001011 // This function creates a MediaStream with label kStreams[0] and
1012 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1013 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001014 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001015 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001016 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001017 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1018 size_t number_of_video_tracks) {
1019 EXPECT_LE(number_of_audio_tracks, 2u);
1020 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001021
1022 reference_collection_ = StreamCollection::Create();
1023 std::string sdp_ms1 = std::string(kSdpStringInit);
1024
1025 std::string mediastream_label = kStreams[0];
1026
1027 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1028 webrtc::MediaStream::Create(mediastream_label));
1029 reference_collection_->AddStream(stream);
1030
1031 if (number_of_audio_tracks > 0) {
1032 sdp_ms1 += std::string(kSdpStringAudio);
1033 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1034 AddAudioTrack(kAudioTracks[0], stream);
1035 }
1036 if (number_of_audio_tracks > 1) {
1037 sdp_ms1 += kSdpStringMs1Audio1;
1038 AddAudioTrack(kAudioTracks[1], stream);
1039 }
1040
1041 if (number_of_video_tracks > 0) {
1042 sdp_ms1 += std::string(kSdpStringVideo);
1043 sdp_ms1 += std::string(kSdpStringMs1Video0);
1044 AddVideoTrack(kVideoTracks[0], stream);
1045 }
1046 if (number_of_video_tracks > 1) {
1047 sdp_ms1 += kSdpStringMs1Video1;
1048 AddVideoTrack(kVideoTracks[1], stream);
1049 }
1050
kwibergd1fe2812016-04-27 06:47:29 -07001051 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001052 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1053 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001054 }
1055
1056 void AddAudioTrack(const std::string& track_id,
1057 MediaStreamInterface* stream) {
1058 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1059 webrtc::AudioTrack::Create(track_id, nullptr));
1060 ASSERT_TRUE(stream->AddTrack(audio_track));
1061 }
1062
1063 void AddVideoTrack(const std::string& track_id,
1064 MediaStreamInterface* stream) {
1065 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001066 webrtc::VideoTrack::Create(track_id,
1067 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001068 ASSERT_TRUE(stream->AddTrack(video_track));
1069 }
1070
kwibergfd8be342016-05-14 19:44:11 -07001071 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001072 CreatePeerConnection();
1073 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001074 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001075 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1076 return offer;
1077 }
1078
kwibergfd8be342016-05-14 19:44:11 -07001079 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001080 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001081 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001082 CreateOfferWithOneAudioStream();
1083 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001084 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001085 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1086 return answer;
1087 }
1088
1089 const std::string& GetFirstAudioStreamCname(
1090 const SessionDescriptionInterface* desc) {
1091 const cricket::ContentInfo* audio_content =
1092 cricket::GetFirstAudioContent(desc->description());
1093 const cricket::AudioContentDescription* audio_desc =
1094 static_cast<const cricket::AudioContentDescription*>(
1095 audio_content->description);
1096 return audio_desc->streams()[0].cname;
1097 }
1098
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001099 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001100 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1101 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1102 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001104 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105};
1106
zhihuang29ff8442016-07-27 11:07:25 -07001107// Test that no callbacks on the PeerConnectionObserver are called after the
1108// PeerConnection is closed.
1109TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001110 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001111 pc_factory_for_test_->CreatePeerConnection(
1112 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1113 nullptr, &observer_));
1114 observer_.SetPeerConnectionInterface(pc.get());
1115 pc->Close();
1116
1117 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001118 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001119 std::vector<cricket::Candidate> candidates;
1120 pc_factory_for_test_->transport_controller->SignalGatheringState(
1121 cricket::IceGatheringState{});
1122 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1123 "", candidates);
1124 pc_factory_for_test_->transport_controller->SignalConnectionState(
1125 cricket::IceConnectionState{});
1126 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1127 candidates);
1128 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001129 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001130}
1131
zhihuang8f65cdf2016-05-06 18:40:30 -07001132// Generate different CNAMEs when PeerConnections are created.
1133// The CNAMEs are expected to be generated randomly. It is possible
1134// that the test fails, though the possibility is very low.
1135TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001136 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001137 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001138 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001139 CreateOfferWithOneAudioStream();
1140 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1141 GetFirstAudioStreamCname(offer2.get()));
1142}
1143
1144TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001145 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001146 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001147 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001148 CreateAnswerWithOneAudioStream();
1149 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1150 GetFirstAudioStreamCname(answer2.get()));
1151}
1152
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153TEST_F(PeerConnectionInterfaceTest,
1154 CreatePeerConnectionWithDifferentConfigurations) {
1155 CreatePeerConnectionWithDifferentConfigurations();
1156}
1157
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001158TEST_F(PeerConnectionInterfaceTest,
1159 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1160 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1161 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1162 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1163 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1164 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1165 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1166 port_allocator_->candidate_filter());
1167 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1168 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1169}
1170
1171// Test that when a PeerConnection is created with a nonzero candidate pool
1172// size, the pooled PortAllocatorSession is created with all the attributes
1173// in the RTCConfiguration.
1174TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1175 PeerConnectionInterface::RTCConfiguration config;
1176 PeerConnectionInterface::IceServer server;
1177 server.uri = kStunAddressOnly;
1178 config.servers.push_back(server);
1179 config.type = PeerConnectionInterface::kRelay;
1180 config.disable_ipv6 = true;
1181 config.tcp_candidate_policy =
1182 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001183 config.candidate_network_policy =
1184 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001185 config.ice_candidate_pool_size = 1;
1186 CreatePeerConnection(config, nullptr);
1187
1188 const cricket::FakePortAllocatorSession* session =
1189 static_cast<const cricket::FakePortAllocatorSession*>(
1190 port_allocator_->GetPooledSession());
1191 ASSERT_NE(nullptr, session);
1192 EXPECT_EQ(1UL, session->stun_servers().size());
1193 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1194 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001195 EXPECT_LT(0U,
1196 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001197}
1198
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001199// Test that the PeerConnection initializes the port allocator passed into it,
1200// and on the correct thread.
1201TEST_F(PeerConnectionInterfaceTest,
1202 CreatePeerConnectionInitializesPortAllocator) {
1203 rtc::Thread network_thread;
1204 network_thread.Start();
1205 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1206 webrtc::CreatePeerConnectionFactory(
1207 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1208 nullptr, nullptr, nullptr));
1209 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1210 new cricket::FakePortAllocator(&network_thread, nullptr));
1211 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1212 PeerConnectionInterface::RTCConfiguration config;
1213 rtc::scoped_refptr<PeerConnectionInterface> pc(
1214 pc_factory->CreatePeerConnection(
1215 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1216 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1217 // so all we have to do here is check that it's initialized.
1218 EXPECT_TRUE(raw_port_allocator->initialized());
1219}
1220
deadbeef46c73892016-11-16 19:42:04 -08001221// Check that GetConfiguration returns the configuration the PeerConnection was
1222// constructed with, before SetConfiguration is called.
1223TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1224 PeerConnectionInterface::RTCConfiguration config;
1225 config.type = PeerConnectionInterface::kRelay;
1226 CreatePeerConnection(config, nullptr);
1227
1228 PeerConnectionInterface::RTCConfiguration returned_config =
1229 pc_->GetConfiguration();
1230 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1231}
1232
1233// Check that GetConfiguration returns the last configuration passed into
1234// SetConfiguration.
1235TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1236 CreatePeerConnection();
1237
1238 PeerConnectionInterface::RTCConfiguration config;
1239 config.type = PeerConnectionInterface::kRelay;
1240 EXPECT_TRUE(pc_->SetConfiguration(config));
1241
1242 PeerConnectionInterface::RTCConfiguration returned_config =
1243 pc_->GetConfiguration();
1244 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1245}
1246
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1248 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001249 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 AddVoiceStream(kStreamLabel2);
1251 ASSERT_EQ(2u, pc_->local_streams()->count());
1252
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001253 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001254 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001256 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1257 pc_factory_->CreateAudioTrack(kStreamLabel3,
1258 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001260 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001261 EXPECT_EQ(3u, pc_->local_streams()->count());
1262
1263 // Remove the third stream.
1264 pc_->RemoveStream(pc_->local_streams()->at(2));
1265 EXPECT_EQ(2u, pc_->local_streams()->count());
1266
1267 // Remove the second stream.
1268 pc_->RemoveStream(pc_->local_streams()->at(1));
1269 EXPECT_EQ(1u, pc_->local_streams()->count());
1270
1271 // Remove the first stream.
1272 pc_->RemoveStream(pc_->local_streams()->at(0));
1273 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274}
1275
deadbeefab9b2d12015-10-14 11:33:11 -07001276// Test that the created offer includes streams we added.
1277TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1278 CreatePeerConnection();
1279 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001280 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001281 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001282
1283 const cricket::ContentInfo* audio_content =
1284 cricket::GetFirstAudioContent(offer->description());
1285 const cricket::AudioContentDescription* audio_desc =
1286 static_cast<const cricket::AudioContentDescription*>(
1287 audio_content->description);
1288 EXPECT_TRUE(
1289 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1290
1291 const cricket::ContentInfo* video_content =
1292 cricket::GetFirstVideoContent(offer->description());
1293 const cricket::VideoContentDescription* video_desc =
1294 static_cast<const cricket::VideoContentDescription*>(
1295 video_content->description);
1296 EXPECT_TRUE(
1297 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1298
1299 // Add another stream and ensure the offer includes both the old and new
1300 // streams.
1301 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001302 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001303
1304 audio_content = cricket::GetFirstAudioContent(offer->description());
1305 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1306 audio_content->description);
1307 EXPECT_TRUE(
1308 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1309 EXPECT_TRUE(
1310 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1311
1312 video_content = cricket::GetFirstVideoContent(offer->description());
1313 video_desc = static_cast<const cricket::VideoContentDescription*>(
1314 video_content->description);
1315 EXPECT_TRUE(
1316 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1317 EXPECT_TRUE(
1318 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1319}
1320
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1322 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001323 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 ASSERT_EQ(1u, pc_->local_streams()->count());
1325 pc_->RemoveStream(pc_->local_streams()->at(0));
1326 EXPECT_EQ(0u, pc_->local_streams()->count());
1327}
1328
deadbeefe1f9d832016-01-14 15:35:42 -08001329// Test for AddTrack and RemoveTrack methods.
1330// Tests that the created offer includes tracks we added,
1331// and that the RtpSenders are created correctly.
1332// Also tests that RemoveTrack removes the tracks from subsequent offers.
1333TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1334 CreatePeerConnection();
1335 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001336 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001337 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1338 std::vector<MediaStreamInterface*> stream_list;
1339 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001340 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001341 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001342 rtc::scoped_refptr<VideoTrackInterface> video_track(
1343 pc_factory_->CreateVideoTrack(
1344 "video_track",
1345 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001346 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1347 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001348 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1349 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001350 EXPECT_EQ("audio_track", audio_sender->id());
1351 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001352 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1353 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001354 EXPECT_EQ("video_track", video_sender->id());
1355 EXPECT_EQ(video_track, video_sender->track());
1356
1357 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001358 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001359 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001360
1361 const cricket::ContentInfo* audio_content =
1362 cricket::GetFirstAudioContent(offer->description());
1363 const cricket::AudioContentDescription* audio_desc =
1364 static_cast<const cricket::AudioContentDescription*>(
1365 audio_content->description);
1366 EXPECT_TRUE(
1367 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1368
1369 const cricket::ContentInfo* video_content =
1370 cricket::GetFirstVideoContent(offer->description());
1371 const cricket::VideoContentDescription* video_desc =
1372 static_cast<const cricket::VideoContentDescription*>(
1373 video_content->description);
1374 EXPECT_TRUE(
1375 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1376
1377 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1378
1379 // Now try removing the tracks.
1380 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1381 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1382
1383 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001384 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001385
1386 audio_content = cricket::GetFirstAudioContent(offer->description());
1387 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1388 audio_content->description);
1389 EXPECT_FALSE(
1390 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1391
1392 video_content = cricket::GetFirstVideoContent(offer->description());
1393 video_desc = static_cast<const cricket::VideoContentDescription*>(
1394 video_content->description);
1395 EXPECT_FALSE(
1396 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1397
1398 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1399
1400 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1401 // should return false.
1402 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1403 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1404}
1405
1406// Test creating senders without a stream specified,
1407// expecting a random stream ID to be generated.
1408TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1409 CreatePeerConnection();
1410 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001411 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001412 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001413 rtc::scoped_refptr<VideoTrackInterface> video_track(
1414 pc_factory_->CreateVideoTrack(
1415 "video_track",
1416 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001417 auto audio_sender =
1418 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1419 auto video_sender =
1420 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1421 EXPECT_EQ("audio_track", audio_sender->id());
1422 EXPECT_EQ(audio_track, audio_sender->track());
1423 EXPECT_EQ("video_track", video_sender->id());
1424 EXPECT_EQ(video_track, video_sender->track());
1425 // If the ID is truly a random GUID, it should be infinitely unlikely they
1426 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001427 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001428}
1429
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1431 InitiateCall();
1432 WaitAndVerifyOnAddStream(kStreamLabel1);
1433 VerifyRemoteRtpHeaderExtensions();
1434}
1435
1436TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1437 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001438 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 CreateOfferAsLocalDescription();
1440 std::string offer;
1441 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1442 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1443 WaitAndVerifyOnAddStream(kStreamLabel1);
1444}
1445
1446TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1447 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001448 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449
1450 CreateOfferAsRemoteDescription();
1451 CreateAnswerAsLocalDescription();
1452
1453 WaitAndVerifyOnAddStream(kStreamLabel1);
1454}
1455
1456TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1457 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001458 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459
1460 CreateOfferAsRemoteDescription();
1461 CreatePrAnswerAsLocalDescription();
1462 CreateAnswerAsLocalDescription();
1463
1464 WaitAndVerifyOnAddStream(kStreamLabel1);
1465}
1466
1467TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1468 InitiateCall();
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());
deadbeefab9b2d12015-10-14 11:33:11 -07001473 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 CreateOfferReceiveAnswer();
1475}
1476
1477// Tests that after negotiating an audio only call, the respondent can perform a
1478// renegotiation that removes the audio stream.
1479TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1480 CreatePeerConnection();
1481 AddVoiceStream(kStreamLabel1);
1482 CreateOfferAsRemoteDescription();
1483 CreateAnswerAsLocalDescription();
1484
1485 ASSERT_EQ(1u, pc_->remote_streams()->count());
1486 pc_->RemoveStream(pc_->local_streams()->at(0));
1487 CreateOfferReceiveAnswer();
1488 EXPECT_EQ(0u, pc_->remote_streams()->count());
1489}
1490
1491// Test that candidates are generated and that we can parse our own candidates.
1492TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1493 CreatePeerConnection();
1494
1495 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1496 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001497 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001498 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001499 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001500 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501
1502 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001503 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001504 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001505 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506
1507 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1508 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1509
1510 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1511}
1512
deadbeefab9b2d12015-10-14 11:33:11 -07001513// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514// not unique.
1515TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1516 CreatePeerConnection();
1517 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001518 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001519 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001520 EXPECT_TRUE(offer);
1521 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522
1523 // Create a local stream with audio&video tracks having same label.
1524 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1525
1526 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001527 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528
1529 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001530 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001531 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532}
1533
1534// Test that we will get different SSRCs for each tracks in the offer and answer
1535// we created.
1536TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1537 CreatePeerConnection();
1538 // Create a local stream with audio&video tracks having different labels.
1539 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1540
1541 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001542 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001543 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544 int audio_ssrc = 0;
1545 int video_ssrc = 0;
1546 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1547 &audio_ssrc));
1548 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1549 &video_ssrc));
1550 EXPECT_NE(audio_ssrc, video_ssrc);
1551
1552 // Test CreateAnswer
1553 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001554 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001555 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 audio_ssrc = 0;
1557 video_ssrc = 0;
1558 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1559 &audio_ssrc));
1560 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1561 &video_ssrc));
1562 EXPECT_NE(audio_ssrc, video_ssrc);
1563}
1564
deadbeefeb459812015-12-15 19:24:43 -08001565// Test that it's possible to call AddTrack on a MediaStream after adding
1566// the stream to a PeerConnection.
1567// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1568TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1569 CreatePeerConnection();
1570 // Create audio stream and add to PeerConnection.
1571 AddVoiceStream(kStreamLabel1);
1572 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1573
1574 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001575 rtc::scoped_refptr<VideoTrackInterface> video_track(
1576 pc_factory_->CreateVideoTrack(
1577 "video_label",
1578 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001579 stream->AddTrack(video_track.get());
1580
kwibergd1fe2812016-04-27 06:47:29 -07001581 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001582 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001583
1584 const cricket::MediaContentDescription* video_desc =
1585 cricket::GetFirstVideoContentDescription(offer->description());
1586 EXPECT_TRUE(video_desc != nullptr);
1587}
1588
1589// Test that it's possible to call RemoveTrack on a MediaStream after adding
1590// the stream to a PeerConnection.
1591// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1592TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1593 CreatePeerConnection();
1594 // Create audio/video stream and add to PeerConnection.
1595 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1596 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1597
1598 // Remove the video track.
1599 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1600
kwibergd1fe2812016-04-27 06:47:29 -07001601 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001602 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001603
1604 const cricket::MediaContentDescription* video_desc =
1605 cricket::GetFirstVideoContentDescription(offer->description());
1606 EXPECT_TRUE(video_desc == nullptr);
1607}
1608
deadbeefbd7d8f72015-12-18 16:58:44 -08001609// Test creating a sender with a stream ID, and ensure the ID is populated
1610// in the offer.
1611TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1612 CreatePeerConnection();
1613 pc_->CreateSender("video", kStreamLabel1);
1614
kwibergd1fe2812016-04-27 06:47:29 -07001615 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001616 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001617
1618 const cricket::MediaContentDescription* video_desc =
1619 cricket::GetFirstVideoContentDescription(offer->description());
1620 ASSERT_TRUE(video_desc != nullptr);
1621 ASSERT_EQ(1u, video_desc->streams().size());
1622 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1623}
1624
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625// Test that we can specify a certain track that we want statistics about.
1626TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1627 InitiateCall();
1628 ASSERT_LT(0u, pc_->remote_streams()->count());
1629 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001630 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1632 EXPECT_TRUE(DoGetStats(remote_audio));
1633
1634 // Remove the stream. Since we are sending to our selves the local
1635 // and the remote stream is the same.
1636 pc_->RemoveStream(pc_->local_streams()->at(0));
1637 // Do a re-negotiation.
1638 CreateOfferReceiveAnswer();
1639
1640 ASSERT_EQ(0u, pc_->remote_streams()->count());
1641
1642 // Test that we still can get statistics for the old track. Even if it is not
1643 // sent any longer.
1644 EXPECT_TRUE(DoGetStats(remote_audio));
1645}
1646
1647// Test that we can get stats on a video track.
1648TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1649 InitiateCall();
1650 ASSERT_LT(0u, pc_->remote_streams()->count());
1651 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001652 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1654 EXPECT_TRUE(DoGetStats(remote_video));
1655}
1656
1657// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001658TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001660 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 pc_factory_->CreateAudioTrack("unknown track", NULL));
1662 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1663}
1664
1665// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 FakeConstraints constraints;
1668 constraints.SetAllowRtpDataChannels();
1669 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001670 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001672 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 pc_->CreateDataChannel("test2", NULL);
1674 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001675 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001677 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 new MockDataChannelObserver(data2));
1679
1680 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1681 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1682 std::string data_to_send1 = "testing testing";
1683 std::string data_to_send2 = "testing something else";
1684 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1685
1686 CreateOfferReceiveAnswer();
1687 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1688 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1689
1690 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1691 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1692 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1693 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1694
1695 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1696 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1697
1698 data1->Close();
1699 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1700 CreateOfferReceiveAnswer();
1701 EXPECT_FALSE(observer1->IsOpen());
1702 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1703 EXPECT_TRUE(observer2->IsOpen());
1704
1705 data_to_send2 = "testing something else again";
1706 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1707
1708 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1709}
1710
1711// This test verifies that sendnig binary data over RTP data channels should
1712// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 FakeConstraints constraints;
1715 constraints.SetAllowRtpDataChannels();
1716 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001717 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001719 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 pc_->CreateDataChannel("test2", NULL);
1721 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001722 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001724 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 new MockDataChannelObserver(data2));
1726
1727 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1728 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1729
1730 CreateOfferReceiveAnswer();
1731 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1732 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1733
1734 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1735 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1736
jbaucheec21bd2016-03-20 06:15:43 -07001737 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1739}
1740
1741// This test setup a RTP data channels in loop back and test that a channel is
1742// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 FakeConstraints constraints;
1745 constraints.SetAllowRtpDataChannels();
1746 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001747 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001749 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 new MockDataChannelObserver(data1));
1751
1752 CreateOfferReceiveAnswerWithoutSsrc();
1753
1754 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1755
1756 data1->Close();
1757 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1758 CreateOfferReceiveAnswerWithoutSsrc();
1759 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1760 EXPECT_FALSE(observer1->IsOpen());
1761}
1762
1763// This test that if a data channel is added in an answer a receive only channel
1764// channel is created.
1765TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1766 FakeConstraints constraints;
1767 constraints.SetAllowRtpDataChannels();
1768 CreatePeerConnection(&constraints);
1769
1770 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001771 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 pc_->CreateDataChannel(offer_label, NULL);
1773
1774 CreateOfferAsLocalDescription();
1775
1776 // Replace the data channel label in the offer and apply it as an answer.
1777 std::string receive_label = "answer_channel";
1778 std::string sdp;
1779 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001780 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 receive_label.c_str(), receive_label.length(),
1782 &sdp);
1783 CreateAnswerAsRemoteDescription(sdp);
1784
1785 // Verify that a new incoming data channel has been created and that
1786 // it is open but can't we written to.
1787 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1788 DataChannelInterface* received_channel = observer_.last_datachannel_;
1789 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1790 EXPECT_EQ(receive_label, received_channel->label());
1791 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1792
1793 // Verify that the channel we initially offered has been rejected.
1794 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1795
1796 // Do another offer / answer exchange and verify that the data channel is
1797 // opened.
1798 CreateOfferReceiveAnswer();
1799 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1800 kTimeout);
1801}
1802
1803// This test that no data channel is returned if a reliable channel is
1804// requested.
1805// TODO(perkj): Remove this test once reliable channels are implemented.
1806TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1807 FakeConstraints constraints;
1808 constraints.SetAllowRtpDataChannels();
1809 CreatePeerConnection(&constraints);
1810
1811 std::string label = "test";
1812 webrtc::DataChannelInit config;
1813 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001814 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 pc_->CreateDataChannel(label, &config);
1816 EXPECT_TRUE(channel == NULL);
1817}
1818
deadbeefab9b2d12015-10-14 11:33:11 -07001819// Verifies that duplicated label is not allowed for RTP data channel.
1820TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1821 FakeConstraints constraints;
1822 constraints.SetAllowRtpDataChannels();
1823 CreatePeerConnection(&constraints);
1824
1825 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001826 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001827 pc_->CreateDataChannel(label, nullptr);
1828 EXPECT_NE(channel, nullptr);
1829
zhihuang9763d562016-08-05 11:14:50 -07001830 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001831 pc_->CreateDataChannel(label, nullptr);
1832 EXPECT_EQ(dup_channel, nullptr);
1833}
1834
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835// This tests that a SCTP data channel is returned using different
1836// DataChannelInit configurations.
1837TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1838 FakeConstraints constraints;
1839 constraints.SetAllowDtlsSctpDataChannels();
1840 CreatePeerConnection(&constraints);
1841
1842 webrtc::DataChannelInit config;
1843
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 pc_->CreateDataChannel("1", &config);
1846 EXPECT_TRUE(channel != NULL);
1847 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001848 EXPECT_TRUE(observer_.renegotiation_needed_);
1849 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850
1851 config.ordered = false;
1852 channel = pc_->CreateDataChannel("2", &config);
1853 EXPECT_TRUE(channel != NULL);
1854 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001855 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856
1857 config.ordered = true;
1858 config.maxRetransmits = 0;
1859 channel = pc_->CreateDataChannel("3", &config);
1860 EXPECT_TRUE(channel != NULL);
1861 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001862 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863
1864 config.maxRetransmits = -1;
1865 config.maxRetransmitTime = 0;
1866 channel = pc_->CreateDataChannel("4", &config);
1867 EXPECT_TRUE(channel != NULL);
1868 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001869 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870}
1871
1872// This tests that no data channel is returned if both maxRetransmits and
1873// maxRetransmitTime are set for SCTP data channels.
1874TEST_F(PeerConnectionInterfaceTest,
1875 CreateSctpDataChannelShouldFailForInvalidConfig) {
1876 FakeConstraints constraints;
1877 constraints.SetAllowDtlsSctpDataChannels();
1878 CreatePeerConnection(&constraints);
1879
1880 std::string label = "test";
1881 webrtc::DataChannelInit config;
1882 config.maxRetransmits = 0;
1883 config.maxRetransmitTime = 0;
1884
zhihuang9763d562016-08-05 11:14:50 -07001885 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 pc_->CreateDataChannel(label, &config);
1887 EXPECT_TRUE(channel == NULL);
1888}
1889
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890// The test verifies that creating a SCTP data channel with an id already in use
1891// or out of range should fail.
1892TEST_F(PeerConnectionInterfaceTest,
1893 CreateSctpDataChannelWithInvalidIdShouldFail) {
1894 FakeConstraints constraints;
1895 constraints.SetAllowDtlsSctpDataChannels();
1896 CreatePeerConnection(&constraints);
1897
1898 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001899 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001901 config.id = 1;
1902 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 EXPECT_TRUE(channel != NULL);
1904 EXPECT_EQ(1, channel->id());
1905
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 channel = pc_->CreateDataChannel("x", &config);
1907 EXPECT_TRUE(channel == NULL);
1908
1909 config.id = cricket::kMaxSctpSid;
1910 channel = pc_->CreateDataChannel("max", &config);
1911 EXPECT_TRUE(channel != NULL);
1912 EXPECT_EQ(config.id, channel->id());
1913
1914 config.id = cricket::kMaxSctpSid + 1;
1915 channel = pc_->CreateDataChannel("x", &config);
1916 EXPECT_TRUE(channel == NULL);
1917}
1918
deadbeefab9b2d12015-10-14 11:33:11 -07001919// Verifies that duplicated label is allowed for SCTP data channel.
1920TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1921 FakeConstraints constraints;
1922 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1923 true);
1924 CreatePeerConnection(&constraints);
1925
1926 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001927 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001928 pc_->CreateDataChannel(label, nullptr);
1929 EXPECT_NE(channel, nullptr);
1930
zhihuang9763d562016-08-05 11:14:50 -07001931 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001932 pc_->CreateDataChannel(label, nullptr);
1933 EXPECT_NE(dup_channel, nullptr);
1934}
1935
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001936// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1937// DataChannel.
1938TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1939 FakeConstraints constraints;
1940 constraints.SetAllowRtpDataChannels();
1941 CreatePeerConnection(&constraints);
1942
zhihuang9763d562016-08-05 11:14:50 -07001943 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001944 pc_->CreateDataChannel("test1", NULL);
1945 EXPECT_TRUE(observer_.renegotiation_needed_);
1946 observer_.renegotiation_needed_ = false;
1947
zhihuang9763d562016-08-05 11:14:50 -07001948 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001949 pc_->CreateDataChannel("test2", NULL);
1950 EXPECT_TRUE(observer_.renegotiation_needed_);
1951}
1952
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 FakeConstraints constraints;
1956 constraints.SetAllowRtpDataChannels();
1957 CreatePeerConnection(&constraints);
1958
zhihuang9763d562016-08-05 11:14:50 -07001959 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001961 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 pc_->CreateDataChannel("test2", NULL);
1963 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001964 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001966 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 new MockDataChannelObserver(data2));
1968
1969 CreateOfferReceiveAnswer();
1970 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1971 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1972
1973 ReleasePeerConnection();
1974 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1975 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1976}
1977
1978// This test that data channels can be rejected in an answer.
1979TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1980 FakeConstraints constraints;
1981 constraints.SetAllowRtpDataChannels();
1982 CreatePeerConnection(&constraints);
1983
zhihuang9763d562016-08-05 11:14:50 -07001984 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 pc_->CreateDataChannel("offer_channel", NULL));
1986
1987 CreateOfferAsLocalDescription();
1988
1989 // Create an answer where the m-line for data channels are rejected.
1990 std::string sdp;
1991 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1992 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1993 SessionDescriptionInterface::kAnswer);
1994 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1995 cricket::ContentInfo* data_info =
1996 answer->description()->GetContentByName("data");
1997 data_info->rejected = true;
1998
1999 DoSetRemoteDescription(answer);
2000 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2001}
2002
2003// Test that we can create a session description from an SDP string from
2004// FireFox, use it as a remote session description, generate an answer and use
2005// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002006TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002007 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 FakeConstraints constraints;
2009 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2010 true);
2011 CreatePeerConnection(&constraints);
2012 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2013 SessionDescriptionInterface* desc =
2014 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002015 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2017 CreateAnswerAsLocalDescription();
2018 ASSERT_TRUE(pc_->local_description() != NULL);
2019 ASSERT_TRUE(pc_->remote_description() != NULL);
2020
2021 const cricket::ContentInfo* content =
2022 cricket::GetFirstAudioContent(pc_->local_description()->description());
2023 ASSERT_TRUE(content != NULL);
2024 EXPECT_FALSE(content->rejected);
2025
2026 content =
2027 cricket::GetFirstVideoContent(pc_->local_description()->description());
2028 ASSERT_TRUE(content != NULL);
2029 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002030#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 content =
2032 cricket::GetFirstDataContent(pc_->local_description()->description());
2033 ASSERT_TRUE(content != NULL);
2034 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002035#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036}
2037
2038// Test that we can create an audio only offer and receive an answer with a
2039// limited set of audio codecs and receive an updated offer with more audio
2040// codecs, where the added codecs are not supported.
2041TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2042 CreatePeerConnection();
2043 AddVoiceStream("audio_label");
2044 CreateOfferAsLocalDescription();
2045
2046 SessionDescriptionInterface* answer =
2047 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002048 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2050
2051 SessionDescriptionInterface* updated_offer =
2052 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002053 webrtc::kAudioSdpWithUnsupportedCodecs,
2054 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2056 CreateAnswerAsLocalDescription();
2057}
2058
deadbeefc80741f2015-10-22 13:14:45 -07002059// Test that if we're receiving (but not sending) a track, subsequent offers
2060// will have m-lines with a=recvonly.
2061TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2062 FakeConstraints constraints;
2063 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2064 true);
2065 CreatePeerConnection(&constraints);
2066 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2067 CreateAnswerAsLocalDescription();
2068
2069 // At this point we should be receiving stream 1, but not sending anything.
2070 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002071 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002072 DoCreateOffer(&offer, nullptr);
2073
2074 const cricket::ContentInfo* video_content =
2075 cricket::GetFirstVideoContent(offer->description());
2076 const cricket::VideoContentDescription* video_desc =
2077 static_cast<const cricket::VideoContentDescription*>(
2078 video_content->description);
2079 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2080
2081 const cricket::ContentInfo* audio_content =
2082 cricket::GetFirstAudioContent(offer->description());
2083 const cricket::AudioContentDescription* audio_desc =
2084 static_cast<const cricket::AudioContentDescription*>(
2085 audio_content->description);
2086 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2087}
2088
2089// Test that if we're receiving (but not sending) a track, and the
2090// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2091// false, the generated m-lines will be a=inactive.
2092TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2093 FakeConstraints constraints;
2094 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2095 true);
2096 CreatePeerConnection(&constraints);
2097 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2098 CreateAnswerAsLocalDescription();
2099
2100 // At this point we should be receiving stream 1, but not sending anything.
2101 // A new offer would be recvonly, but we'll set the "no receive" constraints
2102 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002103 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002104 FakeConstraints offer_constraints;
2105 offer_constraints.AddMandatory(
2106 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2107 offer_constraints.AddMandatory(
2108 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2109 DoCreateOffer(&offer, &offer_constraints);
2110
2111 const cricket::ContentInfo* video_content =
2112 cricket::GetFirstVideoContent(offer->description());
2113 const cricket::VideoContentDescription* video_desc =
2114 static_cast<const cricket::VideoContentDescription*>(
2115 video_content->description);
2116 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2117
2118 const cricket::ContentInfo* audio_content =
2119 cricket::GetFirstAudioContent(offer->description());
2120 const cricket::AudioContentDescription* audio_desc =
2121 static_cast<const cricket::AudioContentDescription*>(
2122 audio_content->description);
2123 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2124}
2125
deadbeef653b8e02015-11-11 12:55:10 -08002126// Test that we can use SetConfiguration to change the ICE servers of the
2127// PortAllocator.
2128TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2129 CreatePeerConnection();
2130
2131 PeerConnectionInterface::RTCConfiguration config;
2132 PeerConnectionInterface::IceServer server;
2133 server.uri = "stun:test_hostname";
2134 config.servers.push_back(server);
2135 EXPECT_TRUE(pc_->SetConfiguration(config));
2136
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002137 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2138 EXPECT_EQ("test_hostname",
2139 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002140}
2141
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002142TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2143 CreatePeerConnection();
2144 PeerConnectionInterface::RTCConfiguration config;
2145 config.type = PeerConnectionInterface::kRelay;
2146 EXPECT_TRUE(pc_->SetConfiguration(config));
2147 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2148}
2149
2150// Test that when SetConfiguration changes both the pool size and other
2151// attributes, the pooled session is created with the updated attributes.
2152TEST_F(PeerConnectionInterfaceTest,
2153 SetConfigurationCreatesPooledSessionCorrectly) {
2154 CreatePeerConnection();
2155 PeerConnectionInterface::RTCConfiguration config;
2156 config.ice_candidate_pool_size = 1;
2157 PeerConnectionInterface::IceServer server;
2158 server.uri = kStunAddressOnly;
2159 config.servers.push_back(server);
2160 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002161 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002162
2163 const cricket::FakePortAllocatorSession* session =
2164 static_cast<const cricket::FakePortAllocatorSession*>(
2165 port_allocator_->GetPooledSession());
2166 ASSERT_NE(nullptr, session);
2167 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002168}
2169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170// Test that PeerConnection::Close changes the states to closed and all remote
2171// tracks change state to ended.
2172TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2173 // Initialize a PeerConnection and negotiate local and remote session
2174 // description.
2175 InitiateCall();
2176 ASSERT_EQ(1u, pc_->local_streams()->count());
2177 ASSERT_EQ(1u, pc_->remote_streams()->count());
2178
2179 pc_->Close();
2180
2181 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2182 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2183 pc_->ice_connection_state());
2184 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2185 pc_->ice_gathering_state());
2186
2187 EXPECT_EQ(1u, pc_->local_streams()->count());
2188 EXPECT_EQ(1u, pc_->remote_streams()->count());
2189
zhihuang9763d562016-08-05 11:14:50 -07002190 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2191 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002192 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002193 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002194 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2195 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2196 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197}
2198
2199// Test that PeerConnection methods fails gracefully after
2200// PeerConnection::Close has been called.
2201TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2202 CreatePeerConnection();
2203 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2204 CreateOfferAsRemoteDescription();
2205 CreateAnswerAsLocalDescription();
2206
2207 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002208 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 pc_->local_streams()->at(0);
2210
2211 pc_->Close();
2212
2213 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002214 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215
2216 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002217 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002219 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220
2221 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2222
2223 EXPECT_TRUE(pc_->local_description() != NULL);
2224 EXPECT_TRUE(pc_->remote_description() != NULL);
2225
kwibergd1fe2812016-04-27 06:47:29 -07002226 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002227 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002228 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002229 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230
2231 std::string sdp;
2232 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2233 SessionDescriptionInterface* remote_offer =
2234 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2235 sdp, NULL);
2236 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2237
2238 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2239 SessionDescriptionInterface* local_offer =
2240 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2241 sdp, NULL);
2242 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2243}
2244
2245// Test that GetStats can still be called after PeerConnection::Close.
2246TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2247 InitiateCall();
2248 pc_->Close();
2249 DoGetStats(NULL);
2250}
deadbeefab9b2d12015-10-14 11:33:11 -07002251
2252// NOTE: The series of tests below come from what used to be
2253// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2254// setting a remote or local description has the expected effects.
2255
2256// This test verifies that the remote MediaStreams corresponding to a received
2257// SDP string is created. In this test the two separate MediaStreams are
2258// signaled.
2259TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2260 FakeConstraints constraints;
2261 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2262 true);
2263 CreatePeerConnection(&constraints);
2264 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2265
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002266 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002267 EXPECT_TRUE(
2268 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2269 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2270 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2271
2272 // Create a session description based on another SDP with another
2273 // MediaStream.
2274 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2275
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002276 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002277 EXPECT_TRUE(
2278 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2279}
2280
2281// This test verifies that when remote tracks are added/removed from SDP, the
2282// created remote streams are updated appropriately.
2283TEST_F(PeerConnectionInterfaceTest,
2284 AddRemoveTrackFromExistingRemoteMediaStream) {
2285 FakeConstraints constraints;
2286 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2287 true);
2288 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002289 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002290 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002291 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2292 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2293 reference_collection_));
2294
2295 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002296 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002297 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002298 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2299 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2300 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002301 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002302 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2303 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002304 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002305 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2306 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002307
2308 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002309 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002310 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002311 MockTrackObserver audio_track_observer(audio_track2);
2312 MockTrackObserver video_track_observer(video_track2);
2313
2314 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2315 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002316 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2317 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2318 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002319 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002320 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002321 audio_track2->state(), kTimeout);
2322 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2323 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002324}
2325
2326// This tests that remote tracks are ended if a local session description is set
2327// that rejects the media content type.
2328TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2329 FakeConstraints constraints;
2330 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2331 true);
2332 CreatePeerConnection(&constraints);
2333 // First create and set a remote offer, then reject its video content in our
2334 // answer.
2335 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2336 ASSERT_EQ(1u, observer_.remote_streams()->count());
2337 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2338 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2339 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2340
2341 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2342 remote_stream->GetVideoTracks()[0];
2343 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2344 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2345 remote_stream->GetAudioTracks()[0];
2346 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2347
kwibergd1fe2812016-04-27 06:47:29 -07002348 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002349 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002350 cricket::ContentInfo* video_info =
2351 local_answer->description()->GetContentByName("video");
2352 video_info->rejected = true;
2353 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2354 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2355 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2356
2357 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002358 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002359 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002360 video_info = local_offer->description()->GetContentByName("video");
2361 ASSERT_TRUE(video_info != nullptr);
2362 video_info->rejected = true;
2363 cricket::ContentInfo* audio_info =
2364 local_offer->description()->GetContentByName("audio");
2365 ASSERT_TRUE(audio_info != nullptr);
2366 audio_info->rejected = true;
2367 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002368 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002369 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002370 remote_audio->state(), kTimeout);
2371 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2372 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002373}
2374
2375// This tests that we won't crash if the remote track has been removed outside
2376// of PeerConnection and then PeerConnection tries to reject the track.
2377TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2378 FakeConstraints constraints;
2379 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2380 true);
2381 CreatePeerConnection(&constraints);
2382 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2383 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2384 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2385 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2386
kwibergd1fe2812016-04-27 06:47:29 -07002387 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002388 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2389 kSdpStringWithStream1, nullptr));
2390 cricket::ContentInfo* video_info =
2391 local_answer->description()->GetContentByName("video");
2392 video_info->rejected = true;
2393 cricket::ContentInfo* audio_info =
2394 local_answer->description()->GetContentByName("audio");
2395 audio_info->rejected = true;
2396 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2397
2398 // No crash is a pass.
2399}
2400
deadbeef5e97fb52015-10-15 12:49:08 -07002401// This tests that if a recvonly remote description is set, no remote streams
2402// will be created, even if the description contains SSRCs/MSIDs.
2403// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2404TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2405 FakeConstraints constraints;
2406 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2407 true);
2408 CreatePeerConnection(&constraints);
2409
2410 std::string recvonly_offer = kSdpStringWithStream1;
2411 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2412 strlen(kRecvonly), &recvonly_offer);
2413 CreateAndSetRemoteOffer(recvonly_offer);
2414
2415 EXPECT_EQ(0u, observer_.remote_streams()->count());
2416}
2417
deadbeefab9b2d12015-10-14 11:33:11 -07002418// This tests that a default MediaStream is created if a remote session
2419// description doesn't contain any streams and no MSID support.
2420// It also tests that the default stream is updated if a video m-line is added
2421// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002422TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002423 FakeConstraints constraints;
2424 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2425 true);
2426 CreatePeerConnection(&constraints);
2427 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2428
2429 ASSERT_EQ(1u, observer_.remote_streams()->count());
2430 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2431
2432 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2433 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2434 EXPECT_EQ("default", remote_stream->label());
2435
2436 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2437 ASSERT_EQ(1u, observer_.remote_streams()->count());
2438 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2439 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002440 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2441 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002442 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2443 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002444 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2445 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002446}
2447
2448// This tests that a default MediaStream is created if a remote session
2449// description doesn't contain any streams and media direction is send only.
2450TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002451 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002452 FakeConstraints constraints;
2453 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2454 true);
2455 CreatePeerConnection(&constraints);
2456 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2457
2458 ASSERT_EQ(1u, observer_.remote_streams()->count());
2459 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2460
2461 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2462 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2463 EXPECT_EQ("default", remote_stream->label());
2464}
2465
2466// This tests that it won't crash when PeerConnection tries to remove
2467// a remote track that as already been removed from the MediaStream.
2468TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2469 FakeConstraints constraints;
2470 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2471 true);
2472 CreatePeerConnection(&constraints);
2473 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2474 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2475 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2476 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2477
2478 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2479
2480 // No crash is a pass.
2481}
2482
2483// This tests that a default MediaStream is created if the remote session
2484// description doesn't contain any streams and don't contain an indication if
2485// MSID is supported.
2486TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002487 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002488 FakeConstraints constraints;
2489 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2490 true);
2491 CreatePeerConnection(&constraints);
2492 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2493
2494 ASSERT_EQ(1u, observer_.remote_streams()->count());
2495 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2496 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2497 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2498}
2499
2500// This tests that a default MediaStream is not created if the remote session
2501// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002502TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002503 FakeConstraints constraints;
2504 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2505 true);
2506 CreatePeerConnection(&constraints);
2507 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2508 EXPECT_EQ(0u, observer_.remote_streams()->count());
2509}
2510
deadbeefbda7e0b2015-12-08 17:13:40 -08002511// This tests that when setting a new description, the old default tracks are
2512// not destroyed and recreated.
2513// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002514TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002515 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002516 FakeConstraints constraints;
2517 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2518 true);
2519 CreatePeerConnection(&constraints);
2520 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2521
2522 ASSERT_EQ(1u, observer_.remote_streams()->count());
2523 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2524 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2525
2526 // Set the track to "disabled", then set a new description and ensure the
2527 // track is still disabled, which ensures it hasn't been recreated.
2528 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2529 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2530 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2531 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2532}
2533
deadbeefab9b2d12015-10-14 11:33:11 -07002534// This tests that a default MediaStream is not created if a remote session
2535// description is updated to not have any MediaStreams.
2536TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2537 FakeConstraints constraints;
2538 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2539 true);
2540 CreatePeerConnection(&constraints);
2541 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002542 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002543 EXPECT_TRUE(
2544 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2545
2546 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2547 EXPECT_EQ(0u, observer_.remote_streams()->count());
2548}
2549
2550// This tests that an RtpSender is created when the local description is set
2551// after adding a local stream.
2552// TODO(deadbeef): This test and the one below it need to be updated when
2553// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002554TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002555 FakeConstraints constraints;
2556 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2557 true);
2558 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002559
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002560 // Create an offer with 1 stream with 2 tracks of each type.
2561 rtc::scoped_refptr<StreamCollection> stream_collection =
2562 CreateStreamCollection(1, 2);
2563 pc_->AddStream(stream_collection->at(0));
2564 std::unique_ptr<SessionDescriptionInterface> offer;
2565 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2566 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002567
deadbeefab9b2d12015-10-14 11:33:11 -07002568 auto senders = pc_->GetSenders();
2569 EXPECT_EQ(4u, senders.size());
2570 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2571 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2572 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2573 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2574
2575 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002576 pc_->RemoveStream(stream_collection->at(0));
2577 stream_collection = CreateStreamCollection(1, 1);
2578 pc_->AddStream(stream_collection->at(0));
2579 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2580 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2581
deadbeefab9b2d12015-10-14 11:33:11 -07002582 senders = pc_->GetSenders();
2583 EXPECT_EQ(2u, senders.size());
2584 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2585 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2586 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2587 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2588}
2589
2590// This tests that an RtpSender is created when the local description is set
2591// before adding a local stream.
2592TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002593 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002594 FakeConstraints constraints;
2595 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2596 true);
2597 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002598
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002599 rtc::scoped_refptr<StreamCollection> stream_collection =
2600 CreateStreamCollection(1, 2);
2601 // Add a stream to create the offer, but remove it afterwards.
2602 pc_->AddStream(stream_collection->at(0));
2603 std::unique_ptr<SessionDescriptionInterface> offer;
2604 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2605 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002606
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002607 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002608 auto senders = pc_->GetSenders();
2609 EXPECT_EQ(0u, senders.size());
2610
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002611 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002612 senders = pc_->GetSenders();
2613 EXPECT_EQ(4u, senders.size());
2614 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2615 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2616 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2617 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2618}
2619
2620// This tests that the expected behavior occurs if the SSRC on a local track is
2621// changed when SetLocalDescription is called.
2622TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002623 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002624 FakeConstraints constraints;
2625 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2626 true);
2627 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002628
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002629 rtc::scoped_refptr<StreamCollection> stream_collection =
2630 CreateStreamCollection(2, 1);
2631 pc_->AddStream(stream_collection->at(0));
2632 std::unique_ptr<SessionDescriptionInterface> offer;
2633 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2634 // Grab a copy of the offer before it gets passed into the PC.
2635 std::unique_ptr<JsepSessionDescription> modified_offer(
2636 new JsepSessionDescription(JsepSessionDescription::kOffer));
2637 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2638 offer->session_version());
2639 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002640
deadbeefab9b2d12015-10-14 11:33:11 -07002641 auto senders = pc_->GetSenders();
2642 EXPECT_EQ(2u, senders.size());
2643 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2644 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2645
2646 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002647 cricket::MediaContentDescription* desc =
2648 cricket::GetFirstAudioContentDescription(modified_offer->description());
2649 ASSERT_TRUE(desc != NULL);
2650 for (StreamParams& stream : desc->mutable_streams()) {
2651 for (unsigned int& ssrc : stream.ssrcs) {
2652 ++ssrc;
2653 }
2654 }
deadbeefab9b2d12015-10-14 11:33:11 -07002655
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002656 desc =
2657 cricket::GetFirstVideoContentDescription(modified_offer->description());
2658 ASSERT_TRUE(desc != NULL);
2659 for (StreamParams& stream : desc->mutable_streams()) {
2660 for (unsigned int& ssrc : stream.ssrcs) {
2661 ++ssrc;
2662 }
2663 }
2664
2665 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002666 senders = pc_->GetSenders();
2667 EXPECT_EQ(2u, senders.size());
2668 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2669 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2670 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2671 // changed.
2672}
2673
2674// This tests that the expected behavior occurs if a new session description is
2675// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002676TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002677 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002678 FakeConstraints constraints;
2679 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2680 true);
2681 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002682
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002683 rtc::scoped_refptr<StreamCollection> stream_collection =
2684 CreateStreamCollection(2, 1);
2685 pc_->AddStream(stream_collection->at(0));
2686 std::unique_ptr<SessionDescriptionInterface> offer;
2687 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2688 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002689
deadbeefab9b2d12015-10-14 11:33:11 -07002690 auto senders = pc_->GetSenders();
2691 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002692 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2693 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002694
2695 // Add a new MediaStream but with the same tracks as in the first stream.
2696 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2697 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002698 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2699 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002700 pc_->AddStream(stream_1);
2701
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002702 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2703 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002704
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002705 auto new_senders = pc_->GetSenders();
2706 // Should be the same senders as before, but with updated stream id.
2707 // Note that this behavior is subject to change in the future.
2708 // We may decide the PC should ignore existing tracks in AddStream.
2709 EXPECT_EQ(senders, new_senders);
2710 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2711 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002712}
2713
zhihuang81c3a032016-11-17 12:06:24 -08002714// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2715TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2716 FakeConstraints constraints;
2717 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2718 true);
2719 CreatePeerConnection(&constraints);
2720 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2721 EXPECT_EQ(observer_.num_added_tracks_, 1);
2722 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2723
2724 // Create and set the updated remote SDP.
2725 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2726 EXPECT_EQ(observer_.num_added_tracks_, 2);
2727 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2728}
2729
nisse51542be2016-02-12 02:27:06 -08002730class PeerConnectionMediaConfigTest : public testing::Test {
2731 protected:
2732 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002733 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002734 pcf_->Initialize();
2735 }
2736 const cricket::MediaConfig& TestCreatePeerConnection(
2737 const PeerConnectionInterface::RTCConfiguration& config,
2738 const MediaConstraintsInterface *constraints) {
2739 pcf_->create_media_controller_called_ = false;
2740
zhihuang9763d562016-08-05 11:14:50 -07002741 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2742 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002743 EXPECT_TRUE(pc.get());
2744 EXPECT_TRUE(pcf_->create_media_controller_called_);
2745 return pcf_->create_media_controller_config_;
2746 }
2747
zhihuang9763d562016-08-05 11:14:50 -07002748 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002749 MockPeerConnectionObserver observer_;
2750};
2751
2752// This test verifies the default behaviour with no constraints and a
2753// default RTCConfiguration.
2754TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2755 PeerConnectionInterface::RTCConfiguration config;
2756 FakeConstraints constraints;
2757
2758 const cricket::MediaConfig& media_config =
2759 TestCreatePeerConnection(config, &constraints);
2760
2761 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002762 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2763 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2764 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002765}
2766
2767// This test verifies the DSCP constraint is recognized and passed to
2768// the CreateMediaController call.
2769TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2770 PeerConnectionInterface::RTCConfiguration config;
2771 FakeConstraints constraints;
2772
2773 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2774 const cricket::MediaConfig& media_config =
2775 TestCreatePeerConnection(config, &constraints);
2776
2777 EXPECT_TRUE(media_config.enable_dscp);
2778}
2779
2780// This test verifies the cpu overuse detection constraint is
2781// recognized and passed to the CreateMediaController call.
2782TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2783 PeerConnectionInterface::RTCConfiguration config;
2784 FakeConstraints constraints;
2785
2786 constraints.AddOptional(
2787 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2788 const cricket::MediaConfig media_config =
2789 TestCreatePeerConnection(config, &constraints);
2790
nisse0db023a2016-03-01 04:29:59 -08002791 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002792}
2793
2794// This test verifies that the disable_prerenderer_smoothing flag is
2795// propagated from RTCConfiguration to the CreateMediaController call.
2796TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2797 PeerConnectionInterface::RTCConfiguration config;
2798 FakeConstraints constraints;
2799
Niels Möller71bdda02016-03-31 12:59:59 +02002800 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002801 const cricket::MediaConfig& media_config =
2802 TestCreatePeerConnection(config, &constraints);
2803
nisse0db023a2016-03-01 04:29:59 -08002804 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2805}
2806
2807// This test verifies the suspend below min bitrate constraint is
2808// recognized and passed to the CreateMediaController call.
2809TEST_F(PeerConnectionMediaConfigTest,
2810 TestSuspendBelowMinBitrateConstraintTrue) {
2811 PeerConnectionInterface::RTCConfiguration config;
2812 FakeConstraints constraints;
2813
2814 constraints.AddOptional(
2815 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2816 true);
2817 const cricket::MediaConfig media_config =
2818 TestCreatePeerConnection(config, &constraints);
2819
2820 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002821}
2822
deadbeefab9b2d12015-10-14 11:33:11 -07002823// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002824// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2825// "verify options are converted correctly", should be "pass options into
2826// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002827
2828TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2829 RTCOfferAnswerOptions rtc_options;
2830 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2831
2832 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002833 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002834
2835 rtc_options.offer_to_receive_audio =
2836 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002837 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002838}
2839
2840TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2841 RTCOfferAnswerOptions rtc_options;
2842 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2843
2844 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002845 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002846
2847 rtc_options.offer_to_receive_video =
2848 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002849 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002850}
2851
2852// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002853// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002854TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2855 RTCOfferAnswerOptions rtc_options;
2856 rtc_options.offer_to_receive_audio = 1;
2857 rtc_options.offer_to_receive_video = 1;
2858
2859 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002860 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002861 EXPECT_TRUE(options.has_audio());
2862 EXPECT_TRUE(options.has_video());
2863 EXPECT_TRUE(options.bundle_enabled);
2864}
2865
2866// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002867// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002868TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2869 RTCOfferAnswerOptions rtc_options;
2870 rtc_options.offer_to_receive_audio = 1;
2871
2872 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002873 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002874 EXPECT_TRUE(options.has_audio());
2875 EXPECT_FALSE(options.has_video());
2876 EXPECT_TRUE(options.bundle_enabled);
2877}
2878
2879// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002880// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002881TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2882 RTCOfferAnswerOptions rtc_options;
2883
2884 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002885 options.transport_options["audio"] = cricket::TransportOptions();
2886 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002887 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002888 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002889 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002890 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002891 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002892 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2893 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002894}
2895
2896// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002897// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002898TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2899 RTCOfferAnswerOptions rtc_options;
2900 rtc_options.offer_to_receive_audio = 0;
2901 rtc_options.offer_to_receive_video = 1;
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_FALSE(options.has_audio());
2906 EXPECT_TRUE(options.has_video());
2907 EXPECT_TRUE(options.bundle_enabled);
2908}
2909
2910// Test that a correct MediaSessionOptions is created for an offer if
2911// UseRtpMux is set to false.
2912TEST(CreateSessionOptionsTest,
2913 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2914 RTCOfferAnswerOptions rtc_options;
2915 rtc_options.offer_to_receive_audio = 1;
2916 rtc_options.offer_to_receive_video = 1;
2917 rtc_options.use_rtp_mux = false;
2918
2919 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002920 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002921 EXPECT_TRUE(options.has_audio());
2922 EXPECT_TRUE(options.has_video());
2923 EXPECT_FALSE(options.bundle_enabled);
2924}
2925
2926// Test that a correct MediaSessionOptions is created to restart ice if
2927// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002928// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002929TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2930 RTCOfferAnswerOptions rtc_options;
2931 rtc_options.ice_restart = true;
2932
2933 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002934 options.transport_options["audio"] = cricket::TransportOptions();
2935 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002936 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002937 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2938 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002939
2940 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002941 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002942 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2943 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002944}
2945
2946// Test that the MediaConstraints in an answer don't affect if audio and video
2947// is offered in an offer but that if kOfferToReceiveAudio or
2948// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2949// included in subsequent answers.
2950TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2951 FakeConstraints answer_c;
2952 answer_c.SetMandatoryReceiveAudio(true);
2953 answer_c.SetMandatoryReceiveVideo(true);
2954
2955 cricket::MediaSessionOptions answer_options;
2956 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2957 EXPECT_TRUE(answer_options.has_audio());
2958 EXPECT_TRUE(answer_options.has_video());
2959
deadbeefc80741f2015-10-22 13:14:45 -07002960 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002961
2962 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002963 EXPECT_TRUE(
2964 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002965 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002966 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002967
deadbeefc80741f2015-10-22 13:14:45 -07002968 RTCOfferAnswerOptions updated_rtc_offer_options;
2969 updated_rtc_offer_options.offer_to_receive_audio = 1;
2970 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002971
2972 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002973 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002974 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002975 EXPECT_TRUE(updated_offer_options.has_audio());
2976 EXPECT_TRUE(updated_offer_options.has_video());
2977
2978 // Since an offer has been created with both audio and video, subsequent
2979 // offers and answers should contain both audio and video.
2980 // Answers will only contain the media types that exist in the offer
2981 // regardless of the value of |updated_answer_options.has_audio| and
2982 // |updated_answer_options.has_video|.
2983 FakeConstraints updated_answer_c;
2984 answer_c.SetMandatoryReceiveAudio(false);
2985 answer_c.SetMandatoryReceiveVideo(false);
2986
2987 cricket::MediaSessionOptions updated_answer_options;
2988 EXPECT_TRUE(
2989 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2990 EXPECT_TRUE(updated_answer_options.has_audio());
2991 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002992}
deadbeef3edec7c2016-12-10 11:44:26 -08002993
2994TEST(RtcErrorTest, OstreamOperator) {
2995 std::ostringstream oss;
2996 oss << webrtc::RtcError::NONE << ' '
2997 << webrtc::RtcError::INVALID_PARAMETER << ' '
2998 << webrtc::RtcError::INTERNAL_ERROR;
2999 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str());
3000}