blob: 2da3755fcb7eef39f4491f2edfe1fe9790a5bb78 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296#define MAYBE_SKIP_TEST(feature) \
297 if (!(feature())) { \
298 LOG(LS_INFO) << "Feature disabled... skipping"; \
299 return; \
300 }
301
perkjd61bf802016-03-24 03:16:19 -0700302using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700303using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700305using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306using webrtc::AudioTrackInterface;
307using webrtc::DataBuffer;
308using webrtc::DataChannelInterface;
309using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700311using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700312using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700313using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314using webrtc::MediaStreamInterface;
315using webrtc::MediaStreamTrackInterface;
316using webrtc::MockCreateSessionDescriptionObserver;
317using webrtc::MockDataChannelObserver;
318using webrtc::MockSetSessionDescriptionObserver;
319using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700320using webrtc::NotifierInterface;
321using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322using webrtc::PeerConnectionInterface;
323using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800324using webrtc::RTCError;
325using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700326using webrtc::RtpReceiverInterface;
327using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::SdpParseError;
329using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700330using webrtc::StreamCollection;
331using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100332using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700333using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334using webrtc::VideoTrackInterface;
335
deadbeefab9b2d12015-10-14 11:33:11 -0700336typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338namespace {
339
340// Gets the first ssrc of given content type from the ContentInfo.
341bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
342 if (!content_info || !ssrc) {
343 return false;
344 }
345 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000346 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 content_info->description);
348 if (!media_desc || media_desc->streams().empty()) {
349 return false;
350 }
351 *ssrc = media_desc->streams().begin()->first_ssrc();
352 return true;
353}
354
deadbeefd1a38b52016-12-10 13:15:33 -0800355// Get the ufrags out of an SDP blob. Useful for testing ICE restart
356// behavior.
357std::vector<std::string> GetUfrags(
358 const webrtc::SessionDescriptionInterface* desc) {
359 std::vector<std::string> ufrags;
360 for (const cricket::TransportInfo& info :
361 desc->description()->transport_infos()) {
362 ufrags.push_back(info.description.ice_ufrag);
363 }
364 return ufrags;
365}
366
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367void SetSsrcToZero(std::string* sdp) {
368 const char kSdpSsrcAtribute[] = "a=ssrc:";
369 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
370 size_t ssrc_pos = 0;
371 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
372 std::string::npos) {
373 size_t end_ssrc = sdp->find(" ", ssrc_pos);
374 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
375 ssrc_pos = end_ssrc;
376 }
377}
378
deadbeefab9b2d12015-10-14 11:33:11 -0700379// Check if |streams| contains the specified track.
380bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
381 const std::string& stream_label,
382 const std::string& track_id) {
383 for (const cricket::StreamParams& params : streams) {
384 if (params.sync_label == stream_label && params.id == track_id) {
385 return true;
386 }
387 }
388 return false;
389}
390
391// Check if |senders| contains the specified sender, by id.
392bool ContainsSender(
393 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
394 const std::string& id) {
395 for (const auto& sender : senders) {
396 if (sender->id() == id) {
397 return true;
398 }
399 }
400 return false;
401}
402
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700403// Check if |senders| contains the specified sender, by id and stream id.
404bool ContainsSender(
405 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
406 const std::string& id,
407 const std::string& stream_id) {
408 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700409 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700410 return true;
411 }
412 }
413 return false;
414}
415
deadbeefab9b2d12015-10-14 11:33:11 -0700416// Create a collection of streams.
417// CreateStreamCollection(1) creates a collection that
418// correspond to kSdpStringWithStream1.
419// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
420rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700421 int number_of_streams,
422 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700423 rtc::scoped_refptr<StreamCollection> local_collection(
424 StreamCollection::Create());
425
426 for (int i = 0; i < number_of_streams; ++i) {
427 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
428 webrtc::MediaStream::Create(kStreams[i]));
429
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700430 for (int j = 0; j < tracks_per_stream; ++j) {
431 // Add a local audio track.
432 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
433 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
434 nullptr));
435 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700436
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700437 // Add a local video track.
438 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
439 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
440 webrtc::FakeVideoTrackSource::Create()));
441 stream->AddTrack(video_track);
442 }
deadbeefab9b2d12015-10-14 11:33:11 -0700443
444 local_collection->AddStream(stream);
445 }
446 return local_collection;
447}
448
449// Check equality of StreamCollections.
450bool CompareStreamCollections(StreamCollectionInterface* s1,
451 StreamCollectionInterface* s2) {
452 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
453 return false;
454 }
455
456 for (size_t i = 0; i != s1->count(); ++i) {
457 if (s1->at(i)->label() != s2->at(i)->label()) {
458 return false;
459 }
460 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
461 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
462 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
463 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
464
465 if (audio_tracks1.size() != audio_tracks2.size()) {
466 return false;
467 }
468 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
469 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
470 return false;
471 }
472 }
473 if (video_tracks1.size() != video_tracks2.size()) {
474 return false;
475 }
476 for (size_t j = 0; j != video_tracks1.size(); ++j) {
477 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
478 return false;
479 }
480 }
481 }
482 return true;
483}
484
perkjd61bf802016-03-24 03:16:19 -0700485// Helper class to test Observer.
486class MockTrackObserver : public ObserverInterface {
487 public:
488 explicit MockTrackObserver(NotifierInterface* notifier)
489 : notifier_(notifier) {
490 notifier_->RegisterObserver(this);
491 }
492
493 ~MockTrackObserver() { Unregister(); }
494
495 void Unregister() {
496 if (notifier_) {
497 notifier_->UnregisterObserver(this);
498 notifier_ = nullptr;
499 }
500 }
501
502 MOCK_METHOD0(OnChanged, void());
503
504 private:
505 NotifierInterface* notifier_;
506};
507
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508class MockPeerConnectionObserver : public PeerConnectionObserver {
509 public:
kjellander71a1b612016-11-07 01:18:08 -0800510 // We need these using declarations because there are two versions of each of
511 // the below methods and we only override one of them.
512 // TODO(deadbeef): Remove once there's only one version of the methods.
513 using PeerConnectionObserver::OnAddStream;
514 using PeerConnectionObserver::OnRemoveStream;
515 using PeerConnectionObserver::OnDataChannel;
516
deadbeefab9b2d12015-10-14 11:33:11 -0700517 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200518 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 }
520 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
521 pc_ = pc;
522 if (pc) {
523 state_ = pc_->signaling_state();
524 }
525 }
nisseef8b61e2016-04-29 06:09:15 -0700526 void OnSignalingChange(
527 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 EXPECT_EQ(pc_->signaling_state(), new_state);
529 state_ = new_state;
530 }
531 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
532 virtual void OnStateChange(StateType state_changed) {
533 if (pc_.get() == NULL)
534 return;
535 switch (state_changed) {
536 case kSignalingState:
537 // OnSignalingChange and OnStateChange(kSignalingState) should always
538 // be called approximately simultaneously. To ease testing, we require
539 // that they always be called in that order. This check verifies
540 // that OnSignalingChange has just been called.
541 EXPECT_EQ(pc_->signaling_state(), state_);
542 break;
543 case kIceState:
544 ADD_FAILURE();
545 break;
546 default:
547 ADD_FAILURE();
548 break;
549 }
550 }
deadbeefab9b2d12015-10-14 11:33:11 -0700551
552 MediaStreamInterface* RemoteStream(const std::string& label) {
553 return remote_streams_->find(label);
554 }
555 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700556 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700558 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700560 void OnRemoveStream(
561 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700563 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 }
perkjdfb769d2016-02-09 03:09:43 -0800565 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700566 void OnDataChannel(
567 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 last_datachannel_ = data_channel;
569 }
570
perkjdfb769d2016-02-09 03:09:43 -0800571 void OnIceConnectionChange(
572 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 EXPECT_EQ(pc_->ice_connection_state(), new_state);
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 OnIceGatheringChange(
577 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800579 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800580 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 }
perkjdfb769d2016-02-09 03:09:43 -0800582 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
584 pc_->ice_gathering_state());
585
586 std::string sdp;
587 EXPECT_TRUE(candidate->ToString(&sdp));
588 EXPECT_LT(0u, sdp.size());
589 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
590 candidate->sdp_mline_index(), sdp, NULL));
591 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800592 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700593 }
594
595 void OnIceCandidatesRemoved(
596 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800597 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700598 }
599
600 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800601 callback_triggered_ = true;
602 }
603
zhihuangc63b8942016-12-02 15:41:10 -0800604 void OnAddTrack(
605 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
606 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
607 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800608 EXPECT_TRUE(receiver != nullptr);
609 num_added_tracks_++;
610 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612
613 // Returns the label of the last added stream.
614 // Empty string if no stream have been added.
615 std::string GetLastAddedStreamLabel() {
616 if (last_added_stream_.get())
617 return last_added_stream_->label();
618 return "";
619 }
620 std::string GetLastRemovedStreamLabel() {
621 if (last_removed_stream_.get())
622 return last_removed_stream_->label();
623 return "";
624 }
625
zhihuang9763d562016-08-05 11:14:50 -0700626 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700628 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700629 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700630 rtc::scoped_refptr<StreamCollection> remote_streams_;
631 bool renegotiation_needed_ = false;
632 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800633 bool callback_triggered_ = false;
634 int num_added_tracks_ = 0;
635 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636
637 private:
zhihuang9763d562016-08-05 11:14:50 -0700638 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
639 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640};
641
642} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700643
zhihuang29ff8442016-07-27 11:07:25 -0700644// The PeerConnectionMediaConfig tests below verify that configuration
645// and constraints are propagated into the MediaConfig passed to
646// CreateMediaController. These settings are intended for MediaChannel
647// constructors, but that is not exercised by these unittest.
648class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
649 public:
650 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700651 const cricket::MediaConfig& config,
652 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700653 create_media_controller_called_ = true;
654 create_media_controller_config_ = config;
655
656 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700657 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700658 EXPECT_TRUE(mc != nullptr);
659 return mc;
660 }
661
662 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700663 cricket::PortAllocator* port_allocator,
664 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700665 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700666 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
667 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700668 return transport_controller;
669 }
670
671 cricket::TransportController* transport_controller;
672 // Mutable, so they can be modified in the above const-declared method.
673 mutable bool create_media_controller_called_ = false;
674 mutable cricket::MediaConfig create_media_controller_config_;
675};
676
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677class PeerConnectionInterfaceTest : public testing::Test {
678 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800679 PeerConnectionInterfaceTest() {
680#ifdef WEBRTC_ANDROID
681 webrtc::InitializeAndroidObjects();
682#endif
683 }
684
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 virtual void SetUp() {
686 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700687 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
688 nullptr, nullptr, nullptr);
689 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700690 pc_factory_for_test_ =
691 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
692 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 }
694
695 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700696 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
deadbeef293e9262017-01-11 12:28:30 -0800699 // DTLS does not work in a loopback call, so is disabled for most of the
700 // tests in this file.
701 void CreatePeerConnectionWithoutDtls() {
702 FakeConstraints no_dtls_constraints;
703 no_dtls_constraints.AddMandatory(
704 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
705
706 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
707 &no_dtls_constraints);
708 }
709
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700711 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
712 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700715 void CreatePeerConnectionWithIceTransportsType(
716 PeerConnectionInterface::IceTransportsType type) {
717 PeerConnectionInterface::RTCConfiguration config;
718 config.type = type;
719 return CreatePeerConnection(config, nullptr);
720 }
721
722 void CreatePeerConnectionWithIceServer(const std::string& uri,
723 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 server.uri = uri;
727 server.password = password;
728 config.servers.push_back(server);
729 CreatePeerConnection(config, nullptr);
730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700732 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
733 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700734 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800735 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
736 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000737
Henrik Boströmd79599d2016-06-01 13:58:50 +0200738 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000739 bool dtls;
740 if (FindConstraint(constraints,
741 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
742 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200743 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200744 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000745 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200746 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800747 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200748 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 ASSERT_TRUE(pc_.get() != NULL);
750 observer_.SetPeerConnectionInterface(pc_.get());
751 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
752 }
753
deadbeef0a6c4ca2015-10-06 11:38:28 -0700754 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700756 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700757 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700759
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700761 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
762 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700764 }
765
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700767 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
769 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
770 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773
deadbeef0a6c4ca2015-10-06 11:38:28 -0700774 CreatePeerConnectionExpectFail(kStunInvalidPort);
775 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
776 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700778 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
780 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800782 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800786 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 }
788
789 void ReleasePeerConnection() {
790 pc_ = NULL;
791 observer_.SetPeerConnectionInterface(NULL);
792 }
793
deadbeefab9b2d12015-10-14 11:33:11 -0700794 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700796 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700798 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700800 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 pc_factory_->CreateVideoTrack(label + "v0", video_source));
802 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000803 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
805 observer_.renegotiation_needed_ = false;
806 }
807
808 void AddVoiceStream(const std::string& label) {
809 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700810 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700812 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 pc_factory_->CreateAudioTrack(label + "a0", NULL));
814 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000815 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
817 observer_.renegotiation_needed_ = false;
818 }
819
820 void AddAudioVideoStream(const std::string& stream_label,
821 const std::string& audio_track_label,
822 const std::string& video_track_label) {
823 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700824 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700826 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 pc_factory_->CreateAudioTrack(
828 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
829 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700830 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700831 pc_factory_->CreateVideoTrack(
832 video_track_label,
833 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000835 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
837 observer_.renegotiation_needed_ = false;
838 }
839
kwibergd1fe2812016-04-27 06:47:29 -0700840 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700841 bool offer,
842 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000843 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
844 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 MockCreateSessionDescriptionObserver>());
846 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700847 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700849 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 }
851 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700852 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 return observer->result();
854 }
855
kwibergd1fe2812016-04-27 06:47:29 -0700856 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700857 MediaConstraintsInterface* constraints) {
858 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
kwibergd1fe2812016-04-27 06:47:29 -0700861 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700862 MediaConstraintsInterface* constraints) {
863 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
865
866 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000867 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
868 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 MockSetSessionDescriptionObserver>());
870 if (local) {
871 pc_->SetLocalDescription(observer, desc);
872 } else {
873 pc_->SetRemoteDescription(observer, desc);
874 }
zhihuang29ff8442016-07-27 11:07:25 -0700875 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
876 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
877 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 return observer->result();
879 }
880
881 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
882 return DoSetSessionDescription(desc, true);
883 }
884
885 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
886 return DoSetSessionDescription(desc, false);
887 }
888
889 // Calls PeerConnection::GetStats and check the return value.
890 // It does not verify the values in the StatReports since a RTCP packet might
891 // be required.
892 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000893 rtc::scoped_refptr<MockStatsObserver> observer(
894 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000895 if (!pc_->GetStats(
896 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 return false;
898 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
899 return observer->called();
900 }
901
902 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800903 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 // Create a local stream with audio&video tracks.
905 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
906 CreateOfferReceiveAnswer();
907 }
908
909 // Verify that RTP Header extensions has been negotiated for audio and video.
910 void VerifyRemoteRtpHeaderExtensions() {
911 const cricket::MediaContentDescription* desc =
912 cricket::GetFirstAudioContentDescription(
913 pc_->remote_description()->description());
914 ASSERT_TRUE(desc != NULL);
915 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
916
917 desc = cricket::GetFirstVideoContentDescription(
918 pc_->remote_description()->description());
919 ASSERT_TRUE(desc != NULL);
920 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
921 }
922
923 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700924 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700925 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 std::string sdp;
927 EXPECT_TRUE(offer->ToString(&sdp));
928 SessionDescriptionInterface* remote_offer =
929 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
930 sdp, NULL);
931 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
932 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
933 }
934
deadbeefab9b2d12015-10-14 11:33:11 -0700935 void CreateAndSetRemoteOffer(const std::string& sdp) {
936 SessionDescriptionInterface* remote_offer =
937 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
938 sdp, nullptr);
939 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
940 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
941 }
942
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700944 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700945 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946
947 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
948 // audio codec change, even if the parameter has nothing to do with
949 // receiving. Not all parameters are serialized to SDP.
950 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
951 // the SessionDescription, it is necessary to do that here to in order to
952 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
953 // https://code.google.com/p/webrtc/issues/detail?id=1356
954 std::string sdp;
955 EXPECT_TRUE(answer->ToString(&sdp));
956 SessionDescriptionInterface* new_answer =
957 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
958 sdp, NULL);
959 EXPECT_TRUE(DoSetLocalDescription(new_answer));
960 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
961 }
962
963 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700964 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700965 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
967 std::string sdp;
968 EXPECT_TRUE(answer->ToString(&sdp));
969 SessionDescriptionInterface* pr_answer =
970 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
971 sdp, NULL);
972 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
973 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
974 }
975
976 void CreateOfferReceiveAnswer() {
977 CreateOfferAsLocalDescription();
978 std::string sdp;
979 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
980 CreateAnswerAsRemoteDescription(sdp);
981 }
982
983 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700984 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700985 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
987 // audio codec change, even if the parameter has nothing to do with
988 // receiving. Not all parameters are serialized to SDP.
989 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
990 // the SessionDescription, it is necessary to do that here to in order to
991 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
992 // https://code.google.com/p/webrtc/issues/detail?id=1356
993 std::string sdp;
994 EXPECT_TRUE(offer->ToString(&sdp));
995 SessionDescriptionInterface* new_offer =
996 webrtc::CreateSessionDescription(
997 SessionDescriptionInterface::kOffer,
998 sdp, NULL);
999
1000 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1001 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001002 // Wait for the ice_complete message, so that SDP will have candidates.
1003 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 }
1005
deadbeefab9b2d12015-10-14 11:33:11 -07001006 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1008 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001009 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 EXPECT_TRUE(DoSetRemoteDescription(answer));
1011 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1012 }
1013
deadbeefab9b2d12015-10-14 11:33:11 -07001014 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 webrtc::JsepSessionDescription* pr_answer =
1016 new webrtc::JsepSessionDescription(
1017 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001018 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1020 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1021 webrtc::JsepSessionDescription* answer =
1022 new webrtc::JsepSessionDescription(
1023 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001024 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 EXPECT_TRUE(DoSetRemoteDescription(answer));
1026 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1027 }
1028
1029 // Help function used for waiting until a the last signaled remote stream has
1030 // the same label as |stream_label|. In a few of the tests in this file we
1031 // answer with the same session description as we offer and thus we can
1032 // check if OnAddStream have been called with the same stream as we offer to
1033 // send.
1034 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1035 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1036 }
1037
1038 // Creates an offer and applies it as a local session description.
1039 // Creates an answer with the same SDP an the offer but removes all lines
1040 // that start with a:ssrc"
1041 void CreateOfferReceiveAnswerWithoutSsrc() {
1042 CreateOfferAsLocalDescription();
1043 std::string sdp;
1044 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1045 SetSsrcToZero(&sdp);
1046 CreateAnswerAsRemoteDescription(sdp);
1047 }
1048
deadbeefab9b2d12015-10-14 11:33:11 -07001049 // This function creates a MediaStream with label kStreams[0] and
1050 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1051 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001052 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001053 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001054 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001055 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1056 size_t number_of_video_tracks) {
1057 EXPECT_LE(number_of_audio_tracks, 2u);
1058 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001059
1060 reference_collection_ = StreamCollection::Create();
1061 std::string sdp_ms1 = std::string(kSdpStringInit);
1062
1063 std::string mediastream_label = kStreams[0];
1064
1065 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1066 webrtc::MediaStream::Create(mediastream_label));
1067 reference_collection_->AddStream(stream);
1068
1069 if (number_of_audio_tracks > 0) {
1070 sdp_ms1 += std::string(kSdpStringAudio);
1071 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1072 AddAudioTrack(kAudioTracks[0], stream);
1073 }
1074 if (number_of_audio_tracks > 1) {
1075 sdp_ms1 += kSdpStringMs1Audio1;
1076 AddAudioTrack(kAudioTracks[1], stream);
1077 }
1078
1079 if (number_of_video_tracks > 0) {
1080 sdp_ms1 += std::string(kSdpStringVideo);
1081 sdp_ms1 += std::string(kSdpStringMs1Video0);
1082 AddVideoTrack(kVideoTracks[0], stream);
1083 }
1084 if (number_of_video_tracks > 1) {
1085 sdp_ms1 += kSdpStringMs1Video1;
1086 AddVideoTrack(kVideoTracks[1], stream);
1087 }
1088
kwibergd1fe2812016-04-27 06:47:29 -07001089 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001090 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1091 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001092 }
1093
1094 void AddAudioTrack(const std::string& track_id,
1095 MediaStreamInterface* stream) {
1096 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1097 webrtc::AudioTrack::Create(track_id, nullptr));
1098 ASSERT_TRUE(stream->AddTrack(audio_track));
1099 }
1100
1101 void AddVideoTrack(const std::string& track_id,
1102 MediaStreamInterface* stream) {
1103 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001104 webrtc::VideoTrack::Create(track_id,
1105 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001106 ASSERT_TRUE(stream->AddTrack(video_track));
1107 }
1108
kwibergfd8be342016-05-14 19:44:11 -07001109 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001110 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001111 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001112 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001113 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1114 return offer;
1115 }
1116
kwibergfd8be342016-05-14 19:44:11 -07001117 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001118 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001119 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001120 CreateOfferWithOneAudioStream();
1121 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001122 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001123 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1124 return answer;
1125 }
1126
1127 const std::string& GetFirstAudioStreamCname(
1128 const SessionDescriptionInterface* desc) {
1129 const cricket::ContentInfo* audio_content =
1130 cricket::GetFirstAudioContent(desc->description());
1131 const cricket::AudioContentDescription* audio_desc =
1132 static_cast<const cricket::AudioContentDescription*>(
1133 audio_content->description);
1134 return audio_desc->streams()[0].cname;
1135 }
1136
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001137 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001138 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1139 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1140 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001142 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143};
1144
zhihuang29ff8442016-07-27 11:07:25 -07001145// Test that no callbacks on the PeerConnectionObserver are called after the
1146// PeerConnection is closed.
1147TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001148 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001149 pc_factory_for_test_->CreatePeerConnection(
1150 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1151 nullptr, &observer_));
1152 observer_.SetPeerConnectionInterface(pc.get());
1153 pc->Close();
1154
1155 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001156 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001157 std::vector<cricket::Candidate> candidates;
1158 pc_factory_for_test_->transport_controller->SignalGatheringState(
1159 cricket::IceGatheringState{});
1160 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1161 "", candidates);
1162 pc_factory_for_test_->transport_controller->SignalConnectionState(
1163 cricket::IceConnectionState{});
1164 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1165 candidates);
1166 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001167 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001168}
1169
zhihuang8f65cdf2016-05-06 18:40:30 -07001170// Generate different CNAMEs when PeerConnections are created.
1171// The CNAMEs are expected to be generated randomly. It is possible
1172// that the test fails, though the possibility is very low.
1173TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001174 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 CreateOfferWithOneAudioStream();
1178 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1179 GetFirstAudioStreamCname(offer2.get()));
1180}
1181
1182TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001185 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 CreateAnswerWithOneAudioStream();
1187 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1188 GetFirstAudioStreamCname(answer2.get()));
1189}
1190
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191TEST_F(PeerConnectionInterfaceTest,
1192 CreatePeerConnectionWithDifferentConfigurations) {
1193 CreatePeerConnectionWithDifferentConfigurations();
1194}
1195
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196TEST_F(PeerConnectionInterfaceTest,
1197 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1199 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1200 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1201 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1202 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1203 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1204 port_allocator_->candidate_filter());
1205 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1206 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1207}
1208
1209// Test that when a PeerConnection is created with a nonzero candidate pool
1210// size, the pooled PortAllocatorSession is created with all the attributes
1211// in the RTCConfiguration.
1212TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1213 PeerConnectionInterface::RTCConfiguration config;
1214 PeerConnectionInterface::IceServer server;
1215 server.uri = kStunAddressOnly;
1216 config.servers.push_back(server);
1217 config.type = PeerConnectionInterface::kRelay;
1218 config.disable_ipv6 = true;
1219 config.tcp_candidate_policy =
1220 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001221 config.candidate_network_policy =
1222 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001223 config.ice_candidate_pool_size = 1;
1224 CreatePeerConnection(config, nullptr);
1225
1226 const cricket::FakePortAllocatorSession* session =
1227 static_cast<const cricket::FakePortAllocatorSession*>(
1228 port_allocator_->GetPooledSession());
1229 ASSERT_NE(nullptr, session);
1230 EXPECT_EQ(1UL, session->stun_servers().size());
1231 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1232 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001233 EXPECT_LT(0U,
1234 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001235}
1236
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001237// Test that the PeerConnection initializes the port allocator passed into it,
1238// and on the correct thread.
1239TEST_F(PeerConnectionInterfaceTest,
1240 CreatePeerConnectionInitializesPortAllocator) {
1241 rtc::Thread network_thread;
1242 network_thread.Start();
1243 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1244 webrtc::CreatePeerConnectionFactory(
1245 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1246 nullptr, nullptr, nullptr));
1247 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1248 new cricket::FakePortAllocator(&network_thread, nullptr));
1249 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1250 PeerConnectionInterface::RTCConfiguration config;
1251 rtc::scoped_refptr<PeerConnectionInterface> pc(
1252 pc_factory->CreatePeerConnection(
1253 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1254 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1255 // so all we have to do here is check that it's initialized.
1256 EXPECT_TRUE(raw_port_allocator->initialized());
1257}
1258
deadbeef46c73892016-11-16 19:42:04 -08001259// Check that GetConfiguration returns the configuration the PeerConnection was
1260// constructed with, before SetConfiguration is called.
1261TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1262 PeerConnectionInterface::RTCConfiguration config;
1263 config.type = PeerConnectionInterface::kRelay;
1264 CreatePeerConnection(config, nullptr);
1265
1266 PeerConnectionInterface::RTCConfiguration returned_config =
1267 pc_->GetConfiguration();
1268 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1269}
1270
1271// Check that GetConfiguration returns the last configuration passed into
1272// SetConfiguration.
1273TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1274 CreatePeerConnection();
1275
1276 PeerConnectionInterface::RTCConfiguration config;
1277 config.type = PeerConnectionInterface::kRelay;
1278 EXPECT_TRUE(pc_->SetConfiguration(config));
1279
1280 PeerConnectionInterface::RTCConfiguration returned_config =
1281 pc_->GetConfiguration();
1282 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1283}
1284
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001286 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001287 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 AddVoiceStream(kStreamLabel2);
1289 ASSERT_EQ(2u, pc_->local_streams()->count());
1290
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001291 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001292 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001294 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1295 pc_factory_->CreateAudioTrack(kStreamLabel3,
1296 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001298 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001299 EXPECT_EQ(3u, pc_->local_streams()->count());
1300
1301 // Remove the third stream.
1302 pc_->RemoveStream(pc_->local_streams()->at(2));
1303 EXPECT_EQ(2u, pc_->local_streams()->count());
1304
1305 // Remove the second stream.
1306 pc_->RemoveStream(pc_->local_streams()->at(1));
1307 EXPECT_EQ(1u, pc_->local_streams()->count());
1308
1309 // Remove the first stream.
1310 pc_->RemoveStream(pc_->local_streams()->at(0));
1311 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312}
1313
deadbeefab9b2d12015-10-14 11:33:11 -07001314// Test that the created offer includes streams we added.
1315TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001316 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001317 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001318 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001319 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001320
1321 const cricket::ContentInfo* audio_content =
1322 cricket::GetFirstAudioContent(offer->description());
1323 const cricket::AudioContentDescription* audio_desc =
1324 static_cast<const cricket::AudioContentDescription*>(
1325 audio_content->description);
1326 EXPECT_TRUE(
1327 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1328
1329 const cricket::ContentInfo* video_content =
1330 cricket::GetFirstVideoContent(offer->description());
1331 const cricket::VideoContentDescription* video_desc =
1332 static_cast<const cricket::VideoContentDescription*>(
1333 video_content->description);
1334 EXPECT_TRUE(
1335 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1336
1337 // Add another stream and ensure the offer includes both the old and new
1338 // streams.
1339 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001340 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001341
1342 audio_content = cricket::GetFirstAudioContent(offer->description());
1343 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1344 audio_content->description);
1345 EXPECT_TRUE(
1346 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1347 EXPECT_TRUE(
1348 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1349
1350 video_content = cricket::GetFirstVideoContent(offer->description());
1351 video_desc = static_cast<const cricket::VideoContentDescription*>(
1352 video_content->description);
1353 EXPECT_TRUE(
1354 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1355 EXPECT_TRUE(
1356 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1357}
1358
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001360 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001361 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 ASSERT_EQ(1u, pc_->local_streams()->count());
1363 pc_->RemoveStream(pc_->local_streams()->at(0));
1364 EXPECT_EQ(0u, pc_->local_streams()->count());
1365}
1366
deadbeefe1f9d832016-01-14 15:35:42 -08001367// Test for AddTrack and RemoveTrack methods.
1368// Tests that the created offer includes tracks we added,
1369// and that the RtpSenders are created correctly.
1370// Also tests that RemoveTrack removes the tracks from subsequent offers.
1371TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001372 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001373 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001374 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001375 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1376 std::vector<MediaStreamInterface*> stream_list;
1377 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001378 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001379 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001380 rtc::scoped_refptr<VideoTrackInterface> video_track(
1381 pc_factory_->CreateVideoTrack(
1382 "video_track",
1383 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001384 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1385 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001386 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1387 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001388 EXPECT_EQ("audio_track", audio_sender->id());
1389 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001390 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1391 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001392 EXPECT_EQ("video_track", video_sender->id());
1393 EXPECT_EQ(video_track, video_sender->track());
1394
1395 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001396 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001397 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001398
1399 const cricket::ContentInfo* audio_content =
1400 cricket::GetFirstAudioContent(offer->description());
1401 const cricket::AudioContentDescription* audio_desc =
1402 static_cast<const cricket::AudioContentDescription*>(
1403 audio_content->description);
1404 EXPECT_TRUE(
1405 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1406
1407 const cricket::ContentInfo* video_content =
1408 cricket::GetFirstVideoContent(offer->description());
1409 const cricket::VideoContentDescription* video_desc =
1410 static_cast<const cricket::VideoContentDescription*>(
1411 video_content->description);
1412 EXPECT_TRUE(
1413 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1414
1415 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1416
1417 // Now try removing the tracks.
1418 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1419 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1420
1421 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001422 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001423
1424 audio_content = cricket::GetFirstAudioContent(offer->description());
1425 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1426 audio_content->description);
1427 EXPECT_FALSE(
1428 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1429
1430 video_content = cricket::GetFirstVideoContent(offer->description());
1431 video_desc = static_cast<const cricket::VideoContentDescription*>(
1432 video_content->description);
1433 EXPECT_FALSE(
1434 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1435
1436 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1437
1438 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1439 // should return false.
1440 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1441 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1442}
1443
1444// Test creating senders without a stream specified,
1445// expecting a random stream ID to be generated.
1446TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001447 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001448 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001449 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001450 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001451 rtc::scoped_refptr<VideoTrackInterface> video_track(
1452 pc_factory_->CreateVideoTrack(
1453 "video_track",
1454 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001455 auto audio_sender =
1456 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1457 auto video_sender =
1458 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1459 EXPECT_EQ("audio_track", audio_sender->id());
1460 EXPECT_EQ(audio_track, audio_sender->track());
1461 EXPECT_EQ("video_track", video_sender->id());
1462 EXPECT_EQ(video_track, video_sender->track());
1463 // If the ID is truly a random GUID, it should be infinitely unlikely they
1464 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001465 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001466}
1467
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1469 InitiateCall();
1470 WaitAndVerifyOnAddStream(kStreamLabel1);
1471 VerifyRemoteRtpHeaderExtensions();
1472}
1473
1474TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001475 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001476 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 CreateOfferAsLocalDescription();
1478 std::string offer;
1479 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1480 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1481 WaitAndVerifyOnAddStream(kStreamLabel1);
1482}
1483
1484TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001485 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001486 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487
1488 CreateOfferAsRemoteDescription();
1489 CreateAnswerAsLocalDescription();
1490
1491 WaitAndVerifyOnAddStream(kStreamLabel1);
1492}
1493
1494TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001495 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001496 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497
1498 CreateOfferAsRemoteDescription();
1499 CreatePrAnswerAsLocalDescription();
1500 CreateAnswerAsLocalDescription();
1501
1502 WaitAndVerifyOnAddStream(kStreamLabel1);
1503}
1504
1505TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1506 InitiateCall();
1507 ASSERT_EQ(1u, pc_->remote_streams()->count());
1508 pc_->RemoveStream(pc_->local_streams()->at(0));
1509 CreateOfferReceiveAnswer();
1510 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001511 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 CreateOfferReceiveAnswer();
1513}
1514
1515// Tests that after negotiating an audio only call, the respondent can perform a
1516// renegotiation that removes the audio stream.
1517TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001518 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 AddVoiceStream(kStreamLabel1);
1520 CreateOfferAsRemoteDescription();
1521 CreateAnswerAsLocalDescription();
1522
1523 ASSERT_EQ(1u, pc_->remote_streams()->count());
1524 pc_->RemoveStream(pc_->local_streams()->at(0));
1525 CreateOfferReceiveAnswer();
1526 EXPECT_EQ(0u, pc_->remote_streams()->count());
1527}
1528
1529// Test that candidates are generated and that we can parse our own candidates.
1530TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001531 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532
1533 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1534 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001536 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001537 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001538 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001541 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001542 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001543 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
1545 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1546 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1547
1548 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1549}
1550
deadbeefab9b2d12015-10-14 11:33:11 -07001551// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552// not unique.
1553TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001554 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001556 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001557 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001558 EXPECT_TRUE(offer);
1559 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560
1561 // Create a local stream with audio&video tracks having same label.
1562 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1563
1564 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001565 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
1567 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001568 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001569 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570}
1571
1572// Test that we will get different SSRCs for each tracks in the offer and answer
1573// we created.
1574TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001575 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 // Create a local stream with audio&video tracks having different labels.
1577 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1578
1579 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001580 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001581 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 int audio_ssrc = 0;
1583 int video_ssrc = 0;
1584 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1585 &audio_ssrc));
1586 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1587 &video_ssrc));
1588 EXPECT_NE(audio_ssrc, video_ssrc);
1589
1590 // Test CreateAnswer
1591 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001592 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001593 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 audio_ssrc = 0;
1595 video_ssrc = 0;
1596 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1597 &audio_ssrc));
1598 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1599 &video_ssrc));
1600 EXPECT_NE(audio_ssrc, video_ssrc);
1601}
1602
deadbeefeb459812015-12-15 19:24:43 -08001603// Test that it's possible to call AddTrack on a MediaStream after adding
1604// the stream to a PeerConnection.
1605// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1606TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001607 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001608 // Create audio stream and add to PeerConnection.
1609 AddVoiceStream(kStreamLabel1);
1610 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1611
1612 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001613 rtc::scoped_refptr<VideoTrackInterface> video_track(
1614 pc_factory_->CreateVideoTrack(
1615 "video_label",
1616 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001617 stream->AddTrack(video_track.get());
1618
kwibergd1fe2812016-04-27 06:47:29 -07001619 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001620 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001621
1622 const cricket::MediaContentDescription* video_desc =
1623 cricket::GetFirstVideoContentDescription(offer->description());
1624 EXPECT_TRUE(video_desc != nullptr);
1625}
1626
1627// Test that it's possible to call RemoveTrack on a MediaStream after adding
1628// the stream to a PeerConnection.
1629// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1630TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001631 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001632 // Create audio/video stream and add to PeerConnection.
1633 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1634 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1635
1636 // Remove the video track.
1637 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1638
kwibergd1fe2812016-04-27 06:47:29 -07001639 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001640 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001641
1642 const cricket::MediaContentDescription* video_desc =
1643 cricket::GetFirstVideoContentDescription(offer->description());
1644 EXPECT_TRUE(video_desc == nullptr);
1645}
1646
deadbeefbd7d8f72015-12-18 16:58:44 -08001647// Test creating a sender with a stream ID, and ensure the ID is populated
1648// in the offer.
1649TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001650 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001651 pc_->CreateSender("video", kStreamLabel1);
1652
kwibergd1fe2812016-04-27 06:47:29 -07001653 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001654 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001655
1656 const cricket::MediaContentDescription* video_desc =
1657 cricket::GetFirstVideoContentDescription(offer->description());
1658 ASSERT_TRUE(video_desc != nullptr);
1659 ASSERT_EQ(1u, video_desc->streams().size());
1660 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1661}
1662
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663// Test that we can specify a certain track that we want statistics about.
1664TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1665 InitiateCall();
1666 ASSERT_LT(0u, pc_->remote_streams()->count());
1667 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001668 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1670 EXPECT_TRUE(DoGetStats(remote_audio));
1671
1672 // Remove the stream. Since we are sending to our selves the local
1673 // and the remote stream is the same.
1674 pc_->RemoveStream(pc_->local_streams()->at(0));
1675 // Do a re-negotiation.
1676 CreateOfferReceiveAnswer();
1677
1678 ASSERT_EQ(0u, pc_->remote_streams()->count());
1679
1680 // Test that we still can get statistics for the old track. Even if it is not
1681 // sent any longer.
1682 EXPECT_TRUE(DoGetStats(remote_audio));
1683}
1684
1685// Test that we can get stats on a video track.
1686TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1687 InitiateCall();
1688 ASSERT_LT(0u, pc_->remote_streams()->count());
1689 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001690 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1692 EXPECT_TRUE(DoGetStats(remote_video));
1693}
1694
1695// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001696TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001698 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 pc_factory_->CreateAudioTrack("unknown track", NULL));
1700 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1701}
1702
1703// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 FakeConstraints constraints;
1706 constraints.SetAllowRtpDataChannels();
1707 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001708 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001710 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 pc_->CreateDataChannel("test2", NULL);
1712 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001713 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001715 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 new MockDataChannelObserver(data2));
1717
1718 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1719 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1720 std::string data_to_send1 = "testing testing";
1721 std::string data_to_send2 = "testing something else";
1722 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1723
1724 CreateOfferReceiveAnswer();
1725 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1726 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1727
1728 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1729 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1730 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1731 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1732
1733 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1734 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1735
1736 data1->Close();
1737 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1738 CreateOfferReceiveAnswer();
1739 EXPECT_FALSE(observer1->IsOpen());
1740 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1741 EXPECT_TRUE(observer2->IsOpen());
1742
1743 data_to_send2 = "testing something else again";
1744 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1745
1746 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1747}
1748
1749// This test verifies that sendnig binary data over RTP data channels should
1750// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 FakeConstraints constraints;
1753 constraints.SetAllowRtpDataChannels();
1754 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001755 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001757 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 pc_->CreateDataChannel("test2", NULL);
1759 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001760 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001762 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 new MockDataChannelObserver(data2));
1764
1765 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1766 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1767
1768 CreateOfferReceiveAnswer();
1769 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1770 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1771
1772 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1773 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1774
jbaucheec21bd2016-03-20 06:15:43 -07001775 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1777}
1778
1779// This test setup a RTP data channels in loop back and test that a channel is
1780// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 FakeConstraints constraints;
1783 constraints.SetAllowRtpDataChannels();
1784 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001785 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001787 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 new MockDataChannelObserver(data1));
1789
1790 CreateOfferReceiveAnswerWithoutSsrc();
1791
1792 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1793
1794 data1->Close();
1795 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1796 CreateOfferReceiveAnswerWithoutSsrc();
1797 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1798 EXPECT_FALSE(observer1->IsOpen());
1799}
1800
1801// This test that if a data channel is added in an answer a receive only channel
1802// channel is created.
1803TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1804 FakeConstraints constraints;
1805 constraints.SetAllowRtpDataChannels();
1806 CreatePeerConnection(&constraints);
1807
1808 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001809 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 pc_->CreateDataChannel(offer_label, NULL);
1811
1812 CreateOfferAsLocalDescription();
1813
1814 // Replace the data channel label in the offer and apply it as an answer.
1815 std::string receive_label = "answer_channel";
1816 std::string sdp;
1817 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001818 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 receive_label.c_str(), receive_label.length(),
1820 &sdp);
1821 CreateAnswerAsRemoteDescription(sdp);
1822
1823 // Verify that a new incoming data channel has been created and that
1824 // it is open but can't we written to.
1825 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1826 DataChannelInterface* received_channel = observer_.last_datachannel_;
1827 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1828 EXPECT_EQ(receive_label, received_channel->label());
1829 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1830
1831 // Verify that the channel we initially offered has been rejected.
1832 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1833
1834 // Do another offer / answer exchange and verify that the data channel is
1835 // opened.
1836 CreateOfferReceiveAnswer();
1837 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1838 kTimeout);
1839}
1840
1841// This test that no data channel is returned if a reliable channel is
1842// requested.
1843// TODO(perkj): Remove this test once reliable channels are implemented.
1844TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1845 FakeConstraints constraints;
1846 constraints.SetAllowRtpDataChannels();
1847 CreatePeerConnection(&constraints);
1848
1849 std::string label = "test";
1850 webrtc::DataChannelInit config;
1851 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001852 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 pc_->CreateDataChannel(label, &config);
1854 EXPECT_TRUE(channel == NULL);
1855}
1856
deadbeefab9b2d12015-10-14 11:33:11 -07001857// Verifies that duplicated label is not allowed for RTP data channel.
1858TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1859 FakeConstraints constraints;
1860 constraints.SetAllowRtpDataChannels();
1861 CreatePeerConnection(&constraints);
1862
1863 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001864 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001865 pc_->CreateDataChannel(label, nullptr);
1866 EXPECT_NE(channel, nullptr);
1867
zhihuang9763d562016-08-05 11:14:50 -07001868 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001869 pc_->CreateDataChannel(label, nullptr);
1870 EXPECT_EQ(dup_channel, nullptr);
1871}
1872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873// This tests that a SCTP data channel is returned using different
1874// DataChannelInit configurations.
1875TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1876 FakeConstraints constraints;
1877 constraints.SetAllowDtlsSctpDataChannels();
1878 CreatePeerConnection(&constraints);
1879
1880 webrtc::DataChannelInit config;
1881
zhihuang9763d562016-08-05 11:14:50 -07001882 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 pc_->CreateDataChannel("1", &config);
1884 EXPECT_TRUE(channel != NULL);
1885 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001886 EXPECT_TRUE(observer_.renegotiation_needed_);
1887 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888
1889 config.ordered = false;
1890 channel = pc_->CreateDataChannel("2", &config);
1891 EXPECT_TRUE(channel != NULL);
1892 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001893 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894
1895 config.ordered = true;
1896 config.maxRetransmits = 0;
1897 channel = pc_->CreateDataChannel("3", &config);
1898 EXPECT_TRUE(channel != NULL);
1899 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001900 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901
1902 config.maxRetransmits = -1;
1903 config.maxRetransmitTime = 0;
1904 channel = pc_->CreateDataChannel("4", &config);
1905 EXPECT_TRUE(channel != NULL);
1906 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001907 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908}
1909
1910// This tests that no data channel is returned if both maxRetransmits and
1911// maxRetransmitTime are set for SCTP data channels.
1912TEST_F(PeerConnectionInterfaceTest,
1913 CreateSctpDataChannelShouldFailForInvalidConfig) {
1914 FakeConstraints constraints;
1915 constraints.SetAllowDtlsSctpDataChannels();
1916 CreatePeerConnection(&constraints);
1917
1918 std::string label = "test";
1919 webrtc::DataChannelInit config;
1920 config.maxRetransmits = 0;
1921 config.maxRetransmitTime = 0;
1922
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 pc_->CreateDataChannel(label, &config);
1925 EXPECT_TRUE(channel == NULL);
1926}
1927
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928// The test verifies that creating a SCTP data channel with an id already in use
1929// or out of range should fail.
1930TEST_F(PeerConnectionInterfaceTest,
1931 CreateSctpDataChannelWithInvalidIdShouldFail) {
1932 FakeConstraints constraints;
1933 constraints.SetAllowDtlsSctpDataChannels();
1934 CreatePeerConnection(&constraints);
1935
1936 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001937 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001939 config.id = 1;
1940 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 EXPECT_TRUE(channel != NULL);
1942 EXPECT_EQ(1, channel->id());
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 channel = pc_->CreateDataChannel("x", &config);
1945 EXPECT_TRUE(channel == NULL);
1946
1947 config.id = cricket::kMaxSctpSid;
1948 channel = pc_->CreateDataChannel("max", &config);
1949 EXPECT_TRUE(channel != NULL);
1950 EXPECT_EQ(config.id, channel->id());
1951
1952 config.id = cricket::kMaxSctpSid + 1;
1953 channel = pc_->CreateDataChannel("x", &config);
1954 EXPECT_TRUE(channel == NULL);
1955}
1956
deadbeefab9b2d12015-10-14 11:33:11 -07001957// Verifies that duplicated label is allowed for SCTP data channel.
1958TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1959 FakeConstraints constraints;
1960 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1961 true);
1962 CreatePeerConnection(&constraints);
1963
1964 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001965 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001966 pc_->CreateDataChannel(label, nullptr);
1967 EXPECT_NE(channel, nullptr);
1968
zhihuang9763d562016-08-05 11:14:50 -07001969 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001970 pc_->CreateDataChannel(label, nullptr);
1971 EXPECT_NE(dup_channel, nullptr);
1972}
1973
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001974// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1975// DataChannel.
1976TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1977 FakeConstraints constraints;
1978 constraints.SetAllowRtpDataChannels();
1979 CreatePeerConnection(&constraints);
1980
zhihuang9763d562016-08-05 11:14:50 -07001981 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001982 pc_->CreateDataChannel("test1", NULL);
1983 EXPECT_TRUE(observer_.renegotiation_needed_);
1984 observer_.renegotiation_needed_ = false;
1985
zhihuang9763d562016-08-05 11:14:50 -07001986 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001987 pc_->CreateDataChannel("test2", NULL);
1988 EXPECT_TRUE(observer_.renegotiation_needed_);
1989}
1990
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 FakeConstraints constraints;
1994 constraints.SetAllowRtpDataChannels();
1995 CreatePeerConnection(&constraints);
1996
zhihuang9763d562016-08-05 11:14:50 -07001997 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001999 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 pc_->CreateDataChannel("test2", NULL);
2001 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002002 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002004 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 new MockDataChannelObserver(data2));
2006
2007 CreateOfferReceiveAnswer();
2008 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2009 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2010
2011 ReleasePeerConnection();
2012 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2013 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2014}
2015
2016// This test that data channels can be rejected in an answer.
2017TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2018 FakeConstraints constraints;
2019 constraints.SetAllowRtpDataChannels();
2020 CreatePeerConnection(&constraints);
2021
zhihuang9763d562016-08-05 11:14:50 -07002022 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 pc_->CreateDataChannel("offer_channel", NULL));
2024
2025 CreateOfferAsLocalDescription();
2026
2027 // Create an answer where the m-line for data channels are rejected.
2028 std::string sdp;
2029 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2030 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2031 SessionDescriptionInterface::kAnswer);
2032 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2033 cricket::ContentInfo* data_info =
2034 answer->description()->GetContentByName("data");
2035 data_info->rejected = true;
2036
2037 DoSetRemoteDescription(answer);
2038 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2039}
2040
2041// Test that we can create a session description from an SDP string from
2042// FireFox, use it as a remote session description, generate an answer and use
2043// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002044TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002045 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 FakeConstraints constraints;
2047 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2048 true);
2049 CreatePeerConnection(&constraints);
2050 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2051 SessionDescriptionInterface* desc =
2052 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002053 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2055 CreateAnswerAsLocalDescription();
2056 ASSERT_TRUE(pc_->local_description() != NULL);
2057 ASSERT_TRUE(pc_->remote_description() != NULL);
2058
2059 const cricket::ContentInfo* content =
2060 cricket::GetFirstAudioContent(pc_->local_description()->description());
2061 ASSERT_TRUE(content != NULL);
2062 EXPECT_FALSE(content->rejected);
2063
2064 content =
2065 cricket::GetFirstVideoContent(pc_->local_description()->description());
2066 ASSERT_TRUE(content != NULL);
2067 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002068#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 content =
2070 cricket::GetFirstDataContent(pc_->local_description()->description());
2071 ASSERT_TRUE(content != NULL);
2072 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002073#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074}
2075
2076// Test that we can create an audio only offer and receive an answer with a
2077// limited set of audio codecs and receive an updated offer with more audio
2078// codecs, where the added codecs are not supported.
2079TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002080 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 AddVoiceStream("audio_label");
2082 CreateOfferAsLocalDescription();
2083
2084 SessionDescriptionInterface* answer =
2085 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002086 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2088
2089 SessionDescriptionInterface* updated_offer =
2090 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002091 webrtc::kAudioSdpWithUnsupportedCodecs,
2092 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2094 CreateAnswerAsLocalDescription();
2095}
2096
deadbeefc80741f2015-10-22 13:14:45 -07002097// Test that if we're receiving (but not sending) a track, subsequent offers
2098// will have m-lines with a=recvonly.
2099TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2100 FakeConstraints constraints;
2101 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2102 true);
2103 CreatePeerConnection(&constraints);
2104 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2105 CreateAnswerAsLocalDescription();
2106
2107 // At this point we should be receiving stream 1, but not sending anything.
2108 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002109 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002110 DoCreateOffer(&offer, nullptr);
2111
2112 const cricket::ContentInfo* video_content =
2113 cricket::GetFirstVideoContent(offer->description());
2114 const cricket::VideoContentDescription* video_desc =
2115 static_cast<const cricket::VideoContentDescription*>(
2116 video_content->description);
2117 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2118
2119 const cricket::ContentInfo* audio_content =
2120 cricket::GetFirstAudioContent(offer->description());
2121 const cricket::AudioContentDescription* audio_desc =
2122 static_cast<const cricket::AudioContentDescription*>(
2123 audio_content->description);
2124 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2125}
2126
2127// Test that if we're receiving (but not sending) a track, and the
2128// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2129// false, the generated m-lines will be a=inactive.
2130TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2131 FakeConstraints constraints;
2132 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2133 true);
2134 CreatePeerConnection(&constraints);
2135 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2136 CreateAnswerAsLocalDescription();
2137
2138 // At this point we should be receiving stream 1, but not sending anything.
2139 // A new offer would be recvonly, but we'll set the "no receive" constraints
2140 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002141 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002142 FakeConstraints offer_constraints;
2143 offer_constraints.AddMandatory(
2144 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2145 offer_constraints.AddMandatory(
2146 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2147 DoCreateOffer(&offer, &offer_constraints);
2148
2149 const cricket::ContentInfo* video_content =
2150 cricket::GetFirstVideoContent(offer->description());
2151 const cricket::VideoContentDescription* video_desc =
2152 static_cast<const cricket::VideoContentDescription*>(
2153 video_content->description);
2154 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2155
2156 const cricket::ContentInfo* audio_content =
2157 cricket::GetFirstAudioContent(offer->description());
2158 const cricket::AudioContentDescription* audio_desc =
2159 static_cast<const cricket::AudioContentDescription*>(
2160 audio_content->description);
2161 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2162}
2163
deadbeef653b8e02015-11-11 12:55:10 -08002164// Test that we can use SetConfiguration to change the ICE servers of the
2165// PortAllocator.
2166TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2167 CreatePeerConnection();
2168
2169 PeerConnectionInterface::RTCConfiguration config;
2170 PeerConnectionInterface::IceServer server;
2171 server.uri = "stun:test_hostname";
2172 config.servers.push_back(server);
2173 EXPECT_TRUE(pc_->SetConfiguration(config));
2174
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002175 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2176 EXPECT_EQ("test_hostname",
2177 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002178}
2179
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002180TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2181 CreatePeerConnection();
2182 PeerConnectionInterface::RTCConfiguration config;
2183 config.type = PeerConnectionInterface::kRelay;
2184 EXPECT_TRUE(pc_->SetConfiguration(config));
2185 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2186}
2187
deadbeef293e9262017-01-11 12:28:30 -08002188TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2189 PeerConnectionInterface::RTCConfiguration config;
2190 config.prune_turn_ports = false;
2191 CreatePeerConnection(config, nullptr);
2192 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2193
2194 config.prune_turn_ports = true;
2195 EXPECT_TRUE(pc_->SetConfiguration(config));
2196 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2197}
2198
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002199// Test that when SetConfiguration changes both the pool size and other
2200// attributes, the pooled session is created with the updated attributes.
2201TEST_F(PeerConnectionInterfaceTest,
2202 SetConfigurationCreatesPooledSessionCorrectly) {
2203 CreatePeerConnection();
2204 PeerConnectionInterface::RTCConfiguration config;
2205 config.ice_candidate_pool_size = 1;
2206 PeerConnectionInterface::IceServer server;
2207 server.uri = kStunAddressOnly;
2208 config.servers.push_back(server);
2209 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002210 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002211
2212 const cricket::FakePortAllocatorSession* session =
2213 static_cast<const cricket::FakePortAllocatorSession*>(
2214 port_allocator_->GetPooledSession());
2215 ASSERT_NE(nullptr, session);
2216 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002217}
2218
deadbeef293e9262017-01-11 12:28:30 -08002219// Test that after SetLocalDescription, changing the pool size is not allowed,
2220// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002221TEST_F(PeerConnectionInterfaceTest,
2222 CantChangePoolSizeAfterSetLocalDescription) {
2223 CreatePeerConnection();
2224 // Start by setting a size of 1.
2225 PeerConnectionInterface::RTCConfiguration config;
2226 config.ice_candidate_pool_size = 1;
2227 EXPECT_TRUE(pc_->SetConfiguration(config));
2228
2229 // Set remote offer; can still change pool size at this point.
2230 CreateOfferAsRemoteDescription();
2231 config.ice_candidate_pool_size = 2;
2232 EXPECT_TRUE(pc_->SetConfiguration(config));
2233
2234 // Set local answer; now it's too late.
2235 CreateAnswerAsLocalDescription();
2236 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002237 RTCError error;
2238 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2239 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2240}
2241
2242// Test that SetConfiguration returns an invalid modification error if
2243// modifying a field in the configuration that isn't allowed to be modified.
2244TEST_F(PeerConnectionInterfaceTest,
2245 SetConfigurationReturnsInvalidModificationError) {
2246 PeerConnectionInterface::RTCConfiguration config;
2247 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2248 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2249 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2250 CreatePeerConnection(config, nullptr);
2251
2252 PeerConnectionInterface::RTCConfiguration modified_config = config;
2253 modified_config.bundle_policy =
2254 PeerConnectionInterface::kBundlePolicyMaxBundle;
2255 RTCError error;
2256 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2257 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2258
2259 modified_config = config;
2260 modified_config.rtcp_mux_policy =
2261 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2262 error.set_type(RTCErrorType::NONE);
2263 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2264 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2265
2266 modified_config = config;
2267 modified_config.continual_gathering_policy =
2268 PeerConnectionInterface::GATHER_CONTINUALLY;
2269 error.set_type(RTCErrorType::NONE);
2270 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2271 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2272}
2273
2274// Test that SetConfiguration returns a range error if the candidate pool size
2275// is negative or larger than allowed by the spec.
2276TEST_F(PeerConnectionInterfaceTest,
2277 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2278 PeerConnectionInterface::RTCConfiguration config;
2279 CreatePeerConnection(config, nullptr);
2280
2281 config.ice_candidate_pool_size = -1;
2282 RTCError error;
2283 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2284 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2285
2286 config.ice_candidate_pool_size = INT_MAX;
2287 error.set_type(RTCErrorType::NONE);
2288 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2289 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2290}
2291
2292// Test that SetConfiguration returns a syntax error if parsing an ICE server
2293// URL failed.
2294TEST_F(PeerConnectionInterfaceTest,
2295 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2296 PeerConnectionInterface::RTCConfiguration config;
2297 CreatePeerConnection(config, nullptr);
2298
2299 PeerConnectionInterface::IceServer bad_server;
2300 bad_server.uri = "stunn:www.example.com";
2301 config.servers.push_back(bad_server);
2302 RTCError error;
2303 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2304 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2305}
2306
2307// Test that SetConfiguration returns an invalid parameter error if a TURN
2308// IceServer is missing a username or password.
2309TEST_F(PeerConnectionInterfaceTest,
2310 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2311 PeerConnectionInterface::RTCConfiguration config;
2312 CreatePeerConnection(config, nullptr);
2313
2314 PeerConnectionInterface::IceServer bad_server;
2315 bad_server.uri = "turn:www.example.com";
2316 // Missing password.
2317 bad_server.username = "foo";
2318 config.servers.push_back(bad_server);
2319 RTCError error;
2320 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2321 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002322}
2323
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324// Test that PeerConnection::Close changes the states to closed and all remote
2325// tracks change state to ended.
2326TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2327 // Initialize a PeerConnection and negotiate local and remote session
2328 // description.
2329 InitiateCall();
2330 ASSERT_EQ(1u, pc_->local_streams()->count());
2331 ASSERT_EQ(1u, pc_->remote_streams()->count());
2332
2333 pc_->Close();
2334
2335 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2336 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2337 pc_->ice_connection_state());
2338 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2339 pc_->ice_gathering_state());
2340
2341 EXPECT_EQ(1u, pc_->local_streams()->count());
2342 EXPECT_EQ(1u, pc_->remote_streams()->count());
2343
zhihuang9763d562016-08-05 11:14:50 -07002344 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2345 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002346 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002347 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002348 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2349 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2350 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351}
2352
2353// Test that PeerConnection methods fails gracefully after
2354// PeerConnection::Close has been called.
2355TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002356 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2358 CreateOfferAsRemoteDescription();
2359 CreateAnswerAsLocalDescription();
2360
2361 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002362 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 pc_->local_streams()->at(0);
2364
2365 pc_->Close();
2366
2367 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002368 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369
2370 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002371 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002373 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374
2375 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2376
2377 EXPECT_TRUE(pc_->local_description() != NULL);
2378 EXPECT_TRUE(pc_->remote_description() != NULL);
2379
kwibergd1fe2812016-04-27 06:47:29 -07002380 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002381 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002382 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002383 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384
2385 std::string sdp;
2386 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2387 SessionDescriptionInterface* remote_offer =
2388 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2389 sdp, NULL);
2390 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2391
2392 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2393 SessionDescriptionInterface* local_offer =
2394 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2395 sdp, NULL);
2396 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2397}
2398
2399// Test that GetStats can still be called after PeerConnection::Close.
2400TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2401 InitiateCall();
2402 pc_->Close();
2403 DoGetStats(NULL);
2404}
deadbeefab9b2d12015-10-14 11:33:11 -07002405
2406// NOTE: The series of tests below come from what used to be
2407// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2408// setting a remote or local description has the expected effects.
2409
2410// This test verifies that the remote MediaStreams corresponding to a received
2411// SDP string is created. In this test the two separate MediaStreams are
2412// signaled.
2413TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2414 FakeConstraints constraints;
2415 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2416 true);
2417 CreatePeerConnection(&constraints);
2418 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2419
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002420 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002421 EXPECT_TRUE(
2422 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2423 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2424 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2425
2426 // Create a session description based on another SDP with another
2427 // MediaStream.
2428 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2429
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002430 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002431 EXPECT_TRUE(
2432 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2433}
2434
2435// This test verifies that when remote tracks are added/removed from SDP, the
2436// created remote streams are updated appropriately.
2437TEST_F(PeerConnectionInterfaceTest,
2438 AddRemoveTrackFromExistingRemoteMediaStream) {
2439 FakeConstraints constraints;
2440 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2441 true);
2442 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002443 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002444 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002445 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2446 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2447 reference_collection_));
2448
2449 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002450 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002451 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002452 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2453 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2454 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002455 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002456 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2457 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002458 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002459 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2460 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002461
2462 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002463 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002464 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002465 MockTrackObserver audio_track_observer(audio_track2);
2466 MockTrackObserver video_track_observer(video_track2);
2467
2468 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2469 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002470 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2471 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2472 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002473 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002474 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002475 audio_track2->state(), kTimeout);
2476 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2477 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002478}
2479
2480// This tests that remote tracks are ended if a local session description is set
2481// that rejects the media content type.
2482TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2483 FakeConstraints constraints;
2484 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2485 true);
2486 CreatePeerConnection(&constraints);
2487 // First create and set a remote offer, then reject its video content in our
2488 // answer.
2489 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2490 ASSERT_EQ(1u, observer_.remote_streams()->count());
2491 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2492 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2493 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2494
2495 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2496 remote_stream->GetVideoTracks()[0];
2497 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2498 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2499 remote_stream->GetAudioTracks()[0];
2500 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2501
kwibergd1fe2812016-04-27 06:47:29 -07002502 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002503 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002504 cricket::ContentInfo* video_info =
2505 local_answer->description()->GetContentByName("video");
2506 video_info->rejected = true;
2507 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2508 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2509 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2510
2511 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002512 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002513 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002514 video_info = local_offer->description()->GetContentByName("video");
2515 ASSERT_TRUE(video_info != nullptr);
2516 video_info->rejected = true;
2517 cricket::ContentInfo* audio_info =
2518 local_offer->description()->GetContentByName("audio");
2519 ASSERT_TRUE(audio_info != nullptr);
2520 audio_info->rejected = true;
2521 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002522 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002523 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002524 remote_audio->state(), kTimeout);
2525 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2526 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002527}
2528
2529// This tests that we won't crash if the remote track has been removed outside
2530// of PeerConnection and then PeerConnection tries to reject the track.
2531TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2532 FakeConstraints constraints;
2533 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2534 true);
2535 CreatePeerConnection(&constraints);
2536 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2537 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2538 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2539 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2540
kwibergd1fe2812016-04-27 06:47:29 -07002541 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002542 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2543 kSdpStringWithStream1, nullptr));
2544 cricket::ContentInfo* video_info =
2545 local_answer->description()->GetContentByName("video");
2546 video_info->rejected = true;
2547 cricket::ContentInfo* audio_info =
2548 local_answer->description()->GetContentByName("audio");
2549 audio_info->rejected = true;
2550 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2551
2552 // No crash is a pass.
2553}
2554
deadbeef5e97fb52015-10-15 12:49:08 -07002555// This tests that if a recvonly remote description is set, no remote streams
2556// will be created, even if the description contains SSRCs/MSIDs.
2557// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2558TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2559 FakeConstraints constraints;
2560 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2561 true);
2562 CreatePeerConnection(&constraints);
2563
2564 std::string recvonly_offer = kSdpStringWithStream1;
2565 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2566 strlen(kRecvonly), &recvonly_offer);
2567 CreateAndSetRemoteOffer(recvonly_offer);
2568
2569 EXPECT_EQ(0u, observer_.remote_streams()->count());
2570}
2571
deadbeefab9b2d12015-10-14 11:33:11 -07002572// This tests that a default MediaStream is created if a remote session
2573// description doesn't contain any streams and no MSID support.
2574// It also tests that the default stream is updated if a video m-line is added
2575// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002576TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002577 FakeConstraints constraints;
2578 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2579 true);
2580 CreatePeerConnection(&constraints);
2581 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2582
2583 ASSERT_EQ(1u, observer_.remote_streams()->count());
2584 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2585
2586 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2587 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2588 EXPECT_EQ("default", remote_stream->label());
2589
2590 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2591 ASSERT_EQ(1u, observer_.remote_streams()->count());
2592 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2593 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002594 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2595 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002596 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2597 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002598 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2599 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002600}
2601
2602// This tests that a default MediaStream is created if a remote session
2603// description doesn't contain any streams and media direction is send only.
2604TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002605 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002606 FakeConstraints constraints;
2607 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2608 true);
2609 CreatePeerConnection(&constraints);
2610 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2611
2612 ASSERT_EQ(1u, observer_.remote_streams()->count());
2613 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2614
2615 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2616 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2617 EXPECT_EQ("default", remote_stream->label());
2618}
2619
2620// This tests that it won't crash when PeerConnection tries to remove
2621// a remote track that as already been removed from the MediaStream.
2622TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2623 FakeConstraints constraints;
2624 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2625 true);
2626 CreatePeerConnection(&constraints);
2627 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2628 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2629 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2630 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2631
2632 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2633
2634 // No crash is a pass.
2635}
2636
2637// This tests that a default MediaStream is created if the remote session
2638// description doesn't contain any streams and don't contain an indication if
2639// MSID is supported.
2640TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002641 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002642 FakeConstraints constraints;
2643 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2644 true);
2645 CreatePeerConnection(&constraints);
2646 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2647
2648 ASSERT_EQ(1u, observer_.remote_streams()->count());
2649 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2650 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2651 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2652}
2653
2654// This tests that a default MediaStream is not created if the remote session
2655// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002656TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002657 FakeConstraints constraints;
2658 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2659 true);
2660 CreatePeerConnection(&constraints);
2661 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2662 EXPECT_EQ(0u, observer_.remote_streams()->count());
2663}
2664
deadbeefbda7e0b2015-12-08 17:13:40 -08002665// This tests that when setting a new description, the old default tracks are
2666// not destroyed and recreated.
2667// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002668TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002669 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002670 FakeConstraints constraints;
2671 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2672 true);
2673 CreatePeerConnection(&constraints);
2674 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2675
2676 ASSERT_EQ(1u, observer_.remote_streams()->count());
2677 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2678 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2679
2680 // Set the track to "disabled", then set a new description and ensure the
2681 // track is still disabled, which ensures it hasn't been recreated.
2682 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2683 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2684 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2685 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2686}
2687
deadbeefab9b2d12015-10-14 11:33:11 -07002688// This tests that a default MediaStream is not created if a remote session
2689// description is updated to not have any MediaStreams.
2690TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2691 FakeConstraints constraints;
2692 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2693 true);
2694 CreatePeerConnection(&constraints);
2695 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002696 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002697 EXPECT_TRUE(
2698 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2699
2700 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2701 EXPECT_EQ(0u, observer_.remote_streams()->count());
2702}
2703
2704// This tests that an RtpSender is created when the local description is set
2705// after adding a local stream.
2706// TODO(deadbeef): This test and the one below it need to be updated when
2707// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002708TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002709 FakeConstraints constraints;
2710 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2711 true);
2712 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002713
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002714 // Create an offer with 1 stream with 2 tracks of each type.
2715 rtc::scoped_refptr<StreamCollection> stream_collection =
2716 CreateStreamCollection(1, 2);
2717 pc_->AddStream(stream_collection->at(0));
2718 std::unique_ptr<SessionDescriptionInterface> offer;
2719 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2720 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002721
deadbeefab9b2d12015-10-14 11:33:11 -07002722 auto senders = pc_->GetSenders();
2723 EXPECT_EQ(4u, senders.size());
2724 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2725 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2726 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2727 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2728
2729 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002730 pc_->RemoveStream(stream_collection->at(0));
2731 stream_collection = CreateStreamCollection(1, 1);
2732 pc_->AddStream(stream_collection->at(0));
2733 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2734 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2735
deadbeefab9b2d12015-10-14 11:33:11 -07002736 senders = pc_->GetSenders();
2737 EXPECT_EQ(2u, senders.size());
2738 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2739 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2740 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2741 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2742}
2743
2744// This tests that an RtpSender is created when the local description is set
2745// before adding a local stream.
2746TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002747 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002748 FakeConstraints constraints;
2749 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2750 true);
2751 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002752
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002753 rtc::scoped_refptr<StreamCollection> stream_collection =
2754 CreateStreamCollection(1, 2);
2755 // Add a stream to create the offer, but remove it afterwards.
2756 pc_->AddStream(stream_collection->at(0));
2757 std::unique_ptr<SessionDescriptionInterface> offer;
2758 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2759 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002760
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002761 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002762 auto senders = pc_->GetSenders();
2763 EXPECT_EQ(0u, senders.size());
2764
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002765 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002766 senders = pc_->GetSenders();
2767 EXPECT_EQ(4u, senders.size());
2768 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2769 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2770 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2771 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2772}
2773
2774// This tests that the expected behavior occurs if the SSRC on a local track is
2775// changed when SetLocalDescription is called.
2776TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002777 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002778 FakeConstraints constraints;
2779 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2780 true);
2781 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002782
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002783 rtc::scoped_refptr<StreamCollection> stream_collection =
2784 CreateStreamCollection(2, 1);
2785 pc_->AddStream(stream_collection->at(0));
2786 std::unique_ptr<SessionDescriptionInterface> offer;
2787 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2788 // Grab a copy of the offer before it gets passed into the PC.
2789 std::unique_ptr<JsepSessionDescription> modified_offer(
2790 new JsepSessionDescription(JsepSessionDescription::kOffer));
2791 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2792 offer->session_version());
2793 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002794
deadbeefab9b2d12015-10-14 11:33:11 -07002795 auto senders = pc_->GetSenders();
2796 EXPECT_EQ(2u, senders.size());
2797 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2798 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2799
2800 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002801 cricket::MediaContentDescription* desc =
2802 cricket::GetFirstAudioContentDescription(modified_offer->description());
2803 ASSERT_TRUE(desc != NULL);
2804 for (StreamParams& stream : desc->mutable_streams()) {
2805 for (unsigned int& ssrc : stream.ssrcs) {
2806 ++ssrc;
2807 }
2808 }
deadbeefab9b2d12015-10-14 11:33:11 -07002809
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002810 desc =
2811 cricket::GetFirstVideoContentDescription(modified_offer->description());
2812 ASSERT_TRUE(desc != NULL);
2813 for (StreamParams& stream : desc->mutable_streams()) {
2814 for (unsigned int& ssrc : stream.ssrcs) {
2815 ++ssrc;
2816 }
2817 }
2818
2819 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002820 senders = pc_->GetSenders();
2821 EXPECT_EQ(2u, senders.size());
2822 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2823 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2824 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2825 // changed.
2826}
2827
2828// This tests that the expected behavior occurs if a new session description is
2829// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002830TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002831 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002832 FakeConstraints constraints;
2833 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2834 true);
2835 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002836
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002837 rtc::scoped_refptr<StreamCollection> stream_collection =
2838 CreateStreamCollection(2, 1);
2839 pc_->AddStream(stream_collection->at(0));
2840 std::unique_ptr<SessionDescriptionInterface> offer;
2841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2842 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002843
deadbeefab9b2d12015-10-14 11:33:11 -07002844 auto senders = pc_->GetSenders();
2845 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002846 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2847 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002848
2849 // Add a new MediaStream but with the same tracks as in the first stream.
2850 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2851 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002852 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2853 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002854 pc_->AddStream(stream_1);
2855
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002856 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2857 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002858
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002859 auto new_senders = pc_->GetSenders();
2860 // Should be the same senders as before, but with updated stream id.
2861 // Note that this behavior is subject to change in the future.
2862 // We may decide the PC should ignore existing tracks in AddStream.
2863 EXPECT_EQ(senders, new_senders);
2864 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2865 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002866}
2867
zhihuang81c3a032016-11-17 12:06:24 -08002868// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2869TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2870 FakeConstraints constraints;
2871 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2872 true);
2873 CreatePeerConnection(&constraints);
2874 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2875 EXPECT_EQ(observer_.num_added_tracks_, 1);
2876 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2877
2878 // Create and set the updated remote SDP.
2879 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2880 EXPECT_EQ(observer_.num_added_tracks_, 2);
2881 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2882}
2883
deadbeefd1a38b52016-12-10 13:15:33 -08002884// Test that when SetConfiguration is called and the configuration is
2885// changing, the next offer causes an ICE restart.
2886TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2887 PeerConnectionInterface::RTCConfiguration config;
2888 config.type = PeerConnectionInterface::kRelay;
2889 // Need to pass default constraints to prevent disabling of DTLS...
2890 FakeConstraints default_constraints;
2891 CreatePeerConnection(config, &default_constraints);
2892 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2893
2894 // Do initial offer/answer so there's something to restart.
2895 CreateOfferAsLocalDescription();
2896 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2897
2898 // Grab the ufrags.
2899 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2900
2901 // Change ICE policy, which should trigger an ICE restart on the next offer.
2902 config.type = PeerConnectionInterface::kAll;
2903 EXPECT_TRUE(pc_->SetConfiguration(config));
2904 CreateOfferAsLocalDescription();
2905
2906 // Grab the new ufrags.
2907 std::vector<std::string> subsequent_ufrags =
2908 GetUfrags(pc_->local_description());
2909
2910 // Sanity check.
2911 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2912 // Check that each ufrag is different.
2913 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2914 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2915 }
2916}
2917
2918// Test that when SetConfiguration is called and the configuration *isn't*
2919// changing, the next offer does *not* cause an ICE restart.
2920TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
2921 PeerConnectionInterface::RTCConfiguration config;
2922 config.type = PeerConnectionInterface::kRelay;
2923 // Need to pass default constraints to prevent disabling of DTLS...
2924 FakeConstraints default_constraints;
2925 CreatePeerConnection(config, &default_constraints);
2926 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2927
2928 // Do initial offer/answer so there's something to restart.
2929 CreateOfferAsLocalDescription();
2930 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2931
2932 // Grab the ufrags.
2933 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2934
2935 // Call SetConfiguration with a config identical to what the PC was
2936 // constructed with.
2937 EXPECT_TRUE(pc_->SetConfiguration(config));
2938 CreateOfferAsLocalDescription();
2939
2940 // Grab the new ufrags.
2941 std::vector<std::string> subsequent_ufrags =
2942 GetUfrags(pc_->local_description());
2943
2944 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
2945}
2946
2947// Test for a weird corner case scenario:
2948// 1. Audio/video session established.
2949// 2. SetConfiguration changes ICE config; ICE restart needed.
2950// 3. ICE restart initiated by remote peer, but only for one m= section.
2951// 4. Next createOffer should initiate an ICE restart, but only for the other
2952// m= section; it would be pointless to do an ICE restart for the m= section
2953// that was already restarted.
2954TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
2955 PeerConnectionInterface::RTCConfiguration config;
2956 config.type = PeerConnectionInterface::kRelay;
2957 // Need to pass default constraints to prevent disabling of DTLS...
2958 FakeConstraints default_constraints;
2959 CreatePeerConnection(config, &default_constraints);
2960 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2961
2962 // Do initial offer/answer so there's something to restart.
2963 CreateOfferAsLocalDescription();
2964 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2965
2966 // Change ICE policy, which should set the "needs-ice-restart" flag.
2967 config.type = PeerConnectionInterface::kAll;
2968 EXPECT_TRUE(pc_->SetConfiguration(config));
2969
2970 // Do ICE restart for the first m= section, initiated by remote peer.
2971 webrtc::JsepSessionDescription* remote_offer =
2972 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
2973 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
2974 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
2975 "modified";
2976 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
2977 CreateAnswerAsLocalDescription();
2978
2979 // Grab the ufrags.
2980 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2981 ASSERT_EQ(2, initial_ufrags.size());
2982
2983 // Create offer and grab the new ufrags.
2984 CreateOfferAsLocalDescription();
2985 std::vector<std::string> subsequent_ufrags =
2986 GetUfrags(pc_->local_description());
2987 ASSERT_EQ(2, subsequent_ufrags.size());
2988
2989 // Ensure that only the ufrag for the second m= section changed.
2990 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
2991 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
2992}
2993
deadbeeffe4a8a42016-12-20 17:56:17 -08002994// Tests that the methods to return current/pending descriptions work as
2995// expected at different points in the offer/answer exchange. This test does
2996// one offer/answer exchange as the offerer, then another as the answerer.
2997TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
2998 // This disables DTLS so we can apply an answer to ourselves.
2999 CreatePeerConnection();
3000
3001 // Create initial local offer and get SDP (which will also be used as
3002 // answer/pranswer);
3003 std::unique_ptr<SessionDescriptionInterface> offer;
3004 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3005 std::string sdp;
3006 EXPECT_TRUE(offer->ToString(&sdp));
3007
3008 // Set local offer.
3009 SessionDescriptionInterface* local_offer = offer.release();
3010 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3011 EXPECT_EQ(local_offer, pc_->pending_local_description());
3012 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3013 EXPECT_EQ(nullptr, pc_->current_local_description());
3014 EXPECT_EQ(nullptr, pc_->current_remote_description());
3015
3016 // Set remote pranswer.
3017 SessionDescriptionInterface* remote_pranswer =
3018 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3019 sdp, nullptr);
3020 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3021 EXPECT_EQ(local_offer, pc_->pending_local_description());
3022 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3023 EXPECT_EQ(nullptr, pc_->current_local_description());
3024 EXPECT_EQ(nullptr, pc_->current_remote_description());
3025
3026 // Set remote answer.
3027 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3028 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3029 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3030 EXPECT_EQ(nullptr, pc_->pending_local_description());
3031 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3032 EXPECT_EQ(local_offer, pc_->current_local_description());
3033 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3034
3035 // Set remote offer.
3036 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3037 SessionDescriptionInterface::kOffer, sdp, nullptr);
3038 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3039 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3040 EXPECT_EQ(nullptr, pc_->pending_local_description());
3041 EXPECT_EQ(local_offer, pc_->current_local_description());
3042 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3043
3044 // Set local pranswer.
3045 SessionDescriptionInterface* local_pranswer =
3046 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3047 sdp, nullptr);
3048 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3049 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3050 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3051 EXPECT_EQ(local_offer, pc_->current_local_description());
3052 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3053
3054 // Set local answer.
3055 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3056 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3057 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3058 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3059 EXPECT_EQ(nullptr, pc_->pending_local_description());
3060 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3061 EXPECT_EQ(local_answer, pc_->current_local_description());
3062}
3063
nisse51542be2016-02-12 02:27:06 -08003064class PeerConnectionMediaConfigTest : public testing::Test {
3065 protected:
3066 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003067 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003068 pcf_->Initialize();
3069 }
3070 const cricket::MediaConfig& TestCreatePeerConnection(
3071 const PeerConnectionInterface::RTCConfiguration& config,
3072 const MediaConstraintsInterface *constraints) {
3073 pcf_->create_media_controller_called_ = false;
3074
zhihuang9763d562016-08-05 11:14:50 -07003075 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3076 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003077 EXPECT_TRUE(pc.get());
3078 EXPECT_TRUE(pcf_->create_media_controller_called_);
3079 return pcf_->create_media_controller_config_;
3080 }
3081
zhihuang9763d562016-08-05 11:14:50 -07003082 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003083 MockPeerConnectionObserver observer_;
3084};
3085
3086// This test verifies the default behaviour with no constraints and a
3087// default RTCConfiguration.
3088TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3089 PeerConnectionInterface::RTCConfiguration config;
3090 FakeConstraints constraints;
3091
3092 const cricket::MediaConfig& media_config =
3093 TestCreatePeerConnection(config, &constraints);
3094
3095 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003096 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3097 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3098 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003099}
3100
3101// This test verifies the DSCP constraint is recognized and passed to
3102// the CreateMediaController call.
3103TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3104 PeerConnectionInterface::RTCConfiguration config;
3105 FakeConstraints constraints;
3106
3107 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3108 const cricket::MediaConfig& media_config =
3109 TestCreatePeerConnection(config, &constraints);
3110
3111 EXPECT_TRUE(media_config.enable_dscp);
3112}
3113
3114// This test verifies the cpu overuse detection constraint is
3115// recognized and passed to the CreateMediaController call.
3116TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3117 PeerConnectionInterface::RTCConfiguration config;
3118 FakeConstraints constraints;
3119
3120 constraints.AddOptional(
3121 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3122 const cricket::MediaConfig media_config =
3123 TestCreatePeerConnection(config, &constraints);
3124
nisse0db023a2016-03-01 04:29:59 -08003125 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003126}
3127
3128// This test verifies that the disable_prerenderer_smoothing flag is
3129// propagated from RTCConfiguration to the CreateMediaController call.
3130TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3131 PeerConnectionInterface::RTCConfiguration config;
3132 FakeConstraints constraints;
3133
Niels Möller71bdda02016-03-31 12:59:59 +02003134 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003135 const cricket::MediaConfig& media_config =
3136 TestCreatePeerConnection(config, &constraints);
3137
nisse0db023a2016-03-01 04:29:59 -08003138 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3139}
3140
3141// This test verifies the suspend below min bitrate constraint is
3142// recognized and passed to the CreateMediaController call.
3143TEST_F(PeerConnectionMediaConfigTest,
3144 TestSuspendBelowMinBitrateConstraintTrue) {
3145 PeerConnectionInterface::RTCConfiguration config;
3146 FakeConstraints constraints;
3147
3148 constraints.AddOptional(
3149 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3150 true);
3151 const cricket::MediaConfig media_config =
3152 TestCreatePeerConnection(config, &constraints);
3153
3154 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003155}
3156
deadbeefab9b2d12015-10-14 11:33:11 -07003157// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003158// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3159// "verify options are converted correctly", should be "pass options into
3160// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003161
3162TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3163 RTCOfferAnswerOptions rtc_options;
3164 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3165
3166 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003167 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003168
3169 rtc_options.offer_to_receive_audio =
3170 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003171 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003172}
3173
3174TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3175 RTCOfferAnswerOptions rtc_options;
3176 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3177
3178 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003179 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003180
3181 rtc_options.offer_to_receive_video =
3182 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003183 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003184}
3185
3186// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003187// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003188TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3189 RTCOfferAnswerOptions rtc_options;
3190 rtc_options.offer_to_receive_audio = 1;
3191 rtc_options.offer_to_receive_video = 1;
3192
3193 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003194 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003195 EXPECT_TRUE(options.has_audio());
3196 EXPECT_TRUE(options.has_video());
3197 EXPECT_TRUE(options.bundle_enabled);
3198}
3199
3200// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003201// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003202TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3203 RTCOfferAnswerOptions rtc_options;
3204 rtc_options.offer_to_receive_audio = 1;
3205
3206 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003207 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003208 EXPECT_TRUE(options.has_audio());
3209 EXPECT_FALSE(options.has_video());
3210 EXPECT_TRUE(options.bundle_enabled);
3211}
3212
3213// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003214// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003215TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3216 RTCOfferAnswerOptions rtc_options;
3217
3218 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003219 options.transport_options["audio"] = cricket::TransportOptions();
3220 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003221 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003222 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003223 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003224 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003225 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003226 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3227 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003228}
3229
3230// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003231// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003232TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3233 RTCOfferAnswerOptions rtc_options;
3234 rtc_options.offer_to_receive_audio = 0;
3235 rtc_options.offer_to_receive_video = 1;
3236
3237 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003238 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003239 EXPECT_FALSE(options.has_audio());
3240 EXPECT_TRUE(options.has_video());
3241 EXPECT_TRUE(options.bundle_enabled);
3242}
3243
3244// Test that a correct MediaSessionOptions is created for an offer if
3245// UseRtpMux is set to false.
3246TEST(CreateSessionOptionsTest,
3247 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3248 RTCOfferAnswerOptions rtc_options;
3249 rtc_options.offer_to_receive_audio = 1;
3250 rtc_options.offer_to_receive_video = 1;
3251 rtc_options.use_rtp_mux = false;
3252
3253 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003254 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003255 EXPECT_TRUE(options.has_audio());
3256 EXPECT_TRUE(options.has_video());
3257 EXPECT_FALSE(options.bundle_enabled);
3258}
3259
3260// Test that a correct MediaSessionOptions is created to restart ice if
3261// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003262// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003263TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3264 RTCOfferAnswerOptions rtc_options;
3265 rtc_options.ice_restart = true;
3266
3267 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003268 options.transport_options["audio"] = cricket::TransportOptions();
3269 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003270 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003271 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3272 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003273
3274 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003275 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003276 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3277 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003278}
3279
3280// Test that the MediaConstraints in an answer don't affect if audio and video
3281// is offered in an offer but that if kOfferToReceiveAudio or
3282// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3283// included in subsequent answers.
3284TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3285 FakeConstraints answer_c;
3286 answer_c.SetMandatoryReceiveAudio(true);
3287 answer_c.SetMandatoryReceiveVideo(true);
3288
3289 cricket::MediaSessionOptions answer_options;
3290 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3291 EXPECT_TRUE(answer_options.has_audio());
3292 EXPECT_TRUE(answer_options.has_video());
3293
deadbeefc80741f2015-10-22 13:14:45 -07003294 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003295
3296 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003297 EXPECT_TRUE(
3298 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003299 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003300 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003301
deadbeefc80741f2015-10-22 13:14:45 -07003302 RTCOfferAnswerOptions updated_rtc_offer_options;
3303 updated_rtc_offer_options.offer_to_receive_audio = 1;
3304 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003305
3306 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003307 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003308 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003309 EXPECT_TRUE(updated_offer_options.has_audio());
3310 EXPECT_TRUE(updated_offer_options.has_video());
3311
3312 // Since an offer has been created with both audio and video, subsequent
3313 // offers and answers should contain both audio and video.
3314 // Answers will only contain the media types that exist in the offer
3315 // regardless of the value of |updated_answer_options.has_audio| and
3316 // |updated_answer_options.has_video|.
3317 FakeConstraints updated_answer_c;
3318 answer_c.SetMandatoryReceiveAudio(false);
3319 answer_c.SetMandatoryReceiveVideo(false);
3320
3321 cricket::MediaSessionOptions updated_answer_options;
3322 EXPECT_TRUE(
3323 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3324 EXPECT_TRUE(updated_answer_options.has_audio());
3325 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003326}
deadbeef3edec7c2016-12-10 11:44:26 -08003327
deadbeef293e9262017-01-11 12:28:30 -08003328TEST(RTCErrorTypeTest, OstreamOperator) {
deadbeef3edec7c2016-12-10 11:44:26 -08003329 std::ostringstream oss;
deadbeef293e9262017-01-11 12:28:30 -08003330 oss << webrtc::RTCErrorType::NONE << ' '
3331 << webrtc::RTCErrorType::INVALID_PARAMETER << ' '
3332 << webrtc::RTCErrorType::INTERNAL_ERROR;
deadbeef3edec7c2016-12-10 11:44:26 -08003333 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str());
3334}
deadbeef293e9262017-01-11 12:28:30 -08003335
3336// Tests a few random fields being different.
3337TEST(RTCConfigurationTest, ComparisonOperators) {
3338 PeerConnectionInterface::RTCConfiguration a;
3339 PeerConnectionInterface::RTCConfiguration b;
3340 EXPECT_EQ(a, b);
3341
3342 PeerConnectionInterface::RTCConfiguration c;
3343 c.servers.push_back(PeerConnectionInterface::IceServer());
3344 EXPECT_NE(a, c);
3345
3346 PeerConnectionInterface::RTCConfiguration d;
3347 d.type = PeerConnectionInterface::kRelay;
3348 EXPECT_NE(a, d);
3349
3350 PeerConnectionInterface::RTCConfiguration e;
3351 e.audio_jitter_buffer_max_packets = 5;
3352 EXPECT_NE(a, e);
3353
3354 PeerConnectionInterface::RTCConfiguration f;
3355 f.ice_connection_receiving_timeout = 1337;
3356 EXPECT_NE(a, f);
3357
3358 PeerConnectionInterface::RTCConfiguration g;
3359 g.disable_ipv6 = true;
3360 EXPECT_NE(a, g);
3361
3362 PeerConnectionInterface::RTCConfiguration h(
3363 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3364 EXPECT_NE(a, h);
3365}