blob: 8154b2d4d5a0e0986528b3272918b357251b0463 [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
zhihuangb19012e2017-09-19 13:47:59 -07002252// Test that fallback from DTLS to SDES is not supported.
2253// The fallback was previously supported but was removed to simplify the code
2254// and because it's non-standard.
2255TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002256 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));
zhihuangb19012e2017-09-19 13:47:59 -07002269 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002270}
2271
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272// Test that we can create an audio only offer and receive an answer with a
2273// limited set of audio codecs and receive an updated offer with more audio
2274// codecs, where the added codecs are not supported.
2275TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002276 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 AddVoiceStream("audio_label");
2278 CreateOfferAsLocalDescription();
2279
Steve Antondb45ca82017-09-11 18:27:34 -07002280 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002282 webrtc::kAudioSdp, nullptr));
2283 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284
Steve Antondb45ca82017-09-11 18:27:34 -07002285 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002287 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002288 nullptr));
2289 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 CreateAnswerAsLocalDescription();
2291}
2292
deadbeefc80741f2015-10-22 13:14:45 -07002293// Test that if we're receiving (but not sending) a track, subsequent offers
2294// will have m-lines with a=recvonly.
2295TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2296 FakeConstraints constraints;
2297 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2298 true);
2299 CreatePeerConnection(&constraints);
2300 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2301 CreateAnswerAsLocalDescription();
2302
2303 // At this point we should be receiving stream 1, but not sending anything.
2304 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002305 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002306 DoCreateOffer(&offer, nullptr);
2307
2308 const cricket::ContentInfo* video_content =
2309 cricket::GetFirstVideoContent(offer->description());
2310 const cricket::VideoContentDescription* video_desc =
2311 static_cast<const cricket::VideoContentDescription*>(
2312 video_content->description);
2313 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2314
2315 const cricket::ContentInfo* audio_content =
2316 cricket::GetFirstAudioContent(offer->description());
2317 const cricket::AudioContentDescription* audio_desc =
2318 static_cast<const cricket::AudioContentDescription*>(
2319 audio_content->description);
2320 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2321}
2322
2323// Test that if we're receiving (but not sending) a track, and the
2324// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2325// false, the generated m-lines will be a=inactive.
2326TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2327 FakeConstraints constraints;
2328 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2329 true);
2330 CreatePeerConnection(&constraints);
2331 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2332 CreateAnswerAsLocalDescription();
2333
2334 // At this point we should be receiving stream 1, but not sending anything.
2335 // A new offer would be recvonly, but we'll set the "no receive" constraints
2336 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002337 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002338 FakeConstraints offer_constraints;
2339 offer_constraints.AddMandatory(
2340 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2341 offer_constraints.AddMandatory(
2342 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2343 DoCreateOffer(&offer, &offer_constraints);
2344
2345 const cricket::ContentInfo* video_content =
2346 cricket::GetFirstVideoContent(offer->description());
2347 const cricket::VideoContentDescription* video_desc =
2348 static_cast<const cricket::VideoContentDescription*>(
2349 video_content->description);
2350 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2351
2352 const cricket::ContentInfo* audio_content =
2353 cricket::GetFirstAudioContent(offer->description());
2354 const cricket::AudioContentDescription* audio_desc =
2355 static_cast<const cricket::AudioContentDescription*>(
2356 audio_content->description);
2357 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2358}
2359
deadbeef653b8e02015-11-11 12:55:10 -08002360// Test that we can use SetConfiguration to change the ICE servers of the
2361// PortAllocator.
2362TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2363 CreatePeerConnection();
2364
2365 PeerConnectionInterface::RTCConfiguration config;
2366 PeerConnectionInterface::IceServer server;
2367 server.uri = "stun:test_hostname";
2368 config.servers.push_back(server);
2369 EXPECT_TRUE(pc_->SetConfiguration(config));
2370
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002371 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2372 EXPECT_EQ("test_hostname",
2373 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002374}
2375
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002376TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2377 CreatePeerConnection();
2378 PeerConnectionInterface::RTCConfiguration config;
2379 config.type = PeerConnectionInterface::kRelay;
2380 EXPECT_TRUE(pc_->SetConfiguration(config));
2381 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2382}
2383
deadbeef293e9262017-01-11 12:28:30 -08002384TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2385 PeerConnectionInterface::RTCConfiguration config;
2386 config.prune_turn_ports = false;
2387 CreatePeerConnection(config, nullptr);
2388 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2389
2390 config.prune_turn_ports = true;
2391 EXPECT_TRUE(pc_->SetConfiguration(config));
2392 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2393}
2394
skvladd1f5fda2017-02-03 16:54:05 -08002395// Test that the ice check interval can be changed. This does not verify that
2396// the setting makes it all the way to P2PTransportChannel, as that would
2397// require a very complex set of mocks.
2398TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2399 PeerConnectionInterface::RTCConfiguration config;
2400 config.ice_check_min_interval = rtc::Optional<int>();
2401 CreatePeerConnection(config, nullptr);
2402 config.ice_check_min_interval = rtc::Optional<int>(100);
2403 EXPECT_TRUE(pc_->SetConfiguration(config));
2404 PeerConnectionInterface::RTCConfiguration new_config =
2405 pc_->GetConfiguration();
2406 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2407}
2408
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002409// Test that when SetConfiguration changes both the pool size and other
2410// attributes, the pooled session is created with the updated attributes.
2411TEST_F(PeerConnectionInterfaceTest,
2412 SetConfigurationCreatesPooledSessionCorrectly) {
2413 CreatePeerConnection();
2414 PeerConnectionInterface::RTCConfiguration config;
2415 config.ice_candidate_pool_size = 1;
2416 PeerConnectionInterface::IceServer server;
2417 server.uri = kStunAddressOnly;
2418 config.servers.push_back(server);
2419 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002420 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002421
2422 const cricket::FakePortAllocatorSession* session =
2423 static_cast<const cricket::FakePortAllocatorSession*>(
2424 port_allocator_->GetPooledSession());
2425 ASSERT_NE(nullptr, session);
2426 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002427}
2428
deadbeef293e9262017-01-11 12:28:30 -08002429// Test that after SetLocalDescription, changing the pool size is not allowed,
2430// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002431TEST_F(PeerConnectionInterfaceTest,
2432 CantChangePoolSizeAfterSetLocalDescription) {
2433 CreatePeerConnection();
2434 // Start by setting a size of 1.
2435 PeerConnectionInterface::RTCConfiguration config;
2436 config.ice_candidate_pool_size = 1;
2437 EXPECT_TRUE(pc_->SetConfiguration(config));
2438
2439 // Set remote offer; can still change pool size at this point.
2440 CreateOfferAsRemoteDescription();
2441 config.ice_candidate_pool_size = 2;
2442 EXPECT_TRUE(pc_->SetConfiguration(config));
2443
2444 // Set local answer; now it's too late.
2445 CreateAnswerAsLocalDescription();
2446 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002447 RTCError error;
2448 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2449 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2450}
2451
deadbeef42a42632017-03-10 15:18:00 -08002452// Test that after setting an answer, extra pooled sessions are discarded. The
2453// ICE candidate pool is only intended to be used for the first offer/answer.
2454TEST_F(PeerConnectionInterfaceTest,
2455 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2456 CreatePeerConnection();
2457
2458 // Set a larger-than-necessary size.
2459 PeerConnectionInterface::RTCConfiguration config;
2460 config.ice_candidate_pool_size = 4;
2461 EXPECT_TRUE(pc_->SetConfiguration(config));
2462
2463 // Do offer/answer.
2464 CreateOfferAsRemoteDescription();
2465 CreateAnswerAsLocalDescription();
2466
2467 // Expect no pooled sessions to be left.
2468 const cricket::PortAllocatorSession* session =
2469 port_allocator_->GetPooledSession();
2470 EXPECT_EQ(nullptr, session);
2471}
2472
2473// After Close is called, pooled candidates should be discarded so as to not
2474// waste network resources.
2475TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2476 CreatePeerConnection();
2477
2478 PeerConnectionInterface::RTCConfiguration config;
2479 config.ice_candidate_pool_size = 3;
2480 EXPECT_TRUE(pc_->SetConfiguration(config));
2481 pc_->Close();
2482
2483 // Expect no pooled sessions to be left.
2484 const cricket::PortAllocatorSession* session =
2485 port_allocator_->GetPooledSession();
2486 EXPECT_EQ(nullptr, session);
2487}
2488
deadbeef293e9262017-01-11 12:28:30 -08002489// Test that SetConfiguration returns an invalid modification error if
2490// modifying a field in the configuration that isn't allowed to be modified.
2491TEST_F(PeerConnectionInterfaceTest,
2492 SetConfigurationReturnsInvalidModificationError) {
2493 PeerConnectionInterface::RTCConfiguration config;
2494 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2495 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2496 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2497 CreatePeerConnection(config, nullptr);
2498
2499 PeerConnectionInterface::RTCConfiguration modified_config = config;
2500 modified_config.bundle_policy =
2501 PeerConnectionInterface::kBundlePolicyMaxBundle;
2502 RTCError error;
2503 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2504 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2505
2506 modified_config = config;
2507 modified_config.rtcp_mux_policy =
2508 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2509 error.set_type(RTCErrorType::NONE);
2510 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2511 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2512
2513 modified_config = config;
2514 modified_config.continual_gathering_policy =
2515 PeerConnectionInterface::GATHER_CONTINUALLY;
2516 error.set_type(RTCErrorType::NONE);
2517 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2518 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2519}
2520
2521// Test that SetConfiguration returns a range error if the candidate pool size
2522// is negative or larger than allowed by the spec.
2523TEST_F(PeerConnectionInterfaceTest,
2524 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2525 PeerConnectionInterface::RTCConfiguration config;
2526 CreatePeerConnection(config, nullptr);
2527
2528 config.ice_candidate_pool_size = -1;
2529 RTCError error;
2530 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2531 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2532
2533 config.ice_candidate_pool_size = INT_MAX;
2534 error.set_type(RTCErrorType::NONE);
2535 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2536 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2537}
2538
2539// Test that SetConfiguration returns a syntax error if parsing an ICE server
2540// URL failed.
2541TEST_F(PeerConnectionInterfaceTest,
2542 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2543 PeerConnectionInterface::RTCConfiguration config;
2544 CreatePeerConnection(config, nullptr);
2545
2546 PeerConnectionInterface::IceServer bad_server;
2547 bad_server.uri = "stunn:www.example.com";
2548 config.servers.push_back(bad_server);
2549 RTCError error;
2550 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2551 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2552}
2553
2554// Test that SetConfiguration returns an invalid parameter error if a TURN
2555// IceServer is missing a username or password.
2556TEST_F(PeerConnectionInterfaceTest,
2557 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2558 PeerConnectionInterface::RTCConfiguration config;
2559 CreatePeerConnection(config, nullptr);
2560
2561 PeerConnectionInterface::IceServer bad_server;
2562 bad_server.uri = "turn:www.example.com";
2563 // Missing password.
2564 bad_server.username = "foo";
2565 config.servers.push_back(bad_server);
2566 RTCError error;
2567 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2568 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002569}
2570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002571// Test that PeerConnection::Close changes the states to closed and all remote
2572// tracks change state to ended.
2573TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2574 // Initialize a PeerConnection and negotiate local and remote session
2575 // description.
2576 InitiateCall();
2577 ASSERT_EQ(1u, pc_->local_streams()->count());
2578 ASSERT_EQ(1u, pc_->remote_streams()->count());
2579
2580 pc_->Close();
2581
2582 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2583 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2584 pc_->ice_connection_state());
2585 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2586 pc_->ice_gathering_state());
2587
2588 EXPECT_EQ(1u, pc_->local_streams()->count());
2589 EXPECT_EQ(1u, pc_->remote_streams()->count());
2590
zhihuang9763d562016-08-05 11:14:50 -07002591 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2592 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002593 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002594 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002595 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2596 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2597 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598}
2599
2600// Test that PeerConnection methods fails gracefully after
2601// PeerConnection::Close has been called.
2602TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002603 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002604 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2605 CreateOfferAsRemoteDescription();
2606 CreateAnswerAsLocalDescription();
2607
2608 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002609 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 pc_->local_streams()->at(0);
2611
2612 pc_->Close();
2613
2614 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002615 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002616
2617 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002618 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002619 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002620 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621
2622 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2623
2624 EXPECT_TRUE(pc_->local_description() != NULL);
2625 EXPECT_TRUE(pc_->remote_description() != NULL);
2626
kwibergd1fe2812016-04-27 06:47:29 -07002627 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002628 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002629 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002630 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002631
2632 std::string sdp;
2633 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002634 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2635 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2636 nullptr));
2637 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002638
2639 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002640 std::unique_ptr<SessionDescriptionInterface> local_offer(
2641 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2642 nullptr));
2643 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644}
2645
2646// Test that GetStats can still be called after PeerConnection::Close.
2647TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2648 InitiateCall();
2649 pc_->Close();
2650 DoGetStats(NULL);
2651}
deadbeefab9b2d12015-10-14 11:33:11 -07002652
2653// NOTE: The series of tests below come from what used to be
2654// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2655// setting a remote or local description has the expected effects.
2656
2657// This test verifies that the remote MediaStreams corresponding to a received
2658// SDP string is created. In this test the two separate MediaStreams are
2659// signaled.
2660TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2661 FakeConstraints constraints;
2662 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2663 true);
2664 CreatePeerConnection(&constraints);
2665 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2666
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002667 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002668 EXPECT_TRUE(
2669 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2670 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2671 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2672
2673 // Create a session description based on another SDP with another
2674 // MediaStream.
2675 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2676
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002677 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002678 EXPECT_TRUE(
2679 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2680}
2681
2682// This test verifies that when remote tracks are added/removed from SDP, the
2683// created remote streams are updated appropriately.
2684TEST_F(PeerConnectionInterfaceTest,
2685 AddRemoveTrackFromExistingRemoteMediaStream) {
2686 FakeConstraints constraints;
2687 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2688 true);
2689 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002690 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002691 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002692 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002693 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2694 reference_collection_));
2695
2696 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002697 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002698 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002699 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002700 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2701 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002702 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002703 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2704 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002705 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002706 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2707 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002708
2709 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002710 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002711 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002712 MockTrackObserver audio_track_observer(audio_track2);
2713 MockTrackObserver video_track_observer(video_track2);
2714
2715 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2716 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002717 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002718 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2719 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002720 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002721 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002722 audio_track2->state(), kTimeout);
2723 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2724 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002725}
2726
2727// This tests that remote tracks are ended if a local session description is set
2728// that rejects the media content type.
2729TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2730 FakeConstraints constraints;
2731 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2732 true);
2733 CreatePeerConnection(&constraints);
2734 // First create and set a remote offer, then reject its video content in our
2735 // answer.
2736 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2737 ASSERT_EQ(1u, observer_.remote_streams()->count());
2738 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2739 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2740 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2741
2742 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2743 remote_stream->GetVideoTracks()[0];
2744 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2745 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2746 remote_stream->GetAudioTracks()[0];
2747 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2748
kwibergd1fe2812016-04-27 06:47:29 -07002749 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002750 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002751 cricket::ContentInfo* video_info =
2752 local_answer->description()->GetContentByName("video");
2753 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002754 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002755 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2756 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2757
2758 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002759 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002760 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002761 video_info = local_offer->description()->GetContentByName("video");
2762 ASSERT_TRUE(video_info != nullptr);
2763 video_info->rejected = true;
2764 cricket::ContentInfo* audio_info =
2765 local_offer->description()->GetContentByName("audio");
2766 ASSERT_TRUE(audio_info != nullptr);
2767 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002768 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002769 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002770 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002771 remote_audio->state(), kTimeout);
2772 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2773 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002774}
2775
2776// This tests that we won't crash if the remote track has been removed outside
2777// of PeerConnection and then PeerConnection tries to reject the track.
2778TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2779 FakeConstraints constraints;
2780 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2781 true);
2782 CreatePeerConnection(&constraints);
2783 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2784 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2785 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2786 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2787
kwibergd1fe2812016-04-27 06:47:29 -07002788 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002789 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2790 kSdpStringWithStream1, nullptr));
2791 cricket::ContentInfo* video_info =
2792 local_answer->description()->GetContentByName("video");
2793 video_info->rejected = true;
2794 cricket::ContentInfo* audio_info =
2795 local_answer->description()->GetContentByName("audio");
2796 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002797 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002798
2799 // No crash is a pass.
2800}
2801
deadbeef5e97fb52015-10-15 12:49:08 -07002802// This tests that if a recvonly remote description is set, no remote streams
2803// will be created, even if the description contains SSRCs/MSIDs.
2804// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2805TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2806 FakeConstraints constraints;
2807 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2808 true);
2809 CreatePeerConnection(&constraints);
2810
2811 std::string recvonly_offer = kSdpStringWithStream1;
2812 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2813 strlen(kRecvonly), &recvonly_offer);
2814 CreateAndSetRemoteOffer(recvonly_offer);
2815
2816 EXPECT_EQ(0u, observer_.remote_streams()->count());
2817}
2818
deadbeefab9b2d12015-10-14 11:33:11 -07002819// This tests that a default MediaStream is created if a remote session
2820// description doesn't contain any streams and no MSID support.
2821// It also tests that the default stream is updated if a video m-line is added
2822// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002823TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002824 FakeConstraints constraints;
2825 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2826 true);
2827 CreatePeerConnection(&constraints);
2828 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2829
2830 ASSERT_EQ(1u, observer_.remote_streams()->count());
2831 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2832
2833 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2834 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2835 EXPECT_EQ("default", remote_stream->label());
2836
2837 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2838 ASSERT_EQ(1u, observer_.remote_streams()->count());
2839 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2840 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002841 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2842 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002843 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2844 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002845 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2846 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002847}
2848
2849// This tests that a default MediaStream is created if a remote session
2850// description doesn't contain any streams and media direction is send only.
2851TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002852 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002853 FakeConstraints constraints;
2854 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2855 true);
2856 CreatePeerConnection(&constraints);
2857 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2858
2859 ASSERT_EQ(1u, observer_.remote_streams()->count());
2860 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2861
2862 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2863 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2864 EXPECT_EQ("default", remote_stream->label());
2865}
2866
2867// This tests that it won't crash when PeerConnection tries to remove
2868// a remote track that as already been removed from the MediaStream.
2869TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2870 FakeConstraints constraints;
2871 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2872 true);
2873 CreatePeerConnection(&constraints);
2874 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2875 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2876 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2877 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2878
2879 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2880
2881 // No crash is a pass.
2882}
2883
2884// This tests that a default MediaStream is created if the remote session
2885// description doesn't contain any streams and don't contain an indication if
2886// MSID is supported.
2887TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002888 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002889 FakeConstraints constraints;
2890 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2891 true);
2892 CreatePeerConnection(&constraints);
2893 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2894
2895 ASSERT_EQ(1u, observer_.remote_streams()->count());
2896 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2897 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2898 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2899}
2900
2901// This tests that a default MediaStream is not created if the remote session
2902// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002903TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002904 FakeConstraints constraints;
2905 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2906 true);
2907 CreatePeerConnection(&constraints);
2908 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2909 EXPECT_EQ(0u, observer_.remote_streams()->count());
2910}
2911
deadbeefbda7e0b2015-12-08 17:13:40 -08002912// This tests that when setting a new description, the old default tracks are
2913// not destroyed and recreated.
2914// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002915TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002916 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002917 FakeConstraints constraints;
2918 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2919 true);
2920 CreatePeerConnection(&constraints);
2921 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2922
2923 ASSERT_EQ(1u, observer_.remote_streams()->count());
2924 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2925 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2926
2927 // Set the track to "disabled", then set a new description and ensure the
2928 // track is still disabled, which ensures it hasn't been recreated.
2929 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2930 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2931 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2932 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2933}
2934
deadbeefab9b2d12015-10-14 11:33:11 -07002935// This tests that a default MediaStream is not created if a remote session
2936// description is updated to not have any MediaStreams.
2937TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2938 FakeConstraints constraints;
2939 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2940 true);
2941 CreatePeerConnection(&constraints);
2942 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002943 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002944 EXPECT_TRUE(
2945 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2946
2947 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2948 EXPECT_EQ(0u, observer_.remote_streams()->count());
2949}
2950
2951// This tests that an RtpSender is created when the local description is set
2952// after adding a local stream.
2953// TODO(deadbeef): This test and the one below it need to be updated when
2954// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002955TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002956 FakeConstraints constraints;
2957 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2958 true);
2959 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002960
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002961 // Create an offer with 1 stream with 2 tracks of each type.
2962 rtc::scoped_refptr<StreamCollection> stream_collection =
2963 CreateStreamCollection(1, 2);
2964 pc_->AddStream(stream_collection->at(0));
2965 std::unique_ptr<SessionDescriptionInterface> offer;
2966 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002967 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002968
deadbeefab9b2d12015-10-14 11:33:11 -07002969 auto senders = pc_->GetSenders();
2970 EXPECT_EQ(4u, senders.size());
2971 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2972 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2974 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2975
2976 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002977 pc_->RemoveStream(stream_collection->at(0));
2978 stream_collection = CreateStreamCollection(1, 1);
2979 pc_->AddStream(stream_collection->at(0));
2980 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002981 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002982
deadbeefab9b2d12015-10-14 11:33:11 -07002983 senders = pc_->GetSenders();
2984 EXPECT_EQ(2u, senders.size());
2985 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2986 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2987 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2988 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2989}
2990
2991// This tests that an RtpSender is created when the local description is set
2992// before adding a local stream.
2993TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002994 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002995 FakeConstraints constraints;
2996 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2997 true);
2998 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002999
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003000 rtc::scoped_refptr<StreamCollection> stream_collection =
3001 CreateStreamCollection(1, 2);
3002 // Add a stream to create the offer, but remove it afterwards.
3003 pc_->AddStream(stream_collection->at(0));
3004 std::unique_ptr<SessionDescriptionInterface> offer;
3005 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3006 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003007
Steve Antondb45ca82017-09-11 18:27:34 -07003008 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003009 auto senders = pc_->GetSenders();
3010 EXPECT_EQ(0u, senders.size());
3011
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003012 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003013 senders = pc_->GetSenders();
3014 EXPECT_EQ(4u, senders.size());
3015 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3016 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3017 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3018 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3019}
3020
3021// This tests that the expected behavior occurs if the SSRC on a local track is
3022// changed when SetLocalDescription is called.
3023TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003024 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003025 FakeConstraints constraints;
3026 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3027 true);
3028 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003029
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003030 rtc::scoped_refptr<StreamCollection> stream_collection =
3031 CreateStreamCollection(2, 1);
3032 pc_->AddStream(stream_collection->at(0));
3033 std::unique_ptr<SessionDescriptionInterface> offer;
3034 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3035 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003036 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3037 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003038 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3039 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003040 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003041
deadbeefab9b2d12015-10-14 11:33:11 -07003042 auto senders = pc_->GetSenders();
3043 EXPECT_EQ(2u, senders.size());
3044 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3045 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3046
3047 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003048 cricket::MediaContentDescription* desc =
3049 cricket::GetFirstAudioContentDescription(modified_offer->description());
3050 ASSERT_TRUE(desc != NULL);
3051 for (StreamParams& stream : desc->mutable_streams()) {
3052 for (unsigned int& ssrc : stream.ssrcs) {
3053 ++ssrc;
3054 }
3055 }
deadbeefab9b2d12015-10-14 11:33:11 -07003056
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003057 desc =
3058 cricket::GetFirstVideoContentDescription(modified_offer->description());
3059 ASSERT_TRUE(desc != NULL);
3060 for (StreamParams& stream : desc->mutable_streams()) {
3061 for (unsigned int& ssrc : stream.ssrcs) {
3062 ++ssrc;
3063 }
3064 }
3065
Steve Antondb45ca82017-09-11 18:27:34 -07003066 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003067 senders = pc_->GetSenders();
3068 EXPECT_EQ(2u, senders.size());
3069 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3070 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3071 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3072 // changed.
3073}
3074
3075// This tests that the expected behavior occurs if a new session description is
3076// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003077TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003078 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003079 FakeConstraints constraints;
3080 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3081 true);
3082 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003083
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003084 rtc::scoped_refptr<StreamCollection> stream_collection =
3085 CreateStreamCollection(2, 1);
3086 pc_->AddStream(stream_collection->at(0));
3087 std::unique_ptr<SessionDescriptionInterface> offer;
3088 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003089 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003090
deadbeefab9b2d12015-10-14 11:33:11 -07003091 auto senders = pc_->GetSenders();
3092 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003093 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3094 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003095
3096 // Add a new MediaStream but with the same tracks as in the first stream.
3097 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3098 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003099 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3100 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003101 pc_->AddStream(stream_1);
3102
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003103 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003104 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003105
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003106 auto new_senders = pc_->GetSenders();
3107 // Should be the same senders as before, but with updated stream id.
3108 // Note that this behavior is subject to change in the future.
3109 // We may decide the PC should ignore existing tracks in AddStream.
3110 EXPECT_EQ(senders, new_senders);
3111 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3112 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003113}
3114
zhihuang81c3a032016-11-17 12:06:24 -08003115// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3116TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3117 FakeConstraints constraints;
3118 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3119 true);
3120 CreatePeerConnection(&constraints);
3121 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3122 EXPECT_EQ(observer_.num_added_tracks_, 1);
3123 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3124
3125 // Create and set the updated remote SDP.
3126 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3127 EXPECT_EQ(observer_.num_added_tracks_, 2);
3128 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3129}
3130
deadbeefd1a38b52016-12-10 13:15:33 -08003131// Test that when SetConfiguration is called and the configuration is
3132// changing, the next offer causes an ICE restart.
3133TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3134 PeerConnectionInterface::RTCConfiguration config;
3135 config.type = PeerConnectionInterface::kRelay;
3136 // Need to pass default constraints to prevent disabling of DTLS...
3137 FakeConstraints default_constraints;
3138 CreatePeerConnection(config, &default_constraints);
3139 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3140
3141 // Do initial offer/answer so there's something to restart.
3142 CreateOfferAsLocalDescription();
3143 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3144
3145 // Grab the ufrags.
3146 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3147
3148 // Change ICE policy, which should trigger an ICE restart on the next offer.
3149 config.type = PeerConnectionInterface::kAll;
3150 EXPECT_TRUE(pc_->SetConfiguration(config));
3151 CreateOfferAsLocalDescription();
3152
3153 // Grab the new ufrags.
3154 std::vector<std::string> subsequent_ufrags =
3155 GetUfrags(pc_->local_description());
3156
3157 // Sanity check.
3158 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3159 // Check that each ufrag is different.
3160 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3161 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3162 }
3163}
3164
3165// Test that when SetConfiguration is called and the configuration *isn't*
3166// changing, the next offer does *not* cause an ICE restart.
3167TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3168 PeerConnectionInterface::RTCConfiguration config;
3169 config.type = PeerConnectionInterface::kRelay;
3170 // Need to pass default constraints to prevent disabling of DTLS...
3171 FakeConstraints default_constraints;
3172 CreatePeerConnection(config, &default_constraints);
3173 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3174
3175 // Do initial offer/answer so there's something to restart.
3176 CreateOfferAsLocalDescription();
3177 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3178
3179 // Grab the ufrags.
3180 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3181
3182 // Call SetConfiguration with a config identical to what the PC was
3183 // constructed with.
3184 EXPECT_TRUE(pc_->SetConfiguration(config));
3185 CreateOfferAsLocalDescription();
3186
3187 // Grab the new ufrags.
3188 std::vector<std::string> subsequent_ufrags =
3189 GetUfrags(pc_->local_description());
3190
3191 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3192}
3193
3194// Test for a weird corner case scenario:
3195// 1. Audio/video session established.
3196// 2. SetConfiguration changes ICE config; ICE restart needed.
3197// 3. ICE restart initiated by remote peer, but only for one m= section.
3198// 4. Next createOffer should initiate an ICE restart, but only for the other
3199// m= section; it would be pointless to do an ICE restart for the m= section
3200// that was already restarted.
3201TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3202 PeerConnectionInterface::RTCConfiguration config;
3203 config.type = PeerConnectionInterface::kRelay;
3204 // Need to pass default constraints to prevent disabling of DTLS...
3205 FakeConstraints default_constraints;
3206 CreatePeerConnection(config, &default_constraints);
3207 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3208
3209 // Do initial offer/answer so there's something to restart.
3210 CreateOfferAsLocalDescription();
3211 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3212
3213 // Change ICE policy, which should set the "needs-ice-restart" flag.
3214 config.type = PeerConnectionInterface::kAll;
3215 EXPECT_TRUE(pc_->SetConfiguration(config));
3216
3217 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003218 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3219 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3220 kSdpStringWithStream1, nullptr));
3221 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003222 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3223 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003224 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003225 CreateAnswerAsLocalDescription();
3226
3227 // Grab the ufrags.
3228 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3229 ASSERT_EQ(2, initial_ufrags.size());
3230
3231 // Create offer and grab the new ufrags.
3232 CreateOfferAsLocalDescription();
3233 std::vector<std::string> subsequent_ufrags =
3234 GetUfrags(pc_->local_description());
3235 ASSERT_EQ(2, subsequent_ufrags.size());
3236
3237 // Ensure that only the ufrag for the second m= section changed.
3238 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3239 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3240}
3241
deadbeeffe4a8a42016-12-20 17:56:17 -08003242// Tests that the methods to return current/pending descriptions work as
3243// expected at different points in the offer/answer exchange. This test does
3244// one offer/answer exchange as the offerer, then another as the answerer.
3245TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3246 // This disables DTLS so we can apply an answer to ourselves.
3247 CreatePeerConnection();
3248
3249 // Create initial local offer and get SDP (which will also be used as
3250 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003251 std::unique_ptr<SessionDescriptionInterface> local_offer;
3252 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003253 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003254 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003255
3256 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003257 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3258 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3259 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003260 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3261 EXPECT_EQ(nullptr, pc_->current_local_description());
3262 EXPECT_EQ(nullptr, pc_->current_remote_description());
3263
3264 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003265 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003266 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003267 sdp, nullptr));
3268 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3269 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3270 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3271 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003272 EXPECT_EQ(nullptr, pc_->current_local_description());
3273 EXPECT_EQ(nullptr, pc_->current_remote_description());
3274
3275 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003276 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3277 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3278 sdp, nullptr));
3279 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3280 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003281 EXPECT_EQ(nullptr, pc_->pending_local_description());
3282 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003283 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3284 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003285
3286 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003287 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3288 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3289 nullptr));
3290 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3291 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3292 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003293 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003294 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3295 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003296
3297 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003298 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003299 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003300 sdp, nullptr));
3301 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3302 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3303 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3304 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3305 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3306 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003307
3308 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003309 std::unique_ptr<SessionDescriptionInterface> local_answer(
3310 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3311 sdp, nullptr));
3312 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3313 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003314 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3315 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003316 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3317 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003318}
3319
zhihuang77985012017-02-07 15:45:16 -08003320// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3321// after the PeerConnection is closed.
3322TEST_F(PeerConnectionInterfaceTest,
3323 StartAndStopLoggingAfterPeerConnectionClosed) {
3324 CreatePeerConnection();
3325 // The RtcEventLog will be reset when the PeerConnection is closed.
3326 pc_->Close();
3327
3328 rtc::PlatformFile file = 0;
3329 int64_t max_size_bytes = 1024;
3330 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3331 pc_->StopRtcEventLog();
3332}
3333
deadbeef30952b42017-04-21 02:41:29 -07003334// Test that generated offers/answers include "ice-option:trickle".
3335TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3336 CreatePeerConnection();
3337
3338 // First, create an offer with audio/video.
3339 FakeConstraints constraints;
3340 constraints.SetMandatoryReceiveAudio(true);
3341 constraints.SetMandatoryReceiveVideo(true);
3342 std::unique_ptr<SessionDescriptionInterface> offer;
3343 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3344 cricket::SessionDescription* desc = offer->description();
3345 ASSERT_EQ(2u, desc->transport_infos().size());
3346 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3347 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3348
3349 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003350 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003351 std::unique_ptr<SessionDescriptionInterface> answer;
3352 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3353 desc = answer->description();
3354 ASSERT_EQ(2u, desc->transport_infos().size());
3355 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3356 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3357}
3358
deadbeef1dcb1642017-03-29 21:08:16 -07003359// Test that ICE renomination isn't offered if it's not enabled in the PC's
3360// RTCConfiguration.
3361TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3362 PeerConnectionInterface::RTCConfiguration config;
3363 config.enable_ice_renomination = false;
3364 CreatePeerConnection(config, nullptr);
3365 AddVoiceStream("foo");
3366
3367 std::unique_ptr<SessionDescriptionInterface> offer;
3368 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3369 cricket::SessionDescription* desc = offer->description();
3370 EXPECT_EQ(1u, desc->transport_infos().size());
3371 EXPECT_FALSE(
3372 desc->transport_infos()[0].description.GetIceParameters().renomination);
3373}
3374
3375// Test that the ICE renomination option is present in generated offers/answers
3376// if it's enabled in the PC's RTCConfiguration.
3377TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3378 PeerConnectionInterface::RTCConfiguration config;
3379 config.enable_ice_renomination = true;
3380 CreatePeerConnection(config, nullptr);
3381 AddVoiceStream("foo");
3382
3383 std::unique_ptr<SessionDescriptionInterface> offer;
3384 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3385 cricket::SessionDescription* desc = offer->description();
3386 EXPECT_EQ(1u, desc->transport_infos().size());
3387 EXPECT_TRUE(
3388 desc->transport_infos()[0].description.GetIceParameters().renomination);
3389
3390 // Set the offer as a remote description, then create an answer and ensure it
3391 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003392 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003393 std::unique_ptr<SessionDescriptionInterface> answer;
3394 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3395 desc = answer->description();
3396 EXPECT_EQ(1u, desc->transport_infos().size());
3397 EXPECT_TRUE(
3398 desc->transport_infos()[0].description.GetIceParameters().renomination);
3399}
3400
3401// Test that if CreateOffer is called with the deprecated "offer to receive
3402// audio/video" constraints, they're processed and result in an offer with
3403// audio/video sections just as if RTCOfferAnswerOptions had been used.
3404TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3405 CreatePeerConnection();
3406
3407 FakeConstraints constraints;
3408 constraints.SetMandatoryReceiveAudio(true);
3409 constraints.SetMandatoryReceiveVideo(true);
3410 std::unique_ptr<SessionDescriptionInterface> offer;
3411 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3412
3413 cricket::SessionDescription* desc = offer->description();
3414 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3415 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3416 ASSERT_NE(nullptr, audio);
3417 ASSERT_NE(nullptr, video);
3418 EXPECT_FALSE(audio->rejected);
3419 EXPECT_FALSE(video->rejected);
3420}
3421
3422// Test that if CreateAnswer is called with the deprecated "offer to receive
3423// audio/video" constraints, they're processed and can be used to reject an
3424// offered m= section just as can be done with RTCOfferAnswerOptions;
3425TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3426 CreatePeerConnection();
3427
3428 // First, create an offer with audio/video and apply it as a remote
3429 // description.
3430 FakeConstraints constraints;
3431 constraints.SetMandatoryReceiveAudio(true);
3432 constraints.SetMandatoryReceiveVideo(true);
3433 std::unique_ptr<SessionDescriptionInterface> offer;
3434 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003435 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003436
3437 // Now create answer that rejects audio/video.
3438 constraints.SetMandatoryReceiveAudio(false);
3439 constraints.SetMandatoryReceiveVideo(false);
3440 std::unique_ptr<SessionDescriptionInterface> answer;
3441 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3442
3443 cricket::SessionDescription* desc = answer->description();
3444 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3445 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3446 ASSERT_NE(nullptr, audio);
3447 ASSERT_NE(nullptr, video);
3448 EXPECT_TRUE(audio->rejected);
3449 EXPECT_TRUE(video->rejected);
3450}
3451
3452#ifdef HAVE_SCTP
3453#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3454 DataChannelOnlyOfferWithMaxBundlePolicy
3455#else
3456#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3457 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3458#endif
3459
3460// Test that negotiation can succeed with a data channel only, and with the max
3461// bundle policy. Previously there was a bug that prevented this.
3462TEST_F(PeerConnectionInterfaceTest,
3463 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3464 PeerConnectionInterface::RTCConfiguration config;
3465 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3466 CreatePeerConnection(config, nullptr);
3467
3468 // First, create an offer with only a data channel and apply it as a remote
3469 // description.
3470 pc_->CreateDataChannel("test", nullptr);
3471 std::unique_ptr<SessionDescriptionInterface> offer;
3472 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003473 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003474
3475 // Create and set answer as well.
3476 std::unique_ptr<SessionDescriptionInterface> answer;
3477 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003478 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003479}
3480
zstein4b979802017-06-02 14:37:37 -07003481TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3482 CreatePeerConnection();
3483 PeerConnectionInterface::BitrateParameters bitrate;
3484 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3485 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3486}
3487
3488TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3489 CreatePeerConnection();
3490 PeerConnectionInterface::BitrateParameters bitrate;
3491 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3492 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3493}
3494
3495TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3496 CreatePeerConnection();
3497 PeerConnectionInterface::BitrateParameters bitrate;
3498 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3499 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3500 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3501}
3502
3503TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3504 CreatePeerConnection();
3505 PeerConnectionInterface::BitrateParameters bitrate;
3506 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3507 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3508}
3509
3510TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3511 CreatePeerConnection();
3512 PeerConnectionInterface::BitrateParameters bitrate;
3513 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3514 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3515 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3516}
3517
3518TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3519 CreatePeerConnection();
3520 PeerConnectionInterface::BitrateParameters bitrate;
3521 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3522 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3523 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3524}
3525
3526TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3527 CreatePeerConnection();
3528 PeerConnectionInterface::BitrateParameters bitrate;
3529 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3530 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3531}
3532
Steve Anton038834f2017-07-14 15:59:59 -07003533// ice_regather_interval_range requires WebRTC to be configured for continual
3534// gathering already.
3535TEST_F(PeerConnectionInterfaceTest,
3536 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3537 PeerConnectionInterface::RTCConfiguration config;
3538 config.ice_regather_interval_range.emplace(1000, 2000);
3539 config.continual_gathering_policy =
3540 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3541 CreatePeerConnectionExpectFail(config);
3542}
3543
3544// Ensures that there is no error when ice_regather_interval_range is set with
3545// continual gathering enabled.
3546TEST_F(PeerConnectionInterfaceTest,
3547 SetIceRegatherIntervalRangeWithContinualGathering) {
3548 PeerConnectionInterface::RTCConfiguration config;
3549 config.ice_regather_interval_range.emplace(1000, 2000);
3550 config.continual_gathering_policy =
3551 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3552 CreatePeerConnection(config, nullptr);
3553}
3554
zstein4b979802017-06-02 14:37:37 -07003555// The current bitrate from Call's BitrateConfigMask is currently clamped by
3556// Call's BitrateConfig, which comes from the SDP or a default value. This test
3557// checks that a call to SetBitrate with a current bitrate that will be clamped
3558// succeeds.
3559TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3560 CreatePeerConnection();
3561 PeerConnectionInterface::BitrateParameters bitrate;
3562 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3563 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3564}
3565
zhihuang1c378ed2017-08-17 14:10:50 -07003566// The following tests verify that the offer can be created correctly.
3567TEST_F(PeerConnectionInterfaceTest,
3568 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3569 RTCOfferAnswerOptions rtc_options;
3570
3571 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3572 // than kMaxOfferToReceiveMedia should be treated as invalid.
3573 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3574 CreatePeerConnection();
3575 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3576
3577 rtc_options.offer_to_receive_audio =
3578 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3579 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3580}
3581
3582TEST_F(PeerConnectionInterfaceTest,
3583 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3584 RTCOfferAnswerOptions rtc_options;
3585
3586 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3587 // than kMaxOfferToReceiveMedia should be treated as invalid.
3588 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3589 CreatePeerConnection();
3590 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3591
3592 rtc_options.offer_to_receive_video =
3593 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3594 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3595}
3596
3597// Test that the audio and video content will be added to an offer if both
3598// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3599TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3600 RTCOfferAnswerOptions rtc_options;
3601 rtc_options.offer_to_receive_audio = 1;
3602 rtc_options.offer_to_receive_video = 1;
3603
3604 std::unique_ptr<SessionDescriptionInterface> offer;
3605 CreatePeerConnection();
3606 offer = CreateOfferWithOptions(rtc_options);
3607 ASSERT_TRUE(offer);
3608 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3609 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3610}
3611
3612// Test that only audio content will be added to the offer if only
3613// |offer_to_receive_audio| options is 1.
3614TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3615 RTCOfferAnswerOptions rtc_options;
3616 rtc_options.offer_to_receive_audio = 1;
3617 rtc_options.offer_to_receive_video = 0;
3618
3619 std::unique_ptr<SessionDescriptionInterface> offer;
3620 CreatePeerConnection();
3621 offer = CreateOfferWithOptions(rtc_options);
3622 ASSERT_TRUE(offer);
3623 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3624 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3625}
3626
3627// Test that only video content will be added if only |offer_to_receive_video|
3628// options is 1.
3629TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3630 RTCOfferAnswerOptions rtc_options;
3631 rtc_options.offer_to_receive_audio = 0;
3632 rtc_options.offer_to_receive_video = 1;
3633
3634 std::unique_ptr<SessionDescriptionInterface> offer;
3635 CreatePeerConnection();
3636 offer = CreateOfferWithOptions(rtc_options);
3637 ASSERT_TRUE(offer);
3638 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3639 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3640}
3641
3642// Test that if |voice_activity_detection| is false, no CN codec is added to the
3643// offer.
3644TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3645 RTCOfferAnswerOptions rtc_options;
3646 rtc_options.offer_to_receive_audio = 1;
3647 rtc_options.offer_to_receive_video = 0;
3648
3649 std::unique_ptr<SessionDescriptionInterface> offer;
3650 CreatePeerConnection();
3651 offer = CreateOfferWithOptions(rtc_options);
3652 ASSERT_TRUE(offer);
3653 const cricket::ContentInfo* audio_content =
3654 offer->description()->GetContentByName(cricket::CN_AUDIO);
3655 ASSERT_TRUE(audio_content);
3656 // |voice_activity_detection| is true by default.
3657 EXPECT_TRUE(HasCNCodecs(audio_content));
3658
3659 rtc_options.voice_activity_detection = false;
3660 CreatePeerConnection();
3661 offer = CreateOfferWithOptions(rtc_options);
3662 ASSERT_TRUE(offer);
3663 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3664 ASSERT_TRUE(audio_content);
3665 EXPECT_FALSE(HasCNCodecs(audio_content));
3666}
3667
3668// Test that no media content will be added to the offer if using default
3669// RTCOfferAnswerOptions.
3670TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3671 RTCOfferAnswerOptions rtc_options;
3672
3673 std::unique_ptr<SessionDescriptionInterface> offer;
3674 CreatePeerConnection();
3675 offer = CreateOfferWithOptions(rtc_options);
3676 ASSERT_TRUE(offer);
3677 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3678 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3679}
3680
3681// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3682// ufrag/pwd will be the same in the new offer.
3683TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3684 RTCOfferAnswerOptions rtc_options;
3685 rtc_options.ice_restart = false;
3686 rtc_options.offer_to_receive_audio = 1;
3687
3688 std::unique_ptr<SessionDescriptionInterface> offer;
3689 CreatePeerConnection();
3690 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3691 auto ufrag1 = offer->description()
3692 ->GetTransportInfoByName(cricket::CN_AUDIO)
3693 ->description.ice_ufrag;
3694 auto pwd1 = offer->description()
3695 ->GetTransportInfoByName(cricket::CN_AUDIO)
3696 ->description.ice_pwd;
3697
3698 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3699 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3700 auto ufrag2 = offer->description()
3701 ->GetTransportInfoByName(cricket::CN_AUDIO)
3702 ->description.ice_ufrag;
3703 auto pwd2 = offer->description()
3704 ->GetTransportInfoByName(cricket::CN_AUDIO)
3705 ->description.ice_pwd;
3706
3707 // |ice_restart| is true, the ufrag/pwd should change.
3708 rtc_options.ice_restart = true;
3709 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3710 auto ufrag3 = offer->description()
3711 ->GetTransportInfoByName(cricket::CN_AUDIO)
3712 ->description.ice_ufrag;
3713 auto pwd3 = offer->description()
3714 ->GetTransportInfoByName(cricket::CN_AUDIO)
3715 ->description.ice_pwd;
3716
3717 EXPECT_EQ(ufrag1, ufrag2);
3718 EXPECT_EQ(pwd1, pwd2);
3719 EXPECT_NE(ufrag2, ufrag3);
3720 EXPECT_NE(pwd2, pwd3);
3721}
3722
3723// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3724// offer; if it is false, there won't be any bundle group in the offer.
3725TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3726 RTCOfferAnswerOptions rtc_options;
3727 rtc_options.offer_to_receive_audio = 1;
3728 rtc_options.offer_to_receive_video = 1;
3729
3730 std::unique_ptr<SessionDescriptionInterface> offer;
3731 CreatePeerConnection();
3732
3733 rtc_options.use_rtp_mux = true;
3734 offer = CreateOfferWithOptions(rtc_options);
3735 ASSERT_TRUE(offer);
3736 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3737 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3738 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3739
3740 rtc_options.use_rtp_mux = false;
3741 offer = CreateOfferWithOptions(rtc_options);
3742 ASSERT_TRUE(offer);
3743 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3744 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3745 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3746}
3747
3748// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3749// called for the answer constraints, but an audio and a video section were
3750// offered, there will still be an audio and a video section in the answer.
3751TEST_F(PeerConnectionInterfaceTest,
3752 RejectAudioAndVideoInAnswerWithConstraints) {
3753 // Offer both audio and video.
3754 RTCOfferAnswerOptions rtc_offer_options;
3755 rtc_offer_options.offer_to_receive_audio = 1;
3756 rtc_offer_options.offer_to_receive_video = 1;
3757
3758 CreatePeerConnection();
3759 std::unique_ptr<SessionDescriptionInterface> offer;
3760 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3761 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3762 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3763
3764 // Since an offer has been created with both audio and video,
3765 // Answers will contain the media types that exist in the offer regardless of
3766 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3767 FakeConstraints answer_c;
3768 // Reject both audio and video.
3769 answer_c.SetMandatoryReceiveAudio(false);
3770 answer_c.SetMandatoryReceiveVideo(false);
3771
3772 std::unique_ptr<SessionDescriptionInterface> answer;
3773 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3774 const cricket::ContentInfo* audio_content =
3775 GetFirstAudioContent(answer->description());
3776 const cricket::ContentInfo* video_content =
3777 GetFirstVideoContent(answer->description());
3778 ASSERT_NE(nullptr, audio_content);
3779 ASSERT_NE(nullptr, video_content);
3780 EXPECT_TRUE(audio_content->rejected);
3781 EXPECT_TRUE(video_content->rejected);
3782}
3783
zhihuang141aacb2017-08-29 13:23:53 -07003784// This test ensures OnRenegotiationNeeded is called when we add track with
3785// MediaStream -> AddTrack in the same way it is called when we add track with
3786// PeerConnection -> AddTrack.
3787// The test can be removed once addStream is rewritten in terms of addTrack
3788// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3789TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3790 CreatePeerConnectionWithoutDtls();
3791 rtc::scoped_refptr<MediaStreamInterface> stream(
3792 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3793 pc_->AddStream(stream);
3794 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3795 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3796 rtc::scoped_refptr<VideoTrackInterface> video_track(
3797 pc_factory_->CreateVideoTrack(
3798 "video_track", pc_factory_->CreateVideoSource(
3799 std::unique_ptr<cricket::VideoCapturer>(
3800 new cricket::FakeVideoCapturer()))));
3801 stream->AddTrack(audio_track);
3802 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3803 observer_.renegotiation_needed_ = false;
3804
3805 stream->AddTrack(video_track);
3806 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3807 observer_.renegotiation_needed_ = false;
3808
3809 stream->RemoveTrack(audio_track);
3810 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3811 observer_.renegotiation_needed_ = false;
3812
3813 stream->RemoveTrack(video_track);
3814 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3815 observer_.renegotiation_needed_ = false;
3816}
3817
3818// Tests that creating answer would fail gracefully without being crashed if the
3819// remote description is unset.
3820TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3821 CreatePeerConnection();
3822 // Creating answer fails because the remote description is unset.
3823 std::unique_ptr<SessionDescriptionInterface> answer;
3824 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3825
3826 // Createing answer succeeds when the remote description is set.
3827 CreateOfferAsRemoteDescription();
3828 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3829}
3830
Zhi Huang2a5e4262017-09-14 01:15:03 -07003831// Test that an error is returned if a description is applied that doesn't
3832// respect the order of existing media sections.
3833TEST_F(PeerConnectionInterfaceTest,
3834 MediaSectionOrderEnforcedForSubsequentOffers) {
3835 CreatePeerConnection();
3836 FakeConstraints constraints;
3837 constraints.SetMandatoryReceiveAudio(true);
3838 constraints.SetMandatoryReceiveVideo(true);
3839 std::unique_ptr<SessionDescriptionInterface> offer;
3840 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3841 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3842
3843 std::unique_ptr<SessionDescriptionInterface> answer;
3844 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3845 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3846
3847 // A remote offer with different m=line order should be rejected.
3848 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3849 std::reverse(offer->description()->contents().begin(),
3850 offer->description()->contents().end());
3851 std::reverse(offer->description()->transport_infos().begin(),
3852 offer->description()->transport_infos().end());
3853 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3854
3855 // A subsequent local offer with different m=line order should be rejected.
3856 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3857 std::reverse(offer->description()->contents().begin(),
3858 offer->description()->contents().end());
3859 std::reverse(offer->description()->transport_infos().begin(),
3860 offer->description()->transport_infos().end());
3861 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3862}
3863
nisse51542be2016-02-12 02:27:06 -08003864class PeerConnectionMediaConfigTest : public testing::Test {
3865 protected:
3866 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003867 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003868 pcf_->Initialize();
3869 }
nisseeaabdf62017-05-05 02:23:02 -07003870 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003871 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003872 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003873 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3874 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003875 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003876 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003877 }
3878
zhihuang9763d562016-08-05 11:14:50 -07003879 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003880 MockPeerConnectionObserver observer_;
3881};
3882
3883// This test verifies the default behaviour with no constraints and a
3884// default RTCConfiguration.
3885TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3886 PeerConnectionInterface::RTCConfiguration config;
3887 FakeConstraints constraints;
3888
3889 const cricket::MediaConfig& media_config =
3890 TestCreatePeerConnection(config, &constraints);
3891
3892 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003893 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3894 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3895 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003896}
3897
3898// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003899// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003900TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3901 PeerConnectionInterface::RTCConfiguration config;
3902 FakeConstraints constraints;
3903
3904 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3905 const cricket::MediaConfig& media_config =
3906 TestCreatePeerConnection(config, &constraints);
3907
3908 EXPECT_TRUE(media_config.enable_dscp);
3909}
3910
3911// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003912// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003913TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3914 PeerConnectionInterface::RTCConfiguration config;
3915 FakeConstraints constraints;
3916
3917 constraints.AddOptional(
3918 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3919 const cricket::MediaConfig media_config =
3920 TestCreatePeerConnection(config, &constraints);
3921
nisse0db023a2016-03-01 04:29:59 -08003922 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003923}
3924
3925// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003926// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003927TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3928 PeerConnectionInterface::RTCConfiguration config;
3929 FakeConstraints constraints;
3930
Niels Möller71bdda02016-03-31 12:59:59 +02003931 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003932 const cricket::MediaConfig& media_config =
3933 TestCreatePeerConnection(config, &constraints);
3934
nisse0db023a2016-03-01 04:29:59 -08003935 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3936}
3937
3938// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003939// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003940TEST_F(PeerConnectionMediaConfigTest,
3941 TestSuspendBelowMinBitrateConstraintTrue) {
3942 PeerConnectionInterface::RTCConfiguration config;
3943 FakeConstraints constraints;
3944
3945 constraints.AddOptional(
3946 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3947 true);
3948 const cricket::MediaConfig media_config =
3949 TestCreatePeerConnection(config, &constraints);
3950
3951 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003952}
3953
deadbeef293e9262017-01-11 12:28:30 -08003954// Tests a few random fields being different.
3955TEST(RTCConfigurationTest, ComparisonOperators) {
3956 PeerConnectionInterface::RTCConfiguration a;
3957 PeerConnectionInterface::RTCConfiguration b;
3958 EXPECT_EQ(a, b);
3959
3960 PeerConnectionInterface::RTCConfiguration c;
3961 c.servers.push_back(PeerConnectionInterface::IceServer());
3962 EXPECT_NE(a, c);
3963
3964 PeerConnectionInterface::RTCConfiguration d;
3965 d.type = PeerConnectionInterface::kRelay;
3966 EXPECT_NE(a, d);
3967
3968 PeerConnectionInterface::RTCConfiguration e;
3969 e.audio_jitter_buffer_max_packets = 5;
3970 EXPECT_NE(a, e);
3971
3972 PeerConnectionInterface::RTCConfiguration f;
3973 f.ice_connection_receiving_timeout = 1337;
3974 EXPECT_NE(a, f);
3975
3976 PeerConnectionInterface::RTCConfiguration g;
3977 g.disable_ipv6 = true;
3978 EXPECT_NE(a, g);
3979
3980 PeerConnectionInterface::RTCConfiguration h(
3981 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3982 EXPECT_NE(a, h);
3983}