blob: 44f135ffab573f4f9a03b91fd7960e15847826a9 [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>
15#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwibergd1fe2812016-04-27 06:47:29 -070017#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080019#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010020#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
Steve Anton1c9c9fc2019-02-14 15:13:09 -080022#include "absl/strings/str_replace.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "absl/types/optional.h"
24#include "api/audio/audio_mixer.h"
25#include "api/audio_codecs/audio_decoder_factory.h"
26#include "api/audio_codecs/audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/audio_codecs/builtin_audio_decoder_factory.h"
28#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/call/call_factory_interface.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010030#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/data_channel_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/jsep_session_description.h"
34#include "api/media_stream_interface.h"
35#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "api/rtc_error.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020037#include "api/rtc_event_log/rtc_event_log.h"
38#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "api/rtc_event_log_output.h"
Niels Möllerd8b9ed72019-05-08 13:53:51 +020040#include "api/rtc_event_log_output_file.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "api/rtp_receiver_interface.h"
42#include "api/rtp_sender_interface.h"
43#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010044#include "api/scoped_refptr.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020045#include "api/task_queue/default_task_queue_factory.h"
Erik Språngceb44952020-09-22 11:36:35 +020046#include "api/transport/field_trial_based_config.h"
Anders Carlsson67537952018-05-03 11:28:29 +020047#include "api/video_codecs/builtin_video_decoder_factory.h"
48#include "api/video_codecs/builtin_video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010049#include "api/video_codecs/video_decoder_factory.h"
50#include "api/video_codecs/video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010051#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080052#include "media/base/media_config.h"
53#include "media/base/media_engine.h"
54#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020056#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "media/sctp/sctp_transport_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010058#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080060#include "p2p/base/fake_port_allocator.h"
61#include "p2p/base/p2p_constants.h"
Yves Gerey3e707812018-11-28 16:47:49 +010062#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "p2p/base/port_allocator.h"
64#include "p2p/base/transport_description.h"
65#include "p2p/base/transport_info.h"
66#include "pc/audio_track.h"
67#include "pc/media_session.h"
68#include "pc/media_stream.h"
69#include "pc/peer_connection.h"
70#include "pc/peer_connection_factory.h"
71#include "pc/rtc_stats_collector.h"
72#include "pc/rtp_sender.h"
73#include "pc/session_description.h"
74#include "pc/stream_collection.h"
75#include "pc/test/fake_audio_capture_module.h"
76#include "pc/test/fake_rtc_certificate_generator.h"
77#include "pc/test/fake_video_track_source.h"
78#include "pc/test/mock_peer_connection_observers.h"
79#include "pc/test/test_sdp_strings.h"
80#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010081#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080082#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020083#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080084#include "rtc_base/ref_counted_object.h"
85#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080086#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010087#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080088#include "rtc_base/time_utils.h"
89#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020090#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010091#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080092#include "test/testsupport/file_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070093
94#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080095#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070096#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010098namespace webrtc {
99namespace {
100
Seth Hampson845e8782018-03-02 11:34:10 -0800101static const char kStreamId1[] = "local_stream_1";
102static const char kStreamId2[] = "local_stream_2";
103static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104static const int kDefaultStunPort = 3478;
105static const char kStunAddressOnly[] = "stun:address";
106static const char kStunInvalidPort[] = "stun:address:-1";
107static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
108static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +0100109static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110static const char kTurnUsername[] = "user";
111static const char kTurnPassword[] = "password";
112static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
deadbeefab9b2d12015-10-14 11:33:11 -0700115static const char kStreams[][8] = {"stream1", "stream2"};
116static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
117static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
118
deadbeef5e97fb52015-10-15 12:49:08 -0700119static const char kRecvonly[] = "recvonly";
120static const char kSendrecv[] = "sendrecv";
121
deadbeefab9b2d12015-10-14 11:33:11 -0700122// Reference SDP with a MediaStream with label "stream1" and audio track with
123// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800124static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800129 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700130 "a=ice-ufrag:e5785931\r\n"
131 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700135 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800136 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:103 ISAC/16000\r\n"
138 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000139 "a=ssrc:1 mslabel:stream1\r\n"
140 "a=ssrc:1 label:audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800142 "a=ice-ufrag:e5785931\r\n"
143 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
144 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
145 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700146 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700147 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800148 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700149 "a=rtpmap:120 VP8/90000\r\n"
150 "a=ssrc:2 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000151 "a=ssrc:2 mslabel:stream1\r\n"
152 "a=ssrc:2 label:videotrack0\r\n";
153// Same string as above but with the MID changed to the Unified Plan default.
154// This is needed so that this SDP can be used as an answer for a Unified Plan
155// offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800156static const char kSdpStringWithStream1UnifiedPlan[] =
157 "v=0\r\n"
158 "o=- 0 0 IN IP4 127.0.0.1\r\n"
159 "s=-\r\n"
160 "t=0 0\r\n"
161 "m=audio 1 RTP/AVPF 103\r\n"
162 "a=ice-ufrag:e5785931\r\n"
163 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
164 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
165 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
166 "a=mid:0\r\n"
167 "a=sendrecv\r\n"
168 "a=rtcp-mux\r\n"
169 "a=rtpmap:103 ISAC/16000\r\n"
170 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000171 "a=ssrc:1 mslabel:stream1\r\n"
172 "a=ssrc:1 label:audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800173 "m=video 1 RTP/AVPF 120\r\n"
174 "a=ice-ufrag:e5785931\r\n"
175 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
176 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
177 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
178 "a=mid:1\r\n"
179 "a=sendrecv\r\n"
180 "a=rtcp-mux\r\n"
181 "a=rtpmap:120 VP8/90000\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000182 "a=ssrc:2 cname:stream1\r\n"
183 "a=ssrc:2 mslabel:stream1\r\n"
184 "a=ssrc:2 label:videotrack0\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700185
zhihuang81c3a032016-11-17 12:06:24 -0800186// Reference SDP with a MediaStream with label "stream1" and audio track with
187// id "audio_1";
188static const char kSdpStringWithStream1AudioTrackOnly[] =
189 "v=0\r\n"
190 "o=- 0 0 IN IP4 127.0.0.1\r\n"
191 "s=-\r\n"
192 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800193 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800194 "a=ice-ufrag:e5785931\r\n"
195 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
196 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
197 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800198 "a=mid:audio\r\n"
199 "a=sendrecv\r\n"
200 "a=rtpmap:103 ISAC/16000\r\n"
201 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000202 "a=ssrc:1 mslabel:stream1\r\n"
203 "a=ssrc:1 label:audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800204 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800205
deadbeefab9b2d12015-10-14 11:33:11 -0700206// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
207// MediaStreams have one audio track and one video track.
208// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800209static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "v=0\r\n"
211 "o=- 0 0 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800214 "a=msid-semantic: WMS stream1 stream2\r\n"
215 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700216 "a=ice-ufrag:e5785931\r\n"
217 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
218 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
219 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700221 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800222 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=rtpmap:103 ISAC/16000\r\n"
224 "a=ssrc:1 cname:stream1\r\n"
225 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
226 "a=ssrc:3 cname:stream2\r\n"
227 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
228 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800229 "a=ice-ufrag:e5785931\r\n"
230 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
231 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
232 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700233 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700234 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800235 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700236 "a=rtpmap:120 VP8/0\r\n"
237 "a=ssrc:2 cname:stream1\r\n"
238 "a=ssrc:2 msid:stream1 videotrack0\r\n"
239 "a=ssrc:4 cname:stream2\r\n"
240 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800241static const char kSdpStringWithStream1And2UnifiedPlan[] =
242 "v=0\r\n"
243 "o=- 0 0 IN IP4 127.0.0.1\r\n"
244 "s=-\r\n"
245 "t=0 0\r\n"
246 "a=msid-semantic: WMS stream1 stream2\r\n"
247 "m=audio 1 RTP/AVPF 103\r\n"
248 "a=ice-ufrag:e5785931\r\n"
249 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
250 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
251 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
252 "a=mid:0\r\n"
253 "a=sendrecv\r\n"
254 "a=rtcp-mux\r\n"
255 "a=rtpmap:103 ISAC/16000\r\n"
256 "a=ssrc:1 cname:stream1\r\n"
257 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
258 "m=video 1 RTP/AVPF 120\r\n"
259 "a=ice-ufrag:e5785931\r\n"
260 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
261 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
262 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
263 "a=mid:1\r\n"
264 "a=sendrecv\r\n"
265 "a=rtcp-mux\r\n"
266 "a=rtpmap:120 VP8/0\r\n"
267 "a=ssrc:2 cname:stream1\r\n"
268 "a=ssrc:2 msid:stream1 videotrack0\r\n"
269 "m=audio 1 RTP/AVPF 103\r\n"
270 "a=ice-ufrag:e5785931\r\n"
271 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
272 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
273 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
274 "a=mid:2\r\n"
275 "a=sendrecv\r\n"
276 "a=rtcp-mux\r\n"
277 "a=rtpmap:103 ISAC/16000\r\n"
278 "a=ssrc:3 cname:stream2\r\n"
279 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
280 "m=video 1 RTP/AVPF 120\r\n"
281 "a=ice-ufrag:e5785931\r\n"
282 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
283 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
284 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
285 "a=mid:3\r\n"
286 "a=sendrecv\r\n"
287 "a=rtcp-mux\r\n"
288 "a=rtpmap:120 VP8/0\r\n"
289 "a=ssrc:4 cname:stream2\r\n"
290 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700291
292// Reference SDP without MediaStreams. Msid is not supported.
293static const char kSdpStringWithoutStreams[] =
294 "v=0\r\n"
295 "o=- 0 0 IN IP4 127.0.0.1\r\n"
296 "s=-\r\n"
297 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800298 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700299 "a=ice-ufrag:e5785931\r\n"
300 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
301 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
302 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700303 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700304 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800305 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700306 "a=rtpmap:103 ISAC/16000\r\n"
307 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800308 "a=ice-ufrag:e5785931\r\n"
309 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
310 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
311 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700312 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700313 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800314 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700315 "a=rtpmap:120 VP8/90000\r\n";
316
317// Reference SDP without MediaStreams. Msid is supported.
318static const char kSdpStringWithMsidWithoutStreams[] =
319 "v=0\r\n"
320 "o=- 0 0 IN IP4 127.0.0.1\r\n"
321 "s=-\r\n"
322 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800323 "a=msid-semantic: WMS\r\n"
324 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700325 "a=ice-ufrag:e5785931\r\n"
326 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
327 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
328 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700329 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700330 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800331 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700332 "a=rtpmap:103 ISAC/16000\r\n"
333 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800334 "a=ice-ufrag:e5785931\r\n"
335 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
336 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
337 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700338 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700339 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800340 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700341 "a=rtpmap:120 VP8/90000\r\n";
342
343// Reference SDP without MediaStreams and audio only.
344static const char kSdpStringWithoutStreamsAudioOnly[] =
345 "v=0\r\n"
346 "o=- 0 0 IN IP4 127.0.0.1\r\n"
347 "s=-\r\n"
348 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800349 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700350 "a=ice-ufrag:e5785931\r\n"
351 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
352 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
353 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700354 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700355 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800356 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700357 "a=rtpmap:103 ISAC/16000\r\n";
358
359// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
360static const char kSdpStringSendOnlyWithoutStreams[] =
361 "v=0\r\n"
362 "o=- 0 0 IN IP4 127.0.0.1\r\n"
363 "s=-\r\n"
364 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800365 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700366 "a=ice-ufrag:e5785931\r\n"
367 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
368 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
369 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700370 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700371 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700372 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800373 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700374 "a=rtpmap:103 ISAC/16000\r\n"
375 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800376 "a=ice-ufrag:e5785931\r\n"
377 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
378 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
379 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700380 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700381 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700382 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800383 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700384 "a=rtpmap:120 VP8/90000\r\n";
385
386static const char kSdpStringInit[] =
387 "v=0\r\n"
388 "o=- 0 0 IN IP4 127.0.0.1\r\n"
389 "s=-\r\n"
390 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700391 "a=msid-semantic: WMS\r\n";
392
393static const char kSdpStringAudio[] =
394 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800395 "a=ice-ufrag:e5785931\r\n"
396 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
397 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
398 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700399 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700400 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800401 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700402 "a=rtpmap:103 ISAC/16000\r\n";
403
404static const char kSdpStringVideo[] =
405 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800406 "a=ice-ufrag:e5785931\r\n"
407 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
408 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
409 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700410 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700411 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800412 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700413 "a=rtpmap:120 VP8/90000\r\n";
414
415static const char kSdpStringMs1Audio0[] =
416 "a=ssrc:1 cname:stream1\r\n"
417 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
418
419static const char kSdpStringMs1Video0[] =
420 "a=ssrc:2 cname:stream1\r\n"
421 "a=ssrc:2 msid:stream1 videotrack0\r\n";
422
423static const char kSdpStringMs1Audio1[] =
424 "a=ssrc:3 cname:stream1\r\n"
425 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
426
427static const char kSdpStringMs1Video1[] =
428 "a=ssrc:4 cname:stream1\r\n"
429 "a=ssrc:4 msid:stream1 videotrack1\r\n";
430
deadbeef8662f942017-01-20 21:20:51 -0800431static const char kDtlsSdesFallbackSdp[] =
432 "v=0\r\n"
433 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
434 "s=-\r\n"
435 "c=IN IP4 0.0.0.0\r\n"
436 "t=0 0\r\n"
437 "a=group:BUNDLE audio\r\n"
438 "a=msid-semantic: WMS\r\n"
439 "m=audio 1 RTP/SAVPF 0\r\n"
440 "a=sendrecv\r\n"
441 "a=rtcp-mux\r\n"
442 "a=mid:audio\r\n"
443 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000444 "a=ssrc:1 mslabel:stream1\r\n"
445 "a=ssrc:1 label:audiotrack0\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800446 "a=ice-ufrag:e5785931\r\n"
447 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
448 "a=rtpmap:0 pcmu/8000\r\n"
449 "a=fingerprint:sha-1 "
450 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
451 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700452 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800453 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
454 "dummy_session_params\r\n";
455
Niels Möllerdec9f742019-06-03 15:25:20 +0200456class RtcEventLogOutputNull final : public RtcEventLogOutput {
457 public:
458 bool IsActive() const override { return true; }
459 bool Write(const std::string& output) override { return true; }
460};
461
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100462using ::cricket::StreamParams;
perkjd61bf802016-03-24 03:16:19 -0700463using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800464using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
Steve Anton36da6ff2018-02-16 16:04:20 -0800466using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
467using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700468
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469// Gets the first ssrc of given content type from the ContentInfo.
470bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
471 if (!content_info || !ssrc) {
472 return false;
473 }
474 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800475 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 if (!media_desc || media_desc->streams().empty()) {
477 return false;
478 }
479 *ssrc = media_desc->streams().begin()->first_ssrc();
480 return true;
481}
482
deadbeefd1a38b52016-12-10 13:15:33 -0800483// Get the ufrags out of an SDP blob. Useful for testing ICE restart
484// behavior.
485std::vector<std::string> GetUfrags(
486 const webrtc::SessionDescriptionInterface* desc) {
487 std::vector<std::string> ufrags;
488 for (const cricket::TransportInfo& info :
489 desc->description()->transport_infos()) {
490 ufrags.push_back(info.description.ice_ufrag);
491 }
492 return ufrags;
493}
494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495void SetSsrcToZero(std::string* sdp) {
496 const char kSdpSsrcAtribute[] = "a=ssrc:";
497 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
498 size_t ssrc_pos = 0;
499 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200500 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 size_t end_ssrc = sdp->find(" ", ssrc_pos);
502 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
503 ssrc_pos = end_ssrc;
504 }
505}
506
Artem Titov880fa812021-07-30 22:30:23 +0200507// Check if `streams` contains the specified track.
deadbeefab9b2d12015-10-14 11:33:11 -0700508bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800509 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700510 const std::string& track_id) {
511 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800512 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700513 return true;
514 }
515 }
516 return false;
517}
518
Artem Titov880fa812021-07-30 22:30:23 +0200519// Check if `senders` contains the specified sender, by id.
deadbeefab9b2d12015-10-14 11:33:11 -0700520bool ContainsSender(
521 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
522 const std::string& id) {
523 for (const auto& sender : senders) {
524 if (sender->id() == id) {
525 return true;
526 }
527 }
528 return false;
529}
530
Artem Titov880fa812021-07-30 22:30:23 +0200531// Check if `senders` contains the specified sender, by id and stream id.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700532bool ContainsSender(
533 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
534 const std::string& id,
535 const std::string& stream_id) {
536 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700537 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700538 return true;
539 }
540 }
541 return false;
542}
543
deadbeefab9b2d12015-10-14 11:33:11 -0700544// Create a collection of streams.
545// CreateStreamCollection(1) creates a collection that
546// correspond to kSdpStringWithStream1.
547// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
548rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700549 int number_of_streams,
550 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700551 rtc::scoped_refptr<StreamCollection> local_collection(
552 StreamCollection::Create());
553
554 for (int i = 0; i < number_of_streams; ++i) {
555 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
556 webrtc::MediaStream::Create(kStreams[i]));
557
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700558 for (int j = 0; j < tracks_per_stream; ++j) {
559 // Add a local audio track.
560 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
561 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
562 nullptr));
563 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700564
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700565 // Add a local video track.
566 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
567 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700568 webrtc::FakeVideoTrackSource::Create(),
569 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700570 stream->AddTrack(video_track);
571 }
deadbeefab9b2d12015-10-14 11:33:11 -0700572
573 local_collection->AddStream(stream);
574 }
575 return local_collection;
576}
577
578// Check equality of StreamCollections.
579bool CompareStreamCollections(StreamCollectionInterface* s1,
580 StreamCollectionInterface* s2) {
581 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
582 return false;
583 }
584
585 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700586 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700587 return false;
588 }
589 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
590 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
591 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
592 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
593
594 if (audio_tracks1.size() != audio_tracks2.size()) {
595 return false;
596 }
597 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
598 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
599 return false;
600 }
601 }
602 if (video_tracks1.size() != video_tracks2.size()) {
603 return false;
604 }
605 for (size_t j = 0; j != video_tracks1.size(); ++j) {
606 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
607 return false;
608 }
609 }
610 }
611 return true;
612}
613
perkjd61bf802016-03-24 03:16:19 -0700614// Helper class to test Observer.
615class MockTrackObserver : public ObserverInterface {
616 public:
617 explicit MockTrackObserver(NotifierInterface* notifier)
618 : notifier_(notifier) {
619 notifier_->RegisterObserver(this);
620 }
621
622 ~MockTrackObserver() { Unregister(); }
623
624 void Unregister() {
625 if (notifier_) {
626 notifier_->UnregisterObserver(this);
627 notifier_ = nullptr;
628 }
629 }
630
Danil Chapovalov3a353122020-05-15 11:16:53 +0200631 MOCK_METHOD(void, OnChanged, (), (override));
perkjd61bf802016-03-24 03:16:19 -0700632
633 private:
634 NotifierInterface* notifier_;
635};
636
nisse528b7932017-05-08 03:21:43 -0700637// The PeerConnectionMediaConfig tests below verify that configuration and
638// constraints are propagated into the PeerConnection's MediaConfig. These
639// settings are intended for MediaChannel constructors, but that is not
640// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700641class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
642 public:
zhihuang38ede132017-06-15 12:52:32 -0700643 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
644 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100645 PeerConnectionFactoryDependencies dependencies;
646 dependencies.worker_thread = rtc::Thread::Current();
647 dependencies.network_thread = rtc::Thread::Current();
648 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200649 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200650 dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200651 cricket::MediaEngineDependencies media_deps;
652 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
henrika919dc2e2017-10-12 14:24:55 +0200653 // Use fake audio device module since we're only testing the interface
654 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200655 media_deps.adm = FakeAudioCaptureModule::Create();
656 SetMediaEngineDefaults(&media_deps);
Erik Språngceb44952020-09-22 11:36:35 +0200657 media_deps.trials = dependencies.trials.get();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200658 dependencies.media_engine =
659 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100660 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200661 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200662 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700663
Tommi87f70902021-04-27 14:43:08 +0200664 return rtc::make_ref_counted<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100665 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700666 }
667
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100668 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800669
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100670 private:
deadbeefd7850b22017-08-23 10:59:19 -0700671 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700672};
673
Steve Anton36da6ff2018-02-16 16:04:20 -0800674// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200675class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800677 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
678 : vss_(new rtc::VirtualSocketServer()),
679 main_(vss_.get()),
680 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800681#ifdef WEBRTC_ANDROID
682 webrtc::InitializeAndroidObjects();
683#endif
684 }
685
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200686 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700687 // Use fake audio capture module since we're only testing the interface
688 // level, and using a real one could make tests flaky when run in parallel.
689 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700691 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200692 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
693 fake_audio_capture_module_),
694 webrtc::CreateBuiltinAudioEncoderFactory(),
695 webrtc::CreateBuiltinAudioDecoderFactory(),
696 webrtc::CreateBuiltinVideoEncoderFactory(),
697 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
698 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700699 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700700 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700701 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200704 void TearDown() override {
705 if (pc_)
706 pc_->Close();
707 }
708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200710 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000713 // DTLS does not work in a loopback call, so is disabled for many
deadbeef293e9262017-01-11 12:28:30 -0800714 // tests in this file.
715 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200716 RTCConfiguration config;
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000717 PeerConnectionFactoryInterface::Options options;
718 options.disable_encryption = true;
719 pc_factory_->SetOptions(options);
Niels Möllerf06f9232018-08-07 12:32:18 +0200720 CreatePeerConnection(config);
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000721 options.disable_encryption = false;
722 pc_factory_->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725 void CreatePeerConnectionWithIceTransportsType(
726 PeerConnectionInterface::IceTransportsType type) {
727 PeerConnectionInterface::RTCConfiguration config;
728 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200729 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 }
731
732 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100733 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800735 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700737 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100738 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 server.password = password;
740 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200741 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
Niels Möllerf06f9232018-08-07 12:32:18 +0200744 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200745 if (pc_) {
746 pc_->Close();
747 pc_ = nullptr;
748 }
kwibergd1fe2812016-04-27 06:47:29 -0700749 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800750 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
751 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000752
deadbeef1dcb1642017-03-29 21:08:16 -0700753 // Create certificate generator unless DTLS constraint is explicitly set to
754 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200755 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200756
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000757 // These won't be used if encryption is turned off, but that's harmless.
758 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
759 cert_generator.reset(fake_certificate_generator_);
760
Steve Anton36da6ff2018-02-16 16:04:20 -0800761 RTCConfiguration modified_config = config;
762 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200763 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200764 modified_config, std::move(port_allocator), std::move(cert_generator),
765 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 ASSERT_TRUE(pc_.get() != NULL);
767 observer_.SetPeerConnectionInterface(pc_.get());
768 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
769 }
770
deadbeef0a6c4ca2015-10-06 11:38:28 -0700771 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700773 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700774 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800776 config.sdp_semantics = sdp_semantics_;
777 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200778 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700780 }
781
Steve Anton038834f2017-07-14 15:59:59 -0700782 void CreatePeerConnectionExpectFail(
783 PeerConnectionInterface::RTCConfiguration config) {
784 PeerConnectionInterface::IceServer server;
785 server.uri = kTurnIceServerUri;
786 server.password = kTurnPassword;
787 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800788 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700789 rtc::scoped_refptr<PeerConnectionInterface> pc =
790 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
791 EXPECT_EQ(nullptr, pc);
792 }
793
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100795 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
797 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
798 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801
deadbeef0a6c4ca2015-10-06 11:38:28 -0700802 CreatePeerConnectionExpectFail(kStunInvalidPort);
803 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
804 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805
Niels Möllerdb4def92019-03-18 16:53:59 +0100806 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
807 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800808 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
809 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800811 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800815 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817
818 void ReleasePeerConnection() {
819 pc_ = NULL;
820 observer_.SetPeerConnectionInterface(NULL);
821 }
822
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
824 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100825 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800826 }
827
828 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800829 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800830 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800831 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
Steve Anton36da6ff2018-02-16 16:04:20 -0800835 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700836 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800838 stream->AddTrack(CreateVideoTrack(label + "v0"));
839 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800840 }
841
842 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
843 const std::string& label) {
844 return pc_factory_->CreateAudioTrack(label, nullptr);
845 }
846
847 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800848 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800849 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800850 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800851 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800852 }
853
854 void AddAudioStream(const std::string& label) {
855 rtc::scoped_refptr<MediaStreamInterface> stream(
856 pc_factory_->CreateLocalMediaStream(label));
857 stream->AddTrack(CreateAudioTrack(label + "a0"));
858 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
Seth Hampson845e8782018-03-02 11:34:10 -0800861 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 const std::string& audio_track_label,
863 const std::string& video_track_label) {
864 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700865 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800866 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800867 stream->AddTrack(CreateAudioTrack(audio_track_label));
868 stream->AddTrack(CreateVideoTrack(video_track_label));
869 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 }
871
Steve Anton36da6ff2018-02-16 16:04:20 -0800872 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
873 cricket::MediaType media_type) {
874 for (auto receiver : pc_->GetReceivers()) {
875 if (receiver->media_type() == media_type) {
876 return receiver;
877 }
878 }
879 return nullptr;
880 }
881
kwibergd1fe2812016-04-27 06:47:29 -0700882 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200883 const RTCOfferAnswerOptions* options,
884 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200885 auto observer =
886 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200888 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200890 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700893 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 return observer->result();
895 }
896
kwibergd1fe2812016-04-27 06:47:29 -0700897 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200898 const RTCOfferAnswerOptions* options) {
899 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901
kwibergd1fe2812016-04-27 06:47:29 -0700902 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200903 const RTCOfferAnswerOptions* options) {
904 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906
Steve Antondb45ca82017-09-11 18:27:34 -0700907 bool DoSetSessionDescription(
908 std::unique_ptr<SessionDescriptionInterface> desc,
909 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200910 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700912 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700914 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
zhihuang29ff8442016-07-27 11:07:25 -0700916 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
917 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
918 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return observer->result();
920 }
921
Steve Antondb45ca82017-09-11 18:27:34 -0700922 bool DoSetLocalDescription(
923 std::unique_ptr<SessionDescriptionInterface> desc) {
924 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
Steve Antondb45ca82017-09-11 18:27:34 -0700927 bool DoSetRemoteDescription(
928 std::unique_ptr<SessionDescriptionInterface> desc) {
929 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 }
931
932 // Calls PeerConnection::GetStats and check the return value.
933 // It does not verify the values in the StatReports since a RTCP packet might
934 // be required.
935 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200936 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Yves Gerey665174f2018-06-19 15:03:05 +0200937 if (!pc_->GetStats(observer, track,
938 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 return false;
940 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
941 return observer->called();
942 }
943
Harald Alvestrand89061872018-01-02 14:08:34 +0100944 // Call the standards-compliant GetStats function.
945 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200946 auto callback =
947 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100948 pc_->GetStats(callback);
949 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
950 return callback->called();
951 }
952
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800954 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800956 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800957 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800958 } else {
959 // Unified Plan does not support AddStream, so just add an audio and video
960 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800961 AddAudioTrack(kAudioTracks[0], {kStreamId1});
962 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800963 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 CreateOfferReceiveAnswer();
965 }
966
967 // Verify that RTP Header extensions has been negotiated for audio and video.
968 void VerifyRemoteRtpHeaderExtensions() {
969 const cricket::MediaContentDescription* desc =
970 cricket::GetFirstAudioContentDescription(
971 pc_->remote_description()->description());
972 ASSERT_TRUE(desc != NULL);
973 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
974
975 desc = cricket::GetFirstVideoContentDescription(
976 pc_->remote_description()->description());
977 ASSERT_TRUE(desc != NULL);
978 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
979 }
980
981 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700982 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 std::string sdp;
985 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700986 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800987 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700988 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
990 }
991
deadbeefab9b2d12015-10-14 11:33:11 -0700992 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700993 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800994 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700995 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700996 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
997 }
998
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001001 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002
1003 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1004 // audio codec change, even if the parameter has nothing to do with
1005 // receiving. Not all parameters are serialized to SDP.
1006 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1007 // the SessionDescription, it is necessary to do that here to in order to
1008 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1009 // https://code.google.com/p/webrtc/issues/detail?id=1356
1010 std::string sdp;
1011 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001012 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001013 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001014 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1016 }
1017
1018 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001019 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001020 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
1022 std::string sdp;
1023 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001024 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001025 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001026 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1028 }
1029
1030 void CreateOfferReceiveAnswer() {
1031 CreateOfferAsLocalDescription();
1032 std::string sdp;
1033 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1034 CreateAnswerAsRemoteDescription(sdp);
1035 }
1036
1037 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001038 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001039 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1041 // audio codec change, even if the parameter has nothing to do with
1042 // receiving. Not all parameters are serialized to SDP.
1043 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1044 // the SessionDescription, it is necessary to do that here to in order to
1045 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1046 // https://code.google.com/p/webrtc/issues/detail?id=1356
1047 std::string sdp;
1048 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001049 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001050 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051
Steve Antondb45ca82017-09-11 18:27:34 -07001052 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001054 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001055 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 }
1057
deadbeefab9b2d12015-10-14 11:33:11 -07001058 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001059 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001060 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001061 ASSERT_TRUE(answer);
1062 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1064 }
1065
deadbeefab9b2d12015-10-14 11:33:11 -07001066 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001067 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001068 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001069 ASSERT_TRUE(pr_answer);
1070 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001072 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001073 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001074 ASSERT_TRUE(answer);
1075 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1077 }
1078
Seth Hampson845e8782018-03-02 11:34:10 -08001079 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001080 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001081 // called with the given stream id and expected number of tracks.
1082 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001083 int expected_num_tracks) {
1084 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001085 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001086 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001087 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 }
1089
1090 // Creates an offer and applies it as a local session description.
1091 // Creates an answer with the same SDP an the offer but removes all lines
1092 // that start with a:ssrc"
1093 void CreateOfferReceiveAnswerWithoutSsrc() {
1094 CreateOfferAsLocalDescription();
1095 std::string sdp;
1096 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1097 SetSsrcToZero(&sdp);
1098 CreateAnswerAsRemoteDescription(sdp);
1099 }
1100
deadbeefab9b2d12015-10-14 11:33:11 -07001101 // This function creates a MediaStream with label kStreams[0] and
Artem Titov880fa812021-07-30 22:30:23 +02001102 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001103 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001104 // is returned and the MediaStream is stored in
Artem Titov880fa812021-07-30 22:30:23 +02001105 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001106 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001107 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1108 size_t number_of_video_tracks) {
1109 EXPECT_LE(number_of_audio_tracks, 2u);
1110 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001111
1112 reference_collection_ = StreamCollection::Create();
1113 std::string sdp_ms1 = std::string(kSdpStringInit);
1114
Seth Hampson845e8782018-03-02 11:34:10 -08001115 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001116
1117 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001118 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001119 reference_collection_->AddStream(stream);
1120
1121 if (number_of_audio_tracks > 0) {
1122 sdp_ms1 += std::string(kSdpStringAudio);
1123 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1124 AddAudioTrack(kAudioTracks[0], stream);
1125 }
1126 if (number_of_audio_tracks > 1) {
1127 sdp_ms1 += kSdpStringMs1Audio1;
1128 AddAudioTrack(kAudioTracks[1], stream);
1129 }
1130
1131 if (number_of_video_tracks > 0) {
1132 sdp_ms1 += std::string(kSdpStringVideo);
1133 sdp_ms1 += std::string(kSdpStringMs1Video0);
1134 AddVideoTrack(kVideoTracks[0], stream);
1135 }
1136 if (number_of_video_tracks > 1) {
1137 sdp_ms1 += kSdpStringMs1Video1;
1138 AddVideoTrack(kVideoTracks[1], stream);
1139 }
1140
kwibergd1fe2812016-04-27 06:47:29 -07001141 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001142 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001143 }
1144
1145 void AddAudioTrack(const std::string& track_id,
1146 MediaStreamInterface* stream) {
1147 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1148 webrtc::AudioTrack::Create(track_id, nullptr));
1149 ASSERT_TRUE(stream->AddTrack(audio_track));
1150 }
1151
1152 void AddVideoTrack(const std::string& track_id,
1153 MediaStreamInterface* stream) {
1154 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001155 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001156 webrtc::FakeVideoTrackSource::Create(),
1157 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001158 ASSERT_TRUE(stream->AddTrack(video_track));
1159 }
1160
Steve Anton36da6ff2018-02-16 16:04:20 -08001161 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001162 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001163 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001164 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001165 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1166 return offer;
1167 }
1168
Steve Anton36da6ff2018-02-16 16:04:20 -08001169 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1170 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001171 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001172 std::unique_ptr<SessionDescriptionInterface> offer;
1173 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1174 return offer;
1175 }
1176
1177 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1178 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1179 std::unique_ptr<SessionDescriptionInterface> answer;
1180 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1181 return answer;
1182 }
1183
kwibergfd8be342016-05-14 19:44:11 -07001184 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001186 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001187 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001188 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1189 return answer;
1190 }
1191
1192 const std::string& GetFirstAudioStreamCname(
1193 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001194 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001195 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001196 return audio_desc->streams()[0].cname;
1197 }
1198
zhihuang1c378ed2017-08-17 14:10:50 -07001199 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1200 const RTCOfferAnswerOptions& offer_answer_options) {
1201 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001202 auto observer =
1203 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
zhihuang1c378ed2017-08-17 14:10:50 -07001204 pc_->CreateOffer(observer, offer_answer_options);
1205 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1206 return observer->MoveDescription();
1207 }
1208
1209 void CreateOfferWithOptionsAsRemoteDescription(
1210 std::unique_ptr<SessionDescriptionInterface>* desc,
1211 const RTCOfferAnswerOptions& offer_answer_options) {
1212 *desc = CreateOfferWithOptions(offer_answer_options);
1213 ASSERT_TRUE(desc != nullptr);
1214 std::string sdp;
1215 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001216 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001217 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001218 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001219 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1220 }
1221
1222 void CreateOfferWithOptionsAsLocalDescription(
1223 std::unique_ptr<SessionDescriptionInterface>* desc,
1224 const RTCOfferAnswerOptions& offer_answer_options) {
1225 *desc = CreateOfferWithOptions(offer_answer_options);
1226 ASSERT_TRUE(desc != nullptr);
1227 std::string sdp;
1228 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001229 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001230 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001231
Steve Antondb45ca82017-09-11 18:27:34 -07001232 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001233 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1234 }
1235
1236 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001237 RTC_DCHECK(content);
1238 RTC_DCHECK(content->media_description());
1239 for (const cricket::AudioCodec& codec :
1240 content->media_description()->as_audio()->codecs()) {
1241 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001242 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001243 }
zhihuang1c378ed2017-08-17 14:10:50 -07001244 }
1245 return false;
1246 }
1247
Steve Anton36da6ff2018-02-16 16:04:20 -08001248 const char* GetSdpStringWithStream1() const {
1249 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1250 return kSdpStringWithStream1PlanB;
1251 } else {
1252 return kSdpStringWithStream1UnifiedPlan;
1253 }
1254 }
1255
1256 const char* GetSdpStringWithStream1And2() const {
1257 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1258 return kSdpStringWithStream1And2PlanB;
1259 } else {
1260 return kSdpStringWithStream1And2UnifiedPlan;
1261 }
1262 }
1263
deadbeef9a6f4d42017-05-15 19:43:33 -07001264 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1265 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001266 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001267 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001268 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001269 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1270 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1271 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001273 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001274 const SdpSemantics sdp_semantics_;
1275};
1276
1277class PeerConnectionInterfaceTest
1278 : public PeerConnectionInterfaceBaseTest,
1279 public ::testing::WithParamInterface<SdpSemantics> {
1280 protected:
1281 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1282};
1283
1284class PeerConnectionInterfaceTestPlanB
1285 : public PeerConnectionInterfaceBaseTest {
1286 protected:
1287 PeerConnectionInterfaceTestPlanB()
1288 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289};
1290
zhihuang8f65cdf2016-05-06 18:40:30 -07001291// Generate different CNAMEs when PeerConnections are created.
1292// The CNAMEs are expected to be generated randomly. It is possible
1293// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001294TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001295 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001296 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001297 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001298 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001299 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1300 GetFirstAudioStreamCname(offer2.get()));
1301}
1302
Steve Anton36da6ff2018-02-16 16:04:20 -08001303TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001304 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001305 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001306 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001307 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001308 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1309 GetFirstAudioStreamCname(answer2.get()));
1310}
1311
Steve Anton36da6ff2018-02-16 16:04:20 -08001312TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 CreatePeerConnectionWithDifferentConfigurations) {
1314 CreatePeerConnectionWithDifferentConfigurations();
1315}
1316
Steve Anton36da6ff2018-02-16 16:04:20 -08001317TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001318 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1319 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1320 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1321 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1322 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1323 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1324 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1325 port_allocator_->candidate_filter());
1326 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1327 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1328}
1329
1330// Test that when a PeerConnection is created with a nonzero candidate pool
1331// size, the pooled PortAllocatorSession is created with all the attributes
1332// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001333TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001335 config.sdp_semantics = sdp_semantics_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336 PeerConnectionInterface::IceServer server;
1337 server.uri = kStunAddressOnly;
1338 config.servers.push_back(server);
1339 config.type = PeerConnectionInterface::kRelay;
1340 config.disable_ipv6 = true;
1341 config.tcp_candidate_policy =
1342 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001343 config.candidate_network_policy =
1344 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001345 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001346 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001347
1348 const cricket::FakePortAllocatorSession* session =
1349 static_cast<const cricket::FakePortAllocatorSession*>(
1350 port_allocator_->GetPooledSession());
1351 ASSERT_NE(nullptr, session);
1352 EXPECT_EQ(1UL, session->stun_servers().size());
1353 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1354 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001355 EXPECT_LT(0U,
1356 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001357}
1358
deadbeefd21eab32017-07-26 16:50:11 -07001359// Test that network-related RTCConfiguration members are applied to the
1360// PortAllocator when CreatePeerConnection is called. Specifically:
1361// - disable_ipv6_on_wifi
1362// - max_ipv6_networks
1363// - tcp_candidate_policy
1364// - candidate_network_policy
1365// - prune_turn_ports
1366//
1367// Note that the candidate filter (RTCConfiguration::type) is already tested
1368// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001369TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001370 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1371 // Create fake port allocator.
1372 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1373 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1374 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1375
1376 // Create RTCConfiguration with some network-related fields relevant to
1377 // PortAllocator populated.
1378 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001379 config.sdp_semantics = sdp_semantics_;
deadbeefd21eab32017-07-26 16:50:11 -07001380 config.disable_ipv6_on_wifi = true;
1381 config.max_ipv6_networks = 10;
1382 config.tcp_candidate_policy =
1383 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1384 config.candidate_network_policy =
1385 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1386 config.prune_turn_ports = true;
1387
1388 // Create the PC factory and PC with the above config.
1389 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1390 webrtc::CreatePeerConnectionFactory(
1391 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001392 rtc::Thread::Current(), fake_audio_capture_module_,
1393 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001394 webrtc::CreateBuiltinAudioDecoderFactory(),
1395 webrtc::CreateBuiltinVideoEncoderFactory(),
1396 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1397 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001398 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001399 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1400 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001401 EXPECT_TRUE(pc.get());
1402 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001403
1404 // Now validate that the config fields set above were applied to the
1405 // PortAllocator, as flags or otherwise.
1406 EXPECT_FALSE(raw_port_allocator->flags() &
1407 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1408 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1409 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1410 EXPECT_TRUE(raw_port_allocator->flags() &
1411 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001412 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1413 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001414}
1415
deadbeef46c73892016-11-16 19:42:04 -08001416// Check that GetConfiguration returns the configuration the PeerConnection was
1417// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001418TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001419 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01001420 config.sdp_semantics = sdp_semantics_;
deadbeef46c73892016-11-16 19:42:04 -08001421 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001422 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001423
1424 PeerConnectionInterface::RTCConfiguration returned_config =
1425 pc_->GetConfiguration();
1426 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1427}
1428
1429// Check that GetConfiguration returns the last configuration passed into
1430// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001431TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001432 PeerConnectionInterface::RTCConfiguration starting_config;
Henrik Boström62995db2022-01-03 09:58:10 +01001433 starting_config.sdp_semantics = sdp_semantics_;
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001434 starting_config.bundle_policy =
1435 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1436 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001437
Steve Anton36da6ff2018-02-16 16:04:20 -08001438 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001439 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001440 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001441
1442 PeerConnectionInterface::RTCConfiguration returned_config =
1443 pc_->GetConfiguration();
1444 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1445}
1446
Steve Antonc79268f2018-04-24 09:54:10 -07001447TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1448 CreatePeerConnection();
1449
1450 pc_->Close();
1451
1452 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001453 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001454}
1455
Steve Anton36da6ff2018-02-16 16:04:20 -08001456TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001457 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001458 AddVideoStream(kStreamId1);
1459 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 ASSERT_EQ(2u, pc_->local_streams()->count());
1461
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001462 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001463 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001464 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001465 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001466 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001467 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001469 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001470 EXPECT_EQ(3u, pc_->local_streams()->count());
1471
1472 // Remove the third stream.
1473 pc_->RemoveStream(pc_->local_streams()->at(2));
1474 EXPECT_EQ(2u, pc_->local_streams()->count());
1475
1476 // Remove the second stream.
1477 pc_->RemoveStream(pc_->local_streams()->at(1));
1478 EXPECT_EQ(1u, pc_->local_streams()->count());
1479
1480 // Remove the first stream.
1481 pc_->RemoveStream(pc_->local_streams()->at(0));
1482 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483}
1484
deadbeefab9b2d12015-10-14 11:33:11 -07001485// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001486// Don't run under Unified Plan since the stream API is not available.
1487TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001488 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001489 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001490 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001491 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001492
deadbeefab9b2d12015-10-14 11:33:11 -07001493 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001494 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001495 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001496
deadbeefab9b2d12015-10-14 11:33:11 -07001497 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001498 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001499 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001500
1501 // Add another stream and ensure the offer includes both the old and new
1502 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001503 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001504 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001505
Steve Antonb1c1de12017-12-21 15:14:30 -08001506 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001507 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1508 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001509
Steve Antonb1c1de12017-12-21 15:14:30 -08001510 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001511 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1512 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001513}
1514
Steve Anton36da6ff2018-02-16 16:04:20 -08001515// Don't run under Unified Plan since the stream API is not available.
1516TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001517 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001518 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 ASSERT_EQ(1u, pc_->local_streams()->count());
1520 pc_->RemoveStream(pc_->local_streams()->at(0));
1521 EXPECT_EQ(0u, pc_->local_streams()->count());
1522}
1523
deadbeefe1f9d832016-01-14 15:35:42 -08001524// Test for AddTrack and RemoveTrack methods.
1525// Tests that the created offer includes tracks we added,
1526// and that the RtpSenders are created correctly.
1527// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001528// Only tested with Plan B since Unified Plan is covered in more detail by tests
1529// in peerconnection_jsep_unittests.cc
1530TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001531 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001532 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001533 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001534 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001535 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001536 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1537 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001538 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001539 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001540 EXPECT_EQ("audio_track", audio_sender->id());
1541 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001542 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001543 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001544 EXPECT_EQ("video_track", video_sender->id());
1545 EXPECT_EQ(video_track, video_sender->track());
1546
1547 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001548 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001549 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 const cricket::ContentInfo* audio_content =
1552 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001553 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001554 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
1556 const cricket::ContentInfo* video_content =
1557 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001558 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001559 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001560
Steve Antondb45ca82017-09-11 18:27:34 -07001561 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001562
1563 // Now try removing the tracks.
1564 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1565 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1566
1567 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001568 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001569
1570 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001571 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001572 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001573
1574 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001575 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001576 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001577
Steve Antondb45ca82017-09-11 18:27:34 -07001578 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001579
1580 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1581 // should return false.
1582 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1583 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1584}
1585
1586// Test creating senders without a stream specified,
1587// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001588TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001589 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001590 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001591 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001592 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001593 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001594 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001595 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001596 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001597 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001598 EXPECT_EQ("audio_track", audio_sender->id());
1599 EXPECT_EQ(audio_track, audio_sender->track());
1600 EXPECT_EQ("video_track", video_sender->id());
1601 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001602 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1603 // If the ID is truly a random GUID, it should be infinitely unlikely they
1604 // will be the same.
1605 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1606 } else {
1607 // We allows creating tracks without stream ids under Unified Plan
1608 // semantics.
1609 EXPECT_EQ(0u, video_sender->stream_ids().size());
1610 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1611 }
deadbeefe1f9d832016-01-14 15:35:42 -08001612}
1613
Harald Alvestrand89061872018-01-02 14:08:34 +01001614// Test that we can call GetStats() after AddTrack but before connecting
1615// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001616TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001617 CreatePeerConnectionWithoutDtls();
1618 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001619 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001620 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001621 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001622 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1623 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001624 EXPECT_TRUE(DoGetStats(nullptr));
1625}
1626
Steve Anton36da6ff2018-02-16 16:04:20 -08001627TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001628 CreatePeerConnectionWithoutDtls();
1629 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001630 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001631 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001632 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001633 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001634 ASSERT_TRUE(audio_sender.ok());
1635 auto* audio_sender_proxy =
1636 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1637 audio_sender.value().get());
1638 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1639
Harald Alvestrandc72af932018-01-11 17:18:19 +01001640 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001641 ASSERT_TRUE(video_sender.ok());
1642 auto* video_sender_proxy =
1643 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1644 video_sender.value().get());
1645 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001646}
1647
Steve Anton36da6ff2018-02-16 16:04:20 -08001648// Don't run under Unified Plan since the stream API is not available.
1649TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001650 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001651 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001652 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001653 ASSERT_EQ(1u, senders.size());
1654 auto* sender_proxy =
1655 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1656 senders[0].get());
1657 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001658}
1659
Steve Anton36da6ff2018-02-16 16:04:20 -08001660TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001662 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 VerifyRemoteRtpHeaderExtensions();
1664}
1665
Steve Anton36da6ff2018-02-16 16:04:20 -08001666TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001667 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001668 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 CreateOfferAsLocalDescription();
1670 std::string offer;
1671 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1672 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001673 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674}
1675
Steve Anton36da6ff2018-02-16 16:04:20 -08001676TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001677 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001678 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679
1680 CreateOfferAsRemoteDescription();
1681 CreateAnswerAsLocalDescription();
1682
Seth Hampson845e8782018-03-02 11:34:10 -08001683 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
Steve Anton36da6ff2018-02-16 16:04:20 -08001686TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001687 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001688 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689
1690 CreateOfferAsRemoteDescription();
1691 CreatePrAnswerAsLocalDescription();
1692 CreateAnswerAsLocalDescription();
1693
Seth Hampson845e8782018-03-02 11:34:10 -08001694 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695}
1696
Steve Anton36da6ff2018-02-16 16:04:20 -08001697// Don't run under Unified Plan since the stream API is not available.
1698TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 InitiateCall();
1700 ASSERT_EQ(1u, pc_->remote_streams()->count());
1701 pc_->RemoveStream(pc_->local_streams()->at(0));
1702 CreateOfferReceiveAnswer();
1703 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001704 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 CreateOfferReceiveAnswer();
1706}
1707
1708// Tests that after negotiating an audio only call, the respondent can perform a
1709// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001710TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001711 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001712 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 CreateOfferAsRemoteDescription();
1714 CreateAnswerAsLocalDescription();
1715
1716 ASSERT_EQ(1u, pc_->remote_streams()->count());
1717 pc_->RemoveStream(pc_->local_streams()->at(0));
1718 CreateOfferReceiveAnswer();
1719 EXPECT_EQ(0u, pc_->remote_streams()->count());
1720}
1721
1722// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001723TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001724 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725
Steve Antonf1c6db12017-10-13 11:13:35 -07001726 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001728 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001729 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001730 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001731 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
1733 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001734 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001735 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001736 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737
Steve Antonf1c6db12017-10-13 11:13:35 -07001738 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001739 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740
Steve Antonf1c6db12017-10-13 11:13:35 -07001741 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742}
1743
deadbeefab9b2d12015-10-14 11:33:11 -07001744// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001746TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001747 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001749 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001750 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001751 EXPECT_TRUE(offer);
1752 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753
1754 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001755 AddAudioTrack("track_label", {kStreamId1});
1756 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757
1758 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001759 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760
1761 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001762 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001763 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764}
1765
1766// Test that we will get different SSRCs for each tracks in the offer and answer
1767// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001768TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001769 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001771 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1772 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773
1774 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001775 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001776 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 int audio_ssrc = 0;
1778 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001779 EXPECT_TRUE(
1780 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1781 EXPECT_TRUE(
1782 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 EXPECT_NE(audio_ssrc, video_ssrc);
1784
1785 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001786 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001787 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001788 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 audio_ssrc = 0;
1790 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001791 EXPECT_TRUE(
1792 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1793 EXPECT_TRUE(
1794 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 EXPECT_NE(audio_ssrc, video_ssrc);
1796}
1797
deadbeefeb459812015-12-15 19:24:43 -08001798// Test that it's possible to call AddTrack on a MediaStream after adding
1799// the stream to a PeerConnection.
1800// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001801// Don't run under Unified Plan since the stream API is not available.
1802TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001803 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001804 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001805 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001806 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1807
1808 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001809 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001810 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001811 stream->AddTrack(video_track.get());
1812
kwibergd1fe2812016-04-27 06:47:29 -07001813 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001814 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001815
1816 const cricket::MediaContentDescription* video_desc =
1817 cricket::GetFirstVideoContentDescription(offer->description());
1818 EXPECT_TRUE(video_desc != nullptr);
1819}
1820
1821// Test that it's possible to call RemoveTrack on a MediaStream after adding
1822// the stream to a PeerConnection.
1823// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001824// Don't run under Unified Plan since the stream API is not available.
1825TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001826 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001827 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001828 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001829 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1830
1831 // Remove the video track.
1832 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1833
kwibergd1fe2812016-04-27 06:47:29 -07001834 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001835 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001836
1837 const cricket::MediaContentDescription* video_desc =
1838 cricket::GetFirstVideoContentDescription(offer->description());
1839 EXPECT_TRUE(video_desc == nullptr);
1840}
1841
deadbeefbd7d8f72015-12-18 16:58:44 -08001842// Test creating a sender with a stream ID, and ensure the ID is populated
1843// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001844// Don't run under Unified Plan since the stream API is not available.
1845TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001846 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001847 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001848
kwibergd1fe2812016-04-27 06:47:29 -07001849 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001850 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001851
1852 const cricket::MediaContentDescription* video_desc =
1853 cricket::GetFirstVideoContentDescription(offer->description());
1854 ASSERT_TRUE(video_desc != nullptr);
1855 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001856 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001857}
1858
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001860TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001862 ASSERT_LT(0u, pc_->GetSenders().size());
1863 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001864 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001865 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 EXPECT_TRUE(DoGetStats(remote_audio));
1867
1868 // Remove the stream. Since we are sending to our selves the local
1869 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001870 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 // Do a re-negotiation.
1872 CreateOfferReceiveAnswer();
1873
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 // Test that we still can get statistics for the old track. Even if it is not
1875 // sent any longer.
1876 EXPECT_TRUE(DoGetStats(remote_audio));
1877}
1878
1879// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001880TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001882 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1883 ASSERT_TRUE(video_receiver);
1884 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885}
1886
1887// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001888TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001890 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 pc_factory_->CreateAudioTrack("unknown track", NULL));
1892 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1893}
1894
Steve Anton36da6ff2018-02-16 16:04:20 -08001895TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001896 CreatePeerConnectionWithoutDtls();
1897 EXPECT_TRUE(DoGetRTCStats());
1898 // Clearing stats cache is needed now, but should be temporary.
1899 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1900 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001901 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1902 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001903 EXPECT_TRUE(DoGetRTCStats());
1904 pc_->ClearStatsCache();
1905 CreateOfferReceiveAnswer();
1906 EXPECT_TRUE(DoGetRTCStats());
1907}
1908
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909// This tests that a SCTP data channel is returned using different
1910// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001911TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001912 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001913 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914
1915 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001916 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 pc_->CreateDataChannel("1", &config);
1918 EXPECT_TRUE(channel != NULL);
1919 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001920 EXPECT_TRUE(observer_.renegotiation_needed_);
1921 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922
1923 config.ordered = false;
1924 channel = pc_->CreateDataChannel("2", &config);
1925 EXPECT_TRUE(channel != NULL);
1926 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001927 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928
1929 config.ordered = true;
1930 config.maxRetransmits = 0;
1931 channel = pc_->CreateDataChannel("3", &config);
1932 EXPECT_TRUE(channel != NULL);
1933 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001934 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001936 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 config.maxRetransmitTime = 0;
1938 channel = pc_->CreateDataChannel("4", &config);
1939 EXPECT_TRUE(channel != NULL);
1940 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001941 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942}
1943
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001944// For backwards compatibility, we want people who "unset" maxRetransmits
1945// and maxRetransmitTime by setting them to -1 to get what they want.
1946TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1947 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001948 CreatePeerConnection(rtc_config);
1949
1950 webrtc::DataChannelInit config;
1951 config.maxRetransmitTime = -1;
1952 config.maxRetransmits = -1;
1953 rtc::scoped_refptr<DataChannelInterface> channel =
1954 pc_->CreateDataChannel("1", &config);
1955 EXPECT_TRUE(channel != NULL);
1956}
1957
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958// This tests that no data channel is returned if both maxRetransmits and
1959// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001960TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 CreateSctpDataChannelShouldFailForInvalidConfig) {
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 std::string label = "test";
1966 webrtc::DataChannelInit config;
1967 config.maxRetransmits = 0;
1968 config.maxRetransmitTime = 0;
1969
zhihuang9763d562016-08-05 11:14:50 -07001970 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 pc_->CreateDataChannel(label, &config);
1972 EXPECT_TRUE(channel == NULL);
1973}
1974
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975// The test verifies that creating a SCTP data channel with an id already in use
1976// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001977TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001979 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001980 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981
1982 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001983 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001985 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001986 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001987 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 EXPECT_TRUE(channel != NULL);
1989 EXPECT_EQ(1, channel->id());
1990
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 channel = pc_->CreateDataChannel("x", &config);
1992 EXPECT_TRUE(channel == NULL);
1993
1994 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001995 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 channel = pc_->CreateDataChannel("max", &config);
1997 EXPECT_TRUE(channel != NULL);
1998 EXPECT_EQ(config.id, channel->id());
1999
2000 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002001 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 channel = pc_->CreateDataChannel("x", &config);
2003 EXPECT_TRUE(channel == NULL);
2004}
2005
deadbeefab9b2d12015-10-14 11:33:11 -07002006// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002007TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002008 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002009 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002010
2011 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002012 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002013 pc_->CreateDataChannel(label, nullptr);
2014 EXPECT_NE(channel, nullptr);
2015
zhihuang9763d562016-08-05 11:14:50 -07002016 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002017 pc_->CreateDataChannel(label, nullptr);
2018 EXPECT_NE(dup_channel, nullptr);
2019}
2020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002022#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002023// This tests that SCTP data channels can be rejected in an answer.
2024TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2025#else
2026TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2027#endif
2028{
Niels Möllerf06f9232018-08-07 12:32:18 +02002029 RTCConfiguration rtc_config;
2030 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002031
2032 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2033 pc_->CreateDataChannel("offer_channel", NULL));
2034
2035 CreateOfferAsLocalDescription();
2036
2037 // Create an answer where the m-line for data channels are rejected.
2038 std::string sdp;
2039 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2040 std::unique_ptr<SessionDescriptionInterface> answer(
2041 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2042 ASSERT_TRUE(answer);
2043 cricket::ContentInfo* data_info =
2044 cricket::GetFirstDataContent(answer->description());
2045 data_info->rejected = true;
2046
2047 DoSetRemoteDescription(std::move(answer));
2048 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2049}
2050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051// Test that we can create a session description from an SDP string from
2052// FireFox, use it as a remote session description, generate an answer and use
2053// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002054TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002055 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002056 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002057 AddAudioTrack("audio_label");
2058 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002059 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002060 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002061 webrtc::kFireFoxSdpOffer, nullptr));
2062 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 CreateAnswerAsLocalDescription();
2064 ASSERT_TRUE(pc_->local_description() != NULL);
2065 ASSERT_TRUE(pc_->remote_description() != NULL);
2066
2067 const cricket::ContentInfo* content =
2068 cricket::GetFirstAudioContent(pc_->local_description()->description());
2069 ASSERT_TRUE(content != NULL);
2070 EXPECT_FALSE(content->rejected);
2071
2072 content =
2073 cricket::GetFirstVideoContent(pc_->local_description()->description());
2074 ASSERT_TRUE(content != NULL);
2075 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002076#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 content =
2078 cricket::GetFirstDataContent(pc_->local_description()->description());
2079 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002080 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002081#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082}
2083
Harald Alvestrand0d018412021-11-04 13:52:31 +00002084// Test that fallback from DTLS to SDES is not supported.
2085// The fallback was previously supported but was removed to simplify the code
2086// and because it's non-standard.
2087TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002088 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002089 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002090 // Wait for fake certificate to be generated. Previously, this is what caused
2091 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002092 AddAudioTrack("audio_label");
2093 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002094 ASSERT_NE(nullptr, fake_certificate_generator_);
2095 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2096 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002097 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002098 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2099 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002100 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002101}
2102
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103// Test that we can create an audio only offer and receive an answer with a
2104// limited set of audio codecs and receive an updated offer with more audio
2105// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002106TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002107 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002108 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 CreateOfferAsLocalDescription();
2110
Steve Anton36da6ff2018-02-16 16:04:20 -08002111 const char* answer_sdp =
2112 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2113 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002114 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002115 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002116 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117
Steve Anton36da6ff2018-02-16 16:04:20 -08002118 const char* reoffer_sdp =
2119 (sdp_semantics_ == SdpSemantics::kPlanB
2120 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2121 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002122 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002123 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002124 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 CreateAnswerAsLocalDescription();
2126}
2127
deadbeefc80741f2015-10-22 13:14:45 -07002128// Test that if we're receiving (but not sending) a track, subsequent offers
2129// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002130TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002131 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002132 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002133 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002134 CreateAnswerAsLocalDescription();
2135
2136 // At this point we should be receiving stream 1, but not sending anything.
2137 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002138 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002139 DoCreateOffer(&offer, nullptr);
2140
2141 const cricket::ContentInfo* video_content =
2142 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002143 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2144 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002145
2146 const cricket::ContentInfo* audio_content =
2147 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002148 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2149 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002150}
2151
2152// Test that if we're receiving (but not sending) a track, and the
2153// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2154// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002155TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002156 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002157 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002158 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002159 CreateAnswerAsLocalDescription();
2160
2161 // At this point we should be receiving stream 1, but not sending anything.
2162 // A new offer would be recvonly, but we'll set the "no receive" constraints
2163 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002164 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002165 RTCOfferAnswerOptions options;
2166 options.offer_to_receive_audio = 0;
2167 options.offer_to_receive_video = 0;
2168 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002169
2170 const cricket::ContentInfo* video_content =
2171 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002172 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2173 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002174
2175 const cricket::ContentInfo* audio_content =
2176 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002177 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2178 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002179}
2180
deadbeef653b8e02015-11-11 12:55:10 -08002181// Test that we can use SetConfiguration to change the ICE servers of the
2182// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002183TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002184 CreatePeerConnection();
2185
Steve Anton36da6ff2018-02-16 16:04:20 -08002186 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002187 PeerConnectionInterface::IceServer server;
2188 server.uri = "stun:test_hostname";
2189 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002190 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002191
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002192 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2193 EXPECT_EQ("test_hostname",
2194 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002195}
2196
Steve Anton36da6ff2018-02-16 16:04:20 -08002197TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002198 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002199 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002200 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002201 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002202 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2203}
2204
Steve Anton36da6ff2018-02-16 16:04:20 -08002205TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002206 PeerConnectionInterface::RTCConfiguration config;
2207 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002208 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002209 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002210 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002211
2212 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002213 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002214 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2215 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002216}
2217
skvladd1f5fda2017-02-03 16:54:05 -08002218// Test that the ice check interval can be changed. This does not verify that
2219// the setting makes it all the way to P2PTransportChannel, as that would
2220// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002221TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002222 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002223 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002224 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002225 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002226 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002227 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002228 config = pc_->GetConfiguration();
2229 EXPECT_EQ(config.ice_check_min_interval, 100);
2230}
2231
2232TEST_P(PeerConnectionInterfaceTest,
2233 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2234 PeerConnectionInterface::RTCConfiguration config;
2235 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2236 CreatePeerConnection(config);
2237 config = pc_->GetConfiguration();
2238 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2239
2240 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002241 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002242 config = pc_->GetConfiguration();
2243 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002244}
2245
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002246// Test that when SetConfiguration changes both the pool size and other
2247// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002248TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002249 SetConfigurationCreatesPooledSessionCorrectly) {
2250 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002251 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002252 config.ice_candidate_pool_size = 1;
2253 PeerConnectionInterface::IceServer server;
2254 server.uri = kStunAddressOnly;
2255 config.servers.push_back(server);
2256 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002257 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002258
2259 const cricket::FakePortAllocatorSession* session =
2260 static_cast<const cricket::FakePortAllocatorSession*>(
2261 port_allocator_->GetPooledSession());
2262 ASSERT_NE(nullptr, session);
2263 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002264}
2265
deadbeef293e9262017-01-11 12:28:30 -08002266// Test that after SetLocalDescription, changing the pool size is not allowed,
2267// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002268TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002269 CantChangePoolSizeAfterSetLocalDescription) {
2270 CreatePeerConnection();
2271 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002272 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002273 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002274 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002275
2276 // Set remote offer; can still change pool size at this point.
2277 CreateOfferAsRemoteDescription();
2278 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002279 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002280
2281 // Set local answer; now it's too late.
2282 CreateAnswerAsLocalDescription();
2283 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002284 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002285 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2286}
2287
deadbeef42a42632017-03-10 15:18:00 -08002288// Test that after setting an answer, extra pooled sessions are discarded. The
2289// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002290TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002291 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2292 CreatePeerConnection();
2293
2294 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002295 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002296 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002297 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002298
2299 // Do offer/answer.
2300 CreateOfferAsRemoteDescription();
2301 CreateAnswerAsLocalDescription();
2302
2303 // Expect no pooled sessions to be left.
2304 const cricket::PortAllocatorSession* session =
2305 port_allocator_->GetPooledSession();
2306 EXPECT_EQ(nullptr, session);
2307}
2308
2309// After Close is called, pooled candidates should be discarded so as to not
2310// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002311TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002312 CreatePeerConnection();
2313
Steve Anton36da6ff2018-02-16 16:04:20 -08002314 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002315 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002316 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002317 pc_->Close();
2318
2319 // Expect no pooled sessions to be left.
2320 const cricket::PortAllocatorSession* session =
2321 port_allocator_->GetPooledSession();
2322 EXPECT_EQ(nullptr, session);
2323}
2324
deadbeef293e9262017-01-11 12:28:30 -08002325// Test that SetConfiguration returns an invalid modification error if
2326// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002327TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002328 SetConfigurationReturnsInvalidModificationError) {
2329 PeerConnectionInterface::RTCConfiguration config;
2330 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2331 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2332 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002333 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002334
Steve Anton36da6ff2018-02-16 16:04:20 -08002335 PeerConnectionInterface::RTCConfiguration modified_config =
2336 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002337 modified_config.bundle_policy =
2338 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002339 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002340 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2341
Steve Anton36da6ff2018-02-16 16:04:20 -08002342 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002343 modified_config.rtcp_mux_policy =
2344 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002345 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002346 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2347
Steve Anton36da6ff2018-02-16 16:04:20 -08002348 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002349 modified_config.continual_gathering_policy =
2350 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002351 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002352 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2353}
2354
2355// Test that SetConfiguration returns a range error if the candidate pool size
2356// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002357TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002358 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2359 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002360 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002361 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002362
2363 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002364 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002365 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2366
2367 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002368 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002369 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2370}
2371
2372// Test that SetConfiguration returns a syntax error if parsing an ICE server
2373// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002374TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002375 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2376 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002377 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002378 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002379
2380 PeerConnectionInterface::IceServer bad_server;
2381 bad_server.uri = "stunn:www.example.com";
2382 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002383 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002384 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2385}
2386
2387// Test that SetConfiguration returns an invalid parameter error if a TURN
2388// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002389TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002390 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2391 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002392 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002393 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002394
2395 PeerConnectionInterface::IceServer bad_server;
2396 bad_server.uri = "turn:www.example.com";
2397 // Missing password.
2398 bad_server.username = "foo";
2399 config.servers.push_back(bad_server);
2400 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002401 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2402 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002403}
2404
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002405// Test that PeerConnection::Close changes the states to closed and all remote
2406// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002407TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 // Initialize a PeerConnection and negotiate local and remote session
2409 // description.
2410 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002411
2412 // With Plan B, verify the stream count. The analog with Unified Plan is the
2413 // RtpTransceiver count.
2414 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2415 ASSERT_EQ(1u, pc_->local_streams()->count());
2416 ASSERT_EQ(1u, pc_->remote_streams()->count());
2417 } else {
2418 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2419 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420
2421 pc_->Close();
2422
2423 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2424 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2425 pc_->ice_connection_state());
2426 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2427 pc_->ice_gathering_state());
2428
Steve Anton36da6ff2018-02-16 16:04:20 -08002429 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2430 EXPECT_EQ(1u, pc_->local_streams()->count());
2431 EXPECT_EQ(1u, pc_->remote_streams()->count());
2432 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002433 // Verify that the RtpTransceivers are still returned.
2434 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002435 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436
Steve Anton36da6ff2018-02-16 16:04:20 -08002437 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002438 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002439 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2440 ASSERT_TRUE(audio_receiver);
2441 ASSERT_TRUE(video_receiver);
2442 // Track state may be updated asynchronously.
2443 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2444 audio_receiver->track()->state(), kTimeout);
2445 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2446 video_receiver->track()->state(), kTimeout);
2447 } else {
2448 ASSERT_FALSE(audio_receiver);
2449 ASSERT_FALSE(video_receiver);
2450 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451}
2452
2453// Test that PeerConnection methods fails gracefully after
2454// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002455// Don't run under Unified Plan since the stream API is not available.
2456TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002457 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002458 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 CreateOfferAsRemoteDescription();
2460 CreateAnswerAsLocalDescription();
2461
2462 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002463 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2464 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465
2466 pc_->Close();
2467
2468 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002469 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2472
2473 EXPECT_TRUE(pc_->local_description() != NULL);
2474 EXPECT_TRUE(pc_->remote_description() != NULL);
2475
kwibergd1fe2812016-04-27 06:47:29 -07002476 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002477 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002478 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002479 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480
2481 std::string sdp;
2482 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002483 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002484 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002485 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486
2487 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002488 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002489 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002490 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491}
2492
2493// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002494TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495 InitiateCall();
2496 pc_->Close();
2497 DoGetStats(NULL);
2498}
deadbeefab9b2d12015-10-14 11:33:11 -07002499
2500// NOTE: The series of tests below come from what used to be
2501// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2502// setting a remote or local description has the expected effects.
2503
2504// This test verifies that the remote MediaStreams corresponding to a received
2505// SDP string is created. In this test the two separate MediaStreams are
2506// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002507TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002508 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002509 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002510 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002511
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002512 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002513 EXPECT_TRUE(
2514 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2515 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2516 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2517
2518 // Create a session description based on another SDP with another
2519 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002520 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002521
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002522 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002523 EXPECT_TRUE(
2524 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2525}
2526
2527// This test verifies that when remote tracks are added/removed from SDP, the
2528// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002529// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2530// specific behavior.
2531TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002532 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002533 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002534 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002535 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002536 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002537 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002538 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2539 reference_collection_));
2540
2541 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002542 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002543 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002544 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002545 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2546 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002547 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002548 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2549 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002550 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002551 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2552 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002553
2554 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002555 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002556 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002557 MockTrackObserver audio_track_observer(audio_track2);
2558 MockTrackObserver video_track_observer(video_track2);
2559
2560 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2561 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002562 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002563 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2564 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002565 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002566 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002567 audio_track2->state(), kTimeout);
2568 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2569 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002570}
2571
2572// This tests that remote tracks are ended if a local session description is set
2573// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002574TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002575 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002576 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002577 // First create and set a remote offer, then reject its video content in our
2578 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002579 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2580 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2581 ASSERT_TRUE(audio_receiver);
2582 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2583 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002584
Steve Anton36da6ff2018-02-16 16:04:20 -08002585 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2586 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002587 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002588 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2589 video_receiver->track();
2590 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002591
kwibergd1fe2812016-04-27 06:47:29 -07002592 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002593 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002594 cricket::ContentInfo* video_info =
2595 local_answer->description()->GetContentByName("video");
2596 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002597 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002598 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2599 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002600
2601 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002602 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002603 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002604 video_info = local_offer->description()->GetContentByName("video");
2605 ASSERT_TRUE(video_info != nullptr);
2606 video_info->rejected = true;
2607 cricket::ContentInfo* audio_info =
2608 local_offer->description()->GetContentByName("audio");
2609 ASSERT_TRUE(audio_info != nullptr);
2610 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002611 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002612 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002613 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2614 kTimeout);
2615 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2616 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002617}
2618
2619// This tests that we won't crash if the remote track has been removed outside
2620// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002621// Don't run under Unified Plan since the stream API is not available.
2622TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002623 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002624 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002625 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002626 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2627 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2628 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2629
kwibergd1fe2812016-04-27 06:47:29 -07002630 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002631 webrtc::CreateSessionDescription(SdpType::kAnswer,
2632 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002633 cricket::ContentInfo* video_info =
2634 local_answer->description()->GetContentByName("video");
2635 video_info->rejected = true;
2636 cricket::ContentInfo* audio_info =
2637 local_answer->description()->GetContentByName("audio");
2638 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002639 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002640
2641 // No crash is a pass.
2642}
2643
deadbeef5e97fb52015-10-15 12:49:08 -07002644// This tests that if a recvonly remote description is set, no remote streams
2645// will be created, even if the description contains SSRCs/MSIDs.
2646// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002647TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002648 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002649 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002650
Steve Anton36da6ff2018-02-16 16:04:20 -08002651 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002652 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002653 CreateAndSetRemoteOffer(recvonly_offer);
2654
2655 EXPECT_EQ(0u, observer_.remote_streams()->count());
2656}
2657
deadbeefab9b2d12015-10-14 11:33:11 -07002658// This tests that a default MediaStream is created if a remote session
2659// description doesn't contain any streams and no MSID support.
2660// It also tests that the default stream is updated if a video m-line is added
2661// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002662// Don't run under Unified Plan since this behavior is Plan B specific.
2663TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002664 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002665 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002666 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2667
2668 ASSERT_EQ(1u, observer_.remote_streams()->count());
2669 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2670
2671 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2672 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002673 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002674
2675 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2676 ASSERT_EQ(1u, observer_.remote_streams()->count());
2677 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2678 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002679 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2680 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002681 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2682 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002683 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2684 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002685}
2686
2687// This tests that a default MediaStream is created if a remote session
2688// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002689// Don't run under Unified Plan since this behavior is Plan B specific.
2690TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002691 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002692 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002693 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002694 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2695
2696 ASSERT_EQ(1u, observer_.remote_streams()->count());
2697 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2698
2699 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2700 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002701 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002702}
2703
2704// This tests that it won't crash when PeerConnection tries to remove
2705// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002706// Don't run under Unified Plan since this behavior is Plan B specific.
2707TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002708 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002709 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002710 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002711 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2712 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2713 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2714
2715 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2716
2717 // No crash is a pass.
2718}
2719
2720// This tests that a default MediaStream is created if the remote session
2721// description doesn't contain any streams and don't contain an indication if
2722// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002723// Don't run under Unified Plan since this behavior is Plan B specific.
2724TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002725 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002726 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002727 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002728 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2729
2730 ASSERT_EQ(1u, observer_.remote_streams()->count());
2731 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2732 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2733 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2734}
2735
2736// This tests that a default MediaStream is not created if the remote session
2737// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002738// Don't run under Unified Plan since this behavior is Plan B specific.
2739TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002740 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002741 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002742 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2743 EXPECT_EQ(0u, observer_.remote_streams()->count());
2744}
2745
deadbeefbda7e0b2015-12-08 17:13:40 -08002746// This tests that when setting a new description, the old default tracks are
2747// not destroyed and recreated.
2748// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002749// Don't run under Unified Plan since this behavior is Plan B specific.
2750TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002751 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002752 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002753 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002754 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2755
2756 ASSERT_EQ(1u, observer_.remote_streams()->count());
2757 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2758 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2759
2760 // Set the track to "disabled", then set a new description and ensure the
2761 // track is still disabled, which ensures it hasn't been recreated.
2762 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2763 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2764 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2765 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2766}
2767
deadbeefab9b2d12015-10-14 11:33:11 -07002768// This tests that a default MediaStream is not created if a remote session
2769// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002770// Don't run under Unified Plan since this behavior is Plan B specific.
2771TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002772 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002773 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002774 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002775 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002776 EXPECT_TRUE(
2777 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2778
2779 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2780 EXPECT_EQ(0u, observer_.remote_streams()->count());
2781}
2782
Seth Hampson5897a6e2018-04-03 11:16:33 -07002783// This tests that a default MediaStream is created if a remote SDP comes from
2784// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2785TEST_F(PeerConnectionInterfaceTestPlanB,
2786 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002787 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002788 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002789 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2790 // Add a=msid lines to simulate a Unified Plan endpoint that only
2791 // signals stream IDs with a=msid lines.
2792 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2793
2794 CreateAndSetRemoteOffer(sdp_string);
2795
2796 ASSERT_EQ(1u, observer_.remote_streams()->count());
2797 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2798 EXPECT_EQ("default", remote_stream->id());
2799 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2800}
2801
Seth Hampson5b4f0752018-04-02 16:31:36 -07002802// This tests that when a Plan B endpoint receives an SDP that signals no media
2803// stream IDs indicated by the special character "-" in the a=msid line, that
2804// a default stream ID will be used for the MediaStream ID. This can occur
2805// when a Unified Plan endpoint signals no media stream IDs, but signals both
2806// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2807TEST_F(PeerConnectionInterfaceTestPlanB,
2808 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002809 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002810 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002811 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2812 // the sender's stream ID will be interpreted as no stream IDs.
2813 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2814 sdp_string.append("a=msid:- audiotrack0\n");
2815
2816 CreateAndSetRemoteOffer(sdp_string);
2817
2818 ASSERT_EQ(1u, observer_.remote_streams()->count());
2819 // Because SSRCs are signaled the track ID will be what was signaled in the
2820 // a=msid line.
2821 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2822 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2823 EXPECT_EQ("default", remote_stream->id());
2824 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002825
2826 // Previously a bug ocurred when setting the remote description a second time.
2827 // This is because we checked equality of the remote StreamParams stream ID
2828 // (empty), and the previously set stream ID for the remote sender
2829 // ("default"). This cause a track to be removed, then added, when really
2830 // nothing should occur because it is the same track.
2831 CreateAndSetRemoteOffer(sdp_string);
2832 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2833 EXPECT_EQ(1u, observer_.add_track_events_.size());
2834 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2835 remote_stream = observer_.remote_streams()->at(0);
2836 EXPECT_EQ("default", remote_stream->id());
2837 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002838}
2839
deadbeefab9b2d12015-10-14 11:33:11 -07002840// This tests that an RtpSender is created when the local description is set
2841// after adding a local stream.
2842// TODO(deadbeef): This test and the one below it need to be updated when
2843// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002844// Don't run under Unified Plan since this behavior is Plan B specific.
2845TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002846 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002847 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002848
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002849 // Create an offer with 1 stream with 2 tracks of each type.
2850 rtc::scoped_refptr<StreamCollection> stream_collection =
2851 CreateStreamCollection(1, 2);
2852 pc_->AddStream(stream_collection->at(0));
2853 std::unique_ptr<SessionDescriptionInterface> offer;
2854 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002855 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002856
deadbeefab9b2d12015-10-14 11:33:11 -07002857 auto senders = pc_->GetSenders();
2858 EXPECT_EQ(4u, senders.size());
2859 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2860 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2861 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2862 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2863
2864 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002865 pc_->RemoveStream(stream_collection->at(0));
2866 stream_collection = CreateStreamCollection(1, 1);
2867 pc_->AddStream(stream_collection->at(0));
2868 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002869 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002870
deadbeefab9b2d12015-10-14 11:33:11 -07002871 senders = pc_->GetSenders();
2872 EXPECT_EQ(2u, senders.size());
2873 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2874 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2875 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2876 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2877}
2878
2879// This tests that an RtpSender is created when the local description is set
2880// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002881// Don't run under Unified Plan since this behavior is Plan B specific.
2882TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002883 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002884 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002885 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002886
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002887 rtc::scoped_refptr<StreamCollection> stream_collection =
2888 CreateStreamCollection(1, 2);
2889 // Add a stream to create the offer, but remove it afterwards.
2890 pc_->AddStream(stream_collection->at(0));
2891 std::unique_ptr<SessionDescriptionInterface> offer;
2892 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2893 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002894
Steve Antondb45ca82017-09-11 18:27:34 -07002895 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002896 auto senders = pc_->GetSenders();
2897 EXPECT_EQ(0u, senders.size());
2898
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002899 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002900 senders = pc_->GetSenders();
2901 EXPECT_EQ(4u, senders.size());
2902 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2903 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2904 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2905 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2906}
2907
2908// This tests that the expected behavior occurs if the SSRC on a local track is
2909// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002910TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002911 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002912 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002913 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002914
Steve Anton36da6ff2018-02-16 16:04:20 -08002915 AddAudioTrack(kAudioTracks[0]);
2916 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002917 std::unique_ptr<SessionDescriptionInterface> offer;
2918 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2919 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002920 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2921 webrtc::CreateSessionDescription(
2922 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002923 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002924 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002925
deadbeefab9b2d12015-10-14 11:33:11 -07002926 auto senders = pc_->GetSenders();
2927 EXPECT_EQ(2u, senders.size());
2928 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2929 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2930
2931 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002932 cricket::MediaContentDescription* desc =
2933 cricket::GetFirstAudioContentDescription(modified_offer->description());
2934 ASSERT_TRUE(desc != NULL);
2935 for (StreamParams& stream : desc->mutable_streams()) {
2936 for (unsigned int& ssrc : stream.ssrcs) {
2937 ++ssrc;
2938 }
2939 }
deadbeefab9b2d12015-10-14 11:33:11 -07002940
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002941 desc =
2942 cricket::GetFirstVideoContentDescription(modified_offer->description());
2943 ASSERT_TRUE(desc != NULL);
2944 for (StreamParams& stream : desc->mutable_streams()) {
2945 for (unsigned int& ssrc : stream.ssrcs) {
2946 ++ssrc;
2947 }
2948 }
2949
Steve Antondb45ca82017-09-11 18:27:34 -07002950 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002951 senders = pc_->GetSenders();
2952 EXPECT_EQ(2u, senders.size());
2953 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2954 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2955 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2956 // changed.
2957}
2958
2959// This tests that the expected behavior occurs if a new session description is
2960// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002961// Don't run under Unified Plan since the stream API is not available.
2962TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002963 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002964 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002965 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002966
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002967 rtc::scoped_refptr<StreamCollection> stream_collection =
2968 CreateStreamCollection(2, 1);
2969 pc_->AddStream(stream_collection->at(0));
2970 std::unique_ptr<SessionDescriptionInterface> offer;
2971 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002972 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002973
deadbeefab9b2d12015-10-14 11:33:11 -07002974 auto senders = pc_->GetSenders();
2975 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2977 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002978
2979 // Add a new MediaStream but with the same tracks as in the first stream.
2980 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2981 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002982 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2983 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002984 pc_->AddStream(stream_1);
2985
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002987 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002988
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002989 auto new_senders = pc_->GetSenders();
2990 // Should be the same senders as before, but with updated stream id.
2991 // Note that this behavior is subject to change in the future.
2992 // We may decide the PC should ignore existing tracks in AddStream.
2993 EXPECT_EQ(senders, new_senders);
2994 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2995 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002996}
2997
zhihuang81c3a032016-11-17 12:06:24 -08002998// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002999TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003000 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003001 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003002 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3003 EXPECT_EQ(observer_.num_added_tracks_, 1);
3004 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3005
3006 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003007 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003008 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003009 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3010}
3011
deadbeefd1a38b52016-12-10 13:15:33 -08003012// Test that when SetConfiguration is called and the configuration is
3013// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003014TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003015 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003016 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003017 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003018 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003019 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003020 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3021 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003022
3023 // Do initial offer/answer so there's something to restart.
3024 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003025 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003026
3027 // Grab the ufrags.
3028 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3029
3030 // Change ICE policy, which should trigger an ICE restart on the next offer.
3031 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003032 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003033 CreateOfferAsLocalDescription();
3034
3035 // Grab the new ufrags.
3036 std::vector<std::string> subsequent_ufrags =
3037 GetUfrags(pc_->local_description());
3038
3039 // Sanity check.
3040 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3041 // Check that each ufrag is different.
3042 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3043 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3044 }
3045}
3046
3047// Test that when SetConfiguration is called and the configuration *isn't*
3048// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003049TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003050 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003051 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003052 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003053 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003054 config = pc_->GetConfiguration();
3055 AddAudioTrack(kAudioTracks[0]);
3056 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003057
3058 // Do initial offer/answer so there's something to restart.
3059 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003060 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003061
3062 // Grab the ufrags.
3063 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3064
3065 // Call SetConfiguration with a config identical to what the PC was
3066 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003067 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003068 CreateOfferAsLocalDescription();
3069
3070 // Grab the new ufrags.
3071 std::vector<std::string> subsequent_ufrags =
3072 GetUfrags(pc_->local_description());
3073
3074 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3075}
3076
3077// Test for a weird corner case scenario:
3078// 1. Audio/video session established.
3079// 2. SetConfiguration changes ICE config; ICE restart needed.
3080// 3. ICE restart initiated by remote peer, but only for one m= section.
3081// 4. Next createOffer should initiate an ICE restart, but only for the other
3082// m= section; it would be pointless to do an ICE restart for the m= section
3083// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003084TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003085 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003086 config.sdp_semantics = sdp_semantics_;
deadbeefd1a38b52016-12-10 13:15:33 -08003087 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003088 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003089 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003090 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3091 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003092
3093 // Do initial offer/answer so there's something to restart.
3094 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003095 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003096
3097 // Change ICE policy, which should set the "needs-ice-restart" flag.
3098 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003099 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003100
3101 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003102 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003103 webrtc::CreateSessionDescription(SdpType::kOffer,
3104 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003105 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003106 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3107 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003108 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003109 CreateAnswerAsLocalDescription();
3110
3111 // Grab the ufrags.
3112 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003113 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003114
3115 // Create offer and grab the new ufrags.
3116 CreateOfferAsLocalDescription();
3117 std::vector<std::string> subsequent_ufrags =
3118 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003119 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003120
3121 // Ensure that only the ufrag for the second m= section changed.
3122 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3123 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3124}
3125
deadbeeffe4a8a42016-12-20 17:56:17 -08003126// Tests that the methods to return current/pending descriptions work as
3127// expected at different points in the offer/answer exchange. This test does
3128// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003129TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003130 // This disables DTLS so we can apply an answer to ourselves.
3131 CreatePeerConnection();
3132
3133 // Create initial local offer and get SDP (which will also be used as
3134 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003135 std::unique_ptr<SessionDescriptionInterface> local_offer;
3136 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003137 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003138 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003139
3140 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003141 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3142 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3143 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003144 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3145 EXPECT_EQ(nullptr, pc_->current_local_description());
3146 EXPECT_EQ(nullptr, pc_->current_remote_description());
3147
3148 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003149 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003150 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003151 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3152 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3153 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3154 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003155 EXPECT_EQ(nullptr, pc_->current_local_description());
3156 EXPECT_EQ(nullptr, pc_->current_remote_description());
3157
3158 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003159 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003160 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003161 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3162 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003163 EXPECT_EQ(nullptr, pc_->pending_local_description());
3164 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003165 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3166 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003167
3168 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003169 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003170 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003171 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3172 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3173 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003174 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003175 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3176 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003177
3178 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003179 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003180 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003181 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3182 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3183 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3184 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3185 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3186 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003187
3188 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003189 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003190 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003191 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3192 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003193 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3194 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003195 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3196 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003197}
3198
zhihuang77985012017-02-07 15:45:16 -08003199// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3200// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003201// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003202// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003203TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003204 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3205 CreatePeerConnection();
3206 // The RtcEventLog will be reset when the PeerConnection is closed.
3207 pc_->Close();
3208
Niels Möllerdec9f742019-06-03 15:25:20 +02003209 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003210 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003211 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003212 pc_->StopRtcEventLog();
3213}
3214
deadbeef30952b42017-04-21 02:41:29 -07003215// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003216TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003217 CreatePeerConnection();
3218
3219 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003220 RTCOfferAnswerOptions options;
3221 options.offer_to_receive_audio = 1;
3222 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003223 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003224 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003225 cricket::SessionDescription* desc = offer->description();
3226 ASSERT_EQ(2u, desc->transport_infos().size());
3227 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3228 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3229
3230 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003231 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003232 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003233 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3234 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003235 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003236 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003237 desc = answer->description();
3238 ASSERT_EQ(2u, desc->transport_infos().size());
3239 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3240 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3241}
3242
deadbeef1dcb1642017-03-29 21:08:16 -07003243// Test that ICE renomination isn't offered if it's not enabled in the PC's
3244// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003245TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003246 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003247 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003248 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003249 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003250 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003251
3252 std::unique_ptr<SessionDescriptionInterface> offer;
3253 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3254 cricket::SessionDescription* desc = offer->description();
3255 EXPECT_EQ(1u, desc->transport_infos().size());
3256 EXPECT_FALSE(
3257 desc->transport_infos()[0].description.GetIceParameters().renomination);
3258}
3259
3260// Test that the ICE renomination option is present in generated offers/answers
3261// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003262TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003263 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003264 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003265 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003266 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003267 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003268
3269 std::unique_ptr<SessionDescriptionInterface> offer;
3270 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3271 cricket::SessionDescription* desc = offer->description();
3272 EXPECT_EQ(1u, desc->transport_infos().size());
3273 EXPECT_TRUE(
3274 desc->transport_infos()[0].description.GetIceParameters().renomination);
3275
3276 // Set the offer as a remote description, then create an answer and ensure it
3277 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003278 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003279 std::unique_ptr<SessionDescriptionInterface> answer;
3280 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3281 desc = answer->description();
3282 EXPECT_EQ(1u, desc->transport_infos().size());
3283 EXPECT_TRUE(
3284 desc->transport_infos()[0].description.GetIceParameters().renomination);
3285}
3286
3287// Test that if CreateOffer is called with the deprecated "offer to receive
3288// audio/video" constraints, they're processed and result in an offer with
3289// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003290TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003291 CreatePeerConnection();
3292
Niels Möllerf06f9232018-08-07 12:32:18 +02003293 RTCOfferAnswerOptions options;
3294 options.offer_to_receive_audio = 1;
3295 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003296 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003297 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003298
3299 cricket::SessionDescription* desc = offer->description();
3300 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3301 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3302 ASSERT_NE(nullptr, audio);
3303 ASSERT_NE(nullptr, video);
3304 EXPECT_FALSE(audio->rejected);
3305 EXPECT_FALSE(video->rejected);
3306}
3307
3308// Test that if CreateAnswer is called with the deprecated "offer to receive
3309// audio/video" constraints, they're processed and can be used to reject an
3310// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003311// Don't run under Unified Plan since this behavior is not supported.
3312TEST_F(PeerConnectionInterfaceTestPlanB,
3313 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003314 CreatePeerConnection();
3315
3316 // First, create an offer with audio/video and apply it as a remote
3317 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003318 RTCOfferAnswerOptions options;
3319 options.offer_to_receive_audio = 1;
3320 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003321 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003322 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003323 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003324
3325 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003326 options.offer_to_receive_audio = 0;
3327 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003328 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003329 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003330
3331 cricket::SessionDescription* desc = answer->description();
3332 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3333 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3334 ASSERT_NE(nullptr, audio);
3335 ASSERT_NE(nullptr, video);
3336 EXPECT_TRUE(audio->rejected);
3337 EXPECT_TRUE(video->rejected);
3338}
3339
deadbeef1dcb1642017-03-29 21:08:16 -07003340// Test that negotiation can succeed with a data channel only, and with the max
3341// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003342#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003343TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3344#else
3345TEST_P(PeerConnectionInterfaceTest,
3346 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003347#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003348 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003349 config.sdp_semantics = sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07003350 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003351 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003352
3353 // First, create an offer with only a data channel and apply it as a remote
3354 // description.
3355 pc_->CreateDataChannel("test", nullptr);
3356 std::unique_ptr<SessionDescriptionInterface> offer;
3357 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003358 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003359
3360 // Create and set answer as well.
3361 std::unique_ptr<SessionDescriptionInterface> answer;
3362 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003363 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003364}
3365
Steve Anton36da6ff2018-02-16 16:04:20 -08003366TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003367 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003368 BitrateSettings bitrate;
3369 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003370 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3371}
3372
Steve Anton36da6ff2018-02-16 16:04:20 -08003373TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003374 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003375 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003376 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003377 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3378}
3379
Steve Anton36da6ff2018-02-16 16:04:20 -08003380TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003381 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003382 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003383 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003384 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003385 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3386}
3387
Steve Anton36da6ff2018-02-16 16:04:20 -08003388TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003389 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003390 BitrateSettings bitrate;
3391 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003392 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3393}
3394
Steve Anton36da6ff2018-02-16 16:04:20 -08003395TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003396 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003397 BitrateSettings bitrate;
3398 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003399 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003400 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3401}
3402
Steve Anton36da6ff2018-02-16 16:04:20 -08003403TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003404 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003405 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003406 bitrate.min_bitrate_bps = 10;
3407 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003408 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3409}
3410
Steve Anton36da6ff2018-02-16 16:04:20 -08003411TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003412 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003413 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003414 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003415 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3416}
3417
Niels Möller0c4f7be2018-05-07 14:01:37 +02003418// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003419// by Call's BitrateConstraints, which comes from the SDP or a default value.
3420// This test checks that a call to SetBitrate with a current bitrate that will
3421// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003422TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003423 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003424 BitrateSettings bitrate;
3425 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003426 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3427}
3428
zhihuang1c378ed2017-08-17 14:10:50 -07003429// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003430TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003431 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3432 RTCOfferAnswerOptions rtc_options;
3433
3434 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3435 // than kMaxOfferToReceiveMedia should be treated as invalid.
3436 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3437 CreatePeerConnection();
3438 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3439
3440 rtc_options.offer_to_receive_audio =
3441 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3442 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3443}
3444
Steve Anton36da6ff2018-02-16 16:04:20 -08003445TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003446 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3447 RTCOfferAnswerOptions rtc_options;
3448
3449 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3450 // than kMaxOfferToReceiveMedia should be treated as invalid.
3451 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3452 CreatePeerConnection();
3453 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3454
3455 rtc_options.offer_to_receive_video =
3456 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3457 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3458}
3459
3460// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003461// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003462TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003463 RTCOfferAnswerOptions rtc_options;
3464 rtc_options.offer_to_receive_audio = 1;
3465 rtc_options.offer_to_receive_video = 1;
3466
3467 std::unique_ptr<SessionDescriptionInterface> offer;
3468 CreatePeerConnection();
3469 offer = CreateOfferWithOptions(rtc_options);
3470 ASSERT_TRUE(offer);
3471 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3472 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3473}
3474
3475// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003476// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003477TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003478 RTCOfferAnswerOptions rtc_options;
3479 rtc_options.offer_to_receive_audio = 1;
3480 rtc_options.offer_to_receive_video = 0;
3481
3482 std::unique_ptr<SessionDescriptionInterface> offer;
3483 CreatePeerConnection();
3484 offer = CreateOfferWithOptions(rtc_options);
3485 ASSERT_TRUE(offer);
3486 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3487 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3488}
3489
Artem Titov880fa812021-07-30 22:30:23 +02003490// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003491// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003492TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003493 RTCOfferAnswerOptions rtc_options;
3494 rtc_options.offer_to_receive_audio = 0;
3495 rtc_options.offer_to_receive_video = 1;
3496
3497 std::unique_ptr<SessionDescriptionInterface> offer;
3498 CreatePeerConnection();
3499 offer = CreateOfferWithOptions(rtc_options);
3500 ASSERT_TRUE(offer);
3501 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3502 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3503}
3504
zhihuang1c378ed2017-08-17 14:10:50 -07003505// Test that no media content will be added to the offer if using default
3506// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003507TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003508 RTCOfferAnswerOptions rtc_options;
3509
3510 std::unique_ptr<SessionDescriptionInterface> offer;
3511 CreatePeerConnection();
3512 offer = CreateOfferWithOptions(rtc_options);
3513 ASSERT_TRUE(offer);
3514 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3515 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3516}
3517
Artem Titov880fa812021-07-30 22:30:23 +02003518// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003519// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003520TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3521 CreatePeerConnection();
3522
zhihuang1c378ed2017-08-17 14:10:50 -07003523 RTCOfferAnswerOptions rtc_options;
3524 rtc_options.ice_restart = false;
3525 rtc_options.offer_to_receive_audio = 1;
3526
3527 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003528 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003529 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3530 auto ufrag1 =
3531 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3532 auto pwd1 =
3533 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003534
Artem Titov880fa812021-07-30 22:30:23 +02003535 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003536 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003537 auto ufrag2 =
3538 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3539 auto pwd2 =
3540 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003541
Artem Titov880fa812021-07-30 22:30:23 +02003542 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003543 rtc_options.ice_restart = true;
3544 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003545 auto ufrag3 =
3546 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3547 auto pwd3 =
3548 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003549
3550 EXPECT_EQ(ufrag1, ufrag2);
3551 EXPECT_EQ(pwd1, pwd2);
3552 EXPECT_NE(ufrag2, ufrag3);
3553 EXPECT_NE(pwd2, pwd3);
3554}
3555
Artem Titov880fa812021-07-30 22:30:23 +02003556// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003557// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003558TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003559 RTCOfferAnswerOptions rtc_options;
3560 rtc_options.offer_to_receive_audio = 1;
3561 rtc_options.offer_to_receive_video = 1;
3562
3563 std::unique_ptr<SessionDescriptionInterface> offer;
3564 CreatePeerConnection();
3565
3566 rtc_options.use_rtp_mux = true;
3567 offer = CreateOfferWithOptions(rtc_options);
3568 ASSERT_TRUE(offer);
3569 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3570 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3571 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3572
3573 rtc_options.use_rtp_mux = false;
3574 offer = CreateOfferWithOptions(rtc_options);
3575 ASSERT_TRUE(offer);
3576 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3577 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3578 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3579}
3580
zhihuang141aacb2017-08-29 13:23:53 -07003581// This test ensures OnRenegotiationNeeded is called when we add track with
3582// MediaStream -> AddTrack in the same way it is called when we add track with
3583// PeerConnection -> AddTrack.
3584// The test can be removed once addStream is rewritten in terms of addTrack
3585// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003586// Don't run under Unified Plan since the stream API is not available.
3587TEST_F(PeerConnectionInterfaceTestPlanB,
3588 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003589 CreatePeerConnectionWithoutDtls();
3590 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003591 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003592 pc_->AddStream(stream);
3593 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003594 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003595 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003596 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003597 stream->AddTrack(audio_track);
3598 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3599 observer_.renegotiation_needed_ = false;
3600
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003601 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003602 stream->AddTrack(video_track);
3603 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3604 observer_.renegotiation_needed_ = false;
3605
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003606 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003607 stream->RemoveTrack(audio_track);
3608 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3609 observer_.renegotiation_needed_ = false;
3610
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003611 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003612 stream->RemoveTrack(video_track);
3613 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3614 observer_.renegotiation_needed_ = false;
3615}
3616
Zhi Huangb2d355e2017-10-26 17:26:37 -07003617// Tests that an error is returned if a description is applied that has fewer
3618// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003619TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003620 MediaSectionCountEnforcedForSubsequentOffer) {
3621 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003622 AddAudioTrack("audio_label");
3623 AddVideoTrack("video_label");
3624
Zhi Huangb2d355e2017-10-26 17:26:37 -07003625 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003626 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003627 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3628
3629 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003630 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003631 offer->description()->contents().pop_back();
3632 offer->description()->contents().pop_back();
3633 ASSERT_TRUE(offer->description()->contents().empty());
3634 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3635
3636 std::unique_ptr<SessionDescriptionInterface> answer;
3637 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3638 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3639
3640 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003641 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003642 offer->description()->contents().pop_back();
3643 offer->description()->contents().pop_back();
3644 ASSERT_TRUE(offer->description()->contents().empty());
3645 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3646}
3647
Johannes Kron89f874e2018-11-12 10:25:48 +01003648TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3649 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003650 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003651 CreatePeerConnection(config);
3652 std::unique_ptr<SessionDescriptionInterface> offer;
3653 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003654 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3655 // Possible to set to false.
3656 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003657 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003658 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003659 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003660 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003661}
3662
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003663INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3664 PeerConnectionInterfaceTest,
3665 Values(SdpSemantics::kPlanB,
3666 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003667
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003668class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003669 protected:
3670 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003671 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003672 }
nisseeaabdf62017-05-05 02:23:02 -07003673 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003674 const RTCConfiguration& config) {
3675 rtc::scoped_refptr<PeerConnectionInterface> pc(
3676 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003677 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003678 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003679 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003680 }
3681
zhihuang9763d562016-08-05 11:14:50 -07003682 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003683 MockPeerConnectionObserver observer_;
3684};
3685
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003686// This sanity check validates the test infrastructure itself.
3687TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3688 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003689 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003690 rtc::scoped_refptr<PeerConnectionInterface> pc(
3691 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3692 EXPECT_TRUE(pc.get());
3693 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3694 pc->Close(); // No abort -> ok.
3695 SUCCEED();
3696}
3697
nisse51542be2016-02-12 02:27:06 -08003698// This test verifies the default behaviour with no constraints and a
3699// default RTCConfiguration.
3700TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3701 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003702 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003703
Niels Möllerf06f9232018-08-07 12:32:18 +02003704 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003705
3706 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003707 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3708 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003709 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003710 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003711}
3712
Niels Möller1d7ecd22018-01-18 15:25:12 +01003713// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003714// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003715TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3716 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003717 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
nisse51542be2016-02-12 02:27:06 -08003718
Niels Möller71bdda02016-03-31 12:59:59 +02003719 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003720 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003721
Niels Möller1d7ecd22018-01-18 15:25:12 +01003722 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003723}
3724
Niels Möller6539f692018-01-18 08:58:50 +01003725// This test verifies that the experiment_cpu_load_estimator flag is
3726// propagated from RTCConfiguration to the PeerConnection.
3727TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3728 PeerConnectionInterface::RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +01003729 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Niels Möller6539f692018-01-18 08:58:50 +01003730
3731 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003732 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003733
3734 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3735}
3736
deadbeef293e9262017-01-11 12:28:30 -08003737// Tests a few random fields being different.
3738TEST(RTCConfigurationTest, ComparisonOperators) {
3739 PeerConnectionInterface::RTCConfiguration a;
3740 PeerConnectionInterface::RTCConfiguration b;
3741 EXPECT_EQ(a, b);
3742
3743 PeerConnectionInterface::RTCConfiguration c;
3744 c.servers.push_back(PeerConnectionInterface::IceServer());
3745 EXPECT_NE(a, c);
3746
3747 PeerConnectionInterface::RTCConfiguration d;
3748 d.type = PeerConnectionInterface::kRelay;
3749 EXPECT_NE(a, d);
3750
3751 PeerConnectionInterface::RTCConfiguration e;
3752 e.audio_jitter_buffer_max_packets = 5;
3753 EXPECT_NE(a, e);
3754
3755 PeerConnectionInterface::RTCConfiguration f;
3756 f.ice_connection_receiving_timeout = 1337;
3757 EXPECT_NE(a, f);
3758
3759 PeerConnectionInterface::RTCConfiguration g;
3760 g.disable_ipv6 = true;
3761 EXPECT_NE(a, g);
3762
3763 PeerConnectionInterface::RTCConfiguration h(
3764 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3765 EXPECT_NE(a, h);
3766}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003767
3768} // namespace
3769} // namespace webrtc