blob: e4c1ce25df44ac5b36dc78ac1e49ffca77390b5c [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/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080076#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010077#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080078#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020079#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010080#include "test/gtest.h"
kwibergac9f8762016-09-30 22:29:43 -070081
82#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080083#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070084#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010086namespace webrtc {
87namespace {
88
Seth Hampson845e8782018-03-02 11:34:10 -080089static const char kStreamId1[] = "local_stream_1";
90static const char kStreamId2[] = "local_stream_2";
91static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092static const int kDefaultStunPort = 3478;
93static const char kStunAddressOnly[] = "stun:address";
94static const char kStunInvalidPort[] = "stun:address:-1";
95static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
96static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +010097static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098static const char kTurnUsername[] = "user";
99static const char kTurnPassword[] = "password";
100static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
deadbeefab9b2d12015-10-14 11:33:11 -0700103static const char kStreams[][8] = {"stream1", "stream2"};
104static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
105static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
106
deadbeef5e97fb52015-10-15 12:49:08 -0700107static const char kRecvonly[] = "recvonly";
108static const char kSendrecv[] = "sendrecv";
Philipp Hanckefd91d022022-10-27 20:08:23 +0200109constexpr uint64_t kTiebreakerDefault = 44444;
deadbeef5e97fb52015-10-15 12:49:08 -0700110
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"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000118 "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"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000126 "a=rtpmap:103 ISAC/16000\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700127 "a=ssrc:1 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000128 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700129 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800130 "a=ice-ufrag:e5785931\r\n"
131 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700135 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800136 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:120 VP8/90000\r\n"
138 "a=ssrc:2 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000139 "a=ssrc:2 msid:stream1 videotrack0\r\n";
140// Same string as above but with the MID changed to the Unified Plan default and
141// a=msid added. This is needed so that this SDP can be used as an answer for a
142// Unified Plan offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800143static const char kSdpStringWithStream1UnifiedPlan[] =
144 "v=0\r\n"
145 "o=- 0 0 IN IP4 127.0.0.1\r\n"
146 "s=-\r\n"
147 "t=0 0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000148 "m=audio 1 RTP/AVPF 103\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
153 "a=mid:0\r\n"
154 "a=sendrecv\r\n"
155 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000156 "a=rtpmap:103 ISAC/16000\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000157 "a=msid:stream1 audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800158 "a=ssrc:1 cname:stream1\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800159 "m=video 1 RTP/AVPF 120\r\n"
160 "a=ice-ufrag:e5785931\r\n"
161 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
162 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
163 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
164 "a=mid:1\r\n"
165 "a=sendrecv\r\n"
166 "a=rtcp-mux\r\n"
167 "a=rtpmap:120 VP8/90000\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000168 "a=msid:stream1 videotrack0\r\n"
169 "a=ssrc:2 cname:stream1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700170
zhihuang81c3a032016-11-17 12:06:24 -0800171// Reference SDP with a MediaStream with label "stream1" and audio track with
172// id "audio_1";
173static const char kSdpStringWithStream1AudioTrackOnly[] =
174 "v=0\r\n"
175 "o=- 0 0 IN IP4 127.0.0.1\r\n"
176 "s=-\r\n"
177 "t=0 0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000178 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800179 "a=ice-ufrag:e5785931\r\n"
180 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
181 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
182 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800183 "a=mid:audio\r\n"
184 "a=sendrecv\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000185 "a=rtpmap:103 ISAC/16000\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800186 "a=ssrc:1 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000187 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800188 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800189
deadbeefab9b2d12015-10-14 11:33:11 -0700190// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
191// MediaStreams have one audio track and one video track.
192// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800193static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "v=0\r\n"
195 "o=- 0 0 IN IP4 127.0.0.1\r\n"
196 "s=-\r\n"
197 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800198 "a=msid-semantic: WMS stream1 stream2\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000199 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=ice-ufrag:e5785931\r\n"
201 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
202 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
203 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700204 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700205 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800206 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000207 "a=rtpmap:103 ISAC/16000\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700208 "a=ssrc:1 cname:stream1\r\n"
209 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
210 "a=ssrc:3 cname:stream2\r\n"
211 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
212 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800213 "a=ice-ufrag:e5785931\r\n"
214 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
215 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
216 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700217 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700218 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800219 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=rtpmap:120 VP8/0\r\n"
221 "a=ssrc:2 cname:stream1\r\n"
222 "a=ssrc:2 msid:stream1 videotrack0\r\n"
223 "a=ssrc:4 cname:stream2\r\n"
224 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800225static const char kSdpStringWithStream1And2UnifiedPlan[] =
226 "v=0\r\n"
227 "o=- 0 0 IN IP4 127.0.0.1\r\n"
228 "s=-\r\n"
229 "t=0 0\r\n"
230 "a=msid-semantic: WMS stream1 stream2\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000231 "m=audio 1 RTP/AVPF 103\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800232 "a=ice-ufrag:e5785931\r\n"
233 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
234 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
235 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
236 "a=mid:0\r\n"
237 "a=sendrecv\r\n"
238 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000239 "a=rtpmap:103 ISAC/16000\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800240 "a=ssrc:1 cname:stream1\r\n"
241 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
242 "m=video 1 RTP/AVPF 120\r\n"
243 "a=ice-ufrag:e5785931\r\n"
244 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
245 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
246 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
247 "a=mid:1\r\n"
248 "a=sendrecv\r\n"
249 "a=rtcp-mux\r\n"
250 "a=rtpmap:120 VP8/0\r\n"
251 "a=ssrc:2 cname:stream1\r\n"
252 "a=ssrc:2 msid:stream1 videotrack0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000253 "m=audio 1 RTP/AVPF 103\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800254 "a=ice-ufrag:e5785931\r\n"
255 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
256 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
257 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
258 "a=mid:2\r\n"
259 "a=sendrecv\r\n"
260 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000261 "a=rtpmap:103 ISAC/16000\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800262 "a=ssrc:3 cname:stream2\r\n"
263 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
264 "m=video 1 RTP/AVPF 120\r\n"
265 "a=ice-ufrag:e5785931\r\n"
266 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
267 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
268 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
269 "a=mid:3\r\n"
270 "a=sendrecv\r\n"
271 "a=rtcp-mux\r\n"
272 "a=rtpmap:120 VP8/0\r\n"
273 "a=ssrc:4 cname:stream2\r\n"
274 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700275
276// Reference SDP without MediaStreams. Msid is not supported.
277static const char kSdpStringWithoutStreams[] =
278 "v=0\r\n"
279 "o=- 0 0 IN IP4 127.0.0.1\r\n"
280 "s=-\r\n"
281 "t=0 0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000282 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700283 "a=ice-ufrag:e5785931\r\n"
284 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
285 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
286 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700287 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700288 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800289 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000290 "a=rtpmap:103 ISAC/16000\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700291 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800292 "a=ice-ufrag:e5785931\r\n"
293 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
294 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
295 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700296 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700297 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800298 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700299 "a=rtpmap:120 VP8/90000\r\n";
300
301// Reference SDP without MediaStreams. Msid is supported.
302static const char kSdpStringWithMsidWithoutStreams[] =
303 "v=0\r\n"
304 "o=- 0 0 IN IP4 127.0.0.1\r\n"
305 "s=-\r\n"
306 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800307 "a=msid-semantic: WMS\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000308 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700309 "a=ice-ufrag:e5785931\r\n"
310 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
311 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
312 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700313 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700314 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800315 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000316 "a=rtpmap:103 ISAC/16000\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700317 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800318 "a=ice-ufrag:e5785931\r\n"
319 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
320 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
321 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700322 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700323 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800324 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700325 "a=rtpmap:120 VP8/90000\r\n";
326
327// Reference SDP without MediaStreams and audio only.
328static const char kSdpStringWithoutStreamsAudioOnly[] =
329 "v=0\r\n"
330 "o=- 0 0 IN IP4 127.0.0.1\r\n"
331 "s=-\r\n"
332 "t=0 0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000333 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700334 "a=ice-ufrag:e5785931\r\n"
335 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
336 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
337 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700338 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700339 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800340 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000341 "a=rtpmap:103 ISAC/16000\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700342
343// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
344static const char kSdpStringSendOnlyWithoutStreams[] =
345 "v=0\r\n"
346 "o=- 0 0 IN IP4 127.0.0.1\r\n"
347 "s=-\r\n"
348 "t=0 0\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000349 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700350 "a=ice-ufrag:e5785931\r\n"
351 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
352 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
353 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700354 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700355 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700356 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800357 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000358 "a=rtpmap:103 ISAC/16000\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700359 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800360 "a=ice-ufrag:e5785931\r\n"
361 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
362 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
363 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700364 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700365 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700366 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800367 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700368 "a=rtpmap:120 VP8/90000\r\n";
369
370static const char kSdpStringInit[] =
371 "v=0\r\n"
372 "o=- 0 0 IN IP4 127.0.0.1\r\n"
373 "s=-\r\n"
374 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700375 "a=msid-semantic: WMS\r\n";
376
377static const char kSdpStringAudio[] =
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000378 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800379 "a=ice-ufrag:e5785931\r\n"
380 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
381 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
382 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700383 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700384 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800385 "a=rtcp-mux\r\n"
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000386 "a=rtpmap:103 ISAC/16000\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700387
388static const char kSdpStringVideo[] =
389 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800390 "a=ice-ufrag:e5785931\r\n"
391 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
392 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
393 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700394 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700395 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800396 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700397 "a=rtpmap:120 VP8/90000\r\n";
398
399static const char kSdpStringMs1Audio0[] =
400 "a=ssrc:1 cname:stream1\r\n"
401 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
402
403static const char kSdpStringMs1Video0[] =
404 "a=ssrc:2 cname:stream1\r\n"
405 "a=ssrc:2 msid:stream1 videotrack0\r\n";
406
407static const char kSdpStringMs1Audio1[] =
408 "a=ssrc:3 cname:stream1\r\n"
409 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
410
411static const char kSdpStringMs1Video1[] =
412 "a=ssrc:4 cname:stream1\r\n"
413 "a=ssrc:4 msid:stream1 videotrack1\r\n";
414
deadbeef8662f942017-01-20 21:20:51 -0800415static const char kDtlsSdesFallbackSdp[] =
416 "v=0\r\n"
417 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
418 "s=-\r\n"
419 "c=IN IP4 0.0.0.0\r\n"
420 "t=0 0\r\n"
421 "a=group:BUNDLE audio\r\n"
422 "a=msid-semantic: WMS\r\n"
423 "m=audio 1 RTP/SAVPF 0\r\n"
424 "a=sendrecv\r\n"
425 "a=rtcp-mux\r\n"
426 "a=mid:audio\r\n"
427 "a=ssrc:1 cname:stream1\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800428 "a=ice-ufrag:e5785931\r\n"
429 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
430 "a=rtpmap:0 pcmu/8000\r\n"
431 "a=fingerprint:sha-1 "
432 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
433 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700434 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800435 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
436 "dummy_session_params\r\n";
437
Niels Möllerdec9f742019-06-03 15:25:20 +0200438class RtcEventLogOutputNull final : public RtcEventLogOutput {
439 public:
440 bool IsActive() const override { return true; }
Björn Terelius63299a32022-07-05 10:58:52 +0200441 bool Write(const absl::string_view /*output*/) override { return true; }
Niels Möllerdec9f742019-06-03 15:25:20 +0200442};
443
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100444using ::cricket::StreamParams;
Jonas Oreland4b2a1062022-10-19 09:24:42 +0200445using ::testing::Eq;
perkjd61bf802016-03-24 03:16:19 -0700446using ::testing::Exactly;
Jonas Oreland4b2a1062022-10-19 09:24:42 +0200447using ::testing::SizeIs;
Steve Anton36da6ff2018-02-16 16:04:20 -0800448using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449
Steve Anton36da6ff2018-02-16 16:04:20 -0800450using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
451using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453// Gets the first ssrc of given content type from the ContentInfo.
454bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
455 if (!content_info || !ssrc) {
456 return false;
457 }
458 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800459 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 if (!media_desc || media_desc->streams().empty()) {
461 return false;
462 }
463 *ssrc = media_desc->streams().begin()->first_ssrc();
464 return true;
465}
466
deadbeefd1a38b52016-12-10 13:15:33 -0800467// Get the ufrags out of an SDP blob. Useful for testing ICE restart
468// behavior.
469std::vector<std::string> GetUfrags(
470 const webrtc::SessionDescriptionInterface* desc) {
471 std::vector<std::string> ufrags;
472 for (const cricket::TransportInfo& info :
473 desc->description()->transport_infos()) {
474 ufrags.push_back(info.description.ice_ufrag);
475 }
476 return ufrags;
477}
478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479void SetSsrcToZero(std::string* sdp) {
480 const char kSdpSsrcAtribute[] = "a=ssrc:";
481 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
482 size_t ssrc_pos = 0;
483 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200484 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 size_t end_ssrc = sdp->find(" ", ssrc_pos);
486 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
487 ssrc_pos = end_ssrc;
488 }
489}
490
Artem Titov880fa812021-07-30 22:30:23 +0200491// Check if `streams` contains the specified track.
deadbeefab9b2d12015-10-14 11:33:11 -0700492bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800493 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700494 const std::string& track_id) {
495 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800496 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700497 return true;
498 }
499 }
500 return false;
501}
502
Artem Titov880fa812021-07-30 22:30:23 +0200503// Check if `senders` contains the specified sender, by id.
deadbeefab9b2d12015-10-14 11:33:11 -0700504bool ContainsSender(
505 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
506 const std::string& id) {
507 for (const auto& sender : senders) {
508 if (sender->id() == id) {
509 return true;
510 }
511 }
512 return false;
513}
514
Artem Titov880fa812021-07-30 22:30:23 +0200515// Check if `senders` contains the specified sender, by id and stream id.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700516bool ContainsSender(
517 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
518 const std::string& id,
519 const std::string& stream_id) {
520 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700521 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700522 return true;
523 }
524 }
525 return false;
526}
527
deadbeefab9b2d12015-10-14 11:33:11 -0700528// Create a collection of streams.
529// CreateStreamCollection(1) creates a collection that
530// correspond to kSdpStringWithStream1.
531// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
532rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700533 int number_of_streams,
534 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700535 rtc::scoped_refptr<StreamCollection> local_collection(
536 StreamCollection::Create());
537
538 for (int i = 0; i < number_of_streams; ++i) {
539 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
540 webrtc::MediaStream::Create(kStreams[i]));
541
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700542 for (int j = 0; j < tracks_per_stream; ++j) {
543 // Add a local audio track.
544 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
545 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
546 nullptr));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000547 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700548
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700549 // Add a local video track.
550 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
551 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700552 webrtc::FakeVideoTrackSource::Create(),
553 rtc::Thread::Current()));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000554 stream->AddTrack(video_track);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700555 }
deadbeefab9b2d12015-10-14 11:33:11 -0700556
557 local_collection->AddStream(stream);
558 }
559 return local_collection;
560}
561
562// Check equality of StreamCollections.
563bool CompareStreamCollections(StreamCollectionInterface* s1,
564 StreamCollectionInterface* s2) {
565 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
566 return false;
567 }
568
569 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700570 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700571 return false;
572 }
573 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
574 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
575 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
576 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
577
578 if (audio_tracks1.size() != audio_tracks2.size()) {
579 return false;
580 }
581 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
582 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
583 return false;
584 }
585 }
586 if (video_tracks1.size() != video_tracks2.size()) {
587 return false;
588 }
589 for (size_t j = 0; j != video_tracks1.size(); ++j) {
590 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
591 return false;
592 }
593 }
594 }
595 return true;
596}
597
perkjd61bf802016-03-24 03:16:19 -0700598// Helper class to test Observer.
599class MockTrackObserver : public ObserverInterface {
600 public:
601 explicit MockTrackObserver(NotifierInterface* notifier)
602 : notifier_(notifier) {
603 notifier_->RegisterObserver(this);
604 }
605
606 ~MockTrackObserver() { Unregister(); }
607
608 void Unregister() {
609 if (notifier_) {
610 notifier_->UnregisterObserver(this);
611 notifier_ = nullptr;
612 }
613 }
614
Danil Chapovalov3a353122020-05-15 11:16:53 +0200615 MOCK_METHOD(void, OnChanged, (), (override));
perkjd61bf802016-03-24 03:16:19 -0700616
617 private:
618 NotifierInterface* notifier_;
619};
620
nisse528b7932017-05-08 03:21:43 -0700621// The PeerConnectionMediaConfig tests below verify that configuration and
622// constraints are propagated into the PeerConnection's MediaConfig. These
623// settings are intended for MediaChannel constructors, but that is not
624// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700625class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
626 public:
zhihuang38ede132017-06-15 12:52:32 -0700627 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
628 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100629 PeerConnectionFactoryDependencies dependencies;
630 dependencies.worker_thread = rtc::Thread::Current();
631 dependencies.network_thread = rtc::Thread::Current();
632 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200633 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200634 dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200635 cricket::MediaEngineDependencies media_deps;
636 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
henrika919dc2e2017-10-12 14:24:55 +0200637 // Use fake audio device module since we're only testing the interface
638 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200639 media_deps.adm = FakeAudioCaptureModule::Create();
640 SetMediaEngineDefaults(&media_deps);
Erik Språngceb44952020-09-22 11:36:35 +0200641 media_deps.trials = dependencies.trials.get();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200642 dependencies.media_engine =
643 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100644 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200645 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200646 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700647
Tommi87f70902021-04-27 14:43:08 +0200648 return rtc::make_ref_counted<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100649 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700650 }
651
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100652 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800653
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100654 private:
deadbeefd7850b22017-08-23 10:59:19 -0700655 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700656};
657
Steve Anton36da6ff2018-02-16 16:04:20 -0800658// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200659class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800661 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
662 : vss_(new rtc::VirtualSocketServer()),
663 main_(vss_.get()),
664 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800665#ifdef WEBRTC_ANDROID
666 webrtc::InitializeAndroidObjects();
667#endif
668 }
669
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200670 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700671 // Use fake audio capture module since we're only testing the interface
672 // level, and using a real one could make tests flaky when run in parallel.
673 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700675 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200676 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
677 fake_audio_capture_module_),
678 webrtc::CreateBuiltinAudioEncoderFactory(),
679 webrtc::CreateBuiltinAudioDecoderFactory(),
680 webrtc::CreateBuiltinVideoEncoderFactory(),
681 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
682 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700683 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700684 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700685 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 }
687
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200688 void TearDown() override {
689 if (pc_)
690 pc_->Close();
691 }
692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200694 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000697 // DTLS does not work in a loopback call, so is disabled for many
deadbeef293e9262017-01-11 12:28:30 -0800698 // tests in this file.
699 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200700 RTCConfiguration config;
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000701 PeerConnectionFactoryInterface::Options options;
702 options.disable_encryption = true;
703 pc_factory_->SetOptions(options);
Niels Möllerf06f9232018-08-07 12:32:18 +0200704 CreatePeerConnection(config);
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000705 options.disable_encryption = false;
706 pc_factory_->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709 void CreatePeerConnectionWithIceTransportsType(
710 PeerConnectionInterface::IceTransportsType type) {
711 PeerConnectionInterface::RTCConfiguration config;
712 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200713 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700714 }
715
716 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100717 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700721 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100722 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700723 server.password = password;
724 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200725 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727
Niels Möllerf06f9232018-08-07 12:32:18 +0200728 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200729 if (pc_) {
730 pc_->Close();
731 pc_ = nullptr;
732 }
kwibergd1fe2812016-04-27 06:47:29 -0700733 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Byoungchan Leed58f5262022-06-27 18:05:22 +0900734 new cricket::FakePortAllocator(
735 rtc::Thread::Current(),
736 std::make_unique<rtc::BasicPacketSocketFactory>(vss_.get())));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800737 port_allocator_ = port_allocator.get();
Philipp Hanckefd91d022022-10-27 20:08:23 +0200738 port_allocator_->SetIceTiebreaker(kTiebreakerDefault);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000739
deadbeef1dcb1642017-03-29 21:08:16 -0700740 // Create certificate generator unless DTLS constraint is explicitly set to
741 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200742 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200743
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000744 // These won't be used if encryption is turned off, but that's harmless.
745 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
746 cert_generator.reset(fake_certificate_generator_);
747
Steve Anton36da6ff2018-02-16 16:04:20 -0800748 RTCConfiguration modified_config = config;
749 modified_config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200750 PeerConnectionDependencies pc_dependencies(&observer_);
751 pc_dependencies.cert_generator = std::move(cert_generator);
752 pc_dependencies.allocator = std::move(port_allocator);
753 auto result = pc_factory_->CreatePeerConnectionOrError(
754 modified_config, std::move(pc_dependencies));
755 ASSERT_TRUE(result.ok());
756 pc_ = result.MoveValue();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 observer_.SetPeerConnectionInterface(pc_.get());
758 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
759 }
760
deadbeef0a6c4ca2015-10-06 11:38:28 -0700761 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800762 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700763 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700764 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800765 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800766 config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200767 PeerConnectionDependencies pc_dependencies(&observer_);
768 auto result = pc_factory_->CreatePeerConnectionOrError(
769 config, std::move(pc_dependencies));
770 EXPECT_FALSE(result.ok());
deadbeef0a6c4ca2015-10-06 11:38:28 -0700771 }
772
Steve Anton038834f2017-07-14 15:59:59 -0700773 void CreatePeerConnectionExpectFail(
774 PeerConnectionInterface::RTCConfiguration config) {
775 PeerConnectionInterface::IceServer server;
776 server.uri = kTurnIceServerUri;
777 server.password = kTurnPassword;
778 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800779 config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200780 PeerConnectionDependencies pc_dependencies(&observer_);
781 auto result = pc_factory_->CreatePeerConnectionOrError(
782 config, std::move(pc_dependencies));
783 EXPECT_FALSE(result.ok());
Steve Anton038834f2017-07-14 15:59:59 -0700784 }
785
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100787 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800788 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
789 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
790 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800792 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
deadbeef0a6c4ca2015-10-06 11:38:28 -0700794 CreatePeerConnectionExpectFail(kStunInvalidPort);
795 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
796 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797
Niels Möllerdb4def92019-03-18 16:53:59 +0100798 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
799 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
801 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800803 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800807 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 }
809
810 void ReleasePeerConnection() {
Niels Möllerafb246b2022-04-20 14:26:50 +0200811 pc_ = nullptr;
812 observer_.SetPeerConnectionInterface(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814
Steve Anton36da6ff2018-02-16 16:04:20 -0800815 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
816 const std::string& label) {
Niels Möllerafb246b2022-04-20 14:26:50 +0200817 return pc_factory_->CreateVideoTrack(label,
818 FakeVideoTrackSource::Create().get());
Steve Anton36da6ff2018-02-16 16:04:20 -0800819 }
820
821 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800822 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800824 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800825 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
Steve Anton36da6ff2018-02-16 16:04:20 -0800828 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700829 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 pc_factory_->CreateLocalMediaStream(label));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000831 stream->AddTrack(CreateVideoTrack(label + "v0"));
Niels Möllerafb246b2022-04-20 14:26:50 +0200832 ASSERT_TRUE(pc_->AddStream(stream.get()));
Steve Anton36da6ff2018-02-16 16:04:20 -0800833 }
834
835 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
836 const std::string& label) {
837 return pc_factory_->CreateAudioTrack(label, nullptr);
838 }
839
840 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800841 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800842 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800843 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800844 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800845 }
846
847 void AddAudioStream(const std::string& label) {
848 rtc::scoped_refptr<MediaStreamInterface> stream(
849 pc_factory_->CreateLocalMediaStream(label));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000850 stream->AddTrack(CreateAudioTrack(label + "a0"));
Niels Möllerafb246b2022-04-20 14:26:50 +0200851 ASSERT_TRUE(pc_->AddStream(stream.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
853
Seth Hampson845e8782018-03-02 11:34:10 -0800854 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 const std::string& audio_track_label,
856 const std::string& video_track_label) {
857 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700858 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800859 pc_factory_->CreateLocalMediaStream(stream_id));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000860 stream->AddTrack(CreateAudioTrack(audio_track_label));
861 stream->AddTrack(CreateVideoTrack(video_track_label));
Niels Möllerafb246b2022-04-20 14:26:50 +0200862 ASSERT_TRUE(pc_->AddStream(stream.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
864
Steve Anton36da6ff2018-02-16 16:04:20 -0800865 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
866 cricket::MediaType media_type) {
867 for (auto receiver : pc_->GetReceivers()) {
868 if (receiver->media_type() == media_type) {
869 return receiver;
870 }
871 }
872 return nullptr;
873 }
874
kwibergd1fe2812016-04-27 06:47:29 -0700875 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200876 const RTCOfferAnswerOptions* options,
877 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200878 auto observer =
879 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 if (offer) {
Niels Möllerafb246b2022-04-20 14:26:50 +0200881 pc_->CreateOffer(observer.get(),
882 options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 } else {
Niels Möllerafb246b2022-04-20 14:26:50 +0200884 pc_->CreateAnswer(observer.get(),
885 options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
887 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700888 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 return observer->result();
890 }
891
kwibergd1fe2812016-04-27 06:47:29 -0700892 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200893 const RTCOfferAnswerOptions* options) {
894 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
896
kwibergd1fe2812016-04-27 06:47:29 -0700897 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200898 const RTCOfferAnswerOptions* options) {
899 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901
Steve Antondb45ca82017-09-11 18:27:34 -0700902 bool DoSetSessionDescription(
903 std::unique_ptr<SessionDescriptionInterface> desc,
904 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200905 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 if (local) {
Niels Möllerafb246b2022-04-20 14:26:50 +0200907 pc_->SetLocalDescription(observer.get(), desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 } else {
Niels Möllerafb246b2022-04-20 14:26:50 +0200909 pc_->SetRemoteDescription(observer.get(), desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
zhihuang29ff8442016-07-27 11:07:25 -0700911 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
912 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
913 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 return observer->result();
915 }
916
Steve Antondb45ca82017-09-11 18:27:34 -0700917 bool DoSetLocalDescription(
918 std::unique_ptr<SessionDescriptionInterface> desc) {
919 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921
Steve Antondb45ca82017-09-11 18:27:34 -0700922 bool DoSetRemoteDescription(
923 std::unique_ptr<SessionDescriptionInterface> desc) {
924 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
927 // Calls PeerConnection::GetStats and check the return value.
928 // It does not verify the values in the StatReports since a RTCP packet might
929 // be required.
930 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200931 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Niels Möllerafb246b2022-04-20 14:26:50 +0200932 if (!pc_->GetStats(observer.get(), track,
Yves Gerey665174f2018-06-19 15:03:05 +0200933 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 return false;
935 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
936 return observer->called();
937 }
938
Harald Alvestrand89061872018-01-02 14:08:34 +0100939 // Call the standards-compliant GetStats function.
940 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200941 auto callback =
942 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Niels Möllerafb246b2022-04-20 14:26:50 +0200943 pc_->GetStats(callback.get());
Harald Alvestrand89061872018-01-02 14:08:34 +0100944 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
945 return callback->called();
946 }
947
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800949 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 // Create a local stream with audio&video tracks.
Florent Castelli15a38de2022-04-06 00:38:21 +0200951 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson845e8782018-03-02 11:34:10 -0800952 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800953 } else {
954 // Unified Plan does not support AddStream, so just add an audio and video
955 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800956 AddAudioTrack(kAudioTracks[0], {kStreamId1});
957 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800958 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 CreateOfferReceiveAnswer();
960 }
961
962 // Verify that RTP Header extensions has been negotiated for audio and video.
963 void VerifyRemoteRtpHeaderExtensions() {
964 const cricket::MediaContentDescription* desc =
965 cricket::GetFirstAudioContentDescription(
966 pc_->remote_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +0200967 ASSERT_TRUE(desc != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
969
970 desc = cricket::GetFirstVideoContentDescription(
971 pc_->remote_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +0200972 ASSERT_TRUE(desc != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
974 }
975
976 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700977 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700978 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 std::string sdp;
980 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700981 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800982 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700983 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
985 }
986
deadbeefab9b2d12015-10-14 11:33:11 -0700987 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700988 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800989 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700990 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700991 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
992 }
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700995 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700996 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997
998 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
999 // audio codec change, even if the parameter has nothing to do with
1000 // receiving. Not all parameters are serialized to SDP.
1001 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1002 // the SessionDescription, it is necessary to do that here to in order to
1003 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1004 // https://code.google.com/p/webrtc/issues/detail?id=1356
1005 std::string sdp;
1006 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001007 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001008 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001009 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1011 }
1012
1013 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001014 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001015 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016
1017 std::string sdp;
1018 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001019 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001020 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001021 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1023 }
1024
1025 void CreateOfferReceiveAnswer() {
1026 CreateOfferAsLocalDescription();
1027 std::string sdp;
1028 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1029 CreateAnswerAsRemoteDescription(sdp);
1030 }
1031
1032 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001033 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001034 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1036 // audio codec change, even if the parameter has nothing to do with
1037 // receiving. Not all parameters are serialized to SDP.
1038 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1039 // the SessionDescription, it is necessary to do that here to in order to
1040 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1041 // https://code.google.com/p/webrtc/issues/detail?id=1356
1042 std::string sdp;
1043 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001044 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001045 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046
Steve Antondb45ca82017-09-11 18:27:34 -07001047 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001049 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001050 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 }
1052
deadbeefab9b2d12015-10-14 11:33:11 -07001053 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001054 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001055 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001056 ASSERT_TRUE(answer);
1057 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1059 }
1060
deadbeefab9b2d12015-10-14 11:33:11 -07001061 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001062 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001063 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001064 ASSERT_TRUE(pr_answer);
1065 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001067 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001068 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001069 ASSERT_TRUE(answer);
1070 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1072 }
1073
Seth Hampson845e8782018-03-02 11:34:10 -08001074 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001075 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001076 // called with the given stream id and expected number of tracks.
1077 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001078 int expected_num_tracks) {
1079 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001080 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001081 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001082 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 }
1084
1085 // Creates an offer and applies it as a local session description.
1086 // Creates an answer with the same SDP an the offer but removes all lines
1087 // that start with a:ssrc"
1088 void CreateOfferReceiveAnswerWithoutSsrc() {
1089 CreateOfferAsLocalDescription();
1090 std::string sdp;
1091 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1092 SetSsrcToZero(&sdp);
1093 CreateAnswerAsRemoteDescription(sdp);
1094 }
1095
deadbeefab9b2d12015-10-14 11:33:11 -07001096 // This function creates a MediaStream with label kStreams[0] and
Artem Titov880fa812021-07-30 22:30:23 +02001097 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001098 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001099 // is returned and the MediaStream is stored in
Artem Titov880fa812021-07-30 22:30:23 +02001100 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001101 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001102 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1103 size_t number_of_video_tracks) {
1104 EXPECT_LE(number_of_audio_tracks, 2u);
1105 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001106
1107 reference_collection_ = StreamCollection::Create();
1108 std::string sdp_ms1 = std::string(kSdpStringInit);
1109
Seth Hampson845e8782018-03-02 11:34:10 -08001110 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001111
1112 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001113 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001114 reference_collection_->AddStream(stream);
1115
1116 if (number_of_audio_tracks > 0) {
1117 sdp_ms1 += std::string(kSdpStringAudio);
1118 sdp_ms1 += std::string(kSdpStringMs1Audio0);
Niels Möllerafb246b2022-04-20 14:26:50 +02001119 AddAudioTrack(kAudioTracks[0], stream.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001120 }
1121 if (number_of_audio_tracks > 1) {
1122 sdp_ms1 += kSdpStringMs1Audio1;
Niels Möllerafb246b2022-04-20 14:26:50 +02001123 AddAudioTrack(kAudioTracks[1], stream.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001124 }
1125
1126 if (number_of_video_tracks > 0) {
1127 sdp_ms1 += std::string(kSdpStringVideo);
1128 sdp_ms1 += std::string(kSdpStringMs1Video0);
Niels Möllerafb246b2022-04-20 14:26:50 +02001129 AddVideoTrack(kVideoTracks[0], stream.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001130 }
1131 if (number_of_video_tracks > 1) {
1132 sdp_ms1 += kSdpStringMs1Video1;
Niels Möllerafb246b2022-04-20 14:26:50 +02001133 AddVideoTrack(kVideoTracks[1], stream.get());
deadbeefab9b2d12015-10-14 11:33:11 -07001134 }
1135
kwibergd1fe2812016-04-27 06:47:29 -07001136 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001137 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001138 }
1139
1140 void AddAudioTrack(const std::string& track_id,
1141 MediaStreamInterface* stream) {
1142 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1143 webrtc::AudioTrack::Create(track_id, nullptr));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001144 ASSERT_TRUE(stream->AddTrack(audio_track));
deadbeefab9b2d12015-10-14 11:33:11 -07001145 }
1146
1147 void AddVideoTrack(const std::string& track_id,
1148 MediaStreamInterface* stream) {
1149 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001150 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001151 webrtc::FakeVideoTrackSource::Create(),
1152 rtc::Thread::Current()));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001153 ASSERT_TRUE(stream->AddTrack(video_track));
deadbeefab9b2d12015-10-14 11:33:11 -07001154 }
1155
Steve Anton36da6ff2018-02-16 16:04:20 -08001156 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001157 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001158 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001159 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001160 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1161 return offer;
1162 }
1163
Steve Anton36da6ff2018-02-16 16:04:20 -08001164 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1165 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001166 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001167 std::unique_ptr<SessionDescriptionInterface> offer;
1168 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1169 return offer;
1170 }
1171
1172 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1173 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1174 std::unique_ptr<SessionDescriptionInterface> answer;
1175 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1176 return answer;
1177 }
1178
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001181 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001182 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001183 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1184 return answer;
1185 }
1186
1187 const std::string& GetFirstAudioStreamCname(
1188 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001189 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001190 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001191 return audio_desc->streams()[0].cname;
1192 }
1193
zhihuang1c378ed2017-08-17 14:10:50 -07001194 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1195 const RTCOfferAnswerOptions& offer_answer_options) {
1196 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001197 auto observer =
1198 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
Niels Möllerafb246b2022-04-20 14:26:50 +02001199 pc_->CreateOffer(observer.get(), offer_answer_options);
zhihuang1c378ed2017-08-17 14:10:50 -07001200 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1201 return observer->MoveDescription();
1202 }
1203
1204 void CreateOfferWithOptionsAsRemoteDescription(
1205 std::unique_ptr<SessionDescriptionInterface>* desc,
1206 const RTCOfferAnswerOptions& offer_answer_options) {
1207 *desc = CreateOfferWithOptions(offer_answer_options);
1208 ASSERT_TRUE(desc != nullptr);
1209 std::string sdp;
1210 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001211 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001212 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001213 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001214 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1215 }
1216
1217 void CreateOfferWithOptionsAsLocalDescription(
1218 std::unique_ptr<SessionDescriptionInterface>* desc,
1219 const RTCOfferAnswerOptions& offer_answer_options) {
1220 *desc = CreateOfferWithOptions(offer_answer_options);
1221 ASSERT_TRUE(desc != nullptr);
1222 std::string sdp;
1223 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001224 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001225 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001226
Steve Antondb45ca82017-09-11 18:27:34 -07001227 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001228 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1229 }
1230
1231 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001232 RTC_DCHECK(content);
1233 RTC_DCHECK(content->media_description());
1234 for (const cricket::AudioCodec& codec :
1235 content->media_description()->as_audio()->codecs()) {
1236 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001237 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001238 }
zhihuang1c378ed2017-08-17 14:10:50 -07001239 }
1240 return false;
1241 }
1242
Steve Anton36da6ff2018-02-16 16:04:20 -08001243 const char* GetSdpStringWithStream1() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001244 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001245 return kSdpStringWithStream1PlanB;
1246 } else {
1247 return kSdpStringWithStream1UnifiedPlan;
1248 }
1249 }
1250
1251 const char* GetSdpStringWithStream1And2() const {
Florent Castelli15a38de2022-04-06 00:38:21 +02001252 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08001253 return kSdpStringWithStream1And2PlanB;
1254 } else {
1255 return kSdpStringWithStream1And2UnifiedPlan;
1256 }
1257 }
1258
Byoungchan Leed58f5262022-06-27 18:05:22 +09001259 rtc::SocketServer* socket_server() const { return vss_.get(); }
1260
deadbeef9a6f4d42017-05-15 19:43:33 -07001261 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1262 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001263 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001264 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001265 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001266 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1267 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1268 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001270 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001271 const SdpSemantics sdp_semantics_;
1272};
1273
1274class PeerConnectionInterfaceTest
1275 : public PeerConnectionInterfaceBaseTest,
1276 public ::testing::WithParamInterface<SdpSemantics> {
1277 protected:
1278 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1279};
1280
1281class PeerConnectionInterfaceTestPlanB
1282 : public PeerConnectionInterfaceBaseTest {
1283 protected:
1284 PeerConnectionInterfaceTestPlanB()
Florent Castelli15a38de2022-04-06 00:38:21 +02001285 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286};
1287
zhihuang8f65cdf2016-05-06 18:40:30 -07001288// Generate different CNAMEs when PeerConnections are created.
1289// The CNAMEs are expected to be generated randomly. It is possible
1290// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001291TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001292 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001293 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001295 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001296 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1297 GetFirstAudioStreamCname(offer2.get()));
1298}
1299
Steve Anton36da6ff2018-02-16 16:04:20 -08001300TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001301 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001302 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001303 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001304 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001305 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1306 GetFirstAudioStreamCname(answer2.get()));
1307}
1308
Steve Anton36da6ff2018-02-16 16:04:20 -08001309TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 CreatePeerConnectionWithDifferentConfigurations) {
1311 CreatePeerConnectionWithDifferentConfigurations();
1312}
1313
Steve Anton36da6ff2018-02-16 16:04:20 -08001314TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1317 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1318 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1319 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1320 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1321 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1322 port_allocator_->candidate_filter());
1323 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1324 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1325}
1326
1327// Test that when a PeerConnection is created with a nonzero candidate pool
1328// size, the pooled PortAllocatorSession is created with all the attributes
1329// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001330TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001331 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001332 config.sdp_semantics = sdp_semantics_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001333 PeerConnectionInterface::IceServer server;
1334 server.uri = kStunAddressOnly;
1335 config.servers.push_back(server);
1336 config.type = PeerConnectionInterface::kRelay;
Henrik Boström24e03372022-10-27 13:49:10 +02001337 config.DEPRECATED_disable_ipv6 = true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001338 config.tcp_candidate_policy =
1339 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001340 config.candidate_network_policy =
1341 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001342 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001343 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001344
1345 const cricket::FakePortAllocatorSession* session =
1346 static_cast<const cricket::FakePortAllocatorSession*>(
1347 port_allocator_->GetPooledSession());
1348 ASSERT_NE(nullptr, session);
1349 EXPECT_EQ(1UL, session->stun_servers().size());
1350 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1351 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001352 EXPECT_LT(0U,
1353 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001354}
1355
deadbeefd21eab32017-07-26 16:50:11 -07001356// Test that network-related RTCConfiguration members are applied to the
1357// PortAllocator when CreatePeerConnection is called. Specifically:
1358// - disable_ipv6_on_wifi
1359// - max_ipv6_networks
1360// - tcp_candidate_policy
1361// - candidate_network_policy
1362// - prune_turn_ports
1363//
1364// Note that the candidate filter (RTCConfiguration::type) is already tested
1365// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001366TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001367 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1368 // Create fake port allocator.
Byoungchan Leed58f5262022-06-27 18:05:22 +09001369 std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory(
1370 new rtc::BasicPacketSocketFactory(socket_server()));
deadbeefd21eab32017-07-26 16:50:11 -07001371 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Byoungchan Leed58f5262022-06-27 18:05:22 +09001372 new cricket::FakePortAllocator(rtc::Thread::Current(),
1373 packet_socket_factory.get()));
deadbeefd21eab32017-07-26 16:50:11 -07001374 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1375
1376 // Create RTCConfiguration with some network-related fields relevant to
1377 // PortAllocator populated.
1378 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001379 config.sdp_semantics = sdp_semantics_;
deadbeefd21eab32017-07-26 16:50:11 -07001380 config.disable_ipv6_on_wifi = true;
1381 config.max_ipv6_networks = 10;
1382 config.tcp_candidate_policy =
1383 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1384 config.candidate_network_policy =
1385 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1386 config.prune_turn_ports = true;
1387
1388 // Create the PC factory and PC with the above config.
1389 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1390 webrtc::CreatePeerConnectionFactory(
1391 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001392 rtc::Thread::Current(), fake_audio_capture_module_,
1393 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001394 webrtc::CreateBuiltinAudioDecoderFactory(),
1395 webrtc::CreateBuiltinVideoEncoderFactory(),
1396 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1397 nullptr /* audio_processing */));
Florent Castelli72424402022-04-06 03:45:10 +02001398 PeerConnectionDependencies pc_dependencies(&observer_);
1399 pc_dependencies.allocator = std::move(port_allocator);
1400 auto result = pc_factory_->CreatePeerConnectionOrError(
1401 config, std::move(pc_dependencies));
1402 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02001403 observer_.SetPeerConnectionInterface(result.value().get());
deadbeefd21eab32017-07-26 16:50:11 -07001404
1405 // Now validate that the config fields set above were applied to the
1406 // PortAllocator, as flags or otherwise.
1407 EXPECT_FALSE(raw_port_allocator->flags() &
1408 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1409 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1410 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1411 EXPECT_TRUE(raw_port_allocator->flags() &
1412 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001413 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1414 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001415}
1416
deadbeef46c73892016-11-16 19:42:04 -08001417// Check that GetConfiguration returns the configuration the PeerConnection was
1418// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001419TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001420 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001421 config.sdp_semantics = sdp_semantics_;
deadbeef46c73892016-11-16 19:42:04 -08001422 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001423 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001424
1425 PeerConnectionInterface::RTCConfiguration returned_config =
1426 pc_->GetConfiguration();
1427 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1428}
1429
1430// Check that GetConfiguration returns the last configuration passed into
1431// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001432TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001433 PeerConnectionInterface::RTCConfiguration starting_config;
Henrik Boström62995db2022-01-03 09:58:10 +01001434 starting_config.sdp_semantics = sdp_semantics_;
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001435 starting_config.bundle_policy =
1436 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1437 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001438
Steve Anton36da6ff2018-02-16 16:04:20 -08001439 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001440 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001441 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001442
1443 PeerConnectionInterface::RTCConfiguration returned_config =
1444 pc_->GetConfiguration();
1445 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1446}
1447
Steve Antonc79268f2018-04-24 09:54:10 -07001448TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1449 CreatePeerConnection();
1450
1451 pc_->Close();
1452
1453 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001454 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001455}
1456
Steve Anton36da6ff2018-02-16 16:04:20 -08001457TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001458 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001459 AddVideoStream(kStreamId1);
1460 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 ASSERT_EQ(2u, pc_->local_streams()->count());
1462
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001463 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001464 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001465 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001466 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möllerafb246b2022-04-20 14:26:50 +02001467 pc_factory_->CreateAudioTrack(
1468 kStreamId3, static_cast<AudioSourceInterface*>(nullptr)));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001469 stream->AddTrack(audio_track);
Niels Möllerafb246b2022-04-20 14:26:50 +02001470 EXPECT_TRUE(pc_->AddStream(stream.get()));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001471 EXPECT_EQ(3u, pc_->local_streams()->count());
1472
1473 // Remove the third stream.
1474 pc_->RemoveStream(pc_->local_streams()->at(2));
1475 EXPECT_EQ(2u, pc_->local_streams()->count());
1476
1477 // Remove the second stream.
1478 pc_->RemoveStream(pc_->local_streams()->at(1));
1479 EXPECT_EQ(1u, pc_->local_streams()->count());
1480
1481 // Remove the first stream.
1482 pc_->RemoveStream(pc_->local_streams()->at(0));
1483 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484}
1485
deadbeefab9b2d12015-10-14 11:33:11 -07001486// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001487// Don't run under Unified Plan since the stream API is not available.
1488TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001489 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001490 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001491 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001492 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001493
deadbeefab9b2d12015-10-14 11:33:11 -07001494 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001495 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001496 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001497
deadbeefab9b2d12015-10-14 11:33:11 -07001498 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001499 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001500 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001501
1502 // Add another stream and ensure the offer includes both the old and new
1503 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001504 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001505 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001506
Steve Antonb1c1de12017-12-21 15:14:30 -08001507 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001508 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1509 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001510
Steve Antonb1c1de12017-12-21 15:14:30 -08001511 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001512 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1513 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001514}
1515
Steve Anton36da6ff2018-02-16 16:04:20 -08001516// Don't run under Unified Plan since the stream API is not available.
1517TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001518 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001519 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 ASSERT_EQ(1u, pc_->local_streams()->count());
1521 pc_->RemoveStream(pc_->local_streams()->at(0));
1522 EXPECT_EQ(0u, pc_->local_streams()->count());
1523}
1524
deadbeefe1f9d832016-01-14 15:35:42 -08001525// Test for AddTrack and RemoveTrack methods.
1526// Tests that the created offer includes tracks we added,
1527// and that the RtpSenders are created correctly.
1528// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001529// Only tested with Plan B since Unified Plan is covered in more detail by tests
1530// in peerconnection_jsep_unittests.cc
1531TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001532 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001533 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001534 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001535 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001536 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001537 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1538 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001539 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001540 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001541 EXPECT_EQ("audio_track", audio_sender->id());
1542 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001543 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001544 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001545 EXPECT_EQ("video_track", video_sender->id());
1546 EXPECT_EQ(video_track, video_sender->track());
1547
1548 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001549 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001550 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001551
1552 const cricket::ContentInfo* audio_content =
1553 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001554 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001555 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001556
1557 const cricket::ContentInfo* video_content =
1558 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001559 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001560 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001561
Steve Antondb45ca82017-09-11 18:27:34 -07001562 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001563
1564 // Now try removing the tracks.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001565 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1566 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001567
1568 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001569 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001570
1571 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001572 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001573 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001574
1575 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001576 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001577 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001578
Steve Antondb45ca82017-09-11 18:27:34 -07001579 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001580
1581 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1582 // should return false.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001583 EXPECT_FALSE(pc_->RemoveTrackOrError(audio_sender).ok());
1584 EXPECT_FALSE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001585}
1586
Jonas Oreland4b2a1062022-10-19 09:24:42 +02001587// Test for AddTrack with init_send_encoding.
1588TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackWithSendEncodings) {
1589 CreatePeerConnectionWithoutDtls();
1590 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1591 CreateAudioTrack("audio_track"));
1592 rtc::scoped_refptr<VideoTrackInterface> video_track(
1593 CreateVideoTrack("video_track"));
1594 RtpEncodingParameters audio_encodings;
1595 audio_encodings.active = false;
1596 auto audio_sender =
1597 pc_->AddTrack(audio_track, {kStreamId1}, {audio_encodings}).MoveValue();
1598 RtpEncodingParameters video_encodings;
1599 video_encodings.active = true;
1600 auto video_sender =
1601 pc_->AddTrack(video_track, {kStreamId1}, {video_encodings}).MoveValue();
1602 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1603 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
1604 EXPECT_EQ("audio_track", audio_sender->id());
1605 EXPECT_EQ(audio_track, audio_sender->track());
1606 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1607 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
1608 EXPECT_EQ("video_track", video_sender->id());
1609 EXPECT_EQ(video_track, video_sender->track());
1610
1611 // Now create an offer and check for the senders.
1612 std::unique_ptr<SessionDescriptionInterface> offer;
1613 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1614
1615 const cricket::ContentInfo* audio_content =
1616 cricket::GetFirstAudioContent(offer->description());
1617 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1618 kStreamId1, "audio_track"));
1619
1620 const cricket::ContentInfo* video_content =
1621 cricket::GetFirstVideoContent(offer->description());
1622 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1623 kStreamId1, "video_track"));
1624
1625 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
1626
1627 // Check the encodings.
1628 ASSERT_THAT(audio_sender->GetParameters().encodings, SizeIs(1));
1629 EXPECT_THAT(audio_sender->GetParameters().encodings[0].active, Eq(false));
1630 ASSERT_THAT(video_sender->GetParameters().encodings, SizeIs(1));
1631 EXPECT_THAT(video_sender->GetParameters().encodings[0].active, Eq(true));
1632
1633 // Now try removing the tracks.
1634 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1635 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
1636}
1637
deadbeefe1f9d832016-01-14 15:35:42 -08001638// Test creating senders without a stream specified,
1639// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001640TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001641 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001642 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001643 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001644 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001645 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001646 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001647 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001648 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001649 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001650 EXPECT_EQ("audio_track", audio_sender->id());
1651 EXPECT_EQ(audio_track, audio_sender->track());
1652 EXPECT_EQ("video_track", video_sender->id());
1653 EXPECT_EQ(video_track, video_sender->track());
Florent Castelli15a38de2022-04-06 00:38:21 +02001654 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07001655 // If the ID is truly a random GUID, it should be infinitely unlikely they
1656 // will be the same.
1657 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1658 } else {
1659 // We allows creating tracks without stream ids under Unified Plan
1660 // semantics.
1661 EXPECT_EQ(0u, video_sender->stream_ids().size());
1662 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1663 }
deadbeefe1f9d832016-01-14 15:35:42 -08001664}
1665
Harald Alvestrand89061872018-01-02 14:08:34 +01001666// Test that we can call GetStats() after AddTrack but before connecting
1667// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001668TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001669 CreatePeerConnectionWithoutDtls();
1670 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001671 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001672 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001673 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001674 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1675 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001676 EXPECT_TRUE(DoGetStats(nullptr));
1677}
1678
Steve Anton36da6ff2018-02-16 16:04:20 -08001679TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001680 CreatePeerConnectionWithoutDtls();
1681 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001682 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001683 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001684 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001685 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001686 ASSERT_TRUE(audio_sender.ok());
1687 auto* audio_sender_proxy =
1688 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1689 audio_sender.value().get());
1690 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1691
Harald Alvestrandc72af932018-01-11 17:18:19 +01001692 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001693 ASSERT_TRUE(video_sender.ok());
1694 auto* video_sender_proxy =
1695 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1696 video_sender.value().get());
1697 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001698}
1699
Steve Anton36da6ff2018-02-16 16:04:20 -08001700// Don't run under Unified Plan since the stream API is not available.
1701TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001702 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001703 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001704 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001705 ASSERT_EQ(1u, senders.size());
1706 auto* sender_proxy =
1707 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1708 senders[0].get());
1709 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001710}
1711
Steve Anton36da6ff2018-02-16 16:04:20 -08001712TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001714 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 VerifyRemoteRtpHeaderExtensions();
1716}
1717
Steve Anton36da6ff2018-02-16 16:04:20 -08001718TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001720 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 CreateOfferAsLocalDescription();
1722 std::string offer;
1723 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1724 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001725 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726}
1727
Steve Anton36da6ff2018-02-16 16:04:20 -08001728TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001729 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001730 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731
1732 CreateOfferAsRemoteDescription();
1733 CreateAnswerAsLocalDescription();
1734
Seth Hampson845e8782018-03-02 11:34:10 -08001735 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736}
1737
Steve Anton36da6ff2018-02-16 16:04:20 -08001738TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001739 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001740 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741
1742 CreateOfferAsRemoteDescription();
1743 CreatePrAnswerAsLocalDescription();
1744 CreateAnswerAsLocalDescription();
1745
Seth Hampson845e8782018-03-02 11:34:10 -08001746 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747}
1748
Steve Anton36da6ff2018-02-16 16:04:20 -08001749// Don't run under Unified Plan since the stream API is not available.
1750TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 InitiateCall();
1752 ASSERT_EQ(1u, pc_->remote_streams()->count());
1753 pc_->RemoveStream(pc_->local_streams()->at(0));
1754 CreateOfferReceiveAnswer();
1755 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001756 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 CreateOfferReceiveAnswer();
1758}
1759
1760// Tests that after negotiating an audio only call, the respondent can perform a
1761// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001762TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001763 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001764 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 CreateOfferAsRemoteDescription();
1766 CreateAnswerAsLocalDescription();
1767
1768 ASSERT_EQ(1u, pc_->remote_streams()->count());
1769 pc_->RemoveStream(pc_->local_streams()->at(0));
1770 CreateOfferReceiveAnswer();
1771 EXPECT_EQ(0u, pc_->remote_streams()->count());
1772}
1773
1774// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001775TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001776 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777
Steve Antonf1c6db12017-10-13 11:13:35 -07001778 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001781 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001782 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001783 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784
1785 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001786 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001787 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001788 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789
Steve Antonf1c6db12017-10-13 11:13:35 -07001790 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001791 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792
Steve Antonf1c6db12017-10-13 11:13:35 -07001793 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794}
1795
deadbeefab9b2d12015-10-14 11:33:11 -07001796// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001798TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001799 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001801 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001802 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001803 EXPECT_TRUE(offer);
1804 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805
1806 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001807 AddAudioTrack("track_label", {kStreamId1});
1808 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809
1810 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001811 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812
1813 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001814 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001815 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816}
1817
1818// Test that we will get different SSRCs for each tracks in the offer and answer
1819// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001820TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001821 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001823 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1824 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825
1826 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001827 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001828 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 int audio_ssrc = 0;
1830 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001831 EXPECT_TRUE(
1832 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1833 EXPECT_TRUE(
1834 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 EXPECT_NE(audio_ssrc, video_ssrc);
1836
1837 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001838 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001839 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001840 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 audio_ssrc = 0;
1842 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001843 EXPECT_TRUE(
1844 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1845 EXPECT_TRUE(
1846 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 EXPECT_NE(audio_ssrc, video_ssrc);
1848}
1849
deadbeefeb459812015-12-15 19:24:43 -08001850// Test that it's possible to call AddTrack on a MediaStream after adding
1851// the stream to a PeerConnection.
1852// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001853// Don't run under Unified Plan since the stream API is not available.
1854TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001855 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001856 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001857 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001858 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1859
1860 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001861 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001862 CreateVideoTrack("video_label"));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001863 stream->AddTrack(video_track);
deadbeefeb459812015-12-15 19:24:43 -08001864
kwibergd1fe2812016-04-27 06:47:29 -07001865 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001866 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001867
1868 const cricket::MediaContentDescription* video_desc =
1869 cricket::GetFirstVideoContentDescription(offer->description());
1870 EXPECT_TRUE(video_desc != nullptr);
1871}
1872
1873// Test that it's possible to call RemoveTrack on a MediaStream after adding
1874// the stream to a PeerConnection.
1875// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001876// Don't run under Unified Plan since the stream API is not available.
1877TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001878 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001879 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001880 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001881 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1882
1883 // Remove the video track.
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001884 stream->RemoveTrack(stream->GetVideoTracks()[0]);
deadbeefeb459812015-12-15 19:24:43 -08001885
kwibergd1fe2812016-04-27 06:47:29 -07001886 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001887 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001888
1889 const cricket::MediaContentDescription* video_desc =
1890 cricket::GetFirstVideoContentDescription(offer->description());
1891 EXPECT_TRUE(video_desc == nullptr);
1892}
1893
deadbeefbd7d8f72015-12-18 16:58:44 -08001894// Test creating a sender with a stream ID, and ensure the ID is populated
1895// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001896// Don't run under Unified Plan since the stream API is not available.
1897TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001898 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001899 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001900
kwibergd1fe2812016-04-27 06:47:29 -07001901 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001902 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001903
1904 const cricket::MediaContentDescription* video_desc =
1905 cricket::GetFirstVideoContentDescription(offer->description());
1906 ASSERT_TRUE(video_desc != nullptr);
1907 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001908 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001909}
1910
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001912TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001914 ASSERT_LT(0u, pc_->GetSenders().size());
1915 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001916 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001917 pc_->GetReceivers()[0]->track();
Niels Möllerafb246b2022-04-20 14:26:50 +02001918 EXPECT_TRUE(DoGetStats(remote_audio.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919
1920 // Remove the stream. Since we are sending to our selves the local
1921 // and the remote stream is the same.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001922 pc_->RemoveTrackOrError(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 // Do a re-negotiation.
1924 CreateOfferReceiveAnswer();
1925
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 // Test that we still can get statistics for the old track. Even if it is not
1927 // sent any longer.
Niels Möllerafb246b2022-04-20 14:26:50 +02001928 EXPECT_TRUE(DoGetStats(remote_audio.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929}
1930
1931// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001932TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001934 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1935 ASSERT_TRUE(video_receiver);
Niels Möllerafb246b2022-04-20 14:26:50 +02001936 EXPECT_TRUE(DoGetStats(video_receiver->track().get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937}
1938
1939// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001940TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001942 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
Niels Möllerafb246b2022-04-20 14:26:50 +02001943 pc_factory_->CreateAudioTrack("unknown track", nullptr));
1944 EXPECT_FALSE(DoGetStats(unknown_audio_track.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945}
1946
Steve Anton36da6ff2018-02-16 16:04:20 -08001947TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001948 CreatePeerConnectionWithoutDtls();
1949 EXPECT_TRUE(DoGetRTCStats());
1950 // Clearing stats cache is needed now, but should be temporary.
1951 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1952 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001953 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1954 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001955 EXPECT_TRUE(DoGetRTCStats());
1956 pc_->ClearStatsCache();
1957 CreateOfferReceiveAnswer();
1958 EXPECT_TRUE(DoGetRTCStats());
1959}
1960
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961// This tests that a SCTP data channel is returned using different
1962// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001963TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001964 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001965 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966
1967 webrtc::DataChannelInit config;
Florent Castelli72424402022-04-06 03:45:10 +02001968 auto channel = pc_->CreateDataChannelOrError("1", &config);
1969 EXPECT_TRUE(channel.ok());
1970 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001971 EXPECT_TRUE(observer_.renegotiation_needed_);
1972 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973
1974 config.ordered = false;
Florent Castelli72424402022-04-06 03:45:10 +02001975 channel = pc_->CreateDataChannelOrError("2", &config);
1976 EXPECT_TRUE(channel.ok());
1977 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001978 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979
1980 config.ordered = true;
1981 config.maxRetransmits = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001982 channel = pc_->CreateDataChannelOrError("3", &config);
1983 EXPECT_TRUE(channel.ok());
1984 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001985 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001987 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 config.maxRetransmitTime = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001989 channel = pc_->CreateDataChannelOrError("4", &config);
1990 EXPECT_TRUE(channel.ok());
1991 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001992 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993}
1994
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001995// For backwards compatibility, we want people who "unset" maxRetransmits
1996// and maxRetransmitTime by setting them to -1 to get what they want.
1997TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1998 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001999 CreatePeerConnection(rtc_config);
2000
2001 webrtc::DataChannelInit config;
2002 config.maxRetransmitTime = -1;
2003 config.maxRetransmits = -1;
Florent Castelli72424402022-04-06 03:45:10 +02002004 auto channel = pc_->CreateDataChannelOrError("1", &config);
2005 EXPECT_TRUE(channel.ok());
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002006}
2007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008// This tests that no data channel is returned if both maxRetransmits and
2009// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002010TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002012 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002013 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014
2015 std::string label = "test";
2016 webrtc::DataChannelInit config;
2017 config.maxRetransmits = 0;
2018 config.maxRetransmitTime = 0;
2019
Florent Castelli72424402022-04-06 03:45:10 +02002020 auto channel = pc_->CreateDataChannelOrError(label, &config);
2021 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022}
2023
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024// The test verifies that creating a SCTP data channel with an id already in use
2025// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002026TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002028 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002029 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030
2031 webrtc::DataChannelInit config;
2032
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002033 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002034 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002035 auto channel = pc_->CreateDataChannelOrError("1", &config);
2036 EXPECT_TRUE(channel.ok());
2037 EXPECT_EQ(1, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038
Florent Castelli72424402022-04-06 03:45:10 +02002039 channel = pc_->CreateDataChannelOrError("x", &config);
2040 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041
2042 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002043 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002044 channel = pc_->CreateDataChannelOrError("max", &config);
2045 EXPECT_TRUE(channel.ok());
2046 EXPECT_EQ(config.id, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047
2048 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002049 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002050 channel = pc_->CreateDataChannelOrError("x", &config);
2051 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052}
2053
deadbeefab9b2d12015-10-14 11:33:11 -07002054// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002055TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002056 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002057 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002058
2059 std::string label = "test";
Florent Castelli72424402022-04-06 03:45:10 +02002060 auto channel = pc_->CreateDataChannelOrError(label, nullptr);
2061 EXPECT_TRUE(channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002062
Florent Castelli72424402022-04-06 03:45:10 +02002063 auto dup_channel = pc_->CreateDataChannelOrError(label, nullptr);
2064 EXPECT_TRUE(dup_channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002065}
2066
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002067#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002068// This tests that SCTP data channels can be rejected in an answer.
2069TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2070#else
2071TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2072#endif
2073{
Niels Möllerf06f9232018-08-07 12:32:18 +02002074 RTCConfiguration rtc_config;
2075 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002076
Florent Castelli72424402022-04-06 03:45:10 +02002077 auto offer_channel = pc_->CreateDataChannelOrError("offer_channel", NULL);
Zhi Huang644fde42018-04-02 19:16:26 -07002078
2079 CreateOfferAsLocalDescription();
2080
2081 // Create an answer where the m-line for data channels are rejected.
2082 std::string sdp;
2083 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2084 std::unique_ptr<SessionDescriptionInterface> answer(
2085 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2086 ASSERT_TRUE(answer);
2087 cricket::ContentInfo* data_info =
2088 cricket::GetFirstDataContent(answer->description());
2089 data_info->rejected = true;
2090
2091 DoSetRemoteDescription(std::move(answer));
Florent Castelli72424402022-04-06 03:45:10 +02002092 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel.value()->state());
Zhi Huang644fde42018-04-02 19:16:26 -07002093}
2094
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095// Test that we can create a session description from an SDP string from
2096// FireFox, use it as a remote session description, generate an answer and use
2097// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002098TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002099 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002100 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002101 AddAudioTrack("audio_label");
2102 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002103 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002104 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002105 webrtc::kFireFoxSdpOffer, nullptr));
2106 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 CreateAnswerAsLocalDescription();
Niels Möllerafb246b2022-04-20 14:26:50 +02002108 ASSERT_TRUE(pc_->local_description() != nullptr);
2109 ASSERT_TRUE(pc_->remote_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110
2111 const cricket::ContentInfo* content =
2112 cricket::GetFirstAudioContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002113 ASSERT_TRUE(content != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 EXPECT_FALSE(content->rejected);
2115
2116 content =
2117 cricket::GetFirstVideoContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002118 ASSERT_TRUE(content != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002120#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 content =
2122 cricket::GetFirstDataContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002123 ASSERT_TRUE(content != nullptr);
Zhi Huange830e682018-03-30 10:48:35 -07002124 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002125#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126}
2127
Harald Alvestrand0d018412021-11-04 13:52:31 +00002128// Test that fallback from DTLS to SDES is not supported.
2129// The fallback was previously supported but was removed to simplify the code
2130// and because it's non-standard.
2131TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002132 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002133 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002134 // Wait for fake certificate to be generated. Previously, this is what caused
2135 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002136 AddAudioTrack("audio_label");
2137 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002138 ASSERT_NE(nullptr, fake_certificate_generator_);
2139 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2140 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002141 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002142 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2143 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002144 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002145}
2146
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147// Test that we can create an audio only offer and receive an answer with a
2148// limited set of audio codecs and receive an updated offer with more audio
2149// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002150TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002151 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002152 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 CreateOfferAsLocalDescription();
2154
Florent Castelli15a38de2022-04-06 00:38:21 +02002155 const char* answer_sdp = (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
2156 ? webrtc::kAudioSdpPlanB
2157 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002158 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002159 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002160 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161
Steve Anton36da6ff2018-02-16 16:04:20 -08002162 const char* reoffer_sdp =
Florent Castelli15a38de2022-04-06 00:38:21 +02002163 (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
Steve Anton36da6ff2018-02-16 16:04:20 -08002164 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2165 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002166 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002167 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002168 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 CreateAnswerAsLocalDescription();
2170}
2171
deadbeefc80741f2015-10-22 13:14:45 -07002172// Test that if we're receiving (but not sending) a track, subsequent offers
2173// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002174TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002175 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002176 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002177 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002178 CreateAnswerAsLocalDescription();
2179
2180 // At this point we should be receiving stream 1, but not sending anything.
2181 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002182 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002183 DoCreateOffer(&offer, nullptr);
2184
2185 const cricket::ContentInfo* video_content =
2186 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002187 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2188 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002189
2190 const cricket::ContentInfo* audio_content =
2191 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002192 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2193 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002194}
2195
2196// Test that if we're receiving (but not sending) a track, and the
2197// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2198// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002199TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002200 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002201 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002202 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002203 CreateAnswerAsLocalDescription();
2204
2205 // At this point we should be receiving stream 1, but not sending anything.
2206 // A new offer would be recvonly, but we'll set the "no receive" constraints
2207 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002208 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002209 RTCOfferAnswerOptions options;
2210 options.offer_to_receive_audio = 0;
2211 options.offer_to_receive_video = 0;
2212 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002213
2214 const cricket::ContentInfo* video_content =
2215 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002216 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2217 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002218
2219 const cricket::ContentInfo* audio_content =
2220 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002221 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2222 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002223}
2224
deadbeef653b8e02015-11-11 12:55:10 -08002225// Test that we can use SetConfiguration to change the ICE servers of the
2226// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002227TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002228 CreatePeerConnection();
2229
Steve Anton36da6ff2018-02-16 16:04:20 -08002230 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002231 PeerConnectionInterface::IceServer server;
2232 server.uri = "stun:test_hostname";
2233 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002234 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002235
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002236 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2237 EXPECT_EQ("test_hostname",
2238 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002239}
2240
Steve Anton36da6ff2018-02-16 16:04:20 -08002241TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002242 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002243 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002244 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002245 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002246 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2247}
2248
Steve Anton36da6ff2018-02-16 16:04:20 -08002249TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002250 PeerConnectionInterface::RTCConfiguration config;
2251 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002252 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002253 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002254 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002255
2256 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002257 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002258 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2259 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002260}
2261
skvladd1f5fda2017-02-03 16:54:05 -08002262// Test that the ice check interval can be changed. This does not verify that
2263// the setting makes it all the way to P2PTransportChannel, as that would
2264// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002265TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002266 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002267 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002268 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002269 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002270 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002271 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002272 config = pc_->GetConfiguration();
2273 EXPECT_EQ(config.ice_check_min_interval, 100);
2274}
2275
2276TEST_P(PeerConnectionInterfaceTest,
2277 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2278 PeerConnectionInterface::RTCConfiguration config;
2279 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2280 CreatePeerConnection(config);
2281 config = pc_->GetConfiguration();
2282 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2283
2284 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002285 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002286 config = pc_->GetConfiguration();
2287 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002288}
2289
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002290// Test that when SetConfiguration changes both the pool size and other
2291// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002292TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002293 SetConfigurationCreatesPooledSessionCorrectly) {
2294 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002295 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002296 config.ice_candidate_pool_size = 1;
2297 PeerConnectionInterface::IceServer server;
2298 server.uri = kStunAddressOnly;
2299 config.servers.push_back(server);
2300 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002301 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002302
2303 const cricket::FakePortAllocatorSession* session =
2304 static_cast<const cricket::FakePortAllocatorSession*>(
2305 port_allocator_->GetPooledSession());
2306 ASSERT_NE(nullptr, session);
2307 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002308}
2309
deadbeef293e9262017-01-11 12:28:30 -08002310// Test that after SetLocalDescription, changing the pool size is not allowed,
2311// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002312TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002313 CantChangePoolSizeAfterSetLocalDescription) {
2314 CreatePeerConnection();
2315 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002316 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002317 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002318 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002319
2320 // Set remote offer; can still change pool size at this point.
2321 CreateOfferAsRemoteDescription();
2322 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002323 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002324
2325 // Set local answer; now it's too late.
2326 CreateAnswerAsLocalDescription();
2327 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002328 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002329 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2330}
2331
deadbeef42a42632017-03-10 15:18:00 -08002332// Test that after setting an answer, extra pooled sessions are discarded. The
2333// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002334TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002335 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2336 CreatePeerConnection();
2337
2338 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002339 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002340 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002341 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002342
2343 // Do offer/answer.
2344 CreateOfferAsRemoteDescription();
2345 CreateAnswerAsLocalDescription();
2346
2347 // Expect no pooled sessions to be left.
2348 const cricket::PortAllocatorSession* session =
2349 port_allocator_->GetPooledSession();
2350 EXPECT_EQ(nullptr, session);
2351}
2352
2353// After Close is called, pooled candidates should be discarded so as to not
2354// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002355TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002356 CreatePeerConnection();
2357
Steve Anton36da6ff2018-02-16 16:04:20 -08002358 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002359 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002360 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002361 pc_->Close();
2362
2363 // Expect no pooled sessions to be left.
2364 const cricket::PortAllocatorSession* session =
2365 port_allocator_->GetPooledSession();
2366 EXPECT_EQ(nullptr, session);
2367}
2368
deadbeef293e9262017-01-11 12:28:30 -08002369// Test that SetConfiguration returns an invalid modification error if
2370// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002371TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002372 SetConfigurationReturnsInvalidModificationError) {
2373 PeerConnectionInterface::RTCConfiguration config;
2374 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2375 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2376 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002377 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002378
Steve Anton36da6ff2018-02-16 16:04:20 -08002379 PeerConnectionInterface::RTCConfiguration modified_config =
2380 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002381 modified_config.bundle_policy =
2382 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002383 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002384 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2385
Steve Anton36da6ff2018-02-16 16:04:20 -08002386 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002387 modified_config.rtcp_mux_policy =
2388 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002389 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002390 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2391
Steve Anton36da6ff2018-02-16 16:04:20 -08002392 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002393 modified_config.continual_gathering_policy =
2394 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002395 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002396 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2397}
2398
2399// Test that SetConfiguration returns a range error if the candidate pool size
2400// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002401TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002402 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2403 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002404 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002405 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002406
2407 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002408 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002409 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2410
2411 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002412 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002413 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2414}
2415
2416// Test that SetConfiguration returns a syntax error if parsing an ICE server
2417// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002418TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002419 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2420 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002421 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002422 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002423
2424 PeerConnectionInterface::IceServer bad_server;
2425 bad_server.uri = "stunn:www.example.com";
2426 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002427 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002428 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2429}
2430
2431// Test that SetConfiguration returns an invalid parameter error if a TURN
2432// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002433TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002434 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2435 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002436 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002437 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002438
2439 PeerConnectionInterface::IceServer bad_server;
2440 bad_server.uri = "turn:www.example.com";
2441 // Missing password.
2442 bad_server.username = "foo";
2443 config.servers.push_back(bad_server);
2444 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002445 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2446 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002447}
2448
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449// Test that PeerConnection::Close changes the states to closed and all remote
2450// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002451TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452 // Initialize a PeerConnection and negotiate local and remote session
2453 // description.
2454 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002455
2456 // With Plan B, verify the stream count. The analog with Unified Plan is the
2457 // RtpTransceiver count.
Florent Castelli15a38de2022-04-06 00:38:21 +02002458 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002459 ASSERT_EQ(1u, pc_->local_streams()->count());
2460 ASSERT_EQ(1u, pc_->remote_streams()->count());
2461 } else {
2462 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464
2465 pc_->Close();
2466
2467 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2468 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2469 pc_->ice_connection_state());
2470 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2471 pc_->ice_gathering_state());
2472
Florent Castelli15a38de2022-04-06 00:38:21 +02002473 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002474 EXPECT_EQ(1u, pc_->local_streams()->count());
2475 EXPECT_EQ(1u, pc_->remote_streams()->count());
2476 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002477 // Verify that the RtpTransceivers are still returned.
2478 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002479 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480
Steve Anton36da6ff2018-02-16 16:04:20 -08002481 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002482 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Florent Castelli15a38de2022-04-06 00:38:21 +02002483 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002484 ASSERT_TRUE(audio_receiver);
2485 ASSERT_TRUE(video_receiver);
2486 // Track state may be updated asynchronously.
2487 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2488 audio_receiver->track()->state(), kTimeout);
2489 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2490 video_receiver->track()->state(), kTimeout);
2491 } else {
2492 ASSERT_FALSE(audio_receiver);
2493 ASSERT_FALSE(video_receiver);
2494 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495}
2496
2497// Test that PeerConnection methods fails gracefully after
2498// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002499// Don't run under Unified Plan since the stream API is not available.
2500TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002501 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002502 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 CreateOfferAsRemoteDescription();
2504 CreateAnswerAsLocalDescription();
2505
2506 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002507 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2508 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509
2510 pc_->Close();
2511
Niels Möllerafb246b2022-04-20 14:26:50 +02002512 pc_->RemoveStream(local_stream.get());
2513 EXPECT_FALSE(pc_->AddStream(local_stream.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514
Florent Castelli72424402022-04-06 03:45:10 +02002515 EXPECT_FALSE(pc_->CreateDataChannelOrError("test", NULL).ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516
Niels Möllerafb246b2022-04-20 14:26:50 +02002517 EXPECT_TRUE(pc_->local_description() != nullptr);
2518 EXPECT_TRUE(pc_->remote_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519
kwibergd1fe2812016-04-27 06:47:29 -07002520 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002521 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002522 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002523 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524
2525 std::string sdp;
2526 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002527 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002528 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002529 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530
2531 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002532 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002533 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002534 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002535}
2536
2537// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002538TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539 InitiateCall();
2540 pc_->Close();
Niels Möllerafb246b2022-04-20 14:26:50 +02002541 DoGetStats(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542}
deadbeefab9b2d12015-10-14 11:33:11 -07002543
2544// NOTE: The series of tests below come from what used to be
2545// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2546// setting a remote or local description has the expected effects.
2547
2548// This test verifies that the remote MediaStreams corresponding to a received
2549// SDP string is created. In this test the two separate MediaStreams are
2550// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002551TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002552 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002553 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002554 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002555
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002556 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002557 EXPECT_TRUE(
2558 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2559 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2560 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2561
2562 // Create a session description based on another SDP with another
2563 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002564 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002565
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002566 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002567 EXPECT_TRUE(
2568 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2569}
2570
2571// This test verifies that when remote tracks are added/removed from SDP, the
2572// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002573// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2574// specific behavior.
2575TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002576 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002577 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002578 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002579 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002580 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002581 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002582 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002583 reference_collection_.get()));
deadbeefab9b2d12015-10-14 11:33:11 -07002584
2585 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002586 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002587 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002588 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002589 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002590 reference_collection_.get()));
zhihuang9763d562016-08-05 11:14:50 -07002591 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002592 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2593 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002594 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002595 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2596 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002597
2598 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002599 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002600 CreateSessionDescriptionAndReference(1, 1);
Niels Möllerafb246b2022-04-20 14:26:50 +02002601 MockTrackObserver audio_track_observer(audio_track2.get());
2602 MockTrackObserver video_track_observer(video_track2.get());
perkjd61bf802016-03-24 03:16:19 -07002603
2604 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2605 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002606 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002607 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002608 reference_collection_.get()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002609 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002610 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002611 audio_track2->state(), kTimeout);
2612 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2613 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002614}
2615
2616// This tests that remote tracks are ended if a local session description is set
2617// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002618TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002619 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002620 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002621 // First create and set a remote offer, then reject its video content in our
2622 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002623 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2624 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2625 ASSERT_TRUE(audio_receiver);
2626 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2627 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002628
Steve Anton36da6ff2018-02-16 16:04:20 -08002629 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2630 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002631 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002632 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2633 video_receiver->track();
2634 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002635
kwibergd1fe2812016-04-27 06:47:29 -07002636 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002637 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002638 cricket::ContentInfo* video_info =
2639 local_answer->description()->GetContentByName("video");
2640 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002641 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002642 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2643 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002644
2645 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002646 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002647 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002648 video_info = local_offer->description()->GetContentByName("video");
2649 ASSERT_TRUE(video_info != nullptr);
2650 video_info->rejected = true;
2651 cricket::ContentInfo* audio_info =
2652 local_offer->description()->GetContentByName("audio");
2653 ASSERT_TRUE(audio_info != nullptr);
2654 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002655 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002656 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002657 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2658 kTimeout);
2659 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2660 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002661}
2662
2663// This tests that we won't crash if the remote track has been removed outside
2664// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002665// Don't run under Unified Plan since the stream API is not available.
2666TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002667 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002668 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002669 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002670 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00002671 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2672 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002673
kwibergd1fe2812016-04-27 06:47:29 -07002674 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002675 webrtc::CreateSessionDescription(SdpType::kAnswer,
2676 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002677 cricket::ContentInfo* video_info =
2678 local_answer->description()->GetContentByName("video");
2679 video_info->rejected = true;
2680 cricket::ContentInfo* audio_info =
2681 local_answer->description()->GetContentByName("audio");
2682 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002683 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002684
2685 // No crash is a pass.
2686}
2687
deadbeef5e97fb52015-10-15 12:49:08 -07002688// This tests that if a recvonly remote description is set, no remote streams
2689// will be created, even if the description contains SSRCs/MSIDs.
2690// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002691TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002692 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002693 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002694
Steve Anton36da6ff2018-02-16 16:04:20 -08002695 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002696 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002697 CreateAndSetRemoteOffer(recvonly_offer);
2698
2699 EXPECT_EQ(0u, observer_.remote_streams()->count());
2700}
2701
deadbeefab9b2d12015-10-14 11:33:11 -07002702// This tests that a default MediaStream is created if a remote session
2703// description doesn't contain any streams and no MSID support.
2704// It also tests that the default stream is updated if a video m-line is added
2705// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002706// Don't run under Unified Plan since this behavior is Plan B specific.
2707TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002708 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002709 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002710 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2711
2712 ASSERT_EQ(1u, observer_.remote_streams()->count());
2713 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2714
2715 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2716 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002717 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002718
2719 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2720 ASSERT_EQ(1u, observer_.remote_streams()->count());
2721 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2722 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002723 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2724 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002725 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2726 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002727 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2728 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002729}
2730
2731// This tests that a default MediaStream is created if a remote session
2732// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002733// Don't run under Unified Plan since this behavior is Plan B specific.
2734TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002735 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002736 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002737 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002738 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2739
2740 ASSERT_EQ(1u, observer_.remote_streams()->count());
2741 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2742
2743 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2744 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002745 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002746}
2747
2748// This tests that it won't crash when PeerConnection tries to remove
2749// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002750// Don't run under Unified Plan since this behavior is Plan B specific.
2751TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002752 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002753 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002754 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002755 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00002756 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2757 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002758
2759 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2760
2761 // No crash is a pass.
2762}
2763
2764// This tests that a default MediaStream is created if the remote session
2765// description doesn't contain any streams and don't contain an indication if
2766// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002767// Don't run under Unified Plan since this behavior is Plan B specific.
2768TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002769 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002770 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002771 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002772 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2773
2774 ASSERT_EQ(1u, observer_.remote_streams()->count());
2775 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2776 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2777 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2778}
2779
2780// This tests that a default MediaStream is not created if the remote session
2781// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002782// Don't run under Unified Plan since this behavior is Plan B specific.
2783TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002784 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002785 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002786 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2787 EXPECT_EQ(0u, observer_.remote_streams()->count());
2788}
2789
deadbeefbda7e0b2015-12-08 17:13:40 -08002790// This tests that when setting a new description, the old default tracks are
2791// not destroyed and recreated.
2792// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002793// Don't run under Unified Plan since this behavior is Plan B specific.
2794TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002795 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002796 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002797 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002798 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2799
2800 ASSERT_EQ(1u, observer_.remote_streams()->count());
2801 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2802 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2803
2804 // Set the track to "disabled", then set a new description and ensure the
2805 // track is still disabled, which ensures it hasn't been recreated.
2806 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2807 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2808 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2809 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2810}
2811
deadbeefab9b2d12015-10-14 11:33:11 -07002812// This tests that a default MediaStream is not created if a remote session
2813// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002814// Don't run under Unified Plan since this behavior is Plan B specific.
2815TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002816 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002817 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002818 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002819 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002820 EXPECT_TRUE(
2821 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2822
2823 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2824 EXPECT_EQ(0u, observer_.remote_streams()->count());
2825}
2826
Seth Hampson5897a6e2018-04-03 11:16:33 -07002827// This tests that a default MediaStream is created if a remote SDP comes from
2828// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2829TEST_F(PeerConnectionInterfaceTestPlanB,
2830 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002831 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002832 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002833 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2834 // Add a=msid lines to simulate a Unified Plan endpoint that only
2835 // signals stream IDs with a=msid lines.
2836 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2837
2838 CreateAndSetRemoteOffer(sdp_string);
2839
2840 ASSERT_EQ(1u, observer_.remote_streams()->count());
2841 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2842 EXPECT_EQ("default", remote_stream->id());
2843 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2844}
2845
Seth Hampson5b4f0752018-04-02 16:31:36 -07002846// This tests that when a Plan B endpoint receives an SDP that signals no media
2847// stream IDs indicated by the special character "-" in the a=msid line, that
2848// a default stream ID will be used for the MediaStream ID. This can occur
2849// when a Unified Plan endpoint signals no media stream IDs, but signals both
2850// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2851TEST_F(PeerConnectionInterfaceTestPlanB,
2852 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002853 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002854 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002855 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2856 // the sender's stream ID will be interpreted as no stream IDs.
2857 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2858 sdp_string.append("a=msid:- audiotrack0\n");
2859
2860 CreateAndSetRemoteOffer(sdp_string);
2861
2862 ASSERT_EQ(1u, observer_.remote_streams()->count());
2863 // Because SSRCs are signaled the track ID will be what was signaled in the
2864 // a=msid line.
2865 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2866 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2867 EXPECT_EQ("default", remote_stream->id());
2868 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002869
2870 // Previously a bug ocurred when setting the remote description a second time.
2871 // This is because we checked equality of the remote StreamParams stream ID
2872 // (empty), and the previously set stream ID for the remote sender
2873 // ("default"). This cause a track to be removed, then added, when really
2874 // nothing should occur because it is the same track.
2875 CreateAndSetRemoteOffer(sdp_string);
2876 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2877 EXPECT_EQ(1u, observer_.add_track_events_.size());
2878 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2879 remote_stream = observer_.remote_streams()->at(0);
2880 EXPECT_EQ("default", remote_stream->id());
2881 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002882}
2883
deadbeefab9b2d12015-10-14 11:33:11 -07002884// This tests that an RtpSender is created when the local description is set
2885// after adding a local stream.
2886// TODO(deadbeef): This test and the one below it need to be updated when
2887// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002888// Don't run under Unified Plan since this behavior is Plan B specific.
2889TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002890 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002891 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002892
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002893 // Create an offer with 1 stream with 2 tracks of each type.
2894 rtc::scoped_refptr<StreamCollection> stream_collection =
2895 CreateStreamCollection(1, 2);
2896 pc_->AddStream(stream_collection->at(0));
2897 std::unique_ptr<SessionDescriptionInterface> offer;
2898 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002899 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002900
deadbeefab9b2d12015-10-14 11:33:11 -07002901 auto senders = pc_->GetSenders();
2902 EXPECT_EQ(4u, senders.size());
2903 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2904 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2905 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2906 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2907
2908 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002909 pc_->RemoveStream(stream_collection->at(0));
2910 stream_collection = CreateStreamCollection(1, 1);
2911 pc_->AddStream(stream_collection->at(0));
2912 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002913 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002914
deadbeefab9b2d12015-10-14 11:33:11 -07002915 senders = pc_->GetSenders();
2916 EXPECT_EQ(2u, senders.size());
2917 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2918 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2919 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2920 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2921}
2922
2923// This tests that an RtpSender is created when the local description is set
2924// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002925// Don't run under Unified Plan since this behavior is Plan B specific.
2926TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002927 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002928 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002929 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002930
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 rtc::scoped_refptr<StreamCollection> stream_collection =
2932 CreateStreamCollection(1, 2);
2933 // Add a stream to create the offer, but remove it afterwards.
2934 pc_->AddStream(stream_collection->at(0));
2935 std::unique_ptr<SessionDescriptionInterface> offer;
2936 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2937 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002938
Steve Antondb45ca82017-09-11 18:27:34 -07002939 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002940 auto senders = pc_->GetSenders();
2941 EXPECT_EQ(0u, senders.size());
2942
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002943 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002944 senders = pc_->GetSenders();
2945 EXPECT_EQ(4u, senders.size());
2946 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2947 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2948 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2949 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2950}
2951
2952// This tests that the expected behavior occurs if the SSRC on a local track is
2953// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002954TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002955 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002956 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002957 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002958
Steve Anton36da6ff2018-02-16 16:04:20 -08002959 AddAudioTrack(kAudioTracks[0]);
2960 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002961 std::unique_ptr<SessionDescriptionInterface> offer;
2962 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2963 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002964 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2965 webrtc::CreateSessionDescription(
2966 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002967 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002968 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002969
deadbeefab9b2d12015-10-14 11:33:11 -07002970 auto senders = pc_->GetSenders();
2971 EXPECT_EQ(2u, senders.size());
2972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2974
2975 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 cricket::MediaContentDescription* desc =
2977 cricket::GetFirstAudioContentDescription(modified_offer->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002978 ASSERT_TRUE(desc != nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002979 for (StreamParams& stream : desc->mutable_streams()) {
2980 for (unsigned int& ssrc : stream.ssrcs) {
2981 ++ssrc;
2982 }
2983 }
deadbeefab9b2d12015-10-14 11:33:11 -07002984
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 desc =
2986 cricket::GetFirstVideoContentDescription(modified_offer->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002987 ASSERT_TRUE(desc != nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002988 for (StreamParams& stream : desc->mutable_streams()) {
2989 for (unsigned int& ssrc : stream.ssrcs) {
2990 ++ssrc;
2991 }
2992 }
2993
Steve Antondb45ca82017-09-11 18:27:34 -07002994 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002995 senders = pc_->GetSenders();
2996 EXPECT_EQ(2u, senders.size());
2997 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2998 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2999 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3000 // changed.
3001}
3002
3003// This tests that the expected behavior occurs if a new session description is
3004// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003005// Don't run under Unified Plan since the stream API is not available.
3006TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003007 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003008 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003009 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003010
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003011 rtc::scoped_refptr<StreamCollection> stream_collection =
3012 CreateStreamCollection(2, 1);
3013 pc_->AddStream(stream_collection->at(0));
3014 std::unique_ptr<SessionDescriptionInterface> offer;
3015 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003016 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003017
deadbeefab9b2d12015-10-14 11:33:11 -07003018 auto senders = pc_->GetSenders();
3019 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003020 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3021 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003022
3023 // Add a new MediaStream but with the same tracks as in the first stream.
3024 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3025 webrtc::MediaStream::Create(kStreams[1]));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003026 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3027 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
Niels Möllerafb246b2022-04-20 14:26:50 +02003028 pc_->AddStream(stream_1.get());
deadbeefab9b2d12015-10-14 11:33:11 -07003029
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003030 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003031 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003032
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003033 auto new_senders = pc_->GetSenders();
3034 // Should be the same senders as before, but with updated stream id.
3035 // Note that this behavior is subject to change in the future.
3036 // We may decide the PC should ignore existing tracks in AddStream.
3037 EXPECT_EQ(senders, new_senders);
3038 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3039 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003040}
3041
zhihuang81c3a032016-11-17 12:06:24 -08003042// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003043TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003044 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003045 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003046 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3047 EXPECT_EQ(observer_.num_added_tracks_, 1);
3048 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3049
3050 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003051 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003052 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003053 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3054}
3055
deadbeefd1a38b52016-12-10 13:15:33 -08003056// Test that when SetConfiguration is called and the configuration is
3057// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003058TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003059 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003060 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003061 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003062 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003063 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003064 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3065 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003066
3067 // Do initial offer/answer so there's something to restart.
3068 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003069 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003070
3071 // Grab the ufrags.
3072 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3073
3074 // Change ICE policy, which should trigger an ICE restart on the next offer.
3075 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003076 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003077 CreateOfferAsLocalDescription();
3078
3079 // Grab the new ufrags.
3080 std::vector<std::string> subsequent_ufrags =
3081 GetUfrags(pc_->local_description());
3082
3083 // Sanity check.
3084 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3085 // Check that each ufrag is different.
3086 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3087 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3088 }
3089}
3090
3091// Test that when SetConfiguration is called and the configuration *isn't*
3092// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003093TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003094 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003095 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003096 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003097 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003098 config = pc_->GetConfiguration();
3099 AddAudioTrack(kAudioTracks[0]);
3100 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003101
3102 // Do initial offer/answer so there's something to restart.
3103 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003104 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003105
3106 // Grab the ufrags.
3107 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3108
3109 // Call SetConfiguration with a config identical to what the PC was
3110 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003111 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003112 CreateOfferAsLocalDescription();
3113
3114 // Grab the new ufrags.
3115 std::vector<std::string> subsequent_ufrags =
3116 GetUfrags(pc_->local_description());
3117
3118 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3119}
3120
3121// Test for a weird corner case scenario:
3122// 1. Audio/video session established.
3123// 2. SetConfiguration changes ICE config; ICE restart needed.
3124// 3. ICE restart initiated by remote peer, but only for one m= section.
3125// 4. Next createOffer should initiate an ICE restart, but only for the other
3126// m= section; it would be pointless to do an ICE restart for the m= section
3127// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003128TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003129 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003130 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003131 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003132 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003133 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003134 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3135 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003136
3137 // Do initial offer/answer so there's something to restart.
3138 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003139 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003140
3141 // Change ICE policy, which should set the "needs-ice-restart" flag.
3142 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003143 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003144
3145 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003146 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003147 webrtc::CreateSessionDescription(SdpType::kOffer,
3148 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003149 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003150 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3151 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003152 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003153 CreateAnswerAsLocalDescription();
3154
3155 // Grab the ufrags.
3156 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003157 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003158
3159 // Create offer and grab the new ufrags.
3160 CreateOfferAsLocalDescription();
3161 std::vector<std::string> subsequent_ufrags =
3162 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003163 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003164
3165 // Ensure that only the ufrag for the second m= section changed.
3166 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3167 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3168}
3169
deadbeeffe4a8a42016-12-20 17:56:17 -08003170// Tests that the methods to return current/pending descriptions work as
3171// expected at different points in the offer/answer exchange. This test does
3172// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003173TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003174 // This disables DTLS so we can apply an answer to ourselves.
3175 CreatePeerConnection();
3176
3177 // Create initial local offer and get SDP (which will also be used as
3178 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003179 std::unique_ptr<SessionDescriptionInterface> local_offer;
3180 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003181 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003182 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003183
3184 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003185 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3186 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3187 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003188 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3189 EXPECT_EQ(nullptr, pc_->current_local_description());
3190 EXPECT_EQ(nullptr, pc_->current_remote_description());
3191
3192 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003193 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003194 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003195 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3196 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3197 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3198 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003199 EXPECT_EQ(nullptr, pc_->current_local_description());
3200 EXPECT_EQ(nullptr, pc_->current_remote_description());
3201
3202 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003203 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003204 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003205 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3206 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003207 EXPECT_EQ(nullptr, pc_->pending_local_description());
3208 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003209 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3210 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003211
3212 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003213 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003214 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003215 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3216 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3217 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003218 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003219 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3220 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003221
3222 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003223 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003224 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003225 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3226 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3227 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3228 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3229 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3230 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003231
3232 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003233 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003234 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003235 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3236 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003237 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3238 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003239 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3240 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003241}
3242
zhihuang77985012017-02-07 15:45:16 -08003243// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3244// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003245// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003246// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003247TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003248 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3249 CreatePeerConnection();
3250 // The RtcEventLog will be reset when the PeerConnection is closed.
3251 pc_->Close();
3252
Niels Möllerdec9f742019-06-03 15:25:20 +02003253 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003254 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003255 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003256 pc_->StopRtcEventLog();
3257}
3258
deadbeef30952b42017-04-21 02:41:29 -07003259// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003260TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003261 CreatePeerConnection();
3262
3263 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003264 RTCOfferAnswerOptions options;
3265 options.offer_to_receive_audio = 1;
3266 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003267 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003268 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003269 cricket::SessionDescription* desc = offer->description();
3270 ASSERT_EQ(2u, desc->transport_infos().size());
3271 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3272 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3273
3274 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003275 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003276 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003277 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3278 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003279 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003280 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003281 desc = answer->description();
3282 ASSERT_EQ(2u, desc->transport_infos().size());
3283 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3284 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3285}
3286
deadbeef1dcb1642017-03-29 21:08:16 -07003287// Test that ICE renomination isn't offered if it's not enabled in the PC's
3288// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003289TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003290 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003291 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003292 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003293 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003294 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003295
3296 std::unique_ptr<SessionDescriptionInterface> offer;
3297 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3298 cricket::SessionDescription* desc = offer->description();
3299 EXPECT_EQ(1u, desc->transport_infos().size());
3300 EXPECT_FALSE(
3301 desc->transport_infos()[0].description.GetIceParameters().renomination);
3302}
3303
3304// Test that the ICE renomination option is present in generated offers/answers
3305// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003306TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003307 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003308 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003309 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003310 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003311 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003312
3313 std::unique_ptr<SessionDescriptionInterface> offer;
3314 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3315 cricket::SessionDescription* desc = offer->description();
3316 EXPECT_EQ(1u, desc->transport_infos().size());
3317 EXPECT_TRUE(
3318 desc->transport_infos()[0].description.GetIceParameters().renomination);
3319
3320 // Set the offer as a remote description, then create an answer and ensure it
3321 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003322 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003323 std::unique_ptr<SessionDescriptionInterface> answer;
3324 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3325 desc = answer->description();
3326 EXPECT_EQ(1u, desc->transport_infos().size());
3327 EXPECT_TRUE(
3328 desc->transport_infos()[0].description.GetIceParameters().renomination);
3329}
3330
3331// Test that if CreateOffer is called with the deprecated "offer to receive
3332// audio/video" constraints, they're processed and result in an offer with
3333// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003334TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003335 CreatePeerConnection();
3336
Niels Möllerf06f9232018-08-07 12:32:18 +02003337 RTCOfferAnswerOptions options;
3338 options.offer_to_receive_audio = 1;
3339 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003340 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003341 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003342
3343 cricket::SessionDescription* desc = offer->description();
3344 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3345 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3346 ASSERT_NE(nullptr, audio);
3347 ASSERT_NE(nullptr, video);
3348 EXPECT_FALSE(audio->rejected);
3349 EXPECT_FALSE(video->rejected);
3350}
3351
3352// Test that if CreateAnswer is called with the deprecated "offer to receive
3353// audio/video" constraints, they're processed and can be used to reject an
3354// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003355// Don't run under Unified Plan since this behavior is not supported.
3356TEST_F(PeerConnectionInterfaceTestPlanB,
3357 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003358 CreatePeerConnection();
3359
3360 // First, create an offer with audio/video and apply it as a remote
3361 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003362 RTCOfferAnswerOptions options;
3363 options.offer_to_receive_audio = 1;
3364 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003365 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003366 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003367 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003368
3369 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003370 options.offer_to_receive_audio = 0;
3371 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003372 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003373 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003374
3375 cricket::SessionDescription* desc = answer->description();
3376 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3377 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3378 ASSERT_NE(nullptr, audio);
3379 ASSERT_NE(nullptr, video);
3380 EXPECT_TRUE(audio->rejected);
3381 EXPECT_TRUE(video->rejected);
3382}
3383
deadbeef1dcb1642017-03-29 21:08:16 -07003384// Test that negotiation can succeed with a data channel only, and with the max
3385// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003386#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003387TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3388#else
3389TEST_P(PeerConnectionInterfaceTest,
3390 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003391#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003392 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003393 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003394 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003395 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003396
3397 // First, create an offer with only a data channel and apply it as a remote
3398 // description.
Florent Castelli72424402022-04-06 03:45:10 +02003399 pc_->CreateDataChannelOrError("test", nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003400 std::unique_ptr<SessionDescriptionInterface> offer;
3401 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003402 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003403
3404 // Create and set answer as well.
3405 std::unique_ptr<SessionDescriptionInterface> answer;
3406 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003407 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003408}
3409
Steve Anton36da6ff2018-02-16 16:04:20 -08003410TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003411 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003412 BitrateSettings bitrate;
3413 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003414 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3415}
3416
Steve Anton36da6ff2018-02-16 16:04:20 -08003417TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003418 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003419 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003420 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003421 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3422}
3423
Steve Anton36da6ff2018-02-16 16:04:20 -08003424TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003425 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003426 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003427 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003428 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003429 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3430}
3431
Steve Anton36da6ff2018-02-16 16:04:20 -08003432TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003433 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003434 BitrateSettings bitrate;
3435 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003436 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3437}
3438
Steve Anton36da6ff2018-02-16 16:04:20 -08003439TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003440 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003441 BitrateSettings bitrate;
3442 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003443 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003444 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3445}
3446
Steve Anton36da6ff2018-02-16 16:04:20 -08003447TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003448 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003449 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003450 bitrate.min_bitrate_bps = 10;
3451 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003452 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3453}
3454
Steve Anton36da6ff2018-02-16 16:04:20 -08003455TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003456 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003457 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003458 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003459 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3460}
3461
Niels Möller0c4f7be2018-05-07 14:01:37 +02003462// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003463// by Call's BitrateConstraints, which comes from the SDP or a default value.
3464// This test checks that a call to SetBitrate with a current bitrate that will
3465// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003466TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003467 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003468 BitrateSettings bitrate;
3469 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003470 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3471}
3472
zhihuang1c378ed2017-08-17 14:10:50 -07003473// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003474TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003475 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3476 RTCOfferAnswerOptions rtc_options;
3477
3478 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3479 // than kMaxOfferToReceiveMedia should be treated as invalid.
3480 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3481 CreatePeerConnection();
3482 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3483
3484 rtc_options.offer_to_receive_audio =
3485 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3486 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3487}
3488
Steve Anton36da6ff2018-02-16 16:04:20 -08003489TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003490 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3491 RTCOfferAnswerOptions rtc_options;
3492
3493 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3494 // than kMaxOfferToReceiveMedia should be treated as invalid.
3495 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3496 CreatePeerConnection();
3497 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3498
3499 rtc_options.offer_to_receive_video =
3500 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3501 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3502}
3503
3504// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003505// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003506TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003507 RTCOfferAnswerOptions rtc_options;
3508 rtc_options.offer_to_receive_audio = 1;
3509 rtc_options.offer_to_receive_video = 1;
3510
3511 std::unique_ptr<SessionDescriptionInterface> offer;
3512 CreatePeerConnection();
3513 offer = CreateOfferWithOptions(rtc_options);
3514 ASSERT_TRUE(offer);
3515 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3516 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3517}
3518
3519// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003520// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003521TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003522 RTCOfferAnswerOptions rtc_options;
3523 rtc_options.offer_to_receive_audio = 1;
3524 rtc_options.offer_to_receive_video = 0;
3525
3526 std::unique_ptr<SessionDescriptionInterface> offer;
3527 CreatePeerConnection();
3528 offer = CreateOfferWithOptions(rtc_options);
3529 ASSERT_TRUE(offer);
3530 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3531 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3532}
3533
Artem Titov880fa812021-07-30 22:30:23 +02003534// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003535// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003536TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003537 RTCOfferAnswerOptions rtc_options;
3538 rtc_options.offer_to_receive_audio = 0;
3539 rtc_options.offer_to_receive_video = 1;
3540
3541 std::unique_ptr<SessionDescriptionInterface> offer;
3542 CreatePeerConnection();
3543 offer = CreateOfferWithOptions(rtc_options);
3544 ASSERT_TRUE(offer);
3545 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3546 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3547}
3548
zhihuang1c378ed2017-08-17 14:10:50 -07003549// Test that no media content will be added to the offer if using default
3550// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003551TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003552 RTCOfferAnswerOptions rtc_options;
3553
3554 std::unique_ptr<SessionDescriptionInterface> offer;
3555 CreatePeerConnection();
3556 offer = CreateOfferWithOptions(rtc_options);
3557 ASSERT_TRUE(offer);
3558 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3559 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3560}
3561
Artem Titov880fa812021-07-30 22:30:23 +02003562// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003563// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003564TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3565 CreatePeerConnection();
3566
zhihuang1c378ed2017-08-17 14:10:50 -07003567 RTCOfferAnswerOptions rtc_options;
3568 rtc_options.ice_restart = false;
3569 rtc_options.offer_to_receive_audio = 1;
3570
3571 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003572 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003573 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3574 auto ufrag1 =
3575 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3576 auto pwd1 =
3577 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003578
Artem Titov880fa812021-07-30 22:30:23 +02003579 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003580 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003581 auto ufrag2 =
3582 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3583 auto pwd2 =
3584 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003585
Artem Titov880fa812021-07-30 22:30:23 +02003586 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003587 rtc_options.ice_restart = true;
3588 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003589 auto ufrag3 =
3590 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3591 auto pwd3 =
3592 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003593
3594 EXPECT_EQ(ufrag1, ufrag2);
3595 EXPECT_EQ(pwd1, pwd2);
3596 EXPECT_NE(ufrag2, ufrag3);
3597 EXPECT_NE(pwd2, pwd3);
3598}
3599
Artem Titov880fa812021-07-30 22:30:23 +02003600// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003601// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003602TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003603 RTCOfferAnswerOptions rtc_options;
3604 rtc_options.offer_to_receive_audio = 1;
3605 rtc_options.offer_to_receive_video = 1;
3606
3607 std::unique_ptr<SessionDescriptionInterface> offer;
3608 CreatePeerConnection();
3609
3610 rtc_options.use_rtp_mux = true;
3611 offer = CreateOfferWithOptions(rtc_options);
3612 ASSERT_TRUE(offer);
3613 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3614 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3615 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3616
3617 rtc_options.use_rtp_mux = false;
3618 offer = CreateOfferWithOptions(rtc_options);
3619 ASSERT_TRUE(offer);
3620 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3621 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3622 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3623}
3624
zhihuang141aacb2017-08-29 13:23:53 -07003625// This test ensures OnRenegotiationNeeded is called when we add track with
3626// MediaStream -> AddTrack in the same way it is called when we add track with
3627// PeerConnection -> AddTrack.
3628// The test can be removed once addStream is rewritten in terms of addTrack
3629// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003630// Don't run under Unified Plan since the stream API is not available.
3631TEST_F(PeerConnectionInterfaceTestPlanB,
3632 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003633 CreatePeerConnectionWithoutDtls();
3634 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003635 pc_factory_->CreateLocalMediaStream(kStreamId1));
Niels Möllerafb246b2022-04-20 14:26:50 +02003636 pc_->AddStream(stream.get());
zhihuang141aacb2017-08-29 13:23:53 -07003637 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003638 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003639 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003640 CreateVideoTrack("video_track"));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003641 stream->AddTrack(audio_track);
zhihuang141aacb2017-08-29 13:23:53 -07003642 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3643 observer_.renegotiation_needed_ = false;
3644
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003645 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003646 stream->AddTrack(video_track);
zhihuang141aacb2017-08-29 13:23:53 -07003647 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3648 observer_.renegotiation_needed_ = false;
3649
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003650 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003651 stream->RemoveTrack(audio_track);
zhihuang141aacb2017-08-29 13:23:53 -07003652 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3653 observer_.renegotiation_needed_ = false;
3654
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003655 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003656 stream->RemoveTrack(video_track);
zhihuang141aacb2017-08-29 13:23:53 -07003657 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3658 observer_.renegotiation_needed_ = false;
3659}
3660
Zhi Huangb2d355e2017-10-26 17:26:37 -07003661// Tests that an error is returned if a description is applied that has fewer
3662// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003663TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003664 MediaSectionCountEnforcedForSubsequentOffer) {
3665 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003666 AddAudioTrack("audio_label");
3667 AddVideoTrack("video_label");
3668
Zhi Huangb2d355e2017-10-26 17:26:37 -07003669 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003670 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003671 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3672
3673 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003674 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003675 offer->description()->contents().pop_back();
3676 offer->description()->contents().pop_back();
3677 ASSERT_TRUE(offer->description()->contents().empty());
3678 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3679
3680 std::unique_ptr<SessionDescriptionInterface> answer;
3681 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3682 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3683
3684 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003685 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003686 offer->description()->contents().pop_back();
3687 offer->description()->contents().pop_back();
3688 ASSERT_TRUE(offer->description()->contents().empty());
3689 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3690}
3691
Johannes Kron89f874e2018-11-12 10:25:48 +01003692TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3693 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003694 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003695 CreatePeerConnection(config);
3696 std::unique_ptr<SessionDescriptionInterface> offer;
3697 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003698 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3699 // Possible to set to false.
3700 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003701 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003702 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003703 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003704 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003705}
3706
Byoungchan Lee10a7d232022-07-20 21:49:17 +09003707TEST_P(PeerConnectionInterfaceTest,
3708 RtpSenderSetDegradationPreferenceWithoutEncodings) {
3709 CreatePeerConnection();
3710 AddVideoTrack("video_label");
3711
3712 std::vector<rtc::scoped_refptr<RtpSenderInterface>> rtp_senders =
3713 pc_->GetSenders();
3714 ASSERT_EQ(rtp_senders.size(), 1u);
3715 ASSERT_EQ(rtp_senders[0]->media_type(), cricket::MEDIA_TYPE_VIDEO);
3716 rtc::scoped_refptr<RtpSenderInterface> video_rtp_sender = rtp_senders[0];
3717 RtpParameters parameters = video_rtp_sender->GetParameters();
3718 ASSERT_NE(parameters.degradation_preference,
3719 DegradationPreference::MAINTAIN_RESOLUTION);
3720 parameters.degradation_preference =
3721 DegradationPreference::MAINTAIN_RESOLUTION;
3722 ASSERT_TRUE(video_rtp_sender->SetParameters(parameters).ok());
3723
3724 std::unique_ptr<SessionDescriptionInterface> local_offer;
3725 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
3726 ASSERT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3727
3728 RtpParameters parameters_new = video_rtp_sender->GetParameters();
3729 ASSERT_EQ(parameters_new.degradation_preference,
3730 DegradationPreference::MAINTAIN_RESOLUTION);
3731}
3732
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003733INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3734 PeerConnectionInterfaceTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003735 Values(SdpSemantics::kPlanB_DEPRECATED,
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003736 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003737
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003738class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003739 protected:
3740 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003741 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003742 }
nisseeaabdf62017-05-05 02:23:02 -07003743 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003744 const RTCConfiguration& config) {
Florent Castelli72424402022-04-06 03:45:10 +02003745 PeerConnectionDependencies pc_dependencies(&observer_);
3746 auto result =
3747 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3748 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02003749 observer_.SetPeerConnectionInterface(result.value().get());
Florent Castelli72424402022-04-06 03:45:10 +02003750 return result.value()->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003751 }
3752
zhihuang9763d562016-08-05 11:14:50 -07003753 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003754 MockPeerConnectionObserver observer_;
3755};
3756
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003757// This sanity check validates the test infrastructure itself.
3758TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3759 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003760 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Florent Castelli72424402022-04-06 03:45:10 +02003761 PeerConnectionDependencies pc_dependencies(&observer_);
3762 auto result =
3763 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3764 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02003765 observer_.SetPeerConnectionInterface(result.value().get());
Florent Castelli72424402022-04-06 03:45:10 +02003766 result.value()->Close(); // No abort -> ok.
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003767 SUCCEED();
3768}
3769
nisse51542be2016-02-12 02:27:06 -08003770// This test verifies the default behaviour with no constraints and a
3771// default RTCConfiguration.
3772TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3773 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003774 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003775
Niels Möllerf06f9232018-08-07 12:32:18 +02003776 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003777
Henrik Boström35c5cc82022-04-14 09:23:20 +02003778 EXPECT_TRUE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003779 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3780 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003781 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003782 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003783}
3784
Niels Möller1d7ecd22018-01-18 15:25:12 +01003785// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003786// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003787TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3788 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003789 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003790
Niels Möller71bdda02016-03-31 12:59:59 +02003791 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003792 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003793
Niels Möller1d7ecd22018-01-18 15:25:12 +01003794 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003795}
3796
Niels Möller6539f692018-01-18 08:58:50 +01003797// This test verifies that the experiment_cpu_load_estimator flag is
3798// propagated from RTCConfiguration to the PeerConnection.
3799TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3800 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003801 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Niels Möller6539f692018-01-18 08:58:50 +01003802
3803 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003804 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003805
3806 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3807}
3808
deadbeef293e9262017-01-11 12:28:30 -08003809// Tests a few random fields being different.
3810TEST(RTCConfigurationTest, ComparisonOperators) {
3811 PeerConnectionInterface::RTCConfiguration a;
3812 PeerConnectionInterface::RTCConfiguration b;
3813 EXPECT_EQ(a, b);
3814
3815 PeerConnectionInterface::RTCConfiguration c;
3816 c.servers.push_back(PeerConnectionInterface::IceServer());
3817 EXPECT_NE(a, c);
3818
3819 PeerConnectionInterface::RTCConfiguration d;
3820 d.type = PeerConnectionInterface::kRelay;
3821 EXPECT_NE(a, d);
3822
3823 PeerConnectionInterface::RTCConfiguration e;
3824 e.audio_jitter_buffer_max_packets = 5;
3825 EXPECT_NE(a, e);
3826
3827 PeerConnectionInterface::RTCConfiguration f;
3828 f.ice_connection_receiving_timeout = 1337;
3829 EXPECT_NE(a, f);
3830
3831 PeerConnectionInterface::RTCConfiguration g;
Henrik Boström24e03372022-10-27 13:49:10 +02003832 g.DEPRECATED_disable_ipv6 = true;
deadbeef293e9262017-01-11 12:28:30 -08003833 EXPECT_NE(a, g);
3834
3835 PeerConnectionInterface::RTCConfiguration h(
3836 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3837 EXPECT_NE(a, h);
3838}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003839
3840} // namespace
3841} // namespace webrtc