blob: e70cbb64a1c4677c490d56aa67b705e0dffed1a9 [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"
deadbeef953c2ce2017-01-09 14:53:41 -080038#include "webrtc/media/sctp/sctptransportinternal.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"
deadbeefd1a38b52016-12-10 13:15:33 -080076 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070077 "a=ice-ufrag:e5785931\r\n"
78 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
79 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
80 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070081 "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"
deadbeefd1a38b52016-12-10 13:15:33 -080089 "a=ice-ufrag:e5785931\r\n"
90 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
91 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
92 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070093 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070094 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080095 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=rtpmap:120 VP8/90000\r\n"
97 "a=ssrc:2 cname:stream1\r\n"
98 "a=ssrc:2 mslabel:stream1\r\n"
99 "a=ssrc:2 label:videotrack0\r\n";
100
zhihuang81c3a032016-11-17 12:06:24 -0800101// Reference SDP with a MediaStream with label "stream1" and audio track with
102// id "audio_1";
103static const char kSdpStringWithStream1AudioTrackOnly[] =
104 "v=0\r\n"
105 "o=- 0 0 IN IP4 127.0.0.1\r\n"
106 "s=-\r\n"
107 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800108 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800109 "a=ice-ufrag:e5785931\r\n"
110 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
111 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
112 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800113 "a=mid:audio\r\n"
114 "a=sendrecv\r\n"
115 "a=rtpmap:103 ISAC/16000\r\n"
116 "a=ssrc:1 cname:stream1\r\n"
117 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800118 "a=ssrc:1 label:audiotrack0\r\n"
119 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800120
deadbeefab9b2d12015-10-14 11:33:11 -0700121// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
122// MediaStreams have one audio track and one video track.
123// This uses MSID.
124static const char kSdpStringWithStream1And2[] =
125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800129 "a=msid-semantic: WMS stream1 stream2\r\n"
130 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700131 "a=ice-ufrag:e5785931\r\n"
132 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
133 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
134 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800137 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=rtpmap:103 ISAC/16000\r\n"
139 "a=ssrc:1 cname:stream1\r\n"
140 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
141 "a=ssrc:3 cname:stream2\r\n"
142 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
143 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800144 "a=ice-ufrag:e5785931\r\n"
145 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
146 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
147 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700148 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700149 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800150 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=rtpmap:120 VP8/0\r\n"
152 "a=ssrc:2 cname:stream1\r\n"
153 "a=ssrc:2 msid:stream1 videotrack0\r\n"
154 "a=ssrc:4 cname:stream2\r\n"
155 "a=ssrc:4 msid:stream2 videotrack1\r\n";
156
157// Reference SDP without MediaStreams. Msid is not supported.
158static const char kSdpStringWithoutStreams[] =
159 "v=0\r\n"
160 "o=- 0 0 IN IP4 127.0.0.1\r\n"
161 "s=-\r\n"
162 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800163 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700164 "a=ice-ufrag:e5785931\r\n"
165 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
166 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
167 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700168 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700169 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800170 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=rtpmap:103 ISAC/16000\r\n"
172 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800173 "a=ice-ufrag:e5785931\r\n"
174 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
175 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
176 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700178 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800179 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=rtpmap:120 VP8/90000\r\n";
181
182// Reference SDP without MediaStreams. Msid is supported.
183static const char kSdpStringWithMsidWithoutStreams[] =
184 "v=0\r\n"
185 "o=- 0 0 IN IP4 127.0.0.1\r\n"
186 "s=-\r\n"
187 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800188 "a=msid-semantic: WMS\r\n"
189 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700190 "a=ice-ufrag:e5785931\r\n"
191 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
192 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
193 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700195 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800196 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=rtpmap:103 ISAC/16000\r\n"
198 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800199 "a=ice-ufrag:e5785931\r\n"
200 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
201 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
202 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=mid:video\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:120 VP8/90000\r\n";
207
208// Reference SDP without MediaStreams and audio only.
209static const char kSdpStringWithoutStreamsAudioOnly[] =
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"
deadbeefd1a38b52016-12-10 13:15:33 -0800214 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700215 "a=ice-ufrag:e5785931\r\n"
216 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
217 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
218 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700219 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700220 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800221 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=rtpmap:103 ISAC/16000\r\n";
223
224// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
225static const char kSdpStringSendOnlyWithoutStreams[] =
226 "v=0\r\n"
227 "o=- 0 0 IN IP4 127.0.0.1\r\n"
228 "s=-\r\n"
229 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800230 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700231 "a=ice-ufrag:e5785931\r\n"
232 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
233 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
234 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700235 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700236 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800238 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=rtpmap:103 ISAC/16000\r\n"
240 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800241 "a=ice-ufrag:e5785931\r\n"
242 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
243 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
244 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700246 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700247 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800248 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700249 "a=rtpmap:120 VP8/90000\r\n";
250
251static const char kSdpStringInit[] =
252 "v=0\r\n"
253 "o=- 0 0 IN IP4 127.0.0.1\r\n"
254 "s=-\r\n"
255 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700256 "a=msid-semantic: WMS\r\n";
257
258static const char kSdpStringAudio[] =
259 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800260 "a=ice-ufrag:e5785931\r\n"
261 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
262 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
263 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700264 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700265 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800266 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=rtpmap:103 ISAC/16000\r\n";
268
269static const char kSdpStringVideo[] =
270 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800271 "a=ice-ufrag:e5785931\r\n"
272 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
273 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
274 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700275 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700276 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800277 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=rtpmap:120 VP8/90000\r\n";
279
280static const char kSdpStringMs1Audio0[] =
281 "a=ssrc:1 cname:stream1\r\n"
282 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
283
284static const char kSdpStringMs1Video0[] =
285 "a=ssrc:2 cname:stream1\r\n"
286 "a=ssrc:2 msid:stream1 videotrack0\r\n";
287
288static const char kSdpStringMs1Audio1[] =
289 "a=ssrc:3 cname:stream1\r\n"
290 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
291
292static const char kSdpStringMs1Video1[] =
293 "a=ssrc:4 cname:stream1\r\n"
294 "a=ssrc:4 msid:stream1 videotrack1\r\n";
295
perkjd61bf802016-03-24 03:16:19 -0700296using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700297using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700299using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300using webrtc::AudioTrackInterface;
301using webrtc::DataBuffer;
302using webrtc::DataChannelInterface;
303using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700305using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700306using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700307using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308using webrtc::MediaStreamInterface;
309using webrtc::MediaStreamTrackInterface;
310using webrtc::MockCreateSessionDescriptionObserver;
311using webrtc::MockDataChannelObserver;
312using webrtc::MockSetSessionDescriptionObserver;
313using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700314using webrtc::NotifierInterface;
315using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316using webrtc::PeerConnectionInterface;
317using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800318using webrtc::RTCError;
319using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700320using webrtc::RtpReceiverInterface;
321using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322using webrtc::SdpParseError;
323using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700324using webrtc::StreamCollection;
325using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100326using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700327using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::VideoTrackInterface;
329
deadbeefab9b2d12015-10-14 11:33:11 -0700330typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332namespace {
333
334// Gets the first ssrc of given content type from the ContentInfo.
335bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
336 if (!content_info || !ssrc) {
337 return false;
338 }
339 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000340 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 content_info->description);
342 if (!media_desc || media_desc->streams().empty()) {
343 return false;
344 }
345 *ssrc = media_desc->streams().begin()->first_ssrc();
346 return true;
347}
348
deadbeefd1a38b52016-12-10 13:15:33 -0800349// Get the ufrags out of an SDP blob. Useful for testing ICE restart
350// behavior.
351std::vector<std::string> GetUfrags(
352 const webrtc::SessionDescriptionInterface* desc) {
353 std::vector<std::string> ufrags;
354 for (const cricket::TransportInfo& info :
355 desc->description()->transport_infos()) {
356 ufrags.push_back(info.description.ice_ufrag);
357 }
358 return ufrags;
359}
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361void SetSsrcToZero(std::string* sdp) {
362 const char kSdpSsrcAtribute[] = "a=ssrc:";
363 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
364 size_t ssrc_pos = 0;
365 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
366 std::string::npos) {
367 size_t end_ssrc = sdp->find(" ", ssrc_pos);
368 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
369 ssrc_pos = end_ssrc;
370 }
371}
372
deadbeefab9b2d12015-10-14 11:33:11 -0700373// Check if |streams| contains the specified track.
374bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
375 const std::string& stream_label,
376 const std::string& track_id) {
377 for (const cricket::StreamParams& params : streams) {
378 if (params.sync_label == stream_label && params.id == track_id) {
379 return true;
380 }
381 }
382 return false;
383}
384
385// Check if |senders| contains the specified sender, by id.
386bool ContainsSender(
387 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
388 const std::string& id) {
389 for (const auto& sender : senders) {
390 if (sender->id() == id) {
391 return true;
392 }
393 }
394 return false;
395}
396
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700397// Check if |senders| contains the specified sender, by id and stream id.
398bool ContainsSender(
399 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
400 const std::string& id,
401 const std::string& stream_id) {
402 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700403 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700404 return true;
405 }
406 }
407 return false;
408}
409
deadbeefab9b2d12015-10-14 11:33:11 -0700410// Create a collection of streams.
411// CreateStreamCollection(1) creates a collection that
412// correspond to kSdpStringWithStream1.
413// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
414rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700415 int number_of_streams,
416 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700417 rtc::scoped_refptr<StreamCollection> local_collection(
418 StreamCollection::Create());
419
420 for (int i = 0; i < number_of_streams; ++i) {
421 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
422 webrtc::MediaStream::Create(kStreams[i]));
423
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700424 for (int j = 0; j < tracks_per_stream; ++j) {
425 // Add a local audio track.
426 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
427 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
428 nullptr));
429 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700430
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700431 // Add a local video track.
432 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
433 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
434 webrtc::FakeVideoTrackSource::Create()));
435 stream->AddTrack(video_track);
436 }
deadbeefab9b2d12015-10-14 11:33:11 -0700437
438 local_collection->AddStream(stream);
439 }
440 return local_collection;
441}
442
443// Check equality of StreamCollections.
444bool CompareStreamCollections(StreamCollectionInterface* s1,
445 StreamCollectionInterface* s2) {
446 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
447 return false;
448 }
449
450 for (size_t i = 0; i != s1->count(); ++i) {
451 if (s1->at(i)->label() != s2->at(i)->label()) {
452 return false;
453 }
454 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
455 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
456 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
457 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
458
459 if (audio_tracks1.size() != audio_tracks2.size()) {
460 return false;
461 }
462 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
463 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
464 return false;
465 }
466 }
467 if (video_tracks1.size() != video_tracks2.size()) {
468 return false;
469 }
470 for (size_t j = 0; j != video_tracks1.size(); ++j) {
471 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
472 return false;
473 }
474 }
475 }
476 return true;
477}
478
perkjd61bf802016-03-24 03:16:19 -0700479// Helper class to test Observer.
480class MockTrackObserver : public ObserverInterface {
481 public:
482 explicit MockTrackObserver(NotifierInterface* notifier)
483 : notifier_(notifier) {
484 notifier_->RegisterObserver(this);
485 }
486
487 ~MockTrackObserver() { Unregister(); }
488
489 void Unregister() {
490 if (notifier_) {
491 notifier_->UnregisterObserver(this);
492 notifier_ = nullptr;
493 }
494 }
495
496 MOCK_METHOD0(OnChanged, void());
497
498 private:
499 NotifierInterface* notifier_;
500};
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502class MockPeerConnectionObserver : public PeerConnectionObserver {
503 public:
kjellander71a1b612016-11-07 01:18:08 -0800504 // We need these using declarations because there are two versions of each of
505 // the below methods and we only override one of them.
506 // TODO(deadbeef): Remove once there's only one version of the methods.
507 using PeerConnectionObserver::OnAddStream;
508 using PeerConnectionObserver::OnRemoveStream;
509 using PeerConnectionObserver::OnDataChannel;
510
deadbeefab9b2d12015-10-14 11:33:11 -0700511 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200512 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 }
514 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
515 pc_ = pc;
516 if (pc) {
517 state_ = pc_->signaling_state();
518 }
519 }
nisseef8b61e2016-04-29 06:09:15 -0700520 void OnSignalingChange(
521 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 EXPECT_EQ(pc_->signaling_state(), new_state);
523 state_ = new_state;
524 }
525 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
526 virtual void OnStateChange(StateType state_changed) {
527 if (pc_.get() == NULL)
528 return;
529 switch (state_changed) {
530 case kSignalingState:
531 // OnSignalingChange and OnStateChange(kSignalingState) should always
532 // be called approximately simultaneously. To ease testing, we require
533 // that they always be called in that order. This check verifies
534 // that OnSignalingChange has just been called.
535 EXPECT_EQ(pc_->signaling_state(), state_);
536 break;
537 case kIceState:
538 ADD_FAILURE();
539 break;
540 default:
541 ADD_FAILURE();
542 break;
543 }
544 }
deadbeefab9b2d12015-10-14 11:33:11 -0700545
546 MediaStreamInterface* RemoteStream(const std::string& label) {
547 return remote_streams_->find(label);
548 }
549 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700550 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700552 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700554 void OnRemoveStream(
555 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700557 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 }
perkjdfb769d2016-02-09 03:09:43 -0800559 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700560 void OnDataChannel(
561 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 last_datachannel_ = data_channel;
563 }
564
perkjdfb769d2016-02-09 03:09:43 -0800565 void OnIceConnectionChange(
566 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800568 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 }
perkjdfb769d2016-02-09 03:09:43 -0800570 void OnIceGatheringChange(
571 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800573 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800574 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 }
perkjdfb769d2016-02-09 03:09:43 -0800576 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
578 pc_->ice_gathering_state());
579
580 std::string sdp;
581 EXPECT_TRUE(candidate->ToString(&sdp));
582 EXPECT_LT(0u, sdp.size());
583 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
584 candidate->sdp_mline_index(), sdp, NULL));
585 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800586 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700587 }
588
589 void OnIceCandidatesRemoved(
590 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800591 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700592 }
593
594 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800595 callback_triggered_ = true;
596 }
597
zhihuangc63b8942016-12-02 15:41:10 -0800598 void OnAddTrack(
599 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
600 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
601 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800602 EXPECT_TRUE(receiver != nullptr);
603 num_added_tracks_++;
604 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
607 // Returns the label of the last added stream.
608 // Empty string if no stream have been added.
609 std::string GetLastAddedStreamLabel() {
610 if (last_added_stream_.get())
611 return last_added_stream_->label();
612 return "";
613 }
614 std::string GetLastRemovedStreamLabel() {
615 if (last_removed_stream_.get())
616 return last_removed_stream_->label();
617 return "";
618 }
619
zhihuang9763d562016-08-05 11:14:50 -0700620 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700622 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700623 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700624 rtc::scoped_refptr<StreamCollection> remote_streams_;
625 bool renegotiation_needed_ = false;
626 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800627 bool callback_triggered_ = false;
628 int num_added_tracks_ = 0;
629 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
631 private:
zhihuang9763d562016-08-05 11:14:50 -0700632 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
633 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634};
635
636} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700637
zhihuang29ff8442016-07-27 11:07:25 -0700638// The PeerConnectionMediaConfig tests below verify that configuration
639// and constraints are propagated into the MediaConfig passed to
640// CreateMediaController. These settings are intended for MediaChannel
641// constructors, but that is not exercised by these unittest.
642class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
643 public:
644 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700645 const cricket::MediaConfig& config,
646 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700647 create_media_controller_called_ = true;
648 create_media_controller_config_ = config;
649
650 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700651 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700652 EXPECT_TRUE(mc != nullptr);
653 return mc;
654 }
655
656 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700657 cricket::PortAllocator* port_allocator,
658 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700659 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700660 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
661 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700662 return transport_controller;
663 }
664
665 cricket::TransportController* transport_controller;
666 // Mutable, so they can be modified in the above const-declared method.
667 mutable bool create_media_controller_called_ = false;
668 mutable cricket::MediaConfig create_media_controller_config_;
669};
670
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671class PeerConnectionInterfaceTest : public testing::Test {
672 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800673 PeerConnectionInterfaceTest() {
674#ifdef WEBRTC_ANDROID
675 webrtc::InitializeAndroidObjects();
676#endif
677 }
678
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 virtual void SetUp() {
680 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700681 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
682 nullptr, nullptr, nullptr);
683 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700684 pc_factory_for_test_ =
685 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
686 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 }
688
689 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700690 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 }
692
deadbeef293e9262017-01-11 12:28:30 -0800693 // DTLS does not work in a loopback call, so is disabled for most of the
694 // tests in this file.
695 void CreatePeerConnectionWithoutDtls() {
696 FakeConstraints no_dtls_constraints;
697 no_dtls_constraints.AddMandatory(
698 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
699
700 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
701 &no_dtls_constraints);
702 }
703
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700705 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
706 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709 void CreatePeerConnectionWithIceTransportsType(
710 PeerConnectionInterface::IceTransportsType type) {
711 PeerConnectionInterface::RTCConfiguration config;
712 config.type = type;
713 return CreatePeerConnection(config, nullptr);
714 }
715
716 void CreatePeerConnectionWithIceServer(const std::string& uri,
717 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720 server.uri = uri;
721 server.password = password;
722 config.servers.push_back(server);
723 CreatePeerConnection(config, nullptr);
724 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
727 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700728 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
730 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000731
Henrik Boströmd79599d2016-06-01 13:58:50 +0200732 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000733 bool dtls;
734 if (FindConstraint(constraints,
735 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
736 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200737 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200738 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000739 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200740 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800741 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200742 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 ASSERT_TRUE(pc_.get() != NULL);
744 observer_.SetPeerConnectionInterface(pc_.get());
745 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
746 }
747
deadbeef0a6c4ca2015-10-06 11:38:28 -0700748 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700750 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700751 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800752 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700753
zhihuang9763d562016-08-05 11:14:50 -0700754 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700755 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
756 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800757 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700758 }
759
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700761 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800762 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
763 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
764 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800766 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767
deadbeef0a6c4ca2015-10-06 11:38:28 -0700768 CreatePeerConnectionExpectFail(kStunInvalidPort);
769 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
770 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700772 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
774 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800778 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 }
782
783 void ReleasePeerConnection() {
784 pc_ = NULL;
785 observer_.SetPeerConnectionInterface(NULL);
786 }
787
deadbeefab9b2d12015-10-14 11:33:11 -0700788 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700790 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700792 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700794 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 pc_factory_->CreateVideoTrack(label + "v0", video_source));
796 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
802 void AddVoiceStream(const std::string& label) {
803 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700804 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700806 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 pc_factory_->CreateAudioTrack(label + "a0", NULL));
808 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000809 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
811 observer_.renegotiation_needed_ = false;
812 }
813
814 void AddAudioVideoStream(const std::string& stream_label,
815 const std::string& audio_track_label,
816 const std::string& video_track_label) {
817 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700818 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700820 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 pc_factory_->CreateAudioTrack(
822 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
823 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700824 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700825 pc_factory_->CreateVideoTrack(
826 video_track_label,
827 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000829 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
831 observer_.renegotiation_needed_ = false;
832 }
833
kwibergd1fe2812016-04-27 06:47:29 -0700834 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700835 bool offer,
836 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000837 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
838 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 MockCreateSessionDescriptionObserver>());
840 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700841 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700843 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 }
845 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700846 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 return observer->result();
848 }
849
kwibergd1fe2812016-04-27 06:47:29 -0700850 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700851 MediaConstraintsInterface* constraints) {
852 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
kwibergd1fe2812016-04-27 06:47:29 -0700855 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700856 MediaConstraintsInterface* constraints) {
857 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859
860 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
862 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 MockSetSessionDescriptionObserver>());
864 if (local) {
865 pc_->SetLocalDescription(observer, desc);
866 } else {
867 pc_->SetRemoteDescription(observer, desc);
868 }
zhihuang29ff8442016-07-27 11:07:25 -0700869 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
870 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
871 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 return observer->result();
873 }
874
875 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
876 return DoSetSessionDescription(desc, true);
877 }
878
879 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
880 return DoSetSessionDescription(desc, false);
881 }
882
883 // Calls PeerConnection::GetStats and check the return value.
884 // It does not verify the values in the StatReports since a RTCP packet might
885 // be required.
886 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000887 rtc::scoped_refptr<MockStatsObserver> observer(
888 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000889 if (!pc_->GetStats(
890 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 return false;
892 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
893 return observer->called();
894 }
895
896 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800897 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 // Create a local stream with audio&video tracks.
899 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
900 CreateOfferReceiveAnswer();
901 }
902
903 // Verify that RTP Header extensions has been negotiated for audio and video.
904 void VerifyRemoteRtpHeaderExtensions() {
905 const cricket::MediaContentDescription* desc =
906 cricket::GetFirstAudioContentDescription(
907 pc_->remote_description()->description());
908 ASSERT_TRUE(desc != NULL);
909 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
910
911 desc = cricket::GetFirstVideoContentDescription(
912 pc_->remote_description()->description());
913 ASSERT_TRUE(desc != NULL);
914 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
915 }
916
917 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700918 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700919 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 std::string sdp;
921 EXPECT_TRUE(offer->ToString(&sdp));
922 SessionDescriptionInterface* remote_offer =
923 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
924 sdp, NULL);
925 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
926 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
927 }
928
deadbeefab9b2d12015-10-14 11:33:11 -0700929 void CreateAndSetRemoteOffer(const std::string& sdp) {
930 SessionDescriptionInterface* remote_offer =
931 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
932 sdp, nullptr);
933 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
934 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
935 }
936
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700938 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700939 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940
941 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
942 // audio codec change, even if the parameter has nothing to do with
943 // receiving. Not all parameters are serialized to SDP.
944 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
945 // the SessionDescription, it is necessary to do that here to in order to
946 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
947 // https://code.google.com/p/webrtc/issues/detail?id=1356
948 std::string sdp;
949 EXPECT_TRUE(answer->ToString(&sdp));
950 SessionDescriptionInterface* new_answer =
951 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
952 sdp, NULL);
953 EXPECT_TRUE(DoSetLocalDescription(new_answer));
954 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
955 }
956
957 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700958 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700959 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960
961 std::string sdp;
962 EXPECT_TRUE(answer->ToString(&sdp));
963 SessionDescriptionInterface* pr_answer =
964 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
965 sdp, NULL);
966 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
967 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
968 }
969
970 void CreateOfferReceiveAnswer() {
971 CreateOfferAsLocalDescription();
972 std::string sdp;
973 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
974 CreateAnswerAsRemoteDescription(sdp);
975 }
976
977 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700978 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700979 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
981 // audio codec change, even if the parameter has nothing to do with
982 // receiving. Not all parameters are serialized to SDP.
983 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
984 // the SessionDescription, it is necessary to do that here to in order to
985 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
986 // https://code.google.com/p/webrtc/issues/detail?id=1356
987 std::string sdp;
988 EXPECT_TRUE(offer->ToString(&sdp));
989 SessionDescriptionInterface* new_offer =
990 webrtc::CreateSessionDescription(
991 SessionDescriptionInterface::kOffer,
992 sdp, NULL);
993
994 EXPECT_TRUE(DoSetLocalDescription(new_offer));
995 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000996 // Wait for the ice_complete message, so that SDP will have candidates.
997 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 }
999
deadbeefab9b2d12015-10-14 11:33:11 -07001000 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1002 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001003 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 EXPECT_TRUE(DoSetRemoteDescription(answer));
1005 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1006 }
1007
deadbeefab9b2d12015-10-14 11:33:11 -07001008 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 webrtc::JsepSessionDescription* pr_answer =
1010 new webrtc::JsepSessionDescription(
1011 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001012 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1014 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1015 webrtc::JsepSessionDescription* answer =
1016 new webrtc::JsepSessionDescription(
1017 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001018 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 EXPECT_TRUE(DoSetRemoteDescription(answer));
1020 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1021 }
1022
1023 // Help function used for waiting until a the last signaled remote stream has
1024 // the same label as |stream_label|. In a few of the tests in this file we
1025 // answer with the same session description as we offer and thus we can
1026 // check if OnAddStream have been called with the same stream as we offer to
1027 // send.
1028 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1029 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1030 }
1031
1032 // Creates an offer and applies it as a local session description.
1033 // Creates an answer with the same SDP an the offer but removes all lines
1034 // that start with a:ssrc"
1035 void CreateOfferReceiveAnswerWithoutSsrc() {
1036 CreateOfferAsLocalDescription();
1037 std::string sdp;
1038 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1039 SetSsrcToZero(&sdp);
1040 CreateAnswerAsRemoteDescription(sdp);
1041 }
1042
deadbeefab9b2d12015-10-14 11:33:11 -07001043 // This function creates a MediaStream with label kStreams[0] and
1044 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1045 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001046 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001047 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001048 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001049 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1050 size_t number_of_video_tracks) {
1051 EXPECT_LE(number_of_audio_tracks, 2u);
1052 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001053
1054 reference_collection_ = StreamCollection::Create();
1055 std::string sdp_ms1 = std::string(kSdpStringInit);
1056
1057 std::string mediastream_label = kStreams[0];
1058
1059 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1060 webrtc::MediaStream::Create(mediastream_label));
1061 reference_collection_->AddStream(stream);
1062
1063 if (number_of_audio_tracks > 0) {
1064 sdp_ms1 += std::string(kSdpStringAudio);
1065 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1066 AddAudioTrack(kAudioTracks[0], stream);
1067 }
1068 if (number_of_audio_tracks > 1) {
1069 sdp_ms1 += kSdpStringMs1Audio1;
1070 AddAudioTrack(kAudioTracks[1], stream);
1071 }
1072
1073 if (number_of_video_tracks > 0) {
1074 sdp_ms1 += std::string(kSdpStringVideo);
1075 sdp_ms1 += std::string(kSdpStringMs1Video0);
1076 AddVideoTrack(kVideoTracks[0], stream);
1077 }
1078 if (number_of_video_tracks > 1) {
1079 sdp_ms1 += kSdpStringMs1Video1;
1080 AddVideoTrack(kVideoTracks[1], stream);
1081 }
1082
kwibergd1fe2812016-04-27 06:47:29 -07001083 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001084 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1085 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001086 }
1087
1088 void AddAudioTrack(const std::string& track_id,
1089 MediaStreamInterface* stream) {
1090 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1091 webrtc::AudioTrack::Create(track_id, nullptr));
1092 ASSERT_TRUE(stream->AddTrack(audio_track));
1093 }
1094
1095 void AddVideoTrack(const std::string& track_id,
1096 MediaStreamInterface* stream) {
1097 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001098 webrtc::VideoTrack::Create(track_id,
1099 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001100 ASSERT_TRUE(stream->AddTrack(video_track));
1101 }
1102
kwibergfd8be342016-05-14 19:44:11 -07001103 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001104 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001105 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001106 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001107 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1108 return offer;
1109 }
1110
kwibergfd8be342016-05-14 19:44:11 -07001111 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001112 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001113 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001114 CreateOfferWithOneAudioStream();
1115 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001116 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001117 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1118 return answer;
1119 }
1120
1121 const std::string& GetFirstAudioStreamCname(
1122 const SessionDescriptionInterface* desc) {
1123 const cricket::ContentInfo* audio_content =
1124 cricket::GetFirstAudioContent(desc->description());
1125 const cricket::AudioContentDescription* audio_desc =
1126 static_cast<const cricket::AudioContentDescription*>(
1127 audio_content->description);
1128 return audio_desc->streams()[0].cname;
1129 }
1130
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001131 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001132 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1133 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1134 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001136 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137};
1138
zhihuang29ff8442016-07-27 11:07:25 -07001139// Test that no callbacks on the PeerConnectionObserver are called after the
1140// PeerConnection is closed.
1141TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001142 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001143 pc_factory_for_test_->CreatePeerConnection(
1144 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1145 nullptr, &observer_));
1146 observer_.SetPeerConnectionInterface(pc.get());
1147 pc->Close();
1148
1149 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001150 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001151 std::vector<cricket::Candidate> candidates;
1152 pc_factory_for_test_->transport_controller->SignalGatheringState(
1153 cricket::IceGatheringState{});
1154 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1155 "", candidates);
1156 pc_factory_for_test_->transport_controller->SignalConnectionState(
1157 cricket::IceConnectionState{});
1158 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1159 candidates);
1160 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001161 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001162}
1163
zhihuang8f65cdf2016-05-06 18:40:30 -07001164// Generate different CNAMEs when PeerConnections are created.
1165// The CNAMEs are expected to be generated randomly. It is possible
1166// that the test fails, though the possibility is very low.
1167TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001169 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateOfferWithOneAudioStream();
1172 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1173 GetFirstAudioStreamCname(offer2.get()));
1174}
1175
1176TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream();
1181 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1182 GetFirstAudioStreamCname(answer2.get()));
1183}
1184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185TEST_F(PeerConnectionInterfaceTest,
1186 CreatePeerConnectionWithDifferentConfigurations) {
1187 CreatePeerConnectionWithDifferentConfigurations();
1188}
1189
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001190TEST_F(PeerConnectionInterfaceTest,
1191 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1192 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1193 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1194 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1195 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1197 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1198 port_allocator_->candidate_filter());
1199 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1200 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1201}
1202
1203// Test that when a PeerConnection is created with a nonzero candidate pool
1204// size, the pooled PortAllocatorSession is created with all the attributes
1205// in the RTCConfiguration.
1206TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1207 PeerConnectionInterface::RTCConfiguration config;
1208 PeerConnectionInterface::IceServer server;
1209 server.uri = kStunAddressOnly;
1210 config.servers.push_back(server);
1211 config.type = PeerConnectionInterface::kRelay;
1212 config.disable_ipv6 = true;
1213 config.tcp_candidate_policy =
1214 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001215 config.candidate_network_policy =
1216 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001217 config.ice_candidate_pool_size = 1;
1218 CreatePeerConnection(config, nullptr);
1219
1220 const cricket::FakePortAllocatorSession* session =
1221 static_cast<const cricket::FakePortAllocatorSession*>(
1222 port_allocator_->GetPooledSession());
1223 ASSERT_NE(nullptr, session);
1224 EXPECT_EQ(1UL, session->stun_servers().size());
1225 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1226 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001227 EXPECT_LT(0U,
1228 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001229}
1230
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001231// Test that the PeerConnection initializes the port allocator passed into it,
1232// and on the correct thread.
1233TEST_F(PeerConnectionInterfaceTest,
1234 CreatePeerConnectionInitializesPortAllocator) {
1235 rtc::Thread network_thread;
1236 network_thread.Start();
1237 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1238 webrtc::CreatePeerConnectionFactory(
1239 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1240 nullptr, nullptr, nullptr));
1241 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1242 new cricket::FakePortAllocator(&network_thread, nullptr));
1243 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1244 PeerConnectionInterface::RTCConfiguration config;
1245 rtc::scoped_refptr<PeerConnectionInterface> pc(
1246 pc_factory->CreatePeerConnection(
1247 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1248 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1249 // so all we have to do here is check that it's initialized.
1250 EXPECT_TRUE(raw_port_allocator->initialized());
1251}
1252
deadbeef46c73892016-11-16 19:42:04 -08001253// Check that GetConfiguration returns the configuration the PeerConnection was
1254// constructed with, before SetConfiguration is called.
1255TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1256 PeerConnectionInterface::RTCConfiguration config;
1257 config.type = PeerConnectionInterface::kRelay;
1258 CreatePeerConnection(config, nullptr);
1259
1260 PeerConnectionInterface::RTCConfiguration returned_config =
1261 pc_->GetConfiguration();
1262 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1263}
1264
1265// Check that GetConfiguration returns the last configuration passed into
1266// SetConfiguration.
1267TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1268 CreatePeerConnection();
1269
1270 PeerConnectionInterface::RTCConfiguration config;
1271 config.type = PeerConnectionInterface::kRelay;
1272 EXPECT_TRUE(pc_->SetConfiguration(config));
1273
1274 PeerConnectionInterface::RTCConfiguration returned_config =
1275 pc_->GetConfiguration();
1276 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1277}
1278
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001280 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001281 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 AddVoiceStream(kStreamLabel2);
1283 ASSERT_EQ(2u, pc_->local_streams()->count());
1284
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001285 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001286 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001288 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1289 pc_factory_->CreateAudioTrack(kStreamLabel3,
1290 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001292 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001293 EXPECT_EQ(3u, pc_->local_streams()->count());
1294
1295 // Remove the third stream.
1296 pc_->RemoveStream(pc_->local_streams()->at(2));
1297 EXPECT_EQ(2u, pc_->local_streams()->count());
1298
1299 // Remove the second stream.
1300 pc_->RemoveStream(pc_->local_streams()->at(1));
1301 EXPECT_EQ(1u, pc_->local_streams()->count());
1302
1303 // Remove the first stream.
1304 pc_->RemoveStream(pc_->local_streams()->at(0));
1305 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306}
1307
deadbeefab9b2d12015-10-14 11:33:11 -07001308// Test that the created offer includes streams we added.
1309TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001310 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001311 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001312 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001313 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001314
1315 const cricket::ContentInfo* audio_content =
1316 cricket::GetFirstAudioContent(offer->description());
1317 const cricket::AudioContentDescription* audio_desc =
1318 static_cast<const cricket::AudioContentDescription*>(
1319 audio_content->description);
1320 EXPECT_TRUE(
1321 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1322
1323 const cricket::ContentInfo* video_content =
1324 cricket::GetFirstVideoContent(offer->description());
1325 const cricket::VideoContentDescription* video_desc =
1326 static_cast<const cricket::VideoContentDescription*>(
1327 video_content->description);
1328 EXPECT_TRUE(
1329 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1330
1331 // Add another stream and ensure the offer includes both the old and new
1332 // streams.
1333 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001334 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001335
1336 audio_content = cricket::GetFirstAudioContent(offer->description());
1337 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1338 audio_content->description);
1339 EXPECT_TRUE(
1340 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1341 EXPECT_TRUE(
1342 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1343
1344 video_content = cricket::GetFirstVideoContent(offer->description());
1345 video_desc = static_cast<const cricket::VideoContentDescription*>(
1346 video_content->description);
1347 EXPECT_TRUE(
1348 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1349 EXPECT_TRUE(
1350 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1351}
1352
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001354 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001355 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 ASSERT_EQ(1u, pc_->local_streams()->count());
1357 pc_->RemoveStream(pc_->local_streams()->at(0));
1358 EXPECT_EQ(0u, pc_->local_streams()->count());
1359}
1360
deadbeefe1f9d832016-01-14 15:35:42 -08001361// Test for AddTrack and RemoveTrack methods.
1362// Tests that the created offer includes tracks we added,
1363// and that the RtpSenders are created correctly.
1364// Also tests that RemoveTrack removes the tracks from subsequent offers.
1365TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001366 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001367 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001368 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001369 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1370 std::vector<MediaStreamInterface*> stream_list;
1371 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001372 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001373 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001374 rtc::scoped_refptr<VideoTrackInterface> video_track(
1375 pc_factory_->CreateVideoTrack(
1376 "video_track",
1377 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001378 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1379 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001380 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1381 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001382 EXPECT_EQ("audio_track", audio_sender->id());
1383 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001384 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1385 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001386 EXPECT_EQ("video_track", video_sender->id());
1387 EXPECT_EQ(video_track, video_sender->track());
1388
1389 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001390 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001391 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001392
1393 const cricket::ContentInfo* audio_content =
1394 cricket::GetFirstAudioContent(offer->description());
1395 const cricket::AudioContentDescription* audio_desc =
1396 static_cast<const cricket::AudioContentDescription*>(
1397 audio_content->description);
1398 EXPECT_TRUE(
1399 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1400
1401 const cricket::ContentInfo* video_content =
1402 cricket::GetFirstVideoContent(offer->description());
1403 const cricket::VideoContentDescription* video_desc =
1404 static_cast<const cricket::VideoContentDescription*>(
1405 video_content->description);
1406 EXPECT_TRUE(
1407 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1408
1409 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1410
1411 // Now try removing the tracks.
1412 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1413 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1414
1415 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001416 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001417
1418 audio_content = cricket::GetFirstAudioContent(offer->description());
1419 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1420 audio_content->description);
1421 EXPECT_FALSE(
1422 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1423
1424 video_content = cricket::GetFirstVideoContent(offer->description());
1425 video_desc = static_cast<const cricket::VideoContentDescription*>(
1426 video_content->description);
1427 EXPECT_FALSE(
1428 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1429
1430 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1431
1432 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1433 // should return false.
1434 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1435 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1436}
1437
1438// Test creating senders without a stream specified,
1439// expecting a random stream ID to be generated.
1440TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001441 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001442 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001443 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001444 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001445 rtc::scoped_refptr<VideoTrackInterface> video_track(
1446 pc_factory_->CreateVideoTrack(
1447 "video_track",
1448 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001449 auto audio_sender =
1450 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1451 auto video_sender =
1452 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1453 EXPECT_EQ("audio_track", audio_sender->id());
1454 EXPECT_EQ(audio_track, audio_sender->track());
1455 EXPECT_EQ("video_track", video_sender->id());
1456 EXPECT_EQ(video_track, video_sender->track());
1457 // If the ID is truly a random GUID, it should be infinitely unlikely they
1458 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001459 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001460}
1461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1463 InitiateCall();
1464 WaitAndVerifyOnAddStream(kStreamLabel1);
1465 VerifyRemoteRtpHeaderExtensions();
1466}
1467
1468TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001469 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001470 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 CreateOfferAsLocalDescription();
1472 std::string offer;
1473 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1474 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1475 WaitAndVerifyOnAddStream(kStreamLabel1);
1476}
1477
1478TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001479 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001480 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481
1482 CreateOfferAsRemoteDescription();
1483 CreateAnswerAsLocalDescription();
1484
1485 WaitAndVerifyOnAddStream(kStreamLabel1);
1486}
1487
1488TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001489 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001490 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491
1492 CreateOfferAsRemoteDescription();
1493 CreatePrAnswerAsLocalDescription();
1494 CreateAnswerAsLocalDescription();
1495
1496 WaitAndVerifyOnAddStream(kStreamLabel1);
1497}
1498
1499TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1500 InitiateCall();
1501 ASSERT_EQ(1u, pc_->remote_streams()->count());
1502 pc_->RemoveStream(pc_->local_streams()->at(0));
1503 CreateOfferReceiveAnswer();
1504 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001505 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 CreateOfferReceiveAnswer();
1507}
1508
1509// Tests that after negotiating an audio only call, the respondent can perform a
1510// renegotiation that removes the audio stream.
1511TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 AddVoiceStream(kStreamLabel1);
1514 CreateOfferAsRemoteDescription();
1515 CreateAnswerAsLocalDescription();
1516
1517 ASSERT_EQ(1u, pc_->remote_streams()->count());
1518 pc_->RemoveStream(pc_->local_streams()->at(0));
1519 CreateOfferReceiveAnswer();
1520 EXPECT_EQ(0u, pc_->remote_streams()->count());
1521}
1522
1523// Test that candidates are generated and that we can parse our own candidates.
1524TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001525 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526
1527 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1528 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001529 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001530 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001531 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001532 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533
1534 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001536 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001537 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538
1539 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1540 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1541
1542 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1543}
1544
deadbeefab9b2d12015-10-14 11:33:11 -07001545// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546// not unique.
1547TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001548 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001550 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001551 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001552 EXPECT_TRUE(offer);
1553 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554
1555 // Create a local stream with audio&video tracks having same label.
1556 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1557
1558 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001559 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560
1561 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001562 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001563 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564}
1565
1566// Test that we will get different SSRCs for each tracks in the offer and answer
1567// we created.
1568TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001569 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 // Create a local stream with audio&video tracks having different labels.
1571 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1572
1573 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001574 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001575 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 int audio_ssrc = 0;
1577 int video_ssrc = 0;
1578 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1579 &audio_ssrc));
1580 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1581 &video_ssrc));
1582 EXPECT_NE(audio_ssrc, video_ssrc);
1583
1584 // Test CreateAnswer
1585 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001586 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001587 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 audio_ssrc = 0;
1589 video_ssrc = 0;
1590 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1591 &audio_ssrc));
1592 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1593 &video_ssrc));
1594 EXPECT_NE(audio_ssrc, video_ssrc);
1595}
1596
deadbeefeb459812015-12-15 19:24:43 -08001597// Test that it's possible to call AddTrack on a MediaStream after adding
1598// the stream to a PeerConnection.
1599// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1600TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001601 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001602 // Create audio stream and add to PeerConnection.
1603 AddVoiceStream(kStreamLabel1);
1604 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1605
1606 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001607 rtc::scoped_refptr<VideoTrackInterface> video_track(
1608 pc_factory_->CreateVideoTrack(
1609 "video_label",
1610 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001611 stream->AddTrack(video_track.get());
1612
kwibergd1fe2812016-04-27 06:47:29 -07001613 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001614 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001615
1616 const cricket::MediaContentDescription* video_desc =
1617 cricket::GetFirstVideoContentDescription(offer->description());
1618 EXPECT_TRUE(video_desc != nullptr);
1619}
1620
1621// Test that it's possible to call RemoveTrack on a MediaStream after adding
1622// the stream to a PeerConnection.
1623// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1624TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001625 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001626 // Create audio/video stream and add to PeerConnection.
1627 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1628 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1629
1630 // Remove the video track.
1631 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1632
kwibergd1fe2812016-04-27 06:47:29 -07001633 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001634 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001635
1636 const cricket::MediaContentDescription* video_desc =
1637 cricket::GetFirstVideoContentDescription(offer->description());
1638 EXPECT_TRUE(video_desc == nullptr);
1639}
1640
deadbeefbd7d8f72015-12-18 16:58:44 -08001641// Test creating a sender with a stream ID, and ensure the ID is populated
1642// in the offer.
1643TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001644 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001645 pc_->CreateSender("video", kStreamLabel1);
1646
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001648 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001649
1650 const cricket::MediaContentDescription* video_desc =
1651 cricket::GetFirstVideoContentDescription(offer->description());
1652 ASSERT_TRUE(video_desc != nullptr);
1653 ASSERT_EQ(1u, video_desc->streams().size());
1654 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1655}
1656
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657// Test that we can specify a certain track that we want statistics about.
1658TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1659 InitiateCall();
1660 ASSERT_LT(0u, pc_->remote_streams()->count());
1661 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001662 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1664 EXPECT_TRUE(DoGetStats(remote_audio));
1665
1666 // Remove the stream. Since we are sending to our selves the local
1667 // and the remote stream is the same.
1668 pc_->RemoveStream(pc_->local_streams()->at(0));
1669 // Do a re-negotiation.
1670 CreateOfferReceiveAnswer();
1671
1672 ASSERT_EQ(0u, pc_->remote_streams()->count());
1673
1674 // Test that we still can get statistics for the old track. Even if it is not
1675 // sent any longer.
1676 EXPECT_TRUE(DoGetStats(remote_audio));
1677}
1678
1679// Test that we can get stats on a video track.
1680TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1681 InitiateCall();
1682 ASSERT_LT(0u, pc_->remote_streams()->count());
1683 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001684 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1686 EXPECT_TRUE(DoGetStats(remote_video));
1687}
1688
1689// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001690TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001692 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 pc_factory_->CreateAudioTrack("unknown track", NULL));
1694 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1695}
1696
1697// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 FakeConstraints constraints;
1700 constraints.SetAllowRtpDataChannels();
1701 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001702 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001704 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 pc_->CreateDataChannel("test2", NULL);
1706 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001707 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001709 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 new MockDataChannelObserver(data2));
1711
1712 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1713 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1714 std::string data_to_send1 = "testing testing";
1715 std::string data_to_send2 = "testing something else";
1716 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1717
1718 CreateOfferReceiveAnswer();
1719 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1720 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1721
1722 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1723 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1724 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1725 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1726
1727 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1728 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1729
1730 data1->Close();
1731 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1732 CreateOfferReceiveAnswer();
1733 EXPECT_FALSE(observer1->IsOpen());
1734 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1735 EXPECT_TRUE(observer2->IsOpen());
1736
1737 data_to_send2 = "testing something else again";
1738 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1739
1740 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1741}
1742
1743// This test verifies that sendnig binary data over RTP data channels should
1744// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 FakeConstraints constraints;
1747 constraints.SetAllowRtpDataChannels();
1748 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001749 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001751 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 pc_->CreateDataChannel("test2", NULL);
1753 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001754 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001756 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 new MockDataChannelObserver(data2));
1758
1759 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1760 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1761
1762 CreateOfferReceiveAnswer();
1763 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1764 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1765
1766 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1767 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1768
jbaucheec21bd2016-03-20 06:15:43 -07001769 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1771}
1772
1773// This test setup a RTP data channels in loop back and test that a channel is
1774// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 FakeConstraints constraints;
1777 constraints.SetAllowRtpDataChannels();
1778 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001779 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 new MockDataChannelObserver(data1));
1783
1784 CreateOfferReceiveAnswerWithoutSsrc();
1785
1786 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1787
1788 data1->Close();
1789 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1790 CreateOfferReceiveAnswerWithoutSsrc();
1791 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1792 EXPECT_FALSE(observer1->IsOpen());
1793}
1794
1795// This test that if a data channel is added in an answer a receive only channel
1796// channel is created.
1797TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1798 FakeConstraints constraints;
1799 constraints.SetAllowRtpDataChannels();
1800 CreatePeerConnection(&constraints);
1801
1802 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001803 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 pc_->CreateDataChannel(offer_label, NULL);
1805
1806 CreateOfferAsLocalDescription();
1807
1808 // Replace the data channel label in the offer and apply it as an answer.
1809 std::string receive_label = "answer_channel";
1810 std::string sdp;
1811 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001812 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 receive_label.c_str(), receive_label.length(),
1814 &sdp);
1815 CreateAnswerAsRemoteDescription(sdp);
1816
1817 // Verify that a new incoming data channel has been created and that
1818 // it is open but can't we written to.
1819 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1820 DataChannelInterface* received_channel = observer_.last_datachannel_;
1821 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1822 EXPECT_EQ(receive_label, received_channel->label());
1823 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1824
1825 // Verify that the channel we initially offered has been rejected.
1826 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1827
1828 // Do another offer / answer exchange and verify that the data channel is
1829 // opened.
1830 CreateOfferReceiveAnswer();
1831 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1832 kTimeout);
1833}
1834
1835// This test that no data channel is returned if a reliable channel is
1836// requested.
1837// TODO(perkj): Remove this test once reliable channels are implemented.
1838TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1839 FakeConstraints constraints;
1840 constraints.SetAllowRtpDataChannels();
1841 CreatePeerConnection(&constraints);
1842
1843 std::string label = "test";
1844 webrtc::DataChannelInit config;
1845 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001846 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 pc_->CreateDataChannel(label, &config);
1848 EXPECT_TRUE(channel == NULL);
1849}
1850
deadbeefab9b2d12015-10-14 11:33:11 -07001851// Verifies that duplicated label is not allowed for RTP data channel.
1852TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1853 FakeConstraints constraints;
1854 constraints.SetAllowRtpDataChannels();
1855 CreatePeerConnection(&constraints);
1856
1857 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001858 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001859 pc_->CreateDataChannel(label, nullptr);
1860 EXPECT_NE(channel, nullptr);
1861
zhihuang9763d562016-08-05 11:14:50 -07001862 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001863 pc_->CreateDataChannel(label, nullptr);
1864 EXPECT_EQ(dup_channel, nullptr);
1865}
1866
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867// This tests that a SCTP data channel is returned using different
1868// DataChannelInit configurations.
1869TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1870 FakeConstraints constraints;
1871 constraints.SetAllowDtlsSctpDataChannels();
1872 CreatePeerConnection(&constraints);
1873
1874 webrtc::DataChannelInit config;
1875
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_->CreateDataChannel("1", &config);
1878 EXPECT_TRUE(channel != NULL);
1879 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001880 EXPECT_TRUE(observer_.renegotiation_needed_);
1881 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882
1883 config.ordered = false;
1884 channel = pc_->CreateDataChannel("2", &config);
1885 EXPECT_TRUE(channel != NULL);
1886 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001887 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888
1889 config.ordered = true;
1890 config.maxRetransmits = 0;
1891 channel = pc_->CreateDataChannel("3", &config);
1892 EXPECT_TRUE(channel != NULL);
1893 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001894 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895
1896 config.maxRetransmits = -1;
1897 config.maxRetransmitTime = 0;
1898 channel = pc_->CreateDataChannel("4", &config);
1899 EXPECT_TRUE(channel != NULL);
1900 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001901 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902}
1903
1904// This tests that no data channel is returned if both maxRetransmits and
1905// maxRetransmitTime are set for SCTP data channels.
1906TEST_F(PeerConnectionInterfaceTest,
1907 CreateSctpDataChannelShouldFailForInvalidConfig) {
1908 FakeConstraints constraints;
1909 constraints.SetAllowDtlsSctpDataChannels();
1910 CreatePeerConnection(&constraints);
1911
1912 std::string label = "test";
1913 webrtc::DataChannelInit config;
1914 config.maxRetransmits = 0;
1915 config.maxRetransmitTime = 0;
1916
zhihuang9763d562016-08-05 11:14:50 -07001917 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 pc_->CreateDataChannel(label, &config);
1919 EXPECT_TRUE(channel == NULL);
1920}
1921
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922// The test verifies that creating a SCTP data channel with an id already in use
1923// or out of range should fail.
1924TEST_F(PeerConnectionInterfaceTest,
1925 CreateSctpDataChannelWithInvalidIdShouldFail) {
1926 FakeConstraints constraints;
1927 constraints.SetAllowDtlsSctpDataChannels();
1928 CreatePeerConnection(&constraints);
1929
1930 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001931 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001933 config.id = 1;
1934 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 EXPECT_TRUE(channel != NULL);
1936 EXPECT_EQ(1, channel->id());
1937
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 channel = pc_->CreateDataChannel("x", &config);
1939 EXPECT_TRUE(channel == NULL);
1940
1941 config.id = cricket::kMaxSctpSid;
1942 channel = pc_->CreateDataChannel("max", &config);
1943 EXPECT_TRUE(channel != NULL);
1944 EXPECT_EQ(config.id, channel->id());
1945
1946 config.id = cricket::kMaxSctpSid + 1;
1947 channel = pc_->CreateDataChannel("x", &config);
1948 EXPECT_TRUE(channel == NULL);
1949}
1950
deadbeefab9b2d12015-10-14 11:33:11 -07001951// Verifies that duplicated label is allowed for SCTP data channel.
1952TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1953 FakeConstraints constraints;
1954 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1955 true);
1956 CreatePeerConnection(&constraints);
1957
1958 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001959 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001960 pc_->CreateDataChannel(label, nullptr);
1961 EXPECT_NE(channel, nullptr);
1962
zhihuang9763d562016-08-05 11:14:50 -07001963 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001964 pc_->CreateDataChannel(label, nullptr);
1965 EXPECT_NE(dup_channel, nullptr);
1966}
1967
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001968// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1969// DataChannel.
1970TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1971 FakeConstraints constraints;
1972 constraints.SetAllowRtpDataChannels();
1973 CreatePeerConnection(&constraints);
1974
zhihuang9763d562016-08-05 11:14:50 -07001975 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001976 pc_->CreateDataChannel("test1", NULL);
1977 EXPECT_TRUE(observer_.renegotiation_needed_);
1978 observer_.renegotiation_needed_ = false;
1979
zhihuang9763d562016-08-05 11:14:50 -07001980 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001981 pc_->CreateDataChannel("test2", NULL);
1982 EXPECT_TRUE(observer_.renegotiation_needed_);
1983}
1984
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 FakeConstraints constraints;
1988 constraints.SetAllowRtpDataChannels();
1989 CreatePeerConnection(&constraints);
1990
zhihuang9763d562016-08-05 11:14:50 -07001991 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001993 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 pc_->CreateDataChannel("test2", NULL);
1995 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001996 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001998 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 new MockDataChannelObserver(data2));
2000
2001 CreateOfferReceiveAnswer();
2002 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2003 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2004
2005 ReleasePeerConnection();
2006 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2007 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2008}
2009
2010// This test that data channels can be rejected in an answer.
2011TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2012 FakeConstraints constraints;
2013 constraints.SetAllowRtpDataChannels();
2014 CreatePeerConnection(&constraints);
2015
zhihuang9763d562016-08-05 11:14:50 -07002016 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 pc_->CreateDataChannel("offer_channel", NULL));
2018
2019 CreateOfferAsLocalDescription();
2020
2021 // Create an answer where the m-line for data channels are rejected.
2022 std::string sdp;
2023 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2024 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2025 SessionDescriptionInterface::kAnswer);
2026 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2027 cricket::ContentInfo* data_info =
2028 answer->description()->GetContentByName("data");
2029 data_info->rejected = true;
2030
2031 DoSetRemoteDescription(answer);
2032 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2033}
2034
2035// Test that we can create a session description from an SDP string from
2036// FireFox, use it as a remote session description, generate an answer and use
2037// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002038TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 FakeConstraints constraints;
2040 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2041 true);
2042 CreatePeerConnection(&constraints);
2043 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2044 SessionDescriptionInterface* desc =
2045 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002046 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2048 CreateAnswerAsLocalDescription();
2049 ASSERT_TRUE(pc_->local_description() != NULL);
2050 ASSERT_TRUE(pc_->remote_description() != NULL);
2051
2052 const cricket::ContentInfo* content =
2053 cricket::GetFirstAudioContent(pc_->local_description()->description());
2054 ASSERT_TRUE(content != NULL);
2055 EXPECT_FALSE(content->rejected);
2056
2057 content =
2058 cricket::GetFirstVideoContent(pc_->local_description()->description());
2059 ASSERT_TRUE(content != NULL);
2060 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002061#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 content =
2063 cricket::GetFirstDataContent(pc_->local_description()->description());
2064 ASSERT_TRUE(content != NULL);
2065 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002066#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067}
2068
2069// Test that we can create an audio only offer and receive an answer with a
2070// limited set of audio codecs and receive an updated offer with more audio
2071// codecs, where the added codecs are not supported.
2072TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002073 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 AddVoiceStream("audio_label");
2075 CreateOfferAsLocalDescription();
2076
2077 SessionDescriptionInterface* answer =
2078 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002079 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2081
2082 SessionDescriptionInterface* updated_offer =
2083 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002084 webrtc::kAudioSdpWithUnsupportedCodecs,
2085 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2087 CreateAnswerAsLocalDescription();
2088}
2089
deadbeefc80741f2015-10-22 13:14:45 -07002090// Test that if we're receiving (but not sending) a track, subsequent offers
2091// will have m-lines with a=recvonly.
2092TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
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 should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002102 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002103 DoCreateOffer(&offer, nullptr);
2104
2105 const cricket::ContentInfo* video_content =
2106 cricket::GetFirstVideoContent(offer->description());
2107 const cricket::VideoContentDescription* video_desc =
2108 static_cast<const cricket::VideoContentDescription*>(
2109 video_content->description);
2110 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2111
2112 const cricket::ContentInfo* audio_content =
2113 cricket::GetFirstAudioContent(offer->description());
2114 const cricket::AudioContentDescription* audio_desc =
2115 static_cast<const cricket::AudioContentDescription*>(
2116 audio_content->description);
2117 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2118}
2119
2120// Test that if we're receiving (but not sending) a track, and the
2121// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2122// false, the generated m-lines will be a=inactive.
2123TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2124 FakeConstraints constraints;
2125 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2126 true);
2127 CreatePeerConnection(&constraints);
2128 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2129 CreateAnswerAsLocalDescription();
2130
2131 // At this point we should be receiving stream 1, but not sending anything.
2132 // A new offer would be recvonly, but we'll set the "no receive" constraints
2133 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002134 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002135 FakeConstraints offer_constraints;
2136 offer_constraints.AddMandatory(
2137 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2138 offer_constraints.AddMandatory(
2139 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2140 DoCreateOffer(&offer, &offer_constraints);
2141
2142 const cricket::ContentInfo* video_content =
2143 cricket::GetFirstVideoContent(offer->description());
2144 const cricket::VideoContentDescription* video_desc =
2145 static_cast<const cricket::VideoContentDescription*>(
2146 video_content->description);
2147 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2148
2149 const cricket::ContentInfo* audio_content =
2150 cricket::GetFirstAudioContent(offer->description());
2151 const cricket::AudioContentDescription* audio_desc =
2152 static_cast<const cricket::AudioContentDescription*>(
2153 audio_content->description);
2154 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2155}
2156
deadbeef653b8e02015-11-11 12:55:10 -08002157// Test that we can use SetConfiguration to change the ICE servers of the
2158// PortAllocator.
2159TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2160 CreatePeerConnection();
2161
2162 PeerConnectionInterface::RTCConfiguration config;
2163 PeerConnectionInterface::IceServer server;
2164 server.uri = "stun:test_hostname";
2165 config.servers.push_back(server);
2166 EXPECT_TRUE(pc_->SetConfiguration(config));
2167
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002168 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2169 EXPECT_EQ("test_hostname",
2170 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002171}
2172
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002173TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2174 CreatePeerConnection();
2175 PeerConnectionInterface::RTCConfiguration config;
2176 config.type = PeerConnectionInterface::kRelay;
2177 EXPECT_TRUE(pc_->SetConfiguration(config));
2178 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2179}
2180
deadbeef293e9262017-01-11 12:28:30 -08002181TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2182 PeerConnectionInterface::RTCConfiguration config;
2183 config.prune_turn_ports = false;
2184 CreatePeerConnection(config, nullptr);
2185 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2186
2187 config.prune_turn_ports = true;
2188 EXPECT_TRUE(pc_->SetConfiguration(config));
2189 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2190}
2191
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002192// Test that when SetConfiguration changes both the pool size and other
2193// attributes, the pooled session is created with the updated attributes.
2194TEST_F(PeerConnectionInterfaceTest,
2195 SetConfigurationCreatesPooledSessionCorrectly) {
2196 CreatePeerConnection();
2197 PeerConnectionInterface::RTCConfiguration config;
2198 config.ice_candidate_pool_size = 1;
2199 PeerConnectionInterface::IceServer server;
2200 server.uri = kStunAddressOnly;
2201 config.servers.push_back(server);
2202 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002203 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002204
2205 const cricket::FakePortAllocatorSession* session =
2206 static_cast<const cricket::FakePortAllocatorSession*>(
2207 port_allocator_->GetPooledSession());
2208 ASSERT_NE(nullptr, session);
2209 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002210}
2211
deadbeef293e9262017-01-11 12:28:30 -08002212// Test that after SetLocalDescription, changing the pool size is not allowed,
2213// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002214TEST_F(PeerConnectionInterfaceTest,
2215 CantChangePoolSizeAfterSetLocalDescription) {
2216 CreatePeerConnection();
2217 // Start by setting a size of 1.
2218 PeerConnectionInterface::RTCConfiguration config;
2219 config.ice_candidate_pool_size = 1;
2220 EXPECT_TRUE(pc_->SetConfiguration(config));
2221
2222 // Set remote offer; can still change pool size at this point.
2223 CreateOfferAsRemoteDescription();
2224 config.ice_candidate_pool_size = 2;
2225 EXPECT_TRUE(pc_->SetConfiguration(config));
2226
2227 // Set local answer; now it's too late.
2228 CreateAnswerAsLocalDescription();
2229 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002230 RTCError error;
2231 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2232 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2233}
2234
2235// Test that SetConfiguration returns an invalid modification error if
2236// modifying a field in the configuration that isn't allowed to be modified.
2237TEST_F(PeerConnectionInterfaceTest,
2238 SetConfigurationReturnsInvalidModificationError) {
2239 PeerConnectionInterface::RTCConfiguration config;
2240 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2241 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2242 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2243 CreatePeerConnection(config, nullptr);
2244
2245 PeerConnectionInterface::RTCConfiguration modified_config = config;
2246 modified_config.bundle_policy =
2247 PeerConnectionInterface::kBundlePolicyMaxBundle;
2248 RTCError error;
2249 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2250 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2251
2252 modified_config = config;
2253 modified_config.rtcp_mux_policy =
2254 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2255 error.set_type(RTCErrorType::NONE);
2256 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2257 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2258
2259 modified_config = config;
2260 modified_config.continual_gathering_policy =
2261 PeerConnectionInterface::GATHER_CONTINUALLY;
2262 error.set_type(RTCErrorType::NONE);
2263 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2264 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2265}
2266
2267// Test that SetConfiguration returns a range error if the candidate pool size
2268// is negative or larger than allowed by the spec.
2269TEST_F(PeerConnectionInterfaceTest,
2270 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2271 PeerConnectionInterface::RTCConfiguration config;
2272 CreatePeerConnection(config, nullptr);
2273
2274 config.ice_candidate_pool_size = -1;
2275 RTCError error;
2276 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2277 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2278
2279 config.ice_candidate_pool_size = INT_MAX;
2280 error.set_type(RTCErrorType::NONE);
2281 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2282 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2283}
2284
2285// Test that SetConfiguration returns a syntax error if parsing an ICE server
2286// URL failed.
2287TEST_F(PeerConnectionInterfaceTest,
2288 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2289 PeerConnectionInterface::RTCConfiguration config;
2290 CreatePeerConnection(config, nullptr);
2291
2292 PeerConnectionInterface::IceServer bad_server;
2293 bad_server.uri = "stunn:www.example.com";
2294 config.servers.push_back(bad_server);
2295 RTCError error;
2296 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2297 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2298}
2299
2300// Test that SetConfiguration returns an invalid parameter error if a TURN
2301// IceServer is missing a username or password.
2302TEST_F(PeerConnectionInterfaceTest,
2303 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2304 PeerConnectionInterface::RTCConfiguration config;
2305 CreatePeerConnection(config, nullptr);
2306
2307 PeerConnectionInterface::IceServer bad_server;
2308 bad_server.uri = "turn:www.example.com";
2309 // Missing password.
2310 bad_server.username = "foo";
2311 config.servers.push_back(bad_server);
2312 RTCError error;
2313 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2314 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002315}
2316
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317// Test that PeerConnection::Close changes the states to closed and all remote
2318// tracks change state to ended.
2319TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2320 // Initialize a PeerConnection and negotiate local and remote session
2321 // description.
2322 InitiateCall();
2323 ASSERT_EQ(1u, pc_->local_streams()->count());
2324 ASSERT_EQ(1u, pc_->remote_streams()->count());
2325
2326 pc_->Close();
2327
2328 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2329 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2330 pc_->ice_connection_state());
2331 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2332 pc_->ice_gathering_state());
2333
2334 EXPECT_EQ(1u, pc_->local_streams()->count());
2335 EXPECT_EQ(1u, pc_->remote_streams()->count());
2336
zhihuang9763d562016-08-05 11:14:50 -07002337 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2338 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002339 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002340 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002341 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2342 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2343 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344}
2345
2346// Test that PeerConnection methods fails gracefully after
2347// PeerConnection::Close has been called.
2348TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002349 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2351 CreateOfferAsRemoteDescription();
2352 CreateAnswerAsLocalDescription();
2353
2354 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002355 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 pc_->local_streams()->at(0);
2357
2358 pc_->Close();
2359
2360 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002361 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362
2363 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002364 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002366 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367
2368 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2369
2370 EXPECT_TRUE(pc_->local_description() != NULL);
2371 EXPECT_TRUE(pc_->remote_description() != NULL);
2372
kwibergd1fe2812016-04-27 06:47:29 -07002373 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002374 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002375 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002376 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377
2378 std::string sdp;
2379 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2380 SessionDescriptionInterface* remote_offer =
2381 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2382 sdp, NULL);
2383 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2384
2385 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2386 SessionDescriptionInterface* local_offer =
2387 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2388 sdp, NULL);
2389 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2390}
2391
2392// Test that GetStats can still be called after PeerConnection::Close.
2393TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2394 InitiateCall();
2395 pc_->Close();
2396 DoGetStats(NULL);
2397}
deadbeefab9b2d12015-10-14 11:33:11 -07002398
2399// NOTE: The series of tests below come from what used to be
2400// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2401// setting a remote or local description has the expected effects.
2402
2403// This test verifies that the remote MediaStreams corresponding to a received
2404// SDP string is created. In this test the two separate MediaStreams are
2405// signaled.
2406TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2407 FakeConstraints constraints;
2408 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2409 true);
2410 CreatePeerConnection(&constraints);
2411 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2412
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002413 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002414 EXPECT_TRUE(
2415 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2416 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2417 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2418
2419 // Create a session description based on another SDP with another
2420 // MediaStream.
2421 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2422
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002423 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002424 EXPECT_TRUE(
2425 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2426}
2427
2428// This test verifies that when remote tracks are added/removed from SDP, the
2429// created remote streams are updated appropriately.
2430TEST_F(PeerConnectionInterfaceTest,
2431 AddRemoveTrackFromExistingRemoteMediaStream) {
2432 FakeConstraints constraints;
2433 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2434 true);
2435 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002436 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002437 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002438 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2439 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2440 reference_collection_));
2441
2442 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002443 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002444 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002445 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2446 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2447 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002448 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002449 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2450 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002451 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002452 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2453 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002454
2455 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002456 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002457 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002458 MockTrackObserver audio_track_observer(audio_track2);
2459 MockTrackObserver video_track_observer(video_track2);
2460
2461 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2462 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002463 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2464 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2465 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002466 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002467 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002468 audio_track2->state(), kTimeout);
2469 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2470 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002471}
2472
2473// This tests that remote tracks are ended if a local session description is set
2474// that rejects the media content type.
2475TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2476 FakeConstraints constraints;
2477 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2478 true);
2479 CreatePeerConnection(&constraints);
2480 // First create and set a remote offer, then reject its video content in our
2481 // answer.
2482 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2483 ASSERT_EQ(1u, observer_.remote_streams()->count());
2484 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2485 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2486 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2487
2488 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2489 remote_stream->GetVideoTracks()[0];
2490 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2491 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2492 remote_stream->GetAudioTracks()[0];
2493 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2494
kwibergd1fe2812016-04-27 06:47:29 -07002495 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002496 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002497 cricket::ContentInfo* video_info =
2498 local_answer->description()->GetContentByName("video");
2499 video_info->rejected = true;
2500 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2501 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2502 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2503
2504 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002505 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002506 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002507 video_info = local_offer->description()->GetContentByName("video");
2508 ASSERT_TRUE(video_info != nullptr);
2509 video_info->rejected = true;
2510 cricket::ContentInfo* audio_info =
2511 local_offer->description()->GetContentByName("audio");
2512 ASSERT_TRUE(audio_info != nullptr);
2513 audio_info->rejected = true;
2514 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002515 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002516 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002517 remote_audio->state(), kTimeout);
2518 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2519 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002520}
2521
2522// This tests that we won't crash if the remote track has been removed outside
2523// of PeerConnection and then PeerConnection tries to reject the track.
2524TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2525 FakeConstraints constraints;
2526 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2527 true);
2528 CreatePeerConnection(&constraints);
2529 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2530 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2531 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2532 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2533
kwibergd1fe2812016-04-27 06:47:29 -07002534 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002535 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2536 kSdpStringWithStream1, nullptr));
2537 cricket::ContentInfo* video_info =
2538 local_answer->description()->GetContentByName("video");
2539 video_info->rejected = true;
2540 cricket::ContentInfo* audio_info =
2541 local_answer->description()->GetContentByName("audio");
2542 audio_info->rejected = true;
2543 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2544
2545 // No crash is a pass.
2546}
2547
deadbeef5e97fb52015-10-15 12:49:08 -07002548// This tests that if a recvonly remote description is set, no remote streams
2549// will be created, even if the description contains SSRCs/MSIDs.
2550// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2551TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2552 FakeConstraints constraints;
2553 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2554 true);
2555 CreatePeerConnection(&constraints);
2556
2557 std::string recvonly_offer = kSdpStringWithStream1;
2558 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2559 strlen(kRecvonly), &recvonly_offer);
2560 CreateAndSetRemoteOffer(recvonly_offer);
2561
2562 EXPECT_EQ(0u, observer_.remote_streams()->count());
2563}
2564
deadbeefab9b2d12015-10-14 11:33:11 -07002565// This tests that a default MediaStream is created if a remote session
2566// description doesn't contain any streams and no MSID support.
2567// It also tests that the default stream is updated if a video m-line is added
2568// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002569TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002570 FakeConstraints constraints;
2571 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2572 true);
2573 CreatePeerConnection(&constraints);
2574 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2575
2576 ASSERT_EQ(1u, observer_.remote_streams()->count());
2577 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2578
2579 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2580 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2581 EXPECT_EQ("default", remote_stream->label());
2582
2583 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2584 ASSERT_EQ(1u, observer_.remote_streams()->count());
2585 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2586 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002587 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2588 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002589 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2590 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002591 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2592 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002593}
2594
2595// This tests that a default MediaStream is created if a remote session
2596// description doesn't contain any streams and media direction is send only.
2597TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002598 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002599 FakeConstraints constraints;
2600 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2601 true);
2602 CreatePeerConnection(&constraints);
2603 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2604
2605 ASSERT_EQ(1u, observer_.remote_streams()->count());
2606 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2607
2608 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2609 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2610 EXPECT_EQ("default", remote_stream->label());
2611}
2612
2613// This tests that it won't crash when PeerConnection tries to remove
2614// a remote track that as already been removed from the MediaStream.
2615TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2616 FakeConstraints constraints;
2617 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2618 true);
2619 CreatePeerConnection(&constraints);
2620 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2621 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2622 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2623 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2624
2625 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2626
2627 // No crash is a pass.
2628}
2629
2630// This tests that a default MediaStream is created if the remote session
2631// description doesn't contain any streams and don't contain an indication if
2632// MSID is supported.
2633TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002634 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002635 FakeConstraints constraints;
2636 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2637 true);
2638 CreatePeerConnection(&constraints);
2639 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2640
2641 ASSERT_EQ(1u, observer_.remote_streams()->count());
2642 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2643 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2644 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2645}
2646
2647// This tests that a default MediaStream is not created if the remote session
2648// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002649TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002650 FakeConstraints constraints;
2651 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2652 true);
2653 CreatePeerConnection(&constraints);
2654 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2655 EXPECT_EQ(0u, observer_.remote_streams()->count());
2656}
2657
deadbeefbda7e0b2015-12-08 17:13:40 -08002658// This tests that when setting a new description, the old default tracks are
2659// not destroyed and recreated.
2660// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002661TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002662 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002663 FakeConstraints constraints;
2664 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2665 true);
2666 CreatePeerConnection(&constraints);
2667 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2668
2669 ASSERT_EQ(1u, observer_.remote_streams()->count());
2670 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2671 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2672
2673 // Set the track to "disabled", then set a new description and ensure the
2674 // track is still disabled, which ensures it hasn't been recreated.
2675 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2676 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2677 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2678 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2679}
2680
deadbeefab9b2d12015-10-14 11:33:11 -07002681// This tests that a default MediaStream is not created if a remote session
2682// description is updated to not have any MediaStreams.
2683TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2684 FakeConstraints constraints;
2685 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2686 true);
2687 CreatePeerConnection(&constraints);
2688 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002689 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002690 EXPECT_TRUE(
2691 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2692
2693 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2694 EXPECT_EQ(0u, observer_.remote_streams()->count());
2695}
2696
2697// This tests that an RtpSender is created when the local description is set
2698// after adding a local stream.
2699// TODO(deadbeef): This test and the one below it need to be updated when
2700// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002701TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002702 FakeConstraints constraints;
2703 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2704 true);
2705 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002706
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002707 // Create an offer with 1 stream with 2 tracks of each type.
2708 rtc::scoped_refptr<StreamCollection> stream_collection =
2709 CreateStreamCollection(1, 2);
2710 pc_->AddStream(stream_collection->at(0));
2711 std::unique_ptr<SessionDescriptionInterface> offer;
2712 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2713 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002714
deadbeefab9b2d12015-10-14 11:33:11 -07002715 auto senders = pc_->GetSenders();
2716 EXPECT_EQ(4u, senders.size());
2717 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2718 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2719 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2720 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2721
2722 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002723 pc_->RemoveStream(stream_collection->at(0));
2724 stream_collection = CreateStreamCollection(1, 1);
2725 pc_->AddStream(stream_collection->at(0));
2726 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2727 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2728
deadbeefab9b2d12015-10-14 11:33:11 -07002729 senders = pc_->GetSenders();
2730 EXPECT_EQ(2u, senders.size());
2731 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2732 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2733 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2734 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2735}
2736
2737// This tests that an RtpSender is created when the local description is set
2738// before adding a local stream.
2739TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002740 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002741 FakeConstraints constraints;
2742 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2743 true);
2744 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002745
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002746 rtc::scoped_refptr<StreamCollection> stream_collection =
2747 CreateStreamCollection(1, 2);
2748 // Add a stream to create the offer, but remove it afterwards.
2749 pc_->AddStream(stream_collection->at(0));
2750 std::unique_ptr<SessionDescriptionInterface> offer;
2751 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2752 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002753
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002754 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002755 auto senders = pc_->GetSenders();
2756 EXPECT_EQ(0u, senders.size());
2757
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002758 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002759 senders = pc_->GetSenders();
2760 EXPECT_EQ(4u, senders.size());
2761 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2762 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2763 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2764 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2765}
2766
2767// This tests that the expected behavior occurs if the SSRC on a local track is
2768// changed when SetLocalDescription is called.
2769TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002770 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002771 FakeConstraints constraints;
2772 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2773 true);
2774 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002775
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002776 rtc::scoped_refptr<StreamCollection> stream_collection =
2777 CreateStreamCollection(2, 1);
2778 pc_->AddStream(stream_collection->at(0));
2779 std::unique_ptr<SessionDescriptionInterface> offer;
2780 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2781 // Grab a copy of the offer before it gets passed into the PC.
2782 std::unique_ptr<JsepSessionDescription> modified_offer(
2783 new JsepSessionDescription(JsepSessionDescription::kOffer));
2784 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2785 offer->session_version());
2786 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002787
deadbeefab9b2d12015-10-14 11:33:11 -07002788 auto senders = pc_->GetSenders();
2789 EXPECT_EQ(2u, senders.size());
2790 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2791 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2792
2793 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002794 cricket::MediaContentDescription* desc =
2795 cricket::GetFirstAudioContentDescription(modified_offer->description());
2796 ASSERT_TRUE(desc != NULL);
2797 for (StreamParams& stream : desc->mutable_streams()) {
2798 for (unsigned int& ssrc : stream.ssrcs) {
2799 ++ssrc;
2800 }
2801 }
deadbeefab9b2d12015-10-14 11:33:11 -07002802
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002803 desc =
2804 cricket::GetFirstVideoContentDescription(modified_offer->description());
2805 ASSERT_TRUE(desc != NULL);
2806 for (StreamParams& stream : desc->mutable_streams()) {
2807 for (unsigned int& ssrc : stream.ssrcs) {
2808 ++ssrc;
2809 }
2810 }
2811
2812 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002813 senders = pc_->GetSenders();
2814 EXPECT_EQ(2u, senders.size());
2815 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2816 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2817 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2818 // changed.
2819}
2820
2821// This tests that the expected behavior occurs if a new session description is
2822// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002823TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002824 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002825 FakeConstraints constraints;
2826 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2827 true);
2828 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002829
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002830 rtc::scoped_refptr<StreamCollection> stream_collection =
2831 CreateStreamCollection(2, 1);
2832 pc_->AddStream(stream_collection->at(0));
2833 std::unique_ptr<SessionDescriptionInterface> offer;
2834 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2835 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002836
deadbeefab9b2d12015-10-14 11:33:11 -07002837 auto senders = pc_->GetSenders();
2838 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002839 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2840 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002841
2842 // Add a new MediaStream but with the same tracks as in the first stream.
2843 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2844 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002845 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2846 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002847 pc_->AddStream(stream_1);
2848
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002849 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2850 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002851
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002852 auto new_senders = pc_->GetSenders();
2853 // Should be the same senders as before, but with updated stream id.
2854 // Note that this behavior is subject to change in the future.
2855 // We may decide the PC should ignore existing tracks in AddStream.
2856 EXPECT_EQ(senders, new_senders);
2857 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2858 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002859}
2860
zhihuang81c3a032016-11-17 12:06:24 -08002861// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2862TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2863 FakeConstraints constraints;
2864 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2865 true);
2866 CreatePeerConnection(&constraints);
2867 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2868 EXPECT_EQ(observer_.num_added_tracks_, 1);
2869 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2870
2871 // Create and set the updated remote SDP.
2872 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2873 EXPECT_EQ(observer_.num_added_tracks_, 2);
2874 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2875}
2876
deadbeefd1a38b52016-12-10 13:15:33 -08002877// Test that when SetConfiguration is called and the configuration is
2878// changing, the next offer causes an ICE restart.
2879TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2880 PeerConnectionInterface::RTCConfiguration config;
2881 config.type = PeerConnectionInterface::kRelay;
2882 // Need to pass default constraints to prevent disabling of DTLS...
2883 FakeConstraints default_constraints;
2884 CreatePeerConnection(config, &default_constraints);
2885 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2886
2887 // Do initial offer/answer so there's something to restart.
2888 CreateOfferAsLocalDescription();
2889 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2890
2891 // Grab the ufrags.
2892 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2893
2894 // Change ICE policy, which should trigger an ICE restart on the next offer.
2895 config.type = PeerConnectionInterface::kAll;
2896 EXPECT_TRUE(pc_->SetConfiguration(config));
2897 CreateOfferAsLocalDescription();
2898
2899 // Grab the new ufrags.
2900 std::vector<std::string> subsequent_ufrags =
2901 GetUfrags(pc_->local_description());
2902
2903 // Sanity check.
2904 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2905 // Check that each ufrag is different.
2906 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2907 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2908 }
2909}
2910
2911// Test that when SetConfiguration is called and the configuration *isn't*
2912// changing, the next offer does *not* cause an ICE restart.
2913TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
2914 PeerConnectionInterface::RTCConfiguration config;
2915 config.type = PeerConnectionInterface::kRelay;
2916 // Need to pass default constraints to prevent disabling of DTLS...
2917 FakeConstraints default_constraints;
2918 CreatePeerConnection(config, &default_constraints);
2919 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2920
2921 // Do initial offer/answer so there's something to restart.
2922 CreateOfferAsLocalDescription();
2923 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2924
2925 // Grab the ufrags.
2926 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2927
2928 // Call SetConfiguration with a config identical to what the PC was
2929 // constructed with.
2930 EXPECT_TRUE(pc_->SetConfiguration(config));
2931 CreateOfferAsLocalDescription();
2932
2933 // Grab the new ufrags.
2934 std::vector<std::string> subsequent_ufrags =
2935 GetUfrags(pc_->local_description());
2936
2937 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
2938}
2939
2940// Test for a weird corner case scenario:
2941// 1. Audio/video session established.
2942// 2. SetConfiguration changes ICE config; ICE restart needed.
2943// 3. ICE restart initiated by remote peer, but only for one m= section.
2944// 4. Next createOffer should initiate an ICE restart, but only for the other
2945// m= section; it would be pointless to do an ICE restart for the m= section
2946// that was already restarted.
2947TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
2948 PeerConnectionInterface::RTCConfiguration config;
2949 config.type = PeerConnectionInterface::kRelay;
2950 // Need to pass default constraints to prevent disabling of DTLS...
2951 FakeConstraints default_constraints;
2952 CreatePeerConnection(config, &default_constraints);
2953 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2954
2955 // Do initial offer/answer so there's something to restart.
2956 CreateOfferAsLocalDescription();
2957 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2958
2959 // Change ICE policy, which should set the "needs-ice-restart" flag.
2960 config.type = PeerConnectionInterface::kAll;
2961 EXPECT_TRUE(pc_->SetConfiguration(config));
2962
2963 // Do ICE restart for the first m= section, initiated by remote peer.
2964 webrtc::JsepSessionDescription* remote_offer =
2965 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
2966 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
2967 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
2968 "modified";
2969 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
2970 CreateAnswerAsLocalDescription();
2971
2972 // Grab the ufrags.
2973 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2974 ASSERT_EQ(2, initial_ufrags.size());
2975
2976 // Create offer and grab the new ufrags.
2977 CreateOfferAsLocalDescription();
2978 std::vector<std::string> subsequent_ufrags =
2979 GetUfrags(pc_->local_description());
2980 ASSERT_EQ(2, subsequent_ufrags.size());
2981
2982 // Ensure that only the ufrag for the second m= section changed.
2983 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
2984 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
2985}
2986
deadbeeffe4a8a42016-12-20 17:56:17 -08002987// Tests that the methods to return current/pending descriptions work as
2988// expected at different points in the offer/answer exchange. This test does
2989// one offer/answer exchange as the offerer, then another as the answerer.
2990TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
2991 // This disables DTLS so we can apply an answer to ourselves.
2992 CreatePeerConnection();
2993
2994 // Create initial local offer and get SDP (which will also be used as
2995 // answer/pranswer);
2996 std::unique_ptr<SessionDescriptionInterface> offer;
2997 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2998 std::string sdp;
2999 EXPECT_TRUE(offer->ToString(&sdp));
3000
3001 // Set local offer.
3002 SessionDescriptionInterface* local_offer = offer.release();
3003 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3004 EXPECT_EQ(local_offer, pc_->pending_local_description());
3005 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3006 EXPECT_EQ(nullptr, pc_->current_local_description());
3007 EXPECT_EQ(nullptr, pc_->current_remote_description());
3008
3009 // Set remote pranswer.
3010 SessionDescriptionInterface* remote_pranswer =
3011 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3012 sdp, nullptr);
3013 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3014 EXPECT_EQ(local_offer, pc_->pending_local_description());
3015 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3016 EXPECT_EQ(nullptr, pc_->current_local_description());
3017 EXPECT_EQ(nullptr, pc_->current_remote_description());
3018
3019 // Set remote answer.
3020 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3021 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3022 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3023 EXPECT_EQ(nullptr, pc_->pending_local_description());
3024 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3025 EXPECT_EQ(local_offer, pc_->current_local_description());
3026 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3027
3028 // Set remote offer.
3029 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3030 SessionDescriptionInterface::kOffer, sdp, nullptr);
3031 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3032 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3033 EXPECT_EQ(nullptr, pc_->pending_local_description());
3034 EXPECT_EQ(local_offer, pc_->current_local_description());
3035 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3036
3037 // Set local pranswer.
3038 SessionDescriptionInterface* local_pranswer =
3039 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3040 sdp, nullptr);
3041 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3042 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3043 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3044 EXPECT_EQ(local_offer, pc_->current_local_description());
3045 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3046
3047 // Set local answer.
3048 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3049 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3050 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3051 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3052 EXPECT_EQ(nullptr, pc_->pending_local_description());
3053 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3054 EXPECT_EQ(local_answer, pc_->current_local_description());
3055}
3056
nisse51542be2016-02-12 02:27:06 -08003057class PeerConnectionMediaConfigTest : public testing::Test {
3058 protected:
3059 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003060 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003061 pcf_->Initialize();
3062 }
3063 const cricket::MediaConfig& TestCreatePeerConnection(
3064 const PeerConnectionInterface::RTCConfiguration& config,
3065 const MediaConstraintsInterface *constraints) {
3066 pcf_->create_media_controller_called_ = false;
3067
zhihuang9763d562016-08-05 11:14:50 -07003068 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3069 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003070 EXPECT_TRUE(pc.get());
3071 EXPECT_TRUE(pcf_->create_media_controller_called_);
3072 return pcf_->create_media_controller_config_;
3073 }
3074
zhihuang9763d562016-08-05 11:14:50 -07003075 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003076 MockPeerConnectionObserver observer_;
3077};
3078
3079// This test verifies the default behaviour with no constraints and a
3080// default RTCConfiguration.
3081TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3082 PeerConnectionInterface::RTCConfiguration config;
3083 FakeConstraints constraints;
3084
3085 const cricket::MediaConfig& media_config =
3086 TestCreatePeerConnection(config, &constraints);
3087
3088 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003089 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3090 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3091 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003092}
3093
3094// This test verifies the DSCP constraint is recognized and passed to
3095// the CreateMediaController call.
3096TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3097 PeerConnectionInterface::RTCConfiguration config;
3098 FakeConstraints constraints;
3099
3100 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3101 const cricket::MediaConfig& media_config =
3102 TestCreatePeerConnection(config, &constraints);
3103
3104 EXPECT_TRUE(media_config.enable_dscp);
3105}
3106
3107// This test verifies the cpu overuse detection constraint is
3108// recognized and passed to the CreateMediaController call.
3109TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3110 PeerConnectionInterface::RTCConfiguration config;
3111 FakeConstraints constraints;
3112
3113 constraints.AddOptional(
3114 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3115 const cricket::MediaConfig media_config =
3116 TestCreatePeerConnection(config, &constraints);
3117
nisse0db023a2016-03-01 04:29:59 -08003118 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003119}
3120
3121// This test verifies that the disable_prerenderer_smoothing flag is
3122// propagated from RTCConfiguration to the CreateMediaController call.
3123TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3124 PeerConnectionInterface::RTCConfiguration config;
3125 FakeConstraints constraints;
3126
Niels Möller71bdda02016-03-31 12:59:59 +02003127 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003128 const cricket::MediaConfig& media_config =
3129 TestCreatePeerConnection(config, &constraints);
3130
nisse0db023a2016-03-01 04:29:59 -08003131 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3132}
3133
3134// This test verifies the suspend below min bitrate constraint is
3135// recognized and passed to the CreateMediaController call.
3136TEST_F(PeerConnectionMediaConfigTest,
3137 TestSuspendBelowMinBitrateConstraintTrue) {
3138 PeerConnectionInterface::RTCConfiguration config;
3139 FakeConstraints constraints;
3140
3141 constraints.AddOptional(
3142 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3143 true);
3144 const cricket::MediaConfig media_config =
3145 TestCreatePeerConnection(config, &constraints);
3146
3147 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003148}
3149
deadbeefab9b2d12015-10-14 11:33:11 -07003150// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003151// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3152// "verify options are converted correctly", should be "pass options into
3153// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003154
3155TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3156 RTCOfferAnswerOptions rtc_options;
3157 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3158
3159 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003160 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003161
3162 rtc_options.offer_to_receive_audio =
3163 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003164 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003165}
3166
3167TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3168 RTCOfferAnswerOptions rtc_options;
3169 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3170
3171 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003172 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003173
3174 rtc_options.offer_to_receive_video =
3175 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003176 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003177}
3178
3179// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003180// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003181TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3182 RTCOfferAnswerOptions rtc_options;
3183 rtc_options.offer_to_receive_audio = 1;
3184 rtc_options.offer_to_receive_video = 1;
3185
3186 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003187 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003188 EXPECT_TRUE(options.has_audio());
3189 EXPECT_TRUE(options.has_video());
3190 EXPECT_TRUE(options.bundle_enabled);
3191}
3192
3193// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003194// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003195TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3196 RTCOfferAnswerOptions rtc_options;
3197 rtc_options.offer_to_receive_audio = 1;
3198
3199 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003200 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003201 EXPECT_TRUE(options.has_audio());
3202 EXPECT_FALSE(options.has_video());
3203 EXPECT_TRUE(options.bundle_enabled);
3204}
3205
3206// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003207// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003208TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3209 RTCOfferAnswerOptions rtc_options;
3210
3211 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003212 options.transport_options["audio"] = cricket::TransportOptions();
3213 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003214 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003215 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003216 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003217 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003218 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003219 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3220 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003221}
3222
3223// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003224// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003225TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3226 RTCOfferAnswerOptions rtc_options;
3227 rtc_options.offer_to_receive_audio = 0;
3228 rtc_options.offer_to_receive_video = 1;
3229
3230 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003231 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003232 EXPECT_FALSE(options.has_audio());
3233 EXPECT_TRUE(options.has_video());
3234 EXPECT_TRUE(options.bundle_enabled);
3235}
3236
3237// Test that a correct MediaSessionOptions is created for an offer if
3238// UseRtpMux is set to false.
3239TEST(CreateSessionOptionsTest,
3240 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3241 RTCOfferAnswerOptions rtc_options;
3242 rtc_options.offer_to_receive_audio = 1;
3243 rtc_options.offer_to_receive_video = 1;
3244 rtc_options.use_rtp_mux = false;
3245
3246 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003247 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003248 EXPECT_TRUE(options.has_audio());
3249 EXPECT_TRUE(options.has_video());
3250 EXPECT_FALSE(options.bundle_enabled);
3251}
3252
3253// Test that a correct MediaSessionOptions is created to restart ice if
3254// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003255// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003256TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3257 RTCOfferAnswerOptions rtc_options;
3258 rtc_options.ice_restart = true;
3259
3260 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003261 options.transport_options["audio"] = cricket::TransportOptions();
3262 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003263 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003264 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3265 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003266
3267 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003268 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003269 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3270 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003271}
3272
3273// Test that the MediaConstraints in an answer don't affect if audio and video
3274// is offered in an offer but that if kOfferToReceiveAudio or
3275// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3276// included in subsequent answers.
3277TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3278 FakeConstraints answer_c;
3279 answer_c.SetMandatoryReceiveAudio(true);
3280 answer_c.SetMandatoryReceiveVideo(true);
3281
3282 cricket::MediaSessionOptions answer_options;
3283 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3284 EXPECT_TRUE(answer_options.has_audio());
3285 EXPECT_TRUE(answer_options.has_video());
3286
deadbeefc80741f2015-10-22 13:14:45 -07003287 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003288
3289 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003290 EXPECT_TRUE(
3291 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003292 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003293 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003294
deadbeefc80741f2015-10-22 13:14:45 -07003295 RTCOfferAnswerOptions updated_rtc_offer_options;
3296 updated_rtc_offer_options.offer_to_receive_audio = 1;
3297 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003298
3299 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003300 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003301 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003302 EXPECT_TRUE(updated_offer_options.has_audio());
3303 EXPECT_TRUE(updated_offer_options.has_video());
3304
3305 // Since an offer has been created with both audio and video, subsequent
3306 // offers and answers should contain both audio and video.
3307 // Answers will only contain the media types that exist in the offer
3308 // regardless of the value of |updated_answer_options.has_audio| and
3309 // |updated_answer_options.has_video|.
3310 FakeConstraints updated_answer_c;
3311 answer_c.SetMandatoryReceiveAudio(false);
3312 answer_c.SetMandatoryReceiveVideo(false);
3313
3314 cricket::MediaSessionOptions updated_answer_options;
3315 EXPECT_TRUE(
3316 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3317 EXPECT_TRUE(updated_answer_options.has_audio());
3318 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003319}
deadbeef3edec7c2016-12-10 11:44:26 -08003320
deadbeef293e9262017-01-11 12:28:30 -08003321TEST(RTCErrorTypeTest, OstreamOperator) {
deadbeef3edec7c2016-12-10 11:44:26 -08003322 std::ostringstream oss;
deadbeef293e9262017-01-11 12:28:30 -08003323 oss << webrtc::RTCErrorType::NONE << ' '
3324 << webrtc::RTCErrorType::INVALID_PARAMETER << ' '
3325 << webrtc::RTCErrorType::INTERNAL_ERROR;
deadbeef3edec7c2016-12-10 11:44:26 -08003326 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str());
3327}
deadbeef293e9262017-01-11 12:28:30 -08003328
3329// Tests a few random fields being different.
3330TEST(RTCConfigurationTest, ComparisonOperators) {
3331 PeerConnectionInterface::RTCConfiguration a;
3332 PeerConnectionInterface::RTCConfiguration b;
3333 EXPECT_EQ(a, b);
3334
3335 PeerConnectionInterface::RTCConfiguration c;
3336 c.servers.push_back(PeerConnectionInterface::IceServer());
3337 EXPECT_NE(a, c);
3338
3339 PeerConnectionInterface::RTCConfiguration d;
3340 d.type = PeerConnectionInterface::kRelay;
3341 EXPECT_NE(a, d);
3342
3343 PeerConnectionInterface::RTCConfiguration e;
3344 e.audio_jitter_buffer_max_packets = 5;
3345 EXPECT_NE(a, e);
3346
3347 PeerConnectionInterface::RTCConfiguration f;
3348 f.ice_connection_receiving_timeout = 1337;
3349 EXPECT_NE(a, f);
3350
3351 PeerConnectionInterface::RTCConfiguration g;
3352 g.disable_ipv6 = true;
3353 EXPECT_NE(a, g);
3354
3355 PeerConnectionInterface::RTCConfiguration h(
3356 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3357 EXPECT_NE(a, h);
3358}