blob: 3aaae64be3c9715a4e65c919009fca1534da9300 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "api/peer_connection_interface.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <limits.h>
14#include <stdint.h>
15#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwibergd1fe2812016-04-27 06:47:29 -070017#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080019#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010020#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
Steve Anton1c9c9fc2019-02-14 15:13:09 -080022#include "absl/strings/str_replace.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "absl/types/optional.h"
24#include "api/audio/audio_mixer.h"
25#include "api/audio_codecs/audio_decoder_factory.h"
26#include "api/audio_codecs/audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/audio_codecs/builtin_audio_decoder_factory.h"
28#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/call/call_factory_interface.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010030#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/data_channel_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/jsep_session_description.h"
34#include "api/media_stream_interface.h"
35#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "api/rtc_error.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020037#include "api/rtc_event_log/rtc_event_log.h"
38#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "api/rtc_event_log_output.h"
Niels Möllerd8b9ed72019-05-08 13:53:51 +020040#include "api/rtc_event_log_output_file.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "api/rtp_receiver_interface.h"
42#include "api/rtp_sender_interface.h"
43#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010044#include "api/scoped_refptr.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020045#include "api/task_queue/default_task_queue_factory.h"
Erik Språngceb44952020-09-22 11:36:35 +020046#include "api/transport/field_trial_based_config.h"
Anders Carlsson67537952018-05-03 11:28:29 +020047#include "api/video_codecs/builtin_video_decoder_factory.h"
48#include "api/video_codecs/builtin_video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010049#include "api/video_codecs/video_decoder_factory.h"
50#include "api/video_codecs/video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010051#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080052#include "media/base/media_config.h"
53#include "media/base/media_engine.h"
54#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020056#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "media/sctp/sctp_transport_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010058#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080060#include "p2p/base/fake_port_allocator.h"
61#include "p2p/base/p2p_constants.h"
Yves Gerey3e707812018-11-28 16:47:49 +010062#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "p2p/base/port_allocator.h"
64#include "p2p/base/transport_description.h"
65#include "p2p/base/transport_info.h"
66#include "pc/audio_track.h"
67#include "pc/media_session.h"
68#include "pc/media_stream.h"
69#include "pc/peer_connection.h"
70#include "pc/peer_connection_factory.h"
71#include "pc/rtc_stats_collector.h"
72#include "pc/rtp_sender.h"
73#include "pc/session_description.h"
74#include "pc/stream_collection.h"
75#include "pc/test/fake_audio_capture_module.h"
76#include "pc/test/fake_rtc_certificate_generator.h"
77#include "pc/test/fake_video_track_source.h"
78#include "pc/test/mock_peer_connection_observers.h"
79#include "pc/test/test_sdp_strings.h"
80#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010081#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080082#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020083#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080084#include "rtc_base/ref_counted_object.h"
85#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080086#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010087#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080088#include "rtc_base/time_utils.h"
89#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020090#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010091#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080092#include "test/testsupport/file_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070093
94#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080095#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070096#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010098namespace webrtc {
99namespace {
100
Seth Hampson845e8782018-03-02 11:34:10 -0800101static const char kStreamId1[] = "local_stream_1";
102static const char kStreamId2[] = "local_stream_2";
103static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104static const int kDefaultStunPort = 3478;
105static const char kStunAddressOnly[] = "stun:address";
106static const char kStunInvalidPort[] = "stun:address:-1";
107static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
108static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +0100109static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110static const char kTurnUsername[] = "user";
111static const char kTurnPassword[] = "password";
112static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
deadbeefab9b2d12015-10-14 11:33:11 -0700115static const char kStreams[][8] = {"stream1", "stream2"};
116static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
117static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
118
deadbeef5e97fb52015-10-15 12:49:08 -0700119static const char kRecvonly[] = "recvonly";
120static const char kSendrecv[] = "sendrecv";
121
deadbeefab9b2d12015-10-14 11:33:11 -0700122// Reference SDP with a MediaStream with label "stream1" and audio track with
123// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800124static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800129 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700130 "a=ice-ufrag:e5785931\r\n"
131 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700135 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800136 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:103 ISAC/16000\r\n"
138 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000139 "a=ssrc:1 mslabel:stream1\r\n"
140 "a=ssrc:1 label:audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800142 "a=ice-ufrag:e5785931\r\n"
143 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
144 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
145 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700146 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700147 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800148 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700149 "a=rtpmap:120 VP8/90000\r\n"
150 "a=ssrc:2 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000151 "a=ssrc:2 mslabel:stream1\r\n"
152 "a=ssrc:2 label:videotrack0\r\n";
153// Same string as above but with the MID changed to the Unified Plan default.
154// This is needed so that this SDP can be used as an answer for a Unified Plan
155// offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800156static const char kSdpStringWithStream1UnifiedPlan[] =
157 "v=0\r\n"
158 "o=- 0 0 IN IP4 127.0.0.1\r\n"
159 "s=-\r\n"
160 "t=0 0\r\n"
161 "m=audio 1 RTP/AVPF 103\r\n"
162 "a=ice-ufrag:e5785931\r\n"
163 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
164 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
165 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
166 "a=mid:0\r\n"
167 "a=sendrecv\r\n"
168 "a=rtcp-mux\r\n"
169 "a=rtpmap:103 ISAC/16000\r\n"
170 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000171 "a=ssrc:1 mslabel:stream1\r\n"
172 "a=ssrc:1 label:audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800173 "m=video 1 RTP/AVPF 120\r\n"
174 "a=ice-ufrag:e5785931\r\n"
175 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
176 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
177 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
178 "a=mid:1\r\n"
179 "a=sendrecv\r\n"
180 "a=rtcp-mux\r\n"
181 "a=rtpmap:120 VP8/90000\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000182 "a=ssrc:2 cname:stream1\r\n"
183 "a=ssrc:2 mslabel:stream1\r\n"
184 "a=ssrc:2 label:videotrack0\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700185
zhihuang81c3a032016-11-17 12:06:24 -0800186// Reference SDP with a MediaStream with label "stream1" and audio track with
187// id "audio_1";
188static const char kSdpStringWithStream1AudioTrackOnly[] =
189 "v=0\r\n"
190 "o=- 0 0 IN IP4 127.0.0.1\r\n"
191 "s=-\r\n"
192 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800193 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800194 "a=ice-ufrag:e5785931\r\n"
195 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
196 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
197 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800198 "a=mid:audio\r\n"
199 "a=sendrecv\r\n"
200 "a=rtpmap:103 ISAC/16000\r\n"
201 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000202 "a=ssrc:1 mslabel:stream1\r\n"
203 "a=ssrc:1 label:audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800204 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800205
deadbeefab9b2d12015-10-14 11:33:11 -0700206// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
207// MediaStreams have one audio track and one video track.
208// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800209static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "v=0\r\n"
211 "o=- 0 0 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800214 "a=msid-semantic: WMS stream1 stream2\r\n"
215 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700216 "a=ice-ufrag:e5785931\r\n"
217 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
218 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
219 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700221 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800222 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=rtpmap:103 ISAC/16000\r\n"
224 "a=ssrc:1 cname:stream1\r\n"
225 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
226 "a=ssrc:3 cname:stream2\r\n"
227 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
228 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800229 "a=ice-ufrag:e5785931\r\n"
230 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
231 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
232 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700233 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700234 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800235 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700236 "a=rtpmap:120 VP8/0\r\n"
237 "a=ssrc:2 cname:stream1\r\n"
238 "a=ssrc:2 msid:stream1 videotrack0\r\n"
239 "a=ssrc:4 cname:stream2\r\n"
240 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800241static const char kSdpStringWithStream1And2UnifiedPlan[] =
242 "v=0\r\n"
243 "o=- 0 0 IN IP4 127.0.0.1\r\n"
244 "s=-\r\n"
245 "t=0 0\r\n"
246 "a=msid-semantic: WMS stream1 stream2\r\n"
247 "m=audio 1 RTP/AVPF 103\r\n"
248 "a=ice-ufrag:e5785931\r\n"
249 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
250 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
251 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
252 "a=mid:0\r\n"
253 "a=sendrecv\r\n"
254 "a=rtcp-mux\r\n"
255 "a=rtpmap:103 ISAC/16000\r\n"
256 "a=ssrc:1 cname:stream1\r\n"
257 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
258 "m=video 1 RTP/AVPF 120\r\n"
259 "a=ice-ufrag:e5785931\r\n"
260 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
261 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
262 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
263 "a=mid:1\r\n"
264 "a=sendrecv\r\n"
265 "a=rtcp-mux\r\n"
266 "a=rtpmap:120 VP8/0\r\n"
267 "a=ssrc:2 cname:stream1\r\n"
268 "a=ssrc:2 msid:stream1 videotrack0\r\n"
269 "m=audio 1 RTP/AVPF 103\r\n"
270 "a=ice-ufrag:e5785931\r\n"
271 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
272 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
273 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
274 "a=mid:2\r\n"
275 "a=sendrecv\r\n"
276 "a=rtcp-mux\r\n"
277 "a=rtpmap:103 ISAC/16000\r\n"
278 "a=ssrc:3 cname:stream2\r\n"
279 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
280 "m=video 1 RTP/AVPF 120\r\n"
281 "a=ice-ufrag:e5785931\r\n"
282 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
283 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
284 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
285 "a=mid:3\r\n"
286 "a=sendrecv\r\n"
287 "a=rtcp-mux\r\n"
288 "a=rtpmap:120 VP8/0\r\n"
289 "a=ssrc:4 cname:stream2\r\n"
290 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700291
292// Reference SDP without MediaStreams. Msid is not supported.
293static const char kSdpStringWithoutStreams[] =
294 "v=0\r\n"
295 "o=- 0 0 IN IP4 127.0.0.1\r\n"
296 "s=-\r\n"
297 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800298 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700299 "a=ice-ufrag:e5785931\r\n"
300 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
301 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
302 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700303 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700304 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800305 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700306 "a=rtpmap:103 ISAC/16000\r\n"
307 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800308 "a=ice-ufrag:e5785931\r\n"
309 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
310 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
311 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700312 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700313 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800314 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700315 "a=rtpmap:120 VP8/90000\r\n";
316
317// Reference SDP without MediaStreams. Msid is supported.
318static const char kSdpStringWithMsidWithoutStreams[] =
319 "v=0\r\n"
320 "o=- 0 0 IN IP4 127.0.0.1\r\n"
321 "s=-\r\n"
322 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800323 "a=msid-semantic: WMS\r\n"
324 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700325 "a=ice-ufrag:e5785931\r\n"
326 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
327 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
328 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700329 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700330 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800331 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700332 "a=rtpmap:103 ISAC/16000\r\n"
333 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800334 "a=ice-ufrag:e5785931\r\n"
335 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
336 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
337 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700338 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700339 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800340 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700341 "a=rtpmap:120 VP8/90000\r\n";
342
343// Reference SDP without MediaStreams and audio only.
344static const char kSdpStringWithoutStreamsAudioOnly[] =
345 "v=0\r\n"
346 "o=- 0 0 IN IP4 127.0.0.1\r\n"
347 "s=-\r\n"
348 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800349 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700350 "a=ice-ufrag:e5785931\r\n"
351 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
352 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
353 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700354 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700355 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800356 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700357 "a=rtpmap:103 ISAC/16000\r\n";
358
359// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
360static const char kSdpStringSendOnlyWithoutStreams[] =
361 "v=0\r\n"
362 "o=- 0 0 IN IP4 127.0.0.1\r\n"
363 "s=-\r\n"
364 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800365 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700366 "a=ice-ufrag:e5785931\r\n"
367 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
368 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
369 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700370 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700371 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700372 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800373 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700374 "a=rtpmap:103 ISAC/16000\r\n"
375 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800376 "a=ice-ufrag:e5785931\r\n"
377 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
378 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
379 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700380 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700381 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700382 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800383 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700384 "a=rtpmap:120 VP8/90000\r\n";
385
386static const char kSdpStringInit[] =
387 "v=0\r\n"
388 "o=- 0 0 IN IP4 127.0.0.1\r\n"
389 "s=-\r\n"
390 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700391 "a=msid-semantic: WMS\r\n";
392
393static const char kSdpStringAudio[] =
394 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800395 "a=ice-ufrag:e5785931\r\n"
396 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
397 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
398 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700399 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700400 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800401 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700402 "a=rtpmap:103 ISAC/16000\r\n";
403
404static const char kSdpStringVideo[] =
405 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800406 "a=ice-ufrag:e5785931\r\n"
407 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
408 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
409 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700410 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700411 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800412 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700413 "a=rtpmap:120 VP8/90000\r\n";
414
415static const char kSdpStringMs1Audio0[] =
416 "a=ssrc:1 cname:stream1\r\n"
417 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
418
419static const char kSdpStringMs1Video0[] =
420 "a=ssrc:2 cname:stream1\r\n"
421 "a=ssrc:2 msid:stream1 videotrack0\r\n";
422
423static const char kSdpStringMs1Audio1[] =
424 "a=ssrc:3 cname:stream1\r\n"
425 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
426
427static const char kSdpStringMs1Video1[] =
428 "a=ssrc:4 cname:stream1\r\n"
429 "a=ssrc:4 msid:stream1 videotrack1\r\n";
430
deadbeef8662f942017-01-20 21:20:51 -0800431static const char kDtlsSdesFallbackSdp[] =
432 "v=0\r\n"
433 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
434 "s=-\r\n"
435 "c=IN IP4 0.0.0.0\r\n"
436 "t=0 0\r\n"
437 "a=group:BUNDLE audio\r\n"
438 "a=msid-semantic: WMS\r\n"
439 "m=audio 1 RTP/SAVPF 0\r\n"
440 "a=sendrecv\r\n"
441 "a=rtcp-mux\r\n"
442 "a=mid:audio\r\n"
443 "a=ssrc:1 cname:stream1\r\n"
Artem Titovf0a34f22020-03-16 17:52:04 +0000444 "a=ssrc:1 mslabel:stream1\r\n"
445 "a=ssrc:1 label:audiotrack0\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800446 "a=ice-ufrag:e5785931\r\n"
447 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
448 "a=rtpmap:0 pcmu/8000\r\n"
449 "a=fingerprint:sha-1 "
450 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
451 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700452 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800453 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
454 "dummy_session_params\r\n";
455
Niels Möllerdec9f742019-06-03 15:25:20 +0200456class RtcEventLogOutputNull final : public RtcEventLogOutput {
457 public:
458 bool IsActive() const override { return true; }
459 bool Write(const std::string& output) override { return true; }
460};
461
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100462using ::cricket::StreamParams;
perkjd61bf802016-03-24 03:16:19 -0700463using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800464using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
Steve Anton36da6ff2018-02-16 16:04:20 -0800466using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
467using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700468
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469// Gets the first ssrc of given content type from the ContentInfo.
470bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
471 if (!content_info || !ssrc) {
472 return false;
473 }
474 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800475 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 if (!media_desc || media_desc->streams().empty()) {
477 return false;
478 }
479 *ssrc = media_desc->streams().begin()->first_ssrc();
480 return true;
481}
482
deadbeefd1a38b52016-12-10 13:15:33 -0800483// Get the ufrags out of an SDP blob. Useful for testing ICE restart
484// behavior.
485std::vector<std::string> GetUfrags(
486 const webrtc::SessionDescriptionInterface* desc) {
487 std::vector<std::string> ufrags;
488 for (const cricket::TransportInfo& info :
489 desc->description()->transport_infos()) {
490 ufrags.push_back(info.description.ice_ufrag);
491 }
492 return ufrags;
493}
494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495void SetSsrcToZero(std::string* sdp) {
496 const char kSdpSsrcAtribute[] = "a=ssrc:";
497 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
498 size_t ssrc_pos = 0;
499 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200500 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 size_t end_ssrc = sdp->find(" ", ssrc_pos);
502 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
503 ssrc_pos = end_ssrc;
504 }
505}
506
Artem Titov880fa812021-07-30 22:30:23 +0200507// Check if `streams` contains the specified track.
deadbeefab9b2d12015-10-14 11:33:11 -0700508bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800509 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700510 const std::string& track_id) {
511 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800512 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700513 return true;
514 }
515 }
516 return false;
517}
518
Artem Titov880fa812021-07-30 22:30:23 +0200519// Check if `senders` contains the specified sender, by id.
deadbeefab9b2d12015-10-14 11:33:11 -0700520bool ContainsSender(
521 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
522 const std::string& id) {
523 for (const auto& sender : senders) {
524 if (sender->id() == id) {
525 return true;
526 }
527 }
528 return false;
529}
530
Artem Titov880fa812021-07-30 22:30:23 +0200531// Check if `senders` contains the specified sender, by id and stream id.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700532bool ContainsSender(
533 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
534 const std::string& id,
535 const std::string& stream_id) {
536 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700537 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700538 return true;
539 }
540 }
541 return false;
542}
543
deadbeefab9b2d12015-10-14 11:33:11 -0700544// Create a collection of streams.
545// CreateStreamCollection(1) creates a collection that
546// correspond to kSdpStringWithStream1.
547// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
548rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700549 int number_of_streams,
550 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700551 rtc::scoped_refptr<StreamCollection> local_collection(
552 StreamCollection::Create());
553
554 for (int i = 0; i < number_of_streams; ++i) {
555 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
556 webrtc::MediaStream::Create(kStreams[i]));
557
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700558 for (int j = 0; j < tracks_per_stream; ++j) {
559 // Add a local audio track.
560 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
561 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
562 nullptr));
563 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700564
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700565 // Add a local video track.
566 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
567 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700568 webrtc::FakeVideoTrackSource::Create(),
569 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700570 stream->AddTrack(video_track);
571 }
deadbeefab9b2d12015-10-14 11:33:11 -0700572
573 local_collection->AddStream(stream);
574 }
575 return local_collection;
576}
577
578// Check equality of StreamCollections.
579bool CompareStreamCollections(StreamCollectionInterface* s1,
580 StreamCollectionInterface* s2) {
581 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
582 return false;
583 }
584
585 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700586 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700587 return false;
588 }
589 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
590 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
591 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
592 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
593
594 if (audio_tracks1.size() != audio_tracks2.size()) {
595 return false;
596 }
597 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
598 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
599 return false;
600 }
601 }
602 if (video_tracks1.size() != video_tracks2.size()) {
603 return false;
604 }
605 for (size_t j = 0; j != video_tracks1.size(); ++j) {
606 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
607 return false;
608 }
609 }
610 }
611 return true;
612}
613
perkjd61bf802016-03-24 03:16:19 -0700614// Helper class to test Observer.
615class MockTrackObserver : public ObserverInterface {
616 public:
617 explicit MockTrackObserver(NotifierInterface* notifier)
618 : notifier_(notifier) {
619 notifier_->RegisterObserver(this);
620 }
621
622 ~MockTrackObserver() { Unregister(); }
623
624 void Unregister() {
625 if (notifier_) {
626 notifier_->UnregisterObserver(this);
627 notifier_ = nullptr;
628 }
629 }
630
Danil Chapovalov3a353122020-05-15 11:16:53 +0200631 MOCK_METHOD(void, OnChanged, (), (override));
perkjd61bf802016-03-24 03:16:19 -0700632
633 private:
634 NotifierInterface* notifier_;
635};
636
nisse528b7932017-05-08 03:21:43 -0700637// The PeerConnectionMediaConfig tests below verify that configuration and
638// constraints are propagated into the PeerConnection's MediaConfig. These
639// settings are intended for MediaChannel constructors, but that is not
640// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700641class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
642 public:
zhihuang38ede132017-06-15 12:52:32 -0700643 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
644 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100645 PeerConnectionFactoryDependencies dependencies;
646 dependencies.worker_thread = rtc::Thread::Current();
647 dependencies.network_thread = rtc::Thread::Current();
648 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200649 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200650 dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200651 cricket::MediaEngineDependencies media_deps;
652 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
henrika919dc2e2017-10-12 14:24:55 +0200653 // Use fake audio device module since we're only testing the interface
654 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200655 media_deps.adm = FakeAudioCaptureModule::Create();
656 SetMediaEngineDefaults(&media_deps);
Erik Språngceb44952020-09-22 11:36:35 +0200657 media_deps.trials = dependencies.trials.get();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200658 dependencies.media_engine =
659 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100660 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200661 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200662 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700663
Tommi87f70902021-04-27 14:43:08 +0200664 return rtc::make_ref_counted<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100665 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700666 }
667
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100668 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800669
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100670 private:
deadbeefd7850b22017-08-23 10:59:19 -0700671 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700672};
673
Steve Anton36da6ff2018-02-16 16:04:20 -0800674// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200675class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800677 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
678 : vss_(new rtc::VirtualSocketServer()),
679 main_(vss_.get()),
680 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800681#ifdef WEBRTC_ANDROID
682 webrtc::InitializeAndroidObjects();
683#endif
684 }
685
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200686 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700687 // Use fake audio capture module since we're only testing the interface
688 // level, and using a real one could make tests flaky when run in parallel.
689 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700691 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200692 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
693 fake_audio_capture_module_),
694 webrtc::CreateBuiltinAudioEncoderFactory(),
695 webrtc::CreateBuiltinAudioDecoderFactory(),
696 webrtc::CreateBuiltinVideoEncoderFactory(),
697 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
698 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700699 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700700 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700701 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200704 void TearDown() override {
705 if (pc_)
706 pc_->Close();
707 }
708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200710 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000713 // DTLS does not work in a loopback call, so is disabled for many
deadbeef293e9262017-01-11 12:28:30 -0800714 // tests in this file.
715 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200716 RTCConfiguration config;
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000717 PeerConnectionFactoryInterface::Options options;
718 options.disable_encryption = true;
719 pc_factory_->SetOptions(options);
Niels Möllerf06f9232018-08-07 12:32:18 +0200720 CreatePeerConnection(config);
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000721 options.disable_encryption = false;
722 pc_factory_->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725 void CreatePeerConnectionWithIceTransportsType(
726 PeerConnectionInterface::IceTransportsType type) {
727 PeerConnectionInterface::RTCConfiguration config;
728 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200729 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 }
731
732 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100733 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800735 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700737 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100738 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 server.password = password;
740 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200741 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
Niels Möllerf06f9232018-08-07 12:32:18 +0200744 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200745 if (pc_) {
746 pc_->Close();
747 pc_ = nullptr;
748 }
kwibergd1fe2812016-04-27 06:47:29 -0700749 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800750 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
751 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000752
deadbeef1dcb1642017-03-29 21:08:16 -0700753 // Create certificate generator unless DTLS constraint is explicitly set to
754 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200755 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200756
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000757 // These won't be used if encryption is turned off, but that's harmless.
758 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
759 cert_generator.reset(fake_certificate_generator_);
760
Steve Anton36da6ff2018-02-16 16:04:20 -0800761 RTCConfiguration modified_config = config;
762 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200763 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200764 modified_config, std::move(port_allocator), std::move(cert_generator),
765 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 ASSERT_TRUE(pc_.get() != NULL);
767 observer_.SetPeerConnectionInterface(pc_.get());
768 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
769 }
770
deadbeef0a6c4ca2015-10-06 11:38:28 -0700771 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700773 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700774 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800776 config.sdp_semantics = sdp_semantics_;
777 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200778 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700780 }
781
Steve Anton038834f2017-07-14 15:59:59 -0700782 void CreatePeerConnectionExpectFail(
783 PeerConnectionInterface::RTCConfiguration config) {
784 PeerConnectionInterface::IceServer server;
785 server.uri = kTurnIceServerUri;
786 server.password = kTurnPassword;
787 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800788 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700789 rtc::scoped_refptr<PeerConnectionInterface> pc =
790 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
791 EXPECT_EQ(nullptr, pc);
792 }
793
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100795 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
797 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
798 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801
deadbeef0a6c4ca2015-10-06 11:38:28 -0700802 CreatePeerConnectionExpectFail(kStunInvalidPort);
803 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
804 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805
Niels Möllerdb4def92019-03-18 16:53:59 +0100806 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
807 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800808 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
809 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800811 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800815 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817
818 void ReleasePeerConnection() {
819 pc_ = NULL;
820 observer_.SetPeerConnectionInterface(NULL);
821 }
822
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
824 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100825 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800826 }
827
828 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800829 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800830 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800831 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
Steve Anton36da6ff2018-02-16 16:04:20 -0800835 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700836 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800838 stream->AddTrack(CreateVideoTrack(label + "v0"));
839 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800840 }
841
842 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
843 const std::string& label) {
844 return pc_factory_->CreateAudioTrack(label, nullptr);
845 }
846
847 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800848 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800849 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800850 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800851 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800852 }
853
854 void AddAudioStream(const std::string& label) {
855 rtc::scoped_refptr<MediaStreamInterface> stream(
856 pc_factory_->CreateLocalMediaStream(label));
857 stream->AddTrack(CreateAudioTrack(label + "a0"));
858 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
Seth Hampson845e8782018-03-02 11:34:10 -0800861 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 const std::string& audio_track_label,
863 const std::string& video_track_label) {
864 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700865 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800866 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800867 stream->AddTrack(CreateAudioTrack(audio_track_label));
868 stream->AddTrack(CreateVideoTrack(video_track_label));
869 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 }
871
Steve Anton36da6ff2018-02-16 16:04:20 -0800872 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
873 cricket::MediaType media_type) {
874 for (auto receiver : pc_->GetReceivers()) {
875 if (receiver->media_type() == media_type) {
876 return receiver;
877 }
878 }
879 return nullptr;
880 }
881
kwibergd1fe2812016-04-27 06:47:29 -0700882 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200883 const RTCOfferAnswerOptions* options,
884 bool offer) {
Tommi87f70902021-04-27 14:43:08 +0200885 auto observer =
886 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200888 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200890 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700893 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 return observer->result();
895 }
896
kwibergd1fe2812016-04-27 06:47:29 -0700897 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200898 const RTCOfferAnswerOptions* options) {
899 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901
kwibergd1fe2812016-04-27 06:47:29 -0700902 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200903 const RTCOfferAnswerOptions* options) {
904 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906
Steve Antondb45ca82017-09-11 18:27:34 -0700907 bool DoSetSessionDescription(
908 std::unique_ptr<SessionDescriptionInterface> desc,
909 bool local) {
Tommi87f70902021-04-27 14:43:08 +0200910 auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700912 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700914 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
zhihuang29ff8442016-07-27 11:07:25 -0700916 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
917 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
918 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return observer->result();
920 }
921
Steve Antondb45ca82017-09-11 18:27:34 -0700922 bool DoSetLocalDescription(
923 std::unique_ptr<SessionDescriptionInterface> desc) {
924 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
Steve Antondb45ca82017-09-11 18:27:34 -0700927 bool DoSetRemoteDescription(
928 std::unique_ptr<SessionDescriptionInterface> desc) {
929 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 }
931
932 // Calls PeerConnection::GetStats and check the return value.
933 // It does not verify the values in the StatReports since a RTCP packet might
934 // be required.
935 bool DoGetStats(MediaStreamTrackInterface* track) {
Tommi87f70902021-04-27 14:43:08 +0200936 auto observer = rtc::make_ref_counted<MockStatsObserver>();
Yves Gerey665174f2018-06-19 15:03:05 +0200937 if (!pc_->GetStats(observer, track,
938 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 return false;
940 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
941 return observer->called();
942 }
943
Harald Alvestrand89061872018-01-02 14:08:34 +0100944 // Call the standards-compliant GetStats function.
945 bool DoGetRTCStats() {
Tommi87f70902021-04-27 14:43:08 +0200946 auto callback =
947 rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
Harald Alvestrand89061872018-01-02 14:08:34 +0100948 pc_->GetStats(callback);
949 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
950 return callback->called();
951 }
952
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800954 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800956 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800957 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800958 } else {
959 // Unified Plan does not support AddStream, so just add an audio and video
960 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800961 AddAudioTrack(kAudioTracks[0], {kStreamId1});
962 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800963 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 CreateOfferReceiveAnswer();
965 }
966
967 // Verify that RTP Header extensions has been negotiated for audio and video.
968 void VerifyRemoteRtpHeaderExtensions() {
969 const cricket::MediaContentDescription* desc =
970 cricket::GetFirstAudioContentDescription(
971 pc_->remote_description()->description());
972 ASSERT_TRUE(desc != NULL);
973 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
974
975 desc = cricket::GetFirstVideoContentDescription(
976 pc_->remote_description()->description());
977 ASSERT_TRUE(desc != NULL);
978 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
979 }
980
981 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700982 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 std::string sdp;
985 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700986 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800987 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700988 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
990 }
991
deadbeefab9b2d12015-10-14 11:33:11 -0700992 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700993 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800994 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700995 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700996 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
997 }
998
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001001 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002
1003 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1004 // audio codec change, even if the parameter has nothing to do with
1005 // receiving. Not all parameters are serialized to SDP.
1006 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1007 // the SessionDescription, it is necessary to do that here to in order to
1008 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1009 // https://code.google.com/p/webrtc/issues/detail?id=1356
1010 std::string sdp;
1011 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001012 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001013 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001014 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1016 }
1017
1018 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001019 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001020 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
1022 std::string sdp;
1023 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001024 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001025 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001026 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1028 }
1029
1030 void CreateOfferReceiveAnswer() {
1031 CreateOfferAsLocalDescription();
1032 std::string sdp;
1033 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1034 CreateAnswerAsRemoteDescription(sdp);
1035 }
1036
1037 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001038 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001039 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1041 // audio codec change, even if the parameter has nothing to do with
1042 // receiving. Not all parameters are serialized to SDP.
1043 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1044 // the SessionDescription, it is necessary to do that here to in order to
1045 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1046 // https://code.google.com/p/webrtc/issues/detail?id=1356
1047 std::string sdp;
1048 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001049 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001050 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051
Steve Antondb45ca82017-09-11 18:27:34 -07001052 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001054 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001055 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 }
1057
deadbeefab9b2d12015-10-14 11:33:11 -07001058 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001059 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001060 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001061 ASSERT_TRUE(answer);
1062 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1064 }
1065
deadbeefab9b2d12015-10-14 11:33:11 -07001066 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001067 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001068 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001069 ASSERT_TRUE(pr_answer);
1070 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001072 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001073 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001074 ASSERT_TRUE(answer);
1075 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1077 }
1078
Seth Hampson845e8782018-03-02 11:34:10 -08001079 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001080 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001081 // called with the given stream id and expected number of tracks.
1082 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001083 int expected_num_tracks) {
1084 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001085 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001086 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001087 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 }
1089
1090 // Creates an offer and applies it as a local session description.
1091 // Creates an answer with the same SDP an the offer but removes all lines
1092 // that start with a:ssrc"
1093 void CreateOfferReceiveAnswerWithoutSsrc() {
1094 CreateOfferAsLocalDescription();
1095 std::string sdp;
1096 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1097 SetSsrcToZero(&sdp);
1098 CreateAnswerAsRemoteDescription(sdp);
1099 }
1100
deadbeefab9b2d12015-10-14 11:33:11 -07001101 // This function creates a MediaStream with label kStreams[0] and
Artem Titov880fa812021-07-30 22:30:23 +02001102 // `number_of_audio_tracks` and `number_of_video_tracks` tracks and the
deadbeefab9b2d12015-10-14 11:33:11 -07001103 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001104 // is returned and the MediaStream is stored in
Artem Titov880fa812021-07-30 22:30:23 +02001105 // `reference_collection_`
kwibergd1fe2812016-04-27 06:47:29 -07001106 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001107 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1108 size_t number_of_video_tracks) {
1109 EXPECT_LE(number_of_audio_tracks, 2u);
1110 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001111
1112 reference_collection_ = StreamCollection::Create();
1113 std::string sdp_ms1 = std::string(kSdpStringInit);
1114
Seth Hampson845e8782018-03-02 11:34:10 -08001115 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001116
1117 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001118 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001119 reference_collection_->AddStream(stream);
1120
1121 if (number_of_audio_tracks > 0) {
1122 sdp_ms1 += std::string(kSdpStringAudio);
1123 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1124 AddAudioTrack(kAudioTracks[0], stream);
1125 }
1126 if (number_of_audio_tracks > 1) {
1127 sdp_ms1 += kSdpStringMs1Audio1;
1128 AddAudioTrack(kAudioTracks[1], stream);
1129 }
1130
1131 if (number_of_video_tracks > 0) {
1132 sdp_ms1 += std::string(kSdpStringVideo);
1133 sdp_ms1 += std::string(kSdpStringMs1Video0);
1134 AddVideoTrack(kVideoTracks[0], stream);
1135 }
1136 if (number_of_video_tracks > 1) {
1137 sdp_ms1 += kSdpStringMs1Video1;
1138 AddVideoTrack(kVideoTracks[1], stream);
1139 }
1140
kwibergd1fe2812016-04-27 06:47:29 -07001141 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001142 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001143 }
1144
1145 void AddAudioTrack(const std::string& track_id,
1146 MediaStreamInterface* stream) {
1147 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1148 webrtc::AudioTrack::Create(track_id, nullptr));
1149 ASSERT_TRUE(stream->AddTrack(audio_track));
1150 }
1151
1152 void AddVideoTrack(const std::string& track_id,
1153 MediaStreamInterface* stream) {
1154 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001155 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001156 webrtc::FakeVideoTrackSource::Create(),
1157 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001158 ASSERT_TRUE(stream->AddTrack(video_track));
1159 }
1160
Steve Anton36da6ff2018-02-16 16:04:20 -08001161 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001162 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001163 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001164 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001165 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1166 return offer;
1167 }
1168
Steve Anton36da6ff2018-02-16 16:04:20 -08001169 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1170 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001171 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001172 std::unique_ptr<SessionDescriptionInterface> offer;
1173 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1174 return offer;
1175 }
1176
1177 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1178 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1179 std::unique_ptr<SessionDescriptionInterface> answer;
1180 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1181 return answer;
1182 }
1183
kwibergfd8be342016-05-14 19:44:11 -07001184 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001186 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001187 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001188 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1189 return answer;
1190 }
1191
1192 const std::string& GetFirstAudioStreamCname(
1193 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001194 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001195 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001196 return audio_desc->streams()[0].cname;
1197 }
1198
zhihuang1c378ed2017-08-17 14:10:50 -07001199 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1200 const RTCOfferAnswerOptions& offer_answer_options) {
1201 RTC_DCHECK(pc_);
Tommi87f70902021-04-27 14:43:08 +02001202 auto observer =
1203 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
zhihuang1c378ed2017-08-17 14:10:50 -07001204 pc_->CreateOffer(observer, offer_answer_options);
1205 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1206 return observer->MoveDescription();
1207 }
1208
1209 void CreateOfferWithOptionsAsRemoteDescription(
1210 std::unique_ptr<SessionDescriptionInterface>* desc,
1211 const RTCOfferAnswerOptions& offer_answer_options) {
1212 *desc = CreateOfferWithOptions(offer_answer_options);
1213 ASSERT_TRUE(desc != nullptr);
1214 std::string sdp;
1215 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001216 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001217 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001218 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001219 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1220 }
1221
1222 void CreateOfferWithOptionsAsLocalDescription(
1223 std::unique_ptr<SessionDescriptionInterface>* desc,
1224 const RTCOfferAnswerOptions& offer_answer_options) {
1225 *desc = CreateOfferWithOptions(offer_answer_options);
1226 ASSERT_TRUE(desc != nullptr);
1227 std::string sdp;
1228 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001229 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001230 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001231
Steve Antondb45ca82017-09-11 18:27:34 -07001232 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001233 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1234 }
1235
1236 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001237 RTC_DCHECK(content);
1238 RTC_DCHECK(content->media_description());
1239 for (const cricket::AudioCodec& codec :
1240 content->media_description()->as_audio()->codecs()) {
1241 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001242 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001243 }
zhihuang1c378ed2017-08-17 14:10:50 -07001244 }
1245 return false;
1246 }
1247
Steve Anton36da6ff2018-02-16 16:04:20 -08001248 const char* GetSdpStringWithStream1() const {
1249 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1250 return kSdpStringWithStream1PlanB;
1251 } else {
1252 return kSdpStringWithStream1UnifiedPlan;
1253 }
1254 }
1255
1256 const char* GetSdpStringWithStream1And2() const {
1257 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1258 return kSdpStringWithStream1And2PlanB;
1259 } else {
1260 return kSdpStringWithStream1And2UnifiedPlan;
1261 }
1262 }
1263
deadbeef9a6f4d42017-05-15 19:43:33 -07001264 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1265 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001266 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001267 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001268 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001269 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1270 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1271 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001273 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001274 const SdpSemantics sdp_semantics_;
1275};
1276
1277class PeerConnectionInterfaceTest
1278 : public PeerConnectionInterfaceBaseTest,
1279 public ::testing::WithParamInterface<SdpSemantics> {
1280 protected:
1281 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1282};
1283
1284class PeerConnectionInterfaceTestPlanB
1285 : public PeerConnectionInterfaceBaseTest {
1286 protected:
1287 PeerConnectionInterfaceTestPlanB()
1288 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289};
1290
zhihuang8f65cdf2016-05-06 18:40:30 -07001291// Generate different CNAMEs when PeerConnections are created.
1292// The CNAMEs are expected to be generated randomly. It is possible
1293// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001294TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001295 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001296 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001297 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001298 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001299 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1300 GetFirstAudioStreamCname(offer2.get()));
1301}
1302
Steve Anton36da6ff2018-02-16 16:04:20 -08001303TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001304 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001305 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001306 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001307 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001308 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1309 GetFirstAudioStreamCname(answer2.get()));
1310}
1311
Steve Anton36da6ff2018-02-16 16:04:20 -08001312TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 CreatePeerConnectionWithDifferentConfigurations) {
1314 CreatePeerConnectionWithDifferentConfigurations();
1315}
1316
Steve Anton36da6ff2018-02-16 16:04:20 -08001317TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001318 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1319 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1320 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1321 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1322 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1323 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1324 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1325 port_allocator_->candidate_filter());
1326 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1327 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1328}
1329
1330// Test that when a PeerConnection is created with a nonzero candidate pool
1331// size, the pooled PortAllocatorSession is created with all the attributes
1332// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001333TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 PeerConnectionInterface::RTCConfiguration config;
1335 PeerConnectionInterface::IceServer server;
1336 server.uri = kStunAddressOnly;
1337 config.servers.push_back(server);
1338 config.type = PeerConnectionInterface::kRelay;
1339 config.disable_ipv6 = true;
1340 config.tcp_candidate_policy =
1341 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001342 config.candidate_network_policy =
1343 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001344 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001345 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346
1347 const cricket::FakePortAllocatorSession* session =
1348 static_cast<const cricket::FakePortAllocatorSession*>(
1349 port_allocator_->GetPooledSession());
1350 ASSERT_NE(nullptr, session);
1351 EXPECT_EQ(1UL, session->stun_servers().size());
1352 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1353 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001354 EXPECT_LT(0U,
1355 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001356}
1357
deadbeefd21eab32017-07-26 16:50:11 -07001358// Test that network-related RTCConfiguration members are applied to the
1359// PortAllocator when CreatePeerConnection is called. Specifically:
1360// - disable_ipv6_on_wifi
1361// - max_ipv6_networks
1362// - tcp_candidate_policy
1363// - candidate_network_policy
1364// - prune_turn_ports
1365//
1366// Note that the candidate filter (RTCConfiguration::type) is already tested
1367// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001368TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001369 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1370 // Create fake port allocator.
1371 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1372 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1373 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1374
1375 // Create RTCConfiguration with some network-related fields relevant to
1376 // PortAllocator populated.
1377 PeerConnectionInterface::RTCConfiguration config;
1378 config.disable_ipv6_on_wifi = true;
1379 config.max_ipv6_networks = 10;
1380 config.tcp_candidate_policy =
1381 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1382 config.candidate_network_policy =
1383 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1384 config.prune_turn_ports = true;
1385
1386 // Create the PC factory and PC with the above config.
1387 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1388 webrtc::CreatePeerConnectionFactory(
1389 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001390 rtc::Thread::Current(), fake_audio_capture_module_,
1391 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001392 webrtc::CreateBuiltinAudioDecoderFactory(),
1393 webrtc::CreateBuiltinVideoEncoderFactory(),
1394 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1395 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001396 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001397 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1398 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001399 EXPECT_TRUE(pc.get());
1400 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001401
1402 // Now validate that the config fields set above were applied to the
1403 // PortAllocator, as flags or otherwise.
1404 EXPECT_FALSE(raw_port_allocator->flags() &
1405 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1406 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1407 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1408 EXPECT_TRUE(raw_port_allocator->flags() &
1409 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001410 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1411 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001412}
1413
deadbeef46c73892016-11-16 19:42:04 -08001414// Check that GetConfiguration returns the configuration the PeerConnection was
1415// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001416TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001417 PeerConnectionInterface::RTCConfiguration config;
1418 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001419 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001420
1421 PeerConnectionInterface::RTCConfiguration returned_config =
1422 pc_->GetConfiguration();
1423 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1424}
1425
1426// Check that GetConfiguration returns the last configuration passed into
1427// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001428TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001429 PeerConnectionInterface::RTCConfiguration starting_config;
1430 starting_config.bundle_policy =
1431 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1432 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001433
Steve Anton36da6ff2018-02-16 16:04:20 -08001434 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001435 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001436 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001437
1438 PeerConnectionInterface::RTCConfiguration returned_config =
1439 pc_->GetConfiguration();
1440 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1441}
1442
Steve Antonc79268f2018-04-24 09:54:10 -07001443TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1444 CreatePeerConnection();
1445
1446 pc_->Close();
1447
1448 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001449 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001450}
1451
Steve Anton36da6ff2018-02-16 16:04:20 -08001452TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001453 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001454 AddVideoStream(kStreamId1);
1455 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 ASSERT_EQ(2u, pc_->local_streams()->count());
1457
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001458 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001459 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001460 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001461 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001462 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001463 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001465 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001466 EXPECT_EQ(3u, pc_->local_streams()->count());
1467
1468 // Remove the third stream.
1469 pc_->RemoveStream(pc_->local_streams()->at(2));
1470 EXPECT_EQ(2u, pc_->local_streams()->count());
1471
1472 // Remove the second stream.
1473 pc_->RemoveStream(pc_->local_streams()->at(1));
1474 EXPECT_EQ(1u, pc_->local_streams()->count());
1475
1476 // Remove the first stream.
1477 pc_->RemoveStream(pc_->local_streams()->at(0));
1478 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479}
1480
deadbeefab9b2d12015-10-14 11:33:11 -07001481// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001482// Don't run under Unified Plan since the stream API is not available.
1483TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001484 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001485 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001486 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001487 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001488
deadbeefab9b2d12015-10-14 11:33:11 -07001489 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001490 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001491 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001492
deadbeefab9b2d12015-10-14 11:33:11 -07001493 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001494 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001495 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001496
1497 // Add another stream and ensure the offer includes both the old and new
1498 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001499 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001500 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001501
Steve Antonb1c1de12017-12-21 15:14:30 -08001502 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001503 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1504 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001505
Steve Antonb1c1de12017-12-21 15:14:30 -08001506 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001507 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1508 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001509}
1510
Steve Anton36da6ff2018-02-16 16:04:20 -08001511// Don't run under Unified Plan since the stream API is not available.
1512TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001513 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001514 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 ASSERT_EQ(1u, pc_->local_streams()->count());
1516 pc_->RemoveStream(pc_->local_streams()->at(0));
1517 EXPECT_EQ(0u, pc_->local_streams()->count());
1518}
1519
deadbeefe1f9d832016-01-14 15:35:42 -08001520// Test for AddTrack and RemoveTrack methods.
1521// Tests that the created offer includes tracks we added,
1522// and that the RtpSenders are created correctly.
1523// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001524// Only tested with Plan B since Unified Plan is covered in more detail by tests
1525// in peerconnection_jsep_unittests.cc
1526TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001527 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001528 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001529 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001530 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001531 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001532 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1533 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001534 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001535 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001536 EXPECT_EQ("audio_track", audio_sender->id());
1537 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001538 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001539 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001540 EXPECT_EQ("video_track", video_sender->id());
1541 EXPECT_EQ(video_track, video_sender->track());
1542
1543 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001544 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001545 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001546
1547 const cricket::ContentInfo* audio_content =
1548 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001549 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001550 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001551
1552 const cricket::ContentInfo* video_content =
1553 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001554 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001555 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001556
Steve Antondb45ca82017-09-11 18:27:34 -07001557 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001558
1559 // Now try removing the tracks.
1560 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1561 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1562
1563 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001564 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001565
1566 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001567 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001568 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001569
1570 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001571 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001572 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001573
Steve Antondb45ca82017-09-11 18:27:34 -07001574 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001575
1576 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1577 // should return false.
1578 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1579 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1580}
1581
1582// Test creating senders without a stream specified,
1583// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001584TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001585 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001586 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001587 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001588 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001589 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001590 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001591 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001592 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001593 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001594 EXPECT_EQ("audio_track", audio_sender->id());
1595 EXPECT_EQ(audio_track, audio_sender->track());
1596 EXPECT_EQ("video_track", video_sender->id());
1597 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001598 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1599 // If the ID is truly a random GUID, it should be infinitely unlikely they
1600 // will be the same.
1601 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1602 } else {
1603 // We allows creating tracks without stream ids under Unified Plan
1604 // semantics.
1605 EXPECT_EQ(0u, video_sender->stream_ids().size());
1606 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1607 }
deadbeefe1f9d832016-01-14 15:35:42 -08001608}
1609
Harald Alvestrand89061872018-01-02 14:08:34 +01001610// Test that we can call GetStats() after AddTrack but before connecting
1611// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001612TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001613 CreatePeerConnectionWithoutDtls();
1614 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001615 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001616 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001617 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001618 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1619 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001620 EXPECT_TRUE(DoGetStats(nullptr));
1621}
1622
Steve Anton36da6ff2018-02-16 16:04:20 -08001623TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001624 CreatePeerConnectionWithoutDtls();
1625 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001626 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001627 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001628 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001629 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001630 ASSERT_TRUE(audio_sender.ok());
1631 auto* audio_sender_proxy =
1632 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1633 audio_sender.value().get());
1634 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1635
Harald Alvestrandc72af932018-01-11 17:18:19 +01001636 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001637 ASSERT_TRUE(video_sender.ok());
1638 auto* video_sender_proxy =
1639 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1640 video_sender.value().get());
1641 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001642}
1643
Steve Anton36da6ff2018-02-16 16:04:20 -08001644// Don't run under Unified Plan since the stream API is not available.
1645TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001646 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001647 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001648 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001649 ASSERT_EQ(1u, senders.size());
1650 auto* sender_proxy =
1651 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1652 senders[0].get());
1653 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001654}
1655
Steve Anton36da6ff2018-02-16 16:04:20 -08001656TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001658 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 VerifyRemoteRtpHeaderExtensions();
1660}
1661
Steve Anton36da6ff2018-02-16 16:04:20 -08001662TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001663 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001664 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 CreateOfferAsLocalDescription();
1666 std::string offer;
1667 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1668 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001669 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670}
1671
Steve Anton36da6ff2018-02-16 16:04:20 -08001672TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001673 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001674 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675
1676 CreateOfferAsRemoteDescription();
1677 CreateAnswerAsLocalDescription();
1678
Seth Hampson845e8782018-03-02 11:34:10 -08001679 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680}
1681
Steve Anton36da6ff2018-02-16 16:04:20 -08001682TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001683 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001684 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685
1686 CreateOfferAsRemoteDescription();
1687 CreatePrAnswerAsLocalDescription();
1688 CreateAnswerAsLocalDescription();
1689
Seth Hampson845e8782018-03-02 11:34:10 -08001690 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691}
1692
Steve Anton36da6ff2018-02-16 16:04:20 -08001693// Don't run under Unified Plan since the stream API is not available.
1694TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 InitiateCall();
1696 ASSERT_EQ(1u, pc_->remote_streams()->count());
1697 pc_->RemoveStream(pc_->local_streams()->at(0));
1698 CreateOfferReceiveAnswer();
1699 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001700 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 CreateOfferReceiveAnswer();
1702}
1703
1704// Tests that after negotiating an audio only call, the respondent can perform a
1705// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001706TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001707 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001708 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 CreateOfferAsRemoteDescription();
1710 CreateAnswerAsLocalDescription();
1711
1712 ASSERT_EQ(1u, pc_->remote_streams()->count());
1713 pc_->RemoveStream(pc_->local_streams()->at(0));
1714 CreateOfferReceiveAnswer();
1715 EXPECT_EQ(0u, pc_->remote_streams()->count());
1716}
1717
1718// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001719TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001720 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
Steve Antonf1c6db12017-10-13 11:13:35 -07001722 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001724 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001725 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001726 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001727 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728
1729 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001730 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001731 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001732 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733
Steve Antonf1c6db12017-10-13 11:13:35 -07001734 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001735 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736
Steve Antonf1c6db12017-10-13 11:13:35 -07001737 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738}
1739
deadbeefab9b2d12015-10-14 11:33:11 -07001740// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001742TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001743 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001745 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001746 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001747 EXPECT_TRUE(offer);
1748 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749
1750 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001751 AddAudioTrack("track_label", {kStreamId1});
1752 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753
1754 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001755 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756
1757 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001758 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001759 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760}
1761
1762// Test that we will get different SSRCs for each tracks in the offer and answer
1763// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001764TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001765 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001767 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1768 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769
1770 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001771 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001772 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 int audio_ssrc = 0;
1774 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001775 EXPECT_TRUE(
1776 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1777 EXPECT_TRUE(
1778 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 EXPECT_NE(audio_ssrc, video_ssrc);
1780
1781 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001782 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001784 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 audio_ssrc = 0;
1786 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001787 EXPECT_TRUE(
1788 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1789 EXPECT_TRUE(
1790 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 EXPECT_NE(audio_ssrc, video_ssrc);
1792}
1793
deadbeefeb459812015-12-15 19:24:43 -08001794// Test that it's possible to call AddTrack on a MediaStream after adding
1795// the stream to a PeerConnection.
1796// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001797// Don't run under Unified Plan since the stream API is not available.
1798TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001799 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001800 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001801 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001802 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1803
1804 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001805 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001806 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001807 stream->AddTrack(video_track.get());
1808
kwibergd1fe2812016-04-27 06:47:29 -07001809 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001810 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001811
1812 const cricket::MediaContentDescription* video_desc =
1813 cricket::GetFirstVideoContentDescription(offer->description());
1814 EXPECT_TRUE(video_desc != nullptr);
1815}
1816
1817// Test that it's possible to call RemoveTrack on a MediaStream after adding
1818// the stream to a PeerConnection.
1819// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001820// Don't run under Unified Plan since the stream API is not available.
1821TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001822 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001823 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001824 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001825 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1826
1827 // Remove the video track.
1828 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1829
kwibergd1fe2812016-04-27 06:47:29 -07001830 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001831 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001832
1833 const cricket::MediaContentDescription* video_desc =
1834 cricket::GetFirstVideoContentDescription(offer->description());
1835 EXPECT_TRUE(video_desc == nullptr);
1836}
1837
deadbeefbd7d8f72015-12-18 16:58:44 -08001838// Test creating a sender with a stream ID, and ensure the ID is populated
1839// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001840// Don't run under Unified Plan since the stream API is not available.
1841TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001842 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001843 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001844
kwibergd1fe2812016-04-27 06:47:29 -07001845 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001846 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001847
1848 const cricket::MediaContentDescription* video_desc =
1849 cricket::GetFirstVideoContentDescription(offer->description());
1850 ASSERT_TRUE(video_desc != nullptr);
1851 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001852 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001853}
1854
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001856TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001858 ASSERT_LT(0u, pc_->GetSenders().size());
1859 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001860 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001861 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 EXPECT_TRUE(DoGetStats(remote_audio));
1863
1864 // Remove the stream. Since we are sending to our selves the local
1865 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001866 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 // Do a re-negotiation.
1868 CreateOfferReceiveAnswer();
1869
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 // Test that we still can get statistics for the old track. Even if it is not
1871 // sent any longer.
1872 EXPECT_TRUE(DoGetStats(remote_audio));
1873}
1874
1875// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001876TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001878 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1879 ASSERT_TRUE(video_receiver);
1880 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881}
1882
1883// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001884TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 pc_factory_->CreateAudioTrack("unknown track", NULL));
1888 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1889}
1890
Steve Anton36da6ff2018-02-16 16:04:20 -08001891TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001892 CreatePeerConnectionWithoutDtls();
1893 EXPECT_TRUE(DoGetRTCStats());
1894 // Clearing stats cache is needed now, but should be temporary.
1895 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1896 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001897 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1898 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001899 EXPECT_TRUE(DoGetRTCStats());
1900 pc_->ClearStatsCache();
1901 CreateOfferReceiveAnswer();
1902 EXPECT_TRUE(DoGetRTCStats());
1903}
1904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905// This tests that a SCTP data channel is returned using different
1906// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001907TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001908 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001909 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910
1911 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001912 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 pc_->CreateDataChannel("1", &config);
1914 EXPECT_TRUE(channel != NULL);
1915 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001916 EXPECT_TRUE(observer_.renegotiation_needed_);
1917 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918
1919 config.ordered = false;
1920 channel = pc_->CreateDataChannel("2", &config);
1921 EXPECT_TRUE(channel != NULL);
1922 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001923 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924
1925 config.ordered = true;
1926 config.maxRetransmits = 0;
1927 channel = pc_->CreateDataChannel("3", &config);
1928 EXPECT_TRUE(channel != NULL);
1929 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001930 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001932 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 config.maxRetransmitTime = 0;
1934 channel = pc_->CreateDataChannel("4", &config);
1935 EXPECT_TRUE(channel != NULL);
1936 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001937 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938}
1939
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001940// For backwards compatibility, we want people who "unset" maxRetransmits
1941// and maxRetransmitTime by setting them to -1 to get what they want.
1942TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
1943 RTCConfiguration rtc_config;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02001944 CreatePeerConnection(rtc_config);
1945
1946 webrtc::DataChannelInit config;
1947 config.maxRetransmitTime = -1;
1948 config.maxRetransmits = -1;
1949 rtc::scoped_refptr<DataChannelInterface> channel =
1950 pc_->CreateDataChannel("1", &config);
1951 EXPECT_TRUE(channel != NULL);
1952}
1953
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954// This tests that no data channel is returned if both maxRetransmits and
1955// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001956TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001958 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02001959 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;
Niels Möllerf06f9232018-08-07 12:32:18 +02001976 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977
1978 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001979 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001981 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001982 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001983 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 EXPECT_TRUE(channel != NULL);
1985 EXPECT_EQ(1, channel->id());
1986
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 channel = pc_->CreateDataChannel("x", &config);
1988 EXPECT_TRUE(channel == NULL);
1989
1990 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001991 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 channel = pc_->CreateDataChannel("max", &config);
1993 EXPECT_TRUE(channel != NULL);
1994 EXPECT_EQ(config.id, channel->id());
1995
1996 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001997 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 channel = pc_->CreateDataChannel("x", &config);
1999 EXPECT_TRUE(channel == NULL);
2000}
2001
deadbeefab9b2d12015-10-14 11:33:11 -07002002// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002003TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002004 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002005 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002006
2007 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002008 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002009 pc_->CreateDataChannel(label, nullptr);
2010 EXPECT_NE(channel, nullptr);
2011
zhihuang9763d562016-08-05 11:14:50 -07002012 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002013 pc_->CreateDataChannel(label, nullptr);
2014 EXPECT_NE(dup_channel, nullptr);
2015}
2016
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002018#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002019// This tests that SCTP data channels can be rejected in an answer.
2020TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2021#else
2022TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2023#endif
2024{
Niels Möllerf06f9232018-08-07 12:32:18 +02002025 RTCConfiguration rtc_config;
2026 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002027
2028 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2029 pc_->CreateDataChannel("offer_channel", NULL));
2030
2031 CreateOfferAsLocalDescription();
2032
2033 // Create an answer where the m-line for data channels are rejected.
2034 std::string sdp;
2035 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2036 std::unique_ptr<SessionDescriptionInterface> answer(
2037 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2038 ASSERT_TRUE(answer);
2039 cricket::ContentInfo* data_info =
2040 cricket::GetFirstDataContent(answer->description());
2041 data_info->rejected = true;
2042
2043 DoSetRemoteDescription(std::move(answer));
2044 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2045}
2046
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047// Test that we can create a session description from an SDP string from
2048// FireFox, use it as a remote session description, generate an answer and use
2049// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002050TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002051 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002052 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002053 AddAudioTrack("audio_label");
2054 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002055 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002056 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002057 webrtc::kFireFoxSdpOffer, nullptr));
2058 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 CreateAnswerAsLocalDescription();
2060 ASSERT_TRUE(pc_->local_description() != NULL);
2061 ASSERT_TRUE(pc_->remote_description() != NULL);
2062
2063 const cricket::ContentInfo* content =
2064 cricket::GetFirstAudioContent(pc_->local_description()->description());
2065 ASSERT_TRUE(content != NULL);
2066 EXPECT_FALSE(content->rejected);
2067
2068 content =
2069 cricket::GetFirstVideoContent(pc_->local_description()->description());
2070 ASSERT_TRUE(content != NULL);
2071 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002072#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 content =
2074 cricket::GetFirstDataContent(pc_->local_description()->description());
2075 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002076 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002077#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078}
2079
Harald Alvestrand0d018412021-11-04 13:52:31 +00002080// Test that fallback from DTLS to SDES is not supported.
2081// The fallback was previously supported but was removed to simplify the code
2082// and because it's non-standard.
2083TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002084 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002085 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002086 // Wait for fake certificate to be generated. Previously, this is what caused
2087 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002088 AddAudioTrack("audio_label");
2089 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002090 ASSERT_NE(nullptr, fake_certificate_generator_);
2091 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2092 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002093 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002094 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2095 nullptr));
Harald Alvestrand0d018412021-11-04 13:52:31 +00002096 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002097}
2098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099// Test that we can create an audio only offer and receive an answer with a
2100// limited set of audio codecs and receive an updated offer with more audio
2101// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002102TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002103 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002104 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 CreateOfferAsLocalDescription();
2106
Steve Anton36da6ff2018-02-16 16:04:20 -08002107 const char* answer_sdp =
2108 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2109 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002110 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002111 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002112 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113
Steve Anton36da6ff2018-02-16 16:04:20 -08002114 const char* reoffer_sdp =
2115 (sdp_semantics_ == SdpSemantics::kPlanB
2116 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2117 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002118 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002119 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002120 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 CreateAnswerAsLocalDescription();
2122}
2123
deadbeefc80741f2015-10-22 13:14:45 -07002124// Test that if we're receiving (but not sending) a track, subsequent offers
2125// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002126TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002127 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002128 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002129 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002130 CreateAnswerAsLocalDescription();
2131
2132 // At this point we should be receiving stream 1, but not sending anything.
2133 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002134 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002135 DoCreateOffer(&offer, nullptr);
2136
2137 const cricket::ContentInfo* video_content =
2138 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002139 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2140 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002141
2142 const cricket::ContentInfo* audio_content =
2143 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002144 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2145 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002146}
2147
2148// Test that if we're receiving (but not sending) a track, and the
2149// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2150// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002151TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002152 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002153 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002154 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002155 CreateAnswerAsLocalDescription();
2156
2157 // At this point we should be receiving stream 1, but not sending anything.
2158 // A new offer would be recvonly, but we'll set the "no receive" constraints
2159 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002160 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002161 RTCOfferAnswerOptions options;
2162 options.offer_to_receive_audio = 0;
2163 options.offer_to_receive_video = 0;
2164 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002165
2166 const cricket::ContentInfo* video_content =
2167 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002168 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2169 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002170
2171 const cricket::ContentInfo* audio_content =
2172 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002173 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2174 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002175}
2176
deadbeef653b8e02015-11-11 12:55:10 -08002177// Test that we can use SetConfiguration to change the ICE servers of the
2178// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002179TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002180 CreatePeerConnection();
2181
Steve Anton36da6ff2018-02-16 16:04:20 -08002182 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002183 PeerConnectionInterface::IceServer server;
2184 server.uri = "stun:test_hostname";
2185 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002186 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002187
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002188 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2189 EXPECT_EQ("test_hostname",
2190 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002191}
2192
Steve Anton36da6ff2018-02-16 16:04:20 -08002193TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002194 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002195 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002196 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002197 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002198 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2199}
2200
Steve Anton36da6ff2018-02-16 16:04:20 -08002201TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002202 PeerConnectionInterface::RTCConfiguration config;
2203 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002204 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002205 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002206 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002207
2208 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002209 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002210 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2211 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002212}
2213
skvladd1f5fda2017-02-03 16:54:05 -08002214// Test that the ice check interval can be changed. This does not verify that
2215// the setting makes it all the way to P2PTransportChannel, as that would
2216// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002217TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002218 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002219 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002220 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002221 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002222 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002223 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002224 config = pc_->GetConfiguration();
2225 EXPECT_EQ(config.ice_check_min_interval, 100);
2226}
2227
2228TEST_P(PeerConnectionInterfaceTest,
2229 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2230 PeerConnectionInterface::RTCConfiguration config;
2231 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2232 CreatePeerConnection(config);
2233 config = pc_->GetConfiguration();
2234 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2235
2236 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002237 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002238 config = pc_->GetConfiguration();
2239 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002240}
2241
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002242// Test that when SetConfiguration changes both the pool size and other
2243// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002244TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002245 SetConfigurationCreatesPooledSessionCorrectly) {
2246 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002247 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002248 config.ice_candidate_pool_size = 1;
2249 PeerConnectionInterface::IceServer server;
2250 server.uri = kStunAddressOnly;
2251 config.servers.push_back(server);
2252 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002253 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002254
2255 const cricket::FakePortAllocatorSession* session =
2256 static_cast<const cricket::FakePortAllocatorSession*>(
2257 port_allocator_->GetPooledSession());
2258 ASSERT_NE(nullptr, session);
2259 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002260}
2261
deadbeef293e9262017-01-11 12:28:30 -08002262// Test that after SetLocalDescription, changing the pool size is not allowed,
2263// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002264TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002265 CantChangePoolSizeAfterSetLocalDescription) {
2266 CreatePeerConnection();
2267 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002268 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002269 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002270 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002271
2272 // Set remote offer; can still change pool size at this point.
2273 CreateOfferAsRemoteDescription();
2274 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002275 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002276
2277 // Set local answer; now it's too late.
2278 CreateAnswerAsLocalDescription();
2279 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002280 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002281 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2282}
2283
deadbeef42a42632017-03-10 15:18:00 -08002284// Test that after setting an answer, extra pooled sessions are discarded. The
2285// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002286TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002287 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2288 CreatePeerConnection();
2289
2290 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002291 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002292 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002293 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002294
2295 // Do offer/answer.
2296 CreateOfferAsRemoteDescription();
2297 CreateAnswerAsLocalDescription();
2298
2299 // Expect no pooled sessions to be left.
2300 const cricket::PortAllocatorSession* session =
2301 port_allocator_->GetPooledSession();
2302 EXPECT_EQ(nullptr, session);
2303}
2304
2305// After Close is called, pooled candidates should be discarded so as to not
2306// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002307TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002308 CreatePeerConnection();
2309
Steve Anton36da6ff2018-02-16 16:04:20 -08002310 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002311 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002312 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002313 pc_->Close();
2314
2315 // Expect no pooled sessions to be left.
2316 const cricket::PortAllocatorSession* session =
2317 port_allocator_->GetPooledSession();
2318 EXPECT_EQ(nullptr, session);
2319}
2320
deadbeef293e9262017-01-11 12:28:30 -08002321// Test that SetConfiguration returns an invalid modification error if
2322// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002323TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002324 SetConfigurationReturnsInvalidModificationError) {
2325 PeerConnectionInterface::RTCConfiguration config;
2326 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2327 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2328 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002329 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002330
Steve Anton36da6ff2018-02-16 16:04:20 -08002331 PeerConnectionInterface::RTCConfiguration modified_config =
2332 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002333 modified_config.bundle_policy =
2334 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002335 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002336 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2337
Steve Anton36da6ff2018-02-16 16:04:20 -08002338 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002339 modified_config.rtcp_mux_policy =
2340 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002341 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.continual_gathering_policy =
2346 PeerConnectionInterface::GATHER_CONTINUALLY;
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}
2350
2351// Test that SetConfiguration returns a range error if the candidate pool size
2352// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002353TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002354 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2355 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002356 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002357 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002358
2359 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002360 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002361 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2362
2363 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002364 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002365 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2366}
2367
2368// Test that SetConfiguration returns a syntax error if parsing an ICE server
2369// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002370TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002371 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2372 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002373 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002374 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002375
2376 PeerConnectionInterface::IceServer bad_server;
2377 bad_server.uri = "stunn:www.example.com";
2378 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002379 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002380 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2381}
2382
2383// Test that SetConfiguration returns an invalid parameter error if a TURN
2384// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002385TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002386 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2387 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002388 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002389 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002390
2391 PeerConnectionInterface::IceServer bad_server;
2392 bad_server.uri = "turn:www.example.com";
2393 // Missing password.
2394 bad_server.username = "foo";
2395 config.servers.push_back(bad_server);
2396 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002397 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2398 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002399}
2400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401// Test that PeerConnection::Close changes the states to closed and all remote
2402// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002403TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404 // Initialize a PeerConnection and negotiate local and remote session
2405 // description.
2406 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002407
2408 // With Plan B, verify the stream count. The analog with Unified Plan is the
2409 // RtpTransceiver count.
2410 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2411 ASSERT_EQ(1u, pc_->local_streams()->count());
2412 ASSERT_EQ(1u, pc_->remote_streams()->count());
2413 } else {
2414 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2415 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416
2417 pc_->Close();
2418
2419 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2420 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2421 pc_->ice_connection_state());
2422 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2423 pc_->ice_gathering_state());
2424
Steve Anton36da6ff2018-02-16 16:04:20 -08002425 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2426 EXPECT_EQ(1u, pc_->local_streams()->count());
2427 EXPECT_EQ(1u, pc_->remote_streams()->count());
2428 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002429 // Verify that the RtpTransceivers are still returned.
2430 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002431 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432
Steve Anton36da6ff2018-02-16 16:04:20 -08002433 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002434 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002435 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2436 ASSERT_TRUE(audio_receiver);
2437 ASSERT_TRUE(video_receiver);
2438 // Track state may be updated asynchronously.
2439 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2440 audio_receiver->track()->state(), kTimeout);
2441 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2442 video_receiver->track()->state(), kTimeout);
2443 } else {
2444 ASSERT_FALSE(audio_receiver);
2445 ASSERT_FALSE(video_receiver);
2446 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447}
2448
2449// Test that PeerConnection methods fails gracefully after
2450// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002451// Don't run under Unified Plan since the stream API is not available.
2452TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002453 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002454 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455 CreateOfferAsRemoteDescription();
2456 CreateAnswerAsLocalDescription();
2457
2458 ASSERT_EQ(1u, pc_->local_streams()->count());
Niels Möllere7cc8832022-01-04 15:20:03 +01002459 rtc::scoped_refptr<MediaStreamInterface> local_stream(
2460 pc_->local_streams()->at(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461
2462 pc_->Close();
2463
2464 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002465 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2468
2469 EXPECT_TRUE(pc_->local_description() != NULL);
2470 EXPECT_TRUE(pc_->remote_description() != NULL);
2471
kwibergd1fe2812016-04-27 06:47:29 -07002472 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002473 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002474 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002475 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476
2477 std::string sdp;
2478 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002479 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002480 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002481 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482
2483 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002484 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002485 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002486 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487}
2488
2489// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002490TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491 InitiateCall();
2492 pc_->Close();
2493 DoGetStats(NULL);
2494}
deadbeefab9b2d12015-10-14 11:33:11 -07002495
2496// NOTE: The series of tests below come from what used to be
2497// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2498// setting a remote or local description has the expected effects.
2499
2500// This test verifies that the remote MediaStreams corresponding to a received
2501// SDP string is created. In this test the two separate MediaStreams are
2502// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002503TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002504 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002505 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002506 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002507
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002508 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002509 EXPECT_TRUE(
2510 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2511 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2512 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2513
2514 // Create a session description based on another SDP with another
2515 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002516 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002517
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002518 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002519 EXPECT_TRUE(
2520 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2521}
2522
2523// This test verifies that when remote tracks are added/removed from SDP, the
2524// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002525// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2526// specific behavior.
2527TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002528 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002529 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002530 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002531 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002532 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002533 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002534 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2535 reference_collection_));
2536
2537 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002538 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002539 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002540 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002541 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2542 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002543 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002544 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2545 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002546 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002547 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2548 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002549
2550 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002551 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002552 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002553 MockTrackObserver audio_track_observer(audio_track2);
2554 MockTrackObserver video_track_observer(video_track2);
2555
2556 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2557 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002558 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002559 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2560 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002561 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002562 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002563 audio_track2->state(), kTimeout);
2564 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2565 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002566}
2567
2568// This tests that remote tracks are ended if a local session description is set
2569// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002570TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002571 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002572 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002573 // First create and set a remote offer, then reject its video content in our
2574 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002575 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2576 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2577 ASSERT_TRUE(audio_receiver);
2578 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2579 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002580
Steve Anton36da6ff2018-02-16 16:04:20 -08002581 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2582 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002583 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002584 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2585 video_receiver->track();
2586 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002587
kwibergd1fe2812016-04-27 06:47:29 -07002588 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002589 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002590 cricket::ContentInfo* video_info =
2591 local_answer->description()->GetContentByName("video");
2592 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002593 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002594 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2595 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002596
2597 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002598 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002599 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002600 video_info = local_offer->description()->GetContentByName("video");
2601 ASSERT_TRUE(video_info != nullptr);
2602 video_info->rejected = true;
2603 cricket::ContentInfo* audio_info =
2604 local_offer->description()->GetContentByName("audio");
2605 ASSERT_TRUE(audio_info != nullptr);
2606 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002607 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002608 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002609 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2610 kTimeout);
2611 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2612 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002613}
2614
2615// This tests that we won't crash if the remote track has been removed outside
2616// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002617// Don't run under Unified Plan since the stream API is not available.
2618TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002619 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002620 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002621 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002622 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2623 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2624 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2625
kwibergd1fe2812016-04-27 06:47:29 -07002626 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002627 webrtc::CreateSessionDescription(SdpType::kAnswer,
2628 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002629 cricket::ContentInfo* video_info =
2630 local_answer->description()->GetContentByName("video");
2631 video_info->rejected = true;
2632 cricket::ContentInfo* audio_info =
2633 local_answer->description()->GetContentByName("audio");
2634 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002635 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002636
2637 // No crash is a pass.
2638}
2639
deadbeef5e97fb52015-10-15 12:49:08 -07002640// This tests that if a recvonly remote description is set, no remote streams
2641// will be created, even if the description contains SSRCs/MSIDs.
2642// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002643TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002644 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002645 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002646
Steve Anton36da6ff2018-02-16 16:04:20 -08002647 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002648 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002649 CreateAndSetRemoteOffer(recvonly_offer);
2650
2651 EXPECT_EQ(0u, observer_.remote_streams()->count());
2652}
2653
deadbeefab9b2d12015-10-14 11:33:11 -07002654// This tests that a default MediaStream is created if a remote session
2655// description doesn't contain any streams and no MSID support.
2656// It also tests that the default stream is updated if a video m-line is added
2657// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002658// Don't run under Unified Plan since this behavior is Plan B specific.
2659TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002660 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002661 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002662 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2663
2664 ASSERT_EQ(1u, observer_.remote_streams()->count());
2665 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2666
2667 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2668 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002669 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002670
2671 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2672 ASSERT_EQ(1u, observer_.remote_streams()->count());
2673 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2674 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002675 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2676 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002677 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2678 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002679 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2680 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002681}
2682
2683// This tests that a default MediaStream is created if a remote session
2684// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002685// Don't run under Unified Plan since this behavior is Plan B specific.
2686TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002687 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002688 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002689 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002690 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2691
2692 ASSERT_EQ(1u, observer_.remote_streams()->count());
2693 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2694
2695 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2696 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002697 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002698}
2699
2700// This tests that it won't crash when PeerConnection tries to remove
2701// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002702// Don't run under Unified Plan since this behavior is Plan B specific.
2703TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002704 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002705 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002706 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002707 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2708 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2709 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2710
2711 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2712
2713 // No crash is a pass.
2714}
2715
2716// This tests that a default MediaStream is created if the remote session
2717// description doesn't contain any streams and don't contain an indication if
2718// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002719// Don't run under Unified Plan since this behavior is Plan B specific.
2720TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002721 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002722 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002723 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002724 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2725
2726 ASSERT_EQ(1u, observer_.remote_streams()->count());
2727 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2728 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2729 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2730}
2731
2732// This tests that a default MediaStream is not created if the remote session
2733// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002734// Don't run under Unified Plan since this behavior is Plan B specific.
2735TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002736 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002737 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002738 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2739 EXPECT_EQ(0u, observer_.remote_streams()->count());
2740}
2741
deadbeefbda7e0b2015-12-08 17:13:40 -08002742// This tests that when setting a new description, the old default tracks are
2743// not destroyed and recreated.
2744// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002745// Don't run under Unified Plan since this behavior is Plan B specific.
2746TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002747 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002748 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002749 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002750 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2751
2752 ASSERT_EQ(1u, observer_.remote_streams()->count());
2753 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2754 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2755
2756 // Set the track to "disabled", then set a new description and ensure the
2757 // track is still disabled, which ensures it hasn't been recreated.
2758 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2759 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2760 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2761 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2762}
2763
deadbeefab9b2d12015-10-14 11:33:11 -07002764// This tests that a default MediaStream is not created if a remote session
2765// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002766// Don't run under Unified Plan since this behavior is Plan B specific.
2767TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002768 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002769 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002770 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002771 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002772 EXPECT_TRUE(
2773 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2774
2775 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2776 EXPECT_EQ(0u, observer_.remote_streams()->count());
2777}
2778
Seth Hampson5897a6e2018-04-03 11:16:33 -07002779// This tests that a default MediaStream is created if a remote SDP comes from
2780// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2781TEST_F(PeerConnectionInterfaceTestPlanB,
2782 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002783 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002784 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002785 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2786 // Add a=msid lines to simulate a Unified Plan endpoint that only
2787 // signals stream IDs with a=msid lines.
2788 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2789
2790 CreateAndSetRemoteOffer(sdp_string);
2791
2792 ASSERT_EQ(1u, observer_.remote_streams()->count());
2793 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2794 EXPECT_EQ("default", remote_stream->id());
2795 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2796}
2797
Seth Hampson5b4f0752018-04-02 16:31:36 -07002798// This tests that when a Plan B endpoint receives an SDP that signals no media
2799// stream IDs indicated by the special character "-" in the a=msid line, that
2800// a default stream ID will be used for the MediaStream ID. This can occur
2801// when a Unified Plan endpoint signals no media stream IDs, but signals both
2802// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2803TEST_F(PeerConnectionInterfaceTestPlanB,
2804 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002805 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002806 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002807 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2808 // the sender's stream ID will be interpreted as no stream IDs.
2809 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2810 sdp_string.append("a=msid:- audiotrack0\n");
2811
2812 CreateAndSetRemoteOffer(sdp_string);
2813
2814 ASSERT_EQ(1u, observer_.remote_streams()->count());
2815 // Because SSRCs are signaled the track ID will be what was signaled in the
2816 // a=msid line.
2817 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2818 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2819 EXPECT_EQ("default", remote_stream->id());
2820 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002821
2822 // Previously a bug ocurred when setting the remote description a second time.
2823 // This is because we checked equality of the remote StreamParams stream ID
2824 // (empty), and the previously set stream ID for the remote sender
2825 // ("default"). This cause a track to be removed, then added, when really
2826 // nothing should occur because it is the same track.
2827 CreateAndSetRemoteOffer(sdp_string);
2828 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2829 EXPECT_EQ(1u, observer_.add_track_events_.size());
2830 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2831 remote_stream = observer_.remote_streams()->at(0);
2832 EXPECT_EQ("default", remote_stream->id());
2833 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002834}
2835
deadbeefab9b2d12015-10-14 11:33:11 -07002836// This tests that an RtpSender is created when the local description is set
2837// after adding a local stream.
2838// TODO(deadbeef): This test and the one below it need to be updated when
2839// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002840// Don't run under Unified Plan since this behavior is Plan B specific.
2841TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002842 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002843 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002844
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002845 // Create an offer with 1 stream with 2 tracks of each type.
2846 rtc::scoped_refptr<StreamCollection> stream_collection =
2847 CreateStreamCollection(1, 2);
2848 pc_->AddStream(stream_collection->at(0));
2849 std::unique_ptr<SessionDescriptionInterface> offer;
2850 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002851 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002852
deadbeefab9b2d12015-10-14 11:33:11 -07002853 auto senders = pc_->GetSenders();
2854 EXPECT_EQ(4u, senders.size());
2855 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2856 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2857 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2858 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2859
2860 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002861 pc_->RemoveStream(stream_collection->at(0));
2862 stream_collection = CreateStreamCollection(1, 1);
2863 pc_->AddStream(stream_collection->at(0));
2864 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002865 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002866
deadbeefab9b2d12015-10-14 11:33:11 -07002867 senders = pc_->GetSenders();
2868 EXPECT_EQ(2u, senders.size());
2869 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2870 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2871 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2872 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2873}
2874
2875// This tests that an RtpSender is created when the local description is set
2876// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002877// Don't run under Unified Plan since this behavior is Plan B specific.
2878TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002879 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002880 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002881 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002882
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002883 rtc::scoped_refptr<StreamCollection> stream_collection =
2884 CreateStreamCollection(1, 2);
2885 // Add a stream to create the offer, but remove it afterwards.
2886 pc_->AddStream(stream_collection->at(0));
2887 std::unique_ptr<SessionDescriptionInterface> offer;
2888 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2889 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002890
Steve Antondb45ca82017-09-11 18:27:34 -07002891 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002892 auto senders = pc_->GetSenders();
2893 EXPECT_EQ(0u, senders.size());
2894
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002895 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002896 senders = pc_->GetSenders();
2897 EXPECT_EQ(4u, senders.size());
2898 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2899 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2900 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2901 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2902}
2903
2904// This tests that the expected behavior occurs if the SSRC on a local track is
2905// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002906TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002907 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002908 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002909 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002910
Steve Anton36da6ff2018-02-16 16:04:20 -08002911 AddAudioTrack(kAudioTracks[0]);
2912 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002913 std::unique_ptr<SessionDescriptionInterface> offer;
2914 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2915 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002916 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2917 webrtc::CreateSessionDescription(
2918 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002919 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002920 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002921
deadbeefab9b2d12015-10-14 11:33:11 -07002922 auto senders = pc_->GetSenders();
2923 EXPECT_EQ(2u, senders.size());
2924 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2925 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2926
2927 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002928 cricket::MediaContentDescription* desc =
2929 cricket::GetFirstAudioContentDescription(modified_offer->description());
2930 ASSERT_TRUE(desc != NULL);
2931 for (StreamParams& stream : desc->mutable_streams()) {
2932 for (unsigned int& ssrc : stream.ssrcs) {
2933 ++ssrc;
2934 }
2935 }
deadbeefab9b2d12015-10-14 11:33:11 -07002936
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002937 desc =
2938 cricket::GetFirstVideoContentDescription(modified_offer->description());
2939 ASSERT_TRUE(desc != NULL);
2940 for (StreamParams& stream : desc->mutable_streams()) {
2941 for (unsigned int& ssrc : stream.ssrcs) {
2942 ++ssrc;
2943 }
2944 }
2945
Steve Antondb45ca82017-09-11 18:27:34 -07002946 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002947 senders = pc_->GetSenders();
2948 EXPECT_EQ(2u, senders.size());
2949 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2950 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2951 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2952 // changed.
2953}
2954
2955// This tests that the expected behavior occurs if a new session description is
2956// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002957// Don't run under Unified Plan since the stream API is not available.
2958TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002959 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002960 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002961 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002962
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002963 rtc::scoped_refptr<StreamCollection> stream_collection =
2964 CreateStreamCollection(2, 1);
2965 pc_->AddStream(stream_collection->at(0));
2966 std::unique_ptr<SessionDescriptionInterface> offer;
2967 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002968 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002969
deadbeefab9b2d12015-10-14 11:33:11 -07002970 auto senders = pc_->GetSenders();
2971 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002974
2975 // Add a new MediaStream but with the same tracks as in the first stream.
2976 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2977 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2979 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002980 pc_->AddStream(stream_1);
2981
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002982 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002983 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002984
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 auto new_senders = pc_->GetSenders();
2986 // Should be the same senders as before, but with updated stream id.
2987 // Note that this behavior is subject to change in the future.
2988 // We may decide the PC should ignore existing tracks in AddStream.
2989 EXPECT_EQ(senders, new_senders);
2990 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2991 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002992}
2993
zhihuang81c3a032016-11-17 12:06:24 -08002994// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002995TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002996 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002997 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08002998 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2999 EXPECT_EQ(observer_.num_added_tracks_, 1);
3000 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3001
3002 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003003 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003004 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003005 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3006}
3007
deadbeefd1a38b52016-12-10 13:15:33 -08003008// Test that when SetConfiguration is called and the configuration is
3009// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003010TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003011 PeerConnectionInterface::RTCConfiguration config;
3012 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003013 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003014 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003015 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3016 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003017
3018 // Do initial offer/answer so there's something to restart.
3019 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003020 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003021
3022 // Grab the ufrags.
3023 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3024
3025 // Change ICE policy, which should trigger an ICE restart on the next offer.
3026 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003027 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003028 CreateOfferAsLocalDescription();
3029
3030 // Grab the new ufrags.
3031 std::vector<std::string> subsequent_ufrags =
3032 GetUfrags(pc_->local_description());
3033
3034 // Sanity check.
3035 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3036 // Check that each ufrag is different.
3037 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3038 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3039 }
3040}
3041
3042// Test that when SetConfiguration is called and the configuration *isn't*
3043// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003044TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003045 PeerConnectionInterface::RTCConfiguration config;
3046 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003047 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003048 config = pc_->GetConfiguration();
3049 AddAudioTrack(kAudioTracks[0]);
3050 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003051
3052 // Do initial offer/answer so there's something to restart.
3053 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003054 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003055
3056 // Grab the ufrags.
3057 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3058
3059 // Call SetConfiguration with a config identical to what the PC was
3060 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003061 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003062 CreateOfferAsLocalDescription();
3063
3064 // Grab the new ufrags.
3065 std::vector<std::string> subsequent_ufrags =
3066 GetUfrags(pc_->local_description());
3067
3068 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3069}
3070
3071// Test for a weird corner case scenario:
3072// 1. Audio/video session established.
3073// 2. SetConfiguration changes ICE config; ICE restart needed.
3074// 3. ICE restart initiated by remote peer, but only for one m= section.
3075// 4. Next createOffer should initiate an ICE restart, but only for the other
3076// m= section; it would be pointless to do an ICE restart for the m= section
3077// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003078TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003079 PeerConnectionInterface::RTCConfiguration config;
3080 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003081 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003082 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003083 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3084 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003085
3086 // Do initial offer/answer so there's something to restart.
3087 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003088 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003089
3090 // Change ICE policy, which should set the "needs-ice-restart" flag.
3091 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003092 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003093
3094 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003095 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003096 webrtc::CreateSessionDescription(SdpType::kOffer,
3097 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003098 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003099 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3100 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003101 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003102 CreateAnswerAsLocalDescription();
3103
3104 // Grab the ufrags.
3105 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003106 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003107
3108 // Create offer and grab the new ufrags.
3109 CreateOfferAsLocalDescription();
3110 std::vector<std::string> subsequent_ufrags =
3111 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003112 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003113
3114 // Ensure that only the ufrag for the second m= section changed.
3115 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3116 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3117}
3118
deadbeeffe4a8a42016-12-20 17:56:17 -08003119// Tests that the methods to return current/pending descriptions work as
3120// expected at different points in the offer/answer exchange. This test does
3121// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003122TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003123 // This disables DTLS so we can apply an answer to ourselves.
3124 CreatePeerConnection();
3125
3126 // Create initial local offer and get SDP (which will also be used as
3127 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003128 std::unique_ptr<SessionDescriptionInterface> local_offer;
3129 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003130 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003131 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003132
3133 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003134 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3135 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3136 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003137 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3138 EXPECT_EQ(nullptr, pc_->current_local_description());
3139 EXPECT_EQ(nullptr, pc_->current_remote_description());
3140
3141 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003142 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003143 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003144 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3145 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3146 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3147 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003148 EXPECT_EQ(nullptr, pc_->current_local_description());
3149 EXPECT_EQ(nullptr, pc_->current_remote_description());
3150
3151 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003152 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003153 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003154 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3155 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003156 EXPECT_EQ(nullptr, pc_->pending_local_description());
3157 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003158 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3159 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003160
3161 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003162 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003163 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003164 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3165 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3166 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003167 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003168 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3169 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003170
3171 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003172 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003173 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003174 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3175 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3176 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3177 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3178 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3179 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003180
3181 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003182 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003183 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003184 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3185 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003186 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3187 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003188 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3189 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003190}
3191
zhihuang77985012017-02-07 15:45:16 -08003192// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3193// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003194// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003195// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003196TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003197 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3198 CreatePeerConnection();
3199 // The RtcEventLog will be reset when the PeerConnection is closed.
3200 pc_->Close();
3201
Niels Möllerdec9f742019-06-03 15:25:20 +02003202 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003203 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003204 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003205 pc_->StopRtcEventLog();
3206}
3207
deadbeef30952b42017-04-21 02:41:29 -07003208// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003209TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003210 CreatePeerConnection();
3211
3212 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003213 RTCOfferAnswerOptions options;
3214 options.offer_to_receive_audio = 1;
3215 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003216 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003217 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003218 cricket::SessionDescription* desc = offer->description();
3219 ASSERT_EQ(2u, desc->transport_infos().size());
3220 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3221 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3222
3223 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003224 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003225 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003226 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3227 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003228 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003229 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003230 desc = answer->description();
3231 ASSERT_EQ(2u, desc->transport_infos().size());
3232 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3233 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3234}
3235
deadbeef1dcb1642017-03-29 21:08:16 -07003236// Test that ICE renomination isn't offered if it's not enabled in the PC's
3237// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003238TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003239 PeerConnectionInterface::RTCConfiguration config;
3240 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003241 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003242 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003243
3244 std::unique_ptr<SessionDescriptionInterface> offer;
3245 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3246 cricket::SessionDescription* desc = offer->description();
3247 EXPECT_EQ(1u, desc->transport_infos().size());
3248 EXPECT_FALSE(
3249 desc->transport_infos()[0].description.GetIceParameters().renomination);
3250}
3251
3252// Test that the ICE renomination option is present in generated offers/answers
3253// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003254TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003255 PeerConnectionInterface::RTCConfiguration config;
3256 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003257 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003258 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003259
3260 std::unique_ptr<SessionDescriptionInterface> offer;
3261 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3262 cricket::SessionDescription* desc = offer->description();
3263 EXPECT_EQ(1u, desc->transport_infos().size());
3264 EXPECT_TRUE(
3265 desc->transport_infos()[0].description.GetIceParameters().renomination);
3266
3267 // Set the offer as a remote description, then create an answer and ensure it
3268 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003269 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003270 std::unique_ptr<SessionDescriptionInterface> answer;
3271 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3272 desc = answer->description();
3273 EXPECT_EQ(1u, desc->transport_infos().size());
3274 EXPECT_TRUE(
3275 desc->transport_infos()[0].description.GetIceParameters().renomination);
3276}
3277
3278// Test that if CreateOffer is called with the deprecated "offer to receive
3279// audio/video" constraints, they're processed and result in an offer with
3280// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003281TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003282 CreatePeerConnection();
3283
Niels Möllerf06f9232018-08-07 12:32:18 +02003284 RTCOfferAnswerOptions options;
3285 options.offer_to_receive_audio = 1;
3286 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003287 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003288 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003289
3290 cricket::SessionDescription* desc = offer->description();
3291 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3292 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3293 ASSERT_NE(nullptr, audio);
3294 ASSERT_NE(nullptr, video);
3295 EXPECT_FALSE(audio->rejected);
3296 EXPECT_FALSE(video->rejected);
3297}
3298
3299// Test that if CreateAnswer is called with the deprecated "offer to receive
3300// audio/video" constraints, they're processed and can be used to reject an
3301// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003302// Don't run under Unified Plan since this behavior is not supported.
3303TEST_F(PeerConnectionInterfaceTestPlanB,
3304 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003305 CreatePeerConnection();
3306
3307 // First, create an offer with audio/video and apply it as a remote
3308 // description.
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));
Steve Antondb45ca82017-09-11 18:27:34 -07003314 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003315
3316 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003317 options.offer_to_receive_audio = 0;
3318 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003319 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003320 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003321
3322 cricket::SessionDescription* desc = answer->description();
3323 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3324 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3325 ASSERT_NE(nullptr, audio);
3326 ASSERT_NE(nullptr, video);
3327 EXPECT_TRUE(audio->rejected);
3328 EXPECT_TRUE(video->rejected);
3329}
3330
deadbeef1dcb1642017-03-29 21:08:16 -07003331// Test that negotiation can succeed with a data channel only, and with the max
3332// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003333#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003334TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3335#else
3336TEST_P(PeerConnectionInterfaceTest,
3337 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003338#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003339 PeerConnectionInterface::RTCConfiguration config;
3340 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003341 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003342
3343 // First, create an offer with only a data channel and apply it as a remote
3344 // description.
3345 pc_->CreateDataChannel("test", nullptr);
3346 std::unique_ptr<SessionDescriptionInterface> offer;
3347 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003348 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003349
3350 // Create and set answer as well.
3351 std::unique_ptr<SessionDescriptionInterface> answer;
3352 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003353 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003354}
3355
Steve Anton36da6ff2018-02-16 16:04:20 -08003356TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003357 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003358 BitrateSettings bitrate;
3359 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003360 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3361}
3362
Steve Anton36da6ff2018-02-16 16:04:20 -08003363TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003364 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003365 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003366 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003367 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3368}
3369
Steve Anton36da6ff2018-02-16 16:04:20 -08003370TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003371 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003372 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003373 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003374 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003375 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3376}
3377
Steve Anton36da6ff2018-02-16 16:04:20 -08003378TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003379 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003380 BitrateSettings bitrate;
3381 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003382 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3383}
3384
Steve Anton36da6ff2018-02-16 16:04:20 -08003385TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003386 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003387 BitrateSettings bitrate;
3388 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003389 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003390 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3391}
3392
Steve Anton36da6ff2018-02-16 16:04:20 -08003393TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003394 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003395 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003396 bitrate.min_bitrate_bps = 10;
3397 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003398 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3399}
3400
Steve Anton36da6ff2018-02-16 16:04:20 -08003401TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003402 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003403 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003404 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003405 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3406}
3407
Niels Möller0c4f7be2018-05-07 14:01:37 +02003408// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003409// by Call's BitrateConstraints, which comes from the SDP or a default value.
3410// This test checks that a call to SetBitrate with a current bitrate that will
3411// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003412TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003413 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003414 BitrateSettings bitrate;
3415 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003416 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3417}
3418
zhihuang1c378ed2017-08-17 14:10:50 -07003419// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003420TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003421 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3422 RTCOfferAnswerOptions rtc_options;
3423
3424 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3425 // than kMaxOfferToReceiveMedia should be treated as invalid.
3426 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3427 CreatePeerConnection();
3428 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3429
3430 rtc_options.offer_to_receive_audio =
3431 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3432 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3433}
3434
Steve Anton36da6ff2018-02-16 16:04:20 -08003435TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003436 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3437 RTCOfferAnswerOptions rtc_options;
3438
3439 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3440 // than kMaxOfferToReceiveMedia should be treated as invalid.
3441 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3442 CreatePeerConnection();
3443 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3444
3445 rtc_options.offer_to_receive_video =
3446 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3447 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3448}
3449
3450// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003451// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003452TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003453 RTCOfferAnswerOptions rtc_options;
3454 rtc_options.offer_to_receive_audio = 1;
3455 rtc_options.offer_to_receive_video = 1;
3456
3457 std::unique_ptr<SessionDescriptionInterface> offer;
3458 CreatePeerConnection();
3459 offer = CreateOfferWithOptions(rtc_options);
3460 ASSERT_TRUE(offer);
3461 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3462 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3463}
3464
3465// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003466// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003467TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003468 RTCOfferAnswerOptions rtc_options;
3469 rtc_options.offer_to_receive_audio = 1;
3470 rtc_options.offer_to_receive_video = 0;
3471
3472 std::unique_ptr<SessionDescriptionInterface> offer;
3473 CreatePeerConnection();
3474 offer = CreateOfferWithOptions(rtc_options);
3475 ASSERT_TRUE(offer);
3476 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3477 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3478}
3479
Artem Titov880fa812021-07-30 22:30:23 +02003480// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003481// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003482TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003483 RTCOfferAnswerOptions rtc_options;
3484 rtc_options.offer_to_receive_audio = 0;
3485 rtc_options.offer_to_receive_video = 1;
3486
3487 std::unique_ptr<SessionDescriptionInterface> offer;
3488 CreatePeerConnection();
3489 offer = CreateOfferWithOptions(rtc_options);
3490 ASSERT_TRUE(offer);
3491 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3492 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3493}
3494
zhihuang1c378ed2017-08-17 14:10:50 -07003495// Test that no media content will be added to the offer if using default
3496// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003497TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003498 RTCOfferAnswerOptions rtc_options;
3499
3500 std::unique_ptr<SessionDescriptionInterface> offer;
3501 CreatePeerConnection();
3502 offer = CreateOfferWithOptions(rtc_options);
3503 ASSERT_TRUE(offer);
3504 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3505 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3506}
3507
Artem Titov880fa812021-07-30 22:30:23 +02003508// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003509// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003510TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3511 CreatePeerConnection();
3512
zhihuang1c378ed2017-08-17 14:10:50 -07003513 RTCOfferAnswerOptions rtc_options;
3514 rtc_options.ice_restart = false;
3515 rtc_options.offer_to_receive_audio = 1;
3516
3517 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003518 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003519 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3520 auto ufrag1 =
3521 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3522 auto pwd1 =
3523 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003524
Artem Titov880fa812021-07-30 22:30:23 +02003525 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003526 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003527 auto ufrag2 =
3528 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3529 auto pwd2 =
3530 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003531
Artem Titov880fa812021-07-30 22:30:23 +02003532 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003533 rtc_options.ice_restart = true;
3534 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003535 auto ufrag3 =
3536 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3537 auto pwd3 =
3538 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003539
3540 EXPECT_EQ(ufrag1, ufrag2);
3541 EXPECT_EQ(pwd1, pwd2);
3542 EXPECT_NE(ufrag2, ufrag3);
3543 EXPECT_NE(pwd2, pwd3);
3544}
3545
Artem Titov880fa812021-07-30 22:30:23 +02003546// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003547// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003548TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003549 RTCOfferAnswerOptions rtc_options;
3550 rtc_options.offer_to_receive_audio = 1;
3551 rtc_options.offer_to_receive_video = 1;
3552
3553 std::unique_ptr<SessionDescriptionInterface> offer;
3554 CreatePeerConnection();
3555
3556 rtc_options.use_rtp_mux = true;
3557 offer = CreateOfferWithOptions(rtc_options);
3558 ASSERT_TRUE(offer);
3559 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3560 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3561 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3562
3563 rtc_options.use_rtp_mux = false;
3564 offer = CreateOfferWithOptions(rtc_options);
3565 ASSERT_TRUE(offer);
3566 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3567 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3568 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3569}
3570
zhihuang141aacb2017-08-29 13:23:53 -07003571// This test ensures OnRenegotiationNeeded is called when we add track with
3572// MediaStream -> AddTrack in the same way it is called when we add track with
3573// PeerConnection -> AddTrack.
3574// The test can be removed once addStream is rewritten in terms of addTrack
3575// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003576// Don't run under Unified Plan since the stream API is not available.
3577TEST_F(PeerConnectionInterfaceTestPlanB,
3578 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003579 CreatePeerConnectionWithoutDtls();
3580 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003581 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003582 pc_->AddStream(stream);
3583 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003584 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003585 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003586 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003587 stream->AddTrack(audio_track);
3588 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3589 observer_.renegotiation_needed_ = false;
3590
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003591 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003592 stream->AddTrack(video_track);
3593 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3594 observer_.renegotiation_needed_ = false;
3595
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003596 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003597 stream->RemoveTrack(audio_track);
3598 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3599 observer_.renegotiation_needed_ = false;
3600
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003601 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003602 stream->RemoveTrack(video_track);
3603 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3604 observer_.renegotiation_needed_ = false;
3605}
3606
Zhi Huangb2d355e2017-10-26 17:26:37 -07003607// Tests that an error is returned if a description is applied that has fewer
3608// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003609TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003610 MediaSectionCountEnforcedForSubsequentOffer) {
3611 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003612 AddAudioTrack("audio_label");
3613 AddVideoTrack("video_label");
3614
Zhi Huangb2d355e2017-10-26 17:26:37 -07003615 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003616 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003617 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3618
3619 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003620 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003621 offer->description()->contents().pop_back();
3622 offer->description()->contents().pop_back();
3623 ASSERT_TRUE(offer->description()->contents().empty());
3624 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3625
3626 std::unique_ptr<SessionDescriptionInterface> answer;
3627 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3628 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3629
3630 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003631 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003632 offer->description()->contents().pop_back();
3633 offer->description()->contents().pop_back();
3634 ASSERT_TRUE(offer->description()->contents().empty());
3635 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3636}
3637
Johannes Kron89f874e2018-11-12 10:25:48 +01003638TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3639 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003640 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003641 CreatePeerConnection(config);
3642 std::unique_ptr<SessionDescriptionInterface> offer;
3643 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003644 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3645 // Possible to set to false.
3646 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003647 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003648 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003649 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003650 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003651}
3652
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003653INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3654 PeerConnectionInterfaceTest,
3655 Values(SdpSemantics::kPlanB,
3656 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003657
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003658class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003659 protected:
3660 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003661 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003662 }
nisseeaabdf62017-05-05 02:23:02 -07003663 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003664 const RTCConfiguration& config) {
3665 rtc::scoped_refptr<PeerConnectionInterface> pc(
3666 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003667 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003668 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003669 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003670 }
3671
zhihuang9763d562016-08-05 11:14:50 -07003672 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003673 MockPeerConnectionObserver observer_;
3674};
3675
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003676// This sanity check validates the test infrastructure itself.
3677TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3678 PeerConnectionInterface::RTCConfiguration config;
3679 rtc::scoped_refptr<PeerConnectionInterface> pc(
3680 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3681 EXPECT_TRUE(pc.get());
3682 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3683 pc->Close(); // No abort -> ok.
3684 SUCCEED();
3685}
3686
nisse51542be2016-02-12 02:27:06 -08003687// This test verifies the default behaviour with no constraints and a
3688// default RTCConfiguration.
3689TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3690 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003691
Niels Möllerf06f9232018-08-07 12:32:18 +02003692 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003693
3694 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003695 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3696 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003697 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003698 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003699}
3700
Niels Möller1d7ecd22018-01-18 15:25:12 +01003701// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003702// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003703TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3704 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003705
Niels Möller71bdda02016-03-31 12:59:59 +02003706 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003707 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003708
Niels Möller1d7ecd22018-01-18 15:25:12 +01003709 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003710}
3711
Niels Möller6539f692018-01-18 08:58:50 +01003712// This test verifies that the experiment_cpu_load_estimator flag is
3713// propagated from RTCConfiguration to the PeerConnection.
3714TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3715 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003716
3717 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003718 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003719
3720 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3721}
3722
deadbeef293e9262017-01-11 12:28:30 -08003723// Tests a few random fields being different.
3724TEST(RTCConfigurationTest, ComparisonOperators) {
3725 PeerConnectionInterface::RTCConfiguration a;
3726 PeerConnectionInterface::RTCConfiguration b;
3727 EXPECT_EQ(a, b);
3728
3729 PeerConnectionInterface::RTCConfiguration c;
3730 c.servers.push_back(PeerConnectionInterface::IceServer());
3731 EXPECT_NE(a, c);
3732
3733 PeerConnectionInterface::RTCConfiguration d;
3734 d.type = PeerConnectionInterface::kRelay;
3735 EXPECT_NE(a, d);
3736
3737 PeerConnectionInterface::RTCConfiguration e;
3738 e.audio_jitter_buffer_max_packets = 5;
3739 EXPECT_NE(a, e);
3740
3741 PeerConnectionInterface::RTCConfiguration f;
3742 f.ice_connection_receiving_timeout = 1337;
3743 EXPECT_NE(a, f);
3744
3745 PeerConnectionInterface::RTCConfiguration g;
3746 g.disable_ipv6 = true;
3747 EXPECT_NE(a, g);
3748
3749 PeerConnectionInterface::RTCConfiguration h(
3750 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3751 EXPECT_NE(a, h);
3752}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003753
3754} // namespace
3755} // namespace webrtc