blob: 731fe8e3ef1fddd34145b17566ff32a585d9f26d [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "api/peer_connection_interface.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <limits.h>
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010018#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
24#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/call/call_factory_interface.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010026#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "api/data_channel_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/media_stream_interface.h"
30#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/rtc_error.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020032#include "api/rtc_event_log/rtc_event_log.h"
33#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "api/rtc_event_log_output.h"
35#include "api/rtp_receiver_interface.h"
36#include "api/rtp_sender_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000037#include "api/rtp_transceiver_direction.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010038#include "api/scoped_refptr.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020039#include "api/task_queue/default_task_queue_factory.h"
Erik Språngceb44952020-09-22 11:36:35 +020040#include "api/transport/field_trial_based_config.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 "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "media/base/media_config.h"
45#include "media/base/media_engine.h"
46#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020048#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "media/sctp/sctp_transport_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010050#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080052#include "p2p/base/fake_port_allocator.h"
53#include "p2p/base/p2p_constants.h"
Yves Gerey3e707812018-11-28 16:47:49 +010054#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "p2p/base/port_allocator.h"
56#include "p2p/base/transport_description.h"
57#include "p2p/base/transport_info.h"
58#include "pc/audio_track.h"
59#include "pc/media_session.h"
60#include "pc/media_stream.h"
61#include "pc/peer_connection.h"
62#include "pc/peer_connection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "pc/rtp_sender.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000064#include "pc/rtp_sender_proxy.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "pc/session_description.h"
66#include "pc/stream_collection.h"
67#include "pc/test/fake_audio_capture_module.h"
68#include "pc/test/fake_rtc_certificate_generator.h"
69#include "pc/test/fake_video_track_source.h"
70#include "pc/test/mock_peer_connection_observers.h"
71#include "pc/test/test_sdp_strings.h"
72#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010073#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020074#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080075#include "rtc_base/ref_counted_object.h"
76#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080077#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010078#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080079#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020080#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010081#include "test/gtest.h"
kwibergac9f8762016-09-30 22:29:43 -070082
83#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080084#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070085#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010087namespace webrtc {
88namespace {
89
Seth Hampson845e8782018-03-02 11:34:10 -080090static const char kStreamId1[] = "local_stream_1";
91static const char kStreamId2[] = "local_stream_2";
92static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093static const int kDefaultStunPort = 3478;
94static const char kStunAddressOnly[] = "stun:address";
95static const char kStunInvalidPort[] = "stun:address:-1";
96static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
97static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +010098static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const char kTurnUsername[] = "user";
100static const char kTurnPassword[] = "password";
101static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
deadbeefab9b2d12015-10-14 11:33:11 -0700104static const char kStreams[][8] = {"stream1", "stream2"};
105static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
106static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
107
deadbeef5e97fb52015-10-15 12:49:08 -0700108static const char kRecvonly[] = "recvonly";
109static const char kSendrecv[] = "sendrecv";
110
deadbeefab9b2d12015-10-14 11:33:11 -0700111// Reference SDP with a MediaStream with label "stream1" and audio track with
112// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800113static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700114 "v=0\r\n"
115 "o=- 0 0 IN IP4 127.0.0.1\r\n"
116 "s=-\r\n"
117 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800118 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700119 "a=ice-ufrag:e5785931\r\n"
120 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
121 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
122 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700123 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700124 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800125 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700126 "a=rtpmap:103 ISAC/16000\r\n"
127 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000128 "a=ssrc:1 mslabel:stream1\r\n"
129 "a=ssrc:1 label:audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700130 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800131 "a=ice-ufrag:e5785931\r\n"
132 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
133 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
134 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800137 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=rtpmap:120 VP8/90000\r\n"
139 "a=ssrc:2 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000140 "a=ssrc:2 mslabel:stream1\r\n"
141 "a=ssrc:2 label:videotrack0\r\n";
142// Same string as above but with the MID changed to the Unified Plan default.
143// This is needed so that this SDP can be used as an answer for a Unified Plan
144// offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800145static const char kSdpStringWithStream1UnifiedPlan[] =
146 "v=0\r\n"
147 "o=- 0 0 IN IP4 127.0.0.1\r\n"
148 "s=-\r\n"
149 "t=0 0\r\n"
150 "m=audio 1 RTP/AVPF 103\r\n"
151 "a=ice-ufrag:e5785931\r\n"
152 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
153 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
154 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
155 "a=mid:0\r\n"
156 "a=sendrecv\r\n"
157 "a=rtcp-mux\r\n"
158 "a=rtpmap:103 ISAC/16000\r\n"
159 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000160 "a=ssrc:1 mslabel:stream1\r\n"
161 "a=ssrc:1 label:audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800162 "m=video 1 RTP/AVPF 120\r\n"
163 "a=ice-ufrag:e5785931\r\n"
164 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
165 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
166 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
167 "a=mid:1\r\n"
168 "a=sendrecv\r\n"
169 "a=rtcp-mux\r\n"
170 "a=rtpmap:120 VP8/90000\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000171 "a=ssrc:2 cname:stream1\r\n"
172 "a=ssrc:2 mslabel:stream1\r\n"
173 "a=ssrc:2 label:videotrack0\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700174
zhihuang81c3a032016-11-17 12:06:24 -0800175// Reference SDP with a MediaStream with label "stream1" and audio track with
176// id "audio_1";
177static const char kSdpStringWithStream1AudioTrackOnly[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800182 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800183 "a=ice-ufrag:e5785931\r\n"
184 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
185 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
186 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800187 "a=mid:audio\r\n"
188 "a=sendrecv\r\n"
189 "a=rtpmap:103 ISAC/16000\r\n"
190 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000191 "a=ssrc:1 mslabel:stream1\r\n"
192 "a=ssrc:1 label:audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800193 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800194
deadbeefab9b2d12015-10-14 11:33:11 -0700195// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
196// MediaStreams have one audio track and one video track.
197// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800198static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700199 "v=0\r\n"
200 "o=- 0 0 IN IP4 127.0.0.1\r\n"
201 "s=-\r\n"
202 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800203 "a=msid-semantic: WMS stream1 stream2\r\n"
204 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=ice-ufrag:e5785931\r\n"
206 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
207 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
208 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800211 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n"
213 "a=ssrc:1 cname:stream1\r\n"
214 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
215 "a=ssrc:3 cname:stream2\r\n"
216 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
217 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800218 "a=ice-ufrag:e5785931\r\n"
219 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
220 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
221 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700223 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800224 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=rtpmap:120 VP8/0\r\n"
226 "a=ssrc:2 cname:stream1\r\n"
227 "a=ssrc:2 msid:stream1 videotrack0\r\n"
228 "a=ssrc:4 cname:stream2\r\n"
229 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800230static const char kSdpStringWithStream1And2UnifiedPlan[] =
231 "v=0\r\n"
232 "o=- 0 0 IN IP4 127.0.0.1\r\n"
233 "s=-\r\n"
234 "t=0 0\r\n"
235 "a=msid-semantic: WMS stream1 stream2\r\n"
236 "m=audio 1 RTP/AVPF 103\r\n"
237 "a=ice-ufrag:e5785931\r\n"
238 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
239 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
240 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
241 "a=mid:0\r\n"
242 "a=sendrecv\r\n"
243 "a=rtcp-mux\r\n"
244 "a=rtpmap:103 ISAC/16000\r\n"
245 "a=ssrc:1 cname:stream1\r\n"
246 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
247 "m=video 1 RTP/AVPF 120\r\n"
248 "a=ice-ufrag:e5785931\r\n"
249 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
250 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
251 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
252 "a=mid:1\r\n"
253 "a=sendrecv\r\n"
254 "a=rtcp-mux\r\n"
255 "a=rtpmap:120 VP8/0\r\n"
256 "a=ssrc:2 cname:stream1\r\n"
257 "a=ssrc:2 msid:stream1 videotrack0\r\n"
258 "m=audio 1 RTP/AVPF 103\r\n"
259 "a=ice-ufrag:e5785931\r\n"
260 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
261 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
262 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
263 "a=mid:2\r\n"
264 "a=sendrecv\r\n"
265 "a=rtcp-mux\r\n"
266 "a=rtpmap:103 ISAC/16000\r\n"
267 "a=ssrc:3 cname:stream2\r\n"
268 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
269 "m=video 1 RTP/AVPF 120\r\n"
270 "a=ice-ufrag:e5785931\r\n"
271 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
272 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
273 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
274 "a=mid:3\r\n"
275 "a=sendrecv\r\n"
276 "a=rtcp-mux\r\n"
277 "a=rtpmap:120 VP8/0\r\n"
278 "a=ssrc:4 cname:stream2\r\n"
279 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700280
281// Reference SDP without MediaStreams. Msid is not supported.
282static const char kSdpStringWithoutStreams[] =
283 "v=0\r\n"
284 "o=- 0 0 IN IP4 127.0.0.1\r\n"
285 "s=-\r\n"
286 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800287 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700288 "a=ice-ufrag:e5785931\r\n"
289 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
290 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
291 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700292 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700293 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800294 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700295 "a=rtpmap:103 ISAC/16000\r\n"
296 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800297 "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:video\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:120 VP8/90000\r\n";
305
306// Reference SDP without MediaStreams. Msid is supported.
307static const char kSdpStringWithMsidWithoutStreams[] =
308 "v=0\r\n"
309 "o=- 0 0 IN IP4 127.0.0.1\r\n"
310 "s=-\r\n"
311 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800312 "a=msid-semantic: WMS\r\n"
313 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700314 "a=ice-ufrag:e5785931\r\n"
315 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
316 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
317 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700318 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700319 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800320 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700321 "a=rtpmap:103 ISAC/16000\r\n"
322 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800323 "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:video\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:120 VP8/90000\r\n";
331
332// Reference SDP without MediaStreams and audio only.
333static const char kSdpStringWithoutStreamsAudioOnly[] =
334 "v=0\r\n"
335 "o=- 0 0 IN IP4 127.0.0.1\r\n"
336 "s=-\r\n"
337 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800338 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700339 "a=ice-ufrag:e5785931\r\n"
340 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
341 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
342 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700343 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700344 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800345 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700346 "a=rtpmap:103 ISAC/16000\r\n";
347
348// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
349static const char kSdpStringSendOnlyWithoutStreams[] =
350 "v=0\r\n"
351 "o=- 0 0 IN IP4 127.0.0.1\r\n"
352 "s=-\r\n"
353 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800354 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700355 "a=ice-ufrag:e5785931\r\n"
356 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
357 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
358 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700359 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700360 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700361 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800362 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700363 "a=rtpmap:103 ISAC/16000\r\n"
364 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800365 "a=ice-ufrag:e5785931\r\n"
366 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
367 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
368 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700369 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700370 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700371 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800372 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700373 "a=rtpmap:120 VP8/90000\r\n";
374
375static const char kSdpStringInit[] =
376 "v=0\r\n"
377 "o=- 0 0 IN IP4 127.0.0.1\r\n"
378 "s=-\r\n"
379 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700380 "a=msid-semantic: WMS\r\n";
381
382static const char kSdpStringAudio[] =
383 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800384 "a=ice-ufrag:e5785931\r\n"
385 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
386 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
387 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700388 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700389 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800390 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700391 "a=rtpmap:103 ISAC/16000\r\n";
392
393static const char kSdpStringVideo[] =
394 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800395 "a=ice-ufrag:e5785931\r\n"
396 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
397 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
398 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700399 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700400 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800401 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700402 "a=rtpmap:120 VP8/90000\r\n";
403
404static const char kSdpStringMs1Audio0[] =
405 "a=ssrc:1 cname:stream1\r\n"
406 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
407
408static const char kSdpStringMs1Video0[] =
409 "a=ssrc:2 cname:stream1\r\n"
410 "a=ssrc:2 msid:stream1 videotrack0\r\n";
411
412static const char kSdpStringMs1Audio1[] =
413 "a=ssrc:3 cname:stream1\r\n"
414 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
415
416static const char kSdpStringMs1Video1[] =
417 "a=ssrc:4 cname:stream1\r\n"
418 "a=ssrc:4 msid:stream1 videotrack1\r\n";
419
deadbeef8662f942017-01-20 21:20:51 -0800420static const char kDtlsSdesFallbackSdp[] =
421 "v=0\r\n"
422 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
423 "s=-\r\n"
424 "c=IN IP4 0.0.0.0\r\n"
425 "t=0 0\r\n"
426 "a=group:BUNDLE audio\r\n"
427 "a=msid-semantic: WMS\r\n"
428 "m=audio 1 RTP/SAVPF 0\r\n"
429 "a=sendrecv\r\n"
430 "a=rtcp-mux\r\n"
431 "a=mid:audio\r\n"
432 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000433 "a=ssrc:1 mslabel:stream1\r\n"
434 "a=ssrc:1 label:audiotrack0\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800435 "a=ice-ufrag:e5785931\r\n"
436 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
437 "a=rtpmap:0 pcmu/8000\r\n"
438 "a=fingerprint:sha-1 "
439 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
440 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700441 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800442 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
443 "dummy_session_params\r\n";
444
Niels Möllerdec9f742019-06-03 15:25:20 +0200445class RtcEventLogOutputNull final : public RtcEventLogOutput {
446 public:
447 bool IsActive() const override { return true; }
448 bool Write(const std::string& output) override { return true; }
449};
450
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100451using ::cricket::StreamParams;
perkjd61bf802016-03-24 03:16:19 -0700452using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800453using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
Steve Anton36da6ff2018-02-16 16:04:20 -0800455using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
456using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700457
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458// Gets the first ssrc of given content type from the ContentInfo.
459bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
460 if (!content_info || !ssrc) {
461 return false;
462 }
463 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800464 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 if (!media_desc || media_desc->streams().empty()) {
466 return false;
467 }
468 *ssrc = media_desc->streams().begin()->first_ssrc();
469 return true;
470}
471
deadbeefd1a38b52016-12-10 13:15:33 -0800472// Get the ufrags out of an SDP blob. Useful for testing ICE restart
473// behavior.
474std::vector<std::string> GetUfrags(
475 const webrtc::SessionDescriptionInterface* desc) {
476 std::vector<std::string> ufrags;
477 for (const cricket::TransportInfo& info :
478 desc->description()->transport_infos()) {
479 ufrags.push_back(info.description.ice_ufrag);
480 }
481 return ufrags;
482}
483
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484void SetSsrcToZero(std::string* sdp) {
485 const char kSdpSsrcAtribute[] = "a=ssrc:";
486 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
487 size_t ssrc_pos = 0;
488 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200489 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 size_t end_ssrc = sdp->find(" ", ssrc_pos);
491 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
492 ssrc_pos = end_ssrc;
493 }
494}
495
Artem Titov880fa812021-07-30 22:30:23 +0200496// Check if `streams` contains the specified track.
deadbeefab9b2d12015-10-14 11:33:11 -0700497bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800498 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700499 const std::string& track_id) {
500 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800501 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700502 return true;
503 }
504 }
505 return false;
506}
507
Artem Titov880fa812021-07-30 22:30:23 +0200508// Check if `senders` contains the specified sender, by id.
deadbeefab9b2d12015-10-14 11:33:11 -0700509bool ContainsSender(
510 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
511 const std::string& id) {
512 for (const auto& sender : senders) {
513 if (sender->id() == id) {
514 return true;
515 }
516 }
517 return false;
518}
519
Artem Titov880fa812021-07-30 22:30:23 +0200520// Check if `senders` contains the specified sender, by id and stream id.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700521bool ContainsSender(
522 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
523 const std::string& id,
524 const std::string& stream_id) {
525 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700526 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700527 return true;
528 }
529 }
530 return false;
531}
532
deadbeefab9b2d12015-10-14 11:33:11 -0700533// Create a collection of streams.
534// CreateStreamCollection(1) creates a collection that
535// correspond to kSdpStringWithStream1.
536// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
537rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700538 int number_of_streams,
539 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700540 rtc::scoped_refptr<StreamCollection> local_collection(
541 StreamCollection::Create());
542
543 for (int i = 0; i < number_of_streams; ++i) {
544 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
545 webrtc::MediaStream::Create(kStreams[i]));
546
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700547 for (int j = 0; j < tracks_per_stream; ++j) {
548 // Add a local audio track.
549 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
550 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
551 nullptr));
552 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700553
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700554 // Add a local video track.
555 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
556 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700557 webrtc::FakeVideoTrackSource::Create(),
558 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700559 stream->AddTrack(video_track);
560 }
deadbeefab9b2d12015-10-14 11:33:11 -0700561
562 local_collection->AddStream(stream);
563 }
564 return local_collection;
565}
566
567// Check equality of StreamCollections.
568bool CompareStreamCollections(StreamCollectionInterface* s1,
569 StreamCollectionInterface* s2) {
570 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
571 return false;
572 }
573
574 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700575 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700576 return false;
577 }
578 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
579 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
580 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
581 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
582
583 if (audio_tracks1.size() != audio_tracks2.size()) {
584 return false;
585 }
586 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
587 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
588 return false;
589 }
590 }
591 if (video_tracks1.size() != video_tracks2.size()) {
592 return false;
593 }
594 for (size_t j = 0; j != video_tracks1.size(); ++j) {
595 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
596 return false;
597 }
598 }
599 }
600 return true;
601}
602
perkjd61bf802016-03-24 03:16:19 -0700603// Helper class to test Observer.
604class MockTrackObserver : public ObserverInterface {
605 public:
606 explicit MockTrackObserver(NotifierInterface* notifier)
607 : notifier_(notifier) {
608 notifier_->RegisterObserver(this);
609 }
610
611 ~MockTrackObserver() { Unregister(); }
612
613 void Unregister() {
614 if (notifier_) {
615 notifier_->UnregisterObserver(this);
616 notifier_ = nullptr;
617 }
618 }
619
Danil Chapovalov3a353122020-05-15 11:16:53 +0200620 MOCK_METHOD(void, OnChanged, (), (override));
perkjd61bf802016-03-24 03:16:19 -0700621
622 private:
623 NotifierInterface* notifier_;
624};
625
nisse528b7932017-05-08 03:21:43 -0700626// The PeerConnectionMediaConfig tests below verify that configuration and
627// constraints are propagated into the PeerConnection's MediaConfig. These
628// settings are intended for MediaChannel constructors, but that is not
629// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700630class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
631 public:
zhihuang38ede132017-06-15 12:52:32 -0700632 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
633 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100634 PeerConnectionFactoryDependencies dependencies;
635 dependencies.worker_thread = rtc::Thread::Current();
636 dependencies.network_thread = rtc::Thread::Current();
637 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200638 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200639 dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200640 cricket::MediaEngineDependencies media_deps;
641 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
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.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200644 media_deps.adm = FakeAudioCaptureModule::Create();
645 SetMediaEngineDefaults(&media_deps);
Erik Språngceb44952020-09-22 11:36:35 +0200646 media_deps.trials = dependencies.trials.get();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200647 dependencies.media_engine =
648 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100649 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200650 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200651 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700652
Tommi87f70902021-04-27 14:43:08 +0200653 return rtc::make_ref_counted<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100654 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700655 }
656
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100657 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800658
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100659 private:
deadbeefd7850b22017-08-23 10:59:19 -0700660 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700661};
662
Steve Anton36da6ff2018-02-16 16:04:20 -0800663// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200664class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800666 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
667 : vss_(new rtc::VirtualSocketServer()),
668 main_(vss_.get()),
669 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800670#ifdef WEBRTC_ANDROID
671 webrtc::InitializeAndroidObjects();
672#endif
673 }
674
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200675 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700676 // Use fake audio capture module since we're only testing the interface
677 // level, and using a real one could make tests flaky when run in parallel.
678 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700680 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200681 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
682 fake_audio_capture_module_),
683 webrtc::CreateBuiltinAudioEncoderFactory(),
684 webrtc::CreateBuiltinAudioDecoderFactory(),
685 webrtc::CreateBuiltinVideoEncoderFactory(),
686 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
687 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700688 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700689 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700690 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 }
692
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200693 void TearDown() override {
694 if (pc_)
695 pc_->Close();
696 }
697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200699 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 }
701
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000702 // DTLS does not work in a loopback call, so is disabled for many
deadbeef293e9262017-01-11 12:28:30 -0800703 // tests in this file.
704 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200705 RTCConfiguration config;
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000706 PeerConnectionFactoryInterface::Options options;
707 options.disable_encryption = true;
708 pc_factory_->SetOptions(options);
Niels Möllerf06f9232018-08-07 12:32:18 +0200709 CreatePeerConnection(config);
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000710 options.disable_encryption = false;
711 pc_factory_->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700714 void CreatePeerConnectionWithIceTransportsType(
715 PeerConnectionInterface::IceTransportsType type) {
716 PeerConnectionInterface::RTCConfiguration config;
717 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200718 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700719 }
720
721 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100722 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700723 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100727 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700728 server.password = password;
729 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200730 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732
Niels Möllerf06f9232018-08-07 12:32:18 +0200733 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200734 if (pc_) {
735 pc_->Close();
736 pc_ = nullptr;
737 }
kwibergd1fe2812016-04-27 06:47:29 -0700738 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800739 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
740 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000741
deadbeef1dcb1642017-03-29 21:08:16 -0700742 // Create certificate generator unless DTLS constraint is explicitly set to
743 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200744 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200745
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000746 // These won't be used if encryption is turned off, but that's harmless.
747 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
748 cert_generator.reset(fake_certificate_generator_);
749
Steve Anton36da6ff2018-02-16 16:04:20 -0800750 RTCConfiguration modified_config = config;
751 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200752 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200753 modified_config, std::move(port_allocator), std::move(cert_generator),
754 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 ASSERT_TRUE(pc_.get() != NULL);
756 observer_.SetPeerConnectionInterface(pc_.get());
757 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
758 }
759
deadbeef0a6c4ca2015-10-06 11:38:28 -0700760 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800761 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700762 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700763 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800764 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800765 config.sdp_semantics = sdp_semantics_;
766 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200767 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700769 }
770
Steve Anton038834f2017-07-14 15:59:59 -0700771 void CreatePeerConnectionExpectFail(
772 PeerConnectionInterface::RTCConfiguration config) {
773 PeerConnectionInterface::IceServer server;
774 server.uri = kTurnIceServerUri;
775 server.password = kTurnPassword;
776 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800777 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700778 rtc::scoped_refptr<PeerConnectionInterface> pc =
779 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
780 EXPECT_EQ(nullptr, pc);
781 }
782
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100784 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800785 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
786 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
787 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800789 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790
deadbeef0a6c4ca2015-10-06 11:38:28 -0700791 CreatePeerConnectionExpectFail(kStunInvalidPort);
792 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
793 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794
Niels Möllerdb4def92019-03-18 16:53:59 +0100795 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
796 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
798 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 }
806
807 void ReleasePeerConnection() {
808 pc_ = NULL;
809 observer_.SetPeerConnectionInterface(NULL);
810 }
811
Steve Anton36da6ff2018-02-16 16:04:20 -0800812 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
813 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100814 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800815 }
816
817 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800818 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800819 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800820 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800821 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823
Steve Anton36da6ff2018-02-16 16:04:20 -0800824 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700825 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 stream->AddTrack(CreateVideoTrack(label + "v0"));
828 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800829 }
830
831 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
832 const std::string& label) {
833 return pc_factory_->CreateAudioTrack(label, nullptr);
834 }
835
836 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800837 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800838 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800839 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800840 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800841 }
842
843 void AddAudioStream(const std::string& label) {
844 rtc::scoped_refptr<MediaStreamInterface> stream(
845 pc_factory_->CreateLocalMediaStream(label));
846 stream->AddTrack(CreateAudioTrack(label + "a0"));
847 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
849
Seth Hampson845e8782018-03-02 11:34:10 -0800850 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 const std::string& audio_track_label,
852 const std::string& video_track_label) {
853 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700854 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800855 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800856 stream->AddTrack(CreateAudioTrack(audio_track_label));
857 stream->AddTrack(CreateVideoTrack(video_track_label));
858 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
Steve Anton36da6ff2018-02-16 16:04:20 -0800861 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
862 cricket::MediaType media_type) {
863 for (auto receiver : pc_->GetReceivers()) {
864 if (receiver->media_type() == media_type) {
865 return receiver;
866 }
867 }
868 return nullptr;
869 }
870
kwibergd1fe2812016-04-27 06:47:29 -0700871 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200872 const RTCOfferAnswerOptions* options,
873 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200874 auto observer =
875 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200877 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200879 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700882 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 return observer->result();
884 }
885
kwibergd1fe2812016-04-27 06:47:29 -0700886 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200887 const RTCOfferAnswerOptions* options) {
888 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890
kwibergd1fe2812016-04-27 06:47:29 -0700891 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200892 const RTCOfferAnswerOptions* options) {
893 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895
Steve Antondb45ca82017-09-11 18:27:34 -0700896 bool DoSetSessionDescription(
897 std::unique_ptr<SessionDescriptionInterface> desc,
898 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200899 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700901 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700903 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
zhihuang29ff8442016-07-27 11:07:25 -0700905 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
906 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
907 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 return observer->result();
909 }
910
Steve Antondb45ca82017-09-11 18:27:34 -0700911 bool DoSetLocalDescription(
912 std::unique_ptr<SessionDescriptionInterface> desc) {
913 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
Steve Antondb45ca82017-09-11 18:27:34 -0700916 bool DoSetRemoteDescription(
917 std::unique_ptr<SessionDescriptionInterface> desc) {
918 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
921 // Calls PeerConnection::GetStats and check the return value.
922 // It does not verify the values in the StatReports since a RTCP packet might
923 // be required.
924 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200925 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Yves Gerey665174f2018-06-19 15:03:05 +0200926 if (!pc_->GetStats(observer, track,
927 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 return false;
929 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
930 return observer->called();
931 }
932
Harald Alvestrand89061872018-01-02 14:08:34 +0100933 // Call the standards-compliant GetStats function.
934 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200935 auto callback =
936 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100937 pc_->GetStats(callback);
938 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
939 return callback->called();
940 }
941
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800943 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 // Create a local stream with audio&video tracks.
Florent Castelli15a38de2022-04-06 00:38:21 +0200945 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson845e8782018-03-02 11:34:10 -0800946 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800947 } else {
948 // Unified Plan does not support AddStream, so just add an audio and video
949 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800950 AddAudioTrack(kAudioTracks[0], {kStreamId1});
951 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800952 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 CreateOfferReceiveAnswer();
954 }
955
956 // Verify that RTP Header extensions has been negotiated for audio and video.
957 void VerifyRemoteRtpHeaderExtensions() {
958 const cricket::MediaContentDescription* desc =
959 cricket::GetFirstAudioContentDescription(
960 pc_->remote_description()->description());
961 ASSERT_TRUE(desc != NULL);
962 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
963
964 desc = cricket::GetFirstVideoContentDescription(
965 pc_->remote_description()->description());
966 ASSERT_TRUE(desc != NULL);
967 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
968 }
969
970 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700971 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700972 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 std::string sdp;
974 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700975 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800976 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700977 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
979 }
980
deadbeefab9b2d12015-10-14 11:33:11 -0700981 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700982 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800983 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700984 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700985 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
986 }
987
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700989 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700990 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991
992 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
993 // audio codec change, even if the parameter has nothing to do with
994 // receiving. Not all parameters are serialized to SDP.
995 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
996 // the SessionDescription, it is necessary to do that here to in order to
997 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
998 // https://code.google.com/p/webrtc/issues/detail?id=1356
999 std::string sdp;
1000 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001001 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001002 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001003 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1005 }
1006
1007 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001008 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001009 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010
1011 std::string sdp;
1012 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001013 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001014 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001015 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1017 }
1018
1019 void CreateOfferReceiveAnswer() {
1020 CreateOfferAsLocalDescription();
1021 std::string sdp;
1022 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1023 CreateAnswerAsRemoteDescription(sdp);
1024 }
1025
1026 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001027 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001028 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1030 // audio codec change, even if the parameter has nothing to do with
1031 // receiving. Not all parameters are serialized to SDP.
1032 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1033 // the SessionDescription, it is necessary to do that here to in order to
1034 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1035 // https://code.google.com/p/webrtc/issues/detail?id=1356
1036 std::string sdp;
1037 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001038 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001039 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040
Steve Antondb45ca82017-09-11 18:27:34 -07001041 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001043 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001044 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 }
1046
deadbeefab9b2d12015-10-14 11:33:11 -07001047 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001048 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001049 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001050 ASSERT_TRUE(answer);
1051 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1053 }
1054
deadbeefab9b2d12015-10-14 11:33:11 -07001055 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001056 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001057 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001058 ASSERT_TRUE(pr_answer);
1059 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001061 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001062 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001063 ASSERT_TRUE(answer);
1064 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1066 }
1067
Seth Hampson845e8782018-03-02 11:34:10 -08001068 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001069 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001070 // called with the given stream id and expected number of tracks.
1071 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001072 int expected_num_tracks) {
1073 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001074 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001075 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001076 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 }
1078
1079 // Creates an offer and applies it as a local session description.
1080 // Creates an answer with the same SDP an the offer but removes all lines
1081 // that start with a:ssrc"
1082 void CreateOfferReceiveAnswerWithoutSsrc() {
1083 CreateOfferAsLocalDescription();
1084 std::string sdp;
1085 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1086 SetSsrcToZero(&sdp);
1087 CreateAnswerAsRemoteDescription(sdp);
1088 }
1089
deadbeefab9b2d12015-10-14 11:33:11 -07001090 // This function creates a MediaStream with label kStreams[0] and
Artem Titov880fa812021-07-30 22:30:23 +02001091 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001092 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001093 // is returned and the MediaStream is stored in
Artem Titov880fa812021-07-30 22:30:23 +02001094 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001095 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001096 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1097 size_t number_of_video_tracks) {
1098 EXPECT_LE(number_of_audio_tracks, 2u);
1099 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001100
1101 reference_collection_ = StreamCollection::Create();
1102 std::string sdp_ms1 = std::string(kSdpStringInit);
1103
Seth Hampson845e8782018-03-02 11:34:10 -08001104 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001105
1106 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001107 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001108 reference_collection_->AddStream(stream);
1109
1110 if (number_of_audio_tracks > 0) {
1111 sdp_ms1 += std::string(kSdpStringAudio);
1112 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1113 AddAudioTrack(kAudioTracks[0], stream);
1114 }
1115 if (number_of_audio_tracks > 1) {
1116 sdp_ms1 += kSdpStringMs1Audio1;
1117 AddAudioTrack(kAudioTracks[1], stream);
1118 }
1119
1120 if (number_of_video_tracks > 0) {
1121 sdp_ms1 += std::string(kSdpStringVideo);
1122 sdp_ms1 += std::string(kSdpStringMs1Video0);
1123 AddVideoTrack(kVideoTracks[0], stream);
1124 }
1125 if (number_of_video_tracks > 1) {
1126 sdp_ms1 += kSdpStringMs1Video1;
1127 AddVideoTrack(kVideoTracks[1], stream);
1128 }
1129
kwibergd1fe2812016-04-27 06:47:29 -07001130 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001131 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001132 }
1133
1134 void AddAudioTrack(const std::string& track_id,
1135 MediaStreamInterface* stream) {
1136 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1137 webrtc::AudioTrack::Create(track_id, nullptr));
1138 ASSERT_TRUE(stream->AddTrack(audio_track));
1139 }
1140
1141 void AddVideoTrack(const std::string& track_id,
1142 MediaStreamInterface* stream) {
1143 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001144 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001145 webrtc::FakeVideoTrackSource::Create(),
1146 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001147 ASSERT_TRUE(stream->AddTrack(video_track));
1148 }
1149
Steve Anton36da6ff2018-02-16 16:04:20 -08001150 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001151 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001152 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001153 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001154 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1155 return offer;
1156 }
1157
Steve Anton36da6ff2018-02-16 16:04:20 -08001158 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1159 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001160 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001161 std::unique_ptr<SessionDescriptionInterface> offer;
1162 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1163 return offer;
1164 }
1165
1166 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1167 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1168 std::unique_ptr<SessionDescriptionInterface> answer;
1169 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1170 return answer;
1171 }
1172
kwibergfd8be342016-05-14 19:44:11 -07001173 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001174 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001175 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1178 return answer;
1179 }
1180
1181 const std::string& GetFirstAudioStreamCname(
1182 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001183 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001184 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 return audio_desc->streams()[0].cname;
1186 }
1187
zhihuang1c378ed2017-08-17 14:10:50 -07001188 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1189 const RTCOfferAnswerOptions& offer_answer_options) {
1190 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001191 auto observer =
1192 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
zhihuang1c378ed2017-08-17 14:10:50 -07001193 pc_->CreateOffer(observer, offer_answer_options);
1194 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1195 return observer->MoveDescription();
1196 }
1197
1198 void CreateOfferWithOptionsAsRemoteDescription(
1199 std::unique_ptr<SessionDescriptionInterface>* desc,
1200 const RTCOfferAnswerOptions& offer_answer_options) {
1201 *desc = CreateOfferWithOptions(offer_answer_options);
1202 ASSERT_TRUE(desc != nullptr);
1203 std::string sdp;
1204 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001205 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001206 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001207 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001208 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1209 }
1210
1211 void CreateOfferWithOptionsAsLocalDescription(
1212 std::unique_ptr<SessionDescriptionInterface>* desc,
1213 const RTCOfferAnswerOptions& offer_answer_options) {
1214 *desc = CreateOfferWithOptions(offer_answer_options);
1215 ASSERT_TRUE(desc != nullptr);
1216 std::string sdp;
1217 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001218 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001219 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001220
Steve Antondb45ca82017-09-11 18:27:34 -07001221 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001222 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1223 }
1224
1225 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001226 RTC_DCHECK(content);
1227 RTC_DCHECK(content->media_description());
1228 for (const cricket::AudioCodec& codec :
1229 content->media_description()->as_audio()->codecs()) {
1230 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001231 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001232 }
zhihuang1c378ed2017-08-17 14:10:50 -07001233 }
1234 return false;
1235 }
1236
Steve Anton36da6ff2018-02-16 16:04:20 -08001237 const char* GetSdpStringWithStream1() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001238 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001239 return kSdpStringWithStream1PlanB;
1240 } else {
1241 return kSdpStringWithStream1UnifiedPlan;
1242 }
1243 }
1244
1245 const char* GetSdpStringWithStream1And2() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001246 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001247 return kSdpStringWithStream1And2PlanB;
1248 } else {
1249 return kSdpStringWithStream1And2UnifiedPlan;
1250 }
1251 }
1252
deadbeef9a6f4d42017-05-15 19:43:33 -07001253 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1254 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001255 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001256 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001257 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001258 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1259 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1260 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001262 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001263 const SdpSemantics sdp_semantics_;
1264};
1265
1266class PeerConnectionInterfaceTest
1267 : public PeerConnectionInterfaceBaseTest,
1268 public ::testing::WithParamInterface<SdpSemantics> {
1269 protected:
1270 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1271};
1272
1273class PeerConnectionInterfaceTestPlanB
1274 : public PeerConnectionInterfaceBaseTest {
1275 protected:
1276 PeerConnectionInterfaceTestPlanB()
Florent Castelli15a38de2022-04-06 00:38:21 +02001277 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278};
1279
zhihuang8f65cdf2016-05-06 18:40:30 -07001280// Generate different CNAMEs when PeerConnections are created.
1281// The CNAMEs are expected to be generated randomly. It is possible
1282// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001283TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001284 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001285 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001286 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001287 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001288 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1289 GetFirstAudioStreamCname(offer2.get()));
1290}
1291
Steve Anton36da6ff2018-02-16 16:04:20 -08001292TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001293 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001294 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001295 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001296 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001297 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1298 GetFirstAudioStreamCname(answer2.get()));
1299}
1300
Steve Anton36da6ff2018-02-16 16:04:20 -08001301TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 CreatePeerConnectionWithDifferentConfigurations) {
1303 CreatePeerConnectionWithDifferentConfigurations();
1304}
1305
Steve Anton36da6ff2018-02-16 16:04:20 -08001306TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1308 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1309 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1310 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1311 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1312 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1313 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1314 port_allocator_->candidate_filter());
1315 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1316 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1317}
1318
1319// Test that when a PeerConnection is created with a nonzero candidate pool
1320// size, the pooled PortAllocatorSession is created with all the attributes
1321// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001322TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001323 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001324 config.sdp_semantics = sdp_semantics_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325 PeerConnectionInterface::IceServer server;
1326 server.uri = kStunAddressOnly;
1327 config.servers.push_back(server);
1328 config.type = PeerConnectionInterface::kRelay;
1329 config.disable_ipv6 = true;
1330 config.tcp_candidate_policy =
1331 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001332 config.candidate_network_policy =
1333 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001335 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336
1337 const cricket::FakePortAllocatorSession* session =
1338 static_cast<const cricket::FakePortAllocatorSession*>(
1339 port_allocator_->GetPooledSession());
1340 ASSERT_NE(nullptr, session);
1341 EXPECT_EQ(1UL, session->stun_servers().size());
1342 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1343 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001344 EXPECT_LT(0U,
1345 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346}
1347
deadbeefd21eab32017-07-26 16:50:11 -07001348// Test that network-related RTCConfiguration members are applied to the
1349// PortAllocator when CreatePeerConnection is called. Specifically:
1350// - disable_ipv6_on_wifi
1351// - max_ipv6_networks
1352// - tcp_candidate_policy
1353// - candidate_network_policy
1354// - prune_turn_ports
1355//
1356// Note that the candidate filter (RTCConfiguration::type) is already tested
1357// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001358TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001359 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1360 // Create fake port allocator.
1361 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1362 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1363 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1364
1365 // Create RTCConfiguration with some network-related fields relevant to
1366 // PortAllocator populated.
1367 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001368 config.sdp_semantics = sdp_semantics_;
deadbeefd21eab32017-07-26 16:50:11 -07001369 config.disable_ipv6_on_wifi = true;
1370 config.max_ipv6_networks = 10;
1371 config.tcp_candidate_policy =
1372 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1373 config.candidate_network_policy =
1374 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1375 config.prune_turn_ports = true;
1376
1377 // Create the PC factory and PC with the above config.
1378 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1379 webrtc::CreatePeerConnectionFactory(
1380 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001381 rtc::Thread::Current(), fake_audio_capture_module_,
1382 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001383 webrtc::CreateBuiltinAudioDecoderFactory(),
1384 webrtc::CreateBuiltinVideoEncoderFactory(),
1385 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1386 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001387 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001388 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1389 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001390 EXPECT_TRUE(pc.get());
1391 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001392
1393 // Now validate that the config fields set above were applied to the
1394 // PortAllocator, as flags or otherwise.
1395 EXPECT_FALSE(raw_port_allocator->flags() &
1396 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1397 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1398 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1399 EXPECT_TRUE(raw_port_allocator->flags() &
1400 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001401 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1402 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001403}
1404
deadbeef46c73892016-11-16 19:42:04 -08001405// Check that GetConfiguration returns the configuration the PeerConnection was
1406// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001407TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001408 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001409 config.sdp_semantics = sdp_semantics_;
deadbeef46c73892016-11-16 19:42:04 -08001410 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001411 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001412
1413 PeerConnectionInterface::RTCConfiguration returned_config =
1414 pc_->GetConfiguration();
1415 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1416}
1417
1418// Check that GetConfiguration returns the last configuration passed into
1419// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001420TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001421 PeerConnectionInterface::RTCConfiguration starting_config;
Henrik Boström62995db2022-01-03 09:58:10 +01001422 starting_config.sdp_semantics = sdp_semantics_;
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001423 starting_config.bundle_policy =
1424 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1425 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001426
Steve Anton36da6ff2018-02-16 16:04:20 -08001427 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001428 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001429 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001430
1431 PeerConnectionInterface::RTCConfiguration returned_config =
1432 pc_->GetConfiguration();
1433 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1434}
1435
Steve Antonc79268f2018-04-24 09:54:10 -07001436TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1437 CreatePeerConnection();
1438
1439 pc_->Close();
1440
1441 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001442 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001443}
1444
Steve Anton36da6ff2018-02-16 16:04:20 -08001445TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001446 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001447 AddVideoStream(kStreamId1);
1448 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 ASSERT_EQ(2u, pc_->local_streams()->count());
1450
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001451 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001452 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001453 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001454 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001455 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001456 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001458 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001459 EXPECT_EQ(3u, pc_->local_streams()->count());
1460
1461 // Remove the third stream.
1462 pc_->RemoveStream(pc_->local_streams()->at(2));
1463 EXPECT_EQ(2u, pc_->local_streams()->count());
1464
1465 // Remove the second stream.
1466 pc_->RemoveStream(pc_->local_streams()->at(1));
1467 EXPECT_EQ(1u, pc_->local_streams()->count());
1468
1469 // Remove the first stream.
1470 pc_->RemoveStream(pc_->local_streams()->at(0));
1471 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472}
1473
deadbeefab9b2d12015-10-14 11:33:11 -07001474// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001475// Don't run under Unified Plan since the stream API is not available.
1476TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001477 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001478 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001479 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001480 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001481
deadbeefab9b2d12015-10-14 11:33:11 -07001482 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001483 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001484 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001485
deadbeefab9b2d12015-10-14 11:33:11 -07001486 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001487 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001488 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001489
1490 // Add another stream and ensure the offer includes both the old and new
1491 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001492 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001493 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001494
Steve Antonb1c1de12017-12-21 15:14:30 -08001495 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001496 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1497 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001498
Steve Antonb1c1de12017-12-21 15:14:30 -08001499 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001500 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1501 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001502}
1503
Steve Anton36da6ff2018-02-16 16:04:20 -08001504// Don't run under Unified Plan since the stream API is not available.
1505TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001506 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001507 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 ASSERT_EQ(1u, pc_->local_streams()->count());
1509 pc_->RemoveStream(pc_->local_streams()->at(0));
1510 EXPECT_EQ(0u, pc_->local_streams()->count());
1511}
1512
deadbeefe1f9d832016-01-14 15:35:42 -08001513// Test for AddTrack and RemoveTrack methods.
1514// Tests that the created offer includes tracks we added,
1515// and that the RtpSenders are created correctly.
1516// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001517// Only tested with Plan B since Unified Plan is covered in more detail by tests
1518// in peerconnection_jsep_unittests.cc
1519TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001520 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001521 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001522 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001523 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001524 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001525 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1526 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001527 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001528 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001529 EXPECT_EQ("audio_track", audio_sender->id());
1530 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001531 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001532 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001533 EXPECT_EQ("video_track", video_sender->id());
1534 EXPECT_EQ(video_track, video_sender->track());
1535
1536 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001537 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001538 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001539
1540 const cricket::ContentInfo* audio_content =
1541 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001542 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001543 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001544
1545 const cricket::ContentInfo* video_content =
1546 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001547 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001548 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001549
Steve Antondb45ca82017-09-11 18:27:34 -07001550 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001551
1552 // Now try removing the tracks.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001553 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1554 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001555
1556 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001557 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001558
1559 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001560 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001561 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001562
1563 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001564 EXPECT_FALSE(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 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1570 // should return false.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001571 EXPECT_FALSE(pc_->RemoveTrackOrError(audio_sender).ok());
1572 EXPECT_FALSE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001573}
1574
1575// Test creating senders without a stream specified,
1576// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001577TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001578 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001579 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001580 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001581 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001582 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001583 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001584 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001585 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001586 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001587 EXPECT_EQ("audio_track", audio_sender->id());
1588 EXPECT_EQ(audio_track, audio_sender->track());
1589 EXPECT_EQ("video_track", video_sender->id());
1590 EXPECT_EQ(video_track, video_sender->track());
Florent Castelli15a38de2022-04-06 00:38:21 +02001591 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07001592 // If the ID is truly a random GUID, it should be infinitely unlikely they
1593 // will be the same.
1594 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1595 } else {
1596 // We allows creating tracks without stream ids under Unified Plan
1597 // semantics.
1598 EXPECT_EQ(0u, video_sender->stream_ids().size());
1599 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1600 }
deadbeefe1f9d832016-01-14 15:35:42 -08001601}
1602
Harald Alvestrand89061872018-01-02 14:08:34 +01001603// Test that we can call GetStats() after AddTrack but before connecting
1604// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001605TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001606 CreatePeerConnectionWithoutDtls();
1607 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001608 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001609 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001610 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001611 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1612 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001613 EXPECT_TRUE(DoGetStats(nullptr));
1614}
1615
Steve Anton36da6ff2018-02-16 16:04:20 -08001616TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001617 CreatePeerConnectionWithoutDtls();
1618 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001619 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001620 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001621 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001622 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001623 ASSERT_TRUE(audio_sender.ok());
1624 auto* audio_sender_proxy =
1625 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1626 audio_sender.value().get());
1627 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1628
Harald Alvestrandc72af932018-01-11 17:18:19 +01001629 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001630 ASSERT_TRUE(video_sender.ok());
1631 auto* video_sender_proxy =
1632 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1633 video_sender.value().get());
1634 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001635}
1636
Steve Anton36da6ff2018-02-16 16:04:20 -08001637// Don't run under Unified Plan since the stream API is not available.
1638TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001639 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001640 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001641 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001642 ASSERT_EQ(1u, senders.size());
1643 auto* sender_proxy =
1644 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1645 senders[0].get());
1646 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001647}
1648
Steve Anton36da6ff2018-02-16 16:04:20 -08001649TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001651 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 VerifyRemoteRtpHeaderExtensions();
1653}
1654
Steve Anton36da6ff2018-02-16 16:04:20 -08001655TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001656 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001657 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 CreateOfferAsLocalDescription();
1659 std::string offer;
1660 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1661 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001662 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663}
1664
Steve Anton36da6ff2018-02-16 16:04:20 -08001665TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001666 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001667 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668
1669 CreateOfferAsRemoteDescription();
1670 CreateAnswerAsLocalDescription();
1671
Seth Hampson845e8782018-03-02 11:34:10 -08001672 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673}
1674
Steve Anton36da6ff2018-02-16 16:04:20 -08001675TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001676 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001677 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678
1679 CreateOfferAsRemoteDescription();
1680 CreatePrAnswerAsLocalDescription();
1681 CreateAnswerAsLocalDescription();
1682
Seth Hampson845e8782018-03-02 11:34:10 -08001683 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
Steve Anton36da6ff2018-02-16 16:04:20 -08001686// Don't run under Unified Plan since the stream API is not available.
1687TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 InitiateCall();
1689 ASSERT_EQ(1u, pc_->remote_streams()->count());
1690 pc_->RemoveStream(pc_->local_streams()->at(0));
1691 CreateOfferReceiveAnswer();
1692 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001693 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 CreateOfferReceiveAnswer();
1695}
1696
1697// Tests that after negotiating an audio only call, the respondent can perform a
1698// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001699TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001700 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001701 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 CreateOfferAsRemoteDescription();
1703 CreateAnswerAsLocalDescription();
1704
1705 ASSERT_EQ(1u, pc_->remote_streams()->count());
1706 pc_->RemoveStream(pc_->local_streams()->at(0));
1707 CreateOfferReceiveAnswer();
1708 EXPECT_EQ(0u, pc_->remote_streams()->count());
1709}
1710
1711// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001712TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001713 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714
Steve Antonf1c6db12017-10-13 11:13:35 -07001715 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001717 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001718 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001719 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001720 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
1722 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001724 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001725 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726
Steve Antonf1c6db12017-10-13 11:13:35 -07001727 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001728 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729
Steve Antonf1c6db12017-10-13 11:13:35 -07001730 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731}
1732
deadbeefab9b2d12015-10-14 11:33:11 -07001733// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001735TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001736 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001738 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001739 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001740 EXPECT_TRUE(offer);
1741 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742
1743 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001744 AddAudioTrack("track_label", {kStreamId1});
1745 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746
1747 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001748 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749
1750 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001751 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001752 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753}
1754
1755// Test that we will get different SSRCs for each tracks in the offer and answer
1756// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001757TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001758 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001760 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1761 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762
1763 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001764 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001765 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 int audio_ssrc = 0;
1767 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001768 EXPECT_TRUE(
1769 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1770 EXPECT_TRUE(
1771 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 EXPECT_NE(audio_ssrc, video_ssrc);
1773
1774 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001775 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001776 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001777 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 audio_ssrc = 0;
1779 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001780 EXPECT_TRUE(
1781 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1782 EXPECT_TRUE(
1783 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 EXPECT_NE(audio_ssrc, video_ssrc);
1785}
1786
deadbeefeb459812015-12-15 19:24:43 -08001787// Test that it's possible to call AddTrack on a MediaStream after adding
1788// the stream to a PeerConnection.
1789// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001790// Don't run under Unified Plan since the stream API is not available.
1791TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001792 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001793 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001794 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001795 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1796
1797 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001798 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001799 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001800 stream->AddTrack(video_track.get());
1801
kwibergd1fe2812016-04-27 06:47:29 -07001802 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001803 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001804
1805 const cricket::MediaContentDescription* video_desc =
1806 cricket::GetFirstVideoContentDescription(offer->description());
1807 EXPECT_TRUE(video_desc != nullptr);
1808}
1809
1810// Test that it's possible to call RemoveTrack on a MediaStream after adding
1811// the stream to a PeerConnection.
1812// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001813// Don't run under Unified Plan since the stream API is not available.
1814TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001815 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001816 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001817 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001818 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1819
1820 // Remove the video track.
1821 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1822
kwibergd1fe2812016-04-27 06:47:29 -07001823 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001824 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001825
1826 const cricket::MediaContentDescription* video_desc =
1827 cricket::GetFirstVideoContentDescription(offer->description());
1828 EXPECT_TRUE(video_desc == nullptr);
1829}
1830
deadbeefbd7d8f72015-12-18 16:58:44 -08001831// Test creating a sender with a stream ID, and ensure the ID is populated
1832// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001833// Don't run under Unified Plan since the stream API is not available.
1834TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001835 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001836 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001837
kwibergd1fe2812016-04-27 06:47:29 -07001838 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001839 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001840
1841 const cricket::MediaContentDescription* video_desc =
1842 cricket::GetFirstVideoContentDescription(offer->description());
1843 ASSERT_TRUE(video_desc != nullptr);
1844 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001845 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001846}
1847
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001849TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001851 ASSERT_LT(0u, pc_->GetSenders().size());
1852 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001853 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001854 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 EXPECT_TRUE(DoGetStats(remote_audio));
1856
1857 // Remove the stream. Since we are sending to our selves the local
1858 // and the remote stream is the same.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001859 pc_->RemoveTrackOrError(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 // Do a re-negotiation.
1861 CreateOfferReceiveAnswer();
1862
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 // Test that we still can get statistics for the old track. Even if it is not
1864 // sent any longer.
1865 EXPECT_TRUE(DoGetStats(remote_audio));
1866}
1867
1868// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001869TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001871 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1872 ASSERT_TRUE(video_receiver);
1873 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874}
1875
1876// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001877TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001879 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 pc_factory_->CreateAudioTrack("unknown track", NULL));
1881 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1882}
1883
Steve Anton36da6ff2018-02-16 16:04:20 -08001884TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001885 CreatePeerConnectionWithoutDtls();
1886 EXPECT_TRUE(DoGetRTCStats());
1887 // Clearing stats cache is needed now, but should be temporary.
1888 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1889 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001890 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1891 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001892 EXPECT_TRUE(DoGetRTCStats());
1893 pc_->ClearStatsCache();
1894 CreateOfferReceiveAnswer();
1895 EXPECT_TRUE(DoGetRTCStats());
1896}
1897
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898// This tests that a SCTP data channel is returned using different
1899// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001900TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001901 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001902 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903
1904 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001905 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 pc_->CreateDataChannel("1", &config);
1907 EXPECT_TRUE(channel != NULL);
1908 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001909 EXPECT_TRUE(observer_.renegotiation_needed_);
1910 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911
1912 config.ordered = false;
1913 channel = pc_->CreateDataChannel("2", &config);
1914 EXPECT_TRUE(channel != NULL);
1915 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001916 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917
1918 config.ordered = true;
1919 config.maxRetransmits = 0;
1920 channel = pc_->CreateDataChannel("3", &config);
1921 EXPECT_TRUE(channel != NULL);
1922 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001923 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001925 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 config.maxRetransmitTime = 0;
1927 channel = pc_->CreateDataChannel("4", &config);
1928 EXPECT_TRUE(channel != NULL);
1929 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001930 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931}
1932
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001933// For backwards compatibility, we want people who "unset" maxRetransmits
1934// and maxRetransmitTime by setting them to -1 to get what they want.
1935TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1936 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001937 CreatePeerConnection(rtc_config);
1938
1939 webrtc::DataChannelInit config;
1940 config.maxRetransmitTime = -1;
1941 config.maxRetransmits = -1;
1942 rtc::scoped_refptr<DataChannelInterface> channel =
1943 pc_->CreateDataChannel("1", &config);
1944 EXPECT_TRUE(channel != NULL);
1945}
1946
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947// This tests that no data channel is returned if both maxRetransmits and
1948// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001949TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001951 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001952 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953
1954 std::string label = "test";
1955 webrtc::DataChannelInit config;
1956 config.maxRetransmits = 0;
1957 config.maxRetransmitTime = 0;
1958
zhihuang9763d562016-08-05 11:14:50 -07001959 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 pc_->CreateDataChannel(label, &config);
1961 EXPECT_TRUE(channel == NULL);
1962}
1963
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964// The test verifies that creating a SCTP data channel with an id already in use
1965// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001966TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001968 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001969 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
1971 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001972 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001974 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001975 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001976 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977 EXPECT_TRUE(channel != NULL);
1978 EXPECT_EQ(1, channel->id());
1979
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 channel = pc_->CreateDataChannel("x", &config);
1981 EXPECT_TRUE(channel == NULL);
1982
1983 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001984 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 channel = pc_->CreateDataChannel("max", &config);
1986 EXPECT_TRUE(channel != NULL);
1987 EXPECT_EQ(config.id, channel->id());
1988
1989 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001990 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 channel = pc_->CreateDataChannel("x", &config);
1992 EXPECT_TRUE(channel == NULL);
1993}
1994
deadbeefab9b2d12015-10-14 11:33:11 -07001995// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08001996TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001997 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001998 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07001999
2000 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002001 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002002 pc_->CreateDataChannel(label, nullptr);
2003 EXPECT_NE(channel, nullptr);
2004
zhihuang9763d562016-08-05 11:14:50 -07002005 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002006 pc_->CreateDataChannel(label, nullptr);
2007 EXPECT_NE(dup_channel, nullptr);
2008}
2009
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002010#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002011// This tests that SCTP data channels can be rejected in an answer.
2012TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2013#else
2014TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2015#endif
2016{
Niels Möllerf06f9232018-08-07 12:32:18 +02002017 RTCConfiguration rtc_config;
2018 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002019
2020 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2021 pc_->CreateDataChannel("offer_channel", NULL));
2022
2023 CreateOfferAsLocalDescription();
2024
2025 // Create an answer where the m-line for data channels are rejected.
2026 std::string sdp;
2027 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2028 std::unique_ptr<SessionDescriptionInterface> answer(
2029 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2030 ASSERT_TRUE(answer);
2031 cricket::ContentInfo* data_info =
2032 cricket::GetFirstDataContent(answer->description());
2033 data_info->rejected = true;
2034
2035 DoSetRemoteDescription(std::move(answer));
2036 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2037}
2038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039// Test that we can create a session description from an SDP string from
2040// FireFox, use it as a remote session description, generate an answer and use
2041// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002042TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002043 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002044 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002045 AddAudioTrack("audio_label");
2046 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002047 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002048 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002049 webrtc::kFireFoxSdpOffer, nullptr));
2050 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 CreateAnswerAsLocalDescription();
2052 ASSERT_TRUE(pc_->local_description() != NULL);
2053 ASSERT_TRUE(pc_->remote_description() != NULL);
2054
2055 const cricket::ContentInfo* content =
2056 cricket::GetFirstAudioContent(pc_->local_description()->description());
2057 ASSERT_TRUE(content != NULL);
2058 EXPECT_FALSE(content->rejected);
2059
2060 content =
2061 cricket::GetFirstVideoContent(pc_->local_description()->description());
2062 ASSERT_TRUE(content != NULL);
2063 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002064#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 content =
2066 cricket::GetFirstDataContent(pc_->local_description()->description());
2067 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002068 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002069#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070}
2071
Harald Alvestrand0d018412021-11-04 13:52:31 +00002072// Test that fallback from DTLS to SDES is not supported.
2073// The fallback was previously supported but was removed to simplify the code
2074// and because it's non-standard.
2075TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002076 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002077 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002078 // Wait for fake certificate to be generated. Previously, this is what caused
2079 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002080 AddAudioTrack("audio_label");
2081 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002082 ASSERT_NE(nullptr, fake_certificate_generator_);
2083 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2084 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002085 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002086 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2087 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002088 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002089}
2090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091// Test that we can create an audio only offer and receive an answer with a
2092// limited set of audio codecs and receive an updated offer with more audio
2093// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002094TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002095 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002096 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 CreateOfferAsLocalDescription();
2098
Florent Castelli15a38de2022-04-06 00:38:21 +02002099 const char* answer_sdp = (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
2100 ? webrtc::kAudioSdpPlanB
2101 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002102 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002103 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002104 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105
Steve Anton36da6ff2018-02-16 16:04:20 -08002106 const char* reoffer_sdp =
Florent Castelli15a38de2022-04-06 00:38:21 +02002107 (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
Steve Anton36da6ff2018-02-16 16:04:20 -08002108 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2109 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002110 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002111 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002112 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 CreateAnswerAsLocalDescription();
2114}
2115
deadbeefc80741f2015-10-22 13:14:45 -07002116// Test that if we're receiving (but not sending) a track, subsequent offers
2117// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002118TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002119 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002120 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002121 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002122 CreateAnswerAsLocalDescription();
2123
2124 // At this point we should be receiving stream 1, but not sending anything.
2125 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002126 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002127 DoCreateOffer(&offer, nullptr);
2128
2129 const cricket::ContentInfo* video_content =
2130 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002131 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2132 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002133
2134 const cricket::ContentInfo* audio_content =
2135 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002136 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2137 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002138}
2139
2140// Test that if we're receiving (but not sending) a track, and the
2141// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2142// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002143TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002144 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002145 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002146 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002147 CreateAnswerAsLocalDescription();
2148
2149 // At this point we should be receiving stream 1, but not sending anything.
2150 // A new offer would be recvonly, but we'll set the "no receive" constraints
2151 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002152 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002153 RTCOfferAnswerOptions options;
2154 options.offer_to_receive_audio = 0;
2155 options.offer_to_receive_video = 0;
2156 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002157
2158 const cricket::ContentInfo* video_content =
2159 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002160 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2161 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002162
2163 const cricket::ContentInfo* audio_content =
2164 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002165 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2166 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002167}
2168
deadbeef653b8e02015-11-11 12:55:10 -08002169// Test that we can use SetConfiguration to change the ICE servers of the
2170// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002171TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002172 CreatePeerConnection();
2173
Steve Anton36da6ff2018-02-16 16:04:20 -08002174 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002175 PeerConnectionInterface::IceServer server;
2176 server.uri = "stun:test_hostname";
2177 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002178 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002179
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002180 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2181 EXPECT_EQ("test_hostname",
2182 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002183}
2184
Steve Anton36da6ff2018-02-16 16:04:20 -08002185TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002186 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002187 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002188 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002189 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002190 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2191}
2192
Steve Anton36da6ff2018-02-16 16:04:20 -08002193TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002194 PeerConnectionInterface::RTCConfiguration config;
2195 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002196 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002197 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002198 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002199
2200 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002201 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002202 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2203 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002204}
2205
skvladd1f5fda2017-02-03 16:54:05 -08002206// Test that the ice check interval can be changed. This does not verify that
2207// the setting makes it all the way to P2PTransportChannel, as that would
2208// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002209TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002210 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002211 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002212 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002213 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002214 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002215 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002216 config = pc_->GetConfiguration();
2217 EXPECT_EQ(config.ice_check_min_interval, 100);
2218}
2219
2220TEST_P(PeerConnectionInterfaceTest,
2221 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2222 PeerConnectionInterface::RTCConfiguration config;
2223 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2224 CreatePeerConnection(config);
2225 config = pc_->GetConfiguration();
2226 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2227
2228 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002229 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002230 config = pc_->GetConfiguration();
2231 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002232}
2233
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002234// Test that when SetConfiguration changes both the pool size and other
2235// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002236TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002237 SetConfigurationCreatesPooledSessionCorrectly) {
2238 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002239 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002240 config.ice_candidate_pool_size = 1;
2241 PeerConnectionInterface::IceServer server;
2242 server.uri = kStunAddressOnly;
2243 config.servers.push_back(server);
2244 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002245 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002246
2247 const cricket::FakePortAllocatorSession* session =
2248 static_cast<const cricket::FakePortAllocatorSession*>(
2249 port_allocator_->GetPooledSession());
2250 ASSERT_NE(nullptr, session);
2251 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002252}
2253
deadbeef293e9262017-01-11 12:28:30 -08002254// Test that after SetLocalDescription, changing the pool size is not allowed,
2255// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002256TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002257 CantChangePoolSizeAfterSetLocalDescription) {
2258 CreatePeerConnection();
2259 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002260 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002261 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002262 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002263
2264 // Set remote offer; can still change pool size at this point.
2265 CreateOfferAsRemoteDescription();
2266 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002267 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002268
2269 // Set local answer; now it's too late.
2270 CreateAnswerAsLocalDescription();
2271 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002272 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002273 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2274}
2275
deadbeef42a42632017-03-10 15:18:00 -08002276// Test that after setting an answer, extra pooled sessions are discarded. The
2277// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002278TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002279 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2280 CreatePeerConnection();
2281
2282 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002283 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002284 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002285 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002286
2287 // Do offer/answer.
2288 CreateOfferAsRemoteDescription();
2289 CreateAnswerAsLocalDescription();
2290
2291 // Expect no pooled sessions to be left.
2292 const cricket::PortAllocatorSession* session =
2293 port_allocator_->GetPooledSession();
2294 EXPECT_EQ(nullptr, session);
2295}
2296
2297// After Close is called, pooled candidates should be discarded so as to not
2298// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002299TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002300 CreatePeerConnection();
2301
Steve Anton36da6ff2018-02-16 16:04:20 -08002302 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002303 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002304 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002305 pc_->Close();
2306
2307 // Expect no pooled sessions to be left.
2308 const cricket::PortAllocatorSession* session =
2309 port_allocator_->GetPooledSession();
2310 EXPECT_EQ(nullptr, session);
2311}
2312
deadbeef293e9262017-01-11 12:28:30 -08002313// Test that SetConfiguration returns an invalid modification error if
2314// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002315TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002316 SetConfigurationReturnsInvalidModificationError) {
2317 PeerConnectionInterface::RTCConfiguration config;
2318 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2319 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2320 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002321 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002322
Steve Anton36da6ff2018-02-16 16:04:20 -08002323 PeerConnectionInterface::RTCConfiguration modified_config =
2324 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002325 modified_config.bundle_policy =
2326 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002327 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002328 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2329
Steve Anton36da6ff2018-02-16 16:04:20 -08002330 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002331 modified_config.rtcp_mux_policy =
2332 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002333 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002334 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2335
Steve Anton36da6ff2018-02-16 16:04:20 -08002336 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002337 modified_config.continual_gathering_policy =
2338 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002339 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002340 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2341}
2342
2343// Test that SetConfiguration returns a range error if the candidate pool size
2344// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002345TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002346 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2347 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002348 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002349 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002350
2351 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002352 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002353 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2354
2355 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002356 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002357 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2358}
2359
2360// Test that SetConfiguration returns a syntax error if parsing an ICE server
2361// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002362TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002363 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2364 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002365 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002366 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002367
2368 PeerConnectionInterface::IceServer bad_server;
2369 bad_server.uri = "stunn:www.example.com";
2370 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002371 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002372 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2373}
2374
2375// Test that SetConfiguration returns an invalid parameter error if a TURN
2376// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002377TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002378 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2379 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002380 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002381 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002382
2383 PeerConnectionInterface::IceServer bad_server;
2384 bad_server.uri = "turn:www.example.com";
2385 // Missing password.
2386 bad_server.username = "foo";
2387 config.servers.push_back(bad_server);
2388 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002389 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2390 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002391}
2392
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393// Test that PeerConnection::Close changes the states to closed and all remote
2394// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002395TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396 // Initialize a PeerConnection and negotiate local and remote session
2397 // description.
2398 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002399
2400 // With Plan B, verify the stream count. The analog with Unified Plan is the
2401 // RtpTransceiver count.
Florent Castelli15a38de2022-04-06 00:38:21 +02002402 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002403 ASSERT_EQ(1u, pc_->local_streams()->count());
2404 ASSERT_EQ(1u, pc_->remote_streams()->count());
2405 } else {
2406 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2407 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408
2409 pc_->Close();
2410
2411 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2412 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2413 pc_->ice_connection_state());
2414 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2415 pc_->ice_gathering_state());
2416
Florent Castelli15a38de2022-04-06 00:38:21 +02002417 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002418 EXPECT_EQ(1u, pc_->local_streams()->count());
2419 EXPECT_EQ(1u, pc_->remote_streams()->count());
2420 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002421 // Verify that the RtpTransceivers are still returned.
2422 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002423 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424
Steve Anton36da6ff2018-02-16 16:04:20 -08002425 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002426 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Florent Castelli15a38de2022-04-06 00:38:21 +02002427 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002428 ASSERT_TRUE(audio_receiver);
2429 ASSERT_TRUE(video_receiver);
2430 // Track state may be updated asynchronously.
2431 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2432 audio_receiver->track()->state(), kTimeout);
2433 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2434 video_receiver->track()->state(), kTimeout);
2435 } else {
2436 ASSERT_FALSE(audio_receiver);
2437 ASSERT_FALSE(video_receiver);
2438 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439}
2440
2441// Test that PeerConnection methods fails gracefully after
2442// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002443// Don't run under Unified Plan since the stream API is not available.
2444TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002445 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002446 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 CreateOfferAsRemoteDescription();
2448 CreateAnswerAsLocalDescription();
2449
2450 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002451 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2452 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453
2454 pc_->Close();
2455
2456 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002457 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2460
2461 EXPECT_TRUE(pc_->local_description() != NULL);
2462 EXPECT_TRUE(pc_->remote_description() != NULL);
2463
kwibergd1fe2812016-04-27 06:47:29 -07002464 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002465 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002466 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002467 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468
2469 std::string sdp;
2470 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002471 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002472 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002473 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474
2475 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002476 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002477 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002478 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479}
2480
2481// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002482TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 InitiateCall();
2484 pc_->Close();
2485 DoGetStats(NULL);
2486}
deadbeefab9b2d12015-10-14 11:33:11 -07002487
2488// NOTE: The series of tests below come from what used to be
2489// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2490// setting a remote or local description has the expected effects.
2491
2492// This test verifies that the remote MediaStreams corresponding to a received
2493// SDP string is created. In this test the two separate MediaStreams are
2494// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002495TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002496 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002497 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002498 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002499
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002500 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002501 EXPECT_TRUE(
2502 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2503 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2504 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2505
2506 // Create a session description based on another SDP with another
2507 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002508 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002509
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002510 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002511 EXPECT_TRUE(
2512 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2513}
2514
2515// This test verifies that when remote tracks are added/removed from SDP, the
2516// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002517// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2518// specific behavior.
2519TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002520 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002521 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002522 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002523 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002524 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002525 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002526 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2527 reference_collection_));
2528
2529 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002530 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002531 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002532 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002533 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2534 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002535 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002536 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2537 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002538 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002539 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2540 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002541
2542 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002543 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002544 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002545 MockTrackObserver audio_track_observer(audio_track2);
2546 MockTrackObserver video_track_observer(video_track2);
2547
2548 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2549 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002550 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002551 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2552 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002553 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002554 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002555 audio_track2->state(), kTimeout);
2556 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2557 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002558}
2559
2560// This tests that remote tracks are ended if a local session description is set
2561// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002562TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002563 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002564 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002565 // First create and set a remote offer, then reject its video content in our
2566 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002567 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2568 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2569 ASSERT_TRUE(audio_receiver);
2570 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2571 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002572
Steve Anton36da6ff2018-02-16 16:04:20 -08002573 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2574 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002575 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002576 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2577 video_receiver->track();
2578 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002579
kwibergd1fe2812016-04-27 06:47:29 -07002580 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002581 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002582 cricket::ContentInfo* video_info =
2583 local_answer->description()->GetContentByName("video");
2584 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002585 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002586 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2587 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002588
2589 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002590 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002591 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002592 video_info = local_offer->description()->GetContentByName("video");
2593 ASSERT_TRUE(video_info != nullptr);
2594 video_info->rejected = true;
2595 cricket::ContentInfo* audio_info =
2596 local_offer->description()->GetContentByName("audio");
2597 ASSERT_TRUE(audio_info != nullptr);
2598 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002599 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002600 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002601 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2602 kTimeout);
2603 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2604 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002605}
2606
2607// This tests that we won't crash if the remote track has been removed outside
2608// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002609// Don't run under Unified Plan since the stream API is not available.
2610TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002611 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002612 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002613 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002614 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2615 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2616 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2617
kwibergd1fe2812016-04-27 06:47:29 -07002618 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002619 webrtc::CreateSessionDescription(SdpType::kAnswer,
2620 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002621 cricket::ContentInfo* video_info =
2622 local_answer->description()->GetContentByName("video");
2623 video_info->rejected = true;
2624 cricket::ContentInfo* audio_info =
2625 local_answer->description()->GetContentByName("audio");
2626 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002627 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002628
2629 // No crash is a pass.
2630}
2631
deadbeef5e97fb52015-10-15 12:49:08 -07002632// This tests that if a recvonly remote description is set, no remote streams
2633// will be created, even if the description contains SSRCs/MSIDs.
2634// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002635TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002636 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002637 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002638
Steve Anton36da6ff2018-02-16 16:04:20 -08002639 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002640 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002641 CreateAndSetRemoteOffer(recvonly_offer);
2642
2643 EXPECT_EQ(0u, observer_.remote_streams()->count());
2644}
2645
deadbeefab9b2d12015-10-14 11:33:11 -07002646// This tests that a default MediaStream is created if a remote session
2647// description doesn't contain any streams and no MSID support.
2648// It also tests that the default stream is updated if a video m-line is added
2649// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002650// Don't run under Unified Plan since this behavior is Plan B specific.
2651TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002652 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002653 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002654 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2655
2656 ASSERT_EQ(1u, observer_.remote_streams()->count());
2657 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2658
2659 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2660 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002661 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002662
2663 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2664 ASSERT_EQ(1u, observer_.remote_streams()->count());
2665 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2666 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002667 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2668 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002669 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2670 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002671 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2672 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002673}
2674
2675// This tests that a default MediaStream is created if a remote session
2676// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002677// Don't run under Unified Plan since this behavior is Plan B specific.
2678TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002679 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002680 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002681 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002682 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2683
2684 ASSERT_EQ(1u, observer_.remote_streams()->count());
2685 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2686
2687 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2688 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002689 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002690}
2691
2692// This tests that it won't crash when PeerConnection tries to remove
2693// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002694// Don't run under Unified Plan since this behavior is Plan B specific.
2695TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002696 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002697 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002698 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002699 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2700 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2701 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2702
2703 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2704
2705 // No crash is a pass.
2706}
2707
2708// This tests that a default MediaStream is created if the remote session
2709// description doesn't contain any streams and don't contain an indication if
2710// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002711// Don't run under Unified Plan since this behavior is Plan B specific.
2712TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002713 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002714 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002715 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002716 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2717
2718 ASSERT_EQ(1u, observer_.remote_streams()->count());
2719 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2720 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2721 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2722}
2723
2724// This tests that a default MediaStream is not created if the remote session
2725// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002726// Don't run under Unified Plan since this behavior is Plan B specific.
2727TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002728 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002729 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002730 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2731 EXPECT_EQ(0u, observer_.remote_streams()->count());
2732}
2733
deadbeefbda7e0b2015-12-08 17:13:40 -08002734// This tests that when setting a new description, the old default tracks are
2735// not destroyed and recreated.
2736// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002737// Don't run under Unified Plan since this behavior is Plan B specific.
2738TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002739 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002740 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002741 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002742 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2743
2744 ASSERT_EQ(1u, observer_.remote_streams()->count());
2745 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2746 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2747
2748 // Set the track to "disabled", then set a new description and ensure the
2749 // track is still disabled, which ensures it hasn't been recreated.
2750 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2751 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2752 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2753 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2754}
2755
deadbeefab9b2d12015-10-14 11:33:11 -07002756// This tests that a default MediaStream is not created if a remote session
2757// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002758// Don't run under Unified Plan since this behavior is Plan B specific.
2759TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002760 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002761 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002762 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002763 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002764 EXPECT_TRUE(
2765 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2766
2767 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2768 EXPECT_EQ(0u, observer_.remote_streams()->count());
2769}
2770
Seth Hampson5897a6e2018-04-03 11:16:33 -07002771// This tests that a default MediaStream is created if a remote SDP comes from
2772// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2773TEST_F(PeerConnectionInterfaceTestPlanB,
2774 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002775 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002776 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002777 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2778 // Add a=msid lines to simulate a Unified Plan endpoint that only
2779 // signals stream IDs with a=msid lines.
2780 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2781
2782 CreateAndSetRemoteOffer(sdp_string);
2783
2784 ASSERT_EQ(1u, observer_.remote_streams()->count());
2785 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2786 EXPECT_EQ("default", remote_stream->id());
2787 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2788}
2789
Seth Hampson5b4f0752018-04-02 16:31:36 -07002790// This tests that when a Plan B endpoint receives an SDP that signals no media
2791// stream IDs indicated by the special character "-" in the a=msid line, that
2792// a default stream ID will be used for the MediaStream ID. This can occur
2793// when a Unified Plan endpoint signals no media stream IDs, but signals both
2794// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2795TEST_F(PeerConnectionInterfaceTestPlanB,
2796 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002797 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002798 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002799 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2800 // the sender's stream ID will be interpreted as no stream IDs.
2801 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2802 sdp_string.append("a=msid:- audiotrack0\n");
2803
2804 CreateAndSetRemoteOffer(sdp_string);
2805
2806 ASSERT_EQ(1u, observer_.remote_streams()->count());
2807 // Because SSRCs are signaled the track ID will be what was signaled in the
2808 // a=msid line.
2809 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2810 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2811 EXPECT_EQ("default", remote_stream->id());
2812 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002813
2814 // Previously a bug ocurred when setting the remote description a second time.
2815 // This is because we checked equality of the remote StreamParams stream ID
2816 // (empty), and the previously set stream ID for the remote sender
2817 // ("default"). This cause a track to be removed, then added, when really
2818 // nothing should occur because it is the same track.
2819 CreateAndSetRemoteOffer(sdp_string);
2820 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2821 EXPECT_EQ(1u, observer_.add_track_events_.size());
2822 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2823 remote_stream = observer_.remote_streams()->at(0);
2824 EXPECT_EQ("default", remote_stream->id());
2825 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002826}
2827
deadbeefab9b2d12015-10-14 11:33:11 -07002828// This tests that an RtpSender is created when the local description is set
2829// after adding a local stream.
2830// TODO(deadbeef): This test and the one below it need to be updated when
2831// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002832// Don't run under Unified Plan since this behavior is Plan B specific.
2833TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002834 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002835 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002836
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002837 // Create an offer with 1 stream with 2 tracks of each type.
2838 rtc::scoped_refptr<StreamCollection> stream_collection =
2839 CreateStreamCollection(1, 2);
2840 pc_->AddStream(stream_collection->at(0));
2841 std::unique_ptr<SessionDescriptionInterface> offer;
2842 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002843 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002844
deadbeefab9b2d12015-10-14 11:33:11 -07002845 auto senders = pc_->GetSenders();
2846 EXPECT_EQ(4u, senders.size());
2847 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2848 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2849 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2850 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2851
2852 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002853 pc_->RemoveStream(stream_collection->at(0));
2854 stream_collection = CreateStreamCollection(1, 1);
2855 pc_->AddStream(stream_collection->at(0));
2856 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002857 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002858
deadbeefab9b2d12015-10-14 11:33:11 -07002859 senders = pc_->GetSenders();
2860 EXPECT_EQ(2u, senders.size());
2861 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2862 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2863 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2864 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2865}
2866
2867// This tests that an RtpSender is created when the local description is set
2868// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002869// Don't run under Unified Plan since this behavior is Plan B specific.
2870TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002871 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002872 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002873 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002874
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002875 rtc::scoped_refptr<StreamCollection> stream_collection =
2876 CreateStreamCollection(1, 2);
2877 // Add a stream to create the offer, but remove it afterwards.
2878 pc_->AddStream(stream_collection->at(0));
2879 std::unique_ptr<SessionDescriptionInterface> offer;
2880 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2881 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002882
Steve Antondb45ca82017-09-11 18:27:34 -07002883 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002884 auto senders = pc_->GetSenders();
2885 EXPECT_EQ(0u, senders.size());
2886
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002887 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002888 senders = pc_->GetSenders();
2889 EXPECT_EQ(4u, senders.size());
2890 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2891 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2892 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2893 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2894}
2895
2896// This tests that the expected behavior occurs if the SSRC on a local track is
2897// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002898TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002899 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002900 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002901 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002902
Steve Anton36da6ff2018-02-16 16:04:20 -08002903 AddAudioTrack(kAudioTracks[0]);
2904 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002905 std::unique_ptr<SessionDescriptionInterface> offer;
2906 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2907 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002908 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2909 webrtc::CreateSessionDescription(
2910 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002911 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002912 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002913
deadbeefab9b2d12015-10-14 11:33:11 -07002914 auto senders = pc_->GetSenders();
2915 EXPECT_EQ(2u, senders.size());
2916 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2917 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2918
2919 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002920 cricket::MediaContentDescription* desc =
2921 cricket::GetFirstAudioContentDescription(modified_offer->description());
2922 ASSERT_TRUE(desc != NULL);
2923 for (StreamParams& stream : desc->mutable_streams()) {
2924 for (unsigned int& ssrc : stream.ssrcs) {
2925 ++ssrc;
2926 }
2927 }
deadbeefab9b2d12015-10-14 11:33:11 -07002928
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002929 desc =
2930 cricket::GetFirstVideoContentDescription(modified_offer->description());
2931 ASSERT_TRUE(desc != NULL);
2932 for (StreamParams& stream : desc->mutable_streams()) {
2933 for (unsigned int& ssrc : stream.ssrcs) {
2934 ++ssrc;
2935 }
2936 }
2937
Steve Antondb45ca82017-09-11 18:27:34 -07002938 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002939 senders = pc_->GetSenders();
2940 EXPECT_EQ(2u, senders.size());
2941 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2942 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2943 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2944 // changed.
2945}
2946
2947// This tests that the expected behavior occurs if a new session description is
2948// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002949// Don't run under Unified Plan since the stream API is not available.
2950TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002951 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002952 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002953 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002954
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002955 rtc::scoped_refptr<StreamCollection> stream_collection =
2956 CreateStreamCollection(2, 1);
2957 pc_->AddStream(stream_collection->at(0));
2958 std::unique_ptr<SessionDescriptionInterface> offer;
2959 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002960 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002961
deadbeefab9b2d12015-10-14 11:33:11 -07002962 auto senders = pc_->GetSenders();
2963 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002964 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2965 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002966
2967 // Add a new MediaStream but with the same tracks as in the first stream.
2968 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2969 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002970 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2971 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002972 pc_->AddStream(stream_1);
2973
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002974 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002975 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002976
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002977 auto new_senders = pc_->GetSenders();
2978 // Should be the same senders as before, but with updated stream id.
2979 // Note that this behavior is subject to change in the future.
2980 // We may decide the PC should ignore existing tracks in AddStream.
2981 EXPECT_EQ(senders, new_senders);
2982 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2983 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002984}
2985
zhihuang81c3a032016-11-17 12:06:24 -08002986// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002987TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002988 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002989 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08002990 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2991 EXPECT_EQ(observer_.num_added_tracks_, 1);
2992 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2993
2994 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08002995 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07002996 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08002997 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2998}
2999
deadbeefd1a38b52016-12-10 13:15:33 -08003000// Test that when SetConfiguration is called and the configuration is
3001// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003002TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003003 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003004 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003005 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003006 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003007 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003008 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3009 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003010
3011 // Do initial offer/answer so there's something to restart.
3012 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003013 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003014
3015 // Grab the ufrags.
3016 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3017
3018 // Change ICE policy, which should trigger an ICE restart on the next offer.
3019 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003020 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003021 CreateOfferAsLocalDescription();
3022
3023 // Grab the new ufrags.
3024 std::vector<std::string> subsequent_ufrags =
3025 GetUfrags(pc_->local_description());
3026
3027 // Sanity check.
3028 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3029 // Check that each ufrag is different.
3030 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3031 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3032 }
3033}
3034
3035// Test that when SetConfiguration is called and the configuration *isn't*
3036// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003037TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003038 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003039 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003040 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003041 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003042 config = pc_->GetConfiguration();
3043 AddAudioTrack(kAudioTracks[0]);
3044 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003045
3046 // Do initial offer/answer so there's something to restart.
3047 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003048 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003049
3050 // Grab the ufrags.
3051 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3052
3053 // Call SetConfiguration with a config identical to what the PC was
3054 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003055 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003056 CreateOfferAsLocalDescription();
3057
3058 // Grab the new ufrags.
3059 std::vector<std::string> subsequent_ufrags =
3060 GetUfrags(pc_->local_description());
3061
3062 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3063}
3064
3065// Test for a weird corner case scenario:
3066// 1. Audio/video session established.
3067// 2. SetConfiguration changes ICE config; ICE restart needed.
3068// 3. ICE restart initiated by remote peer, but only for one m= section.
3069// 4. Next createOffer should initiate an ICE restart, but only for the other
3070// m= section; it would be pointless to do an ICE restart for the m= section
3071// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003072TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003073 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003074 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003075 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003076 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003077 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003078 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3079 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003080
3081 // Do initial offer/answer so there's something to restart.
3082 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003083 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003084
3085 // Change ICE policy, which should set the "needs-ice-restart" flag.
3086 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003087 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003088
3089 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003090 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003091 webrtc::CreateSessionDescription(SdpType::kOffer,
3092 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003093 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003094 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3095 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003096 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003097 CreateAnswerAsLocalDescription();
3098
3099 // Grab the ufrags.
3100 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003101 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003102
3103 // Create offer and grab the new ufrags.
3104 CreateOfferAsLocalDescription();
3105 std::vector<std::string> subsequent_ufrags =
3106 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003107 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003108
3109 // Ensure that only the ufrag for the second m= section changed.
3110 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3111 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3112}
3113
deadbeeffe4a8a42016-12-20 17:56:17 -08003114// Tests that the methods to return current/pending descriptions work as
3115// expected at different points in the offer/answer exchange. This test does
3116// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003117TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003118 // This disables DTLS so we can apply an answer to ourselves.
3119 CreatePeerConnection();
3120
3121 // Create initial local offer and get SDP (which will also be used as
3122 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003123 std::unique_ptr<SessionDescriptionInterface> local_offer;
3124 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003125 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003126 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003127
3128 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003129 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3130 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3131 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003132 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3133 EXPECT_EQ(nullptr, pc_->current_local_description());
3134 EXPECT_EQ(nullptr, pc_->current_remote_description());
3135
3136 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003137 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003138 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003139 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3140 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3141 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3142 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003143 EXPECT_EQ(nullptr, pc_->current_local_description());
3144 EXPECT_EQ(nullptr, pc_->current_remote_description());
3145
3146 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003147 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003148 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003149 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3150 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003151 EXPECT_EQ(nullptr, pc_->pending_local_description());
3152 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003153 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3154 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003155
3156 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003157 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003158 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003159 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3160 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3161 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003162 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003163 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3164 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003165
3166 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003167 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003168 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003169 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3170 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3171 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3172 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3173 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3174 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003175
3176 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003177 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003178 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003179 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3180 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003181 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3182 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003183 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3184 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003185}
3186
zhihuang77985012017-02-07 15:45:16 -08003187// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3188// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003189// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003190// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003191TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003192 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3193 CreatePeerConnection();
3194 // The RtcEventLog will be reset when the PeerConnection is closed.
3195 pc_->Close();
3196
Niels Möllerdec9f742019-06-03 15:25:20 +02003197 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003198 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003199 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003200 pc_->StopRtcEventLog();
3201}
3202
deadbeef30952b42017-04-21 02:41:29 -07003203// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003204TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003205 CreatePeerConnection();
3206
3207 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003208 RTCOfferAnswerOptions options;
3209 options.offer_to_receive_audio = 1;
3210 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003211 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003212 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003213 cricket::SessionDescription* desc = offer->description();
3214 ASSERT_EQ(2u, desc->transport_infos().size());
3215 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3216 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3217
3218 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003219 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003220 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003221 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3222 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003223 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003224 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003225 desc = answer->description();
3226 ASSERT_EQ(2u, desc->transport_infos().size());
3227 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3228 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3229}
3230
deadbeef1dcb1642017-03-29 21:08:16 -07003231// Test that ICE renomination isn't offered if it's not enabled in the PC's
3232// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003233TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003234 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003235 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003236 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003237 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003238 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003239
3240 std::unique_ptr<SessionDescriptionInterface> offer;
3241 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3242 cricket::SessionDescription* desc = offer->description();
3243 EXPECT_EQ(1u, desc->transport_infos().size());
3244 EXPECT_FALSE(
3245 desc->transport_infos()[0].description.GetIceParameters().renomination);
3246}
3247
3248// Test that the ICE renomination option is present in generated offers/answers
3249// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003250TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003251 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003252 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003253 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003254 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003255 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003256
3257 std::unique_ptr<SessionDescriptionInterface> offer;
3258 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3259 cricket::SessionDescription* desc = offer->description();
3260 EXPECT_EQ(1u, desc->transport_infos().size());
3261 EXPECT_TRUE(
3262 desc->transport_infos()[0].description.GetIceParameters().renomination);
3263
3264 // Set the offer as a remote description, then create an answer and ensure it
3265 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003266 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003267 std::unique_ptr<SessionDescriptionInterface> answer;
3268 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3269 desc = answer->description();
3270 EXPECT_EQ(1u, desc->transport_infos().size());
3271 EXPECT_TRUE(
3272 desc->transport_infos()[0].description.GetIceParameters().renomination);
3273}
3274
3275// Test that if CreateOffer is called with the deprecated "offer to receive
3276// audio/video" constraints, they're processed and result in an offer with
3277// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003278TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003279 CreatePeerConnection();
3280
Niels Möllerf06f9232018-08-07 12:32:18 +02003281 RTCOfferAnswerOptions options;
3282 options.offer_to_receive_audio = 1;
3283 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003284 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003285 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003286
3287 cricket::SessionDescription* desc = offer->description();
3288 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3289 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3290 ASSERT_NE(nullptr, audio);
3291 ASSERT_NE(nullptr, video);
3292 EXPECT_FALSE(audio->rejected);
3293 EXPECT_FALSE(video->rejected);
3294}
3295
3296// Test that if CreateAnswer is called with the deprecated "offer to receive
3297// audio/video" constraints, they're processed and can be used to reject an
3298// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003299// Don't run under Unified Plan since this behavior is not supported.
3300TEST_F(PeerConnectionInterfaceTestPlanB,
3301 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003302 CreatePeerConnection();
3303
3304 // First, create an offer with audio/video and apply it as a remote
3305 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003306 RTCOfferAnswerOptions options;
3307 options.offer_to_receive_audio = 1;
3308 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003309 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003310 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003311 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003312
3313 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003314 options.offer_to_receive_audio = 0;
3315 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003316 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003317 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003318
3319 cricket::SessionDescription* desc = answer->description();
3320 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3321 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3322 ASSERT_NE(nullptr, audio);
3323 ASSERT_NE(nullptr, video);
3324 EXPECT_TRUE(audio->rejected);
3325 EXPECT_TRUE(video->rejected);
3326}
3327
deadbeef1dcb1642017-03-29 21:08:16 -07003328// Test that negotiation can succeed with a data channel only, and with the max
3329// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003330#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003331TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3332#else
3333TEST_P(PeerConnectionInterfaceTest,
3334 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003335#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003336 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003337 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003338 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003339 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003340
3341 // First, create an offer with only a data channel and apply it as a remote
3342 // description.
3343 pc_->CreateDataChannel("test", nullptr);
3344 std::unique_ptr<SessionDescriptionInterface> offer;
3345 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003346 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003347
3348 // Create and set answer as well.
3349 std::unique_ptr<SessionDescriptionInterface> answer;
3350 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003351 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003352}
3353
Steve Anton36da6ff2018-02-16 16:04:20 -08003354TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003355 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003356 BitrateSettings bitrate;
3357 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003358 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3359}
3360
Steve Anton36da6ff2018-02-16 16:04:20 -08003361TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003362 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003363 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003364 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003365 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3366}
3367
Steve Anton36da6ff2018-02-16 16:04:20 -08003368TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003369 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003370 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003371 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003372 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003373 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3374}
3375
Steve Anton36da6ff2018-02-16 16:04:20 -08003376TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003377 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003378 BitrateSettings bitrate;
3379 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003380 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3381}
3382
Steve Anton36da6ff2018-02-16 16:04:20 -08003383TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003384 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003385 BitrateSettings bitrate;
3386 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003387 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003388 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3389}
3390
Steve Anton36da6ff2018-02-16 16:04:20 -08003391TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003392 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003393 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003394 bitrate.min_bitrate_bps = 10;
3395 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003396 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3397}
3398
Steve Anton36da6ff2018-02-16 16:04:20 -08003399TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003400 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003401 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003402 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003403 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3404}
3405
Niels Möller0c4f7be2018-05-07 14:01:37 +02003406// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003407// by Call's BitrateConstraints, which comes from the SDP or a default value.
3408// This test checks that a call to SetBitrate with a current bitrate that will
3409// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003410TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003411 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003412 BitrateSettings bitrate;
3413 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003414 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3415}
3416
zhihuang1c378ed2017-08-17 14:10:50 -07003417// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003418TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003419 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3420 RTCOfferAnswerOptions rtc_options;
3421
3422 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3423 // than kMaxOfferToReceiveMedia should be treated as invalid.
3424 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3425 CreatePeerConnection();
3426 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3427
3428 rtc_options.offer_to_receive_audio =
3429 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3430 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3431}
3432
Steve Anton36da6ff2018-02-16 16:04:20 -08003433TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003434 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3435 RTCOfferAnswerOptions rtc_options;
3436
3437 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3438 // than kMaxOfferToReceiveMedia should be treated as invalid.
3439 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3440 CreatePeerConnection();
3441 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3442
3443 rtc_options.offer_to_receive_video =
3444 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3445 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3446}
3447
3448// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003449// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003450TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003451 RTCOfferAnswerOptions rtc_options;
3452 rtc_options.offer_to_receive_audio = 1;
3453 rtc_options.offer_to_receive_video = 1;
3454
3455 std::unique_ptr<SessionDescriptionInterface> offer;
3456 CreatePeerConnection();
3457 offer = CreateOfferWithOptions(rtc_options);
3458 ASSERT_TRUE(offer);
3459 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3460 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3461}
3462
3463// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003464// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003465TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003466 RTCOfferAnswerOptions rtc_options;
3467 rtc_options.offer_to_receive_audio = 1;
3468 rtc_options.offer_to_receive_video = 0;
3469
3470 std::unique_ptr<SessionDescriptionInterface> offer;
3471 CreatePeerConnection();
3472 offer = CreateOfferWithOptions(rtc_options);
3473 ASSERT_TRUE(offer);
3474 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3475 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3476}
3477
Artem Titov880fa812021-07-30 22:30:23 +02003478// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003479// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003480TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003481 RTCOfferAnswerOptions rtc_options;
3482 rtc_options.offer_to_receive_audio = 0;
3483 rtc_options.offer_to_receive_video = 1;
3484
3485 std::unique_ptr<SessionDescriptionInterface> offer;
3486 CreatePeerConnection();
3487 offer = CreateOfferWithOptions(rtc_options);
3488 ASSERT_TRUE(offer);
3489 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3490 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3491}
3492
zhihuang1c378ed2017-08-17 14:10:50 -07003493// Test that no media content will be added to the offer if using default
3494// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003495TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003496 RTCOfferAnswerOptions rtc_options;
3497
3498 std::unique_ptr<SessionDescriptionInterface> offer;
3499 CreatePeerConnection();
3500 offer = CreateOfferWithOptions(rtc_options);
3501 ASSERT_TRUE(offer);
3502 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3503 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3504}
3505
Artem Titov880fa812021-07-30 22:30:23 +02003506// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003507// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003508TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3509 CreatePeerConnection();
3510
zhihuang1c378ed2017-08-17 14:10:50 -07003511 RTCOfferAnswerOptions rtc_options;
3512 rtc_options.ice_restart = false;
3513 rtc_options.offer_to_receive_audio = 1;
3514
3515 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003516 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003517 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3518 auto ufrag1 =
3519 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3520 auto pwd1 =
3521 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003522
Artem Titov880fa812021-07-30 22:30:23 +02003523 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003524 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003525 auto ufrag2 =
3526 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3527 auto pwd2 =
3528 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003529
Artem Titov880fa812021-07-30 22:30:23 +02003530 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003531 rtc_options.ice_restart = true;
3532 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003533 auto ufrag3 =
3534 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3535 auto pwd3 =
3536 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003537
3538 EXPECT_EQ(ufrag1, ufrag2);
3539 EXPECT_EQ(pwd1, pwd2);
3540 EXPECT_NE(ufrag2, ufrag3);
3541 EXPECT_NE(pwd2, pwd3);
3542}
3543
Artem Titov880fa812021-07-30 22:30:23 +02003544// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003545// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003546TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003547 RTCOfferAnswerOptions rtc_options;
3548 rtc_options.offer_to_receive_audio = 1;
3549 rtc_options.offer_to_receive_video = 1;
3550
3551 std::unique_ptr<SessionDescriptionInterface> offer;
3552 CreatePeerConnection();
3553
3554 rtc_options.use_rtp_mux = true;
3555 offer = CreateOfferWithOptions(rtc_options);
3556 ASSERT_TRUE(offer);
3557 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3558 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3559 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3560
3561 rtc_options.use_rtp_mux = false;
3562 offer = CreateOfferWithOptions(rtc_options);
3563 ASSERT_TRUE(offer);
3564 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3565 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3566 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3567}
3568
zhihuang141aacb2017-08-29 13:23:53 -07003569// This test ensures OnRenegotiationNeeded is called when we add track with
3570// MediaStream -> AddTrack in the same way it is called when we add track with
3571// PeerConnection -> AddTrack.
3572// The test can be removed once addStream is rewritten in terms of addTrack
3573// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003574// Don't run under Unified Plan since the stream API is not available.
3575TEST_F(PeerConnectionInterfaceTestPlanB,
3576 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003577 CreatePeerConnectionWithoutDtls();
3578 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003579 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003580 pc_->AddStream(stream);
3581 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003582 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003583 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003584 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003585 stream->AddTrack(audio_track);
3586 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3587 observer_.renegotiation_needed_ = false;
3588
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003589 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003590 stream->AddTrack(video_track);
3591 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3592 observer_.renegotiation_needed_ = false;
3593
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003594 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003595 stream->RemoveTrack(audio_track);
3596 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3597 observer_.renegotiation_needed_ = false;
3598
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003599 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003600 stream->RemoveTrack(video_track);
3601 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3602 observer_.renegotiation_needed_ = false;
3603}
3604
Zhi Huangb2d355e2017-10-26 17:26:37 -07003605// Tests that an error is returned if a description is applied that has fewer
3606// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003607TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003608 MediaSectionCountEnforcedForSubsequentOffer) {
3609 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003610 AddAudioTrack("audio_label");
3611 AddVideoTrack("video_label");
3612
Zhi Huangb2d355e2017-10-26 17:26:37 -07003613 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003614 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003615 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3616
3617 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003618 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003619 offer->description()->contents().pop_back();
3620 offer->description()->contents().pop_back();
3621 ASSERT_TRUE(offer->description()->contents().empty());
3622 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3623
3624 std::unique_ptr<SessionDescriptionInterface> answer;
3625 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3626 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3627
3628 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003629 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003630 offer->description()->contents().pop_back();
3631 offer->description()->contents().pop_back();
3632 ASSERT_TRUE(offer->description()->contents().empty());
3633 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3634}
3635
Johannes Kron89f874e2018-11-12 10:25:48 +01003636TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3637 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003638 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003639 CreatePeerConnection(config);
3640 std::unique_ptr<SessionDescriptionInterface> offer;
3641 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003642 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3643 // Possible to set to false.
3644 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003645 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003646 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003647 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003648 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003649}
3650
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003651INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3652 PeerConnectionInterfaceTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003653 Values(SdpSemantics::kPlanB_DEPRECATED,
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003654 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003655
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003656class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003657 protected:
3658 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003659 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003660 }
nisseeaabdf62017-05-05 02:23:02 -07003661 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003662 const RTCConfiguration& config) {
3663 rtc::scoped_refptr<PeerConnectionInterface> pc(
3664 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003665 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003666 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003667 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003668 }
3669
zhihuang9763d562016-08-05 11:14:50 -07003670 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003671 MockPeerConnectionObserver observer_;
3672};
3673
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003674// This sanity check validates the test infrastructure itself.
3675TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3676 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003677 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003678 rtc::scoped_refptr<PeerConnectionInterface> pc(
3679 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3680 EXPECT_TRUE(pc.get());
3681 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3682 pc->Close(); // No abort -> ok.
3683 SUCCEED();
3684}
3685
nisse51542be2016-02-12 02:27:06 -08003686// This test verifies the default behaviour with no constraints and a
3687// default RTCConfiguration.
3688TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3689 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003690 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003691
Niels Möllerf06f9232018-08-07 12:32:18 +02003692 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003693
3694 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003695 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3696 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003697 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003698 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003699}
3700
Niels Möller1d7ecd22018-01-18 15:25:12 +01003701// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003702// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003703TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3704 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003705 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003706
Niels Möller71bdda02016-03-31 12:59:59 +02003707 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003708 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003709
Niels Möller1d7ecd22018-01-18 15:25:12 +01003710 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003711}
3712
Niels Möller6539f692018-01-18 08:58:50 +01003713// This test verifies that the experiment_cpu_load_estimator flag is
3714// propagated from RTCConfiguration to the PeerConnection.
3715TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3716 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003717 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Niels Möller6539f692018-01-18 08:58:50 +01003718
3719 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003720 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003721
3722 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3723}
3724
deadbeef293e9262017-01-11 12:28:30 -08003725// Tests a few random fields being different.
3726TEST(RTCConfigurationTest, ComparisonOperators) {
3727 PeerConnectionInterface::RTCConfiguration a;
3728 PeerConnectionInterface::RTCConfiguration b;
3729 EXPECT_EQ(a, b);
3730
3731 PeerConnectionInterface::RTCConfiguration c;
3732 c.servers.push_back(PeerConnectionInterface::IceServer());
3733 EXPECT_NE(a, c);
3734
3735 PeerConnectionInterface::RTCConfiguration d;
3736 d.type = PeerConnectionInterface::kRelay;
3737 EXPECT_NE(a, d);
3738
3739 PeerConnectionInterface::RTCConfiguration e;
3740 e.audio_jitter_buffer_max_packets = 5;
3741 EXPECT_NE(a, e);
3742
3743 PeerConnectionInterface::RTCConfiguration f;
3744 f.ice_connection_receiving_timeout = 1337;
3745 EXPECT_NE(a, f);
3746
3747 PeerConnectionInterface::RTCConfiguration g;
3748 g.disable_ipv6 = true;
3749 EXPECT_NE(a, g);
3750
3751 PeerConnectionInterface::RTCConfiguration h(
3752 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3753 EXPECT_NE(a, h);
3754}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003755
3756} // namespace
3757} // namespace webrtc