blob: 4731cc80907b6a5bcd3ac352f5191a675fbd385b [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 Alvestrandee212a72021-11-02 12:06:45 +00002080// Test that SDP containing both a=fingerprint and a=crypto is handled
2081// by ignoring the a=crypto part.
2082// Prior to 2017, such an SDP would be accepted with SDES crypto, but
2083// the fallback was removed.
2084// Prior to 2021, such an SDP would be rejected because of the mixture.
2085// Post 2021, a=crypto lines are totally ignored by the SDP parser.
2086TEST_P(PeerConnectionInterfaceTest, SdesIgnored) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002087 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002088 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002089 // Wait for fake certificate to be generated. Previously, this is what caused
2090 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002091 AddAudioTrack("audio_label");
2092 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002093 ASSERT_NE(nullptr, fake_certificate_generator_);
2094 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2095 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002096 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002097 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2098 nullptr));
Harald Alvestrandee212a72021-11-02 12:06:45 +00002099 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002100}
2101
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102// Test that we can create an audio only offer and receive an answer with a
2103// limited set of audio codecs and receive an updated offer with more audio
2104// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002105TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002106 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002107 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 CreateOfferAsLocalDescription();
2109
Steve Anton36da6ff2018-02-16 16:04:20 -08002110 const char* answer_sdp =
2111 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2112 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002113 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002114 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002115 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116
Steve Anton36da6ff2018-02-16 16:04:20 -08002117 const char* reoffer_sdp =
2118 (sdp_semantics_ == SdpSemantics::kPlanB
2119 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2120 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002121 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002122 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002123 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 CreateAnswerAsLocalDescription();
2125}
2126
deadbeefc80741f2015-10-22 13:14:45 -07002127// Test that if we're receiving (but not sending) a track, subsequent offers
2128// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002129TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002130 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002131 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002132 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002133 CreateAnswerAsLocalDescription();
2134
2135 // At this point we should be receiving stream 1, but not sending anything.
2136 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002137 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002138 DoCreateOffer(&offer, nullptr);
2139
2140 const cricket::ContentInfo* video_content =
2141 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002142 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2143 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002144
2145 const cricket::ContentInfo* audio_content =
2146 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002147 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2148 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002149}
2150
2151// Test that if we're receiving (but not sending) a track, and the
2152// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2153// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002154TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002155 RTCConfiguration rtc_config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002156 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002157 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002158 CreateAnswerAsLocalDescription();
2159
2160 // At this point we should be receiving stream 1, but not sending anything.
2161 // A new offer would be recvonly, but we'll set the "no receive" constraints
2162 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002163 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002164 RTCOfferAnswerOptions options;
2165 options.offer_to_receive_audio = 0;
2166 options.offer_to_receive_video = 0;
2167 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002168
2169 const cricket::ContentInfo* video_content =
2170 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002171 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2172 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002173
2174 const cricket::ContentInfo* audio_content =
2175 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002176 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2177 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002178}
2179
deadbeef653b8e02015-11-11 12:55:10 -08002180// Test that we can use SetConfiguration to change the ICE servers of the
2181// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002182TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002183 CreatePeerConnection();
2184
Steve Anton36da6ff2018-02-16 16:04:20 -08002185 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002186 PeerConnectionInterface::IceServer server;
2187 server.uri = "stun:test_hostname";
2188 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002189 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002190
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002191 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2192 EXPECT_EQ("test_hostname",
2193 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002194}
2195
Steve Anton36da6ff2018-02-16 16:04:20 -08002196TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002197 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002198 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002199 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002200 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002201 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2202}
2203
Steve Anton36da6ff2018-02-16 16:04:20 -08002204TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002205 PeerConnectionInterface::RTCConfiguration config;
2206 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002207 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002208 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002209 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002210
2211 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002212 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002213 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2214 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002215}
2216
skvladd1f5fda2017-02-03 16:54:05 -08002217// Test that the ice check interval can be changed. This does not verify that
2218// the setting makes it all the way to P2PTransportChannel, as that would
2219// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002220TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002221 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002222 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002223 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002224 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002225 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002226 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002227 config = pc_->GetConfiguration();
2228 EXPECT_EQ(config.ice_check_min_interval, 100);
2229}
2230
2231TEST_P(PeerConnectionInterfaceTest,
2232 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2233 PeerConnectionInterface::RTCConfiguration config;
2234 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2235 CreatePeerConnection(config);
2236 config = pc_->GetConfiguration();
2237 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2238
2239 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002240 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002241 config = pc_->GetConfiguration();
2242 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002243}
2244
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002245// Test that when SetConfiguration changes both the pool size and other
2246// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002247TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002248 SetConfigurationCreatesPooledSessionCorrectly) {
2249 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002250 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002251 config.ice_candidate_pool_size = 1;
2252 PeerConnectionInterface::IceServer server;
2253 server.uri = kStunAddressOnly;
2254 config.servers.push_back(server);
2255 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002256 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002257
2258 const cricket::FakePortAllocatorSession* session =
2259 static_cast<const cricket::FakePortAllocatorSession*>(
2260 port_allocator_->GetPooledSession());
2261 ASSERT_NE(nullptr, session);
2262 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002263}
2264
deadbeef293e9262017-01-11 12:28:30 -08002265// Test that after SetLocalDescription, changing the pool size is not allowed,
2266// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002267TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002268 CantChangePoolSizeAfterSetLocalDescription) {
2269 CreatePeerConnection();
2270 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002271 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002272 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002273 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002274
2275 // Set remote offer; can still change pool size at this point.
2276 CreateOfferAsRemoteDescription();
2277 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002278 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002279
2280 // Set local answer; now it's too late.
2281 CreateAnswerAsLocalDescription();
2282 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002283 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002284 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2285}
2286
deadbeef42a42632017-03-10 15:18:00 -08002287// Test that after setting an answer, extra pooled sessions are discarded. The
2288// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002289TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002290 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2291 CreatePeerConnection();
2292
2293 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002294 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002295 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002296 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002297
2298 // Do offer/answer.
2299 CreateOfferAsRemoteDescription();
2300 CreateAnswerAsLocalDescription();
2301
2302 // Expect no pooled sessions to be left.
2303 const cricket::PortAllocatorSession* session =
2304 port_allocator_->GetPooledSession();
2305 EXPECT_EQ(nullptr, session);
2306}
2307
2308// After Close is called, pooled candidates should be discarded so as to not
2309// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002310TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002311 CreatePeerConnection();
2312
Steve Anton36da6ff2018-02-16 16:04:20 -08002313 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002314 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002315 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002316 pc_->Close();
2317
2318 // Expect no pooled sessions to be left.
2319 const cricket::PortAllocatorSession* session =
2320 port_allocator_->GetPooledSession();
2321 EXPECT_EQ(nullptr, session);
2322}
2323
deadbeef293e9262017-01-11 12:28:30 -08002324// Test that SetConfiguration returns an invalid modification error if
2325// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002326TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002327 SetConfigurationReturnsInvalidModificationError) {
2328 PeerConnectionInterface::RTCConfiguration config;
2329 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2330 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2331 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002332 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002333
Steve Anton36da6ff2018-02-16 16:04:20 -08002334 PeerConnectionInterface::RTCConfiguration modified_config =
2335 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002336 modified_config.bundle_policy =
2337 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002338 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002339 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2340
Steve Anton36da6ff2018-02-16 16:04:20 -08002341 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002342 modified_config.rtcp_mux_policy =
2343 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002344 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002345 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2346
Steve Anton36da6ff2018-02-16 16:04:20 -08002347 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002348 modified_config.continual_gathering_policy =
2349 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002350 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002351 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2352}
2353
2354// Test that SetConfiguration returns a range error if the candidate pool size
2355// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002356TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002357 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2358 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002359 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002360 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002361
2362 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002363 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002364 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2365
2366 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002367 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002368 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2369}
2370
2371// Test that SetConfiguration returns a syntax error if parsing an ICE server
2372// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002373TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002374 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2375 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002376 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002377 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002378
2379 PeerConnectionInterface::IceServer bad_server;
2380 bad_server.uri = "stunn:www.example.com";
2381 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002382 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002383 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2384}
2385
2386// Test that SetConfiguration returns an invalid parameter error if a TURN
2387// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002388TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002389 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2390 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002391 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002392 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002393
2394 PeerConnectionInterface::IceServer bad_server;
2395 bad_server.uri = "turn:www.example.com";
2396 // Missing password.
2397 bad_server.username = "foo";
2398 config.servers.push_back(bad_server);
2399 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002400 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2401 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002402}
2403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404// Test that PeerConnection::Close changes the states to closed and all remote
2405// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002406TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 // Initialize a PeerConnection and negotiate local and remote session
2408 // description.
2409 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002410
2411 // With Plan B, verify the stream count. The analog with Unified Plan is the
2412 // RtpTransceiver count.
2413 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2414 ASSERT_EQ(1u, pc_->local_streams()->count());
2415 ASSERT_EQ(1u, pc_->remote_streams()->count());
2416 } else {
2417 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2418 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419
2420 pc_->Close();
2421
2422 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2423 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2424 pc_->ice_connection_state());
2425 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2426 pc_->ice_gathering_state());
2427
Steve Anton36da6ff2018-02-16 16:04:20 -08002428 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2429 EXPECT_EQ(1u, pc_->local_streams()->count());
2430 EXPECT_EQ(1u, pc_->remote_streams()->count());
2431 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002432 // Verify that the RtpTransceivers are still returned.
2433 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002434 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002435
Steve Anton36da6ff2018-02-16 16:04:20 -08002436 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002437 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002438 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2439 ASSERT_TRUE(audio_receiver);
2440 ASSERT_TRUE(video_receiver);
2441 // Track state may be updated asynchronously.
2442 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2443 audio_receiver->track()->state(), kTimeout);
2444 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2445 video_receiver->track()->state(), kTimeout);
2446 } else {
2447 ASSERT_FALSE(audio_receiver);
2448 ASSERT_FALSE(video_receiver);
2449 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450}
2451
2452// Test that PeerConnection methods fails gracefully after
2453// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002454// Don't run under Unified Plan since the stream API is not available.
2455TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002456 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002457 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002458 CreateOfferAsRemoteDescription();
2459 CreateAnswerAsLocalDescription();
2460
2461 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002462 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463 pc_->local_streams()->at(0);
2464
2465 pc_->Close();
2466
2467 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002468 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2471
2472 EXPECT_TRUE(pc_->local_description() != NULL);
2473 EXPECT_TRUE(pc_->remote_description() != NULL);
2474
kwibergd1fe2812016-04-27 06:47:29 -07002475 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002476 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002477 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002478 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479
2480 std::string sdp;
2481 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002482 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002483 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002484 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002485
2486 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002487 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002488 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002489 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490}
2491
2492// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002493TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494 InitiateCall();
2495 pc_->Close();
2496 DoGetStats(NULL);
2497}
deadbeefab9b2d12015-10-14 11:33:11 -07002498
2499// NOTE: The series of tests below come from what used to be
2500// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2501// setting a remote or local description has the expected effects.
2502
2503// This test verifies that the remote MediaStreams corresponding to a received
2504// SDP string is created. In this test the two separate MediaStreams are
2505// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002506TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002507 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002508 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002509 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002510
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002511 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002512 EXPECT_TRUE(
2513 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2514 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2515 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2516
2517 // Create a session description based on another SDP with another
2518 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002519 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002520
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002521 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002522 EXPECT_TRUE(
2523 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2524}
2525
2526// This test verifies that when remote tracks are added/removed from SDP, the
2527// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002528// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2529// specific behavior.
2530TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002531 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002532 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002533 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002534 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002535 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002536 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002537 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2538 reference_collection_));
2539
2540 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002541 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002542 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002543 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002544 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2545 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002546 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002547 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2548 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002549 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002550 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2551 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002552
2553 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002554 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002555 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002556 MockTrackObserver audio_track_observer(audio_track2);
2557 MockTrackObserver video_track_observer(video_track2);
2558
2559 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2560 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002561 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002562 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2563 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002564 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002565 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002566 audio_track2->state(), kTimeout);
2567 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2568 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002569}
2570
2571// This tests that remote tracks are ended if a local session description is set
2572// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002573TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002574 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002575 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002576 // First create and set a remote offer, then reject its video content in our
2577 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002578 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2579 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2580 ASSERT_TRUE(audio_receiver);
2581 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2582 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002583
Steve Anton36da6ff2018-02-16 16:04:20 -08002584 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2585 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002586 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002587 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2588 video_receiver->track();
2589 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002590
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002592 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002593 cricket::ContentInfo* video_info =
2594 local_answer->description()->GetContentByName("video");
2595 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002596 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002597 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2598 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002599
2600 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002601 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002602 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002603 video_info = local_offer->description()->GetContentByName("video");
2604 ASSERT_TRUE(video_info != nullptr);
2605 video_info->rejected = true;
2606 cricket::ContentInfo* audio_info =
2607 local_offer->description()->GetContentByName("audio");
2608 ASSERT_TRUE(audio_info != nullptr);
2609 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002610 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002611 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002612 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2613 kTimeout);
2614 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2615 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002616}
2617
2618// This tests that we won't crash if the remote track has been removed outside
2619// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002620// Don't run under Unified Plan since the stream API is not available.
2621TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002622 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002623 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002624 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002625 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2626 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2627 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2628
kwibergd1fe2812016-04-27 06:47:29 -07002629 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002630 webrtc::CreateSessionDescription(SdpType::kAnswer,
2631 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002632 cricket::ContentInfo* video_info =
2633 local_answer->description()->GetContentByName("video");
2634 video_info->rejected = true;
2635 cricket::ContentInfo* audio_info =
2636 local_answer->description()->GetContentByName("audio");
2637 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002638 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002639
2640 // No crash is a pass.
2641}
2642
deadbeef5e97fb52015-10-15 12:49:08 -07002643// This tests that if a recvonly remote description is set, no remote streams
2644// will be created, even if the description contains SSRCs/MSIDs.
2645// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002646TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002647 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002648 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002649
Steve Anton36da6ff2018-02-16 16:04:20 -08002650 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002651 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002652 CreateAndSetRemoteOffer(recvonly_offer);
2653
2654 EXPECT_EQ(0u, observer_.remote_streams()->count());
2655}
2656
deadbeefab9b2d12015-10-14 11:33:11 -07002657// This tests that a default MediaStream is created if a remote session
2658// description doesn't contain any streams and no MSID support.
2659// It also tests that the default stream is updated if a video m-line is added
2660// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002661// Don't run under Unified Plan since this behavior is Plan B specific.
2662TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002663 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002664 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002665 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2666
2667 ASSERT_EQ(1u, observer_.remote_streams()->count());
2668 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2669
2670 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2671 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002672 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002673
2674 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2675 ASSERT_EQ(1u, observer_.remote_streams()->count());
2676 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2677 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002678 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2679 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002680 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2681 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002682 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2683 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002684}
2685
2686// This tests that a default MediaStream is created if a remote session
2687// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002688// Don't run under Unified Plan since this behavior is Plan B specific.
2689TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002690 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002691 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002692 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002693 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2694
2695 ASSERT_EQ(1u, observer_.remote_streams()->count());
2696 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2697
2698 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2699 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002700 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002701}
2702
2703// This tests that it won't crash when PeerConnection tries to remove
2704// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002705// Don't run under Unified Plan since this behavior is Plan B specific.
2706TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002707 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002708 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002709 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002710 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2711 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2712 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2713
2714 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2715
2716 // No crash is a pass.
2717}
2718
2719// This tests that a default MediaStream is created if the remote session
2720// description doesn't contain any streams and don't contain an indication if
2721// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002722// Don't run under Unified Plan since this behavior is Plan B specific.
2723TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002724 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002725 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002726 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002727 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2728
2729 ASSERT_EQ(1u, observer_.remote_streams()->count());
2730 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2731 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2732 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2733}
2734
2735// This tests that a default MediaStream is not created if the remote session
2736// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002737// Don't run under Unified Plan since this behavior is Plan B specific.
2738TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002739 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002740 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002741 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2742 EXPECT_EQ(0u, observer_.remote_streams()->count());
2743}
2744
deadbeefbda7e0b2015-12-08 17:13:40 -08002745// This tests that when setting a new description, the old default tracks are
2746// not destroyed and recreated.
2747// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002748// Don't run under Unified Plan since this behavior is Plan B specific.
2749TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002750 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002751 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002752 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002753 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2754
2755 ASSERT_EQ(1u, observer_.remote_streams()->count());
2756 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2757 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2758
2759 // Set the track to "disabled", then set a new description and ensure the
2760 // track is still disabled, which ensures it hasn't been recreated.
2761 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2762 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2763 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2764 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2765}
2766
deadbeefab9b2d12015-10-14 11:33:11 -07002767// This tests that a default MediaStream is not created if a remote session
2768// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002769// Don't run under Unified Plan since this behavior is Plan B specific.
2770TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002771 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002772 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002773 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002774 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002775 EXPECT_TRUE(
2776 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2777
2778 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2779 EXPECT_EQ(0u, observer_.remote_streams()->count());
2780}
2781
Seth Hampson5897a6e2018-04-03 11:16:33 -07002782// This tests that a default MediaStream is created if a remote SDP comes from
2783// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2784TEST_F(PeerConnectionInterfaceTestPlanB,
2785 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002786 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002787 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002788 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2789 // Add a=msid lines to simulate a Unified Plan endpoint that only
2790 // signals stream IDs with a=msid lines.
2791 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2792
2793 CreateAndSetRemoteOffer(sdp_string);
2794
2795 ASSERT_EQ(1u, observer_.remote_streams()->count());
2796 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2797 EXPECT_EQ("default", remote_stream->id());
2798 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2799}
2800
Seth Hampson5b4f0752018-04-02 16:31:36 -07002801// This tests that when a Plan B endpoint receives an SDP that signals no media
2802// stream IDs indicated by the special character "-" in the a=msid line, that
2803// a default stream ID will be used for the MediaStream ID. This can occur
2804// when a Unified Plan endpoint signals no media stream IDs, but signals both
2805// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2806TEST_F(PeerConnectionInterfaceTestPlanB,
2807 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002808 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002809 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002810 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2811 // the sender's stream ID will be interpreted as no stream IDs.
2812 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2813 sdp_string.append("a=msid:- audiotrack0\n");
2814
2815 CreateAndSetRemoteOffer(sdp_string);
2816
2817 ASSERT_EQ(1u, observer_.remote_streams()->count());
2818 // Because SSRCs are signaled the track ID will be what was signaled in the
2819 // a=msid line.
2820 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2821 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2822 EXPECT_EQ("default", remote_stream->id());
2823 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002824
2825 // Previously a bug ocurred when setting the remote description a second time.
2826 // This is because we checked equality of the remote StreamParams stream ID
2827 // (empty), and the previously set stream ID for the remote sender
2828 // ("default"). This cause a track to be removed, then added, when really
2829 // nothing should occur because it is the same track.
2830 CreateAndSetRemoteOffer(sdp_string);
2831 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2832 EXPECT_EQ(1u, observer_.add_track_events_.size());
2833 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2834 remote_stream = observer_.remote_streams()->at(0);
2835 EXPECT_EQ("default", remote_stream->id());
2836 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002837}
2838
deadbeefab9b2d12015-10-14 11:33:11 -07002839// This tests that an RtpSender is created when the local description is set
2840// after adding a local stream.
2841// TODO(deadbeef): This test and the one below it need to be updated when
2842// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002843// Don't run under Unified Plan since this behavior is Plan B specific.
2844TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002845 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002846 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002847
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002848 // Create an offer with 1 stream with 2 tracks of each type.
2849 rtc::scoped_refptr<StreamCollection> stream_collection =
2850 CreateStreamCollection(1, 2);
2851 pc_->AddStream(stream_collection->at(0));
2852 std::unique_ptr<SessionDescriptionInterface> offer;
2853 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002854 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002855
deadbeefab9b2d12015-10-14 11:33:11 -07002856 auto senders = pc_->GetSenders();
2857 EXPECT_EQ(4u, senders.size());
2858 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2859 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2860 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2861 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2862
2863 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002864 pc_->RemoveStream(stream_collection->at(0));
2865 stream_collection = CreateStreamCollection(1, 1);
2866 pc_->AddStream(stream_collection->at(0));
2867 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002868 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002869
deadbeefab9b2d12015-10-14 11:33:11 -07002870 senders = pc_->GetSenders();
2871 EXPECT_EQ(2u, senders.size());
2872 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2873 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2874 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2875 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2876}
2877
2878// This tests that an RtpSender is created when the local description is set
2879// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002880// Don't run under Unified Plan since this behavior is Plan B specific.
2881TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002882 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002883 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002884 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002885
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002886 rtc::scoped_refptr<StreamCollection> stream_collection =
2887 CreateStreamCollection(1, 2);
2888 // Add a stream to create the offer, but remove it afterwards.
2889 pc_->AddStream(stream_collection->at(0));
2890 std::unique_ptr<SessionDescriptionInterface> offer;
2891 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2892 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002893
Steve Antondb45ca82017-09-11 18:27:34 -07002894 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002895 auto senders = pc_->GetSenders();
2896 EXPECT_EQ(0u, senders.size());
2897
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002898 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002899 senders = pc_->GetSenders();
2900 EXPECT_EQ(4u, senders.size());
2901 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2902 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2903 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2904 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2905}
2906
2907// This tests that the expected behavior occurs if the SSRC on a local track is
2908// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002909TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002910 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002911 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002912 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002913
Steve Anton36da6ff2018-02-16 16:04:20 -08002914 AddAudioTrack(kAudioTracks[0]);
2915 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002916 std::unique_ptr<SessionDescriptionInterface> offer;
2917 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2918 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002919 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2920 webrtc::CreateSessionDescription(
2921 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002922 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002923 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002924
deadbeefab9b2d12015-10-14 11:33:11 -07002925 auto senders = pc_->GetSenders();
2926 EXPECT_EQ(2u, senders.size());
2927 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2928 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2929
2930 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 cricket::MediaContentDescription* desc =
2932 cricket::GetFirstAudioContentDescription(modified_offer->description());
2933 ASSERT_TRUE(desc != NULL);
2934 for (StreamParams& stream : desc->mutable_streams()) {
2935 for (unsigned int& ssrc : stream.ssrcs) {
2936 ++ssrc;
2937 }
2938 }
deadbeefab9b2d12015-10-14 11:33:11 -07002939
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002940 desc =
2941 cricket::GetFirstVideoContentDescription(modified_offer->description());
2942 ASSERT_TRUE(desc != NULL);
2943 for (StreamParams& stream : desc->mutable_streams()) {
2944 for (unsigned int& ssrc : stream.ssrcs) {
2945 ++ssrc;
2946 }
2947 }
2948
Steve Antondb45ca82017-09-11 18:27:34 -07002949 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002950 senders = pc_->GetSenders();
2951 EXPECT_EQ(2u, senders.size());
2952 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2953 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2954 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2955 // changed.
2956}
2957
2958// This tests that the expected behavior occurs if a new session description is
2959// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002960// Don't run under Unified Plan since the stream API is not available.
2961TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002962 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002963 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002964 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002965
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002966 rtc::scoped_refptr<StreamCollection> stream_collection =
2967 CreateStreamCollection(2, 1);
2968 pc_->AddStream(stream_collection->at(0));
2969 std::unique_ptr<SessionDescriptionInterface> offer;
2970 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002972
deadbeefab9b2d12015-10-14 11:33:11 -07002973 auto senders = pc_->GetSenders();
2974 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002975 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2976 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002977
2978 // Add a new MediaStream but with the same tracks as in the first stream.
2979 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2980 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002981 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2982 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002983 pc_->AddStream(stream_1);
2984
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002986 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002987
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002988 auto new_senders = pc_->GetSenders();
2989 // Should be the same senders as before, but with updated stream id.
2990 // Note that this behavior is subject to change in the future.
2991 // We may decide the PC should ignore existing tracks in AddStream.
2992 EXPECT_EQ(senders, new_senders);
2993 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2994 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002995}
2996
zhihuang81c3a032016-11-17 12:06:24 -08002997// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002998TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002999 RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02003000 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003001 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3002 EXPECT_EQ(observer_.num_added_tracks_, 1);
3003 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3004
3005 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003006 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003007 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003008 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3009}
3010
deadbeefd1a38b52016-12-10 13:15:33 -08003011// Test that when SetConfiguration is called and the configuration is
3012// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003013TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003014 PeerConnectionInterface::RTCConfiguration config;
3015 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003016 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003017 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003018 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3019 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003020
3021 // Do initial offer/answer so there's something to restart.
3022 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003023 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003024
3025 // Grab the ufrags.
3026 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3027
3028 // Change ICE policy, which should trigger an ICE restart on the next offer.
3029 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003030 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003031 CreateOfferAsLocalDescription();
3032
3033 // Grab the new ufrags.
3034 std::vector<std::string> subsequent_ufrags =
3035 GetUfrags(pc_->local_description());
3036
3037 // Sanity check.
3038 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3039 // Check that each ufrag is different.
3040 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3041 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3042 }
3043}
3044
3045// Test that when SetConfiguration is called and the configuration *isn't*
3046// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003047TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003048 PeerConnectionInterface::RTCConfiguration config;
3049 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003050 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003051 config = pc_->GetConfiguration();
3052 AddAudioTrack(kAudioTracks[0]);
3053 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003054
3055 // Do initial offer/answer so there's something to restart.
3056 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003057 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003058
3059 // Grab the ufrags.
3060 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3061
3062 // Call SetConfiguration with a config identical to what the PC was
3063 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003064 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003065 CreateOfferAsLocalDescription();
3066
3067 // Grab the new ufrags.
3068 std::vector<std::string> subsequent_ufrags =
3069 GetUfrags(pc_->local_description());
3070
3071 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3072}
3073
3074// Test for a weird corner case scenario:
3075// 1. Audio/video session established.
3076// 2. SetConfiguration changes ICE config; ICE restart needed.
3077// 3. ICE restart initiated by remote peer, but only for one m= section.
3078// 4. Next createOffer should initiate an ICE restart, but only for the other
3079// m= section; it would be pointless to do an ICE restart for the m= section
3080// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003081TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003082 PeerConnectionInterface::RTCConfiguration config;
3083 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003084 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003085 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003086 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3087 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003088
3089 // Do initial offer/answer so there's something to restart.
3090 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003091 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003092
3093 // Change ICE policy, which should set the "needs-ice-restart" flag.
3094 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003095 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003096
3097 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003098 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003099 webrtc::CreateSessionDescription(SdpType::kOffer,
3100 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003101 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003102 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3103 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003104 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003105 CreateAnswerAsLocalDescription();
3106
3107 // Grab the ufrags.
3108 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003109 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003110
3111 // Create offer and grab the new ufrags.
3112 CreateOfferAsLocalDescription();
3113 std::vector<std::string> subsequent_ufrags =
3114 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003115 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003116
3117 // Ensure that only the ufrag for the second m= section changed.
3118 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3119 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3120}
3121
deadbeeffe4a8a42016-12-20 17:56:17 -08003122// Tests that the methods to return current/pending descriptions work as
3123// expected at different points in the offer/answer exchange. This test does
3124// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003125TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003126 // This disables DTLS so we can apply an answer to ourselves.
3127 CreatePeerConnection();
3128
3129 // Create initial local offer and get SDP (which will also be used as
3130 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003131 std::unique_ptr<SessionDescriptionInterface> local_offer;
3132 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003133 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003134 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003135
3136 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003137 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3138 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3139 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003140 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3141 EXPECT_EQ(nullptr, pc_->current_local_description());
3142 EXPECT_EQ(nullptr, pc_->current_remote_description());
3143
3144 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003145 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003146 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003147 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3148 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3149 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3150 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003151 EXPECT_EQ(nullptr, pc_->current_local_description());
3152 EXPECT_EQ(nullptr, pc_->current_remote_description());
3153
3154 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003155 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003156 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003157 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3158 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003159 EXPECT_EQ(nullptr, pc_->pending_local_description());
3160 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003161 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3162 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003163
3164 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003165 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003166 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003167 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3168 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3169 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003170 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003171 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3172 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003173
3174 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003175 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003176 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003177 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3178 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3179 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3180 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3181 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3182 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003183
3184 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003185 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003186 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003187 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3188 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003189 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3190 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003191 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3192 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003193}
3194
zhihuang77985012017-02-07 15:45:16 -08003195// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3196// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003197// This version tests the StartRtcEventLog version that receives an object
Artem Titov880fa812021-07-30 22:30:23 +02003198// of type `RtcEventLogOutput`.
Steve Anton36da6ff2018-02-16 16:04:20 -08003199TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003200 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3201 CreatePeerConnection();
3202 // The RtcEventLog will be reset when the PeerConnection is closed.
3203 pc_->Close();
3204
Niels Möllerdec9f742019-06-03 15:25:20 +02003205 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003206 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003207 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003208 pc_->StopRtcEventLog();
3209}
3210
deadbeef30952b42017-04-21 02:41:29 -07003211// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003212TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003213 CreatePeerConnection();
3214
3215 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003216 RTCOfferAnswerOptions options;
3217 options.offer_to_receive_audio = 1;
3218 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003219 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003220 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003221 cricket::SessionDescription* desc = offer->description();
3222 ASSERT_EQ(2u, desc->transport_infos().size());
3223 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3224 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3225
3226 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003227 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003228 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003229 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3230 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003231 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003232 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003233 desc = answer->description();
3234 ASSERT_EQ(2u, desc->transport_infos().size());
3235 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3236 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3237}
3238
deadbeef1dcb1642017-03-29 21:08:16 -07003239// Test that ICE renomination isn't offered if it's not enabled in the PC's
3240// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003241TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003242 PeerConnectionInterface::RTCConfiguration config;
3243 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003244 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003245 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003246
3247 std::unique_ptr<SessionDescriptionInterface> offer;
3248 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3249 cricket::SessionDescription* desc = offer->description();
3250 EXPECT_EQ(1u, desc->transport_infos().size());
3251 EXPECT_FALSE(
3252 desc->transport_infos()[0].description.GetIceParameters().renomination);
3253}
3254
3255// Test that the ICE renomination option is present in generated offers/answers
3256// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003257TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003258 PeerConnectionInterface::RTCConfiguration config;
3259 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003260 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003261 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003262
3263 std::unique_ptr<SessionDescriptionInterface> offer;
3264 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3265 cricket::SessionDescription* desc = offer->description();
3266 EXPECT_EQ(1u, desc->transport_infos().size());
3267 EXPECT_TRUE(
3268 desc->transport_infos()[0].description.GetIceParameters().renomination);
3269
3270 // Set the offer as a remote description, then create an answer and ensure it
3271 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003272 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003273 std::unique_ptr<SessionDescriptionInterface> answer;
3274 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3275 desc = answer->description();
3276 EXPECT_EQ(1u, desc->transport_infos().size());
3277 EXPECT_TRUE(
3278 desc->transport_infos()[0].description.GetIceParameters().renomination);
3279}
3280
3281// Test that if CreateOffer is called with the deprecated "offer to receive
3282// audio/video" constraints, they're processed and result in an offer with
3283// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003284TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003285 CreatePeerConnection();
3286
Niels Möllerf06f9232018-08-07 12:32:18 +02003287 RTCOfferAnswerOptions options;
3288 options.offer_to_receive_audio = 1;
3289 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003290 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003291 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003292
3293 cricket::SessionDescription* desc = offer->description();
3294 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3295 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3296 ASSERT_NE(nullptr, audio);
3297 ASSERT_NE(nullptr, video);
3298 EXPECT_FALSE(audio->rejected);
3299 EXPECT_FALSE(video->rejected);
3300}
3301
3302// Test that if CreateAnswer is called with the deprecated "offer to receive
3303// audio/video" constraints, they're processed and can be used to reject an
3304// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003305// Don't run under Unified Plan since this behavior is not supported.
3306TEST_F(PeerConnectionInterfaceTestPlanB,
3307 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003308 CreatePeerConnection();
3309
3310 // First, create an offer with audio/video and apply it as a remote
3311 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003312 RTCOfferAnswerOptions options;
3313 options.offer_to_receive_audio = 1;
3314 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003315 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003316 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003317 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003318
3319 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003320 options.offer_to_receive_audio = 0;
3321 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003322 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003323 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003324
3325 cricket::SessionDescription* desc = answer->description();
3326 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3327 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3328 ASSERT_NE(nullptr, audio);
3329 ASSERT_NE(nullptr, video);
3330 EXPECT_TRUE(audio->rejected);
3331 EXPECT_TRUE(video->rejected);
3332}
3333
deadbeef1dcb1642017-03-29 21:08:16 -07003334// Test that negotiation can succeed with a data channel only, and with the max
3335// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003336#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003337TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3338#else
3339TEST_P(PeerConnectionInterfaceTest,
3340 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003341#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003342 PeerConnectionInterface::RTCConfiguration config;
3343 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003344 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003345
3346 // First, create an offer with only a data channel and apply it as a remote
3347 // description.
3348 pc_->CreateDataChannel("test", nullptr);
3349 std::unique_ptr<SessionDescriptionInterface> offer;
3350 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003351 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003352
3353 // Create and set answer as well.
3354 std::unique_ptr<SessionDescriptionInterface> answer;
3355 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003356 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003357}
3358
Steve Anton36da6ff2018-02-16 16:04:20 -08003359TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003360 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003361 BitrateSettings bitrate;
3362 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003363 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3364}
3365
Steve Anton36da6ff2018-02-16 16:04:20 -08003366TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003367 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003368 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003369 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003370 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3371}
3372
Steve Anton36da6ff2018-02-16 16:04:20 -08003373TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003374 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003375 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003376 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003377 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003378 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3379}
3380
Steve Anton36da6ff2018-02-16 16:04:20 -08003381TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003382 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003383 BitrateSettings bitrate;
3384 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003385 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3386}
3387
Steve Anton36da6ff2018-02-16 16:04:20 -08003388TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003389 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003390 BitrateSettings bitrate;
3391 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003392 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003393 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3394}
3395
Steve Anton36da6ff2018-02-16 16:04:20 -08003396TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003397 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003398 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003399 bitrate.min_bitrate_bps = 10;
3400 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003401 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3402}
3403
Steve Anton36da6ff2018-02-16 16:04:20 -08003404TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003405 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003406 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003407 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003408 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3409}
3410
Niels Möller0c4f7be2018-05-07 14:01:37 +02003411// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003412// by Call's BitrateConstraints, which comes from the SDP or a default value.
3413// This test checks that a call to SetBitrate with a current bitrate that will
3414// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003415TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003416 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003417 BitrateSettings bitrate;
3418 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003419 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3420}
3421
zhihuang1c378ed2017-08-17 14:10:50 -07003422// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003423TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003424 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3425 RTCOfferAnswerOptions rtc_options;
3426
3427 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3428 // than kMaxOfferToReceiveMedia should be treated as invalid.
3429 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3430 CreatePeerConnection();
3431 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3432
3433 rtc_options.offer_to_receive_audio =
3434 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3435 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3436}
3437
Steve Anton36da6ff2018-02-16 16:04:20 -08003438TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003439 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3440 RTCOfferAnswerOptions rtc_options;
3441
3442 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3443 // than kMaxOfferToReceiveMedia should be treated as invalid.
3444 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3445 CreatePeerConnection();
3446 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3447
3448 rtc_options.offer_to_receive_video =
3449 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3450 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3451}
3452
3453// Test that the audio and video content will be added to an offer if both
Artem Titov880fa812021-07-30 22:30:23 +02003454// `offer_to_receive_audio` and `offer_to_receive_video` options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003455TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003456 RTCOfferAnswerOptions rtc_options;
3457 rtc_options.offer_to_receive_audio = 1;
3458 rtc_options.offer_to_receive_video = 1;
3459
3460 std::unique_ptr<SessionDescriptionInterface> offer;
3461 CreatePeerConnection();
3462 offer = CreateOfferWithOptions(rtc_options);
3463 ASSERT_TRUE(offer);
3464 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3465 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3466}
3467
3468// Test that only audio content will be added to the offer if only
Artem Titov880fa812021-07-30 22:30:23 +02003469// `offer_to_receive_audio` options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003470TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003471 RTCOfferAnswerOptions rtc_options;
3472 rtc_options.offer_to_receive_audio = 1;
3473 rtc_options.offer_to_receive_video = 0;
3474
3475 std::unique_ptr<SessionDescriptionInterface> offer;
3476 CreatePeerConnection();
3477 offer = CreateOfferWithOptions(rtc_options);
3478 ASSERT_TRUE(offer);
3479 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3480 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3481}
3482
Artem Titov880fa812021-07-30 22:30:23 +02003483// Test that only video content will be added if only `offer_to_receive_video`
zhihuang1c378ed2017-08-17 14:10:50 -07003484// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003485TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003486 RTCOfferAnswerOptions rtc_options;
3487 rtc_options.offer_to_receive_audio = 0;
3488 rtc_options.offer_to_receive_video = 1;
3489
3490 std::unique_ptr<SessionDescriptionInterface> offer;
3491 CreatePeerConnection();
3492 offer = CreateOfferWithOptions(rtc_options);
3493 ASSERT_TRUE(offer);
3494 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3495 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3496}
3497
zhihuang1c378ed2017-08-17 14:10:50 -07003498// Test that no media content will be added to the offer if using default
3499// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003500TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003501 RTCOfferAnswerOptions rtc_options;
3502
3503 std::unique_ptr<SessionDescriptionInterface> offer;
3504 CreatePeerConnection();
3505 offer = CreateOfferWithOptions(rtc_options);
3506 ASSERT_TRUE(offer);
3507 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3508 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3509}
3510
Artem Titov880fa812021-07-30 22:30:23 +02003511// Test that if `ice_restart` is true, the ufrag/pwd will change, otherwise
zhihuang1c378ed2017-08-17 14:10:50 -07003512// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003513TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3514 CreatePeerConnection();
3515
zhihuang1c378ed2017-08-17 14:10:50 -07003516 RTCOfferAnswerOptions rtc_options;
3517 rtc_options.ice_restart = false;
3518 rtc_options.offer_to_receive_audio = 1;
3519
3520 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003521 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003522 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3523 auto ufrag1 =
3524 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3525 auto pwd1 =
3526 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003527
Artem Titov880fa812021-07-30 22:30:23 +02003528 // `ice_restart` is false, the ufrag/pwd shouldn't change.
zhihuang1c378ed2017-08-17 14:10:50 -07003529 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003530 auto ufrag2 =
3531 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3532 auto pwd2 =
3533 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003534
Artem Titov880fa812021-07-30 22:30:23 +02003535 // `ice_restart` is true, the ufrag/pwd should change.
zhihuang1c378ed2017-08-17 14:10:50 -07003536 rtc_options.ice_restart = true;
3537 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003538 auto ufrag3 =
3539 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3540 auto pwd3 =
3541 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003542
3543 EXPECT_EQ(ufrag1, ufrag2);
3544 EXPECT_EQ(pwd1, pwd2);
3545 EXPECT_NE(ufrag2, ufrag3);
3546 EXPECT_NE(pwd2, pwd3);
3547}
3548
Artem Titov880fa812021-07-30 22:30:23 +02003549// Test that if `use_rtp_mux` is true, the bundling will be enabled in the
zhihuang1c378ed2017-08-17 14:10:50 -07003550// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003551TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003552 RTCOfferAnswerOptions rtc_options;
3553 rtc_options.offer_to_receive_audio = 1;
3554 rtc_options.offer_to_receive_video = 1;
3555
3556 std::unique_ptr<SessionDescriptionInterface> offer;
3557 CreatePeerConnection();
3558
3559 rtc_options.use_rtp_mux = true;
3560 offer = CreateOfferWithOptions(rtc_options);
3561 ASSERT_TRUE(offer);
3562 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3563 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3564 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3565
3566 rtc_options.use_rtp_mux = false;
3567 offer = CreateOfferWithOptions(rtc_options);
3568 ASSERT_TRUE(offer);
3569 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3570 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3571 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3572}
3573
zhihuang141aacb2017-08-29 13:23:53 -07003574// This test ensures OnRenegotiationNeeded is called when we add track with
3575// MediaStream -> AddTrack in the same way it is called when we add track with
3576// PeerConnection -> AddTrack.
3577// The test can be removed once addStream is rewritten in terms of addTrack
3578// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003579// Don't run under Unified Plan since the stream API is not available.
3580TEST_F(PeerConnectionInterfaceTestPlanB,
3581 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003582 CreatePeerConnectionWithoutDtls();
3583 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003584 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003585 pc_->AddStream(stream);
3586 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003587 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003588 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003589 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003590 stream->AddTrack(audio_track);
3591 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3592 observer_.renegotiation_needed_ = false;
3593
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003594 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003595 stream->AddTrack(video_track);
3596 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3597 observer_.renegotiation_needed_ = false;
3598
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003599 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003600 stream->RemoveTrack(audio_track);
3601 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3602 observer_.renegotiation_needed_ = false;
3603
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003604 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003605 stream->RemoveTrack(video_track);
3606 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3607 observer_.renegotiation_needed_ = false;
3608}
3609
Zhi Huangb2d355e2017-10-26 17:26:37 -07003610// Tests that an error is returned if a description is applied that has fewer
3611// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003612TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003613 MediaSectionCountEnforcedForSubsequentOffer) {
3614 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003615 AddAudioTrack("audio_label");
3616 AddVideoTrack("video_label");
3617
Zhi Huangb2d355e2017-10-26 17:26:37 -07003618 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003619 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003620 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3621
3622 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003623 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003624 offer->description()->contents().pop_back();
3625 offer->description()->contents().pop_back();
3626 ASSERT_TRUE(offer->description()->contents().empty());
3627 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3628
3629 std::unique_ptr<SessionDescriptionInterface> answer;
3630 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3631 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3632
3633 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003634 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003635 offer->description()->contents().pop_back();
3636 offer->description()->contents().pop_back();
3637 ASSERT_TRUE(offer->description()->contents().empty());
3638 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3639}
3640
Johannes Kron89f874e2018-11-12 10:25:48 +01003641TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3642 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003643 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003644 CreatePeerConnection(config);
3645 std::unique_ptr<SessionDescriptionInterface> offer;
3646 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003647 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3648 // Possible to set to false.
3649 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003650 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003651 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003652 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003653 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003654}
3655
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003656INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3657 PeerConnectionInterfaceTest,
3658 Values(SdpSemantics::kPlanB,
3659 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003660
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003661class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003662 protected:
3663 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003664 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003665 }
nisseeaabdf62017-05-05 02:23:02 -07003666 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003667 const RTCConfiguration& config) {
3668 rtc::scoped_refptr<PeerConnectionInterface> pc(
3669 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003670 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003671 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003672 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003673 }
3674
zhihuang9763d562016-08-05 11:14:50 -07003675 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003676 MockPeerConnectionObserver observer_;
3677};
3678
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003679// This sanity check validates the test infrastructure itself.
3680TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3681 PeerConnectionInterface::RTCConfiguration config;
3682 rtc::scoped_refptr<PeerConnectionInterface> pc(
3683 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3684 EXPECT_TRUE(pc.get());
3685 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3686 pc->Close(); // No abort -> ok.
3687 SUCCEED();
3688}
3689
nisse51542be2016-02-12 02:27:06 -08003690// This test verifies the default behaviour with no constraints and a
3691// default RTCConfiguration.
3692TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3693 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003694
Niels Möllerf06f9232018-08-07 12:32:18 +02003695 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003696
3697 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003698 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3699 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003700 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003701 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003702}
3703
Niels Möller1d7ecd22018-01-18 15:25:12 +01003704// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003705// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003706TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3707 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003708
Niels Möller71bdda02016-03-31 12:59:59 +02003709 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003710 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003711
Niels Möller1d7ecd22018-01-18 15:25:12 +01003712 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003713}
3714
Niels Möller6539f692018-01-18 08:58:50 +01003715// This test verifies that the experiment_cpu_load_estimator flag is
3716// propagated from RTCConfiguration to the PeerConnection.
3717TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3718 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003719
3720 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003721 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003722
3723 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3724}
3725
deadbeef293e9262017-01-11 12:28:30 -08003726// Tests a few random fields being different.
3727TEST(RTCConfigurationTest, ComparisonOperators) {
3728 PeerConnectionInterface::RTCConfiguration a;
3729 PeerConnectionInterface::RTCConfiguration b;
3730 EXPECT_EQ(a, b);
3731
3732 PeerConnectionInterface::RTCConfiguration c;
3733 c.servers.push_back(PeerConnectionInterface::IceServer());
3734 EXPECT_NE(a, c);
3735
3736 PeerConnectionInterface::RTCConfiguration d;
3737 d.type = PeerConnectionInterface::kRelay;
3738 EXPECT_NE(a, d);
3739
3740 PeerConnectionInterface::RTCConfiguration e;
3741 e.audio_jitter_buffer_max_packets = 5;
3742 EXPECT_NE(a, e);
3743
3744 PeerConnectionInterface::RTCConfiguration f;
3745 f.ice_connection_receiving_timeout = 1337;
3746 EXPECT_NE(a, f);
3747
3748 PeerConnectionInterface::RTCConfiguration g;
3749 g.disable_ipv6 = true;
3750 EXPECT_NE(a, g);
3751
3752 PeerConnectionInterface::RTCConfiguration h(
3753 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3754 EXPECT_NE(a, h);
3755}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003756
3757} // namespace
3758} // namespace webrtc