blob: 2105c784a44e83fbcb1089fb9831e7ffc5abf92b [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 Titov37ee0f52021-07-26 15:29:11 +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 Titov37ee0f52021-07-26 15:29:11 +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 Titov37ee0f52021-07-26 15:29:11 +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
deadbeef293e9262017-01-11 12:28:30 -0800713 // DTLS does not work in a loopback call, so is disabled for most of the
714 // tests in this file.
715 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200716 RTCConfiguration config;
717 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800718
Niels Möllerf06f9232018-08-07 12:32:18 +0200719 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 }
721
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 void CreatePeerConnectionWithIceTransportsType(
723 PeerConnectionInterface::IceTransportsType type) {
724 PeerConnectionInterface::RTCConfiguration config;
725 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200726 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 }
728
729 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100730 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800732 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100735 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 server.password = password;
737 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200738 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740
Niels Möllerf06f9232018-08-07 12:32:18 +0200741 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200742 if (pc_) {
743 pc_->Close();
744 pc_ = nullptr;
745 }
kwibergd1fe2812016-04-27 06:47:29 -0700746 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800747 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
748 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000749
deadbeef1dcb1642017-03-29 21:08:16 -0700750 // Create certificate generator unless DTLS constraint is explicitly set to
751 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200752 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200753
754 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800755 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
756 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000757 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800758 RTCConfiguration modified_config = config;
759 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200760 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200761 modified_config, std::move(port_allocator), std::move(cert_generator),
762 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 ASSERT_TRUE(pc_.get() != NULL);
764 observer_.SetPeerConnectionInterface(pc_.get());
765 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
766 }
767
deadbeef0a6c4ca2015-10-06 11:38:28 -0700768 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800769 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700770 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700771 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800773 config.sdp_semantics = sdp_semantics_;
774 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200775 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700777 }
778
Steve Anton038834f2017-07-14 15:59:59 -0700779 void CreatePeerConnectionExpectFail(
780 PeerConnectionInterface::RTCConfiguration config) {
781 PeerConnectionInterface::IceServer server;
782 server.uri = kTurnIceServerUri;
783 server.password = kTurnPassword;
784 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800785 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700786 rtc::scoped_refptr<PeerConnectionInterface> pc =
787 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
788 EXPECT_EQ(nullptr, pc);
789 }
790
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100792 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800793 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
794 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
795 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798
deadbeef0a6c4ca2015-10-06 11:38:28 -0700799 CreatePeerConnectionExpectFail(kStunInvalidPort);
800 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
801 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802
Niels Möllerdb4def92019-03-18 16:53:59 +0100803 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
804 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
806 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800808 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800810 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800812 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814
815 void ReleasePeerConnection() {
816 pc_ = NULL;
817 observer_.SetPeerConnectionInterface(NULL);
818 }
819
Steve Anton36da6ff2018-02-16 16:04:20 -0800820 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
821 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100822 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 }
824
825 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800826 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800828 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800829 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800835 stream->AddTrack(CreateVideoTrack(label + "v0"));
836 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800837 }
838
839 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
840 const std::string& label) {
841 return pc_factory_->CreateAudioTrack(label, nullptr);
842 }
843
844 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800845 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800846 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800847 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800848 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800849 }
850
851 void AddAudioStream(const std::string& label) {
852 rtc::scoped_refptr<MediaStreamInterface> stream(
853 pc_factory_->CreateLocalMediaStream(label));
854 stream->AddTrack(CreateAudioTrack(label + "a0"));
855 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857
Seth Hampson845e8782018-03-02 11:34:10 -0800858 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 const std::string& audio_track_label,
860 const std::string& video_track_label) {
861 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700862 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800863 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800864 stream->AddTrack(CreateAudioTrack(audio_track_label));
865 stream->AddTrack(CreateVideoTrack(video_track_label));
866 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868
Steve Anton36da6ff2018-02-16 16:04:20 -0800869 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
870 cricket::MediaType media_type) {
871 for (auto receiver : pc_->GetReceivers()) {
872 if (receiver->media_type() == media_type) {
873 return receiver;
874 }
875 }
876 return nullptr;
877 }
878
kwibergd1fe2812016-04-27 06:47:29 -0700879 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200880 const RTCOfferAnswerOptions* options,
881 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200882 auto observer =
883 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200885 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200887 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700890 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 return observer->result();
892 }
893
kwibergd1fe2812016-04-27 06:47:29 -0700894 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200895 const RTCOfferAnswerOptions* options) {
896 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 }
898
kwibergd1fe2812016-04-27 06:47:29 -0700899 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200900 const RTCOfferAnswerOptions* options) {
901 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903
Steve Antondb45ca82017-09-11 18:27:34 -0700904 bool DoSetSessionDescription(
905 std::unique_ptr<SessionDescriptionInterface> desc,
906 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200907 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700909 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700911 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
zhihuang29ff8442016-07-27 11:07:25 -0700913 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
914 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
915 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 return observer->result();
917 }
918
Steve Antondb45ca82017-09-11 18:27:34 -0700919 bool DoSetLocalDescription(
920 std::unique_ptr<SessionDescriptionInterface> desc) {
921 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
923
Steve Antondb45ca82017-09-11 18:27:34 -0700924 bool DoSetRemoteDescription(
925 std::unique_ptr<SessionDescriptionInterface> desc) {
926 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928
929 // Calls PeerConnection::GetStats and check the return value.
930 // It does not verify the values in the StatReports since a RTCP packet might
931 // be required.
932 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200933 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Yves Gerey665174f2018-06-19 15:03:05 +0200934 if (!pc_->GetStats(observer, track,
935 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 return false;
937 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
938 return observer->called();
939 }
940
Harald Alvestrand89061872018-01-02 14:08:34 +0100941 // Call the standards-compliant GetStats function.
942 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200943 auto callback =
944 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100945 pc_->GetStats(callback);
946 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
947 return callback->called();
948 }
949
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800951 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800953 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800954 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800955 } else {
956 // Unified Plan does not support AddStream, so just add an audio and video
957 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800958 AddAudioTrack(kAudioTracks[0], {kStreamId1});
959 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800960 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 CreateOfferReceiveAnswer();
962 }
963
964 // Verify that RTP Header extensions has been negotiated for audio and video.
965 void VerifyRemoteRtpHeaderExtensions() {
966 const cricket::MediaContentDescription* desc =
967 cricket::GetFirstAudioContentDescription(
968 pc_->remote_description()->description());
969 ASSERT_TRUE(desc != NULL);
970 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
971
972 desc = cricket::GetFirstVideoContentDescription(
973 pc_->remote_description()->description());
974 ASSERT_TRUE(desc != NULL);
975 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
976 }
977
978 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700979 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700980 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 std::string sdp;
982 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700983 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800984 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700985 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
987 }
988
deadbeefab9b2d12015-10-14 11:33:11 -0700989 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700990 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800991 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700992 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700993 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
994 }
995
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700997 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700998 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999
1000 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1001 // audio codec change, even if the parameter has nothing to do with
1002 // receiving. Not all parameters are serialized to SDP.
1003 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1004 // the SessionDescription, it is necessary to do that here to in order to
1005 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1006 // https://code.google.com/p/webrtc/issues/detail?id=1356
1007 std::string sdp;
1008 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001009 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001010 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001011 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1013 }
1014
1015 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001016 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001017 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018
1019 std::string sdp;
1020 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001021 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001022 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001023 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1025 }
1026
1027 void CreateOfferReceiveAnswer() {
1028 CreateOfferAsLocalDescription();
1029 std::string sdp;
1030 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1031 CreateAnswerAsRemoteDescription(sdp);
1032 }
1033
1034 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001035 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001036 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1038 // audio codec change, even if the parameter has nothing to do with
1039 // receiving. Not all parameters are serialized to SDP.
1040 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1041 // the SessionDescription, it is necessary to do that here to in order to
1042 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1043 // https://code.google.com/p/webrtc/issues/detail?id=1356
1044 std::string sdp;
1045 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001046 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001047 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048
Steve Antondb45ca82017-09-11 18:27:34 -07001049 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001051 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001052 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 }
1054
deadbeefab9b2d12015-10-14 11:33:11 -07001055 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001056 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001057 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001058 ASSERT_TRUE(answer);
1059 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1061 }
1062
deadbeefab9b2d12015-10-14 11:33:11 -07001063 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001064 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001065 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001066 ASSERT_TRUE(pr_answer);
1067 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001069 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001070 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001071 ASSERT_TRUE(answer);
1072 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1074 }
1075
Seth Hampson845e8782018-03-02 11:34:10 -08001076 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001077 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001078 // called with the given stream id and expected number of tracks.
1079 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001080 int expected_num_tracks) {
1081 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001082 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001083 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001084 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 }
1086
1087 // Creates an offer and applies it as a local session description.
1088 // Creates an answer with the same SDP an the offer but removes all lines
1089 // that start with a:ssrc"
1090 void CreateOfferReceiveAnswerWithoutSsrc() {
1091 CreateOfferAsLocalDescription();
1092 std::string sdp;
1093 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1094 SetSsrcToZero(&sdp);
1095 CreateAnswerAsRemoteDescription(sdp);
1096 }
1097
deadbeefab9b2d12015-10-14 11:33:11 -07001098 // This function creates a MediaStream with label kStreams[0] and
Artem Titov37ee0f52021-07-26 15:29:11 +02001099 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001100 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001101 // is returned and the MediaStream is stored in
Artem Titov37ee0f52021-07-26 15:29:11 +02001102 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001103 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001104 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1105 size_t number_of_video_tracks) {
1106 EXPECT_LE(number_of_audio_tracks, 2u);
1107 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001108
1109 reference_collection_ = StreamCollection::Create();
1110 std::string sdp_ms1 = std::string(kSdpStringInit);
1111
Seth Hampson845e8782018-03-02 11:34:10 -08001112 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001113
1114 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001115 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001116 reference_collection_->AddStream(stream);
1117
1118 if (number_of_audio_tracks > 0) {
1119 sdp_ms1 += std::string(kSdpStringAudio);
1120 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1121 AddAudioTrack(kAudioTracks[0], stream);
1122 }
1123 if (number_of_audio_tracks > 1) {
1124 sdp_ms1 += kSdpStringMs1Audio1;
1125 AddAudioTrack(kAudioTracks[1], stream);
1126 }
1127
1128 if (number_of_video_tracks > 0) {
1129 sdp_ms1 += std::string(kSdpStringVideo);
1130 sdp_ms1 += std::string(kSdpStringMs1Video0);
1131 AddVideoTrack(kVideoTracks[0], stream);
1132 }
1133 if (number_of_video_tracks > 1) {
1134 sdp_ms1 += kSdpStringMs1Video1;
1135 AddVideoTrack(kVideoTracks[1], stream);
1136 }
1137
kwibergd1fe2812016-04-27 06:47:29 -07001138 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001139 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001140 }
1141
1142 void AddAudioTrack(const std::string& track_id,
1143 MediaStreamInterface* stream) {
1144 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1145 webrtc::AudioTrack::Create(track_id, nullptr));
1146 ASSERT_TRUE(stream->AddTrack(audio_track));
1147 }
1148
1149 void AddVideoTrack(const std::string& track_id,
1150 MediaStreamInterface* stream) {
1151 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001152 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001153 webrtc::FakeVideoTrackSource::Create(),
1154 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001155 ASSERT_TRUE(stream->AddTrack(video_track));
1156 }
1157
Steve Anton36da6ff2018-02-16 16:04:20 -08001158 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001159 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001160 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001161 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001162 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1163 return offer;
1164 }
1165
Steve Anton36da6ff2018-02-16 16:04:20 -08001166 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1167 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001168 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001169 std::unique_ptr<SessionDescriptionInterface> offer;
1170 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1171 return offer;
1172 }
1173
1174 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1175 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1176 std::unique_ptr<SessionDescriptionInterface> answer;
1177 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1178 return answer;
1179 }
1180
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001183 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001184 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1186 return answer;
1187 }
1188
1189 const std::string& GetFirstAudioStreamCname(
1190 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001191 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001192 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001193 return audio_desc->streams()[0].cname;
1194 }
1195
zhihuang1c378ed2017-08-17 14:10:50 -07001196 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1197 const RTCOfferAnswerOptions& offer_answer_options) {
1198 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001199 auto observer =
1200 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
zhihuang1c378ed2017-08-17 14:10:50 -07001201 pc_->CreateOffer(observer, offer_answer_options);
1202 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1203 return observer->MoveDescription();
1204 }
1205
1206 void CreateOfferWithOptionsAsRemoteDescription(
1207 std::unique_ptr<SessionDescriptionInterface>* desc,
1208 const RTCOfferAnswerOptions& offer_answer_options) {
1209 *desc = CreateOfferWithOptions(offer_answer_options);
1210 ASSERT_TRUE(desc != nullptr);
1211 std::string sdp;
1212 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001213 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001214 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001215 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001216 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1217 }
1218
1219 void CreateOfferWithOptionsAsLocalDescription(
1220 std::unique_ptr<SessionDescriptionInterface>* desc,
1221 const RTCOfferAnswerOptions& offer_answer_options) {
1222 *desc = CreateOfferWithOptions(offer_answer_options);
1223 ASSERT_TRUE(desc != nullptr);
1224 std::string sdp;
1225 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001226 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001227 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001228
Steve Antondb45ca82017-09-11 18:27:34 -07001229 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001230 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1231 }
1232
1233 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001234 RTC_DCHECK(content);
1235 RTC_DCHECK(content->media_description());
1236 for (const cricket::AudioCodec& codec :
1237 content->media_description()->as_audio()->codecs()) {
1238 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001239 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001240 }
zhihuang1c378ed2017-08-17 14:10:50 -07001241 }
1242 return false;
1243 }
1244
Steve Anton36da6ff2018-02-16 16:04:20 -08001245 const char* GetSdpStringWithStream1() const {
1246 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1247 return kSdpStringWithStream1PlanB;
1248 } else {
1249 return kSdpStringWithStream1UnifiedPlan;
1250 }
1251 }
1252
1253 const char* GetSdpStringWithStream1And2() const {
1254 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1255 return kSdpStringWithStream1And2PlanB;
1256 } else {
1257 return kSdpStringWithStream1And2UnifiedPlan;
1258 }
1259 }
1260
deadbeef9a6f4d42017-05-15 19:43:33 -07001261 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1262 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001263 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001264 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001265 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001266 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1267 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1268 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001270 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001271 const SdpSemantics sdp_semantics_;
1272};
1273
1274class PeerConnectionInterfaceTest
1275 : public PeerConnectionInterfaceBaseTest,
1276 public ::testing::WithParamInterface<SdpSemantics> {
1277 protected:
1278 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1279};
1280
1281class PeerConnectionInterfaceTestPlanB
1282 : public PeerConnectionInterfaceBaseTest {
1283 protected:
1284 PeerConnectionInterfaceTestPlanB()
1285 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286};
1287
zhihuang8f65cdf2016-05-06 18:40:30 -07001288// Generate different CNAMEs when PeerConnections are created.
1289// The CNAMEs are expected to be generated randomly. It is possible
1290// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001291TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001292 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001293 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001295 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001296 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1297 GetFirstAudioStreamCname(offer2.get()));
1298}
1299
Steve Anton36da6ff2018-02-16 16:04:20 -08001300TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001301 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001302 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001303 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001304 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001305 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1306 GetFirstAudioStreamCname(answer2.get()));
1307}
1308
Steve Anton36da6ff2018-02-16 16:04:20 -08001309TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 CreatePeerConnectionWithDifferentConfigurations) {
1311 CreatePeerConnectionWithDifferentConfigurations();
1312}
1313
Steve Anton36da6ff2018-02-16 16:04:20 -08001314TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1317 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1318 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1319 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1320 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1321 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1322 port_allocator_->candidate_filter());
1323 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1324 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1325}
1326
1327// Test that when a PeerConnection is created with a nonzero candidate pool
1328// size, the pooled PortAllocatorSession is created with all the attributes
1329// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001330TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001331 PeerConnectionInterface::RTCConfiguration config;
1332 PeerConnectionInterface::IceServer server;
1333 server.uri = kStunAddressOnly;
1334 config.servers.push_back(server);
1335 config.type = PeerConnectionInterface::kRelay;
1336 config.disable_ipv6 = true;
1337 config.tcp_candidate_policy =
1338 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001339 config.candidate_network_policy =
1340 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001341 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001342 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343
1344 const cricket::FakePortAllocatorSession* session =
1345 static_cast<const cricket::FakePortAllocatorSession*>(
1346 port_allocator_->GetPooledSession());
1347 ASSERT_NE(nullptr, session);
1348 EXPECT_EQ(1UL, session->stun_servers().size());
1349 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1350 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001351 EXPECT_LT(0U,
1352 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001353}
1354
deadbeefd21eab32017-07-26 16:50:11 -07001355// Test that network-related RTCConfiguration members are applied to the
1356// PortAllocator when CreatePeerConnection is called. Specifically:
1357// - disable_ipv6_on_wifi
1358// - max_ipv6_networks
1359// - tcp_candidate_policy
1360// - candidate_network_policy
1361// - prune_turn_ports
1362//
1363// Note that the candidate filter (RTCConfiguration::type) is already tested
1364// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001365TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001366 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1367 // Create fake port allocator.
1368 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1369 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1370 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1371
1372 // Create RTCConfiguration with some network-related fields relevant to
1373 // PortAllocator populated.
1374 PeerConnectionInterface::RTCConfiguration config;
1375 config.disable_ipv6_on_wifi = true;
1376 config.max_ipv6_networks = 10;
1377 config.tcp_candidate_policy =
1378 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1379 config.candidate_network_policy =
1380 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1381 config.prune_turn_ports = true;
1382
1383 // Create the PC factory and PC with the above config.
1384 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1385 webrtc::CreatePeerConnectionFactory(
1386 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001387 rtc::Thread::Current(), fake_audio_capture_module_,
1388 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001389 webrtc::CreateBuiltinAudioDecoderFactory(),
1390 webrtc::CreateBuiltinVideoEncoderFactory(),
1391 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1392 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001393 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001394 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1395 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001396 EXPECT_TRUE(pc.get());
1397 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001398
1399 // Now validate that the config fields set above were applied to the
1400 // PortAllocator, as flags or otherwise.
1401 EXPECT_FALSE(raw_port_allocator->flags() &
1402 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1403 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1404 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1405 EXPECT_TRUE(raw_port_allocator->flags() &
1406 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001407 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1408 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001409}
1410
deadbeef46c73892016-11-16 19:42:04 -08001411// Check that GetConfiguration returns the configuration the PeerConnection was
1412// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001413TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001414 PeerConnectionInterface::RTCConfiguration config;
1415 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001416 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001417
1418 PeerConnectionInterface::RTCConfiguration returned_config =
1419 pc_->GetConfiguration();
1420 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1421}
1422
1423// Check that GetConfiguration returns the last configuration passed into
1424// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001425TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001426 PeerConnectionInterface::RTCConfiguration starting_config;
1427 starting_config.bundle_policy =
1428 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1429 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001430
Steve Anton36da6ff2018-02-16 16:04:20 -08001431 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001432 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001433 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001434
1435 PeerConnectionInterface::RTCConfiguration returned_config =
1436 pc_->GetConfiguration();
1437 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1438}
1439
Steve Antonc79268f2018-04-24 09:54:10 -07001440TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1441 CreatePeerConnection();
1442
1443 pc_->Close();
1444
1445 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001446 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001447}
1448
Steve Anton36da6ff2018-02-16 16:04:20 -08001449TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001450 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001451 AddVideoStream(kStreamId1);
1452 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 ASSERT_EQ(2u, pc_->local_streams()->count());
1454
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001455 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001456 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001457 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001458 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001459 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001460 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001462 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001463 EXPECT_EQ(3u, pc_->local_streams()->count());
1464
1465 // Remove the third stream.
1466 pc_->RemoveStream(pc_->local_streams()->at(2));
1467 EXPECT_EQ(2u, pc_->local_streams()->count());
1468
1469 // Remove the second stream.
1470 pc_->RemoveStream(pc_->local_streams()->at(1));
1471 EXPECT_EQ(1u, pc_->local_streams()->count());
1472
1473 // Remove the first stream.
1474 pc_->RemoveStream(pc_->local_streams()->at(0));
1475 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476}
1477
deadbeefab9b2d12015-10-14 11:33:11 -07001478// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001479// Don't run under Unified Plan since the stream API is not available.
1480TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001481 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001482 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001483 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001484 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001485
deadbeefab9b2d12015-10-14 11:33:11 -07001486 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001487 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001488 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001489
deadbeefab9b2d12015-10-14 11:33:11 -07001490 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001491 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001492 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001493
1494 // Add another stream and ensure the offer includes both the old and new
1495 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001496 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001497 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001498
Steve Antonb1c1de12017-12-21 15:14:30 -08001499 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001500 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1501 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001502
Steve Antonb1c1de12017-12-21 15:14:30 -08001503 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001504 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1505 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001506}
1507
Steve Anton36da6ff2018-02-16 16:04:20 -08001508// Don't run under Unified Plan since the stream API is not available.
1509TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001510 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001511 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 ASSERT_EQ(1u, pc_->local_streams()->count());
1513 pc_->RemoveStream(pc_->local_streams()->at(0));
1514 EXPECT_EQ(0u, pc_->local_streams()->count());
1515}
1516
deadbeefe1f9d832016-01-14 15:35:42 -08001517// Test for AddTrack and RemoveTrack methods.
1518// Tests that the created offer includes tracks we added,
1519// and that the RtpSenders are created correctly.
1520// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001521// Only tested with Plan B since Unified Plan is covered in more detail by tests
1522// in peerconnection_jsep_unittests.cc
1523TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001524 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001525 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001526 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001528 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001529 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1530 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001531 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001532 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001533 EXPECT_EQ("audio_track", audio_sender->id());
1534 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001535 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001536 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001537 EXPECT_EQ("video_track", video_sender->id());
1538 EXPECT_EQ(video_track, video_sender->track());
1539
1540 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001541 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001542 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001543
1544 const cricket::ContentInfo* audio_content =
1545 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001546 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001547 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001548
1549 const cricket::ContentInfo* video_content =
1550 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001551 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001552 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001553
Steve Antondb45ca82017-09-11 18:27:34 -07001554 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
1556 // Now try removing the tracks.
1557 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1558 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1559
1560 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001561 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001562
1563 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001564 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001565 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001566
1567 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001568 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001569 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001570
Steve Antondb45ca82017-09-11 18:27:34 -07001571 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001572
1573 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1574 // should return false.
1575 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1576 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1577}
1578
1579// Test creating senders without a stream specified,
1580// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001581TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001582 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001583 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001584 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001586 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001587 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001588 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001589 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001590 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001591 EXPECT_EQ("audio_track", audio_sender->id());
1592 EXPECT_EQ(audio_track, audio_sender->track());
1593 EXPECT_EQ("video_track", video_sender->id());
1594 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001595 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1596 // If the ID is truly a random GUID, it should be infinitely unlikely they
1597 // will be the same.
1598 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1599 } else {
1600 // We allows creating tracks without stream ids under Unified Plan
1601 // semantics.
1602 EXPECT_EQ(0u, video_sender->stream_ids().size());
1603 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1604 }
deadbeefe1f9d832016-01-14 15:35:42 -08001605}
1606
Harald Alvestrand89061872018-01-02 14:08:34 +01001607// Test that we can call GetStats() after AddTrack but before connecting
1608// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001609TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001610 CreatePeerConnectionWithoutDtls();
1611 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001612 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001613 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001614 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001615 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1616 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001617 EXPECT_TRUE(DoGetStats(nullptr));
1618}
1619
Steve Anton36da6ff2018-02-16 16:04:20 -08001620TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001621 CreatePeerConnectionWithoutDtls();
1622 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001623 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001624 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001625 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001626 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001627 ASSERT_TRUE(audio_sender.ok());
1628 auto* audio_sender_proxy =
1629 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1630 audio_sender.value().get());
1631 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1632
Harald Alvestrandc72af932018-01-11 17:18:19 +01001633 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001634 ASSERT_TRUE(video_sender.ok());
1635 auto* video_sender_proxy =
1636 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1637 video_sender.value().get());
1638 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001639}
1640
Steve Anton36da6ff2018-02-16 16:04:20 -08001641// Don't run under Unified Plan since the stream API is not available.
1642TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001643 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001644 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001645 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001646 ASSERT_EQ(1u, senders.size());
1647 auto* sender_proxy =
1648 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1649 senders[0].get());
1650 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001651}
1652
Steve Anton36da6ff2018-02-16 16:04:20 -08001653TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001655 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 VerifyRemoteRtpHeaderExtensions();
1657}
1658
Steve Anton36da6ff2018-02-16 16:04:20 -08001659TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001660 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001661 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 CreateOfferAsLocalDescription();
1663 std::string offer;
1664 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1665 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001666 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667}
1668
Steve Anton36da6ff2018-02-16 16:04:20 -08001669TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001670 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001671 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672
1673 CreateOfferAsRemoteDescription();
1674 CreateAnswerAsLocalDescription();
1675
Seth Hampson845e8782018-03-02 11:34:10 -08001676 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677}
1678
Steve Anton36da6ff2018-02-16 16:04:20 -08001679TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001680 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001681 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682
1683 CreateOfferAsRemoteDescription();
1684 CreatePrAnswerAsLocalDescription();
1685 CreateAnswerAsLocalDescription();
1686
Seth Hampson845e8782018-03-02 11:34:10 -08001687 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688}
1689
Steve Anton36da6ff2018-02-16 16:04:20 -08001690// Don't run under Unified Plan since the stream API is not available.
1691TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 InitiateCall();
1693 ASSERT_EQ(1u, pc_->remote_streams()->count());
1694 pc_->RemoveStream(pc_->local_streams()->at(0));
1695 CreateOfferReceiveAnswer();
1696 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001697 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 CreateOfferReceiveAnswer();
1699}
1700
1701// Tests that after negotiating an audio only call, the respondent can perform a
1702// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001703TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001704 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001705 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 CreateOfferAsRemoteDescription();
1707 CreateAnswerAsLocalDescription();
1708
1709 ASSERT_EQ(1u, pc_->remote_streams()->count());
1710 pc_->RemoveStream(pc_->local_streams()->at(0));
1711 CreateOfferReceiveAnswer();
1712 EXPECT_EQ(0u, pc_->remote_streams()->count());
1713}
1714
1715// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001716TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718
Steve Antonf1c6db12017-10-13 11:13:35 -07001719 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001722 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001723 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001724 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725
1726 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001727 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001728 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001729 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730
Steve Antonf1c6db12017-10-13 11:13:35 -07001731 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001732 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733
Steve Antonf1c6db12017-10-13 11:13:35 -07001734 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735}
1736
deadbeefab9b2d12015-10-14 11:33:11 -07001737// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001739TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001740 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001742 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001743 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001744 EXPECT_TRUE(offer);
1745 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746
1747 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001748 AddAudioTrack("track_label", {kStreamId1});
1749 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750
1751 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001752 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753
1754 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001755 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001756 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757}
1758
1759// Test that we will get different SSRCs for each tracks in the offer and answer
1760// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001761TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001762 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001764 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1765 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766
1767 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001768 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001769 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 int audio_ssrc = 0;
1771 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001772 EXPECT_TRUE(
1773 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1774 EXPECT_TRUE(
1775 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 EXPECT_NE(audio_ssrc, video_ssrc);
1777
1778 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001779 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001781 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 audio_ssrc = 0;
1783 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001784 EXPECT_TRUE(
1785 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1786 EXPECT_TRUE(
1787 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 EXPECT_NE(audio_ssrc, video_ssrc);
1789}
1790
deadbeefeb459812015-12-15 19:24:43 -08001791// Test that it's possible to call AddTrack on a MediaStream after adding
1792// the stream to a PeerConnection.
1793// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001794// Don't run under Unified Plan since the stream API is not available.
1795TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001796 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001797 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001798 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001799 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1800
1801 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001802 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001803 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001804 stream->AddTrack(video_track.get());
1805
kwibergd1fe2812016-04-27 06:47:29 -07001806 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001807 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001808
1809 const cricket::MediaContentDescription* video_desc =
1810 cricket::GetFirstVideoContentDescription(offer->description());
1811 EXPECT_TRUE(video_desc != nullptr);
1812}
1813
1814// Test that it's possible to call RemoveTrack on a MediaStream after adding
1815// the stream to a PeerConnection.
1816// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001817// Don't run under Unified Plan since the stream API is not available.
1818TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001819 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001820 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001821 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001822 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1823
1824 // Remove the video track.
1825 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1826
kwibergd1fe2812016-04-27 06:47:29 -07001827 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001828 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001829
1830 const cricket::MediaContentDescription* video_desc =
1831 cricket::GetFirstVideoContentDescription(offer->description());
1832 EXPECT_TRUE(video_desc == nullptr);
1833}
1834
deadbeefbd7d8f72015-12-18 16:58:44 -08001835// Test creating a sender with a stream ID, and ensure the ID is populated
1836// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001837// Don't run under Unified Plan since the stream API is not available.
1838TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001839 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001840 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001841
kwibergd1fe2812016-04-27 06:47:29 -07001842 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001843 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001844
1845 const cricket::MediaContentDescription* video_desc =
1846 cricket::GetFirstVideoContentDescription(offer->description());
1847 ASSERT_TRUE(video_desc != nullptr);
1848 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001849 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001850}
1851
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001853TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001855 ASSERT_LT(0u, pc_->GetSenders().size());
1856 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001857 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001858 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 EXPECT_TRUE(DoGetStats(remote_audio));
1860
1861 // Remove the stream. Since we are sending to our selves the local
1862 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001863 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 // Do a re-negotiation.
1865 CreateOfferReceiveAnswer();
1866
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 // Test that we still can get statistics for the old track. Even if it is not
1868 // sent any longer.
1869 EXPECT_TRUE(DoGetStats(remote_audio));
1870}
1871
1872// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001873TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001875 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1876 ASSERT_TRUE(video_receiver);
1877 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878}
1879
1880// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001881TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 pc_factory_->CreateAudioTrack("unknown track", NULL));
1885 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1886}
1887
Steve Anton36da6ff2018-02-16 16:04:20 -08001888TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001889 CreatePeerConnectionWithoutDtls();
1890 EXPECT_TRUE(DoGetRTCStats());
1891 // Clearing stats cache is needed now, but should be temporary.
1892 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1893 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001894 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1895 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001896 EXPECT_TRUE(DoGetRTCStats());
1897 pc_->ClearStatsCache();
1898 CreateOfferReceiveAnswer();
1899 EXPECT_TRUE(DoGetRTCStats());
1900}
1901
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902// This tests that a SCTP data channel is returned using different
1903// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001904TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001905 RTCConfiguration rtc_config;
1906 rtc_config.enable_dtls_srtp = true;
1907 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908
1909 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001910 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 pc_->CreateDataChannel("1", &config);
1912 EXPECT_TRUE(channel != NULL);
1913 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001914 EXPECT_TRUE(observer_.renegotiation_needed_);
1915 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916
1917 config.ordered = false;
1918 channel = pc_->CreateDataChannel("2", &config);
1919 EXPECT_TRUE(channel != NULL);
1920 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001921 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922
1923 config.ordered = true;
1924 config.maxRetransmits = 0;
1925 channel = pc_->CreateDataChannel("3", &config);
1926 EXPECT_TRUE(channel != NULL);
1927 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001928 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001930 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 config.maxRetransmitTime = 0;
1932 channel = pc_->CreateDataChannel("4", &config);
1933 EXPECT_TRUE(channel != NULL);
1934 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001935 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936}
1937
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001938// For backwards compatibility, we want people who "unset" maxRetransmits
1939// and maxRetransmitTime by setting them to -1 to get what they want.
1940TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1941 RTCConfiguration rtc_config;
1942 rtc_config.enable_dtls_srtp = true;
1943 CreatePeerConnection(rtc_config);
1944
1945 webrtc::DataChannelInit config;
1946 config.maxRetransmitTime = -1;
1947 config.maxRetransmits = -1;
1948 rtc::scoped_refptr<DataChannelInterface> channel =
1949 pc_->CreateDataChannel("1", &config);
1950 EXPECT_TRUE(channel != NULL);
1951}
1952
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953// This tests that no data channel is returned if both maxRetransmits and
1954// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001955TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001957 RTCConfiguration rtc_config;
1958 rtc_config.enable_dtls_srtp = true;
1959 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960
1961 std::string label = "test";
1962 webrtc::DataChannelInit config;
1963 config.maxRetransmits = 0;
1964 config.maxRetransmitTime = 0;
1965
zhihuang9763d562016-08-05 11:14:50 -07001966 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 pc_->CreateDataChannel(label, &config);
1968 EXPECT_TRUE(channel == NULL);
1969}
1970
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971// The test verifies that creating a SCTP data channel with an id already in use
1972// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001973TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001975 RTCConfiguration rtc_config;
1976 rtc_config.enable_dtls_srtp = true;
1977 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978
1979 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001980 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001982 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001983 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001984 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 EXPECT_TRUE(channel != NULL);
1986 EXPECT_EQ(1, channel->id());
1987
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 channel = pc_->CreateDataChannel("x", &config);
1989 EXPECT_TRUE(channel == NULL);
1990
1991 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001992 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 channel = pc_->CreateDataChannel("max", &config);
1994 EXPECT_TRUE(channel != NULL);
1995 EXPECT_EQ(config.id, channel->id());
1996
1997 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001998 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 channel = pc_->CreateDataChannel("x", &config);
2000 EXPECT_TRUE(channel == NULL);
2001}
2002
deadbeefab9b2d12015-10-14 11:33:11 -07002003// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002004TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002005 RTCConfiguration rtc_config;
2006 rtc_config.enable_dtls_srtp = true;
2007 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002008
2009 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002010 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002011 pc_->CreateDataChannel(label, nullptr);
2012 EXPECT_NE(channel, nullptr);
2013
zhihuang9763d562016-08-05 11:14:50 -07002014 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002015 pc_->CreateDataChannel(label, nullptr);
2016 EXPECT_NE(dup_channel, nullptr);
2017}
2018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002020#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002021// This tests that SCTP data channels can be rejected in an answer.
2022TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2023#else
2024TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2025#endif
2026{
Niels Möllerf06f9232018-08-07 12:32:18 +02002027 RTCConfiguration rtc_config;
2028 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002029
2030 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2031 pc_->CreateDataChannel("offer_channel", NULL));
2032
2033 CreateOfferAsLocalDescription();
2034
2035 // Create an answer where the m-line for data channels are rejected.
2036 std::string sdp;
2037 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2038 std::unique_ptr<SessionDescriptionInterface> answer(
2039 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2040 ASSERT_TRUE(answer);
2041 cricket::ContentInfo* data_info =
2042 cricket::GetFirstDataContent(answer->description());
2043 data_info->rejected = true;
2044
2045 DoSetRemoteDescription(std::move(answer));
2046 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2047}
2048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049// Test that we can create a session description from an SDP string from
2050// FireFox, use it as a remote session description, generate an answer and use
2051// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002052TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002053 RTCConfiguration rtc_config;
2054 rtc_config.enable_dtls_srtp = true;
2055 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002056 AddAudioTrack("audio_label");
2057 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002058 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002059 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002060 webrtc::kFireFoxSdpOffer, nullptr));
2061 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 CreateAnswerAsLocalDescription();
2063 ASSERT_TRUE(pc_->local_description() != NULL);
2064 ASSERT_TRUE(pc_->remote_description() != NULL);
2065
2066 const cricket::ContentInfo* content =
2067 cricket::GetFirstAudioContent(pc_->local_description()->description());
2068 ASSERT_TRUE(content != NULL);
2069 EXPECT_FALSE(content->rejected);
2070
2071 content =
2072 cricket::GetFirstVideoContent(pc_->local_description()->description());
2073 ASSERT_TRUE(content != NULL);
2074 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002075#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 content =
2077 cricket::GetFirstDataContent(pc_->local_description()->description());
2078 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002079 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002080#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081}
2082
zhihuangb19012e2017-09-19 13:47:59 -07002083// Test that fallback from DTLS to SDES is not supported.
2084// The fallback was previously supported but was removed to simplify the code
2085// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002086TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002087 RTCConfiguration rtc_config;
2088 rtc_config.enable_dtls_srtp = true;
2089 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));
Zhi Huange830e682018-03-30 10:48:35 -07002100 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;
2132 rtc_config.enable_dtls_srtp = true;
2133 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002134 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002135 CreateAnswerAsLocalDescription();
2136
2137 // At this point we should be receiving stream 1, but not sending anything.
2138 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002139 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002140 DoCreateOffer(&offer, nullptr);
2141
2142 const cricket::ContentInfo* video_content =
2143 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002144 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2145 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002146
2147 const cricket::ContentInfo* audio_content =
2148 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002149 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2150 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002151}
2152
2153// Test that if we're receiving (but not sending) a track, and the
2154// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2155// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002156TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002157 RTCConfiguration rtc_config;
2158 rtc_config.enable_dtls_srtp = true;
2159 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002160 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002161 CreateAnswerAsLocalDescription();
2162
2163 // At this point we should be receiving stream 1, but not sending anything.
2164 // A new offer would be recvonly, but we'll set the "no receive" constraints
2165 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002166 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002167 RTCOfferAnswerOptions options;
2168 options.offer_to_receive_audio = 0;
2169 options.offer_to_receive_video = 0;
2170 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002171
2172 const cricket::ContentInfo* video_content =
2173 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002174 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2175 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002176
2177 const cricket::ContentInfo* audio_content =
2178 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002179 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2180 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002181}
2182
deadbeef653b8e02015-11-11 12:55:10 -08002183// Test that we can use SetConfiguration to change the ICE servers of the
2184// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002185TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002186 CreatePeerConnection();
2187
Steve Anton36da6ff2018-02-16 16:04:20 -08002188 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002189 PeerConnectionInterface::IceServer server;
2190 server.uri = "stun:test_hostname";
2191 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002192 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002193
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002194 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2195 EXPECT_EQ("test_hostname",
2196 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002197}
2198
Steve Anton36da6ff2018-02-16 16:04:20 -08002199TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002200 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002201 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002202 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002203 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002204 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2205}
2206
Steve Anton36da6ff2018-02-16 16:04:20 -08002207TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002208 PeerConnectionInterface::RTCConfiguration config;
2209 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002210 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002211 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002212 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002213
2214 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002215 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002216 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2217 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002218}
2219
skvladd1f5fda2017-02-03 16:54:05 -08002220// Test that the ice check interval can be changed. This does not verify that
2221// the setting makes it all the way to P2PTransportChannel, as that would
2222// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002223TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002224 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002225 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002226 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002227 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002228 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002229 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002230 config = pc_->GetConfiguration();
2231 EXPECT_EQ(config.ice_check_min_interval, 100);
2232}
2233
2234TEST_P(PeerConnectionInterfaceTest,
2235 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2236 PeerConnectionInterface::RTCConfiguration config;
2237 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2238 CreatePeerConnection(config);
2239 config = pc_->GetConfiguration();
2240 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2241
2242 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002243 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002244 config = pc_->GetConfiguration();
2245 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002246}
2247
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002248// Test that when SetConfiguration changes both the pool size and other
2249// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002250TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002251 SetConfigurationCreatesPooledSessionCorrectly) {
2252 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002253 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002254 config.ice_candidate_pool_size = 1;
2255 PeerConnectionInterface::IceServer server;
2256 server.uri = kStunAddressOnly;
2257 config.servers.push_back(server);
2258 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002259 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002260
2261 const cricket::FakePortAllocatorSession* session =
2262 static_cast<const cricket::FakePortAllocatorSession*>(
2263 port_allocator_->GetPooledSession());
2264 ASSERT_NE(nullptr, session);
2265 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002266}
2267
deadbeef293e9262017-01-11 12:28:30 -08002268// Test that after SetLocalDescription, changing the pool size is not allowed,
2269// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002270TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002271 CantChangePoolSizeAfterSetLocalDescription) {
2272 CreatePeerConnection();
2273 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002274 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002275 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002276 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002277
2278 // Set remote offer; can still change pool size at this point.
2279 CreateOfferAsRemoteDescription();
2280 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002281 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002282
2283 // Set local answer; now it's too late.
2284 CreateAnswerAsLocalDescription();
2285 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002286 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002287 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2288}
2289
deadbeef42a42632017-03-10 15:18:00 -08002290// Test that after setting an answer, extra pooled sessions are discarded. The
2291// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002292TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002293 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2294 CreatePeerConnection();
2295
2296 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002297 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002298 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002299 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002300
2301 // Do offer/answer.
2302 CreateOfferAsRemoteDescription();
2303 CreateAnswerAsLocalDescription();
2304
2305 // Expect no pooled sessions to be left.
2306 const cricket::PortAllocatorSession* session =
2307 port_allocator_->GetPooledSession();
2308 EXPECT_EQ(nullptr, session);
2309}
2310
2311// After Close is called, pooled candidates should be discarded so as to not
2312// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002313TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002314 CreatePeerConnection();
2315
Steve Anton36da6ff2018-02-16 16:04:20 -08002316 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002317 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002318 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002319 pc_->Close();
2320
2321 // Expect no pooled sessions to be left.
2322 const cricket::PortAllocatorSession* session =
2323 port_allocator_->GetPooledSession();
2324 EXPECT_EQ(nullptr, session);
2325}
2326
deadbeef293e9262017-01-11 12:28:30 -08002327// Test that SetConfiguration returns an invalid modification error if
2328// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002329TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002330 SetConfigurationReturnsInvalidModificationError) {
2331 PeerConnectionInterface::RTCConfiguration config;
2332 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2333 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2334 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002335 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002336
Steve Anton36da6ff2018-02-16 16:04:20 -08002337 PeerConnectionInterface::RTCConfiguration modified_config =
2338 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002339 modified_config.bundle_policy =
2340 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002341 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002342 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2343
Steve Anton36da6ff2018-02-16 16:04:20 -08002344 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002345 modified_config.rtcp_mux_policy =
2346 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002347 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002348 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2349
Steve Anton36da6ff2018-02-16 16:04:20 -08002350 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002351 modified_config.continual_gathering_policy =
2352 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002353 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002354 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2355}
2356
2357// Test that SetConfiguration returns a range error if the candidate pool size
2358// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002359TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002360 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2361 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002362 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002363 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002364
2365 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002366 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002367 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2368
2369 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002370 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002371 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2372}
2373
2374// Test that SetConfiguration returns a syntax error if parsing an ICE server
2375// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002376TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002377 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2378 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002379 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002380 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002381
2382 PeerConnectionInterface::IceServer bad_server;
2383 bad_server.uri = "stunn:www.example.com";
2384 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002385 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002386 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2387}
2388
2389// Test that SetConfiguration returns an invalid parameter error if a TURN
2390// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002391TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002392 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2393 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002394 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002395 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002396
2397 PeerConnectionInterface::IceServer bad_server;
2398 bad_server.uri = "turn:www.example.com";
2399 // Missing password.
2400 bad_server.username = "foo";
2401 config.servers.push_back(bad_server);
2402 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002403 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2404 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002405}
2406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407// Test that PeerConnection::Close changes the states to closed and all remote
2408// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002409TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 // Initialize a PeerConnection and negotiate local and remote session
2411 // description.
2412 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002413
2414 // With Plan B, verify the stream count. The analog with Unified Plan is the
2415 // RtpTransceiver count.
2416 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2417 ASSERT_EQ(1u, pc_->local_streams()->count());
2418 ASSERT_EQ(1u, pc_->remote_streams()->count());
2419 } else {
2420 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2421 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422
2423 pc_->Close();
2424
2425 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2426 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2427 pc_->ice_connection_state());
2428 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2429 pc_->ice_gathering_state());
2430
Steve Anton36da6ff2018-02-16 16:04:20 -08002431 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2432 EXPECT_EQ(1u, pc_->local_streams()->count());
2433 EXPECT_EQ(1u, pc_->remote_streams()->count());
2434 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002435 // Verify that the RtpTransceivers are still returned.
2436 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002437 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438
Steve Anton36da6ff2018-02-16 16:04:20 -08002439 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002440 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002441 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2442 ASSERT_TRUE(audio_receiver);
2443 ASSERT_TRUE(video_receiver);
2444 // Track state may be updated asynchronously.
2445 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2446 audio_receiver->track()->state(), kTimeout);
2447 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2448 video_receiver->track()->state(), kTimeout);
2449 } else {
2450 ASSERT_FALSE(audio_receiver);
2451 ASSERT_FALSE(video_receiver);
2452 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453}
2454
2455// Test that PeerConnection methods fails gracefully after
2456// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002457// Don't run under Unified Plan since the stream API is not available.
2458TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002459 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002460 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461 CreateOfferAsRemoteDescription();
2462 CreateAnswerAsLocalDescription();
2463
2464 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002465 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002466 pc_->local_streams()->at(0);
2467
2468 pc_->Close();
2469
2470 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002471 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002473 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2474
2475 EXPECT_TRUE(pc_->local_description() != NULL);
2476 EXPECT_TRUE(pc_->remote_description() != NULL);
2477
kwibergd1fe2812016-04-27 06:47:29 -07002478 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002479 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002480 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002481 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482
2483 std::string sdp;
2484 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002485 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002486 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002487 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002488
2489 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002490 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002491 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002492 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493}
2494
2495// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002496TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497 InitiateCall();
2498 pc_->Close();
2499 DoGetStats(NULL);
2500}
deadbeefab9b2d12015-10-14 11:33:11 -07002501
2502// NOTE: The series of tests below come from what used to be
2503// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2504// setting a remote or local description has the expected effects.
2505
2506// This test verifies that the remote MediaStreams corresponding to a received
2507// SDP string is created. In this test the two separate MediaStreams are
2508// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002509TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002510 RTCConfiguration config;
2511 config.enable_dtls_srtp = true;
2512 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002513 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002514
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002515 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002516 EXPECT_TRUE(
2517 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2518 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2519 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2520
2521 // Create a session description based on another SDP with another
2522 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002523 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002524
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002525 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002526 EXPECT_TRUE(
2527 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2528}
2529
2530// This test verifies that when remote tracks are added/removed from SDP, the
2531// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002532// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2533// specific behavior.
2534TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002535 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002536 RTCConfiguration config;
2537 config.enable_dtls_srtp = true;
2538 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002539 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002540 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002541 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002542 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2543 reference_collection_));
2544
2545 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002546 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002547 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002548 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002549 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2550 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002551 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002552 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2553 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002554 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002555 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2556 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002557
2558 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002559 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002560 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002561 MockTrackObserver audio_track_observer(audio_track2);
2562 MockTrackObserver video_track_observer(video_track2);
2563
2564 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2565 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002566 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002567 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2568 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002569 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002570 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002571 audio_track2->state(), kTimeout);
2572 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2573 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002574}
2575
2576// This tests that remote tracks are ended if a local session description is set
2577// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002578TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002579 RTCConfiguration config;
2580 config.enable_dtls_srtp = true;
2581 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002582 // First create and set a remote offer, then reject its video content in our
2583 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002584 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2585 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2586 ASSERT_TRUE(audio_receiver);
2587 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2588 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002589
Steve Anton36da6ff2018-02-16 16:04:20 -08002590 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2591 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002592 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002593 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2594 video_receiver->track();
2595 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002596
kwibergd1fe2812016-04-27 06:47:29 -07002597 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002598 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002599 cricket::ContentInfo* video_info =
2600 local_answer->description()->GetContentByName("video");
2601 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002602 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002603 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2604 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002605
2606 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002607 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002608 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002609 video_info = local_offer->description()->GetContentByName("video");
2610 ASSERT_TRUE(video_info != nullptr);
2611 video_info->rejected = true;
2612 cricket::ContentInfo* audio_info =
2613 local_offer->description()->GetContentByName("audio");
2614 ASSERT_TRUE(audio_info != nullptr);
2615 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002616 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002617 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002618 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2619 kTimeout);
2620 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2621 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002622}
2623
2624// This tests that we won't crash if the remote track has been removed outside
2625// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002626// Don't run under Unified Plan since the stream API is not available.
2627TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002628 RTCConfiguration config;
2629 config.enable_dtls_srtp = true;
2630 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002631 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002632 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2633 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2634 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2635
kwibergd1fe2812016-04-27 06:47:29 -07002636 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002637 webrtc::CreateSessionDescription(SdpType::kAnswer,
2638 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002639 cricket::ContentInfo* video_info =
2640 local_answer->description()->GetContentByName("video");
2641 video_info->rejected = true;
2642 cricket::ContentInfo* audio_info =
2643 local_answer->description()->GetContentByName("audio");
2644 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002645 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002646
2647 // No crash is a pass.
2648}
2649
deadbeef5e97fb52015-10-15 12:49:08 -07002650// This tests that if a recvonly remote description is set, no remote streams
2651// will be created, even if the description contains SSRCs/MSIDs.
2652// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002653TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002654 RTCConfiguration config;
2655 config.enable_dtls_srtp = true;
2656 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002657
Steve Anton36da6ff2018-02-16 16:04:20 -08002658 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002659 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002660 CreateAndSetRemoteOffer(recvonly_offer);
2661
2662 EXPECT_EQ(0u, observer_.remote_streams()->count());
2663}
2664
deadbeefab9b2d12015-10-14 11:33:11 -07002665// This tests that a default MediaStream is created if a remote session
2666// description doesn't contain any streams and no MSID support.
2667// It also tests that the default stream is updated if a video m-line is added
2668// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002669// Don't run under Unified Plan since this behavior is Plan B specific.
2670TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002671 RTCConfiguration config;
2672 config.enable_dtls_srtp = true;
2673 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002674 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2675
2676 ASSERT_EQ(1u, observer_.remote_streams()->count());
2677 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2678
2679 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2680 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002681 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002682
2683 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2684 ASSERT_EQ(1u, observer_.remote_streams()->count());
2685 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2686 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002687 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2688 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002689 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2690 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002691 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2692 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002693}
2694
2695// This tests that a default MediaStream is created if a remote session
2696// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002697// Don't run under Unified Plan since this behavior is Plan B specific.
2698TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002699 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002700 RTCConfiguration config;
2701 config.enable_dtls_srtp = true;
2702 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002703 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2704
2705 ASSERT_EQ(1u, observer_.remote_streams()->count());
2706 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2707
2708 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2709 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002710 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002711}
2712
2713// This tests that it won't crash when PeerConnection tries to remove
2714// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002715// Don't run under Unified Plan since this behavior is Plan B specific.
2716TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002717 RTCConfiguration config;
2718 config.enable_dtls_srtp = true;
2719 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002720 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002721 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2722 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2723 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2724
2725 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2726
2727 // No crash is a pass.
2728}
2729
2730// This tests that a default MediaStream is created if the remote session
2731// description doesn't contain any streams and don't contain an indication if
2732// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002733// Don't run under Unified Plan since this behavior is Plan B specific.
2734TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002735 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002736 RTCConfiguration config;
2737 config.enable_dtls_srtp = true;
2738 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002739 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2740
2741 ASSERT_EQ(1u, observer_.remote_streams()->count());
2742 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2743 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2744 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2745}
2746
2747// This tests that a default MediaStream is not created if the remote session
2748// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002749// Don't run under Unified Plan since this behavior is Plan B specific.
2750TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002751 RTCConfiguration config;
2752 config.enable_dtls_srtp = true;
2753 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002754 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2755 EXPECT_EQ(0u, observer_.remote_streams()->count());
2756}
2757
deadbeefbda7e0b2015-12-08 17:13:40 -08002758// This tests that when setting a new description, the old default tracks are
2759// not destroyed and recreated.
2760// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002761// Don't run under Unified Plan since this behavior is Plan B specific.
2762TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002763 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002764 RTCConfiguration config;
2765 config.enable_dtls_srtp = true;
2766 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002767 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2768
2769 ASSERT_EQ(1u, observer_.remote_streams()->count());
2770 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2771 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2772
2773 // Set the track to "disabled", then set a new description and ensure the
2774 // track is still disabled, which ensures it hasn't been recreated.
2775 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2776 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2777 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2778 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2779}
2780
deadbeefab9b2d12015-10-14 11:33:11 -07002781// This tests that a default MediaStream is not created if a remote session
2782// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002783// Don't run under Unified Plan since this behavior is Plan B specific.
2784TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002785 RTCConfiguration config;
2786 config.enable_dtls_srtp = true;
2787 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002788 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002789 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002790 EXPECT_TRUE(
2791 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2792
2793 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2794 EXPECT_EQ(0u, observer_.remote_streams()->count());
2795}
2796
Seth Hampson5897a6e2018-04-03 11:16:33 -07002797// This tests that a default MediaStream is created if a remote SDP comes from
2798// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2799TEST_F(PeerConnectionInterfaceTestPlanB,
2800 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002801 RTCConfiguration config;
2802 config.enable_dtls_srtp = true;
2803 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002804 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2805 // Add a=msid lines to simulate a Unified Plan endpoint that only
2806 // signals stream IDs with a=msid lines.
2807 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2808
2809 CreateAndSetRemoteOffer(sdp_string);
2810
2811 ASSERT_EQ(1u, observer_.remote_streams()->count());
2812 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2813 EXPECT_EQ("default", remote_stream->id());
2814 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2815}
2816
Seth Hampson5b4f0752018-04-02 16:31:36 -07002817// This tests that when a Plan B endpoint receives an SDP that signals no media
2818// stream IDs indicated by the special character "-" in the a=msid line, that
2819// a default stream ID will be used for the MediaStream ID. This can occur
2820// when a Unified Plan endpoint signals no media stream IDs, but signals both
2821// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2822TEST_F(PeerConnectionInterfaceTestPlanB,
2823 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002824 RTCConfiguration config;
2825 config.enable_dtls_srtp = true;
2826 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002827 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2828 // the sender's stream ID will be interpreted as no stream IDs.
2829 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2830 sdp_string.append("a=msid:- audiotrack0\n");
2831
2832 CreateAndSetRemoteOffer(sdp_string);
2833
2834 ASSERT_EQ(1u, observer_.remote_streams()->count());
2835 // Because SSRCs are signaled the track ID will be what was signaled in the
2836 // a=msid line.
2837 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2838 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2839 EXPECT_EQ("default", remote_stream->id());
2840 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002841
2842 // Previously a bug ocurred when setting the remote description a second time.
2843 // This is because we checked equality of the remote StreamParams stream ID
2844 // (empty), and the previously set stream ID for the remote sender
2845 // ("default"). This cause a track to be removed, then added, when really
2846 // nothing should occur because it is the same track.
2847 CreateAndSetRemoteOffer(sdp_string);
2848 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2849 EXPECT_EQ(1u, observer_.add_track_events_.size());
2850 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2851 remote_stream = observer_.remote_streams()->at(0);
2852 EXPECT_EQ("default", remote_stream->id());
2853 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002854}
2855
deadbeefab9b2d12015-10-14 11:33:11 -07002856// This tests that an RtpSender is created when the local description is set
2857// after adding a local stream.
2858// TODO(deadbeef): This test and the one below it need to be updated when
2859// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002860// Don't run under Unified Plan since this behavior is Plan B specific.
2861TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002862 RTCConfiguration config;
2863 config.enable_dtls_srtp = true;
2864 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002865
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002866 // Create an offer with 1 stream with 2 tracks of each type.
2867 rtc::scoped_refptr<StreamCollection> stream_collection =
2868 CreateStreamCollection(1, 2);
2869 pc_->AddStream(stream_collection->at(0));
2870 std::unique_ptr<SessionDescriptionInterface> offer;
2871 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002872 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002873
deadbeefab9b2d12015-10-14 11:33:11 -07002874 auto senders = pc_->GetSenders();
2875 EXPECT_EQ(4u, senders.size());
2876 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2877 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2878 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2879 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2880
2881 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002882 pc_->RemoveStream(stream_collection->at(0));
2883 stream_collection = CreateStreamCollection(1, 1);
2884 pc_->AddStream(stream_collection->at(0));
2885 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002886 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002887
deadbeefab9b2d12015-10-14 11:33:11 -07002888 senders = pc_->GetSenders();
2889 EXPECT_EQ(2u, senders.size());
2890 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2891 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2892 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2893 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2894}
2895
2896// This tests that an RtpSender is created when the local description is set
2897// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002898// Don't run under Unified Plan since this behavior is Plan B specific.
2899TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002900 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002901 RTCConfiguration config;
2902 config.enable_dtls_srtp = true;
2903 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002904
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002905 rtc::scoped_refptr<StreamCollection> stream_collection =
2906 CreateStreamCollection(1, 2);
2907 // Add a stream to create the offer, but remove it afterwards.
2908 pc_->AddStream(stream_collection->at(0));
2909 std::unique_ptr<SessionDescriptionInterface> offer;
2910 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2911 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002912
Steve Antondb45ca82017-09-11 18:27:34 -07002913 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002914 auto senders = pc_->GetSenders();
2915 EXPECT_EQ(0u, senders.size());
2916
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002917 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002918 senders = pc_->GetSenders();
2919 EXPECT_EQ(4u, senders.size());
2920 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2921 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2922 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2923 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2924}
2925
2926// This tests that the expected behavior occurs if the SSRC on a local track is
2927// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002928TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002929 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002930 RTCConfiguration config;
2931 config.enable_dtls_srtp = true;
2932 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002933
Steve Anton36da6ff2018-02-16 16:04:20 -08002934 AddAudioTrack(kAudioTracks[0]);
2935 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002936 std::unique_ptr<SessionDescriptionInterface> offer;
2937 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2938 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002939 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2940 webrtc::CreateSessionDescription(
2941 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002942 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002943 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002944
deadbeefab9b2d12015-10-14 11:33:11 -07002945 auto senders = pc_->GetSenders();
2946 EXPECT_EQ(2u, senders.size());
2947 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2948 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2949
2950 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002951 cricket::MediaContentDescription* desc =
2952 cricket::GetFirstAudioContentDescription(modified_offer->description());
2953 ASSERT_TRUE(desc != NULL);
2954 for (StreamParams& stream : desc->mutable_streams()) {
2955 for (unsigned int& ssrc : stream.ssrcs) {
2956 ++ssrc;
2957 }
2958 }
deadbeefab9b2d12015-10-14 11:33:11 -07002959
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002960 desc =
2961 cricket::GetFirstVideoContentDescription(modified_offer->description());
2962 ASSERT_TRUE(desc != NULL);
2963 for (StreamParams& stream : desc->mutable_streams()) {
2964 for (unsigned int& ssrc : stream.ssrcs) {
2965 ++ssrc;
2966 }
2967 }
2968
Steve Antondb45ca82017-09-11 18:27:34 -07002969 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002970 senders = pc_->GetSenders();
2971 EXPECT_EQ(2u, senders.size());
2972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2974 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2975 // changed.
2976}
2977
2978// This tests that the expected behavior occurs if a new session description is
2979// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002980// Don't run under Unified Plan since the stream API is not available.
2981TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002982 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002983 RTCConfiguration config;
2984 config.enable_dtls_srtp = true;
2985 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002986
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002987 rtc::scoped_refptr<StreamCollection> stream_collection =
2988 CreateStreamCollection(2, 1);
2989 pc_->AddStream(stream_collection->at(0));
2990 std::unique_ptr<SessionDescriptionInterface> offer;
2991 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002992 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002993
deadbeefab9b2d12015-10-14 11:33:11 -07002994 auto senders = pc_->GetSenders();
2995 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002996 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2997 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002998
2999 // Add a new MediaStream but with the same tracks as in the first stream.
3000 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3001 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003002 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3003 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003004 pc_->AddStream(stream_1);
3005
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003006 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003007 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003008
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003009 auto new_senders = pc_->GetSenders();
3010 // Should be the same senders as before, but with updated stream id.
3011 // Note that this behavior is subject to change in the future.
3012 // We may decide the PC should ignore existing tracks in AddStream.
3013 EXPECT_EQ(senders, new_senders);
3014 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3015 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003016}
3017
zhihuang81c3a032016-11-17 12:06:24 -08003018// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003019TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003020 RTCConfiguration config;
3021 config.enable_dtls_srtp = true;
3022 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003023 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3024 EXPECT_EQ(observer_.num_added_tracks_, 1);
3025 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3026
3027 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003028 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003029 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003030 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3031}
3032
deadbeefd1a38b52016-12-10 13:15:33 -08003033// Test that when SetConfiguration is called and the configuration is
3034// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003035TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003036 PeerConnectionInterface::RTCConfiguration config;
3037 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003038 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003039 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003040 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3041 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003042
3043 // Do initial offer/answer so there's something to restart.
3044 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003045 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003046
3047 // Grab the ufrags.
3048 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3049
3050 // Change ICE policy, which should trigger an ICE restart on the next offer.
3051 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003052 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003053 CreateOfferAsLocalDescription();
3054
3055 // Grab the new ufrags.
3056 std::vector<std::string> subsequent_ufrags =
3057 GetUfrags(pc_->local_description());
3058
3059 // Sanity check.
3060 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3061 // Check that each ufrag is different.
3062 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3063 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3064 }
3065}
3066
3067// Test that when SetConfiguration is called and the configuration *isn't*
3068// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003069TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003070 PeerConnectionInterface::RTCConfiguration config;
3071 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003072 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003073 config = pc_->GetConfiguration();
3074 AddAudioTrack(kAudioTracks[0]);
3075 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003076
3077 // Do initial offer/answer so there's something to restart.
3078 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003079 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003080
3081 // Grab the ufrags.
3082 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3083
3084 // Call SetConfiguration with a config identical to what the PC was
3085 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003086 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003087 CreateOfferAsLocalDescription();
3088
3089 // Grab the new ufrags.
3090 std::vector<std::string> subsequent_ufrags =
3091 GetUfrags(pc_->local_description());
3092
3093 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3094}
3095
3096// Test for a weird corner case scenario:
3097// 1. Audio/video session established.
3098// 2. SetConfiguration changes ICE config; ICE restart needed.
3099// 3. ICE restart initiated by remote peer, but only for one m= section.
3100// 4. Next createOffer should initiate an ICE restart, but only for the other
3101// m= section; it would be pointless to do an ICE restart for the m= section
3102// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003103TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003104 PeerConnectionInterface::RTCConfiguration config;
3105 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003106 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003107 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003108 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3109 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003110
3111 // Do initial offer/answer so there's something to restart.
3112 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003113 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003114
3115 // Change ICE policy, which should set the "needs-ice-restart" flag.
3116 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003117 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003118
3119 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003120 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003121 webrtc::CreateSessionDescription(SdpType::kOffer,
3122 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003123 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003124 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3125 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003126 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003127 CreateAnswerAsLocalDescription();
3128
3129 // Grab the ufrags.
3130 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003131 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003132
3133 // Create offer and grab the new ufrags.
3134 CreateOfferAsLocalDescription();
3135 std::vector<std::string> subsequent_ufrags =
3136 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003137 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003138
3139 // Ensure that only the ufrag for the second m= section changed.
3140 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3141 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3142}
3143
deadbeeffe4a8a42016-12-20 17:56:17 -08003144// Tests that the methods to return current/pending descriptions work as
3145// expected at different points in the offer/answer exchange. This test does
3146// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003147TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003148 // This disables DTLS so we can apply an answer to ourselves.
3149 CreatePeerConnection();
3150
3151 // Create initial local offer and get SDP (which will also be used as
3152 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003153 std::unique_ptr<SessionDescriptionInterface> local_offer;
3154 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003155 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003156 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003157
3158 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003159 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3160 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3161 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003162 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3163 EXPECT_EQ(nullptr, pc_->current_local_description());
3164 EXPECT_EQ(nullptr, pc_->current_remote_description());
3165
3166 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003167 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003168 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003169 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3170 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3171 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3172 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003173 EXPECT_EQ(nullptr, pc_->current_local_description());
3174 EXPECT_EQ(nullptr, pc_->current_remote_description());
3175
3176 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003177 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003178 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003179 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3180 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003181 EXPECT_EQ(nullptr, pc_->pending_local_description());
3182 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003183 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3184 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003185
3186 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003187 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003188 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003189 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3190 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3191 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003192 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003193 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3194 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003195
3196 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003197 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003198 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003199 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3200 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3201 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3202 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3203 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3204 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003205
3206 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003207 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003208 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003209 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3210 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003211 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3212 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003213 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3214 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003215}
3216
zhihuang77985012017-02-07 15:45:16 -08003217// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3218// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003219// This version tests the StartRtcEventLog version that receives an object
Artem Titov37ee0f52021-07-26 15:29:11 +02003220// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003221TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003222 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3223 CreatePeerConnection();
3224 // The RtcEventLog will be reset when the PeerConnection is closed.
3225 pc_->Close();
3226
Niels Möllerdec9f742019-06-03 15:25:20 +02003227 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003228 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003229 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003230 pc_->StopRtcEventLog();
3231}
3232
deadbeef30952b42017-04-21 02:41:29 -07003233// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003234TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003235 CreatePeerConnection();
3236
3237 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003238 RTCOfferAnswerOptions options;
3239 options.offer_to_receive_audio = 1;
3240 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003241 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003242 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003243 cricket::SessionDescription* desc = offer->description();
3244 ASSERT_EQ(2u, desc->transport_infos().size());
3245 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3246 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3247
3248 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003249 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003250 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003251 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3252 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003253 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003254 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003255 desc = answer->description();
3256 ASSERT_EQ(2u, desc->transport_infos().size());
3257 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3258 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3259}
3260
deadbeef1dcb1642017-03-29 21:08:16 -07003261// Test that ICE renomination isn't offered if it's not enabled in the PC's
3262// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003263TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003264 PeerConnectionInterface::RTCConfiguration config;
3265 config.enable_ice_renomination = false;
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_FALSE(
3274 desc->transport_infos()[0].description.GetIceParameters().renomination);
3275}
3276
3277// Test that the ICE renomination option is present in generated offers/answers
3278// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003279TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003280 PeerConnectionInterface::RTCConfiguration config;
3281 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003282 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003283 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003284
3285 std::unique_ptr<SessionDescriptionInterface> offer;
3286 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3287 cricket::SessionDescription* desc = offer->description();
3288 EXPECT_EQ(1u, desc->transport_infos().size());
3289 EXPECT_TRUE(
3290 desc->transport_infos()[0].description.GetIceParameters().renomination);
3291
3292 // Set the offer as a remote description, then create an answer and ensure it
3293 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003294 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003295 std::unique_ptr<SessionDescriptionInterface> answer;
3296 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3297 desc = answer->description();
3298 EXPECT_EQ(1u, desc->transport_infos().size());
3299 EXPECT_TRUE(
3300 desc->transport_infos()[0].description.GetIceParameters().renomination);
3301}
3302
3303// Test that if CreateOffer is called with the deprecated "offer to receive
3304// audio/video" constraints, they're processed and result in an offer with
3305// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003306TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003307 CreatePeerConnection();
3308
Niels Möllerf06f9232018-08-07 12:32:18 +02003309 RTCOfferAnswerOptions options;
3310 options.offer_to_receive_audio = 1;
3311 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003312 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003313 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003314
3315 cricket::SessionDescription* desc = offer->description();
3316 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3317 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3318 ASSERT_NE(nullptr, audio);
3319 ASSERT_NE(nullptr, video);
3320 EXPECT_FALSE(audio->rejected);
3321 EXPECT_FALSE(video->rejected);
3322}
3323
3324// Test that if CreateAnswer is called with the deprecated "offer to receive
3325// audio/video" constraints, they're processed and can be used to reject an
3326// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003327// Don't run under Unified Plan since this behavior is not supported.
3328TEST_F(PeerConnectionInterfaceTestPlanB,
3329 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003330 CreatePeerConnection();
3331
3332 // First, create an offer with audio/video and apply it as a remote
3333 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003334 RTCOfferAnswerOptions options;
3335 options.offer_to_receive_audio = 1;
3336 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003337 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003338 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003339 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003340
3341 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003342 options.offer_to_receive_audio = 0;
3343 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003344 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003345 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003346
3347 cricket::SessionDescription* desc = answer->description();
3348 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3349 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3350 ASSERT_NE(nullptr, audio);
3351 ASSERT_NE(nullptr, video);
3352 EXPECT_TRUE(audio->rejected);
3353 EXPECT_TRUE(video->rejected);
3354}
3355
deadbeef1dcb1642017-03-29 21:08:16 -07003356// Test that negotiation can succeed with a data channel only, and with the max
3357// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003358#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003359TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3360#else
3361TEST_P(PeerConnectionInterfaceTest,
3362 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003363#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003364 PeerConnectionInterface::RTCConfiguration config;
3365 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003366 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003367
3368 // First, create an offer with only a data channel and apply it as a remote
3369 // description.
3370 pc_->CreateDataChannel("test", nullptr);
3371 std::unique_ptr<SessionDescriptionInterface> offer;
3372 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003373 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003374
3375 // Create and set answer as well.
3376 std::unique_ptr<SessionDescriptionInterface> answer;
3377 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003378 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003379}
3380
Steve Anton36da6ff2018-02-16 16:04:20 -08003381TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003382 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003383 BitrateSettings bitrate;
3384 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003385 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3386}
3387
Steve Anton36da6ff2018-02-16 16:04:20 -08003388TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003389 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003390 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003391 bitrate.min_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, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003396 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003397 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003398 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003399 bitrate.start_bitrate_bps = 3;
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, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003404 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003405 BitrateSettings bitrate;
3406 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003407 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3408}
3409
Steve Anton36da6ff2018-02-16 16:04:20 -08003410TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003411 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003412 BitrateSettings bitrate;
3413 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003414 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003415 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3416}
3417
Steve Anton36da6ff2018-02-16 16:04:20 -08003418TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003419 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003420 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003421 bitrate.min_bitrate_bps = 10;
3422 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003423 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3424}
3425
Steve Anton36da6ff2018-02-16 16:04:20 -08003426TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003427 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003428 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003429 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003430 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3431}
3432
Niels Möller0c4f7be2018-05-07 14:01:37 +02003433// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003434// by Call's BitrateConstraints, which comes from the SDP or a default value.
3435// This test checks that a call to SetBitrate with a current bitrate that will
3436// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003437TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003438 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003439 BitrateSettings bitrate;
3440 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003441 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3442}
3443
zhihuang1c378ed2017-08-17 14:10:50 -07003444// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003445TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003446 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3447 RTCOfferAnswerOptions rtc_options;
3448
3449 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3450 // than kMaxOfferToReceiveMedia should be treated as invalid.
3451 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3452 CreatePeerConnection();
3453 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3454
3455 rtc_options.offer_to_receive_audio =
3456 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3457 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3458}
3459
Steve Anton36da6ff2018-02-16 16:04:20 -08003460TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003461 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3462 RTCOfferAnswerOptions rtc_options;
3463
3464 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3465 // than kMaxOfferToReceiveMedia should be treated as invalid.
3466 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3467 CreatePeerConnection();
3468 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3469
3470 rtc_options.offer_to_receive_video =
3471 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3472 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3473}
3474
3475// Test that the audio and video content will be added to an offer if both
Artem Titov37ee0f52021-07-26 15:29:11 +02003476// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003477TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
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 = 1;
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_NE(nullptr, GetFirstVideoContent(offer->description()));
3488}
3489
3490// Test that only audio content will be added to the offer if only
Artem Titov37ee0f52021-07-26 15:29:11 +02003491// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003492TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003493 RTCOfferAnswerOptions rtc_options;
3494 rtc_options.offer_to_receive_audio = 1;
3495 rtc_options.offer_to_receive_video = 0;
3496
3497 std::unique_ptr<SessionDescriptionInterface> offer;
3498 CreatePeerConnection();
3499 offer = CreateOfferWithOptions(rtc_options);
3500 ASSERT_TRUE(offer);
3501 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3502 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3503}
3504
Artem Titov37ee0f52021-07-26 15:29:11 +02003505// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003506// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003507TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003508 RTCOfferAnswerOptions rtc_options;
3509 rtc_options.offer_to_receive_audio = 0;
3510 rtc_options.offer_to_receive_video = 1;
3511
3512 std::unique_ptr<SessionDescriptionInterface> offer;
3513 CreatePeerConnection();
3514 offer = CreateOfferWithOptions(rtc_options);
3515 ASSERT_TRUE(offer);
3516 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3517 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3518}
3519
zhihuang1c378ed2017-08-17 14:10:50 -07003520// Test that no media content will be added to the offer if using default
3521// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003522TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003523 RTCOfferAnswerOptions rtc_options;
3524
3525 std::unique_ptr<SessionDescriptionInterface> offer;
3526 CreatePeerConnection();
3527 offer = CreateOfferWithOptions(rtc_options);
3528 ASSERT_TRUE(offer);
3529 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3530 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3531}
3532
Artem Titov37ee0f52021-07-26 15:29:11 +02003533// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003534// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003535TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3536 CreatePeerConnection();
3537
zhihuang1c378ed2017-08-17 14:10:50 -07003538 RTCOfferAnswerOptions rtc_options;
3539 rtc_options.ice_restart = false;
3540 rtc_options.offer_to_receive_audio = 1;
3541
3542 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003543 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003544 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3545 auto ufrag1 =
3546 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3547 auto pwd1 =
3548 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003549
Artem Titov37ee0f52021-07-26 15:29:11 +02003550 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003551 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003552 auto ufrag2 =
3553 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3554 auto pwd2 =
3555 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003556
Artem Titov37ee0f52021-07-26 15:29:11 +02003557 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003558 rtc_options.ice_restart = true;
3559 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003560 auto ufrag3 =
3561 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3562 auto pwd3 =
3563 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003564
3565 EXPECT_EQ(ufrag1, ufrag2);
3566 EXPECT_EQ(pwd1, pwd2);
3567 EXPECT_NE(ufrag2, ufrag3);
3568 EXPECT_NE(pwd2, pwd3);
3569}
3570
Artem Titov37ee0f52021-07-26 15:29:11 +02003571// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003572// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003573TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003574 RTCOfferAnswerOptions rtc_options;
3575 rtc_options.offer_to_receive_audio = 1;
3576 rtc_options.offer_to_receive_video = 1;
3577
3578 std::unique_ptr<SessionDescriptionInterface> offer;
3579 CreatePeerConnection();
3580
3581 rtc_options.use_rtp_mux = true;
3582 offer = CreateOfferWithOptions(rtc_options);
3583 ASSERT_TRUE(offer);
3584 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3585 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3586 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3587
3588 rtc_options.use_rtp_mux = false;
3589 offer = CreateOfferWithOptions(rtc_options);
3590 ASSERT_TRUE(offer);
3591 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3592 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3593 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3594}
3595
zhihuang141aacb2017-08-29 13:23:53 -07003596// This test ensures OnRenegotiationNeeded is called when we add track with
3597// MediaStream -> AddTrack in the same way it is called when we add track with
3598// PeerConnection -> AddTrack.
3599// The test can be removed once addStream is rewritten in terms of addTrack
3600// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003601// Don't run under Unified Plan since the stream API is not available.
3602TEST_F(PeerConnectionInterfaceTestPlanB,
3603 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003604 CreatePeerConnectionWithoutDtls();
3605 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003606 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003607 pc_->AddStream(stream);
3608 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003609 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003610 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003611 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003612 stream->AddTrack(audio_track);
3613 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3614 observer_.renegotiation_needed_ = false;
3615
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003616 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003617 stream->AddTrack(video_track);
3618 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3619 observer_.renegotiation_needed_ = false;
3620
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003621 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003622 stream->RemoveTrack(audio_track);
3623 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3624 observer_.renegotiation_needed_ = false;
3625
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003626 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003627 stream->RemoveTrack(video_track);
3628 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3629 observer_.renegotiation_needed_ = false;
3630}
3631
Zhi Huangb2d355e2017-10-26 17:26:37 -07003632// Tests that an error is returned if a description is applied that has fewer
3633// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003634TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003635 MediaSectionCountEnforcedForSubsequentOffer) {
3636 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003637 AddAudioTrack("audio_label");
3638 AddVideoTrack("video_label");
3639
Zhi Huangb2d355e2017-10-26 17:26:37 -07003640 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003641 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003642 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3643
3644 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003645 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003646 offer->description()->contents().pop_back();
3647 offer->description()->contents().pop_back();
3648 ASSERT_TRUE(offer->description()->contents().empty());
3649 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3650
3651 std::unique_ptr<SessionDescriptionInterface> answer;
3652 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3653 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3654
3655 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003656 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003657 offer->description()->contents().pop_back();
3658 offer->description()->contents().pop_back();
3659 ASSERT_TRUE(offer->description()->contents().empty());
3660 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3661}
3662
Johannes Kron89f874e2018-11-12 10:25:48 +01003663TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3664 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003665 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003666 CreatePeerConnection(config);
3667 std::unique_ptr<SessionDescriptionInterface> offer;
3668 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003669 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3670 // Possible to set to false.
3671 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003672 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003673 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003674 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003675 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003676}
3677
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003678INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3679 PeerConnectionInterfaceTest,
3680 Values(SdpSemantics::kPlanB,
3681 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003682
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003683class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003684 protected:
3685 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003686 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003687 }
nisseeaabdf62017-05-05 02:23:02 -07003688 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003689 const RTCConfiguration& config) {
3690 rtc::scoped_refptr<PeerConnectionInterface> pc(
3691 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003692 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003693 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003694 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003695 }
3696
zhihuang9763d562016-08-05 11:14:50 -07003697 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003698 MockPeerConnectionObserver observer_;
3699};
3700
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003701// This sanity check validates the test infrastructure itself.
3702TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3703 PeerConnectionInterface::RTCConfiguration config;
3704 rtc::scoped_refptr<PeerConnectionInterface> pc(
3705 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3706 EXPECT_TRUE(pc.get());
3707 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3708 pc->Close(); // No abort -> ok.
3709 SUCCEED();
3710}
3711
nisse51542be2016-02-12 02:27:06 -08003712// This test verifies the default behaviour with no constraints and a
3713// default RTCConfiguration.
3714TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3715 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003716
Niels Möllerf06f9232018-08-07 12:32:18 +02003717 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003718
3719 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003720 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3721 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003722 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003723 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003724}
3725
Niels Möller1d7ecd22018-01-18 15:25:12 +01003726// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003727// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003728TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3729 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003730
Niels Möller71bdda02016-03-31 12:59:59 +02003731 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003732 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003733
Niels Möller1d7ecd22018-01-18 15:25:12 +01003734 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003735}
3736
Niels Möller6539f692018-01-18 08:58:50 +01003737// This test verifies that the experiment_cpu_load_estimator flag is
3738// propagated from RTCConfiguration to the PeerConnection.
3739TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3740 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003741
3742 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003743 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003744
3745 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3746}
3747
deadbeef293e9262017-01-11 12:28:30 -08003748// Tests a few random fields being different.
3749TEST(RTCConfigurationTest, ComparisonOperators) {
3750 PeerConnectionInterface::RTCConfiguration a;
3751 PeerConnectionInterface::RTCConfiguration b;
3752 EXPECT_EQ(a, b);
3753
3754 PeerConnectionInterface::RTCConfiguration c;
3755 c.servers.push_back(PeerConnectionInterface::IceServer());
3756 EXPECT_NE(a, c);
3757
3758 PeerConnectionInterface::RTCConfiguration d;
3759 d.type = PeerConnectionInterface::kRelay;
3760 EXPECT_NE(a, d);
3761
3762 PeerConnectionInterface::RTCConfiguration e;
3763 e.audio_jitter_buffer_max_packets = 5;
3764 EXPECT_NE(a, e);
3765
3766 PeerConnectionInterface::RTCConfiguration f;
3767 f.ice_connection_receiving_timeout = 1337;
3768 EXPECT_NE(a, f);
3769
3770 PeerConnectionInterface::RTCConfiguration g;
3771 g.disable_ipv6 = true;
3772 EXPECT_NE(a, g);
3773
3774 PeerConnectionInterface::RTCConfiguration h(
3775 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3776 EXPECT_NE(a, h);
3777}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003778
3779} // namespace
3780} // namespace webrtc