blob: 7cec6ed9dc058a3294ad40d3f7477fae602ff0eb [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
henrika919dc2e2017-10-12 14:24:55 +0200642 // Use fake audio device module since we're only testing the interface
643 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700644 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
645 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200646 FakeAudioCaptureModule::Create(), audio_encoder_factory,
Anders Carlssonb3306882018-05-14 10:11:42 +0200647 audio_decoder_factory, std::move(video_encoder_factory),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000648 std::move(video_decoder_factory), nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +0100649 webrtc::AudioProcessingBuilder().Create()));
zhihuang38ede132017-06-15 12:52:32 -0700650
651 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
652 webrtc::CreateCallFactory();
653
654 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
655 webrtc::CreateRtcEventLogFactory();
656
657 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
658 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200659 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700660 std::move(event_log_factory));
661 }
662
663 PeerConnectionFactoryForTest(
664 rtc::Thread* network_thread,
665 rtc::Thread* worker_thread,
666 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700667 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
668 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
669 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
670 : webrtc::PeerConnectionFactory(network_thread,
671 worker_thread,
672 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700673 std::move(media_engine),
674 std::move(call_factory),
675 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800676
deadbeefd7850b22017-08-23 10:59:19 -0700677 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700678};
679
Steve Anton36da6ff2018-02-16 16:04:20 -0800680// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
681class PeerConnectionInterfaceBaseTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800683 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
684 : vss_(new rtc::VirtualSocketServer()),
685 main_(vss_.get()),
686 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800687#ifdef WEBRTC_ANDROID
688 webrtc::InitializeAndroidObjects();
689#endif
690 }
691
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700693 // Use fake audio capture module since we're only testing the interface
694 // level, and using a real one could make tests flaky when run in parallel.
695 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700697 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200698 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
699 fake_audio_capture_module_),
700 webrtc::CreateBuiltinAudioEncoderFactory(),
701 webrtc::CreateBuiltinAudioDecoderFactory(),
702 webrtc::CreateBuiltinVideoEncoderFactory(),
703 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
704 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700705 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700706 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700707 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700708 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
710
711 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200712 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
deadbeef293e9262017-01-11 12:28:30 -0800715 // DTLS does not work in a loopback call, so is disabled for most of the
716 // tests in this file.
717 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200718 RTCConfiguration config;
719 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800720
Niels Möllerf06f9232018-08-07 12:32:18 +0200721 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 }
723
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700724 void CreatePeerConnectionWithIceTransportsType(
725 PeerConnectionInterface::IceTransportsType type) {
726 PeerConnectionInterface::RTCConfiguration config;
727 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200728 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700729 }
730
731 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100732 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700733 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800734 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100737 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700738 server.password = password;
739 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200740 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700741 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742
Niels Möllerf06f9232018-08-07 12:32:18 +0200743 void CreatePeerConnection(const RTCConfiguration& config) {
kwibergd1fe2812016-04-27 06:47:29 -0700744 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800745 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
746 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000747
deadbeef1dcb1642017-03-29 21:08:16 -0700748 // Create certificate generator unless DTLS constraint is explicitly set to
749 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200750 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200751
752 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800753 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
754 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000755 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800756 RTCConfiguration modified_config = config;
757 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200758 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200759 modified_config, std::move(port_allocator), std::move(cert_generator),
760 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 ASSERT_TRUE(pc_.get() != NULL);
762 observer_.SetPeerConnectionInterface(pc_.get());
763 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
764 }
765
deadbeef0a6c4ca2015-10-06 11:38:28 -0700766 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700768 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700769 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800770 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800771 config.sdp_semantics = sdp_semantics_;
772 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200773 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800774 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700775 }
776
Steve Anton038834f2017-07-14 15:59:59 -0700777 void CreatePeerConnectionExpectFail(
778 PeerConnectionInterface::RTCConfiguration config) {
779 PeerConnectionInterface::IceServer server;
780 server.uri = kTurnIceServerUri;
781 server.password = kTurnPassword;
782 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800783 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700784 rtc::scoped_refptr<PeerConnectionInterface> pc =
785 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
786 EXPECT_EQ(nullptr, pc);
787 }
788
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100790 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800791 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
792 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
793 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800795 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796
deadbeef0a6c4ca2015-10-06 11:38:28 -0700797 CreatePeerConnectionExpectFail(kStunInvalidPort);
798 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
799 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800
Niels Möllerdb4def92019-03-18 16:53:59 +0100801 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
802 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800803 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
804 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800806 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800808 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800810 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812
813 void ReleasePeerConnection() {
814 pc_ = NULL;
815 observer_.SetPeerConnectionInterface(NULL);
816 }
817
Steve Anton36da6ff2018-02-16 16:04:20 -0800818 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
819 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100820 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800821 }
822
823 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800824 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800825 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800826 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
829 observer_.renegotiation_needed_ = false;
830 }
831
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800835 stream->AddTrack(CreateVideoTrack(label + "v0"));
836 ASSERT_TRUE(pc_->AddStream(stream));
837 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
838 observer_.renegotiation_needed_ = false;
839 }
840
841 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
842 const std::string& label) {
843 return pc_factory_->CreateAudioTrack(label, nullptr);
844 }
845
846 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800847 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800848 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800849 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800850 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
851 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
852 observer_.renegotiation_needed_ = false;
853 }
854
855 void AddAudioStream(const std::string& label) {
856 rtc::scoped_refptr<MediaStreamInterface> stream(
857 pc_factory_->CreateLocalMediaStream(label));
858 stream->AddTrack(CreateAudioTrack(label + "a0"));
859 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
861 observer_.renegotiation_needed_ = false;
862 }
863
Seth Hampson845e8782018-03-02 11:34:10 -0800864 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 const std::string& audio_track_label,
866 const std::string& video_track_label) {
867 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700868 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800869 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800870 stream->AddTrack(CreateAudioTrack(audio_track_label));
871 stream->AddTrack(CreateVideoTrack(video_track_label));
872 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
874 observer_.renegotiation_needed_ = false;
875 }
876
Steve Anton36da6ff2018-02-16 16:04:20 -0800877 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
878 cricket::MediaType media_type) {
879 for (auto receiver : pc_->GetReceivers()) {
880 if (receiver->media_type() == media_type) {
881 return receiver;
882 }
883 }
884 return nullptr;
885 }
886
kwibergd1fe2812016-04-27 06:47:29 -0700887 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200888 const RTCOfferAnswerOptions* options,
889 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200890 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
891 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200893 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200895 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700898 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 return observer->result();
900 }
901
kwibergd1fe2812016-04-27 06:47:29 -0700902 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200903 const RTCOfferAnswerOptions* options) {
904 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906
kwibergd1fe2812016-04-27 06:47:29 -0700907 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200908 const RTCOfferAnswerOptions* options) {
909 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911
Steve Antondb45ca82017-09-11 18:27:34 -0700912 bool DoSetSessionDescription(
913 std::unique_ptr<SessionDescriptionInterface> desc,
914 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200915 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
916 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700918 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700920 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
zhihuang29ff8442016-07-27 11:07:25 -0700922 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
923 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
924 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 return observer->result();
926 }
927
Steve Antondb45ca82017-09-11 18:27:34 -0700928 bool DoSetLocalDescription(
929 std::unique_ptr<SessionDescriptionInterface> desc) {
930 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 }
932
Steve Antondb45ca82017-09-11 18:27:34 -0700933 bool DoSetRemoteDescription(
934 std::unique_ptr<SessionDescriptionInterface> desc) {
935 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937
938 // Calls PeerConnection::GetStats and check the return value.
939 // It does not verify the values in the StatReports since a RTCP packet might
940 // be required.
941 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000942 rtc::scoped_refptr<MockStatsObserver> observer(
943 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200944 if (!pc_->GetStats(observer, track,
945 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 return false;
947 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
948 return observer->called();
949 }
950
Harald Alvestrand89061872018-01-02 14:08:34 +0100951 // Call the standards-compliant GetStats function.
952 bool DoGetRTCStats() {
953 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
954 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
955 pc_->GetStats(callback);
956 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
957 return callback->called();
958 }
959
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800961 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800963 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800964 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800965 } else {
966 // Unified Plan does not support AddStream, so just add an audio and video
967 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800968 AddAudioTrack(kAudioTracks[0], {kStreamId1});
969 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800970 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 CreateOfferReceiveAnswer();
972 }
973
974 // Verify that RTP Header extensions has been negotiated for audio and video.
975 void VerifyRemoteRtpHeaderExtensions() {
976 const cricket::MediaContentDescription* desc =
977 cricket::GetFirstAudioContentDescription(
978 pc_->remote_description()->description());
979 ASSERT_TRUE(desc != NULL);
980 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
981
982 desc = cricket::GetFirstVideoContentDescription(
983 pc_->remote_description()->description());
984 ASSERT_TRUE(desc != NULL);
985 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
986 }
987
988 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700989 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700990 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 std::string sdp;
992 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700993 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800994 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700995 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
997 }
998
deadbeefab9b2d12015-10-14 11:33:11 -0700999 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001000 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001001 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001002 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07001003 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1004 }
1005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001007 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001008 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009
1010 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1011 // audio codec change, even if the parameter has nothing to do with
1012 // receiving. Not all parameters are serialized to SDP.
1013 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1014 // the SessionDescription, it is necessary to do that here to in order to
1015 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1016 // https://code.google.com/p/webrtc/issues/detail?id=1356
1017 std::string sdp;
1018 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001019 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001020 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001021 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1023 }
1024
1025 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001026 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001027 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028
1029 std::string sdp;
1030 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001031 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001032 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001033 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1035 }
1036
1037 void CreateOfferReceiveAnswer() {
1038 CreateOfferAsLocalDescription();
1039 std::string sdp;
1040 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1041 CreateAnswerAsRemoteDescription(sdp);
1042 }
1043
1044 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001045 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001046 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1048 // audio codec change, even if the parameter has nothing to do with
1049 // receiving. Not all parameters are serialized to SDP.
1050 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1051 // the SessionDescription, it is necessary to do that here to in order to
1052 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1053 // https://code.google.com/p/webrtc/issues/detail?id=1356
1054 std::string sdp;
1055 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001056 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001057 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058
Steve Antondb45ca82017-09-11 18:27:34 -07001059 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001061 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001062 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 }
1064
deadbeefab9b2d12015-10-14 11:33:11 -07001065 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001066 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001067 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001068 ASSERT_TRUE(answer);
1069 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1071 }
1072
deadbeefab9b2d12015-10-14 11:33:11 -07001073 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001074 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001075 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001076 ASSERT_TRUE(pr_answer);
1077 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001079 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001080 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001081 ASSERT_TRUE(answer);
1082 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1084 }
1085
Seth Hampson845e8782018-03-02 11:34:10 -08001086 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001087 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001088 // called with the given stream id and expected number of tracks.
1089 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001090 int expected_num_tracks) {
1091 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001092 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001093 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001094 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 }
1096
1097 // Creates an offer and applies it as a local session description.
1098 // Creates an answer with the same SDP an the offer but removes all lines
1099 // that start with a:ssrc"
1100 void CreateOfferReceiveAnswerWithoutSsrc() {
1101 CreateOfferAsLocalDescription();
1102 std::string sdp;
1103 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1104 SetSsrcToZero(&sdp);
1105 CreateAnswerAsRemoteDescription(sdp);
1106 }
1107
deadbeefab9b2d12015-10-14 11:33:11 -07001108 // This function creates a MediaStream with label kStreams[0] and
1109 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1110 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001111 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001112 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001113 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001114 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1115 size_t number_of_video_tracks) {
1116 EXPECT_LE(number_of_audio_tracks, 2u);
1117 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001118
1119 reference_collection_ = StreamCollection::Create();
1120 std::string sdp_ms1 = std::string(kSdpStringInit);
1121
Seth Hampson845e8782018-03-02 11:34:10 -08001122 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001123
1124 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001125 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001126 reference_collection_->AddStream(stream);
1127
1128 if (number_of_audio_tracks > 0) {
1129 sdp_ms1 += std::string(kSdpStringAudio);
1130 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1131 AddAudioTrack(kAudioTracks[0], stream);
1132 }
1133 if (number_of_audio_tracks > 1) {
1134 sdp_ms1 += kSdpStringMs1Audio1;
1135 AddAudioTrack(kAudioTracks[1], stream);
1136 }
1137
1138 if (number_of_video_tracks > 0) {
1139 sdp_ms1 += std::string(kSdpStringVideo);
1140 sdp_ms1 += std::string(kSdpStringMs1Video0);
1141 AddVideoTrack(kVideoTracks[0], stream);
1142 }
1143 if (number_of_video_tracks > 1) {
1144 sdp_ms1 += kSdpStringMs1Video1;
1145 AddVideoTrack(kVideoTracks[1], stream);
1146 }
1147
kwibergd1fe2812016-04-27 06:47:29 -07001148 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001149 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001150 }
1151
1152 void AddAudioTrack(const std::string& track_id,
1153 MediaStreamInterface* stream) {
1154 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1155 webrtc::AudioTrack::Create(track_id, nullptr));
1156 ASSERT_TRUE(stream->AddTrack(audio_track));
1157 }
1158
1159 void AddVideoTrack(const std::string& track_id,
1160 MediaStreamInterface* stream) {
1161 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001162 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001163 webrtc::FakeVideoTrackSource::Create(),
1164 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001165 ASSERT_TRUE(stream->AddTrack(video_track));
1166 }
1167
Steve Anton36da6ff2018-02-16 16:04:20 -08001168 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001169 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001170 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001171 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001172 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1173 return offer;
1174 }
1175
Steve Anton36da6ff2018-02-16 16:04:20 -08001176 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1177 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001178 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001179 std::unique_ptr<SessionDescriptionInterface> offer;
1180 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1181 return offer;
1182 }
1183
1184 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1185 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1186 std::unique_ptr<SessionDescriptionInterface> answer;
1187 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1188 return answer;
1189 }
1190
kwibergfd8be342016-05-14 19:44:11 -07001191 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001192 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001193 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001194 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001195 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1196 return answer;
1197 }
1198
1199 const std::string& GetFirstAudioStreamCname(
1200 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001201 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001202 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001203 return audio_desc->streams()[0].cname;
1204 }
1205
zhihuang1c378ed2017-08-17 14:10:50 -07001206 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1207 const RTCOfferAnswerOptions& offer_answer_options) {
1208 RTC_DCHECK(pc_);
1209 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1210 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1211 pc_->CreateOffer(observer, offer_answer_options);
1212 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1213 return observer->MoveDescription();
1214 }
1215
1216 void CreateOfferWithOptionsAsRemoteDescription(
1217 std::unique_ptr<SessionDescriptionInterface>* desc,
1218 const RTCOfferAnswerOptions& offer_answer_options) {
1219 *desc = CreateOfferWithOptions(offer_answer_options);
1220 ASSERT_TRUE(desc != nullptr);
1221 std::string sdp;
1222 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001223 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001224 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001225 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001226 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1227 }
1228
1229 void CreateOfferWithOptionsAsLocalDescription(
1230 std::unique_ptr<SessionDescriptionInterface>* desc,
1231 const RTCOfferAnswerOptions& offer_answer_options) {
1232 *desc = CreateOfferWithOptions(offer_answer_options);
1233 ASSERT_TRUE(desc != nullptr);
1234 std::string sdp;
1235 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001236 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001237 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001238
Steve Antondb45ca82017-09-11 18:27:34 -07001239 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001240 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1241 }
1242
1243 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001244 RTC_DCHECK(content);
1245 RTC_DCHECK(content->media_description());
1246 for (const cricket::AudioCodec& codec :
1247 content->media_description()->as_audio()->codecs()) {
1248 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001249 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001250 }
zhihuang1c378ed2017-08-17 14:10:50 -07001251 }
1252 return false;
1253 }
1254
Steve Anton36da6ff2018-02-16 16:04:20 -08001255 const char* GetSdpStringWithStream1() const {
1256 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1257 return kSdpStringWithStream1PlanB;
1258 } else {
1259 return kSdpStringWithStream1UnifiedPlan;
1260 }
1261 }
1262
1263 const char* GetSdpStringWithStream1And2() const {
1264 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1265 return kSdpStringWithStream1And2PlanB;
1266 } else {
1267 return kSdpStringWithStream1And2UnifiedPlan;
1268 }
1269 }
1270
deadbeef9a6f4d42017-05-15 19:43:33 -07001271 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1272 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001273 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001274 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001275 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001276 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1277 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1278 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001280 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001281 const SdpSemantics sdp_semantics_;
1282};
1283
1284class PeerConnectionInterfaceTest
1285 : public PeerConnectionInterfaceBaseTest,
1286 public ::testing::WithParamInterface<SdpSemantics> {
1287 protected:
1288 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1289};
1290
1291class PeerConnectionInterfaceTestPlanB
1292 : public PeerConnectionInterfaceBaseTest {
1293 protected:
1294 PeerConnectionInterfaceTestPlanB()
1295 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296};
1297
zhihuang8f65cdf2016-05-06 18:40:30 -07001298// Generate different CNAMEs when PeerConnections are created.
1299// The CNAMEs are expected to be generated randomly. It is possible
1300// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001301TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001302 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001303 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001304 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001305 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001306 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1307 GetFirstAudioStreamCname(offer2.get()));
1308}
1309
Steve Anton36da6ff2018-02-16 16:04:20 -08001310TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001311 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001312 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001313 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001314 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001315 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1316 GetFirstAudioStreamCname(answer2.get()));
1317}
1318
Steve Anton36da6ff2018-02-16 16:04:20 -08001319TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 CreatePeerConnectionWithDifferentConfigurations) {
1321 CreatePeerConnectionWithDifferentConfigurations();
1322}
1323
Steve Anton36da6ff2018-02-16 16:04:20 -08001324TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1326 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1327 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1328 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1329 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1330 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1331 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1332 port_allocator_->candidate_filter());
1333 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1334 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1335}
1336
1337// Test that when a PeerConnection is created with a nonzero candidate pool
1338// size, the pooled PortAllocatorSession is created with all the attributes
1339// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001340TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001341 PeerConnectionInterface::RTCConfiguration config;
1342 PeerConnectionInterface::IceServer server;
1343 server.uri = kStunAddressOnly;
1344 config.servers.push_back(server);
1345 config.type = PeerConnectionInterface::kRelay;
1346 config.disable_ipv6 = true;
1347 config.tcp_candidate_policy =
1348 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001349 config.candidate_network_policy =
1350 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001351 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001352 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001353
1354 const cricket::FakePortAllocatorSession* session =
1355 static_cast<const cricket::FakePortAllocatorSession*>(
1356 port_allocator_->GetPooledSession());
1357 ASSERT_NE(nullptr, session);
1358 EXPECT_EQ(1UL, session->stun_servers().size());
1359 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1360 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001361 EXPECT_LT(0U,
1362 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001363}
1364
deadbeefd21eab32017-07-26 16:50:11 -07001365// Test that network-related RTCConfiguration members are applied to the
1366// PortAllocator when CreatePeerConnection is called. Specifically:
1367// - disable_ipv6_on_wifi
1368// - max_ipv6_networks
1369// - tcp_candidate_policy
1370// - candidate_network_policy
1371// - prune_turn_ports
1372//
1373// Note that the candidate filter (RTCConfiguration::type) is already tested
1374// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001375TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001376 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1377 // Create fake port allocator.
1378 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1379 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1380 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1381
1382 // Create RTCConfiguration with some network-related fields relevant to
1383 // PortAllocator populated.
1384 PeerConnectionInterface::RTCConfiguration config;
1385 config.disable_ipv6_on_wifi = true;
1386 config.max_ipv6_networks = 10;
1387 config.tcp_candidate_policy =
1388 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1389 config.candidate_network_policy =
1390 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1391 config.prune_turn_ports = true;
1392
1393 // Create the PC factory and PC with the above config.
1394 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1395 webrtc::CreatePeerConnectionFactory(
1396 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001397 rtc::Thread::Current(), fake_audio_capture_module_,
1398 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001399 webrtc::CreateBuiltinAudioDecoderFactory(),
1400 webrtc::CreateBuiltinVideoEncoderFactory(),
1401 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1402 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001403 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001404 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1405 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001406 EXPECT_TRUE(pc.get());
1407 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001408
1409 // Now validate that the config fields set above were applied to the
1410 // PortAllocator, as flags or otherwise.
1411 EXPECT_FALSE(raw_port_allocator->flags() &
1412 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1413 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1414 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1415 EXPECT_TRUE(raw_port_allocator->flags() &
1416 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1417 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1418}
1419
deadbeef46c73892016-11-16 19:42:04 -08001420// Check that GetConfiguration returns the configuration the PeerConnection was
1421// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001422TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001423 PeerConnectionInterface::RTCConfiguration config;
1424 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001425 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001426
1427 PeerConnectionInterface::RTCConfiguration returned_config =
1428 pc_->GetConfiguration();
1429 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1430}
1431
1432// Check that GetConfiguration returns the last configuration passed into
1433// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001434TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001435 PeerConnectionInterface::RTCConfiguration starting_config;
1436 starting_config.bundle_policy =
1437 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1438 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001439
Steve Anton36da6ff2018-02-16 16:04:20 -08001440 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001441 config.type = PeerConnectionInterface::kRelay;
Piotr (Peter) Slatalaaa1e7c22018-10-16 10:04:45 -07001442 config.use_media_transport = true;
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001443 config.use_media_transport_for_data_channels = true;
deadbeef46c73892016-11-16 19:42:04 -08001444 EXPECT_TRUE(pc_->SetConfiguration(config));
1445
1446 PeerConnectionInterface::RTCConfiguration returned_config =
1447 pc_->GetConfiguration();
1448 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
Piotr (Peter) Slatalaaa1e7c22018-10-16 10:04:45 -07001449 EXPECT_TRUE(returned_config.use_media_transport);
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001450 EXPECT_TRUE(returned_config.use_media_transport_for_data_channels);
deadbeef46c73892016-11-16 19:42:04 -08001451}
1452
Steve Antonc79268f2018-04-24 09:54:10 -07001453TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1454 CreatePeerConnection();
1455
1456 pc_->Close();
1457
1458 EXPECT_FALSE(
1459 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()));
1460}
1461
Steve Anton36da6ff2018-02-16 16:04:20 -08001462TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001463 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001464 AddVideoStream(kStreamId1);
1465 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 ASSERT_EQ(2u, pc_->local_streams()->count());
1467
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001468 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001469 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001470 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001471 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001472 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001473 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001475 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001476 EXPECT_EQ(3u, pc_->local_streams()->count());
1477
1478 // Remove the third stream.
1479 pc_->RemoveStream(pc_->local_streams()->at(2));
1480 EXPECT_EQ(2u, pc_->local_streams()->count());
1481
1482 // Remove the second stream.
1483 pc_->RemoveStream(pc_->local_streams()->at(1));
1484 EXPECT_EQ(1u, pc_->local_streams()->count());
1485
1486 // Remove the first stream.
1487 pc_->RemoveStream(pc_->local_streams()->at(0));
1488 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489}
1490
deadbeefab9b2d12015-10-14 11:33:11 -07001491// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001492// Don't run under Unified Plan since the stream API is not available.
1493TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001494 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001495 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001496 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001497 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001498
deadbeefab9b2d12015-10-14 11:33:11 -07001499 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001500 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001501 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001502
deadbeefab9b2d12015-10-14 11:33:11 -07001503 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001504 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001505 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001506
1507 // Add another stream and ensure the offer includes both the old and new
1508 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001509 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001510 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001511
Steve Antonb1c1de12017-12-21 15:14:30 -08001512 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001513 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1514 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001515
Steve Antonb1c1de12017-12-21 15:14:30 -08001516 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001517 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1518 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001519}
1520
Steve Anton36da6ff2018-02-16 16:04:20 -08001521// Don't run under Unified Plan since the stream API is not available.
1522TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001523 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001524 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 ASSERT_EQ(1u, pc_->local_streams()->count());
1526 pc_->RemoveStream(pc_->local_streams()->at(0));
1527 EXPECT_EQ(0u, pc_->local_streams()->count());
1528}
1529
deadbeefe1f9d832016-01-14 15:35:42 -08001530// Test for AddTrack and RemoveTrack methods.
1531// Tests that the created offer includes tracks we added,
1532// and that the RtpSenders are created correctly.
1533// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001534// Only tested with Plan B since Unified Plan is covered in more detail by tests
1535// in peerconnection_jsep_unittests.cc
1536TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001537 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001538 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001539 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001540 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001541 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001542 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1543 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001544 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001545 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001546 EXPECT_EQ("audio_track", audio_sender->id());
1547 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001548 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001549 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001550 EXPECT_EQ("video_track", video_sender->id());
1551 EXPECT_EQ(video_track, video_sender->track());
1552
1553 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001554 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001555 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001556
1557 const cricket::ContentInfo* audio_content =
1558 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001559 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001560 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001561
1562 const cricket::ContentInfo* video_content =
1563 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001564 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001565 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001566
Steve Antondb45ca82017-09-11 18:27:34 -07001567 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 // Now try removing the tracks.
1570 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1571 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1572
1573 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001574 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001575
1576 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001577 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001578 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001579
1580 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001581 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001582 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001583
Steve Antondb45ca82017-09-11 18:27:34 -07001584 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001585
1586 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1587 // should return false.
1588 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1589 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1590}
1591
1592// Test creating senders without a stream specified,
1593// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001594TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001595 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001596 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001597 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001598 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001599 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001600 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001601 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001602 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001603 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001604 EXPECT_EQ("audio_track", audio_sender->id());
1605 EXPECT_EQ(audio_track, audio_sender->track());
1606 EXPECT_EQ("video_track", video_sender->id());
1607 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001608 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1609 // If the ID is truly a random GUID, it should be infinitely unlikely they
1610 // will be the same.
1611 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1612 } else {
1613 // We allows creating tracks without stream ids under Unified Plan
1614 // semantics.
1615 EXPECT_EQ(0u, video_sender->stream_ids().size());
1616 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1617 }
deadbeefe1f9d832016-01-14 15:35:42 -08001618}
1619
Harald Alvestrand89061872018-01-02 14:08:34 +01001620// Test that we can call GetStats() after AddTrack but before connecting
1621// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001622TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001623 CreatePeerConnectionWithoutDtls();
1624 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001625 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001626 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001627 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001628 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1629 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001630 EXPECT_TRUE(DoGetStats(nullptr));
1631}
1632
Steve Anton36da6ff2018-02-16 16:04:20 -08001633TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001634 CreatePeerConnectionWithoutDtls();
1635 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001636 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001637 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001638 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001639 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001640 ASSERT_TRUE(audio_sender.ok());
1641 auto* audio_sender_proxy =
1642 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1643 audio_sender.value().get());
1644 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1645
Harald Alvestrandc72af932018-01-11 17:18:19 +01001646 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001647 ASSERT_TRUE(video_sender.ok());
1648 auto* video_sender_proxy =
1649 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1650 video_sender.value().get());
1651 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001652}
1653
Steve Anton36da6ff2018-02-16 16:04:20 -08001654// Don't run under Unified Plan since the stream API is not available.
1655TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001656 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001657 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001658 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001659 ASSERT_EQ(1u, senders.size());
1660 auto* sender_proxy =
1661 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1662 senders[0].get());
1663 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001664}
1665
Steve Anton36da6ff2018-02-16 16:04:20 -08001666TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001668 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 VerifyRemoteRtpHeaderExtensions();
1670}
1671
Steve Anton36da6ff2018-02-16 16:04:20 -08001672TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001673 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001674 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 CreateOfferAsLocalDescription();
1676 std::string offer;
1677 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1678 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001679 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680}
1681
Steve Anton36da6ff2018-02-16 16:04:20 -08001682TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001683 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001684 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685
1686 CreateOfferAsRemoteDescription();
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 -08001692TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001693 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001694 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695
1696 CreateOfferAsRemoteDescription();
1697 CreatePrAnswerAsLocalDescription();
1698 CreateAnswerAsLocalDescription();
1699
Seth Hampson845e8782018-03-02 11:34:10 -08001700 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701}
1702
Steve Anton36da6ff2018-02-16 16:04:20 -08001703// Don't run under Unified Plan since the stream API is not available.
1704TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 InitiateCall();
1706 ASSERT_EQ(1u, pc_->remote_streams()->count());
1707 pc_->RemoveStream(pc_->local_streams()->at(0));
1708 CreateOfferReceiveAnswer();
1709 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001710 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 CreateOfferReceiveAnswer();
1712}
1713
1714// Tests that after negotiating an audio only call, the respondent can perform a
1715// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001716TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001718 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 CreateOfferAsRemoteDescription();
1720 CreateAnswerAsLocalDescription();
1721
1722 ASSERT_EQ(1u, pc_->remote_streams()->count());
1723 pc_->RemoveStream(pc_->local_streams()->at(0));
1724 CreateOfferReceiveAnswer();
1725 EXPECT_EQ(0u, pc_->remote_streams()->count());
1726}
1727
1728// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001729TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001730 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731
Steve Antonf1c6db12017-10-13 11:13:35 -07001732 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001734 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001735 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001736 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001737 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738
1739 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001740 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001741 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001742 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743
Steve Antonf1c6db12017-10-13 11:13:35 -07001744 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001745 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746
Steve Antonf1c6db12017-10-13 11:13:35 -07001747 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748}
1749
deadbeefab9b2d12015-10-14 11:33:11 -07001750// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001752TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001753 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001755 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001756 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001757 EXPECT_TRUE(offer);
1758 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759
1760 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001761 AddAudioTrack("track_label", {kStreamId1});
1762 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763
1764 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001765 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766
1767 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001768 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001769 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770}
1771
1772// Test that we will get different SSRCs for each tracks in the offer and answer
1773// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001774TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001775 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001777 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1778 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779
1780 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001782 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 int audio_ssrc = 0;
1784 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001785 EXPECT_TRUE(
1786 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1787 EXPECT_TRUE(
1788 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 EXPECT_NE(audio_ssrc, video_ssrc);
1790
1791 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001792 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001793 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001794 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 audio_ssrc = 0;
1796 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001797 EXPECT_TRUE(
1798 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1799 EXPECT_TRUE(
1800 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 EXPECT_NE(audio_ssrc, video_ssrc);
1802}
1803
deadbeefeb459812015-12-15 19:24:43 -08001804// Test that it's possible to call AddTrack on a MediaStream after adding
1805// the stream to a PeerConnection.
1806// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001807// Don't run under Unified Plan since the stream API is not available.
1808TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001809 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001810 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001811 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001812 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1813
1814 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001815 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001816 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001817 stream->AddTrack(video_track.get());
1818
kwibergd1fe2812016-04-27 06:47:29 -07001819 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001820 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001821
1822 const cricket::MediaContentDescription* video_desc =
1823 cricket::GetFirstVideoContentDescription(offer->description());
1824 EXPECT_TRUE(video_desc != nullptr);
1825}
1826
1827// Test that it's possible to call RemoveTrack on a MediaStream after adding
1828// the stream to a PeerConnection.
1829// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001830// Don't run under Unified Plan since the stream API is not available.
1831TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001832 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001833 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001834 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001835 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1836
1837 // Remove the video track.
1838 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1839
kwibergd1fe2812016-04-27 06:47:29 -07001840 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001842
1843 const cricket::MediaContentDescription* video_desc =
1844 cricket::GetFirstVideoContentDescription(offer->description());
1845 EXPECT_TRUE(video_desc == nullptr);
1846}
1847
deadbeefbd7d8f72015-12-18 16:58:44 -08001848// Test creating a sender with a stream ID, and ensure the ID is populated
1849// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001850// Don't run under Unified Plan since the stream API is not available.
1851TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001852 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001853 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001854
kwibergd1fe2812016-04-27 06:47:29 -07001855 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001856 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001857
1858 const cricket::MediaContentDescription* video_desc =
1859 cricket::GetFirstVideoContentDescription(offer->description());
1860 ASSERT_TRUE(video_desc != nullptr);
1861 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001862 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001863}
1864
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001866TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001868 ASSERT_LT(0u, pc_->GetSenders().size());
1869 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001870 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001871 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 EXPECT_TRUE(DoGetStats(remote_audio));
1873
1874 // Remove the stream. Since we are sending to our selves the local
1875 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001876 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 // Do a re-negotiation.
1878 CreateOfferReceiveAnswer();
1879
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 // Test that we still can get statistics for the old track. Even if it is not
1881 // sent any longer.
1882 EXPECT_TRUE(DoGetStats(remote_audio));
1883}
1884
1885// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001886TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001888 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1889 ASSERT_TRUE(video_receiver);
1890 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891}
1892
1893// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001894TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001896 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 pc_factory_->CreateAudioTrack("unknown track", NULL));
1898 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1899}
1900
Steve Anton36da6ff2018-02-16 16:04:20 -08001901TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001902 CreatePeerConnectionWithoutDtls();
1903 EXPECT_TRUE(DoGetRTCStats());
1904 // Clearing stats cache is needed now, but should be temporary.
1905 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1906 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001907 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1908 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001909 EXPECT_TRUE(DoGetRTCStats());
1910 pc_->ClearStatsCache();
1911 CreateOfferReceiveAnswer();
1912 EXPECT_TRUE(DoGetRTCStats());
1913}
1914
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001916TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001917 RTCConfiguration config;
1918 config.enable_rtp_data_channel = true;
1919 config.enable_dtls_srtp = false;
1920 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001921 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 pc_->CreateDataChannel("test2", NULL);
1925 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001926 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001928 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 new MockDataChannelObserver(data2));
1930
1931 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1932 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1933 std::string data_to_send1 = "testing testing";
1934 std::string data_to_send2 = "testing something else";
1935 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1936
1937 CreateOfferReceiveAnswer();
1938 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1939 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1940
1941 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1942 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1943 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1944 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1945
1946 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1947 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1948
1949 data1->Close();
1950 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1951 CreateOfferReceiveAnswer();
1952 EXPECT_FALSE(observer1->IsOpen());
1953 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1954 EXPECT_TRUE(observer2->IsOpen());
1955
1956 data_to_send2 = "testing something else again";
1957 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1958
1959 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1960}
1961
1962// This test verifies that sendnig binary data over RTP data channels should
1963// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001964TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001965 RTCConfiguration config;
1966 config.enable_rtp_data_channel = true;
1967 config.enable_dtls_srtp = false;
1968 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001969 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001971 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 pc_->CreateDataChannel("test2", NULL);
1973 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001974 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001976 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977 new MockDataChannelObserver(data2));
1978
1979 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1980 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1981
1982 CreateOfferReceiveAnswer();
1983 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1984 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1985
1986 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1987 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1988
jbaucheec21bd2016-03-20 06:15:43 -07001989 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1991}
1992
1993// This test setup a RTP data channels in loop back and test that a channel is
1994// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001995TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001996 RTCConfiguration config;
1997 config.enable_rtp_data_channel = true;
1998 config.enable_dtls_srtp = false;
1999 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07002000 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002002 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 new MockDataChannelObserver(data1));
2004
2005 CreateOfferReceiveAnswerWithoutSsrc();
2006
2007 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2008
2009 data1->Close();
2010 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2011 CreateOfferReceiveAnswerWithoutSsrc();
2012 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2013 EXPECT_FALSE(observer1->IsOpen());
2014}
2015
2016// This test that if a data channel is added in an answer a receive only channel
2017// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08002018TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002019 RTCConfiguration config;
2020 config.enable_rtp_data_channel = true;
2021 config.enable_dtls_srtp = false;
2022
2023 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024
2025 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002026 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 pc_->CreateDataChannel(offer_label, NULL);
2028
2029 CreateOfferAsLocalDescription();
2030
2031 // Replace the data channel label in the offer and apply it as an answer.
2032 std::string receive_label = "answer_channel";
2033 std::string sdp;
2034 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002035 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 CreateAnswerAsRemoteDescription(sdp);
2037
2038 // Verify that a new incoming data channel has been created and that
2039 // it is open but can't we written to.
2040 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2041 DataChannelInterface* received_channel = observer_.last_datachannel_;
2042 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2043 EXPECT_EQ(receive_label, received_channel->label());
2044 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2045
2046 // Verify that the channel we initially offered has been rejected.
2047 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2048
2049 // Do another offer / answer exchange and verify that the data channel is
2050 // opened.
2051 CreateOfferReceiveAnswer();
2052 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2053 kTimeout);
2054}
2055
2056// This test that no data channel is returned if a reliable channel is
2057// requested.
2058// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002059TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002060 RTCConfiguration rtc_config;
2061 rtc_config.enable_rtp_data_channel = true;
2062 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063
2064 std::string label = "test";
2065 webrtc::DataChannelInit config;
2066 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002067 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 pc_->CreateDataChannel(label, &config);
2069 EXPECT_TRUE(channel == NULL);
2070}
2071
deadbeefab9b2d12015-10-14 11:33:11 -07002072// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002073TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002074 RTCConfiguration config;
2075 config.enable_rtp_data_channel = true;
2076 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002077
2078 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002079 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002080 pc_->CreateDataChannel(label, nullptr);
2081 EXPECT_NE(channel, nullptr);
2082
zhihuang9763d562016-08-05 11:14:50 -07002083 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002084 pc_->CreateDataChannel(label, nullptr);
2085 EXPECT_EQ(dup_channel, nullptr);
2086}
2087
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088// This tests that a SCTP data channel is returned using different
2089// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002090TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002091 RTCConfiguration rtc_config;
2092 rtc_config.enable_dtls_srtp = true;
2093 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094
2095 webrtc::DataChannelInit config;
2096
zhihuang9763d562016-08-05 11:14:50 -07002097 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 pc_->CreateDataChannel("1", &config);
2099 EXPECT_TRUE(channel != NULL);
2100 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002101 EXPECT_TRUE(observer_.renegotiation_needed_);
2102 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103
2104 config.ordered = false;
2105 channel = pc_->CreateDataChannel("2", &config);
2106 EXPECT_TRUE(channel != NULL);
2107 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002108 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109
2110 config.ordered = true;
2111 config.maxRetransmits = 0;
2112 channel = pc_->CreateDataChannel("3", &config);
2113 EXPECT_TRUE(channel != NULL);
2114 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002115 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116
2117 config.maxRetransmits = -1;
2118 config.maxRetransmitTime = 0;
2119 channel = pc_->CreateDataChannel("4", &config);
2120 EXPECT_TRUE(channel != NULL);
2121 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002122 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123}
2124
2125// This tests that no data channel is returned if both maxRetransmits and
2126// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002127TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002129 RTCConfiguration rtc_config;
2130 rtc_config.enable_dtls_srtp = true;
2131 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132
2133 std::string label = "test";
2134 webrtc::DataChannelInit config;
2135 config.maxRetransmits = 0;
2136 config.maxRetransmitTime = 0;
2137
zhihuang9763d562016-08-05 11:14:50 -07002138 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 pc_->CreateDataChannel(label, &config);
2140 EXPECT_TRUE(channel == NULL);
2141}
2142
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143// The test verifies that creating a SCTP data channel with an id already in use
2144// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002145TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002147 RTCConfiguration rtc_config;
2148 rtc_config.enable_dtls_srtp = true;
2149 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150
2151 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002152 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002154 config.id = 1;
2155 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 EXPECT_TRUE(channel != NULL);
2157 EXPECT_EQ(1, channel->id());
2158
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 channel = pc_->CreateDataChannel("x", &config);
2160 EXPECT_TRUE(channel == NULL);
2161
2162 config.id = cricket::kMaxSctpSid;
2163 channel = pc_->CreateDataChannel("max", &config);
2164 EXPECT_TRUE(channel != NULL);
2165 EXPECT_EQ(config.id, channel->id());
2166
2167 config.id = cricket::kMaxSctpSid + 1;
2168 channel = pc_->CreateDataChannel("x", &config);
2169 EXPECT_TRUE(channel == NULL);
2170}
2171
deadbeefab9b2d12015-10-14 11:33:11 -07002172// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002173TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002174 RTCConfiguration rtc_config;
2175 rtc_config.enable_dtls_srtp = true;
2176 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002177
2178 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002179 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002180 pc_->CreateDataChannel(label, nullptr);
2181 EXPECT_NE(channel, nullptr);
2182
zhihuang9763d562016-08-05 11:14:50 -07002183 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002184 pc_->CreateDataChannel(label, nullptr);
2185 EXPECT_NE(dup_channel, nullptr);
2186}
2187
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002188// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2189// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002190TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002191 RTCConfiguration rtc_config;
2192 rtc_config.enable_rtp_data_channel = true;
2193 rtc_config.enable_dtls_srtp = false;
2194 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002195
zhihuang9763d562016-08-05 11:14:50 -07002196 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002197 pc_->CreateDataChannel("test1", NULL);
2198 EXPECT_TRUE(observer_.renegotiation_needed_);
2199 observer_.renegotiation_needed_ = false;
2200
zhihuang9763d562016-08-05 11:14:50 -07002201 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002202 pc_->CreateDataChannel("test2", NULL);
2203 EXPECT_TRUE(observer_.renegotiation_needed_);
2204}
2205
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002207TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002208 RTCConfiguration rtc_config;
2209 rtc_config.enable_rtp_data_channel = true;
2210 rtc_config.enable_dtls_srtp = false;
2211 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212
zhihuang9763d562016-08-05 11:14:50 -07002213 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002215 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 pc_->CreateDataChannel("test2", NULL);
2217 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002218 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002220 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 new MockDataChannelObserver(data2));
2222
2223 CreateOfferReceiveAnswer();
2224 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2225 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2226
2227 ReleasePeerConnection();
2228 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2229 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2230}
2231
Zhi Huang644fde42018-04-02 19:16:26 -07002232// This tests that RTP data channels can be rejected in an answer.
2233TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002234 RTCConfiguration rtc_config;
2235 rtc_config.enable_rtp_data_channel = true;
2236 rtc_config.enable_dtls_srtp = false;
2237 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238
zhihuang9763d562016-08-05 11:14:50 -07002239 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 pc_->CreateDataChannel("offer_channel", NULL));
2241
2242 CreateOfferAsLocalDescription();
2243
2244 // Create an answer where the m-line for data channels are rejected.
2245 std::string sdp;
2246 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002247 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002248 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002249 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002251 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 data_info->rejected = true;
2253
Steve Antondb45ca82017-09-11 18:27:34 -07002254 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2256}
2257
Zhi Huang644fde42018-04-02 19:16:26 -07002258#ifdef HAVE_SCTP
2259// This tests that SCTP data channels can be rejected in an answer.
2260TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2261#else
2262TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2263#endif
2264{
Niels Möllerf06f9232018-08-07 12:32:18 +02002265 RTCConfiguration rtc_config;
2266 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002267
2268 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2269 pc_->CreateDataChannel("offer_channel", NULL));
2270
2271 CreateOfferAsLocalDescription();
2272
2273 // Create an answer where the m-line for data channels are rejected.
2274 std::string sdp;
2275 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2276 std::unique_ptr<SessionDescriptionInterface> answer(
2277 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2278 ASSERT_TRUE(answer);
2279 cricket::ContentInfo* data_info =
2280 cricket::GetFirstDataContent(answer->description());
2281 data_info->rejected = true;
2282
2283 DoSetRemoteDescription(std::move(answer));
2284 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2285}
2286
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287// Test that we can create a session description from an SDP string from
2288// FireFox, use it as a remote session description, generate an answer and use
2289// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002290TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002291 RTCConfiguration rtc_config;
2292 rtc_config.enable_dtls_srtp = true;
2293 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002294 AddAudioTrack("audio_label");
2295 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002296 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002297 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002298 webrtc::kFireFoxSdpOffer, nullptr));
2299 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 CreateAnswerAsLocalDescription();
2301 ASSERT_TRUE(pc_->local_description() != NULL);
2302 ASSERT_TRUE(pc_->remote_description() != NULL);
2303
2304 const cricket::ContentInfo* content =
2305 cricket::GetFirstAudioContent(pc_->local_description()->description());
2306 ASSERT_TRUE(content != NULL);
2307 EXPECT_FALSE(content->rejected);
2308
2309 content =
2310 cricket::GetFirstVideoContent(pc_->local_description()->description());
2311 ASSERT_TRUE(content != NULL);
2312 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002313#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 content =
2315 cricket::GetFirstDataContent(pc_->local_description()->description());
2316 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002317 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002318#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319}
2320
zhihuangb19012e2017-09-19 13:47:59 -07002321// Test that fallback from DTLS to SDES is not supported.
2322// The fallback was previously supported but was removed to simplify the code
2323// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002324TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002325 RTCConfiguration rtc_config;
2326 rtc_config.enable_dtls_srtp = true;
2327 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002328 // Wait for fake certificate to be generated. Previously, this is what caused
2329 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002330 AddAudioTrack("audio_label");
2331 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002332 ASSERT_NE(nullptr, fake_certificate_generator_);
2333 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2334 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002335 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002336 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2337 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002338 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002339}
2340
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341// Test that we can create an audio only offer and receive an answer with a
2342// limited set of audio codecs and receive an updated offer with more audio
2343// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002344TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002345 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002346 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 CreateOfferAsLocalDescription();
2348
Steve Anton36da6ff2018-02-16 16:04:20 -08002349 const char* answer_sdp =
2350 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2351 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002352 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002353 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002354 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355
Steve Anton36da6ff2018-02-16 16:04:20 -08002356 const char* reoffer_sdp =
2357 (sdp_semantics_ == SdpSemantics::kPlanB
2358 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2359 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002360 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002361 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002362 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 CreateAnswerAsLocalDescription();
2364}
2365
deadbeefc80741f2015-10-22 13:14:45 -07002366// Test that if we're receiving (but not sending) a track, subsequent offers
2367// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002368TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002369 RTCConfiguration rtc_config;
2370 rtc_config.enable_dtls_srtp = true;
2371 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002372 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002373 CreateAnswerAsLocalDescription();
2374
2375 // At this point we should be receiving stream 1, but not sending anything.
2376 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002377 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002378 DoCreateOffer(&offer, nullptr);
2379
2380 const cricket::ContentInfo* video_content =
2381 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002382 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2383 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002384
2385 const cricket::ContentInfo* audio_content =
2386 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002387 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2388 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002389}
2390
2391// Test that if we're receiving (but not sending) a track, and the
2392// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2393// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002394TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002395 RTCConfiguration rtc_config;
2396 rtc_config.enable_dtls_srtp = true;
2397 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002398 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002399 CreateAnswerAsLocalDescription();
2400
2401 // At this point we should be receiving stream 1, but not sending anything.
2402 // A new offer would be recvonly, but we'll set the "no receive" constraints
2403 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002404 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002405 RTCOfferAnswerOptions options;
2406 options.offer_to_receive_audio = 0;
2407 options.offer_to_receive_video = 0;
2408 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002409
2410 const cricket::ContentInfo* video_content =
2411 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002412 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2413 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002414
2415 const cricket::ContentInfo* audio_content =
2416 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002417 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2418 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002419}
2420
deadbeef653b8e02015-11-11 12:55:10 -08002421// Test that we can use SetConfiguration to change the ICE servers of the
2422// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002423TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002424 CreatePeerConnection();
2425
Steve Anton36da6ff2018-02-16 16:04:20 -08002426 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002427 PeerConnectionInterface::IceServer server;
2428 server.uri = "stun:test_hostname";
2429 config.servers.push_back(server);
2430 EXPECT_TRUE(pc_->SetConfiguration(config));
2431
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002432 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2433 EXPECT_EQ("test_hostname",
2434 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002435}
2436
Steve Anton36da6ff2018-02-16 16:04:20 -08002437TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002438 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002439 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002440 config.type = PeerConnectionInterface::kRelay;
2441 EXPECT_TRUE(pc_->SetConfiguration(config));
2442 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2443}
2444
Steve Anton36da6ff2018-02-16 16:04:20 -08002445TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002446 PeerConnectionInterface::RTCConfiguration config;
2447 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002448 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002449 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002450 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2451
2452 config.prune_turn_ports = true;
2453 EXPECT_TRUE(pc_->SetConfiguration(config));
2454 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2455}
2456
skvladd1f5fda2017-02-03 16:54:05 -08002457// Test that the ice check interval can be changed. This does not verify that
2458// the setting makes it all the way to P2PTransportChannel, as that would
2459// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002460TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002461 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002462 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002463 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002464 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002465 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002466 EXPECT_TRUE(pc_->SetConfiguration(config));
2467 PeerConnectionInterface::RTCConfiguration new_config =
2468 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002469 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002470}
2471
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002472// Test that when SetConfiguration changes both the pool size and other
2473// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002474TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002475 SetConfigurationCreatesPooledSessionCorrectly) {
2476 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002477 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002478 config.ice_candidate_pool_size = 1;
2479 PeerConnectionInterface::IceServer server;
2480 server.uri = kStunAddressOnly;
2481 config.servers.push_back(server);
2482 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002483 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002484
2485 const cricket::FakePortAllocatorSession* session =
2486 static_cast<const cricket::FakePortAllocatorSession*>(
2487 port_allocator_->GetPooledSession());
2488 ASSERT_NE(nullptr, session);
2489 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002490}
2491
deadbeef293e9262017-01-11 12:28:30 -08002492// Test that after SetLocalDescription, changing the pool size is not allowed,
2493// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002494TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002495 CantChangePoolSizeAfterSetLocalDescription) {
2496 CreatePeerConnection();
2497 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002498 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002499 config.ice_candidate_pool_size = 1;
2500 EXPECT_TRUE(pc_->SetConfiguration(config));
2501
2502 // Set remote offer; can still change pool size at this point.
2503 CreateOfferAsRemoteDescription();
2504 config.ice_candidate_pool_size = 2;
2505 EXPECT_TRUE(pc_->SetConfiguration(config));
2506
2507 // Set local answer; now it's too late.
2508 CreateAnswerAsLocalDescription();
2509 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002510 RTCError error;
2511 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2512 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2513}
2514
deadbeef42a42632017-03-10 15:18:00 -08002515// Test that after setting an answer, extra pooled sessions are discarded. The
2516// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002517TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002518 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2519 CreatePeerConnection();
2520
2521 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002522 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002523 config.ice_candidate_pool_size = 4;
2524 EXPECT_TRUE(pc_->SetConfiguration(config));
2525
2526 // Do offer/answer.
2527 CreateOfferAsRemoteDescription();
2528 CreateAnswerAsLocalDescription();
2529
2530 // Expect no pooled sessions to be left.
2531 const cricket::PortAllocatorSession* session =
2532 port_allocator_->GetPooledSession();
2533 EXPECT_EQ(nullptr, session);
2534}
2535
2536// After Close is called, pooled candidates should be discarded so as to not
2537// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002538TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002539 CreatePeerConnection();
2540
Steve Anton36da6ff2018-02-16 16:04:20 -08002541 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002542 config.ice_candidate_pool_size = 3;
2543 EXPECT_TRUE(pc_->SetConfiguration(config));
2544 pc_->Close();
2545
2546 // Expect no pooled sessions to be left.
2547 const cricket::PortAllocatorSession* session =
2548 port_allocator_->GetPooledSession();
2549 EXPECT_EQ(nullptr, session);
2550}
2551
deadbeef293e9262017-01-11 12:28:30 -08002552// Test that SetConfiguration returns an invalid modification error if
2553// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002554TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002555 SetConfigurationReturnsInvalidModificationError) {
2556 PeerConnectionInterface::RTCConfiguration config;
2557 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2558 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2559 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002560 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002561
Steve Anton36da6ff2018-02-16 16:04:20 -08002562 PeerConnectionInterface::RTCConfiguration modified_config =
2563 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002564 modified_config.bundle_policy =
2565 PeerConnectionInterface::kBundlePolicyMaxBundle;
2566 RTCError error;
2567 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2568 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2569
Steve Anton36da6ff2018-02-16 16:04:20 -08002570 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002571 modified_config.rtcp_mux_policy =
2572 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2573 error.set_type(RTCErrorType::NONE);
2574 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2575 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2576
Steve Anton36da6ff2018-02-16 16:04:20 -08002577 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002578 modified_config.continual_gathering_policy =
2579 PeerConnectionInterface::GATHER_CONTINUALLY;
2580 error.set_type(RTCErrorType::NONE);
2581 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2582 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2583}
2584
2585// Test that SetConfiguration returns a range error if the candidate pool size
2586// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002587TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002588 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2589 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002590 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002591 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002592
2593 config.ice_candidate_pool_size = -1;
2594 RTCError error;
2595 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2596 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2597
2598 config.ice_candidate_pool_size = INT_MAX;
2599 error.set_type(RTCErrorType::NONE);
2600 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2601 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2602}
2603
2604// Test that SetConfiguration returns a syntax error if parsing an ICE server
2605// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002606TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002607 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2608 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002609 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002610 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002611
2612 PeerConnectionInterface::IceServer bad_server;
2613 bad_server.uri = "stunn:www.example.com";
2614 config.servers.push_back(bad_server);
2615 RTCError error;
2616 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2617 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2618}
2619
2620// Test that SetConfiguration returns an invalid parameter error if a TURN
2621// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002622TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002623 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2624 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002625 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002626 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002627
2628 PeerConnectionInterface::IceServer bad_server;
2629 bad_server.uri = "turn:www.example.com";
2630 // Missing password.
2631 bad_server.username = "foo";
2632 config.servers.push_back(bad_server);
2633 RTCError error;
2634 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2635 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002636}
2637
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002638// Test that PeerConnection::Close changes the states to closed and all remote
2639// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002640TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002641 // Initialize a PeerConnection and negotiate local and remote session
2642 // description.
2643 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002644
2645 // With Plan B, verify the stream count. The analog with Unified Plan is the
2646 // RtpTransceiver count.
2647 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2648 ASSERT_EQ(1u, pc_->local_streams()->count());
2649 ASSERT_EQ(1u, pc_->remote_streams()->count());
2650 } else {
2651 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2652 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653
2654 pc_->Close();
2655
2656 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2657 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2658 pc_->ice_connection_state());
2659 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2660 pc_->ice_gathering_state());
2661
Steve Anton36da6ff2018-02-16 16:04:20 -08002662 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2663 EXPECT_EQ(1u, pc_->local_streams()->count());
2664 EXPECT_EQ(1u, pc_->remote_streams()->count());
2665 } else {
2666 // Verify that the RtpTransceivers are still present but all stopped.
2667 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Mirko Bonadei739baf02019-01-27 17:29:42 +01002668 for (const auto& transceiver : pc_->GetTransceivers()) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002669 EXPECT_TRUE(transceiver->stopped());
2670 }
2671 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002672
Steve Anton36da6ff2018-02-16 16:04:20 -08002673 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2674 ASSERT_TRUE(audio_receiver);
2675 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2676 ASSERT_TRUE(video_receiver);
2677
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002678 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002679 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002680 audio_receiver->track()->state(), kTimeout);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002681 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002682 video_receiver->track()->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002683}
2684
2685// Test that PeerConnection methods fails gracefully after
2686// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002687// Don't run under Unified Plan since the stream API is not available.
2688TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002689 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002690 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002691 CreateOfferAsRemoteDescription();
2692 CreateAnswerAsLocalDescription();
2693
2694 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002695 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002696 pc_->local_streams()->at(0);
2697
2698 pc_->Close();
2699
2700 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002701 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002702
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002703 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2704
2705 EXPECT_TRUE(pc_->local_description() != NULL);
2706 EXPECT_TRUE(pc_->remote_description() != NULL);
2707
kwibergd1fe2812016-04-27 06:47:29 -07002708 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002709 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002710 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002711 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002712
2713 std::string sdp;
2714 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002715 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002716 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002717 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002718
2719 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002720 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002721 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002722 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002723}
2724
2725// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002726TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002727 InitiateCall();
2728 pc_->Close();
2729 DoGetStats(NULL);
2730}
deadbeefab9b2d12015-10-14 11:33:11 -07002731
2732// NOTE: The series of tests below come from what used to be
2733// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2734// setting a remote or local description has the expected effects.
2735
2736// This test verifies that the remote MediaStreams corresponding to a received
2737// SDP string is created. In this test the two separate MediaStreams are
2738// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002739TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002740 RTCConfiguration config;
2741 config.enable_dtls_srtp = true;
2742 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002743 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002744
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002745 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002746 EXPECT_TRUE(
2747 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2748 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2749 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2750
2751 // Create a session description based on another SDP with another
2752 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002753 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002754
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002755 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002756 EXPECT_TRUE(
2757 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2758}
2759
2760// This test verifies that when remote tracks are added/removed from SDP, the
2761// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002762// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2763// specific behavior.
2764TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002765 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002766 RTCConfiguration config;
2767 config.enable_dtls_srtp = true;
2768 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002769 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002770 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002771 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002772 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2773 reference_collection_));
2774
2775 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002776 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002777 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002778 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002779 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2780 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002781 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002782 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2783 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002784 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002785 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2786 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002787
2788 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002789 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002790 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002791 MockTrackObserver audio_track_observer(audio_track2);
2792 MockTrackObserver video_track_observer(video_track2);
2793
2794 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2795 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002796 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002797 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2798 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002799 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002800 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002801 audio_track2->state(), kTimeout);
2802 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2803 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002804}
2805
2806// This tests that remote tracks are ended if a local session description is set
2807// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002808TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002809 RTCConfiguration config;
2810 config.enable_dtls_srtp = true;
2811 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002812 // First create and set a remote offer, then reject its video content in our
2813 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002814 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2815 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2816 ASSERT_TRUE(audio_receiver);
2817 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2818 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002819
Steve Anton36da6ff2018-02-16 16:04:20 -08002820 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2821 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002822 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002823 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2824 video_receiver->track();
2825 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002826
kwibergd1fe2812016-04-27 06:47:29 -07002827 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002828 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002829 cricket::ContentInfo* video_info =
2830 local_answer->description()->GetContentByName("video");
2831 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002832 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002833 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2834 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002835
2836 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002837 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002838 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002839 video_info = local_offer->description()->GetContentByName("video");
2840 ASSERT_TRUE(video_info != nullptr);
2841 video_info->rejected = true;
2842 cricket::ContentInfo* audio_info =
2843 local_offer->description()->GetContentByName("audio");
2844 ASSERT_TRUE(audio_info != nullptr);
2845 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002846 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002847 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002848 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2849 kTimeout);
2850 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2851 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002852}
2853
2854// This tests that we won't crash if the remote track has been removed outside
2855// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002856// Don't run under Unified Plan since the stream API is not available.
2857TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002858 RTCConfiguration config;
2859 config.enable_dtls_srtp = true;
2860 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002861 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002862 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2863 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2864 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2865
kwibergd1fe2812016-04-27 06:47:29 -07002866 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002867 webrtc::CreateSessionDescription(SdpType::kAnswer,
2868 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002869 cricket::ContentInfo* video_info =
2870 local_answer->description()->GetContentByName("video");
2871 video_info->rejected = true;
2872 cricket::ContentInfo* audio_info =
2873 local_answer->description()->GetContentByName("audio");
2874 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002875 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002876
2877 // No crash is a pass.
2878}
2879
deadbeef5e97fb52015-10-15 12:49:08 -07002880// This tests that if a recvonly remote description is set, no remote streams
2881// will be created, even if the description contains SSRCs/MSIDs.
2882// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002883TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002884 RTCConfiguration config;
2885 config.enable_dtls_srtp = true;
2886 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002887
Steve Anton36da6ff2018-02-16 16:04:20 -08002888 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002889 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002890 CreateAndSetRemoteOffer(recvonly_offer);
2891
2892 EXPECT_EQ(0u, observer_.remote_streams()->count());
2893}
2894
deadbeefab9b2d12015-10-14 11:33:11 -07002895// This tests that a default MediaStream is created if a remote session
2896// description doesn't contain any streams and no MSID support.
2897// It also tests that the default stream is updated if a video m-line is added
2898// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002899// Don't run under Unified Plan since this behavior is Plan B specific.
2900TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002901 RTCConfiguration config;
2902 config.enable_dtls_srtp = true;
2903 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002904 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2905
2906 ASSERT_EQ(1u, observer_.remote_streams()->count());
2907 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2908
2909 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2910 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002911 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002912
2913 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2914 ASSERT_EQ(1u, observer_.remote_streams()->count());
2915 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2916 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002917 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2918 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002919 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2920 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002921 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2922 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002923}
2924
2925// This tests that a default MediaStream is created if a remote session
2926// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002927// Don't run under Unified Plan since this behavior is Plan B specific.
2928TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002929 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002930 RTCConfiguration config;
2931 config.enable_dtls_srtp = true;
2932 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002933 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2934
2935 ASSERT_EQ(1u, observer_.remote_streams()->count());
2936 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2937
2938 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2939 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002940 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002941}
2942
2943// This tests that it won't crash when PeerConnection tries to remove
2944// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002945// Don't run under Unified Plan since this behavior is Plan B specific.
2946TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002947 RTCConfiguration config;
2948 config.enable_dtls_srtp = true;
2949 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002950 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002951 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2952 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2953 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2954
2955 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2956
2957 // No crash is a pass.
2958}
2959
2960// This tests that a default MediaStream is created if the remote session
2961// description doesn't contain any streams and don't contain an indication if
2962// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002963// Don't run under Unified Plan since this behavior is Plan B specific.
2964TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002965 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002966 RTCConfiguration config;
2967 config.enable_dtls_srtp = true;
2968 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002969 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2970
2971 ASSERT_EQ(1u, observer_.remote_streams()->count());
2972 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2973 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2974 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2975}
2976
2977// This tests that a default MediaStream is not created if the remote session
2978// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002979// Don't run under Unified Plan since this behavior is Plan B specific.
2980TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002981 RTCConfiguration config;
2982 config.enable_dtls_srtp = true;
2983 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002984 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2985 EXPECT_EQ(0u, observer_.remote_streams()->count());
2986}
2987
deadbeefbda7e0b2015-12-08 17:13:40 -08002988// This tests that when setting a new description, the old default tracks are
2989// not destroyed and recreated.
2990// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002991// Don't run under Unified Plan since this behavior is Plan B specific.
2992TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002993 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002994 RTCConfiguration config;
2995 config.enable_dtls_srtp = true;
2996 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002997 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2998
2999 ASSERT_EQ(1u, observer_.remote_streams()->count());
3000 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3001 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3002
3003 // Set the track to "disabled", then set a new description and ensure the
3004 // track is still disabled, which ensures it hasn't been recreated.
3005 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3006 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3007 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3008 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3009}
3010
deadbeefab9b2d12015-10-14 11:33:11 -07003011// This tests that a default MediaStream is not created if a remote session
3012// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003013// Don't run under Unified Plan since this behavior is Plan B specific.
3014TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003015 RTCConfiguration config;
3016 config.enable_dtls_srtp = true;
3017 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003018 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003019 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003020 EXPECT_TRUE(
3021 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3022
3023 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3024 EXPECT_EQ(0u, observer_.remote_streams()->count());
3025}
3026
Seth Hampson5897a6e2018-04-03 11:16:33 -07003027// This tests that a default MediaStream is created if a remote SDP comes from
3028// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3029TEST_F(PeerConnectionInterfaceTestPlanB,
3030 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003031 RTCConfiguration config;
3032 config.enable_dtls_srtp = true;
3033 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003034 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3035 // Add a=msid lines to simulate a Unified Plan endpoint that only
3036 // signals stream IDs with a=msid lines.
3037 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3038
3039 CreateAndSetRemoteOffer(sdp_string);
3040
3041 ASSERT_EQ(1u, observer_.remote_streams()->count());
3042 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3043 EXPECT_EQ("default", remote_stream->id());
3044 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3045}
3046
Seth Hampson5b4f0752018-04-02 16:31:36 -07003047// This tests that when a Plan B endpoint receives an SDP that signals no media
3048// stream IDs indicated by the special character "-" in the a=msid line, that
3049// a default stream ID will be used for the MediaStream ID. This can occur
3050// when a Unified Plan endpoint signals no media stream IDs, but signals both
3051// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3052TEST_F(PeerConnectionInterfaceTestPlanB,
3053 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003054 RTCConfiguration config;
3055 config.enable_dtls_srtp = true;
3056 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003057 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3058 // the sender's stream ID will be interpreted as no stream IDs.
3059 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3060 sdp_string.append("a=msid:- audiotrack0\n");
3061
3062 CreateAndSetRemoteOffer(sdp_string);
3063
3064 ASSERT_EQ(1u, observer_.remote_streams()->count());
3065 // Because SSRCs are signaled the track ID will be what was signaled in the
3066 // a=msid line.
3067 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3068 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3069 EXPECT_EQ("default", remote_stream->id());
3070 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003071
3072 // Previously a bug ocurred when setting the remote description a second time.
3073 // This is because we checked equality of the remote StreamParams stream ID
3074 // (empty), and the previously set stream ID for the remote sender
3075 // ("default"). This cause a track to be removed, then added, when really
3076 // nothing should occur because it is the same track.
3077 CreateAndSetRemoteOffer(sdp_string);
3078 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3079 EXPECT_EQ(1u, observer_.add_track_events_.size());
3080 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3081 remote_stream = observer_.remote_streams()->at(0);
3082 EXPECT_EQ("default", remote_stream->id());
3083 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003084}
3085
deadbeefab9b2d12015-10-14 11:33:11 -07003086// This tests that an RtpSender is created when the local description is set
3087// after adding a local stream.
3088// TODO(deadbeef): This test and the one below it need to be updated when
3089// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003090// Don't run under Unified Plan since this behavior is Plan B specific.
3091TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003092 RTCConfiguration config;
3093 config.enable_dtls_srtp = true;
3094 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003095
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003096 // Create an offer with 1 stream with 2 tracks of each type.
3097 rtc::scoped_refptr<StreamCollection> stream_collection =
3098 CreateStreamCollection(1, 2);
3099 pc_->AddStream(stream_collection->at(0));
3100 std::unique_ptr<SessionDescriptionInterface> offer;
3101 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003102 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003103
deadbeefab9b2d12015-10-14 11:33:11 -07003104 auto senders = pc_->GetSenders();
3105 EXPECT_EQ(4u, senders.size());
3106 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3107 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3108 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3109 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3110
3111 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003112 pc_->RemoveStream(stream_collection->at(0));
3113 stream_collection = CreateStreamCollection(1, 1);
3114 pc_->AddStream(stream_collection->at(0));
3115 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003116 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003117
deadbeefab9b2d12015-10-14 11:33:11 -07003118 senders = pc_->GetSenders();
3119 EXPECT_EQ(2u, senders.size());
3120 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3121 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3122 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3123 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3124}
3125
3126// This tests that an RtpSender is created when the local description is set
3127// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003128// Don't run under Unified Plan since this behavior is Plan B specific.
3129TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003130 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003131 RTCConfiguration config;
3132 config.enable_dtls_srtp = true;
3133 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003134
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003135 rtc::scoped_refptr<StreamCollection> stream_collection =
3136 CreateStreamCollection(1, 2);
3137 // Add a stream to create the offer, but remove it afterwards.
3138 pc_->AddStream(stream_collection->at(0));
3139 std::unique_ptr<SessionDescriptionInterface> offer;
3140 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3141 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003142
Steve Antondb45ca82017-09-11 18:27:34 -07003143 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003144 auto senders = pc_->GetSenders();
3145 EXPECT_EQ(0u, senders.size());
3146
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003147 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003148 senders = pc_->GetSenders();
3149 EXPECT_EQ(4u, senders.size());
3150 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3151 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3152 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3153 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3154}
3155
3156// This tests that the expected behavior occurs if the SSRC on a local track is
3157// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003158TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003159 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003160 RTCConfiguration config;
3161 config.enable_dtls_srtp = true;
3162 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003163
Steve Anton36da6ff2018-02-16 16:04:20 -08003164 AddAudioTrack(kAudioTracks[0]);
3165 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003166 std::unique_ptr<SessionDescriptionInterface> offer;
3167 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3168 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003169 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3170 webrtc::CreateSessionDescription(
3171 webrtc::SdpType::kOffer, offer->session_id(),
3172 offer->session_version(),
3173 absl::WrapUnique(offer->description()->Copy()));
Steve Antondb45ca82017-09-11 18:27:34 -07003174 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003175
deadbeefab9b2d12015-10-14 11:33:11 -07003176 auto senders = pc_->GetSenders();
3177 EXPECT_EQ(2u, senders.size());
3178 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3179 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3180
3181 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003182 cricket::MediaContentDescription* desc =
3183 cricket::GetFirstAudioContentDescription(modified_offer->description());
3184 ASSERT_TRUE(desc != NULL);
3185 for (StreamParams& stream : desc->mutable_streams()) {
3186 for (unsigned int& ssrc : stream.ssrcs) {
3187 ++ssrc;
3188 }
3189 }
deadbeefab9b2d12015-10-14 11:33:11 -07003190
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003191 desc =
3192 cricket::GetFirstVideoContentDescription(modified_offer->description());
3193 ASSERT_TRUE(desc != NULL);
3194 for (StreamParams& stream : desc->mutable_streams()) {
3195 for (unsigned int& ssrc : stream.ssrcs) {
3196 ++ssrc;
3197 }
3198 }
3199
Steve Antondb45ca82017-09-11 18:27:34 -07003200 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003201 senders = pc_->GetSenders();
3202 EXPECT_EQ(2u, senders.size());
3203 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3204 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3205 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3206 // changed.
3207}
3208
3209// This tests that the expected behavior occurs if a new session description is
3210// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003211// Don't run under Unified Plan since the stream API is not available.
3212TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003213 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003214 RTCConfiguration config;
3215 config.enable_dtls_srtp = true;
3216 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003217
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003218 rtc::scoped_refptr<StreamCollection> stream_collection =
3219 CreateStreamCollection(2, 1);
3220 pc_->AddStream(stream_collection->at(0));
3221 std::unique_ptr<SessionDescriptionInterface> offer;
3222 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003223 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003224
deadbeefab9b2d12015-10-14 11:33:11 -07003225 auto senders = pc_->GetSenders();
3226 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003227 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3228 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003229
3230 // Add a new MediaStream but with the same tracks as in the first stream.
3231 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3232 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003233 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3234 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003235 pc_->AddStream(stream_1);
3236
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003237 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003238 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003239
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003240 auto new_senders = pc_->GetSenders();
3241 // Should be the same senders as before, but with updated stream id.
3242 // Note that this behavior is subject to change in the future.
3243 // We may decide the PC should ignore existing tracks in AddStream.
3244 EXPECT_EQ(senders, new_senders);
3245 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3246 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003247}
3248
zhihuang81c3a032016-11-17 12:06:24 -08003249// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003250TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003251 RTCConfiguration config;
3252 config.enable_dtls_srtp = true;
3253 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003254 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3255 EXPECT_EQ(observer_.num_added_tracks_, 1);
3256 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3257
3258 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003259 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003260 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003261 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3262}
3263
deadbeefd1a38b52016-12-10 13:15:33 -08003264// Test that when SetConfiguration is called and the configuration is
3265// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003266TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003267 PeerConnectionInterface::RTCConfiguration config;
3268 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003269 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003270 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003271 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3272 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003273
3274 // Do initial offer/answer so there's something to restart.
3275 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003276 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003277
3278 // Grab the ufrags.
3279 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3280
3281 // Change ICE policy, which should trigger an ICE restart on the next offer.
3282 config.type = PeerConnectionInterface::kAll;
3283 EXPECT_TRUE(pc_->SetConfiguration(config));
3284 CreateOfferAsLocalDescription();
3285
3286 // Grab the new ufrags.
3287 std::vector<std::string> subsequent_ufrags =
3288 GetUfrags(pc_->local_description());
3289
3290 // Sanity check.
3291 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3292 // Check that each ufrag is different.
3293 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3294 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3295 }
3296}
3297
3298// Test that when SetConfiguration is called and the configuration *isn't*
3299// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003300TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003301 PeerConnectionInterface::RTCConfiguration config;
3302 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003303 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003304 config = pc_->GetConfiguration();
3305 AddAudioTrack(kAudioTracks[0]);
3306 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003307
3308 // Do initial offer/answer so there's something to restart.
3309 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003310 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003311
3312 // Grab the ufrags.
3313 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3314
3315 // Call SetConfiguration with a config identical to what the PC was
3316 // constructed with.
3317 EXPECT_TRUE(pc_->SetConfiguration(config));
3318 CreateOfferAsLocalDescription();
3319
3320 // Grab the new ufrags.
3321 std::vector<std::string> subsequent_ufrags =
3322 GetUfrags(pc_->local_description());
3323
3324 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3325}
3326
3327// Test for a weird corner case scenario:
3328// 1. Audio/video session established.
3329// 2. SetConfiguration changes ICE config; ICE restart needed.
3330// 3. ICE restart initiated by remote peer, but only for one m= section.
3331// 4. Next createOffer should initiate an ICE restart, but only for the other
3332// m= section; it would be pointless to do an ICE restart for the m= section
3333// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003334TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003335 PeerConnectionInterface::RTCConfiguration config;
3336 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003337 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003338 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003339 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3340 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003341
3342 // Do initial offer/answer so there's something to restart.
3343 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003344 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003345
3346 // Change ICE policy, which should set the "needs-ice-restart" flag.
3347 config.type = PeerConnectionInterface::kAll;
3348 EXPECT_TRUE(pc_->SetConfiguration(config));
3349
3350 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003351 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003352 webrtc::CreateSessionDescription(SdpType::kOffer,
3353 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003354 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003355 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3356 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003357 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003358 CreateAnswerAsLocalDescription();
3359
3360 // Grab the ufrags.
3361 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003362 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003363
3364 // Create offer and grab the new ufrags.
3365 CreateOfferAsLocalDescription();
3366 std::vector<std::string> subsequent_ufrags =
3367 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003368 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003369
3370 // Ensure that only the ufrag for the second m= section changed.
3371 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3372 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3373}
3374
deadbeeffe4a8a42016-12-20 17:56:17 -08003375// Tests that the methods to return current/pending descriptions work as
3376// expected at different points in the offer/answer exchange. This test does
3377// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003378TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003379 // This disables DTLS so we can apply an answer to ourselves.
3380 CreatePeerConnection();
3381
3382 // Create initial local offer and get SDP (which will also be used as
3383 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003384 std::unique_ptr<SessionDescriptionInterface> local_offer;
3385 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003386 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003387 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003388
3389 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003390 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3391 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3392 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003393 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3394 EXPECT_EQ(nullptr, pc_->current_local_description());
3395 EXPECT_EQ(nullptr, pc_->current_remote_description());
3396
3397 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003398 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003399 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003400 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3401 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3402 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3403 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003404 EXPECT_EQ(nullptr, pc_->current_local_description());
3405 EXPECT_EQ(nullptr, pc_->current_remote_description());
3406
3407 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003408 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003409 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003410 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3411 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003412 EXPECT_EQ(nullptr, pc_->pending_local_description());
3413 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003414 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3415 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003416
3417 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003418 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003419 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003420 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3421 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3422 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003423 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003424 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3425 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003426
3427 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003428 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003429 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003430 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3431 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3432 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3433 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3434 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3435 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003436
3437 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003438 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003439 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003440 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3441 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003442 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3443 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003444 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3445 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003446}
3447
zhihuang77985012017-02-07 15:45:16 -08003448// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3449// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003450// This version tests the StartRtcEventLog version that receives a file.
Steve Anton36da6ff2018-02-16 16:04:20 -08003451TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003452 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003453 CreatePeerConnection();
3454 // The RtcEventLog will be reset when the PeerConnection is closed.
3455 pc_->Close();
3456
Elad Alon9e6565b2017-10-11 16:04:13 +02003457 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3458 std::string filename = webrtc::test::OutputPath() +
3459 test_info->test_case_name() + test_info->name();
3460 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3461
3462 constexpr int64_t max_size_bytes = 1024;
3463
zhihuang77985012017-02-07 15:45:16 -08003464 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3465 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003466
3467 // Cleanup.
3468 rtc::ClosePlatformFile(file);
3469 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003470}
3471
Elad Alon99c3fe52017-10-13 16:29:40 +02003472// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3473// after the PeerConnection is closed.
3474// This version tests the StartRtcEventLog version that receives an object
3475// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003476TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003477 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3478 CreatePeerConnection();
3479 // The RtcEventLog will be reset when the PeerConnection is closed.
3480 pc_->Close();
3481
3482 rtc::PlatformFile file = 0;
3483 int64_t max_size_bytes = 1024;
3484 EXPECT_FALSE(pc_->StartRtcEventLog(
Karl Wiberg918f50c2018-07-05 11:40:33 +02003485 absl::make_unique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
Bjorn Tereliusde939432017-11-20 17:38:14 +01003486 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003487 pc_->StopRtcEventLog();
3488}
3489
deadbeef30952b42017-04-21 02:41:29 -07003490// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003491TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003492 CreatePeerConnection();
3493
3494 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003495 RTCOfferAnswerOptions options;
3496 options.offer_to_receive_audio = 1;
3497 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003498 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003499 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003500 cricket::SessionDescription* desc = offer->description();
3501 ASSERT_EQ(2u, desc->transport_infos().size());
3502 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3503 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3504
3505 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003506 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003507 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003508 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003509 desc = answer->description();
3510 ASSERT_EQ(2u, desc->transport_infos().size());
3511 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3512 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3513}
3514
deadbeef1dcb1642017-03-29 21:08:16 -07003515// Test that ICE renomination isn't offered if it's not enabled in the PC's
3516// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003517TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003518 PeerConnectionInterface::RTCConfiguration config;
3519 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003520 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003521 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003522
3523 std::unique_ptr<SessionDescriptionInterface> offer;
3524 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3525 cricket::SessionDescription* desc = offer->description();
3526 EXPECT_EQ(1u, desc->transport_infos().size());
3527 EXPECT_FALSE(
3528 desc->transport_infos()[0].description.GetIceParameters().renomination);
3529}
3530
3531// Test that the ICE renomination option is present in generated offers/answers
3532// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003533TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003534 PeerConnectionInterface::RTCConfiguration config;
3535 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003536 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003537 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003538
3539 std::unique_ptr<SessionDescriptionInterface> offer;
3540 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3541 cricket::SessionDescription* desc = offer->description();
3542 EXPECT_EQ(1u, desc->transport_infos().size());
3543 EXPECT_TRUE(
3544 desc->transport_infos()[0].description.GetIceParameters().renomination);
3545
3546 // Set the offer as a remote description, then create an answer and ensure it
3547 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003548 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003549 std::unique_ptr<SessionDescriptionInterface> answer;
3550 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3551 desc = answer->description();
3552 EXPECT_EQ(1u, desc->transport_infos().size());
3553 EXPECT_TRUE(
3554 desc->transport_infos()[0].description.GetIceParameters().renomination);
3555}
3556
3557// Test that if CreateOffer is called with the deprecated "offer to receive
3558// audio/video" constraints, they're processed and result in an offer with
3559// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003560TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003561 CreatePeerConnection();
3562
Niels Möllerf06f9232018-08-07 12:32:18 +02003563 RTCOfferAnswerOptions options;
3564 options.offer_to_receive_audio = 1;
3565 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003566 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003567 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003568
3569 cricket::SessionDescription* desc = offer->description();
3570 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3571 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3572 ASSERT_NE(nullptr, audio);
3573 ASSERT_NE(nullptr, video);
3574 EXPECT_FALSE(audio->rejected);
3575 EXPECT_FALSE(video->rejected);
3576}
3577
3578// Test that if CreateAnswer is called with the deprecated "offer to receive
3579// audio/video" constraints, they're processed and can be used to reject an
3580// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003581// Don't run under Unified Plan since this behavior is not supported.
3582TEST_F(PeerConnectionInterfaceTestPlanB,
3583 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003584 CreatePeerConnection();
3585
3586 // First, create an offer with audio/video and apply it as a remote
3587 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003588 RTCOfferAnswerOptions options;
3589 options.offer_to_receive_audio = 1;
3590 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003591 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003592 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003593 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003594
3595 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003596 options.offer_to_receive_audio = 0;
3597 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003598 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003599 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003600
3601 cricket::SessionDescription* desc = answer->description();
3602 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3603 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3604 ASSERT_NE(nullptr, audio);
3605 ASSERT_NE(nullptr, video);
3606 EXPECT_TRUE(audio->rejected);
3607 EXPECT_TRUE(video->rejected);
3608}
3609
deadbeef1dcb1642017-03-29 21:08:16 -07003610// Test that negotiation can succeed with a data channel only, and with the max
3611// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003612#ifdef HAVE_SCTP
3613TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3614#else
3615TEST_P(PeerConnectionInterfaceTest,
3616 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3617#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003618 PeerConnectionInterface::RTCConfiguration config;
3619 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003620 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003621
3622 // First, create an offer with only a data channel and apply it as a remote
3623 // description.
3624 pc_->CreateDataChannel("test", nullptr);
3625 std::unique_ptr<SessionDescriptionInterface> offer;
3626 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003627 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003628
3629 // Create and set answer as well.
3630 std::unique_ptr<SessionDescriptionInterface> answer;
3631 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003632 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003633}
3634
Steve Anton36da6ff2018-02-16 16:04:20 -08003635TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003636 CreatePeerConnection();
3637 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003638 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003639 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3640}
3641
Steve Anton36da6ff2018-02-16 16:04:20 -08003642TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003643 CreatePeerConnection();
3644 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003645 bitrate.min_bitrate_bps = -1;
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, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003650 CreatePeerConnection();
3651 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003652 bitrate.min_bitrate_bps = 5;
3653 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003654 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3655}
3656
Steve Anton36da6ff2018-02-16 16:04:20 -08003657TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003658 CreatePeerConnection();
3659 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003660 bitrate.current_bitrate_bps = -1;
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, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003665 CreatePeerConnection();
3666 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003667 bitrate.current_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, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003673 CreatePeerConnection();
3674 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003675 bitrate.min_bitrate_bps = 10;
3676 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003677 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3678}
3679
Steve Anton36da6ff2018-02-16 16:04:20 -08003680TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003681 CreatePeerConnection();
3682 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003683 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003684 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3685}
3686
Steve Anton038834f2017-07-14 15:59:59 -07003687// ice_regather_interval_range requires WebRTC to be configured for continual
3688// gathering already.
Steve Anton36da6ff2018-02-16 16:04:20 -08003689TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003690 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3691 PeerConnectionInterface::RTCConfiguration config;
3692 config.ice_regather_interval_range.emplace(1000, 2000);
3693 config.continual_gathering_policy =
3694 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3695 CreatePeerConnectionExpectFail(config);
3696}
3697
3698// Ensures that there is no error when ice_regather_interval_range is set with
3699// continual gathering enabled.
Steve Anton36da6ff2018-02-16 16:04:20 -08003700TEST_P(PeerConnectionInterfaceTest,
Steve Anton038834f2017-07-14 15:59:59 -07003701 SetIceRegatherIntervalRangeWithContinualGathering) {
3702 PeerConnectionInterface::RTCConfiguration config;
3703 config.ice_regather_interval_range.emplace(1000, 2000);
3704 config.continual_gathering_policy =
3705 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
Niels Möllerf06f9232018-08-07 12:32:18 +02003706 CreatePeerConnection(config);
Steve Anton038834f2017-07-14 15:59:59 -07003707}
3708
Niels Möller0c4f7be2018-05-07 14:01:37 +02003709// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003710// by Call's BitrateConstraints, which comes from the SDP or a default value.
3711// This test checks that a call to SetBitrate with a current bitrate that will
3712// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003713TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003714 CreatePeerConnection();
3715 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003716 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003717 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3718}
3719
zhihuang1c378ed2017-08-17 14:10:50 -07003720// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003721TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003722 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3723 RTCOfferAnswerOptions rtc_options;
3724
3725 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3726 // than kMaxOfferToReceiveMedia should be treated as invalid.
3727 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3728 CreatePeerConnection();
3729 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3730
3731 rtc_options.offer_to_receive_audio =
3732 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3733 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3734}
3735
Steve Anton36da6ff2018-02-16 16:04:20 -08003736TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003737 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3738 RTCOfferAnswerOptions rtc_options;
3739
3740 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3741 // than kMaxOfferToReceiveMedia should be treated as invalid.
3742 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3743 CreatePeerConnection();
3744 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3745
3746 rtc_options.offer_to_receive_video =
3747 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3748 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3749}
3750
3751// Test that the audio and video content will be added to an offer if both
3752// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003753TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003754 RTCOfferAnswerOptions rtc_options;
3755 rtc_options.offer_to_receive_audio = 1;
3756 rtc_options.offer_to_receive_video = 1;
3757
3758 std::unique_ptr<SessionDescriptionInterface> offer;
3759 CreatePeerConnection();
3760 offer = CreateOfferWithOptions(rtc_options);
3761 ASSERT_TRUE(offer);
3762 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3763 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3764}
3765
3766// Test that only audio content will be added to the offer if only
3767// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003768TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003769 RTCOfferAnswerOptions rtc_options;
3770 rtc_options.offer_to_receive_audio = 1;
3771 rtc_options.offer_to_receive_video = 0;
3772
3773 std::unique_ptr<SessionDescriptionInterface> offer;
3774 CreatePeerConnection();
3775 offer = CreateOfferWithOptions(rtc_options);
3776 ASSERT_TRUE(offer);
3777 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3778 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3779}
3780
3781// Test that only video content will be added if only |offer_to_receive_video|
3782// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003783TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003784 RTCOfferAnswerOptions rtc_options;
3785 rtc_options.offer_to_receive_audio = 0;
3786 rtc_options.offer_to_receive_video = 1;
3787
3788 std::unique_ptr<SessionDescriptionInterface> offer;
3789 CreatePeerConnection();
3790 offer = CreateOfferWithOptions(rtc_options);
3791 ASSERT_TRUE(offer);
3792 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3793 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3794}
3795
zhihuang1c378ed2017-08-17 14:10:50 -07003796// Test that no media content will be added to the offer if using default
3797// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003798TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003799 RTCOfferAnswerOptions rtc_options;
3800
3801 std::unique_ptr<SessionDescriptionInterface> offer;
3802 CreatePeerConnection();
3803 offer = CreateOfferWithOptions(rtc_options);
3804 ASSERT_TRUE(offer);
3805 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3806 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3807}
3808
3809// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3810// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003811TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3812 CreatePeerConnection();
3813
zhihuang1c378ed2017-08-17 14:10:50 -07003814 RTCOfferAnswerOptions rtc_options;
3815 rtc_options.ice_restart = false;
3816 rtc_options.offer_to_receive_audio = 1;
3817
3818 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003819 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003820 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3821 auto ufrag1 =
3822 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3823 auto pwd1 =
3824 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003825
3826 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3827 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003828 auto ufrag2 =
3829 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3830 auto pwd2 =
3831 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003832
3833 // |ice_restart| is true, the ufrag/pwd should change.
3834 rtc_options.ice_restart = true;
3835 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003836 auto ufrag3 =
3837 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3838 auto pwd3 =
3839 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003840
3841 EXPECT_EQ(ufrag1, ufrag2);
3842 EXPECT_EQ(pwd1, pwd2);
3843 EXPECT_NE(ufrag2, ufrag3);
3844 EXPECT_NE(pwd2, pwd3);
3845}
3846
3847// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3848// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003849TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003850 RTCOfferAnswerOptions rtc_options;
3851 rtc_options.offer_to_receive_audio = 1;
3852 rtc_options.offer_to_receive_video = 1;
3853
3854 std::unique_ptr<SessionDescriptionInterface> offer;
3855 CreatePeerConnection();
3856
3857 rtc_options.use_rtp_mux = true;
3858 offer = CreateOfferWithOptions(rtc_options);
3859 ASSERT_TRUE(offer);
3860 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3861 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3862 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3863
3864 rtc_options.use_rtp_mux = false;
3865 offer = CreateOfferWithOptions(rtc_options);
3866 ASSERT_TRUE(offer);
3867 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3868 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3869 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3870}
3871
zhihuang141aacb2017-08-29 13:23:53 -07003872// This test ensures OnRenegotiationNeeded is called when we add track with
3873// MediaStream -> AddTrack in the same way it is called when we add track with
3874// PeerConnection -> AddTrack.
3875// The test can be removed once addStream is rewritten in terms of addTrack
3876// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003877// Don't run under Unified Plan since the stream API is not available.
3878TEST_F(PeerConnectionInterfaceTestPlanB,
3879 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003880 CreatePeerConnectionWithoutDtls();
3881 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003882 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003883 pc_->AddStream(stream);
3884 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003885 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003886 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003887 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003888 stream->AddTrack(audio_track);
3889 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3890 observer_.renegotiation_needed_ = false;
3891
3892 stream->AddTrack(video_track);
3893 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3894 observer_.renegotiation_needed_ = false;
3895
3896 stream->RemoveTrack(audio_track);
3897 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3898 observer_.renegotiation_needed_ = false;
3899
3900 stream->RemoveTrack(video_track);
3901 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3902 observer_.renegotiation_needed_ = false;
3903}
3904
Zhi Huangb2d355e2017-10-26 17:26:37 -07003905// Tests that an error is returned if a description is applied that has fewer
3906// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003907TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003908 MediaSectionCountEnforcedForSubsequentOffer) {
3909 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003910 AddAudioTrack("audio_label");
3911 AddVideoTrack("video_label");
3912
Zhi Huangb2d355e2017-10-26 17:26:37 -07003913 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003914 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003915 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3916
3917 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003918 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003919 offer->description()->contents().pop_back();
3920 offer->description()->contents().pop_back();
3921 ASSERT_TRUE(offer->description()->contents().empty());
3922 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3923
3924 std::unique_ptr<SessionDescriptionInterface> answer;
3925 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3926 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3927
3928 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003929 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003930 offer->description()->contents().pop_back();
3931 offer->description()->contents().pop_back();
3932 ASSERT_TRUE(offer->description()->contents().empty());
3933 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3934}
3935
Johannes Kron89f874e2018-11-12 10:25:48 +01003936TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3937 RTCConfiguration config;
3938 // Default behavior is false.
3939 CreatePeerConnection(config);
3940 std::unique_ptr<SessionDescriptionInterface> offer;
3941 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3942 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
3943 // Possible to set to true.
3944 config.offer_extmap_allow_mixed = true;
3945 CreatePeerConnection(config);
3946 offer.release();
3947 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3948 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3949}
3950
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003951INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3952 PeerConnectionInterfaceTest,
3953 Values(SdpSemantics::kPlanB,
3954 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003955
nisse51542be2016-02-12 02:27:06 -08003956class PeerConnectionMediaConfigTest : public testing::Test {
3957 protected:
3958 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003959 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003960 pcf_->Initialize();
3961 }
nisseeaabdf62017-05-05 02:23:02 -07003962 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003963 const RTCConfiguration& config) {
3964 rtc::scoped_refptr<PeerConnectionInterface> pc(
3965 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003966 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003967 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003968 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003969 }
3970
zhihuang9763d562016-08-05 11:14:50 -07003971 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003972 MockPeerConnectionObserver observer_;
3973};
3974
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003975// This sanity check validates the test infrastructure itself.
3976TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3977 PeerConnectionInterface::RTCConfiguration config;
3978 rtc::scoped_refptr<PeerConnectionInterface> pc(
3979 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3980 EXPECT_TRUE(pc.get());
3981 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3982 pc->Close(); // No abort -> ok.
3983 SUCCEED();
3984}
3985
nisse51542be2016-02-12 02:27:06 -08003986// This test verifies the default behaviour with no constraints and a
3987// default RTCConfiguration.
3988TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3989 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003990
Niels Möllerf06f9232018-08-07 12:32:18 +02003991 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003992
3993 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003994 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3995 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003996 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003997 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003998}
3999
Niels Möller1d7ecd22018-01-18 15:25:12 +01004000// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07004001// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08004002TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
4003 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08004004
Niels Möller71bdda02016-03-31 12:59:59 +02004005 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02004006 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08004007
Niels Möller1d7ecd22018-01-18 15:25:12 +01004008 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08004009}
4010
Niels Möller6539f692018-01-18 08:58:50 +01004011// This test verifies that the experiment_cpu_load_estimator flag is
4012// propagated from RTCConfiguration to the PeerConnection.
4013TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
4014 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01004015
4016 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02004017 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01004018
4019 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
4020}
4021
deadbeef293e9262017-01-11 12:28:30 -08004022// Tests a few random fields being different.
4023TEST(RTCConfigurationTest, ComparisonOperators) {
4024 PeerConnectionInterface::RTCConfiguration a;
4025 PeerConnectionInterface::RTCConfiguration b;
4026 EXPECT_EQ(a, b);
4027
4028 PeerConnectionInterface::RTCConfiguration c;
4029 c.servers.push_back(PeerConnectionInterface::IceServer());
4030 EXPECT_NE(a, c);
4031
4032 PeerConnectionInterface::RTCConfiguration d;
4033 d.type = PeerConnectionInterface::kRelay;
4034 EXPECT_NE(a, d);
4035
4036 PeerConnectionInterface::RTCConfiguration e;
4037 e.audio_jitter_buffer_max_packets = 5;
4038 EXPECT_NE(a, e);
4039
4040 PeerConnectionInterface::RTCConfiguration f;
4041 f.ice_connection_receiving_timeout = 1337;
4042 EXPECT_NE(a, f);
4043
4044 PeerConnectionInterface::RTCConfiguration g;
4045 g.disable_ipv6 = true;
4046 EXPECT_NE(a, g);
4047
4048 PeerConnectionInterface::RTCConfiguration h(
4049 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4050 EXPECT_NE(a, h);
4051}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004052
4053} // namespace
4054} // namespace webrtc