blob: ccf64d241594c3055f47a6a95f7d7053aba86dd7 [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
Zhi Huang2a5e4262017-09-14 01:15:03 -070011#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070012#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080013#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
kwiberg087bd342017-02-10 08:15:44 -080017#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
ossueb1fde42017-05-02 06:46:30 -070018#include "webrtc/api/audio_codecs/builtin_audio_encoder_factory.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010024#include "webrtc/api/test/fakeconstraints.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/fakevideocapturer.h"
zhihuang38ede132017-06-15 12:52:32 -070026#include "webrtc/media/engine/webrtcmediaengine.h"
deadbeef953c2ce2017-01-09 14:53:41 -080027#include "webrtc/media/sctp/sctptransportinternal.h"
peaha9cc40b2017-06-29 08:32:09 -070028#include "webrtc/modules/audio_processing/include/audio_processing.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070029#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080030#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010031#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080032#include "webrtc/pc/mediastream.h"
33#include "webrtc/pc/peerconnection.h"
34#include "webrtc/pc/streamcollection.h"
deadbeefd7850b22017-08-23 10:59:19 -070035#include "webrtc/pc/test/fakeaudiocapturemodule.h"
ossu7bb87ee2017-01-23 04:56:25 -080036#include "webrtc/pc/test/fakertccertificategenerator.h"
37#include "webrtc/pc/test/fakevideotracksource.h"
38#include "webrtc/pc/test/mockpeerconnectionobservers.h"
39#include "webrtc/pc/test/testsdpstrings.h"
40#include "webrtc/pc/videocapturertracksource.h"
41#include "webrtc/pc/videotrack.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020042#include "webrtc/rtc_base/gunit.h"
43#include "webrtc/rtc_base/ssladapter.h"
44#include "webrtc/rtc_base/sslstreamadapter.h"
45#include "webrtc/rtc_base/stringutils.h"
46#include "webrtc/rtc_base/thread.h"
47#include "webrtc/rtc_base/virtualsocketserver.h"
kwibergac9f8762016-09-30 22:29:43 -070048#include "webrtc/test/gmock.h"
49
50#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080051#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070052#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54static const char kStreamLabel1[] = "local_stream_1";
55static const char kStreamLabel2[] = "local_stream_2";
56static const char kStreamLabel3[] = "local_stream_3";
57static const int kDefaultStunPort = 3478;
58static const char kStunAddressOnly[] = "stun:address";
59static const char kStunInvalidPort[] = "stun:address:-1";
60static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
61static const char kStunAddressPortAndMore2[] = "stun:address:port more";
62static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
63static const char kTurnUsername[] = "user";
64static const char kTurnPassword[] = "password";
65static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020066static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
deadbeefab9b2d12015-10-14 11:33:11 -070068static const char kStreams[][8] = {"stream1", "stream2"};
69static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
70static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
71
deadbeef5e97fb52015-10-15 12:49:08 -070072static const char kRecvonly[] = "recvonly";
73static const char kSendrecv[] = "sendrecv";
74
deadbeefab9b2d12015-10-14 11:33:11 -070075// Reference SDP with a MediaStream with label "stream1" and audio track with
76// id "audio_1" and a video track with id "video_1;
77static const char kSdpStringWithStream1[] =
78 "v=0\r\n"
79 "o=- 0 0 IN IP4 127.0.0.1\r\n"
80 "s=-\r\n"
81 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080082 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=ice-ufrag:e5785931\r\n"
84 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
85 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
86 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080089 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070090 "a=rtpmap:103 ISAC/16000\r\n"
91 "a=ssrc:1 cname:stream1\r\n"
92 "a=ssrc:1 mslabel:stream1\r\n"
93 "a=ssrc:1 label:audiotrack0\r\n"
94 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080095 "a=ice-ufrag:e5785931\r\n"
96 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
97 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
98 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700100 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800101 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700102 "a=rtpmap:120 VP8/90000\r\n"
103 "a=ssrc:2 cname:stream1\r\n"
104 "a=ssrc:2 mslabel:stream1\r\n"
105 "a=ssrc:2 label:videotrack0\r\n";
106
zhihuang81c3a032016-11-17 12:06:24 -0800107// Reference SDP with a MediaStream with label "stream1" and audio track with
108// id "audio_1";
109static const char kSdpStringWithStream1AudioTrackOnly[] =
110 "v=0\r\n"
111 "o=- 0 0 IN IP4 127.0.0.1\r\n"
112 "s=-\r\n"
113 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800114 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800115 "a=ice-ufrag:e5785931\r\n"
116 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
117 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
118 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800119 "a=mid:audio\r\n"
120 "a=sendrecv\r\n"
121 "a=rtpmap:103 ISAC/16000\r\n"
122 "a=ssrc:1 cname:stream1\r\n"
123 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800124 "a=ssrc:1 label:audiotrack0\r\n"
125 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800126
deadbeefab9b2d12015-10-14 11:33:11 -0700127// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
128// MediaStreams have one audio track and one video track.
129// This uses MSID.
130static const char kSdpStringWithStream1And2[] =
131 "v=0\r\n"
132 "o=- 0 0 IN IP4 127.0.0.1\r\n"
133 "s=-\r\n"
134 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800135 "a=msid-semantic: WMS stream1 stream2\r\n"
136 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=ice-ufrag:e5785931\r\n"
138 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
139 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
140 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700142 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800143 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=rtpmap:103 ISAC/16000\r\n"
145 "a=ssrc:1 cname:stream1\r\n"
146 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
147 "a=ssrc:3 cname:stream2\r\n"
148 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
149 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800150 "a=ice-ufrag:e5785931\r\n"
151 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
152 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
153 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700155 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800156 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:120 VP8/0\r\n"
158 "a=ssrc:2 cname:stream1\r\n"
159 "a=ssrc:2 msid:stream1 videotrack0\r\n"
160 "a=ssrc:4 cname:stream2\r\n"
161 "a=ssrc:4 msid:stream2 videotrack1\r\n";
162
163// Reference SDP without MediaStreams. Msid is not supported.
164static const char kSdpStringWithoutStreams[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800169 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=ice-ufrag:e5785931\r\n"
171 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
172 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
173 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800176 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=rtpmap:103 ISAC/16000\r\n"
178 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800179 "a=ice-ufrag:e5785931\r\n"
180 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
181 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
182 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700184 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800185 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700186 "a=rtpmap:120 VP8/90000\r\n";
187
188// Reference SDP without MediaStreams. Msid is supported.
189static const char kSdpStringWithMsidWithoutStreams[] =
190 "v=0\r\n"
191 "o=- 0 0 IN IP4 127.0.0.1\r\n"
192 "s=-\r\n"
193 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800194 "a=msid-semantic: WMS\r\n"
195 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=ice-ufrag:e5785931\r\n"
197 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
198 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
199 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700201 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800202 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=rtpmap:103 ISAC/16000\r\n"
204 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800205 "a=ice-ufrag:e5785931\r\n"
206 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
207 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
208 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800211 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:120 VP8/90000\r\n";
213
214// Reference SDP without MediaStreams and audio only.
215static const char kSdpStringWithoutStreamsAudioOnly[] =
216 "v=0\r\n"
217 "o=- 0 0 IN IP4 127.0.0.1\r\n"
218 "s=-\r\n"
219 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800220 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=ice-ufrag:e5785931\r\n"
222 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
223 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
224 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700226 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:103 ISAC/16000\r\n";
229
230// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
231static const char kSdpStringSendOnlyWithoutStreams[] =
232 "v=0\r\n"
233 "o=- 0 0 IN IP4 127.0.0.1\r\n"
234 "s=-\r\n"
235 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800236 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=ice-ufrag:e5785931\r\n"
238 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
239 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
240 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700242 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800244 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=rtpmap:103 ISAC/16000\r\n"
246 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800247 "a=ice-ufrag:e5785931\r\n"
248 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
249 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
250 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700252 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800254 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700255 "a=rtpmap:120 VP8/90000\r\n";
256
257static const char kSdpStringInit[] =
258 "v=0\r\n"
259 "o=- 0 0 IN IP4 127.0.0.1\r\n"
260 "s=-\r\n"
261 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700262 "a=msid-semantic: WMS\r\n";
263
264static const char kSdpStringAudio[] =
265 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800266 "a=ice-ufrag:e5785931\r\n"
267 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
268 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
269 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700271 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800272 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700273 "a=rtpmap:103 ISAC/16000\r\n";
274
275static const char kSdpStringVideo[] =
276 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800277 "a=ice-ufrag:e5785931\r\n"
278 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
279 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
280 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700282 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800283 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700284 "a=rtpmap:120 VP8/90000\r\n";
285
286static const char kSdpStringMs1Audio0[] =
287 "a=ssrc:1 cname:stream1\r\n"
288 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
289
290static const char kSdpStringMs1Video0[] =
291 "a=ssrc:2 cname:stream1\r\n"
292 "a=ssrc:2 msid:stream1 videotrack0\r\n";
293
294static const char kSdpStringMs1Audio1[] =
295 "a=ssrc:3 cname:stream1\r\n"
296 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
297
298static const char kSdpStringMs1Video1[] =
299 "a=ssrc:4 cname:stream1\r\n"
300 "a=ssrc:4 msid:stream1 videotrack1\r\n";
301
deadbeef8662f942017-01-20 21:20:51 -0800302static const char kDtlsSdesFallbackSdp[] =
303 "v=0\r\n"
304 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
305 "s=-\r\n"
306 "c=IN IP4 0.0.0.0\r\n"
307 "t=0 0\r\n"
308 "a=group:BUNDLE audio\r\n"
309 "a=msid-semantic: WMS\r\n"
310 "m=audio 1 RTP/SAVPF 0\r\n"
311 "a=sendrecv\r\n"
312 "a=rtcp-mux\r\n"
313 "a=mid:audio\r\n"
314 "a=ssrc:1 cname:stream1\r\n"
315 "a=ssrc:1 mslabel:stream1\r\n"
316 "a=ssrc:1 label:audiotrack0\r\n"
317 "a=ice-ufrag:e5785931\r\n"
318 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
319 "a=rtpmap:0 pcmu/8000\r\n"
320 "a=fingerprint:sha-1 "
321 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
322 "a=setup:actpass\r\n"
323 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
324 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
325 "dummy_session_params\r\n";
326
perkjd61bf802016-03-24 03:16:19 -0700327using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700328using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700330using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331using webrtc::AudioTrackInterface;
332using webrtc::DataBuffer;
333using webrtc::DataChannelInterface;
334using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700336using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700337using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338using webrtc::MediaStreamInterface;
339using webrtc::MediaStreamTrackInterface;
340using webrtc::MockCreateSessionDescriptionObserver;
341using webrtc::MockDataChannelObserver;
342using webrtc::MockSetSessionDescriptionObserver;
343using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700344using webrtc::NotifierInterface;
345using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346using webrtc::PeerConnectionInterface;
347using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800348using webrtc::RTCError;
349using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700350using webrtc::RtpReceiverInterface;
351using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352using webrtc::SdpParseError;
353using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700354using webrtc::StreamCollection;
355using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100356using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700357using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358using webrtc::VideoTrackInterface;
359
deadbeefab9b2d12015-10-14 11:33:11 -0700360typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362namespace {
363
364// Gets the first ssrc of given content type from the ContentInfo.
365bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
366 if (!content_info || !ssrc) {
367 return false;
368 }
369 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000370 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 content_info->description);
372 if (!media_desc || media_desc->streams().empty()) {
373 return false;
374 }
375 *ssrc = media_desc->streams().begin()->first_ssrc();
376 return true;
377}
378
deadbeefd1a38b52016-12-10 13:15:33 -0800379// Get the ufrags out of an SDP blob. Useful for testing ICE restart
380// behavior.
381std::vector<std::string> GetUfrags(
382 const webrtc::SessionDescriptionInterface* desc) {
383 std::vector<std::string> ufrags;
384 for (const cricket::TransportInfo& info :
385 desc->description()->transport_infos()) {
386 ufrags.push_back(info.description.ice_ufrag);
387 }
388 return ufrags;
389}
390
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391void SetSsrcToZero(std::string* sdp) {
392 const char kSdpSsrcAtribute[] = "a=ssrc:";
393 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
394 size_t ssrc_pos = 0;
395 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
396 std::string::npos) {
397 size_t end_ssrc = sdp->find(" ", ssrc_pos);
398 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
399 ssrc_pos = end_ssrc;
400 }
401}
402
deadbeefab9b2d12015-10-14 11:33:11 -0700403// Check if |streams| contains the specified track.
404bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
405 const std::string& stream_label,
406 const std::string& track_id) {
407 for (const cricket::StreamParams& params : streams) {
408 if (params.sync_label == stream_label && params.id == track_id) {
409 return true;
410 }
411 }
412 return false;
413}
414
415// Check if |senders| contains the specified sender, by id.
416bool ContainsSender(
417 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
418 const std::string& id) {
419 for (const auto& sender : senders) {
420 if (sender->id() == id) {
421 return true;
422 }
423 }
424 return false;
425}
426
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700427// Check if |senders| contains the specified sender, by id and stream id.
428bool ContainsSender(
429 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
430 const std::string& id,
431 const std::string& stream_id) {
432 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700433 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700434 return true;
435 }
436 }
437 return false;
438}
439
deadbeefab9b2d12015-10-14 11:33:11 -0700440// Create a collection of streams.
441// CreateStreamCollection(1) creates a collection that
442// correspond to kSdpStringWithStream1.
443// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
444rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700445 int number_of_streams,
446 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700447 rtc::scoped_refptr<StreamCollection> local_collection(
448 StreamCollection::Create());
449
450 for (int i = 0; i < number_of_streams; ++i) {
451 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
452 webrtc::MediaStream::Create(kStreams[i]));
453
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700454 for (int j = 0; j < tracks_per_stream; ++j) {
455 // Add a local audio track.
456 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
457 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
458 nullptr));
459 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700460
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700461 // Add a local video track.
462 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
463 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700464 webrtc::FakeVideoTrackSource::Create(),
465 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700466 stream->AddTrack(video_track);
467 }
deadbeefab9b2d12015-10-14 11:33:11 -0700468
469 local_collection->AddStream(stream);
470 }
471 return local_collection;
472}
473
474// Check equality of StreamCollections.
475bool CompareStreamCollections(StreamCollectionInterface* s1,
476 StreamCollectionInterface* s2) {
477 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
478 return false;
479 }
480
481 for (size_t i = 0; i != s1->count(); ++i) {
482 if (s1->at(i)->label() != s2->at(i)->label()) {
483 return false;
484 }
485 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
486 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
487 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
488 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
489
490 if (audio_tracks1.size() != audio_tracks2.size()) {
491 return false;
492 }
493 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
494 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
495 return false;
496 }
497 }
498 if (video_tracks1.size() != video_tracks2.size()) {
499 return false;
500 }
501 for (size_t j = 0; j != video_tracks1.size(); ++j) {
502 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
503 return false;
504 }
505 }
506 }
507 return true;
508}
509
perkjd61bf802016-03-24 03:16:19 -0700510// Helper class to test Observer.
511class MockTrackObserver : public ObserverInterface {
512 public:
513 explicit MockTrackObserver(NotifierInterface* notifier)
514 : notifier_(notifier) {
515 notifier_->RegisterObserver(this);
516 }
517
518 ~MockTrackObserver() { Unregister(); }
519
520 void Unregister() {
521 if (notifier_) {
522 notifier_->UnregisterObserver(this);
523 notifier_ = nullptr;
524 }
525 }
526
527 MOCK_METHOD0(OnChanged, void());
528
529 private:
530 NotifierInterface* notifier_;
531};
532
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533class MockPeerConnectionObserver : public PeerConnectionObserver {
534 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700535 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200536 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
538 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
539 pc_ = pc;
540 if (pc) {
541 state_ = pc_->signaling_state();
542 }
543 }
nisseef8b61e2016-04-29 06:09:15 -0700544 void OnSignalingChange(
545 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 EXPECT_EQ(pc_->signaling_state(), new_state);
547 state_ = new_state;
548 }
deadbeefab9b2d12015-10-14 11:33:11 -0700549
550 MediaStreamInterface* RemoteStream(const std::string& label) {
551 return remote_streams_->find(label);
552 }
553 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700554 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700556 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700558 void OnRemoveStream(
559 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700561 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
perkjdfb769d2016-02-09 03:09:43 -0800563 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700564 void OnDataChannel(
565 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 last_datachannel_ = data_channel;
567 }
568
perkjdfb769d2016-02-09 03:09:43 -0800569 void OnIceConnectionChange(
570 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800572 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
perkjdfb769d2016-02-09 03:09:43 -0800574 void OnIceGatheringChange(
575 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800577 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800578 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
perkjdfb769d2016-02-09 03:09:43 -0800580 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
582 pc_->ice_gathering_state());
583
584 std::string sdp;
585 EXPECT_TRUE(candidate->ToString(&sdp));
586 EXPECT_LT(0u, sdp.size());
587 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
588 candidate->sdp_mline_index(), sdp, NULL));
589 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800590 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700591 }
592
593 void OnIceCandidatesRemoved(
594 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800595 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700596 }
597
598 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800599 callback_triggered_ = true;
600 }
601
zhihuangc63b8942016-12-02 15:41:10 -0800602 void OnAddTrack(
603 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
604 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
605 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800606 EXPECT_TRUE(receiver != nullptr);
607 num_added_tracks_++;
608 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
611 // Returns the label of the last added stream.
612 // Empty string if no stream have been added.
613 std::string GetLastAddedStreamLabel() {
614 if (last_added_stream_.get())
615 return last_added_stream_->label();
616 return "";
617 }
618 std::string GetLastRemovedStreamLabel() {
619 if (last_removed_stream_.get())
620 return last_removed_stream_->label();
621 return "";
622 }
623
zhihuang9763d562016-08-05 11:14:50 -0700624 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700626 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700627 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700628 rtc::scoped_refptr<StreamCollection> remote_streams_;
629 bool renegotiation_needed_ = false;
630 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800631 bool callback_triggered_ = false;
632 int num_added_tracks_ = 0;
633 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
635 private:
zhihuang9763d562016-08-05 11:14:50 -0700636 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
637 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638};
639
640} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700641
nisse528b7932017-05-08 03:21:43 -0700642// The PeerConnectionMediaConfig tests below verify that configuration and
643// constraints are propagated into the PeerConnection's MediaConfig. These
644// settings are intended for MediaChannel constructors, but that is not
645// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700646class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
647 public:
zhihuang38ede132017-06-15 12:52:32 -0700648 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
649 CreatePeerConnectionFactoryForTest() {
650 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
651 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
652
653 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
654 cricket::WebRtcMediaEngineFactory::Create(
655 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700656 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700657
658 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
659 webrtc::CreateCallFactory();
660
661 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
662 webrtc::CreateRtcEventLogFactory();
663
664 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
665 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700666 FakeAudioCaptureModule::Create(), audio_encoder_factory,
667 audio_decoder_factory, nullptr, nullptr, nullptr,
668 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700669 std::move(event_log_factory));
670 }
671
672 PeerConnectionFactoryForTest(
673 rtc::Thread* network_thread,
674 rtc::Thread* worker_thread,
675 rtc::Thread* signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700676 rtc::scoped_refptr<FakeAudioCaptureModule> fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700677 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
678 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
679 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
680 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
681 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
682 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
683 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
684 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
685 : webrtc::PeerConnectionFactory(network_thread,
686 worker_thread,
687 signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700688 fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700689 audio_encoder_factory,
690 audio_decoder_factory,
691 video_encoder_factory,
692 video_decoder_factory,
693 audio_mixer,
694 std::move(media_engine),
695 std::move(call_factory),
696 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800697
zhihuang29ff8442016-07-27 11:07:25 -0700698 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700699 cricket::PortAllocator* port_allocator,
700 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700701 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700702 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700703 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700704 return transport_controller;
705 }
706
deadbeefd7850b22017-08-23 10:59:19 -0700707 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700708 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700709};
710
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711class PeerConnectionInterfaceTest : public testing::Test {
712 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700713 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700714 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800715#ifdef WEBRTC_ANDROID
716 webrtc::InitializeAndroidObjects();
717#endif
718 }
719
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700721 // Use fake audio capture module since we're only testing the interface
722 // level, and using a real one could make tests flaky when run in parallel.
723 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700725 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700726 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700727 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700728 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700729 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700730 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732
733 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736
deadbeef293e9262017-01-11 12:28:30 -0800737 // DTLS does not work in a loopback call, so is disabled for most of the
738 // tests in this file.
739 void CreatePeerConnectionWithoutDtls() {
740 FakeConstraints no_dtls_constraints;
741 no_dtls_constraints.AddMandatory(
742 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
743
744 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
745 &no_dtls_constraints);
746 }
747
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700749 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
750 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700753 void CreatePeerConnectionWithIceTransportsType(
754 PeerConnectionInterface::IceTransportsType type) {
755 PeerConnectionInterface::RTCConfiguration config;
756 config.type = type;
757 return CreatePeerConnection(config, nullptr);
758 }
759
760 void CreatePeerConnectionWithIceServer(const std::string& uri,
761 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800762 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764 server.uri = uri;
765 server.password = password;
766 config.servers.push_back(server);
767 CreatePeerConnection(config, nullptr);
768 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700770 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
771 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700772 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
774 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000775
deadbeef1dcb1642017-03-29 21:08:16 -0700776 // Create certificate generator unless DTLS constraint is explicitly set to
777 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200778 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000779 bool dtls;
780 if (FindConstraint(constraints,
781 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
782 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200783 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800784 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
785 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000786 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200787 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800788 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200789 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 ASSERT_TRUE(pc_.get() != NULL);
791 observer_.SetPeerConnectionInterface(pc_.get());
792 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
793 }
794
deadbeef0a6c4ca2015-10-06 11:38:28 -0700795 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700797 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700798 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800799 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700800
zhihuang9763d562016-08-05 11:14:50 -0700801 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700802 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
803 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700805 }
806
Steve Anton038834f2017-07-14 15:59:59 -0700807 void CreatePeerConnectionExpectFail(
808 PeerConnectionInterface::RTCConfiguration config) {
809 PeerConnectionInterface::IceServer server;
810 server.uri = kTurnIceServerUri;
811 server.password = kTurnPassword;
812 config.servers.push_back(server);
813 rtc::scoped_refptr<PeerConnectionInterface> pc =
814 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
815 EXPECT_EQ(nullptr, pc);
816 }
817
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700819 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800820 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
821 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
822 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800824 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825
deadbeef0a6c4ca2015-10-06 11:38:28 -0700826 CreatePeerConnectionExpectFail(kStunInvalidPort);
827 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
828 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700830 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800831 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
832 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800834 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800836 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800838 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840
841 void ReleasePeerConnection() {
842 pc_ = NULL;
843 observer_.SetPeerConnectionInterface(NULL);
844 }
845
deadbeefab9b2d12015-10-14 11:33:11 -0700846 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700848 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700850 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800851 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
852 new cricket::FakeVideoCapturer()),
853 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700854 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 pc_factory_->CreateVideoTrack(label + "v0", video_source));
856 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000857 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
859 observer_.renegotiation_needed_ = false;
860 }
861
862 void AddVoiceStream(const std::string& label) {
863 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700864 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700866 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 pc_factory_->CreateAudioTrack(label + "a0", NULL));
868 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000869 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
871 observer_.renegotiation_needed_ = false;
872 }
873
874 void AddAudioVideoStream(const std::string& stream_label,
875 const std::string& audio_track_label,
876 const std::string& video_track_label) {
877 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700878 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700880 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 pc_factory_->CreateAudioTrack(
882 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
883 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700884 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700885 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800886 video_track_label, pc_factory_->CreateVideoSource(
887 std::unique_ptr<cricket::VideoCapturer>(
888 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000890 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
892 observer_.renegotiation_needed_ = false;
893 }
894
kwibergd1fe2812016-04-27 06:47:29 -0700895 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700896 bool offer,
897 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000898 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
899 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 MockCreateSessionDescriptionObserver>());
901 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700902 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700904 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700907 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 return observer->result();
909 }
910
kwibergd1fe2812016-04-27 06:47:29 -0700911 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700912 MediaConstraintsInterface* constraints) {
913 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
kwibergd1fe2812016-04-27 06:47:29 -0700916 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700917 MediaConstraintsInterface* constraints) {
918 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
Steve Antondb45ca82017-09-11 18:27:34 -0700921 bool DoSetSessionDescription(
922 std::unique_ptr<SessionDescriptionInterface> desc,
923 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000924 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
925 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 MockSetSessionDescriptionObserver>());
927 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700928 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700930 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 }
zhihuang29ff8442016-07-27 11:07:25 -0700932 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
933 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
934 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 return observer->result();
936 }
937
Steve Antondb45ca82017-09-11 18:27:34 -0700938 bool DoSetLocalDescription(
939 std::unique_ptr<SessionDescriptionInterface> desc) {
940 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 }
942
Steve Antondb45ca82017-09-11 18:27:34 -0700943 bool DoSetRemoteDescription(
944 std::unique_ptr<SessionDescriptionInterface> desc) {
945 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 }
947
948 // Calls PeerConnection::GetStats and check the return value.
949 // It does not verify the values in the StatReports since a RTCP packet might
950 // be required.
951 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000952 rtc::scoped_refptr<MockStatsObserver> observer(
953 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000954 if (!pc_->GetStats(
955 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 return false;
957 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
958 return observer->called();
959 }
960
961 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800962 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 // Create a local stream with audio&video tracks.
964 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
965 CreateOfferReceiveAnswer();
966 }
967
968 // Verify that RTP Header extensions has been negotiated for audio and video.
969 void VerifyRemoteRtpHeaderExtensions() {
970 const cricket::MediaContentDescription* desc =
971 cricket::GetFirstAudioContentDescription(
972 pc_->remote_description()->description());
973 ASSERT_TRUE(desc != NULL);
974 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
975
976 desc = cricket::GetFirstVideoContentDescription(
977 pc_->remote_description()->description());
978 ASSERT_TRUE(desc != NULL);
979 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
980 }
981
982 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700983 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700984 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 std::string sdp;
986 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700987 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700989 sdp, nullptr));
990 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
992 }
993
deadbeefab9b2d12015-10-14 11:33:11 -0700994 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700995 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700996 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700997 sdp, nullptr));
998 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700999 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1000 }
1001
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001003 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001004 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005
1006 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1007 // audio codec change, even if the parameter has nothing to do with
1008 // receiving. Not all parameters are serialized to SDP.
1009 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1010 // the SessionDescription, it is necessary to do that here to in order to
1011 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1012 // https://code.google.com/p/webrtc/issues/detail?id=1356
1013 std::string sdp;
1014 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001015 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001017 sdp, nullptr));
1018 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1020 }
1021
1022 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001023 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001024 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025
1026 std::string sdp;
1027 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001028 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001030 sdp, nullptr));
1031 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1033 }
1034
1035 void CreateOfferReceiveAnswer() {
1036 CreateOfferAsLocalDescription();
1037 std::string sdp;
1038 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1039 CreateAnswerAsRemoteDescription(sdp);
1040 }
1041
1042 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001043 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001044 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1046 // audio codec change, even if the parameter has nothing to do with
1047 // receiving. Not all parameters are serialized to SDP.
1048 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1049 // the SessionDescription, it is necessary to do that here to in order to
1050 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1051 // https://code.google.com/p/webrtc/issues/detail?id=1356
1052 std::string sdp;
1053 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001054 std::unique_ptr<SessionDescriptionInterface> new_offer(
1055 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1056 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057
Steve Antondb45ca82017-09-11 18:27:34 -07001058 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001060 // Wait for the ice_complete message, so that SDP will have candidates.
1061 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 }
1063
deadbeefab9b2d12015-10-14 11:33:11 -07001064 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001065 std::unique_ptr<SessionDescriptionInterface> answer(
1066 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1067 sdp, nullptr));
1068 ASSERT_TRUE(answer);
1069 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1071 }
1072
deadbeefab9b2d12015-10-14 11:33:11 -07001073 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001074 std::unique_ptr<SessionDescriptionInterface> pr_answer(
1075 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1076 sdp, nullptr));
1077 ASSERT_TRUE(pr_answer);
1078 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001080 std::unique_ptr<SessionDescriptionInterface> answer(
1081 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1082 sdp, nullptr));
1083 ASSERT_TRUE(answer);
1084 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1086 }
1087
1088 // Help function used for waiting until a the last signaled remote stream has
1089 // the same label as |stream_label|. In a few of the tests in this file we
1090 // answer with the same session description as we offer and thus we can
1091 // check if OnAddStream have been called with the same stream as we offer to
1092 // send.
1093 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1094 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1095 }
1096
1097 // Creates an offer and applies it as a local session description.
1098 // Creates an answer with the same SDP an the offer but removes all lines
1099 // that start with a:ssrc"
1100 void CreateOfferReceiveAnswerWithoutSsrc() {
1101 CreateOfferAsLocalDescription();
1102 std::string sdp;
1103 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1104 SetSsrcToZero(&sdp);
1105 CreateAnswerAsRemoteDescription(sdp);
1106 }
1107
deadbeefab9b2d12015-10-14 11:33:11 -07001108 // This function creates a MediaStream with label kStreams[0] and
1109 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1110 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001111 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001112 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001113 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001114 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1115 size_t number_of_video_tracks) {
1116 EXPECT_LE(number_of_audio_tracks, 2u);
1117 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001118
1119 reference_collection_ = StreamCollection::Create();
1120 std::string sdp_ms1 = std::string(kSdpStringInit);
1121
1122 std::string mediastream_label = kStreams[0];
1123
1124 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1125 webrtc::MediaStream::Create(mediastream_label));
1126 reference_collection_->AddStream(stream);
1127
1128 if (number_of_audio_tracks > 0) {
1129 sdp_ms1 += std::string(kSdpStringAudio);
1130 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1131 AddAudioTrack(kAudioTracks[0], stream);
1132 }
1133 if (number_of_audio_tracks > 1) {
1134 sdp_ms1 += kSdpStringMs1Audio1;
1135 AddAudioTrack(kAudioTracks[1], stream);
1136 }
1137
1138 if (number_of_video_tracks > 0) {
1139 sdp_ms1 += std::string(kSdpStringVideo);
1140 sdp_ms1 += std::string(kSdpStringMs1Video0);
1141 AddVideoTrack(kVideoTracks[0], stream);
1142 }
1143 if (number_of_video_tracks > 1) {
1144 sdp_ms1 += kSdpStringMs1Video1;
1145 AddVideoTrack(kVideoTracks[1], stream);
1146 }
1147
kwibergd1fe2812016-04-27 06:47:29 -07001148 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001149 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1150 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001151 }
1152
1153 void AddAudioTrack(const std::string& track_id,
1154 MediaStreamInterface* stream) {
1155 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1156 webrtc::AudioTrack::Create(track_id, nullptr));
1157 ASSERT_TRUE(stream->AddTrack(audio_track));
1158 }
1159
1160 void AddVideoTrack(const std::string& track_id,
1161 MediaStreamInterface* stream) {
1162 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001163 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001164 webrtc::FakeVideoTrackSource::Create(),
1165 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001166 ASSERT_TRUE(stream->AddTrack(video_track));
1167 }
1168
kwibergfd8be342016-05-14 19:44:11 -07001169 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001170 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1174 return offer;
1175 }
1176
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001179 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001180 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001181 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1182 return answer;
1183 }
1184
1185 const std::string& GetFirstAudioStreamCname(
1186 const SessionDescriptionInterface* desc) {
1187 const cricket::ContentInfo* audio_content =
1188 cricket::GetFirstAudioContent(desc->description());
1189 const cricket::AudioContentDescription* audio_desc =
1190 static_cast<const cricket::AudioContentDescription*>(
1191 audio_content->description);
1192 return audio_desc->streams()[0].cname;
1193 }
1194
zhihuang1c378ed2017-08-17 14:10:50 -07001195 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1196 const RTCOfferAnswerOptions& offer_answer_options) {
1197 RTC_DCHECK(pc_);
1198 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1199 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1200 pc_->CreateOffer(observer, offer_answer_options);
1201 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1202 return observer->MoveDescription();
1203 }
1204
1205 void CreateOfferWithOptionsAsRemoteDescription(
1206 std::unique_ptr<SessionDescriptionInterface>* desc,
1207 const RTCOfferAnswerOptions& offer_answer_options) {
1208 *desc = CreateOfferWithOptions(offer_answer_options);
1209 ASSERT_TRUE(desc != nullptr);
1210 std::string sdp;
1211 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001212 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001213 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001214 sdp, nullptr));
1215 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001216 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1217 }
1218
1219 void CreateOfferWithOptionsAsLocalDescription(
1220 std::unique_ptr<SessionDescriptionInterface>* desc,
1221 const RTCOfferAnswerOptions& offer_answer_options) {
1222 *desc = CreateOfferWithOptions(offer_answer_options);
1223 ASSERT_TRUE(desc != nullptr);
1224 std::string sdp;
1225 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001226 std::unique_ptr<SessionDescriptionInterface> new_offer(
1227 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1228 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001229
Steve Antondb45ca82017-09-11 18:27:34 -07001230 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001231 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1232 }
1233
1234 bool HasCNCodecs(const cricket::ContentInfo* content) {
1235 const cricket::ContentDescription* description = content->description;
1236 RTC_DCHECK(description);
1237 const cricket::AudioContentDescription* audio_content_desc =
1238 static_cast<const cricket::AudioContentDescription*>(description);
1239 RTC_DCHECK(audio_content_desc);
1240 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1241 if (audio_content_desc->codecs()[i].name == "CN")
1242 return true;
1243 }
1244 return false;
1245 }
1246
deadbeef9a6f4d42017-05-15 19:43:33 -07001247 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1248 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001249 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001250 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001251 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001252 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1253 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1254 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001256 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257};
1258
zhihuang29ff8442016-07-27 11:07:25 -07001259// Test that no callbacks on the PeerConnectionObserver are called after the
1260// PeerConnection is closed.
1261TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001262 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001263 pc_factory_for_test_->CreatePeerConnection(
1264 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1265 nullptr, &observer_));
1266 observer_.SetPeerConnectionInterface(pc.get());
1267 pc->Close();
1268
1269 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001270 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001271 std::vector<cricket::Candidate> candidates;
1272 pc_factory_for_test_->transport_controller->SignalGatheringState(
1273 cricket::IceGatheringState{});
1274 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1275 "", candidates);
1276 pc_factory_for_test_->transport_controller->SignalConnectionState(
1277 cricket::IceConnectionState{});
1278 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1279 candidates);
1280 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001281 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001282}
1283
zhihuang8f65cdf2016-05-06 18:40:30 -07001284// Generate different CNAMEs when PeerConnections are created.
1285// The CNAMEs are expected to be generated randomly. It is possible
1286// that the test fails, though the possibility is very low.
1287TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001288 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001289 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001290 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001291 CreateOfferWithOneAudioStream();
1292 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1293 GetFirstAudioStreamCname(offer2.get()));
1294}
1295
1296TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001297 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001298 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001299 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001300 CreateAnswerWithOneAudioStream();
1301 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1302 GetFirstAudioStreamCname(answer2.get()));
1303}
1304
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305TEST_F(PeerConnectionInterfaceTest,
1306 CreatePeerConnectionWithDifferentConfigurations) {
1307 CreatePeerConnectionWithDifferentConfigurations();
1308}
1309
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001310TEST_F(PeerConnectionInterfaceTest,
1311 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1312 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1313 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1314 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1315 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1317 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1318 port_allocator_->candidate_filter());
1319 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1320 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1321}
1322
1323// Test that when a PeerConnection is created with a nonzero candidate pool
1324// size, the pooled PortAllocatorSession is created with all the attributes
1325// in the RTCConfiguration.
1326TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1327 PeerConnectionInterface::RTCConfiguration config;
1328 PeerConnectionInterface::IceServer server;
1329 server.uri = kStunAddressOnly;
1330 config.servers.push_back(server);
1331 config.type = PeerConnectionInterface::kRelay;
1332 config.disable_ipv6 = true;
1333 config.tcp_candidate_policy =
1334 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001335 config.candidate_network_policy =
1336 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001337 config.ice_candidate_pool_size = 1;
1338 CreatePeerConnection(config, nullptr);
1339
1340 const cricket::FakePortAllocatorSession* session =
1341 static_cast<const cricket::FakePortAllocatorSession*>(
1342 port_allocator_->GetPooledSession());
1343 ASSERT_NE(nullptr, session);
1344 EXPECT_EQ(1UL, session->stun_servers().size());
1345 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1346 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001347 EXPECT_LT(0U,
1348 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001349}
1350
deadbeefd21eab32017-07-26 16:50:11 -07001351// Test that network-related RTCConfiguration members are applied to the
1352// PortAllocator when CreatePeerConnection is called. Specifically:
1353// - disable_ipv6_on_wifi
1354// - max_ipv6_networks
1355// - tcp_candidate_policy
1356// - candidate_network_policy
1357// - prune_turn_ports
1358//
1359// Note that the candidate filter (RTCConfiguration::type) is already tested
1360// above.
1361TEST_F(PeerConnectionInterfaceTest,
1362 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1363 // Create fake port allocator.
1364 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1365 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1366 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1367
1368 // Create RTCConfiguration with some network-related fields relevant to
1369 // PortAllocator populated.
1370 PeerConnectionInterface::RTCConfiguration config;
1371 config.disable_ipv6_on_wifi = true;
1372 config.max_ipv6_networks = 10;
1373 config.tcp_candidate_policy =
1374 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1375 config.candidate_network_policy =
1376 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1377 config.prune_turn_ports = true;
1378
1379 // Create the PC factory and PC with the above config.
1380 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1381 webrtc::CreatePeerConnectionFactory(
1382 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001383 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1384 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001385 rtc::scoped_refptr<PeerConnectionInterface> pc(
1386 pc_factory->CreatePeerConnection(
1387 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1388
1389 // Now validate that the config fields set above were applied to the
1390 // PortAllocator, as flags or otherwise.
1391 EXPECT_FALSE(raw_port_allocator->flags() &
1392 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1393 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1394 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1395 EXPECT_TRUE(raw_port_allocator->flags() &
1396 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1397 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1398}
1399
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001400// Test that the PeerConnection initializes the port allocator passed into it,
1401// and on the correct thread.
1402TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001403 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001404 std::unique_ptr<rtc::Thread> network_thread(
1405 rtc::Thread::CreateWithSocketServer());
1406 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001407 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1408 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001409 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001410 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001411 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001412 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001413 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1414 PeerConnectionInterface::RTCConfiguration config;
1415 rtc::scoped_refptr<PeerConnectionInterface> pc(
1416 pc_factory->CreatePeerConnection(
1417 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1418 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1419 // so all we have to do here is check that it's initialized.
1420 EXPECT_TRUE(raw_port_allocator->initialized());
1421}
1422
deadbeef46c73892016-11-16 19:42:04 -08001423// Check that GetConfiguration returns the configuration the PeerConnection was
1424// constructed with, before SetConfiguration is called.
1425TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1426 PeerConnectionInterface::RTCConfiguration config;
1427 config.type = PeerConnectionInterface::kRelay;
1428 CreatePeerConnection(config, nullptr);
1429
1430 PeerConnectionInterface::RTCConfiguration returned_config =
1431 pc_->GetConfiguration();
1432 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1433}
1434
1435// Check that GetConfiguration returns the last configuration passed into
1436// SetConfiguration.
1437TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1438 CreatePeerConnection();
1439
1440 PeerConnectionInterface::RTCConfiguration config;
1441 config.type = PeerConnectionInterface::kRelay;
1442 EXPECT_TRUE(pc_->SetConfiguration(config));
1443
1444 PeerConnectionInterface::RTCConfiguration returned_config =
1445 pc_->GetConfiguration();
1446 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1447}
1448
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001450 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001451 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 AddVoiceStream(kStreamLabel2);
1453 ASSERT_EQ(2u, pc_->local_streams()->count());
1454
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001455 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001456 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001458 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1459 pc_factory_->CreateAudioTrack(kStreamLabel3,
1460 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001462 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001463 EXPECT_EQ(3u, pc_->local_streams()->count());
1464
1465 // Remove the third stream.
1466 pc_->RemoveStream(pc_->local_streams()->at(2));
1467 EXPECT_EQ(2u, pc_->local_streams()->count());
1468
1469 // Remove the second stream.
1470 pc_->RemoveStream(pc_->local_streams()->at(1));
1471 EXPECT_EQ(1u, pc_->local_streams()->count());
1472
1473 // Remove the first stream.
1474 pc_->RemoveStream(pc_->local_streams()->at(0));
1475 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476}
1477
deadbeefab9b2d12015-10-14 11:33:11 -07001478// Test that the created offer includes streams we added.
1479TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001480 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001481 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001482 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001483 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001484
1485 const cricket::ContentInfo* audio_content =
1486 cricket::GetFirstAudioContent(offer->description());
1487 const cricket::AudioContentDescription* audio_desc =
1488 static_cast<const cricket::AudioContentDescription*>(
1489 audio_content->description);
1490 EXPECT_TRUE(
1491 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1492
1493 const cricket::ContentInfo* video_content =
1494 cricket::GetFirstVideoContent(offer->description());
1495 const cricket::VideoContentDescription* video_desc =
1496 static_cast<const cricket::VideoContentDescription*>(
1497 video_content->description);
1498 EXPECT_TRUE(
1499 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1500
1501 // Add another stream and ensure the offer includes both the old and new
1502 // streams.
1503 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001504 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001505
1506 audio_content = cricket::GetFirstAudioContent(offer->description());
1507 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1508 audio_content->description);
1509 EXPECT_TRUE(
1510 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1511 EXPECT_TRUE(
1512 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1513
1514 video_content = cricket::GetFirstVideoContent(offer->description());
1515 video_desc = static_cast<const cricket::VideoContentDescription*>(
1516 video_content->description);
1517 EXPECT_TRUE(
1518 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1519 EXPECT_TRUE(
1520 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001524 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001525 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 ASSERT_EQ(1u, pc_->local_streams()->count());
1527 pc_->RemoveStream(pc_->local_streams()->at(0));
1528 EXPECT_EQ(0u, pc_->local_streams()->count());
1529}
1530
deadbeefe1f9d832016-01-14 15:35:42 -08001531// Test for AddTrack and RemoveTrack methods.
1532// Tests that the created offer includes tracks we added,
1533// and that the RtpSenders are created correctly.
1534// Also tests that RemoveTrack removes the tracks from subsequent offers.
1535TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001536 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001537 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001538 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001539 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1540 std::vector<MediaStreamInterface*> stream_list;
1541 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001542 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001543 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001544 rtc::scoped_refptr<VideoTrackInterface> video_track(
1545 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001546 "video_track", pc_factory_->CreateVideoSource(
1547 std::unique_ptr<cricket::VideoCapturer>(
1548 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001549 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1550 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001551 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1552 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001553 EXPECT_EQ("audio_track", audio_sender->id());
1554 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001555 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1556 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001557 EXPECT_EQ("video_track", video_sender->id());
1558 EXPECT_EQ(video_track, video_sender->track());
1559
1560 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001561 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001562 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001563
1564 const cricket::ContentInfo* audio_content =
1565 cricket::GetFirstAudioContent(offer->description());
1566 const cricket::AudioContentDescription* audio_desc =
1567 static_cast<const cricket::AudioContentDescription*>(
1568 audio_content->description);
1569 EXPECT_TRUE(
1570 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1571
1572 const cricket::ContentInfo* video_content =
1573 cricket::GetFirstVideoContent(offer->description());
1574 const cricket::VideoContentDescription* video_desc =
1575 static_cast<const cricket::VideoContentDescription*>(
1576 video_content->description);
1577 EXPECT_TRUE(
1578 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1579
Steve Antondb45ca82017-09-11 18:27:34 -07001580 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001581
1582 // Now try removing the tracks.
1583 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1584 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1585
1586 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001587 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001588
1589 audio_content = cricket::GetFirstAudioContent(offer->description());
1590 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1591 audio_content->description);
1592 EXPECT_FALSE(
1593 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1594
1595 video_content = cricket::GetFirstVideoContent(offer->description());
1596 video_desc = static_cast<const cricket::VideoContentDescription*>(
1597 video_content->description);
1598 EXPECT_FALSE(
1599 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1600
Steve Antondb45ca82017-09-11 18:27:34 -07001601 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001602
1603 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1604 // should return false.
1605 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1606 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1607}
1608
1609// Test creating senders without a stream specified,
1610// expecting a random stream ID to be generated.
1611TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001612 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001613 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001614 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001615 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001616 rtc::scoped_refptr<VideoTrackInterface> video_track(
1617 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001618 "video_track", pc_factory_->CreateVideoSource(
1619 std::unique_ptr<cricket::VideoCapturer>(
1620 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001621 auto audio_sender =
1622 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1623 auto video_sender =
1624 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1625 EXPECT_EQ("audio_track", audio_sender->id());
1626 EXPECT_EQ(audio_track, audio_sender->track());
1627 EXPECT_EQ("video_track", video_sender->id());
1628 EXPECT_EQ(video_track, video_sender->track());
1629 // If the ID is truly a random GUID, it should be infinitely unlikely they
1630 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001631 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001632}
1633
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1635 InitiateCall();
1636 WaitAndVerifyOnAddStream(kStreamLabel1);
1637 VerifyRemoteRtpHeaderExtensions();
1638}
1639
1640TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001641 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001642 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 CreateOfferAsLocalDescription();
1644 std::string offer;
1645 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1646 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1647 WaitAndVerifyOnAddStream(kStreamLabel1);
1648}
1649
1650TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001651 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001652 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653
1654 CreateOfferAsRemoteDescription();
1655 CreateAnswerAsLocalDescription();
1656
1657 WaitAndVerifyOnAddStream(kStreamLabel1);
1658}
1659
1660TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001661 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001662 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663
1664 CreateOfferAsRemoteDescription();
1665 CreatePrAnswerAsLocalDescription();
1666 CreateAnswerAsLocalDescription();
1667
1668 WaitAndVerifyOnAddStream(kStreamLabel1);
1669}
1670
1671TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1672 InitiateCall();
1673 ASSERT_EQ(1u, pc_->remote_streams()->count());
1674 pc_->RemoveStream(pc_->local_streams()->at(0));
1675 CreateOfferReceiveAnswer();
1676 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001677 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 CreateOfferReceiveAnswer();
1679}
1680
1681// Tests that after negotiating an audio only call, the respondent can perform a
1682// renegotiation that removes the audio stream.
1683TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001684 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 AddVoiceStream(kStreamLabel1);
1686 CreateOfferAsRemoteDescription();
1687 CreateAnswerAsLocalDescription();
1688
1689 ASSERT_EQ(1u, pc_->remote_streams()->count());
1690 pc_->RemoveStream(pc_->local_streams()->at(0));
1691 CreateOfferReceiveAnswer();
1692 EXPECT_EQ(0u, pc_->remote_streams()->count());
1693}
1694
1695// Test that candidates are generated and that we can parse our own candidates.
1696TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001697 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698
1699 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1700 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001701 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001702 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001703 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001704 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705
1706 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001707 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001708 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001709 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710
1711 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1712 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1713
1714 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1715}
1716
deadbeefab9b2d12015-10-14 11:33:11 -07001717// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718// not unique.
1719TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001720 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001722 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001723 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001724 EXPECT_TRUE(offer);
1725 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726
1727 // Create a local stream with audio&video tracks having same label.
1728 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1729
1730 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001731 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
1733 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001734 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001735 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736}
1737
1738// Test that we will get different SSRCs for each tracks in the offer and answer
1739// we created.
1740TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001741 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 // Create a local stream with audio&video tracks having different labels.
1743 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1744
1745 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001746 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001747 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 int audio_ssrc = 0;
1749 int video_ssrc = 0;
1750 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1751 &audio_ssrc));
1752 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1753 &video_ssrc));
1754 EXPECT_NE(audio_ssrc, video_ssrc);
1755
1756 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001757 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001758 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001759 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 audio_ssrc = 0;
1761 video_ssrc = 0;
1762 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1763 &audio_ssrc));
1764 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1765 &video_ssrc));
1766 EXPECT_NE(audio_ssrc, video_ssrc);
1767}
1768
deadbeefeb459812015-12-15 19:24:43 -08001769// Test that it's possible to call AddTrack on a MediaStream after adding
1770// the stream to a PeerConnection.
1771// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1772TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001773 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001774 // Create audio stream and add to PeerConnection.
1775 AddVoiceStream(kStreamLabel1);
1776 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1777
1778 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001779 rtc::scoped_refptr<VideoTrackInterface> video_track(
1780 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001781 "video_label", pc_factory_->CreateVideoSource(
1782 std::unique_ptr<cricket::VideoCapturer>(
1783 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001784 stream->AddTrack(video_track.get());
1785
kwibergd1fe2812016-04-27 06:47:29 -07001786 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001787 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001788
1789 const cricket::MediaContentDescription* video_desc =
1790 cricket::GetFirstVideoContentDescription(offer->description());
1791 EXPECT_TRUE(video_desc != nullptr);
1792}
1793
1794// Test that it's possible to call RemoveTrack on a MediaStream after adding
1795// the stream to a PeerConnection.
1796// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1797TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001798 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001799 // Create audio/video stream and add to PeerConnection.
1800 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1801 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1802
1803 // Remove the video track.
1804 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1805
kwibergd1fe2812016-04-27 06:47:29 -07001806 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001807 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001808
1809 const cricket::MediaContentDescription* video_desc =
1810 cricket::GetFirstVideoContentDescription(offer->description());
1811 EXPECT_TRUE(video_desc == nullptr);
1812}
1813
deadbeef1dcb1642017-03-29 21:08:16 -07001814// Verify that CreateDtmfSender only succeeds if called with a valid local
1815// track. Other aspects of DtmfSenders are tested in
1816// peerconnection_integrationtest.cc.
1817TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1818 CreatePeerConnection();
1819 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1820 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1821 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1822 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1823 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1824}
1825
deadbeefbd7d8f72015-12-18 16:58:44 -08001826// Test creating a sender with a stream ID, and ensure the ID is populated
1827// in the offer.
1828TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001829 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001830 pc_->CreateSender("video", kStreamLabel1);
1831
kwibergd1fe2812016-04-27 06:47:29 -07001832 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001833 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001834
1835 const cricket::MediaContentDescription* video_desc =
1836 cricket::GetFirstVideoContentDescription(offer->description());
1837 ASSERT_TRUE(video_desc != nullptr);
1838 ASSERT_EQ(1u, video_desc->streams().size());
1839 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1840}
1841
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842// Test that we can specify a certain track that we want statistics about.
1843TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1844 InitiateCall();
1845 ASSERT_LT(0u, pc_->remote_streams()->count());
1846 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001847 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1849 EXPECT_TRUE(DoGetStats(remote_audio));
1850
1851 // Remove the stream. Since we are sending to our selves the local
1852 // and the remote stream is the same.
1853 pc_->RemoveStream(pc_->local_streams()->at(0));
1854 // Do a re-negotiation.
1855 CreateOfferReceiveAnswer();
1856
1857 ASSERT_EQ(0u, pc_->remote_streams()->count());
1858
1859 // Test that we still can get statistics for the old track. Even if it is not
1860 // sent any longer.
1861 EXPECT_TRUE(DoGetStats(remote_audio));
1862}
1863
1864// Test that we can get stats on a video track.
1865TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1866 InitiateCall();
1867 ASSERT_LT(0u, pc_->remote_streams()->count());
1868 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001869 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1871 EXPECT_TRUE(DoGetStats(remote_video));
1872}
1873
1874// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001875TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 pc_factory_->CreateAudioTrack("unknown track", NULL));
1879 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1880}
1881
1882// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 FakeConstraints constraints;
1885 constraints.SetAllowRtpDataChannels();
1886 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001887 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001889 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 pc_->CreateDataChannel("test2", NULL);
1891 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001892 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001894 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 new MockDataChannelObserver(data2));
1896
1897 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1898 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1899 std::string data_to_send1 = "testing testing";
1900 std::string data_to_send2 = "testing something else";
1901 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1902
1903 CreateOfferReceiveAnswer();
1904 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1905 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1906
1907 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1908 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1909 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1910 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1911
1912 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1913 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1914
1915 data1->Close();
1916 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1917 CreateOfferReceiveAnswer();
1918 EXPECT_FALSE(observer1->IsOpen());
1919 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1920 EXPECT_TRUE(observer2->IsOpen());
1921
1922 data_to_send2 = "testing something else again";
1923 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1924
1925 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1926}
1927
1928// This test verifies that sendnig binary data over RTP data channels should
1929// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 FakeConstraints constraints;
1932 constraints.SetAllowRtpDataChannels();
1933 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001934 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001936 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 pc_->CreateDataChannel("test2", NULL);
1938 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001939 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001941 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 new MockDataChannelObserver(data2));
1943
1944 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1945 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1946
1947 CreateOfferReceiveAnswer();
1948 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1949 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1950
1951 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1952 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1953
jbaucheec21bd2016-03-20 06:15:43 -07001954 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1956}
1957
1958// This test setup a RTP data channels in loop back and test that a channel is
1959// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 FakeConstraints constraints;
1962 constraints.SetAllowRtpDataChannels();
1963 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001964 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001966 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 new MockDataChannelObserver(data1));
1968
1969 CreateOfferReceiveAnswerWithoutSsrc();
1970
1971 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1972
1973 data1->Close();
1974 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1975 CreateOfferReceiveAnswerWithoutSsrc();
1976 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1977 EXPECT_FALSE(observer1->IsOpen());
1978}
1979
1980// This test that if a data channel is added in an answer a receive only channel
1981// channel is created.
1982TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1983 FakeConstraints constraints;
1984 constraints.SetAllowRtpDataChannels();
1985 CreatePeerConnection(&constraints);
1986
1987 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001988 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 pc_->CreateDataChannel(offer_label, NULL);
1990
1991 CreateOfferAsLocalDescription();
1992
1993 // Replace the data channel label in the offer and apply it as an answer.
1994 std::string receive_label = "answer_channel";
1995 std::string sdp;
1996 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001997 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 receive_label.c_str(), receive_label.length(),
1999 &sdp);
2000 CreateAnswerAsRemoteDescription(sdp);
2001
2002 // Verify that a new incoming data channel has been created and that
2003 // it is open but can't we written to.
2004 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2005 DataChannelInterface* received_channel = observer_.last_datachannel_;
2006 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2007 EXPECT_EQ(receive_label, received_channel->label());
2008 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2009
2010 // Verify that the channel we initially offered has been rejected.
2011 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2012
2013 // Do another offer / answer exchange and verify that the data channel is
2014 // opened.
2015 CreateOfferReceiveAnswer();
2016 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2017 kTimeout);
2018}
2019
2020// This test that no data channel is returned if a reliable channel is
2021// requested.
2022// TODO(perkj): Remove this test once reliable channels are implemented.
2023TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2024 FakeConstraints constraints;
2025 constraints.SetAllowRtpDataChannels();
2026 CreatePeerConnection(&constraints);
2027
2028 std::string label = "test";
2029 webrtc::DataChannelInit config;
2030 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002031 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 pc_->CreateDataChannel(label, &config);
2033 EXPECT_TRUE(channel == NULL);
2034}
2035
deadbeefab9b2d12015-10-14 11:33:11 -07002036// Verifies that duplicated label is not allowed for RTP data channel.
2037TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2038 FakeConstraints constraints;
2039 constraints.SetAllowRtpDataChannels();
2040 CreatePeerConnection(&constraints);
2041
2042 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002043 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002044 pc_->CreateDataChannel(label, nullptr);
2045 EXPECT_NE(channel, nullptr);
2046
zhihuang9763d562016-08-05 11:14:50 -07002047 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002048 pc_->CreateDataChannel(label, nullptr);
2049 EXPECT_EQ(dup_channel, nullptr);
2050}
2051
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052// This tests that a SCTP data channel is returned using different
2053// DataChannelInit configurations.
2054TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2055 FakeConstraints constraints;
2056 constraints.SetAllowDtlsSctpDataChannels();
2057 CreatePeerConnection(&constraints);
2058
2059 webrtc::DataChannelInit config;
2060
zhihuang9763d562016-08-05 11:14:50 -07002061 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 pc_->CreateDataChannel("1", &config);
2063 EXPECT_TRUE(channel != NULL);
2064 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002065 EXPECT_TRUE(observer_.renegotiation_needed_);
2066 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067
2068 config.ordered = false;
2069 channel = pc_->CreateDataChannel("2", &config);
2070 EXPECT_TRUE(channel != NULL);
2071 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002072 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073
2074 config.ordered = true;
2075 config.maxRetransmits = 0;
2076 channel = pc_->CreateDataChannel("3", &config);
2077 EXPECT_TRUE(channel != NULL);
2078 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002079 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080
2081 config.maxRetransmits = -1;
2082 config.maxRetransmitTime = 0;
2083 channel = pc_->CreateDataChannel("4", &config);
2084 EXPECT_TRUE(channel != NULL);
2085 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002086 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087}
2088
2089// This tests that no data channel is returned if both maxRetransmits and
2090// maxRetransmitTime are set for SCTP data channels.
2091TEST_F(PeerConnectionInterfaceTest,
2092 CreateSctpDataChannelShouldFailForInvalidConfig) {
2093 FakeConstraints constraints;
2094 constraints.SetAllowDtlsSctpDataChannels();
2095 CreatePeerConnection(&constraints);
2096
2097 std::string label = "test";
2098 webrtc::DataChannelInit config;
2099 config.maxRetransmits = 0;
2100 config.maxRetransmitTime = 0;
2101
zhihuang9763d562016-08-05 11:14:50 -07002102 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 pc_->CreateDataChannel(label, &config);
2104 EXPECT_TRUE(channel == NULL);
2105}
2106
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107// The test verifies that creating a SCTP data channel with an id already in use
2108// or out of range should fail.
2109TEST_F(PeerConnectionInterfaceTest,
2110 CreateSctpDataChannelWithInvalidIdShouldFail) {
2111 FakeConstraints constraints;
2112 constraints.SetAllowDtlsSctpDataChannels();
2113 CreatePeerConnection(&constraints);
2114
2115 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002116 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002118 config.id = 1;
2119 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 EXPECT_TRUE(channel != NULL);
2121 EXPECT_EQ(1, channel->id());
2122
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 channel = pc_->CreateDataChannel("x", &config);
2124 EXPECT_TRUE(channel == NULL);
2125
2126 config.id = cricket::kMaxSctpSid;
2127 channel = pc_->CreateDataChannel("max", &config);
2128 EXPECT_TRUE(channel != NULL);
2129 EXPECT_EQ(config.id, channel->id());
2130
2131 config.id = cricket::kMaxSctpSid + 1;
2132 channel = pc_->CreateDataChannel("x", &config);
2133 EXPECT_TRUE(channel == NULL);
2134}
2135
deadbeefab9b2d12015-10-14 11:33:11 -07002136// Verifies that duplicated label is allowed for SCTP data channel.
2137TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2138 FakeConstraints constraints;
2139 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2140 true);
2141 CreatePeerConnection(&constraints);
2142
2143 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002144 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002145 pc_->CreateDataChannel(label, nullptr);
2146 EXPECT_NE(channel, nullptr);
2147
zhihuang9763d562016-08-05 11:14:50 -07002148 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002149 pc_->CreateDataChannel(label, nullptr);
2150 EXPECT_NE(dup_channel, nullptr);
2151}
2152
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002153// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2154// DataChannel.
2155TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2156 FakeConstraints constraints;
2157 constraints.SetAllowRtpDataChannels();
2158 CreatePeerConnection(&constraints);
2159
zhihuang9763d562016-08-05 11:14:50 -07002160 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002161 pc_->CreateDataChannel("test1", NULL);
2162 EXPECT_TRUE(observer_.renegotiation_needed_);
2163 observer_.renegotiation_needed_ = false;
2164
zhihuang9763d562016-08-05 11:14:50 -07002165 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002166 pc_->CreateDataChannel("test2", NULL);
2167 EXPECT_TRUE(observer_.renegotiation_needed_);
2168}
2169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 FakeConstraints constraints;
2173 constraints.SetAllowRtpDataChannels();
2174 CreatePeerConnection(&constraints);
2175
zhihuang9763d562016-08-05 11:14:50 -07002176 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002178 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 pc_->CreateDataChannel("test2", NULL);
2180 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002181 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002183 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 new MockDataChannelObserver(data2));
2185
2186 CreateOfferReceiveAnswer();
2187 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2188 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2189
2190 ReleasePeerConnection();
2191 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2192 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2193}
2194
2195// This test that data channels can be rejected in an answer.
2196TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2197 FakeConstraints constraints;
2198 constraints.SetAllowRtpDataChannels();
2199 CreatePeerConnection(&constraints);
2200
zhihuang9763d562016-08-05 11:14:50 -07002201 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 pc_->CreateDataChannel("offer_channel", NULL));
2203
2204 CreateOfferAsLocalDescription();
2205
2206 // Create an answer where the m-line for data channels are rejected.
2207 std::string sdp;
2208 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002209 std::unique_ptr<SessionDescriptionInterface> answer(
2210 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2211 sdp, nullptr));
2212 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 cricket::ContentInfo* data_info =
2214 answer->description()->GetContentByName("data");
2215 data_info->rejected = true;
2216
Steve Antondb45ca82017-09-11 18:27:34 -07002217 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2219}
2220
2221// Test that we can create a session description from an SDP string from
2222// FireFox, use it as a remote session description, generate an answer and use
2223// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002224TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 FakeConstraints constraints;
2226 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2227 true);
2228 CreatePeerConnection(&constraints);
2229 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002230 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002232 webrtc::kFireFoxSdpOffer, nullptr));
2233 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 CreateAnswerAsLocalDescription();
2235 ASSERT_TRUE(pc_->local_description() != NULL);
2236 ASSERT_TRUE(pc_->remote_description() != NULL);
2237
2238 const cricket::ContentInfo* content =
2239 cricket::GetFirstAudioContent(pc_->local_description()->description());
2240 ASSERT_TRUE(content != NULL);
2241 EXPECT_FALSE(content->rejected);
2242
2243 content =
2244 cricket::GetFirstVideoContent(pc_->local_description()->description());
2245 ASSERT_TRUE(content != NULL);
2246 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002247#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 content =
2249 cricket::GetFirstDataContent(pc_->local_description()->description());
2250 ASSERT_TRUE(content != NULL);
2251 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002252#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253}
2254
deadbeef8662f942017-01-20 21:20:51 -08002255// Test that an offer can be received which offers DTLS with SDES fallback.
2256// Regression test for issue:
2257// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2258TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2259 FakeConstraints constraints;
2260 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2261 true);
2262 CreatePeerConnection(&constraints);
2263 // Wait for fake certificate to be generated. Previously, this is what caused
2264 // the "a=crypto" lines to be rejected.
2265 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2266 ASSERT_NE(nullptr, fake_certificate_generator_);
2267 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2268 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002269 std::unique_ptr<SessionDescriptionInterface> desc(
2270 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2271 kDtlsSdesFallbackSdp, nullptr));
2272 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002273 CreateAnswerAsLocalDescription();
2274}
2275
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276// Test that we can create an audio only offer and receive an answer with a
2277// limited set of audio codecs and receive an updated offer with more audio
2278// codecs, where the added codecs are not supported.
2279TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002280 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 AddVoiceStream("audio_label");
2282 CreateOfferAsLocalDescription();
2283
Steve Antondb45ca82017-09-11 18:27:34 -07002284 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002286 webrtc::kAudioSdp, nullptr));
2287 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288
Steve Antondb45ca82017-09-11 18:27:34 -07002289 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002291 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002292 nullptr));
2293 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 CreateAnswerAsLocalDescription();
2295}
2296
deadbeefc80741f2015-10-22 13:14:45 -07002297// Test that if we're receiving (but not sending) a track, subsequent offers
2298// will have m-lines with a=recvonly.
2299TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2300 FakeConstraints constraints;
2301 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2302 true);
2303 CreatePeerConnection(&constraints);
2304 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2305 CreateAnswerAsLocalDescription();
2306
2307 // At this point we should be receiving stream 1, but not sending anything.
2308 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002309 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002310 DoCreateOffer(&offer, nullptr);
2311
2312 const cricket::ContentInfo* video_content =
2313 cricket::GetFirstVideoContent(offer->description());
2314 const cricket::VideoContentDescription* video_desc =
2315 static_cast<const cricket::VideoContentDescription*>(
2316 video_content->description);
2317 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2318
2319 const cricket::ContentInfo* audio_content =
2320 cricket::GetFirstAudioContent(offer->description());
2321 const cricket::AudioContentDescription* audio_desc =
2322 static_cast<const cricket::AudioContentDescription*>(
2323 audio_content->description);
2324 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2325}
2326
2327// Test that if we're receiving (but not sending) a track, and the
2328// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2329// false, the generated m-lines will be a=inactive.
2330TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2331 FakeConstraints constraints;
2332 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2333 true);
2334 CreatePeerConnection(&constraints);
2335 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2336 CreateAnswerAsLocalDescription();
2337
2338 // At this point we should be receiving stream 1, but not sending anything.
2339 // A new offer would be recvonly, but we'll set the "no receive" constraints
2340 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002341 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002342 FakeConstraints offer_constraints;
2343 offer_constraints.AddMandatory(
2344 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2345 offer_constraints.AddMandatory(
2346 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2347 DoCreateOffer(&offer, &offer_constraints);
2348
2349 const cricket::ContentInfo* video_content =
2350 cricket::GetFirstVideoContent(offer->description());
2351 const cricket::VideoContentDescription* video_desc =
2352 static_cast<const cricket::VideoContentDescription*>(
2353 video_content->description);
2354 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2355
2356 const cricket::ContentInfo* audio_content =
2357 cricket::GetFirstAudioContent(offer->description());
2358 const cricket::AudioContentDescription* audio_desc =
2359 static_cast<const cricket::AudioContentDescription*>(
2360 audio_content->description);
2361 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2362}
2363
deadbeef653b8e02015-11-11 12:55:10 -08002364// Test that we can use SetConfiguration to change the ICE servers of the
2365// PortAllocator.
2366TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2367 CreatePeerConnection();
2368
2369 PeerConnectionInterface::RTCConfiguration config;
2370 PeerConnectionInterface::IceServer server;
2371 server.uri = "stun:test_hostname";
2372 config.servers.push_back(server);
2373 EXPECT_TRUE(pc_->SetConfiguration(config));
2374
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002375 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2376 EXPECT_EQ("test_hostname",
2377 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002378}
2379
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002380TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2381 CreatePeerConnection();
2382 PeerConnectionInterface::RTCConfiguration config;
2383 config.type = PeerConnectionInterface::kRelay;
2384 EXPECT_TRUE(pc_->SetConfiguration(config));
2385 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2386}
2387
deadbeef293e9262017-01-11 12:28:30 -08002388TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2389 PeerConnectionInterface::RTCConfiguration config;
2390 config.prune_turn_ports = false;
2391 CreatePeerConnection(config, nullptr);
2392 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2393
2394 config.prune_turn_ports = true;
2395 EXPECT_TRUE(pc_->SetConfiguration(config));
2396 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2397}
2398
skvladd1f5fda2017-02-03 16:54:05 -08002399// Test that the ice check interval can be changed. This does not verify that
2400// the setting makes it all the way to P2PTransportChannel, as that would
2401// require a very complex set of mocks.
2402TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2403 PeerConnectionInterface::RTCConfiguration config;
2404 config.ice_check_min_interval = rtc::Optional<int>();
2405 CreatePeerConnection(config, nullptr);
2406 config.ice_check_min_interval = rtc::Optional<int>(100);
2407 EXPECT_TRUE(pc_->SetConfiguration(config));
2408 PeerConnectionInterface::RTCConfiguration new_config =
2409 pc_->GetConfiguration();
2410 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2411}
2412
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002413// Test that when SetConfiguration changes both the pool size and other
2414// attributes, the pooled session is created with the updated attributes.
2415TEST_F(PeerConnectionInterfaceTest,
2416 SetConfigurationCreatesPooledSessionCorrectly) {
2417 CreatePeerConnection();
2418 PeerConnectionInterface::RTCConfiguration config;
2419 config.ice_candidate_pool_size = 1;
2420 PeerConnectionInterface::IceServer server;
2421 server.uri = kStunAddressOnly;
2422 config.servers.push_back(server);
2423 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002424 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002425
2426 const cricket::FakePortAllocatorSession* session =
2427 static_cast<const cricket::FakePortAllocatorSession*>(
2428 port_allocator_->GetPooledSession());
2429 ASSERT_NE(nullptr, session);
2430 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002431}
2432
deadbeef293e9262017-01-11 12:28:30 -08002433// Test that after SetLocalDescription, changing the pool size is not allowed,
2434// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002435TEST_F(PeerConnectionInterfaceTest,
2436 CantChangePoolSizeAfterSetLocalDescription) {
2437 CreatePeerConnection();
2438 // Start by setting a size of 1.
2439 PeerConnectionInterface::RTCConfiguration config;
2440 config.ice_candidate_pool_size = 1;
2441 EXPECT_TRUE(pc_->SetConfiguration(config));
2442
2443 // Set remote offer; can still change pool size at this point.
2444 CreateOfferAsRemoteDescription();
2445 config.ice_candidate_pool_size = 2;
2446 EXPECT_TRUE(pc_->SetConfiguration(config));
2447
2448 // Set local answer; now it's too late.
2449 CreateAnswerAsLocalDescription();
2450 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002451 RTCError error;
2452 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2453 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2454}
2455
deadbeef42a42632017-03-10 15:18:00 -08002456// Test that after setting an answer, extra pooled sessions are discarded. The
2457// ICE candidate pool is only intended to be used for the first offer/answer.
2458TEST_F(PeerConnectionInterfaceTest,
2459 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2460 CreatePeerConnection();
2461
2462 // Set a larger-than-necessary size.
2463 PeerConnectionInterface::RTCConfiguration config;
2464 config.ice_candidate_pool_size = 4;
2465 EXPECT_TRUE(pc_->SetConfiguration(config));
2466
2467 // Do offer/answer.
2468 CreateOfferAsRemoteDescription();
2469 CreateAnswerAsLocalDescription();
2470
2471 // Expect no pooled sessions to be left.
2472 const cricket::PortAllocatorSession* session =
2473 port_allocator_->GetPooledSession();
2474 EXPECT_EQ(nullptr, session);
2475}
2476
2477// After Close is called, pooled candidates should be discarded so as to not
2478// waste network resources.
2479TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2480 CreatePeerConnection();
2481
2482 PeerConnectionInterface::RTCConfiguration config;
2483 config.ice_candidate_pool_size = 3;
2484 EXPECT_TRUE(pc_->SetConfiguration(config));
2485 pc_->Close();
2486
2487 // Expect no pooled sessions to be left.
2488 const cricket::PortAllocatorSession* session =
2489 port_allocator_->GetPooledSession();
2490 EXPECT_EQ(nullptr, session);
2491}
2492
deadbeef293e9262017-01-11 12:28:30 -08002493// Test that SetConfiguration returns an invalid modification error if
2494// modifying a field in the configuration that isn't allowed to be modified.
2495TEST_F(PeerConnectionInterfaceTest,
2496 SetConfigurationReturnsInvalidModificationError) {
2497 PeerConnectionInterface::RTCConfiguration config;
2498 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2499 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2500 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2501 CreatePeerConnection(config, nullptr);
2502
2503 PeerConnectionInterface::RTCConfiguration modified_config = config;
2504 modified_config.bundle_policy =
2505 PeerConnectionInterface::kBundlePolicyMaxBundle;
2506 RTCError error;
2507 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2508 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2509
2510 modified_config = config;
2511 modified_config.rtcp_mux_policy =
2512 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2513 error.set_type(RTCErrorType::NONE);
2514 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2515 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2516
2517 modified_config = config;
2518 modified_config.continual_gathering_policy =
2519 PeerConnectionInterface::GATHER_CONTINUALLY;
2520 error.set_type(RTCErrorType::NONE);
2521 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2522 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2523}
2524
2525// Test that SetConfiguration returns a range error if the candidate pool size
2526// is negative or larger than allowed by the spec.
2527TEST_F(PeerConnectionInterfaceTest,
2528 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2529 PeerConnectionInterface::RTCConfiguration config;
2530 CreatePeerConnection(config, nullptr);
2531
2532 config.ice_candidate_pool_size = -1;
2533 RTCError error;
2534 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2535 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2536
2537 config.ice_candidate_pool_size = INT_MAX;
2538 error.set_type(RTCErrorType::NONE);
2539 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2540 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2541}
2542
2543// Test that SetConfiguration returns a syntax error if parsing an ICE server
2544// URL failed.
2545TEST_F(PeerConnectionInterfaceTest,
2546 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2547 PeerConnectionInterface::RTCConfiguration config;
2548 CreatePeerConnection(config, nullptr);
2549
2550 PeerConnectionInterface::IceServer bad_server;
2551 bad_server.uri = "stunn:www.example.com";
2552 config.servers.push_back(bad_server);
2553 RTCError error;
2554 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2555 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2556}
2557
2558// Test that SetConfiguration returns an invalid parameter error if a TURN
2559// IceServer is missing a username or password.
2560TEST_F(PeerConnectionInterfaceTest,
2561 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2562 PeerConnectionInterface::RTCConfiguration config;
2563 CreatePeerConnection(config, nullptr);
2564
2565 PeerConnectionInterface::IceServer bad_server;
2566 bad_server.uri = "turn:www.example.com";
2567 // Missing password.
2568 bad_server.username = "foo";
2569 config.servers.push_back(bad_server);
2570 RTCError error;
2571 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2572 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002573}
2574
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575// Test that PeerConnection::Close changes the states to closed and all remote
2576// tracks change state to ended.
2577TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2578 // Initialize a PeerConnection and negotiate local and remote session
2579 // description.
2580 InitiateCall();
2581 ASSERT_EQ(1u, pc_->local_streams()->count());
2582 ASSERT_EQ(1u, pc_->remote_streams()->count());
2583
2584 pc_->Close();
2585
2586 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2587 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2588 pc_->ice_connection_state());
2589 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2590 pc_->ice_gathering_state());
2591
2592 EXPECT_EQ(1u, pc_->local_streams()->count());
2593 EXPECT_EQ(1u, pc_->remote_streams()->count());
2594
zhihuang9763d562016-08-05 11:14:50 -07002595 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2596 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002597 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002598 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002599 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2600 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2601 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602}
2603
2604// Test that PeerConnection methods fails gracefully after
2605// PeerConnection::Close has been called.
2606TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002607 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002608 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2609 CreateOfferAsRemoteDescription();
2610 CreateAnswerAsLocalDescription();
2611
2612 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002613 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002614 pc_->local_streams()->at(0);
2615
2616 pc_->Close();
2617
2618 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002619 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620
2621 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002622 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002624 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002625
2626 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2627
2628 EXPECT_TRUE(pc_->local_description() != NULL);
2629 EXPECT_TRUE(pc_->remote_description() != NULL);
2630
kwibergd1fe2812016-04-27 06:47:29 -07002631 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002632 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002633 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002634 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002635
2636 std::string sdp;
2637 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002638 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2639 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2640 nullptr));
2641 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002642
2643 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002644 std::unique_ptr<SessionDescriptionInterface> local_offer(
2645 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2646 nullptr));
2647 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648}
2649
2650// Test that GetStats can still be called after PeerConnection::Close.
2651TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2652 InitiateCall();
2653 pc_->Close();
2654 DoGetStats(NULL);
2655}
deadbeefab9b2d12015-10-14 11:33:11 -07002656
2657// NOTE: The series of tests below come from what used to be
2658// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2659// setting a remote or local description has the expected effects.
2660
2661// This test verifies that the remote MediaStreams corresponding to a received
2662// SDP string is created. In this test the two separate MediaStreams are
2663// signaled.
2664TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2665 FakeConstraints constraints;
2666 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2667 true);
2668 CreatePeerConnection(&constraints);
2669 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2670
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002671 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002672 EXPECT_TRUE(
2673 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2674 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2675 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2676
2677 // Create a session description based on another SDP with another
2678 // MediaStream.
2679 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2680
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002681 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002682 EXPECT_TRUE(
2683 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2684}
2685
2686// This test verifies that when remote tracks are added/removed from SDP, the
2687// created remote streams are updated appropriately.
2688TEST_F(PeerConnectionInterfaceTest,
2689 AddRemoveTrackFromExistingRemoteMediaStream) {
2690 FakeConstraints constraints;
2691 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2692 true);
2693 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002694 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002695 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002696 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002697 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2698 reference_collection_));
2699
2700 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002701 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002702 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002703 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002704 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2705 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002706 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002707 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2708 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002709 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002710 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2711 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002712
2713 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002714 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002715 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002716 MockTrackObserver audio_track_observer(audio_track2);
2717 MockTrackObserver video_track_observer(video_track2);
2718
2719 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2720 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002721 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002722 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2723 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002724 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002725 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002726 audio_track2->state(), kTimeout);
2727 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2728 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002729}
2730
2731// This tests that remote tracks are ended if a local session description is set
2732// that rejects the media content type.
2733TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2734 FakeConstraints constraints;
2735 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2736 true);
2737 CreatePeerConnection(&constraints);
2738 // First create and set a remote offer, then reject its video content in our
2739 // answer.
2740 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2741 ASSERT_EQ(1u, observer_.remote_streams()->count());
2742 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2743 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2744 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2745
2746 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2747 remote_stream->GetVideoTracks()[0];
2748 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2749 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2750 remote_stream->GetAudioTracks()[0];
2751 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2752
kwibergd1fe2812016-04-27 06:47:29 -07002753 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002754 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002755 cricket::ContentInfo* video_info =
2756 local_answer->description()->GetContentByName("video");
2757 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002758 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002759 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2760 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2761
2762 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002763 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002764 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002765 video_info = local_offer->description()->GetContentByName("video");
2766 ASSERT_TRUE(video_info != nullptr);
2767 video_info->rejected = true;
2768 cricket::ContentInfo* audio_info =
2769 local_offer->description()->GetContentByName("audio");
2770 ASSERT_TRUE(audio_info != nullptr);
2771 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002772 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002773 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002774 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002775 remote_audio->state(), kTimeout);
2776 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2777 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002778}
2779
2780// This tests that we won't crash if the remote track has been removed outside
2781// of PeerConnection and then PeerConnection tries to reject the track.
2782TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2783 FakeConstraints constraints;
2784 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2785 true);
2786 CreatePeerConnection(&constraints);
2787 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2788 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2789 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2790 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2791
kwibergd1fe2812016-04-27 06:47:29 -07002792 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002793 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2794 kSdpStringWithStream1, nullptr));
2795 cricket::ContentInfo* video_info =
2796 local_answer->description()->GetContentByName("video");
2797 video_info->rejected = true;
2798 cricket::ContentInfo* audio_info =
2799 local_answer->description()->GetContentByName("audio");
2800 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002801 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002802
2803 // No crash is a pass.
2804}
2805
deadbeef5e97fb52015-10-15 12:49:08 -07002806// This tests that if a recvonly remote description is set, no remote streams
2807// will be created, even if the description contains SSRCs/MSIDs.
2808// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2809TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2810 FakeConstraints constraints;
2811 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2812 true);
2813 CreatePeerConnection(&constraints);
2814
2815 std::string recvonly_offer = kSdpStringWithStream1;
2816 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2817 strlen(kRecvonly), &recvonly_offer);
2818 CreateAndSetRemoteOffer(recvonly_offer);
2819
2820 EXPECT_EQ(0u, observer_.remote_streams()->count());
2821}
2822
deadbeefab9b2d12015-10-14 11:33:11 -07002823// This tests that a default MediaStream is created if a remote session
2824// description doesn't contain any streams and no MSID support.
2825// It also tests that the default stream is updated if a video m-line is added
2826// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002827TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002828 FakeConstraints constraints;
2829 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2830 true);
2831 CreatePeerConnection(&constraints);
2832 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2833
2834 ASSERT_EQ(1u, observer_.remote_streams()->count());
2835 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2836
2837 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2838 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2839 EXPECT_EQ("default", remote_stream->label());
2840
2841 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2842 ASSERT_EQ(1u, observer_.remote_streams()->count());
2843 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2844 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002845 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2846 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002847 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2848 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002849 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2850 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002851}
2852
2853// This tests that a default MediaStream is created if a remote session
2854// description doesn't contain any streams and media direction is send only.
2855TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002856 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002857 FakeConstraints constraints;
2858 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2859 true);
2860 CreatePeerConnection(&constraints);
2861 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2862
2863 ASSERT_EQ(1u, observer_.remote_streams()->count());
2864 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2865
2866 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2867 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2868 EXPECT_EQ("default", remote_stream->label());
2869}
2870
2871// This tests that it won't crash when PeerConnection tries to remove
2872// a remote track that as already been removed from the MediaStream.
2873TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2874 FakeConstraints constraints;
2875 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2876 true);
2877 CreatePeerConnection(&constraints);
2878 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2879 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2880 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2881 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2882
2883 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2884
2885 // No crash is a pass.
2886}
2887
2888// This tests that a default MediaStream is created if the remote session
2889// description doesn't contain any streams and don't contain an indication if
2890// MSID is supported.
2891TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002892 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002893 FakeConstraints constraints;
2894 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2895 true);
2896 CreatePeerConnection(&constraints);
2897 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2898
2899 ASSERT_EQ(1u, observer_.remote_streams()->count());
2900 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2901 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2902 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2903}
2904
2905// This tests that a default MediaStream is not created if the remote session
2906// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002907TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002908 FakeConstraints constraints;
2909 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2910 true);
2911 CreatePeerConnection(&constraints);
2912 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2913 EXPECT_EQ(0u, observer_.remote_streams()->count());
2914}
2915
deadbeefbda7e0b2015-12-08 17:13:40 -08002916// This tests that when setting a new description, the old default tracks are
2917// not destroyed and recreated.
2918// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002919TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002920 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002921 FakeConstraints constraints;
2922 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2923 true);
2924 CreatePeerConnection(&constraints);
2925 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2926
2927 ASSERT_EQ(1u, observer_.remote_streams()->count());
2928 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2929 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2930
2931 // Set the track to "disabled", then set a new description and ensure the
2932 // track is still disabled, which ensures it hasn't been recreated.
2933 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2934 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2935 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2936 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2937}
2938
deadbeefab9b2d12015-10-14 11:33:11 -07002939// This tests that a default MediaStream is not created if a remote session
2940// description is updated to not have any MediaStreams.
2941TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2942 FakeConstraints constraints;
2943 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2944 true);
2945 CreatePeerConnection(&constraints);
2946 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002947 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002948 EXPECT_TRUE(
2949 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2950
2951 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2952 EXPECT_EQ(0u, observer_.remote_streams()->count());
2953}
2954
2955// This tests that an RtpSender is created when the local description is set
2956// after adding a local stream.
2957// TODO(deadbeef): This test and the one below it need to be updated when
2958// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002959TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002960 FakeConstraints constraints;
2961 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2962 true);
2963 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002964
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002965 // Create an offer with 1 stream with 2 tracks of each type.
2966 rtc::scoped_refptr<StreamCollection> stream_collection =
2967 CreateStreamCollection(1, 2);
2968 pc_->AddStream(stream_collection->at(0));
2969 std::unique_ptr<SessionDescriptionInterface> offer;
2970 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002972
deadbeefab9b2d12015-10-14 11:33:11 -07002973 auto senders = pc_->GetSenders();
2974 EXPECT_EQ(4u, senders.size());
2975 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2976 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2977 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2978 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2979
2980 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002981 pc_->RemoveStream(stream_collection->at(0));
2982 stream_collection = CreateStreamCollection(1, 1);
2983 pc_->AddStream(stream_collection->at(0));
2984 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002985 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986
deadbeefab9b2d12015-10-14 11:33:11 -07002987 senders = pc_->GetSenders();
2988 EXPECT_EQ(2u, senders.size());
2989 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2990 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2991 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2992 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2993}
2994
2995// This tests that an RtpSender is created when the local description is set
2996// before adding a local stream.
2997TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002998 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002999 FakeConstraints constraints;
3000 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3001 true);
3002 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003003
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003004 rtc::scoped_refptr<StreamCollection> stream_collection =
3005 CreateStreamCollection(1, 2);
3006 // Add a stream to create the offer, but remove it afterwards.
3007 pc_->AddStream(stream_collection->at(0));
3008 std::unique_ptr<SessionDescriptionInterface> offer;
3009 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3010 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003011
Steve Antondb45ca82017-09-11 18:27:34 -07003012 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003013 auto senders = pc_->GetSenders();
3014 EXPECT_EQ(0u, senders.size());
3015
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003016 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003017 senders = pc_->GetSenders();
3018 EXPECT_EQ(4u, senders.size());
3019 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3020 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3021 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3022 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3023}
3024
3025// This tests that the expected behavior occurs if the SSRC on a local track is
3026// changed when SetLocalDescription is called.
3027TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003028 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003029 FakeConstraints constraints;
3030 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3031 true);
3032 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003033
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003034 rtc::scoped_refptr<StreamCollection> stream_collection =
3035 CreateStreamCollection(2, 1);
3036 pc_->AddStream(stream_collection->at(0));
3037 std::unique_ptr<SessionDescriptionInterface> offer;
3038 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3039 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003040 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3041 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003042 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3043 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003044 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003045
deadbeefab9b2d12015-10-14 11:33:11 -07003046 auto senders = pc_->GetSenders();
3047 EXPECT_EQ(2u, senders.size());
3048 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3049 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3050
3051 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003052 cricket::MediaContentDescription* desc =
3053 cricket::GetFirstAudioContentDescription(modified_offer->description());
3054 ASSERT_TRUE(desc != NULL);
3055 for (StreamParams& stream : desc->mutable_streams()) {
3056 for (unsigned int& ssrc : stream.ssrcs) {
3057 ++ssrc;
3058 }
3059 }
deadbeefab9b2d12015-10-14 11:33:11 -07003060
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003061 desc =
3062 cricket::GetFirstVideoContentDescription(modified_offer->description());
3063 ASSERT_TRUE(desc != NULL);
3064 for (StreamParams& stream : desc->mutable_streams()) {
3065 for (unsigned int& ssrc : stream.ssrcs) {
3066 ++ssrc;
3067 }
3068 }
3069
Steve Antondb45ca82017-09-11 18:27:34 -07003070 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003071 senders = pc_->GetSenders();
3072 EXPECT_EQ(2u, senders.size());
3073 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3074 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3075 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3076 // changed.
3077}
3078
3079// This tests that the expected behavior occurs if a new session description is
3080// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003081TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003082 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003083 FakeConstraints constraints;
3084 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3085 true);
3086 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003087
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003088 rtc::scoped_refptr<StreamCollection> stream_collection =
3089 CreateStreamCollection(2, 1);
3090 pc_->AddStream(stream_collection->at(0));
3091 std::unique_ptr<SessionDescriptionInterface> offer;
3092 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003093 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003094
deadbeefab9b2d12015-10-14 11:33:11 -07003095 auto senders = pc_->GetSenders();
3096 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003097 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3098 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003099
3100 // Add a new MediaStream but with the same tracks as in the first stream.
3101 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3102 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003103 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3104 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003105 pc_->AddStream(stream_1);
3106
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003107 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003108 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003109
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003110 auto new_senders = pc_->GetSenders();
3111 // Should be the same senders as before, but with updated stream id.
3112 // Note that this behavior is subject to change in the future.
3113 // We may decide the PC should ignore existing tracks in AddStream.
3114 EXPECT_EQ(senders, new_senders);
3115 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3116 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003117}
3118
zhihuang81c3a032016-11-17 12:06:24 -08003119// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3120TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3121 FakeConstraints constraints;
3122 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3123 true);
3124 CreatePeerConnection(&constraints);
3125 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3126 EXPECT_EQ(observer_.num_added_tracks_, 1);
3127 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3128
3129 // Create and set the updated remote SDP.
3130 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3131 EXPECT_EQ(observer_.num_added_tracks_, 2);
3132 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3133}
3134
deadbeefd1a38b52016-12-10 13:15:33 -08003135// Test that when SetConfiguration is called and the configuration is
3136// changing, the next offer causes an ICE restart.
3137TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3138 PeerConnectionInterface::RTCConfiguration config;
3139 config.type = PeerConnectionInterface::kRelay;
3140 // Need to pass default constraints to prevent disabling of DTLS...
3141 FakeConstraints default_constraints;
3142 CreatePeerConnection(config, &default_constraints);
3143 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3144
3145 // Do initial offer/answer so there's something to restart.
3146 CreateOfferAsLocalDescription();
3147 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3148
3149 // Grab the ufrags.
3150 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3151
3152 // Change ICE policy, which should trigger an ICE restart on the next offer.
3153 config.type = PeerConnectionInterface::kAll;
3154 EXPECT_TRUE(pc_->SetConfiguration(config));
3155 CreateOfferAsLocalDescription();
3156
3157 // Grab the new ufrags.
3158 std::vector<std::string> subsequent_ufrags =
3159 GetUfrags(pc_->local_description());
3160
3161 // Sanity check.
3162 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3163 // Check that each ufrag is different.
3164 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3165 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3166 }
3167}
3168
3169// Test that when SetConfiguration is called and the configuration *isn't*
3170// changing, the next offer does *not* cause an ICE restart.
3171TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3172 PeerConnectionInterface::RTCConfiguration config;
3173 config.type = PeerConnectionInterface::kRelay;
3174 // Need to pass default constraints to prevent disabling of DTLS...
3175 FakeConstraints default_constraints;
3176 CreatePeerConnection(config, &default_constraints);
3177 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3178
3179 // Do initial offer/answer so there's something to restart.
3180 CreateOfferAsLocalDescription();
3181 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3182
3183 // Grab the ufrags.
3184 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3185
3186 // Call SetConfiguration with a config identical to what the PC was
3187 // constructed with.
3188 EXPECT_TRUE(pc_->SetConfiguration(config));
3189 CreateOfferAsLocalDescription();
3190
3191 // Grab the new ufrags.
3192 std::vector<std::string> subsequent_ufrags =
3193 GetUfrags(pc_->local_description());
3194
3195 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3196}
3197
3198// Test for a weird corner case scenario:
3199// 1. Audio/video session established.
3200// 2. SetConfiguration changes ICE config; ICE restart needed.
3201// 3. ICE restart initiated by remote peer, but only for one m= section.
3202// 4. Next createOffer should initiate an ICE restart, but only for the other
3203// m= section; it would be pointless to do an ICE restart for the m= section
3204// that was already restarted.
3205TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3206 PeerConnectionInterface::RTCConfiguration config;
3207 config.type = PeerConnectionInterface::kRelay;
3208 // Need to pass default constraints to prevent disabling of DTLS...
3209 FakeConstraints default_constraints;
3210 CreatePeerConnection(config, &default_constraints);
3211 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3212
3213 // Do initial offer/answer so there's something to restart.
3214 CreateOfferAsLocalDescription();
3215 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3216
3217 // Change ICE policy, which should set the "needs-ice-restart" flag.
3218 config.type = PeerConnectionInterface::kAll;
3219 EXPECT_TRUE(pc_->SetConfiguration(config));
3220
3221 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003222 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3223 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3224 kSdpStringWithStream1, nullptr));
3225 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003226 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3227 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003228 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003229 CreateAnswerAsLocalDescription();
3230
3231 // Grab the ufrags.
3232 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3233 ASSERT_EQ(2, initial_ufrags.size());
3234
3235 // Create offer and grab the new ufrags.
3236 CreateOfferAsLocalDescription();
3237 std::vector<std::string> subsequent_ufrags =
3238 GetUfrags(pc_->local_description());
3239 ASSERT_EQ(2, subsequent_ufrags.size());
3240
3241 // Ensure that only the ufrag for the second m= section changed.
3242 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3243 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3244}
3245
deadbeeffe4a8a42016-12-20 17:56:17 -08003246// Tests that the methods to return current/pending descriptions work as
3247// expected at different points in the offer/answer exchange. This test does
3248// one offer/answer exchange as the offerer, then another as the answerer.
3249TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3250 // This disables DTLS so we can apply an answer to ourselves.
3251 CreatePeerConnection();
3252
3253 // Create initial local offer and get SDP (which will also be used as
3254 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003255 std::unique_ptr<SessionDescriptionInterface> local_offer;
3256 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003257 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003258 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003259
3260 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003261 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3262 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3263 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003264 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3265 EXPECT_EQ(nullptr, pc_->current_local_description());
3266 EXPECT_EQ(nullptr, pc_->current_remote_description());
3267
3268 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003269 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003270 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003271 sdp, nullptr));
3272 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3273 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3274 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3275 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003276 EXPECT_EQ(nullptr, pc_->current_local_description());
3277 EXPECT_EQ(nullptr, pc_->current_remote_description());
3278
3279 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003280 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3281 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3282 sdp, nullptr));
3283 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3284 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003285 EXPECT_EQ(nullptr, pc_->pending_local_description());
3286 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003287 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3288 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003289
3290 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003291 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3292 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3293 nullptr));
3294 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3295 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3296 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003297 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003298 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3299 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003300
3301 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003302 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003303 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003304 sdp, nullptr));
3305 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3306 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3307 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3308 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3309 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3310 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003311
3312 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003313 std::unique_ptr<SessionDescriptionInterface> local_answer(
3314 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3315 sdp, nullptr));
3316 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3317 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003318 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3319 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003320 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3321 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003322}
3323
zhihuang77985012017-02-07 15:45:16 -08003324// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3325// after the PeerConnection is closed.
3326TEST_F(PeerConnectionInterfaceTest,
3327 StartAndStopLoggingAfterPeerConnectionClosed) {
3328 CreatePeerConnection();
3329 // The RtcEventLog will be reset when the PeerConnection is closed.
3330 pc_->Close();
3331
3332 rtc::PlatformFile file = 0;
3333 int64_t max_size_bytes = 1024;
3334 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3335 pc_->StopRtcEventLog();
3336}
3337
deadbeef30952b42017-04-21 02:41:29 -07003338// Test that generated offers/answers include "ice-option:trickle".
3339TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3340 CreatePeerConnection();
3341
3342 // First, create an offer with audio/video.
3343 FakeConstraints constraints;
3344 constraints.SetMandatoryReceiveAudio(true);
3345 constraints.SetMandatoryReceiveVideo(true);
3346 std::unique_ptr<SessionDescriptionInterface> offer;
3347 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3348 cricket::SessionDescription* desc = offer->description();
3349 ASSERT_EQ(2u, desc->transport_infos().size());
3350 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3351 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3352
3353 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003354 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003355 std::unique_ptr<SessionDescriptionInterface> answer;
3356 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3357 desc = answer->description();
3358 ASSERT_EQ(2u, desc->transport_infos().size());
3359 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3360 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3361}
3362
deadbeef1dcb1642017-03-29 21:08:16 -07003363// Test that ICE renomination isn't offered if it's not enabled in the PC's
3364// RTCConfiguration.
3365TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3366 PeerConnectionInterface::RTCConfiguration config;
3367 config.enable_ice_renomination = false;
3368 CreatePeerConnection(config, nullptr);
3369 AddVoiceStream("foo");
3370
3371 std::unique_ptr<SessionDescriptionInterface> offer;
3372 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3373 cricket::SessionDescription* desc = offer->description();
3374 EXPECT_EQ(1u, desc->transport_infos().size());
3375 EXPECT_FALSE(
3376 desc->transport_infos()[0].description.GetIceParameters().renomination);
3377}
3378
3379// Test that the ICE renomination option is present in generated offers/answers
3380// if it's enabled in the PC's RTCConfiguration.
3381TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3382 PeerConnectionInterface::RTCConfiguration config;
3383 config.enable_ice_renomination = true;
3384 CreatePeerConnection(config, nullptr);
3385 AddVoiceStream("foo");
3386
3387 std::unique_ptr<SessionDescriptionInterface> offer;
3388 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3389 cricket::SessionDescription* desc = offer->description();
3390 EXPECT_EQ(1u, desc->transport_infos().size());
3391 EXPECT_TRUE(
3392 desc->transport_infos()[0].description.GetIceParameters().renomination);
3393
3394 // Set the offer as a remote description, then create an answer and ensure it
3395 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003396 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003397 std::unique_ptr<SessionDescriptionInterface> answer;
3398 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3399 desc = answer->description();
3400 EXPECT_EQ(1u, desc->transport_infos().size());
3401 EXPECT_TRUE(
3402 desc->transport_infos()[0].description.GetIceParameters().renomination);
3403}
3404
3405// Test that if CreateOffer is called with the deprecated "offer to receive
3406// audio/video" constraints, they're processed and result in an offer with
3407// audio/video sections just as if RTCOfferAnswerOptions had been used.
3408TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3409 CreatePeerConnection();
3410
3411 FakeConstraints constraints;
3412 constraints.SetMandatoryReceiveAudio(true);
3413 constraints.SetMandatoryReceiveVideo(true);
3414 std::unique_ptr<SessionDescriptionInterface> offer;
3415 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3416
3417 cricket::SessionDescription* desc = offer->description();
3418 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3419 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3420 ASSERT_NE(nullptr, audio);
3421 ASSERT_NE(nullptr, video);
3422 EXPECT_FALSE(audio->rejected);
3423 EXPECT_FALSE(video->rejected);
3424}
3425
3426// Test that if CreateAnswer is called with the deprecated "offer to receive
3427// audio/video" constraints, they're processed and can be used to reject an
3428// offered m= section just as can be done with RTCOfferAnswerOptions;
3429TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3430 CreatePeerConnection();
3431
3432 // First, create an offer with audio/video and apply it as a remote
3433 // description.
3434 FakeConstraints constraints;
3435 constraints.SetMandatoryReceiveAudio(true);
3436 constraints.SetMandatoryReceiveVideo(true);
3437 std::unique_ptr<SessionDescriptionInterface> offer;
3438 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003439 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003440
3441 // Now create answer that rejects audio/video.
3442 constraints.SetMandatoryReceiveAudio(false);
3443 constraints.SetMandatoryReceiveVideo(false);
3444 std::unique_ptr<SessionDescriptionInterface> answer;
3445 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3446
3447 cricket::SessionDescription* desc = answer->description();
3448 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3449 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3450 ASSERT_NE(nullptr, audio);
3451 ASSERT_NE(nullptr, video);
3452 EXPECT_TRUE(audio->rejected);
3453 EXPECT_TRUE(video->rejected);
3454}
3455
3456#ifdef HAVE_SCTP
3457#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3458 DataChannelOnlyOfferWithMaxBundlePolicy
3459#else
3460#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3461 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3462#endif
3463
3464// Test that negotiation can succeed with a data channel only, and with the max
3465// bundle policy. Previously there was a bug that prevented this.
3466TEST_F(PeerConnectionInterfaceTest,
3467 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3468 PeerConnectionInterface::RTCConfiguration config;
3469 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3470 CreatePeerConnection(config, nullptr);
3471
3472 // First, create an offer with only a data channel and apply it as a remote
3473 // description.
3474 pc_->CreateDataChannel("test", nullptr);
3475 std::unique_ptr<SessionDescriptionInterface> offer;
3476 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003477 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003478
3479 // Create and set answer as well.
3480 std::unique_ptr<SessionDescriptionInterface> answer;
3481 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003482 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003483}
3484
zstein4b979802017-06-02 14:37:37 -07003485TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3486 CreatePeerConnection();
3487 PeerConnectionInterface::BitrateParameters bitrate;
3488 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3489 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3490}
3491
3492TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3493 CreatePeerConnection();
3494 PeerConnectionInterface::BitrateParameters bitrate;
3495 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3496 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3497}
3498
3499TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3500 CreatePeerConnection();
3501 PeerConnectionInterface::BitrateParameters bitrate;
3502 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3503 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3504 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3505}
3506
3507TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3508 CreatePeerConnection();
3509 PeerConnectionInterface::BitrateParameters bitrate;
3510 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3511 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3512}
3513
3514TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3515 CreatePeerConnection();
3516 PeerConnectionInterface::BitrateParameters bitrate;
3517 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3518 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3519 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3520}
3521
3522TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3523 CreatePeerConnection();
3524 PeerConnectionInterface::BitrateParameters bitrate;
3525 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3526 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3527 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3528}
3529
3530TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3531 CreatePeerConnection();
3532 PeerConnectionInterface::BitrateParameters bitrate;
3533 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3534 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3535}
3536
Steve Anton038834f2017-07-14 15:59:59 -07003537// ice_regather_interval_range requires WebRTC to be configured for continual
3538// gathering already.
3539TEST_F(PeerConnectionInterfaceTest,
3540 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3541 PeerConnectionInterface::RTCConfiguration config;
3542 config.ice_regather_interval_range.emplace(1000, 2000);
3543 config.continual_gathering_policy =
3544 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3545 CreatePeerConnectionExpectFail(config);
3546}
3547
3548// Ensures that there is no error when ice_regather_interval_range is set with
3549// continual gathering enabled.
3550TEST_F(PeerConnectionInterfaceTest,
3551 SetIceRegatherIntervalRangeWithContinualGathering) {
3552 PeerConnectionInterface::RTCConfiguration config;
3553 config.ice_regather_interval_range.emplace(1000, 2000);
3554 config.continual_gathering_policy =
3555 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3556 CreatePeerConnection(config, nullptr);
3557}
3558
zstein4b979802017-06-02 14:37:37 -07003559// The current bitrate from Call's BitrateConfigMask is currently clamped by
3560// Call's BitrateConfig, which comes from the SDP or a default value. This test
3561// checks that a call to SetBitrate with a current bitrate that will be clamped
3562// succeeds.
3563TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3564 CreatePeerConnection();
3565 PeerConnectionInterface::BitrateParameters bitrate;
3566 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3567 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3568}
3569
zhihuang1c378ed2017-08-17 14:10:50 -07003570// The following tests verify that the offer can be created correctly.
3571TEST_F(PeerConnectionInterfaceTest,
3572 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3573 RTCOfferAnswerOptions rtc_options;
3574
3575 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3576 // than kMaxOfferToReceiveMedia should be treated as invalid.
3577 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3578 CreatePeerConnection();
3579 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3580
3581 rtc_options.offer_to_receive_audio =
3582 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3583 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3584}
3585
3586TEST_F(PeerConnectionInterfaceTest,
3587 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3588 RTCOfferAnswerOptions rtc_options;
3589
3590 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3591 // than kMaxOfferToReceiveMedia should be treated as invalid.
3592 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3593 CreatePeerConnection();
3594 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3595
3596 rtc_options.offer_to_receive_video =
3597 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3598 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3599}
3600
3601// Test that the audio and video content will be added to an offer if both
3602// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3603TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3604 RTCOfferAnswerOptions rtc_options;
3605 rtc_options.offer_to_receive_audio = 1;
3606 rtc_options.offer_to_receive_video = 1;
3607
3608 std::unique_ptr<SessionDescriptionInterface> offer;
3609 CreatePeerConnection();
3610 offer = CreateOfferWithOptions(rtc_options);
3611 ASSERT_TRUE(offer);
3612 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3613 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3614}
3615
3616// Test that only audio content will be added to the offer if only
3617// |offer_to_receive_audio| options is 1.
3618TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3619 RTCOfferAnswerOptions rtc_options;
3620 rtc_options.offer_to_receive_audio = 1;
3621 rtc_options.offer_to_receive_video = 0;
3622
3623 std::unique_ptr<SessionDescriptionInterface> offer;
3624 CreatePeerConnection();
3625 offer = CreateOfferWithOptions(rtc_options);
3626 ASSERT_TRUE(offer);
3627 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3628 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3629}
3630
3631// Test that only video content will be added if only |offer_to_receive_video|
3632// options is 1.
3633TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3634 RTCOfferAnswerOptions rtc_options;
3635 rtc_options.offer_to_receive_audio = 0;
3636 rtc_options.offer_to_receive_video = 1;
3637
3638 std::unique_ptr<SessionDescriptionInterface> offer;
3639 CreatePeerConnection();
3640 offer = CreateOfferWithOptions(rtc_options);
3641 ASSERT_TRUE(offer);
3642 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3643 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3644}
3645
3646// Test that if |voice_activity_detection| is false, no CN codec is added to the
3647// offer.
3648TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3649 RTCOfferAnswerOptions rtc_options;
3650 rtc_options.offer_to_receive_audio = 1;
3651 rtc_options.offer_to_receive_video = 0;
3652
3653 std::unique_ptr<SessionDescriptionInterface> offer;
3654 CreatePeerConnection();
3655 offer = CreateOfferWithOptions(rtc_options);
3656 ASSERT_TRUE(offer);
3657 const cricket::ContentInfo* audio_content =
3658 offer->description()->GetContentByName(cricket::CN_AUDIO);
3659 ASSERT_TRUE(audio_content);
3660 // |voice_activity_detection| is true by default.
3661 EXPECT_TRUE(HasCNCodecs(audio_content));
3662
3663 rtc_options.voice_activity_detection = false;
3664 CreatePeerConnection();
3665 offer = CreateOfferWithOptions(rtc_options);
3666 ASSERT_TRUE(offer);
3667 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3668 ASSERT_TRUE(audio_content);
3669 EXPECT_FALSE(HasCNCodecs(audio_content));
3670}
3671
3672// Test that no media content will be added to the offer if using default
3673// RTCOfferAnswerOptions.
3674TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3675 RTCOfferAnswerOptions rtc_options;
3676
3677 std::unique_ptr<SessionDescriptionInterface> offer;
3678 CreatePeerConnection();
3679 offer = CreateOfferWithOptions(rtc_options);
3680 ASSERT_TRUE(offer);
3681 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3682 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3683}
3684
3685// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3686// ufrag/pwd will be the same in the new offer.
3687TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3688 RTCOfferAnswerOptions rtc_options;
3689 rtc_options.ice_restart = false;
3690 rtc_options.offer_to_receive_audio = 1;
3691
3692 std::unique_ptr<SessionDescriptionInterface> offer;
3693 CreatePeerConnection();
3694 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3695 auto ufrag1 = offer->description()
3696 ->GetTransportInfoByName(cricket::CN_AUDIO)
3697 ->description.ice_ufrag;
3698 auto pwd1 = offer->description()
3699 ->GetTransportInfoByName(cricket::CN_AUDIO)
3700 ->description.ice_pwd;
3701
3702 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3703 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3704 auto ufrag2 = offer->description()
3705 ->GetTransportInfoByName(cricket::CN_AUDIO)
3706 ->description.ice_ufrag;
3707 auto pwd2 = offer->description()
3708 ->GetTransportInfoByName(cricket::CN_AUDIO)
3709 ->description.ice_pwd;
3710
3711 // |ice_restart| is true, the ufrag/pwd should change.
3712 rtc_options.ice_restart = true;
3713 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3714 auto ufrag3 = offer->description()
3715 ->GetTransportInfoByName(cricket::CN_AUDIO)
3716 ->description.ice_ufrag;
3717 auto pwd3 = offer->description()
3718 ->GetTransportInfoByName(cricket::CN_AUDIO)
3719 ->description.ice_pwd;
3720
3721 EXPECT_EQ(ufrag1, ufrag2);
3722 EXPECT_EQ(pwd1, pwd2);
3723 EXPECT_NE(ufrag2, ufrag3);
3724 EXPECT_NE(pwd2, pwd3);
3725}
3726
3727// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3728// offer; if it is false, there won't be any bundle group in the offer.
3729TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3730 RTCOfferAnswerOptions rtc_options;
3731 rtc_options.offer_to_receive_audio = 1;
3732 rtc_options.offer_to_receive_video = 1;
3733
3734 std::unique_ptr<SessionDescriptionInterface> offer;
3735 CreatePeerConnection();
3736
3737 rtc_options.use_rtp_mux = true;
3738 offer = CreateOfferWithOptions(rtc_options);
3739 ASSERT_TRUE(offer);
3740 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3741 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3742 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3743
3744 rtc_options.use_rtp_mux = false;
3745 offer = CreateOfferWithOptions(rtc_options);
3746 ASSERT_TRUE(offer);
3747 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3748 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3749 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3750}
3751
3752// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3753// called for the answer constraints, but an audio and a video section were
3754// offered, there will still be an audio and a video section in the answer.
3755TEST_F(PeerConnectionInterfaceTest,
3756 RejectAudioAndVideoInAnswerWithConstraints) {
3757 // Offer both audio and video.
3758 RTCOfferAnswerOptions rtc_offer_options;
3759 rtc_offer_options.offer_to_receive_audio = 1;
3760 rtc_offer_options.offer_to_receive_video = 1;
3761
3762 CreatePeerConnection();
3763 std::unique_ptr<SessionDescriptionInterface> offer;
3764 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3765 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3766 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3767
3768 // Since an offer has been created with both audio and video,
3769 // Answers will contain the media types that exist in the offer regardless of
3770 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3771 FakeConstraints answer_c;
3772 // Reject both audio and video.
3773 answer_c.SetMandatoryReceiveAudio(false);
3774 answer_c.SetMandatoryReceiveVideo(false);
3775
3776 std::unique_ptr<SessionDescriptionInterface> answer;
3777 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3778 const cricket::ContentInfo* audio_content =
3779 GetFirstAudioContent(answer->description());
3780 const cricket::ContentInfo* video_content =
3781 GetFirstVideoContent(answer->description());
3782 ASSERT_NE(nullptr, audio_content);
3783 ASSERT_NE(nullptr, video_content);
3784 EXPECT_TRUE(audio_content->rejected);
3785 EXPECT_TRUE(video_content->rejected);
3786}
3787
zhihuang141aacb2017-08-29 13:23:53 -07003788// This test ensures OnRenegotiationNeeded is called when we add track with
3789// MediaStream -> AddTrack in the same way it is called when we add track with
3790// PeerConnection -> AddTrack.
3791// The test can be removed once addStream is rewritten in terms of addTrack
3792// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3793TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3794 CreatePeerConnectionWithoutDtls();
3795 rtc::scoped_refptr<MediaStreamInterface> stream(
3796 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3797 pc_->AddStream(stream);
3798 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3799 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3800 rtc::scoped_refptr<VideoTrackInterface> video_track(
3801 pc_factory_->CreateVideoTrack(
3802 "video_track", pc_factory_->CreateVideoSource(
3803 std::unique_ptr<cricket::VideoCapturer>(
3804 new cricket::FakeVideoCapturer()))));
3805 stream->AddTrack(audio_track);
3806 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3807 observer_.renegotiation_needed_ = false;
3808
3809 stream->AddTrack(video_track);
3810 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3811 observer_.renegotiation_needed_ = false;
3812
3813 stream->RemoveTrack(audio_track);
3814 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3815 observer_.renegotiation_needed_ = false;
3816
3817 stream->RemoveTrack(video_track);
3818 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3819 observer_.renegotiation_needed_ = false;
3820}
3821
3822// Tests that creating answer would fail gracefully without being crashed if the
3823// remote description is unset.
3824TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3825 CreatePeerConnection();
3826 // Creating answer fails because the remote description is unset.
3827 std::unique_ptr<SessionDescriptionInterface> answer;
3828 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3829
3830 // Createing answer succeeds when the remote description is set.
3831 CreateOfferAsRemoteDescription();
3832 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3833}
3834
Zhi Huang2a5e4262017-09-14 01:15:03 -07003835// Test that an error is returned if a description is applied that doesn't
3836// respect the order of existing media sections.
3837TEST_F(PeerConnectionInterfaceTest,
3838 MediaSectionOrderEnforcedForSubsequentOffers) {
3839 CreatePeerConnection();
3840 FakeConstraints constraints;
3841 constraints.SetMandatoryReceiveAudio(true);
3842 constraints.SetMandatoryReceiveVideo(true);
3843 std::unique_ptr<SessionDescriptionInterface> offer;
3844 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3845 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3846
3847 std::unique_ptr<SessionDescriptionInterface> answer;
3848 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3849 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3850
3851 // A remote offer with different m=line order should be rejected.
3852 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3853 std::reverse(offer->description()->contents().begin(),
3854 offer->description()->contents().end());
3855 std::reverse(offer->description()->transport_infos().begin(),
3856 offer->description()->transport_infos().end());
3857 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3858
3859 // A subsequent local offer with different m=line order should be rejected.
3860 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3861 std::reverse(offer->description()->contents().begin(),
3862 offer->description()->contents().end());
3863 std::reverse(offer->description()->transport_infos().begin(),
3864 offer->description()->transport_infos().end());
3865 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3866}
3867
nisse51542be2016-02-12 02:27:06 -08003868class PeerConnectionMediaConfigTest : public testing::Test {
3869 protected:
3870 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003871 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003872 pcf_->Initialize();
3873 }
nisseeaabdf62017-05-05 02:23:02 -07003874 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003875 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003876 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003877 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3878 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003879 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003880 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003881 }
3882
zhihuang9763d562016-08-05 11:14:50 -07003883 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003884 MockPeerConnectionObserver observer_;
3885};
3886
3887// This test verifies the default behaviour with no constraints and a
3888// default RTCConfiguration.
3889TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3890 PeerConnectionInterface::RTCConfiguration config;
3891 FakeConstraints constraints;
3892
3893 const cricket::MediaConfig& media_config =
3894 TestCreatePeerConnection(config, &constraints);
3895
3896 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003897 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3898 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3899 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003900}
3901
3902// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003903// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003904TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3905 PeerConnectionInterface::RTCConfiguration config;
3906 FakeConstraints constraints;
3907
3908 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3909 const cricket::MediaConfig& media_config =
3910 TestCreatePeerConnection(config, &constraints);
3911
3912 EXPECT_TRUE(media_config.enable_dscp);
3913}
3914
3915// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003916// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003917TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3918 PeerConnectionInterface::RTCConfiguration config;
3919 FakeConstraints constraints;
3920
3921 constraints.AddOptional(
3922 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3923 const cricket::MediaConfig media_config =
3924 TestCreatePeerConnection(config, &constraints);
3925
nisse0db023a2016-03-01 04:29:59 -08003926 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003927}
3928
3929// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003930// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003931TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3932 PeerConnectionInterface::RTCConfiguration config;
3933 FakeConstraints constraints;
3934
Niels Möller71bdda02016-03-31 12:59:59 +02003935 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003936 const cricket::MediaConfig& media_config =
3937 TestCreatePeerConnection(config, &constraints);
3938
nisse0db023a2016-03-01 04:29:59 -08003939 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3940}
3941
3942// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003943// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003944TEST_F(PeerConnectionMediaConfigTest,
3945 TestSuspendBelowMinBitrateConstraintTrue) {
3946 PeerConnectionInterface::RTCConfiguration config;
3947 FakeConstraints constraints;
3948
3949 constraints.AddOptional(
3950 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3951 true);
3952 const cricket::MediaConfig media_config =
3953 TestCreatePeerConnection(config, &constraints);
3954
3955 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003956}
3957
deadbeef293e9262017-01-11 12:28:30 -08003958// Tests a few random fields being different.
3959TEST(RTCConfigurationTest, ComparisonOperators) {
3960 PeerConnectionInterface::RTCConfiguration a;
3961 PeerConnectionInterface::RTCConfiguration b;
3962 EXPECT_EQ(a, b);
3963
3964 PeerConnectionInterface::RTCConfiguration c;
3965 c.servers.push_back(PeerConnectionInterface::IceServer());
3966 EXPECT_NE(a, c);
3967
3968 PeerConnectionInterface::RTCConfiguration d;
3969 d.type = PeerConnectionInterface::kRelay;
3970 EXPECT_NE(a, d);
3971
3972 PeerConnectionInterface::RTCConfiguration e;
3973 e.audio_jitter_buffer_max_packets = 5;
3974 EXPECT_NE(a, e);
3975
3976 PeerConnectionInterface::RTCConfiguration f;
3977 f.ice_connection_receiving_timeout = 1337;
3978 EXPECT_NE(a, f);
3979
3980 PeerConnectionInterface::RTCConfiguration g;
3981 g.disable_ipv6 = true;
3982 EXPECT_NE(a, g);
3983
3984 PeerConnectionInterface::RTCConfiguration h(
3985 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3986 EXPECT_NE(a, h);
3987}