blob: fa1581ea83daf6a01ad4d81e43239bb0810a25a8 [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) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001912 ASSERT_LT(0u, pc_->GetSenders().size());
1913 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001914 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001915 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 EXPECT_TRUE(DoGetStats(remote_audio));
1917
1918 // Remove the stream. Since we are sending to our selves the local
1919 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001920 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 // Do a re-negotiation.
1922 CreateOfferReceiveAnswer();
1923
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 // Test that we still can get statistics for the old track. Even if it is not
1925 // sent any longer.
1926 EXPECT_TRUE(DoGetStats(remote_audio));
1927}
1928
1929// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001930TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001932 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1933 ASSERT_TRUE(video_receiver);
1934 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935}
1936
1937// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001938TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001940 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 pc_factory_->CreateAudioTrack("unknown track", NULL));
1942 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1943}
1944
Steve Anton36da6ff2018-02-16 16:04:20 -08001945TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001946 CreatePeerConnectionWithoutDtls();
1947 EXPECT_TRUE(DoGetRTCStats());
1948 // Clearing stats cache is needed now, but should be temporary.
1949 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1950 pc_->ClearStatsCache();
Steve Anton36da6ff2018-02-16 16:04:20 -08001951 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
1952 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001953 EXPECT_TRUE(DoGetRTCStats());
1954 pc_->ClearStatsCache();
1955 CreateOfferReceiveAnswer();
1956 EXPECT_TRUE(DoGetRTCStats());
1957}
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001960TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 FakeConstraints constraints;
1962 constraints.SetAllowRtpDataChannels();
1963 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001964 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001966 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 pc_->CreateDataChannel("test2", NULL);
1968 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001969 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001971 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 new MockDataChannelObserver(data2));
1973
1974 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1975 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1976 std::string data_to_send1 = "testing testing";
1977 std::string data_to_send2 = "testing something else";
1978 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1979
1980 CreateOfferReceiveAnswer();
1981 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1982 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1983
1984 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1985 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1986 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1987 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1988
1989 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1990 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1991
1992 data1->Close();
1993 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1994 CreateOfferReceiveAnswer();
1995 EXPECT_FALSE(observer1->IsOpen());
1996 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1997 EXPECT_TRUE(observer2->IsOpen());
1998
1999 data_to_send2 = "testing something else again";
2000 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
2001
2002 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
2003}
2004
2005// This test verifies that sendnig binary data over RTP data channels should
2006// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002007TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 FakeConstraints constraints;
2009 constraints.SetAllowRtpDataChannels();
2010 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07002011 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002013 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 pc_->CreateDataChannel("test2", NULL);
2015 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002016 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002018 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 new MockDataChannelObserver(data2));
2020
2021 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
2022 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
2023
2024 CreateOfferReceiveAnswer();
2025 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2026 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2027
2028 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
2029 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
2030
jbaucheec21bd2016-03-20 06:15:43 -07002031 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
2033}
2034
2035// This test setup a RTP data channels in loop back and test that a channel is
2036// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08002037TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 FakeConstraints constraints;
2039 constraints.SetAllowRtpDataChannels();
2040 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07002041 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002043 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 new MockDataChannelObserver(data1));
2045
2046 CreateOfferReceiveAnswerWithoutSsrc();
2047
2048 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2049
2050 data1->Close();
2051 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2052 CreateOfferReceiveAnswerWithoutSsrc();
2053 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2054 EXPECT_FALSE(observer1->IsOpen());
2055}
2056
2057// This test that if a data channel is added in an answer a receive only channel
2058// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08002059TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 FakeConstraints constraints;
2061 constraints.SetAllowRtpDataChannels();
2062 CreatePeerConnection(&constraints);
2063
2064 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002065 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 pc_->CreateDataChannel(offer_label, NULL);
2067
2068 CreateOfferAsLocalDescription();
2069
2070 // Replace the data channel label in the offer and apply it as an answer.
2071 std::string receive_label = "answer_channel";
2072 std::string sdp;
2073 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002074 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 receive_label.c_str(), receive_label.length(),
2076 &sdp);
2077 CreateAnswerAsRemoteDescription(sdp);
2078
2079 // Verify that a new incoming data channel has been created and that
2080 // it is open but can't we written to.
2081 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2082 DataChannelInterface* received_channel = observer_.last_datachannel_;
2083 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2084 EXPECT_EQ(receive_label, received_channel->label());
2085 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2086
2087 // Verify that the channel we initially offered has been rejected.
2088 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2089
2090 // Do another offer / answer exchange and verify that the data channel is
2091 // opened.
2092 CreateOfferReceiveAnswer();
2093 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2094 kTimeout);
2095}
2096
2097// This test that no data channel is returned if a reliable channel is
2098// requested.
2099// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002100TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 FakeConstraints constraints;
2102 constraints.SetAllowRtpDataChannels();
2103 CreatePeerConnection(&constraints);
2104
2105 std::string label = "test";
2106 webrtc::DataChannelInit config;
2107 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002108 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 pc_->CreateDataChannel(label, &config);
2110 EXPECT_TRUE(channel == NULL);
2111}
2112
deadbeefab9b2d12015-10-14 11:33:11 -07002113// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002114TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
deadbeefab9b2d12015-10-14 11:33:11 -07002115 FakeConstraints constraints;
2116 constraints.SetAllowRtpDataChannels();
2117 CreatePeerConnection(&constraints);
2118
2119 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002120 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002121 pc_->CreateDataChannel(label, nullptr);
2122 EXPECT_NE(channel, nullptr);
2123
zhihuang9763d562016-08-05 11:14:50 -07002124 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002125 pc_->CreateDataChannel(label, nullptr);
2126 EXPECT_EQ(dup_channel, nullptr);
2127}
2128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129// This tests that a SCTP data channel is returned using different
2130// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002131TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 FakeConstraints constraints;
2133 constraints.SetAllowDtlsSctpDataChannels();
2134 CreatePeerConnection(&constraints);
2135
2136 webrtc::DataChannelInit config;
2137
zhihuang9763d562016-08-05 11:14:50 -07002138 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 pc_->CreateDataChannel("1", &config);
2140 EXPECT_TRUE(channel != NULL);
2141 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002142 EXPECT_TRUE(observer_.renegotiation_needed_);
2143 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 config.ordered = false;
2146 channel = pc_->CreateDataChannel("2", &config);
2147 EXPECT_TRUE(channel != NULL);
2148 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002149 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150
2151 config.ordered = true;
2152 config.maxRetransmits = 0;
2153 channel = pc_->CreateDataChannel("3", &config);
2154 EXPECT_TRUE(channel != NULL);
2155 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002156 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157
2158 config.maxRetransmits = -1;
2159 config.maxRetransmitTime = 0;
2160 channel = pc_->CreateDataChannel("4", &config);
2161 EXPECT_TRUE(channel != NULL);
2162 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002163 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164}
2165
2166// This tests that no data channel is returned if both maxRetransmits and
2167// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002168TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 CreateSctpDataChannelShouldFailForInvalidConfig) {
2170 FakeConstraints constraints;
2171 constraints.SetAllowDtlsSctpDataChannels();
2172 CreatePeerConnection(&constraints);
2173
2174 std::string label = "test";
2175 webrtc::DataChannelInit config;
2176 config.maxRetransmits = 0;
2177 config.maxRetransmitTime = 0;
2178
zhihuang9763d562016-08-05 11:14:50 -07002179 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 pc_->CreateDataChannel(label, &config);
2181 EXPECT_TRUE(channel == NULL);
2182}
2183
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184// The test verifies that creating a SCTP data channel with an id already in use
2185// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002186TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187 CreateSctpDataChannelWithInvalidIdShouldFail) {
2188 FakeConstraints constraints;
2189 constraints.SetAllowDtlsSctpDataChannels();
2190 CreatePeerConnection(&constraints);
2191
2192 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002193 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002195 config.id = 1;
2196 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 EXPECT_TRUE(channel != NULL);
2198 EXPECT_EQ(1, channel->id());
2199
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 channel = pc_->CreateDataChannel("x", &config);
2201 EXPECT_TRUE(channel == NULL);
2202
2203 config.id = cricket::kMaxSctpSid;
2204 channel = pc_->CreateDataChannel("max", &config);
2205 EXPECT_TRUE(channel != NULL);
2206 EXPECT_EQ(config.id, channel->id());
2207
2208 config.id = cricket::kMaxSctpSid + 1;
2209 channel = pc_->CreateDataChannel("x", &config);
2210 EXPECT_TRUE(channel == NULL);
2211}
2212
deadbeefab9b2d12015-10-14 11:33:11 -07002213// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002214TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
deadbeefab9b2d12015-10-14 11:33:11 -07002215 FakeConstraints constraints;
2216 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2217 true);
2218 CreatePeerConnection(&constraints);
2219
2220 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002221 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002222 pc_->CreateDataChannel(label, nullptr);
2223 EXPECT_NE(channel, nullptr);
2224
zhihuang9763d562016-08-05 11:14:50 -07002225 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002226 pc_->CreateDataChannel(label, nullptr);
2227 EXPECT_NE(dup_channel, nullptr);
2228}
2229
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002230// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2231// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002232TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002233 FakeConstraints constraints;
2234 constraints.SetAllowRtpDataChannels();
2235 CreatePeerConnection(&constraints);
2236
zhihuang9763d562016-08-05 11:14:50 -07002237 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002238 pc_->CreateDataChannel("test1", NULL);
2239 EXPECT_TRUE(observer_.renegotiation_needed_);
2240 observer_.renegotiation_needed_ = false;
2241
zhihuang9763d562016-08-05 11:14:50 -07002242 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002243 pc_->CreateDataChannel("test2", NULL);
2244 EXPECT_TRUE(observer_.renegotiation_needed_);
2245}
2246
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002248TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 FakeConstraints constraints;
2250 constraints.SetAllowRtpDataChannels();
2251 CreatePeerConnection(&constraints);
2252
zhihuang9763d562016-08-05 11:14:50 -07002253 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002255 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 pc_->CreateDataChannel("test2", NULL);
2257 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002258 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002260 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 new MockDataChannelObserver(data2));
2262
2263 CreateOfferReceiveAnswer();
2264 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2265 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2266
2267 ReleasePeerConnection();
2268 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2269 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2270}
2271
2272// This test that data channels can be rejected in an answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002273TEST_P(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 FakeConstraints constraints;
2275 constraints.SetAllowRtpDataChannels();
2276 CreatePeerConnection(&constraints);
2277
zhihuang9763d562016-08-05 11:14:50 -07002278 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 pc_->CreateDataChannel("offer_channel", NULL));
2280
2281 CreateOfferAsLocalDescription();
2282
2283 // Create an answer where the m-line for data channels are rejected.
2284 std::string sdp;
2285 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002286 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002287 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002288 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002290 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291 data_info->rejected = true;
2292
Steve Antondb45ca82017-09-11 18:27:34 -07002293 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2295}
2296
2297// Test that we can create a session description from an SDP string from
2298// FireFox, use it as a remote session description, generate an answer and use
2299// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002300TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 FakeConstraints constraints;
2302 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2303 true);
2304 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002305 AddAudioTrack("audio_label");
2306 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002307 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002308 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002309 webrtc::kFireFoxSdpOffer, nullptr));
2310 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 CreateAnswerAsLocalDescription();
2312 ASSERT_TRUE(pc_->local_description() != NULL);
2313 ASSERT_TRUE(pc_->remote_description() != NULL);
2314
2315 const cricket::ContentInfo* content =
2316 cricket::GetFirstAudioContent(pc_->local_description()->description());
2317 ASSERT_TRUE(content != NULL);
2318 EXPECT_FALSE(content->rejected);
2319
2320 content =
2321 cricket::GetFirstVideoContent(pc_->local_description()->description());
2322 ASSERT_TRUE(content != NULL);
2323 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002324#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 content =
2326 cricket::GetFirstDataContent(pc_->local_description()->description());
2327 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002328 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002330#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331}
2332
zhihuangb19012e2017-09-19 13:47:59 -07002333// Test that fallback from DTLS to SDES is not supported.
2334// The fallback was previously supported but was removed to simplify the code
2335// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002336TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002337 FakeConstraints constraints;
2338 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2339 true);
2340 CreatePeerConnection(&constraints);
2341 // Wait for fake certificate to be generated. Previously, this is what caused
2342 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002343 AddAudioTrack("audio_label");
2344 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002345 ASSERT_NE(nullptr, fake_certificate_generator_);
2346 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2347 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002348 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002349 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2350 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002351 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002352}
2353
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354// Test that we can create an audio only offer and receive an answer with a
2355// limited set of audio codecs and receive an updated offer with more audio
2356// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002357TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002358 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002359 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 CreateOfferAsLocalDescription();
2361
Steve Anton36da6ff2018-02-16 16:04:20 -08002362 const char* answer_sdp =
2363 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2364 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002365 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002366 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002367 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368
Steve Anton36da6ff2018-02-16 16:04:20 -08002369 const char* reoffer_sdp =
2370 (sdp_semantics_ == SdpSemantics::kPlanB
2371 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2372 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002373 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002374 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002375 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 CreateAnswerAsLocalDescription();
2377}
2378
deadbeefc80741f2015-10-22 13:14:45 -07002379// Test that if we're receiving (but not sending) a track, subsequent offers
2380// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002381TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
deadbeefc80741f2015-10-22 13:14:45 -07002382 FakeConstraints constraints;
2383 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2384 true);
2385 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002386 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002387 CreateAnswerAsLocalDescription();
2388
2389 // At this point we should be receiving stream 1, but not sending anything.
2390 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002391 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002392 DoCreateOffer(&offer, nullptr);
2393
2394 const cricket::ContentInfo* video_content =
2395 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002396 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2397 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002398
2399 const cricket::ContentInfo* audio_content =
2400 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002401 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2402 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002403}
2404
2405// Test that if we're receiving (but not sending) a track, and the
2406// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2407// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002408TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
deadbeefc80741f2015-10-22 13:14:45 -07002409 FakeConstraints constraints;
2410 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2411 true);
2412 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002413 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002414 CreateAnswerAsLocalDescription();
2415
2416 // At this point we should be receiving stream 1, but not sending anything.
2417 // A new offer would be recvonly, but we'll set the "no receive" constraints
2418 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002419 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002420 FakeConstraints offer_constraints;
2421 offer_constraints.AddMandatory(
2422 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2423 offer_constraints.AddMandatory(
2424 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2425 DoCreateOffer(&offer, &offer_constraints);
2426
2427 const cricket::ContentInfo* video_content =
2428 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002429 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2430 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002431
2432 const cricket::ContentInfo* audio_content =
2433 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002434 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2435 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002436}
2437
deadbeef653b8e02015-11-11 12:55:10 -08002438// Test that we can use SetConfiguration to change the ICE servers of the
2439// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002440TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002441 CreatePeerConnection();
2442
Steve Anton36da6ff2018-02-16 16:04:20 -08002443 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002444 PeerConnectionInterface::IceServer server;
2445 server.uri = "stun:test_hostname";
2446 config.servers.push_back(server);
2447 EXPECT_TRUE(pc_->SetConfiguration(config));
2448
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002449 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2450 EXPECT_EQ("test_hostname",
2451 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002452}
2453
Steve Anton36da6ff2018-02-16 16:04:20 -08002454TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002455 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002456 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002457 config.type = PeerConnectionInterface::kRelay;
2458 EXPECT_TRUE(pc_->SetConfiguration(config));
2459 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2460}
2461
Steve Anton36da6ff2018-02-16 16:04:20 -08002462TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002463 PeerConnectionInterface::RTCConfiguration config;
2464 config.prune_turn_ports = false;
2465 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002466 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002467 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2468
2469 config.prune_turn_ports = true;
2470 EXPECT_TRUE(pc_->SetConfiguration(config));
2471 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2472}
2473
skvladd1f5fda2017-02-03 16:54:05 -08002474// Test that the ice check interval can be changed. This does not verify that
2475// the setting makes it all the way to P2PTransportChannel, as that would
2476// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002477TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002478 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002479 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002480 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002481 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002482 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002483 EXPECT_TRUE(pc_->SetConfiguration(config));
2484 PeerConnectionInterface::RTCConfiguration new_config =
2485 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002486 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002487}
2488
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002489// Test that when SetConfiguration changes both the pool size and other
2490// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002491TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002492 SetConfigurationCreatesPooledSessionCorrectly) {
2493 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002494 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002495 config.ice_candidate_pool_size = 1;
2496 PeerConnectionInterface::IceServer server;
2497 server.uri = kStunAddressOnly;
2498 config.servers.push_back(server);
2499 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002500 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002501
2502 const cricket::FakePortAllocatorSession* session =
2503 static_cast<const cricket::FakePortAllocatorSession*>(
2504 port_allocator_->GetPooledSession());
2505 ASSERT_NE(nullptr, session);
2506 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002507}
2508
deadbeef293e9262017-01-11 12:28:30 -08002509// Test that after SetLocalDescription, changing the pool size is not allowed,
2510// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002511TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002512 CantChangePoolSizeAfterSetLocalDescription) {
2513 CreatePeerConnection();
2514 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002515 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002516 config.ice_candidate_pool_size = 1;
2517 EXPECT_TRUE(pc_->SetConfiguration(config));
2518
2519 // Set remote offer; can still change pool size at this point.
2520 CreateOfferAsRemoteDescription();
2521 config.ice_candidate_pool_size = 2;
2522 EXPECT_TRUE(pc_->SetConfiguration(config));
2523
2524 // Set local answer; now it's too late.
2525 CreateAnswerAsLocalDescription();
2526 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002527 RTCError error;
2528 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2529 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2530}
2531
deadbeef42a42632017-03-10 15:18:00 -08002532// Test that after setting an answer, extra pooled sessions are discarded. The
2533// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002534TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002535 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2536 CreatePeerConnection();
2537
2538 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002539 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002540 config.ice_candidate_pool_size = 4;
2541 EXPECT_TRUE(pc_->SetConfiguration(config));
2542
2543 // Do offer/answer.
2544 CreateOfferAsRemoteDescription();
2545 CreateAnswerAsLocalDescription();
2546
2547 // Expect no pooled sessions to be left.
2548 const cricket::PortAllocatorSession* session =
2549 port_allocator_->GetPooledSession();
2550 EXPECT_EQ(nullptr, session);
2551}
2552
2553// After Close is called, pooled candidates should be discarded so as to not
2554// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002555TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002556 CreatePeerConnection();
2557
Steve Anton36da6ff2018-02-16 16:04:20 -08002558 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002559 config.ice_candidate_pool_size = 3;
2560 EXPECT_TRUE(pc_->SetConfiguration(config));
2561 pc_->Close();
2562
2563 // Expect no pooled sessions to be left.
2564 const cricket::PortAllocatorSession* session =
2565 port_allocator_->GetPooledSession();
2566 EXPECT_EQ(nullptr, session);
2567}
2568
deadbeef293e9262017-01-11 12:28:30 -08002569// Test that SetConfiguration returns an invalid modification error if
2570// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002571TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002572 SetConfigurationReturnsInvalidModificationError) {
2573 PeerConnectionInterface::RTCConfiguration config;
2574 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2575 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2576 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2577 CreatePeerConnection(config, nullptr);
2578
Steve Anton36da6ff2018-02-16 16:04:20 -08002579 PeerConnectionInterface::RTCConfiguration modified_config =
2580 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002581 modified_config.bundle_policy =
2582 PeerConnectionInterface::kBundlePolicyMaxBundle;
2583 RTCError error;
2584 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2585 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2586
Steve Anton36da6ff2018-02-16 16:04:20 -08002587 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002588 modified_config.rtcp_mux_policy =
2589 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2590 error.set_type(RTCErrorType::NONE);
2591 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2592 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2593
Steve Anton36da6ff2018-02-16 16:04:20 -08002594 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002595 modified_config.continual_gathering_policy =
2596 PeerConnectionInterface::GATHER_CONTINUALLY;
2597 error.set_type(RTCErrorType::NONE);
2598 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2599 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2600}
2601
2602// Test that SetConfiguration returns a range error if the candidate pool size
2603// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002604TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002605 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2606 PeerConnectionInterface::RTCConfiguration config;
2607 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002608 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002609
2610 config.ice_candidate_pool_size = -1;
2611 RTCError error;
2612 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2613 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2614
2615 config.ice_candidate_pool_size = INT_MAX;
2616 error.set_type(RTCErrorType::NONE);
2617 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2618 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2619}
2620
2621// Test that SetConfiguration returns a syntax error if parsing an ICE server
2622// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002623TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002624 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2625 PeerConnectionInterface::RTCConfiguration config;
2626 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002627 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002628
2629 PeerConnectionInterface::IceServer bad_server;
2630 bad_server.uri = "stunn:www.example.com";
2631 config.servers.push_back(bad_server);
2632 RTCError error;
2633 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2634 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2635}
2636
2637// Test that SetConfiguration returns an invalid parameter error if a TURN
2638// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002639TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002640 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2641 PeerConnectionInterface::RTCConfiguration config;
2642 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08002643 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002644
2645 PeerConnectionInterface::IceServer bad_server;
2646 bad_server.uri = "turn:www.example.com";
2647 // Missing password.
2648 bad_server.username = "foo";
2649 config.servers.push_back(bad_server);
2650 RTCError error;
2651 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2652 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002653}
2654
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002655// Test that PeerConnection::Close changes the states to closed and all remote
2656// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002657TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002658 // Initialize a PeerConnection and negotiate local and remote session
2659 // description.
2660 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002661
2662 // With Plan B, verify the stream count. The analog with Unified Plan is the
2663 // RtpTransceiver count.
2664 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2665 ASSERT_EQ(1u, pc_->local_streams()->count());
2666 ASSERT_EQ(1u, pc_->remote_streams()->count());
2667 } else {
2668 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2669 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002670
2671 pc_->Close();
2672
2673 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2674 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2675 pc_->ice_connection_state());
2676 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2677 pc_->ice_gathering_state());
2678
Steve Anton36da6ff2018-02-16 16:04:20 -08002679 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2680 EXPECT_EQ(1u, pc_->local_streams()->count());
2681 EXPECT_EQ(1u, pc_->remote_streams()->count());
2682 } else {
2683 // Verify that the RtpTransceivers are still present but all stopped.
2684 EXPECT_EQ(2u, pc_->GetTransceivers().size());
2685 for (auto transceiver : pc_->GetTransceivers()) {
2686 EXPECT_TRUE(transceiver->stopped());
2687 }
2688 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689
Steve Anton36da6ff2018-02-16 16:04:20 -08002690 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2691 ASSERT_TRUE(audio_receiver);
2692 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2693 ASSERT_TRUE(video_receiver);
2694
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002695 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002696 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002697 audio_receiver->track()->state(), kTimeout);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002698 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002699 video_receiver->track()->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700}
2701
2702// Test that PeerConnection methods fails gracefully after
2703// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002704// Don't run under Unified Plan since the stream API is not available.
2705TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002706 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002707 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2708 CreateOfferAsRemoteDescription();
2709 CreateAnswerAsLocalDescription();
2710
2711 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002712 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002713 pc_->local_streams()->at(0);
2714
2715 pc_->Close();
2716
2717 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002718 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719
2720 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002721 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002723 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002724
2725 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2726
2727 EXPECT_TRUE(pc_->local_description() != NULL);
2728 EXPECT_TRUE(pc_->remote_description() != NULL);
2729
kwibergd1fe2812016-04-27 06:47:29 -07002730 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002731 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002732 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002733 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002734
2735 std::string sdp;
2736 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002737 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002738 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002739 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002740
2741 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002742 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002743 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002744 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002745}
2746
2747// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002748TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002749 InitiateCall();
2750 pc_->Close();
2751 DoGetStats(NULL);
2752}
deadbeefab9b2d12015-10-14 11:33:11 -07002753
2754// NOTE: The series of tests below come from what used to be
2755// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2756// setting a remote or local description has the expected effects.
2757
2758// This test verifies that the remote MediaStreams corresponding to a received
2759// SDP string is created. In this test the two separate MediaStreams are
2760// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002761TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
deadbeefab9b2d12015-10-14 11:33:11 -07002762 FakeConstraints constraints;
2763 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2764 true);
2765 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002766 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002767
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002768 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002769 EXPECT_TRUE(
2770 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2771 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2772 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2773
2774 // Create a session description based on another SDP with another
2775 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002776 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002777
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002778 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002779 EXPECT_TRUE(
2780 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2781}
2782
2783// This test verifies that when remote tracks are added/removed from SDP, the
2784// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002785// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2786// specific behavior.
2787TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002788 AddRemoveTrackFromExistingRemoteMediaStream) {
2789 FakeConstraints constraints;
2790 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2791 true);
2792 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002793 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002794 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002795 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002796 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2797 reference_collection_));
2798
2799 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002800 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002801 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002802 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002803 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2804 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002805 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002806 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2807 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002808 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002809 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2810 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002811
2812 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002813 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002814 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002815 MockTrackObserver audio_track_observer(audio_track2);
2816 MockTrackObserver video_track_observer(video_track2);
2817
2818 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2819 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002820 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002821 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2822 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002823 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002824 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002825 audio_track2->state(), kTimeout);
2826 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2827 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002828}
2829
2830// This tests that remote tracks are ended if a local session description is set
2831// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002832TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
deadbeefab9b2d12015-10-14 11:33:11 -07002833 FakeConstraints constraints;
2834 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2835 true);
2836 CreatePeerConnection(&constraints);
2837 // First create and set a remote offer, then reject its video content in our
2838 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002839 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2840 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2841 ASSERT_TRUE(audio_receiver);
2842 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2843 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002844
Steve Anton36da6ff2018-02-16 16:04:20 -08002845 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2846 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002847 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002848 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2849 video_receiver->track();
2850 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002851
kwibergd1fe2812016-04-27 06:47:29 -07002852 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002853 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002854 cricket::ContentInfo* video_info =
2855 local_answer->description()->GetContentByName("video");
2856 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002857 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002858 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2859 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002860
2861 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002862 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002863 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002864 video_info = local_offer->description()->GetContentByName("video");
2865 ASSERT_TRUE(video_info != nullptr);
2866 video_info->rejected = true;
2867 cricket::ContentInfo* audio_info =
2868 local_offer->description()->GetContentByName("audio");
2869 ASSERT_TRUE(audio_info != nullptr);
2870 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002871 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002872 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002873 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2874 kTimeout);
2875 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2876 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002877}
2878
2879// This tests that we won't crash if the remote track has been removed outside
2880// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002881// Don't run under Unified Plan since the stream API is not available.
2882TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
deadbeefab9b2d12015-10-14 11:33:11 -07002883 FakeConstraints constraints;
2884 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2885 true);
2886 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002887 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002888 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2889 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2890 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2891
kwibergd1fe2812016-04-27 06:47:29 -07002892 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002893 webrtc::CreateSessionDescription(SdpType::kAnswer,
2894 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002895 cricket::ContentInfo* video_info =
2896 local_answer->description()->GetContentByName("video");
2897 video_info->rejected = true;
2898 cricket::ContentInfo* audio_info =
2899 local_answer->description()->GetContentByName("audio");
2900 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002901 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002902
2903 // No crash is a pass.
2904}
2905
deadbeef5e97fb52015-10-15 12:49:08 -07002906// This tests that if a recvonly remote description is set, no remote streams
2907// will be created, even if the description contains SSRCs/MSIDs.
2908// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002909TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
deadbeef5e97fb52015-10-15 12:49:08 -07002910 FakeConstraints constraints;
2911 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2912 true);
2913 CreatePeerConnection(&constraints);
2914
Steve Anton36da6ff2018-02-16 16:04:20 -08002915 std::string recvonly_offer = GetSdpStringWithStream1();
deadbeef5e97fb52015-10-15 12:49:08 -07002916 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2917 strlen(kRecvonly), &recvonly_offer);
2918 CreateAndSetRemoteOffer(recvonly_offer);
2919
2920 EXPECT_EQ(0u, observer_.remote_streams()->count());
2921}
2922
deadbeefab9b2d12015-10-14 11:33:11 -07002923// This tests that a default MediaStream is created if a remote session
2924// description doesn't contain any streams and no MSID support.
2925// It also tests that the default stream is updated if a video m-line is added
2926// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002927// Don't run under Unified Plan since this behavior is Plan B specific.
2928TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002929 FakeConstraints constraints;
2930 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2931 true);
2932 CreatePeerConnection(&constraints);
2933 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2934
2935 ASSERT_EQ(1u, observer_.remote_streams()->count());
2936 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2937
2938 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2939 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2940 EXPECT_EQ("default", remote_stream->label());
2941
2942 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2943 ASSERT_EQ(1u, observer_.remote_streams()->count());
2944 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2945 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002946 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2947 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002948 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2949 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002950 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2951 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002952}
2953
2954// This tests that a default MediaStream is created if a remote session
2955// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002956// Don't run under Unified Plan since this behavior is Plan B specific.
2957TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002958 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002959 FakeConstraints constraints;
2960 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2961 true);
2962 CreatePeerConnection(&constraints);
2963 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2964
2965 ASSERT_EQ(1u, observer_.remote_streams()->count());
2966 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2967
2968 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2969 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2970 EXPECT_EQ("default", remote_stream->label());
2971}
2972
2973// This tests that it won't crash when PeerConnection tries to remove
2974// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002975// Don't run under Unified Plan since this behavior is Plan B specific.
2976TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002977 FakeConstraints constraints;
2978 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2979 true);
2980 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08002981 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002982 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2983 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2984 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2985
2986 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2987
2988 // No crash is a pass.
2989}
2990
2991// This tests that a default MediaStream is created if the remote session
2992// description doesn't contain any streams and don't contain an indication if
2993// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002994// Don't run under Unified Plan since this behavior is Plan B specific.
2995TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002996 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002997 FakeConstraints constraints;
2998 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2999 true);
3000 CreatePeerConnection(&constraints);
3001 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3002
3003 ASSERT_EQ(1u, observer_.remote_streams()->count());
3004 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3005 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
3006 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
3007}
3008
3009// This tests that a default MediaStream is not created if the remote session
3010// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08003011// Don't run under Unified Plan since this behavior is Plan B specific.
3012TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003013 FakeConstraints constraints;
3014 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3015 true);
3016 CreatePeerConnection(&constraints);
3017 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
3018 EXPECT_EQ(0u, observer_.remote_streams()->count());
3019}
3020
deadbeefbda7e0b2015-12-08 17:13:40 -08003021// This tests that when setting a new description, the old default tracks are
3022// not destroyed and recreated.
3023// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08003024// Don't run under Unified Plan since this behavior is Plan B specific.
3025TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003026 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08003027 FakeConstraints constraints;
3028 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3029 true);
3030 CreatePeerConnection(&constraints);
3031 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3032
3033 ASSERT_EQ(1u, observer_.remote_streams()->count());
3034 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3035 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3036
3037 // Set the track to "disabled", then set a new description and ensure the
3038 // track is still disabled, which ensures it hasn't been recreated.
3039 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3040 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3041 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3042 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3043}
3044
deadbeefab9b2d12015-10-14 11:33:11 -07003045// This tests that a default MediaStream is not created if a remote session
3046// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003047// Don't run under Unified Plan since this behavior is Plan B specific.
3048TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
deadbeefab9b2d12015-10-14 11:33:11 -07003049 FakeConstraints constraints;
3050 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3051 true);
3052 CreatePeerConnection(&constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003053 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003054 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003055 EXPECT_TRUE(
3056 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3057
3058 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3059 EXPECT_EQ(0u, observer_.remote_streams()->count());
3060}
3061
3062// This tests that an RtpSender is created when the local description is set
3063// after adding a local stream.
3064// TODO(deadbeef): This test and the one below it need to be updated when
3065// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003066// Don't run under Unified Plan since this behavior is Plan B specific.
3067TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07003068 FakeConstraints constraints;
3069 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3070 true);
3071 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003072
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003073 // Create an offer with 1 stream with 2 tracks of each type.
3074 rtc::scoped_refptr<StreamCollection> stream_collection =
3075 CreateStreamCollection(1, 2);
3076 pc_->AddStream(stream_collection->at(0));
3077 std::unique_ptr<SessionDescriptionInterface> offer;
3078 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003079 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003080
deadbeefab9b2d12015-10-14 11:33:11 -07003081 auto senders = pc_->GetSenders();
3082 EXPECT_EQ(4u, senders.size());
3083 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3084 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3085 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3086 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3087
3088 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003089 pc_->RemoveStream(stream_collection->at(0));
3090 stream_collection = CreateStreamCollection(1, 1);
3091 pc_->AddStream(stream_collection->at(0));
3092 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003093 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003094
deadbeefab9b2d12015-10-14 11:33:11 -07003095 senders = pc_->GetSenders();
3096 EXPECT_EQ(2u, senders.size());
3097 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3098 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3099 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3100 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3101}
3102
3103// This tests that an RtpSender is created when the local description is set
3104// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003105// Don't run under Unified Plan since this behavior is Plan B specific.
3106TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003107 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07003108 FakeConstraints constraints;
3109 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3110 true);
3111 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003112
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003113 rtc::scoped_refptr<StreamCollection> stream_collection =
3114 CreateStreamCollection(1, 2);
3115 // Add a stream to create the offer, but remove it afterwards.
3116 pc_->AddStream(stream_collection->at(0));
3117 std::unique_ptr<SessionDescriptionInterface> offer;
3118 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3119 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003120
Steve Antondb45ca82017-09-11 18:27:34 -07003121 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003122 auto senders = pc_->GetSenders();
3123 EXPECT_EQ(0u, senders.size());
3124
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003125 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003126 senders = pc_->GetSenders();
3127 EXPECT_EQ(4u, senders.size());
3128 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3129 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3130 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3131 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3132}
3133
3134// This tests that the expected behavior occurs if the SSRC on a local track is
3135// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003136TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003137 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003138 FakeConstraints constraints;
3139 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3140 true);
3141 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003142
Steve Anton36da6ff2018-02-16 16:04:20 -08003143 AddAudioTrack(kAudioTracks[0]);
3144 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003145 std::unique_ptr<SessionDescriptionInterface> offer;
3146 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3147 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08003148 auto modified_offer =
3149 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003150 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3151 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003152 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003153
deadbeefab9b2d12015-10-14 11:33:11 -07003154 auto senders = pc_->GetSenders();
3155 EXPECT_EQ(2u, senders.size());
3156 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3157 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3158
3159 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003160 cricket::MediaContentDescription* desc =
3161 cricket::GetFirstAudioContentDescription(modified_offer->description());
3162 ASSERT_TRUE(desc != NULL);
3163 for (StreamParams& stream : desc->mutable_streams()) {
3164 for (unsigned int& ssrc : stream.ssrcs) {
3165 ++ssrc;
3166 }
3167 }
deadbeefab9b2d12015-10-14 11:33:11 -07003168
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003169 desc =
3170 cricket::GetFirstVideoContentDescription(modified_offer->description());
3171 ASSERT_TRUE(desc != NULL);
3172 for (StreamParams& stream : desc->mutable_streams()) {
3173 for (unsigned int& ssrc : stream.ssrcs) {
3174 ++ssrc;
3175 }
3176 }
3177
Steve Antondb45ca82017-09-11 18:27:34 -07003178 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003179 senders = pc_->GetSenders();
3180 EXPECT_EQ(2u, senders.size());
3181 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3182 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3183 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3184 // changed.
3185}
3186
3187// This tests that the expected behavior occurs if a new session description is
3188// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003189// Don't run under Unified Plan since the stream API is not available.
3190TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003191 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003192 FakeConstraints constraints;
3193 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3194 true);
3195 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003196
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003197 rtc::scoped_refptr<StreamCollection> stream_collection =
3198 CreateStreamCollection(2, 1);
3199 pc_->AddStream(stream_collection->at(0));
3200 std::unique_ptr<SessionDescriptionInterface> offer;
3201 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003202 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003203
deadbeefab9b2d12015-10-14 11:33:11 -07003204 auto senders = pc_->GetSenders();
3205 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003206 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3207 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003208
3209 // Add a new MediaStream but with the same tracks as in the first stream.
3210 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3211 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003212 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3213 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003214 pc_->AddStream(stream_1);
3215
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003216 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003217 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003218
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003219 auto new_senders = pc_->GetSenders();
3220 // Should be the same senders as before, but with updated stream id.
3221 // Note that this behavior is subject to change in the future.
3222 // We may decide the PC should ignore existing tracks in AddStream.
3223 EXPECT_EQ(senders, new_senders);
3224 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3225 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003226}
3227
zhihuang81c3a032016-11-17 12:06:24 -08003228// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003229TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
zhihuang81c3a032016-11-17 12:06:24 -08003230 FakeConstraints constraints;
3231 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3232 true);
3233 CreatePeerConnection(&constraints);
3234 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3235 EXPECT_EQ(observer_.num_added_tracks_, 1);
3236 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3237
3238 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003239 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
3240 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3241 EXPECT_EQ(observer_.num_added_tracks_, 2);
3242 } else {
3243 // With Unified Plan, OnAddTrack will fire every time SetRemoteDescription
3244 // is called until the offer/answer exchange is complete. So in this case
3245 // OnAddTrack is fired twice for the first audio track plus the one time
3246 // for the video track.
3247 EXPECT_EQ(observer_.num_added_tracks_, 3);
3248 }
zhihuang81c3a032016-11-17 12:06:24 -08003249 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3250}
3251
deadbeefd1a38b52016-12-10 13:15:33 -08003252// Test that when SetConfiguration is called and the configuration is
3253// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003254TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003255 PeerConnectionInterface::RTCConfiguration config;
3256 config.type = PeerConnectionInterface::kRelay;
3257 // Need to pass default constraints to prevent disabling of DTLS...
3258 FakeConstraints default_constraints;
3259 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003260 config = pc_->GetConfiguration();
3261 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
3262 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
deadbeefd1a38b52016-12-10 13:15:33 -08003263
3264 // Do initial offer/answer so there's something to restart.
3265 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003266 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003267
3268 // Grab the ufrags.
3269 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3270
3271 // Change ICE policy, which should trigger an ICE restart on the next offer.
3272 config.type = PeerConnectionInterface::kAll;
3273 EXPECT_TRUE(pc_->SetConfiguration(config));
3274 CreateOfferAsLocalDescription();
3275
3276 // Grab the new ufrags.
3277 std::vector<std::string> subsequent_ufrags =
3278 GetUfrags(pc_->local_description());
3279
3280 // Sanity check.
3281 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3282 // Check that each ufrag is different.
3283 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3284 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3285 }
3286}
3287
3288// Test that when SetConfiguration is called and the configuration *isn't*
3289// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003290TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003291 PeerConnectionInterface::RTCConfiguration config;
3292 config.type = PeerConnectionInterface::kRelay;
3293 // Need to pass default constraints to prevent disabling of DTLS...
3294 FakeConstraints default_constraints;
3295 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003296 config = pc_->GetConfiguration();
3297 AddAudioTrack(kAudioTracks[0]);
3298 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003299
3300 // Do initial offer/answer so there's something to restart.
3301 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003302 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003303
3304 // Grab the ufrags.
3305 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3306
3307 // Call SetConfiguration with a config identical to what the PC was
3308 // constructed with.
3309 EXPECT_TRUE(pc_->SetConfiguration(config));
3310 CreateOfferAsLocalDescription();
3311
3312 // Grab the new ufrags.
3313 std::vector<std::string> subsequent_ufrags =
3314 GetUfrags(pc_->local_description());
3315
3316 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3317}
3318
3319// Test for a weird corner case scenario:
3320// 1. Audio/video session established.
3321// 2. SetConfiguration changes ICE config; ICE restart needed.
3322// 3. ICE restart initiated by remote peer, but only for one m= section.
3323// 4. Next createOffer should initiate an ICE restart, but only for the other
3324// m= section; it would be pointless to do an ICE restart for the m= section
3325// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003326TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003327 PeerConnectionInterface::RTCConfiguration config;
3328 config.type = PeerConnectionInterface::kRelay;
3329 // Need to pass default constraints to prevent disabling of DTLS...
3330 FakeConstraints default_constraints;
3331 CreatePeerConnection(config, &default_constraints);
Steve Anton36da6ff2018-02-16 16:04:20 -08003332 config = pc_->GetConfiguration();
3333 AddAudioTrack(kAudioTracks[0], {kStreamLabel1});
3334 AddVideoTrack(kVideoTracks[0], {kStreamLabel1});
deadbeefd1a38b52016-12-10 13:15:33 -08003335
3336 // Do initial offer/answer so there's something to restart.
3337 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003338 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003339
3340 // Change ICE policy, which should set the "needs-ice-restart" flag.
3341 config.type = PeerConnectionInterface::kAll;
3342 EXPECT_TRUE(pc_->SetConfiguration(config));
3343
3344 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003345 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003346 webrtc::CreateSessionDescription(SdpType::kOffer,
3347 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003348 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003349 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3350 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003351 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003352 CreateAnswerAsLocalDescription();
3353
3354 // Grab the ufrags.
3355 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3356 ASSERT_EQ(2, initial_ufrags.size());
3357
3358 // Create offer and grab the new ufrags.
3359 CreateOfferAsLocalDescription();
3360 std::vector<std::string> subsequent_ufrags =
3361 GetUfrags(pc_->local_description());
3362 ASSERT_EQ(2, subsequent_ufrags.size());
3363
3364 // Ensure that only the ufrag for the second m= section changed.
3365 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3366 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3367}
3368
deadbeeffe4a8a42016-12-20 17:56:17 -08003369// Tests that the methods to return current/pending descriptions work as
3370// expected at different points in the offer/answer exchange. This test does
3371// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003372TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003373 // This disables DTLS so we can apply an answer to ourselves.
3374 CreatePeerConnection();
3375
3376 // Create initial local offer and get SDP (which will also be used as
3377 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003378 std::unique_ptr<SessionDescriptionInterface> local_offer;
3379 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003380 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003381 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003382
3383 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003384 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3385 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3386 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003387 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3388 EXPECT_EQ(nullptr, pc_->current_local_description());
3389 EXPECT_EQ(nullptr, pc_->current_remote_description());
3390
3391 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003392 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003393 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003394 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3395 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3396 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3397 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003398 EXPECT_EQ(nullptr, pc_->current_local_description());
3399 EXPECT_EQ(nullptr, pc_->current_remote_description());
3400
3401 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003402 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003403 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003404 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3405 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003406 EXPECT_EQ(nullptr, pc_->pending_local_description());
3407 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003408 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3409 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003410
3411 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003412 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003413 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003414 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3415 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3416 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003417 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003418 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3419 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003420
3421 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003422 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003423 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003424 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3425 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3426 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3427 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3428 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3429 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003430
3431 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003432 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003433 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003434 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3435 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003436 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3437 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003438 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3439 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003440}
3441
zhihuang77985012017-02-07 15:45:16 -08003442// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3443// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003444// This version tests the StartRtcEventLog version that receives a file.
Steve Anton36da6ff2018-02-16 16:04:20 -08003445TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003446 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003447 CreatePeerConnection();
3448 // The RtcEventLog will be reset when the PeerConnection is closed.
3449 pc_->Close();
3450
Elad Alon9e6565b2017-10-11 16:04:13 +02003451 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3452 std::string filename = webrtc::test::OutputPath() +
3453 test_info->test_case_name() + test_info->name();
3454 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3455
3456 constexpr int64_t max_size_bytes = 1024;
3457
zhihuang77985012017-02-07 15:45:16 -08003458 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3459 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003460
3461 // Cleanup.
3462 rtc::ClosePlatformFile(file);
3463 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003464}
3465
Elad Alon99c3fe52017-10-13 16:29:40 +02003466// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3467// after the PeerConnection is closed.
3468// This version tests the StartRtcEventLog version that receives an object
3469// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003470TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003471 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3472 CreatePeerConnection();
3473 // The RtcEventLog will be reset when the PeerConnection is closed.
3474 pc_->Close();
3475
3476 rtc::PlatformFile file = 0;
3477 int64_t max_size_bytes = 1024;
3478 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003479 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3480 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003481 pc_->StopRtcEventLog();
3482}
3483
deadbeef30952b42017-04-21 02:41:29 -07003484// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003485TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003486 CreatePeerConnection();
3487
3488 // First, create an offer with audio/video.
3489 FakeConstraints constraints;
3490 constraints.SetMandatoryReceiveAudio(true);
3491 constraints.SetMandatoryReceiveVideo(true);
3492 std::unique_ptr<SessionDescriptionInterface> offer;
3493 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3494 cricket::SessionDescription* desc = offer->description();
3495 ASSERT_EQ(2u, desc->transport_infos().size());
3496 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3497 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3498
3499 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003500 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003501 std::unique_ptr<SessionDescriptionInterface> answer;
3502 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3503 desc = answer->description();
3504 ASSERT_EQ(2u, desc->transport_infos().size());
3505 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3506 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3507}
3508
deadbeef1dcb1642017-03-29 21:08:16 -07003509// Test that ICE renomination isn't offered if it's not enabled in the PC's
3510// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003511TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003512 PeerConnectionInterface::RTCConfiguration config;
3513 config.enable_ice_renomination = false;
3514 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08003515 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003516
3517 std::unique_ptr<SessionDescriptionInterface> offer;
3518 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3519 cricket::SessionDescription* desc = offer->description();
3520 EXPECT_EQ(1u, desc->transport_infos().size());
3521 EXPECT_FALSE(
3522 desc->transport_infos()[0].description.GetIceParameters().renomination);
3523}
3524
3525// Test that the ICE renomination option is present in generated offers/answers
3526// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003527TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003528 PeerConnectionInterface::RTCConfiguration config;
3529 config.enable_ice_renomination = true;
3530 CreatePeerConnection(config, nullptr);
Steve Anton36da6ff2018-02-16 16:04:20 -08003531 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003532
3533 std::unique_ptr<SessionDescriptionInterface> offer;
3534 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3535 cricket::SessionDescription* desc = offer->description();
3536 EXPECT_EQ(1u, desc->transport_infos().size());
3537 EXPECT_TRUE(
3538 desc->transport_infos()[0].description.GetIceParameters().renomination);
3539
3540 // Set the offer as a remote description, then create an answer and ensure it
3541 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003542 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003543 std::unique_ptr<SessionDescriptionInterface> answer;
3544 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3545 desc = answer->description();
3546 EXPECT_EQ(1u, desc->transport_infos().size());
3547 EXPECT_TRUE(
3548 desc->transport_infos()[0].description.GetIceParameters().renomination);
3549}
3550
3551// Test that if CreateOffer is called with the deprecated "offer to receive
3552// audio/video" constraints, they're processed and result in an offer with
3553// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003554TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003555 CreatePeerConnection();
3556
3557 FakeConstraints constraints;
3558 constraints.SetMandatoryReceiveAudio(true);
3559 constraints.SetMandatoryReceiveVideo(true);
3560 std::unique_ptr<SessionDescriptionInterface> offer;
3561 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3562
3563 cricket::SessionDescription* desc = offer->description();
3564 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3565 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3566 ASSERT_NE(nullptr, audio);
3567 ASSERT_NE(nullptr, video);
3568 EXPECT_FALSE(audio->rejected);
3569 EXPECT_FALSE(video->rejected);
3570}
3571
3572// Test that if CreateAnswer is called with the deprecated "offer to receive
3573// audio/video" constraints, they're processed and can be used to reject an
3574// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003575// Don't run under Unified Plan since this behavior is not supported.
3576TEST_F(PeerConnectionInterfaceTestPlanB,
3577 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003578 CreatePeerConnection();
3579
3580 // First, create an offer with audio/video and apply it as a remote
3581 // description.
3582 FakeConstraints constraints;
3583 constraints.SetMandatoryReceiveAudio(true);
3584 constraints.SetMandatoryReceiveVideo(true);
3585 std::unique_ptr<SessionDescriptionInterface> offer;
3586 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003587 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003588
3589 // Now create answer that rejects audio/video.
3590 constraints.SetMandatoryReceiveAudio(false);
3591 constraints.SetMandatoryReceiveVideo(false);
3592 std::unique_ptr<SessionDescriptionInterface> answer;
3593 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3594
3595 cricket::SessionDescription* desc = answer->description();
3596 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3597 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3598 ASSERT_NE(nullptr, audio);
3599 ASSERT_NE(nullptr, video);
3600 EXPECT_TRUE(audio->rejected);
3601 EXPECT_TRUE(video->rejected);
3602}
3603
deadbeef1dcb1642017-03-29 21:08:16 -07003604// Test that negotiation can succeed with a data channel only, and with the max
3605// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003606#ifdef HAVE_SCTP
3607TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3608#else
3609TEST_P(PeerConnectionInterfaceTest,
3610 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3611#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003612 PeerConnectionInterface::RTCConfiguration config;
3613 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3614 CreatePeerConnection(config, nullptr);
3615
3616 // First, create an offer with only a data channel and apply it as a remote
3617 // description.
3618 pc_->CreateDataChannel("test", nullptr);
3619 std::unique_ptr<SessionDescriptionInterface> offer;
3620 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003621 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003622
3623 // Create and set answer as well.
3624 std::unique_ptr<SessionDescriptionInterface> answer;
3625 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003626 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003627}
3628
Steve Anton36da6ff2018-02-16 16:04:20 -08003629TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003630 CreatePeerConnection();
3631 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003632 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003633 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3634}
3635
Steve Anton36da6ff2018-02-16 16:04:20 -08003636TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003637 CreatePeerConnection();
3638 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003639 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003640 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3641}
3642
Steve Anton36da6ff2018-02-16 16:04:20 -08003643TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003644 CreatePeerConnection();
3645 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003646 bitrate.min_bitrate_bps = 5;
3647 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003648 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3649}
3650
Steve Anton36da6ff2018-02-16 16:04:20 -08003651TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003652 CreatePeerConnection();
3653 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003654 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003655 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3656}
3657
Steve Anton36da6ff2018-02-16 16:04:20 -08003658TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003659 CreatePeerConnection();
3660 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003661 bitrate.current_bitrate_bps = 10;
3662 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003663 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3664}
3665
Steve Anton36da6ff2018-02-16 16:04:20 -08003666TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003667 CreatePeerConnection();
3668 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003669 bitrate.min_bitrate_bps = 10;
3670 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003671 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3672}
3673
Steve Anton36da6ff2018-02-16 16:04:20 -08003674TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003675 CreatePeerConnection();
3676 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003677 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003678 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3679}
3680
Steve Anton038834f2017-07-14 15:59:59 -07003681// ice_regather_interval_range requires WebRTC to be configured for continual
3682// gathering already.
Steve Anton36da6ff2018-02-16 16:04:20 -08003683TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003684 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3685 PeerConnectionInterface::RTCConfiguration config;
3686 config.ice_regather_interval_range.emplace(1000, 2000);
3687 config.continual_gathering_policy =
3688 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3689 CreatePeerConnectionExpectFail(config);
3690}
3691
3692// Ensures that there is no error when ice_regather_interval_range is set with
3693// continual gathering enabled.
Steve Anton36da6ff2018-02-16 16:04:20 -08003694TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003695 SetIceRegatherIntervalRangeWithContinualGathering) {
3696 PeerConnectionInterface::RTCConfiguration config;
3697 config.ice_regather_interval_range.emplace(1000, 2000);
3698 config.continual_gathering_policy =
3699 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3700 CreatePeerConnection(config, nullptr);
3701}
3702
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003703// The current bitrate from Call's BitrateConstraintsMask is currently clamped
3704// by Call's BitrateConstraints, which comes from the SDP or a default value.
3705// This test checks that a call to SetBitrate with a current bitrate that will
3706// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003707TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003708 CreatePeerConnection();
3709 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003710 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003711 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3712}
3713
zhihuang1c378ed2017-08-17 14:10:50 -07003714// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003715TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003716 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3717 RTCOfferAnswerOptions rtc_options;
3718
3719 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3720 // than kMaxOfferToReceiveMedia should be treated as invalid.
3721 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3722 CreatePeerConnection();
3723 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3724
3725 rtc_options.offer_to_receive_audio =
3726 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3727 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3728}
3729
Steve Anton36da6ff2018-02-16 16:04:20 -08003730TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003731 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3732 RTCOfferAnswerOptions rtc_options;
3733
3734 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3735 // than kMaxOfferToReceiveMedia should be treated as invalid.
3736 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3737 CreatePeerConnection();
3738 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3739
3740 rtc_options.offer_to_receive_video =
3741 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3742 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3743}
3744
3745// Test that the audio and video content will be added to an offer if both
3746// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003747TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003748 RTCOfferAnswerOptions rtc_options;
3749 rtc_options.offer_to_receive_audio = 1;
3750 rtc_options.offer_to_receive_video = 1;
3751
3752 std::unique_ptr<SessionDescriptionInterface> offer;
3753 CreatePeerConnection();
3754 offer = CreateOfferWithOptions(rtc_options);
3755 ASSERT_TRUE(offer);
3756 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3757 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3758}
3759
3760// Test that only audio content will be added to the offer if only
3761// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003762TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003763 RTCOfferAnswerOptions rtc_options;
3764 rtc_options.offer_to_receive_audio = 1;
3765 rtc_options.offer_to_receive_video = 0;
3766
3767 std::unique_ptr<SessionDescriptionInterface> offer;
3768 CreatePeerConnection();
3769 offer = CreateOfferWithOptions(rtc_options);
3770 ASSERT_TRUE(offer);
3771 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3772 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3773}
3774
3775// Test that only video content will be added if only |offer_to_receive_video|
3776// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003777TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003778 RTCOfferAnswerOptions rtc_options;
3779 rtc_options.offer_to_receive_audio = 0;
3780 rtc_options.offer_to_receive_video = 1;
3781
3782 std::unique_ptr<SessionDescriptionInterface> offer;
3783 CreatePeerConnection();
3784 offer = CreateOfferWithOptions(rtc_options);
3785 ASSERT_TRUE(offer);
3786 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3787 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3788}
3789
zhihuang1c378ed2017-08-17 14:10:50 -07003790// Test that no media content will be added to the offer if using default
3791// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003792TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003793 RTCOfferAnswerOptions rtc_options;
3794
3795 std::unique_ptr<SessionDescriptionInterface> offer;
3796 CreatePeerConnection();
3797 offer = CreateOfferWithOptions(rtc_options);
3798 ASSERT_TRUE(offer);
3799 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3800 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3801}
3802
3803// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3804// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003805TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3806 CreatePeerConnection();
3807
zhihuang1c378ed2017-08-17 14:10:50 -07003808 RTCOfferAnswerOptions rtc_options;
3809 rtc_options.ice_restart = false;
3810 rtc_options.offer_to_receive_audio = 1;
3811
3812 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003813 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003814 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3815 auto ufrag1 =
3816 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3817 auto pwd1 =
3818 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003819
3820 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3821 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003822 auto ufrag2 =
3823 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3824 auto pwd2 =
3825 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003826
3827 // |ice_restart| is true, the ufrag/pwd should change.
3828 rtc_options.ice_restart = true;
3829 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003830 auto ufrag3 =
3831 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3832 auto pwd3 =
3833 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003834
3835 EXPECT_EQ(ufrag1, ufrag2);
3836 EXPECT_EQ(pwd1, pwd2);
3837 EXPECT_NE(ufrag2, ufrag3);
3838 EXPECT_NE(pwd2, pwd3);
3839}
3840
3841// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3842// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003843TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003844 RTCOfferAnswerOptions rtc_options;
3845 rtc_options.offer_to_receive_audio = 1;
3846 rtc_options.offer_to_receive_video = 1;
3847
3848 std::unique_ptr<SessionDescriptionInterface> offer;
3849 CreatePeerConnection();
3850
3851 rtc_options.use_rtp_mux = true;
3852 offer = CreateOfferWithOptions(rtc_options);
3853 ASSERT_TRUE(offer);
3854 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3855 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3856 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3857
3858 rtc_options.use_rtp_mux = false;
3859 offer = CreateOfferWithOptions(rtc_options);
3860 ASSERT_TRUE(offer);
3861 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3862 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3863 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3864}
3865
zhihuang141aacb2017-08-29 13:23:53 -07003866// This test ensures OnRenegotiationNeeded is called when we add track with
3867// MediaStream -> AddTrack in the same way it is called when we add track with
3868// PeerConnection -> AddTrack.
3869// The test can be removed once addStream is rewritten in terms of addTrack
3870// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003871// Don't run under Unified Plan since the stream API is not available.
3872TEST_F(PeerConnectionInterfaceTestPlanB,
3873 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003874 CreatePeerConnectionWithoutDtls();
3875 rtc::scoped_refptr<MediaStreamInterface> stream(
3876 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3877 pc_->AddStream(stream);
3878 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3879 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3880 rtc::scoped_refptr<VideoTrackInterface> video_track(
3881 pc_factory_->CreateVideoTrack(
3882 "video_track", pc_factory_->CreateVideoSource(
3883 std::unique_ptr<cricket::VideoCapturer>(
3884 new cricket::FakeVideoCapturer()))));
3885 stream->AddTrack(audio_track);
3886 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3887 observer_.renegotiation_needed_ = false;
3888
3889 stream->AddTrack(video_track);
3890 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3891 observer_.renegotiation_needed_ = false;
3892
3893 stream->RemoveTrack(audio_track);
3894 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3895 observer_.renegotiation_needed_ = false;
3896
3897 stream->RemoveTrack(video_track);
3898 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3899 observer_.renegotiation_needed_ = false;
3900}
3901
Zhi Huangb2d355e2017-10-26 17:26:37 -07003902// Tests that an error is returned if a description is applied that has fewer
3903// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003904TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003905 MediaSectionCountEnforcedForSubsequentOffer) {
3906 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003907 AddAudioTrack("audio_label");
3908 AddVideoTrack("video_label");
3909
Zhi Huangb2d355e2017-10-26 17:26:37 -07003910 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003911 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003912 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3913
3914 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003915 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003916 offer->description()->contents().pop_back();
3917 offer->description()->contents().pop_back();
3918 ASSERT_TRUE(offer->description()->contents().empty());
3919 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3920
3921 std::unique_ptr<SessionDescriptionInterface> answer;
3922 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3923 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3924
3925 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003926 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003927 offer->description()->contents().pop_back();
3928 offer->description()->contents().pop_back();
3929 ASSERT_TRUE(offer->description()->contents().empty());
3930 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3931}
3932
Steve Anton36da6ff2018-02-16 16:04:20 -08003933INSTANTIATE_TEST_CASE_P(PeerConnectionInterfaceTest,
3934 PeerConnectionInterfaceTest,
3935 Values(SdpSemantics::kPlanB,
3936 SdpSemantics::kUnifiedPlan));
3937
nisse51542be2016-02-12 02:27:06 -08003938class PeerConnectionMediaConfigTest : public testing::Test {
3939 protected:
3940 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003941 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003942 pcf_->Initialize();
3943 }
nisseeaabdf62017-05-05 02:23:02 -07003944 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003945 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003946 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003947 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3948 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003949 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003950 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003951 }
3952
zhihuang9763d562016-08-05 11:14:50 -07003953 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003954 MockPeerConnectionObserver observer_;
3955};
3956
3957// This test verifies the default behaviour with no constraints and a
3958// default RTCConfiguration.
3959TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3960 PeerConnectionInterface::RTCConfiguration config;
3961 FakeConstraints constraints;
3962
3963 const cricket::MediaConfig& media_config =
3964 TestCreatePeerConnection(config, &constraints);
3965
3966 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003967 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3968 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003969 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003970 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003971}
3972
3973// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003974// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003975TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3976 PeerConnectionInterface::RTCConfiguration config;
3977 FakeConstraints constraints;
3978
3979 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3980 const cricket::MediaConfig& media_config =
3981 TestCreatePeerConnection(config, &constraints);
3982
3983 EXPECT_TRUE(media_config.enable_dscp);
3984}
3985
3986// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003987// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003988TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3989 PeerConnectionInterface::RTCConfiguration config;
3990 FakeConstraints constraints;
3991
3992 constraints.AddOptional(
3993 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3994 const cricket::MediaConfig media_config =
3995 TestCreatePeerConnection(config, &constraints);
3996
Niels Möller1d7ecd22018-01-18 15:25:12 +01003997 EXPECT_FALSE(media_config.video.enable_cpu_adaptation);
nisse51542be2016-02-12 02:27:06 -08003998}
3999
Niels Möller1d7ecd22018-01-18 15:25:12 +01004000// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07004001// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08004002TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
4003 PeerConnectionInterface::RTCConfiguration config;
4004 FakeConstraints constraints;
4005
Niels Möller71bdda02016-03-31 12:59:59 +02004006 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08004007 const cricket::MediaConfig& media_config =
4008 TestCreatePeerConnection(config, &constraints);
4009
Niels Möller1d7ecd22018-01-18 15:25:12 +01004010 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08004011}
4012
Niels Möller6539f692018-01-18 08:58:50 +01004013// This test verifies that the experiment_cpu_load_estimator flag is
4014// propagated from RTCConfiguration to the PeerConnection.
4015TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
4016 PeerConnectionInterface::RTCConfiguration config;
4017 FakeConstraints constraints;
4018
4019 config.set_experiment_cpu_load_estimator(true);
4020 const cricket::MediaConfig& media_config =
4021 TestCreatePeerConnection(config, &constraints);
4022
4023 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
4024}
4025
nisse0db023a2016-03-01 04:29:59 -08004026// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07004027// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08004028TEST_F(PeerConnectionMediaConfigTest,
4029 TestSuspendBelowMinBitrateConstraintTrue) {
4030 PeerConnectionInterface::RTCConfiguration config;
4031 FakeConstraints constraints;
4032
4033 constraints.AddOptional(
4034 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
4035 true);
4036 const cricket::MediaConfig media_config =
4037 TestCreatePeerConnection(config, &constraints);
4038
4039 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08004040}
4041
deadbeef293e9262017-01-11 12:28:30 -08004042// Tests a few random fields being different.
4043TEST(RTCConfigurationTest, ComparisonOperators) {
4044 PeerConnectionInterface::RTCConfiguration a;
4045 PeerConnectionInterface::RTCConfiguration b;
4046 EXPECT_EQ(a, b);
4047
4048 PeerConnectionInterface::RTCConfiguration c;
4049 c.servers.push_back(PeerConnectionInterface::IceServer());
4050 EXPECT_NE(a, c);
4051
4052 PeerConnectionInterface::RTCConfiguration d;
4053 d.type = PeerConnectionInterface::kRelay;
4054 EXPECT_NE(a, d);
4055
4056 PeerConnectionInterface::RTCConfiguration e;
4057 e.audio_jitter_buffer_max_packets = 5;
4058 EXPECT_NE(a, e);
4059
4060 PeerConnectionInterface::RTCConfiguration f;
4061 f.ice_connection_receiving_timeout = 1337;
4062 EXPECT_NE(a, f);
4063
4064 PeerConnectionInterface::RTCConfiguration g;
4065 g.disable_ipv6 = true;
4066 EXPECT_NE(a, g);
4067
4068 PeerConnectionInterface::RTCConfiguration h(
4069 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4070 EXPECT_NE(a, h);
4071}