blob: d454dd2a05afd962b177622913805b63517e4c23 [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
deadbeefab9b2d12015-10-14 11:33:11 -0700507// Check if |streams| contains the specified track.
508bool 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
519// Check if |senders| contains the specified sender, by id.
520bool 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
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700531// Check if |senders| contains the specified sender, by id and stream id.
532bool 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
664 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100665 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700666 }
667
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100668 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800669
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100670 private:
deadbeefd7850b22017-08-23 10:59:19 -0700671 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700672};
673
Steve Anton36da6ff2018-02-16 16:04:20 -0800674// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200675class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800677 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
678 : vss_(new rtc::VirtualSocketServer()),
679 main_(vss_.get()),
680 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800681#ifdef WEBRTC_ANDROID
682 webrtc::InitializeAndroidObjects();
683#endif
684 }
685
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200686 void SetUp() override {
deadbeefd7850b22017-08-23 10:59:19 -0700687 // Use fake audio capture module since we're only testing the interface
688 // level, and using a real one could make tests flaky when run in parallel.
689 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700691 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200692 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
693 fake_audio_capture_module_),
694 webrtc::CreateBuiltinAudioEncoderFactory(),
695 webrtc::CreateBuiltinAudioDecoderFactory(),
696 webrtc::CreateBuiltinVideoEncoderFactory(),
697 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
698 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700699 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700700 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700701 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200704 void TearDown() override {
705 if (pc_)
706 pc_->Close();
707 }
708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200710 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
deadbeef293e9262017-01-11 12:28:30 -0800713 // DTLS does not work in a loopback call, so is disabled for most of the
714 // tests in this file.
715 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200716 RTCConfiguration config;
717 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800718
Niels Möllerf06f9232018-08-07 12:32:18 +0200719 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 }
721
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 void CreatePeerConnectionWithIceTransportsType(
723 PeerConnectionInterface::IceTransportsType type) {
724 PeerConnectionInterface::RTCConfiguration config;
725 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200726 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 }
728
729 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100730 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800732 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100735 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 server.password = password;
737 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200738 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740
Niels Möllerf06f9232018-08-07 12:32:18 +0200741 void CreatePeerConnection(const RTCConfiguration& config) {
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200742 if (pc_) {
743 pc_->Close();
744 pc_ = nullptr;
745 }
kwibergd1fe2812016-04-27 06:47:29 -0700746 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800747 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
748 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000749
deadbeef1dcb1642017-03-29 21:08:16 -0700750 // Create certificate generator unless DTLS constraint is explicitly set to
751 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200752 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200753
754 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800755 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
756 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000757 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800758 RTCConfiguration modified_config = config;
759 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200760 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200761 modified_config, std::move(port_allocator), std::move(cert_generator),
762 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 ASSERT_TRUE(pc_.get() != NULL);
764 observer_.SetPeerConnectionInterface(pc_.get());
765 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
766 }
767
deadbeef0a6c4ca2015-10-06 11:38:28 -0700768 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800769 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700770 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700771 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800773 config.sdp_semantics = sdp_semantics_;
774 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200775 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700777 }
778
Steve Anton038834f2017-07-14 15:59:59 -0700779 void CreatePeerConnectionExpectFail(
780 PeerConnectionInterface::RTCConfiguration config) {
781 PeerConnectionInterface::IceServer server;
782 server.uri = kTurnIceServerUri;
783 server.password = kTurnPassword;
784 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800785 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700786 rtc::scoped_refptr<PeerConnectionInterface> pc =
787 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
788 EXPECT_EQ(nullptr, pc);
789 }
790
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100792 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800793 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
794 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
795 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798
deadbeef0a6c4ca2015-10-06 11:38:28 -0700799 CreatePeerConnectionExpectFail(kStunInvalidPort);
800 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
801 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802
Niels Möllerdb4def92019-03-18 16:53:59 +0100803 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
804 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
806 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800808 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800810 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800812 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814
815 void ReleasePeerConnection() {
816 pc_ = NULL;
817 observer_.SetPeerConnectionInterface(NULL);
818 }
819
Steve Anton36da6ff2018-02-16 16:04:20 -0800820 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
821 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100822 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 }
824
825 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800826 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800828 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800829 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
Steve Anton36da6ff2018-02-16 16:04:20 -0800832 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800835 stream->AddTrack(CreateVideoTrack(label + "v0"));
836 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800837 }
838
839 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
840 const std::string& label) {
841 return pc_factory_->CreateAudioTrack(label, nullptr);
842 }
843
844 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800845 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800846 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800847 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800848 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800849 }
850
851 void AddAudioStream(const std::string& label) {
852 rtc::scoped_refptr<MediaStreamInterface> stream(
853 pc_factory_->CreateLocalMediaStream(label));
854 stream->AddTrack(CreateAudioTrack(label + "a0"));
855 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857
Seth Hampson845e8782018-03-02 11:34:10 -0800858 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 const std::string& audio_track_label,
860 const std::string& video_track_label) {
861 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700862 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800863 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800864 stream->AddTrack(CreateAudioTrack(audio_track_label));
865 stream->AddTrack(CreateVideoTrack(video_track_label));
866 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868
Steve Anton36da6ff2018-02-16 16:04:20 -0800869 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
870 cricket::MediaType media_type) {
871 for (auto receiver : pc_->GetReceivers()) {
872 if (receiver->media_type() == media_type) {
873 return receiver;
874 }
875 }
876 return nullptr;
877 }
878
kwibergd1fe2812016-04-27 06:47:29 -0700879 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200880 const RTCOfferAnswerOptions* options,
881 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200882 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
883 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200885 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200887 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700890 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 return observer->result();
892 }
893
kwibergd1fe2812016-04-27 06:47:29 -0700894 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200895 const RTCOfferAnswerOptions* options) {
896 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 }
898
kwibergd1fe2812016-04-27 06:47:29 -0700899 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200900 const RTCOfferAnswerOptions* options) {
901 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903
Steve Antondb45ca82017-09-11 18:27:34 -0700904 bool DoSetSessionDescription(
905 std::unique_ptr<SessionDescriptionInterface> desc,
906 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200907 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
908 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700910 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700912 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 }
zhihuang29ff8442016-07-27 11:07:25 -0700914 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
915 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
916 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 return observer->result();
918 }
919
Steve Antondb45ca82017-09-11 18:27:34 -0700920 bool DoSetLocalDescription(
921 std::unique_ptr<SessionDescriptionInterface> desc) {
922 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
924
Steve Antondb45ca82017-09-11 18:27:34 -0700925 bool DoSetRemoteDescription(
926 std::unique_ptr<SessionDescriptionInterface> desc) {
927 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929
930 // Calls PeerConnection::GetStats and check the return value.
931 // It does not verify the values in the StatReports since a RTCP packet might
932 // be required.
933 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000934 rtc::scoped_refptr<MockStatsObserver> observer(
935 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200936 if (!pc_->GetStats(observer, track,
937 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 return false;
939 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
940 return observer->called();
941 }
942
Harald Alvestrand89061872018-01-02 14:08:34 +0100943 // Call the standards-compliant GetStats function.
944 bool DoGetRTCStats() {
945 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
946 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
947 pc_->GetStats(callback);
948 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
949 return callback->called();
950 }
951
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800953 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800955 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800956 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800957 } else {
958 // Unified Plan does not support AddStream, so just add an audio and video
959 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800960 AddAudioTrack(kAudioTracks[0], {kStreamId1});
961 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 CreateOfferReceiveAnswer();
964 }
965
966 // Verify that RTP Header extensions has been negotiated for audio and video.
967 void VerifyRemoteRtpHeaderExtensions() {
968 const cricket::MediaContentDescription* desc =
969 cricket::GetFirstAudioContentDescription(
970 pc_->remote_description()->description());
971 ASSERT_TRUE(desc != NULL);
972 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
973
974 desc = cricket::GetFirstVideoContentDescription(
975 pc_->remote_description()->description());
976 ASSERT_TRUE(desc != NULL);
977 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
978 }
979
980 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700981 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700982 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 std::string sdp;
984 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700985 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800986 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700987 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
989 }
990
deadbeefab9b2d12015-10-14 11:33:11 -0700991 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700992 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800993 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700994 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700995 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
996 }
997
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700999 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001000 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001
1002 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1003 // audio codec change, even if the parameter has nothing to do with
1004 // receiving. Not all parameters are serialized to SDP.
1005 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1006 // the SessionDescription, it is necessary to do that here to in order to
1007 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1008 // https://code.google.com/p/webrtc/issues/detail?id=1356
1009 std::string sdp;
1010 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001011 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001012 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001013 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1015 }
1016
1017 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001018 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001019 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020
1021 std::string sdp;
1022 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001023 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001024 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001025 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1027 }
1028
1029 void CreateOfferReceiveAnswer() {
1030 CreateOfferAsLocalDescription();
1031 std::string sdp;
1032 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1033 CreateAnswerAsRemoteDescription(sdp);
1034 }
1035
1036 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001037 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001038 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1040 // audio codec change, even if the parameter has nothing to do with
1041 // receiving. Not all parameters are serialized to SDP.
1042 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1043 // the SessionDescription, it is necessary to do that here to in order to
1044 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1045 // https://code.google.com/p/webrtc/issues/detail?id=1356
1046 std::string sdp;
1047 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001048 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001049 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050
Steve Antondb45ca82017-09-11 18:27:34 -07001051 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001053 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001054 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 }
1056
deadbeefab9b2d12015-10-14 11:33:11 -07001057 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001058 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001059 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001060 ASSERT_TRUE(answer);
1061 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1063 }
1064
deadbeefab9b2d12015-10-14 11:33:11 -07001065 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001066 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001067 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001068 ASSERT_TRUE(pr_answer);
1069 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001071 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001072 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001073 ASSERT_TRUE(answer);
1074 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1076 }
1077
Seth Hampson845e8782018-03-02 11:34:10 -08001078 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001079 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001080 // called with the given stream id and expected number of tracks.
1081 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001082 int expected_num_tracks) {
1083 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001084 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001085 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001086 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 }
1088
1089 // Creates an offer and applies it as a local session description.
1090 // Creates an answer with the same SDP an the offer but removes all lines
1091 // that start with a:ssrc"
1092 void CreateOfferReceiveAnswerWithoutSsrc() {
1093 CreateOfferAsLocalDescription();
1094 std::string sdp;
1095 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1096 SetSsrcToZero(&sdp);
1097 CreateAnswerAsRemoteDescription(sdp);
1098 }
1099
deadbeefab9b2d12015-10-14 11:33:11 -07001100 // This function creates a MediaStream with label kStreams[0] and
1101 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1102 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001103 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001104 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001105 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001106 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1107 size_t number_of_video_tracks) {
1108 EXPECT_LE(number_of_audio_tracks, 2u);
1109 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001110
1111 reference_collection_ = StreamCollection::Create();
1112 std::string sdp_ms1 = std::string(kSdpStringInit);
1113
Seth Hampson845e8782018-03-02 11:34:10 -08001114 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001115
1116 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001117 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001118 reference_collection_->AddStream(stream);
1119
1120 if (number_of_audio_tracks > 0) {
1121 sdp_ms1 += std::string(kSdpStringAudio);
1122 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1123 AddAudioTrack(kAudioTracks[0], stream);
1124 }
1125 if (number_of_audio_tracks > 1) {
1126 sdp_ms1 += kSdpStringMs1Audio1;
1127 AddAudioTrack(kAudioTracks[1], stream);
1128 }
1129
1130 if (number_of_video_tracks > 0) {
1131 sdp_ms1 += std::string(kSdpStringVideo);
1132 sdp_ms1 += std::string(kSdpStringMs1Video0);
1133 AddVideoTrack(kVideoTracks[0], stream);
1134 }
1135 if (number_of_video_tracks > 1) {
1136 sdp_ms1 += kSdpStringMs1Video1;
1137 AddVideoTrack(kVideoTracks[1], stream);
1138 }
1139
kwibergd1fe2812016-04-27 06:47:29 -07001140 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001141 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001142 }
1143
1144 void AddAudioTrack(const std::string& track_id,
1145 MediaStreamInterface* stream) {
1146 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1147 webrtc::AudioTrack::Create(track_id, nullptr));
1148 ASSERT_TRUE(stream->AddTrack(audio_track));
1149 }
1150
1151 void AddVideoTrack(const std::string& track_id,
1152 MediaStreamInterface* stream) {
1153 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001154 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001155 webrtc::FakeVideoTrackSource::Create(),
1156 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001157 ASSERT_TRUE(stream->AddTrack(video_track));
1158 }
1159
Steve Anton36da6ff2018-02-16 16:04:20 -08001160 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001161 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001162 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001163 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001164 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1165 return offer;
1166 }
1167
Steve Anton36da6ff2018-02-16 16:04:20 -08001168 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1169 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001170 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001171 std::unique_ptr<SessionDescriptionInterface> offer;
1172 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1173 return offer;
1174 }
1175
1176 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1177 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1178 std::unique_ptr<SessionDescriptionInterface> answer;
1179 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1180 return answer;
1181 }
1182
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001185 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001186 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001187 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1188 return answer;
1189 }
1190
1191 const std::string& GetFirstAudioStreamCname(
1192 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001193 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001194 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001195 return audio_desc->streams()[0].cname;
1196 }
1197
zhihuang1c378ed2017-08-17 14:10:50 -07001198 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1199 const RTCOfferAnswerOptions& offer_answer_options) {
1200 RTC_DCHECK(pc_);
1201 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1202 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1203 pc_->CreateOffer(observer, offer_answer_options);
1204 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1205 return observer->MoveDescription();
1206 }
1207
1208 void CreateOfferWithOptionsAsRemoteDescription(
1209 std::unique_ptr<SessionDescriptionInterface>* desc,
1210 const RTCOfferAnswerOptions& offer_answer_options) {
1211 *desc = CreateOfferWithOptions(offer_answer_options);
1212 ASSERT_TRUE(desc != nullptr);
1213 std::string sdp;
1214 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001215 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001216 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001217 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001218 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1219 }
1220
1221 void CreateOfferWithOptionsAsLocalDescription(
1222 std::unique_ptr<SessionDescriptionInterface>* desc,
1223 const RTCOfferAnswerOptions& offer_answer_options) {
1224 *desc = CreateOfferWithOptions(offer_answer_options);
1225 ASSERT_TRUE(desc != nullptr);
1226 std::string sdp;
1227 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001228 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001229 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001230
Steve Antondb45ca82017-09-11 18:27:34 -07001231 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001232 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1233 }
1234
1235 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001236 RTC_DCHECK(content);
1237 RTC_DCHECK(content->media_description());
1238 for (const cricket::AudioCodec& codec :
1239 content->media_description()->as_audio()->codecs()) {
1240 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001241 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001242 }
zhihuang1c378ed2017-08-17 14:10:50 -07001243 }
1244 return false;
1245 }
1246
Steve Anton36da6ff2018-02-16 16:04:20 -08001247 const char* GetSdpStringWithStream1() const {
1248 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1249 return kSdpStringWithStream1PlanB;
1250 } else {
1251 return kSdpStringWithStream1UnifiedPlan;
1252 }
1253 }
1254
1255 const char* GetSdpStringWithStream1And2() const {
1256 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1257 return kSdpStringWithStream1And2PlanB;
1258 } else {
1259 return kSdpStringWithStream1And2UnifiedPlan;
1260 }
1261 }
1262
deadbeef9a6f4d42017-05-15 19:43:33 -07001263 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1264 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001265 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001266 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001267 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001268 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1269 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1270 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001272 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001273 const SdpSemantics sdp_semantics_;
1274};
1275
1276class PeerConnectionInterfaceTest
1277 : public PeerConnectionInterfaceBaseTest,
1278 public ::testing::WithParamInterface<SdpSemantics> {
1279 protected:
1280 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1281};
1282
1283class PeerConnectionInterfaceTestPlanB
1284 : public PeerConnectionInterfaceBaseTest {
1285 protected:
1286 PeerConnectionInterfaceTestPlanB()
1287 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288};
1289
zhihuang8f65cdf2016-05-06 18:40:30 -07001290// Generate different CNAMEs when PeerConnections are created.
1291// The CNAMEs are expected to be generated randomly. It is possible
1292// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001293TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001295 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001296 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001297 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001298 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1299 GetFirstAudioStreamCname(offer2.get()));
1300}
1301
Steve Anton36da6ff2018-02-16 16:04:20 -08001302TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001303 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001304 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001305 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001306 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001307 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1308 GetFirstAudioStreamCname(answer2.get()));
1309}
1310
Steve Anton36da6ff2018-02-16 16:04:20 -08001311TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 CreatePeerConnectionWithDifferentConfigurations) {
1313 CreatePeerConnectionWithDifferentConfigurations();
1314}
1315
Steve Anton36da6ff2018-02-16 16:04:20 -08001316TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001317 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1318 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1319 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1320 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1321 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1322 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1323 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1324 port_allocator_->candidate_filter());
1325 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1326 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1327}
1328
1329// Test that when a PeerConnection is created with a nonzero candidate pool
1330// size, the pooled PortAllocatorSession is created with all the attributes
1331// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001332TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001333 PeerConnectionInterface::RTCConfiguration config;
1334 PeerConnectionInterface::IceServer server;
1335 server.uri = kStunAddressOnly;
1336 config.servers.push_back(server);
1337 config.type = PeerConnectionInterface::kRelay;
1338 config.disable_ipv6 = true;
1339 config.tcp_candidate_policy =
1340 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001341 config.candidate_network_policy =
1342 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001344 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001345
1346 const cricket::FakePortAllocatorSession* session =
1347 static_cast<const cricket::FakePortAllocatorSession*>(
1348 port_allocator_->GetPooledSession());
1349 ASSERT_NE(nullptr, session);
1350 EXPECT_EQ(1UL, session->stun_servers().size());
1351 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1352 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001353 EXPECT_LT(0U,
1354 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001355}
1356
deadbeefd21eab32017-07-26 16:50:11 -07001357// Test that network-related RTCConfiguration members are applied to the
1358// PortAllocator when CreatePeerConnection is called. Specifically:
1359// - disable_ipv6_on_wifi
1360// - max_ipv6_networks
1361// - tcp_candidate_policy
1362// - candidate_network_policy
1363// - prune_turn_ports
1364//
1365// Note that the candidate filter (RTCConfiguration::type) is already tested
1366// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001367TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001368 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1369 // Create fake port allocator.
1370 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1371 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1372 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1373
1374 // Create RTCConfiguration with some network-related fields relevant to
1375 // PortAllocator populated.
1376 PeerConnectionInterface::RTCConfiguration config;
1377 config.disable_ipv6_on_wifi = true;
1378 config.max_ipv6_networks = 10;
1379 config.tcp_candidate_policy =
1380 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1381 config.candidate_network_policy =
1382 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1383 config.prune_turn_ports = true;
1384
1385 // Create the PC factory and PC with the above config.
1386 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1387 webrtc::CreatePeerConnectionFactory(
1388 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001389 rtc::Thread::Current(), fake_audio_capture_module_,
1390 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001391 webrtc::CreateBuiltinAudioDecoderFactory(),
1392 webrtc::CreateBuiltinVideoEncoderFactory(),
1393 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1394 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001395 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001396 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1397 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001398 EXPECT_TRUE(pc.get());
1399 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001400
1401 // Now validate that the config fields set above were applied to the
1402 // PortAllocator, as flags or otherwise.
1403 EXPECT_FALSE(raw_port_allocator->flags() &
1404 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1405 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1406 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1407 EXPECT_TRUE(raw_port_allocator->flags() &
1408 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001409 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1410 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001411}
1412
deadbeef46c73892016-11-16 19:42:04 -08001413// Check that GetConfiguration returns the configuration the PeerConnection was
1414// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001415TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001416 PeerConnectionInterface::RTCConfiguration config;
1417 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001418 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001419
1420 PeerConnectionInterface::RTCConfiguration returned_config =
1421 pc_->GetConfiguration();
1422 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1423}
1424
1425// Check that GetConfiguration returns the last configuration passed into
1426// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001427TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001428 PeerConnectionInterface::RTCConfiguration starting_config;
1429 starting_config.bundle_policy =
1430 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1431 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001432
Steve Anton36da6ff2018-02-16 16:04:20 -08001433 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001434 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001435 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001436
1437 PeerConnectionInterface::RTCConfiguration returned_config =
1438 pc_->GetConfiguration();
1439 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1440}
1441
Steve Antonc79268f2018-04-24 09:54:10 -07001442TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1443 CreatePeerConnection();
1444
1445 pc_->Close();
1446
1447 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001448 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001449}
1450
Steve Anton36da6ff2018-02-16 16:04:20 -08001451TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001452 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001453 AddVideoStream(kStreamId1);
1454 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 ASSERT_EQ(2u, pc_->local_streams()->count());
1456
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001457 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001458 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001459 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001460 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001461 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001462 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001464 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001465 EXPECT_EQ(3u, pc_->local_streams()->count());
1466
1467 // Remove the third stream.
1468 pc_->RemoveStream(pc_->local_streams()->at(2));
1469 EXPECT_EQ(2u, pc_->local_streams()->count());
1470
1471 // Remove the second stream.
1472 pc_->RemoveStream(pc_->local_streams()->at(1));
1473 EXPECT_EQ(1u, pc_->local_streams()->count());
1474
1475 // Remove the first stream.
1476 pc_->RemoveStream(pc_->local_streams()->at(0));
1477 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
deadbeefab9b2d12015-10-14 11:33:11 -07001480// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001481// Don't run under Unified Plan since the stream API is not available.
1482TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001483 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001484 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001485 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001486 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001487
deadbeefab9b2d12015-10-14 11:33:11 -07001488 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001489 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001490 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001491
deadbeefab9b2d12015-10-14 11:33:11 -07001492 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001493 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001494 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001495
1496 // Add another stream and ensure the offer includes both the old and new
1497 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001498 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001499 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001500
Steve Antonb1c1de12017-12-21 15:14:30 -08001501 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001502 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1503 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001504
Steve Antonb1c1de12017-12-21 15:14:30 -08001505 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001506 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1507 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001508}
1509
Steve Anton36da6ff2018-02-16 16:04:20 -08001510// Don't run under Unified Plan since the stream API is not available.
1511TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001513 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 ASSERT_EQ(1u, pc_->local_streams()->count());
1515 pc_->RemoveStream(pc_->local_streams()->at(0));
1516 EXPECT_EQ(0u, pc_->local_streams()->count());
1517}
1518
deadbeefe1f9d832016-01-14 15:35:42 -08001519// Test for AddTrack and RemoveTrack methods.
1520// Tests that the created offer includes tracks we added,
1521// and that the RtpSenders are created correctly.
1522// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001523// Only tested with Plan B since Unified Plan is covered in more detail by tests
1524// in peerconnection_jsep_unittests.cc
1525TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001526 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001528 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001529 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001530 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001531 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1532 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001533 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001534 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001535 EXPECT_EQ("audio_track", audio_sender->id());
1536 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001537 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001538 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001539 EXPECT_EQ("video_track", video_sender->id());
1540 EXPECT_EQ(video_track, video_sender->track());
1541
1542 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001543 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001544 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001545
1546 const cricket::ContentInfo* audio_content =
1547 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001548 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001549 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 const cricket::ContentInfo* video_content =
1552 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001553 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001554 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
Steve Antondb45ca82017-09-11 18:27:34 -07001556 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001557
1558 // Now try removing the tracks.
1559 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1560 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1561
1562 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001563 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001564
1565 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001566 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001567 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001570 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001571 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001572
Steve Antondb45ca82017-09-11 18:27:34 -07001573 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001574
1575 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1576 // should return false.
1577 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1578 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1579}
1580
1581// Test creating senders without a stream specified,
1582// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001583TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001584 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001586 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001587 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001588 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001589 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001590 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001591 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001592 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001593 EXPECT_EQ("audio_track", audio_sender->id());
1594 EXPECT_EQ(audio_track, audio_sender->track());
1595 EXPECT_EQ("video_track", video_sender->id());
1596 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001597 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1598 // If the ID is truly a random GUID, it should be infinitely unlikely they
1599 // will be the same.
1600 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1601 } else {
1602 // We allows creating tracks without stream ids under Unified Plan
1603 // semantics.
1604 EXPECT_EQ(0u, video_sender->stream_ids().size());
1605 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1606 }
deadbeefe1f9d832016-01-14 15:35:42 -08001607}
1608
Harald Alvestrand89061872018-01-02 14:08:34 +01001609// Test that we can call GetStats() after AddTrack but before connecting
1610// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001611TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001612 CreatePeerConnectionWithoutDtls();
1613 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001614 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001615 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001616 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001617 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1618 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001619 EXPECT_TRUE(DoGetStats(nullptr));
1620}
1621
Steve Anton36da6ff2018-02-16 16:04:20 -08001622TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001623 CreatePeerConnectionWithoutDtls();
1624 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001625 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001626 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001627 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001628 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001629 ASSERT_TRUE(audio_sender.ok());
1630 auto* audio_sender_proxy =
1631 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1632 audio_sender.value().get());
1633 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1634
Harald Alvestrandc72af932018-01-11 17:18:19 +01001635 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001636 ASSERT_TRUE(video_sender.ok());
1637 auto* video_sender_proxy =
1638 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1639 video_sender.value().get());
1640 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001641}
1642
Steve Anton36da6ff2018-02-16 16:04:20 -08001643// Don't run under Unified Plan since the stream API is not available.
1644TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001645 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001646 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001647 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001648 ASSERT_EQ(1u, senders.size());
1649 auto* sender_proxy =
1650 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1651 senders[0].get());
1652 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001653}
1654
Steve Anton36da6ff2018-02-16 16:04:20 -08001655TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001657 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 VerifyRemoteRtpHeaderExtensions();
1659}
1660
Steve Anton36da6ff2018-02-16 16:04:20 -08001661TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001663 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 CreateOfferAsLocalDescription();
1665 std::string offer;
1666 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1667 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001668 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
Steve Anton36da6ff2018-02-16 16:04:20 -08001671TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001672 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001673 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674
1675 CreateOfferAsRemoteDescription();
1676 CreateAnswerAsLocalDescription();
1677
Seth Hampson845e8782018-03-02 11:34:10 -08001678 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
Steve Anton36da6ff2018-02-16 16:04:20 -08001681TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001682 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001683 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684
1685 CreateOfferAsRemoteDescription();
1686 CreatePrAnswerAsLocalDescription();
1687 CreateAnswerAsLocalDescription();
1688
Seth Hampson845e8782018-03-02 11:34:10 -08001689 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
Steve Anton36da6ff2018-02-16 16:04:20 -08001692// Don't run under Unified Plan since the stream API is not available.
1693TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 InitiateCall();
1695 ASSERT_EQ(1u, pc_->remote_streams()->count());
1696 pc_->RemoveStream(pc_->local_streams()->at(0));
1697 CreateOfferReceiveAnswer();
1698 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001699 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 CreateOfferReceiveAnswer();
1701}
1702
1703// Tests that after negotiating an audio only call, the respondent can perform a
1704// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001705TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001706 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001707 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 CreateOfferAsRemoteDescription();
1709 CreateAnswerAsLocalDescription();
1710
1711 ASSERT_EQ(1u, pc_->remote_streams()->count());
1712 pc_->RemoveStream(pc_->local_streams()->at(0));
1713 CreateOfferReceiveAnswer();
1714 EXPECT_EQ(0u, pc_->remote_streams()->count());
1715}
1716
1717// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001718TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720
Steve Antonf1c6db12017-10-13 11:13:35 -07001721 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001724 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001725 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001726 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
1728 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001729 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001730 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001731 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
Steve Antonf1c6db12017-10-13 11:13:35 -07001733 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001734 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735
Steve Antonf1c6db12017-10-13 11:13:35 -07001736 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
deadbeefab9b2d12015-10-14 11:33:11 -07001739// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001741TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001742 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001744 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001745 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001746 EXPECT_TRUE(offer);
1747 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748
1749 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001750 AddAudioTrack("track_label", {kStreamId1});
1751 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752
1753 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001754 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755
1756 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001757 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001758 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759}
1760
1761// Test that we will get different SSRCs for each tracks in the offer and answer
1762// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001763TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001764 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001766 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1767 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768
1769 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001770 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001771 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 int audio_ssrc = 0;
1773 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001774 EXPECT_TRUE(
1775 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1776 EXPECT_TRUE(
1777 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 EXPECT_NE(audio_ssrc, video_ssrc);
1779
1780 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001781 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001783 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 audio_ssrc = 0;
1785 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001786 EXPECT_TRUE(
1787 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1788 EXPECT_TRUE(
1789 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 EXPECT_NE(audio_ssrc, video_ssrc);
1791}
1792
deadbeefeb459812015-12-15 19:24:43 -08001793// Test that it's possible to call AddTrack on a MediaStream after adding
1794// the stream to a PeerConnection.
1795// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001796// Don't run under Unified Plan since the stream API is not available.
1797TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001798 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001799 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001800 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001801 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1802
1803 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001804 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001805 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001806 stream->AddTrack(video_track.get());
1807
kwibergd1fe2812016-04-27 06:47:29 -07001808 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001809 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001810
1811 const cricket::MediaContentDescription* video_desc =
1812 cricket::GetFirstVideoContentDescription(offer->description());
1813 EXPECT_TRUE(video_desc != nullptr);
1814}
1815
1816// Test that it's possible to call RemoveTrack on a MediaStream after adding
1817// the stream to a PeerConnection.
1818// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001819// Don't run under Unified Plan since the stream API is not available.
1820TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001821 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001822 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001823 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001824 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1825
1826 // Remove the video track.
1827 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1828
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001830 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001831
1832 const cricket::MediaContentDescription* video_desc =
1833 cricket::GetFirstVideoContentDescription(offer->description());
1834 EXPECT_TRUE(video_desc == nullptr);
1835}
1836
deadbeefbd7d8f72015-12-18 16:58:44 -08001837// Test creating a sender with a stream ID, and ensure the ID is populated
1838// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001839// Don't run under Unified Plan since the stream API is not available.
1840TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001841 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001842 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001843
kwibergd1fe2812016-04-27 06:47:29 -07001844 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001845 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001846
1847 const cricket::MediaContentDescription* video_desc =
1848 cricket::GetFirstVideoContentDescription(offer->description());
1849 ASSERT_TRUE(video_desc != nullptr);
1850 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001851 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001855TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001857 ASSERT_LT(0u, pc_->GetSenders().size());
1858 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001859 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001860 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 EXPECT_TRUE(DoGetStats(remote_audio));
1862
1863 // Remove the stream. Since we are sending to our selves the local
1864 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001865 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 // Do a re-negotiation.
1867 CreateOfferReceiveAnswer();
1868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 // Test that we still can get statistics for the old track. Even if it is not
1870 // sent any longer.
1871 EXPECT_TRUE(DoGetStats(remote_audio));
1872}
1873
1874// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001875TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001877 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1878 ASSERT_TRUE(video_receiver);
1879 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880}
1881
1882// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001883TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001885 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 pc_factory_->CreateAudioTrack("unknown track", NULL));
1887 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1888}
1889
Steve Anton36da6ff2018-02-16 16:04:20 -08001890TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001891 CreatePeerConnectionWithoutDtls();
1892 EXPECT_TRUE(DoGetRTCStats());
1893 // Clearing stats cache is needed now, but should be temporary.
1894 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1895 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001896 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1897 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001898 EXPECT_TRUE(DoGetRTCStats());
1899 pc_->ClearStatsCache();
1900 CreateOfferReceiveAnswer();
1901 EXPECT_TRUE(DoGetRTCStats());
1902}
1903
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904// This tests that a SCTP data channel is returned using different
1905// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08001906TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001907 RTCConfiguration rtc_config;
1908 rtc_config.enable_dtls_srtp = true;
1909 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;
1944 rtc_config.enable_dtls_srtp = true;
1945 CreatePeerConnection(rtc_config);
1946
1947 webrtc::DataChannelInit config;
1948 config.maxRetransmitTime = -1;
1949 config.maxRetransmits = -1;
1950 rtc::scoped_refptr<DataChannelInterface> channel =
1951 pc_->CreateDataChannel("1", &config);
1952 EXPECT_TRUE(channel != NULL);
1953}
1954
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955// This tests that no data channel is returned if both maxRetransmits and
1956// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08001957TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001959 RTCConfiguration rtc_config;
1960 rtc_config.enable_dtls_srtp = true;
1961 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 std::string label = "test";
1964 webrtc::DataChannelInit config;
1965 config.maxRetransmits = 0;
1966 config.maxRetransmitTime = 0;
1967
zhihuang9763d562016-08-05 11:14:50 -07001968 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 pc_->CreateDataChannel(label, &config);
1970 EXPECT_TRUE(channel == NULL);
1971}
1972
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973// The test verifies that creating a SCTP data channel with an id already in use
1974// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001975TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001977 RTCConfiguration rtc_config;
1978 rtc_config.enable_dtls_srtp = true;
1979 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980
1981 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001982 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001984 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001985 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001986 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 EXPECT_TRUE(channel != NULL);
1988 EXPECT_EQ(1, channel->id());
1989
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 channel = pc_->CreateDataChannel("x", &config);
1991 EXPECT_TRUE(channel == NULL);
1992
1993 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02001994 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 channel = pc_->CreateDataChannel("max", &config);
1996 EXPECT_TRUE(channel != NULL);
1997 EXPECT_EQ(config.id, channel->id());
1998
1999 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002000 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 channel = pc_->CreateDataChannel("x", &config);
2002 EXPECT_TRUE(channel == NULL);
2003}
2004
deadbeefab9b2d12015-10-14 11:33:11 -07002005// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002006TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002007 RTCConfiguration rtc_config;
2008 rtc_config.enable_dtls_srtp = true;
2009 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002010
2011 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002012 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002013 pc_->CreateDataChannel(label, nullptr);
2014 EXPECT_NE(channel, nullptr);
2015
zhihuang9763d562016-08-05 11:14:50 -07002016 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002017 pc_->CreateDataChannel(label, nullptr);
2018 EXPECT_NE(dup_channel, nullptr);
2019}
2020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002022#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002023// This tests that SCTP data channels can be rejected in an answer.
2024TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2025#else
2026TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2027#endif
2028{
Niels Möllerf06f9232018-08-07 12:32:18 +02002029 RTCConfiguration rtc_config;
2030 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002031
2032 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2033 pc_->CreateDataChannel("offer_channel", NULL));
2034
2035 CreateOfferAsLocalDescription();
2036
2037 // Create an answer where the m-line for data channels are rejected.
2038 std::string sdp;
2039 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2040 std::unique_ptr<SessionDescriptionInterface> answer(
2041 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2042 ASSERT_TRUE(answer);
2043 cricket::ContentInfo* data_info =
2044 cricket::GetFirstDataContent(answer->description());
2045 data_info->rejected = true;
2046
2047 DoSetRemoteDescription(std::move(answer));
2048 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2049}
2050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051// Test that we can create a session description from an SDP string from
2052// FireFox, use it as a remote session description, generate an answer and use
2053// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002054TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002055 RTCConfiguration rtc_config;
2056 rtc_config.enable_dtls_srtp = true;
2057 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002058 AddAudioTrack("audio_label");
2059 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002060 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002061 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002062 webrtc::kFireFoxSdpOffer, nullptr));
2063 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 CreateAnswerAsLocalDescription();
2065 ASSERT_TRUE(pc_->local_description() != NULL);
2066 ASSERT_TRUE(pc_->remote_description() != NULL);
2067
2068 const cricket::ContentInfo* content =
2069 cricket::GetFirstAudioContent(pc_->local_description()->description());
2070 ASSERT_TRUE(content != NULL);
2071 EXPECT_FALSE(content->rejected);
2072
2073 content =
2074 cricket::GetFirstVideoContent(pc_->local_description()->description());
2075 ASSERT_TRUE(content != NULL);
2076 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002077#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 content =
2079 cricket::GetFirstDataContent(pc_->local_description()->description());
2080 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002081 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002082#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083}
2084
zhihuangb19012e2017-09-19 13:47:59 -07002085// Test that fallback from DTLS to SDES is not supported.
2086// The fallback was previously supported but was removed to simplify the code
2087// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002088TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002089 RTCConfiguration rtc_config;
2090 rtc_config.enable_dtls_srtp = true;
2091 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002092 // Wait for fake certificate to be generated. Previously, this is what caused
2093 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002094 AddAudioTrack("audio_label");
2095 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002096 ASSERT_NE(nullptr, fake_certificate_generator_);
2097 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2098 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002099 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002100 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2101 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002102 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002103}
2104
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105// Test that we can create an audio only offer and receive an answer with a
2106// limited set of audio codecs and receive an updated offer with more audio
2107// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002108TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002109 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002110 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 CreateOfferAsLocalDescription();
2112
Steve Anton36da6ff2018-02-16 16:04:20 -08002113 const char* answer_sdp =
2114 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2115 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002116 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002117 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002118 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119
Steve Anton36da6ff2018-02-16 16:04:20 -08002120 const char* reoffer_sdp =
2121 (sdp_semantics_ == SdpSemantics::kPlanB
2122 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2123 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002124 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002125 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002126 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 CreateAnswerAsLocalDescription();
2128}
2129
deadbeefc80741f2015-10-22 13:14:45 -07002130// Test that if we're receiving (but not sending) a track, subsequent offers
2131// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002132TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002133 RTCConfiguration rtc_config;
2134 rtc_config.enable_dtls_srtp = true;
2135 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002136 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002137 CreateAnswerAsLocalDescription();
2138
2139 // At this point we should be receiving stream 1, but not sending anything.
2140 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002141 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002142 DoCreateOffer(&offer, nullptr);
2143
2144 const cricket::ContentInfo* video_content =
2145 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002146 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2147 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002148
2149 const cricket::ContentInfo* audio_content =
2150 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002151 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2152 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002153}
2154
2155// Test that if we're receiving (but not sending) a track, and the
2156// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2157// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002158TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002159 RTCConfiguration rtc_config;
2160 rtc_config.enable_dtls_srtp = true;
2161 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002162 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002163 CreateAnswerAsLocalDescription();
2164
2165 // At this point we should be receiving stream 1, but not sending anything.
2166 // A new offer would be recvonly, but we'll set the "no receive" constraints
2167 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002168 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002169 RTCOfferAnswerOptions options;
2170 options.offer_to_receive_audio = 0;
2171 options.offer_to_receive_video = 0;
2172 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002173
2174 const cricket::ContentInfo* video_content =
2175 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002176 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2177 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002178
2179 const cricket::ContentInfo* audio_content =
2180 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002181 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2182 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002183}
2184
deadbeef653b8e02015-11-11 12:55:10 -08002185// Test that we can use SetConfiguration to change the ICE servers of the
2186// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002187TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002188 CreatePeerConnection();
2189
Steve Anton36da6ff2018-02-16 16:04:20 -08002190 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002191 PeerConnectionInterface::IceServer server;
2192 server.uri = "stun:test_hostname";
2193 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002194 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002195
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002196 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2197 EXPECT_EQ("test_hostname",
2198 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002199}
2200
Steve Anton36da6ff2018-02-16 16:04:20 -08002201TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002202 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002203 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002204 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002205 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002206 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2207}
2208
Steve Anton36da6ff2018-02-16 16:04:20 -08002209TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002210 PeerConnectionInterface::RTCConfiguration config;
2211 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002212 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002213 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002214 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002215
2216 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002217 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002218 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2219 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002220}
2221
skvladd1f5fda2017-02-03 16:54:05 -08002222// Test that the ice check interval can be changed. This does not verify that
2223// the setting makes it all the way to P2PTransportChannel, as that would
2224// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002225TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002226 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002227 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002228 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002229 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002230 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002231 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002232 config = pc_->GetConfiguration();
2233 EXPECT_EQ(config.ice_check_min_interval, 100);
2234}
2235
2236TEST_P(PeerConnectionInterfaceTest,
2237 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2238 PeerConnectionInterface::RTCConfiguration config;
2239 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2240 CreatePeerConnection(config);
2241 config = pc_->GetConfiguration();
2242 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2243
2244 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002245 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002246 config = pc_->GetConfiguration();
2247 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002248}
2249
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002250// Test that when SetConfiguration changes both the pool size and other
2251// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002252TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002253 SetConfigurationCreatesPooledSessionCorrectly) {
2254 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002255 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002256 config.ice_candidate_pool_size = 1;
2257 PeerConnectionInterface::IceServer server;
2258 server.uri = kStunAddressOnly;
2259 config.servers.push_back(server);
2260 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002261 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002262
2263 const cricket::FakePortAllocatorSession* session =
2264 static_cast<const cricket::FakePortAllocatorSession*>(
2265 port_allocator_->GetPooledSession());
2266 ASSERT_NE(nullptr, session);
2267 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002268}
2269
deadbeef293e9262017-01-11 12:28:30 -08002270// Test that after SetLocalDescription, changing the pool size is not allowed,
2271// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002272TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002273 CantChangePoolSizeAfterSetLocalDescription) {
2274 CreatePeerConnection();
2275 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002276 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002277 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002278 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002279
2280 // Set remote offer; can still change pool size at this point.
2281 CreateOfferAsRemoteDescription();
2282 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002283 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002284
2285 // Set local answer; now it's too late.
2286 CreateAnswerAsLocalDescription();
2287 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002288 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002289 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2290}
2291
deadbeef42a42632017-03-10 15:18:00 -08002292// Test that after setting an answer, extra pooled sessions are discarded. The
2293// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002294TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002295 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2296 CreatePeerConnection();
2297
2298 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002299 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002300 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002301 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002302
2303 // Do offer/answer.
2304 CreateOfferAsRemoteDescription();
2305 CreateAnswerAsLocalDescription();
2306
2307 // Expect no pooled sessions to be left.
2308 const cricket::PortAllocatorSession* session =
2309 port_allocator_->GetPooledSession();
2310 EXPECT_EQ(nullptr, session);
2311}
2312
2313// After Close is called, pooled candidates should be discarded so as to not
2314// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002315TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002316 CreatePeerConnection();
2317
Steve Anton36da6ff2018-02-16 16:04:20 -08002318 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002319 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002320 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002321 pc_->Close();
2322
2323 // Expect no pooled sessions to be left.
2324 const cricket::PortAllocatorSession* session =
2325 port_allocator_->GetPooledSession();
2326 EXPECT_EQ(nullptr, session);
2327}
2328
deadbeef293e9262017-01-11 12:28:30 -08002329// Test that SetConfiguration returns an invalid modification error if
2330// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002331TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002332 SetConfigurationReturnsInvalidModificationError) {
2333 PeerConnectionInterface::RTCConfiguration config;
2334 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2335 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2336 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002337 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002338
Steve Anton36da6ff2018-02-16 16:04:20 -08002339 PeerConnectionInterface::RTCConfiguration modified_config =
2340 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002341 modified_config.bundle_policy =
2342 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002343 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002344 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2345
Steve Anton36da6ff2018-02-16 16:04:20 -08002346 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002347 modified_config.rtcp_mux_policy =
2348 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002349 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002350 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2351
Steve Anton36da6ff2018-02-16 16:04:20 -08002352 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002353 modified_config.continual_gathering_policy =
2354 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002355 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002356 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2357}
2358
2359// Test that SetConfiguration returns a range error if the candidate pool size
2360// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002361TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002362 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2363 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002364 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002365 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002366
2367 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002368 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002369 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2370
2371 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002372 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002373 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2374}
2375
2376// Test that SetConfiguration returns a syntax error if parsing an ICE server
2377// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002378TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002379 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2380 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002381 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002382 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002383
2384 PeerConnectionInterface::IceServer bad_server;
2385 bad_server.uri = "stunn:www.example.com";
2386 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002387 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002388 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2389}
2390
2391// Test that SetConfiguration returns an invalid parameter error if a TURN
2392// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002393TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002394 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2395 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002396 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002397 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002398
2399 PeerConnectionInterface::IceServer bad_server;
2400 bad_server.uri = "turn:www.example.com";
2401 // Missing password.
2402 bad_server.username = "foo";
2403 config.servers.push_back(bad_server);
2404 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002405 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2406 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002407}
2408
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409// Test that PeerConnection::Close changes the states to closed and all remote
2410// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002411TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 // Initialize a PeerConnection and negotiate local and remote session
2413 // description.
2414 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002415
2416 // With Plan B, verify the stream count. The analog with Unified Plan is the
2417 // RtpTransceiver count.
2418 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2419 ASSERT_EQ(1u, pc_->local_streams()->count());
2420 ASSERT_EQ(1u, pc_->remote_streams()->count());
2421 } else {
2422 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2423 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424
2425 pc_->Close();
2426
2427 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2428 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2429 pc_->ice_connection_state());
2430 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2431 pc_->ice_gathering_state());
2432
Steve Anton36da6ff2018-02-16 16:04:20 -08002433 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2434 EXPECT_EQ(1u, pc_->local_streams()->count());
2435 EXPECT_EQ(1u, pc_->remote_streams()->count());
2436 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002437 // Verify that the RtpTransceivers are still returned.
2438 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440
Steve Anton36da6ff2018-02-16 16:04:20 -08002441 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002442 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002443 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2444 ASSERT_TRUE(audio_receiver);
2445 ASSERT_TRUE(video_receiver);
2446 // Track state may be updated asynchronously.
2447 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2448 audio_receiver->track()->state(), kTimeout);
2449 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2450 video_receiver->track()->state(), kTimeout);
2451 } else {
2452 ASSERT_FALSE(audio_receiver);
2453 ASSERT_FALSE(video_receiver);
2454 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455}
2456
2457// Test that PeerConnection methods fails gracefully after
2458// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002459// Don't run under Unified Plan since the stream API is not available.
2460TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002461 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002462 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463 CreateOfferAsRemoteDescription();
2464 CreateAnswerAsLocalDescription();
2465
2466 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002467 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468 pc_->local_streams()->at(0);
2469
2470 pc_->Close();
2471
2472 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002473 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002475 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2476
2477 EXPECT_TRUE(pc_->local_description() != NULL);
2478 EXPECT_TRUE(pc_->remote_description() != NULL);
2479
kwibergd1fe2812016-04-27 06:47:29 -07002480 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002481 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002482 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002483 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484
2485 std::string sdp;
2486 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002487 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002488 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002489 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490
2491 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002492 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002493 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002494 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495}
2496
2497// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002498TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499 InitiateCall();
2500 pc_->Close();
2501 DoGetStats(NULL);
2502}
deadbeefab9b2d12015-10-14 11:33:11 -07002503
2504// NOTE: The series of tests below come from what used to be
2505// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2506// setting a remote or local description has the expected effects.
2507
2508// This test verifies that the remote MediaStreams corresponding to a received
2509// SDP string is created. In this test the two separate MediaStreams are
2510// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002511TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002512 RTCConfiguration config;
2513 config.enable_dtls_srtp = true;
2514 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002515 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002516
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002517 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002518 EXPECT_TRUE(
2519 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2520 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2521 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2522
2523 // Create a session description based on another SDP with another
2524 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002525 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002526
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002527 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002528 EXPECT_TRUE(
2529 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2530}
2531
2532// This test verifies that when remote tracks are added/removed from SDP, the
2533// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002534// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2535// specific behavior.
2536TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002537 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002538 RTCConfiguration config;
2539 config.enable_dtls_srtp = true;
2540 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002541 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002542 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002543 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002544 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2545 reference_collection_));
2546
2547 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002548 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002549 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002550 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002551 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2552 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002553 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002554 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2555 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002556 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002557 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2558 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002559
2560 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002561 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002562 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002563 MockTrackObserver audio_track_observer(audio_track2);
2564 MockTrackObserver video_track_observer(video_track2);
2565
2566 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2567 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002568 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002569 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2570 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002571 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002572 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002573 audio_track2->state(), kTimeout);
2574 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2575 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002576}
2577
2578// This tests that remote tracks are ended if a local session description is set
2579// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002580TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002581 RTCConfiguration config;
2582 config.enable_dtls_srtp = true;
2583 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002584 // First create and set a remote offer, then reject its video content in our
2585 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002586 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2587 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2588 ASSERT_TRUE(audio_receiver);
2589 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2590 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002591
Steve Anton36da6ff2018-02-16 16:04:20 -08002592 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2593 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002594 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002595 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2596 video_receiver->track();
2597 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002598
kwibergd1fe2812016-04-27 06:47:29 -07002599 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002600 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002601 cricket::ContentInfo* video_info =
2602 local_answer->description()->GetContentByName("video");
2603 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002604 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002605 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2606 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002607
2608 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002609 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002610 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002611 video_info = local_offer->description()->GetContentByName("video");
2612 ASSERT_TRUE(video_info != nullptr);
2613 video_info->rejected = true;
2614 cricket::ContentInfo* audio_info =
2615 local_offer->description()->GetContentByName("audio");
2616 ASSERT_TRUE(audio_info != nullptr);
2617 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002618 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002619 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002620 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2621 kTimeout);
2622 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2623 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002624}
2625
2626// This tests that we won't crash if the remote track has been removed outside
2627// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002628// Don't run under Unified Plan since the stream API is not available.
2629TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002630 RTCConfiguration config;
2631 config.enable_dtls_srtp = true;
2632 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002633 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002634 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2635 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2636 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2637
kwibergd1fe2812016-04-27 06:47:29 -07002638 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002639 webrtc::CreateSessionDescription(SdpType::kAnswer,
2640 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002641 cricket::ContentInfo* video_info =
2642 local_answer->description()->GetContentByName("video");
2643 video_info->rejected = true;
2644 cricket::ContentInfo* audio_info =
2645 local_answer->description()->GetContentByName("audio");
2646 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002647 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002648
2649 // No crash is a pass.
2650}
2651
deadbeef5e97fb52015-10-15 12:49:08 -07002652// This tests that if a recvonly remote description is set, no remote streams
2653// will be created, even if the description contains SSRCs/MSIDs.
2654// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002655TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002656 RTCConfiguration config;
2657 config.enable_dtls_srtp = true;
2658 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002659
Steve Anton36da6ff2018-02-16 16:04:20 -08002660 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002661 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002662 CreateAndSetRemoteOffer(recvonly_offer);
2663
2664 EXPECT_EQ(0u, observer_.remote_streams()->count());
2665}
2666
deadbeefab9b2d12015-10-14 11:33:11 -07002667// This tests that a default MediaStream is created if a remote session
2668// description doesn't contain any streams and no MSID support.
2669// It also tests that the default stream is updated if a video m-line is added
2670// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002671// Don't run under Unified Plan since this behavior is Plan B specific.
2672TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002673 RTCConfiguration config;
2674 config.enable_dtls_srtp = true;
2675 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002676 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2677
2678 ASSERT_EQ(1u, observer_.remote_streams()->count());
2679 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2680
2681 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2682 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002683 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002684
2685 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2686 ASSERT_EQ(1u, observer_.remote_streams()->count());
2687 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2688 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002689 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2690 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002691 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2692 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002693 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2694 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002695}
2696
2697// This tests that a default MediaStream is created if a remote session
2698// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002699// Don't run under Unified Plan since this behavior is Plan B specific.
2700TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002701 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002702 RTCConfiguration config;
2703 config.enable_dtls_srtp = true;
2704 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002705 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2706
2707 ASSERT_EQ(1u, observer_.remote_streams()->count());
2708 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2709
2710 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2711 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002712 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002713}
2714
2715// This tests that it won't crash when PeerConnection tries to remove
2716// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002717// Don't run under Unified Plan since this behavior is Plan B specific.
2718TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002719 RTCConfiguration config;
2720 config.enable_dtls_srtp = true;
2721 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002722 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002723 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2724 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2725 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2726
2727 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2728
2729 // No crash is a pass.
2730}
2731
2732// This tests that a default MediaStream is created if the remote session
2733// description doesn't contain any streams and don't contain an indication if
2734// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002735// Don't run under Unified Plan since this behavior is Plan B specific.
2736TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002737 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002738 RTCConfiguration config;
2739 config.enable_dtls_srtp = true;
2740 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002741 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2742
2743 ASSERT_EQ(1u, observer_.remote_streams()->count());
2744 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2745 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2746 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2747}
2748
2749// This tests that a default MediaStream is not created if the remote session
2750// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002751// Don't run under Unified Plan since this behavior is Plan B specific.
2752TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002753 RTCConfiguration config;
2754 config.enable_dtls_srtp = true;
2755 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002756 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2757 EXPECT_EQ(0u, observer_.remote_streams()->count());
2758}
2759
deadbeefbda7e0b2015-12-08 17:13:40 -08002760// This tests that when setting a new description, the old default tracks are
2761// not destroyed and recreated.
2762// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002763// Don't run under Unified Plan since this behavior is Plan B specific.
2764TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002765 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002766 RTCConfiguration config;
2767 config.enable_dtls_srtp = true;
2768 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002769 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2770
2771 ASSERT_EQ(1u, observer_.remote_streams()->count());
2772 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2773 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2774
2775 // Set the track to "disabled", then set a new description and ensure the
2776 // track is still disabled, which ensures it hasn't been recreated.
2777 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2778 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2779 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2780 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2781}
2782
deadbeefab9b2d12015-10-14 11:33:11 -07002783// This tests that a default MediaStream is not created if a remote session
2784// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08002785// Don't run under Unified Plan since this behavior is Plan B specific.
2786TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002787 RTCConfiguration config;
2788 config.enable_dtls_srtp = true;
2789 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002790 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002791 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002792 EXPECT_TRUE(
2793 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2794
2795 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2796 EXPECT_EQ(0u, observer_.remote_streams()->count());
2797}
2798
Seth Hampson5897a6e2018-04-03 11:16:33 -07002799// This tests that a default MediaStream is created if a remote SDP comes from
2800// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
2801TEST_F(PeerConnectionInterfaceTestPlanB,
2802 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002803 RTCConfiguration config;
2804 config.enable_dtls_srtp = true;
2805 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07002806 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
2807 // Add a=msid lines to simulate a Unified Plan endpoint that only
2808 // signals stream IDs with a=msid lines.
2809 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
2810
2811 CreateAndSetRemoteOffer(sdp_string);
2812
2813 ASSERT_EQ(1u, observer_.remote_streams()->count());
2814 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2815 EXPECT_EQ("default", remote_stream->id());
2816 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2817}
2818
Seth Hampson5b4f0752018-04-02 16:31:36 -07002819// This tests that when a Plan B endpoint receives an SDP that signals no media
2820// stream IDs indicated by the special character "-" in the a=msid line, that
2821// a default stream ID will be used for the MediaStream ID. This can occur
2822// when a Unified Plan endpoint signals no media stream IDs, but signals both
2823// a=ssrc msid and a=msid lines for interop signaling with Plan B.
2824TEST_F(PeerConnectionInterfaceTestPlanB,
2825 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002826 RTCConfiguration config;
2827 config.enable_dtls_srtp = true;
2828 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07002829 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
2830 // the sender's stream ID will be interpreted as no stream IDs.
2831 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
2832 sdp_string.append("a=msid:- audiotrack0\n");
2833
2834 CreateAndSetRemoteOffer(sdp_string);
2835
2836 ASSERT_EQ(1u, observer_.remote_streams()->count());
2837 // Because SSRCs are signaled the track ID will be what was signaled in the
2838 // a=msid line.
2839 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2840 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2841 EXPECT_EQ("default", remote_stream->id());
2842 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07002843
2844 // Previously a bug ocurred when setting the remote description a second time.
2845 // This is because we checked equality of the remote StreamParams stream ID
2846 // (empty), and the previously set stream ID for the remote sender
2847 // ("default"). This cause a track to be removed, then added, when really
2848 // nothing should occur because it is the same track.
2849 CreateAndSetRemoteOffer(sdp_string);
2850 EXPECT_EQ(0u, observer_.remove_track_events_.size());
2851 EXPECT_EQ(1u, observer_.add_track_events_.size());
2852 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
2853 remote_stream = observer_.remote_streams()->at(0);
2854 EXPECT_EQ("default", remote_stream->id());
2855 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07002856}
2857
deadbeefab9b2d12015-10-14 11:33:11 -07002858// This tests that an RtpSender is created when the local description is set
2859// after adding a local stream.
2860// TODO(deadbeef): This test and the one below it need to be updated when
2861// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08002862// Don't run under Unified Plan since this behavior is Plan B specific.
2863TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002864 RTCConfiguration config;
2865 config.enable_dtls_srtp = true;
2866 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002867
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002868 // Create an offer with 1 stream with 2 tracks of each type.
2869 rtc::scoped_refptr<StreamCollection> stream_collection =
2870 CreateStreamCollection(1, 2);
2871 pc_->AddStream(stream_collection->at(0));
2872 std::unique_ptr<SessionDescriptionInterface> offer;
2873 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002874 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002875
deadbeefab9b2d12015-10-14 11:33:11 -07002876 auto senders = pc_->GetSenders();
2877 EXPECT_EQ(4u, senders.size());
2878 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2879 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2880 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2881 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2882
2883 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002884 pc_->RemoveStream(stream_collection->at(0));
2885 stream_collection = CreateStreamCollection(1, 1);
2886 pc_->AddStream(stream_collection->at(0));
2887 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002888 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002889
deadbeefab9b2d12015-10-14 11:33:11 -07002890 senders = pc_->GetSenders();
2891 EXPECT_EQ(2u, senders.size());
2892 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2893 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2894 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2895 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2896}
2897
2898// This tests that an RtpSender is created when the local description is set
2899// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002900// Don't run under Unified Plan since this behavior is Plan B specific.
2901TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002902 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002903 RTCConfiguration config;
2904 config.enable_dtls_srtp = true;
2905 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002906
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002907 rtc::scoped_refptr<StreamCollection> stream_collection =
2908 CreateStreamCollection(1, 2);
2909 // Add a stream to create the offer, but remove it afterwards.
2910 pc_->AddStream(stream_collection->at(0));
2911 std::unique_ptr<SessionDescriptionInterface> offer;
2912 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2913 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002914
Steve Antondb45ca82017-09-11 18:27:34 -07002915 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002916 auto senders = pc_->GetSenders();
2917 EXPECT_EQ(0u, senders.size());
2918
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002919 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002920 senders = pc_->GetSenders();
2921 EXPECT_EQ(4u, senders.size());
2922 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2923 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2924 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2925 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2926}
2927
2928// This tests that the expected behavior occurs if the SSRC on a local track is
2929// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002930TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002931 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002932 RTCConfiguration config;
2933 config.enable_dtls_srtp = true;
2934 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002935
Steve Anton36da6ff2018-02-16 16:04:20 -08002936 AddAudioTrack(kAudioTracks[0]);
2937 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002938 std::unique_ptr<SessionDescriptionInterface> offer;
2939 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2940 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07002941 std::unique_ptr<SessionDescriptionInterface> modified_offer =
2942 webrtc::CreateSessionDescription(
2943 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02002944 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07002945 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002946
deadbeefab9b2d12015-10-14 11:33:11 -07002947 auto senders = pc_->GetSenders();
2948 EXPECT_EQ(2u, senders.size());
2949 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2950 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2951
2952 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002953 cricket::MediaContentDescription* desc =
2954 cricket::GetFirstAudioContentDescription(modified_offer->description());
2955 ASSERT_TRUE(desc != NULL);
2956 for (StreamParams& stream : desc->mutable_streams()) {
2957 for (unsigned int& ssrc : stream.ssrcs) {
2958 ++ssrc;
2959 }
2960 }
deadbeefab9b2d12015-10-14 11:33:11 -07002961
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002962 desc =
2963 cricket::GetFirstVideoContentDescription(modified_offer->description());
2964 ASSERT_TRUE(desc != NULL);
2965 for (StreamParams& stream : desc->mutable_streams()) {
2966 for (unsigned int& ssrc : stream.ssrcs) {
2967 ++ssrc;
2968 }
2969 }
2970
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002972 senders = pc_->GetSenders();
2973 EXPECT_EQ(2u, senders.size());
2974 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2975 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2976 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2977 // changed.
2978}
2979
2980// This tests that the expected behavior occurs if a new session description is
2981// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002982// Don't run under Unified Plan since the stream API is not available.
2983TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002984 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002985 RTCConfiguration config;
2986 config.enable_dtls_srtp = true;
2987 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002988
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002989 rtc::scoped_refptr<StreamCollection> stream_collection =
2990 CreateStreamCollection(2, 1);
2991 pc_->AddStream(stream_collection->at(0));
2992 std::unique_ptr<SessionDescriptionInterface> offer;
2993 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002994 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002995
deadbeefab9b2d12015-10-14 11:33:11 -07002996 auto senders = pc_->GetSenders();
2997 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002998 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2999 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003000
3001 // Add a new MediaStream but with the same tracks as in the first stream.
3002 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3003 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003004 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3005 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003006 pc_->AddStream(stream_1);
3007
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003008 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003009 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003010
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003011 auto new_senders = pc_->GetSenders();
3012 // Should be the same senders as before, but with updated stream id.
3013 // Note that this behavior is subject to change in the future.
3014 // We may decide the PC should ignore existing tracks in AddStream.
3015 EXPECT_EQ(senders, new_senders);
3016 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3017 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003018}
3019
zhihuang81c3a032016-11-17 12:06:24 -08003020// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003021TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003022 RTCConfiguration config;
3023 config.enable_dtls_srtp = true;
3024 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003025 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3026 EXPECT_EQ(observer_.num_added_tracks_, 1);
3027 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3028
3029 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003030 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003031 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003032 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3033}
3034
deadbeefd1a38b52016-12-10 13:15:33 -08003035// Test that when SetConfiguration is called and the configuration is
3036// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003037TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003038 PeerConnectionInterface::RTCConfiguration config;
3039 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003040 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003041 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003042 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3043 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003044
3045 // Do initial offer/answer so there's something to restart.
3046 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003047 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003048
3049 // Grab the ufrags.
3050 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3051
3052 // Change ICE policy, which should trigger an ICE restart on the next offer.
3053 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003054 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003055 CreateOfferAsLocalDescription();
3056
3057 // Grab the new ufrags.
3058 std::vector<std::string> subsequent_ufrags =
3059 GetUfrags(pc_->local_description());
3060
3061 // Sanity check.
3062 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3063 // Check that each ufrag is different.
3064 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3065 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3066 }
3067}
3068
3069// Test that when SetConfiguration is called and the configuration *isn't*
3070// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003071TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003072 PeerConnectionInterface::RTCConfiguration config;
3073 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003074 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003075 config = pc_->GetConfiguration();
3076 AddAudioTrack(kAudioTracks[0]);
3077 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003078
3079 // Do initial offer/answer so there's something to restart.
3080 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003081 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003082
3083 // Grab the ufrags.
3084 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3085
3086 // Call SetConfiguration with a config identical to what the PC was
3087 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003088 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003089 CreateOfferAsLocalDescription();
3090
3091 // Grab the new ufrags.
3092 std::vector<std::string> subsequent_ufrags =
3093 GetUfrags(pc_->local_description());
3094
3095 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3096}
3097
3098// Test for a weird corner case scenario:
3099// 1. Audio/video session established.
3100// 2. SetConfiguration changes ICE config; ICE restart needed.
3101// 3. ICE restart initiated by remote peer, but only for one m= section.
3102// 4. Next createOffer should initiate an ICE restart, but only for the other
3103// m= section; it would be pointless to do an ICE restart for the m= section
3104// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003105TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003106 PeerConnectionInterface::RTCConfiguration config;
3107 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003108 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003109 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003110 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3111 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003112
3113 // Do initial offer/answer so there's something to restart.
3114 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003115 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003116
3117 // Change ICE policy, which should set the "needs-ice-restart" flag.
3118 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003119 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003120
3121 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003122 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003123 webrtc::CreateSessionDescription(SdpType::kOffer,
3124 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003125 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003126 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3127 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003128 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003129 CreateAnswerAsLocalDescription();
3130
3131 // Grab the ufrags.
3132 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003133 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003134
3135 // Create offer and grab the new ufrags.
3136 CreateOfferAsLocalDescription();
3137 std::vector<std::string> subsequent_ufrags =
3138 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003139 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003140
3141 // Ensure that only the ufrag for the second m= section changed.
3142 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3143 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3144}
3145
deadbeeffe4a8a42016-12-20 17:56:17 -08003146// Tests that the methods to return current/pending descriptions work as
3147// expected at different points in the offer/answer exchange. This test does
3148// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003149TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003150 // This disables DTLS so we can apply an answer to ourselves.
3151 CreatePeerConnection();
3152
3153 // Create initial local offer and get SDP (which will also be used as
3154 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003155 std::unique_ptr<SessionDescriptionInterface> local_offer;
3156 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003157 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003158 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003159
3160 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003161 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3162 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3163 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003164 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3165 EXPECT_EQ(nullptr, pc_->current_local_description());
3166 EXPECT_EQ(nullptr, pc_->current_remote_description());
3167
3168 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003169 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003170 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003171 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3172 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3173 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3174 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003175 EXPECT_EQ(nullptr, pc_->current_local_description());
3176 EXPECT_EQ(nullptr, pc_->current_remote_description());
3177
3178 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003179 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003180 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003181 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3182 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003183 EXPECT_EQ(nullptr, pc_->pending_local_description());
3184 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003185 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3186 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003187
3188 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003189 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003190 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003191 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3192 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3193 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003194 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003195 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3196 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003197
3198 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003199 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003200 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003201 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3202 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3203 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3204 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3205 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3206 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003207
3208 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003209 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003210 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003211 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3212 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003213 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3214 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003215 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3216 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003217}
3218
zhihuang77985012017-02-07 15:45:16 -08003219// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3220// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003221// This version tests the StartRtcEventLog version that receives an object
3222// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003223TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003224 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3225 CreatePeerConnection();
3226 // The RtcEventLog will be reset when the PeerConnection is closed.
3227 pc_->Close();
3228
Niels Möllerdec9f742019-06-03 15:25:20 +02003229 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003230 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003231 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003232 pc_->StopRtcEventLog();
3233}
3234
deadbeef30952b42017-04-21 02:41:29 -07003235// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003236TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003237 CreatePeerConnection();
3238
3239 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003240 RTCOfferAnswerOptions options;
3241 options.offer_to_receive_audio = 1;
3242 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003243 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003244 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003245 cricket::SessionDescription* desc = offer->description();
3246 ASSERT_EQ(2u, desc->transport_infos().size());
3247 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3248 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3249
3250 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003251 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003252 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003253 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3254 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003255 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003256 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003257 desc = answer->description();
3258 ASSERT_EQ(2u, desc->transport_infos().size());
3259 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3260 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3261}
3262
deadbeef1dcb1642017-03-29 21:08:16 -07003263// Test that ICE renomination isn't offered if it's not enabled in the PC's
3264// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003265TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003266 PeerConnectionInterface::RTCConfiguration config;
3267 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003268 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003269 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003270
3271 std::unique_ptr<SessionDescriptionInterface> offer;
3272 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3273 cricket::SessionDescription* desc = offer->description();
3274 EXPECT_EQ(1u, desc->transport_infos().size());
3275 EXPECT_FALSE(
3276 desc->transport_infos()[0].description.GetIceParameters().renomination);
3277}
3278
3279// Test that the ICE renomination option is present in generated offers/answers
3280// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003281TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003282 PeerConnectionInterface::RTCConfiguration config;
3283 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003284 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003285 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003286
3287 std::unique_ptr<SessionDescriptionInterface> offer;
3288 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3289 cricket::SessionDescription* desc = offer->description();
3290 EXPECT_EQ(1u, desc->transport_infos().size());
3291 EXPECT_TRUE(
3292 desc->transport_infos()[0].description.GetIceParameters().renomination);
3293
3294 // Set the offer as a remote description, then create an answer and ensure it
3295 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003296 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003297 std::unique_ptr<SessionDescriptionInterface> answer;
3298 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3299 desc = answer->description();
3300 EXPECT_EQ(1u, desc->transport_infos().size());
3301 EXPECT_TRUE(
3302 desc->transport_infos()[0].description.GetIceParameters().renomination);
3303}
3304
3305// Test that if CreateOffer is called with the deprecated "offer to receive
3306// audio/video" constraints, they're processed and result in an offer with
3307// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003308TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003309 CreatePeerConnection();
3310
Niels Möllerf06f9232018-08-07 12:32:18 +02003311 RTCOfferAnswerOptions options;
3312 options.offer_to_receive_audio = 1;
3313 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003314 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003315 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003316
3317 cricket::SessionDescription* desc = offer->description();
3318 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3319 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3320 ASSERT_NE(nullptr, audio);
3321 ASSERT_NE(nullptr, video);
3322 EXPECT_FALSE(audio->rejected);
3323 EXPECT_FALSE(video->rejected);
3324}
3325
3326// Test that if CreateAnswer is called with the deprecated "offer to receive
3327// audio/video" constraints, they're processed and can be used to reject an
3328// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003329// Don't run under Unified Plan since this behavior is not supported.
3330TEST_F(PeerConnectionInterfaceTestPlanB,
3331 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003332 CreatePeerConnection();
3333
3334 // First, create an offer with audio/video and apply it as a remote
3335 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003336 RTCOfferAnswerOptions options;
3337 options.offer_to_receive_audio = 1;
3338 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003339 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003340 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003341 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003342
3343 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003344 options.offer_to_receive_audio = 0;
3345 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003346 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003347 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003348
3349 cricket::SessionDescription* desc = answer->description();
3350 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3351 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3352 ASSERT_NE(nullptr, audio);
3353 ASSERT_NE(nullptr, video);
3354 EXPECT_TRUE(audio->rejected);
3355 EXPECT_TRUE(video->rejected);
3356}
3357
deadbeef1dcb1642017-03-29 21:08:16 -07003358// Test that negotiation can succeed with a data channel only, and with the max
3359// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003360#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003361TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3362#else
3363TEST_P(PeerConnectionInterfaceTest,
3364 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003365#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003366 PeerConnectionInterface::RTCConfiguration config;
3367 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003368 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003369
3370 // First, create an offer with only a data channel and apply it as a remote
3371 // description.
3372 pc_->CreateDataChannel("test", nullptr);
3373 std::unique_ptr<SessionDescriptionInterface> offer;
3374 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003375 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003376
3377 // Create and set answer as well.
3378 std::unique_ptr<SessionDescriptionInterface> answer;
3379 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003380 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003381}
3382
Steve Anton36da6ff2018-02-16 16:04:20 -08003383TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003384 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003385 BitrateSettings bitrate;
3386 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003387 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3388}
3389
Steve Anton36da6ff2018-02-16 16:04:20 -08003390TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003391 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003392 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003393 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003394 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3395}
3396
Steve Anton36da6ff2018-02-16 16:04:20 -08003397TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003398 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003399 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003400 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003401 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003402 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3403}
3404
Steve Anton36da6ff2018-02-16 16:04:20 -08003405TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003406 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003407 BitrateSettings bitrate;
3408 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003409 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3410}
3411
Steve Anton36da6ff2018-02-16 16:04:20 -08003412TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003413 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003414 BitrateSettings bitrate;
3415 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003416 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003417 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3418}
3419
Steve Anton36da6ff2018-02-16 16:04:20 -08003420TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003421 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003422 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003423 bitrate.min_bitrate_bps = 10;
3424 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003425 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3426}
3427
Steve Anton36da6ff2018-02-16 16:04:20 -08003428TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003429 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003430 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003431 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003432 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3433}
3434
Niels Möller0c4f7be2018-05-07 14:01:37 +02003435// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003436// by Call's BitrateConstraints, which comes from the SDP or a default value.
3437// This test checks that a call to SetBitrate with a current bitrate that will
3438// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003439TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003440 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003441 BitrateSettings bitrate;
3442 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003443 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3444}
3445
zhihuang1c378ed2017-08-17 14:10:50 -07003446// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003447TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003448 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3449 RTCOfferAnswerOptions rtc_options;
3450
3451 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3452 // than kMaxOfferToReceiveMedia should be treated as invalid.
3453 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3454 CreatePeerConnection();
3455 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3456
3457 rtc_options.offer_to_receive_audio =
3458 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3459 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3460}
3461
Steve Anton36da6ff2018-02-16 16:04:20 -08003462TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003463 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3464 RTCOfferAnswerOptions rtc_options;
3465
3466 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3467 // than kMaxOfferToReceiveMedia should be treated as invalid.
3468 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3469 CreatePeerConnection();
3470 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3471
3472 rtc_options.offer_to_receive_video =
3473 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3474 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3475}
3476
3477// Test that the audio and video content will be added to an offer if both
3478// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003479TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003480 RTCOfferAnswerOptions rtc_options;
3481 rtc_options.offer_to_receive_audio = 1;
3482 rtc_options.offer_to_receive_video = 1;
3483
3484 std::unique_ptr<SessionDescriptionInterface> offer;
3485 CreatePeerConnection();
3486 offer = CreateOfferWithOptions(rtc_options);
3487 ASSERT_TRUE(offer);
3488 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3489 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3490}
3491
3492// Test that only audio content will be added to the offer if only
3493// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003494TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003495 RTCOfferAnswerOptions rtc_options;
3496 rtc_options.offer_to_receive_audio = 1;
3497 rtc_options.offer_to_receive_video = 0;
3498
3499 std::unique_ptr<SessionDescriptionInterface> offer;
3500 CreatePeerConnection();
3501 offer = CreateOfferWithOptions(rtc_options);
3502 ASSERT_TRUE(offer);
3503 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3504 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3505}
3506
3507// Test that only video content will be added if only |offer_to_receive_video|
3508// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003509TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003510 RTCOfferAnswerOptions rtc_options;
3511 rtc_options.offer_to_receive_audio = 0;
3512 rtc_options.offer_to_receive_video = 1;
3513
3514 std::unique_ptr<SessionDescriptionInterface> offer;
3515 CreatePeerConnection();
3516 offer = CreateOfferWithOptions(rtc_options);
3517 ASSERT_TRUE(offer);
3518 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3519 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3520}
3521
zhihuang1c378ed2017-08-17 14:10:50 -07003522// Test that no media content will be added to the offer if using default
3523// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003524TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003525 RTCOfferAnswerOptions rtc_options;
3526
3527 std::unique_ptr<SessionDescriptionInterface> offer;
3528 CreatePeerConnection();
3529 offer = CreateOfferWithOptions(rtc_options);
3530 ASSERT_TRUE(offer);
3531 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3532 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3533}
3534
3535// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3536// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003537TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3538 CreatePeerConnection();
3539
zhihuang1c378ed2017-08-17 14:10:50 -07003540 RTCOfferAnswerOptions rtc_options;
3541 rtc_options.ice_restart = false;
3542 rtc_options.offer_to_receive_audio = 1;
3543
3544 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003545 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003546 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3547 auto ufrag1 =
3548 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3549 auto pwd1 =
3550 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003551
3552 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3553 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003554 auto ufrag2 =
3555 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3556 auto pwd2 =
3557 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003558
3559 // |ice_restart| is true, the ufrag/pwd should change.
3560 rtc_options.ice_restart = true;
3561 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003562 auto ufrag3 =
3563 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3564 auto pwd3 =
3565 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003566
3567 EXPECT_EQ(ufrag1, ufrag2);
3568 EXPECT_EQ(pwd1, pwd2);
3569 EXPECT_NE(ufrag2, ufrag3);
3570 EXPECT_NE(pwd2, pwd3);
3571}
3572
3573// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3574// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003575TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003576 RTCOfferAnswerOptions rtc_options;
3577 rtc_options.offer_to_receive_audio = 1;
3578 rtc_options.offer_to_receive_video = 1;
3579
3580 std::unique_ptr<SessionDescriptionInterface> offer;
3581 CreatePeerConnection();
3582
3583 rtc_options.use_rtp_mux = true;
3584 offer = CreateOfferWithOptions(rtc_options);
3585 ASSERT_TRUE(offer);
3586 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3587 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3588 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3589
3590 rtc_options.use_rtp_mux = false;
3591 offer = CreateOfferWithOptions(rtc_options);
3592 ASSERT_TRUE(offer);
3593 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3594 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3595 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3596}
3597
zhihuang141aacb2017-08-29 13:23:53 -07003598// This test ensures OnRenegotiationNeeded is called when we add track with
3599// MediaStream -> AddTrack in the same way it is called when we add track with
3600// PeerConnection -> AddTrack.
3601// The test can be removed once addStream is rewritten in terms of addTrack
3602// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003603// Don't run under Unified Plan since the stream API is not available.
3604TEST_F(PeerConnectionInterfaceTestPlanB,
3605 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003606 CreatePeerConnectionWithoutDtls();
3607 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003608 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003609 pc_->AddStream(stream);
3610 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003611 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003612 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003613 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003614 stream->AddTrack(audio_track);
3615 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3616 observer_.renegotiation_needed_ = false;
3617
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003618 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003619 stream->AddTrack(video_track);
3620 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3621 observer_.renegotiation_needed_ = false;
3622
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003623 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003624 stream->RemoveTrack(audio_track);
3625 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3626 observer_.renegotiation_needed_ = false;
3627
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003628 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003629 stream->RemoveTrack(video_track);
3630 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3631 observer_.renegotiation_needed_ = false;
3632}
3633
Zhi Huangb2d355e2017-10-26 17:26:37 -07003634// Tests that an error is returned if a description is applied that has fewer
3635// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003636TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003637 MediaSectionCountEnforcedForSubsequentOffer) {
3638 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003639 AddAudioTrack("audio_label");
3640 AddVideoTrack("video_label");
3641
Zhi Huangb2d355e2017-10-26 17:26:37 -07003642 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003643 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003644 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3645
3646 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003647 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003648 offer->description()->contents().pop_back();
3649 offer->description()->contents().pop_back();
3650 ASSERT_TRUE(offer->description()->contents().empty());
3651 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3652
3653 std::unique_ptr<SessionDescriptionInterface> answer;
3654 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3655 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3656
3657 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003658 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003659 offer->description()->contents().pop_back();
3660 offer->description()->contents().pop_back();
3661 ASSERT_TRUE(offer->description()->contents().empty());
3662 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3663}
3664
Johannes Kron89f874e2018-11-12 10:25:48 +01003665TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3666 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003667 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003668 CreatePeerConnection(config);
3669 std::unique_ptr<SessionDescriptionInterface> offer;
3670 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003671 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3672 // Possible to set to false.
3673 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003674 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003675 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003676 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003677 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003678}
3679
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003680INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3681 PeerConnectionInterfaceTest,
3682 Values(SdpSemantics::kPlanB,
3683 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003684
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003685class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003686 protected:
3687 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003688 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003689 }
nisseeaabdf62017-05-05 02:23:02 -07003690 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003691 const RTCConfiguration& config) {
3692 rtc::scoped_refptr<PeerConnectionInterface> pc(
3693 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003694 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003695 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003696 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003697 }
3698
zhihuang9763d562016-08-05 11:14:50 -07003699 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003700 MockPeerConnectionObserver observer_;
3701};
3702
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003703// This sanity check validates the test infrastructure itself.
3704TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3705 PeerConnectionInterface::RTCConfiguration config;
3706 rtc::scoped_refptr<PeerConnectionInterface> pc(
3707 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3708 EXPECT_TRUE(pc.get());
3709 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3710 pc->Close(); // No abort -> ok.
3711 SUCCEED();
3712}
3713
nisse51542be2016-02-12 02:27:06 -08003714// This test verifies the default behaviour with no constraints and a
3715// default RTCConfiguration.
3716TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3717 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003718
Niels Möllerf06f9232018-08-07 12:32:18 +02003719 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003720
3721 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003722 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3723 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003724 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003725 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003726}
3727
Niels Möller1d7ecd22018-01-18 15:25:12 +01003728// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003729// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003730TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3731 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003732
Niels Möller71bdda02016-03-31 12:59:59 +02003733 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003734 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003735
Niels Möller1d7ecd22018-01-18 15:25:12 +01003736 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003737}
3738
Niels Möller6539f692018-01-18 08:58:50 +01003739// This test verifies that the experiment_cpu_load_estimator flag is
3740// propagated from RTCConfiguration to the PeerConnection.
3741TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3742 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003743
3744 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003745 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003746
3747 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3748}
3749
deadbeef293e9262017-01-11 12:28:30 -08003750// Tests a few random fields being different.
3751TEST(RTCConfigurationTest, ComparisonOperators) {
3752 PeerConnectionInterface::RTCConfiguration a;
3753 PeerConnectionInterface::RTCConfiguration b;
3754 EXPECT_EQ(a, b);
3755
3756 PeerConnectionInterface::RTCConfiguration c;
3757 c.servers.push_back(PeerConnectionInterface::IceServer());
3758 EXPECT_NE(a, c);
3759
3760 PeerConnectionInterface::RTCConfiguration d;
3761 d.type = PeerConnectionInterface::kRelay;
3762 EXPECT_NE(a, d);
3763
3764 PeerConnectionInterface::RTCConfiguration e;
3765 e.audio_jitter_buffer_max_packets = 5;
3766 EXPECT_NE(a, e);
3767
3768 PeerConnectionInterface::RTCConfiguration f;
3769 f.ice_connection_receiving_timeout = 1337;
3770 EXPECT_NE(a, f);
3771
3772 PeerConnectionInterface::RTCConfiguration g;
3773 g.disable_ipv6 = true;
3774 EXPECT_NE(a, g);
3775
3776 PeerConnectionInterface::RTCConfiguration h(
3777 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3778 EXPECT_NE(a, h);
3779}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01003780
3781} // namespace
3782} // namespace webrtc