blob: 371cdc150d50f0e0e0e94607f766203ecd0af730 [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <limits.h>
12#include <stdint.h>
13#include <string.h>
kwibergd1fe2812016-04-27 06:47:29 -070014#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010017#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Steve Anton1c9c9fc2019-02-14 15:13:09 -080020#include "absl/strings/str_replace.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/optional.h"
22#include "api/audio/audio_mixer.h"
23#include "api/audio_codecs/audio_decoder_factory.h"
24#include "api/audio_codecs/audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "api/call/call_factory_interface.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010028#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/data_channel_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/jsep_session_description.h"
32#include "api/media_stream_interface.h"
33#include "api/media_types.h"
34#include "api/peer_connection_interface.h"
35#include "api/rtc_error.h"
36#include "api/rtc_event_log_output.h"
37#include "api/rtp_receiver_interface.h"
38#include "api/rtp_sender_interface.h"
39#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010040#include "api/scoped_refptr.h"
Anders Carlsson67537952018-05-03 11:28:29 +020041#include "api/video_codecs/builtin_video_decoder_factory.h"
42#include "api/video_codecs/builtin_video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010043#include "api/video_codecs/video_decoder_factory.h"
44#include "api/video_codecs/video_encoder_factory.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020045#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Yves Gerey3e707812018-11-28 16:47:49 +010046#include "logging/rtc_event_log/rtc_event_log.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010047#include "logging/rtc_event_log/rtc_event_log_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010048#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
49#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080050#include "media/base/media_config.h"
51#include "media/base/media_engine.h"
52#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080053#include "media/engine/webrtc_media_engine.h"
54#include "media/sctp/sctp_transport_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010055#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "p2p/base/fake_port_allocator.h"
58#include "p2p/base/p2p_constants.h"
Yves Gerey3e707812018-11-28 16:47:49 +010059#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080060#include "p2p/base/port_allocator.h"
61#include "p2p/base/transport_description.h"
62#include "p2p/base/transport_info.h"
63#include "pc/audio_track.h"
64#include "pc/media_session.h"
65#include "pc/media_stream.h"
66#include "pc/peer_connection.h"
67#include "pc/peer_connection_factory.h"
68#include "pc/rtc_stats_collector.h"
69#include "pc/rtp_sender.h"
70#include "pc/session_description.h"
71#include "pc/stream_collection.h"
72#include "pc/test/fake_audio_capture_module.h"
73#include "pc/test/fake_rtc_certificate_generator.h"
74#include "pc/test/fake_video_track_source.h"
75#include "pc/test/mock_peer_connection_observers.h"
76#include "pc/test/test_sdp_strings.h"
77#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010078#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080079#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020080#include "rtc_base/gunit.h"
Yves Gerey3e707812018-11-28 16:47:49 +010081#include "rtc_base/platform_file.h"
Steve Anton10542f22019-01-11 09:11:00 -080082#include "rtc_base/ref_counted_object.h"
83#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080084#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010085#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080086#include "rtc_base/time_utils.h"
87#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010089#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080090#include "test/testsupport/file_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070091
92#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080093#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070094#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010096namespace webrtc {
97namespace {
98
Seth Hampson845e8782018-03-02 11:34:10 -080099static const char kStreamId1[] = "local_stream_1";
100static const char kStreamId2[] = "local_stream_2";
101static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102static const int kDefaultStunPort = 3478;
103static const char kStunAddressOnly[] = "stun:address";
104static const char kStunInvalidPort[] = "stun:address:-1";
105static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
106static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +0100107static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108static const char kTurnUsername[] = "user";
109static const char kTurnPassword[] = "password";
110static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
deadbeefab9b2d12015-10-14 11:33:11 -0700113static const char kStreams[][8] = {"stream1", "stream2"};
114static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
115static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
116
deadbeef5e97fb52015-10-15 12:49:08 -0700117static const char kRecvonly[] = "recvonly";
118static const char kSendrecv[] = "sendrecv";
119
deadbeefab9b2d12015-10-14 11:33:11 -0700120// Reference SDP with a MediaStream with label "stream1" and audio track with
121// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800122static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700123 "v=0\r\n"
124 "o=- 0 0 IN IP4 127.0.0.1\r\n"
125 "s=-\r\n"
126 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800127 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700128 "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"
deadbeefab9b2d12015-10-14 11:33:11 -0700132 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700133 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800134 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "a=ssrc:1 cname:stream1\r\n"
137 "a=ssrc:1 mslabel:stream1\r\n"
138 "a=ssrc:1 label:audiotrack0\r\n"
139 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800140 "a=ice-ufrag:e5785931\r\n"
141 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
142 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
143 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700145 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800146 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700147 "a=rtpmap:120 VP8/90000\r\n"
148 "a=ssrc:2 cname:stream1\r\n"
149 "a=ssrc:2 mslabel:stream1\r\n"
150 "a=ssrc:2 label:videotrack0\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800151// Same string as above but with the MID changed to the Unified Plan default.
152// This is needed so that this SDP can be used as an answer for a Unified Plan
153// offer.
154static const char kSdpStringWithStream1UnifiedPlan[] =
155 "v=0\r\n"
156 "o=- 0 0 IN IP4 127.0.0.1\r\n"
157 "s=-\r\n"
158 "t=0 0\r\n"
159 "m=audio 1 RTP/AVPF 103\r\n"
160 "a=ice-ufrag:e5785931\r\n"
161 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
162 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
163 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
164 "a=mid:0\r\n"
165 "a=sendrecv\r\n"
166 "a=rtcp-mux\r\n"
167 "a=rtpmap:103 ISAC/16000\r\n"
168 "a=ssrc:1 cname:stream1\r\n"
169 "a=ssrc:1 mslabel:stream1\r\n"
170 "a=ssrc:1 label:audiotrack0\r\n"
171 "m=video 1 RTP/AVPF 120\r\n"
172 "a=ice-ufrag:e5785931\r\n"
173 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
174 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
175 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
176 "a=mid:1\r\n"
177 "a=sendrecv\r\n"
178 "a=rtcp-mux\r\n"
179 "a=rtpmap:120 VP8/90000\r\n"
180 "a=ssrc:2 cname:stream1\r\n"
181 "a=ssrc:2 mslabel:stream1\r\n"
182 "a=ssrc:2 label:videotrack0\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700183
zhihuang81c3a032016-11-17 12:06:24 -0800184// Reference SDP with a MediaStream with label "stream1" and audio track with
185// id "audio_1";
186static const char kSdpStringWithStream1AudioTrackOnly[] =
187 "v=0\r\n"
188 "o=- 0 0 IN IP4 127.0.0.1\r\n"
189 "s=-\r\n"
190 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800191 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800192 "a=ice-ufrag:e5785931\r\n"
193 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
194 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
195 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800196 "a=mid:audio\r\n"
197 "a=sendrecv\r\n"
198 "a=rtpmap:103 ISAC/16000\r\n"
199 "a=ssrc:1 cname:stream1\r\n"
200 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800201 "a=ssrc:1 label:audiotrack0\r\n"
202 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800203
deadbeefab9b2d12015-10-14 11:33:11 -0700204// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
205// MediaStreams have one audio track and one video track.
206// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800207static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700208 "v=0\r\n"
209 "o=- 0 0 IN IP4 127.0.0.1\r\n"
210 "s=-\r\n"
211 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800212 "a=msid-semantic: WMS stream1 stream2\r\n"
213 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700214 "a=ice-ufrag:e5785931\r\n"
215 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
216 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
217 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800220 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=rtpmap:103 ISAC/16000\r\n"
222 "a=ssrc:1 cname:stream1\r\n"
223 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
224 "a=ssrc:3 cname:stream2\r\n"
225 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
226 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800227 "a=ice-ufrag:e5785931\r\n"
228 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
229 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
230 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700231 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700232 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800233 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700234 "a=rtpmap:120 VP8/0\r\n"
235 "a=ssrc:2 cname:stream1\r\n"
236 "a=ssrc:2 msid:stream1 videotrack0\r\n"
237 "a=ssrc:4 cname:stream2\r\n"
238 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800239static const char kSdpStringWithStream1And2UnifiedPlan[] =
240 "v=0\r\n"
241 "o=- 0 0 IN IP4 127.0.0.1\r\n"
242 "s=-\r\n"
243 "t=0 0\r\n"
244 "a=msid-semantic: WMS stream1 stream2\r\n"
245 "m=audio 1 RTP/AVPF 103\r\n"
246 "a=ice-ufrag:e5785931\r\n"
247 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
248 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
249 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
250 "a=mid:0\r\n"
251 "a=sendrecv\r\n"
252 "a=rtcp-mux\r\n"
253 "a=rtpmap:103 ISAC/16000\r\n"
254 "a=ssrc:1 cname:stream1\r\n"
255 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
256 "m=video 1 RTP/AVPF 120\r\n"
257 "a=ice-ufrag:e5785931\r\n"
258 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
259 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
260 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
261 "a=mid:1\r\n"
262 "a=sendrecv\r\n"
263 "a=rtcp-mux\r\n"
264 "a=rtpmap:120 VP8/0\r\n"
265 "a=ssrc:2 cname:stream1\r\n"
266 "a=ssrc:2 msid:stream1 videotrack0\r\n"
267 "m=audio 1 RTP/AVPF 103\r\n"
268 "a=ice-ufrag:e5785931\r\n"
269 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
270 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
271 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
272 "a=mid:2\r\n"
273 "a=sendrecv\r\n"
274 "a=rtcp-mux\r\n"
275 "a=rtpmap:103 ISAC/16000\r\n"
276 "a=ssrc:3 cname:stream2\r\n"
277 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
278 "m=video 1 RTP/AVPF 120\r\n"
279 "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"
283 "a=mid:3\r\n"
284 "a=sendrecv\r\n"
285 "a=rtcp-mux\r\n"
286 "a=rtpmap:120 VP8/0\r\n"
287 "a=ssrc:4 cname:stream2\r\n"
288 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700289
290// Reference SDP without MediaStreams. Msid is not supported.
291static const char kSdpStringWithoutStreams[] =
292 "v=0\r\n"
293 "o=- 0 0 IN IP4 127.0.0.1\r\n"
294 "s=-\r\n"
295 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800296 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700297 "a=ice-ufrag:e5785931\r\n"
298 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
299 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
300 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700301 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700302 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800303 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700304 "a=rtpmap:103 ISAC/16000\r\n"
305 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800306 "a=ice-ufrag:e5785931\r\n"
307 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
308 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
309 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700310 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700311 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800312 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700313 "a=rtpmap:120 VP8/90000\r\n";
314
315// Reference SDP without MediaStreams. Msid is supported.
316static const char kSdpStringWithMsidWithoutStreams[] =
317 "v=0\r\n"
318 "o=- 0 0 IN IP4 127.0.0.1\r\n"
319 "s=-\r\n"
320 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800321 "a=msid-semantic: WMS\r\n"
322 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700323 "a=ice-ufrag:e5785931\r\n"
324 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
325 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
326 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700327 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700328 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800329 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700330 "a=rtpmap:103 ISAC/16000\r\n"
331 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800332 "a=ice-ufrag:e5785931\r\n"
333 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
334 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
335 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700336 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700337 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800338 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700339 "a=rtpmap:120 VP8/90000\r\n";
340
341// Reference SDP without MediaStreams and audio only.
342static const char kSdpStringWithoutStreamsAudioOnly[] =
343 "v=0\r\n"
344 "o=- 0 0 IN IP4 127.0.0.1\r\n"
345 "s=-\r\n"
346 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800347 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700348 "a=ice-ufrag:e5785931\r\n"
349 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
350 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
351 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700352 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700353 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800354 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700355 "a=rtpmap:103 ISAC/16000\r\n";
356
357// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
358static const char kSdpStringSendOnlyWithoutStreams[] =
359 "v=0\r\n"
360 "o=- 0 0 IN IP4 127.0.0.1\r\n"
361 "s=-\r\n"
362 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800363 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700364 "a=ice-ufrag:e5785931\r\n"
365 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
366 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
367 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700368 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700369 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700370 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800371 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700372 "a=rtpmap:103 ISAC/16000\r\n"
373 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800374 "a=ice-ufrag:e5785931\r\n"
375 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
376 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
377 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700378 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700379 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700380 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800381 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700382 "a=rtpmap:120 VP8/90000\r\n";
383
384static const char kSdpStringInit[] =
385 "v=0\r\n"
386 "o=- 0 0 IN IP4 127.0.0.1\r\n"
387 "s=-\r\n"
388 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700389 "a=msid-semantic: WMS\r\n";
390
391static const char kSdpStringAudio[] =
392 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800393 "a=ice-ufrag:e5785931\r\n"
394 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
395 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
396 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700397 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700398 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800399 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700400 "a=rtpmap:103 ISAC/16000\r\n";
401
402static const char kSdpStringVideo[] =
403 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800404 "a=ice-ufrag:e5785931\r\n"
405 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
406 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
407 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700408 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700409 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800410 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700411 "a=rtpmap:120 VP8/90000\r\n";
412
413static const char kSdpStringMs1Audio0[] =
414 "a=ssrc:1 cname:stream1\r\n"
415 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
416
417static const char kSdpStringMs1Video0[] =
418 "a=ssrc:2 cname:stream1\r\n"
419 "a=ssrc:2 msid:stream1 videotrack0\r\n";
420
421static const char kSdpStringMs1Audio1[] =
422 "a=ssrc:3 cname:stream1\r\n"
423 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
424
425static const char kSdpStringMs1Video1[] =
426 "a=ssrc:4 cname:stream1\r\n"
427 "a=ssrc:4 msid:stream1 videotrack1\r\n";
428
deadbeef8662f942017-01-20 21:20:51 -0800429static const char kDtlsSdesFallbackSdp[] =
430 "v=0\r\n"
431 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
432 "s=-\r\n"
433 "c=IN IP4 0.0.0.0\r\n"
434 "t=0 0\r\n"
435 "a=group:BUNDLE audio\r\n"
436 "a=msid-semantic: WMS\r\n"
437 "m=audio 1 RTP/SAVPF 0\r\n"
438 "a=sendrecv\r\n"
439 "a=rtcp-mux\r\n"
440 "a=mid:audio\r\n"
441 "a=ssrc:1 cname:stream1\r\n"
442 "a=ssrc:1 mslabel:stream1\r\n"
443 "a=ssrc:1 label:audiotrack0\r\n"
444 "a=ice-ufrag:e5785931\r\n"
445 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
446 "a=rtpmap:0 pcmu/8000\r\n"
447 "a=fingerprint:sha-1 "
448 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
449 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700450 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800451 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
452 "dummy_session_params\r\n";
453
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100454using ::cricket::StreamParams;
perkjd61bf802016-03-24 03:16:19 -0700455using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800456using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
Steve Anton36da6ff2018-02-16 16:04:20 -0800458using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
459using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700460
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461// Gets the first ssrc of given content type from the ContentInfo.
462bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
463 if (!content_info || !ssrc) {
464 return false;
465 }
466 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800467 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 if (!media_desc || media_desc->streams().empty()) {
469 return false;
470 }
471 *ssrc = media_desc->streams().begin()->first_ssrc();
472 return true;
473}
474
deadbeefd1a38b52016-12-10 13:15:33 -0800475// Get the ufrags out of an SDP blob. Useful for testing ICE restart
476// behavior.
477std::vector<std::string> GetUfrags(
478 const webrtc::SessionDescriptionInterface* desc) {
479 std::vector<std::string> ufrags;
480 for (const cricket::TransportInfo& info :
481 desc->description()->transport_infos()) {
482 ufrags.push_back(info.description.ice_ufrag);
483 }
484 return ufrags;
485}
486
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487void SetSsrcToZero(std::string* sdp) {
488 const char kSdpSsrcAtribute[] = "a=ssrc:";
489 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
490 size_t ssrc_pos = 0;
491 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200492 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 size_t end_ssrc = sdp->find(" ", ssrc_pos);
494 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
495 ssrc_pos = end_ssrc;
496 }
497}
498
deadbeefab9b2d12015-10-14 11:33:11 -0700499// Check if |streams| contains the specified track.
500bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800501 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700502 const std::string& track_id) {
503 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800504 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700505 return true;
506 }
507 }
508 return false;
509}
510
511// Check if |senders| contains the specified sender, by id.
512bool ContainsSender(
513 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
514 const std::string& id) {
515 for (const auto& sender : senders) {
516 if (sender->id() == id) {
517 return true;
518 }
519 }
520 return false;
521}
522
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700523// Check if |senders| contains the specified sender, by id and stream id.
524bool ContainsSender(
525 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
526 const std::string& id,
527 const std::string& stream_id) {
528 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700529 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700530 return true;
531 }
532 }
533 return false;
534}
535
deadbeefab9b2d12015-10-14 11:33:11 -0700536// Create a collection of streams.
537// CreateStreamCollection(1) creates a collection that
538// correspond to kSdpStringWithStream1.
539// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
540rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700541 int number_of_streams,
542 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700543 rtc::scoped_refptr<StreamCollection> local_collection(
544 StreamCollection::Create());
545
546 for (int i = 0; i < number_of_streams; ++i) {
547 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
548 webrtc::MediaStream::Create(kStreams[i]));
549
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700550 for (int j = 0; j < tracks_per_stream; ++j) {
551 // Add a local audio track.
552 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
553 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
554 nullptr));
555 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700556
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700557 // Add a local video track.
558 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
559 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700560 webrtc::FakeVideoTrackSource::Create(),
561 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700562 stream->AddTrack(video_track);
563 }
deadbeefab9b2d12015-10-14 11:33:11 -0700564
565 local_collection->AddStream(stream);
566 }
567 return local_collection;
568}
569
570// Check equality of StreamCollections.
571bool CompareStreamCollections(StreamCollectionInterface* s1,
572 StreamCollectionInterface* s2) {
573 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
574 return false;
575 }
576
577 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700578 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700579 return false;
580 }
581 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
582 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
583 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
584 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
585
586 if (audio_tracks1.size() != audio_tracks2.size()) {
587 return false;
588 }
589 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
590 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
591 return false;
592 }
593 }
594 if (video_tracks1.size() != video_tracks2.size()) {
595 return false;
596 }
597 for (size_t j = 0; j != video_tracks1.size(); ++j) {
598 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
599 return false;
600 }
601 }
602 }
603 return true;
604}
605
perkjd61bf802016-03-24 03:16:19 -0700606// Helper class to test Observer.
607class MockTrackObserver : public ObserverInterface {
608 public:
609 explicit MockTrackObserver(NotifierInterface* notifier)
610 : notifier_(notifier) {
611 notifier_->RegisterObserver(this);
612 }
613
614 ~MockTrackObserver() { Unregister(); }
615
616 void Unregister() {
617 if (notifier_) {
618 notifier_->UnregisterObserver(this);
619 notifier_ = nullptr;
620 }
621 }
622
623 MOCK_METHOD0(OnChanged, void());
624
625 private:
626 NotifierInterface* notifier_;
627};
628
nisse528b7932017-05-08 03:21:43 -0700629// The PeerConnectionMediaConfig tests below verify that configuration and
630// constraints are propagated into the PeerConnection's MediaConfig. These
631// settings are intended for MediaChannel constructors, but that is not
632// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700633class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
634 public:
zhihuang38ede132017-06-15 12:52:32 -0700635 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
636 CreatePeerConnectionFactoryForTest() {
637 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
638 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
Anders Carlssonb3306882018-05-14 10:11:42 +0200639 auto video_encoder_factory = webrtc::CreateBuiltinVideoEncoderFactory();
640 auto video_decoder_factory = webrtc::CreateBuiltinVideoDecoderFactory();
zhihuang38ede132017-06-15 12:52:32 -0700641
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100642 PeerConnectionFactoryDependencies dependencies;
643 dependencies.worker_thread = rtc::Thread::Current();
644 dependencies.network_thread = rtc::Thread::Current();
645 dependencies.signaling_thread = rtc::Thread::Current();
646
henrika919dc2e2017-10-12 14:24:55 +0200647 // Use fake audio device module since we're only testing the interface
648 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100649 dependencies.media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
zhihuang38ede132017-06-15 12:52:32 -0700650 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200651 FakeAudioCaptureModule::Create(), audio_encoder_factory,
Anders Carlssonb3306882018-05-14 10:11:42 +0200652 audio_decoder_factory, std::move(video_encoder_factory),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000653 std::move(video_decoder_factory), nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +0100654 webrtc::AudioProcessingBuilder().Create()));
zhihuang38ede132017-06-15 12:52:32 -0700655
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100656 dependencies.call_factory = webrtc::CreateCallFactory();
657 dependencies.event_log_factory = webrtc::CreateRtcEventLogFactory();
zhihuang38ede132017-06-15 12:52:32 -0700658
659 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100660 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700661 }
662
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100663 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800664
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100665 private:
deadbeefd7850b22017-08-23 10:59:19 -0700666 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700667};
668
Steve Anton36da6ff2018-02-16 16:04:20 -0800669// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
670class PeerConnectionInterfaceBaseTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800672 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
673 : vss_(new rtc::VirtualSocketServer()),
674 main_(vss_.get()),
675 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800676#ifdef WEBRTC_ANDROID
677 webrtc::InitializeAndroidObjects();
678#endif
679 }
680
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700682 // Use fake audio capture module since we're only testing the interface
683 // level, and using a real one could make tests flaky when run in parallel.
684 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700686 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200687 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
688 fake_audio_capture_module_),
689 webrtc::CreateBuiltinAudioEncoderFactory(),
690 webrtc::CreateBuiltinAudioDecoderFactory(),
691 webrtc::CreateBuiltinVideoEncoderFactory(),
692 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
693 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700694 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700695 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700696 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700697 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 }
699
700 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200701 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703
deadbeef293e9262017-01-11 12:28:30 -0800704 // DTLS does not work in a loopback call, so is disabled for most of the
705 // tests in this file.
706 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200707 RTCConfiguration config;
708 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800709
Niels Möllerf06f9232018-08-07 12:32:18 +0200710 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700713 void CreatePeerConnectionWithIceTransportsType(
714 PeerConnectionInterface::IceTransportsType type) {
715 PeerConnectionInterface::RTCConfiguration config;
716 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200717 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718 }
719
720 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100721 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100726 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 server.password = password;
728 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200729 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
Niels Möllerf06f9232018-08-07 12:32:18 +0200732 void CreatePeerConnection(const RTCConfiguration& config) {
kwibergd1fe2812016-04-27 06:47:29 -0700733 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800734 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
735 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000736
deadbeef1dcb1642017-03-29 21:08:16 -0700737 // Create certificate generator unless DTLS constraint is explicitly set to
738 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200739 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200740
741 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800742 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
743 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000744 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800745 RTCConfiguration modified_config = config;
746 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200747 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200748 modified_config, std::move(port_allocator), std::move(cert_generator),
749 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 ASSERT_TRUE(pc_.get() != NULL);
751 observer_.SetPeerConnectionInterface(pc_.get());
752 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
753 }
754
deadbeef0a6c4ca2015-10-06 11:38:28 -0700755 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700757 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700758 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800760 config.sdp_semantics = sdp_semantics_;
761 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200762 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700764 }
765
Steve Anton038834f2017-07-14 15:59:59 -0700766 void CreatePeerConnectionExpectFail(
767 PeerConnectionInterface::RTCConfiguration config) {
768 PeerConnectionInterface::IceServer server;
769 server.uri = kTurnIceServerUri;
770 server.password = kTurnPassword;
771 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800772 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700773 rtc::scoped_refptr<PeerConnectionInterface> pc =
774 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
775 EXPECT_EQ(nullptr, pc);
776 }
777
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100779 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
781 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
782 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785
deadbeef0a6c4ca2015-10-06 11:38:28 -0700786 CreatePeerConnectionExpectFail(kStunInvalidPort);
787 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
788 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789
Niels Möllerdb4def92019-03-18 16:53:59 +0100790 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
791 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800792 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
793 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800795 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800799 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801
802 void ReleasePeerConnection() {
803 pc_ = NULL;
804 observer_.SetPeerConnectionInterface(NULL);
805 }
806
Steve Anton36da6ff2018-02-16 16:04:20 -0800807 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
808 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100809 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800810 }
811
812 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800813 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800814 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800815 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800816 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
818 observer_.renegotiation_needed_ = false;
819 }
820
Steve Anton36da6ff2018-02-16 16:04:20 -0800821 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700822 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800824 stream->AddTrack(CreateVideoTrack(label + "v0"));
825 ASSERT_TRUE(pc_->AddStream(stream));
826 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
827 observer_.renegotiation_needed_ = false;
828 }
829
830 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
831 const std::string& label) {
832 return pc_factory_->CreateAudioTrack(label, nullptr);
833 }
834
835 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800836 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800837 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800838 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800839 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
840 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
841 observer_.renegotiation_needed_ = false;
842 }
843
844 void AddAudioStream(const std::string& label) {
845 rtc::scoped_refptr<MediaStreamInterface> stream(
846 pc_factory_->CreateLocalMediaStream(label));
847 stream->AddTrack(CreateAudioTrack(label + "a0"));
848 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
850 observer_.renegotiation_needed_ = false;
851 }
852
Seth Hampson845e8782018-03-02 11:34:10 -0800853 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 const std::string& audio_track_label,
855 const std::string& video_track_label) {
856 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700857 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800858 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800859 stream->AddTrack(CreateAudioTrack(audio_track_label));
860 stream->AddTrack(CreateVideoTrack(video_track_label));
861 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
863 observer_.renegotiation_needed_ = false;
864 }
865
Steve Anton36da6ff2018-02-16 16:04:20 -0800866 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
867 cricket::MediaType media_type) {
868 for (auto receiver : pc_->GetReceivers()) {
869 if (receiver->media_type() == media_type) {
870 return receiver;
871 }
872 }
873 return nullptr;
874 }
875
kwibergd1fe2812016-04-27 06:47:29 -0700876 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200877 const RTCOfferAnswerOptions* options,
878 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200879 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
880 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200882 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200884 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700887 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 return observer->result();
889 }
890
kwibergd1fe2812016-04-27 06:47:29 -0700891 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200892 const RTCOfferAnswerOptions* options) {
893 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895
kwibergd1fe2812016-04-27 06:47:29 -0700896 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200897 const RTCOfferAnswerOptions* options) {
898 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900
Steve Antondb45ca82017-09-11 18:27:34 -0700901 bool DoSetSessionDescription(
902 std::unique_ptr<SessionDescriptionInterface> desc,
903 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200904 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
905 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700907 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700909 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
zhihuang29ff8442016-07-27 11:07:25 -0700911 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
912 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
913 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 return observer->result();
915 }
916
Steve Antondb45ca82017-09-11 18:27:34 -0700917 bool DoSetLocalDescription(
918 std::unique_ptr<SessionDescriptionInterface> desc) {
919 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921
Steve Antondb45ca82017-09-11 18:27:34 -0700922 bool DoSetRemoteDescription(
923 std::unique_ptr<SessionDescriptionInterface> desc) {
924 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
927 // Calls PeerConnection::GetStats and check the return value.
928 // It does not verify the values in the StatReports since a RTCP packet might
929 // be required.
930 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000931 rtc::scoped_refptr<MockStatsObserver> observer(
932 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200933 if (!pc_->GetStats(observer, track,
934 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 return false;
936 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
937 return observer->called();
938 }
939
Harald Alvestrand89061872018-01-02 14:08:34 +0100940 // Call the standards-compliant GetStats function.
941 bool DoGetRTCStats() {
942 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
943 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
944 pc_->GetStats(callback);
945 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
946 return callback->called();
947 }
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800950 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800952 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800953 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800954 } else {
955 // Unified Plan does not support AddStream, so just add an audio and video
956 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800957 AddAudioTrack(kAudioTracks[0], {kStreamId1});
958 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800959 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 CreateOfferReceiveAnswer();
961 }
962
963 // Verify that RTP Header extensions has been negotiated for audio and video.
964 void VerifyRemoteRtpHeaderExtensions() {
965 const cricket::MediaContentDescription* desc =
966 cricket::GetFirstAudioContentDescription(
967 pc_->remote_description()->description());
968 ASSERT_TRUE(desc != NULL);
969 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
970
971 desc = cricket::GetFirstVideoContentDescription(
972 pc_->remote_description()->description());
973 ASSERT_TRUE(desc != NULL);
974 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
975 }
976
977 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700978 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700979 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 std::string sdp;
981 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700982 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800983 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700984 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
986 }
987
deadbeefab9b2d12015-10-14 11:33:11 -0700988 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700989 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800990 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700991 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700992 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
993 }
994
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700996 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700997 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998
999 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1000 // audio codec change, even if the parameter has nothing to do with
1001 // receiving. Not all parameters are serialized to SDP.
1002 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1003 // the SessionDescription, it is necessary to do that here to in order to
1004 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1005 // https://code.google.com/p/webrtc/issues/detail?id=1356
1006 std::string sdp;
1007 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001008 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001009 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001010 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1012 }
1013
1014 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001015 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001016 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017
1018 std::string sdp;
1019 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001020 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001021 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001022 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1024 }
1025
1026 void CreateOfferReceiveAnswer() {
1027 CreateOfferAsLocalDescription();
1028 std::string sdp;
1029 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1030 CreateAnswerAsRemoteDescription(sdp);
1031 }
1032
1033 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001034 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001035 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1037 // audio codec change, even if the parameter has nothing to do with
1038 // receiving. Not all parameters are serialized to SDP.
1039 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1040 // the SessionDescription, it is necessary to do that here to in order to
1041 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1042 // https://code.google.com/p/webrtc/issues/detail?id=1356
1043 std::string sdp;
1044 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001045 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001046 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047
Steve Antondb45ca82017-09-11 18:27:34 -07001048 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001050 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001051 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 }
1053
deadbeefab9b2d12015-10-14 11:33:11 -07001054 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001055 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001056 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001057 ASSERT_TRUE(answer);
1058 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1060 }
1061
deadbeefab9b2d12015-10-14 11:33:11 -07001062 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001063 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001064 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001065 ASSERT_TRUE(pr_answer);
1066 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001068 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001069 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001070 ASSERT_TRUE(answer);
1071 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1073 }
1074
Seth Hampson845e8782018-03-02 11:34:10 -08001075 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001076 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001077 // called with the given stream id and expected number of tracks.
1078 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001079 int expected_num_tracks) {
1080 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001081 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001082 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001083 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 }
1085
1086 // Creates an offer and applies it as a local session description.
1087 // Creates an answer with the same SDP an the offer but removes all lines
1088 // that start with a:ssrc"
1089 void CreateOfferReceiveAnswerWithoutSsrc() {
1090 CreateOfferAsLocalDescription();
1091 std::string sdp;
1092 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1093 SetSsrcToZero(&sdp);
1094 CreateAnswerAsRemoteDescription(sdp);
1095 }
1096
deadbeefab9b2d12015-10-14 11:33:11 -07001097 // This function creates a MediaStream with label kStreams[0] and
1098 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1099 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001100 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001101 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001102 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001103 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1104 size_t number_of_video_tracks) {
1105 EXPECT_LE(number_of_audio_tracks, 2u);
1106 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001107
1108 reference_collection_ = StreamCollection::Create();
1109 std::string sdp_ms1 = std::string(kSdpStringInit);
1110
Seth Hampson845e8782018-03-02 11:34:10 -08001111 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001112
1113 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001114 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001115 reference_collection_->AddStream(stream);
1116
1117 if (number_of_audio_tracks > 0) {
1118 sdp_ms1 += std::string(kSdpStringAudio);
1119 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1120 AddAudioTrack(kAudioTracks[0], stream);
1121 }
1122 if (number_of_audio_tracks > 1) {
1123 sdp_ms1 += kSdpStringMs1Audio1;
1124 AddAudioTrack(kAudioTracks[1], stream);
1125 }
1126
1127 if (number_of_video_tracks > 0) {
1128 sdp_ms1 += std::string(kSdpStringVideo);
1129 sdp_ms1 += std::string(kSdpStringMs1Video0);
1130 AddVideoTrack(kVideoTracks[0], stream);
1131 }
1132 if (number_of_video_tracks > 1) {
1133 sdp_ms1 += kSdpStringMs1Video1;
1134 AddVideoTrack(kVideoTracks[1], stream);
1135 }
1136
kwibergd1fe2812016-04-27 06:47:29 -07001137 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001138 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001139 }
1140
1141 void AddAudioTrack(const std::string& track_id,
1142 MediaStreamInterface* stream) {
1143 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1144 webrtc::AudioTrack::Create(track_id, nullptr));
1145 ASSERT_TRUE(stream->AddTrack(audio_track));
1146 }
1147
1148 void AddVideoTrack(const std::string& track_id,
1149 MediaStreamInterface* stream) {
1150 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001151 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001152 webrtc::FakeVideoTrackSource::Create(),
1153 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001154 ASSERT_TRUE(stream->AddTrack(video_track));
1155 }
1156
Steve Anton36da6ff2018-02-16 16:04:20 -08001157 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001158 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001159 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001160 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001161 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1162 return offer;
1163 }
1164
Steve Anton36da6ff2018-02-16 16:04:20 -08001165 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1166 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001167 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001168 std::unique_ptr<SessionDescriptionInterface> offer;
1169 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1170 return offer;
1171 }
1172
1173 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1174 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1175 std::unique_ptr<SessionDescriptionInterface> answer;
1176 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1177 return answer;
1178 }
1179
kwibergfd8be342016-05-14 19:44:11 -07001180 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001181 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001182 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1185 return answer;
1186 }
1187
1188 const std::string& GetFirstAudioStreamCname(
1189 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001190 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001191 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001192 return audio_desc->streams()[0].cname;
1193 }
1194
zhihuang1c378ed2017-08-17 14:10:50 -07001195 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1196 const RTCOfferAnswerOptions& offer_answer_options) {
1197 RTC_DCHECK(pc_);
1198 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1199 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1200 pc_->CreateOffer(observer, offer_answer_options);
1201 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1202 return observer->MoveDescription();
1203 }
1204
1205 void CreateOfferWithOptionsAsRemoteDescription(
1206 std::unique_ptr<SessionDescriptionInterface>* desc,
1207 const RTCOfferAnswerOptions& offer_answer_options) {
1208 *desc = CreateOfferWithOptions(offer_answer_options);
1209 ASSERT_TRUE(desc != nullptr);
1210 std::string sdp;
1211 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001212 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001213 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001214 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001215 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1216 }
1217
1218 void CreateOfferWithOptionsAsLocalDescription(
1219 std::unique_ptr<SessionDescriptionInterface>* desc,
1220 const RTCOfferAnswerOptions& offer_answer_options) {
1221 *desc = CreateOfferWithOptions(offer_answer_options);
1222 ASSERT_TRUE(desc != nullptr);
1223 std::string sdp;
1224 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001225 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001226 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001227
Steve Antondb45ca82017-09-11 18:27:34 -07001228 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001229 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1230 }
1231
1232 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001233 RTC_DCHECK(content);
1234 RTC_DCHECK(content->media_description());
1235 for (const cricket::AudioCodec& codec :
1236 content->media_description()->as_audio()->codecs()) {
1237 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001238 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001239 }
zhihuang1c378ed2017-08-17 14:10:50 -07001240 }
1241 return false;
1242 }
1243
Steve Anton36da6ff2018-02-16 16:04:20 -08001244 const char* GetSdpStringWithStream1() const {
1245 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1246 return kSdpStringWithStream1PlanB;
1247 } else {
1248 return kSdpStringWithStream1UnifiedPlan;
1249 }
1250 }
1251
1252 const char* GetSdpStringWithStream1And2() const {
1253 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1254 return kSdpStringWithStream1And2PlanB;
1255 } else {
1256 return kSdpStringWithStream1And2UnifiedPlan;
1257 }
1258 }
1259
deadbeef9a6f4d42017-05-15 19:43:33 -07001260 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1261 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001262 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001263 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001264 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001265 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1266 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1267 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001269 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001270 const SdpSemantics sdp_semantics_;
1271};
1272
1273class PeerConnectionInterfaceTest
1274 : public PeerConnectionInterfaceBaseTest,
1275 public ::testing::WithParamInterface<SdpSemantics> {
1276 protected:
1277 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1278};
1279
1280class PeerConnectionInterfaceTestPlanB
1281 : public PeerConnectionInterfaceBaseTest {
1282 protected:
1283 PeerConnectionInterfaceTestPlanB()
1284 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285};
1286
zhihuang8f65cdf2016-05-06 18:40:30 -07001287// Generate different CNAMEs when PeerConnections are created.
1288// The CNAMEs are expected to be generated randomly. It is possible
1289// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001290TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001291 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001292 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001293 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001294 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001295 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1296 GetFirstAudioStreamCname(offer2.get()));
1297}
1298
Steve Anton36da6ff2018-02-16 16:04:20 -08001299TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001300 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001301 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001302 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001303 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001304 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1305 GetFirstAudioStreamCname(answer2.get()));
1306}
1307
Steve Anton36da6ff2018-02-16 16:04:20 -08001308TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 CreatePeerConnectionWithDifferentConfigurations) {
1310 CreatePeerConnectionWithDifferentConfigurations();
1311}
1312
Steve Anton36da6ff2018-02-16 16:04:20 -08001313TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001314 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1315 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1316 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1317 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1318 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1319 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1320 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1321 port_allocator_->candidate_filter());
1322 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1323 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1324}
1325
1326// Test that when a PeerConnection is created with a nonzero candidate pool
1327// size, the pooled PortAllocatorSession is created with all the attributes
1328// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001329TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001330 PeerConnectionInterface::RTCConfiguration config;
1331 PeerConnectionInterface::IceServer server;
1332 server.uri = kStunAddressOnly;
1333 config.servers.push_back(server);
1334 config.type = PeerConnectionInterface::kRelay;
1335 config.disable_ipv6 = true;
1336 config.tcp_candidate_policy =
1337 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001338 config.candidate_network_policy =
1339 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001340 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001341 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001342
1343 const cricket::FakePortAllocatorSession* session =
1344 static_cast<const cricket::FakePortAllocatorSession*>(
1345 port_allocator_->GetPooledSession());
1346 ASSERT_NE(nullptr, session);
1347 EXPECT_EQ(1UL, session->stun_servers().size());
1348 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1349 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001350 EXPECT_LT(0U,
1351 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001352}
1353
deadbeefd21eab32017-07-26 16:50:11 -07001354// Test that network-related RTCConfiguration members are applied to the
1355// PortAllocator when CreatePeerConnection is called. Specifically:
1356// - disable_ipv6_on_wifi
1357// - max_ipv6_networks
1358// - tcp_candidate_policy
1359// - candidate_network_policy
1360// - prune_turn_ports
1361//
1362// Note that the candidate filter (RTCConfiguration::type) is already tested
1363// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001364TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001365 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1366 // Create fake port allocator.
1367 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1368 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1369 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1370
1371 // Create RTCConfiguration with some network-related fields relevant to
1372 // PortAllocator populated.
1373 PeerConnectionInterface::RTCConfiguration config;
1374 config.disable_ipv6_on_wifi = true;
1375 config.max_ipv6_networks = 10;
1376 config.tcp_candidate_policy =
1377 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1378 config.candidate_network_policy =
1379 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1380 config.prune_turn_ports = true;
1381
1382 // Create the PC factory and PC with the above config.
1383 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1384 webrtc::CreatePeerConnectionFactory(
1385 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001386 rtc::Thread::Current(), fake_audio_capture_module_,
1387 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001388 webrtc::CreateBuiltinAudioDecoderFactory(),
1389 webrtc::CreateBuiltinVideoEncoderFactory(),
1390 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1391 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001392 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001393 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1394 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001395 EXPECT_TRUE(pc.get());
1396 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001397
1398 // Now validate that the config fields set above were applied to the
1399 // PortAllocator, as flags or otherwise.
1400 EXPECT_FALSE(raw_port_allocator->flags() &
1401 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1402 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1403 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1404 EXPECT_TRUE(raw_port_allocator->flags() &
1405 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1406 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1407}
1408
deadbeef46c73892016-11-16 19:42:04 -08001409// Check that GetConfiguration returns the configuration the PeerConnection was
1410// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001411TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001412 PeerConnectionInterface::RTCConfiguration config;
1413 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001414 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001415
1416 PeerConnectionInterface::RTCConfiguration returned_config =
1417 pc_->GetConfiguration();
1418 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1419}
1420
1421// Check that GetConfiguration returns the last configuration passed into
1422// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001423TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001424 PeerConnectionInterface::RTCConfiguration starting_config;
1425 starting_config.bundle_policy =
1426 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1427 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001428
Steve Anton36da6ff2018-02-16 16:04:20 -08001429 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001430 config.type = PeerConnectionInterface::kRelay;
Piotr (Peter) Slatalaaa1e7c22018-10-16 10:04:45 -07001431 config.use_media_transport = true;
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001432 config.use_media_transport_for_data_channels = true;
deadbeef46c73892016-11-16 19:42:04 -08001433 EXPECT_TRUE(pc_->SetConfiguration(config));
1434
1435 PeerConnectionInterface::RTCConfiguration returned_config =
1436 pc_->GetConfiguration();
1437 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
Piotr (Peter) Slatalaaa1e7c22018-10-16 10:04:45 -07001438 EXPECT_TRUE(returned_config.use_media_transport);
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001439 EXPECT_TRUE(returned_config.use_media_transport_for_data_channels);
deadbeef46c73892016-11-16 19:42:04 -08001440}
1441
Steve Antonc79268f2018-04-24 09:54:10 -07001442TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1443 CreatePeerConnection();
1444
1445 pc_->Close();
1446
1447 EXPECT_FALSE(
1448 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()));
1449}
1450
Steve Anton36da6ff2018-02-16 16:04:20 -08001451TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001452 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001453 AddVideoStream(kStreamId1);
1454 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 ASSERT_EQ(2u, pc_->local_streams()->count());
1456
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001457 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001458 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001459 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001460 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001461 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001462 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001464 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001465 EXPECT_EQ(3u, pc_->local_streams()->count());
1466
1467 // Remove the third stream.
1468 pc_->RemoveStream(pc_->local_streams()->at(2));
1469 EXPECT_EQ(2u, pc_->local_streams()->count());
1470
1471 // Remove the second stream.
1472 pc_->RemoveStream(pc_->local_streams()->at(1));
1473 EXPECT_EQ(1u, pc_->local_streams()->count());
1474
1475 // Remove the first stream.
1476 pc_->RemoveStream(pc_->local_streams()->at(0));
1477 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
deadbeefab9b2d12015-10-14 11:33:11 -07001480// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001481// Don't run under Unified Plan since the stream API is not available.
1482TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001483 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001484 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001485 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001486 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001487
deadbeefab9b2d12015-10-14 11:33:11 -07001488 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001489 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001490 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001491
deadbeefab9b2d12015-10-14 11:33:11 -07001492 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001493 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001494 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001495
1496 // Add another stream and ensure the offer includes both the old and new
1497 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001498 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001499 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001500
Steve Antonb1c1de12017-12-21 15:14:30 -08001501 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001502 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1503 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001504
Steve Antonb1c1de12017-12-21 15:14:30 -08001505 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001506 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1507 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001508}
1509
Steve Anton36da6ff2018-02-16 16:04:20 -08001510// Don't run under Unified Plan since the stream API is not available.
1511TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001513 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 ASSERT_EQ(1u, pc_->local_streams()->count());
1515 pc_->RemoveStream(pc_->local_streams()->at(0));
1516 EXPECT_EQ(0u, pc_->local_streams()->count());
1517}
1518
deadbeefe1f9d832016-01-14 15:35:42 -08001519// Test for AddTrack and RemoveTrack methods.
1520// Tests that the created offer includes tracks we added,
1521// and that the RtpSenders are created correctly.
1522// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001523// Only tested with Plan B since Unified Plan is covered in more detail by tests
1524// in peerconnection_jsep_unittests.cc
1525TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001526 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001528 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001529 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001530 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001531 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1532 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001533 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001534 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001535 EXPECT_EQ("audio_track", audio_sender->id());
1536 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001537 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001538 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001539 EXPECT_EQ("video_track", video_sender->id());
1540 EXPECT_EQ(video_track, video_sender->track());
1541
1542 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001543 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001544 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001545
1546 const cricket::ContentInfo* audio_content =
1547 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001548 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001549 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 const cricket::ContentInfo* video_content =
1552 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001553 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001554 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
Steve Antondb45ca82017-09-11 18:27:34 -07001556 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001557
1558 // Now try removing the tracks.
1559 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1560 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1561
1562 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001563 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001564
1565 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001566 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001567 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001570 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001571 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001572
Steve Antondb45ca82017-09-11 18:27:34 -07001573 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001574
1575 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1576 // should return false.
1577 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1578 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1579}
1580
1581// Test creating senders without a stream specified,
1582// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001583TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001584 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001586 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001587 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001588 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001589 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001590 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001591 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001592 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001593 EXPECT_EQ("audio_track", audio_sender->id());
1594 EXPECT_EQ(audio_track, audio_sender->track());
1595 EXPECT_EQ("video_track", video_sender->id());
1596 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001597 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1598 // If the ID is truly a random GUID, it should be infinitely unlikely they
1599 // will be the same.
1600 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1601 } else {
1602 // We allows creating tracks without stream ids under Unified Plan
1603 // semantics.
1604 EXPECT_EQ(0u, video_sender->stream_ids().size());
1605 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1606 }
deadbeefe1f9d832016-01-14 15:35:42 -08001607}
1608
Harald Alvestrand89061872018-01-02 14:08:34 +01001609// Test that we can call GetStats() after AddTrack but before connecting
1610// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001611TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001612 CreatePeerConnectionWithoutDtls();
1613 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001614 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001615 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001616 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001617 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1618 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001619 EXPECT_TRUE(DoGetStats(nullptr));
1620}
1621
Steve Anton36da6ff2018-02-16 16:04:20 -08001622TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001623 CreatePeerConnectionWithoutDtls();
1624 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001625 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001626 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001627 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001628 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001629 ASSERT_TRUE(audio_sender.ok());
1630 auto* audio_sender_proxy =
1631 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1632 audio_sender.value().get());
1633 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1634
Harald Alvestrandc72af932018-01-11 17:18:19 +01001635 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001636 ASSERT_TRUE(video_sender.ok());
1637 auto* video_sender_proxy =
1638 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1639 video_sender.value().get());
1640 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001641}
1642
Steve Anton36da6ff2018-02-16 16:04:20 -08001643// Don't run under Unified Plan since the stream API is not available.
1644TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001645 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001646 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001647 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001648 ASSERT_EQ(1u, senders.size());
1649 auto* sender_proxy =
1650 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1651 senders[0].get());
1652 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001653}
1654
Steve Anton36da6ff2018-02-16 16:04:20 -08001655TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001657 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 VerifyRemoteRtpHeaderExtensions();
1659}
1660
Steve Anton36da6ff2018-02-16 16:04:20 -08001661TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001663 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 CreateOfferAsLocalDescription();
1665 std::string offer;
1666 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1667 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001668 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
Steve Anton36da6ff2018-02-16 16:04:20 -08001671TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001672 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001673 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674
1675 CreateOfferAsRemoteDescription();
1676 CreateAnswerAsLocalDescription();
1677
Seth Hampson845e8782018-03-02 11:34:10 -08001678 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
Steve Anton36da6ff2018-02-16 16:04:20 -08001681TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001682 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001683 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684
1685 CreateOfferAsRemoteDescription();
1686 CreatePrAnswerAsLocalDescription();
1687 CreateAnswerAsLocalDescription();
1688
Seth Hampson845e8782018-03-02 11:34:10 -08001689 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
Steve Anton36da6ff2018-02-16 16:04:20 -08001692// Don't run under Unified Plan since the stream API is not available.
1693TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 InitiateCall();
1695 ASSERT_EQ(1u, pc_->remote_streams()->count());
1696 pc_->RemoveStream(pc_->local_streams()->at(0));
1697 CreateOfferReceiveAnswer();
1698 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001699 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 CreateOfferReceiveAnswer();
1701}
1702
1703// Tests that after negotiating an audio only call, the respondent can perform a
1704// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001705TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001706 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001707 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 CreateOfferAsRemoteDescription();
1709 CreateAnswerAsLocalDescription();
1710
1711 ASSERT_EQ(1u, pc_->remote_streams()->count());
1712 pc_->RemoveStream(pc_->local_streams()->at(0));
1713 CreateOfferReceiveAnswer();
1714 EXPECT_EQ(0u, pc_->remote_streams()->count());
1715}
1716
1717// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001718TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720
Steve Antonf1c6db12017-10-13 11:13:35 -07001721 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001724 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001725 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001726 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
1728 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001729 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001730 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001731 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
Steve Antonf1c6db12017-10-13 11:13:35 -07001733 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001734 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735
Steve Antonf1c6db12017-10-13 11:13:35 -07001736 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
deadbeefab9b2d12015-10-14 11:33:11 -07001739// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001741TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001742 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001744 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001745 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001746 EXPECT_TRUE(offer);
1747 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748
1749 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001750 AddAudioTrack("track_label", {kStreamId1});
1751 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752
1753 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001754 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755
1756 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001757 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001758 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759}
1760
1761// Test that we will get different SSRCs for each tracks in the offer and answer
1762// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001763TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001764 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001766 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1767 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768
1769 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001770 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001771 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 int audio_ssrc = 0;
1773 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001774 EXPECT_TRUE(
1775 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1776 EXPECT_TRUE(
1777 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 EXPECT_NE(audio_ssrc, video_ssrc);
1779
1780 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001781 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001783 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 audio_ssrc = 0;
1785 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001786 EXPECT_TRUE(
1787 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1788 EXPECT_TRUE(
1789 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 EXPECT_NE(audio_ssrc, video_ssrc);
1791}
1792
deadbeefeb459812015-12-15 19:24:43 -08001793// Test that it's possible to call AddTrack on a MediaStream after adding
1794// the stream to a PeerConnection.
1795// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001796// Don't run under Unified Plan since the stream API is not available.
1797TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001798 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001799 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001800 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001801 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1802
1803 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001804 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001805 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001806 stream->AddTrack(video_track.get());
1807
kwibergd1fe2812016-04-27 06:47:29 -07001808 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001809 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001810
1811 const cricket::MediaContentDescription* video_desc =
1812 cricket::GetFirstVideoContentDescription(offer->description());
1813 EXPECT_TRUE(video_desc != nullptr);
1814}
1815
1816// Test that it's possible to call RemoveTrack on a MediaStream after adding
1817// the stream to a PeerConnection.
1818// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001819// Don't run under Unified Plan since the stream API is not available.
1820TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001821 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001822 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001823 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001824 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1825
1826 // Remove the video track.
1827 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1828
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001830 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001831
1832 const cricket::MediaContentDescription* video_desc =
1833 cricket::GetFirstVideoContentDescription(offer->description());
1834 EXPECT_TRUE(video_desc == nullptr);
1835}
1836
deadbeefbd7d8f72015-12-18 16:58:44 -08001837// Test creating a sender with a stream ID, and ensure the ID is populated
1838// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001839// Don't run under Unified Plan since the stream API is not available.
1840TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001841 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001842 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001843
kwibergd1fe2812016-04-27 06:47:29 -07001844 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001845 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001846
1847 const cricket::MediaContentDescription* video_desc =
1848 cricket::GetFirstVideoContentDescription(offer->description());
1849 ASSERT_TRUE(video_desc != nullptr);
1850 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001851 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001855TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001857 ASSERT_LT(0u, pc_->GetSenders().size());
1858 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001859 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001860 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 EXPECT_TRUE(DoGetStats(remote_audio));
1862
1863 // Remove the stream. Since we are sending to our selves the local
1864 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001865 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 // Do a re-negotiation.
1867 CreateOfferReceiveAnswer();
1868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 // Test that we still can get statistics for the old track. Even if it is not
1870 // sent any longer.
1871 EXPECT_TRUE(DoGetStats(remote_audio));
1872}
1873
1874// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001875TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001877 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1878 ASSERT_TRUE(video_receiver);
1879 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880}
1881
1882// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001883TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001885 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 pc_factory_->CreateAudioTrack("unknown track", NULL));
1887 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1888}
1889
Steve Anton36da6ff2018-02-16 16:04:20 -08001890TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001891 CreatePeerConnectionWithoutDtls();
1892 EXPECT_TRUE(DoGetRTCStats());
1893 // Clearing stats cache is needed now, but should be temporary.
1894 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1895 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001896 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1897 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001898 EXPECT_TRUE(DoGetRTCStats());
1899 pc_->ClearStatsCache();
1900 CreateOfferReceiveAnswer();
1901 EXPECT_TRUE(DoGetRTCStats());
1902}
1903
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001905TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001906 RTCConfiguration config;
1907 config.enable_rtp_data_channel = true;
1908 config.enable_dtls_srtp = false;
1909 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001910 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001912 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 pc_->CreateDataChannel("test2", NULL);
1914 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001915 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001917 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 new MockDataChannelObserver(data2));
1919
1920 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1921 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1922 std::string data_to_send1 = "testing testing";
1923 std::string data_to_send2 = "testing something else";
1924 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1925
1926 CreateOfferReceiveAnswer();
1927 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1928 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1929
1930 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1931 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1932 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1933 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1934
1935 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1936 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1937
1938 data1->Close();
1939 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1940 CreateOfferReceiveAnswer();
1941 EXPECT_FALSE(observer1->IsOpen());
1942 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1943 EXPECT_TRUE(observer2->IsOpen());
1944
1945 data_to_send2 = "testing something else again";
1946 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1947
1948 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1949}
1950
1951// This test verifies that sendnig binary data over RTP data channels should
1952// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001953TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001954 RTCConfiguration config;
1955 config.enable_rtp_data_channel = true;
1956 config.enable_dtls_srtp = false;
1957 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001958 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001960 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 pc_->CreateDataChannel("test2", NULL);
1962 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001963 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001965 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 new MockDataChannelObserver(data2));
1967
1968 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1969 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1970
1971 CreateOfferReceiveAnswer();
1972 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1973 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1974
1975 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1976 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1977
jbaucheec21bd2016-03-20 06:15:43 -07001978 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1980}
1981
1982// This test setup a RTP data channels in loop back and test that a channel is
1983// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001984TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001985 RTCConfiguration config;
1986 config.enable_rtp_data_channel = true;
1987 config.enable_dtls_srtp = false;
1988 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001989 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001991 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 new MockDataChannelObserver(data1));
1993
1994 CreateOfferReceiveAnswerWithoutSsrc();
1995
1996 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1997
1998 data1->Close();
1999 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2000 CreateOfferReceiveAnswerWithoutSsrc();
2001 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2002 EXPECT_FALSE(observer1->IsOpen());
2003}
2004
2005// This test that if a data channel is added in an answer a receive only channel
2006// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08002007TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002008 RTCConfiguration config;
2009 config.enable_rtp_data_channel = true;
2010 config.enable_dtls_srtp = false;
2011
2012 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013
2014 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002015 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 pc_->CreateDataChannel(offer_label, NULL);
2017
2018 CreateOfferAsLocalDescription();
2019
2020 // Replace the data channel label in the offer and apply it as an answer.
2021 std::string receive_label = "answer_channel";
2022 std::string sdp;
2023 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002024 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 CreateAnswerAsRemoteDescription(sdp);
2026
2027 // Verify that a new incoming data channel has been created and that
2028 // it is open but can't we written to.
2029 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2030 DataChannelInterface* received_channel = observer_.last_datachannel_;
2031 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2032 EXPECT_EQ(receive_label, received_channel->label());
2033 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2034
2035 // Verify that the channel we initially offered has been rejected.
2036 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2037
2038 // Do another offer / answer exchange and verify that the data channel is
2039 // opened.
2040 CreateOfferReceiveAnswer();
2041 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2042 kTimeout);
2043}
2044
2045// This test that no data channel is returned if a reliable channel is
2046// requested.
2047// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002048TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002049 RTCConfiguration rtc_config;
2050 rtc_config.enable_rtp_data_channel = true;
2051 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052
2053 std::string label = "test";
2054 webrtc::DataChannelInit config;
2055 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002056 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 pc_->CreateDataChannel(label, &config);
2058 EXPECT_TRUE(channel == NULL);
2059}
2060
deadbeefab9b2d12015-10-14 11:33:11 -07002061// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002062TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002063 RTCConfiguration config;
2064 config.enable_rtp_data_channel = true;
2065 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002066
2067 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002068 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002069 pc_->CreateDataChannel(label, nullptr);
2070 EXPECT_NE(channel, nullptr);
2071
zhihuang9763d562016-08-05 11:14:50 -07002072 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002073 pc_->CreateDataChannel(label, nullptr);
2074 EXPECT_EQ(dup_channel, nullptr);
2075}
2076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077// This tests that a SCTP data channel is returned using different
2078// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002079TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002080 RTCConfiguration rtc_config;
2081 rtc_config.enable_dtls_srtp = true;
2082 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083
2084 webrtc::DataChannelInit config;
2085
zhihuang9763d562016-08-05 11:14:50 -07002086 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 pc_->CreateDataChannel("1", &config);
2088 EXPECT_TRUE(channel != NULL);
2089 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002090 EXPECT_TRUE(observer_.renegotiation_needed_);
2091 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092
2093 config.ordered = false;
2094 channel = pc_->CreateDataChannel("2", &config);
2095 EXPECT_TRUE(channel != NULL);
2096 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002097 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098
2099 config.ordered = true;
2100 config.maxRetransmits = 0;
2101 channel = pc_->CreateDataChannel("3", &config);
2102 EXPECT_TRUE(channel != NULL);
2103 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002104 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105
2106 config.maxRetransmits = -1;
2107 config.maxRetransmitTime = 0;
2108 channel = pc_->CreateDataChannel("4", &config);
2109 EXPECT_TRUE(channel != NULL);
2110 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002111 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112}
2113
2114// This tests that no data channel is returned if both maxRetransmits and
2115// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002116TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002118 RTCConfiguration rtc_config;
2119 rtc_config.enable_dtls_srtp = true;
2120 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121
2122 std::string label = "test";
2123 webrtc::DataChannelInit config;
2124 config.maxRetransmits = 0;
2125 config.maxRetransmitTime = 0;
2126
zhihuang9763d562016-08-05 11:14:50 -07002127 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 pc_->CreateDataChannel(label, &config);
2129 EXPECT_TRUE(channel == NULL);
2130}
2131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132// The test verifies that creating a SCTP data channel with an id already in use
2133// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002134TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002136 RTCConfiguration rtc_config;
2137 rtc_config.enable_dtls_srtp = true;
2138 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
2140 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002141 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002143 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002144 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002145 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 EXPECT_TRUE(channel != NULL);
2147 EXPECT_EQ(1, channel->id());
2148
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 channel = pc_->CreateDataChannel("x", &config);
2150 EXPECT_TRUE(channel == NULL);
2151
2152 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002153 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 channel = pc_->CreateDataChannel("max", &config);
2155 EXPECT_TRUE(channel != NULL);
2156 EXPECT_EQ(config.id, channel->id());
2157
2158 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002159 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 channel = pc_->CreateDataChannel("x", &config);
2161 EXPECT_TRUE(channel == NULL);
2162}
2163
deadbeefab9b2d12015-10-14 11:33:11 -07002164// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002165TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002166 RTCConfiguration rtc_config;
2167 rtc_config.enable_dtls_srtp = true;
2168 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002169
2170 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002171 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002172 pc_->CreateDataChannel(label, nullptr);
2173 EXPECT_NE(channel, nullptr);
2174
zhihuang9763d562016-08-05 11:14:50 -07002175 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002176 pc_->CreateDataChannel(label, nullptr);
2177 EXPECT_NE(dup_channel, nullptr);
2178}
2179
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002180// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2181// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002182TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002183 RTCConfiguration rtc_config;
2184 rtc_config.enable_rtp_data_channel = true;
2185 rtc_config.enable_dtls_srtp = false;
2186 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002187
zhihuang9763d562016-08-05 11:14:50 -07002188 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002189 pc_->CreateDataChannel("test1", NULL);
2190 EXPECT_TRUE(observer_.renegotiation_needed_);
2191 observer_.renegotiation_needed_ = false;
2192
zhihuang9763d562016-08-05 11:14:50 -07002193 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002194 pc_->CreateDataChannel("test2", NULL);
2195 EXPECT_TRUE(observer_.renegotiation_needed_);
2196}
2197
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002199TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002200 RTCConfiguration rtc_config;
2201 rtc_config.enable_rtp_data_channel = true;
2202 rtc_config.enable_dtls_srtp = false;
2203 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204
zhihuang9763d562016-08-05 11:14:50 -07002205 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002207 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 pc_->CreateDataChannel("test2", NULL);
2209 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002210 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002212 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 new MockDataChannelObserver(data2));
2214
2215 CreateOfferReceiveAnswer();
2216 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2217 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2218
2219 ReleasePeerConnection();
2220 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2221 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2222}
2223
Zhi Huang644fde42018-04-02 19:16:26 -07002224// This tests that RTP data channels can be rejected in an answer.
2225TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002226 RTCConfiguration rtc_config;
2227 rtc_config.enable_rtp_data_channel = true;
2228 rtc_config.enable_dtls_srtp = false;
2229 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230
zhihuang9763d562016-08-05 11:14:50 -07002231 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 pc_->CreateDataChannel("offer_channel", NULL));
2233
2234 CreateOfferAsLocalDescription();
2235
2236 // Create an answer where the m-line for data channels are rejected.
2237 std::string sdp;
2238 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002239 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002240 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002241 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002243 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 data_info->rejected = true;
2245
Steve Antondb45ca82017-09-11 18:27:34 -07002246 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2248}
2249
Zhi Huang644fde42018-04-02 19:16:26 -07002250#ifdef HAVE_SCTP
2251// This tests that SCTP data channels can be rejected in an answer.
2252TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2253#else
2254TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2255#endif
2256{
Niels Möllerf06f9232018-08-07 12:32:18 +02002257 RTCConfiguration rtc_config;
2258 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002259
2260 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2261 pc_->CreateDataChannel("offer_channel", NULL));
2262
2263 CreateOfferAsLocalDescription();
2264
2265 // Create an answer where the m-line for data channels are rejected.
2266 std::string sdp;
2267 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2268 std::unique_ptr<SessionDescriptionInterface> answer(
2269 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2270 ASSERT_TRUE(answer);
2271 cricket::ContentInfo* data_info =
2272 cricket::GetFirstDataContent(answer->description());
2273 data_info->rejected = true;
2274
2275 DoSetRemoteDescription(std::move(answer));
2276 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2277}
2278
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279// Test that we can create a session description from an SDP string from
2280// FireFox, use it as a remote session description, generate an answer and use
2281// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002282TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002283 RTCConfiguration rtc_config;
2284 rtc_config.enable_dtls_srtp = true;
2285 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002286 AddAudioTrack("audio_label");
2287 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002288 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002289 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002290 webrtc::kFireFoxSdpOffer, nullptr));
2291 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 CreateAnswerAsLocalDescription();
2293 ASSERT_TRUE(pc_->local_description() != NULL);
2294 ASSERT_TRUE(pc_->remote_description() != NULL);
2295
2296 const cricket::ContentInfo* content =
2297 cricket::GetFirstAudioContent(pc_->local_description()->description());
2298 ASSERT_TRUE(content != NULL);
2299 EXPECT_FALSE(content->rejected);
2300
2301 content =
2302 cricket::GetFirstVideoContent(pc_->local_description()->description());
2303 ASSERT_TRUE(content != NULL);
2304 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002305#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 content =
2307 cricket::GetFirstDataContent(pc_->local_description()->description());
2308 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002309 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002310#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311}
2312
zhihuangb19012e2017-09-19 13:47:59 -07002313// Test that fallback from DTLS to SDES is not supported.
2314// The fallback was previously supported but was removed to simplify the code
2315// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002316TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002317 RTCConfiguration rtc_config;
2318 rtc_config.enable_dtls_srtp = true;
2319 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002320 // Wait for fake certificate to be generated. Previously, this is what caused
2321 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002322 AddAudioTrack("audio_label");
2323 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002324 ASSERT_NE(nullptr, fake_certificate_generator_);
2325 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2326 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002327 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002328 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2329 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002330 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002331}
2332
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333// Test that we can create an audio only offer and receive an answer with a
2334// limited set of audio codecs and receive an updated offer with more audio
2335// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002336TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002337 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002338 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 CreateOfferAsLocalDescription();
2340
Steve Anton36da6ff2018-02-16 16:04:20 -08002341 const char* answer_sdp =
2342 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2343 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002344 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002345 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002346 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347
Steve Anton36da6ff2018-02-16 16:04:20 -08002348 const char* reoffer_sdp =
2349 (sdp_semantics_ == SdpSemantics::kPlanB
2350 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2351 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002352 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002353 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002354 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 CreateAnswerAsLocalDescription();
2356}
2357
deadbeefc80741f2015-10-22 13:14:45 -07002358// Test that if we're receiving (but not sending) a track, subsequent offers
2359// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002360TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002361 RTCConfiguration rtc_config;
2362 rtc_config.enable_dtls_srtp = true;
2363 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002364 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002365 CreateAnswerAsLocalDescription();
2366
2367 // At this point we should be receiving stream 1, but not sending anything.
2368 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002369 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002370 DoCreateOffer(&offer, nullptr);
2371
2372 const cricket::ContentInfo* video_content =
2373 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002374 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2375 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002376
2377 const cricket::ContentInfo* audio_content =
2378 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002379 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2380 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002381}
2382
2383// Test that if we're receiving (but not sending) a track, and the
2384// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2385// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002386TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002387 RTCConfiguration rtc_config;
2388 rtc_config.enable_dtls_srtp = true;
2389 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002390 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002391 CreateAnswerAsLocalDescription();
2392
2393 // At this point we should be receiving stream 1, but not sending anything.
2394 // A new offer would be recvonly, but we'll set the "no receive" constraints
2395 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002396 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002397 RTCOfferAnswerOptions options;
2398 options.offer_to_receive_audio = 0;
2399 options.offer_to_receive_video = 0;
2400 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002401
2402 const cricket::ContentInfo* video_content =
2403 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002404 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2405 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002406
2407 const cricket::ContentInfo* audio_content =
2408 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002409 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2410 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002411}
2412
deadbeef653b8e02015-11-11 12:55:10 -08002413// Test that we can use SetConfiguration to change the ICE servers of the
2414// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002415TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002416 CreatePeerConnection();
2417
Steve Anton36da6ff2018-02-16 16:04:20 -08002418 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002419 PeerConnectionInterface::IceServer server;
2420 server.uri = "stun:test_hostname";
2421 config.servers.push_back(server);
2422 EXPECT_TRUE(pc_->SetConfiguration(config));
2423
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002424 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2425 EXPECT_EQ("test_hostname",
2426 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002427}
2428
Steve Anton36da6ff2018-02-16 16:04:20 -08002429TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002430 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002431 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002432 config.type = PeerConnectionInterface::kRelay;
2433 EXPECT_TRUE(pc_->SetConfiguration(config));
2434 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2435}
2436
Steve Anton36da6ff2018-02-16 16:04:20 -08002437TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002438 PeerConnectionInterface::RTCConfiguration config;
2439 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002440 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002441 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002442 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2443
2444 config.prune_turn_ports = true;
2445 EXPECT_TRUE(pc_->SetConfiguration(config));
2446 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2447}
2448
skvladd1f5fda2017-02-03 16:54:05 -08002449// Test that the ice check interval can be changed. This does not verify that
2450// the setting makes it all the way to P2PTransportChannel, as that would
2451// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002452TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002453 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002454 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002455 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002456 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002457 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002458 EXPECT_TRUE(pc_->SetConfiguration(config));
2459 PeerConnectionInterface::RTCConfiguration new_config =
2460 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002461 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002462}
2463
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002464// Test that when SetConfiguration changes both the pool size and other
2465// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002466TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002467 SetConfigurationCreatesPooledSessionCorrectly) {
2468 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002469 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002470 config.ice_candidate_pool_size = 1;
2471 PeerConnectionInterface::IceServer server;
2472 server.uri = kStunAddressOnly;
2473 config.servers.push_back(server);
2474 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002475 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002476
2477 const cricket::FakePortAllocatorSession* session =
2478 static_cast<const cricket::FakePortAllocatorSession*>(
2479 port_allocator_->GetPooledSession());
2480 ASSERT_NE(nullptr, session);
2481 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002482}
2483
deadbeef293e9262017-01-11 12:28:30 -08002484// Test that after SetLocalDescription, changing the pool size is not allowed,
2485// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002486TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002487 CantChangePoolSizeAfterSetLocalDescription) {
2488 CreatePeerConnection();
2489 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002490 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002491 config.ice_candidate_pool_size = 1;
2492 EXPECT_TRUE(pc_->SetConfiguration(config));
2493
2494 // Set remote offer; can still change pool size at this point.
2495 CreateOfferAsRemoteDescription();
2496 config.ice_candidate_pool_size = 2;
2497 EXPECT_TRUE(pc_->SetConfiguration(config));
2498
2499 // Set local answer; now it's too late.
2500 CreateAnswerAsLocalDescription();
2501 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002502 RTCError error;
2503 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2504 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2505}
2506
deadbeef42a42632017-03-10 15:18:00 -08002507// Test that after setting an answer, extra pooled sessions are discarded. The
2508// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002509TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002510 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2511 CreatePeerConnection();
2512
2513 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002514 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002515 config.ice_candidate_pool_size = 4;
2516 EXPECT_TRUE(pc_->SetConfiguration(config));
2517
2518 // Do offer/answer.
2519 CreateOfferAsRemoteDescription();
2520 CreateAnswerAsLocalDescription();
2521
2522 // Expect no pooled sessions to be left.
2523 const cricket::PortAllocatorSession* session =
2524 port_allocator_->GetPooledSession();
2525 EXPECT_EQ(nullptr, session);
2526}
2527
2528// After Close is called, pooled candidates should be discarded so as to not
2529// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002530TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002531 CreatePeerConnection();
2532
Steve Anton36da6ff2018-02-16 16:04:20 -08002533 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002534 config.ice_candidate_pool_size = 3;
2535 EXPECT_TRUE(pc_->SetConfiguration(config));
2536 pc_->Close();
2537
2538 // Expect no pooled sessions to be left.
2539 const cricket::PortAllocatorSession* session =
2540 port_allocator_->GetPooledSession();
2541 EXPECT_EQ(nullptr, session);
2542}
2543
deadbeef293e9262017-01-11 12:28:30 -08002544// Test that SetConfiguration returns an invalid modification error if
2545// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002546TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002547 SetConfigurationReturnsInvalidModificationError) {
2548 PeerConnectionInterface::RTCConfiguration config;
2549 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2550 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2551 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002552 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002553
Steve Anton36da6ff2018-02-16 16:04:20 -08002554 PeerConnectionInterface::RTCConfiguration modified_config =
2555 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002556 modified_config.bundle_policy =
2557 PeerConnectionInterface::kBundlePolicyMaxBundle;
2558 RTCError error;
2559 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2560 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2561
Steve Anton36da6ff2018-02-16 16:04:20 -08002562 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002563 modified_config.rtcp_mux_policy =
2564 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2565 error.set_type(RTCErrorType::NONE);
2566 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2567 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2568
Steve Anton36da6ff2018-02-16 16:04:20 -08002569 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002570 modified_config.continual_gathering_policy =
2571 PeerConnectionInterface::GATHER_CONTINUALLY;
2572 error.set_type(RTCErrorType::NONE);
2573 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2574 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2575}
2576
2577// Test that SetConfiguration returns a range error if the candidate pool size
2578// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002579TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002580 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2581 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002582 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002583 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002584
2585 config.ice_candidate_pool_size = -1;
2586 RTCError error;
2587 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2588 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2589
2590 config.ice_candidate_pool_size = INT_MAX;
2591 error.set_type(RTCErrorType::NONE);
2592 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2593 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2594}
2595
2596// Test that SetConfiguration returns a syntax error if parsing an ICE server
2597// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002598TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002599 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2600 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002601 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002602 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002603
2604 PeerConnectionInterface::IceServer bad_server;
2605 bad_server.uri = "stunn:www.example.com";
2606 config.servers.push_back(bad_server);
2607 RTCError error;
2608 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2609 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2610}
2611
2612// Test that SetConfiguration returns an invalid parameter error if a TURN
2613// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002614TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002615 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2616 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002617 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002618 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002619
2620 PeerConnectionInterface::IceServer bad_server;
2621 bad_server.uri = "turn:www.example.com";
2622 // Missing password.
2623 bad_server.username = "foo";
2624 config.servers.push_back(bad_server);
2625 RTCError error;
2626 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2627 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002628}
2629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630// Test that PeerConnection::Close changes the states to closed and all remote
2631// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002632TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002633 // Initialize a PeerConnection and negotiate local and remote session
2634 // description.
2635 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002636
2637 // With Plan B, verify the stream count. The analog with Unified Plan is the
2638 // RtpTransceiver count.
2639 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2640 ASSERT_EQ(1u, pc_->local_streams()->count());
2641 ASSERT_EQ(1u, pc_->remote_streams()->count());
2642 } else {
2643 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2644 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645
2646 pc_->Close();
2647
2648 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2649 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2650 pc_->ice_connection_state());
2651 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2652 pc_->ice_gathering_state());
2653
Steve Anton36da6ff2018-02-16 16:04:20 -08002654 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2655 EXPECT_EQ(1u, pc_->local_streams()->count());
2656 EXPECT_EQ(1u, pc_->remote_streams()->count());
2657 } else {
2658 // Verify that the RtpTransceivers are still present but all stopped.
2659 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Mirko Bonadei739baf02019-01-27 17:29:42 +01002660 for (const auto& transceiver : pc_->GetTransceivers()) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002661 EXPECT_TRUE(transceiver->stopped());
2662 }
2663 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002664
Steve Anton36da6ff2018-02-16 16:04:20 -08002665 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2666 ASSERT_TRUE(audio_receiver);
2667 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2668 ASSERT_TRUE(video_receiver);
2669
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002670 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002671 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002672 audio_receiver->track()->state(), kTimeout);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002673 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002674 video_receiver->track()->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002675}
2676
2677// Test that PeerConnection methods fails gracefully after
2678// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002679// Don't run under Unified Plan since the stream API is not available.
2680TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002681 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002682 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002683 CreateOfferAsRemoteDescription();
2684 CreateAnswerAsLocalDescription();
2685
2686 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002687 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002688 pc_->local_streams()->at(0);
2689
2690 pc_->Close();
2691
2692 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002693 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002694
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002695 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2696
2697 EXPECT_TRUE(pc_->local_description() != NULL);
2698 EXPECT_TRUE(pc_->remote_description() != NULL);
2699
kwibergd1fe2812016-04-27 06:47:29 -07002700 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002701 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002702 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002703 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002704
2705 std::string sdp;
2706 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002707 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002708 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002709 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710
2711 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002712 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002713 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002714 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002715}
2716
2717// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002718TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719 InitiateCall();
2720 pc_->Close();
2721 DoGetStats(NULL);
2722}
deadbeefab9b2d12015-10-14 11:33:11 -07002723
2724// NOTE: The series of tests below come from what used to be
2725// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2726// setting a remote or local description has the expected effects.
2727
2728// This test verifies that the remote MediaStreams corresponding to a received
2729// SDP string is created. In this test the two separate MediaStreams are
2730// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002731TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002732 RTCConfiguration config;
2733 config.enable_dtls_srtp = true;
2734 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002735 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002736
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002737 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002738 EXPECT_TRUE(
2739 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2740 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2741 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2742
2743 // Create a session description based on another SDP with another
2744 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002745 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002746
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002747 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002748 EXPECT_TRUE(
2749 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2750}
2751
2752// This test verifies that when remote tracks are added/removed from SDP, the
2753// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002754// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2755// specific behavior.
2756TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002757 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002758 RTCConfiguration config;
2759 config.enable_dtls_srtp = true;
2760 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002761 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002762 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002763 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002764 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2765 reference_collection_));
2766
2767 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002768 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002769 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002770 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002771 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2772 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002773 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002774 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2775 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002776 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002777 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2778 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002779
2780 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002781 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002782 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002783 MockTrackObserver audio_track_observer(audio_track2);
2784 MockTrackObserver video_track_observer(video_track2);
2785
2786 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2787 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002788 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002789 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2790 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002791 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002792 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002793 audio_track2->state(), kTimeout);
2794 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2795 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002796}
2797
2798// This tests that remote tracks are ended if a local session description is set
2799// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002800TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002801 RTCConfiguration config;
2802 config.enable_dtls_srtp = true;
2803 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002804 // First create and set a remote offer, then reject its video content in our
2805 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002806 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2807 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2808 ASSERT_TRUE(audio_receiver);
2809 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2810 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002811
Steve Anton36da6ff2018-02-16 16:04:20 -08002812 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2813 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002814 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002815 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2816 video_receiver->track();
2817 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002818
kwibergd1fe2812016-04-27 06:47:29 -07002819 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002820 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002821 cricket::ContentInfo* video_info =
2822 local_answer->description()->GetContentByName("video");
2823 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002824 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002825 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2826 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002827
2828 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002829 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002830 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002831 video_info = local_offer->description()->GetContentByName("video");
2832 ASSERT_TRUE(video_info != nullptr);
2833 video_info->rejected = true;
2834 cricket::ContentInfo* audio_info =
2835 local_offer->description()->GetContentByName("audio");
2836 ASSERT_TRUE(audio_info != nullptr);
2837 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002838 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002839 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002840 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2841 kTimeout);
2842 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2843 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002844}
2845
2846// This tests that we won't crash if the remote track has been removed outside
2847// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002848// Don't run under Unified Plan since the stream API is not available.
2849TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002850 RTCConfiguration config;
2851 config.enable_dtls_srtp = true;
2852 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002853 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002854 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2855 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2856 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2857
kwibergd1fe2812016-04-27 06:47:29 -07002858 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002859 webrtc::CreateSessionDescription(SdpType::kAnswer,
2860 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002861 cricket::ContentInfo* video_info =
2862 local_answer->description()->GetContentByName("video");
2863 video_info->rejected = true;
2864 cricket::ContentInfo* audio_info =
2865 local_answer->description()->GetContentByName("audio");
2866 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002867 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002868
2869 // No crash is a pass.
2870}
2871
deadbeef5e97fb52015-10-15 12:49:08 -07002872// This tests that if a recvonly remote description is set, no remote streams
2873// will be created, even if the description contains SSRCs/MSIDs.
2874// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002875TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002876 RTCConfiguration config;
2877 config.enable_dtls_srtp = true;
2878 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002879
Steve Anton36da6ff2018-02-16 16:04:20 -08002880 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002881 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002882 CreateAndSetRemoteOffer(recvonly_offer);
2883
2884 EXPECT_EQ(0u, observer_.remote_streams()->count());
2885}
2886
deadbeefab9b2d12015-10-14 11:33:11 -07002887// This tests that a default MediaStream is created if a remote session
2888// description doesn't contain any streams and no MSID support.
2889// It also tests that the default stream is updated if a video m-line is added
2890// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002891// Don't run under Unified Plan since this behavior is Plan B specific.
2892TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002893 RTCConfiguration config;
2894 config.enable_dtls_srtp = true;
2895 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002896 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2897
2898 ASSERT_EQ(1u, observer_.remote_streams()->count());
2899 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2900
2901 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2902 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002903 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002904
2905 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2906 ASSERT_EQ(1u, observer_.remote_streams()->count());
2907 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2908 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002909 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2910 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002911 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2912 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002913 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2914 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002915}
2916
2917// This tests that a default MediaStream is created if a remote session
2918// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002919// Don't run under Unified Plan since this behavior is Plan B specific.
2920TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002921 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002922 RTCConfiguration config;
2923 config.enable_dtls_srtp = true;
2924 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002925 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2926
2927 ASSERT_EQ(1u, observer_.remote_streams()->count());
2928 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2929
2930 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2931 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002932 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002933}
2934
2935// This tests that it won't crash when PeerConnection tries to remove
2936// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002937// Don't run under Unified Plan since this behavior is Plan B specific.
2938TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002939 RTCConfiguration config;
2940 config.enable_dtls_srtp = true;
2941 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002942 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002943 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2944 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2945 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2946
2947 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2948
2949 // No crash is a pass.
2950}
2951
2952// This tests that a default MediaStream is created if the remote session
2953// description doesn't contain any streams and don't contain an indication if
2954// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002955// Don't run under Unified Plan since this behavior is Plan B specific.
2956TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002957 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002958 RTCConfiguration config;
2959 config.enable_dtls_srtp = true;
2960 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002961 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2962
2963 ASSERT_EQ(1u, observer_.remote_streams()->count());
2964 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2965 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2966 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2967}
2968
2969// This tests that a default MediaStream is not created if the remote session
2970// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002971// Don't run under Unified Plan since this behavior is Plan B specific.
2972TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002973 RTCConfiguration config;
2974 config.enable_dtls_srtp = true;
2975 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002976 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2977 EXPECT_EQ(0u, observer_.remote_streams()->count());
2978}
2979
deadbeefbda7e0b2015-12-08 17:13:40 -08002980// This tests that when setting a new description, the old default tracks are
2981// not destroyed and recreated.
2982// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002983// Don't run under Unified Plan since this behavior is Plan B specific.
2984TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002985 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002986 RTCConfiguration config;
2987 config.enable_dtls_srtp = true;
2988 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002989 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2990
2991 ASSERT_EQ(1u, observer_.remote_streams()->count());
2992 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2993 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2994
2995 // Set the track to "disabled", then set a new description and ensure the
2996 // track is still disabled, which ensures it hasn't been recreated.
2997 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2998 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2999 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3000 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3001}
3002
deadbeefab9b2d12015-10-14 11:33:11 -07003003// This tests that a default MediaStream is not created if a remote session
3004// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003005// Don't run under Unified Plan since this behavior is Plan B specific.
3006TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003007 RTCConfiguration config;
3008 config.enable_dtls_srtp = true;
3009 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003010 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003011 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003012 EXPECT_TRUE(
3013 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3014
3015 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3016 EXPECT_EQ(0u, observer_.remote_streams()->count());
3017}
3018
Seth Hampson5897a6e2018-04-03 11:16:33 -07003019// This tests that a default MediaStream is created if a remote SDP comes from
3020// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3021TEST_F(PeerConnectionInterfaceTestPlanB,
3022 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003023 RTCConfiguration config;
3024 config.enable_dtls_srtp = true;
3025 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003026 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3027 // Add a=msid lines to simulate a Unified Plan endpoint that only
3028 // signals stream IDs with a=msid lines.
3029 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3030
3031 CreateAndSetRemoteOffer(sdp_string);
3032
3033 ASSERT_EQ(1u, observer_.remote_streams()->count());
3034 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3035 EXPECT_EQ("default", remote_stream->id());
3036 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3037}
3038
Seth Hampson5b4f0752018-04-02 16:31:36 -07003039// This tests that when a Plan B endpoint receives an SDP that signals no media
3040// stream IDs indicated by the special character "-" in the a=msid line, that
3041// a default stream ID will be used for the MediaStream ID. This can occur
3042// when a Unified Plan endpoint signals no media stream IDs, but signals both
3043// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3044TEST_F(PeerConnectionInterfaceTestPlanB,
3045 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003046 RTCConfiguration config;
3047 config.enable_dtls_srtp = true;
3048 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003049 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3050 // the sender's stream ID will be interpreted as no stream IDs.
3051 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3052 sdp_string.append("a=msid:- audiotrack0\n");
3053
3054 CreateAndSetRemoteOffer(sdp_string);
3055
3056 ASSERT_EQ(1u, observer_.remote_streams()->count());
3057 // Because SSRCs are signaled the track ID will be what was signaled in the
3058 // a=msid line.
3059 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3060 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3061 EXPECT_EQ("default", remote_stream->id());
3062 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003063
3064 // Previously a bug ocurred when setting the remote description a second time.
3065 // This is because we checked equality of the remote StreamParams stream ID
3066 // (empty), and the previously set stream ID for the remote sender
3067 // ("default"). This cause a track to be removed, then added, when really
3068 // nothing should occur because it is the same track.
3069 CreateAndSetRemoteOffer(sdp_string);
3070 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3071 EXPECT_EQ(1u, observer_.add_track_events_.size());
3072 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3073 remote_stream = observer_.remote_streams()->at(0);
3074 EXPECT_EQ("default", remote_stream->id());
3075 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003076}
3077
deadbeefab9b2d12015-10-14 11:33:11 -07003078// This tests that an RtpSender is created when the local description is set
3079// after adding a local stream.
3080// TODO(deadbeef): This test and the one below it need to be updated when
3081// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003082// Don't run under Unified Plan since this behavior is Plan B specific.
3083TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003084 RTCConfiguration config;
3085 config.enable_dtls_srtp = true;
3086 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003087
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003088 // Create an offer with 1 stream with 2 tracks of each type.
3089 rtc::scoped_refptr<StreamCollection> stream_collection =
3090 CreateStreamCollection(1, 2);
3091 pc_->AddStream(stream_collection->at(0));
3092 std::unique_ptr<SessionDescriptionInterface> offer;
3093 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003094 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003095
deadbeefab9b2d12015-10-14 11:33:11 -07003096 auto senders = pc_->GetSenders();
3097 EXPECT_EQ(4u, senders.size());
3098 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3099 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3100 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3101 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3102
3103 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003104 pc_->RemoveStream(stream_collection->at(0));
3105 stream_collection = CreateStreamCollection(1, 1);
3106 pc_->AddStream(stream_collection->at(0));
3107 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003108 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003109
deadbeefab9b2d12015-10-14 11:33:11 -07003110 senders = pc_->GetSenders();
3111 EXPECT_EQ(2u, senders.size());
3112 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3113 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3114 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3115 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3116}
3117
3118// This tests that an RtpSender is created when the local description is set
3119// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003120// Don't run under Unified Plan since this behavior is Plan B specific.
3121TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003122 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003123 RTCConfiguration config;
3124 config.enable_dtls_srtp = true;
3125 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003126
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003127 rtc::scoped_refptr<StreamCollection> stream_collection =
3128 CreateStreamCollection(1, 2);
3129 // Add a stream to create the offer, but remove it afterwards.
3130 pc_->AddStream(stream_collection->at(0));
3131 std::unique_ptr<SessionDescriptionInterface> offer;
3132 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3133 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003134
Steve Antondb45ca82017-09-11 18:27:34 -07003135 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003136 auto senders = pc_->GetSenders();
3137 EXPECT_EQ(0u, senders.size());
3138
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003139 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003140 senders = pc_->GetSenders();
3141 EXPECT_EQ(4u, senders.size());
3142 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3143 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3144 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3145 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3146}
3147
3148// This tests that the expected behavior occurs if the SSRC on a local track is
3149// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003150TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003151 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003152 RTCConfiguration config;
3153 config.enable_dtls_srtp = true;
3154 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003155
Steve Anton36da6ff2018-02-16 16:04:20 -08003156 AddAudioTrack(kAudioTracks[0]);
3157 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003158 std::unique_ptr<SessionDescriptionInterface> offer;
3159 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3160 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003161 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3162 webrtc::CreateSessionDescription(
3163 webrtc::SdpType::kOffer, offer->session_id(),
3164 offer->session_version(),
3165 absl::WrapUnique(offer->description()->Copy()));
Steve Antondb45ca82017-09-11 18:27:34 -07003166 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003167
deadbeefab9b2d12015-10-14 11:33:11 -07003168 auto senders = pc_->GetSenders();
3169 EXPECT_EQ(2u, senders.size());
3170 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3171 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3172
3173 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003174 cricket::MediaContentDescription* desc =
3175 cricket::GetFirstAudioContentDescription(modified_offer->description());
3176 ASSERT_TRUE(desc != NULL);
3177 for (StreamParams& stream : desc->mutable_streams()) {
3178 for (unsigned int& ssrc : stream.ssrcs) {
3179 ++ssrc;
3180 }
3181 }
deadbeefab9b2d12015-10-14 11:33:11 -07003182
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003183 desc =
3184 cricket::GetFirstVideoContentDescription(modified_offer->description());
3185 ASSERT_TRUE(desc != NULL);
3186 for (StreamParams& stream : desc->mutable_streams()) {
3187 for (unsigned int& ssrc : stream.ssrcs) {
3188 ++ssrc;
3189 }
3190 }
3191
Steve Antondb45ca82017-09-11 18:27:34 -07003192 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003193 senders = pc_->GetSenders();
3194 EXPECT_EQ(2u, senders.size());
3195 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3196 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3197 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3198 // changed.
3199}
3200
3201// This tests that the expected behavior occurs if a new session description is
3202// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003203// Don't run under Unified Plan since the stream API is not available.
3204TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003205 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003206 RTCConfiguration config;
3207 config.enable_dtls_srtp = true;
3208 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003209
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003210 rtc::scoped_refptr<StreamCollection> stream_collection =
3211 CreateStreamCollection(2, 1);
3212 pc_->AddStream(stream_collection->at(0));
3213 std::unique_ptr<SessionDescriptionInterface> offer;
3214 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003215 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003216
deadbeefab9b2d12015-10-14 11:33:11 -07003217 auto senders = pc_->GetSenders();
3218 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003219 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3220 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003221
3222 // Add a new MediaStream but with the same tracks as in the first stream.
3223 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3224 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003225 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3226 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003227 pc_->AddStream(stream_1);
3228
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003229 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003230 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003231
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003232 auto new_senders = pc_->GetSenders();
3233 // Should be the same senders as before, but with updated stream id.
3234 // Note that this behavior is subject to change in the future.
3235 // We may decide the PC should ignore existing tracks in AddStream.
3236 EXPECT_EQ(senders, new_senders);
3237 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3238 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003239}
3240
zhihuang81c3a032016-11-17 12:06:24 -08003241// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003242TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003243 RTCConfiguration config;
3244 config.enable_dtls_srtp = true;
3245 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003246 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3247 EXPECT_EQ(observer_.num_added_tracks_, 1);
3248 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3249
3250 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003251 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003252 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003253 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3254}
3255
deadbeefd1a38b52016-12-10 13:15:33 -08003256// Test that when SetConfiguration is called and the configuration is
3257// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003258TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003259 PeerConnectionInterface::RTCConfiguration config;
3260 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003261 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003262 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003263 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3264 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003265
3266 // Do initial offer/answer so there's something to restart.
3267 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003268 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003269
3270 // Grab the ufrags.
3271 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3272
3273 // Change ICE policy, which should trigger an ICE restart on the next offer.
3274 config.type = PeerConnectionInterface::kAll;
3275 EXPECT_TRUE(pc_->SetConfiguration(config));
3276 CreateOfferAsLocalDescription();
3277
3278 // Grab the new ufrags.
3279 std::vector<std::string> subsequent_ufrags =
3280 GetUfrags(pc_->local_description());
3281
3282 // Sanity check.
3283 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3284 // Check that each ufrag is different.
3285 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3286 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3287 }
3288}
3289
3290// Test that when SetConfiguration is called and the configuration *isn't*
3291// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003292TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003293 PeerConnectionInterface::RTCConfiguration config;
3294 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003295 CreatePeerConnection(config);
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;
Niels Möllerf06f9232018-08-07 12:32:18 +02003329 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003330 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003331 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3332 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003333
3334 // Do initial offer/answer so there's something to restart.
3335 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003336 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003337
3338 // Change ICE policy, which should set the "needs-ice-restart" flag.
3339 config.type = PeerConnectionInterface::kAll;
3340 EXPECT_TRUE(pc_->SetConfiguration(config));
3341
3342 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003343 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003344 webrtc::CreateSessionDescription(SdpType::kOffer,
3345 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003346 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003347 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3348 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003349 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003350 CreateAnswerAsLocalDescription();
3351
3352 // Grab the ufrags.
3353 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003354 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003355
3356 // Create offer and grab the new ufrags.
3357 CreateOfferAsLocalDescription();
3358 std::vector<std::string> subsequent_ufrags =
3359 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003360 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003361
3362 // Ensure that only the ufrag for the second m= section changed.
3363 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3364 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3365}
3366
deadbeeffe4a8a42016-12-20 17:56:17 -08003367// Tests that the methods to return current/pending descriptions work as
3368// expected at different points in the offer/answer exchange. This test does
3369// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003370TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003371 // This disables DTLS so we can apply an answer to ourselves.
3372 CreatePeerConnection();
3373
3374 // Create initial local offer and get SDP (which will also be used as
3375 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003376 std::unique_ptr<SessionDescriptionInterface> local_offer;
3377 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003378 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003379 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003380
3381 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003382 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3383 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3384 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003385 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3386 EXPECT_EQ(nullptr, pc_->current_local_description());
3387 EXPECT_EQ(nullptr, pc_->current_remote_description());
3388
3389 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003390 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003391 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003392 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3393 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3394 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3395 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003396 EXPECT_EQ(nullptr, pc_->current_local_description());
3397 EXPECT_EQ(nullptr, pc_->current_remote_description());
3398
3399 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003400 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003401 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003402 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3403 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003404 EXPECT_EQ(nullptr, pc_->pending_local_description());
3405 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003406 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3407 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003408
3409 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003410 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003411 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003412 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3413 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3414 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003415 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003416 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3417 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003418
3419 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003420 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003421 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003422 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3423 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3424 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3425 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3426 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3427 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003428
3429 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003430 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003431 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003432 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3433 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003434 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3435 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003436 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3437 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003438}
3439
zhihuang77985012017-02-07 15:45:16 -08003440// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3441// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003442// This version tests the StartRtcEventLog version that receives a file.
Steve Anton36da6ff2018-02-16 16:04:20 -08003443TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003444 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003445 CreatePeerConnection();
3446 // The RtcEventLog will be reset when the PeerConnection is closed.
3447 pc_->Close();
3448
Elad Alon9e6565b2017-10-11 16:04:13 +02003449 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3450 std::string filename = webrtc::test::OutputPath() +
3451 test_info->test_case_name() + test_info->name();
3452 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3453
3454 constexpr int64_t max_size_bytes = 1024;
3455
zhihuang77985012017-02-07 15:45:16 -08003456 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3457 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003458
3459 // Cleanup.
3460 rtc::ClosePlatformFile(file);
3461 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003462}
3463
Elad Alon99c3fe52017-10-13 16:29:40 +02003464// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3465// after the PeerConnection is closed.
3466// This version tests the StartRtcEventLog version that receives an object
3467// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003468TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003469 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3470 CreatePeerConnection();
3471 // The RtcEventLog will be reset when the PeerConnection is closed.
3472 pc_->Close();
3473
3474 rtc::PlatformFile file = 0;
3475 int64_t max_size_bytes = 1024;
3476 EXPECT_FALSE(pc_->StartRtcEventLog(
Karl Wiberg918f50c2018-07-05 11:40:33 +02003477 absl::make_unique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
Bjorn Tereliusde939432017-11-20 17:38:14 +01003478 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003479 pc_->StopRtcEventLog();
3480}
3481
deadbeef30952b42017-04-21 02:41:29 -07003482// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003483TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003484 CreatePeerConnection();
3485
3486 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003487 RTCOfferAnswerOptions options;
3488 options.offer_to_receive_audio = 1;
3489 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003490 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003491 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003492 cricket::SessionDescription* desc = offer->description();
3493 ASSERT_EQ(2u, desc->transport_infos().size());
3494 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3495 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3496
3497 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003498 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003499 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003500 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003501 desc = answer->description();
3502 ASSERT_EQ(2u, desc->transport_infos().size());
3503 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3504 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3505}
3506
deadbeef1dcb1642017-03-29 21:08:16 -07003507// Test that ICE renomination isn't offered if it's not enabled in the PC's
3508// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003509TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003510 PeerConnectionInterface::RTCConfiguration config;
3511 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003512 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003513 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003514
3515 std::unique_ptr<SessionDescriptionInterface> offer;
3516 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3517 cricket::SessionDescription* desc = offer->description();
3518 EXPECT_EQ(1u, desc->transport_infos().size());
3519 EXPECT_FALSE(
3520 desc->transport_infos()[0].description.GetIceParameters().renomination);
3521}
3522
3523// Test that the ICE renomination option is present in generated offers/answers
3524// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003525TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003526 PeerConnectionInterface::RTCConfiguration config;
3527 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003528 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003529 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003530
3531 std::unique_ptr<SessionDescriptionInterface> offer;
3532 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3533 cricket::SessionDescription* desc = offer->description();
3534 EXPECT_EQ(1u, desc->transport_infos().size());
3535 EXPECT_TRUE(
3536 desc->transport_infos()[0].description.GetIceParameters().renomination);
3537
3538 // Set the offer as a remote description, then create an answer and ensure it
3539 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003540 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003541 std::unique_ptr<SessionDescriptionInterface> answer;
3542 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3543 desc = answer->description();
3544 EXPECT_EQ(1u, desc->transport_infos().size());
3545 EXPECT_TRUE(
3546 desc->transport_infos()[0].description.GetIceParameters().renomination);
3547}
3548
3549// Test that if CreateOffer is called with the deprecated "offer to receive
3550// audio/video" constraints, they're processed and result in an offer with
3551// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003552TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003553 CreatePeerConnection();
3554
Niels Möllerf06f9232018-08-07 12:32:18 +02003555 RTCOfferAnswerOptions options;
3556 options.offer_to_receive_audio = 1;
3557 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003558 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003559 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003560
3561 cricket::SessionDescription* desc = offer->description();
3562 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3563 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3564 ASSERT_NE(nullptr, audio);
3565 ASSERT_NE(nullptr, video);
3566 EXPECT_FALSE(audio->rejected);
3567 EXPECT_FALSE(video->rejected);
3568}
3569
3570// Test that if CreateAnswer is called with the deprecated "offer to receive
3571// audio/video" constraints, they're processed and can be used to reject an
3572// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003573// Don't run under Unified Plan since this behavior is not supported.
3574TEST_F(PeerConnectionInterfaceTestPlanB,
3575 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003576 CreatePeerConnection();
3577
3578 // First, create an offer with audio/video and apply it as a remote
3579 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003580 RTCOfferAnswerOptions options;
3581 options.offer_to_receive_audio = 1;
3582 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003583 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003584 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003585 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003586
3587 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003588 options.offer_to_receive_audio = 0;
3589 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003590 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003591 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003592
3593 cricket::SessionDescription* desc = answer->description();
3594 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3595 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3596 ASSERT_NE(nullptr, audio);
3597 ASSERT_NE(nullptr, video);
3598 EXPECT_TRUE(audio->rejected);
3599 EXPECT_TRUE(video->rejected);
3600}
3601
deadbeef1dcb1642017-03-29 21:08:16 -07003602// Test that negotiation can succeed with a data channel only, and with the max
3603// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003604#ifdef HAVE_SCTP
3605TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3606#else
3607TEST_P(PeerConnectionInterfaceTest,
3608 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3609#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003610 PeerConnectionInterface::RTCConfiguration config;
3611 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003612 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003613
3614 // First, create an offer with only a data channel and apply it as a remote
3615 // description.
3616 pc_->CreateDataChannel("test", nullptr);
3617 std::unique_ptr<SessionDescriptionInterface> offer;
3618 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003619 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003620
3621 // Create and set answer as well.
3622 std::unique_ptr<SessionDescriptionInterface> answer;
3623 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003624 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003625}
3626
Steve Anton36da6ff2018-02-16 16:04:20 -08003627TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003628 CreatePeerConnection();
3629 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003630 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003631 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3632}
3633
Steve Anton36da6ff2018-02-16 16:04:20 -08003634TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003635 CreatePeerConnection();
3636 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003637 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003638 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3639}
3640
Steve Anton36da6ff2018-02-16 16:04:20 -08003641TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003642 CreatePeerConnection();
3643 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003644 bitrate.min_bitrate_bps = 5;
3645 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003646 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3647}
3648
Steve Anton36da6ff2018-02-16 16:04:20 -08003649TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003650 CreatePeerConnection();
3651 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003652 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003653 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3654}
3655
Steve Anton36da6ff2018-02-16 16:04:20 -08003656TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003657 CreatePeerConnection();
3658 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003659 bitrate.current_bitrate_bps = 10;
3660 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003661 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3662}
3663
Steve Anton36da6ff2018-02-16 16:04:20 -08003664TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003665 CreatePeerConnection();
3666 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003667 bitrate.min_bitrate_bps = 10;
3668 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003669 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3670}
3671
Steve Anton36da6ff2018-02-16 16:04:20 -08003672TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003673 CreatePeerConnection();
3674 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003675 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003676 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3677}
3678
Steve Anton038834f2017-07-14 15:59:59 -07003679// ice_regather_interval_range requires WebRTC to be configured for continual
3680// gathering already.
Steve Anton36da6ff2018-02-16 16:04:20 -08003681TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003682 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3683 PeerConnectionInterface::RTCConfiguration config;
3684 config.ice_regather_interval_range.emplace(1000, 2000);
3685 config.continual_gathering_policy =
3686 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3687 CreatePeerConnectionExpectFail(config);
3688}
3689
3690// Ensures that there is no error when ice_regather_interval_range is set with
3691// continual gathering enabled.
Steve Anton36da6ff2018-02-16 16:04:20 -08003692TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003693 SetIceRegatherIntervalRangeWithContinualGathering) {
3694 PeerConnectionInterface::RTCConfiguration config;
3695 config.ice_regather_interval_range.emplace(1000, 2000);
3696 config.continual_gathering_policy =
3697 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
Niels Möllerf06f9232018-08-07 12:32:18 +02003698 CreatePeerConnection(config);
Steve Anton038834f2017-07-14 15:59:59 -07003699}
3700
Niels Möller0c4f7be2018-05-07 14:01:37 +02003701// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003702// by Call's BitrateConstraints, which comes from the SDP or a default value.
3703// This test checks that a call to SetBitrate with a current bitrate that will
3704// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003705TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003706 CreatePeerConnection();
3707 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003708 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003709 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3710}
3711
zhihuang1c378ed2017-08-17 14:10:50 -07003712// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003713TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003714 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3715 RTCOfferAnswerOptions rtc_options;
3716
3717 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3718 // than kMaxOfferToReceiveMedia should be treated as invalid.
3719 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3720 CreatePeerConnection();
3721 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3722
3723 rtc_options.offer_to_receive_audio =
3724 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3725 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3726}
3727
Steve Anton36da6ff2018-02-16 16:04:20 -08003728TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003729 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3730 RTCOfferAnswerOptions rtc_options;
3731
3732 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3733 // than kMaxOfferToReceiveMedia should be treated as invalid.
3734 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3735 CreatePeerConnection();
3736 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3737
3738 rtc_options.offer_to_receive_video =
3739 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3740 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3741}
3742
3743// Test that the audio and video content will be added to an offer if both
3744// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003745TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003746 RTCOfferAnswerOptions rtc_options;
3747 rtc_options.offer_to_receive_audio = 1;
3748 rtc_options.offer_to_receive_video = 1;
3749
3750 std::unique_ptr<SessionDescriptionInterface> offer;
3751 CreatePeerConnection();
3752 offer = CreateOfferWithOptions(rtc_options);
3753 ASSERT_TRUE(offer);
3754 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3755 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3756}
3757
3758// Test that only audio content will be added to the offer if only
3759// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003760TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003761 RTCOfferAnswerOptions rtc_options;
3762 rtc_options.offer_to_receive_audio = 1;
3763 rtc_options.offer_to_receive_video = 0;
3764
3765 std::unique_ptr<SessionDescriptionInterface> offer;
3766 CreatePeerConnection();
3767 offer = CreateOfferWithOptions(rtc_options);
3768 ASSERT_TRUE(offer);
3769 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3770 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3771}
3772
3773// Test that only video content will be added if only |offer_to_receive_video|
3774// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003775TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003776 RTCOfferAnswerOptions rtc_options;
3777 rtc_options.offer_to_receive_audio = 0;
3778 rtc_options.offer_to_receive_video = 1;
3779
3780 std::unique_ptr<SessionDescriptionInterface> offer;
3781 CreatePeerConnection();
3782 offer = CreateOfferWithOptions(rtc_options);
3783 ASSERT_TRUE(offer);
3784 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3785 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3786}
3787
zhihuang1c378ed2017-08-17 14:10:50 -07003788// Test that no media content will be added to the offer if using default
3789// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003790TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003791 RTCOfferAnswerOptions rtc_options;
3792
3793 std::unique_ptr<SessionDescriptionInterface> offer;
3794 CreatePeerConnection();
3795 offer = CreateOfferWithOptions(rtc_options);
3796 ASSERT_TRUE(offer);
3797 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3798 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3799}
3800
3801// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3802// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003803TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3804 CreatePeerConnection();
3805
zhihuang1c378ed2017-08-17 14:10:50 -07003806 RTCOfferAnswerOptions rtc_options;
3807 rtc_options.ice_restart = false;
3808 rtc_options.offer_to_receive_audio = 1;
3809
3810 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003811 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003812 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3813 auto ufrag1 =
3814 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3815 auto pwd1 =
3816 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003817
3818 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3819 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003820 auto ufrag2 =
3821 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3822 auto pwd2 =
3823 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003824
3825 // |ice_restart| is true, the ufrag/pwd should change.
3826 rtc_options.ice_restart = true;
3827 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003828 auto ufrag3 =
3829 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3830 auto pwd3 =
3831 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003832
3833 EXPECT_EQ(ufrag1, ufrag2);
3834 EXPECT_EQ(pwd1, pwd2);
3835 EXPECT_NE(ufrag2, ufrag3);
3836 EXPECT_NE(pwd2, pwd3);
3837}
3838
3839// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3840// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003841TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003842 RTCOfferAnswerOptions rtc_options;
3843 rtc_options.offer_to_receive_audio = 1;
3844 rtc_options.offer_to_receive_video = 1;
3845
3846 std::unique_ptr<SessionDescriptionInterface> offer;
3847 CreatePeerConnection();
3848
3849 rtc_options.use_rtp_mux = true;
3850 offer = CreateOfferWithOptions(rtc_options);
3851 ASSERT_TRUE(offer);
3852 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3853 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3854 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3855
3856 rtc_options.use_rtp_mux = false;
3857 offer = CreateOfferWithOptions(rtc_options);
3858 ASSERT_TRUE(offer);
3859 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3860 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3861 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3862}
3863
zhihuang141aacb2017-08-29 13:23:53 -07003864// This test ensures OnRenegotiationNeeded is called when we add track with
3865// MediaStream -> AddTrack in the same way it is called when we add track with
3866// PeerConnection -> AddTrack.
3867// The test can be removed once addStream is rewritten in terms of addTrack
3868// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003869// Don't run under Unified Plan since the stream API is not available.
3870TEST_F(PeerConnectionInterfaceTestPlanB,
3871 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003872 CreatePeerConnectionWithoutDtls();
3873 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003874 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003875 pc_->AddStream(stream);
3876 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003877 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003878 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003879 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003880 stream->AddTrack(audio_track);
3881 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3882 observer_.renegotiation_needed_ = false;
3883
3884 stream->AddTrack(video_track);
3885 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3886 observer_.renegotiation_needed_ = false;
3887
3888 stream->RemoveTrack(audio_track);
3889 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3890 observer_.renegotiation_needed_ = false;
3891
3892 stream->RemoveTrack(video_track);
3893 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3894 observer_.renegotiation_needed_ = false;
3895}
3896
Zhi Huangb2d355e2017-10-26 17:26:37 -07003897// Tests that an error is returned if a description is applied that has fewer
3898// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003899TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003900 MediaSectionCountEnforcedForSubsequentOffer) {
3901 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003902 AddAudioTrack("audio_label");
3903 AddVideoTrack("video_label");
3904
Zhi Huangb2d355e2017-10-26 17:26:37 -07003905 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003906 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003907 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3908
3909 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003910 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003911 offer->description()->contents().pop_back();
3912 offer->description()->contents().pop_back();
3913 ASSERT_TRUE(offer->description()->contents().empty());
3914 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3915
3916 std::unique_ptr<SessionDescriptionInterface> answer;
3917 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3918 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3919
3920 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003921 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003922 offer->description()->contents().pop_back();
3923 offer->description()->contents().pop_back();
3924 ASSERT_TRUE(offer->description()->contents().empty());
3925 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3926}
3927
Johannes Kron89f874e2018-11-12 10:25:48 +01003928TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3929 RTCConfiguration config;
3930 // Default behavior is false.
3931 CreatePeerConnection(config);
3932 std::unique_ptr<SessionDescriptionInterface> offer;
3933 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3934 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
3935 // Possible to set to true.
3936 config.offer_extmap_allow_mixed = true;
3937 CreatePeerConnection(config);
3938 offer.release();
3939 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3940 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3941}
3942
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003943INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3944 PeerConnectionInterfaceTest,
3945 Values(SdpSemantics::kPlanB,
3946 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003947
nisse51542be2016-02-12 02:27:06 -08003948class PeerConnectionMediaConfigTest : public testing::Test {
3949 protected:
3950 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003951 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003952 pcf_->Initialize();
3953 }
nisseeaabdf62017-05-05 02:23:02 -07003954 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003955 const RTCConfiguration& config) {
3956 rtc::scoped_refptr<PeerConnectionInterface> pc(
3957 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003958 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003959 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003960 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003961 }
3962
zhihuang9763d562016-08-05 11:14:50 -07003963 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003964 MockPeerConnectionObserver observer_;
3965};
3966
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003967// This sanity check validates the test infrastructure itself.
3968TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3969 PeerConnectionInterface::RTCConfiguration config;
3970 rtc::scoped_refptr<PeerConnectionInterface> pc(
3971 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3972 EXPECT_TRUE(pc.get());
3973 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3974 pc->Close(); // No abort -> ok.
3975 SUCCEED();
3976}
3977
nisse51542be2016-02-12 02:27:06 -08003978// This test verifies the default behaviour with no constraints and a
3979// default RTCConfiguration.
3980TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3981 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003982
Niels Möllerf06f9232018-08-07 12:32:18 +02003983 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003984
3985 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003986 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3987 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003988 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003989 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003990}
3991
Niels Möller1d7ecd22018-01-18 15:25:12 +01003992// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003993// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003994TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3995 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003996
Niels Möller71bdda02016-03-31 12:59:59 +02003997 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003998 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003999
Niels Möller1d7ecd22018-01-18 15:25:12 +01004000 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08004001}
4002
Niels Möller6539f692018-01-18 08:58:50 +01004003// This test verifies that the experiment_cpu_load_estimator flag is
4004// propagated from RTCConfiguration to the PeerConnection.
4005TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
4006 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01004007
4008 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02004009 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01004010
4011 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
4012}
4013
deadbeef293e9262017-01-11 12:28:30 -08004014// Tests a few random fields being different.
4015TEST(RTCConfigurationTest, ComparisonOperators) {
4016 PeerConnectionInterface::RTCConfiguration a;
4017 PeerConnectionInterface::RTCConfiguration b;
4018 EXPECT_EQ(a, b);
4019
4020 PeerConnectionInterface::RTCConfiguration c;
4021 c.servers.push_back(PeerConnectionInterface::IceServer());
4022 EXPECT_NE(a, c);
4023
4024 PeerConnectionInterface::RTCConfiguration d;
4025 d.type = PeerConnectionInterface::kRelay;
4026 EXPECT_NE(a, d);
4027
4028 PeerConnectionInterface::RTCConfiguration e;
4029 e.audio_jitter_buffer_max_packets = 5;
4030 EXPECT_NE(a, e);
4031
4032 PeerConnectionInterface::RTCConfiguration f;
4033 f.ice_connection_receiving_timeout = 1337;
4034 EXPECT_NE(a, f);
4035
4036 PeerConnectionInterface::RTCConfiguration g;
4037 g.disable_ipv6 = true;
4038 EXPECT_NE(a, g);
4039
4040 PeerConnectionInterface::RTCConfiguration h(
4041 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4042 EXPECT_NE(a, h);
4043}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004044
4045} // namespace
4046} // namespace webrtc