blob: f7f408bcc82f5d3f2c53dfcdf842c6b601e05ec4 [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"
Sameer Vijaykar0793ee72023-01-23 16:31:29 +010081#include "test/scoped_key_value_config.h"
kwibergac9f8762016-09-30 22:29:43 -070082
83#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080084#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070085#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010087namespace webrtc {
88namespace {
89
Seth Hampson845e8782018-03-02 11:34:10 -080090static const char kStreamId1[] = "local_stream_1";
91static const char kStreamId2[] = "local_stream_2";
92static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093static const int kDefaultStunPort = 3478;
94static const char kStunAddressOnly[] = "stun:address";
95static const char kStunInvalidPort[] = "stun:address:-1";
96static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
97static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +010098static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const char kTurnUsername[] = "user";
100static const char kTurnPassword[] = "password";
101static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
deadbeefab9b2d12015-10-14 11:33:11 -0700104static const char kStreams[][8] = {"stream1", "stream2"};
105static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
106static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
107
deadbeef5e97fb52015-10-15 12:49:08 -0700108static const char kRecvonly[] = "recvonly";
109static const char kSendrecv[] = "sendrecv";
Philipp Hanckefd91d022022-10-27 20:08:23 +0200110constexpr uint64_t kTiebreakerDefault = 44444;
deadbeef5e97fb52015-10-15 12:49:08 -0700111
deadbeefab9b2d12015-10-14 11:33:11 -0700112// Reference SDP with a MediaStream with label "stream1" and audio track with
113// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800114static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700115 "v=0\r\n"
116 "o=- 0 0 IN IP4 127.0.0.1\r\n"
117 "s=-\r\n"
118 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100119 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700120 "a=ice-ufrag:e5785931\r\n"
121 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
122 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
123 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700124 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700125 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800126 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100127 "a=rtpmap:111 OPUS/48000/2\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700128 "a=ssrc:1 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000129 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700130 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800131 "a=ice-ufrag:e5785931\r\n"
132 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
133 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
134 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800137 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=rtpmap:120 VP8/90000\r\n"
139 "a=ssrc:2 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000140 "a=ssrc:2 msid:stream1 videotrack0\r\n";
141// Same string as above but with the MID changed to the Unified Plan default and
142// a=msid added. This is needed so that this SDP can be used as an answer for a
143// Unified Plan offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800144static const char kSdpStringWithStream1UnifiedPlan[] =
145 "v=0\r\n"
146 "o=- 0 0 IN IP4 127.0.0.1\r\n"
147 "s=-\r\n"
148 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100149 "m=audio 1 RTP/AVPF 111\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800150 "a=ice-ufrag:e5785931\r\n"
151 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
152 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
153 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
154 "a=mid:0\r\n"
155 "a=sendrecv\r\n"
156 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100157 "a=rtpmap:111 OPUS/48000/2\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000158 "a=msid:stream1 audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800159 "a=ssrc:1 cname:stream1\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800160 "m=video 1 RTP/AVPF 120\r\n"
161 "a=ice-ufrag:e5785931\r\n"
162 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
163 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
164 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
165 "a=mid:1\r\n"
166 "a=sendrecv\r\n"
167 "a=rtcp-mux\r\n"
168 "a=rtpmap:120 VP8/90000\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000169 "a=msid:stream1 videotrack0\r\n"
170 "a=ssrc:2 cname:stream1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700171
zhihuang81c3a032016-11-17 12:06:24 -0800172// Reference SDP with a MediaStream with label "stream1" and audio track with
173// id "audio_1";
174static const char kSdpStringWithStream1AudioTrackOnly[] =
175 "v=0\r\n"
176 "o=- 0 0 IN IP4 127.0.0.1\r\n"
177 "s=-\r\n"
178 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100179 "m=audio 1 RTP/AVPF 111\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800180 "a=ice-ufrag:e5785931\r\n"
181 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
182 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
183 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800184 "a=mid:audio\r\n"
185 "a=sendrecv\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100186 "a=rtpmap:111 OPUS/48000/2\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800187 "a=ssrc:1 cname:stream1\r\n"
Harald Alvestrand88b8dec2022-04-07 09:54:51 +0000188 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800189 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800190
deadbeefab9b2d12015-10-14 11:33:11 -0700191// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
192// MediaStreams have one audio track and one video track.
193// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800194static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700195 "v=0\r\n"
196 "o=- 0 0 IN IP4 127.0.0.1\r\n"
197 "s=-\r\n"
198 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800199 "a=msid-semantic: WMS stream1 stream2\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100200 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700201 "a=ice-ufrag:e5785931\r\n"
202 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
203 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
204 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700206 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800207 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100208 "a=rtpmap:111 OPUS/48000/2\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=ssrc:1 cname:stream1\r\n"
210 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
211 "a=ssrc:3 cname:stream2\r\n"
212 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
213 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800214 "a=ice-ufrag:e5785931\r\n"
215 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
216 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
217 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800220 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=rtpmap:120 VP8/0\r\n"
222 "a=ssrc:2 cname:stream1\r\n"
223 "a=ssrc:2 msid:stream1 videotrack0\r\n"
224 "a=ssrc:4 cname:stream2\r\n"
225 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800226static const char kSdpStringWithStream1And2UnifiedPlan[] =
227 "v=0\r\n"
228 "o=- 0 0 IN IP4 127.0.0.1\r\n"
229 "s=-\r\n"
230 "t=0 0\r\n"
231 "a=msid-semantic: WMS stream1 stream2\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100232 "m=audio 1 RTP/AVPF 111\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800233 "a=ice-ufrag:e5785931\r\n"
234 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
235 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
236 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
237 "a=mid:0\r\n"
238 "a=sendrecv\r\n"
239 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100240 "a=rtpmap:111 OPUS/48000/2\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800241 "a=ssrc:1 cname:stream1\r\n"
242 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
243 "m=video 1 RTP/AVPF 120\r\n"
244 "a=ice-ufrag:e5785931\r\n"
245 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
246 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
247 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
248 "a=mid:1\r\n"
249 "a=sendrecv\r\n"
250 "a=rtcp-mux\r\n"
251 "a=rtpmap:120 VP8/0\r\n"
252 "a=ssrc:2 cname:stream1\r\n"
253 "a=ssrc:2 msid:stream1 videotrack0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100254 "m=audio 1 RTP/AVPF 111\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800255 "a=ice-ufrag:e5785931\r\n"
256 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
257 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
258 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
259 "a=mid:2\r\n"
260 "a=sendrecv\r\n"
261 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100262 "a=rtpmap:111 OPUS/48000/2\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800263 "a=ssrc:3 cname:stream2\r\n"
264 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
265 "m=video 1 RTP/AVPF 120\r\n"
266 "a=ice-ufrag:e5785931\r\n"
267 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
268 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
269 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
270 "a=mid:3\r\n"
271 "a=sendrecv\r\n"
272 "a=rtcp-mux\r\n"
273 "a=rtpmap:120 VP8/0\r\n"
274 "a=ssrc:4 cname:stream2\r\n"
275 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700276
277// Reference SDP without MediaStreams. Msid is not supported.
278static const char kSdpStringWithoutStreams[] =
279 "v=0\r\n"
280 "o=- 0 0 IN IP4 127.0.0.1\r\n"
281 "s=-\r\n"
282 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100283 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700284 "a=ice-ufrag:e5785931\r\n"
285 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
286 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
287 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700288 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700289 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800290 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100291 "a=rtpmap:111 OPUS/48000/2\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700292 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800293 "a=ice-ufrag:e5785931\r\n"
294 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
295 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
296 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700297 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700298 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800299 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700300 "a=rtpmap:120 VP8/90000\r\n";
301
302// Reference SDP without MediaStreams. Msid is supported.
303static const char kSdpStringWithMsidWithoutStreams[] =
304 "v=0\r\n"
305 "o=- 0 0 IN IP4 127.0.0.1\r\n"
306 "s=-\r\n"
307 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800308 "a=msid-semantic: WMS\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100309 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700310 "a=ice-ufrag:e5785931\r\n"
311 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
312 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
313 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700314 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700315 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800316 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100317 "a=rtpmap:111 OPUS/48000/2\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700318 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800319 "a=ice-ufrag:e5785931\r\n"
320 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
321 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
322 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700323 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700324 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800325 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700326 "a=rtpmap:120 VP8/90000\r\n";
327
328// Reference SDP without MediaStreams and audio only.
329static const char kSdpStringWithoutStreamsAudioOnly[] =
330 "v=0\r\n"
331 "o=- 0 0 IN IP4 127.0.0.1\r\n"
332 "s=-\r\n"
333 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100334 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700335 "a=ice-ufrag:e5785931\r\n"
336 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
337 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
338 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700339 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700340 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800341 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100342 "a=rtpmap:111 OPUS/48000/2\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700343
344// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
345static const char kSdpStringSendOnlyWithoutStreams[] =
346 "v=0\r\n"
347 "o=- 0 0 IN IP4 127.0.0.1\r\n"
348 "s=-\r\n"
349 "t=0 0\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100350 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700351 "a=ice-ufrag:e5785931\r\n"
352 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
353 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
354 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700355 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700356 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700357 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800358 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100359 "a=rtpmap:111 OPUS/48000/2\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700360 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800361 "a=ice-ufrag:e5785931\r\n"
362 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
363 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
364 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700365 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700366 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700367 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800368 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700369 "a=rtpmap:120 VP8/90000\r\n";
370
371static const char kSdpStringInit[] =
372 "v=0\r\n"
373 "o=- 0 0 IN IP4 127.0.0.1\r\n"
374 "s=-\r\n"
375 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700376 "a=msid-semantic: WMS\r\n";
377
378static const char kSdpStringAudio[] =
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100379 "m=audio 1 RTP/AVPF 111\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800380 "a=ice-ufrag:e5785931\r\n"
381 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
382 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
383 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700384 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700385 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800386 "a=rtcp-mux\r\n"
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100387 "a=rtpmap:111 OPUS/48000/2\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700388
389static const char kSdpStringVideo[] =
390 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800391 "a=ice-ufrag:e5785931\r\n"
392 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
393 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
394 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700395 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700396 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800397 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700398 "a=rtpmap:120 VP8/90000\r\n";
399
400static const char kSdpStringMs1Audio0[] =
401 "a=ssrc:1 cname:stream1\r\n"
402 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
403
404static const char kSdpStringMs1Video0[] =
405 "a=ssrc:2 cname:stream1\r\n"
406 "a=ssrc:2 msid:stream1 videotrack0\r\n";
407
408static const char kSdpStringMs1Audio1[] =
409 "a=ssrc:3 cname:stream1\r\n"
410 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
411
412static const char kSdpStringMs1Video1[] =
413 "a=ssrc:4 cname:stream1\r\n"
414 "a=ssrc:4 msid:stream1 videotrack1\r\n";
415
deadbeef8662f942017-01-20 21:20:51 -0800416static const char kDtlsSdesFallbackSdp[] =
417 "v=0\r\n"
418 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
419 "s=-\r\n"
420 "c=IN IP4 0.0.0.0\r\n"
421 "t=0 0\r\n"
422 "a=group:BUNDLE audio\r\n"
423 "a=msid-semantic: WMS\r\n"
424 "m=audio 1 RTP/SAVPF 0\r\n"
425 "a=sendrecv\r\n"
426 "a=rtcp-mux\r\n"
427 "a=mid:audio\r\n"
428 "a=ssrc:1 cname:stream1\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800429 "a=ice-ufrag:e5785931\r\n"
430 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
431 "a=rtpmap:0 pcmu/8000\r\n"
432 "a=fingerprint:sha-1 "
433 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
434 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700435 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800436 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
437 "dummy_session_params\r\n";
438
Niels Möllerdec9f742019-06-03 15:25:20 +0200439class RtcEventLogOutputNull final : public RtcEventLogOutput {
440 public:
441 bool IsActive() const override { return true; }
Björn Terelius63299a32022-07-05 10:58:52 +0200442 bool Write(const absl::string_view /*output*/) override { return true; }
Niels Möllerdec9f742019-06-03 15:25:20 +0200443};
444
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100445using ::cricket::StreamParams;
Jonas Oreland4b2a1062022-10-19 09:24:42 +0200446using ::testing::Eq;
perkjd61bf802016-03-24 03:16:19 -0700447using ::testing::Exactly;
Jonas Oreland4b2a1062022-10-19 09:24:42 +0200448using ::testing::SizeIs;
Steve Anton36da6ff2018-02-16 16:04:20 -0800449using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450
Steve Anton36da6ff2018-02-16 16:04:20 -0800451using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
452using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700453
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454// Gets the first ssrc of given content type from the ContentInfo.
455bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
456 if (!content_info || !ssrc) {
457 return false;
458 }
459 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800460 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 if (!media_desc || media_desc->streams().empty()) {
462 return false;
463 }
464 *ssrc = media_desc->streams().begin()->first_ssrc();
465 return true;
466}
467
deadbeefd1a38b52016-12-10 13:15:33 -0800468// Get the ufrags out of an SDP blob. Useful for testing ICE restart
469// behavior.
470std::vector<std::string> GetUfrags(
471 const webrtc::SessionDescriptionInterface* desc) {
472 std::vector<std::string> ufrags;
473 for (const cricket::TransportInfo& info :
474 desc->description()->transport_infos()) {
475 ufrags.push_back(info.description.ice_ufrag);
476 }
477 return ufrags;
478}
479
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480void SetSsrcToZero(std::string* sdp) {
481 const char kSdpSsrcAtribute[] = "a=ssrc:";
482 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
483 size_t ssrc_pos = 0;
484 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200485 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 size_t end_ssrc = sdp->find(" ", ssrc_pos);
487 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
488 ssrc_pos = end_ssrc;
489 }
490}
491
Artem Titov880fa812021-07-30 22:30:23 +0200492// Check if `streams` contains the specified track.
deadbeefab9b2d12015-10-14 11:33:11 -0700493bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800494 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700495 const std::string& track_id) {
496 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800497 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700498 return true;
499 }
500 }
501 return false;
502}
503
Artem Titov880fa812021-07-30 22:30:23 +0200504// Check if `senders` contains the specified sender, by id.
deadbeefab9b2d12015-10-14 11:33:11 -0700505bool ContainsSender(
506 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
507 const std::string& id) {
508 for (const auto& sender : senders) {
509 if (sender->id() == id) {
510 return true;
511 }
512 }
513 return false;
514}
515
Artem Titov880fa812021-07-30 22:30:23 +0200516// Check if `senders` contains the specified sender, by id and stream id.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700517bool ContainsSender(
518 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
519 const std::string& id,
520 const std::string& stream_id) {
521 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700522 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700523 return true;
524 }
525 }
526 return false;
527}
528
deadbeefab9b2d12015-10-14 11:33:11 -0700529// Create a collection of streams.
530// CreateStreamCollection(1) creates a collection that
531// correspond to kSdpStringWithStream1.
532// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
533rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700534 int number_of_streams,
535 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700536 rtc::scoped_refptr<StreamCollection> local_collection(
537 StreamCollection::Create());
538
539 for (int i = 0; i < number_of_streams; ++i) {
540 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
541 webrtc::MediaStream::Create(kStreams[i]));
542
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700543 for (int j = 0; j < tracks_per_stream; ++j) {
544 // Add a local audio track.
545 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
546 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
547 nullptr));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000548 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700549
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700550 // Add a local video track.
551 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
552 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700553 webrtc::FakeVideoTrackSource::Create(),
554 rtc::Thread::Current()));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000555 stream->AddTrack(video_track);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700556 }
deadbeefab9b2d12015-10-14 11:33:11 -0700557
558 local_collection->AddStream(stream);
559 }
560 return local_collection;
561}
562
563// Check equality of StreamCollections.
564bool CompareStreamCollections(StreamCollectionInterface* s1,
565 StreamCollectionInterface* s2) {
566 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
567 return false;
568 }
569
570 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700571 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700572 return false;
573 }
574 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
575 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
576 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
577 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
578
579 if (audio_tracks1.size() != audio_tracks2.size()) {
580 return false;
581 }
582 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
583 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
584 return false;
585 }
586 }
587 if (video_tracks1.size() != video_tracks2.size()) {
588 return false;
589 }
590 for (size_t j = 0; j != video_tracks1.size(); ++j) {
591 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
592 return false;
593 }
594 }
595 }
596 return true;
597}
598
perkjd61bf802016-03-24 03:16:19 -0700599// Helper class to test Observer.
600class MockTrackObserver : public ObserverInterface {
601 public:
602 explicit MockTrackObserver(NotifierInterface* notifier)
603 : notifier_(notifier) {
604 notifier_->RegisterObserver(this);
605 }
606
607 ~MockTrackObserver() { Unregister(); }
608
609 void Unregister() {
610 if (notifier_) {
611 notifier_->UnregisterObserver(this);
612 notifier_ = nullptr;
613 }
614 }
615
Danil Chapovalov3a353122020-05-15 11:16:53 +0200616 MOCK_METHOD(void, OnChanged, (), (override));
perkjd61bf802016-03-24 03:16:19 -0700617
618 private:
619 NotifierInterface* notifier_;
620};
621
nisse528b7932017-05-08 03:21:43 -0700622// The PeerConnectionMediaConfig tests below verify that configuration and
623// constraints are propagated into the PeerConnection's MediaConfig. These
624// settings are intended for MediaChannel constructors, but that is not
625// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700626class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
627 public:
zhihuang38ede132017-06-15 12:52:32 -0700628 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
629 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100630 PeerConnectionFactoryDependencies dependencies;
631 dependencies.worker_thread = rtc::Thread::Current();
632 dependencies.network_thread = rtc::Thread::Current();
633 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200634 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200635 dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200636 cricket::MediaEngineDependencies media_deps;
637 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
henrika919dc2e2017-10-12 14:24:55 +0200638 // Use fake audio device module since we're only testing the interface
639 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200640 media_deps.adm = FakeAudioCaptureModule::Create();
641 SetMediaEngineDefaults(&media_deps);
Erik Språngceb44952020-09-22 11:36:35 +0200642 media_deps.trials = dependencies.trials.get();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200643 dependencies.media_engine =
644 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100645 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200646 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200647 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700648
Tommi87f70902021-04-27 14:43:08 +0200649 return rtc::make_ref_counted<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100650 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700651 }
652
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100653 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800654
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100655 private:
deadbeefd7850b22017-08-23 10:59:19 -0700656 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700657};
658
Steve Anton36da6ff2018-02-16 16:04:20 -0800659// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200660class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800662 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
663 : vss_(new rtc::VirtualSocketServer()),
664 main_(vss_.get()),
665 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800666#ifdef WEBRTC_ANDROID
667 webrtc::InitializeAndroidObjects();
668#endif
669 }
670
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200671 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700672 // Use fake audio capture module since we're only testing the interface
673 // level, and using a real one could make tests flaky when run in parallel.
674 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700676 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200677 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
678 fake_audio_capture_module_),
679 webrtc::CreateBuiltinAudioEncoderFactory(),
680 webrtc::CreateBuiltinAudioDecoderFactory(),
681 webrtc::CreateBuiltinVideoEncoderFactory(),
682 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
683 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700684 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 }
686
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200687 void TearDown() override {
688 if (pc_)
689 pc_->Close();
690 }
691
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200693 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
695
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000696 // DTLS does not work in a loopback call, so is disabled for many
deadbeef293e9262017-01-11 12:28:30 -0800697 // tests in this file.
698 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200699 RTCConfiguration config;
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000700 PeerConnectionFactoryInterface::Options options;
701 options.disable_encryption = true;
702 pc_factory_->SetOptions(options);
Niels Möllerf06f9232018-08-07 12:32:18 +0200703 CreatePeerConnection(config);
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000704 options.disable_encryption = false;
705 pc_factory_->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
707
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 void CreatePeerConnectionWithIceTransportsType(
709 PeerConnectionInterface::IceTransportsType type) {
710 PeerConnectionInterface::RTCConfiguration config;
711 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200712 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700713 }
714
715 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100716 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100721 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 server.password = password;
723 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200724 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
Niels Möllerf06f9232018-08-07 12:32:18 +0200727 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200728 if (pc_) {
729 pc_->Close();
730 pc_ = nullptr;
731 }
kwibergd1fe2812016-04-27 06:47:29 -0700732 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Byoungchan Leed58f5262022-06-27 18:05:22 +0900733 new cricket::FakePortAllocator(
734 rtc::Thread::Current(),
Sameer Vijaykar0793ee72023-01-23 16:31:29 +0100735 std::make_unique<rtc::BasicPacketSocketFactory>(vss_.get()),
736 &field_trials_));
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
Sameer Vijaykar0793ee72023-01-23 16:31:29 +01001261 webrtc::test::ScopedKeyValueConfig field_trials_;
deadbeef9a6f4d42017-05-15 19:43:33 -07001262 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1263 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001264 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001265 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001266 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001267 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
zhihuang9763d562016-08-05 11:14:50 -07001268 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;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001337 config.tcp_candidate_policy =
1338 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001339 config.candidate_network_policy =
1340 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001341 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001342 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343
1344 const cricket::FakePortAllocatorSession* session =
1345 static_cast<const cricket::FakePortAllocatorSession*>(
1346 port_allocator_->GetPooledSession());
1347 ASSERT_NE(nullptr, session);
1348 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001349 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001350 EXPECT_LT(0U,
1351 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001352}
1353
deadbeefd21eab32017-07-26 16:50:11 -07001354// Test that network-related RTCConfiguration members are applied to the
1355// PortAllocator when CreatePeerConnection is called. Specifically:
1356// - disable_ipv6_on_wifi
1357// - max_ipv6_networks
1358// - tcp_candidate_policy
1359// - candidate_network_policy
1360// - prune_turn_ports
1361//
1362// Note that the candidate filter (RTCConfiguration::type) is already tested
1363// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001364TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001365 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1366 // Create fake port allocator.
Byoungchan Leed58f5262022-06-27 18:05:22 +09001367 std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory(
1368 new rtc::BasicPacketSocketFactory(socket_server()));
deadbeefd21eab32017-07-26 16:50:11 -07001369 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Sameer Vijaykar0793ee72023-01-23 16:31:29 +01001370 new cricket::FakePortAllocator(
1371 rtc::Thread::Current(), packet_socket_factory.get(), &field_trials_));
deadbeefd21eab32017-07-26 16:50:11 -07001372 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1373
1374 // Create RTCConfiguration with some network-related fields relevant to
1375 // PortAllocator populated.
1376 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001377 config.sdp_semantics = sdp_semantics_;
deadbeefd21eab32017-07-26 16:50:11 -07001378 config.disable_ipv6_on_wifi = true;
1379 config.max_ipv6_networks = 10;
1380 config.tcp_candidate_policy =
1381 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1382 config.candidate_network_policy =
1383 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1384 config.prune_turn_ports = true;
1385
1386 // Create the PC factory and PC with the above config.
1387 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1388 webrtc::CreatePeerConnectionFactory(
1389 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001390 rtc::Thread::Current(), fake_audio_capture_module_,
1391 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001392 webrtc::CreateBuiltinAudioDecoderFactory(),
1393 webrtc::CreateBuiltinVideoEncoderFactory(),
1394 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1395 nullptr /* audio_processing */));
Florent Castelli72424402022-04-06 03:45:10 +02001396 PeerConnectionDependencies pc_dependencies(&observer_);
1397 pc_dependencies.allocator = std::move(port_allocator);
1398 auto result = pc_factory_->CreatePeerConnectionOrError(
1399 config, std::move(pc_dependencies));
1400 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02001401 observer_.SetPeerConnectionInterface(result.value().get());
deadbeefd21eab32017-07-26 16:50:11 -07001402
1403 // Now validate that the config fields set above were applied to the
1404 // PortAllocator, as flags or otherwise.
1405 EXPECT_FALSE(raw_port_allocator->flags() &
1406 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1407 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1408 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1409 EXPECT_TRUE(raw_port_allocator->flags() &
1410 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001411 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1412 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001413}
1414
deadbeef46c73892016-11-16 19:42:04 -08001415// Check that GetConfiguration returns the configuration the PeerConnection was
1416// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001417TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001418 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001419 config.sdp_semantics = sdp_semantics_;
deadbeef46c73892016-11-16 19:42:04 -08001420 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001421 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001422
1423 PeerConnectionInterface::RTCConfiguration returned_config =
1424 pc_->GetConfiguration();
1425 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1426}
1427
1428// Check that GetConfiguration returns the last configuration passed into
1429// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001430TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001431 PeerConnectionInterface::RTCConfiguration starting_config;
Henrik Boström62995db2022-01-03 09:58:10 +01001432 starting_config.sdp_semantics = sdp_semantics_;
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001433 starting_config.bundle_policy =
1434 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1435 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001436
Steve Anton36da6ff2018-02-16 16:04:20 -08001437 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001438 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001439 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001440
1441 PeerConnectionInterface::RTCConfiguration returned_config =
1442 pc_->GetConfiguration();
1443 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1444}
1445
Steve Antonc79268f2018-04-24 09:54:10 -07001446TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1447 CreatePeerConnection();
1448
1449 pc_->Close();
1450
1451 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001452 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001453}
1454
Steve Anton36da6ff2018-02-16 16:04:20 -08001455TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001456 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001457 AddVideoStream(kStreamId1);
1458 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 ASSERT_EQ(2u, pc_->local_streams()->count());
1460
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001461 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001462 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001463 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001464 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möllerafb246b2022-04-20 14:26:50 +02001465 pc_factory_->CreateAudioTrack(
1466 kStreamId3, static_cast<AudioSourceInterface*>(nullptr)));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001467 stream->AddTrack(audio_track);
Niels Möllerafb246b2022-04-20 14:26:50 +02001468 EXPECT_TRUE(pc_->AddStream(stream.get()));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001469 EXPECT_EQ(3u, pc_->local_streams()->count());
1470
1471 // Remove the third stream.
1472 pc_->RemoveStream(pc_->local_streams()->at(2));
1473 EXPECT_EQ(2u, pc_->local_streams()->count());
1474
1475 // Remove the second stream.
1476 pc_->RemoveStream(pc_->local_streams()->at(1));
1477 EXPECT_EQ(1u, pc_->local_streams()->count());
1478
1479 // Remove the first stream.
1480 pc_->RemoveStream(pc_->local_streams()->at(0));
1481 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482}
1483
deadbeefab9b2d12015-10-14 11:33:11 -07001484// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001485// Don't run under Unified Plan since the stream API is not available.
1486TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001487 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001488 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001489 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001490 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001491
deadbeefab9b2d12015-10-14 11:33:11 -07001492 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001493 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001494 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001495
deadbeefab9b2d12015-10-14 11:33:11 -07001496 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001497 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001498 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001499
1500 // Add another stream and ensure the offer includes both the old and new
1501 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001502 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001503 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001504
Steve Antonb1c1de12017-12-21 15:14:30 -08001505 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001506 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1507 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001508
Steve Antonb1c1de12017-12-21 15:14:30 -08001509 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001510 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1511 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001512}
1513
Steve Anton36da6ff2018-02-16 16:04:20 -08001514// Don't run under Unified Plan since the stream API is not available.
1515TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001516 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001517 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 ASSERT_EQ(1u, pc_->local_streams()->count());
1519 pc_->RemoveStream(pc_->local_streams()->at(0));
1520 EXPECT_EQ(0u, pc_->local_streams()->count());
1521}
1522
deadbeefe1f9d832016-01-14 15:35:42 -08001523// Test for AddTrack and RemoveTrack methods.
1524// Tests that the created offer includes tracks we added,
1525// and that the RtpSenders are created correctly.
1526// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001527// Only tested with Plan B since Unified Plan is covered in more detail by tests
1528// in peerconnection_jsep_unittests.cc
1529TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001530 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001531 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001532 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001533 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001534 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001535 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1536 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001537 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001538 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001539 EXPECT_EQ("audio_track", audio_sender->id());
1540 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001541 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001542 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001543 EXPECT_EQ("video_track", video_sender->id());
1544 EXPECT_EQ(video_track, video_sender->track());
1545
1546 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001547 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001548 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001549
1550 const cricket::ContentInfo* audio_content =
1551 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001552 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001553 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001554
1555 const cricket::ContentInfo* video_content =
1556 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001557 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001558 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001559
Steve Antondb45ca82017-09-11 18:27:34 -07001560 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001561
1562 // Now try removing the tracks.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001563 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1564 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001565
1566 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001567 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001570 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001571 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001572
1573 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001574 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001575 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001576
Steve Antondb45ca82017-09-11 18:27:34 -07001577 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001578
1579 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1580 // should return false.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001581 EXPECT_FALSE(pc_->RemoveTrackOrError(audio_sender).ok());
1582 EXPECT_FALSE(pc_->RemoveTrackOrError(video_sender).ok());
deadbeefe1f9d832016-01-14 15:35:42 -08001583}
1584
Jonas Oreland4b2a1062022-10-19 09:24:42 +02001585// Test for AddTrack with init_send_encoding.
1586TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackWithSendEncodings) {
1587 CreatePeerConnectionWithoutDtls();
1588 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1589 CreateAudioTrack("audio_track"));
1590 rtc::scoped_refptr<VideoTrackInterface> video_track(
1591 CreateVideoTrack("video_track"));
1592 RtpEncodingParameters audio_encodings;
1593 audio_encodings.active = false;
1594 auto audio_sender =
1595 pc_->AddTrack(audio_track, {kStreamId1}, {audio_encodings}).MoveValue();
1596 RtpEncodingParameters video_encodings;
1597 video_encodings.active = true;
1598 auto video_sender =
1599 pc_->AddTrack(video_track, {kStreamId1}, {video_encodings}).MoveValue();
1600 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1601 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
1602 EXPECT_EQ("audio_track", audio_sender->id());
1603 EXPECT_EQ(audio_track, audio_sender->track());
1604 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1605 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
1606 EXPECT_EQ("video_track", video_sender->id());
1607 EXPECT_EQ(video_track, video_sender->track());
1608
1609 // Now create an offer and check for the senders.
1610 std::unique_ptr<SessionDescriptionInterface> offer;
1611 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1612
1613 const cricket::ContentInfo* audio_content =
1614 cricket::GetFirstAudioContent(offer->description());
1615 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1616 kStreamId1, "audio_track"));
1617
1618 const cricket::ContentInfo* video_content =
1619 cricket::GetFirstVideoContent(offer->description());
1620 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1621 kStreamId1, "video_track"));
1622
1623 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
1624
1625 // Check the encodings.
1626 ASSERT_THAT(audio_sender->GetParameters().encodings, SizeIs(1));
1627 EXPECT_THAT(audio_sender->GetParameters().encodings[0].active, Eq(false));
1628 ASSERT_THAT(video_sender->GetParameters().encodings, SizeIs(1));
1629 EXPECT_THAT(video_sender->GetParameters().encodings[0].active, Eq(true));
1630
1631 // Now try removing the tracks.
1632 EXPECT_TRUE(pc_->RemoveTrackOrError(audio_sender).ok());
1633 EXPECT_TRUE(pc_->RemoveTrackOrError(video_sender).ok());
1634}
1635
deadbeefe1f9d832016-01-14 15:35:42 -08001636// Test creating senders without a stream specified,
1637// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001638TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001639 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001640 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001641 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001642 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001643 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001644 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001645 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001646 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001647 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001648 EXPECT_EQ("audio_track", audio_sender->id());
1649 EXPECT_EQ(audio_track, audio_sender->track());
1650 EXPECT_EQ("video_track", video_sender->id());
1651 EXPECT_EQ(video_track, video_sender->track());
Florent Castelli15a38de2022-04-06 00:38:21 +02001652 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07001653 // If the ID is truly a random GUID, it should be infinitely unlikely they
1654 // will be the same.
1655 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1656 } else {
1657 // We allows creating tracks without stream ids under Unified Plan
1658 // semantics.
1659 EXPECT_EQ(0u, video_sender->stream_ids().size());
1660 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1661 }
deadbeefe1f9d832016-01-14 15:35:42 -08001662}
1663
Harald Alvestrand89061872018-01-02 14:08:34 +01001664// Test that we can call GetStats() after AddTrack but before connecting
1665// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001666TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001667 CreatePeerConnectionWithoutDtls();
1668 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001669 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001670 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001671 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001672 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1673 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001674 EXPECT_TRUE(DoGetStats(nullptr));
1675}
1676
Steve Anton36da6ff2018-02-16 16:04:20 -08001677TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001678 CreatePeerConnectionWithoutDtls();
1679 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001680 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001681 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001682 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001683 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001684 ASSERT_TRUE(audio_sender.ok());
1685 auto* audio_sender_proxy =
1686 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1687 audio_sender.value().get());
1688 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1689
Harald Alvestrandc72af932018-01-11 17:18:19 +01001690 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001691 ASSERT_TRUE(video_sender.ok());
1692 auto* video_sender_proxy =
1693 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1694 video_sender.value().get());
1695 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001696}
1697
Steve Anton36da6ff2018-02-16 16:04:20 -08001698// Don't run under Unified Plan since the stream API is not available.
1699TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001700 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001701 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001702 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001703 ASSERT_EQ(1u, senders.size());
1704 auto* sender_proxy =
1705 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1706 senders[0].get());
1707 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001708}
1709
Steve Anton36da6ff2018-02-16 16:04:20 -08001710TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001712 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 VerifyRemoteRtpHeaderExtensions();
1714}
1715
Steve Anton36da6ff2018-02-16 16:04:20 -08001716TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001718 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 CreateOfferAsLocalDescription();
1720 std::string offer;
1721 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1722 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001723 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724}
1725
Steve Anton36da6ff2018-02-16 16:04:20 -08001726TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001727 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001728 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729
1730 CreateOfferAsRemoteDescription();
1731 CreateAnswerAsLocalDescription();
1732
Seth Hampson845e8782018-03-02 11:34:10 -08001733 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734}
1735
Steve Anton36da6ff2018-02-16 16:04:20 -08001736TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001737 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001738 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739
1740 CreateOfferAsRemoteDescription();
1741 CreatePrAnswerAsLocalDescription();
1742 CreateAnswerAsLocalDescription();
1743
Seth Hampson845e8782018-03-02 11:34:10 -08001744 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
Steve Anton36da6ff2018-02-16 16:04:20 -08001747// Don't run under Unified Plan since the stream API is not available.
1748TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 InitiateCall();
1750 ASSERT_EQ(1u, pc_->remote_streams()->count());
1751 pc_->RemoveStream(pc_->local_streams()->at(0));
1752 CreateOfferReceiveAnswer();
1753 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001754 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 CreateOfferReceiveAnswer();
1756}
1757
1758// Tests that after negotiating an audio only call, the respondent can perform a
1759// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001760TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001761 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001762 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 CreateOfferAsRemoteDescription();
1764 CreateAnswerAsLocalDescription();
1765
1766 ASSERT_EQ(1u, pc_->remote_streams()->count());
1767 pc_->RemoveStream(pc_->local_streams()->at(0));
1768 CreateOfferReceiveAnswer();
1769 EXPECT_EQ(0u, pc_->remote_streams()->count());
1770}
1771
1772// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001773TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001774 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
Steve Antonf1c6db12017-10-13 11:13:35 -07001776 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001778 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001779 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001780 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001781 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782
1783 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001784 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001785 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001786 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787
Steve Antonf1c6db12017-10-13 11:13:35 -07001788 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001789 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790
Steve Antonf1c6db12017-10-13 11:13:35 -07001791 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792}
1793
deadbeefab9b2d12015-10-14 11:33:11 -07001794// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001796TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001797 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001799 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001800 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001801 EXPECT_TRUE(offer);
1802 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803
1804 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001805 AddAudioTrack("track_label", {kStreamId1});
1806 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807
1808 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001809 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810
1811 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001812 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001813 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814}
1815
1816// Test that we will get different SSRCs for each tracks in the offer and answer
1817// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001818TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001819 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001821 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1822 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823
1824 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001825 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001826 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 int audio_ssrc = 0;
1828 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001829 EXPECT_TRUE(
1830 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1831 EXPECT_TRUE(
1832 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 EXPECT_NE(audio_ssrc, video_ssrc);
1834
1835 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001836 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001837 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001838 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 audio_ssrc = 0;
1840 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001841 EXPECT_TRUE(
1842 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1843 EXPECT_TRUE(
1844 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 EXPECT_NE(audio_ssrc, video_ssrc);
1846}
1847
deadbeefeb459812015-12-15 19:24:43 -08001848// Test that it's possible to call AddTrack on a MediaStream after adding
1849// the stream to a PeerConnection.
1850// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001851// Don't run under Unified Plan since the stream API is not available.
1852TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001853 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001854 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001855 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001856 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1857
1858 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001859 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001860 CreateVideoTrack("video_label"));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001861 stream->AddTrack(video_track);
deadbeefeb459812015-12-15 19:24:43 -08001862
kwibergd1fe2812016-04-27 06:47:29 -07001863 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001864 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001865
1866 const cricket::MediaContentDescription* video_desc =
1867 cricket::GetFirstVideoContentDescription(offer->description());
1868 EXPECT_TRUE(video_desc != nullptr);
1869}
1870
1871// Test that it's possible to call RemoveTrack on a MediaStream after adding
1872// the stream to a PeerConnection.
1873// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001874// Don't run under Unified Plan since the stream API is not available.
1875TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001876 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001877 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001878 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001879 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1880
1881 // Remove the video track.
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00001882 stream->RemoveTrack(stream->GetVideoTracks()[0]);
deadbeefeb459812015-12-15 19:24:43 -08001883
kwibergd1fe2812016-04-27 06:47:29 -07001884 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001885 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001886
1887 const cricket::MediaContentDescription* video_desc =
1888 cricket::GetFirstVideoContentDescription(offer->description());
1889 EXPECT_TRUE(video_desc == nullptr);
1890}
1891
deadbeefbd7d8f72015-12-18 16:58:44 -08001892// Test creating a sender with a stream ID, and ensure the ID is populated
1893// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001894// Don't run under Unified Plan since the stream API is not available.
1895TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001896 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001897 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001898
kwibergd1fe2812016-04-27 06:47:29 -07001899 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001900 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001901
1902 const cricket::MediaContentDescription* video_desc =
1903 cricket::GetFirstVideoContentDescription(offer->description());
1904 ASSERT_TRUE(video_desc != nullptr);
1905 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001906 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001907}
1908
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001910TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001912 ASSERT_LT(0u, pc_->GetSenders().size());
1913 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001914 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001915 pc_->GetReceivers()[0]->track();
Niels Möllerafb246b2022-04-20 14:26:50 +02001916 EXPECT_TRUE(DoGetStats(remote_audio.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917
1918 // Remove the stream. Since we are sending to our selves the local
1919 // and the remote stream is the same.
Harald Alvestrand93dd7632022-01-19 12:28:45 +00001920 pc_->RemoveTrackOrError(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 // Do a re-negotiation.
1922 CreateOfferReceiveAnswer();
1923
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 // Test that we still can get statistics for the old track. Even if it is not
1925 // sent any longer.
Niels Möllerafb246b2022-04-20 14:26:50 +02001926 EXPECT_TRUE(DoGetStats(remote_audio.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927}
1928
1929// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001930TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001932 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1933 ASSERT_TRUE(video_receiver);
Niels Möllerafb246b2022-04-20 14:26:50 +02001934 EXPECT_TRUE(DoGetStats(video_receiver->track().get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935}
1936
1937// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001938TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001940 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
Niels Möllerafb246b2022-04-20 14:26:50 +02001941 pc_factory_->CreateAudioTrack("unknown track", nullptr));
1942 EXPECT_FALSE(DoGetStats(unknown_audio_track.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943}
1944
Steve Anton36da6ff2018-02-16 16:04:20 -08001945TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001946 CreatePeerConnectionWithoutDtls();
1947 EXPECT_TRUE(DoGetRTCStats());
1948 // Clearing stats cache is needed now, but should be temporary.
1949 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1950 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001951 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1952 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001953 EXPECT_TRUE(DoGetRTCStats());
1954 pc_->ClearStatsCache();
1955 CreateOfferReceiveAnswer();
1956 EXPECT_TRUE(DoGetRTCStats());
1957}
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959// This tests that a SCTP data channel is returned using different
1960// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001961TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001962 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001963 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964
1965 webrtc::DataChannelInit config;
Florent Castelli72424402022-04-06 03:45:10 +02001966 auto channel = pc_->CreateDataChannelOrError("1", &config);
1967 EXPECT_TRUE(channel.ok());
1968 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001969 EXPECT_TRUE(observer_.renegotiation_needed_);
1970 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971
1972 config.ordered = false;
Florent Castelli72424402022-04-06 03:45:10 +02001973 channel = pc_->CreateDataChannelOrError("2", &config);
1974 EXPECT_TRUE(channel.ok());
1975 EXPECT_TRUE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001976 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977
1978 config.ordered = true;
1979 config.maxRetransmits = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001980 channel = pc_->CreateDataChannelOrError("3", &config);
1981 EXPECT_TRUE(channel.ok());
1982 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001983 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001985 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 config.maxRetransmitTime = 0;
Florent Castelli72424402022-04-06 03:45:10 +02001987 channel = pc_->CreateDataChannelOrError("4", &config);
1988 EXPECT_TRUE(channel.ok());
1989 EXPECT_FALSE(channel.value()->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001990 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991}
1992
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001993// For backwards compatibility, we want people who "unset" maxRetransmits
1994// and maxRetransmitTime by setting them to -1 to get what they want.
1995TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1996 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001997 CreatePeerConnection(rtc_config);
1998
1999 webrtc::DataChannelInit config;
2000 config.maxRetransmitTime = -1;
2001 config.maxRetransmits = -1;
Florent Castelli72424402022-04-06 03:45:10 +02002002 auto channel = pc_->CreateDataChannelOrError("1", &config);
2003 EXPECT_TRUE(channel.ok());
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002004}
2005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006// This tests that no data channel is returned if both maxRetransmits and
2007// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002008TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002010 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002011 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012
2013 std::string label = "test";
2014 webrtc::DataChannelInit config;
2015 config.maxRetransmits = 0;
2016 config.maxRetransmitTime = 0;
2017
Florent Castelli72424402022-04-06 03:45:10 +02002018 auto channel = pc_->CreateDataChannelOrError(label, &config);
2019 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020}
2021
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022// The test verifies that creating a SCTP data channel with an id already in use
2023// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002024TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002026 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002027 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028
2029 webrtc::DataChannelInit config;
2030
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002031 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002032 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002033 auto channel = pc_->CreateDataChannelOrError("1", &config);
2034 EXPECT_TRUE(channel.ok());
2035 EXPECT_EQ(1, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036
Florent Castelli72424402022-04-06 03:45:10 +02002037 channel = pc_->CreateDataChannelOrError("x", &config);
2038 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039
2040 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002041 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002042 channel = pc_->CreateDataChannelOrError("max", &config);
2043 EXPECT_TRUE(channel.ok());
2044 EXPECT_EQ(config.id, channel.value()->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045
2046 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002047 config.negotiated = true;
Florent Castelli72424402022-04-06 03:45:10 +02002048 channel = pc_->CreateDataChannelOrError("x", &config);
2049 EXPECT_FALSE(channel.ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050}
2051
deadbeefab9b2d12015-10-14 11:33:11 -07002052// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002053TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002054 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002055 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002056
2057 std::string label = "test";
Florent Castelli72424402022-04-06 03:45:10 +02002058 auto channel = pc_->CreateDataChannelOrError(label, nullptr);
2059 EXPECT_TRUE(channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002060
Florent Castelli72424402022-04-06 03:45:10 +02002061 auto dup_channel = pc_->CreateDataChannelOrError(label, nullptr);
2062 EXPECT_TRUE(dup_channel.ok());
deadbeefab9b2d12015-10-14 11:33:11 -07002063}
2064
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002065#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002066// This tests that SCTP data channels can be rejected in an answer.
2067TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2068#else
2069TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2070#endif
2071{
Niels Möllerf06f9232018-08-07 12:32:18 +02002072 RTCConfiguration rtc_config;
2073 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002074
Florent Castelli72424402022-04-06 03:45:10 +02002075 auto offer_channel = pc_->CreateDataChannelOrError("offer_channel", NULL);
Zhi Huang644fde42018-04-02 19:16:26 -07002076
2077 CreateOfferAsLocalDescription();
2078
2079 // Create an answer where the m-line for data channels are rejected.
2080 std::string sdp;
2081 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2082 std::unique_ptr<SessionDescriptionInterface> answer(
2083 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2084 ASSERT_TRUE(answer);
2085 cricket::ContentInfo* data_info =
2086 cricket::GetFirstDataContent(answer->description());
2087 data_info->rejected = true;
2088
2089 DoSetRemoteDescription(std::move(answer));
Florent Castelli72424402022-04-06 03:45:10 +02002090 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel.value()->state());
Zhi Huang644fde42018-04-02 19:16:26 -07002091}
2092
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093// Test that we can create a session description from an SDP string from
2094// FireFox, use it as a remote session description, generate an answer and use
2095// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002096TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002097 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002098 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002099 AddAudioTrack("audio_label");
2100 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002101 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002102 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002103 webrtc::kFireFoxSdpOffer, nullptr));
2104 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 CreateAnswerAsLocalDescription();
Niels Möllerafb246b2022-04-20 14:26:50 +02002106 ASSERT_TRUE(pc_->local_description() != nullptr);
2107 ASSERT_TRUE(pc_->remote_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108
2109 const cricket::ContentInfo* content =
2110 cricket::GetFirstAudioContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002111 ASSERT_TRUE(content != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 EXPECT_FALSE(content->rejected);
2113
2114 content =
2115 cricket::GetFirstVideoContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002116 ASSERT_TRUE(content != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002118#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 content =
2120 cricket::GetFirstDataContent(pc_->local_description()->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002121 ASSERT_TRUE(content != nullptr);
Zhi Huange830e682018-03-30 10:48:35 -07002122 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002123#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124}
2125
Harald Alvestrand0d018412021-11-04 13:52:31 +00002126// Test that fallback from DTLS to SDES is not supported.
2127// The fallback was previously supported but was removed to simplify the code
2128// and because it's non-standard.
2129TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002130 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002131 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002132 // Wait for fake certificate to be generated. Previously, this is what caused
2133 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002134 AddAudioTrack("audio_label");
2135 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002136 ASSERT_NE(nullptr, fake_certificate_generator_);
2137 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2138 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002139 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002140 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2141 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002142 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002143}
2144
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145// Test that we can create an audio only offer and receive an answer with a
2146// limited set of audio codecs and receive an updated offer with more audio
2147// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002148TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002149 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002150 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 CreateOfferAsLocalDescription();
2152
Florent Castelli15a38de2022-04-06 00:38:21 +02002153 const char* answer_sdp = (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
2154 ? webrtc::kAudioSdpPlanB
2155 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002156 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002157 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002158 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159
Steve Anton36da6ff2018-02-16 16:04:20 -08002160 const char* reoffer_sdp =
Florent Castelli15a38de2022-04-06 00:38:21 +02002161 (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED
Steve Anton36da6ff2018-02-16 16:04:20 -08002162 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2163 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002164 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002165 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002166 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 CreateAnswerAsLocalDescription();
2168}
2169
deadbeefc80741f2015-10-22 13:14:45 -07002170// Test that if we're receiving (but not sending) a track, subsequent offers
2171// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002172TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002173 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002174 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002175 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002176 CreateAnswerAsLocalDescription();
2177
2178 // At this point we should be receiving stream 1, but not sending anything.
2179 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002181 DoCreateOffer(&offer, nullptr);
2182
2183 const cricket::ContentInfo* video_content =
2184 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002185 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2186 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002187
2188 const cricket::ContentInfo* audio_content =
2189 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002190 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2191 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002192}
2193
2194// Test that if we're receiving (but not sending) a track, and the
2195// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2196// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002197TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002198 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002199 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002200 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002201 CreateAnswerAsLocalDescription();
2202
2203 // At this point we should be receiving stream 1, but not sending anything.
2204 // A new offer would be recvonly, but we'll set the "no receive" constraints
2205 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002206 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002207 RTCOfferAnswerOptions options;
2208 options.offer_to_receive_audio = 0;
2209 options.offer_to_receive_video = 0;
2210 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002211
2212 const cricket::ContentInfo* video_content =
2213 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002214 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2215 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002216
2217 const cricket::ContentInfo* audio_content =
2218 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002219 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2220 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002221}
2222
deadbeef653b8e02015-11-11 12:55:10 -08002223// Test that we can use SetConfiguration to change the ICE servers of the
2224// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002225TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002226 CreatePeerConnection();
2227
Steve Anton36da6ff2018-02-16 16:04:20 -08002228 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002229 PeerConnectionInterface::IceServer server;
2230 server.uri = "stun:test_hostname";
2231 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002232 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002233
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002234 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2235 EXPECT_EQ("test_hostname",
2236 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002237}
2238
Steve Anton36da6ff2018-02-16 16:04:20 -08002239TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002240 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002241 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002242 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002243 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002244 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2245}
2246
Steve Anton36da6ff2018-02-16 16:04:20 -08002247TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002248 PeerConnectionInterface::RTCConfiguration config;
2249 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002250 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002251 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002252 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002253
2254 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002255 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002256 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2257 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002258}
2259
skvladd1f5fda2017-02-03 16:54:05 -08002260// Test that the ice check interval can be changed. This does not verify that
2261// the setting makes it all the way to P2PTransportChannel, as that would
2262// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002263TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002264 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002265 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002266 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002267 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002268 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002269 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002270 config = pc_->GetConfiguration();
2271 EXPECT_EQ(config.ice_check_min_interval, 100);
2272}
2273
2274TEST_P(PeerConnectionInterfaceTest,
2275 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2276 PeerConnectionInterface::RTCConfiguration config;
2277 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2278 CreatePeerConnection(config);
2279 config = pc_->GetConfiguration();
2280 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2281
2282 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002283 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002284 config = pc_->GetConfiguration();
2285 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002286}
2287
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002288// Test that when SetConfiguration changes both the pool size and other
2289// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002290TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002291 SetConfigurationCreatesPooledSessionCorrectly) {
2292 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002293 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002294 config.ice_candidate_pool_size = 1;
2295 PeerConnectionInterface::IceServer server;
2296 server.uri = kStunAddressOnly;
2297 config.servers.push_back(server);
2298 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002299 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002300
2301 const cricket::FakePortAllocatorSession* session =
2302 static_cast<const cricket::FakePortAllocatorSession*>(
2303 port_allocator_->GetPooledSession());
2304 ASSERT_NE(nullptr, session);
2305 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002306}
2307
deadbeef293e9262017-01-11 12:28:30 -08002308// Test that after SetLocalDescription, changing the pool size is not allowed,
2309// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002310TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002311 CantChangePoolSizeAfterSetLocalDescription) {
2312 CreatePeerConnection();
2313 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002314 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002315 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002316 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002317
2318 // Set remote offer; can still change pool size at this point.
2319 CreateOfferAsRemoteDescription();
2320 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002321 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002322
2323 // Set local answer; now it's too late.
2324 CreateAnswerAsLocalDescription();
2325 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002326 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002327 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2328}
2329
deadbeef42a42632017-03-10 15:18:00 -08002330// Test that after setting an answer, extra pooled sessions are discarded. The
2331// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002332TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002333 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2334 CreatePeerConnection();
2335
2336 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002337 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002338 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002339 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002340
2341 // Do offer/answer.
2342 CreateOfferAsRemoteDescription();
2343 CreateAnswerAsLocalDescription();
2344
2345 // Expect no pooled sessions to be left.
2346 const cricket::PortAllocatorSession* session =
2347 port_allocator_->GetPooledSession();
2348 EXPECT_EQ(nullptr, session);
2349}
2350
2351// After Close is called, pooled candidates should be discarded so as to not
2352// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002353TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002354 CreatePeerConnection();
2355
Steve Anton36da6ff2018-02-16 16:04:20 -08002356 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002357 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002358 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002359 pc_->Close();
2360
2361 // Expect no pooled sessions to be left.
2362 const cricket::PortAllocatorSession* session =
2363 port_allocator_->GetPooledSession();
2364 EXPECT_EQ(nullptr, session);
2365}
2366
deadbeef293e9262017-01-11 12:28:30 -08002367// Test that SetConfiguration returns an invalid modification error if
2368// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002369TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002370 SetConfigurationReturnsInvalidModificationError) {
2371 PeerConnectionInterface::RTCConfiguration config;
2372 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2373 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2374 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002375 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002376
Steve Anton36da6ff2018-02-16 16:04:20 -08002377 PeerConnectionInterface::RTCConfiguration modified_config =
2378 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002379 modified_config.bundle_policy =
2380 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002381 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002382 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2383
Steve Anton36da6ff2018-02-16 16:04:20 -08002384 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002385 modified_config.rtcp_mux_policy =
2386 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002387 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002388 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2389
Steve Anton36da6ff2018-02-16 16:04:20 -08002390 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002391 modified_config.continual_gathering_policy =
2392 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002393 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002394 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2395}
2396
2397// Test that SetConfiguration returns a range error if the candidate pool size
2398// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002399TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002400 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2401 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002402 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002403 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002404
2405 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002406 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002407 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2408
2409 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002410 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002411 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2412}
2413
2414// Test that SetConfiguration returns a syntax error if parsing an ICE server
2415// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002416TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002417 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2418 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002419 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002420 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002421
2422 PeerConnectionInterface::IceServer bad_server;
2423 bad_server.uri = "stunn:www.example.com";
2424 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002425 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002426 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2427}
2428
2429// Test that SetConfiguration returns an invalid parameter error if a TURN
2430// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002431TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002432 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2433 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002434 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002435 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002436
2437 PeerConnectionInterface::IceServer bad_server;
2438 bad_server.uri = "turn:www.example.com";
2439 // Missing password.
2440 bad_server.username = "foo";
2441 config.servers.push_back(bad_server);
2442 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002443 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2444 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002445}
2446
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447// Test that PeerConnection::Close changes the states to closed and all remote
2448// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002449TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450 // Initialize a PeerConnection and negotiate local and remote session
2451 // description.
2452 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002453
2454 // With Plan B, verify the stream count. The analog with Unified Plan is the
2455 // RtpTransceiver count.
Florent Castelli15a38de2022-04-06 00:38:21 +02002456 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002457 ASSERT_EQ(1u, pc_->local_streams()->count());
2458 ASSERT_EQ(1u, pc_->remote_streams()->count());
2459 } else {
2460 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2461 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462
2463 pc_->Close();
2464
2465 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2466 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2467 pc_->ice_connection_state());
2468 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2469 pc_->ice_gathering_state());
2470
Florent Castelli15a38de2022-04-06 00:38:21 +02002471 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002472 EXPECT_EQ(1u, pc_->local_streams()->count());
2473 EXPECT_EQ(1u, pc_->remote_streams()->count());
2474 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002475 // Verify that the RtpTransceivers are still returned.
2476 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002477 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478
Steve Anton36da6ff2018-02-16 16:04:20 -08002479 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002480 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Florent Castelli15a38de2022-04-06 00:38:21 +02002481 if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002482 ASSERT_TRUE(audio_receiver);
2483 ASSERT_TRUE(video_receiver);
2484 // Track state may be updated asynchronously.
2485 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2486 audio_receiver->track()->state(), kTimeout);
2487 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2488 video_receiver->track()->state(), kTimeout);
2489 } else {
2490 ASSERT_FALSE(audio_receiver);
2491 ASSERT_FALSE(video_receiver);
2492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493}
2494
2495// Test that PeerConnection methods fails gracefully after
2496// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002497// Don't run under Unified Plan since the stream API is not available.
2498TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002499 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002500 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501 CreateOfferAsRemoteDescription();
2502 CreateAnswerAsLocalDescription();
2503
2504 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002505 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2506 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507
2508 pc_->Close();
2509
Niels Möllerafb246b2022-04-20 14:26:50 +02002510 pc_->RemoveStream(local_stream.get());
2511 EXPECT_FALSE(pc_->AddStream(local_stream.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512
Florent Castelli72424402022-04-06 03:45:10 +02002513 EXPECT_FALSE(pc_->CreateDataChannelOrError("test", NULL).ok());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514
Niels Möllerafb246b2022-04-20 14:26:50 +02002515 EXPECT_TRUE(pc_->local_description() != nullptr);
2516 EXPECT_TRUE(pc_->remote_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517
kwibergd1fe2812016-04-27 06:47:29 -07002518 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002519 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002520 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002521 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522
2523 std::string sdp;
2524 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002525 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002526 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002527 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002528
2529 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002530 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002531 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002532 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533}
2534
2535// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002536TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537 InitiateCall();
2538 pc_->Close();
Niels Möllerafb246b2022-04-20 14:26:50 +02002539 DoGetStats(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002540}
deadbeefab9b2d12015-10-14 11:33:11 -07002541
2542// NOTE: The series of tests below come from what used to be
2543// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2544// setting a remote or local description has the expected effects.
2545
2546// This test verifies that the remote MediaStreams corresponding to a received
2547// SDP string is created. In this test the two separate MediaStreams are
2548// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002549TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002550 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002551 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002552 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002553
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002554 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002555 EXPECT_TRUE(
2556 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2557 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2558 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2559
2560 // Create a session description based on another SDP with another
2561 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002562 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002563
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002564 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002565 EXPECT_TRUE(
2566 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2567}
2568
2569// This test verifies that when remote tracks are added/removed from SDP, the
2570// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002571// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2572// specific behavior.
2573TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002574 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002575 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002576 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002577 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002578 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002579 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002580 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002581 reference_collection_.get()));
deadbeefab9b2d12015-10-14 11:33:11 -07002582
2583 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002584 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002585 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002586 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002587 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002588 reference_collection_.get()));
zhihuang9763d562016-08-05 11:14:50 -07002589 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002590 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2591 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002592 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002593 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2594 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002595
2596 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002597 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002598 CreateSessionDescriptionAndReference(1, 1);
Niels Möllerafb246b2022-04-20 14:26:50 +02002599 MockTrackObserver audio_track_observer(audio_track2.get());
2600 MockTrackObserver video_track_observer(video_track2.get());
perkjd61bf802016-03-24 03:16:19 -07002601
2602 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2603 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002604 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002605 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
Niels Möllerafb246b2022-04-20 14:26:50 +02002606 reference_collection_.get()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002607 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002608 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002609 audio_track2->state(), kTimeout);
2610 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2611 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002612}
2613
2614// This tests that remote tracks are ended if a local session description is set
2615// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002616TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002617 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002618 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002619 // First create and set a remote offer, then reject its video content in our
2620 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002621 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2622 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2623 ASSERT_TRUE(audio_receiver);
2624 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2625 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002626
Steve Anton36da6ff2018-02-16 16:04:20 -08002627 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2628 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002629 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002630 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2631 video_receiver->track();
2632 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002633
kwibergd1fe2812016-04-27 06:47:29 -07002634 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002635 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002636 cricket::ContentInfo* video_info =
2637 local_answer->description()->GetContentByName("video");
2638 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002639 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002640 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2641 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002642
2643 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002644 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002645 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002646 video_info = local_offer->description()->GetContentByName("video");
2647 ASSERT_TRUE(video_info != nullptr);
2648 video_info->rejected = true;
2649 cricket::ContentInfo* audio_info =
2650 local_offer->description()->GetContentByName("audio");
2651 ASSERT_TRUE(audio_info != nullptr);
2652 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002653 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002654 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002655 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2656 kTimeout);
2657 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2658 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002659}
2660
2661// This tests that we won't crash if the remote track has been removed outside
2662// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002663// Don't run under Unified Plan since the stream API is not available.
2664TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002665 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002666 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002667 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002668 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00002669 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2670 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002671
kwibergd1fe2812016-04-27 06:47:29 -07002672 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002673 webrtc::CreateSessionDescription(SdpType::kAnswer,
2674 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002675 cricket::ContentInfo* video_info =
2676 local_answer->description()->GetContentByName("video");
2677 video_info->rejected = true;
2678 cricket::ContentInfo* audio_info =
2679 local_answer->description()->GetContentByName("audio");
2680 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002681 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002682
2683 // No crash is a pass.
2684}
2685
deadbeef5e97fb52015-10-15 12:49:08 -07002686// This tests that if a recvonly remote description is set, no remote streams
2687// will be created, even if the description contains SSRCs/MSIDs.
2688// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002689TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002690 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002691 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002692
Steve Anton36da6ff2018-02-16 16:04:20 -08002693 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002694 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002695 CreateAndSetRemoteOffer(recvonly_offer);
2696
2697 EXPECT_EQ(0u, observer_.remote_streams()->count());
2698}
2699
deadbeefab9b2d12015-10-14 11:33:11 -07002700// This tests that a default MediaStream is created if a remote session
2701// description doesn't contain any streams and no MSID support.
2702// It also tests that the default stream is updated if a video m-line is added
2703// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002704// Don't run under Unified Plan since this behavior is Plan B specific.
2705TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002706 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002707 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002708 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2709
2710 ASSERT_EQ(1u, observer_.remote_streams()->count());
2711 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2712
2713 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2714 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002715 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002716
2717 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2718 ASSERT_EQ(1u, observer_.remote_streams()->count());
2719 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2720 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002721 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2722 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002723 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2724 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002725 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2726 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002727}
2728
2729// This tests that a default MediaStream is created if a remote session
2730// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002731// Don't run under Unified Plan since this behavior is Plan B specific.
2732TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002733 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002734 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002735 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002736 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2737
2738 ASSERT_EQ(1u, observer_.remote_streams()->count());
2739 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2740
2741 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2742 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002743 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002744}
2745
2746// This tests that it won't crash when PeerConnection tries to remove
2747// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002748// Don't run under Unified Plan since this behavior is Plan B specific.
2749TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002750 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002751 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002752 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002753 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00002754 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2755 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002756
2757 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2758
2759 // No crash is a pass.
2760}
2761
2762// This tests that a default MediaStream is created if the remote session
2763// description doesn't contain any streams and don't contain an indication if
2764// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002765// Don't run under Unified Plan since this behavior is Plan B specific.
2766TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002767 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002768 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002769 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002770 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2771
2772 ASSERT_EQ(1u, observer_.remote_streams()->count());
2773 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2774 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2775 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2776}
2777
2778// This tests that a default MediaStream is not created if the remote session
2779// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002780// Don't run under Unified Plan since this behavior is Plan B specific.
2781TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002782 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002783 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002784 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2785 EXPECT_EQ(0u, observer_.remote_streams()->count());
2786}
2787
deadbeefbda7e0b2015-12-08 17:13:40 -08002788// This tests that when setting a new description, the old default tracks are
2789// not destroyed and recreated.
2790// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002791// Don't run under Unified Plan since this behavior is Plan B specific.
2792TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002793 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002794 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002795 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002796 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2797
2798 ASSERT_EQ(1u, observer_.remote_streams()->count());
2799 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2800 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2801
2802 // Set the track to "disabled", then set a new description and ensure the
2803 // track is still disabled, which ensures it hasn't been recreated.
2804 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2805 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2806 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2807 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2808}
2809
deadbeefab9b2d12015-10-14 11:33:11 -07002810// This tests that a default MediaStream is not created if a remote session
2811// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002812// Don't run under Unified Plan since this behavior is Plan B specific.
2813TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002814 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002815 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002816 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002817 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002818 EXPECT_TRUE(
2819 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2820
2821 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2822 EXPECT_EQ(0u, observer_.remote_streams()->count());
2823}
2824
Seth Hampson5897a6e2018-04-03 11:16:33 -07002825// This tests that a default MediaStream is created if a remote SDP comes from
2826// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2827TEST_F(PeerConnectionInterfaceTestPlanB,
2828 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002829 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002830 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002831 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2832 // Add a=msid lines to simulate a Unified Plan endpoint that only
2833 // signals stream IDs with a=msid lines.
2834 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2835
2836 CreateAndSetRemoteOffer(sdp_string);
2837
2838 ASSERT_EQ(1u, observer_.remote_streams()->count());
2839 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2840 EXPECT_EQ("default", remote_stream->id());
2841 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2842}
2843
Seth Hampson5b4f0752018-04-02 16:31:36 -07002844// This tests that when a Plan B endpoint receives an SDP that signals no media
2845// stream IDs indicated by the special character "-" in the a=msid line, that
2846// a default stream ID will be used for the MediaStream ID. This can occur
2847// when a Unified Plan endpoint signals no media stream IDs, but signals both
2848// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2849TEST_F(PeerConnectionInterfaceTestPlanB,
2850 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002851 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002852 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002853 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2854 // the sender's stream ID will be interpreted as no stream IDs.
2855 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2856 sdp_string.append("a=msid:- audiotrack0\n");
2857
2858 CreateAndSetRemoteOffer(sdp_string);
2859
2860 ASSERT_EQ(1u, observer_.remote_streams()->count());
2861 // Because SSRCs are signaled the track ID will be what was signaled in the
2862 // a=msid line.
2863 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2864 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2865 EXPECT_EQ("default", remote_stream->id());
2866 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002867
2868 // Previously a bug ocurred when setting the remote description a second time.
2869 // This is because we checked equality of the remote StreamParams stream ID
2870 // (empty), and the previously set stream ID for the remote sender
2871 // ("default"). This cause a track to be removed, then added, when really
2872 // nothing should occur because it is the same track.
2873 CreateAndSetRemoteOffer(sdp_string);
2874 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2875 EXPECT_EQ(1u, observer_.add_track_events_.size());
2876 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2877 remote_stream = observer_.remote_streams()->at(0);
2878 EXPECT_EQ("default", remote_stream->id());
2879 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002880}
2881
deadbeefab9b2d12015-10-14 11:33:11 -07002882// This tests that an RtpSender is created when the local description is set
2883// after adding a local stream.
2884// TODO(deadbeef): This test and the one below it need to be updated when
2885// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002886// Don't run under Unified Plan since this behavior is Plan B specific.
2887TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002888 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002889 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002890
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002891 // Create an offer with 1 stream with 2 tracks of each type.
2892 rtc::scoped_refptr<StreamCollection> stream_collection =
2893 CreateStreamCollection(1, 2);
2894 pc_->AddStream(stream_collection->at(0));
2895 std::unique_ptr<SessionDescriptionInterface> offer;
2896 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002897 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002898
deadbeefab9b2d12015-10-14 11:33:11 -07002899 auto senders = pc_->GetSenders();
2900 EXPECT_EQ(4u, senders.size());
2901 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2902 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2903 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2904 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2905
2906 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002907 pc_->RemoveStream(stream_collection->at(0));
2908 stream_collection = CreateStreamCollection(1, 1);
2909 pc_->AddStream(stream_collection->at(0));
2910 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002911 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002912
deadbeefab9b2d12015-10-14 11:33:11 -07002913 senders = pc_->GetSenders();
2914 EXPECT_EQ(2u, senders.size());
2915 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2916 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2917 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2918 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2919}
2920
2921// This tests that an RtpSender is created when the local description is set
2922// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002923// Don't run under Unified Plan since this behavior is Plan B specific.
2924TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002925 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002926 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002927 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002928
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002929 rtc::scoped_refptr<StreamCollection> stream_collection =
2930 CreateStreamCollection(1, 2);
2931 // Add a stream to create the offer, but remove it afterwards.
2932 pc_->AddStream(stream_collection->at(0));
2933 std::unique_ptr<SessionDescriptionInterface> offer;
2934 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2935 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002936
Steve Antondb45ca82017-09-11 18:27:34 -07002937 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002938 auto senders = pc_->GetSenders();
2939 EXPECT_EQ(0u, senders.size());
2940
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002941 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002942 senders = pc_->GetSenders();
2943 EXPECT_EQ(4u, senders.size());
2944 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2945 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2946 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2947 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2948}
2949
2950// This tests that the expected behavior occurs if the SSRC on a local track is
2951// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002952TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002953 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002954 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002955 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002956
Steve Anton36da6ff2018-02-16 16:04:20 -08002957 AddAudioTrack(kAudioTracks[0]);
2958 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002959 std::unique_ptr<SessionDescriptionInterface> offer;
2960 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2961 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002962 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2963 webrtc::CreateSessionDescription(
2964 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002965 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002966 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002967
deadbeefab9b2d12015-10-14 11:33:11 -07002968 auto senders = pc_->GetSenders();
2969 EXPECT_EQ(2u, senders.size());
2970 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2971 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2972
2973 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002974 cricket::MediaContentDescription* desc =
2975 cricket::GetFirstAudioContentDescription(modified_offer->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002976 ASSERT_TRUE(desc != nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002977 for (StreamParams& stream : desc->mutable_streams()) {
2978 for (unsigned int& ssrc : stream.ssrcs) {
2979 ++ssrc;
2980 }
2981 }
deadbeefab9b2d12015-10-14 11:33:11 -07002982
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002983 desc =
2984 cricket::GetFirstVideoContentDescription(modified_offer->description());
Niels Möllerafb246b2022-04-20 14:26:50 +02002985 ASSERT_TRUE(desc != nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986 for (StreamParams& stream : desc->mutable_streams()) {
2987 for (unsigned int& ssrc : stream.ssrcs) {
2988 ++ssrc;
2989 }
2990 }
2991
Steve Antondb45ca82017-09-11 18:27:34 -07002992 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002993 senders = pc_->GetSenders();
2994 EXPECT_EQ(2u, senders.size());
2995 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2996 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2997 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2998 // changed.
2999}
3000
3001// This tests that the expected behavior occurs if a new session description is
3002// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003003// Don't run under Unified Plan since the stream API is not available.
3004TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003005 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003006 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003007 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003008
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003009 rtc::scoped_refptr<StreamCollection> stream_collection =
3010 CreateStreamCollection(2, 1);
3011 pc_->AddStream(stream_collection->at(0));
3012 std::unique_ptr<SessionDescriptionInterface> offer;
3013 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003014 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003015
deadbeefab9b2d12015-10-14 11:33:11 -07003016 auto senders = pc_->GetSenders();
3017 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003018 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3019 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003020
3021 // Add a new MediaStream but with the same tracks as in the first stream.
3022 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3023 webrtc::MediaStream::Create(kStreams[1]));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003024 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3025 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
Niels Möllerafb246b2022-04-20 14:26:50 +02003026 pc_->AddStream(stream_1.get());
deadbeefab9b2d12015-10-14 11:33:11 -07003027
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003028 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003029 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003030
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003031 auto new_senders = pc_->GetSenders();
3032 // Should be the same senders as before, but with updated stream id.
3033 // Note that this behavior is subject to change in the future.
3034 // We may decide the PC should ignore existing tracks in AddStream.
3035 EXPECT_EQ(senders, new_senders);
3036 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3037 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003038}
3039
zhihuang81c3a032016-11-17 12:06:24 -08003040// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003041TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003042 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003043 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003044 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3045 EXPECT_EQ(observer_.num_added_tracks_, 1);
3046 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3047
3048 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003049 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003050 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003051 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3052}
3053
deadbeefd1a38b52016-12-10 13:15:33 -08003054// Test that when SetConfiguration is called and the configuration is
3055// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003056TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003057 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003058 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003059 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003060 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003061 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003062 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3063 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003064
3065 // Do initial offer/answer so there's something to restart.
3066 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003067 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003068
3069 // Grab the ufrags.
3070 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3071
3072 // Change ICE policy, which should trigger an ICE restart on the next offer.
3073 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003074 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003075 CreateOfferAsLocalDescription();
3076
3077 // Grab the new ufrags.
3078 std::vector<std::string> subsequent_ufrags =
3079 GetUfrags(pc_->local_description());
3080
3081 // Sanity check.
3082 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3083 // Check that each ufrag is different.
3084 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3085 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3086 }
3087}
3088
3089// Test that when SetConfiguration is called and the configuration *isn't*
3090// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003091TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003092 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003093 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003094 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003095 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003096 config = pc_->GetConfiguration();
3097 AddAudioTrack(kAudioTracks[0]);
3098 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003099
3100 // Do initial offer/answer so there's something to restart.
3101 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003102 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003103
3104 // Grab the ufrags.
3105 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3106
3107 // Call SetConfiguration with a config identical to what the PC was
3108 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003109 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003110 CreateOfferAsLocalDescription();
3111
3112 // Grab the new ufrags.
3113 std::vector<std::string> subsequent_ufrags =
3114 GetUfrags(pc_->local_description());
3115
3116 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3117}
3118
3119// Test for a weird corner case scenario:
3120// 1. Audio/video session established.
3121// 2. SetConfiguration changes ICE config; ICE restart needed.
3122// 3. ICE restart initiated by remote peer, but only for one m= section.
3123// 4. Next createOffer should initiate an ICE restart, but only for the other
3124// m= section; it would be pointless to do an ICE restart for the m= section
3125// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003126TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003127 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003128 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003129 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003130 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003131 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003132 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3133 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003134
3135 // Do initial offer/answer so there's something to restart.
3136 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003137 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003138
3139 // Change ICE policy, which should set the "needs-ice-restart" flag.
3140 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003141 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003142
3143 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003144 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003145 webrtc::CreateSessionDescription(SdpType::kOffer,
3146 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003147 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003148 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3149 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003150 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003151 CreateAnswerAsLocalDescription();
3152
3153 // Grab the ufrags.
3154 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003155 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003156
3157 // Create offer and grab the new ufrags.
3158 CreateOfferAsLocalDescription();
3159 std::vector<std::string> subsequent_ufrags =
3160 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003161 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003162
3163 // Ensure that only the ufrag for the second m= section changed.
3164 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3165 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3166}
3167
deadbeeffe4a8a42016-12-20 17:56:17 -08003168// Tests that the methods to return current/pending descriptions work as
3169// expected at different points in the offer/answer exchange. This test does
3170// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003171TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003172 // This disables DTLS so we can apply an answer to ourselves.
3173 CreatePeerConnection();
3174
3175 // Create initial local offer and get SDP (which will also be used as
3176 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003177 std::unique_ptr<SessionDescriptionInterface> local_offer;
3178 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003179 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003180 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003181
3182 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003183 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3184 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3185 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003186 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3187 EXPECT_EQ(nullptr, pc_->current_local_description());
3188 EXPECT_EQ(nullptr, pc_->current_remote_description());
3189
3190 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003191 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003192 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003193 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3194 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3195 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3196 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003197 EXPECT_EQ(nullptr, pc_->current_local_description());
3198 EXPECT_EQ(nullptr, pc_->current_remote_description());
3199
3200 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003201 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003202 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003203 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3204 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003205 EXPECT_EQ(nullptr, pc_->pending_local_description());
3206 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003207 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3208 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003209
3210 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003211 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003212 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003213 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3214 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3215 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003216 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003217 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3218 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003219
3220 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003221 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003222 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003223 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3224 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3225 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3226 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3227 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3228 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003229
3230 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003231 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003232 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003233 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3234 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003235 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3236 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003237 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3238 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003239}
3240
zhihuang77985012017-02-07 15:45:16 -08003241// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3242// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003243// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003244// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003245TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003246 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3247 CreatePeerConnection();
3248 // The RtcEventLog will be reset when the PeerConnection is closed.
3249 pc_->Close();
3250
Niels Möllerdec9f742019-06-03 15:25:20 +02003251 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003252 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003253 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003254 pc_->StopRtcEventLog();
3255}
3256
deadbeef30952b42017-04-21 02:41:29 -07003257// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003258TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003259 CreatePeerConnection();
3260
3261 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003262 RTCOfferAnswerOptions options;
3263 options.offer_to_receive_audio = 1;
3264 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003265 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003266 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003267 cricket::SessionDescription* desc = offer->description();
3268 ASSERT_EQ(2u, desc->transport_infos().size());
3269 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3270 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3271
3272 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003273 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003274 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003275 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3276 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003277 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003278 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003279 desc = answer->description();
3280 ASSERT_EQ(2u, desc->transport_infos().size());
3281 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3282 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3283}
3284
deadbeef1dcb1642017-03-29 21:08:16 -07003285// Test that ICE renomination isn't offered if it's not enabled in the PC's
3286// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003287TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003288 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003289 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003290 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003291 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003292 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003293
3294 std::unique_ptr<SessionDescriptionInterface> offer;
3295 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3296 cricket::SessionDescription* desc = offer->description();
3297 EXPECT_EQ(1u, desc->transport_infos().size());
3298 EXPECT_FALSE(
3299 desc->transport_infos()[0].description.GetIceParameters().renomination);
3300}
3301
3302// Test that the ICE renomination option is present in generated offers/answers
3303// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003304TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003305 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003306 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003307 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003308 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003309 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003310
3311 std::unique_ptr<SessionDescriptionInterface> offer;
3312 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3313 cricket::SessionDescription* desc = offer->description();
3314 EXPECT_EQ(1u, desc->transport_infos().size());
3315 EXPECT_TRUE(
3316 desc->transport_infos()[0].description.GetIceParameters().renomination);
3317
3318 // Set the offer as a remote description, then create an answer and ensure it
3319 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003320 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003321 std::unique_ptr<SessionDescriptionInterface> answer;
3322 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3323 desc = answer->description();
3324 EXPECT_EQ(1u, desc->transport_infos().size());
3325 EXPECT_TRUE(
3326 desc->transport_infos()[0].description.GetIceParameters().renomination);
3327}
3328
3329// Test that if CreateOffer is called with the deprecated "offer to receive
3330// audio/video" constraints, they're processed and result in an offer with
3331// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003332TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003333 CreatePeerConnection();
3334
Niels Möllerf06f9232018-08-07 12:32:18 +02003335 RTCOfferAnswerOptions options;
3336 options.offer_to_receive_audio = 1;
3337 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003338 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003339 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003340
3341 cricket::SessionDescription* desc = offer->description();
3342 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3343 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3344 ASSERT_NE(nullptr, audio);
3345 ASSERT_NE(nullptr, video);
3346 EXPECT_FALSE(audio->rejected);
3347 EXPECT_FALSE(video->rejected);
3348}
3349
3350// Test that if CreateAnswer is called with the deprecated "offer to receive
3351// audio/video" constraints, they're processed and can be used to reject an
3352// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003353// Don't run under Unified Plan since this behavior is not supported.
3354TEST_F(PeerConnectionInterfaceTestPlanB,
3355 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003356 CreatePeerConnection();
3357
3358 // First, create an offer with audio/video and apply it as a remote
3359 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003360 RTCOfferAnswerOptions options;
3361 options.offer_to_receive_audio = 1;
3362 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003363 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003364 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003365 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003366
3367 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003368 options.offer_to_receive_audio = 0;
3369 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003370 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003371 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003372
3373 cricket::SessionDescription* desc = answer->description();
3374 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3375 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3376 ASSERT_NE(nullptr, audio);
3377 ASSERT_NE(nullptr, video);
3378 EXPECT_TRUE(audio->rejected);
3379 EXPECT_TRUE(video->rejected);
3380}
3381
deadbeef1dcb1642017-03-29 21:08:16 -07003382// Test that negotiation can succeed with a data channel only, and with the max
3383// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003384#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003385TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3386#else
3387TEST_P(PeerConnectionInterfaceTest,
3388 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003389#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003390 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003391 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003392 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003393 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003394
3395 // First, create an offer with only a data channel and apply it as a remote
3396 // description.
Florent Castelli72424402022-04-06 03:45:10 +02003397 pc_->CreateDataChannelOrError("test", nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003398 std::unique_ptr<SessionDescriptionInterface> offer;
3399 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003400 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003401
3402 // Create and set answer as well.
3403 std::unique_ptr<SessionDescriptionInterface> answer;
3404 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003405 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003406}
3407
Steve Anton36da6ff2018-02-16 16:04:20 -08003408TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003409 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003410 BitrateSettings bitrate;
3411 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003412 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3413}
3414
Steve Anton36da6ff2018-02-16 16:04:20 -08003415TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003416 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003417 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003418 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003419 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3420}
3421
Steve Anton36da6ff2018-02-16 16:04:20 -08003422TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003423 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003424 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003425 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003426 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003427 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3428}
3429
Steve Anton36da6ff2018-02-16 16:04:20 -08003430TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003431 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003432 BitrateSettings bitrate;
3433 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003434 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3435}
3436
Steve Anton36da6ff2018-02-16 16:04:20 -08003437TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003438 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003439 BitrateSettings bitrate;
3440 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003441 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003442 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3443}
3444
Steve Anton36da6ff2018-02-16 16:04:20 -08003445TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003446 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003447 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003448 bitrate.min_bitrate_bps = 10;
3449 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003450 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3451}
3452
Steve Anton36da6ff2018-02-16 16:04:20 -08003453TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003454 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003455 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003456 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003457 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3458}
3459
Niels Möller0c4f7be2018-05-07 14:01:37 +02003460// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003461// by Call's BitrateConstraints, which comes from the SDP or a default value.
3462// This test checks that a call to SetBitrate with a current bitrate that will
3463// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003464TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003465 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003466 BitrateSettings bitrate;
3467 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003468 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3469}
3470
zhihuang1c378ed2017-08-17 14:10:50 -07003471// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003472TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003473 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3474 RTCOfferAnswerOptions rtc_options;
3475
3476 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3477 // than kMaxOfferToReceiveMedia should be treated as invalid.
3478 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3479 CreatePeerConnection();
3480 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3481
3482 rtc_options.offer_to_receive_audio =
3483 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3484 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3485}
3486
Steve Anton36da6ff2018-02-16 16:04:20 -08003487TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003488 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3489 RTCOfferAnswerOptions rtc_options;
3490
3491 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3492 // than kMaxOfferToReceiveMedia should be treated as invalid.
3493 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3494 CreatePeerConnection();
3495 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3496
3497 rtc_options.offer_to_receive_video =
3498 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3499 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3500}
3501
3502// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003503// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003504TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003505 RTCOfferAnswerOptions rtc_options;
3506 rtc_options.offer_to_receive_audio = 1;
3507 rtc_options.offer_to_receive_video = 1;
3508
3509 std::unique_ptr<SessionDescriptionInterface> offer;
3510 CreatePeerConnection();
3511 offer = CreateOfferWithOptions(rtc_options);
3512 ASSERT_TRUE(offer);
3513 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3514 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3515}
3516
3517// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003518// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003519TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003520 RTCOfferAnswerOptions rtc_options;
3521 rtc_options.offer_to_receive_audio = 1;
3522 rtc_options.offer_to_receive_video = 0;
3523
3524 std::unique_ptr<SessionDescriptionInterface> offer;
3525 CreatePeerConnection();
3526 offer = CreateOfferWithOptions(rtc_options);
3527 ASSERT_TRUE(offer);
3528 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3529 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3530}
3531
Artem Titov880fa812021-07-30 22:30:23 +02003532// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003533// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003534TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003535 RTCOfferAnswerOptions rtc_options;
3536 rtc_options.offer_to_receive_audio = 0;
3537 rtc_options.offer_to_receive_video = 1;
3538
3539 std::unique_ptr<SessionDescriptionInterface> offer;
3540 CreatePeerConnection();
3541 offer = CreateOfferWithOptions(rtc_options);
3542 ASSERT_TRUE(offer);
3543 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3544 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3545}
3546
zhihuang1c378ed2017-08-17 14:10:50 -07003547// Test that no media content will be added to the offer if using default
3548// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003549TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003550 RTCOfferAnswerOptions rtc_options;
3551
3552 std::unique_ptr<SessionDescriptionInterface> offer;
3553 CreatePeerConnection();
3554 offer = CreateOfferWithOptions(rtc_options);
3555 ASSERT_TRUE(offer);
3556 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3557 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3558}
3559
Artem Titov880fa812021-07-30 22:30:23 +02003560// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003561// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003562TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3563 CreatePeerConnection();
3564
zhihuang1c378ed2017-08-17 14:10:50 -07003565 RTCOfferAnswerOptions rtc_options;
3566 rtc_options.ice_restart = false;
3567 rtc_options.offer_to_receive_audio = 1;
3568
3569 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003570 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003571 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3572 auto ufrag1 =
3573 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3574 auto pwd1 =
3575 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003576
Artem Titov880fa812021-07-30 22:30:23 +02003577 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003578 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003579 auto ufrag2 =
3580 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3581 auto pwd2 =
3582 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003583
Artem Titov880fa812021-07-30 22:30:23 +02003584 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003585 rtc_options.ice_restart = true;
3586 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003587 auto ufrag3 =
3588 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3589 auto pwd3 =
3590 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003591
3592 EXPECT_EQ(ufrag1, ufrag2);
3593 EXPECT_EQ(pwd1, pwd2);
3594 EXPECT_NE(ufrag2, ufrag3);
3595 EXPECT_NE(pwd2, pwd3);
3596}
3597
Artem Titov880fa812021-07-30 22:30:23 +02003598// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003599// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003600TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003601 RTCOfferAnswerOptions rtc_options;
3602 rtc_options.offer_to_receive_audio = 1;
3603 rtc_options.offer_to_receive_video = 1;
3604
3605 std::unique_ptr<SessionDescriptionInterface> offer;
3606 CreatePeerConnection();
3607
3608 rtc_options.use_rtp_mux = true;
3609 offer = CreateOfferWithOptions(rtc_options);
3610 ASSERT_TRUE(offer);
3611 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3612 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3613 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3614
3615 rtc_options.use_rtp_mux = false;
3616 offer = CreateOfferWithOptions(rtc_options);
3617 ASSERT_TRUE(offer);
3618 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3619 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3620 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3621}
3622
zhihuang141aacb2017-08-29 13:23:53 -07003623// This test ensures OnRenegotiationNeeded is called when we add track with
3624// MediaStream -> AddTrack in the same way it is called when we add track with
3625// PeerConnection -> AddTrack.
3626// The test can be removed once addStream is rewritten in terms of addTrack
3627// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003628// Don't run under Unified Plan since the stream API is not available.
3629TEST_F(PeerConnectionInterfaceTestPlanB,
3630 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003631 CreatePeerConnectionWithoutDtls();
3632 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003633 pc_factory_->CreateLocalMediaStream(kStreamId1));
Niels Möllerafb246b2022-04-20 14:26:50 +02003634 pc_->AddStream(stream.get());
zhihuang141aacb2017-08-29 13:23:53 -07003635 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003636 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003637 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003638 CreateVideoTrack("video_track"));
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003639 stream->AddTrack(audio_track);
zhihuang141aacb2017-08-29 13:23:53 -07003640 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3641 observer_.renegotiation_needed_ = false;
3642
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003643 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003644 stream->AddTrack(video_track);
zhihuang141aacb2017-08-29 13:23:53 -07003645 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3646 observer_.renegotiation_needed_ = false;
3647
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003648 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003649 stream->RemoveTrack(audio_track);
zhihuang141aacb2017-08-29 13:23:53 -07003650 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3651 observer_.renegotiation_needed_ = false;
3652
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003653 CreateOfferReceiveAnswer();
Harald Alvestrand2f7ad282022-04-21 11:35:43 +00003654 stream->RemoveTrack(video_track);
zhihuang141aacb2017-08-29 13:23:53 -07003655 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3656 observer_.renegotiation_needed_ = false;
3657}
3658
Zhi Huangb2d355e2017-10-26 17:26:37 -07003659// Tests that an error is returned if a description is applied that has fewer
3660// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003661TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003662 MediaSectionCountEnforcedForSubsequentOffer) {
3663 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003664 AddAudioTrack("audio_label");
3665 AddVideoTrack("video_label");
3666
Zhi Huangb2d355e2017-10-26 17:26:37 -07003667 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003668 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003669 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3670
3671 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003672 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003673 offer->description()->contents().pop_back();
3674 offer->description()->contents().pop_back();
3675 ASSERT_TRUE(offer->description()->contents().empty());
3676 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3677
3678 std::unique_ptr<SessionDescriptionInterface> answer;
3679 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3680 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3681
3682 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003683 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003684 offer->description()->contents().pop_back();
3685 offer->description()->contents().pop_back();
3686 ASSERT_TRUE(offer->description()->contents().empty());
3687 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3688}
3689
Johannes Kron89f874e2018-11-12 10:25:48 +01003690TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3691 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003692 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003693 CreatePeerConnection(config);
3694 std::unique_ptr<SessionDescriptionInterface> offer;
3695 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003696 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3697 // Possible to set to false.
3698 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003699 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003700 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003701 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003702 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003703}
3704
Byoungchan Lee10a7d232022-07-20 21:49:17 +09003705TEST_P(PeerConnectionInterfaceTest,
3706 RtpSenderSetDegradationPreferenceWithoutEncodings) {
3707 CreatePeerConnection();
3708 AddVideoTrack("video_label");
3709
3710 std::vector<rtc::scoped_refptr<RtpSenderInterface>> rtp_senders =
3711 pc_->GetSenders();
3712 ASSERT_EQ(rtp_senders.size(), 1u);
3713 ASSERT_EQ(rtp_senders[0]->media_type(), cricket::MEDIA_TYPE_VIDEO);
3714 rtc::scoped_refptr<RtpSenderInterface> video_rtp_sender = rtp_senders[0];
3715 RtpParameters parameters = video_rtp_sender->GetParameters();
3716 ASSERT_NE(parameters.degradation_preference,
3717 DegradationPreference::MAINTAIN_RESOLUTION);
3718 parameters.degradation_preference =
3719 DegradationPreference::MAINTAIN_RESOLUTION;
3720 ASSERT_TRUE(video_rtp_sender->SetParameters(parameters).ok());
3721
3722 std::unique_ptr<SessionDescriptionInterface> local_offer;
3723 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
3724 ASSERT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3725
3726 RtpParameters parameters_new = video_rtp_sender->GetParameters();
3727 ASSERT_EQ(parameters_new.degradation_preference,
3728 DegradationPreference::MAINTAIN_RESOLUTION);
3729}
3730
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003731INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3732 PeerConnectionInterfaceTest,
Florent Castelli15a38de2022-04-06 00:38:21 +02003733 Values(SdpSemantics::kPlanB_DEPRECATED,
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003734 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003735
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003736class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003737 protected:
3738 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003739 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003740 }
nisseeaabdf62017-05-05 02:23:02 -07003741 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003742 const RTCConfiguration& config) {
Florent Castelli72424402022-04-06 03:45:10 +02003743 PeerConnectionDependencies pc_dependencies(&observer_);
3744 auto result =
3745 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3746 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02003747 observer_.SetPeerConnectionInterface(result.value().get());
Florent Castelli72424402022-04-06 03:45:10 +02003748 return result.value()->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003749 }
3750
zhihuang9763d562016-08-05 11:14:50 -07003751 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003752 MockPeerConnectionObserver observer_;
3753};
3754
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003755// This sanity check validates the test infrastructure itself.
3756TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3757 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003758 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Florent Castelli72424402022-04-06 03:45:10 +02003759 PeerConnectionDependencies pc_dependencies(&observer_);
3760 auto result =
3761 pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
3762 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +02003763 observer_.SetPeerConnectionInterface(result.value().get());
Florent Castelli72424402022-04-06 03:45:10 +02003764 result.value()->Close(); // No abort -> ok.
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003765 SUCCEED();
3766}
3767
nisse51542be2016-02-12 02:27:06 -08003768// This test verifies the default behaviour with no constraints and a
3769// default RTCConfiguration.
3770TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3771 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003772 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003773
Niels Möllerf06f9232018-08-07 12:32:18 +02003774 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003775
Henrik Boström35c5cc82022-04-14 09:23:20 +02003776 EXPECT_TRUE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003777 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3778 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003779 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003780 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003781}
3782
Niels Möller1d7ecd22018-01-18 15:25:12 +01003783// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003784// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003785TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3786 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003787 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003788
Niels Möller71bdda02016-03-31 12:59:59 +02003789 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003790 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003791
Niels Möller1d7ecd22018-01-18 15:25:12 +01003792 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003793}
3794
Niels Möller6539f692018-01-18 08:58:50 +01003795// This test verifies that the experiment_cpu_load_estimator flag is
3796// propagated from RTCConfiguration to the PeerConnection.
3797TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3798 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003799 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Niels Möller6539f692018-01-18 08:58:50 +01003800
3801 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003802 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003803
3804 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3805}
3806
deadbeef293e9262017-01-11 12:28:30 -08003807// Tests a few random fields being different.
3808TEST(RTCConfigurationTest, ComparisonOperators) {
3809 PeerConnectionInterface::RTCConfiguration a;
3810 PeerConnectionInterface::RTCConfiguration b;
3811 EXPECT_EQ(a, b);
3812
3813 PeerConnectionInterface::RTCConfiguration c;
3814 c.servers.push_back(PeerConnectionInterface::IceServer());
3815 EXPECT_NE(a, c);
3816
3817 PeerConnectionInterface::RTCConfiguration d;
3818 d.type = PeerConnectionInterface::kRelay;
3819 EXPECT_NE(a, d);
3820
3821 PeerConnectionInterface::RTCConfiguration e;
3822 e.audio_jitter_buffer_max_packets = 5;
3823 EXPECT_NE(a, e);
3824
3825 PeerConnectionInterface::RTCConfiguration f;
3826 f.ice_connection_receiving_timeout = 1337;
3827 EXPECT_NE(a, f);
3828
deadbeef293e9262017-01-11 12:28:30 -08003829 PeerConnectionInterface::RTCConfiguration h(
3830 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3831 EXPECT_NE(a, h);
3832}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003833
3834} // namespace
3835} // namespace webrtc