blob: fa37348c91dade687b7be7846972bd1b500a6864 [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"
Elad Alon99c3fe52017-10-13 16:29:40 +020025#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakevideocapturer.h"
27#include "media/engine/webrtcmediaengine.h"
28#include "media/sctp/sctptransportinternal.h"
29#include "modules/audio_processing/include/audio_processing.h"
30#include "p2p/base/fakeportallocator.h"
31#include "pc/audiotrack.h"
32#include "pc/mediasession.h"
33#include "pc/mediastream.h"
34#include "pc/peerconnection.h"
Steve Anton57858b32018-02-15 15:19:50 -080035#include "pc/rtpsender.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "pc/streamcollection.h"
37#include "pc/test/fakeaudiocapturemodule.h"
38#include "pc/test/fakertccertificategenerator.h"
39#include "pc/test/fakevideotracksource.h"
40#include "pc/test/mockpeerconnectionobservers.h"
41#include "pc/test/testsdpstrings.h"
42#include "pc/videocapturertracksource.h"
43#include "pc/videotrack.h"
44#include "rtc_base/gunit.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020045#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/virtualsocketserver.h"
48#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020049#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070050
51#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070053#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55static const char kStreamLabel1[] = "local_stream_1";
56static const char kStreamLabel2[] = "local_stream_2";
57static const char kStreamLabel3[] = "local_stream_3";
58static const int kDefaultStunPort = 3478;
59static const char kStunAddressOnly[] = "stun:address";
60static const char kStunInvalidPort[] = "stun:address:-1";
61static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
62static const char kStunAddressPortAndMore2[] = "stun:address:port more";
63static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
64static const char kTurnUsername[] = "user";
65static const char kTurnPassword[] = "password";
66static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020067static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
deadbeefab9b2d12015-10-14 11:33:11 -070069static const char kStreams[][8] = {"stream1", "stream2"};
70static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
71static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
72
deadbeef5e97fb52015-10-15 12:49:08 -070073static const char kRecvonly[] = "recvonly";
74static const char kSendrecv[] = "sendrecv";
75
deadbeefab9b2d12015-10-14 11:33:11 -070076// Reference SDP with a MediaStream with label "stream1" and audio track with
77// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -080078static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -070079 "v=0\r\n"
80 "o=- 0 0 IN IP4 127.0.0.1\r\n"
81 "s=-\r\n"
82 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080083 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=ice-ufrag:e5785931\r\n"
85 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
86 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
87 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070088 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070089 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080090 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070091 "a=rtpmap:103 ISAC/16000\r\n"
92 "a=ssrc:1 cname:stream1\r\n"
93 "a=ssrc:1 mslabel:stream1\r\n"
94 "a=ssrc:1 label:audiotrack0\r\n"
95 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080096 "a=ice-ufrag:e5785931\r\n"
97 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
98 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
99 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700100 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700101 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800102 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700103 "a=rtpmap:120 VP8/90000\r\n"
104 "a=ssrc:2 cname:stream1\r\n"
105 "a=ssrc:2 mslabel:stream1\r\n"
106 "a=ssrc:2 label:videotrack0\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800107// Same string as above but with the MID changed to the Unified Plan default.
108// This is needed so that this SDP can be used as an answer for a Unified Plan
109// offer.
110static const char kSdpStringWithStream1UnifiedPlan[] =
111 "v=0\r\n"
112 "o=- 0 0 IN IP4 127.0.0.1\r\n"
113 "s=-\r\n"
114 "t=0 0\r\n"
115 "m=audio 1 RTP/AVPF 103\r\n"
116 "a=ice-ufrag:e5785931\r\n"
117 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
118 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
119 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
120 "a=mid:0\r\n"
121 "a=sendrecv\r\n"
122 "a=rtcp-mux\r\n"
123 "a=rtpmap:103 ISAC/16000\r\n"
124 "a=ssrc:1 cname:stream1\r\n"
125 "a=ssrc:1 mslabel:stream1\r\n"
126 "a=ssrc:1 label:audiotrack0\r\n"
127 "m=video 1 RTP/AVPF 120\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "a=mid:1\r\n"
133 "a=sendrecv\r\n"
134 "a=rtcp-mux\r\n"
135 "a=rtpmap:120 VP8/90000\r\n"
136 "a=ssrc:2 cname:stream1\r\n"
137 "a=ssrc:2 mslabel:stream1\r\n"
138 "a=ssrc:2 label:videotrack0\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700139
zhihuang81c3a032016-11-17 12:06:24 -0800140// Reference SDP with a MediaStream with label "stream1" and audio track with
141// id "audio_1";
142static const char kSdpStringWithStream1AudioTrackOnly[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800147 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800148 "a=ice-ufrag:e5785931\r\n"
149 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
150 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
151 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800152 "a=mid:audio\r\n"
153 "a=sendrecv\r\n"
154 "a=rtpmap:103 ISAC/16000\r\n"
155 "a=ssrc:1 cname:stream1\r\n"
156 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800157 "a=ssrc:1 label:audiotrack0\r\n"
158 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800159
deadbeefab9b2d12015-10-14 11:33:11 -0700160// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
161// MediaStreams have one audio track and one video track.
162// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800163static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700164 "v=0\r\n"
165 "o=- 0 0 IN IP4 127.0.0.1\r\n"
166 "s=-\r\n"
167 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800168 "a=msid-semantic: WMS stream1 stream2\r\n"
169 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=ice-ufrag:e5785931\r\n"
171 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
172 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
173 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800176 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=rtpmap:103 ISAC/16000\r\n"
178 "a=ssrc:1 cname:stream1\r\n"
179 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
180 "a=ssrc:3 cname:stream2\r\n"
181 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
182 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800183 "a=ice-ufrag:e5785931\r\n"
184 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
185 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
186 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700187 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800189 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700190 "a=rtpmap:120 VP8/0\r\n"
191 "a=ssrc:2 cname:stream1\r\n"
192 "a=ssrc:2 msid:stream1 videotrack0\r\n"
193 "a=ssrc:4 cname:stream2\r\n"
194 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800195static const char kSdpStringWithStream1And2UnifiedPlan[] =
196 "v=0\r\n"
197 "o=- 0 0 IN IP4 127.0.0.1\r\n"
198 "s=-\r\n"
199 "t=0 0\r\n"
200 "a=msid-semantic: WMS stream1 stream2\r\n"
201 "m=audio 1 RTP/AVPF 103\r\n"
202 "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"
206 "a=mid:0\r\n"
207 "a=sendrecv\r\n"
208 "a=rtcp-mux\r\n"
209 "a=rtpmap:103 ISAC/16000\r\n"
210 "a=ssrc:1 cname:stream1\r\n"
211 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
212 "m=video 1 RTP/AVPF 120\r\n"
213 "a=ice-ufrag:e5785931\r\n"
214 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
215 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
216 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
217 "a=mid:1\r\n"
218 "a=sendrecv\r\n"
219 "a=rtcp-mux\r\n"
220 "a=rtpmap:120 VP8/0\r\n"
221 "a=ssrc:2 cname:stream1\r\n"
222 "a=ssrc:2 msid:stream1 videotrack0\r\n"
223 "m=audio 1 RTP/AVPF 103\r\n"
224 "a=ice-ufrag:e5785931\r\n"
225 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
226 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
227 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
228 "a=mid:2\r\n"
229 "a=sendrecv\r\n"
230 "a=rtcp-mux\r\n"
231 "a=rtpmap:103 ISAC/16000\r\n"
232 "a=ssrc:3 cname:stream2\r\n"
233 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
234 "m=video 1 RTP/AVPF 120\r\n"
235 "a=ice-ufrag:e5785931\r\n"
236 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
237 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
238 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
239 "a=mid:3\r\n"
240 "a=sendrecv\r\n"
241 "a=rtcp-mux\r\n"
242 "a=rtpmap:120 VP8/0\r\n"
243 "a=ssrc:4 cname:stream2\r\n"
244 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700245
246// Reference SDP without MediaStreams. Msid is not supported.
247static const char kSdpStringWithoutStreams[] =
248 "v=0\r\n"
249 "o=- 0 0 IN IP4 127.0.0.1\r\n"
250 "s=-\r\n"
251 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800252 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=ice-ufrag:e5785931\r\n"
254 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
255 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
256 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700257 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700258 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800259 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700260 "a=rtpmap:103 ISAC/16000\r\n"
261 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800262 "a=ice-ufrag:e5785931\r\n"
263 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
264 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
265 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700266 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700267 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800268 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700269 "a=rtpmap:120 VP8/90000\r\n";
270
271// Reference SDP without MediaStreams. Msid is supported.
272static const char kSdpStringWithMsidWithoutStreams[] =
273 "v=0\r\n"
274 "o=- 0 0 IN IP4 127.0.0.1\r\n"
275 "s=-\r\n"
276 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800277 "a=msid-semantic: WMS\r\n"
278 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700279 "a=ice-ufrag:e5785931\r\n"
280 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
281 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
282 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700283 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700284 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800285 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700286 "a=rtpmap:103 ISAC/16000\r\n"
287 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800288 "a=ice-ufrag:e5785931\r\n"
289 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
290 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
291 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700292 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700293 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800294 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700295 "a=rtpmap:120 VP8/90000\r\n";
296
297// Reference SDP without MediaStreams and audio only.
298static const char kSdpStringWithoutStreamsAudioOnly[] =
299 "v=0\r\n"
300 "o=- 0 0 IN IP4 127.0.0.1\r\n"
301 "s=-\r\n"
302 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800303 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700304 "a=ice-ufrag:e5785931\r\n"
305 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
306 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
307 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700308 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700309 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800310 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700311 "a=rtpmap:103 ISAC/16000\r\n";
312
313// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
314static const char kSdpStringSendOnlyWithoutStreams[] =
315 "v=0\r\n"
316 "o=- 0 0 IN IP4 127.0.0.1\r\n"
317 "s=-\r\n"
318 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800319 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700320 "a=ice-ufrag:e5785931\r\n"
321 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
322 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
323 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700324 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700325 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700326 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800327 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700328 "a=rtpmap:103 ISAC/16000\r\n"
329 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800330 "a=ice-ufrag:e5785931\r\n"
331 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
332 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
333 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700334 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700335 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700336 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800337 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700338 "a=rtpmap:120 VP8/90000\r\n";
339
340static const char kSdpStringInit[] =
341 "v=0\r\n"
342 "o=- 0 0 IN IP4 127.0.0.1\r\n"
343 "s=-\r\n"
344 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700345 "a=msid-semantic: WMS\r\n";
346
347static const char kSdpStringAudio[] =
348 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800349 "a=ice-ufrag:e5785931\r\n"
350 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
351 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
352 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700353 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700354 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800355 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700356 "a=rtpmap:103 ISAC/16000\r\n";
357
358static const char kSdpStringVideo[] =
359 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800360 "a=ice-ufrag:e5785931\r\n"
361 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
362 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
363 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700364 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700365 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800366 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700367 "a=rtpmap:120 VP8/90000\r\n";
368
369static const char kSdpStringMs1Audio0[] =
370 "a=ssrc:1 cname:stream1\r\n"
371 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
372
373static const char kSdpStringMs1Video0[] =
374 "a=ssrc:2 cname:stream1\r\n"
375 "a=ssrc:2 msid:stream1 videotrack0\r\n";
376
377static const char kSdpStringMs1Audio1[] =
378 "a=ssrc:3 cname:stream1\r\n"
379 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
380
381static const char kSdpStringMs1Video1[] =
382 "a=ssrc:4 cname:stream1\r\n"
383 "a=ssrc:4 msid:stream1 videotrack1\r\n";
384
deadbeef8662f942017-01-20 21:20:51 -0800385static const char kDtlsSdesFallbackSdp[] =
386 "v=0\r\n"
387 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
388 "s=-\r\n"
389 "c=IN IP4 0.0.0.0\r\n"
390 "t=0 0\r\n"
391 "a=group:BUNDLE audio\r\n"
392 "a=msid-semantic: WMS\r\n"
393 "m=audio 1 RTP/SAVPF 0\r\n"
394 "a=sendrecv\r\n"
395 "a=rtcp-mux\r\n"
396 "a=mid:audio\r\n"
397 "a=ssrc:1 cname:stream1\r\n"
398 "a=ssrc:1 mslabel:stream1\r\n"
399 "a=ssrc:1 label:audiotrack0\r\n"
400 "a=ice-ufrag:e5785931\r\n"
401 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
402 "a=rtpmap:0 pcmu/8000\r\n"
403 "a=fingerprint:sha-1 "
404 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
405 "a=setup:actpass\r\n"
Tommi8e545ee2018-02-08 16:25:20 +0000406 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
deadbeef8662f942017-01-20 21:20:51 -0800407 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
408 "dummy_session_params\r\n";
409
perkjd61bf802016-03-24 03:16:19 -0700410using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800411using ::testing::Values;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700412using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700414using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415using webrtc::AudioTrackInterface;
416using webrtc::DataBuffer;
417using webrtc::DataChannelInterface;
418using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700420using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700421using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422using webrtc::MediaStreamInterface;
423using webrtc::MediaStreamTrackInterface;
424using webrtc::MockCreateSessionDescriptionObserver;
425using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700426using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427using webrtc::MockSetSessionDescriptionObserver;
428using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700429using webrtc::NotifierInterface;
430using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431using webrtc::PeerConnectionInterface;
432using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800433using webrtc::RTCError;
434using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700435using webrtc::RtpReceiverInterface;
436using webrtc::RtpSenderInterface;
Steve Anton57858b32018-02-15 15:19:50 -0800437using webrtc::RtpSenderProxyWithInternal;
438using webrtc::RtpSenderInternal;
Steve Anton4e70a722017-11-28 14:57:10 -0800439using webrtc::RtpTransceiverDirection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440using webrtc::SdpParseError;
Steve Anton36da6ff2018-02-16 16:04:20 -0800441using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800442using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700444using webrtc::StreamCollection;
445using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100446using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700447using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448using webrtc::VideoTrackInterface;
449
Steve Anton36da6ff2018-02-16 16:04:20 -0800450using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
451using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453namespace {
454
455// Gets the first ssrc of given content type from the ContentInfo.
456bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
457 if (!content_info || !ssrc) {
458 return false;
459 }
460 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800461 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 if (!media_desc || media_desc->streams().empty()) {
463 return false;
464 }
465 *ssrc = media_desc->streams().begin()->first_ssrc();
466 return true;
467}
468
deadbeefd1a38b52016-12-10 13:15:33 -0800469// Get the ufrags out of an SDP blob. Useful for testing ICE restart
470// behavior.
471std::vector<std::string> GetUfrags(
472 const webrtc::SessionDescriptionInterface* desc) {
473 std::vector<std::string> ufrags;
474 for (const cricket::TransportInfo& info :
475 desc->description()->transport_infos()) {
476 ufrags.push_back(info.description.ice_ufrag);
477 }
478 return ufrags;
479}
480
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481void SetSsrcToZero(std::string* sdp) {
482 const char kSdpSsrcAtribute[] = "a=ssrc:";
483 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
484 size_t ssrc_pos = 0;
485 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
486 std::string::npos) {
487 size_t end_ssrc = sdp->find(" ", ssrc_pos);
488 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
489 ssrc_pos = end_ssrc;
490 }
491}
492
deadbeefab9b2d12015-10-14 11:33:11 -0700493// Check if |streams| contains the specified track.
494bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
495 const std::string& stream_label,
496 const std::string& track_id) {
497 for (const cricket::StreamParams& params : streams) {
498 if (params.sync_label == stream_label && params.id == track_id) {
499 return true;
500 }
501 }
502 return false;
503}
504
505// Check if |senders| contains the specified sender, by id.
506bool ContainsSender(
507 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
508 const std::string& id) {
509 for (const auto& sender : senders) {
510 if (sender->id() == id) {
511 return true;
512 }
513 }
514 return false;
515}
516
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700517// Check if |senders| contains the specified sender, by id and stream id.
518bool ContainsSender(
519 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
520 const std::string& id,
521 const std::string& stream_id) {
522 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700523 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700524 return true;
525 }
526 }
527 return false;
528}
529
deadbeefab9b2d12015-10-14 11:33:11 -0700530// Create a collection of streams.
531// CreateStreamCollection(1) creates a collection that
532// correspond to kSdpStringWithStream1.
533// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
534rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700535 int number_of_streams,
536 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700537 rtc::scoped_refptr<StreamCollection> local_collection(
538 StreamCollection::Create());
539
540 for (int i = 0; i < number_of_streams; ++i) {
541 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
542 webrtc::MediaStream::Create(kStreams[i]));
543
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700544 for (int j = 0; j < tracks_per_stream; ++j) {
545 // Add a local audio track.
546 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
547 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
548 nullptr));
549 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700550
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700551 // Add a local video track.
552 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
553 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700554 webrtc::FakeVideoTrackSource::Create(),
555 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700556 stream->AddTrack(video_track);
557 }
deadbeefab9b2d12015-10-14 11:33:11 -0700558
559 local_collection->AddStream(stream);
560 }
561 return local_collection;
562}
563
564// Check equality of StreamCollections.
565bool CompareStreamCollections(StreamCollectionInterface* s1,
566 StreamCollectionInterface* s2) {
567 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
568 return false;
569 }
570
571 for (size_t i = 0; i != s1->count(); ++i) {
572 if (s1->at(i)->label() != s2->at(i)->label()) {
573 return false;
574 }
575 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
576 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
577 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
578 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
579
580 if (audio_tracks1.size() != audio_tracks2.size()) {
581 return false;
582 }
583 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
584 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
585 return false;
586 }
587 }
588 if (video_tracks1.size() != video_tracks2.size()) {
589 return false;
590 }
591 for (size_t j = 0; j != video_tracks1.size(); ++j) {
592 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
593 return false;
594 }
595 }
596 }
597 return true;
598}
599
perkjd61bf802016-03-24 03:16:19 -0700600// Helper class to test Observer.
601class MockTrackObserver : public ObserverInterface {
602 public:
603 explicit MockTrackObserver(NotifierInterface* notifier)
604 : notifier_(notifier) {
605 notifier_->RegisterObserver(this);
606 }
607
608 ~MockTrackObserver() { Unregister(); }
609
610 void Unregister() {
611 if (notifier_) {
612 notifier_->UnregisterObserver(this);
613 notifier_ = nullptr;
614 }
615 }
616
617 MOCK_METHOD0(OnChanged, void());
618
619 private:
620 NotifierInterface* notifier_;
621};
622
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700624
nisse528b7932017-05-08 03:21:43 -0700625// The PeerConnectionMediaConfig tests below verify that configuration and
626// constraints are propagated into the PeerConnection's MediaConfig. These
627// settings are intended for MediaChannel constructors, but that is not
628// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700629class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
630 public:
zhihuang38ede132017-06-15 12:52:32 -0700631 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
632 CreatePeerConnectionFactoryForTest() {
633 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
634 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
635
henrika919dc2e2017-10-12 14:24:55 +0200636 // Use fake audio device module since we're only testing the interface
637 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700638 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
639 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200640 FakeAudioCaptureModule::Create(), audio_encoder_factory,
641 audio_decoder_factory, nullptr, nullptr, nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +0100642 webrtc::AudioProcessingBuilder().Create()));
zhihuang38ede132017-06-15 12:52:32 -0700643
644 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
645 webrtc::CreateCallFactory();
646
647 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
648 webrtc::CreateRtcEventLogFactory();
649
650 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
651 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200652 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700653 std::move(event_log_factory));
654 }
655
656 PeerConnectionFactoryForTest(
657 rtc::Thread* network_thread,
658 rtc::Thread* worker_thread,
659 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700660 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
661 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
662 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
663 : webrtc::PeerConnectionFactory(network_thread,
664 worker_thread,
665 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700666 std::move(media_engine),
667 std::move(call_factory),
668 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800669
zhihuang29ff8442016-07-27 11:07:25 -0700670 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700671 cricket::PortAllocator* port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800672 bool redetermine_role_on_ice_restart,
673 webrtc::RtcEventLog* event_log = nullptr) override {
zhihuang29ff8442016-07-27 11:07:25 -0700674 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700675 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800676 redetermine_role_on_ice_restart, rtc::CryptoOptions(), event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700677 return transport_controller;
678 }
679
deadbeefd7850b22017-08-23 10:59:19 -0700680 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700681 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700682};
683
Steve Anton36da6ff2018-02-16 16:04:20 -0800684// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
685class PeerConnectionInterfaceBaseTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800687 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
688 : vss_(new rtc::VirtualSocketServer()),
689 main_(vss_.get()),
690 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800691#ifdef WEBRTC_ANDROID
692 webrtc::InitializeAndroidObjects();
693#endif
694 }
695
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700697 // Use fake audio capture module since we're only testing the interface
698 // level, and using a real one could make tests flaky when run in parallel.
699 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700701 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200702 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
703 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700704 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700705 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700706 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700707 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
709
710 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700711 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
deadbeef293e9262017-01-11 12:28:30 -0800714 // DTLS does not work in a loopback call, so is disabled for most of the
715 // tests in this file.
716 void CreatePeerConnectionWithoutDtls() {
717 FakeConstraints no_dtls_constraints;
718 no_dtls_constraints.AddMandatory(
719 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
720
721 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
722 &no_dtls_constraints);
723 }
724
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
727 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 void CreatePeerConnectionWithIceTransportsType(
731 PeerConnectionInterface::IceTransportsType type) {
732 PeerConnectionInterface::RTCConfiguration config;
733 config.type = type;
734 return CreatePeerConnection(config, nullptr);
735 }
736
737 void CreatePeerConnectionWithIceServer(const std::string& uri,
738 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800739 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700741 server.uri = uri;
742 server.password = password;
743 config.servers.push_back(server);
744 CreatePeerConnection(config, nullptr);
745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100747 void CreatePeerConnection(
748 const PeerConnectionInterface::RTCConfiguration& config,
749 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700750 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
752 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000753
deadbeef1dcb1642017-03-29 21:08:16 -0700754 // Create certificate generator unless DTLS constraint is explicitly set to
755 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200756 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000757 bool dtls;
758 if (FindConstraint(constraints,
759 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
760 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200761 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800762 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
763 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000764 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800765 RTCConfiguration modified_config = config;
766 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200767 pc_ = pc_factory_->CreatePeerConnection(
Steve Anton36da6ff2018-02-16 16:04:20 -0800768 modified_config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200769 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 ASSERT_TRUE(pc_.get() != NULL);
771 observer_.SetPeerConnectionInterface(pc_.get());
772 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
773 }
774
deadbeef0a6c4ca2015-10-06 11:38:28 -0700775 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700777 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700778 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800780 config.sdp_semantics = sdp_semantics_;
781 rtc::scoped_refptr<PeerConnectionInterface> pc =
782 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
783 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700785 }
786
Steve Anton038834f2017-07-14 15:59:59 -0700787 void CreatePeerConnectionExpectFail(
788 PeerConnectionInterface::RTCConfiguration config) {
789 PeerConnectionInterface::IceServer server;
790 server.uri = kTurnIceServerUri;
791 server.password = kTurnPassword;
792 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800793 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700794 rtc::scoped_refptr<PeerConnectionInterface> pc =
795 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
796 EXPECT_EQ(nullptr, pc);
797 }
798
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700800 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800801 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
802 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
803 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806
deadbeef0a6c4ca2015-10-06 11:38:28 -0700807 CreatePeerConnectionExpectFail(kStunInvalidPort);
808 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
809 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700811 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800812 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
813 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800815 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800819 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
821
822 void ReleasePeerConnection() {
823 pc_ = NULL;
824 observer_.SetPeerConnectionInterface(NULL);
825 }
826
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
828 const std::string& label) {
829 auto video_source = pc_factory_->CreateVideoSource(
830 rtc::MakeUnique<cricket::FakeVideoCapturer>(), nullptr);
831 return pc_factory_->CreateVideoTrack(label, video_source);
832 }
833
834 void AddVideoTrack(const std::string& track_label,
835 const std::vector<std::string>& stream_labels = {}) {
836 auto sender_or_error =
837 pc_->AddTrack(CreateVideoTrack(track_label), stream_labels);
838 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
840 observer_.renegotiation_needed_ = false;
841 }
842
Steve Anton36da6ff2018-02-16 16:04:20 -0800843 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700844 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800846 stream->AddTrack(CreateVideoTrack(label + "v0"));
847 ASSERT_TRUE(pc_->AddStream(stream));
848 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
849 observer_.renegotiation_needed_ = false;
850 }
851
852 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
853 const std::string& label) {
854 return pc_factory_->CreateAudioTrack(label, nullptr);
855 }
856
857 void AddAudioTrack(const std::string& track_label,
858 const std::vector<std::string>& stream_labels = {}) {
859 auto sender_or_error =
860 pc_->AddTrack(CreateAudioTrack(track_label), stream_labels);
861 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
862 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
863 observer_.renegotiation_needed_ = false;
864 }
865
866 void AddAudioStream(const std::string& label) {
867 rtc::scoped_refptr<MediaStreamInterface> stream(
868 pc_factory_->CreateLocalMediaStream(label));
869 stream->AddTrack(CreateAudioTrack(label + "a0"));
870 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
872 observer_.renegotiation_needed_ = false;
873 }
874
875 void AddAudioVideoStream(const std::string& stream_label,
876 const std::string& audio_track_label,
877 const std::string& video_track_label) {
878 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700879 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 pc_factory_->CreateLocalMediaStream(stream_label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800881 stream->AddTrack(CreateAudioTrack(audio_track_label));
882 stream->AddTrack(CreateVideoTrack(video_track_label));
883 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
885 observer_.renegotiation_needed_ = false;
886 }
887
Steve Anton36da6ff2018-02-16 16:04:20 -0800888 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
889 cricket::MediaType media_type) {
890 for (auto receiver : pc_->GetReceivers()) {
891 if (receiver->media_type() == media_type) {
892 return receiver;
893 }
894 }
895 return nullptr;
896 }
897
kwibergd1fe2812016-04-27 06:47:29 -0700898 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700899 bool offer,
900 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000901 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
902 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 MockCreateSessionDescriptionObserver>());
904 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700905 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700907 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700910 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 return observer->result();
912 }
913
kwibergd1fe2812016-04-27 06:47:29 -0700914 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700915 MediaConstraintsInterface* constraints) {
916 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 }
918
kwibergd1fe2812016-04-27 06:47:29 -0700919 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700920 MediaConstraintsInterface* constraints) {
921 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
923
Steve Antondb45ca82017-09-11 18:27:34 -0700924 bool DoSetSessionDescription(
925 std::unique_ptr<SessionDescriptionInterface> desc,
926 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000927 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
928 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 MockSetSessionDescriptionObserver>());
930 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700931 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700933 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 }
zhihuang29ff8442016-07-27 11:07:25 -0700935 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
936 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 return observer->result();
939 }
940
Steve Antondb45ca82017-09-11 18:27:34 -0700941 bool DoSetLocalDescription(
942 std::unique_ptr<SessionDescriptionInterface> desc) {
943 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 }
945
Steve Antondb45ca82017-09-11 18:27:34 -0700946 bool DoSetRemoteDescription(
947 std::unique_ptr<SessionDescriptionInterface> desc) {
948 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 }
950
951 // Calls PeerConnection::GetStats and check the return value.
952 // It does not verify the values in the StatReports since a RTCP packet might
953 // be required.
954 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000955 rtc::scoped_refptr<MockStatsObserver> observer(
956 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000957 if (!pc_->GetStats(
958 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 return false;
960 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
961 return observer->called();
962 }
963
Harald Alvestrand89061872018-01-02 14:08:34 +0100964 // Call the standards-compliant GetStats function.
965 bool DoGetRTCStats() {
966 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
967 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
968 pc_->GetStats(callback);
969 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
970 return callback->called();
971 }
972
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800974 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800976 if (sdp_semantics_ == SdpSemantics::kPlanB) {
977 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
978 } else {
979 // Unified Plan does not support AddStream, so just add an audio and video
980 // track.
981 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
982 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
983 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 CreateOfferReceiveAnswer();
985 }
986
987 // Verify that RTP Header extensions has been negotiated for audio and video.
988 void VerifyRemoteRtpHeaderExtensions() {
989 const cricket::MediaContentDescription* desc =
990 cricket::GetFirstAudioContentDescription(
991 pc_->remote_description()->description());
992 ASSERT_TRUE(desc != NULL);
993 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
994
995 desc = cricket::GetFirstVideoContentDescription(
996 pc_->remote_description()->description());
997 ASSERT_TRUE(desc != NULL);
998 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
999 }
1000
1001 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001002 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001003 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 std::string sdp;
1005 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001006 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001007 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001008 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1010 }
1011
deadbeefab9b2d12015-10-14 11:33:11 -07001012 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001013 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001014 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001015 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07001016 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1017 }
1018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 void CreateAnswerAsLocalDescription() {
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 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1024 // audio codec change, even if the parameter has nothing to do with
1025 // receiving. Not all parameters are serialized to SDP.
1026 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1027 // the SessionDescription, it is necessary to do that here to in order to
1028 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1029 // https://code.google.com/p/webrtc/issues/detail?id=1356
1030 std::string sdp;
1031 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001032 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001033 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001034 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1036 }
1037
1038 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001039 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001040 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
1042 std::string sdp;
1043 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001044 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001045 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001046 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1048 }
1049
1050 void CreateOfferReceiveAnswer() {
1051 CreateOfferAsLocalDescription();
1052 std::string sdp;
1053 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1054 CreateAnswerAsRemoteDescription(sdp);
1055 }
1056
1057 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001058 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001059 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1061 // audio codec change, even if the parameter has nothing to do with
1062 // receiving. Not all parameters are serialized to SDP.
1063 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1064 // the SessionDescription, it is necessary to do that here to in order to
1065 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1066 // https://code.google.com/p/webrtc/issues/detail?id=1356
1067 std::string sdp;
1068 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001069 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001070 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071
Steve Antondb45ca82017-09-11 18:27:34 -07001072 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001074 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001075 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 }
1077
deadbeefab9b2d12015-10-14 11:33:11 -07001078 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001079 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001080 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001081 ASSERT_TRUE(answer);
1082 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1084 }
1085
deadbeefab9b2d12015-10-14 11:33:11 -07001086 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001087 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001088 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001089 ASSERT_TRUE(pr_answer);
1090 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001092 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001093 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001094 ASSERT_TRUE(answer);
1095 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1097 }
1098
Steve Anton36da6ff2018-02-16 16:04:20 -08001099 // Waits until a remote stream with the given label is signaled. This helper
1100 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
1101 // called with the given stream label and expected number of tracks.
1102 void WaitAndVerifyOnAddStream(const std::string& stream_label,
1103 int expected_num_tracks) {
1104 // Verify that both OnAddStream and OnAddTrack are called.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001106 EXPECT_EQ_WAIT(expected_num_tracks,
1107 observer_.CountAddTrackEventsForStream(stream_label),
1108 kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
1110
1111 // Creates an offer and applies it as a local session description.
1112 // Creates an answer with the same SDP an the offer but removes all lines
1113 // that start with a:ssrc"
1114 void CreateOfferReceiveAnswerWithoutSsrc() {
1115 CreateOfferAsLocalDescription();
1116 std::string sdp;
1117 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1118 SetSsrcToZero(&sdp);
1119 CreateAnswerAsRemoteDescription(sdp);
1120 }
1121
deadbeefab9b2d12015-10-14 11:33:11 -07001122 // This function creates a MediaStream with label kStreams[0] and
1123 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1124 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001125 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001126 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001127 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001128 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1129 size_t number_of_video_tracks) {
1130 EXPECT_LE(number_of_audio_tracks, 2u);
1131 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001132
1133 reference_collection_ = StreamCollection::Create();
1134 std::string sdp_ms1 = std::string(kSdpStringInit);
1135
1136 std::string mediastream_label = kStreams[0];
1137
1138 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1139 webrtc::MediaStream::Create(mediastream_label));
1140 reference_collection_->AddStream(stream);
1141
1142 if (number_of_audio_tracks > 0) {
1143 sdp_ms1 += std::string(kSdpStringAudio);
1144 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1145 AddAudioTrack(kAudioTracks[0], stream);
1146 }
1147 if (number_of_audio_tracks > 1) {
1148 sdp_ms1 += kSdpStringMs1Audio1;
1149 AddAudioTrack(kAudioTracks[1], stream);
1150 }
1151
1152 if (number_of_video_tracks > 0) {
1153 sdp_ms1 += std::string(kSdpStringVideo);
1154 sdp_ms1 += std::string(kSdpStringMs1Video0);
1155 AddVideoTrack(kVideoTracks[0], stream);
1156 }
1157 if (number_of_video_tracks > 1) {
1158 sdp_ms1 += kSdpStringMs1Video1;
1159 AddVideoTrack(kVideoTracks[1], stream);
1160 }
1161
kwibergd1fe2812016-04-27 06:47:29 -07001162 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001163 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001164 }
1165
1166 void AddAudioTrack(const std::string& track_id,
1167 MediaStreamInterface* stream) {
1168 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1169 webrtc::AudioTrack::Create(track_id, nullptr));
1170 ASSERT_TRUE(stream->AddTrack(audio_track));
1171 }
1172
1173 void AddVideoTrack(const std::string& track_id,
1174 MediaStreamInterface* stream) {
1175 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001176 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001177 webrtc::FakeVideoTrackSource::Create(),
1178 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001179 ASSERT_TRUE(stream->AddTrack(video_track));
1180 }
1181
Steve Anton36da6ff2018-02-16 16:04:20 -08001182 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001183 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001184 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001185 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1187 return offer;
1188 }
1189
Steve Anton36da6ff2018-02-16 16:04:20 -08001190 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1191 CreatePeerConnectionWithoutDtls();
1192 AddAudioStream(kStreamLabel1);
1193 std::unique_ptr<SessionDescriptionInterface> offer;
1194 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1195 return offer;
1196 }
1197
1198 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1199 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1200 std::unique_ptr<SessionDescriptionInterface> answer;
1201 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1202 return answer;
1203 }
1204
kwibergfd8be342016-05-14 19:44:11 -07001205 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001206 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001207 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001208 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001209 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1210 return answer;
1211 }
1212
1213 const std::string& GetFirstAudioStreamCname(
1214 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001215 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001216 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001217 return audio_desc->streams()[0].cname;
1218 }
1219
zhihuang1c378ed2017-08-17 14:10:50 -07001220 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1221 const RTCOfferAnswerOptions& offer_answer_options) {
1222 RTC_DCHECK(pc_);
1223 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1224 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1225 pc_->CreateOffer(observer, offer_answer_options);
1226 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1227 return observer->MoveDescription();
1228 }
1229
1230 void CreateOfferWithOptionsAsRemoteDescription(
1231 std::unique_ptr<SessionDescriptionInterface>* desc,
1232 const RTCOfferAnswerOptions& offer_answer_options) {
1233 *desc = CreateOfferWithOptions(offer_answer_options);
1234 ASSERT_TRUE(desc != nullptr);
1235 std::string sdp;
1236 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001237 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001238 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001239 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001240 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1241 }
1242
1243 void CreateOfferWithOptionsAsLocalDescription(
1244 std::unique_ptr<SessionDescriptionInterface>* desc,
1245 const RTCOfferAnswerOptions& offer_answer_options) {
1246 *desc = CreateOfferWithOptions(offer_answer_options);
1247 ASSERT_TRUE(desc != nullptr);
1248 std::string sdp;
1249 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001250 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001251 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001252
Steve Antondb45ca82017-09-11 18:27:34 -07001253 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001254 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1255 }
1256
1257 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001258 RTC_DCHECK(content);
1259 RTC_DCHECK(content->media_description());
1260 for (const cricket::AudioCodec& codec :
1261 content->media_description()->as_audio()->codecs()) {
1262 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001263 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001264 }
zhihuang1c378ed2017-08-17 14:10:50 -07001265 }
1266 return false;
1267 }
1268
Steve Anton36da6ff2018-02-16 16:04:20 -08001269 const char* GetSdpStringWithStream1() const {
1270 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1271 return kSdpStringWithStream1PlanB;
1272 } else {
1273 return kSdpStringWithStream1UnifiedPlan;
1274 }
1275 }
1276
1277 const char* GetSdpStringWithStream1And2() const {
1278 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1279 return kSdpStringWithStream1And2PlanB;
1280 } else {
1281 return kSdpStringWithStream1And2UnifiedPlan;
1282 }
1283 }
1284
deadbeef9a6f4d42017-05-15 19:43:33 -07001285 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1286 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001287 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001288 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001289 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001290 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1291 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1292 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001294 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001295 const SdpSemantics sdp_semantics_;
1296};
1297
1298class PeerConnectionInterfaceTest
1299 : public PeerConnectionInterfaceBaseTest,
1300 public ::testing::WithParamInterface<SdpSemantics> {
1301 protected:
1302 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1303};
1304
1305class PeerConnectionInterfaceTestPlanB
1306 : public PeerConnectionInterfaceBaseTest {
1307 protected:
1308 PeerConnectionInterfaceTestPlanB()
1309 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310};
1311
zhihuang8f65cdf2016-05-06 18:40:30 -07001312// Generate different CNAMEs when PeerConnections are created.
1313// The CNAMEs are expected to be generated randomly. It is possible
1314// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001315TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001316 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001317 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001318 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001319 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001320 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1321 GetFirstAudioStreamCname(offer2.get()));
1322}
1323
Steve Anton36da6ff2018-02-16 16:04:20 -08001324TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001325 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001326 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001327 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001328 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001329 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1330 GetFirstAudioStreamCname(answer2.get()));
1331}
1332
Steve Anton36da6ff2018-02-16 16:04:20 -08001333TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 CreatePeerConnectionWithDifferentConfigurations) {
1335 CreatePeerConnectionWithDifferentConfigurations();
1336}
1337
Steve Anton36da6ff2018-02-16 16:04:20 -08001338TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001339 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1340 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1341 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1342 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1343 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1344 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1345 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1346 port_allocator_->candidate_filter());
1347 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1348 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1349}
1350
1351// Test that when a PeerConnection is created with a nonzero candidate pool
1352// size, the pooled PortAllocatorSession is created with all the attributes
1353// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001354TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001355 PeerConnectionInterface::RTCConfiguration config;
1356 PeerConnectionInterface::IceServer server;
1357 server.uri = kStunAddressOnly;
1358 config.servers.push_back(server);
1359 config.type = PeerConnectionInterface::kRelay;
1360 config.disable_ipv6 = true;
1361 config.tcp_candidate_policy =
1362 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001363 config.candidate_network_policy =
1364 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001365 config.ice_candidate_pool_size = 1;
1366 CreatePeerConnection(config, nullptr);
1367
1368 const cricket::FakePortAllocatorSession* session =
1369 static_cast<const cricket::FakePortAllocatorSession*>(
1370 port_allocator_->GetPooledSession());
1371 ASSERT_NE(nullptr, session);
1372 EXPECT_EQ(1UL, session->stun_servers().size());
1373 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1374 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001375 EXPECT_LT(0U,
1376 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001377}
1378
deadbeefd21eab32017-07-26 16:50:11 -07001379// Test that network-related RTCConfiguration members are applied to the
1380// PortAllocator when CreatePeerConnection is called. Specifically:
1381// - disable_ipv6_on_wifi
1382// - max_ipv6_networks
1383// - tcp_candidate_policy
1384// - candidate_network_policy
1385// - prune_turn_ports
1386//
1387// Note that the candidate filter (RTCConfiguration::type) is already tested
1388// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001389TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001390 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1391 // Create fake port allocator.
1392 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1393 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1394 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1395
1396 // Create RTCConfiguration with some network-related fields relevant to
1397 // PortAllocator populated.
1398 PeerConnectionInterface::RTCConfiguration config;
1399 config.disable_ipv6_on_wifi = true;
1400 config.max_ipv6_networks = 10;
1401 config.tcp_candidate_policy =
1402 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1403 config.candidate_network_policy =
1404 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1405 config.prune_turn_ports = true;
1406
1407 // Create the PC factory and PC with the above config.
1408 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1409 webrtc::CreatePeerConnectionFactory(
1410 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001411 rtc::Thread::Current(), fake_audio_capture_module_,
1412 webrtc::CreateBuiltinAudioEncoderFactory(),
1413 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001414 rtc::scoped_refptr<PeerConnectionInterface> pc(
1415 pc_factory->CreatePeerConnection(
1416 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1417
1418 // Now validate that the config fields set above were applied to the
1419 // PortAllocator, as flags or otherwise.
1420 EXPECT_FALSE(raw_port_allocator->flags() &
1421 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1422 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1423 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1424 EXPECT_TRUE(raw_port_allocator->flags() &
1425 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1426 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1427}
1428
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001429// Test that the PeerConnection initializes the port allocator passed into it,
1430// and on the correct thread.
Steve Anton36da6ff2018-02-16 16:04:20 -08001431TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001432 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001433 std::unique_ptr<rtc::Thread> network_thread(
1434 rtc::Thread::CreateWithSocketServer());
1435 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001436 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1437 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001438 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001439 fake_audio_capture_module_,
1440 webrtc::CreateBuiltinAudioEncoderFactory(),
1441 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001442 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001443 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001444 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1445 PeerConnectionInterface::RTCConfiguration config;
1446 rtc::scoped_refptr<PeerConnectionInterface> pc(
1447 pc_factory->CreatePeerConnection(
1448 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1449 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1450 // so all we have to do here is check that it's initialized.
1451 EXPECT_TRUE(raw_port_allocator->initialized());
1452}
1453
deadbeef46c73892016-11-16 19:42:04 -08001454// Check that GetConfiguration returns the configuration the PeerConnection was
1455// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001456TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001457 PeerConnectionInterface::RTCConfiguration config;
1458 config.type = PeerConnectionInterface::kRelay;
1459 CreatePeerConnection(config, nullptr);
1460
1461 PeerConnectionInterface::RTCConfiguration returned_config =
1462 pc_->GetConfiguration();
1463 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1464}
1465
1466// Check that GetConfiguration returns the last configuration passed into
1467// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001468TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
deadbeef46c73892016-11-16 19:42:04 -08001469 CreatePeerConnection();
1470
Steve Anton36da6ff2018-02-16 16:04:20 -08001471 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001472 config.type = PeerConnectionInterface::kRelay;
1473 EXPECT_TRUE(pc_->SetConfiguration(config));
1474
1475 PeerConnectionInterface::RTCConfiguration returned_config =
1476 pc_->GetConfiguration();
1477 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1478}
1479
Steve Anton36da6ff2018-02-16 16:04:20 -08001480TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001481 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001482 AddVideoStream(kStreamLabel1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001483 AddAudioStream(kStreamLabel2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 ASSERT_EQ(2u, pc_->local_streams()->count());
1485
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001486 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001487 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001489 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1490 pc_factory_->CreateAudioTrack(kStreamLabel3,
1491 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001493 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001494 EXPECT_EQ(3u, pc_->local_streams()->count());
1495
1496 // Remove the third stream.
1497 pc_->RemoveStream(pc_->local_streams()->at(2));
1498 EXPECT_EQ(2u, pc_->local_streams()->count());
1499
1500 // Remove the second stream.
1501 pc_->RemoveStream(pc_->local_streams()->at(1));
1502 EXPECT_EQ(1u, pc_->local_streams()->count());
1503
1504 // Remove the first stream.
1505 pc_->RemoveStream(pc_->local_streams()->at(0));
1506 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507}
1508
deadbeefab9b2d12015-10-14 11:33:11 -07001509// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001510// Don't run under Unified Plan since the stream API is not available.
1511TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001513 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001514 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001515 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001516
deadbeefab9b2d12015-10-14 11:33:11 -07001517 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001518 cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001519 EXPECT_TRUE(
1520 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1521
deadbeefab9b2d12015-10-14 11:33:11 -07001522 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001523 cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001524 EXPECT_TRUE(
1525 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1526
1527 // Add another stream and ensure the offer includes both the old and new
1528 // streams.
1529 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001530 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001531
Steve Antonb1c1de12017-12-21 15:14:30 -08001532 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001533 EXPECT_TRUE(
1534 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1535 EXPECT_TRUE(
1536 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1537
Steve Antonb1c1de12017-12-21 15:14:30 -08001538 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001539 EXPECT_TRUE(
1540 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1541 EXPECT_TRUE(
1542 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1543}
1544
Steve Anton36da6ff2018-02-16 16:04:20 -08001545// Don't run under Unified Plan since the stream API is not available.
1546TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001547 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001548 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 ASSERT_EQ(1u, pc_->local_streams()->count());
1550 pc_->RemoveStream(pc_->local_streams()->at(0));
1551 EXPECT_EQ(0u, pc_->local_streams()->count());
1552}
1553
deadbeefe1f9d832016-01-14 15:35:42 -08001554// Test for AddTrack and RemoveTrack methods.
1555// Tests that the created offer includes tracks we added,
1556// and that the RtpSenders are created correctly.
1557// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001558// Only tested with Plan B since Unified Plan is covered in more detail by tests
1559// in peerconnection_jsep_unittests.cc
1560TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001561 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001562 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001563 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001564 rtc::scoped_refptr<VideoTrackInterface> video_track(
1565 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001566 "video_track", pc_factory_->CreateVideoSource(
1567 std::unique_ptr<cricket::VideoCapturer>(
1568 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001569 auto audio_sender = pc_->AddTrack(audio_track, {kStreamLabel1}).MoveValue();
1570 auto video_sender = pc_->AddTrack(video_track, {kStreamLabel1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001571 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1572 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001573 EXPECT_EQ("audio_track", audio_sender->id());
1574 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001575 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1576 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001577 EXPECT_EQ("video_track", video_sender->id());
1578 EXPECT_EQ(video_track, video_sender->track());
1579
1580 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001581 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001582 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001583
1584 const cricket::ContentInfo* audio_content =
1585 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001586 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1587 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001588
1589 const cricket::ContentInfo* video_content =
1590 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001591 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1592 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001593
Steve Antondb45ca82017-09-11 18:27:34 -07001594 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001595
1596 // Now try removing the tracks.
1597 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1598 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1599
1600 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001601 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001602
1603 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001604 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1605 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001606
1607 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001608 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1609 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001610
Steve Antondb45ca82017-09-11 18:27:34 -07001611 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001612
1613 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1614 // should return false.
1615 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1616 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1617}
1618
1619// Test creating senders without a stream specified,
1620// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001621TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001622 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001623 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001624 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001625 rtc::scoped_refptr<VideoTrackInterface> video_track(
1626 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001627 "video_track", pc_factory_->CreateVideoSource(
1628 std::unique_ptr<cricket::VideoCapturer>(
1629 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001630 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001631 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001632 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001633 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001634 EXPECT_EQ("audio_track", audio_sender->id());
1635 EXPECT_EQ(audio_track, audio_sender->track());
1636 EXPECT_EQ("video_track", video_sender->id());
1637 EXPECT_EQ(video_track, video_sender->track());
1638 // If the ID is truly a random GUID, it should be infinitely unlikely they
1639 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001640 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001641}
1642
Harald Alvestrand89061872018-01-02 14:08:34 +01001643// Test that we can call GetStats() after AddTrack but before connecting
1644// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001645TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001646 CreatePeerConnectionWithoutDtls();
1647 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1648 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1649 rtc::scoped_refptr<VideoTrackInterface> video_track(
1650 pc_factory_->CreateVideoTrack(
1651 "video_track", pc_factory_->CreateVideoSource(
1652 std::unique_ptr<cricket::VideoCapturer>(
1653 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001654 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1655 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001656 EXPECT_TRUE(DoGetStats(nullptr));
1657}
1658
Steve Anton36da6ff2018-02-16 16:04:20 -08001659TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001660 CreatePeerConnectionWithoutDtls();
1661 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1662 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1663 rtc::scoped_refptr<VideoTrackInterface> video_track(
1664 pc_factory_->CreateVideoTrack(
1665 "video_track", pc_factory_->CreateVideoSource(
1666 std::unique_ptr<cricket::VideoCapturer>(
1667 new cricket::FakeVideoCapturer()))));
1668 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001669 ASSERT_TRUE(audio_sender.ok());
1670 auto* audio_sender_proxy =
1671 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1672 audio_sender.value().get());
1673 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1674
Harald Alvestrandc72af932018-01-11 17:18:19 +01001675 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001676 ASSERT_TRUE(video_sender.ok());
1677 auto* video_sender_proxy =
1678 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1679 video_sender.value().get());
1680 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001681}
1682
Steve Anton36da6ff2018-02-16 16:04:20 -08001683// Don't run under Unified Plan since the stream API is not available.
1684TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001685 CreatePeerConnectionWithoutDtls();
1686 AddVideoStream(kStreamLabel1);
1687 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001688 ASSERT_EQ(1u, senders.size());
1689 auto* sender_proxy =
1690 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1691 senders[0].get());
1692 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001693}
1694
Steve Anton36da6ff2018-02-16 16:04:20 -08001695TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001697 WaitAndVerifyOnAddStream(kStreamLabel1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 VerifyRemoteRtpHeaderExtensions();
1699}
1700
Steve Anton36da6ff2018-02-16 16:04:20 -08001701TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001702 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001703 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 CreateOfferAsLocalDescription();
1705 std::string offer;
1706 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1707 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Steve Anton36da6ff2018-02-16 16:04:20 -08001708 WaitAndVerifyOnAddStream(kStreamLabel1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709}
1710
Steve Anton36da6ff2018-02-16 16:04:20 -08001711TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001712 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001713 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714
1715 CreateOfferAsRemoteDescription();
1716 CreateAnswerAsLocalDescription();
1717
Steve Anton36da6ff2018-02-16 16:04:20 -08001718 WaitAndVerifyOnAddStream(kStreamLabel1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719}
1720
Steve Anton36da6ff2018-02-16 16:04:20 -08001721TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001722 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001723 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724
1725 CreateOfferAsRemoteDescription();
1726 CreatePrAnswerAsLocalDescription();
1727 CreateAnswerAsLocalDescription();
1728
Steve Anton36da6ff2018-02-16 16:04:20 -08001729 WaitAndVerifyOnAddStream(kStreamLabel1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730}
1731
Steve Anton36da6ff2018-02-16 16:04:20 -08001732// Don't run under Unified Plan since the stream API is not available.
1733TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 InitiateCall();
1735 ASSERT_EQ(1u, pc_->remote_streams()->count());
1736 pc_->RemoveStream(pc_->local_streams()->at(0));
1737 CreateOfferReceiveAnswer();
1738 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001739 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 CreateOfferReceiveAnswer();
1741}
1742
1743// Tests that after negotiating an audio only call, the respondent can perform a
1744// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001745TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001746 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001747 AddAudioStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 CreateOfferAsRemoteDescription();
1749 CreateAnswerAsLocalDescription();
1750
1751 ASSERT_EQ(1u, pc_->remote_streams()->count());
1752 pc_->RemoveStream(pc_->local_streams()->at(0));
1753 CreateOfferReceiveAnswer();
1754 EXPECT_EQ(0u, pc_->remote_streams()->count());
1755}
1756
1757// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001758TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001759 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760
Steve Antonf1c6db12017-10-13 11:13:35 -07001761 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001763 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001764 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001765 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001766 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767
1768 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001769 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001770 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001771 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772
Steve Antonf1c6db12017-10-13 11:13:35 -07001773 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001774 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
Steve Antonf1c6db12017-10-13 11:13:35 -07001776 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777}
1778
deadbeefab9b2d12015-10-14 11:33:11 -07001779// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780// not unique.
Steve Anton36da6ff2018-02-16 16:04:20 -08001781TEST_P(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001782 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001784 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001785 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001786 EXPECT_TRUE(offer);
1787 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788
1789 // Create a local stream with audio&video tracks having same label.
Steve Anton36da6ff2018-02-16 16:04:20 -08001790 AddAudioTrack("track_label", {kStreamLabel1});
1791 AddVideoTrack("track_label", {kStreamLabel1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792
1793 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001794 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795
1796 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001797 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001798 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799}
1800
1801// Test that we will get different SSRCs for each tracks in the offer and answer
1802// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001803TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001804 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 // Create a local stream with audio&video tracks having different labels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001806 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
1807 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808
1809 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001810 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001811 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 int audio_ssrc = 0;
1813 int video_ssrc = 0;
1814 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1815 &audio_ssrc));
1816 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1817 &video_ssrc));
1818 EXPECT_NE(audio_ssrc, video_ssrc);
1819
1820 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001821 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001822 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001823 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 audio_ssrc = 0;
1825 video_ssrc = 0;
1826 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1827 &audio_ssrc));
1828 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1829 &video_ssrc));
1830 EXPECT_NE(audio_ssrc, video_ssrc);
1831}
1832
deadbeefeb459812015-12-15 19:24:43 -08001833// Test that it's possible to call AddTrack on a MediaStream after adding
1834// the stream to a PeerConnection.
1835// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001836// Don't run under Unified Plan since the stream API is not available.
1837TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001838 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001839 // Create audio stream and add to PeerConnection.
Steve Anton36da6ff2018-02-16 16:04:20 -08001840 AddAudioStream(kStreamLabel1);
deadbeefeb459812015-12-15 19:24:43 -08001841 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1842
1843 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<VideoTrackInterface> video_track(
1845 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001846 "video_label", pc_factory_->CreateVideoSource(
1847 std::unique_ptr<cricket::VideoCapturer>(
1848 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001849 stream->AddTrack(video_track.get());
1850
kwibergd1fe2812016-04-27 06:47:29 -07001851 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001852 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001853
1854 const cricket::MediaContentDescription* video_desc =
1855 cricket::GetFirstVideoContentDescription(offer->description());
1856 EXPECT_TRUE(video_desc != nullptr);
1857}
1858
1859// Test that it's possible to call RemoveTrack on a MediaStream after adding
1860// the stream to a PeerConnection.
1861// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001862// Don't run under Unified Plan since the stream API is not available.
1863TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001864 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001865 // Create audio/video stream and add to PeerConnection.
1866 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1867 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1868
1869 // Remove the video track.
1870 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1871
kwibergd1fe2812016-04-27 06:47:29 -07001872 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001873 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001874
1875 const cricket::MediaContentDescription* video_desc =
1876 cricket::GetFirstVideoContentDescription(offer->description());
1877 EXPECT_TRUE(video_desc == nullptr);
1878}
1879
deadbeef1dcb1642017-03-29 21:08:16 -07001880// Verify that CreateDtmfSender only succeeds if called with a valid local
1881// track. Other aspects of DtmfSenders are tested in
1882// peerconnection_integrationtest.cc.
Steve Anton36da6ff2018-02-16 16:04:20 -08001883TEST_P(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
deadbeef1dcb1642017-03-29 21:08:16 -07001884 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08001885 AddAudioTrack(kAudioTracks[0]);
deadbeef1dcb1642017-03-29 21:08:16 -07001886 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1887 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1888 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1889 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1890}
1891
deadbeefbd7d8f72015-12-18 16:58:44 -08001892// Test creating a sender with a stream ID, and ensure the ID is populated
1893// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001894// Don't run under Unified Plan since the stream API is not available.
1895TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001896 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001897 pc_->CreateSender("video", kStreamLabel1);
1898
kwibergd1fe2812016-04-27 06:47:29 -07001899 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001900 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001901
1902 const cricket::MediaContentDescription* video_desc =
1903 cricket::GetFirstVideoContentDescription(offer->description());
1904 ASSERT_TRUE(video_desc != nullptr);
1905 ASSERT_EQ(1u, video_desc->streams().size());
1906 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1907}
1908
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001910TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1911 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
1912 // TODO(bugs.webrtc.org/8764): Re-enable when stats are supported with
1913 // Unified Plan.
1914 return;
1915 }
1916
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001918 ASSERT_LT(0u, pc_->GetSenders().size());
1919 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001920 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001921 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 EXPECT_TRUE(DoGetStats(remote_audio));
1923
1924 // Remove the stream. Since we are sending to our selves the local
1925 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001926 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 // Do a re-negotiation.
1928 CreateOfferReceiveAnswer();
1929
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 // Test that we still can get statistics for the old track. Even if it is not
1931 // sent any longer.
1932 EXPECT_TRUE(DoGetStats(remote_audio));
1933}
1934
1935// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001936TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1937 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
1938 // TODO(bugs.webrtc.org/8764): Re-enable when stats are supported with
1939 // Unified Plan.
1940 return;
1941 }
1942
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001944 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1945 ASSERT_TRUE(video_receiver);
1946 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947}
1948
1949// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001950TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
1951 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
1952 // TODO(bugs.webrtc.org/8764): Re-enable when stats are supported with
1953 // Unified Plan.
1954 return;
1955 }
1956
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001958 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 pc_factory_->CreateAudioTrack("unknown track", NULL));
1960 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1961}
1962
Steve Anton36da6ff2018-02-16 16:04:20 -08001963TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
1964 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
1965 // TODO(bugs.webrtc.org/8764): Re-enable when stats are supported with
1966 // Unified Plan.
1967 return;
1968 }
1969
Harald Alvestrand89061872018-01-02 14:08:34 +01001970 CreatePeerConnectionWithoutDtls();
1971 EXPECT_TRUE(DoGetRTCStats());
1972 // Clearing stats cache is needed now, but should be temporary.
1973 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1974 pc_->ClearStatsCache();
Steve Anton36da6ff2018-02-16 16:04:20 -08001975 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
1976 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001977 EXPECT_TRUE(DoGetRTCStats());
1978 pc_->ClearStatsCache();
1979 CreateOfferReceiveAnswer();
1980 EXPECT_TRUE(DoGetRTCStats());
1981}
1982
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001984TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 FakeConstraints constraints;
1986 constraints.SetAllowRtpDataChannels();
1987 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001988 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001990 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 pc_->CreateDataChannel("test2", NULL);
1992 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001993 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001995 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 new MockDataChannelObserver(data2));
1997
1998 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1999 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
2000 std::string data_to_send1 = "testing testing";
2001 std::string data_to_send2 = "testing something else";
2002 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
2003
2004 CreateOfferReceiveAnswer();
2005 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2006 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2007
2008 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
2009 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
2010 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
2011 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
2012
2013 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
2014 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
2015
2016 data1->Close();
2017 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2018 CreateOfferReceiveAnswer();
2019 EXPECT_FALSE(observer1->IsOpen());
2020 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2021 EXPECT_TRUE(observer2->IsOpen());
2022
2023 data_to_send2 = "testing something else again";
2024 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
2025
2026 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
2027}
2028
2029// This test verifies that sendnig binary data over RTP data channels should
2030// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002031TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 FakeConstraints constraints;
2033 constraints.SetAllowRtpDataChannels();
2034 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07002035 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002037 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 pc_->CreateDataChannel("test2", NULL);
2039 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002040 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002042 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 new MockDataChannelObserver(data2));
2044
2045 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
2046 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
2047
2048 CreateOfferReceiveAnswer();
2049 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2050 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2051
2052 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
2053 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
2054
jbaucheec21bd2016-03-20 06:15:43 -07002055 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
2057}
2058
2059// This test setup a RTP data channels in loop back and test that a channel is
2060// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08002061TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 FakeConstraints constraints;
2063 constraints.SetAllowRtpDataChannels();
2064 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07002065 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002067 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 new MockDataChannelObserver(data1));
2069
2070 CreateOfferReceiveAnswerWithoutSsrc();
2071
2072 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2073
2074 data1->Close();
2075 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2076 CreateOfferReceiveAnswerWithoutSsrc();
2077 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2078 EXPECT_FALSE(observer1->IsOpen());
2079}
2080
2081// This test that if a data channel is added in an answer a receive only channel
2082// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08002083TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 FakeConstraints constraints;
2085 constraints.SetAllowRtpDataChannels();
2086 CreatePeerConnection(&constraints);
2087
2088 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002089 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 pc_->CreateDataChannel(offer_label, NULL);
2091
2092 CreateOfferAsLocalDescription();
2093
2094 // Replace the data channel label in the offer and apply it as an answer.
2095 std::string receive_label = "answer_channel";
2096 std::string sdp;
2097 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002098 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 receive_label.c_str(), receive_label.length(),
2100 &sdp);
2101 CreateAnswerAsRemoteDescription(sdp);
2102
2103 // Verify that a new incoming data channel has been created and that
2104 // it is open but can't we written to.
2105 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2106 DataChannelInterface* received_channel = observer_.last_datachannel_;
2107 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2108 EXPECT_EQ(receive_label, received_channel->label());
2109 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2110
2111 // Verify that the channel we initially offered has been rejected.
2112 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2113
2114 // Do another offer / answer exchange and verify that the data channel is
2115 // opened.
2116 CreateOfferReceiveAnswer();
2117 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2118 kTimeout);
2119}
2120
2121// This test that no data channel is returned if a reliable channel is
2122// requested.
2123// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002124TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 FakeConstraints constraints;
2126 constraints.SetAllowRtpDataChannels();
2127 CreatePeerConnection(&constraints);
2128
2129 std::string label = "test";
2130 webrtc::DataChannelInit config;
2131 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002132 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 pc_->CreateDataChannel(label, &config);
2134 EXPECT_TRUE(channel == NULL);
2135}
2136
deadbeefab9b2d12015-10-14 11:33:11 -07002137// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002138TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
deadbeefab9b2d12015-10-14 11:33:11 -07002139 FakeConstraints constraints;
2140 constraints.SetAllowRtpDataChannels();
2141 CreatePeerConnection(&constraints);
2142
2143 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002144 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002145 pc_->CreateDataChannel(label, nullptr);
2146 EXPECT_NE(channel, nullptr);
2147
zhihuang9763d562016-08-05 11:14:50 -07002148 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002149 pc_->CreateDataChannel(label, nullptr);
2150 EXPECT_EQ(dup_channel, nullptr);
2151}
2152
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153// This tests that a SCTP data channel is returned using different
2154// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002155TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 FakeConstraints constraints;
2157 constraints.SetAllowDtlsSctpDataChannels();
2158 CreatePeerConnection(&constraints);
2159
2160 webrtc::DataChannelInit config;
2161
zhihuang9763d562016-08-05 11:14:50 -07002162 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 pc_->CreateDataChannel("1", &config);
2164 EXPECT_TRUE(channel != NULL);
2165 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002166 EXPECT_TRUE(observer_.renegotiation_needed_);
2167 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168
2169 config.ordered = false;
2170 channel = pc_->CreateDataChannel("2", &config);
2171 EXPECT_TRUE(channel != NULL);
2172 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002173 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174
2175 config.ordered = true;
2176 config.maxRetransmits = 0;
2177 channel = pc_->CreateDataChannel("3", &config);
2178 EXPECT_TRUE(channel != NULL);
2179 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002180 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181
2182 config.maxRetransmits = -1;
2183 config.maxRetransmitTime = 0;
2184 channel = pc_->CreateDataChannel("4", &config);
2185 EXPECT_TRUE(channel != NULL);
2186 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002187 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
2190// This tests that no data channel is returned if both maxRetransmits and
2191// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002192TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 CreateSctpDataChannelShouldFailForInvalidConfig) {
2194 FakeConstraints constraints;
2195 constraints.SetAllowDtlsSctpDataChannels();
2196 CreatePeerConnection(&constraints);
2197
2198 std::string label = "test";
2199 webrtc::DataChannelInit config;
2200 config.maxRetransmits = 0;
2201 config.maxRetransmitTime = 0;
2202
zhihuang9763d562016-08-05 11:14:50 -07002203 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 pc_->CreateDataChannel(label, &config);
2205 EXPECT_TRUE(channel == NULL);
2206}
2207
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208// The test verifies that creating a SCTP data channel with an id already in use
2209// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002210TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 CreateSctpDataChannelWithInvalidIdShouldFail) {
2212 FakeConstraints constraints;
2213 constraints.SetAllowDtlsSctpDataChannels();
2214 CreatePeerConnection(&constraints);
2215
2216 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002217 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002219 config.id = 1;
2220 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 EXPECT_TRUE(channel != NULL);
2222 EXPECT_EQ(1, channel->id());
2223
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 channel = pc_->CreateDataChannel("x", &config);
2225 EXPECT_TRUE(channel == NULL);
2226
2227 config.id = cricket::kMaxSctpSid;
2228 channel = pc_->CreateDataChannel("max", &config);
2229 EXPECT_TRUE(channel != NULL);
2230 EXPECT_EQ(config.id, channel->id());
2231
2232 config.id = cricket::kMaxSctpSid + 1;
2233 channel = pc_->CreateDataChannel("x", &config);
2234 EXPECT_TRUE(channel == NULL);
2235}
2236
deadbeefab9b2d12015-10-14 11:33:11 -07002237// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002238TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
deadbeefab9b2d12015-10-14 11:33:11 -07002239 FakeConstraints constraints;
2240 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2241 true);
2242 CreatePeerConnection(&constraints);
2243
2244 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002245 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002246 pc_->CreateDataChannel(label, nullptr);
2247 EXPECT_NE(channel, nullptr);
2248
zhihuang9763d562016-08-05 11:14:50 -07002249 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002250 pc_->CreateDataChannel(label, nullptr);
2251 EXPECT_NE(dup_channel, nullptr);
2252}
2253
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002254// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2255// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002256TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002257 FakeConstraints constraints;
2258 constraints.SetAllowRtpDataChannels();
2259 CreatePeerConnection(&constraints);
2260
zhihuang9763d562016-08-05 11:14:50 -07002261 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002262 pc_->CreateDataChannel("test1", NULL);
2263 EXPECT_TRUE(observer_.renegotiation_needed_);
2264 observer_.renegotiation_needed_ = false;
2265
zhihuang9763d562016-08-05 11:14:50 -07002266 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002267 pc_->CreateDataChannel("test2", NULL);
2268 EXPECT_TRUE(observer_.renegotiation_needed_);
2269}
2270
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002272TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273 FakeConstraints constraints;
2274 constraints.SetAllowRtpDataChannels();
2275 CreatePeerConnection(&constraints);
2276
zhihuang9763d562016-08-05 11:14:50 -07002277 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002279 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 pc_->CreateDataChannel("test2", NULL);
2281 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002282 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002284 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 new MockDataChannelObserver(data2));
2286
2287 CreateOfferReceiveAnswer();
2288 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2289 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2290
2291 ReleasePeerConnection();
2292 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2293 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2294}
2295
2296// This test that data channels can be rejected in an answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002297TEST_P(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 FakeConstraints constraints;
2299 constraints.SetAllowRtpDataChannels();
2300 CreatePeerConnection(&constraints);
2301
zhihuang9763d562016-08-05 11:14:50 -07002302 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 pc_->CreateDataChannel("offer_channel", NULL));
2304
2305 CreateOfferAsLocalDescription();
2306
2307 // Create an answer where the m-line for data channels are rejected.
2308 std::string sdp;
2309 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002310 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002311 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002312 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002314 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 data_info->rejected = true;
2316
Steve Antondb45ca82017-09-11 18:27:34 -07002317 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2319}
2320
2321// Test that we can create a session description from an SDP string from
2322// FireFox, use it as a remote session description, generate an answer and use
2323// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002324TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 FakeConstraints constraints;
2326 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2327 true);
2328 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002329 AddAudioTrack("audio_label");
2330 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002331 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002332 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002333 webrtc::kFireFoxSdpOffer, nullptr));
2334 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 CreateAnswerAsLocalDescription();
2336 ASSERT_TRUE(pc_->local_description() != NULL);
2337 ASSERT_TRUE(pc_->remote_description() != NULL);
2338
2339 const cricket::ContentInfo* content =
2340 cricket::GetFirstAudioContent(pc_->local_description()->description());
2341 ASSERT_TRUE(content != NULL);
2342 EXPECT_FALSE(content->rejected);
2343
2344 content =
2345 cricket::GetFirstVideoContent(pc_->local_description()->description());
2346 ASSERT_TRUE(content != NULL);
2347 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002348#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 content =
2350 cricket::GetFirstDataContent(pc_->local_description()->description());
2351 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002352 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002354#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355}
2356
zhihuangb19012e2017-09-19 13:47:59 -07002357// Test that fallback from DTLS to SDES is not supported.
2358// The fallback was previously supported but was removed to simplify the code
2359// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002360TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002361 FakeConstraints constraints;
2362 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2363 true);
2364 CreatePeerConnection(&constraints);
2365 // Wait for fake certificate to be generated. Previously, this is what caused
2366 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002367 AddAudioTrack("audio_label");
2368 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002369 ASSERT_NE(nullptr, fake_certificate_generator_);
2370 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2371 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002372 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002373 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2374 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002375 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002376}
2377
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378// Test that we can create an audio only offer and receive an answer with a
2379// limited set of audio codecs and receive an updated offer with more audio
2380// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002381TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002382 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002383 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 CreateOfferAsLocalDescription();
2385
Steve Anton36da6ff2018-02-16 16:04:20 -08002386 const char* answer_sdp =
2387 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2388 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002389 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002390 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002391 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392
Steve Anton36da6ff2018-02-16 16:04:20 -08002393 const char* reoffer_sdp =
2394 (sdp_semantics_ == SdpSemantics::kPlanB
2395 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2396 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002397 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002398 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002399 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400 CreateAnswerAsLocalDescription();
2401}
2402
deadbeefc80741f2015-10-22 13:14:45 -07002403// Test that if we're receiving (but not sending) a track, subsequent offers
2404// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002405TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
deadbeefc80741f2015-10-22 13:14:45 -07002406 FakeConstraints constraints;
2407 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2408 true);
2409 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002410 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002411 CreateAnswerAsLocalDescription();
2412
2413 // At this point we should be receiving stream 1, but not sending anything.
2414 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002415 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002416 DoCreateOffer(&offer, nullptr);
2417
2418 const cricket::ContentInfo* video_content =
2419 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002420 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2421 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002422
2423 const cricket::ContentInfo* audio_content =
2424 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002425 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2426 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002427}
2428
2429// Test that if we're receiving (but not sending) a track, and the
2430// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2431// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002432TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
deadbeefc80741f2015-10-22 13:14:45 -07002433 FakeConstraints constraints;
2434 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2435 true);
2436 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002437 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002438 CreateAnswerAsLocalDescription();
2439
2440 // At this point we should be receiving stream 1, but not sending anything.
2441 // A new offer would be recvonly, but we'll set the "no receive" constraints
2442 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002443 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002444 FakeConstraints offer_constraints;
2445 offer_constraints.AddMandatory(
2446 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2447 offer_constraints.AddMandatory(
2448 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2449 DoCreateOffer(&offer, &offer_constraints);
2450
2451 const cricket::ContentInfo* video_content =
2452 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002453 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2454 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002455
2456 const cricket::ContentInfo* audio_content =
2457 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002458 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2459 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002460}
2461
deadbeef653b8e02015-11-11 12:55:10 -08002462// Test that we can use SetConfiguration to change the ICE servers of the
2463// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002464TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002465 CreatePeerConnection();
2466
Steve Anton36da6ff2018-02-16 16:04:20 -08002467 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002468 PeerConnectionInterface::IceServer server;
2469 server.uri = "stun:test_hostname";
2470 config.servers.push_back(server);
2471 EXPECT_TRUE(pc_->SetConfiguration(config));
2472
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002473 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2474 EXPECT_EQ("test_hostname",
2475 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002476}
2477
Steve Anton36da6ff2018-02-16 16:04:20 -08002478TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002479 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002480 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002481 config.type = PeerConnectionInterface::kRelay;
2482 EXPECT_TRUE(pc_->SetConfiguration(config));
2483 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2484}
2485
Steve Anton36da6ff2018-02-16 16:04:20 -08002486TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002487 PeerConnectionInterface::RTCConfiguration config;
2488 config.prune_turn_ports = false;
2489 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002490 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002491 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2492
2493 config.prune_turn_ports = true;
2494 EXPECT_TRUE(pc_->SetConfiguration(config));
2495 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2496}
2497
skvladd1f5fda2017-02-03 16:54:05 -08002498// Test that the ice check interval can be changed. This does not verify that
2499// the setting makes it all the way to P2PTransportChannel, as that would
2500// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002501TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002502 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002503 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002504 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002505 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002506 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002507 EXPECT_TRUE(pc_->SetConfiguration(config));
2508 PeerConnectionInterface::RTCConfiguration new_config =
2509 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002510 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002511}
2512
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002513// Test that when SetConfiguration changes both the pool size and other
2514// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002515TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002516 SetConfigurationCreatesPooledSessionCorrectly) {
2517 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002518 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002519 config.ice_candidate_pool_size = 1;
2520 PeerConnectionInterface::IceServer server;
2521 server.uri = kStunAddressOnly;
2522 config.servers.push_back(server);
2523 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002524 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002525
2526 const cricket::FakePortAllocatorSession* session =
2527 static_cast<const cricket::FakePortAllocatorSession*>(
2528 port_allocator_->GetPooledSession());
2529 ASSERT_NE(nullptr, session);
2530 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002531}
2532
deadbeef293e9262017-01-11 12:28:30 -08002533// Test that after SetLocalDescription, changing the pool size is not allowed,
2534// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002535TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002536 CantChangePoolSizeAfterSetLocalDescription) {
2537 CreatePeerConnection();
2538 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002539 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002540 config.ice_candidate_pool_size = 1;
2541 EXPECT_TRUE(pc_->SetConfiguration(config));
2542
2543 // Set remote offer; can still change pool size at this point.
2544 CreateOfferAsRemoteDescription();
2545 config.ice_candidate_pool_size = 2;
2546 EXPECT_TRUE(pc_->SetConfiguration(config));
2547
2548 // Set local answer; now it's too late.
2549 CreateAnswerAsLocalDescription();
2550 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002551 RTCError error;
2552 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2553 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2554}
2555
deadbeef42a42632017-03-10 15:18:00 -08002556// Test that after setting an answer, extra pooled sessions are discarded. The
2557// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002558TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002559 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2560 CreatePeerConnection();
2561
2562 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002563 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002564 config.ice_candidate_pool_size = 4;
2565 EXPECT_TRUE(pc_->SetConfiguration(config));
2566
2567 // Do offer/answer.
2568 CreateOfferAsRemoteDescription();
2569 CreateAnswerAsLocalDescription();
2570
2571 // Expect no pooled sessions to be left.
2572 const cricket::PortAllocatorSession* session =
2573 port_allocator_->GetPooledSession();
2574 EXPECT_EQ(nullptr, session);
2575}
2576
2577// After Close is called, pooled candidates should be discarded so as to not
2578// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002579TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002580 CreatePeerConnection();
2581
Steve Anton36da6ff2018-02-16 16:04:20 -08002582 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002583 config.ice_candidate_pool_size = 3;
2584 EXPECT_TRUE(pc_->SetConfiguration(config));
2585 pc_->Close();
2586
2587 // Expect no pooled sessions to be left.
2588 const cricket::PortAllocatorSession* session =
2589 port_allocator_->GetPooledSession();
2590 EXPECT_EQ(nullptr, session);
2591}
2592
deadbeef293e9262017-01-11 12:28:30 -08002593// Test that SetConfiguration returns an invalid modification error if
2594// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002595TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002596 SetConfigurationReturnsInvalidModificationError) {
2597 PeerConnectionInterface::RTCConfiguration config;
2598 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2599 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2600 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2601 CreatePeerConnection(config, nullptr);
2602
Steve Anton36da6ff2018-02-16 16:04:20 -08002603 PeerConnectionInterface::RTCConfiguration modified_config =
2604 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002605 modified_config.bundle_policy =
2606 PeerConnectionInterface::kBundlePolicyMaxBundle;
2607 RTCError error;
2608 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2609 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2610
Steve Anton36da6ff2018-02-16 16:04:20 -08002611 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002612 modified_config.rtcp_mux_policy =
2613 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2614 error.set_type(RTCErrorType::NONE);
2615 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2616 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2617
Steve Anton36da6ff2018-02-16 16:04:20 -08002618 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002619 modified_config.continual_gathering_policy =
2620 PeerConnectionInterface::GATHER_CONTINUALLY;
2621 error.set_type(RTCErrorType::NONE);
2622 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2623 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2624}
2625
2626// Test that SetConfiguration returns a range error if the candidate pool size
2627// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002628TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002629 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2630 PeerConnectionInterface::RTCConfiguration config;
2631 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002632 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002633
2634 config.ice_candidate_pool_size = -1;
2635 RTCError error;
2636 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2637 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2638
2639 config.ice_candidate_pool_size = INT_MAX;
2640 error.set_type(RTCErrorType::NONE);
2641 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2642 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2643}
2644
2645// Test that SetConfiguration returns a syntax error if parsing an ICE server
2646// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002647TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002648 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2649 PeerConnectionInterface::RTCConfiguration config;
2650 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002651 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002652
2653 PeerConnectionInterface::IceServer bad_server;
2654 bad_server.uri = "stunn:www.example.com";
2655 config.servers.push_back(bad_server);
2656 RTCError error;
2657 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2658 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2659}
2660
2661// Test that SetConfiguration returns an invalid parameter error if a TURN
2662// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002663TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002664 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2665 PeerConnectionInterface::RTCConfiguration config;
2666 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002667 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002668
2669 PeerConnectionInterface::IceServer bad_server;
2670 bad_server.uri = "turn:www.example.com";
2671 // Missing password.
2672 bad_server.username = "foo";
2673 config.servers.push_back(bad_server);
2674 RTCError error;
2675 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2676 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002677}
2678
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002679// Test that PeerConnection::Close changes the states to closed and all remote
2680// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002681TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002682 // Initialize a PeerConnection and negotiate local and remote session
2683 // description.
2684 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002685
2686 // With Plan B, verify the stream count. The analog with Unified Plan is the
2687 // RtpTransceiver count.
2688 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2689 ASSERT_EQ(1u, pc_->local_streams()->count());
2690 ASSERT_EQ(1u, pc_->remote_streams()->count());
2691 } else {
2692 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2693 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002694
2695 pc_->Close();
2696
2697 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2698 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2699 pc_->ice_connection_state());
2700 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2701 pc_->ice_gathering_state());
2702
Steve Anton36da6ff2018-02-16 16:04:20 -08002703 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2704 EXPECT_EQ(1u, pc_->local_streams()->count());
2705 EXPECT_EQ(1u, pc_->remote_streams()->count());
2706 } else {
2707 // Verify that the RtpTransceivers are still present but all stopped.
2708 EXPECT_EQ(2u, pc_->GetTransceivers().size());
2709 for (auto transceiver : pc_->GetTransceivers()) {
2710 EXPECT_TRUE(transceiver->stopped());
2711 }
2712 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002713
Steve Anton36da6ff2018-02-16 16:04:20 -08002714 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2715 ASSERT_TRUE(audio_receiver);
2716 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2717 ASSERT_TRUE(video_receiver);
2718
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002719 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002720 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002721 audio_receiver->track()->state(), kTimeout);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002722 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002723 video_receiver->track()->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002724}
2725
2726// Test that PeerConnection methods fails gracefully after
2727// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002728// Don't run under Unified Plan since the stream API is not available.
2729TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002730 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002731 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2732 CreateOfferAsRemoteDescription();
2733 CreateAnswerAsLocalDescription();
2734
2735 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002736 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002737 pc_->local_streams()->at(0);
2738
2739 pc_->Close();
2740
2741 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002742 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002743
2744 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002745 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002746 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002747 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002748
2749 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2750
2751 EXPECT_TRUE(pc_->local_description() != NULL);
2752 EXPECT_TRUE(pc_->remote_description() != NULL);
2753
kwibergd1fe2812016-04-27 06:47:29 -07002754 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002755 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002756 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002757 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002758
2759 std::string sdp;
2760 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002761 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002762 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002763 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002764
2765 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002766 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002767 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002768 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002769}
2770
2771// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002772TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002773 InitiateCall();
2774 pc_->Close();
2775 DoGetStats(NULL);
2776}
deadbeefab9b2d12015-10-14 11:33:11 -07002777
2778// NOTE: The series of tests below come from what used to be
2779// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2780// setting a remote or local description has the expected effects.
2781
2782// This test verifies that the remote MediaStreams corresponding to a received
2783// SDP string is created. In this test the two separate MediaStreams are
2784// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002785TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
deadbeefab9b2d12015-10-14 11:33:11 -07002786 FakeConstraints constraints;
2787 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2788 true);
2789 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002790 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002791
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002792 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002793 EXPECT_TRUE(
2794 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2795 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2796 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2797
2798 // Create a session description based on another SDP with another
2799 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002800 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002801
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002802 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002803 EXPECT_TRUE(
2804 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2805}
2806
2807// This test verifies that when remote tracks are added/removed from SDP, the
2808// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002809// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2810// specific behavior.
2811TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002812 AddRemoveTrackFromExistingRemoteMediaStream) {
2813 FakeConstraints constraints;
2814 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2815 true);
2816 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002817 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002818 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002819 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002820 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2821 reference_collection_));
2822
2823 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002824 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002825 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002826 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002827 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2828 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002829 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002830 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2831 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002832 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002833 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2834 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002835
2836 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002837 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002838 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002839 MockTrackObserver audio_track_observer(audio_track2);
2840 MockTrackObserver video_track_observer(video_track2);
2841
2842 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2843 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002844 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002845 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2846 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002847 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002848 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002849 audio_track2->state(), kTimeout);
2850 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2851 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002852}
2853
2854// This tests that remote tracks are ended if a local session description is set
2855// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002856TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
deadbeefab9b2d12015-10-14 11:33:11 -07002857 FakeConstraints constraints;
2858 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2859 true);
2860 CreatePeerConnection(&constraints);
2861 // First create and set a remote offer, then reject its video content in our
2862 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002863 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2864 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2865 ASSERT_TRUE(audio_receiver);
2866 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2867 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002868
Steve Anton36da6ff2018-02-16 16:04:20 -08002869 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2870 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002871 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002872 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2873 video_receiver->track();
2874 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002875
kwibergd1fe2812016-04-27 06:47:29 -07002876 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002877 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002878 cricket::ContentInfo* video_info =
2879 local_answer->description()->GetContentByName("video");
2880 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002881 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002882 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2883 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002884
2885 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002886 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002887 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002888 video_info = local_offer->description()->GetContentByName("video");
2889 ASSERT_TRUE(video_info != nullptr);
2890 video_info->rejected = true;
2891 cricket::ContentInfo* audio_info =
2892 local_offer->description()->GetContentByName("audio");
2893 ASSERT_TRUE(audio_info != nullptr);
2894 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002895 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002896 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002897 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2898 kTimeout);
2899 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2900 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002901}
2902
2903// This tests that we won't crash if the remote track has been removed outside
2904// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002905// Don't run under Unified Plan since the stream API is not available.
2906TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
deadbeefab9b2d12015-10-14 11:33:11 -07002907 FakeConstraints constraints;
2908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2909 true);
2910 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002911 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002912 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2913 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2914 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2915
kwibergd1fe2812016-04-27 06:47:29 -07002916 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002917 webrtc::CreateSessionDescription(SdpType::kAnswer,
2918 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002919 cricket::ContentInfo* video_info =
2920 local_answer->description()->GetContentByName("video");
2921 video_info->rejected = true;
2922 cricket::ContentInfo* audio_info =
2923 local_answer->description()->GetContentByName("audio");
2924 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002925 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002926
2927 // No crash is a pass.
2928}
2929
deadbeef5e97fb52015-10-15 12:49:08 -07002930// This tests that if a recvonly remote description is set, no remote streams
2931// will be created, even if the description contains SSRCs/MSIDs.
2932// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002933TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
deadbeef5e97fb52015-10-15 12:49:08 -07002934 FakeConstraints constraints;
2935 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2936 true);
2937 CreatePeerConnection(&constraints);
2938
Steve Anton36da6ff2018-02-16 16:04:20 -08002939 std::string recvonly_offer = GetSdpStringWithStream1();
deadbeef5e97fb52015-10-15 12:49:08 -07002940 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2941 strlen(kRecvonly), &recvonly_offer);
2942 CreateAndSetRemoteOffer(recvonly_offer);
2943
2944 EXPECT_EQ(0u, observer_.remote_streams()->count());
2945}
2946
deadbeefab9b2d12015-10-14 11:33:11 -07002947// This tests that a default MediaStream is created if a remote session
2948// description doesn't contain any streams and no MSID support.
2949// It also tests that the default stream is updated if a video m-line is added
2950// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002951// Don't run under Unified Plan since this behavior is Plan B specific.
2952TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002953 FakeConstraints constraints;
2954 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2955 true);
2956 CreatePeerConnection(&constraints);
2957 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2958
2959 ASSERT_EQ(1u, observer_.remote_streams()->count());
2960 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2961
2962 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2963 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2964 EXPECT_EQ("default", remote_stream->label());
2965
2966 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2967 ASSERT_EQ(1u, observer_.remote_streams()->count());
2968 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2969 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002970 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2971 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002972 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2973 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002974 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2975 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002976}
2977
2978// This tests that a default MediaStream is created if a remote session
2979// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002980// Don't run under Unified Plan since this behavior is Plan B specific.
2981TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002982 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002983 FakeConstraints constraints;
2984 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2985 true);
2986 CreatePeerConnection(&constraints);
2987 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2988
2989 ASSERT_EQ(1u, observer_.remote_streams()->count());
2990 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2991
2992 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2993 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2994 EXPECT_EQ("default", remote_stream->label());
2995}
2996
2997// This tests that it won't crash when PeerConnection tries to remove
2998// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002999// Don't run under Unified Plan since this behavior is Plan B specific.
3000TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003001 FakeConstraints constraints;
3002 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3003 true);
3004 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003005 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07003006 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3007 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
3008 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
3009
3010 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3011
3012 // No crash is a pass.
3013}
3014
3015// This tests that a default MediaStream is created if the remote session
3016// description doesn't contain any streams and don't contain an indication if
3017// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08003018// Don't run under Unified Plan since this behavior is Plan B specific.
3019TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003020 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003021 FakeConstraints constraints;
3022 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3023 true);
3024 CreatePeerConnection(&constraints);
3025 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3026
3027 ASSERT_EQ(1u, observer_.remote_streams()->count());
3028 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3029 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
3030 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
3031}
3032
3033// This tests that a default MediaStream is not created if the remote session
3034// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08003035// Don't run under Unified Plan since this behavior is Plan B specific.
3036TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003037 FakeConstraints constraints;
3038 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3039 true);
3040 CreatePeerConnection(&constraints);
3041 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
3042 EXPECT_EQ(0u, observer_.remote_streams()->count());
3043}
3044
deadbeefbda7e0b2015-12-08 17:13:40 -08003045// This tests that when setting a new description, the old default tracks are
3046// not destroyed and recreated.
3047// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08003048// Don't run under Unified Plan since this behavior is Plan B specific.
3049TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003050 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08003051 FakeConstraints constraints;
3052 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3053 true);
3054 CreatePeerConnection(&constraints);
3055 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3056
3057 ASSERT_EQ(1u, observer_.remote_streams()->count());
3058 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3059 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3060
3061 // Set the track to "disabled", then set a new description and ensure the
3062 // track is still disabled, which ensures it hasn't been recreated.
3063 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3064 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3065 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3066 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3067}
3068
deadbeefab9b2d12015-10-14 11:33:11 -07003069// This tests that a default MediaStream is not created if a remote session
3070// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003071// Don't run under Unified Plan since this behavior is Plan B specific.
3072TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
deadbeefab9b2d12015-10-14 11:33:11 -07003073 FakeConstraints constraints;
3074 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3075 true);
3076 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003077 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003078 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003079 EXPECT_TRUE(
3080 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3081
3082 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3083 EXPECT_EQ(0u, observer_.remote_streams()->count());
3084}
3085
3086// This tests that an RtpSender is created when the local description is set
3087// after adding a local stream.
3088// TODO(deadbeef): This test and the one below it need to be updated when
3089// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003090// Don't run under Unified Plan since this behavior is Plan B specific.
3091TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07003092 FakeConstraints constraints;
3093 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3094 true);
3095 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003096
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003097 // Create an offer with 1 stream with 2 tracks of each type.
3098 rtc::scoped_refptr<StreamCollection> stream_collection =
3099 CreateStreamCollection(1, 2);
3100 pc_->AddStream(stream_collection->at(0));
3101 std::unique_ptr<SessionDescriptionInterface> offer;
3102 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003103 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003104
deadbeefab9b2d12015-10-14 11:33:11 -07003105 auto senders = pc_->GetSenders();
3106 EXPECT_EQ(4u, senders.size());
3107 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3108 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3109 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3110 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3111
3112 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003113 pc_->RemoveStream(stream_collection->at(0));
3114 stream_collection = CreateStreamCollection(1, 1);
3115 pc_->AddStream(stream_collection->at(0));
3116 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003117 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003118
deadbeefab9b2d12015-10-14 11:33:11 -07003119 senders = pc_->GetSenders();
3120 EXPECT_EQ(2u, senders.size());
3121 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3122 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3123 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3124 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3125}
3126
3127// This tests that an RtpSender is created when the local description is set
3128// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003129// Don't run under Unified Plan since this behavior is Plan B specific.
3130TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003131 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07003132 FakeConstraints constraints;
3133 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3134 true);
3135 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003136
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003137 rtc::scoped_refptr<StreamCollection> stream_collection =
3138 CreateStreamCollection(1, 2);
3139 // Add a stream to create the offer, but remove it afterwards.
3140 pc_->AddStream(stream_collection->at(0));
3141 std::unique_ptr<SessionDescriptionInterface> offer;
3142 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3143 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003144
Steve Antondb45ca82017-09-11 18:27:34 -07003145 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003146 auto senders = pc_->GetSenders();
3147 EXPECT_EQ(0u, senders.size());
3148
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003149 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003150 senders = pc_->GetSenders();
3151 EXPECT_EQ(4u, senders.size());
3152 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3153 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3154 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3155 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3156}
3157
3158// This tests that the expected behavior occurs if the SSRC on a local track is
3159// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003160TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003161 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003162 FakeConstraints constraints;
3163 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3164 true);
3165 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003166
Steve Anton36da6ff2018-02-16 16:04:20 -08003167 AddAudioTrack(kAudioTracks[0]);
3168 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003169 std::unique_ptr<SessionDescriptionInterface> offer;
3170 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3171 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08003172 auto modified_offer =
3173 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003174 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3175 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003176 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003177
deadbeefab9b2d12015-10-14 11:33:11 -07003178 auto senders = pc_->GetSenders();
3179 EXPECT_EQ(2u, senders.size());
3180 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3181 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3182
3183 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003184 cricket::MediaContentDescription* desc =
3185 cricket::GetFirstAudioContentDescription(modified_offer->description());
3186 ASSERT_TRUE(desc != NULL);
3187 for (StreamParams& stream : desc->mutable_streams()) {
3188 for (unsigned int& ssrc : stream.ssrcs) {
3189 ++ssrc;
3190 }
3191 }
deadbeefab9b2d12015-10-14 11:33:11 -07003192
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003193 desc =
3194 cricket::GetFirstVideoContentDescription(modified_offer->description());
3195 ASSERT_TRUE(desc != NULL);
3196 for (StreamParams& stream : desc->mutable_streams()) {
3197 for (unsigned int& ssrc : stream.ssrcs) {
3198 ++ssrc;
3199 }
3200 }
3201
Steve Antondb45ca82017-09-11 18:27:34 -07003202 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003203 senders = pc_->GetSenders();
3204 EXPECT_EQ(2u, senders.size());
3205 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3206 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3207 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3208 // changed.
3209}
3210
3211// This tests that the expected behavior occurs if a new session description is
3212// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003213// Don't run under Unified Plan since the stream API is not available.
3214TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003215 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003216 FakeConstraints constraints;
3217 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3218 true);
3219 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003220
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003221 rtc::scoped_refptr<StreamCollection> stream_collection =
3222 CreateStreamCollection(2, 1);
3223 pc_->AddStream(stream_collection->at(0));
3224 std::unique_ptr<SessionDescriptionInterface> offer;
3225 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003226 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003227
deadbeefab9b2d12015-10-14 11:33:11 -07003228 auto senders = pc_->GetSenders();
3229 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003230 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3231 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003232
3233 // Add a new MediaStream but with the same tracks as in the first stream.
3234 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3235 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003236 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3237 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003238 pc_->AddStream(stream_1);
3239
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003240 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003241 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003242
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003243 auto new_senders = pc_->GetSenders();
3244 // Should be the same senders as before, but with updated stream id.
3245 // Note that this behavior is subject to change in the future.
3246 // We may decide the PC should ignore existing tracks in AddStream.
3247 EXPECT_EQ(senders, new_senders);
3248 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3249 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003250}
3251
zhihuang81c3a032016-11-17 12:06:24 -08003252// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003253TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
zhihuang81c3a032016-11-17 12:06:24 -08003254 FakeConstraints constraints;
3255 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3256 true);
3257 CreatePeerConnection(&constraints);
3258 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3259 EXPECT_EQ(observer_.num_added_tracks_, 1);
3260 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3261
3262 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003263 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
3264 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3265 EXPECT_EQ(observer_.num_added_tracks_, 2);
3266 } else {
3267 // With Unified Plan, OnAddTrack will fire every time SetRemoteDescription
3268 // is called until the offer/answer exchange is complete. So in this case
3269 // OnAddTrack is fired twice for the first audio track plus the one time
3270 // for the video track.
3271 EXPECT_EQ(observer_.num_added_tracks_, 3);
3272 }
zhihuang81c3a032016-11-17 12:06:24 -08003273 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3274}
3275
deadbeefd1a38b52016-12-10 13:15:33 -08003276// Test that when SetConfiguration is called and the configuration is
3277// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003278TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003279 PeerConnectionInterface::RTCConfiguration config;
3280 config.type = PeerConnectionInterface::kRelay;
3281 // Need to pass default constraints to prevent disabling of DTLS...
3282 FakeConstraints default_constraints;
3283 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003284 config = pc_->GetConfiguration();
3285 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
3286 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
deadbeefd1a38b52016-12-10 13:15:33 -08003287
3288 // Do initial offer/answer so there's something to restart.
3289 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003290 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003291
3292 // Grab the ufrags.
3293 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3294
3295 // Change ICE policy, which should trigger an ICE restart on the next offer.
3296 config.type = PeerConnectionInterface::kAll;
3297 EXPECT_TRUE(pc_->SetConfiguration(config));
3298 CreateOfferAsLocalDescription();
3299
3300 // Grab the new ufrags.
3301 std::vector<std::string> subsequent_ufrags =
3302 GetUfrags(pc_->local_description());
3303
3304 // Sanity check.
3305 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3306 // Check that each ufrag is different.
3307 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3308 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3309 }
3310}
3311
3312// Test that when SetConfiguration is called and the configuration *isn't*
3313// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003314TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003315 PeerConnectionInterface::RTCConfiguration config;
3316 config.type = PeerConnectionInterface::kRelay;
3317 // Need to pass default constraints to prevent disabling of DTLS...
3318 FakeConstraints default_constraints;
3319 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003320 config = pc_->GetConfiguration();
3321 AddAudioTrack(kAudioTracks[0]);
3322 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003323
3324 // Do initial offer/answer so there's something to restart.
3325 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003326 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003327
3328 // Grab the ufrags.
3329 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3330
3331 // Call SetConfiguration with a config identical to what the PC was
3332 // constructed with.
3333 EXPECT_TRUE(pc_->SetConfiguration(config));
3334 CreateOfferAsLocalDescription();
3335
3336 // Grab the new ufrags.
3337 std::vector<std::string> subsequent_ufrags =
3338 GetUfrags(pc_->local_description());
3339
3340 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3341}
3342
3343// Test for a weird corner case scenario:
3344// 1. Audio/video session established.
3345// 2. SetConfiguration changes ICE config; ICE restart needed.
3346// 3. ICE restart initiated by remote peer, but only for one m= section.
3347// 4. Next createOffer should initiate an ICE restart, but only for the other
3348// m= section; it would be pointless to do an ICE restart for the m= section
3349// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003350TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003351 PeerConnectionInterface::RTCConfiguration config;
3352 config.type = PeerConnectionInterface::kRelay;
3353 // Need to pass default constraints to prevent disabling of DTLS...
3354 FakeConstraints default_constraints;
3355 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003356 config = pc_->GetConfiguration();
3357 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
3358 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
deadbeefd1a38b52016-12-10 13:15:33 -08003359
3360 // Do initial offer/answer so there's something to restart.
3361 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003362 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003363
3364 // Change ICE policy, which should set the "needs-ice-restart" flag.
3365 config.type = PeerConnectionInterface::kAll;
3366 EXPECT_TRUE(pc_->SetConfiguration(config));
3367
3368 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003369 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003370 webrtc::CreateSessionDescription(SdpType::kOffer,
3371 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003372 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003373 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3374 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003375 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003376 CreateAnswerAsLocalDescription();
3377
3378 // Grab the ufrags.
3379 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3380 ASSERT_EQ(2, initial_ufrags.size());
3381
3382 // Create offer and grab the new ufrags.
3383 CreateOfferAsLocalDescription();
3384 std::vector<std::string> subsequent_ufrags =
3385 GetUfrags(pc_->local_description());
3386 ASSERT_EQ(2, subsequent_ufrags.size());
3387
3388 // Ensure that only the ufrag for the second m= section changed.
3389 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3390 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3391}
3392
deadbeeffe4a8a42016-12-20 17:56:17 -08003393// Tests that the methods to return current/pending descriptions work as
3394// expected at different points in the offer/answer exchange. This test does
3395// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003396TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003397 // This disables DTLS so we can apply an answer to ourselves.
3398 CreatePeerConnection();
3399
3400 // Create initial local offer and get SDP (which will also be used as
3401 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003402 std::unique_ptr<SessionDescriptionInterface> local_offer;
3403 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003404 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003405 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003406
3407 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003408 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3409 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3410 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003411 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3412 EXPECT_EQ(nullptr, pc_->current_local_description());
3413 EXPECT_EQ(nullptr, pc_->current_remote_description());
3414
3415 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003416 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003417 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003418 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3419 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3420 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3421 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003422 EXPECT_EQ(nullptr, pc_->current_local_description());
3423 EXPECT_EQ(nullptr, pc_->current_remote_description());
3424
3425 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003426 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003427 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003428 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3429 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003430 EXPECT_EQ(nullptr, pc_->pending_local_description());
3431 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003432 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3433 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003434
3435 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003436 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003437 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003438 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3439 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3440 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003441 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003442 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3443 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003444
3445 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003446 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003447 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003448 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3449 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3450 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3451 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3452 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3453 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003454
3455 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003456 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003457 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003458 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3459 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003460 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3461 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003462 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3463 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003464}
3465
zhihuang77985012017-02-07 15:45:16 -08003466// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3467// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003468// This version tests the StartRtcEventLog version that receives a file.
Steve Anton36da6ff2018-02-16 16:04:20 -08003469TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003470 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003471 CreatePeerConnection();
3472 // The RtcEventLog will be reset when the PeerConnection is closed.
3473 pc_->Close();
3474
Elad Alon9e6565b2017-10-11 16:04:13 +02003475 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3476 std::string filename = webrtc::test::OutputPath() +
3477 test_info->test_case_name() + test_info->name();
3478 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3479
3480 constexpr int64_t max_size_bytes = 1024;
3481
zhihuang77985012017-02-07 15:45:16 -08003482 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3483 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003484
3485 // Cleanup.
3486 rtc::ClosePlatformFile(file);
3487 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003488}
3489
Elad Alon99c3fe52017-10-13 16:29:40 +02003490// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3491// after the PeerConnection is closed.
3492// This version tests the StartRtcEventLog version that receives an object
3493// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003494TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003495 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3496 CreatePeerConnection();
3497 // The RtcEventLog will be reset when the PeerConnection is closed.
3498 pc_->Close();
3499
3500 rtc::PlatformFile file = 0;
3501 int64_t max_size_bytes = 1024;
3502 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003503 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3504 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003505 pc_->StopRtcEventLog();
3506}
3507
deadbeef30952b42017-04-21 02:41:29 -07003508// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003509TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003510 CreatePeerConnection();
3511
3512 // First, create an offer with audio/video.
3513 FakeConstraints constraints;
3514 constraints.SetMandatoryReceiveAudio(true);
3515 constraints.SetMandatoryReceiveVideo(true);
3516 std::unique_ptr<SessionDescriptionInterface> offer;
3517 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3518 cricket::SessionDescription* desc = offer->description();
3519 ASSERT_EQ(2u, desc->transport_infos().size());
3520 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3521 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3522
3523 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003524 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003525 std::unique_ptr<SessionDescriptionInterface> answer;
3526 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3527 desc = answer->description();
3528 ASSERT_EQ(2u, desc->transport_infos().size());
3529 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3530 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3531}
3532
deadbeef1dcb1642017-03-29 21:08:16 -07003533// Test that ICE renomination isn't offered if it's not enabled in the PC's
3534// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003535TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003536 PeerConnectionInterface::RTCConfiguration config;
3537 config.enable_ice_renomination = false;
3538 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08003539 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003540
3541 std::unique_ptr<SessionDescriptionInterface> offer;
3542 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3543 cricket::SessionDescription* desc = offer->description();
3544 EXPECT_EQ(1u, desc->transport_infos().size());
3545 EXPECT_FALSE(
3546 desc->transport_infos()[0].description.GetIceParameters().renomination);
3547}
3548
3549// Test that the ICE renomination option is present in generated offers/answers
3550// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003551TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003552 PeerConnectionInterface::RTCConfiguration config;
3553 config.enable_ice_renomination = true;
3554 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08003555 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003556
3557 std::unique_ptr<SessionDescriptionInterface> offer;
3558 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3559 cricket::SessionDescription* desc = offer->description();
3560 EXPECT_EQ(1u, desc->transport_infos().size());
3561 EXPECT_TRUE(
3562 desc->transport_infos()[0].description.GetIceParameters().renomination);
3563
3564 // Set the offer as a remote description, then create an answer and ensure it
3565 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003566 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003567 std::unique_ptr<SessionDescriptionInterface> answer;
3568 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3569 desc = answer->description();
3570 EXPECT_EQ(1u, desc->transport_infos().size());
3571 EXPECT_TRUE(
3572 desc->transport_infos()[0].description.GetIceParameters().renomination);
3573}
3574
3575// Test that if CreateOffer is called with the deprecated "offer to receive
3576// audio/video" constraints, they're processed and result in an offer with
3577// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003578TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003579 CreatePeerConnection();
3580
3581 FakeConstraints constraints;
3582 constraints.SetMandatoryReceiveAudio(true);
3583 constraints.SetMandatoryReceiveVideo(true);
3584 std::unique_ptr<SessionDescriptionInterface> offer;
3585 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3586
3587 cricket::SessionDescription* desc = offer->description();
3588 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3589 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3590 ASSERT_NE(nullptr, audio);
3591 ASSERT_NE(nullptr, video);
3592 EXPECT_FALSE(audio->rejected);
3593 EXPECT_FALSE(video->rejected);
3594}
3595
3596// Test that if CreateAnswer is called with the deprecated "offer to receive
3597// audio/video" constraints, they're processed and can be used to reject an
3598// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003599// Don't run under Unified Plan since this behavior is not supported.
3600TEST_F(PeerConnectionInterfaceTestPlanB,
3601 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003602 CreatePeerConnection();
3603
3604 // First, create an offer with audio/video and apply it as a remote
3605 // description.
3606 FakeConstraints constraints;
3607 constraints.SetMandatoryReceiveAudio(true);
3608 constraints.SetMandatoryReceiveVideo(true);
3609 std::unique_ptr<SessionDescriptionInterface> offer;
3610 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003611 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003612
3613 // Now create answer that rejects audio/video.
3614 constraints.SetMandatoryReceiveAudio(false);
3615 constraints.SetMandatoryReceiveVideo(false);
3616 std::unique_ptr<SessionDescriptionInterface> answer;
3617 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3618
3619 cricket::SessionDescription* desc = answer->description();
3620 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3621 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3622 ASSERT_NE(nullptr, audio);
3623 ASSERT_NE(nullptr, video);
3624 EXPECT_TRUE(audio->rejected);
3625 EXPECT_TRUE(video->rejected);
3626}
3627
deadbeef1dcb1642017-03-29 21:08:16 -07003628// Test that negotiation can succeed with a data channel only, and with the max
3629// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003630#ifdef HAVE_SCTP
3631TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3632#else
3633TEST_P(PeerConnectionInterfaceTest,
3634 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3635#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003636 PeerConnectionInterface::RTCConfiguration config;
3637 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3638 CreatePeerConnection(config, nullptr);
3639
3640 // First, create an offer with only a data channel and apply it as a remote
3641 // description.
3642 pc_->CreateDataChannel("test", nullptr);
3643 std::unique_ptr<SessionDescriptionInterface> offer;
3644 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003645 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003646
3647 // Create and set answer as well.
3648 std::unique_ptr<SessionDescriptionInterface> answer;
3649 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003650 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003651}
3652
Steve Anton36da6ff2018-02-16 16:04:20 -08003653TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003654 CreatePeerConnection();
3655 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003656 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003657 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3658}
3659
Steve Anton36da6ff2018-02-16 16:04:20 -08003660TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003661 CreatePeerConnection();
3662 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003663 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003664 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3665}
3666
Steve Anton36da6ff2018-02-16 16:04:20 -08003667TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003668 CreatePeerConnection();
3669 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003670 bitrate.min_bitrate_bps = 5;
3671 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003672 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3673}
3674
Steve Anton36da6ff2018-02-16 16:04:20 -08003675TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003676 CreatePeerConnection();
3677 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003678 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003679 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3680}
3681
Steve Anton36da6ff2018-02-16 16:04:20 -08003682TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003683 CreatePeerConnection();
3684 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003685 bitrate.current_bitrate_bps = 10;
3686 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003687 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3688}
3689
Steve Anton36da6ff2018-02-16 16:04:20 -08003690TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003691 CreatePeerConnection();
3692 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003693 bitrate.min_bitrate_bps = 10;
3694 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003695 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3696}
3697
Steve Anton36da6ff2018-02-16 16:04:20 -08003698TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003699 CreatePeerConnection();
3700 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003701 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003702 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3703}
3704
Steve Anton038834f2017-07-14 15:59:59 -07003705// ice_regather_interval_range requires WebRTC to be configured for continual
3706// gathering already.
Steve Anton36da6ff2018-02-16 16:04:20 -08003707TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003708 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3709 PeerConnectionInterface::RTCConfiguration config;
3710 config.ice_regather_interval_range.emplace(1000, 2000);
3711 config.continual_gathering_policy =
3712 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3713 CreatePeerConnectionExpectFail(config);
3714}
3715
3716// Ensures that there is no error when ice_regather_interval_range is set with
3717// continual gathering enabled.
Steve Anton36da6ff2018-02-16 16:04:20 -08003718TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003719 SetIceRegatherIntervalRangeWithContinualGathering) {
3720 PeerConnectionInterface::RTCConfiguration config;
3721 config.ice_regather_interval_range.emplace(1000, 2000);
3722 config.continual_gathering_policy =
3723 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3724 CreatePeerConnection(config, nullptr);
3725}
3726
Lu Liue4bf6002018-02-20 19:16:26 +00003727// The current bitrate from Call's BitrateConfigMask is currently clamped by
3728// Call's BitrateConfig, which comes from the SDP or a default value. This test
3729// checks that a call to SetBitrate with a current bitrate that will be clamped
3730// succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003731TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003732 CreatePeerConnection();
3733 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003734 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003735 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3736}
3737
zhihuang1c378ed2017-08-17 14:10:50 -07003738// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003739TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003740 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3741 RTCOfferAnswerOptions rtc_options;
3742
3743 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3744 // than kMaxOfferToReceiveMedia should be treated as invalid.
3745 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3746 CreatePeerConnection();
3747 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3748
3749 rtc_options.offer_to_receive_audio =
3750 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3751 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3752}
3753
Steve Anton36da6ff2018-02-16 16:04:20 -08003754TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003755 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3756 RTCOfferAnswerOptions rtc_options;
3757
3758 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3759 // than kMaxOfferToReceiveMedia should be treated as invalid.
3760 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3761 CreatePeerConnection();
3762 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3763
3764 rtc_options.offer_to_receive_video =
3765 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3766 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3767}
3768
3769// Test that the audio and video content will be added to an offer if both
3770// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003771TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003772 RTCOfferAnswerOptions rtc_options;
3773 rtc_options.offer_to_receive_audio = 1;
3774 rtc_options.offer_to_receive_video = 1;
3775
3776 std::unique_ptr<SessionDescriptionInterface> offer;
3777 CreatePeerConnection();
3778 offer = CreateOfferWithOptions(rtc_options);
3779 ASSERT_TRUE(offer);
3780 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3781 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3782}
3783
3784// Test that only audio content will be added to the offer if only
3785// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003786TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003787 RTCOfferAnswerOptions rtc_options;
3788 rtc_options.offer_to_receive_audio = 1;
3789 rtc_options.offer_to_receive_video = 0;
3790
3791 std::unique_ptr<SessionDescriptionInterface> offer;
3792 CreatePeerConnection();
3793 offer = CreateOfferWithOptions(rtc_options);
3794 ASSERT_TRUE(offer);
3795 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3796 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3797}
3798
3799// Test that only video content will be added if only |offer_to_receive_video|
3800// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003801TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003802 RTCOfferAnswerOptions rtc_options;
3803 rtc_options.offer_to_receive_audio = 0;
3804 rtc_options.offer_to_receive_video = 1;
3805
3806 std::unique_ptr<SessionDescriptionInterface> offer;
3807 CreatePeerConnection();
3808 offer = CreateOfferWithOptions(rtc_options);
3809 ASSERT_TRUE(offer);
3810 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3811 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3812}
3813
zhihuang1c378ed2017-08-17 14:10:50 -07003814// Test that no media content will be added to the offer if using default
3815// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003816TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003817 RTCOfferAnswerOptions rtc_options;
3818
3819 std::unique_ptr<SessionDescriptionInterface> offer;
3820 CreatePeerConnection();
3821 offer = CreateOfferWithOptions(rtc_options);
3822 ASSERT_TRUE(offer);
3823 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3824 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3825}
3826
3827// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3828// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003829TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3830 CreatePeerConnection();
3831
zhihuang1c378ed2017-08-17 14:10:50 -07003832 RTCOfferAnswerOptions rtc_options;
3833 rtc_options.ice_restart = false;
3834 rtc_options.offer_to_receive_audio = 1;
3835
3836 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003837 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003838 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3839 auto ufrag1 =
3840 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3841 auto pwd1 =
3842 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003843
3844 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3845 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003846 auto ufrag2 =
3847 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3848 auto pwd2 =
3849 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003850
3851 // |ice_restart| is true, the ufrag/pwd should change.
3852 rtc_options.ice_restart = true;
3853 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003854 auto ufrag3 =
3855 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3856 auto pwd3 =
3857 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003858
3859 EXPECT_EQ(ufrag1, ufrag2);
3860 EXPECT_EQ(pwd1, pwd2);
3861 EXPECT_NE(ufrag2, ufrag3);
3862 EXPECT_NE(pwd2, pwd3);
3863}
3864
3865// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3866// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003867TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003868 RTCOfferAnswerOptions rtc_options;
3869 rtc_options.offer_to_receive_audio = 1;
3870 rtc_options.offer_to_receive_video = 1;
3871
3872 std::unique_ptr<SessionDescriptionInterface> offer;
3873 CreatePeerConnection();
3874
3875 rtc_options.use_rtp_mux = true;
3876 offer = CreateOfferWithOptions(rtc_options);
3877 ASSERT_TRUE(offer);
3878 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3879 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3880 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3881
3882 rtc_options.use_rtp_mux = false;
3883 offer = CreateOfferWithOptions(rtc_options);
3884 ASSERT_TRUE(offer);
3885 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3886 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3887 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3888}
3889
zhihuang141aacb2017-08-29 13:23:53 -07003890// This test ensures OnRenegotiationNeeded is called when we add track with
3891// MediaStream -> AddTrack in the same way it is called when we add track with
3892// PeerConnection -> AddTrack.
3893// The test can be removed once addStream is rewritten in terms of addTrack
3894// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003895// Don't run under Unified Plan since the stream API is not available.
3896TEST_F(PeerConnectionInterfaceTestPlanB,
3897 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003898 CreatePeerConnectionWithoutDtls();
3899 rtc::scoped_refptr<MediaStreamInterface> stream(
3900 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3901 pc_->AddStream(stream);
3902 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3903 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3904 rtc::scoped_refptr<VideoTrackInterface> video_track(
3905 pc_factory_->CreateVideoTrack(
3906 "video_track", pc_factory_->CreateVideoSource(
3907 std::unique_ptr<cricket::VideoCapturer>(
3908 new cricket::FakeVideoCapturer()))));
3909 stream->AddTrack(audio_track);
3910 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3911 observer_.renegotiation_needed_ = false;
3912
3913 stream->AddTrack(video_track);
3914 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3915 observer_.renegotiation_needed_ = false;
3916
3917 stream->RemoveTrack(audio_track);
3918 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3919 observer_.renegotiation_needed_ = false;
3920
3921 stream->RemoveTrack(video_track);
3922 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3923 observer_.renegotiation_needed_ = false;
3924}
3925
Zhi Huangb2d355e2017-10-26 17:26:37 -07003926// Tests that an error is returned if a description is applied that has fewer
3927// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003928TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003929 MediaSectionCountEnforcedForSubsequentOffer) {
3930 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003931 AddAudioTrack("audio_label");
3932 AddVideoTrack("video_label");
3933
Zhi Huangb2d355e2017-10-26 17:26:37 -07003934 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003935 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003936 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3937
3938 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003939 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003940 offer->description()->contents().pop_back();
3941 offer->description()->contents().pop_back();
3942 ASSERT_TRUE(offer->description()->contents().empty());
3943 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3944
3945 std::unique_ptr<SessionDescriptionInterface> answer;
3946 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3947 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3948
3949 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003950 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003951 offer->description()->contents().pop_back();
3952 offer->description()->contents().pop_back();
3953 ASSERT_TRUE(offer->description()->contents().empty());
3954 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3955}
3956
Steve Anton36da6ff2018-02-16 16:04:20 -08003957INSTANTIATE_TEST_CASE_P(PeerConnectionInterfaceTest,
3958 PeerConnectionInterfaceTest,
3959 Values(SdpSemantics::kPlanB,
3960 SdpSemantics::kUnifiedPlan));
3961
nisse51542be2016-02-12 02:27:06 -08003962class PeerConnectionMediaConfigTest : public testing::Test {
3963 protected:
3964 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003965 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003966 pcf_->Initialize();
3967 }
nisseeaabdf62017-05-05 02:23:02 -07003968 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003969 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003970 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003971 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3972 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003973 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003974 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003975 }
3976
zhihuang9763d562016-08-05 11:14:50 -07003977 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003978 MockPeerConnectionObserver observer_;
3979};
3980
3981// This test verifies the default behaviour with no constraints and a
3982// default RTCConfiguration.
3983TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3984 PeerConnectionInterface::RTCConfiguration config;
3985 FakeConstraints constraints;
3986
3987 const cricket::MediaConfig& media_config =
3988 TestCreatePeerConnection(config, &constraints);
3989
3990 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003991 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3992 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003993 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003994 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003995}
3996
3997// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003998// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003999TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
4000 PeerConnectionInterface::RTCConfiguration config;
4001 FakeConstraints constraints;
4002
4003 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
4004 const cricket::MediaConfig& media_config =
4005 TestCreatePeerConnection(config, &constraints);
4006
4007 EXPECT_TRUE(media_config.enable_dscp);
4008}
4009
4010// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07004011// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08004012TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
4013 PeerConnectionInterface::RTCConfiguration config;
4014 FakeConstraints constraints;
4015
4016 constraints.AddOptional(
4017 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
4018 const cricket::MediaConfig media_config =
4019 TestCreatePeerConnection(config, &constraints);
4020
Niels Möller1d7ecd22018-01-18 15:25:12 +01004021 EXPECT_FALSE(media_config.video.enable_cpu_adaptation);
nisse51542be2016-02-12 02:27:06 -08004022}
4023
Niels Möller1d7ecd22018-01-18 15:25:12 +01004024// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07004025// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08004026TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
4027 PeerConnectionInterface::RTCConfiguration config;
4028 FakeConstraints constraints;
4029
Niels Möller71bdda02016-03-31 12:59:59 +02004030 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08004031 const cricket::MediaConfig& media_config =
4032 TestCreatePeerConnection(config, &constraints);
4033
Niels Möller1d7ecd22018-01-18 15:25:12 +01004034 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08004035}
4036
Niels Möller6539f692018-01-18 08:58:50 +01004037// This test verifies that the experiment_cpu_load_estimator flag is
4038// propagated from RTCConfiguration to the PeerConnection.
4039TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
4040 PeerConnectionInterface::RTCConfiguration config;
4041 FakeConstraints constraints;
4042
4043 config.set_experiment_cpu_load_estimator(true);
4044 const cricket::MediaConfig& media_config =
4045 TestCreatePeerConnection(config, &constraints);
4046
4047 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
4048}
4049
nisse0db023a2016-03-01 04:29:59 -08004050// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07004051// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08004052TEST_F(PeerConnectionMediaConfigTest,
4053 TestSuspendBelowMinBitrateConstraintTrue) {
4054 PeerConnectionInterface::RTCConfiguration config;
4055 FakeConstraints constraints;
4056
4057 constraints.AddOptional(
4058 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
4059 true);
4060 const cricket::MediaConfig media_config =
4061 TestCreatePeerConnection(config, &constraints);
4062
4063 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08004064}
4065
deadbeef293e9262017-01-11 12:28:30 -08004066// Tests a few random fields being different.
4067TEST(RTCConfigurationTest, ComparisonOperators) {
4068 PeerConnectionInterface::RTCConfiguration a;
4069 PeerConnectionInterface::RTCConfiguration b;
4070 EXPECT_EQ(a, b);
4071
4072 PeerConnectionInterface::RTCConfiguration c;
4073 c.servers.push_back(PeerConnectionInterface::IceServer());
4074 EXPECT_NE(a, c);
4075
4076 PeerConnectionInterface::RTCConfiguration d;
4077 d.type = PeerConnectionInterface::kRelay;
4078 EXPECT_NE(a, d);
4079
4080 PeerConnectionInterface::RTCConfiguration e;
4081 e.audio_jitter_buffer_max_packets = 5;
4082 EXPECT_NE(a, e);
4083
4084 PeerConnectionInterface::RTCConfiguration f;
4085 f.ice_connection_receiving_timeout = 1337;
4086 EXPECT_NE(a, f);
4087
4088 PeerConnectionInterface::RTCConfiguration g;
4089 g.disable_ipv6 = true;
4090 EXPECT_NE(a, g);
4091
4092 PeerConnectionInterface::RTCConfiguration h(
4093 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4094 EXPECT_NE(a, h);
4095}