blob: 15e7da4cf1b217713f014f31c4f6c0433f257d23 [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_;
Florent Castelli72424402022-04-06 03:45:10 +0200752 PeerConnectionDependencies pc_dependencies(&observer_);
753 pc_dependencies.cert_generator = std::move(cert_generator);
754 pc_dependencies.allocator = std::move(port_allocator);
755 auto result = pc_factory_->CreatePeerConnectionOrError(
756 modified_config, std::move(pc_dependencies));
757 ASSERT_TRUE(result.ok());
758 pc_ = result.MoveValue();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 observer_.SetPeerConnectionInterface(pc_.get());
760 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
761 }
762
deadbeef0a6c4ca2015-10-06 11:38:28 -0700763 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800764 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700765 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700766 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800768 config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200769 PeerConnectionDependencies pc_dependencies(&observer_);
770 auto result = pc_factory_->CreatePeerConnectionOrError(
771 config, std::move(pc_dependencies));
772 EXPECT_FALSE(result.ok());
deadbeef0a6c4ca2015-10-06 11:38:28 -0700773 }
774
Steve Anton038834f2017-07-14 15:59:59 -0700775 void CreatePeerConnectionExpectFail(
776 PeerConnectionInterface::RTCConfiguration config) {
777 PeerConnectionInterface::IceServer server;
778 server.uri = kTurnIceServerUri;
779 server.password = kTurnPassword;
780 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800781 config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200782 PeerConnectionDependencies pc_dependencies(&observer_);
783 auto result = pc_factory_->CreatePeerConnectionOrError(
784 config, std::move(pc_dependencies));
785 EXPECT_FALSE(result.ok());
Steve Anton038834f2017-07-14 15:59:59 -0700786 }
787
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100789 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800790 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
791 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
792 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800794 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795
deadbeef0a6c4ca2015-10-06 11:38:28 -0700796 CreatePeerConnectionExpectFail(kStunInvalidPort);
797 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
798 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799
Niels Möllerdb4def92019-03-18 16:53:59 +0100800 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
801 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
803 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800807 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800809 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811
812 void ReleasePeerConnection() {
813 pc_ = NULL;
814 observer_.SetPeerConnectionInterface(NULL);
815 }
816
Steve Anton36da6ff2018-02-16 16:04:20 -0800817 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
818 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100819 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800820 }
821
822 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800823 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800824 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800825 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800826 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
828
Steve Anton36da6ff2018-02-16 16:04:20 -0800829 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700830 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 stream->AddTrack(CreateVideoTrack(label + "v0"));
833 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800834 }
835
836 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
837 const std::string& label) {
838 return pc_factory_->CreateAudioTrack(label, nullptr);
839 }
840
841 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800842 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800843 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800844 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800845 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800846 }
847
848 void AddAudioStream(const std::string& label) {
849 rtc::scoped_refptr<MediaStreamInterface> stream(
850 pc_factory_->CreateLocalMediaStream(label));
851 stream->AddTrack(CreateAudioTrack(label + "a0"));
852 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
Seth Hampson845e8782018-03-02 11:34:10 -0800855 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 const std::string& audio_track_label,
857 const std::string& video_track_label) {
858 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700859 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800860 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800861 stream->AddTrack(CreateAudioTrack(audio_track_label));
862 stream->AddTrack(CreateVideoTrack(video_track_label));
863 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
865
Steve Anton36da6ff2018-02-16 16:04:20 -0800866 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
867 cricket::MediaType media_type) {
868 for (auto receiver : pc_->GetReceivers()) {
869 if (receiver->media_type() == media_type) {
870 return receiver;
871 }
872 }
873 return nullptr;
874 }
875
kwibergd1fe2812016-04-27 06:47:29 -0700876 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200877 const RTCOfferAnswerOptions* options,
878 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200879 auto observer =
880 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200882 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200884 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700887 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 return observer->result();
889 }
890
kwibergd1fe2812016-04-27 06:47:29 -0700891 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200892 const RTCOfferAnswerOptions* options) {
893 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895
kwibergd1fe2812016-04-27 06:47:29 -0700896 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200897 const RTCOfferAnswerOptions* options) {
898 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900
Steve Antondb45ca82017-09-11 18:27:34 -0700901 bool DoSetSessionDescription(
902 std::unique_ptr<SessionDescriptionInterface> desc,
903 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200904 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700906 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700908 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
zhihuang29ff8442016-07-27 11:07:25 -0700910 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
911 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 return observer->result();
914 }
915
Steve Antondb45ca82017-09-11 18:27:34 -0700916 bool DoSetLocalDescription(
917 std::unique_ptr<SessionDescriptionInterface> desc) {
918 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
Steve Antondb45ca82017-09-11 18:27:34 -0700921 bool DoSetRemoteDescription(
922 std::unique_ptr<SessionDescriptionInterface> desc) {
923 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
926 // Calls PeerConnection::GetStats and check the return value.
927 // It does not verify the values in the StatReports since a RTCP packet might
928 // be required.
929 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200930 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Yves Gerey665174f2018-06-19 15:03:05 +0200931 if (!pc_->GetStats(observer, track,
932 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 return false;
934 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
935 return observer->called();
936 }
937
Harald Alvestrand89061872018-01-02 14:08:34 +0100938 // Call the standards-compliant GetStats function.
939 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200940 auto callback =
941 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100942 pc_->GetStats(callback);
943 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
944 return callback->called();
945 }
946
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800948 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 // Create a local stream with audio&video tracks.
Florent Castelli15a38de2022-04-06 00:38:21 +0200950 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson845e8782018-03-02 11:34:10 -0800951 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800952 } else {
953 // Unified Plan does not support AddStream, so just add an audio and video
954 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800955 AddAudioTrack(kAudioTracks[0], {kStreamId1});
956 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 CreateOfferReceiveAnswer();
959 }
960
961 // Verify that RTP Header extensions has been negotiated for audio and video.
962 void VerifyRemoteRtpHeaderExtensions() {
963 const cricket::MediaContentDescription* desc =
964 cricket::GetFirstAudioContentDescription(
965 pc_->remote_description()->description());
966 ASSERT_TRUE(desc != NULL);
967 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
968
969 desc = cricket::GetFirstVideoContentDescription(
970 pc_->remote_description()->description());
971 ASSERT_TRUE(desc != NULL);
972 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
973 }
974
975 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700976 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700977 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 std::string sdp;
979 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700980 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800981 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700982 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
984 }
985
deadbeefab9b2d12015-10-14 11:33:11 -0700986 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700987 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800988 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700989 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700990 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
991 }
992
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700994 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700995 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996
997 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
998 // audio codec change, even if the parameter has nothing to do with
999 // receiving. Not all parameters are serialized to SDP.
1000 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1001 // the SessionDescription, it is necessary to do that here to in order to
1002 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1003 // https://code.google.com/p/webrtc/issues/detail?id=1356
1004 std::string sdp;
1005 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001006 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001007 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001008 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1010 }
1011
1012 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001013 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001014 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015
1016 std::string sdp;
1017 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001018 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001019 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001020 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1022 }
1023
1024 void CreateOfferReceiveAnswer() {
1025 CreateOfferAsLocalDescription();
1026 std::string sdp;
1027 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1028 CreateAnswerAsRemoteDescription(sdp);
1029 }
1030
1031 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001032 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001033 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1035 // audio codec change, even if the parameter has nothing to do with
1036 // receiving. Not all parameters are serialized to SDP.
1037 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1038 // the SessionDescription, it is necessary to do that here to in order to
1039 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1040 // https://code.google.com/p/webrtc/issues/detail?id=1356
1041 std::string sdp;
1042 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001043 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001044 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045
Steve Antondb45ca82017-09-11 18:27:34 -07001046 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001048 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001049 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 }
1051
deadbeefab9b2d12015-10-14 11:33:11 -07001052 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001053 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001054 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001055 ASSERT_TRUE(answer);
1056 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1058 }
1059
deadbeefab9b2d12015-10-14 11:33:11 -07001060 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001061 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001062 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001063 ASSERT_TRUE(pr_answer);
1064 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001066 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001067 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001068 ASSERT_TRUE(answer);
1069 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1071 }
1072
Seth Hampson845e8782018-03-02 11:34:10 -08001073 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001074 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001075 // called with the given stream id and expected number of tracks.
1076 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001077 int expected_num_tracks) {
1078 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001079 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001080 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001081 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 }
1083
1084 // Creates an offer and applies it as a local session description.
1085 // Creates an answer with the same SDP an the offer but removes all lines
1086 // that start with a:ssrc"
1087 void CreateOfferReceiveAnswerWithoutSsrc() {
1088 CreateOfferAsLocalDescription();
1089 std::string sdp;
1090 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1091 SetSsrcToZero(&sdp);
1092 CreateAnswerAsRemoteDescription(sdp);
1093 }
1094
deadbeefab9b2d12015-10-14 11:33:11 -07001095 // This function creates a MediaStream with label kStreams[0] and
Artem Titov880fa812021-07-30 22:30:23 +02001096 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001097 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001098 // is returned and the MediaStream is stored in
Artem Titov880fa812021-07-30 22:30:23 +02001099 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001100 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001101 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1102 size_t number_of_video_tracks) {
1103 EXPECT_LE(number_of_audio_tracks, 2u);
1104 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001105
1106 reference_collection_ = StreamCollection::Create();
1107 std::string sdp_ms1 = std::string(kSdpStringInit);
1108
Seth Hampson845e8782018-03-02 11:34:10 -08001109 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001110
1111 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001112 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001113 reference_collection_->AddStream(stream);
1114
1115 if (number_of_audio_tracks > 0) {
1116 sdp_ms1 += std::string(kSdpStringAudio);
1117 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1118 AddAudioTrack(kAudioTracks[0], stream);
1119 }
1120 if (number_of_audio_tracks > 1) {
1121 sdp_ms1 += kSdpStringMs1Audio1;
1122 AddAudioTrack(kAudioTracks[1], stream);
1123 }
1124
1125 if (number_of_video_tracks > 0) {
1126 sdp_ms1 += std::string(kSdpStringVideo);
1127 sdp_ms1 += std::string(kSdpStringMs1Video0);
1128 AddVideoTrack(kVideoTracks[0], stream);
1129 }
1130 if (number_of_video_tracks > 1) {
1131 sdp_ms1 += kSdpStringMs1Video1;
1132 AddVideoTrack(kVideoTracks[1], stream);
1133 }
1134
kwibergd1fe2812016-04-27 06:47:29 -07001135 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001136 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001137 }
1138
1139 void AddAudioTrack(const std::string& track_id,
1140 MediaStreamInterface* stream) {
1141 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1142 webrtc::AudioTrack::Create(track_id, nullptr));
1143 ASSERT_TRUE(stream->AddTrack(audio_track));
1144 }
1145
1146 void AddVideoTrack(const std::string& track_id,
1147 MediaStreamInterface* stream) {
1148 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001149 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001150 webrtc::FakeVideoTrackSource::Create(),
1151 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001152 ASSERT_TRUE(stream->AddTrack(video_track));
1153 }
1154
Steve Anton36da6ff2018-02-16 16:04:20 -08001155 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001156 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001157 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001158 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001159 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1160 return offer;
1161 }
1162
Steve Anton36da6ff2018-02-16 16:04:20 -08001163 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1164 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001165 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001166 std::unique_ptr<SessionDescriptionInterface> offer;
1167 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1168 return offer;
1169 }
1170
1171 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1172 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1173 std::unique_ptr<SessionDescriptionInterface> answer;
1174 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1175 return answer;
1176 }
1177
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001180 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1183 return answer;
1184 }
1185
1186 const std::string& GetFirstAudioStreamCname(
1187 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001188 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001189 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001190 return audio_desc->streams()[0].cname;
1191 }
1192
zhihuang1c378ed2017-08-17 14:10:50 -07001193 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1194 const RTCOfferAnswerOptions& offer_answer_options) {
1195 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001196 auto observer =
1197 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
zhihuang1c378ed2017-08-17 14:10:50 -07001198 pc_->CreateOffer(observer, offer_answer_options);
1199 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1200 return observer->MoveDescription();
1201 }
1202
1203 void CreateOfferWithOptionsAsRemoteDescription(
1204 std::unique_ptr<SessionDescriptionInterface>* desc,
1205 const RTCOfferAnswerOptions& offer_answer_options) {
1206 *desc = CreateOfferWithOptions(offer_answer_options);
1207 ASSERT_TRUE(desc != nullptr);
1208 std::string sdp;
1209 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001210 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001211 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001212 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001213 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1214 }
1215
1216 void CreateOfferWithOptionsAsLocalDescription(
1217 std::unique_ptr<SessionDescriptionInterface>* desc,
1218 const RTCOfferAnswerOptions& offer_answer_options) {
1219 *desc = CreateOfferWithOptions(offer_answer_options);
1220 ASSERT_TRUE(desc != nullptr);
1221 std::string sdp;
1222 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001223 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001224 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001225
Steve Antondb45ca82017-09-11 18:27:34 -07001226 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001227 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1228 }
1229
1230 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001231 RTC_DCHECK(content);
1232 RTC_DCHECK(content->media_description());
1233 for (const cricket::AudioCodec& codec :
1234 content->media_description()->as_audio()->codecs()) {
1235 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001236 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001237 }
zhihuang1c378ed2017-08-17 14:10:50 -07001238 }
1239 return false;
1240 }
1241
Steve Anton36da6ff2018-02-16 16:04:20 -08001242 const char* GetSdpStringWithStream1() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001243 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001244 return kSdpStringWithStream1PlanB;
1245 } else {
1246 return kSdpStringWithStream1UnifiedPlan;
1247 }
1248 }
1249
1250 const char* GetSdpStringWithStream1And2() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001251 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001252 return kSdpStringWithStream1And2PlanB;
1253 } else {
1254 return kSdpStringWithStream1And2UnifiedPlan;
1255 }
1256 }
1257
deadbeef9a6f4d42017-05-15 19:43:33 -07001258 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1259 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001260 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001261 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001262 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001263 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1264 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1265 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001267 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001268 const SdpSemantics sdp_semantics_;
1269};
1270
1271class PeerConnectionInterfaceTest
1272 : public PeerConnectionInterfaceBaseTest,
1273 public ::testing::WithParamInterface<SdpSemantics> {
1274 protected:
1275 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1276};
1277
1278class PeerConnectionInterfaceTestPlanB
1279 : public PeerConnectionInterfaceBaseTest {
1280 protected:
1281 PeerConnectionInterfaceTestPlanB()
Florent Castelli15a38de2022-04-06 00:38:21 +02001282 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283};
1284
zhihuang8f65cdf2016-05-06 18:40:30 -07001285// Generate different CNAMEs when PeerConnections are created.
1286// The CNAMEs are expected to be generated randomly. It is possible
1287// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001288TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001289 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001290 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001291 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001292 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001293 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1294 GetFirstAudioStreamCname(offer2.get()));
1295}
1296
Steve Anton36da6ff2018-02-16 16:04:20 -08001297TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001298 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001299 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001300 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001301 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001302 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1303 GetFirstAudioStreamCname(answer2.get()));
1304}
1305
Steve Anton36da6ff2018-02-16 16:04:20 -08001306TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 CreatePeerConnectionWithDifferentConfigurations) {
1308 CreatePeerConnectionWithDifferentConfigurations();
1309}
1310
Steve Anton36da6ff2018-02-16 16:04:20 -08001311TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001312 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1313 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1314 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1315 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1316 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1317 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1318 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1319 port_allocator_->candidate_filter());
1320 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1321 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1322}
1323
1324// Test that when a PeerConnection is created with a nonzero candidate pool
1325// size, the pooled PortAllocatorSession is created with all the attributes
1326// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001327TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001328 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001329 config.sdp_semantics = sdp_semantics_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001330 PeerConnectionInterface::IceServer server;
1331 server.uri = kStunAddressOnly;
1332 config.servers.push_back(server);
1333 config.type = PeerConnectionInterface::kRelay;
1334 config.disable_ipv6 = true;
1335 config.tcp_candidate_policy =
1336 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001337 config.candidate_network_policy =
1338 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001339 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001340 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001341
1342 const cricket::FakePortAllocatorSession* session =
1343 static_cast<const cricket::FakePortAllocatorSession*>(
1344 port_allocator_->GetPooledSession());
1345 ASSERT_NE(nullptr, session);
1346 EXPECT_EQ(1UL, session->stun_servers().size());
1347 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1348 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001349 EXPECT_LT(0U,
1350 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001351}
1352
deadbeefd21eab32017-07-26 16:50:11 -07001353// Test that network-related RTCConfiguration members are applied to the
1354// PortAllocator when CreatePeerConnection is called. Specifically:
1355// - disable_ipv6_on_wifi
1356// - max_ipv6_networks
1357// - tcp_candidate_policy
1358// - candidate_network_policy
1359// - prune_turn_ports
1360//
1361// Note that the candidate filter (RTCConfiguration::type) is already tested
1362// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001363TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001364 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1365 // Create fake port allocator.
1366 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1367 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1368 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1369
1370 // Create RTCConfiguration with some network-related fields relevant to
1371 // PortAllocator populated.
1372 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001373 config.sdp_semantics = sdp_semantics_;
deadbeefd21eab32017-07-26 16:50:11 -07001374 config.disable_ipv6_on_wifi = true;
1375 config.max_ipv6_networks = 10;
1376 config.tcp_candidate_policy =
1377 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1378 config.candidate_network_policy =
1379 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1380 config.prune_turn_ports = true;
1381
1382 // Create the PC factory and PC with the above config.
1383 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1384 webrtc::CreatePeerConnectionFactory(
1385 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001386 rtc::Thread::Current(), fake_audio_capture_module_,
1387 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001388 webrtc::CreateBuiltinAudioDecoderFactory(),
1389 webrtc::CreateBuiltinVideoEncoderFactory(),
1390 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1391 nullptr /* audio_processing */));
Florent Castelli72424402022-04-06 03:45:10 +02001392 PeerConnectionDependencies pc_dependencies(&observer_);
1393 pc_dependencies.allocator = std::move(port_allocator);
1394 auto result = pc_factory_->CreatePeerConnectionOrError(
1395 config, std::move(pc_dependencies));
1396 EXPECT_TRUE(result.ok());
1397 observer_.SetPeerConnectionInterface(result.value());
deadbeefd21eab32017-07-26 16:50:11 -07001398
1399 // Now validate that the config fields set above were applied to the
1400 // PortAllocator, as flags or otherwise.
1401 EXPECT_FALSE(raw_port_allocator->flags() &
1402 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1403 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1404 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1405 EXPECT_TRUE(raw_port_allocator->flags() &
1406 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001407 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1408 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001409}
1410
deadbeef46c73892016-11-16 19:42:04 -08001411// Check that GetConfiguration returns the configuration the PeerConnection was
1412// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001413TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001414 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001415 config.sdp_semantics = sdp_semantics_;
deadbeef46c73892016-11-16 19:42:04 -08001416 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001417 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001418
1419 PeerConnectionInterface::RTCConfiguration returned_config =
1420 pc_->GetConfiguration();
1421 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1422}
1423
1424// Check that GetConfiguration returns the last configuration passed into
1425// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001426TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001427 PeerConnectionInterface::RTCConfiguration starting_config;
Henrik Boström62995db2022-01-03 09:58:10 +01001428 starting_config.sdp_semantics = sdp_semantics_;
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001429 starting_config.bundle_policy =
1430 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1431 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001432
Steve Anton36da6ff2018-02-16 16:04:20 -08001433 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001434 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001435 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001436
1437 PeerConnectionInterface::RTCConfiguration returned_config =
1438 pc_->GetConfiguration();
1439 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1440}
1441
Steve Antonc79268f2018-04-24 09:54:10 -07001442TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1443 CreatePeerConnection();
1444
1445 pc_->Close();
1446
1447 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001448 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001449}
1450
Steve Anton36da6ff2018-02-16 16:04:20 -08001451TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001452 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001453 AddVideoStream(kStreamId1);
1454 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 ASSERT_EQ(2u, pc_->local_streams()->count());
1456
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001457 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001458 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001459 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001460 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001461 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001462 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001464 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001465 EXPECT_EQ(3u, pc_->local_streams()->count());
1466
1467 // Remove the third stream.
1468 pc_->RemoveStream(pc_->local_streams()->at(2));
1469 EXPECT_EQ(2u, pc_->local_streams()->count());
1470
1471 // Remove the second stream.
1472 pc_->RemoveStream(pc_->local_streams()->at(1));
1473 EXPECT_EQ(1u, pc_->local_streams()->count());
1474
1475 // Remove the first stream.
1476 pc_->RemoveStream(pc_->local_streams()->at(0));
1477 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
deadbeefab9b2d12015-10-14 11:33:11 -07001480// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001481// Don't run under Unified Plan since the stream API is not available.
1482TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001483 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001484 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001485 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001486 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001487
deadbeefab9b2d12015-10-14 11:33:11 -07001488 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001489 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001490 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001491
deadbeefab9b2d12015-10-14 11:33:11 -07001492 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001493 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001494 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001495
1496 // Add another stream and ensure the offer includes both the old and new
1497 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001498 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001499 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001500
Steve Antonb1c1de12017-12-21 15:14:30 -08001501 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001502 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1503 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001504
Steve Antonb1c1de12017-12-21 15:14:30 -08001505 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001506 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1507 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001508}
1509
Steve Anton36da6ff2018-02-16 16:04:20 -08001510// Don't run under Unified Plan since the stream API is not available.
1511TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001513 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 ASSERT_EQ(1u, pc_->local_streams()->count());
1515 pc_->RemoveStream(pc_->local_streams()->at(0));
1516 EXPECT_EQ(0u, pc_->local_streams()->count());
1517}
1518
deadbeefe1f9d832016-01-14 15:35:42 -08001519// Test for AddTrack and RemoveTrack methods.
1520// Tests that the created offer includes tracks we added,
1521// and that the RtpSenders are created correctly.
1522// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001523// Only tested with Plan B since Unified Plan is covered in more detail by tests
1524// in peerconnection_jsep_unittests.cc
1525TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001526 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001528 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001529 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001530 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001531 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1532 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001533 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001534 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001535 EXPECT_EQ("audio_track", audio_sender->id());
1536 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001537 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001538 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001539 EXPECT_EQ("video_track", video_sender->id());
1540 EXPECT_EQ(video_track, video_sender->track());
1541
1542 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001543 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001544 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001545
1546 const cricket::ContentInfo* audio_content =
1547 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001548 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001549 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 const cricket::ContentInfo* video_content =
1552 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001553 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001554 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
Steve Antondb45ca82017-09-11 18:27:34 -07001556 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001557
1558 // Now try removing the tracks.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001559 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1560 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001561
1562 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001563 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001564
1565 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001566 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001567 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001570 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001571 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001572
Steve Antondb45ca82017-09-11 18:27:34 -07001573 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001574
1575 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1576 // should return false.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001577 EXPECT_FALSE(pc_->RemoveTrackOrError(audio_sender).ok());
1578 EXPECT_FALSE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001579}
1580
1581// Test creating senders without a stream specified,
1582// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001583TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001584 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001586 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001587 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001588 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001589 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001590 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001591 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001592 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001593 EXPECT_EQ("audio_track", audio_sender->id());
1594 EXPECT_EQ(audio_track, audio_sender->track());
1595 EXPECT_EQ("video_track", video_sender->id());
1596 EXPECT_EQ(video_track, video_sender->track());
Florent Castelli15a38de2022-04-06 00:38:21 +02001597 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07001598 // If the ID is truly a random GUID, it should be infinitely unlikely they
1599 // will be the same.
1600 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1601 } else {
1602 // We allows creating tracks without stream ids under Unified Plan
1603 // semantics.
1604 EXPECT_EQ(0u, video_sender->stream_ids().size());
1605 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1606 }
deadbeefe1f9d832016-01-14 15:35:42 -08001607}
1608
Harald Alvestrand89061872018-01-02 14:08:34 +01001609// Test that we can call GetStats() after AddTrack but before connecting
1610// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001611TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001612 CreatePeerConnectionWithoutDtls();
1613 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001614 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001615 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001616 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001617 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1618 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001619 EXPECT_TRUE(DoGetStats(nullptr));
1620}
1621
Steve Anton36da6ff2018-02-16 16:04:20 -08001622TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001623 CreatePeerConnectionWithoutDtls();
1624 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001625 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001626 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001627 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001628 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001629 ASSERT_TRUE(audio_sender.ok());
1630 auto* audio_sender_proxy =
1631 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1632 audio_sender.value().get());
1633 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1634
Harald Alvestrandc72af932018-01-11 17:18:19 +01001635 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001636 ASSERT_TRUE(video_sender.ok());
1637 auto* video_sender_proxy =
1638 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1639 video_sender.value().get());
1640 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001641}
1642
Steve Anton36da6ff2018-02-16 16:04:20 -08001643// Don't run under Unified Plan since the stream API is not available.
1644TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001645 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001646 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001647 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001648 ASSERT_EQ(1u, senders.size());
1649 auto* sender_proxy =
1650 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1651 senders[0].get());
1652 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001653}
1654
Steve Anton36da6ff2018-02-16 16:04:20 -08001655TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001657 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 VerifyRemoteRtpHeaderExtensions();
1659}
1660
Steve Anton36da6ff2018-02-16 16:04:20 -08001661TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001663 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 CreateOfferAsLocalDescription();
1665 std::string offer;
1666 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1667 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001668 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
Steve Anton36da6ff2018-02-16 16:04:20 -08001671TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001672 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001673 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674
1675 CreateOfferAsRemoteDescription();
1676 CreateAnswerAsLocalDescription();
1677
Seth Hampson845e8782018-03-02 11:34:10 -08001678 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
Steve Anton36da6ff2018-02-16 16:04:20 -08001681TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001682 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001683 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684
1685 CreateOfferAsRemoteDescription();
1686 CreatePrAnswerAsLocalDescription();
1687 CreateAnswerAsLocalDescription();
1688
Seth Hampson845e8782018-03-02 11:34:10 -08001689 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
Steve Anton36da6ff2018-02-16 16:04:20 -08001692// Don't run under Unified Plan since the stream API is not available.
1693TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 InitiateCall();
1695 ASSERT_EQ(1u, pc_->remote_streams()->count());
1696 pc_->RemoveStream(pc_->local_streams()->at(0));
1697 CreateOfferReceiveAnswer();
1698 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001699 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 CreateOfferReceiveAnswer();
1701}
1702
1703// Tests that after negotiating an audio only call, the respondent can perform a
1704// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001705TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001706 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001707 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 CreateOfferAsRemoteDescription();
1709 CreateAnswerAsLocalDescription();
1710
1711 ASSERT_EQ(1u, pc_->remote_streams()->count());
1712 pc_->RemoveStream(pc_->local_streams()->at(0));
1713 CreateOfferReceiveAnswer();
1714 EXPECT_EQ(0u, pc_->remote_streams()->count());
1715}
1716
1717// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001718TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720
Steve Antonf1c6db12017-10-13 11:13:35 -07001721 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001724 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001725 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001726 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
1728 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001729 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001730 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001731 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
Steve Antonf1c6db12017-10-13 11:13:35 -07001733 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001734 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735
Steve Antonf1c6db12017-10-13 11:13:35 -07001736 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
deadbeefab9b2d12015-10-14 11:33:11 -07001739// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001741TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001742 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001744 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001745 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001746 EXPECT_TRUE(offer);
1747 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748
1749 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001750 AddAudioTrack("track_label", {kStreamId1});
1751 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752
1753 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001754 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755
1756 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001757 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001758 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759}
1760
1761// Test that we will get different SSRCs for each tracks in the offer and answer
1762// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001763TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001764 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001766 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1767 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768
1769 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001770 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001771 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 int audio_ssrc = 0;
1773 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001774 EXPECT_TRUE(
1775 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1776 EXPECT_TRUE(
1777 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 EXPECT_NE(audio_ssrc, video_ssrc);
1779
1780 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001781 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001783 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 audio_ssrc = 0;
1785 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001786 EXPECT_TRUE(
1787 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1788 EXPECT_TRUE(
1789 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 EXPECT_NE(audio_ssrc, video_ssrc);
1791}
1792
deadbeefeb459812015-12-15 19:24:43 -08001793// Test that it's possible to call AddTrack on a MediaStream after adding
1794// the stream to a PeerConnection.
1795// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001796// Don't run under Unified Plan since the stream API is not available.
1797TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001798 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001799 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001800 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001801 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1802
1803 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001804 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001805 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001806 stream->AddTrack(video_track.get());
1807
kwibergd1fe2812016-04-27 06:47:29 -07001808 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001809 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001810
1811 const cricket::MediaContentDescription* video_desc =
1812 cricket::GetFirstVideoContentDescription(offer->description());
1813 EXPECT_TRUE(video_desc != nullptr);
1814}
1815
1816// Test that it's possible to call RemoveTrack on a MediaStream after adding
1817// the stream to a PeerConnection.
1818// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001819// Don't run under Unified Plan since the stream API is not available.
1820TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001821 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001822 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001823 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001824 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1825
1826 // Remove the video track.
1827 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1828
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001830 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001831
1832 const cricket::MediaContentDescription* video_desc =
1833 cricket::GetFirstVideoContentDescription(offer->description());
1834 EXPECT_TRUE(video_desc == nullptr);
1835}
1836
deadbeefbd7d8f72015-12-18 16:58:44 -08001837// Test creating a sender with a stream ID, and ensure the ID is populated
1838// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001839// Don't run under Unified Plan since the stream API is not available.
1840TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001841 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001842 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001843
kwibergd1fe2812016-04-27 06:47:29 -07001844 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001845 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001846
1847 const cricket::MediaContentDescription* video_desc =
1848 cricket::GetFirstVideoContentDescription(offer->description());
1849 ASSERT_TRUE(video_desc != nullptr);
1850 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001851 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001855TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001857 ASSERT_LT(0u, pc_->GetSenders().size());
1858 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001859 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001860 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 EXPECT_TRUE(DoGetStats(remote_audio));
1862
1863 // Remove the stream. Since we are sending to our selves the local
1864 // and the remote stream is the same.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001865 pc_->RemoveTrackOrError(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 // Do a re-negotiation.
1867 CreateOfferReceiveAnswer();
1868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 // Test that we still can get statistics for the old track. Even if it is not
1870 // sent any longer.
1871 EXPECT_TRUE(DoGetStats(remote_audio));
1872}
1873
1874// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001875TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001877 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1878 ASSERT_TRUE(video_receiver);
1879 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880}
1881
1882// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001883TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001885 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 pc_factory_->CreateAudioTrack("unknown track", NULL));
1887 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1888}
1889
Steve Anton36da6ff2018-02-16 16:04:20 -08001890TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001891 CreatePeerConnectionWithoutDtls();
1892 EXPECT_TRUE(DoGetRTCStats());
1893 // Clearing stats cache is needed now, but should be temporary.
1894 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1895 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001896 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1897 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001898 EXPECT_TRUE(DoGetRTCStats());
1899 pc_->ClearStatsCache();
1900 CreateOfferReceiveAnswer();
1901 EXPECT_TRUE(DoGetRTCStats());
1902}
1903
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904// This tests that a SCTP data channel is returned using different
1905// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001906TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001907 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001908 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909
1910 webrtc::DataChannelInit config;
Florent Castelli72424402022-04-06 03:45:10 +02001911 auto channel = pc_->CreateDataChannelOrError("1", &config);
1912 EXPECT_TRUE(channel.ok());
1913 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001914 EXPECT_TRUE(observer_.renegotiation_needed_);
1915 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916
1917 config.ordered = false;
Florent Castelli72424402022-04-06 03:45:10 +02001918 channel = pc_->CreateDataChannelOrError("2", &config);
1919 EXPECT_TRUE(channel.ok());
1920 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001921 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922
1923 config.ordered = true;
1924 config.maxRetransmits = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001925 channel = pc_->CreateDataChannelOrError("3", &config);
1926 EXPECT_TRUE(channel.ok());
1927 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001928 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001930 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 config.maxRetransmitTime = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001932 channel = pc_->CreateDataChannelOrError("4", &config);
1933 EXPECT_TRUE(channel.ok());
1934 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001935 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936}
1937
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001938// For backwards compatibility, we want people who "unset" maxRetransmits
1939// and maxRetransmitTime by setting them to -1 to get what they want.
1940TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1941 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001942 CreatePeerConnection(rtc_config);
1943
1944 webrtc::DataChannelInit config;
1945 config.maxRetransmitTime = -1;
1946 config.maxRetransmits = -1;
Florent Castelli72424402022-04-06 03:45:10 +02001947 auto channel = pc_->CreateDataChannelOrError("1", &config);
1948 EXPECT_TRUE(channel.ok());
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001949}
1950
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951// This tests that no data channel is returned if both maxRetransmits and
1952// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001953TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001955 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001956 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957
1958 std::string label = "test";
1959 webrtc::DataChannelInit config;
1960 config.maxRetransmits = 0;
1961 config.maxRetransmitTime = 0;
1962
Florent Castelli72424402022-04-06 03:45:10 +02001963 auto channel = pc_->CreateDataChannelOrError(label, &config);
1964 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965}
1966
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967// The test verifies that creating a SCTP data channel with an id already in use
1968// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001969TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001971 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001972 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973
1974 webrtc::DataChannelInit config;
1975
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001976 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001977 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02001978 auto channel = pc_->CreateDataChannelOrError("1", &config);
1979 EXPECT_TRUE(channel.ok());
1980 EXPECT_EQ(1, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981
Florent Castelli72424402022-04-06 03:45:10 +02001982 channel = pc_->CreateDataChannelOrError("x", &config);
1983 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984
1985 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001986 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02001987 channel = pc_->CreateDataChannelOrError("max", &config);
1988 EXPECT_TRUE(channel.ok());
1989 EXPECT_EQ(config.id, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990
1991 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001992 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02001993 channel = pc_->CreateDataChannelOrError("x", &config);
1994 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995}
1996
deadbeefab9b2d12015-10-14 11:33:11 -07001997// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08001998TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001999 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002000 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002001
2002 std::string label = "test";
Florent Castelli72424402022-04-06 03:45:10 +02002003 auto channel = pc_->CreateDataChannelOrError(label, nullptr);
2004 EXPECT_TRUE(channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002005
Florent Castelli72424402022-04-06 03:45:10 +02002006 auto dup_channel = pc_->CreateDataChannelOrError(label, nullptr);
2007 EXPECT_TRUE(dup_channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002008}
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
Florent Castelli72424402022-04-06 03:45:10 +02002020 auto offer_channel = pc_->CreateDataChannelOrError("offer_channel", NULL);
Zhi Huang644fde42018-04-02 19:16:26 -07002021
2022 CreateOfferAsLocalDescription();
2023
2024 // Create an answer where the m-line for data channels are rejected.
2025 std::string sdp;
2026 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2027 std::unique_ptr<SessionDescriptionInterface> answer(
2028 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2029 ASSERT_TRUE(answer);
2030 cricket::ContentInfo* data_info =
2031 cricket::GetFirstDataContent(answer->description());
2032 data_info->rejected = true;
2033
2034 DoSetRemoteDescription(std::move(answer));
Florent Castelli72424402022-04-06 03:45:10 +02002035 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel.value()->state());
Zhi Huang644fde42018-04-02 19:16:26 -07002036}
2037
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038// Test that we can create a session description from an SDP string from
2039// FireFox, use it as a remote session description, generate an answer and use
2040// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002041TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002042 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002043 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002044 AddAudioTrack("audio_label");
2045 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002046 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002047 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002048 webrtc::kFireFoxSdpOffer, nullptr));
2049 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 CreateAnswerAsLocalDescription();
2051 ASSERT_TRUE(pc_->local_description() != NULL);
2052 ASSERT_TRUE(pc_->remote_description() != NULL);
2053
2054 const cricket::ContentInfo* content =
2055 cricket::GetFirstAudioContent(pc_->local_description()->description());
2056 ASSERT_TRUE(content != NULL);
2057 EXPECT_FALSE(content->rejected);
2058
2059 content =
2060 cricket::GetFirstVideoContent(pc_->local_description()->description());
2061 ASSERT_TRUE(content != NULL);
2062 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002063#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 content =
2065 cricket::GetFirstDataContent(pc_->local_description()->description());
2066 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002067 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002068#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069}
2070
Harald Alvestrand0d018412021-11-04 13:52:31 +00002071// Test that fallback from DTLS to SDES is not supported.
2072// The fallback was previously supported but was removed to simplify the code
2073// and because it's non-standard.
2074TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002075 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002076 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002077 // Wait for fake certificate to be generated. Previously, this is what caused
2078 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002079 AddAudioTrack("audio_label");
2080 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002081 ASSERT_NE(nullptr, fake_certificate_generator_);
2082 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2083 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002084 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002085 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2086 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002087 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002088}
2089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090// Test that we can create an audio only offer and receive an answer with a
2091// limited set of audio codecs and receive an updated offer with more audio
2092// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002093TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002094 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002095 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 CreateOfferAsLocalDescription();
2097
Florent Castelli15a38de2022-04-06 00:38:21 +02002098 const char* answer_sdp = (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
2099 ? webrtc::kAudioSdpPlanB
2100 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002101 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002102 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002103 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104
Steve Anton36da6ff2018-02-16 16:04:20 -08002105 const char* reoffer_sdp =
Florent Castelli15a38de2022-04-06 00:38:21 +02002106 (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
Steve Anton36da6ff2018-02-16 16:04:20 -08002107 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2108 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002109 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002110 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002111 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 CreateAnswerAsLocalDescription();
2113}
2114
deadbeefc80741f2015-10-22 13:14:45 -07002115// Test that if we're receiving (but not sending) a track, subsequent offers
2116// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002117TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002118 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002119 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002120 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002121 CreateAnswerAsLocalDescription();
2122
2123 // At this point we should be receiving stream 1, but not sending anything.
2124 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002125 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002126 DoCreateOffer(&offer, nullptr);
2127
2128 const cricket::ContentInfo* video_content =
2129 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002130 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2131 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002132
2133 const cricket::ContentInfo* audio_content =
2134 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002135 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2136 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002137}
2138
2139// Test that if we're receiving (but not sending) a track, and the
2140// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2141// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002142TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002143 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002144 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002145 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002146 CreateAnswerAsLocalDescription();
2147
2148 // At this point we should be receiving stream 1, but not sending anything.
2149 // A new offer would be recvonly, but we'll set the "no receive" constraints
2150 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002151 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002152 RTCOfferAnswerOptions options;
2153 options.offer_to_receive_audio = 0;
2154 options.offer_to_receive_video = 0;
2155 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002156
2157 const cricket::ContentInfo* video_content =
2158 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002159 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2160 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002161
2162 const cricket::ContentInfo* audio_content =
2163 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002164 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2165 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002166}
2167
deadbeef653b8e02015-11-11 12:55:10 -08002168// Test that we can use SetConfiguration to change the ICE servers of the
2169// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002170TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002171 CreatePeerConnection();
2172
Steve Anton36da6ff2018-02-16 16:04:20 -08002173 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002174 PeerConnectionInterface::IceServer server;
2175 server.uri = "stun:test_hostname";
2176 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002177 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002178
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002179 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2180 EXPECT_EQ("test_hostname",
2181 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002182}
2183
Steve Anton36da6ff2018-02-16 16:04:20 -08002184TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002185 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002186 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002187 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002188 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002189 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2190}
2191
Steve Anton36da6ff2018-02-16 16:04:20 -08002192TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002193 PeerConnectionInterface::RTCConfiguration config;
2194 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002195 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002196 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002197 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002198
2199 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002200 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002201 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2202 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002203}
2204
skvladd1f5fda2017-02-03 16:54:05 -08002205// Test that the ice check interval can be changed. This does not verify that
2206// the setting makes it all the way to P2PTransportChannel, as that would
2207// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002208TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002209 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002210 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002211 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002212 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002213 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002214 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002215 config = pc_->GetConfiguration();
2216 EXPECT_EQ(config.ice_check_min_interval, 100);
2217}
2218
2219TEST_P(PeerConnectionInterfaceTest,
2220 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2221 PeerConnectionInterface::RTCConfiguration config;
2222 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2223 CreatePeerConnection(config);
2224 config = pc_->GetConfiguration();
2225 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2226
2227 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002228 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002229 config = pc_->GetConfiguration();
2230 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002231}
2232
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002233// Test that when SetConfiguration changes both the pool size and other
2234// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002235TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002236 SetConfigurationCreatesPooledSessionCorrectly) {
2237 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002238 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002239 config.ice_candidate_pool_size = 1;
2240 PeerConnectionInterface::IceServer server;
2241 server.uri = kStunAddressOnly;
2242 config.servers.push_back(server);
2243 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002244 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002245
2246 const cricket::FakePortAllocatorSession* session =
2247 static_cast<const cricket::FakePortAllocatorSession*>(
2248 port_allocator_->GetPooledSession());
2249 ASSERT_NE(nullptr, session);
2250 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002251}
2252
deadbeef293e9262017-01-11 12:28:30 -08002253// Test that after SetLocalDescription, changing the pool size is not allowed,
2254// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002255TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002256 CantChangePoolSizeAfterSetLocalDescription) {
2257 CreatePeerConnection();
2258 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002259 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002260 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002261 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002262
2263 // Set remote offer; can still change pool size at this point.
2264 CreateOfferAsRemoteDescription();
2265 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002266 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002267
2268 // Set local answer; now it's too late.
2269 CreateAnswerAsLocalDescription();
2270 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002271 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002272 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2273}
2274
deadbeef42a42632017-03-10 15:18:00 -08002275// Test that after setting an answer, extra pooled sessions are discarded. The
2276// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002277TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002278 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2279 CreatePeerConnection();
2280
2281 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002282 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002283 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002284 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002285
2286 // Do offer/answer.
2287 CreateOfferAsRemoteDescription();
2288 CreateAnswerAsLocalDescription();
2289
2290 // Expect no pooled sessions to be left.
2291 const cricket::PortAllocatorSession* session =
2292 port_allocator_->GetPooledSession();
2293 EXPECT_EQ(nullptr, session);
2294}
2295
2296// After Close is called, pooled candidates should be discarded so as to not
2297// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002298TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002299 CreatePeerConnection();
2300
Steve Anton36da6ff2018-02-16 16:04:20 -08002301 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002302 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002303 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002304 pc_->Close();
2305
2306 // Expect no pooled sessions to be left.
2307 const cricket::PortAllocatorSession* session =
2308 port_allocator_->GetPooledSession();
2309 EXPECT_EQ(nullptr, session);
2310}
2311
deadbeef293e9262017-01-11 12:28:30 -08002312// Test that SetConfiguration returns an invalid modification error if
2313// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002314TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002315 SetConfigurationReturnsInvalidModificationError) {
2316 PeerConnectionInterface::RTCConfiguration config;
2317 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2318 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2319 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002320 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002321
Steve Anton36da6ff2018-02-16 16:04:20 -08002322 PeerConnectionInterface::RTCConfiguration modified_config =
2323 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002324 modified_config.bundle_policy =
2325 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002326 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002327 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2328
Steve Anton36da6ff2018-02-16 16:04:20 -08002329 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002330 modified_config.rtcp_mux_policy =
2331 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002332 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002333 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2334
Steve Anton36da6ff2018-02-16 16:04:20 -08002335 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002336 modified_config.continual_gathering_policy =
2337 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002338 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002339 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2340}
2341
2342// Test that SetConfiguration returns a range error if the candidate pool size
2343// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002344TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002345 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2346 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002347 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002348 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002349
2350 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002351 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002352 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2353
2354 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002355 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002356 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2357}
2358
2359// Test that SetConfiguration returns a syntax error if parsing an ICE server
2360// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002361TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002362 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2363 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002364 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002365 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002366
2367 PeerConnectionInterface::IceServer bad_server;
2368 bad_server.uri = "stunn:www.example.com";
2369 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002370 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002371 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2372}
2373
2374// Test that SetConfiguration returns an invalid parameter error if a TURN
2375// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002376TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002377 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2378 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002379 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002380 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002381
2382 PeerConnectionInterface::IceServer bad_server;
2383 bad_server.uri = "turn:www.example.com";
2384 // Missing password.
2385 bad_server.username = "foo";
2386 config.servers.push_back(bad_server);
2387 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002388 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2389 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002390}
2391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392// Test that PeerConnection::Close changes the states to closed and all remote
2393// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002394TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 // Initialize a PeerConnection and negotiate local and remote session
2396 // description.
2397 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002398
2399 // With Plan B, verify the stream count. The analog with Unified Plan is the
2400 // RtpTransceiver count.
Florent Castelli15a38de2022-04-06 00:38:21 +02002401 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002402 ASSERT_EQ(1u, pc_->local_streams()->count());
2403 ASSERT_EQ(1u, pc_->remote_streams()->count());
2404 } else {
2405 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2406 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407
2408 pc_->Close();
2409
2410 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2411 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2412 pc_->ice_connection_state());
2413 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2414 pc_->ice_gathering_state());
2415
Florent Castelli15a38de2022-04-06 00:38:21 +02002416 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002417 EXPECT_EQ(1u, pc_->local_streams()->count());
2418 EXPECT_EQ(1u, pc_->remote_streams()->count());
2419 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002420 // Verify that the RtpTransceivers are still returned.
2421 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002422 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423
Steve Anton36da6ff2018-02-16 16:04:20 -08002424 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002425 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Florent Castelli15a38de2022-04-06 00:38:21 +02002426 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002427 ASSERT_TRUE(audio_receiver);
2428 ASSERT_TRUE(video_receiver);
2429 // Track state may be updated asynchronously.
2430 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2431 audio_receiver->track()->state(), kTimeout);
2432 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2433 video_receiver->track()->state(), kTimeout);
2434 } else {
2435 ASSERT_FALSE(audio_receiver);
2436 ASSERT_FALSE(video_receiver);
2437 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438}
2439
2440// Test that PeerConnection methods fails gracefully after
2441// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002442// Don't run under Unified Plan since the stream API is not available.
2443TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002444 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002445 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 CreateOfferAsRemoteDescription();
2447 CreateAnswerAsLocalDescription();
2448
2449 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002450 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2451 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452
2453 pc_->Close();
2454
2455 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002456 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457
Florent Castelli72424402022-04-06 03:45:10 +02002458 EXPECT_FALSE(pc_->CreateDataChannelOrError("test", NULL).ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459
2460 EXPECT_TRUE(pc_->local_description() != NULL);
2461 EXPECT_TRUE(pc_->remote_description() != NULL);
2462
kwibergd1fe2812016-04-27 06:47:29 -07002463 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002464 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002465 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002466 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467
2468 std::string sdp;
2469 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002470 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002471 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002472 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002473
2474 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002475 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002476 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002477 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478}
2479
2480// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002481TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 InitiateCall();
2483 pc_->Close();
2484 DoGetStats(NULL);
2485}
deadbeefab9b2d12015-10-14 11:33:11 -07002486
2487// NOTE: The series of tests below come from what used to be
2488// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2489// setting a remote or local description has the expected effects.
2490
2491// This test verifies that the remote MediaStreams corresponding to a received
2492// SDP string is created. In this test the two separate MediaStreams are
2493// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002494TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002495 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002496 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002497 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002498
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002499 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002500 EXPECT_TRUE(
2501 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2502 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2503 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2504
2505 // Create a session description based on another SDP with another
2506 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002507 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002508
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002509 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002510 EXPECT_TRUE(
2511 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2512}
2513
2514// This test verifies that when remote tracks are added/removed from SDP, the
2515// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002516// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2517// specific behavior.
2518TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002519 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002520 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002521 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002522 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002523 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002524 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002525 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2526 reference_collection_));
2527
2528 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002529 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002530 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002531 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002532 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2533 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002534 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002535 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2536 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002537 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002538 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2539 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002540
2541 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002542 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002543 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002544 MockTrackObserver audio_track_observer(audio_track2);
2545 MockTrackObserver video_track_observer(video_track2);
2546
2547 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2548 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002549 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002550 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2551 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002552 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002553 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002554 audio_track2->state(), kTimeout);
2555 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2556 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002557}
2558
2559// This tests that remote tracks are ended if a local session description is set
2560// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002561TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002562 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002563 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002564 // First create and set a remote offer, then reject its video content in our
2565 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002566 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2567 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2568 ASSERT_TRUE(audio_receiver);
2569 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2570 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002571
Steve Anton36da6ff2018-02-16 16:04:20 -08002572 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2573 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002574 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002575 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2576 video_receiver->track();
2577 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002578
kwibergd1fe2812016-04-27 06:47:29 -07002579 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002580 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002581 cricket::ContentInfo* video_info =
2582 local_answer->description()->GetContentByName("video");
2583 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002584 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002585 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2586 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002587
2588 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002589 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002590 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002591 video_info = local_offer->description()->GetContentByName("video");
2592 ASSERT_TRUE(video_info != nullptr);
2593 video_info->rejected = true;
2594 cricket::ContentInfo* audio_info =
2595 local_offer->description()->GetContentByName("audio");
2596 ASSERT_TRUE(audio_info != nullptr);
2597 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002598 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002599 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002600 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2601 kTimeout);
2602 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2603 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002604}
2605
2606// This tests that we won't crash if the remote track has been removed outside
2607// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002608// Don't run under Unified Plan since the stream API is not available.
2609TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002610 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002611 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002612 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002613 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2614 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2615 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2616
kwibergd1fe2812016-04-27 06:47:29 -07002617 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002618 webrtc::CreateSessionDescription(SdpType::kAnswer,
2619 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002620 cricket::ContentInfo* video_info =
2621 local_answer->description()->GetContentByName("video");
2622 video_info->rejected = true;
2623 cricket::ContentInfo* audio_info =
2624 local_answer->description()->GetContentByName("audio");
2625 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002626 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002627
2628 // No crash is a pass.
2629}
2630
deadbeef5e97fb52015-10-15 12:49:08 -07002631// This tests that if a recvonly remote description is set, no remote streams
2632// will be created, even if the description contains SSRCs/MSIDs.
2633// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002634TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002635 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002636 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002637
Steve Anton36da6ff2018-02-16 16:04:20 -08002638 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002639 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002640 CreateAndSetRemoteOffer(recvonly_offer);
2641
2642 EXPECT_EQ(0u, observer_.remote_streams()->count());
2643}
2644
deadbeefab9b2d12015-10-14 11:33:11 -07002645// This tests that a default MediaStream is created if a remote session
2646// description doesn't contain any streams and no MSID support.
2647// It also tests that the default stream is updated if a video m-line is added
2648// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002649// Don't run under Unified Plan since this behavior is Plan B specific.
2650TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002651 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002652 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002653 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2654
2655 ASSERT_EQ(1u, observer_.remote_streams()->count());
2656 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2657
2658 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2659 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002660 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002661
2662 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2663 ASSERT_EQ(1u, observer_.remote_streams()->count());
2664 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2665 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002666 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2667 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002668 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2669 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002670 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2671 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002672}
2673
2674// This tests that a default MediaStream is created if a remote session
2675// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002676// Don't run under Unified Plan since this behavior is Plan B specific.
2677TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002678 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002679 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002680 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002681 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2682
2683 ASSERT_EQ(1u, observer_.remote_streams()->count());
2684 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2685
2686 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2687 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002688 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002689}
2690
2691// This tests that it won't crash when PeerConnection tries to remove
2692// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002693// Don't run under Unified Plan since this behavior is Plan B specific.
2694TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002695 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002696 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002697 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002698 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2699 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2700 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2701
2702 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2703
2704 // No crash is a pass.
2705}
2706
2707// This tests that a default MediaStream is created if the remote session
2708// description doesn't contain any streams and don't contain an indication if
2709// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002710// Don't run under Unified Plan since this behavior is Plan B specific.
2711TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002712 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002713 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002714 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002715 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2716
2717 ASSERT_EQ(1u, observer_.remote_streams()->count());
2718 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2719 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2720 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2721}
2722
2723// This tests that a default MediaStream is not created if the remote session
2724// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002725// Don't run under Unified Plan since this behavior is Plan B specific.
2726TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002727 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002728 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002729 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2730 EXPECT_EQ(0u, observer_.remote_streams()->count());
2731}
2732
deadbeefbda7e0b2015-12-08 17:13:40 -08002733// This tests that when setting a new description, the old default tracks are
2734// not destroyed and recreated.
2735// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002736// Don't run under Unified Plan since this behavior is Plan B specific.
2737TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002738 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002739 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002740 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002741 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2742
2743 ASSERT_EQ(1u, observer_.remote_streams()->count());
2744 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2745 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2746
2747 // Set the track to "disabled", then set a new description and ensure the
2748 // track is still disabled, which ensures it hasn't been recreated.
2749 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2750 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2751 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2752 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2753}
2754
deadbeefab9b2d12015-10-14 11:33:11 -07002755// This tests that a default MediaStream is not created if a remote session
2756// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002757// Don't run under Unified Plan since this behavior is Plan B specific.
2758TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002759 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002760 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002761 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002762 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002763 EXPECT_TRUE(
2764 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2765
2766 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2767 EXPECT_EQ(0u, observer_.remote_streams()->count());
2768}
2769
Seth Hampson5897a6e2018-04-03 11:16:33 -07002770// This tests that a default MediaStream is created if a remote SDP comes from
2771// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2772TEST_F(PeerConnectionInterfaceTestPlanB,
2773 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002774 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002775 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002776 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2777 // Add a=msid lines to simulate a Unified Plan endpoint that only
2778 // signals stream IDs with a=msid lines.
2779 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2780
2781 CreateAndSetRemoteOffer(sdp_string);
2782
2783 ASSERT_EQ(1u, observer_.remote_streams()->count());
2784 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2785 EXPECT_EQ("default", remote_stream->id());
2786 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2787}
2788
Seth Hampson5b4f0752018-04-02 16:31:36 -07002789// This tests that when a Plan B endpoint receives an SDP that signals no media
2790// stream IDs indicated by the special character "-" in the a=msid line, that
2791// a default stream ID will be used for the MediaStream ID. This can occur
2792// when a Unified Plan endpoint signals no media stream IDs, but signals both
2793// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2794TEST_F(PeerConnectionInterfaceTestPlanB,
2795 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002796 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002797 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002798 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2799 // the sender's stream ID will be interpreted as no stream IDs.
2800 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2801 sdp_string.append("a=msid:- audiotrack0\n");
2802
2803 CreateAndSetRemoteOffer(sdp_string);
2804
2805 ASSERT_EQ(1u, observer_.remote_streams()->count());
2806 // Because SSRCs are signaled the track ID will be what was signaled in the
2807 // a=msid line.
2808 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2809 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2810 EXPECT_EQ("default", remote_stream->id());
2811 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002812
2813 // Previously a bug ocurred when setting the remote description a second time.
2814 // This is because we checked equality of the remote StreamParams stream ID
2815 // (empty), and the previously set stream ID for the remote sender
2816 // ("default"). This cause a track to be removed, then added, when really
2817 // nothing should occur because it is the same track.
2818 CreateAndSetRemoteOffer(sdp_string);
2819 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2820 EXPECT_EQ(1u, observer_.add_track_events_.size());
2821 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2822 remote_stream = observer_.remote_streams()->at(0);
2823 EXPECT_EQ("default", remote_stream->id());
2824 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002825}
2826
deadbeefab9b2d12015-10-14 11:33:11 -07002827// This tests that an RtpSender is created when the local description is set
2828// after adding a local stream.
2829// TODO(deadbeef): This test and the one below it need to be updated when
2830// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002831// Don't run under Unified Plan since this behavior is Plan B specific.
2832TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002833 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002834 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002835
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002836 // Create an offer with 1 stream with 2 tracks of each type.
2837 rtc::scoped_refptr<StreamCollection> stream_collection =
2838 CreateStreamCollection(1, 2);
2839 pc_->AddStream(stream_collection->at(0));
2840 std::unique_ptr<SessionDescriptionInterface> offer;
2841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002842 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002843
deadbeefab9b2d12015-10-14 11:33:11 -07002844 auto senders = pc_->GetSenders();
2845 EXPECT_EQ(4u, senders.size());
2846 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2847 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2848 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2849 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2850
2851 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002852 pc_->RemoveStream(stream_collection->at(0));
2853 stream_collection = CreateStreamCollection(1, 1);
2854 pc_->AddStream(stream_collection->at(0));
2855 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002856 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002857
deadbeefab9b2d12015-10-14 11:33:11 -07002858 senders = pc_->GetSenders();
2859 EXPECT_EQ(2u, senders.size());
2860 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2861 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2862 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2863 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2864}
2865
2866// This tests that an RtpSender is created when the local description is set
2867// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002868// Don't run under Unified Plan since this behavior is Plan B specific.
2869TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002870 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002871 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002872 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002873
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002874 rtc::scoped_refptr<StreamCollection> stream_collection =
2875 CreateStreamCollection(1, 2);
2876 // Add a stream to create the offer, but remove it afterwards.
2877 pc_->AddStream(stream_collection->at(0));
2878 std::unique_ptr<SessionDescriptionInterface> offer;
2879 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2880 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002881
Steve Antondb45ca82017-09-11 18:27:34 -07002882 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002883 auto senders = pc_->GetSenders();
2884 EXPECT_EQ(0u, senders.size());
2885
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002886 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002887 senders = pc_->GetSenders();
2888 EXPECT_EQ(4u, senders.size());
2889 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2890 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2891 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2892 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2893}
2894
2895// This tests that the expected behavior occurs if the SSRC on a local track is
2896// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002897TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002898 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002899 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002900 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002901
Steve Anton36da6ff2018-02-16 16:04:20 -08002902 AddAudioTrack(kAudioTracks[0]);
2903 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002904 std::unique_ptr<SessionDescriptionInterface> offer;
2905 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2906 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002907 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2908 webrtc::CreateSessionDescription(
2909 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002910 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002911 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002912
deadbeefab9b2d12015-10-14 11:33:11 -07002913 auto senders = pc_->GetSenders();
2914 EXPECT_EQ(2u, senders.size());
2915 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2916 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2917
2918 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002919 cricket::MediaContentDescription* desc =
2920 cricket::GetFirstAudioContentDescription(modified_offer->description());
2921 ASSERT_TRUE(desc != NULL);
2922 for (StreamParams& stream : desc->mutable_streams()) {
2923 for (unsigned int& ssrc : stream.ssrcs) {
2924 ++ssrc;
2925 }
2926 }
deadbeefab9b2d12015-10-14 11:33:11 -07002927
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002928 desc =
2929 cricket::GetFirstVideoContentDescription(modified_offer->description());
2930 ASSERT_TRUE(desc != NULL);
2931 for (StreamParams& stream : desc->mutable_streams()) {
2932 for (unsigned int& ssrc : stream.ssrcs) {
2933 ++ssrc;
2934 }
2935 }
2936
Steve Antondb45ca82017-09-11 18:27:34 -07002937 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002938 senders = pc_->GetSenders();
2939 EXPECT_EQ(2u, senders.size());
2940 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2941 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2942 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2943 // changed.
2944}
2945
2946// This tests that the expected behavior occurs if a new session description is
2947// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002948// Don't run under Unified Plan since the stream API is not available.
2949TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002950 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002951 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002952 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002953
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002954 rtc::scoped_refptr<StreamCollection> stream_collection =
2955 CreateStreamCollection(2, 1);
2956 pc_->AddStream(stream_collection->at(0));
2957 std::unique_ptr<SessionDescriptionInterface> offer;
2958 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002959 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002960
deadbeefab9b2d12015-10-14 11:33:11 -07002961 auto senders = pc_->GetSenders();
2962 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002963 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2964 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002965
2966 // Add a new MediaStream but with the same tracks as in the first stream.
2967 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2968 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002969 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2970 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002971 pc_->AddStream(stream_1);
2972
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002973 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002974 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002975
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 auto new_senders = pc_->GetSenders();
2977 // Should be the same senders as before, but with updated stream id.
2978 // Note that this behavior is subject to change in the future.
2979 // We may decide the PC should ignore existing tracks in AddStream.
2980 EXPECT_EQ(senders, new_senders);
2981 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2982 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002983}
2984
zhihuang81c3a032016-11-17 12:06:24 -08002985// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002986TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002987 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002988 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08002989 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2990 EXPECT_EQ(observer_.num_added_tracks_, 1);
2991 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2992
2993 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08002994 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07002995 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08002996 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2997}
2998
deadbeefd1a38b52016-12-10 13:15:33 -08002999// Test that when SetConfiguration is called and the configuration is
3000// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003001TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003002 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003003 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003004 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003005 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003006 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003007 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3008 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003009
3010 // Do initial offer/answer so there's something to restart.
3011 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003012 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003013
3014 // Grab the ufrags.
3015 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3016
3017 // Change ICE policy, which should trigger an ICE restart on the next offer.
3018 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003019 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003020 CreateOfferAsLocalDescription();
3021
3022 // Grab the new ufrags.
3023 std::vector<std::string> subsequent_ufrags =
3024 GetUfrags(pc_->local_description());
3025
3026 // Sanity check.
3027 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3028 // Check that each ufrag is different.
3029 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3030 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3031 }
3032}
3033
3034// Test that when SetConfiguration is called and the configuration *isn't*
3035// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003036TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003037 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003038 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003039 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003040 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003041 config = pc_->GetConfiguration();
3042 AddAudioTrack(kAudioTracks[0]);
3043 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003044
3045 // Do initial offer/answer so there's something to restart.
3046 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003047 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003048
3049 // Grab the ufrags.
3050 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3051
3052 // Call SetConfiguration with a config identical to what the PC was
3053 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003054 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003055 CreateOfferAsLocalDescription();
3056
3057 // Grab the new ufrags.
3058 std::vector<std::string> subsequent_ufrags =
3059 GetUfrags(pc_->local_description());
3060
3061 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3062}
3063
3064// Test for a weird corner case scenario:
3065// 1. Audio/video session established.
3066// 2. SetConfiguration changes ICE config; ICE restart needed.
3067// 3. ICE restart initiated by remote peer, but only for one m= section.
3068// 4. Next createOffer should initiate an ICE restart, but only for the other
3069// m= section; it would be pointless to do an ICE restart for the m= section
3070// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003071TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003072 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003073 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003074 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003075 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003076 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003077 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3078 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003079
3080 // Do initial offer/answer so there's something to restart.
3081 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003082 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003083
3084 // Change ICE policy, which should set the "needs-ice-restart" flag.
3085 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003086 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003087
3088 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003089 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003090 webrtc::CreateSessionDescription(SdpType::kOffer,
3091 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003092 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003093 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3094 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003095 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003096 CreateAnswerAsLocalDescription();
3097
3098 // Grab the ufrags.
3099 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003100 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003101
3102 // Create offer and grab the new ufrags.
3103 CreateOfferAsLocalDescription();
3104 std::vector<std::string> subsequent_ufrags =
3105 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003106 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003107
3108 // Ensure that only the ufrag for the second m= section changed.
3109 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3110 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3111}
3112
deadbeeffe4a8a42016-12-20 17:56:17 -08003113// Tests that the methods to return current/pending descriptions work as
3114// expected at different points in the offer/answer exchange. This test does
3115// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003116TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003117 // This disables DTLS so we can apply an answer to ourselves.
3118 CreatePeerConnection();
3119
3120 // Create initial local offer and get SDP (which will also be used as
3121 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003122 std::unique_ptr<SessionDescriptionInterface> local_offer;
3123 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003124 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003125 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003126
3127 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003128 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3129 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3130 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003131 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3132 EXPECT_EQ(nullptr, pc_->current_local_description());
3133 EXPECT_EQ(nullptr, pc_->current_remote_description());
3134
3135 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003136 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003137 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003138 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3139 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3140 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3141 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003142 EXPECT_EQ(nullptr, pc_->current_local_description());
3143 EXPECT_EQ(nullptr, pc_->current_remote_description());
3144
3145 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003146 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003147 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003148 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3149 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003150 EXPECT_EQ(nullptr, pc_->pending_local_description());
3151 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003152 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3153 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003154
3155 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003156 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003157 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003158 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3159 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3160 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003161 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003162 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3163 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003164
3165 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003166 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003167 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003168 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3169 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3170 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3171 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3172 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3173 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003174
3175 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003176 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003177 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003178 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3179 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003180 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3181 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003182 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3183 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003184}
3185
zhihuang77985012017-02-07 15:45:16 -08003186// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3187// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003188// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003189// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003190TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003191 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3192 CreatePeerConnection();
3193 // The RtcEventLog will be reset when the PeerConnection is closed.
3194 pc_->Close();
3195
Niels Möllerdec9f742019-06-03 15:25:20 +02003196 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003197 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003198 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003199 pc_->StopRtcEventLog();
3200}
3201
deadbeef30952b42017-04-21 02:41:29 -07003202// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003203TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003204 CreatePeerConnection();
3205
3206 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003207 RTCOfferAnswerOptions options;
3208 options.offer_to_receive_audio = 1;
3209 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003210 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003211 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003212 cricket::SessionDescription* desc = offer->description();
3213 ASSERT_EQ(2u, desc->transport_infos().size());
3214 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3215 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3216
3217 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003218 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003219 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003220 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3221 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003222 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003223 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003224 desc = answer->description();
3225 ASSERT_EQ(2u, desc->transport_infos().size());
3226 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3227 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3228}
3229
deadbeef1dcb1642017-03-29 21:08:16 -07003230// Test that ICE renomination isn't offered if it's not enabled in the PC's
3231// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003232TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003233 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003234 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003235 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003236 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003237 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003238
3239 std::unique_ptr<SessionDescriptionInterface> offer;
3240 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3241 cricket::SessionDescription* desc = offer->description();
3242 EXPECT_EQ(1u, desc->transport_infos().size());
3243 EXPECT_FALSE(
3244 desc->transport_infos()[0].description.GetIceParameters().renomination);
3245}
3246
3247// Test that the ICE renomination option is present in generated offers/answers
3248// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003249TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003250 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003251 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003252 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003253 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003254 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003255
3256 std::unique_ptr<SessionDescriptionInterface> offer;
3257 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3258 cricket::SessionDescription* desc = offer->description();
3259 EXPECT_EQ(1u, desc->transport_infos().size());
3260 EXPECT_TRUE(
3261 desc->transport_infos()[0].description.GetIceParameters().renomination);
3262
3263 // Set the offer as a remote description, then create an answer and ensure it
3264 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003265 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003266 std::unique_ptr<SessionDescriptionInterface> answer;
3267 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3268 desc = answer->description();
3269 EXPECT_EQ(1u, desc->transport_infos().size());
3270 EXPECT_TRUE(
3271 desc->transport_infos()[0].description.GetIceParameters().renomination);
3272}
3273
3274// Test that if CreateOffer is called with the deprecated "offer to receive
3275// audio/video" constraints, they're processed and result in an offer with
3276// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003277TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003278 CreatePeerConnection();
3279
Niels Möllerf06f9232018-08-07 12:32:18 +02003280 RTCOfferAnswerOptions options;
3281 options.offer_to_receive_audio = 1;
3282 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003283 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003284 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003285
3286 cricket::SessionDescription* desc = offer->description();
3287 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3288 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3289 ASSERT_NE(nullptr, audio);
3290 ASSERT_NE(nullptr, video);
3291 EXPECT_FALSE(audio->rejected);
3292 EXPECT_FALSE(video->rejected);
3293}
3294
3295// Test that if CreateAnswer is called with the deprecated "offer to receive
3296// audio/video" constraints, they're processed and can be used to reject an
3297// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003298// Don't run under Unified Plan since this behavior is not supported.
3299TEST_F(PeerConnectionInterfaceTestPlanB,
3300 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003301 CreatePeerConnection();
3302
3303 // First, create an offer with audio/video and apply it as a remote
3304 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003305 RTCOfferAnswerOptions options;
3306 options.offer_to_receive_audio = 1;
3307 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003308 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003309 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003310 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003311
3312 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003313 options.offer_to_receive_audio = 0;
3314 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003315 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003316 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003317
3318 cricket::SessionDescription* desc = answer->description();
3319 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3320 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3321 ASSERT_NE(nullptr, audio);
3322 ASSERT_NE(nullptr, video);
3323 EXPECT_TRUE(audio->rejected);
3324 EXPECT_TRUE(video->rejected);
3325}
3326
deadbeef1dcb1642017-03-29 21:08:16 -07003327// Test that negotiation can succeed with a data channel only, and with the max
3328// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003329#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003330TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3331#else
3332TEST_P(PeerConnectionInterfaceTest,
3333 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003334#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003335 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003336 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003337 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003338 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003339
3340 // First, create an offer with only a data channel and apply it as a remote
3341 // description.
Florent Castelli72424402022-04-06 03:45:10 +02003342 pc_->CreateDataChannelOrError("test", nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003343 std::unique_ptr<SessionDescriptionInterface> offer;
3344 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003345 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003346
3347 // Create and set answer as well.
3348 std::unique_ptr<SessionDescriptionInterface> answer;
3349 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003350 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003351}
3352
Steve Anton36da6ff2018-02-16 16:04:20 -08003353TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003354 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003355 BitrateSettings bitrate;
3356 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003357 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3358}
3359
Steve Anton36da6ff2018-02-16 16:04:20 -08003360TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003361 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003362 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003363 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003364 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3365}
3366
Steve Anton36da6ff2018-02-16 16:04:20 -08003367TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003368 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003369 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003370 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003371 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003372 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3373}
3374
Steve Anton36da6ff2018-02-16 16:04:20 -08003375TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003376 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003377 BitrateSettings bitrate;
3378 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003379 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3380}
3381
Steve Anton36da6ff2018-02-16 16:04:20 -08003382TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003383 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003384 BitrateSettings bitrate;
3385 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003386 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003387 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3388}
3389
Steve Anton36da6ff2018-02-16 16:04:20 -08003390TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003391 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003392 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003393 bitrate.min_bitrate_bps = 10;
3394 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003395 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3396}
3397
Steve Anton36da6ff2018-02-16 16:04:20 -08003398TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003399 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003400 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003401 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003402 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3403}
3404
Niels Möller0c4f7be2018-05-07 14:01:37 +02003405// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003406// by Call's BitrateConstraints, which comes from the SDP or a default value.
3407// This test checks that a call to SetBitrate with a current bitrate that will
3408// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003409TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003410 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003411 BitrateSettings bitrate;
3412 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003413 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3414}
3415
zhihuang1c378ed2017-08-17 14:10:50 -07003416// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003417TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003418 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3419 RTCOfferAnswerOptions rtc_options;
3420
3421 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3422 // than kMaxOfferToReceiveMedia should be treated as invalid.
3423 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3424 CreatePeerConnection();
3425 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3426
3427 rtc_options.offer_to_receive_audio =
3428 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3429 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3430}
3431
Steve Anton36da6ff2018-02-16 16:04:20 -08003432TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003433 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3434 RTCOfferAnswerOptions rtc_options;
3435
3436 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3437 // than kMaxOfferToReceiveMedia should be treated as invalid.
3438 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3439 CreatePeerConnection();
3440 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3441
3442 rtc_options.offer_to_receive_video =
3443 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3444 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3445}
3446
3447// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003448// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003449TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003450 RTCOfferAnswerOptions rtc_options;
3451 rtc_options.offer_to_receive_audio = 1;
3452 rtc_options.offer_to_receive_video = 1;
3453
3454 std::unique_ptr<SessionDescriptionInterface> offer;
3455 CreatePeerConnection();
3456 offer = CreateOfferWithOptions(rtc_options);
3457 ASSERT_TRUE(offer);
3458 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3459 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3460}
3461
3462// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003463// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003464TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003465 RTCOfferAnswerOptions rtc_options;
3466 rtc_options.offer_to_receive_audio = 1;
3467 rtc_options.offer_to_receive_video = 0;
3468
3469 std::unique_ptr<SessionDescriptionInterface> offer;
3470 CreatePeerConnection();
3471 offer = CreateOfferWithOptions(rtc_options);
3472 ASSERT_TRUE(offer);
3473 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3474 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3475}
3476
Artem Titov880fa812021-07-30 22:30:23 +02003477// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003478// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003479TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003480 RTCOfferAnswerOptions rtc_options;
3481 rtc_options.offer_to_receive_audio = 0;
3482 rtc_options.offer_to_receive_video = 1;
3483
3484 std::unique_ptr<SessionDescriptionInterface> offer;
3485 CreatePeerConnection();
3486 offer = CreateOfferWithOptions(rtc_options);
3487 ASSERT_TRUE(offer);
3488 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3489 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3490}
3491
zhihuang1c378ed2017-08-17 14:10:50 -07003492// Test that no media content will be added to the offer if using default
3493// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003494TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003495 RTCOfferAnswerOptions rtc_options;
3496
3497 std::unique_ptr<SessionDescriptionInterface> offer;
3498 CreatePeerConnection();
3499 offer = CreateOfferWithOptions(rtc_options);
3500 ASSERT_TRUE(offer);
3501 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3502 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3503}
3504
Artem Titov880fa812021-07-30 22:30:23 +02003505// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003506// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003507TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3508 CreatePeerConnection();
3509
zhihuang1c378ed2017-08-17 14:10:50 -07003510 RTCOfferAnswerOptions rtc_options;
3511 rtc_options.ice_restart = false;
3512 rtc_options.offer_to_receive_audio = 1;
3513
3514 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003515 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003516 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3517 auto ufrag1 =
3518 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3519 auto pwd1 =
3520 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003521
Artem Titov880fa812021-07-30 22:30:23 +02003522 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003523 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003524 auto ufrag2 =
3525 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3526 auto pwd2 =
3527 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003528
Artem Titov880fa812021-07-30 22:30:23 +02003529 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003530 rtc_options.ice_restart = true;
3531 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003532 auto ufrag3 =
3533 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3534 auto pwd3 =
3535 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003536
3537 EXPECT_EQ(ufrag1, ufrag2);
3538 EXPECT_EQ(pwd1, pwd2);
3539 EXPECT_NE(ufrag2, ufrag3);
3540 EXPECT_NE(pwd2, pwd3);
3541}
3542
Artem Titov880fa812021-07-30 22:30:23 +02003543// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003544// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003545TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003546 RTCOfferAnswerOptions rtc_options;
3547 rtc_options.offer_to_receive_audio = 1;
3548 rtc_options.offer_to_receive_video = 1;
3549
3550 std::unique_ptr<SessionDescriptionInterface> offer;
3551 CreatePeerConnection();
3552
3553 rtc_options.use_rtp_mux = true;
3554 offer = CreateOfferWithOptions(rtc_options);
3555 ASSERT_TRUE(offer);
3556 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3557 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3558 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3559
3560 rtc_options.use_rtp_mux = false;
3561 offer = CreateOfferWithOptions(rtc_options);
3562 ASSERT_TRUE(offer);
3563 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3564 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3565 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3566}
3567
zhihuang141aacb2017-08-29 13:23:53 -07003568// This test ensures OnRenegotiationNeeded is called when we add track with
3569// MediaStream -> AddTrack in the same way it is called when we add track with
3570// PeerConnection -> AddTrack.
3571// The test can be removed once addStream is rewritten in terms of addTrack
3572// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003573// Don't run under Unified Plan since the stream API is not available.
3574TEST_F(PeerConnectionInterfaceTestPlanB,
3575 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003576 CreatePeerConnectionWithoutDtls();
3577 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003578 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003579 pc_->AddStream(stream);
3580 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003581 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003582 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003583 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003584 stream->AddTrack(audio_track);
3585 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3586 observer_.renegotiation_needed_ = false;
3587
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003588 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003589 stream->AddTrack(video_track);
3590 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3591 observer_.renegotiation_needed_ = false;
3592
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003593 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003594 stream->RemoveTrack(audio_track);
3595 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3596 observer_.renegotiation_needed_ = false;
3597
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003598 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003599 stream->RemoveTrack(video_track);
3600 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3601 observer_.renegotiation_needed_ = false;
3602}
3603
Zhi Huangb2d355e2017-10-26 17:26:37 -07003604// Tests that an error is returned if a description is applied that has fewer
3605// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003606TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003607 MediaSectionCountEnforcedForSubsequentOffer) {
3608 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003609 AddAudioTrack("audio_label");
3610 AddVideoTrack("video_label");
3611
Zhi Huangb2d355e2017-10-26 17:26:37 -07003612 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003613 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003614 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3615
3616 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003617 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003618 offer->description()->contents().pop_back();
3619 offer->description()->contents().pop_back();
3620 ASSERT_TRUE(offer->description()->contents().empty());
3621 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3622
3623 std::unique_ptr<SessionDescriptionInterface> answer;
3624 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3625 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3626
3627 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003628 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003629 offer->description()->contents().pop_back();
3630 offer->description()->contents().pop_back();
3631 ASSERT_TRUE(offer->description()->contents().empty());
3632 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3633}
3634
Johannes Kron89f874e2018-11-12 10:25:48 +01003635TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3636 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003637 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003638 CreatePeerConnection(config);
3639 std::unique_ptr<SessionDescriptionInterface> offer;
3640 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003641 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3642 // Possible to set to false.
3643 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003644 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003645 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003646 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003647 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003648}
3649
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003650INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3651 PeerConnectionInterfaceTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003652 Values(SdpSemantics::kPlanB_DEPRECATED,
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003653 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003654
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003655class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003656 protected:
3657 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003658 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003659 }
nisseeaabdf62017-05-05 02:23:02 -07003660 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003661 const RTCConfiguration& config) {
Florent Castelli72424402022-04-06 03:45:10 +02003662 PeerConnectionDependencies pc_dependencies(&observer_);
3663 auto result =
3664 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3665 EXPECT_TRUE(result.ok());
3666 observer_.SetPeerConnectionInterface(result.value());
3667 return result.value()->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;
Florent Castelli72424402022-04-06 03:45:10 +02003678 PeerConnectionDependencies pc_dependencies(&observer_);
3679 auto result =
3680 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3681 EXPECT_TRUE(result.ok());
3682 observer_.SetPeerConnectionInterface(result.value());
3683 result.value()->Close(); // No abort -> ok.
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003684 SUCCEED();
3685}
3686
nisse51542be2016-02-12 02:27:06 -08003687// This test verifies the default behaviour with no constraints and a
3688// default RTCConfiguration.
3689TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3690 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003691 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003692
Niels Möllerf06f9232018-08-07 12:32:18 +02003693 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003694
3695 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003696 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3697 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003698 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003699 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003700}
3701
Niels Möller1d7ecd22018-01-18 15:25:12 +01003702// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003703// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003704TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3705 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003706 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003707
Niels Möller71bdda02016-03-31 12:59:59 +02003708 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003709 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003710
Niels Möller1d7ecd22018-01-18 15:25:12 +01003711 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003712}
3713
Niels Möller6539f692018-01-18 08:58:50 +01003714// This test verifies that the experiment_cpu_load_estimator flag is
3715// propagated from RTCConfiguration to the PeerConnection.
3716TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3717 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003718 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Niels Möller6539f692018-01-18 08:58:50 +01003719
3720 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003721 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003722
3723 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3724}
3725
deadbeef293e9262017-01-11 12:28:30 -08003726// Tests a few random fields being different.
3727TEST(RTCConfigurationTest, ComparisonOperators) {
3728 PeerConnectionInterface::RTCConfiguration a;
3729 PeerConnectionInterface::RTCConfiguration b;
3730 EXPECT_EQ(a, b);
3731
3732 PeerConnectionInterface::RTCConfiguration c;
3733 c.servers.push_back(PeerConnectionInterface::IceServer());
3734 EXPECT_NE(a, c);
3735
3736 PeerConnectionInterface::RTCConfiguration d;
3737 d.type = PeerConnectionInterface::kRelay;
3738 EXPECT_NE(a, d);
3739
3740 PeerConnectionInterface::RTCConfiguration e;
3741 e.audio_jitter_buffer_max_packets = 5;
3742 EXPECT_NE(a, e);
3743
3744 PeerConnectionInterface::RTCConfiguration f;
3745 f.ice_connection_receiving_timeout = 1337;
3746 EXPECT_NE(a, f);
3747
3748 PeerConnectionInterface::RTCConfiguration g;
3749 g.disable_ipv6 = true;
3750 EXPECT_NE(a, g);
3751
3752 PeerConnectionInterface::RTCConfiguration h(
3753 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3754 EXPECT_NE(a, h);
3755}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003756
3757} // namespace
3758} // namespace webrtc