blob: a3cf3159a888d3ca0122872f3ff4bf24e62b0055 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "api/rtpreceiverinterface.h"
23#include "api/rtpsenderinterface.h"
24#include "api/test/fakeconstraints.h"
25#include "media/base/fakevideocapturer.h"
26#include "media/engine/webrtcmediaengine.h"
27#include "media/sctp/sctptransportinternal.h"
28#include "modules/audio_processing/include/audio_processing.h"
29#include "p2p/base/fakeportallocator.h"
30#include "pc/audiotrack.h"
31#include "pc/mediasession.h"
32#include "pc/mediastream.h"
33#include "pc/peerconnection.h"
34#include "pc/streamcollection.h"
35#include "pc/test/fakeaudiocapturemodule.h"
36#include "pc/test/fakertccertificategenerator.h"
37#include "pc/test/fakevideotracksource.h"
38#include "pc/test/mockpeerconnectionobservers.h"
39#include "pc/test/testsdpstrings.h"
40#include "pc/videocapturertracksource.h"
41#include "pc/videotrack.h"
42#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/virtualsocketserver.h"
45#include "test/gmock.h"
kwibergac9f8762016-09-30 22:29:43 -070046
47#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070049#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51static const char kStreamLabel1[] = "local_stream_1";
52static const char kStreamLabel2[] = "local_stream_2";
53static const char kStreamLabel3[] = "local_stream_3";
54static const int kDefaultStunPort = 3478;
55static const char kStunAddressOnly[] = "stun:address";
56static const char kStunInvalidPort[] = "stun:address:-1";
57static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
58static const char kStunAddressPortAndMore2[] = "stun:address:port more";
59static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
60static const char kTurnUsername[] = "user";
61static const char kTurnPassword[] = "password";
62static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020063static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
deadbeefab9b2d12015-10-14 11:33:11 -070065static const char kStreams[][8] = {"stream1", "stream2"};
66static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
67static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
68
deadbeef5e97fb52015-10-15 12:49:08 -070069static const char kRecvonly[] = "recvonly";
70static const char kSendrecv[] = "sendrecv";
71
deadbeefab9b2d12015-10-14 11:33:11 -070072// Reference SDP with a MediaStream with label "stream1" and audio track with
73// id "audio_1" and a video track with id "video_1;
74static const char kSdpStringWithStream1[] =
75 "v=0\r\n"
76 "o=- 0 0 IN IP4 127.0.0.1\r\n"
77 "s=-\r\n"
78 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080079 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=ice-ufrag:e5785931\r\n"
81 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
82 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
83 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070085 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080086 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:103 ISAC/16000\r\n"
88 "a=ssrc:1 cname:stream1\r\n"
89 "a=ssrc:1 mslabel:stream1\r\n"
90 "a=ssrc:1 label:audiotrack0\r\n"
91 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080092 "a=ice-ufrag:e5785931\r\n"
93 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
94 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
95 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070097 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080098 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=rtpmap:120 VP8/90000\r\n"
100 "a=ssrc:2 cname:stream1\r\n"
101 "a=ssrc:2 mslabel:stream1\r\n"
102 "a=ssrc:2 label:videotrack0\r\n";
103
zhihuang81c3a032016-11-17 12:06:24 -0800104// Reference SDP with a MediaStream with label "stream1" and audio track with
105// id "audio_1";
106static const char kSdpStringWithStream1AudioTrackOnly[] =
107 "v=0\r\n"
108 "o=- 0 0 IN IP4 127.0.0.1\r\n"
109 "s=-\r\n"
110 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800111 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800112 "a=ice-ufrag:e5785931\r\n"
113 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
114 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
115 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800116 "a=mid:audio\r\n"
117 "a=sendrecv\r\n"
118 "a=rtpmap:103 ISAC/16000\r\n"
119 "a=ssrc:1 cname:stream1\r\n"
120 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800121 "a=ssrc:1 label:audiotrack0\r\n"
122 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800123
deadbeefab9b2d12015-10-14 11:33:11 -0700124// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
125// MediaStreams have one audio track and one video track.
126// This uses MSID.
127static const char kSdpStringWithStream1And2[] =
128 "v=0\r\n"
129 "o=- 0 0 IN IP4 127.0.0.1\r\n"
130 "s=-\r\n"
131 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800132 "a=msid-semantic: WMS stream1 stream2\r\n"
133 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=ice-ufrag:e5785931\r\n"
135 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
136 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
137 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700139 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800140 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:103 ISAC/16000\r\n"
142 "a=ssrc:1 cname:stream1\r\n"
143 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
144 "a=ssrc:3 cname:stream2\r\n"
145 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
146 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700152 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800153 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=rtpmap:120 VP8/0\r\n"
155 "a=ssrc:2 cname:stream1\r\n"
156 "a=ssrc:2 msid:stream1 videotrack0\r\n"
157 "a=ssrc:4 cname:stream2\r\n"
158 "a=ssrc:4 msid:stream2 videotrack1\r\n";
159
160// Reference SDP without MediaStreams. Msid is not supported.
161static const char kSdpStringWithoutStreams[] =
162 "v=0\r\n"
163 "o=- 0 0 IN IP4 127.0.0.1\r\n"
164 "s=-\r\n"
165 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800166 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700172 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800173 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n"
175 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800176 "a=ice-ufrag:e5785931\r\n"
177 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
178 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
179 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700181 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800182 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=rtpmap:120 VP8/90000\r\n";
184
185// Reference SDP without MediaStreams. Msid is supported.
186static const char kSdpStringWithMsidWithoutStreams[] =
187 "v=0\r\n"
188 "o=- 0 0 IN IP4 127.0.0.1\r\n"
189 "s=-\r\n"
190 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800191 "a=msid-semantic: WMS\r\n"
192 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700193 "a=ice-ufrag:e5785931\r\n"
194 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
195 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
196 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700198 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800199 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=rtpmap:103 ISAC/16000\r\n"
201 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700207 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800208 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=rtpmap:120 VP8/90000\r\n";
210
211// Reference SDP without MediaStreams and audio only.
212static const char kSdpStringWithoutStreamsAudioOnly[] =
213 "v=0\r\n"
214 "o=- 0 0 IN IP4 127.0.0.1\r\n"
215 "s=-\r\n"
216 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800217 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=ice-ufrag:e5785931\r\n"
219 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
220 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
221 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700223 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800224 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=rtpmap:103 ISAC/16000\r\n";
226
227// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
228static const char kSdpStringSendOnlyWithoutStreams[] =
229 "v=0\r\n"
230 "o=- 0 0 IN IP4 127.0.0.1\r\n"
231 "s=-\r\n"
232 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800233 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700234 "a=ice-ufrag:e5785931\r\n"
235 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
236 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
237 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700239 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800241 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=rtpmap:103 ISAC/16000\r\n"
243 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800244 "a=ice-ufrag:e5785931\r\n"
245 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
246 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
247 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700248 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700249 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800251 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=rtpmap:120 VP8/90000\r\n";
253
254static const char kSdpStringInit[] =
255 "v=0\r\n"
256 "o=- 0 0 IN IP4 127.0.0.1\r\n"
257 "s=-\r\n"
258 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700259 "a=msid-semantic: WMS\r\n";
260
261static const char kSdpStringAudio[] =
262 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800263 "a=ice-ufrag:e5785931\r\n"
264 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
265 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
266 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700268 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800269 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=rtpmap:103 ISAC/16000\r\n";
271
272static const char kSdpStringVideo[] =
273 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800274 "a=ice-ufrag:e5785931\r\n"
275 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
276 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
277 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700279 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800280 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=rtpmap:120 VP8/90000\r\n";
282
283static const char kSdpStringMs1Audio0[] =
284 "a=ssrc:1 cname:stream1\r\n"
285 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
286
287static const char kSdpStringMs1Video0[] =
288 "a=ssrc:2 cname:stream1\r\n"
289 "a=ssrc:2 msid:stream1 videotrack0\r\n";
290
291static const char kSdpStringMs1Audio1[] =
292 "a=ssrc:3 cname:stream1\r\n"
293 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
294
295static const char kSdpStringMs1Video1[] =
296 "a=ssrc:4 cname:stream1\r\n"
297 "a=ssrc:4 msid:stream1 videotrack1\r\n";
298
deadbeef8662f942017-01-20 21:20:51 -0800299static const char kDtlsSdesFallbackSdp[] =
300 "v=0\r\n"
301 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
302 "s=-\r\n"
303 "c=IN IP4 0.0.0.0\r\n"
304 "t=0 0\r\n"
305 "a=group:BUNDLE audio\r\n"
306 "a=msid-semantic: WMS\r\n"
307 "m=audio 1 RTP/SAVPF 0\r\n"
308 "a=sendrecv\r\n"
309 "a=rtcp-mux\r\n"
310 "a=mid:audio\r\n"
311 "a=ssrc:1 cname:stream1\r\n"
312 "a=ssrc:1 mslabel:stream1\r\n"
313 "a=ssrc:1 label:audiotrack0\r\n"
314 "a=ice-ufrag:e5785931\r\n"
315 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
316 "a=rtpmap:0 pcmu/8000\r\n"
317 "a=fingerprint:sha-1 "
318 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
319 "a=setup:actpass\r\n"
320 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
321 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
322 "dummy_session_params\r\n";
323
perkjd61bf802016-03-24 03:16:19 -0700324using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700325using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700327using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::AudioTrackInterface;
329using webrtc::DataBuffer;
330using webrtc::DataChannelInterface;
331using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700333using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700334using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::MediaStreamInterface;
336using webrtc::MediaStreamTrackInterface;
337using webrtc::MockCreateSessionDescriptionObserver;
338using webrtc::MockDataChannelObserver;
339using webrtc::MockSetSessionDescriptionObserver;
340using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700341using webrtc::NotifierInterface;
342using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343using webrtc::PeerConnectionInterface;
344using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800345using webrtc::RTCError;
346using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700347using webrtc::RtpReceiverInterface;
348using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349using webrtc::SdpParseError;
350using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700351using webrtc::StreamCollection;
352using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100353using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700354using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355using webrtc::VideoTrackInterface;
356
deadbeefab9b2d12015-10-14 11:33:11 -0700357typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
358
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359namespace {
360
361// Gets the first ssrc of given content type from the ContentInfo.
362bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
363 if (!content_info || !ssrc) {
364 return false;
365 }
366 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000367 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 content_info->description);
369 if (!media_desc || media_desc->streams().empty()) {
370 return false;
371 }
372 *ssrc = media_desc->streams().begin()->first_ssrc();
373 return true;
374}
375
deadbeefd1a38b52016-12-10 13:15:33 -0800376// Get the ufrags out of an SDP blob. Useful for testing ICE restart
377// behavior.
378std::vector<std::string> GetUfrags(
379 const webrtc::SessionDescriptionInterface* desc) {
380 std::vector<std::string> ufrags;
381 for (const cricket::TransportInfo& info :
382 desc->description()->transport_infos()) {
383 ufrags.push_back(info.description.ice_ufrag);
384 }
385 return ufrags;
386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388void SetSsrcToZero(std::string* sdp) {
389 const char kSdpSsrcAtribute[] = "a=ssrc:";
390 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
391 size_t ssrc_pos = 0;
392 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
393 std::string::npos) {
394 size_t end_ssrc = sdp->find(" ", ssrc_pos);
395 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
396 ssrc_pos = end_ssrc;
397 }
398}
399
deadbeefab9b2d12015-10-14 11:33:11 -0700400// Check if |streams| contains the specified track.
401bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
402 const std::string& stream_label,
403 const std::string& track_id) {
404 for (const cricket::StreamParams& params : streams) {
405 if (params.sync_label == stream_label && params.id == track_id) {
406 return true;
407 }
408 }
409 return false;
410}
411
412// Check if |senders| contains the specified sender, by id.
413bool ContainsSender(
414 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
415 const std::string& id) {
416 for (const auto& sender : senders) {
417 if (sender->id() == id) {
418 return true;
419 }
420 }
421 return false;
422}
423
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700424// Check if |senders| contains the specified sender, by id and stream id.
425bool ContainsSender(
426 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
427 const std::string& id,
428 const std::string& stream_id) {
429 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700430 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700431 return true;
432 }
433 }
434 return false;
435}
436
deadbeefab9b2d12015-10-14 11:33:11 -0700437// Create a collection of streams.
438// CreateStreamCollection(1) creates a collection that
439// correspond to kSdpStringWithStream1.
440// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
441rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700442 int number_of_streams,
443 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700444 rtc::scoped_refptr<StreamCollection> local_collection(
445 StreamCollection::Create());
446
447 for (int i = 0; i < number_of_streams; ++i) {
448 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
449 webrtc::MediaStream::Create(kStreams[i]));
450
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700451 for (int j = 0; j < tracks_per_stream; ++j) {
452 // Add a local audio track.
453 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
454 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
455 nullptr));
456 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700457
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700458 // Add a local video track.
459 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
460 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700461 webrtc::FakeVideoTrackSource::Create(),
462 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700463 stream->AddTrack(video_track);
464 }
deadbeefab9b2d12015-10-14 11:33:11 -0700465
466 local_collection->AddStream(stream);
467 }
468 return local_collection;
469}
470
471// Check equality of StreamCollections.
472bool CompareStreamCollections(StreamCollectionInterface* s1,
473 StreamCollectionInterface* s2) {
474 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
475 return false;
476 }
477
478 for (size_t i = 0; i != s1->count(); ++i) {
479 if (s1->at(i)->label() != s2->at(i)->label()) {
480 return false;
481 }
482 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
483 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
484 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
485 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
486
487 if (audio_tracks1.size() != audio_tracks2.size()) {
488 return false;
489 }
490 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
491 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
492 return false;
493 }
494 }
495 if (video_tracks1.size() != video_tracks2.size()) {
496 return false;
497 }
498 for (size_t j = 0; j != video_tracks1.size(); ++j) {
499 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
500 return false;
501 }
502 }
503 }
504 return true;
505}
506
perkjd61bf802016-03-24 03:16:19 -0700507// Helper class to test Observer.
508class MockTrackObserver : public ObserverInterface {
509 public:
510 explicit MockTrackObserver(NotifierInterface* notifier)
511 : notifier_(notifier) {
512 notifier_->RegisterObserver(this);
513 }
514
515 ~MockTrackObserver() { Unregister(); }
516
517 void Unregister() {
518 if (notifier_) {
519 notifier_->UnregisterObserver(this);
520 notifier_ = nullptr;
521 }
522 }
523
524 MOCK_METHOD0(OnChanged, void());
525
526 private:
527 NotifierInterface* notifier_;
528};
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530class MockPeerConnectionObserver : public PeerConnectionObserver {
531 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700532 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200533 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
535 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
536 pc_ = pc;
537 if (pc) {
538 state_ = pc_->signaling_state();
539 }
540 }
nisseef8b61e2016-04-29 06:09:15 -0700541 void OnSignalingChange(
542 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 EXPECT_EQ(pc_->signaling_state(), new_state);
544 state_ = new_state;
545 }
deadbeefab9b2d12015-10-14 11:33:11 -0700546
547 MediaStreamInterface* RemoteStream(const std::string& label) {
548 return remote_streams_->find(label);
549 }
550 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700551 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700553 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700555 void OnRemoveStream(
556 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700558 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 }
perkjdfb769d2016-02-09 03:09:43 -0800560 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700561 void OnDataChannel(
562 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 last_datachannel_ = data_channel;
564 }
565
perkjdfb769d2016-02-09 03:09:43 -0800566 void OnIceConnectionChange(
567 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800569 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 }
perkjdfb769d2016-02-09 03:09:43 -0800571 void OnIceGatheringChange(
572 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800574 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800575 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 }
perkjdfb769d2016-02-09 03:09:43 -0800577 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
579 pc_->ice_gathering_state());
580
581 std::string sdp;
582 EXPECT_TRUE(candidate->ToString(&sdp));
583 EXPECT_LT(0u, sdp.size());
584 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
585 candidate->sdp_mline_index(), sdp, NULL));
586 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800587 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700588 }
589
590 void OnIceCandidatesRemoved(
591 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800592 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700593 }
594
595 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800596 callback_triggered_ = true;
597 }
598
zhihuangc63b8942016-12-02 15:41:10 -0800599 void OnAddTrack(
600 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
601 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
602 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800603 EXPECT_TRUE(receiver != nullptr);
604 num_added_tracks_++;
605 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
608 // Returns the label of the last added stream.
609 // Empty string if no stream have been added.
610 std::string GetLastAddedStreamLabel() {
611 if (last_added_stream_.get())
612 return last_added_stream_->label();
613 return "";
614 }
615 std::string GetLastRemovedStreamLabel() {
616 if (last_removed_stream_.get())
617 return last_removed_stream_->label();
618 return "";
619 }
620
zhihuang9763d562016-08-05 11:14:50 -0700621 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700623 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700624 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700625 rtc::scoped_refptr<StreamCollection> remote_streams_;
626 bool renegotiation_needed_ = false;
627 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800628 bool callback_triggered_ = false;
629 int num_added_tracks_ = 0;
630 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
632 private:
zhihuang9763d562016-08-05 11:14:50 -0700633 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
634 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635};
636
637} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700638
nisse528b7932017-05-08 03:21:43 -0700639// The PeerConnectionMediaConfig tests below verify that configuration and
640// constraints are propagated into the PeerConnection's MediaConfig. These
641// settings are intended for MediaChannel constructors, but that is not
642// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700643class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
644 public:
zhihuang38ede132017-06-15 12:52:32 -0700645 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
646 CreatePeerConnectionFactoryForTest() {
647 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
648 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
649
650 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
651 cricket::WebRtcMediaEngineFactory::Create(
652 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700653 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700654
655 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
656 webrtc::CreateCallFactory();
657
658 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
659 webrtc::CreateRtcEventLogFactory();
660
661 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
662 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700663 FakeAudioCaptureModule::Create(), audio_encoder_factory,
664 audio_decoder_factory, nullptr, nullptr, nullptr,
665 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700666 std::move(event_log_factory));
667 }
668
669 PeerConnectionFactoryForTest(
670 rtc::Thread* network_thread,
671 rtc::Thread* worker_thread,
672 rtc::Thread* signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700673 rtc::scoped_refptr<FakeAudioCaptureModule> fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700674 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
675 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
676 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
677 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
678 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
679 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
680 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
681 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
682 : webrtc::PeerConnectionFactory(network_thread,
683 worker_thread,
684 signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700685 fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700686 audio_encoder_factory,
687 audio_decoder_factory,
688 video_encoder_factory,
689 video_decoder_factory,
690 audio_mixer,
691 std::move(media_engine),
692 std::move(call_factory),
693 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800694
zhihuang29ff8442016-07-27 11:07:25 -0700695 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700696 cricket::PortAllocator* port_allocator,
697 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700698 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700699 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700700 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700701 return transport_controller;
702 }
703
deadbeefd7850b22017-08-23 10:59:19 -0700704 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700705 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700706};
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708class PeerConnectionInterfaceTest : public testing::Test {
709 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700710 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700711 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800712#ifdef WEBRTC_ANDROID
713 webrtc::InitializeAndroidObjects();
714#endif
715 }
716
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700718 // Use fake audio capture module since we're only testing the interface
719 // level, and using a real one could make tests flaky when run in parallel.
720 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700722 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700723 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700724 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700725 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700726 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700727 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
730 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 }
733
deadbeef293e9262017-01-11 12:28:30 -0800734 // DTLS does not work in a loopback call, so is disabled for most of the
735 // tests in this file.
736 void CreatePeerConnectionWithoutDtls() {
737 FakeConstraints no_dtls_constraints;
738 no_dtls_constraints.AddMandatory(
739 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
740
741 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
742 &no_dtls_constraints);
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700746 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
747 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 }
749
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700750 void CreatePeerConnectionWithIceTransportsType(
751 PeerConnectionInterface::IceTransportsType type) {
752 PeerConnectionInterface::RTCConfiguration config;
753 config.type = type;
754 return CreatePeerConnection(config, nullptr);
755 }
756
757 void CreatePeerConnectionWithIceServer(const std::string& uri,
758 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700761 server.uri = uri;
762 server.password = password;
763 config.servers.push_back(server);
764 CreatePeerConnection(config, nullptr);
765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700767 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
768 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700769 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800770 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
771 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000772
deadbeef1dcb1642017-03-29 21:08:16 -0700773 // Create certificate generator unless DTLS constraint is explicitly set to
774 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200775 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000776 bool dtls;
777 if (FindConstraint(constraints,
778 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
779 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200780 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800781 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
782 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000783 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200784 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800785 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200786 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 ASSERT_TRUE(pc_.get() != NULL);
788 observer_.SetPeerConnectionInterface(pc_.get());
789 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
790 }
791
deadbeef0a6c4ca2015-10-06 11:38:28 -0700792 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800793 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700794 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700795 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700797
zhihuang9763d562016-08-05 11:14:50 -0700798 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700799 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
800 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800801 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700802 }
803
Steve Anton038834f2017-07-14 15:59:59 -0700804 void CreatePeerConnectionExpectFail(
805 PeerConnectionInterface::RTCConfiguration config) {
806 PeerConnectionInterface::IceServer server;
807 server.uri = kTurnIceServerUri;
808 server.password = kTurnPassword;
809 config.servers.push_back(server);
810 rtc::scoped_refptr<PeerConnectionInterface> pc =
811 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
812 EXPECT_EQ(nullptr, pc);
813 }
814
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700816 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
818 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
819 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800821 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822
deadbeef0a6c4ca2015-10-06 11:38:28 -0700823 CreatePeerConnectionExpectFail(kStunInvalidPort);
824 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
825 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700827 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800828 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
829 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800831 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800833 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800835 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
837
838 void ReleasePeerConnection() {
839 pc_ = NULL;
840 observer_.SetPeerConnectionInterface(NULL);
841 }
842
deadbeefab9b2d12015-10-14 11:33:11 -0700843 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700845 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700847 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800848 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
849 new cricket::FakeVideoCapturer()),
850 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700851 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 pc_factory_->CreateVideoTrack(label + "v0", video_source));
853 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000854 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
856 observer_.renegotiation_needed_ = false;
857 }
858
859 void AddVoiceStream(const std::string& label) {
860 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700861 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700863 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 pc_factory_->CreateAudioTrack(label + "a0", NULL));
865 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000866 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
868 observer_.renegotiation_needed_ = false;
869 }
870
871 void AddAudioVideoStream(const std::string& stream_label,
872 const std::string& audio_track_label,
873 const std::string& video_track_label) {
874 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700875 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700877 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 pc_factory_->CreateAudioTrack(
879 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
880 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700881 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700882 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800883 video_track_label, pc_factory_->CreateVideoSource(
884 std::unique_ptr<cricket::VideoCapturer>(
885 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000887 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
889 observer_.renegotiation_needed_ = false;
890 }
891
kwibergd1fe2812016-04-27 06:47:29 -0700892 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700893 bool offer,
894 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000895 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
896 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 MockCreateSessionDescriptionObserver>());
898 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700899 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700901 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return observer->result();
906 }
907
kwibergd1fe2812016-04-27 06:47:29 -0700908 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700909 MediaConstraintsInterface* constraints) {
910 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 }
912
kwibergd1fe2812016-04-27 06:47:29 -0700913 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700914 MediaConstraintsInterface* constraints) {
915 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 }
917
Steve Antondb45ca82017-09-11 18:27:34 -0700918 bool DoSetSessionDescription(
919 std::unique_ptr<SessionDescriptionInterface> desc,
920 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000921 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
922 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 MockSetSessionDescriptionObserver>());
924 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700925 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700927 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
zhihuang29ff8442016-07-27 11:07:25 -0700929 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
930 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
931 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 return observer->result();
933 }
934
Steve Antondb45ca82017-09-11 18:27:34 -0700935 bool DoSetLocalDescription(
936 std::unique_ptr<SessionDescriptionInterface> desc) {
937 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 }
939
Steve Antondb45ca82017-09-11 18:27:34 -0700940 bool DoSetRemoteDescription(
941 std::unique_ptr<SessionDescriptionInterface> desc) {
942 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 }
944
945 // Calls PeerConnection::GetStats and check the return value.
946 // It does not verify the values in the StatReports since a RTCP packet might
947 // be required.
948 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000949 rtc::scoped_refptr<MockStatsObserver> observer(
950 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000951 if (!pc_->GetStats(
952 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 return false;
954 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
955 return observer->called();
956 }
957
958 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800959 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 // Create a local stream with audio&video tracks.
961 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
962 CreateOfferReceiveAnswer();
963 }
964
965 // Verify that RTP Header extensions has been negotiated for audio and video.
966 void VerifyRemoteRtpHeaderExtensions() {
967 const cricket::MediaContentDescription* desc =
968 cricket::GetFirstAudioContentDescription(
969 pc_->remote_description()->description());
970 ASSERT_TRUE(desc != NULL);
971 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
972
973 desc = cricket::GetFirstVideoContentDescription(
974 pc_->remote_description()->description());
975 ASSERT_TRUE(desc != NULL);
976 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
977 }
978
979 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700980 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700981 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 std::string sdp;
983 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700984 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700986 sdp, nullptr));
987 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
989 }
990
deadbeefab9b2d12015-10-14 11:33:11 -0700991 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700992 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700993 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700994 sdp, nullptr));
995 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700996 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
997 }
998
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001001 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002
1003 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1004 // audio codec change, even if the parameter has nothing to do with
1005 // receiving. Not all parameters are serialized to SDP.
1006 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1007 // the SessionDescription, it is necessary to do that here to in order to
1008 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1009 // https://code.google.com/p/webrtc/issues/detail?id=1356
1010 std::string sdp;
1011 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001012 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001014 sdp, nullptr));
1015 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1017 }
1018
1019 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001020 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001021 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022
1023 std::string sdp;
1024 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001025 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001027 sdp, nullptr));
1028 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1030 }
1031
1032 void CreateOfferReceiveAnswer() {
1033 CreateOfferAsLocalDescription();
1034 std::string sdp;
1035 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1036 CreateAnswerAsRemoteDescription(sdp);
1037 }
1038
1039 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001040 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001041 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1043 // audio codec change, even if the parameter has nothing to do with
1044 // receiving. Not all parameters are serialized to SDP.
1045 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1046 // the SessionDescription, it is necessary to do that here to in order to
1047 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1048 // https://code.google.com/p/webrtc/issues/detail?id=1356
1049 std::string sdp;
1050 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001051 std::unique_ptr<SessionDescriptionInterface> new_offer(
1052 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1053 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054
Steve Antondb45ca82017-09-11 18:27:34 -07001055 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001057 // Wait for the ice_complete message, so that SDP will have candidates.
1058 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 }
1060
deadbeefab9b2d12015-10-14 11:33:11 -07001061 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001062 std::unique_ptr<SessionDescriptionInterface> answer(
1063 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1064 sdp, nullptr));
1065 ASSERT_TRUE(answer);
1066 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1068 }
1069
deadbeefab9b2d12015-10-14 11:33:11 -07001070 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001071 std::unique_ptr<SessionDescriptionInterface> pr_answer(
1072 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1073 sdp, nullptr));
1074 ASSERT_TRUE(pr_answer);
1075 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001077 std::unique_ptr<SessionDescriptionInterface> answer(
1078 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1079 sdp, nullptr));
1080 ASSERT_TRUE(answer);
1081 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1083 }
1084
1085 // Help function used for waiting until a the last signaled remote stream has
1086 // the same label as |stream_label|. In a few of the tests in this file we
1087 // answer with the same session description as we offer and thus we can
1088 // check if OnAddStream have been called with the same stream as we offer to
1089 // send.
1090 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1091 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1092 }
1093
1094 // Creates an offer and applies it as a local session description.
1095 // Creates an answer with the same SDP an the offer but removes all lines
1096 // that start with a:ssrc"
1097 void CreateOfferReceiveAnswerWithoutSsrc() {
1098 CreateOfferAsLocalDescription();
1099 std::string sdp;
1100 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1101 SetSsrcToZero(&sdp);
1102 CreateAnswerAsRemoteDescription(sdp);
1103 }
1104
deadbeefab9b2d12015-10-14 11:33:11 -07001105 // This function creates a MediaStream with label kStreams[0] and
1106 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1107 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001108 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001109 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001110 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001111 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1112 size_t number_of_video_tracks) {
1113 EXPECT_LE(number_of_audio_tracks, 2u);
1114 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001115
1116 reference_collection_ = StreamCollection::Create();
1117 std::string sdp_ms1 = std::string(kSdpStringInit);
1118
1119 std::string mediastream_label = kStreams[0];
1120
1121 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1122 webrtc::MediaStream::Create(mediastream_label));
1123 reference_collection_->AddStream(stream);
1124
1125 if (number_of_audio_tracks > 0) {
1126 sdp_ms1 += std::string(kSdpStringAudio);
1127 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1128 AddAudioTrack(kAudioTracks[0], stream);
1129 }
1130 if (number_of_audio_tracks > 1) {
1131 sdp_ms1 += kSdpStringMs1Audio1;
1132 AddAudioTrack(kAudioTracks[1], stream);
1133 }
1134
1135 if (number_of_video_tracks > 0) {
1136 sdp_ms1 += std::string(kSdpStringVideo);
1137 sdp_ms1 += std::string(kSdpStringMs1Video0);
1138 AddVideoTrack(kVideoTracks[0], stream);
1139 }
1140 if (number_of_video_tracks > 1) {
1141 sdp_ms1 += kSdpStringMs1Video1;
1142 AddVideoTrack(kVideoTracks[1], stream);
1143 }
1144
kwibergd1fe2812016-04-27 06:47:29 -07001145 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001146 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1147 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001148 }
1149
1150 void AddAudioTrack(const std::string& track_id,
1151 MediaStreamInterface* stream) {
1152 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1153 webrtc::AudioTrack::Create(track_id, nullptr));
1154 ASSERT_TRUE(stream->AddTrack(audio_track));
1155 }
1156
1157 void AddVideoTrack(const std::string& track_id,
1158 MediaStreamInterface* stream) {
1159 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001160 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001161 webrtc::FakeVideoTrackSource::Create(),
1162 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001163 ASSERT_TRUE(stream->AddTrack(video_track));
1164 }
1165
kwibergfd8be342016-05-14 19:44:11 -07001166 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001167 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001168 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001169 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001170 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1171 return offer;
1172 }
1173
kwibergfd8be342016-05-14 19:44:11 -07001174 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001176 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1179 return answer;
1180 }
1181
1182 const std::string& GetFirstAudioStreamCname(
1183 const SessionDescriptionInterface* desc) {
1184 const cricket::ContentInfo* audio_content =
1185 cricket::GetFirstAudioContent(desc->description());
1186 const cricket::AudioContentDescription* audio_desc =
1187 static_cast<const cricket::AudioContentDescription*>(
1188 audio_content->description);
1189 return audio_desc->streams()[0].cname;
1190 }
1191
zhihuang1c378ed2017-08-17 14:10:50 -07001192 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1193 const RTCOfferAnswerOptions& offer_answer_options) {
1194 RTC_DCHECK(pc_);
1195 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1196 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1197 pc_->CreateOffer(observer, offer_answer_options);
1198 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1199 return observer->MoveDescription();
1200 }
1201
1202 void CreateOfferWithOptionsAsRemoteDescription(
1203 std::unique_ptr<SessionDescriptionInterface>* desc,
1204 const RTCOfferAnswerOptions& offer_answer_options) {
1205 *desc = CreateOfferWithOptions(offer_answer_options);
1206 ASSERT_TRUE(desc != nullptr);
1207 std::string sdp;
1208 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001209 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001210 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001211 sdp, nullptr));
1212 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001213 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1214 }
1215
1216 void CreateOfferWithOptionsAsLocalDescription(
1217 std::unique_ptr<SessionDescriptionInterface>* desc,
1218 const RTCOfferAnswerOptions& offer_answer_options) {
1219 *desc = CreateOfferWithOptions(offer_answer_options);
1220 ASSERT_TRUE(desc != nullptr);
1221 std::string sdp;
1222 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001223 std::unique_ptr<SessionDescriptionInterface> new_offer(
1224 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1225 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001226
Steve Antondb45ca82017-09-11 18:27:34 -07001227 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001228 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1229 }
1230
1231 bool HasCNCodecs(const cricket::ContentInfo* content) {
1232 const cricket::ContentDescription* description = content->description;
1233 RTC_DCHECK(description);
1234 const cricket::AudioContentDescription* audio_content_desc =
1235 static_cast<const cricket::AudioContentDescription*>(description);
1236 RTC_DCHECK(audio_content_desc);
1237 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1238 if (audio_content_desc->codecs()[i].name == "CN")
1239 return true;
1240 }
1241 return false;
1242 }
1243
deadbeef9a6f4d42017-05-15 19:43:33 -07001244 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1245 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001246 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001247 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001248 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001249 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1250 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1251 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001253 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254};
1255
zhihuang29ff8442016-07-27 11:07:25 -07001256// Test that no callbacks on the PeerConnectionObserver are called after the
1257// PeerConnection is closed.
1258TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001259 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001260 pc_factory_for_test_->CreatePeerConnection(
1261 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1262 nullptr, &observer_));
1263 observer_.SetPeerConnectionInterface(pc.get());
1264 pc->Close();
1265
1266 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001267 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001268 std::vector<cricket::Candidate> candidates;
1269 pc_factory_for_test_->transport_controller->SignalGatheringState(
1270 cricket::IceGatheringState{});
1271 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1272 "", candidates);
1273 pc_factory_for_test_->transport_controller->SignalConnectionState(
1274 cricket::IceConnectionState{});
1275 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1276 candidates);
1277 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001278 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001279}
1280
zhihuang8f65cdf2016-05-06 18:40:30 -07001281// Generate different CNAMEs when PeerConnections are created.
1282// The CNAMEs are expected to be generated randomly. It is possible
1283// that the test fails, though the possibility is very low.
1284TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001285 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001286 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001288 CreateOfferWithOneAudioStream();
1289 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1290 GetFirstAudioStreamCname(offer2.get()));
1291}
1292
1293TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001295 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001296 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001297 CreateAnswerWithOneAudioStream();
1298 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1299 GetFirstAudioStreamCname(answer2.get()));
1300}
1301
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302TEST_F(PeerConnectionInterfaceTest,
1303 CreatePeerConnectionWithDifferentConfigurations) {
1304 CreatePeerConnectionWithDifferentConfigurations();
1305}
1306
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307TEST_F(PeerConnectionInterfaceTest,
1308 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1309 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1310 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1311 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1312 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1313 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1314 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1315 port_allocator_->candidate_filter());
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1317 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1318}
1319
1320// Test that when a PeerConnection is created with a nonzero candidate pool
1321// size, the pooled PortAllocatorSession is created with all the attributes
1322// in the RTCConfiguration.
1323TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1324 PeerConnectionInterface::RTCConfiguration config;
1325 PeerConnectionInterface::IceServer server;
1326 server.uri = kStunAddressOnly;
1327 config.servers.push_back(server);
1328 config.type = PeerConnectionInterface::kRelay;
1329 config.disable_ipv6 = true;
1330 config.tcp_candidate_policy =
1331 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001332 config.candidate_network_policy =
1333 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 config.ice_candidate_pool_size = 1;
1335 CreatePeerConnection(config, nullptr);
1336
1337 const cricket::FakePortAllocatorSession* session =
1338 static_cast<const cricket::FakePortAllocatorSession*>(
1339 port_allocator_->GetPooledSession());
1340 ASSERT_NE(nullptr, session);
1341 EXPECT_EQ(1UL, session->stun_servers().size());
1342 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1343 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001344 EXPECT_LT(0U,
1345 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346}
1347
deadbeefd21eab32017-07-26 16:50:11 -07001348// Test that network-related RTCConfiguration members are applied to the
1349// PortAllocator when CreatePeerConnection is called. Specifically:
1350// - disable_ipv6_on_wifi
1351// - max_ipv6_networks
1352// - tcp_candidate_policy
1353// - candidate_network_policy
1354// - prune_turn_ports
1355//
1356// Note that the candidate filter (RTCConfiguration::type) is already tested
1357// above.
1358TEST_F(PeerConnectionInterfaceTest,
1359 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1360 // Create fake port allocator.
1361 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1362 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1363 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1364
1365 // Create RTCConfiguration with some network-related fields relevant to
1366 // PortAllocator populated.
1367 PeerConnectionInterface::RTCConfiguration config;
1368 config.disable_ipv6_on_wifi = true;
1369 config.max_ipv6_networks = 10;
1370 config.tcp_candidate_policy =
1371 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1372 config.candidate_network_policy =
1373 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1374 config.prune_turn_ports = true;
1375
1376 // Create the PC factory and PC with the above config.
1377 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1378 webrtc::CreatePeerConnectionFactory(
1379 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001380 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1381 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001382 rtc::scoped_refptr<PeerConnectionInterface> pc(
1383 pc_factory->CreatePeerConnection(
1384 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1385
1386 // Now validate that the config fields set above were applied to the
1387 // PortAllocator, as flags or otherwise.
1388 EXPECT_FALSE(raw_port_allocator->flags() &
1389 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1390 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1391 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1392 EXPECT_TRUE(raw_port_allocator->flags() &
1393 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1394 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1395}
1396
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001397// Test that the PeerConnection initializes the port allocator passed into it,
1398// and on the correct thread.
1399TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001400 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001401 std::unique_ptr<rtc::Thread> network_thread(
1402 rtc::Thread::CreateWithSocketServer());
1403 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001404 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1405 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001406 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001407 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001408 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001409 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001410 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1411 PeerConnectionInterface::RTCConfiguration config;
1412 rtc::scoped_refptr<PeerConnectionInterface> pc(
1413 pc_factory->CreatePeerConnection(
1414 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1415 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1416 // so all we have to do here is check that it's initialized.
1417 EXPECT_TRUE(raw_port_allocator->initialized());
1418}
1419
deadbeef46c73892016-11-16 19:42:04 -08001420// Check that GetConfiguration returns the configuration the PeerConnection was
1421// constructed with, before SetConfiguration is called.
1422TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1423 PeerConnectionInterface::RTCConfiguration config;
1424 config.type = PeerConnectionInterface::kRelay;
1425 CreatePeerConnection(config, nullptr);
1426
1427 PeerConnectionInterface::RTCConfiguration returned_config =
1428 pc_->GetConfiguration();
1429 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1430}
1431
1432// Check that GetConfiguration returns the last configuration passed into
1433// SetConfiguration.
1434TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1435 CreatePeerConnection();
1436
1437 PeerConnectionInterface::RTCConfiguration config;
1438 config.type = PeerConnectionInterface::kRelay;
1439 EXPECT_TRUE(pc_->SetConfiguration(config));
1440
1441 PeerConnectionInterface::RTCConfiguration returned_config =
1442 pc_->GetConfiguration();
1443 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1444}
1445
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001447 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001448 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 AddVoiceStream(kStreamLabel2);
1450 ASSERT_EQ(2u, pc_->local_streams()->count());
1451
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001452 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001453 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001455 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1456 pc_factory_->CreateAudioTrack(kStreamLabel3,
1457 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001459 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001460 EXPECT_EQ(3u, pc_->local_streams()->count());
1461
1462 // Remove the third stream.
1463 pc_->RemoveStream(pc_->local_streams()->at(2));
1464 EXPECT_EQ(2u, pc_->local_streams()->count());
1465
1466 // Remove the second stream.
1467 pc_->RemoveStream(pc_->local_streams()->at(1));
1468 EXPECT_EQ(1u, pc_->local_streams()->count());
1469
1470 // Remove the first stream.
1471 pc_->RemoveStream(pc_->local_streams()->at(0));
1472 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473}
1474
deadbeefab9b2d12015-10-14 11:33:11 -07001475// Test that the created offer includes streams we added.
1476TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001477 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001478 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001479 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001480 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001481
1482 const cricket::ContentInfo* audio_content =
1483 cricket::GetFirstAudioContent(offer->description());
1484 const cricket::AudioContentDescription* audio_desc =
1485 static_cast<const cricket::AudioContentDescription*>(
1486 audio_content->description);
1487 EXPECT_TRUE(
1488 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1489
1490 const cricket::ContentInfo* video_content =
1491 cricket::GetFirstVideoContent(offer->description());
1492 const cricket::VideoContentDescription* video_desc =
1493 static_cast<const cricket::VideoContentDescription*>(
1494 video_content->description);
1495 EXPECT_TRUE(
1496 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1497
1498 // Add another stream and ensure the offer includes both the old and new
1499 // streams.
1500 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001501 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001502
1503 audio_content = cricket::GetFirstAudioContent(offer->description());
1504 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1505 audio_content->description);
1506 EXPECT_TRUE(
1507 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1508 EXPECT_TRUE(
1509 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1510
1511 video_content = cricket::GetFirstVideoContent(offer->description());
1512 video_desc = static_cast<const cricket::VideoContentDescription*>(
1513 video_content->description);
1514 EXPECT_TRUE(
1515 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1516 EXPECT_TRUE(
1517 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1518}
1519
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001521 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001522 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 ASSERT_EQ(1u, pc_->local_streams()->count());
1524 pc_->RemoveStream(pc_->local_streams()->at(0));
1525 EXPECT_EQ(0u, pc_->local_streams()->count());
1526}
1527
deadbeefe1f9d832016-01-14 15:35:42 -08001528// Test for AddTrack and RemoveTrack methods.
1529// Tests that the created offer includes tracks we added,
1530// and that the RtpSenders are created correctly.
1531// Also tests that RemoveTrack removes the tracks from subsequent offers.
1532TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001533 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001534 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001535 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001536 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1537 std::vector<MediaStreamInterface*> stream_list;
1538 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001539 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001540 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001541 rtc::scoped_refptr<VideoTrackInterface> video_track(
1542 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001543 "video_track", pc_factory_->CreateVideoSource(
1544 std::unique_ptr<cricket::VideoCapturer>(
1545 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001546 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1547 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001548 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1549 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001550 EXPECT_EQ("audio_track", audio_sender->id());
1551 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001552 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1553 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001554 EXPECT_EQ("video_track", video_sender->id());
1555 EXPECT_EQ(video_track, video_sender->track());
1556
1557 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001558 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001559 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001560
1561 const cricket::ContentInfo* audio_content =
1562 cricket::GetFirstAudioContent(offer->description());
1563 const cricket::AudioContentDescription* audio_desc =
1564 static_cast<const cricket::AudioContentDescription*>(
1565 audio_content->description);
1566 EXPECT_TRUE(
1567 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1568
1569 const cricket::ContentInfo* video_content =
1570 cricket::GetFirstVideoContent(offer->description());
1571 const cricket::VideoContentDescription* video_desc =
1572 static_cast<const cricket::VideoContentDescription*>(
1573 video_content->description);
1574 EXPECT_TRUE(
1575 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1576
Steve Antondb45ca82017-09-11 18:27:34 -07001577 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001578
1579 // Now try removing the tracks.
1580 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1581 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1582
1583 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001584 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001585
1586 audio_content = cricket::GetFirstAudioContent(offer->description());
1587 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1588 audio_content->description);
1589 EXPECT_FALSE(
1590 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1591
1592 video_content = cricket::GetFirstVideoContent(offer->description());
1593 video_desc = static_cast<const cricket::VideoContentDescription*>(
1594 video_content->description);
1595 EXPECT_FALSE(
1596 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1597
Steve Antondb45ca82017-09-11 18:27:34 -07001598 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001599
1600 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1601 // should return false.
1602 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1603 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1604}
1605
1606// Test creating senders without a stream specified,
1607// expecting a random stream ID to be generated.
1608TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001609 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001610 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001611 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001612 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001613 rtc::scoped_refptr<VideoTrackInterface> video_track(
1614 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001615 "video_track", pc_factory_->CreateVideoSource(
1616 std::unique_ptr<cricket::VideoCapturer>(
1617 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001618 auto audio_sender =
1619 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1620 auto video_sender =
1621 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1622 EXPECT_EQ("audio_track", audio_sender->id());
1623 EXPECT_EQ(audio_track, audio_sender->track());
1624 EXPECT_EQ("video_track", video_sender->id());
1625 EXPECT_EQ(video_track, video_sender->track());
1626 // If the ID is truly a random GUID, it should be infinitely unlikely they
1627 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001628 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001629}
1630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1632 InitiateCall();
1633 WaitAndVerifyOnAddStream(kStreamLabel1);
1634 VerifyRemoteRtpHeaderExtensions();
1635}
1636
1637TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001638 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001639 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 CreateOfferAsLocalDescription();
1641 std::string offer;
1642 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1643 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1644 WaitAndVerifyOnAddStream(kStreamLabel1);
1645}
1646
1647TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001648 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001649 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650
1651 CreateOfferAsRemoteDescription();
1652 CreateAnswerAsLocalDescription();
1653
1654 WaitAndVerifyOnAddStream(kStreamLabel1);
1655}
1656
1657TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001658 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001659 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660
1661 CreateOfferAsRemoteDescription();
1662 CreatePrAnswerAsLocalDescription();
1663 CreateAnswerAsLocalDescription();
1664
1665 WaitAndVerifyOnAddStream(kStreamLabel1);
1666}
1667
1668TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1669 InitiateCall();
1670 ASSERT_EQ(1u, pc_->remote_streams()->count());
1671 pc_->RemoveStream(pc_->local_streams()->at(0));
1672 CreateOfferReceiveAnswer();
1673 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001674 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 CreateOfferReceiveAnswer();
1676}
1677
1678// Tests that after negotiating an audio only call, the respondent can perform a
1679// renegotiation that removes the audio stream.
1680TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001681 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 AddVoiceStream(kStreamLabel1);
1683 CreateOfferAsRemoteDescription();
1684 CreateAnswerAsLocalDescription();
1685
1686 ASSERT_EQ(1u, pc_->remote_streams()->count());
1687 pc_->RemoveStream(pc_->local_streams()->at(0));
1688 CreateOfferReceiveAnswer();
1689 EXPECT_EQ(0u, pc_->remote_streams()->count());
1690}
1691
1692// Test that candidates are generated and that we can parse our own candidates.
1693TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001694 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695
1696 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1697 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001698 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001699 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001700 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001701 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702
1703 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001704 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001705 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001706 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707
1708 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1709 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1710
1711 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1712}
1713
deadbeefab9b2d12015-10-14 11:33:11 -07001714// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715// not unique.
1716TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001719 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001720 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001721 EXPECT_TRUE(offer);
1722 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723
1724 // Create a local stream with audio&video tracks having same label.
1725 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1726
1727 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001728 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729
1730 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001731 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001732 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733}
1734
1735// Test that we will get different SSRCs for each tracks in the offer and answer
1736// we created.
1737TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001738 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 // Create a local stream with audio&video tracks having different labels.
1740 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1741
1742 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001743 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001744 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 int audio_ssrc = 0;
1746 int video_ssrc = 0;
1747 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1748 &audio_ssrc));
1749 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1750 &video_ssrc));
1751 EXPECT_NE(audio_ssrc, video_ssrc);
1752
1753 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001754 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001755 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001756 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 audio_ssrc = 0;
1758 video_ssrc = 0;
1759 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1760 &audio_ssrc));
1761 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1762 &video_ssrc));
1763 EXPECT_NE(audio_ssrc, video_ssrc);
1764}
1765
deadbeefeb459812015-12-15 19:24:43 -08001766// Test that it's possible to call AddTrack on a MediaStream after adding
1767// the stream to a PeerConnection.
1768// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1769TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001770 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001771 // Create audio stream and add to PeerConnection.
1772 AddVoiceStream(kStreamLabel1);
1773 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1774
1775 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001776 rtc::scoped_refptr<VideoTrackInterface> video_track(
1777 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001778 "video_label", pc_factory_->CreateVideoSource(
1779 std::unique_ptr<cricket::VideoCapturer>(
1780 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001781 stream->AddTrack(video_track.get());
1782
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001784 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001785
1786 const cricket::MediaContentDescription* video_desc =
1787 cricket::GetFirstVideoContentDescription(offer->description());
1788 EXPECT_TRUE(video_desc != nullptr);
1789}
1790
1791// Test that it's possible to call RemoveTrack on a MediaStream after adding
1792// the stream to a PeerConnection.
1793// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1794TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001795 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001796 // Create audio/video stream and add to PeerConnection.
1797 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1798 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1799
1800 // Remove the video track.
1801 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1802
kwibergd1fe2812016-04-27 06:47:29 -07001803 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001804 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001805
1806 const cricket::MediaContentDescription* video_desc =
1807 cricket::GetFirstVideoContentDescription(offer->description());
1808 EXPECT_TRUE(video_desc == nullptr);
1809}
1810
deadbeef1dcb1642017-03-29 21:08:16 -07001811// Verify that CreateDtmfSender only succeeds if called with a valid local
1812// track. Other aspects of DtmfSenders are tested in
1813// peerconnection_integrationtest.cc.
1814TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1815 CreatePeerConnection();
1816 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1817 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1818 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1819 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1820 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1821}
1822
deadbeefbd7d8f72015-12-18 16:58:44 -08001823// Test creating a sender with a stream ID, and ensure the ID is populated
1824// in the offer.
1825TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001826 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001827 pc_->CreateSender("video", kStreamLabel1);
1828
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001830 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001831
1832 const cricket::MediaContentDescription* video_desc =
1833 cricket::GetFirstVideoContentDescription(offer->description());
1834 ASSERT_TRUE(video_desc != nullptr);
1835 ASSERT_EQ(1u, video_desc->streams().size());
1836 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1837}
1838
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839// Test that we can specify a certain track that we want statistics about.
1840TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1841 InitiateCall();
1842 ASSERT_LT(0u, pc_->remote_streams()->count());
1843 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1846 EXPECT_TRUE(DoGetStats(remote_audio));
1847
1848 // Remove the stream. Since we are sending to our selves the local
1849 // and the remote stream is the same.
1850 pc_->RemoveStream(pc_->local_streams()->at(0));
1851 // Do a re-negotiation.
1852 CreateOfferReceiveAnswer();
1853
1854 ASSERT_EQ(0u, pc_->remote_streams()->count());
1855
1856 // Test that we still can get statistics for the old track. Even if it is not
1857 // sent any longer.
1858 EXPECT_TRUE(DoGetStats(remote_audio));
1859}
1860
1861// Test that we can get stats on a video track.
1862TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1863 InitiateCall();
1864 ASSERT_LT(0u, pc_->remote_streams()->count());
1865 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001866 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1868 EXPECT_TRUE(DoGetStats(remote_video));
1869}
1870
1871// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001872TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001874 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 pc_factory_->CreateAudioTrack("unknown track", NULL));
1876 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1877}
1878
1879// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 FakeConstraints constraints;
1882 constraints.SetAllowRtpDataChannels();
1883 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 pc_->CreateDataChannel("test2", NULL);
1888 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001889 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001891 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 new MockDataChannelObserver(data2));
1893
1894 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1895 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1896 std::string data_to_send1 = "testing testing";
1897 std::string data_to_send2 = "testing something else";
1898 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1899
1900 CreateOfferReceiveAnswer();
1901 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1902 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1903
1904 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1905 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1906 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1907 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1908
1909 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1910 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1911
1912 data1->Close();
1913 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1914 CreateOfferReceiveAnswer();
1915 EXPECT_FALSE(observer1->IsOpen());
1916 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1917 EXPECT_TRUE(observer2->IsOpen());
1918
1919 data_to_send2 = "testing something else again";
1920 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1921
1922 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1923}
1924
1925// This test verifies that sendnig binary data over RTP data channels should
1926// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 FakeConstraints constraints;
1929 constraints.SetAllowRtpDataChannels();
1930 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001931 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001933 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 pc_->CreateDataChannel("test2", NULL);
1935 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001936 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001938 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 new MockDataChannelObserver(data2));
1940
1941 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1942 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1943
1944 CreateOfferReceiveAnswer();
1945 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1946 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1947
1948 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1949 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1950
jbaucheec21bd2016-03-20 06:15:43 -07001951 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1953}
1954
1955// This test setup a RTP data channels in loop back and test that a channel is
1956// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 FakeConstraints constraints;
1959 constraints.SetAllowRtpDataChannels();
1960 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001961 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001963 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 new MockDataChannelObserver(data1));
1965
1966 CreateOfferReceiveAnswerWithoutSsrc();
1967
1968 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1969
1970 data1->Close();
1971 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1972 CreateOfferReceiveAnswerWithoutSsrc();
1973 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1974 EXPECT_FALSE(observer1->IsOpen());
1975}
1976
1977// This test that if a data channel is added in an answer a receive only channel
1978// channel is created.
1979TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1980 FakeConstraints constraints;
1981 constraints.SetAllowRtpDataChannels();
1982 CreatePeerConnection(&constraints);
1983
1984 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001985 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 pc_->CreateDataChannel(offer_label, NULL);
1987
1988 CreateOfferAsLocalDescription();
1989
1990 // Replace the data channel label in the offer and apply it as an answer.
1991 std::string receive_label = "answer_channel";
1992 std::string sdp;
1993 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001994 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 receive_label.c_str(), receive_label.length(),
1996 &sdp);
1997 CreateAnswerAsRemoteDescription(sdp);
1998
1999 // Verify that a new incoming data channel has been created and that
2000 // it is open but can't we written to.
2001 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2002 DataChannelInterface* received_channel = observer_.last_datachannel_;
2003 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2004 EXPECT_EQ(receive_label, received_channel->label());
2005 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2006
2007 // Verify that the channel we initially offered has been rejected.
2008 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2009
2010 // Do another offer / answer exchange and verify that the data channel is
2011 // opened.
2012 CreateOfferReceiveAnswer();
2013 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2014 kTimeout);
2015}
2016
2017// This test that no data channel is returned if a reliable channel is
2018// requested.
2019// TODO(perkj): Remove this test once reliable channels are implemented.
2020TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2021 FakeConstraints constraints;
2022 constraints.SetAllowRtpDataChannels();
2023 CreatePeerConnection(&constraints);
2024
2025 std::string label = "test";
2026 webrtc::DataChannelInit config;
2027 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002028 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 pc_->CreateDataChannel(label, &config);
2030 EXPECT_TRUE(channel == NULL);
2031}
2032
deadbeefab9b2d12015-10-14 11:33:11 -07002033// Verifies that duplicated label is not allowed for RTP data channel.
2034TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2035 FakeConstraints constraints;
2036 constraints.SetAllowRtpDataChannels();
2037 CreatePeerConnection(&constraints);
2038
2039 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002040 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002041 pc_->CreateDataChannel(label, nullptr);
2042 EXPECT_NE(channel, nullptr);
2043
zhihuang9763d562016-08-05 11:14:50 -07002044 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002045 pc_->CreateDataChannel(label, nullptr);
2046 EXPECT_EQ(dup_channel, nullptr);
2047}
2048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049// This tests that a SCTP data channel is returned using different
2050// DataChannelInit configurations.
2051TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2052 FakeConstraints constraints;
2053 constraints.SetAllowDtlsSctpDataChannels();
2054 CreatePeerConnection(&constraints);
2055
2056 webrtc::DataChannelInit config;
2057
zhihuang9763d562016-08-05 11:14:50 -07002058 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 pc_->CreateDataChannel("1", &config);
2060 EXPECT_TRUE(channel != NULL);
2061 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002062 EXPECT_TRUE(observer_.renegotiation_needed_);
2063 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064
2065 config.ordered = false;
2066 channel = pc_->CreateDataChannel("2", &config);
2067 EXPECT_TRUE(channel != NULL);
2068 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002069 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070
2071 config.ordered = true;
2072 config.maxRetransmits = 0;
2073 channel = pc_->CreateDataChannel("3", &config);
2074 EXPECT_TRUE(channel != NULL);
2075 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002076 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077
2078 config.maxRetransmits = -1;
2079 config.maxRetransmitTime = 0;
2080 channel = pc_->CreateDataChannel("4", &config);
2081 EXPECT_TRUE(channel != NULL);
2082 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002083 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084}
2085
2086// This tests that no data channel is returned if both maxRetransmits and
2087// maxRetransmitTime are set for SCTP data channels.
2088TEST_F(PeerConnectionInterfaceTest,
2089 CreateSctpDataChannelShouldFailForInvalidConfig) {
2090 FakeConstraints constraints;
2091 constraints.SetAllowDtlsSctpDataChannels();
2092 CreatePeerConnection(&constraints);
2093
2094 std::string label = "test";
2095 webrtc::DataChannelInit config;
2096 config.maxRetransmits = 0;
2097 config.maxRetransmitTime = 0;
2098
zhihuang9763d562016-08-05 11:14:50 -07002099 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 pc_->CreateDataChannel(label, &config);
2101 EXPECT_TRUE(channel == NULL);
2102}
2103
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104// The test verifies that creating a SCTP data channel with an id already in use
2105// or out of range should fail.
2106TEST_F(PeerConnectionInterfaceTest,
2107 CreateSctpDataChannelWithInvalidIdShouldFail) {
2108 FakeConstraints constraints;
2109 constraints.SetAllowDtlsSctpDataChannels();
2110 CreatePeerConnection(&constraints);
2111
2112 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002113 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002115 config.id = 1;
2116 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 EXPECT_TRUE(channel != NULL);
2118 EXPECT_EQ(1, channel->id());
2119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 channel = pc_->CreateDataChannel("x", &config);
2121 EXPECT_TRUE(channel == NULL);
2122
2123 config.id = cricket::kMaxSctpSid;
2124 channel = pc_->CreateDataChannel("max", &config);
2125 EXPECT_TRUE(channel != NULL);
2126 EXPECT_EQ(config.id, channel->id());
2127
2128 config.id = cricket::kMaxSctpSid + 1;
2129 channel = pc_->CreateDataChannel("x", &config);
2130 EXPECT_TRUE(channel == NULL);
2131}
2132
deadbeefab9b2d12015-10-14 11:33:11 -07002133// Verifies that duplicated label is allowed for SCTP data channel.
2134TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2135 FakeConstraints constraints;
2136 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2137 true);
2138 CreatePeerConnection(&constraints);
2139
2140 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002141 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002142 pc_->CreateDataChannel(label, nullptr);
2143 EXPECT_NE(channel, nullptr);
2144
zhihuang9763d562016-08-05 11:14:50 -07002145 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002146 pc_->CreateDataChannel(label, nullptr);
2147 EXPECT_NE(dup_channel, nullptr);
2148}
2149
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002150// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2151// DataChannel.
2152TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2153 FakeConstraints constraints;
2154 constraints.SetAllowRtpDataChannels();
2155 CreatePeerConnection(&constraints);
2156
zhihuang9763d562016-08-05 11:14:50 -07002157 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002158 pc_->CreateDataChannel("test1", NULL);
2159 EXPECT_TRUE(observer_.renegotiation_needed_);
2160 observer_.renegotiation_needed_ = false;
2161
zhihuang9763d562016-08-05 11:14:50 -07002162 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002163 pc_->CreateDataChannel("test2", NULL);
2164 EXPECT_TRUE(observer_.renegotiation_needed_);
2165}
2166
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 FakeConstraints constraints;
2170 constraints.SetAllowRtpDataChannels();
2171 CreatePeerConnection(&constraints);
2172
zhihuang9763d562016-08-05 11:14:50 -07002173 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002175 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 pc_->CreateDataChannel("test2", NULL);
2177 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002178 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 new MockDataChannelObserver(data2));
2182
2183 CreateOfferReceiveAnswer();
2184 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2185 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2186
2187 ReleasePeerConnection();
2188 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2189 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2190}
2191
2192// This test that data channels can be rejected in an answer.
2193TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2194 FakeConstraints constraints;
2195 constraints.SetAllowRtpDataChannels();
2196 CreatePeerConnection(&constraints);
2197
zhihuang9763d562016-08-05 11:14:50 -07002198 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 pc_->CreateDataChannel("offer_channel", NULL));
2200
2201 CreateOfferAsLocalDescription();
2202
2203 // Create an answer where the m-line for data channels are rejected.
2204 std::string sdp;
2205 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002206 std::unique_ptr<SessionDescriptionInterface> answer(
2207 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2208 sdp, nullptr));
2209 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 cricket::ContentInfo* data_info =
2211 answer->description()->GetContentByName("data");
2212 data_info->rejected = true;
2213
Steve Antondb45ca82017-09-11 18:27:34 -07002214 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2216}
2217
2218// Test that we can create a session description from an SDP string from
2219// FireFox, use it as a remote session description, generate an answer and use
2220// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002221TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 FakeConstraints constraints;
2223 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2224 true);
2225 CreatePeerConnection(&constraints);
2226 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002227 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002229 webrtc::kFireFoxSdpOffer, nullptr));
2230 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 CreateAnswerAsLocalDescription();
2232 ASSERT_TRUE(pc_->local_description() != NULL);
2233 ASSERT_TRUE(pc_->remote_description() != NULL);
2234
2235 const cricket::ContentInfo* content =
2236 cricket::GetFirstAudioContent(pc_->local_description()->description());
2237 ASSERT_TRUE(content != NULL);
2238 EXPECT_FALSE(content->rejected);
2239
2240 content =
2241 cricket::GetFirstVideoContent(pc_->local_description()->description());
2242 ASSERT_TRUE(content != NULL);
2243 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002244#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 content =
2246 cricket::GetFirstDataContent(pc_->local_description()->description());
2247 ASSERT_TRUE(content != NULL);
2248 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002249#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250}
2251
deadbeef8662f942017-01-20 21:20:51 -08002252// Test that an offer can be received which offers DTLS with SDES fallback.
2253// Regression test for issue:
2254// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2255TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2256 FakeConstraints constraints;
2257 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2258 true);
2259 CreatePeerConnection(&constraints);
2260 // Wait for fake certificate to be generated. Previously, this is what caused
2261 // the "a=crypto" lines to be rejected.
2262 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2263 ASSERT_NE(nullptr, fake_certificate_generator_);
2264 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2265 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002266 std::unique_ptr<SessionDescriptionInterface> desc(
2267 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2268 kDtlsSdesFallbackSdp, nullptr));
2269 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002270 CreateAnswerAsLocalDescription();
2271}
2272
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273// Test that we can create an audio only offer and receive an answer with a
2274// limited set of audio codecs and receive an updated offer with more audio
2275// codecs, where the added codecs are not supported.
2276TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002277 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 AddVoiceStream("audio_label");
2279 CreateOfferAsLocalDescription();
2280
Steve Antondb45ca82017-09-11 18:27:34 -07002281 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002283 webrtc::kAudioSdp, nullptr));
2284 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285
Steve Antondb45ca82017-09-11 18:27:34 -07002286 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002288 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002289 nullptr));
2290 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291 CreateAnswerAsLocalDescription();
2292}
2293
deadbeefc80741f2015-10-22 13:14:45 -07002294// Test that if we're receiving (but not sending) a track, subsequent offers
2295// will have m-lines with a=recvonly.
2296TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2297 FakeConstraints constraints;
2298 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2299 true);
2300 CreatePeerConnection(&constraints);
2301 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2302 CreateAnswerAsLocalDescription();
2303
2304 // At this point we should be receiving stream 1, but not sending anything.
2305 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002306 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002307 DoCreateOffer(&offer, nullptr);
2308
2309 const cricket::ContentInfo* video_content =
2310 cricket::GetFirstVideoContent(offer->description());
2311 const cricket::VideoContentDescription* video_desc =
2312 static_cast<const cricket::VideoContentDescription*>(
2313 video_content->description);
2314 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2315
2316 const cricket::ContentInfo* audio_content =
2317 cricket::GetFirstAudioContent(offer->description());
2318 const cricket::AudioContentDescription* audio_desc =
2319 static_cast<const cricket::AudioContentDescription*>(
2320 audio_content->description);
2321 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2322}
2323
2324// Test that if we're receiving (but not sending) a track, and the
2325// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2326// false, the generated m-lines will be a=inactive.
2327TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2328 FakeConstraints constraints;
2329 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2330 true);
2331 CreatePeerConnection(&constraints);
2332 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2333 CreateAnswerAsLocalDescription();
2334
2335 // At this point we should be receiving stream 1, but not sending anything.
2336 // A new offer would be recvonly, but we'll set the "no receive" constraints
2337 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002338 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002339 FakeConstraints offer_constraints;
2340 offer_constraints.AddMandatory(
2341 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2342 offer_constraints.AddMandatory(
2343 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2344 DoCreateOffer(&offer, &offer_constraints);
2345
2346 const cricket::ContentInfo* video_content =
2347 cricket::GetFirstVideoContent(offer->description());
2348 const cricket::VideoContentDescription* video_desc =
2349 static_cast<const cricket::VideoContentDescription*>(
2350 video_content->description);
2351 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2352
2353 const cricket::ContentInfo* audio_content =
2354 cricket::GetFirstAudioContent(offer->description());
2355 const cricket::AudioContentDescription* audio_desc =
2356 static_cast<const cricket::AudioContentDescription*>(
2357 audio_content->description);
2358 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2359}
2360
deadbeef653b8e02015-11-11 12:55:10 -08002361// Test that we can use SetConfiguration to change the ICE servers of the
2362// PortAllocator.
2363TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2364 CreatePeerConnection();
2365
2366 PeerConnectionInterface::RTCConfiguration config;
2367 PeerConnectionInterface::IceServer server;
2368 server.uri = "stun:test_hostname";
2369 config.servers.push_back(server);
2370 EXPECT_TRUE(pc_->SetConfiguration(config));
2371
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002372 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2373 EXPECT_EQ("test_hostname",
2374 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002375}
2376
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002377TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2378 CreatePeerConnection();
2379 PeerConnectionInterface::RTCConfiguration config;
2380 config.type = PeerConnectionInterface::kRelay;
2381 EXPECT_TRUE(pc_->SetConfiguration(config));
2382 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2383}
2384
deadbeef293e9262017-01-11 12:28:30 -08002385TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2386 PeerConnectionInterface::RTCConfiguration config;
2387 config.prune_turn_ports = false;
2388 CreatePeerConnection(config, nullptr);
2389 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2390
2391 config.prune_turn_ports = true;
2392 EXPECT_TRUE(pc_->SetConfiguration(config));
2393 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2394}
2395
skvladd1f5fda2017-02-03 16:54:05 -08002396// Test that the ice check interval can be changed. This does not verify that
2397// the setting makes it all the way to P2PTransportChannel, as that would
2398// require a very complex set of mocks.
2399TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2400 PeerConnectionInterface::RTCConfiguration config;
2401 config.ice_check_min_interval = rtc::Optional<int>();
2402 CreatePeerConnection(config, nullptr);
2403 config.ice_check_min_interval = rtc::Optional<int>(100);
2404 EXPECT_TRUE(pc_->SetConfiguration(config));
2405 PeerConnectionInterface::RTCConfiguration new_config =
2406 pc_->GetConfiguration();
2407 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2408}
2409
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002410// Test that when SetConfiguration changes both the pool size and other
2411// attributes, the pooled session is created with the updated attributes.
2412TEST_F(PeerConnectionInterfaceTest,
2413 SetConfigurationCreatesPooledSessionCorrectly) {
2414 CreatePeerConnection();
2415 PeerConnectionInterface::RTCConfiguration config;
2416 config.ice_candidate_pool_size = 1;
2417 PeerConnectionInterface::IceServer server;
2418 server.uri = kStunAddressOnly;
2419 config.servers.push_back(server);
2420 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002421 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002422
2423 const cricket::FakePortAllocatorSession* session =
2424 static_cast<const cricket::FakePortAllocatorSession*>(
2425 port_allocator_->GetPooledSession());
2426 ASSERT_NE(nullptr, session);
2427 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002428}
2429
deadbeef293e9262017-01-11 12:28:30 -08002430// Test that after SetLocalDescription, changing the pool size is not allowed,
2431// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002432TEST_F(PeerConnectionInterfaceTest,
2433 CantChangePoolSizeAfterSetLocalDescription) {
2434 CreatePeerConnection();
2435 // Start by setting a size of 1.
2436 PeerConnectionInterface::RTCConfiguration config;
2437 config.ice_candidate_pool_size = 1;
2438 EXPECT_TRUE(pc_->SetConfiguration(config));
2439
2440 // Set remote offer; can still change pool size at this point.
2441 CreateOfferAsRemoteDescription();
2442 config.ice_candidate_pool_size = 2;
2443 EXPECT_TRUE(pc_->SetConfiguration(config));
2444
2445 // Set local answer; now it's too late.
2446 CreateAnswerAsLocalDescription();
2447 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002448 RTCError error;
2449 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2450 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2451}
2452
deadbeef42a42632017-03-10 15:18:00 -08002453// Test that after setting an answer, extra pooled sessions are discarded. The
2454// ICE candidate pool is only intended to be used for the first offer/answer.
2455TEST_F(PeerConnectionInterfaceTest,
2456 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2457 CreatePeerConnection();
2458
2459 // Set a larger-than-necessary size.
2460 PeerConnectionInterface::RTCConfiguration config;
2461 config.ice_candidate_pool_size = 4;
2462 EXPECT_TRUE(pc_->SetConfiguration(config));
2463
2464 // Do offer/answer.
2465 CreateOfferAsRemoteDescription();
2466 CreateAnswerAsLocalDescription();
2467
2468 // Expect no pooled sessions to be left.
2469 const cricket::PortAllocatorSession* session =
2470 port_allocator_->GetPooledSession();
2471 EXPECT_EQ(nullptr, session);
2472}
2473
2474// After Close is called, pooled candidates should be discarded so as to not
2475// waste network resources.
2476TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2477 CreatePeerConnection();
2478
2479 PeerConnectionInterface::RTCConfiguration config;
2480 config.ice_candidate_pool_size = 3;
2481 EXPECT_TRUE(pc_->SetConfiguration(config));
2482 pc_->Close();
2483
2484 // Expect no pooled sessions to be left.
2485 const cricket::PortAllocatorSession* session =
2486 port_allocator_->GetPooledSession();
2487 EXPECT_EQ(nullptr, session);
2488}
2489
deadbeef293e9262017-01-11 12:28:30 -08002490// Test that SetConfiguration returns an invalid modification error if
2491// modifying a field in the configuration that isn't allowed to be modified.
2492TEST_F(PeerConnectionInterfaceTest,
2493 SetConfigurationReturnsInvalidModificationError) {
2494 PeerConnectionInterface::RTCConfiguration config;
2495 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2496 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2497 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2498 CreatePeerConnection(config, nullptr);
2499
2500 PeerConnectionInterface::RTCConfiguration modified_config = config;
2501 modified_config.bundle_policy =
2502 PeerConnectionInterface::kBundlePolicyMaxBundle;
2503 RTCError error;
2504 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2505 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2506
2507 modified_config = config;
2508 modified_config.rtcp_mux_policy =
2509 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2510 error.set_type(RTCErrorType::NONE);
2511 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2512 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2513
2514 modified_config = config;
2515 modified_config.continual_gathering_policy =
2516 PeerConnectionInterface::GATHER_CONTINUALLY;
2517 error.set_type(RTCErrorType::NONE);
2518 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2519 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2520}
2521
2522// Test that SetConfiguration returns a range error if the candidate pool size
2523// is negative or larger than allowed by the spec.
2524TEST_F(PeerConnectionInterfaceTest,
2525 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2526 PeerConnectionInterface::RTCConfiguration config;
2527 CreatePeerConnection(config, nullptr);
2528
2529 config.ice_candidate_pool_size = -1;
2530 RTCError error;
2531 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2532 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2533
2534 config.ice_candidate_pool_size = INT_MAX;
2535 error.set_type(RTCErrorType::NONE);
2536 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2537 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2538}
2539
2540// Test that SetConfiguration returns a syntax error if parsing an ICE server
2541// URL failed.
2542TEST_F(PeerConnectionInterfaceTest,
2543 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2544 PeerConnectionInterface::RTCConfiguration config;
2545 CreatePeerConnection(config, nullptr);
2546
2547 PeerConnectionInterface::IceServer bad_server;
2548 bad_server.uri = "stunn:www.example.com";
2549 config.servers.push_back(bad_server);
2550 RTCError error;
2551 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2552 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2553}
2554
2555// Test that SetConfiguration returns an invalid parameter error if a TURN
2556// IceServer is missing a username or password.
2557TEST_F(PeerConnectionInterfaceTest,
2558 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2559 PeerConnectionInterface::RTCConfiguration config;
2560 CreatePeerConnection(config, nullptr);
2561
2562 PeerConnectionInterface::IceServer bad_server;
2563 bad_server.uri = "turn:www.example.com";
2564 // Missing password.
2565 bad_server.username = "foo";
2566 config.servers.push_back(bad_server);
2567 RTCError error;
2568 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2569 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002570}
2571
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002572// Test that PeerConnection::Close changes the states to closed and all remote
2573// tracks change state to ended.
2574TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2575 // Initialize a PeerConnection and negotiate local and remote session
2576 // description.
2577 InitiateCall();
2578 ASSERT_EQ(1u, pc_->local_streams()->count());
2579 ASSERT_EQ(1u, pc_->remote_streams()->count());
2580
2581 pc_->Close();
2582
2583 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2584 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2585 pc_->ice_connection_state());
2586 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2587 pc_->ice_gathering_state());
2588
2589 EXPECT_EQ(1u, pc_->local_streams()->count());
2590 EXPECT_EQ(1u, pc_->remote_streams()->count());
2591
zhihuang9763d562016-08-05 11:14:50 -07002592 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2593 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002594 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002595 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002596 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2597 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2598 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002599}
2600
2601// Test that PeerConnection methods fails gracefully after
2602// PeerConnection::Close has been called.
2603TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002604 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2606 CreateOfferAsRemoteDescription();
2607 CreateAnswerAsLocalDescription();
2608
2609 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002610 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002611 pc_->local_streams()->at(0);
2612
2613 pc_->Close();
2614
2615 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002616 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617
2618 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002619 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002621 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622
2623 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2624
2625 EXPECT_TRUE(pc_->local_description() != NULL);
2626 EXPECT_TRUE(pc_->remote_description() != NULL);
2627
kwibergd1fe2812016-04-27 06:47:29 -07002628 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002629 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002630 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002631 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632
2633 std::string sdp;
2634 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002635 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2636 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2637 nullptr));
2638 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002639
2640 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002641 std::unique_ptr<SessionDescriptionInterface> local_offer(
2642 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2643 nullptr));
2644 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645}
2646
2647// Test that GetStats can still be called after PeerConnection::Close.
2648TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2649 InitiateCall();
2650 pc_->Close();
2651 DoGetStats(NULL);
2652}
deadbeefab9b2d12015-10-14 11:33:11 -07002653
2654// NOTE: The series of tests below come from what used to be
2655// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2656// setting a remote or local description has the expected effects.
2657
2658// This test verifies that the remote MediaStreams corresponding to a received
2659// SDP string is created. In this test the two separate MediaStreams are
2660// signaled.
2661TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2662 FakeConstraints constraints;
2663 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2664 true);
2665 CreatePeerConnection(&constraints);
2666 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2667
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002668 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002669 EXPECT_TRUE(
2670 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2671 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2672 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2673
2674 // Create a session description based on another SDP with another
2675 // MediaStream.
2676 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2677
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002678 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002679 EXPECT_TRUE(
2680 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2681}
2682
2683// This test verifies that when remote tracks are added/removed from SDP, the
2684// created remote streams are updated appropriately.
2685TEST_F(PeerConnectionInterfaceTest,
2686 AddRemoveTrackFromExistingRemoteMediaStream) {
2687 FakeConstraints constraints;
2688 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2689 true);
2690 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002691 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002692 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002693 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002694 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2695 reference_collection_));
2696
2697 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002698 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002699 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002700 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002701 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2702 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002703 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002704 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2705 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002706 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002707 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2708 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002709
2710 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002711 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002712 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002713 MockTrackObserver audio_track_observer(audio_track2);
2714 MockTrackObserver video_track_observer(video_track2);
2715
2716 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2717 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002718 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002719 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2720 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002721 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002722 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002723 audio_track2->state(), kTimeout);
2724 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2725 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002726}
2727
2728// This tests that remote tracks are ended if a local session description is set
2729// that rejects the media content type.
2730TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2731 FakeConstraints constraints;
2732 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2733 true);
2734 CreatePeerConnection(&constraints);
2735 // First create and set a remote offer, then reject its video content in our
2736 // answer.
2737 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2738 ASSERT_EQ(1u, observer_.remote_streams()->count());
2739 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2740 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2741 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2742
2743 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2744 remote_stream->GetVideoTracks()[0];
2745 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2746 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2747 remote_stream->GetAudioTracks()[0];
2748 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2749
kwibergd1fe2812016-04-27 06:47:29 -07002750 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002751 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002752 cricket::ContentInfo* video_info =
2753 local_answer->description()->GetContentByName("video");
2754 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002755 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002756 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2757 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2758
2759 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002760 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002761 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002762 video_info = local_offer->description()->GetContentByName("video");
2763 ASSERT_TRUE(video_info != nullptr);
2764 video_info->rejected = true;
2765 cricket::ContentInfo* audio_info =
2766 local_offer->description()->GetContentByName("audio");
2767 ASSERT_TRUE(audio_info != nullptr);
2768 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002769 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002770 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002771 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002772 remote_audio->state(), kTimeout);
2773 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2774 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002775}
2776
2777// This tests that we won't crash if the remote track has been removed outside
2778// of PeerConnection and then PeerConnection tries to reject the track.
2779TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2780 FakeConstraints constraints;
2781 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2782 true);
2783 CreatePeerConnection(&constraints);
2784 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2785 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2786 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2787 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2788
kwibergd1fe2812016-04-27 06:47:29 -07002789 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002790 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2791 kSdpStringWithStream1, nullptr));
2792 cricket::ContentInfo* video_info =
2793 local_answer->description()->GetContentByName("video");
2794 video_info->rejected = true;
2795 cricket::ContentInfo* audio_info =
2796 local_answer->description()->GetContentByName("audio");
2797 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002798 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002799
2800 // No crash is a pass.
2801}
2802
deadbeef5e97fb52015-10-15 12:49:08 -07002803// This tests that if a recvonly remote description is set, no remote streams
2804// will be created, even if the description contains SSRCs/MSIDs.
2805// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2806TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2807 FakeConstraints constraints;
2808 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2809 true);
2810 CreatePeerConnection(&constraints);
2811
2812 std::string recvonly_offer = kSdpStringWithStream1;
2813 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2814 strlen(kRecvonly), &recvonly_offer);
2815 CreateAndSetRemoteOffer(recvonly_offer);
2816
2817 EXPECT_EQ(0u, observer_.remote_streams()->count());
2818}
2819
deadbeefab9b2d12015-10-14 11:33:11 -07002820// This tests that a default MediaStream is created if a remote session
2821// description doesn't contain any streams and no MSID support.
2822// It also tests that the default stream is updated if a video m-line is added
2823// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002824TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002825 FakeConstraints constraints;
2826 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2827 true);
2828 CreatePeerConnection(&constraints);
2829 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2830
2831 ASSERT_EQ(1u, observer_.remote_streams()->count());
2832 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2833
2834 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2835 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2836 EXPECT_EQ("default", remote_stream->label());
2837
2838 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2839 ASSERT_EQ(1u, observer_.remote_streams()->count());
2840 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2841 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002842 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2843 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002844 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2845 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002846 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2847 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002848}
2849
2850// This tests that a default MediaStream is created if a remote session
2851// description doesn't contain any streams and media direction is send only.
2852TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002853 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002854 FakeConstraints constraints;
2855 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2856 true);
2857 CreatePeerConnection(&constraints);
2858 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2859
2860 ASSERT_EQ(1u, observer_.remote_streams()->count());
2861 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2862
2863 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2864 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2865 EXPECT_EQ("default", remote_stream->label());
2866}
2867
2868// This tests that it won't crash when PeerConnection tries to remove
2869// a remote track that as already been removed from the MediaStream.
2870TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2871 FakeConstraints constraints;
2872 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2873 true);
2874 CreatePeerConnection(&constraints);
2875 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2876 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2877 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2878 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2879
2880 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2881
2882 // No crash is a pass.
2883}
2884
2885// This tests that a default MediaStream is created if the remote session
2886// description doesn't contain any streams and don't contain an indication if
2887// MSID is supported.
2888TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002889 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002890 FakeConstraints constraints;
2891 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2892 true);
2893 CreatePeerConnection(&constraints);
2894 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2895
2896 ASSERT_EQ(1u, observer_.remote_streams()->count());
2897 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2898 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2899 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2900}
2901
2902// This tests that a default MediaStream is not created if the remote session
2903// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002904TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002905 FakeConstraints constraints;
2906 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2907 true);
2908 CreatePeerConnection(&constraints);
2909 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2910 EXPECT_EQ(0u, observer_.remote_streams()->count());
2911}
2912
deadbeefbda7e0b2015-12-08 17:13:40 -08002913// This tests that when setting a new description, the old default tracks are
2914// not destroyed and recreated.
2915// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002916TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002917 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002918 FakeConstraints constraints;
2919 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2920 true);
2921 CreatePeerConnection(&constraints);
2922 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2923
2924 ASSERT_EQ(1u, observer_.remote_streams()->count());
2925 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2926 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2927
2928 // Set the track to "disabled", then set a new description and ensure the
2929 // track is still disabled, which ensures it hasn't been recreated.
2930 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2931 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2932 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2933 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2934}
2935
deadbeefab9b2d12015-10-14 11:33:11 -07002936// This tests that a default MediaStream is not created if a remote session
2937// description is updated to not have any MediaStreams.
2938TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2939 FakeConstraints constraints;
2940 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2941 true);
2942 CreatePeerConnection(&constraints);
2943 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002944 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002945 EXPECT_TRUE(
2946 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2947
2948 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2949 EXPECT_EQ(0u, observer_.remote_streams()->count());
2950}
2951
2952// This tests that an RtpSender is created when the local description is set
2953// after adding a local stream.
2954// TODO(deadbeef): This test and the one below it need to be updated when
2955// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002956TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002957 FakeConstraints constraints;
2958 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2959 true);
2960 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002961
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002962 // Create an offer with 1 stream with 2 tracks of each type.
2963 rtc::scoped_refptr<StreamCollection> stream_collection =
2964 CreateStreamCollection(1, 2);
2965 pc_->AddStream(stream_collection->at(0));
2966 std::unique_ptr<SessionDescriptionInterface> offer;
2967 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002968 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002969
deadbeefab9b2d12015-10-14 11:33:11 -07002970 auto senders = pc_->GetSenders();
2971 EXPECT_EQ(4u, senders.size());
2972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2974 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2975 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2976
2977 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 pc_->RemoveStream(stream_collection->at(0));
2979 stream_collection = CreateStreamCollection(1, 1);
2980 pc_->AddStream(stream_collection->at(0));
2981 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002982 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002983
deadbeefab9b2d12015-10-14 11:33:11 -07002984 senders = pc_->GetSenders();
2985 EXPECT_EQ(2u, senders.size());
2986 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2987 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2988 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2989 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2990}
2991
2992// This tests that an RtpSender is created when the local description is set
2993// before adding a local stream.
2994TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002995 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002996 FakeConstraints constraints;
2997 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2998 true);
2999 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003000
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003001 rtc::scoped_refptr<StreamCollection> stream_collection =
3002 CreateStreamCollection(1, 2);
3003 // Add a stream to create the offer, but remove it afterwards.
3004 pc_->AddStream(stream_collection->at(0));
3005 std::unique_ptr<SessionDescriptionInterface> offer;
3006 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3007 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003008
Steve Antondb45ca82017-09-11 18:27:34 -07003009 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003010 auto senders = pc_->GetSenders();
3011 EXPECT_EQ(0u, senders.size());
3012
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003013 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003014 senders = pc_->GetSenders();
3015 EXPECT_EQ(4u, senders.size());
3016 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3017 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3018 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3019 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3020}
3021
3022// This tests that the expected behavior occurs if the SSRC on a local track is
3023// changed when SetLocalDescription is called.
3024TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003025 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003026 FakeConstraints constraints;
3027 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3028 true);
3029 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003030
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003031 rtc::scoped_refptr<StreamCollection> stream_collection =
3032 CreateStreamCollection(2, 1);
3033 pc_->AddStream(stream_collection->at(0));
3034 std::unique_ptr<SessionDescriptionInterface> offer;
3035 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3036 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003037 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3038 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003039 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3040 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003041 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003042
deadbeefab9b2d12015-10-14 11:33:11 -07003043 auto senders = pc_->GetSenders();
3044 EXPECT_EQ(2u, senders.size());
3045 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3046 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3047
3048 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003049 cricket::MediaContentDescription* desc =
3050 cricket::GetFirstAudioContentDescription(modified_offer->description());
3051 ASSERT_TRUE(desc != NULL);
3052 for (StreamParams& stream : desc->mutable_streams()) {
3053 for (unsigned int& ssrc : stream.ssrcs) {
3054 ++ssrc;
3055 }
3056 }
deadbeefab9b2d12015-10-14 11:33:11 -07003057
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003058 desc =
3059 cricket::GetFirstVideoContentDescription(modified_offer->description());
3060 ASSERT_TRUE(desc != NULL);
3061 for (StreamParams& stream : desc->mutable_streams()) {
3062 for (unsigned int& ssrc : stream.ssrcs) {
3063 ++ssrc;
3064 }
3065 }
3066
Steve Antondb45ca82017-09-11 18:27:34 -07003067 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003068 senders = pc_->GetSenders();
3069 EXPECT_EQ(2u, senders.size());
3070 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3071 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3072 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3073 // changed.
3074}
3075
3076// This tests that the expected behavior occurs if a new session description is
3077// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003078TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003079 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003080 FakeConstraints constraints;
3081 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3082 true);
3083 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003084
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003085 rtc::scoped_refptr<StreamCollection> stream_collection =
3086 CreateStreamCollection(2, 1);
3087 pc_->AddStream(stream_collection->at(0));
3088 std::unique_ptr<SessionDescriptionInterface> offer;
3089 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003090 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003091
deadbeefab9b2d12015-10-14 11:33:11 -07003092 auto senders = pc_->GetSenders();
3093 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003094 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3095 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003096
3097 // Add a new MediaStream but with the same tracks as in the first stream.
3098 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3099 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003100 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3101 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003102 pc_->AddStream(stream_1);
3103
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003104 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003105 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003106
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003107 auto new_senders = pc_->GetSenders();
3108 // Should be the same senders as before, but with updated stream id.
3109 // Note that this behavior is subject to change in the future.
3110 // We may decide the PC should ignore existing tracks in AddStream.
3111 EXPECT_EQ(senders, new_senders);
3112 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3113 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003114}
3115
zhihuang81c3a032016-11-17 12:06:24 -08003116// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3117TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3118 FakeConstraints constraints;
3119 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3120 true);
3121 CreatePeerConnection(&constraints);
3122 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3123 EXPECT_EQ(observer_.num_added_tracks_, 1);
3124 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3125
3126 // Create and set the updated remote SDP.
3127 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3128 EXPECT_EQ(observer_.num_added_tracks_, 2);
3129 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3130}
3131
deadbeefd1a38b52016-12-10 13:15:33 -08003132// Test that when SetConfiguration is called and the configuration is
3133// changing, the next offer causes an ICE restart.
3134TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3135 PeerConnectionInterface::RTCConfiguration config;
3136 config.type = PeerConnectionInterface::kRelay;
3137 // Need to pass default constraints to prevent disabling of DTLS...
3138 FakeConstraints default_constraints;
3139 CreatePeerConnection(config, &default_constraints);
3140 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3141
3142 // Do initial offer/answer so there's something to restart.
3143 CreateOfferAsLocalDescription();
3144 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3145
3146 // Grab the ufrags.
3147 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3148
3149 // Change ICE policy, which should trigger an ICE restart on the next offer.
3150 config.type = PeerConnectionInterface::kAll;
3151 EXPECT_TRUE(pc_->SetConfiguration(config));
3152 CreateOfferAsLocalDescription();
3153
3154 // Grab the new ufrags.
3155 std::vector<std::string> subsequent_ufrags =
3156 GetUfrags(pc_->local_description());
3157
3158 // Sanity check.
3159 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3160 // Check that each ufrag is different.
3161 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3162 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3163 }
3164}
3165
3166// Test that when SetConfiguration is called and the configuration *isn't*
3167// changing, the next offer does *not* cause an ICE restart.
3168TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3169 PeerConnectionInterface::RTCConfiguration config;
3170 config.type = PeerConnectionInterface::kRelay;
3171 // Need to pass default constraints to prevent disabling of DTLS...
3172 FakeConstraints default_constraints;
3173 CreatePeerConnection(config, &default_constraints);
3174 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3175
3176 // Do initial offer/answer so there's something to restart.
3177 CreateOfferAsLocalDescription();
3178 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3179
3180 // Grab the ufrags.
3181 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3182
3183 // Call SetConfiguration with a config identical to what the PC was
3184 // constructed with.
3185 EXPECT_TRUE(pc_->SetConfiguration(config));
3186 CreateOfferAsLocalDescription();
3187
3188 // Grab the new ufrags.
3189 std::vector<std::string> subsequent_ufrags =
3190 GetUfrags(pc_->local_description());
3191
3192 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3193}
3194
3195// Test for a weird corner case scenario:
3196// 1. Audio/video session established.
3197// 2. SetConfiguration changes ICE config; ICE restart needed.
3198// 3. ICE restart initiated by remote peer, but only for one m= section.
3199// 4. Next createOffer should initiate an ICE restart, but only for the other
3200// m= section; it would be pointless to do an ICE restart for the m= section
3201// that was already restarted.
3202TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3203 PeerConnectionInterface::RTCConfiguration config;
3204 config.type = PeerConnectionInterface::kRelay;
3205 // Need to pass default constraints to prevent disabling of DTLS...
3206 FakeConstraints default_constraints;
3207 CreatePeerConnection(config, &default_constraints);
3208 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3209
3210 // Do initial offer/answer so there's something to restart.
3211 CreateOfferAsLocalDescription();
3212 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3213
3214 // Change ICE policy, which should set the "needs-ice-restart" flag.
3215 config.type = PeerConnectionInterface::kAll;
3216 EXPECT_TRUE(pc_->SetConfiguration(config));
3217
3218 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003219 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3220 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3221 kSdpStringWithStream1, nullptr));
3222 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003223 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3224 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003225 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003226 CreateAnswerAsLocalDescription();
3227
3228 // Grab the ufrags.
3229 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3230 ASSERT_EQ(2, initial_ufrags.size());
3231
3232 // Create offer and grab the new ufrags.
3233 CreateOfferAsLocalDescription();
3234 std::vector<std::string> subsequent_ufrags =
3235 GetUfrags(pc_->local_description());
3236 ASSERT_EQ(2, subsequent_ufrags.size());
3237
3238 // Ensure that only the ufrag for the second m= section changed.
3239 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3240 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3241}
3242
deadbeeffe4a8a42016-12-20 17:56:17 -08003243// Tests that the methods to return current/pending descriptions work as
3244// expected at different points in the offer/answer exchange. This test does
3245// one offer/answer exchange as the offerer, then another as the answerer.
3246TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3247 // This disables DTLS so we can apply an answer to ourselves.
3248 CreatePeerConnection();
3249
3250 // Create initial local offer and get SDP (which will also be used as
3251 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003252 std::unique_ptr<SessionDescriptionInterface> local_offer;
3253 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003254 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003255 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003256
3257 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003258 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3259 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3260 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003261 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3262 EXPECT_EQ(nullptr, pc_->current_local_description());
3263 EXPECT_EQ(nullptr, pc_->current_remote_description());
3264
3265 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003266 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003267 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003268 sdp, nullptr));
3269 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3270 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3271 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3272 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003273 EXPECT_EQ(nullptr, pc_->current_local_description());
3274 EXPECT_EQ(nullptr, pc_->current_remote_description());
3275
3276 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003277 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3278 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3279 sdp, nullptr));
3280 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3281 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003282 EXPECT_EQ(nullptr, pc_->pending_local_description());
3283 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003284 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3285 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003286
3287 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003288 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3289 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3290 nullptr));
3291 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3292 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3293 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003294 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003295 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3296 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003297
3298 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003299 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003300 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003301 sdp, nullptr));
3302 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3303 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3304 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3305 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3306 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3307 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003308
3309 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003310 std::unique_ptr<SessionDescriptionInterface> local_answer(
3311 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3312 sdp, nullptr));
3313 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3314 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003315 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3316 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003317 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3318 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003319}
3320
zhihuang77985012017-02-07 15:45:16 -08003321// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3322// after the PeerConnection is closed.
3323TEST_F(PeerConnectionInterfaceTest,
3324 StartAndStopLoggingAfterPeerConnectionClosed) {
3325 CreatePeerConnection();
3326 // The RtcEventLog will be reset when the PeerConnection is closed.
3327 pc_->Close();
3328
3329 rtc::PlatformFile file = 0;
3330 int64_t max_size_bytes = 1024;
3331 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3332 pc_->StopRtcEventLog();
3333}
3334
deadbeef30952b42017-04-21 02:41:29 -07003335// Test that generated offers/answers include "ice-option:trickle".
3336TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3337 CreatePeerConnection();
3338
3339 // First, create an offer with audio/video.
3340 FakeConstraints constraints;
3341 constraints.SetMandatoryReceiveAudio(true);
3342 constraints.SetMandatoryReceiveVideo(true);
3343 std::unique_ptr<SessionDescriptionInterface> offer;
3344 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3345 cricket::SessionDescription* desc = offer->description();
3346 ASSERT_EQ(2u, desc->transport_infos().size());
3347 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3348 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3349
3350 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003351 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003352 std::unique_ptr<SessionDescriptionInterface> answer;
3353 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3354 desc = answer->description();
3355 ASSERT_EQ(2u, desc->transport_infos().size());
3356 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3357 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3358}
3359
deadbeef1dcb1642017-03-29 21:08:16 -07003360// Test that ICE renomination isn't offered if it's not enabled in the PC's
3361// RTCConfiguration.
3362TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3363 PeerConnectionInterface::RTCConfiguration config;
3364 config.enable_ice_renomination = false;
3365 CreatePeerConnection(config, nullptr);
3366 AddVoiceStream("foo");
3367
3368 std::unique_ptr<SessionDescriptionInterface> offer;
3369 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3370 cricket::SessionDescription* desc = offer->description();
3371 EXPECT_EQ(1u, desc->transport_infos().size());
3372 EXPECT_FALSE(
3373 desc->transport_infos()[0].description.GetIceParameters().renomination);
3374}
3375
3376// Test that the ICE renomination option is present in generated offers/answers
3377// if it's enabled in the PC's RTCConfiguration.
3378TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3379 PeerConnectionInterface::RTCConfiguration config;
3380 config.enable_ice_renomination = true;
3381 CreatePeerConnection(config, nullptr);
3382 AddVoiceStream("foo");
3383
3384 std::unique_ptr<SessionDescriptionInterface> offer;
3385 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3386 cricket::SessionDescription* desc = offer->description();
3387 EXPECT_EQ(1u, desc->transport_infos().size());
3388 EXPECT_TRUE(
3389 desc->transport_infos()[0].description.GetIceParameters().renomination);
3390
3391 // Set the offer as a remote description, then create an answer and ensure it
3392 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003393 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003394 std::unique_ptr<SessionDescriptionInterface> answer;
3395 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3396 desc = answer->description();
3397 EXPECT_EQ(1u, desc->transport_infos().size());
3398 EXPECT_TRUE(
3399 desc->transport_infos()[0].description.GetIceParameters().renomination);
3400}
3401
3402// Test that if CreateOffer is called with the deprecated "offer to receive
3403// audio/video" constraints, they're processed and result in an offer with
3404// audio/video sections just as if RTCOfferAnswerOptions had been used.
3405TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3406 CreatePeerConnection();
3407
3408 FakeConstraints constraints;
3409 constraints.SetMandatoryReceiveAudio(true);
3410 constraints.SetMandatoryReceiveVideo(true);
3411 std::unique_ptr<SessionDescriptionInterface> offer;
3412 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3413
3414 cricket::SessionDescription* desc = offer->description();
3415 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3416 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3417 ASSERT_NE(nullptr, audio);
3418 ASSERT_NE(nullptr, video);
3419 EXPECT_FALSE(audio->rejected);
3420 EXPECT_FALSE(video->rejected);
3421}
3422
3423// Test that if CreateAnswer is called with the deprecated "offer to receive
3424// audio/video" constraints, they're processed and can be used to reject an
3425// offered m= section just as can be done with RTCOfferAnswerOptions;
3426TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3427 CreatePeerConnection();
3428
3429 // First, create an offer with audio/video and apply it as a remote
3430 // description.
3431 FakeConstraints constraints;
3432 constraints.SetMandatoryReceiveAudio(true);
3433 constraints.SetMandatoryReceiveVideo(true);
3434 std::unique_ptr<SessionDescriptionInterface> offer;
3435 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003436 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003437
3438 // Now create answer that rejects audio/video.
3439 constraints.SetMandatoryReceiveAudio(false);
3440 constraints.SetMandatoryReceiveVideo(false);
3441 std::unique_ptr<SessionDescriptionInterface> answer;
3442 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3443
3444 cricket::SessionDescription* desc = answer->description();
3445 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3446 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3447 ASSERT_NE(nullptr, audio);
3448 ASSERT_NE(nullptr, video);
3449 EXPECT_TRUE(audio->rejected);
3450 EXPECT_TRUE(video->rejected);
3451}
3452
3453#ifdef HAVE_SCTP
3454#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3455 DataChannelOnlyOfferWithMaxBundlePolicy
3456#else
3457#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3458 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3459#endif
3460
3461// Test that negotiation can succeed with a data channel only, and with the max
3462// bundle policy. Previously there was a bug that prevented this.
3463TEST_F(PeerConnectionInterfaceTest,
3464 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3465 PeerConnectionInterface::RTCConfiguration config;
3466 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3467 CreatePeerConnection(config, nullptr);
3468
3469 // First, create an offer with only a data channel and apply it as a remote
3470 // description.
3471 pc_->CreateDataChannel("test", nullptr);
3472 std::unique_ptr<SessionDescriptionInterface> offer;
3473 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003474 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003475
3476 // Create and set answer as well.
3477 std::unique_ptr<SessionDescriptionInterface> answer;
3478 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003479 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003480}
3481
zstein4b979802017-06-02 14:37:37 -07003482TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3483 CreatePeerConnection();
3484 PeerConnectionInterface::BitrateParameters bitrate;
3485 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3486 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3487}
3488
3489TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3490 CreatePeerConnection();
3491 PeerConnectionInterface::BitrateParameters bitrate;
3492 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3493 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3494}
3495
3496TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3497 CreatePeerConnection();
3498 PeerConnectionInterface::BitrateParameters bitrate;
3499 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3500 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3501 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3502}
3503
3504TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3505 CreatePeerConnection();
3506 PeerConnectionInterface::BitrateParameters bitrate;
3507 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3508 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3509}
3510
3511TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3512 CreatePeerConnection();
3513 PeerConnectionInterface::BitrateParameters bitrate;
3514 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3515 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3516 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3517}
3518
3519TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3520 CreatePeerConnection();
3521 PeerConnectionInterface::BitrateParameters bitrate;
3522 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3523 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3524 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3525}
3526
3527TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3528 CreatePeerConnection();
3529 PeerConnectionInterface::BitrateParameters bitrate;
3530 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3531 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3532}
3533
Steve Anton038834f2017-07-14 15:59:59 -07003534// ice_regather_interval_range requires WebRTC to be configured for continual
3535// gathering already.
3536TEST_F(PeerConnectionInterfaceTest,
3537 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3538 PeerConnectionInterface::RTCConfiguration config;
3539 config.ice_regather_interval_range.emplace(1000, 2000);
3540 config.continual_gathering_policy =
3541 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3542 CreatePeerConnectionExpectFail(config);
3543}
3544
3545// Ensures that there is no error when ice_regather_interval_range is set with
3546// continual gathering enabled.
3547TEST_F(PeerConnectionInterfaceTest,
3548 SetIceRegatherIntervalRangeWithContinualGathering) {
3549 PeerConnectionInterface::RTCConfiguration config;
3550 config.ice_regather_interval_range.emplace(1000, 2000);
3551 config.continual_gathering_policy =
3552 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3553 CreatePeerConnection(config, nullptr);
3554}
3555
zstein4b979802017-06-02 14:37:37 -07003556// The current bitrate from Call's BitrateConfigMask is currently clamped by
3557// Call's BitrateConfig, which comes from the SDP or a default value. This test
3558// checks that a call to SetBitrate with a current bitrate that will be clamped
3559// succeeds.
3560TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3561 CreatePeerConnection();
3562 PeerConnectionInterface::BitrateParameters bitrate;
3563 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3564 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3565}
3566
zhihuang1c378ed2017-08-17 14:10:50 -07003567// The following tests verify that the offer can be created correctly.
3568TEST_F(PeerConnectionInterfaceTest,
3569 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3570 RTCOfferAnswerOptions rtc_options;
3571
3572 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3573 // than kMaxOfferToReceiveMedia should be treated as invalid.
3574 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3575 CreatePeerConnection();
3576 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3577
3578 rtc_options.offer_to_receive_audio =
3579 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3580 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3581}
3582
3583TEST_F(PeerConnectionInterfaceTest,
3584 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3585 RTCOfferAnswerOptions rtc_options;
3586
3587 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3588 // than kMaxOfferToReceiveMedia should be treated as invalid.
3589 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3590 CreatePeerConnection();
3591 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3592
3593 rtc_options.offer_to_receive_video =
3594 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3595 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3596}
3597
3598// Test that the audio and video content will be added to an offer if both
3599// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3600TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3601 RTCOfferAnswerOptions rtc_options;
3602 rtc_options.offer_to_receive_audio = 1;
3603 rtc_options.offer_to_receive_video = 1;
3604
3605 std::unique_ptr<SessionDescriptionInterface> offer;
3606 CreatePeerConnection();
3607 offer = CreateOfferWithOptions(rtc_options);
3608 ASSERT_TRUE(offer);
3609 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3610 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3611}
3612
3613// Test that only audio content will be added to the offer if only
3614// |offer_to_receive_audio| options is 1.
3615TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3616 RTCOfferAnswerOptions rtc_options;
3617 rtc_options.offer_to_receive_audio = 1;
3618 rtc_options.offer_to_receive_video = 0;
3619
3620 std::unique_ptr<SessionDescriptionInterface> offer;
3621 CreatePeerConnection();
3622 offer = CreateOfferWithOptions(rtc_options);
3623 ASSERT_TRUE(offer);
3624 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3625 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3626}
3627
3628// Test that only video content will be added if only |offer_to_receive_video|
3629// options is 1.
3630TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3631 RTCOfferAnswerOptions rtc_options;
3632 rtc_options.offer_to_receive_audio = 0;
3633 rtc_options.offer_to_receive_video = 1;
3634
3635 std::unique_ptr<SessionDescriptionInterface> offer;
3636 CreatePeerConnection();
3637 offer = CreateOfferWithOptions(rtc_options);
3638 ASSERT_TRUE(offer);
3639 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3640 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3641}
3642
3643// Test that if |voice_activity_detection| is false, no CN codec is added to the
3644// offer.
3645TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3646 RTCOfferAnswerOptions rtc_options;
3647 rtc_options.offer_to_receive_audio = 1;
3648 rtc_options.offer_to_receive_video = 0;
3649
3650 std::unique_ptr<SessionDescriptionInterface> offer;
3651 CreatePeerConnection();
3652 offer = CreateOfferWithOptions(rtc_options);
3653 ASSERT_TRUE(offer);
3654 const cricket::ContentInfo* audio_content =
3655 offer->description()->GetContentByName(cricket::CN_AUDIO);
3656 ASSERT_TRUE(audio_content);
3657 // |voice_activity_detection| is true by default.
3658 EXPECT_TRUE(HasCNCodecs(audio_content));
3659
3660 rtc_options.voice_activity_detection = false;
3661 CreatePeerConnection();
3662 offer = CreateOfferWithOptions(rtc_options);
3663 ASSERT_TRUE(offer);
3664 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3665 ASSERT_TRUE(audio_content);
3666 EXPECT_FALSE(HasCNCodecs(audio_content));
3667}
3668
3669// Test that no media content will be added to the offer if using default
3670// RTCOfferAnswerOptions.
3671TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3672 RTCOfferAnswerOptions rtc_options;
3673
3674 std::unique_ptr<SessionDescriptionInterface> offer;
3675 CreatePeerConnection();
3676 offer = CreateOfferWithOptions(rtc_options);
3677 ASSERT_TRUE(offer);
3678 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3679 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3680}
3681
3682// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3683// ufrag/pwd will be the same in the new offer.
3684TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3685 RTCOfferAnswerOptions rtc_options;
3686 rtc_options.ice_restart = false;
3687 rtc_options.offer_to_receive_audio = 1;
3688
3689 std::unique_ptr<SessionDescriptionInterface> offer;
3690 CreatePeerConnection();
3691 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3692 auto ufrag1 = offer->description()
3693 ->GetTransportInfoByName(cricket::CN_AUDIO)
3694 ->description.ice_ufrag;
3695 auto pwd1 = offer->description()
3696 ->GetTransportInfoByName(cricket::CN_AUDIO)
3697 ->description.ice_pwd;
3698
3699 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3700 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3701 auto ufrag2 = offer->description()
3702 ->GetTransportInfoByName(cricket::CN_AUDIO)
3703 ->description.ice_ufrag;
3704 auto pwd2 = offer->description()
3705 ->GetTransportInfoByName(cricket::CN_AUDIO)
3706 ->description.ice_pwd;
3707
3708 // |ice_restart| is true, the ufrag/pwd should change.
3709 rtc_options.ice_restart = true;
3710 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3711 auto ufrag3 = offer->description()
3712 ->GetTransportInfoByName(cricket::CN_AUDIO)
3713 ->description.ice_ufrag;
3714 auto pwd3 = offer->description()
3715 ->GetTransportInfoByName(cricket::CN_AUDIO)
3716 ->description.ice_pwd;
3717
3718 EXPECT_EQ(ufrag1, ufrag2);
3719 EXPECT_EQ(pwd1, pwd2);
3720 EXPECT_NE(ufrag2, ufrag3);
3721 EXPECT_NE(pwd2, pwd3);
3722}
3723
3724// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3725// offer; if it is false, there won't be any bundle group in the offer.
3726TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3727 RTCOfferAnswerOptions rtc_options;
3728 rtc_options.offer_to_receive_audio = 1;
3729 rtc_options.offer_to_receive_video = 1;
3730
3731 std::unique_ptr<SessionDescriptionInterface> offer;
3732 CreatePeerConnection();
3733
3734 rtc_options.use_rtp_mux = true;
3735 offer = CreateOfferWithOptions(rtc_options);
3736 ASSERT_TRUE(offer);
3737 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3738 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3739 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3740
3741 rtc_options.use_rtp_mux = false;
3742 offer = CreateOfferWithOptions(rtc_options);
3743 ASSERT_TRUE(offer);
3744 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3745 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3746 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3747}
3748
3749// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3750// called for the answer constraints, but an audio and a video section were
3751// offered, there will still be an audio and a video section in the answer.
3752TEST_F(PeerConnectionInterfaceTest,
3753 RejectAudioAndVideoInAnswerWithConstraints) {
3754 // Offer both audio and video.
3755 RTCOfferAnswerOptions rtc_offer_options;
3756 rtc_offer_options.offer_to_receive_audio = 1;
3757 rtc_offer_options.offer_to_receive_video = 1;
3758
3759 CreatePeerConnection();
3760 std::unique_ptr<SessionDescriptionInterface> offer;
3761 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3762 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3763 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3764
3765 // Since an offer has been created with both audio and video,
3766 // Answers will contain the media types that exist in the offer regardless of
3767 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3768 FakeConstraints answer_c;
3769 // Reject both audio and video.
3770 answer_c.SetMandatoryReceiveAudio(false);
3771 answer_c.SetMandatoryReceiveVideo(false);
3772
3773 std::unique_ptr<SessionDescriptionInterface> answer;
3774 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3775 const cricket::ContentInfo* audio_content =
3776 GetFirstAudioContent(answer->description());
3777 const cricket::ContentInfo* video_content =
3778 GetFirstVideoContent(answer->description());
3779 ASSERT_NE(nullptr, audio_content);
3780 ASSERT_NE(nullptr, video_content);
3781 EXPECT_TRUE(audio_content->rejected);
3782 EXPECT_TRUE(video_content->rejected);
3783}
3784
zhihuang141aacb2017-08-29 13:23:53 -07003785// This test ensures OnRenegotiationNeeded is called when we add track with
3786// MediaStream -> AddTrack in the same way it is called when we add track with
3787// PeerConnection -> AddTrack.
3788// The test can be removed once addStream is rewritten in terms of addTrack
3789// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3790TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3791 CreatePeerConnectionWithoutDtls();
3792 rtc::scoped_refptr<MediaStreamInterface> stream(
3793 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3794 pc_->AddStream(stream);
3795 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3796 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3797 rtc::scoped_refptr<VideoTrackInterface> video_track(
3798 pc_factory_->CreateVideoTrack(
3799 "video_track", pc_factory_->CreateVideoSource(
3800 std::unique_ptr<cricket::VideoCapturer>(
3801 new cricket::FakeVideoCapturer()))));
3802 stream->AddTrack(audio_track);
3803 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3804 observer_.renegotiation_needed_ = false;
3805
3806 stream->AddTrack(video_track);
3807 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3808 observer_.renegotiation_needed_ = false;
3809
3810 stream->RemoveTrack(audio_track);
3811 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3812 observer_.renegotiation_needed_ = false;
3813
3814 stream->RemoveTrack(video_track);
3815 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3816 observer_.renegotiation_needed_ = false;
3817}
3818
3819// Tests that creating answer would fail gracefully without being crashed if the
3820// remote description is unset.
3821TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3822 CreatePeerConnection();
3823 // Creating answer fails because the remote description is unset.
3824 std::unique_ptr<SessionDescriptionInterface> answer;
3825 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3826
3827 // Createing answer succeeds when the remote description is set.
3828 CreateOfferAsRemoteDescription();
3829 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3830}
3831
Zhi Huang2a5e4262017-09-14 01:15:03 -07003832// Test that an error is returned if a description is applied that doesn't
3833// respect the order of existing media sections.
3834TEST_F(PeerConnectionInterfaceTest,
3835 MediaSectionOrderEnforcedForSubsequentOffers) {
3836 CreatePeerConnection();
3837 FakeConstraints constraints;
3838 constraints.SetMandatoryReceiveAudio(true);
3839 constraints.SetMandatoryReceiveVideo(true);
3840 std::unique_ptr<SessionDescriptionInterface> offer;
3841 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3842 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3843
3844 std::unique_ptr<SessionDescriptionInterface> answer;
3845 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3846 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3847
3848 // A remote offer with different m=line order should be rejected.
3849 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3850 std::reverse(offer->description()->contents().begin(),
3851 offer->description()->contents().end());
3852 std::reverse(offer->description()->transport_infos().begin(),
3853 offer->description()->transport_infos().end());
3854 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3855
3856 // A subsequent local offer with different m=line order should be rejected.
3857 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3858 std::reverse(offer->description()->contents().begin(),
3859 offer->description()->contents().end());
3860 std::reverse(offer->description()->transport_infos().begin(),
3861 offer->description()->transport_infos().end());
3862 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3863}
3864
nisse51542be2016-02-12 02:27:06 -08003865class PeerConnectionMediaConfigTest : public testing::Test {
3866 protected:
3867 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003868 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003869 pcf_->Initialize();
3870 }
nisseeaabdf62017-05-05 02:23:02 -07003871 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003872 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003873 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003874 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3875 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003876 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003877 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003878 }
3879
zhihuang9763d562016-08-05 11:14:50 -07003880 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003881 MockPeerConnectionObserver observer_;
3882};
3883
3884// This test verifies the default behaviour with no constraints and a
3885// default RTCConfiguration.
3886TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3887 PeerConnectionInterface::RTCConfiguration config;
3888 FakeConstraints constraints;
3889
3890 const cricket::MediaConfig& media_config =
3891 TestCreatePeerConnection(config, &constraints);
3892
3893 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003894 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3895 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3896 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003897}
3898
3899// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003900// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003901TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3902 PeerConnectionInterface::RTCConfiguration config;
3903 FakeConstraints constraints;
3904
3905 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3906 const cricket::MediaConfig& media_config =
3907 TestCreatePeerConnection(config, &constraints);
3908
3909 EXPECT_TRUE(media_config.enable_dscp);
3910}
3911
3912// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003913// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003914TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3915 PeerConnectionInterface::RTCConfiguration config;
3916 FakeConstraints constraints;
3917
3918 constraints.AddOptional(
3919 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3920 const cricket::MediaConfig media_config =
3921 TestCreatePeerConnection(config, &constraints);
3922
nisse0db023a2016-03-01 04:29:59 -08003923 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003924}
3925
3926// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003927// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003928TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3929 PeerConnectionInterface::RTCConfiguration config;
3930 FakeConstraints constraints;
3931
Niels Möller71bdda02016-03-31 12:59:59 +02003932 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003933 const cricket::MediaConfig& media_config =
3934 TestCreatePeerConnection(config, &constraints);
3935
nisse0db023a2016-03-01 04:29:59 -08003936 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3937}
3938
3939// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003940// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003941TEST_F(PeerConnectionMediaConfigTest,
3942 TestSuspendBelowMinBitrateConstraintTrue) {
3943 PeerConnectionInterface::RTCConfiguration config;
3944 FakeConstraints constraints;
3945
3946 constraints.AddOptional(
3947 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3948 true);
3949 const cricket::MediaConfig media_config =
3950 TestCreatePeerConnection(config, &constraints);
3951
3952 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003953}
3954
deadbeef293e9262017-01-11 12:28:30 -08003955// Tests a few random fields being different.
3956TEST(RTCConfigurationTest, ComparisonOperators) {
3957 PeerConnectionInterface::RTCConfiguration a;
3958 PeerConnectionInterface::RTCConfiguration b;
3959 EXPECT_EQ(a, b);
3960
3961 PeerConnectionInterface::RTCConfiguration c;
3962 c.servers.push_back(PeerConnectionInterface::IceServer());
3963 EXPECT_NE(a, c);
3964
3965 PeerConnectionInterface::RTCConfiguration d;
3966 d.type = PeerConnectionInterface::kRelay;
3967 EXPECT_NE(a, d);
3968
3969 PeerConnectionInterface::RTCConfiguration e;
3970 e.audio_jitter_buffer_max_packets = 5;
3971 EXPECT_NE(a, e);
3972
3973 PeerConnectionInterface::RTCConfiguration f;
3974 f.ice_connection_receiving_timeout = 1337;
3975 EXPECT_NE(a, f);
3976
3977 PeerConnectionInterface::RTCConfiguration g;
3978 g.disable_ipv6 = true;
3979 EXPECT_NE(a, g);
3980
3981 PeerConnectionInterface::RTCConfiguration h(
3982 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3983 EXPECT_NE(a, h);
3984}