blob: 3f73168d47fecc154978e83d8093814142e8c45e [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 test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001905TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001906 RTCConfiguration config;
1907 config.enable_rtp_data_channel = true;
1908 config.enable_dtls_srtp = false;
1909 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001910 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001912 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 pc_->CreateDataChannel("test2", NULL);
1914 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001915 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001917 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 new MockDataChannelObserver(data2));
1919
1920 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1921 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1922 std::string data_to_send1 = "testing testing";
1923 std::string data_to_send2 = "testing something else";
1924 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1925
1926 CreateOfferReceiveAnswer();
1927 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1928 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1929
1930 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1931 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1932 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1933 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1934
1935 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1936 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1937
1938 data1->Close();
1939 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1940 CreateOfferReceiveAnswer();
1941 EXPECT_FALSE(observer1->IsOpen());
1942 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1943 EXPECT_TRUE(observer2->IsOpen());
1944
1945 data_to_send2 = "testing something else again";
1946 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1947
1948 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1949}
1950
1951// This test verifies that sendnig binary data over RTP data channels should
1952// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001953TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001954 RTCConfiguration config;
1955 config.enable_rtp_data_channel = true;
1956 config.enable_dtls_srtp = false;
1957 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001958 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001960 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 pc_->CreateDataChannel("test2", NULL);
1962 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001963 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001965 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 new MockDataChannelObserver(data2));
1967
1968 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1969 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1970
1971 CreateOfferReceiveAnswer();
1972 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1973 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1974
1975 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1976 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1977
jbaucheec21bd2016-03-20 06:15:43 -07001978 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1980}
1981
1982// This test setup a RTP data channels in loop back and test that a channel is
1983// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001984TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001985 RTCConfiguration config;
1986 config.enable_rtp_data_channel = true;
1987 config.enable_dtls_srtp = false;
1988 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001989 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001991 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 new MockDataChannelObserver(data1));
1993
1994 CreateOfferReceiveAnswerWithoutSsrc();
1995
1996 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1997
1998 data1->Close();
1999 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2000 CreateOfferReceiveAnswerWithoutSsrc();
2001 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2002 EXPECT_FALSE(observer1->IsOpen());
2003}
2004
2005// This test that if a data channel is added in an answer a receive only channel
2006// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08002007TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002008 RTCConfiguration config;
2009 config.enable_rtp_data_channel = true;
2010 config.enable_dtls_srtp = false;
2011
2012 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013
2014 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002015 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 pc_->CreateDataChannel(offer_label, NULL);
2017
2018 CreateOfferAsLocalDescription();
2019
2020 // Replace the data channel label in the offer and apply it as an answer.
2021 std::string receive_label = "answer_channel";
2022 std::string sdp;
2023 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002024 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 CreateAnswerAsRemoteDescription(sdp);
2026
2027 // Verify that a new incoming data channel has been created and that
2028 // it is open but can't we written to.
2029 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2030 DataChannelInterface* received_channel = observer_.last_datachannel_;
2031 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2032 EXPECT_EQ(receive_label, received_channel->label());
2033 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2034
2035 // Verify that the channel we initially offered has been rejected.
2036 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2037
2038 // Do another offer / answer exchange and verify that the data channel is
2039 // opened.
2040 CreateOfferReceiveAnswer();
2041 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2042 kTimeout);
2043}
2044
2045// This test that no data channel is returned if a reliable channel is
2046// requested.
2047// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002048TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002049 RTCConfiguration rtc_config;
2050 rtc_config.enable_rtp_data_channel = true;
2051 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052
2053 std::string label = "test";
2054 webrtc::DataChannelInit config;
2055 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002056 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 pc_->CreateDataChannel(label, &config);
2058 EXPECT_TRUE(channel == NULL);
2059}
2060
deadbeefab9b2d12015-10-14 11:33:11 -07002061// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002062TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002063 RTCConfiguration config;
2064 config.enable_rtp_data_channel = true;
2065 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002066
2067 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002068 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002069 pc_->CreateDataChannel(label, nullptr);
2070 EXPECT_NE(channel, nullptr);
2071
zhihuang9763d562016-08-05 11:14:50 -07002072 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002073 pc_->CreateDataChannel(label, nullptr);
2074 EXPECT_EQ(dup_channel, nullptr);
2075}
2076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077// This tests that a SCTP data channel is returned using different
2078// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002079TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002080 RTCConfiguration rtc_config;
2081 rtc_config.enable_dtls_srtp = true;
2082 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083
2084 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002085 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 pc_->CreateDataChannel("1", &config);
2087 EXPECT_TRUE(channel != NULL);
2088 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002089 EXPECT_TRUE(observer_.renegotiation_needed_);
2090 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091
2092 config.ordered = false;
2093 channel = pc_->CreateDataChannel("2", &config);
2094 EXPECT_TRUE(channel != NULL);
2095 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002096 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097
2098 config.ordered = true;
2099 config.maxRetransmits = 0;
2100 channel = pc_->CreateDataChannel("3", &config);
2101 EXPECT_TRUE(channel != NULL);
2102 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002103 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002105 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 config.maxRetransmitTime = 0;
2107 channel = pc_->CreateDataChannel("4", &config);
2108 EXPECT_TRUE(channel != NULL);
2109 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002110 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111}
2112
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002113// For backwards compatibility, we want people who "unset" maxRetransmits
2114// and maxRetransmitTime by setting them to -1 to get what they want.
2115TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
2116 RTCConfiguration rtc_config;
2117 rtc_config.enable_dtls_srtp = true;
2118 CreatePeerConnection(rtc_config);
2119
2120 webrtc::DataChannelInit config;
2121 config.maxRetransmitTime = -1;
2122 config.maxRetransmits = -1;
2123 rtc::scoped_refptr<DataChannelInterface> channel =
2124 pc_->CreateDataChannel("1", &config);
2125 EXPECT_TRUE(channel != NULL);
2126}
2127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128// This tests that no data channel is returned if both maxRetransmits and
2129// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002130TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002132 RTCConfiguration rtc_config;
2133 rtc_config.enable_dtls_srtp = true;
2134 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135
2136 std::string label = "test";
2137 webrtc::DataChannelInit config;
2138 config.maxRetransmits = 0;
2139 config.maxRetransmitTime = 0;
2140
zhihuang9763d562016-08-05 11:14:50 -07002141 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 pc_->CreateDataChannel(label, &config);
2143 EXPECT_TRUE(channel == NULL);
2144}
2145
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146// The test verifies that creating a SCTP data channel with an id already in use
2147// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002148TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002150 RTCConfiguration rtc_config;
2151 rtc_config.enable_dtls_srtp = true;
2152 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153
2154 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002155 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002157 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002158 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002159 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 EXPECT_TRUE(channel != NULL);
2161 EXPECT_EQ(1, channel->id());
2162
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 channel = pc_->CreateDataChannel("x", &config);
2164 EXPECT_TRUE(channel == NULL);
2165
2166 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002167 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 channel = pc_->CreateDataChannel("max", &config);
2169 EXPECT_TRUE(channel != NULL);
2170 EXPECT_EQ(config.id, channel->id());
2171
2172 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002173 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 channel = pc_->CreateDataChannel("x", &config);
2175 EXPECT_TRUE(channel == NULL);
2176}
2177
deadbeefab9b2d12015-10-14 11:33:11 -07002178// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002179TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002180 RTCConfiguration rtc_config;
2181 rtc_config.enable_dtls_srtp = true;
2182 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002183
2184 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002185 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002186 pc_->CreateDataChannel(label, nullptr);
2187 EXPECT_NE(channel, nullptr);
2188
zhihuang9763d562016-08-05 11:14:50 -07002189 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002190 pc_->CreateDataChannel(label, nullptr);
2191 EXPECT_NE(dup_channel, nullptr);
2192}
2193
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002194// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2195// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002196TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002197 RTCConfiguration rtc_config;
2198 rtc_config.enable_rtp_data_channel = true;
2199 rtc_config.enable_dtls_srtp = false;
2200 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002201
zhihuang9763d562016-08-05 11:14:50 -07002202 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002203 pc_->CreateDataChannel("test1", NULL);
2204 EXPECT_TRUE(observer_.renegotiation_needed_);
2205 observer_.renegotiation_needed_ = false;
2206
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002207 CreateOfferReceiveAnswer();
2208
zhihuang9763d562016-08-05 11:14:50 -07002209 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002210 pc_->CreateDataChannel("test2", NULL);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002211 EXPECT_EQ(observer_.renegotiation_needed_,
2212 GetParam() == SdpSemantics::kPlanB);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002213}
2214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002216TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002217 RTCConfiguration rtc_config;
2218 rtc_config.enable_rtp_data_channel = true;
2219 rtc_config.enable_dtls_srtp = false;
2220 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221
zhihuang9763d562016-08-05 11:14:50 -07002222 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002224 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 pc_->CreateDataChannel("test2", NULL);
2226 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002227 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002229 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 new MockDataChannelObserver(data2));
2231
2232 CreateOfferReceiveAnswer();
2233 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2234 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2235
2236 ReleasePeerConnection();
2237 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2238 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2239}
2240
Zhi Huang644fde42018-04-02 19:16:26 -07002241// This tests that RTP data channels can be rejected in an answer.
2242TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002243 RTCConfiguration rtc_config;
2244 rtc_config.enable_rtp_data_channel = true;
2245 rtc_config.enable_dtls_srtp = false;
2246 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247
zhihuang9763d562016-08-05 11:14:50 -07002248 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 pc_->CreateDataChannel("offer_channel", NULL));
2250
2251 CreateOfferAsLocalDescription();
2252
2253 // Create an answer where the m-line for data channels are rejected.
2254 std::string sdp;
2255 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002256 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002257 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002258 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002260 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 data_info->rejected = true;
2262
Steve Antondb45ca82017-09-11 18:27:34 -07002263 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2265}
2266
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002267#ifdef WEBRTC_HAVE_SCTP
Zhi Huang644fde42018-04-02 19:16:26 -07002268// This tests that SCTP data channels can be rejected in an answer.
2269TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2270#else
2271TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2272#endif
2273{
Niels Möllerf06f9232018-08-07 12:32:18 +02002274 RTCConfiguration rtc_config;
2275 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002276
2277 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2278 pc_->CreateDataChannel("offer_channel", NULL));
2279
2280 CreateOfferAsLocalDescription();
2281
2282 // Create an answer where the m-line for data channels are rejected.
2283 std::string sdp;
2284 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2285 std::unique_ptr<SessionDescriptionInterface> answer(
2286 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2287 ASSERT_TRUE(answer);
2288 cricket::ContentInfo* data_info =
2289 cricket::GetFirstDataContent(answer->description());
2290 data_info->rejected = true;
2291
2292 DoSetRemoteDescription(std::move(answer));
2293 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2294}
2295
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296// Test that we can create a session description from an SDP string from
2297// FireFox, use it as a remote session description, generate an answer and use
2298// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002299TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002300 RTCConfiguration rtc_config;
2301 rtc_config.enable_dtls_srtp = true;
2302 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002303 AddAudioTrack("audio_label");
2304 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002305 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002306 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002307 webrtc::kFireFoxSdpOffer, nullptr));
2308 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 CreateAnswerAsLocalDescription();
2310 ASSERT_TRUE(pc_->local_description() != NULL);
2311 ASSERT_TRUE(pc_->remote_description() != NULL);
2312
2313 const cricket::ContentInfo* content =
2314 cricket::GetFirstAudioContent(pc_->local_description()->description());
2315 ASSERT_TRUE(content != NULL);
2316 EXPECT_FALSE(content->rejected);
2317
2318 content =
2319 cricket::GetFirstVideoContent(pc_->local_description()->description());
2320 ASSERT_TRUE(content != NULL);
2321 EXPECT_FALSE(content->rejected);
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01002322#ifdef WEBRTC_HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 content =
2324 cricket::GetFirstDataContent(pc_->local_description()->description());
2325 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002326 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002327#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328}
2329
zhihuangb19012e2017-09-19 13:47:59 -07002330// Test that fallback from DTLS to SDES is not supported.
2331// The fallback was previously supported but was removed to simplify the code
2332// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002333TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002334 RTCConfiguration rtc_config;
2335 rtc_config.enable_dtls_srtp = true;
2336 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002337 // Wait for fake certificate to be generated. Previously, this is what caused
2338 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002339 AddAudioTrack("audio_label");
2340 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002341 ASSERT_NE(nullptr, fake_certificate_generator_);
2342 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2343 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002344 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002345 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2346 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002347 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002348}
2349
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350// Test that we can create an audio only offer and receive an answer with a
2351// limited set of audio codecs and receive an updated offer with more audio
2352// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002353TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002354 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002355 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 CreateOfferAsLocalDescription();
2357
Steve Anton36da6ff2018-02-16 16:04:20 -08002358 const char* answer_sdp =
2359 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2360 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002361 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002362 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002363 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364
Steve Anton36da6ff2018-02-16 16:04:20 -08002365 const char* reoffer_sdp =
2366 (sdp_semantics_ == SdpSemantics::kPlanB
2367 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2368 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002369 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002370 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002371 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 CreateAnswerAsLocalDescription();
2373}
2374
deadbeefc80741f2015-10-22 13:14:45 -07002375// Test that if we're receiving (but not sending) a track, subsequent offers
2376// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002377TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002378 RTCConfiguration rtc_config;
2379 rtc_config.enable_dtls_srtp = true;
2380 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002381 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002382 CreateAnswerAsLocalDescription();
2383
2384 // At this point we should be receiving stream 1, but not sending anything.
2385 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002386 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002387 DoCreateOffer(&offer, nullptr);
2388
2389 const cricket::ContentInfo* video_content =
2390 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002391 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2392 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002393
2394 const cricket::ContentInfo* audio_content =
2395 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002396 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2397 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002398}
2399
2400// Test that if we're receiving (but not sending) a track, and the
2401// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2402// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002403TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002404 RTCConfiguration rtc_config;
2405 rtc_config.enable_dtls_srtp = true;
2406 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002407 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002408 CreateAnswerAsLocalDescription();
2409
2410 // At this point we should be receiving stream 1, but not sending anything.
2411 // A new offer would be recvonly, but we'll set the "no receive" constraints
2412 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002413 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002414 RTCOfferAnswerOptions options;
2415 options.offer_to_receive_audio = 0;
2416 options.offer_to_receive_video = 0;
2417 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002418
2419 const cricket::ContentInfo* video_content =
2420 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002421 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2422 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002423
2424 const cricket::ContentInfo* audio_content =
2425 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002426 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2427 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002428}
2429
deadbeef653b8e02015-11-11 12:55:10 -08002430// Test that we can use SetConfiguration to change the ICE servers of the
2431// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002432TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002433 CreatePeerConnection();
2434
Steve Anton36da6ff2018-02-16 16:04:20 -08002435 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002436 PeerConnectionInterface::IceServer server;
2437 server.uri = "stun:test_hostname";
2438 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002439 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002440
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002441 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2442 EXPECT_EQ("test_hostname",
2443 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002444}
2445
Steve Anton36da6ff2018-02-16 16:04:20 -08002446TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002447 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002448 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002449 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002450 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002451 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2452}
2453
Steve Anton36da6ff2018-02-16 16:04:20 -08002454TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002455 PeerConnectionInterface::RTCConfiguration config;
2456 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002457 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002458 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002459 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002460
2461 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002462 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002463 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2464 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002465}
2466
skvladd1f5fda2017-02-03 16:54:05 -08002467// Test that the ice check interval can be changed. This does not verify that
2468// the setting makes it all the way to P2PTransportChannel, as that would
2469// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002470TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002471 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002472 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002473 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002474 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002475 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002476 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002477 config = pc_->GetConfiguration();
2478 EXPECT_EQ(config.ice_check_min_interval, 100);
2479}
2480
2481TEST_P(PeerConnectionInterfaceTest,
2482 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2483 PeerConnectionInterface::RTCConfiguration config;
2484 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2485 CreatePeerConnection(config);
2486 config = pc_->GetConfiguration();
2487 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2488
2489 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002490 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002491 config = pc_->GetConfiguration();
2492 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002493}
2494
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002495// Test that when SetConfiguration changes both the pool size and other
2496// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002497TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002498 SetConfigurationCreatesPooledSessionCorrectly) {
2499 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002500 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002501 config.ice_candidate_pool_size = 1;
2502 PeerConnectionInterface::IceServer server;
2503 server.uri = kStunAddressOnly;
2504 config.servers.push_back(server);
2505 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002506 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002507
2508 const cricket::FakePortAllocatorSession* session =
2509 static_cast<const cricket::FakePortAllocatorSession*>(
2510 port_allocator_->GetPooledSession());
2511 ASSERT_NE(nullptr, session);
2512 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002513}
2514
deadbeef293e9262017-01-11 12:28:30 -08002515// Test that after SetLocalDescription, changing the pool size is not allowed,
2516// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002517TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002518 CantChangePoolSizeAfterSetLocalDescription) {
2519 CreatePeerConnection();
2520 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002521 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002522 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002523 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002524
2525 // Set remote offer; can still change pool size at this point.
2526 CreateOfferAsRemoteDescription();
2527 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002528 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002529
2530 // Set local answer; now it's too late.
2531 CreateAnswerAsLocalDescription();
2532 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002533 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002534 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2535}
2536
deadbeef42a42632017-03-10 15:18:00 -08002537// Test that after setting an answer, extra pooled sessions are discarded. The
2538// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002539TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002540 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2541 CreatePeerConnection();
2542
2543 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002544 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002545 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002546 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002547
2548 // Do offer/answer.
2549 CreateOfferAsRemoteDescription();
2550 CreateAnswerAsLocalDescription();
2551
2552 // Expect no pooled sessions to be left.
2553 const cricket::PortAllocatorSession* session =
2554 port_allocator_->GetPooledSession();
2555 EXPECT_EQ(nullptr, session);
2556}
2557
2558// After Close is called, pooled candidates should be discarded so as to not
2559// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002560TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002561 CreatePeerConnection();
2562
Steve Anton36da6ff2018-02-16 16:04:20 -08002563 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002564 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002565 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002566 pc_->Close();
2567
2568 // Expect no pooled sessions to be left.
2569 const cricket::PortAllocatorSession* session =
2570 port_allocator_->GetPooledSession();
2571 EXPECT_EQ(nullptr, session);
2572}
2573
deadbeef293e9262017-01-11 12:28:30 -08002574// Test that SetConfiguration returns an invalid modification error if
2575// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002576TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002577 SetConfigurationReturnsInvalidModificationError) {
2578 PeerConnectionInterface::RTCConfiguration config;
2579 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2580 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2581 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002582 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002583
Steve Anton36da6ff2018-02-16 16:04:20 -08002584 PeerConnectionInterface::RTCConfiguration modified_config =
2585 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002586 modified_config.bundle_policy =
2587 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002588 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002589 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2590
Steve Anton36da6ff2018-02-16 16:04:20 -08002591 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002592 modified_config.rtcp_mux_policy =
2593 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002594 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002595 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2596
Steve Anton36da6ff2018-02-16 16:04:20 -08002597 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002598 modified_config.continual_gathering_policy =
2599 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002600 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002601 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2602}
2603
2604// Test that SetConfiguration returns a range error if the candidate pool size
2605// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002606TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002607 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2608 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002609 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002610 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002611
2612 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002613 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002614 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2615
2616 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002617 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002618 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2619}
2620
2621// Test that SetConfiguration returns a syntax error if parsing an ICE server
2622// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002623TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002624 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2625 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002626 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002627 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002628
2629 PeerConnectionInterface::IceServer bad_server;
2630 bad_server.uri = "stunn:www.example.com";
2631 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002632 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002633 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2634}
2635
2636// Test that SetConfiguration returns an invalid parameter error if a TURN
2637// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002638TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002639 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2640 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002641 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002642 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002643
2644 PeerConnectionInterface::IceServer bad_server;
2645 bad_server.uri = "turn:www.example.com";
2646 // Missing password.
2647 bad_server.username = "foo";
2648 config.servers.push_back(bad_server);
2649 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002650 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2651 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002652}
2653
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002654// Test that PeerConnection::Close changes the states to closed and all remote
2655// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002656TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 // Initialize a PeerConnection and negotiate local and remote session
2658 // description.
2659 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002660
2661 // With Plan B, verify the stream count. The analog with Unified Plan is the
2662 // RtpTransceiver count.
2663 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2664 ASSERT_EQ(1u, pc_->local_streams()->count());
2665 ASSERT_EQ(1u, pc_->remote_streams()->count());
2666 } else {
2667 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2668 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002669
2670 pc_->Close();
2671
2672 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2673 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2674 pc_->ice_connection_state());
2675 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2676 pc_->ice_gathering_state());
2677
Steve Anton36da6ff2018-02-16 16:04:20 -08002678 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2679 EXPECT_EQ(1u, pc_->local_streams()->count());
2680 EXPECT_EQ(1u, pc_->remote_streams()->count());
2681 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002682 // Verify that the RtpTransceivers are still returned.
2683 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002684 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002685
Steve Anton36da6ff2018-02-16 16:04:20 -08002686 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002687 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002688 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2689 ASSERT_TRUE(audio_receiver);
2690 ASSERT_TRUE(video_receiver);
2691 // Track state may be updated asynchronously.
2692 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2693 audio_receiver->track()->state(), kTimeout);
2694 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2695 video_receiver->track()->state(), kTimeout);
2696 } else {
2697 ASSERT_FALSE(audio_receiver);
2698 ASSERT_FALSE(video_receiver);
2699 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700}
2701
2702// Test that PeerConnection methods fails gracefully after
2703// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002704// Don't run under Unified Plan since the stream API is not available.
2705TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002706 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002707 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002708 CreateOfferAsRemoteDescription();
2709 CreateAnswerAsLocalDescription();
2710
2711 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002712 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002713 pc_->local_streams()->at(0);
2714
2715 pc_->Close();
2716
2717 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002718 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2721
2722 EXPECT_TRUE(pc_->local_description() != NULL);
2723 EXPECT_TRUE(pc_->remote_description() != NULL);
2724
kwibergd1fe2812016-04-27 06:47:29 -07002725 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002726 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002727 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002728 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002729
2730 std::string sdp;
2731 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002732 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002733 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002734 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002735
2736 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002737 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002738 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002739 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002740}
2741
2742// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002743TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002744 InitiateCall();
2745 pc_->Close();
2746 DoGetStats(NULL);
2747}
deadbeefab9b2d12015-10-14 11:33:11 -07002748
2749// NOTE: The series of tests below come from what used to be
2750// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2751// setting a remote or local description has the expected effects.
2752
2753// This test verifies that the remote MediaStreams corresponding to a received
2754// SDP string is created. In this test the two separate MediaStreams are
2755// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002756TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002757 RTCConfiguration config;
2758 config.enable_dtls_srtp = true;
2759 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002760 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002761
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002762 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002763 EXPECT_TRUE(
2764 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2765 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2766 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2767
2768 // Create a session description based on another SDP with another
2769 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002770 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002771
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002772 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002773 EXPECT_TRUE(
2774 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2775}
2776
2777// This test verifies that when remote tracks are added/removed from SDP, the
2778// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002779// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2780// specific behavior.
2781TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002782 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002783 RTCConfiguration config;
2784 config.enable_dtls_srtp = true;
2785 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002786 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002787 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002788 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002789 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2790 reference_collection_));
2791
2792 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002793 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002794 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002795 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002796 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2797 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002798 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002799 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2800 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002801 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002802 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2803 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002804
2805 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002806 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002807 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002808 MockTrackObserver audio_track_observer(audio_track2);
2809 MockTrackObserver video_track_observer(video_track2);
2810
2811 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2812 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002813 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002814 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2815 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002816 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002817 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002818 audio_track2->state(), kTimeout);
2819 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2820 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002821}
2822
2823// This tests that remote tracks are ended if a local session description is set
2824// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002825TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002826 RTCConfiguration config;
2827 config.enable_dtls_srtp = true;
2828 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002829 // First create and set a remote offer, then reject its video content in our
2830 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002831 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2832 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2833 ASSERT_TRUE(audio_receiver);
2834 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2835 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002836
Steve Anton36da6ff2018-02-16 16:04:20 -08002837 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2838 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002839 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002840 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2841 video_receiver->track();
2842 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002843
kwibergd1fe2812016-04-27 06:47:29 -07002844 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002845 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002846 cricket::ContentInfo* video_info =
2847 local_answer->description()->GetContentByName("video");
2848 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002849 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002850 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2851 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002852
2853 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002854 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002855 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002856 video_info = local_offer->description()->GetContentByName("video");
2857 ASSERT_TRUE(video_info != nullptr);
2858 video_info->rejected = true;
2859 cricket::ContentInfo* audio_info =
2860 local_offer->description()->GetContentByName("audio");
2861 ASSERT_TRUE(audio_info != nullptr);
2862 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002863 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002864 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002865 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2866 kTimeout);
2867 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2868 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002869}
2870
2871// This tests that we won't crash if the remote track has been removed outside
2872// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002873// Don't run under Unified Plan since the stream API is not available.
2874TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002875 RTCConfiguration config;
2876 config.enable_dtls_srtp = true;
2877 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002878 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002879 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2880 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2881 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2882
kwibergd1fe2812016-04-27 06:47:29 -07002883 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002884 webrtc::CreateSessionDescription(SdpType::kAnswer,
2885 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002886 cricket::ContentInfo* video_info =
2887 local_answer->description()->GetContentByName("video");
2888 video_info->rejected = true;
2889 cricket::ContentInfo* audio_info =
2890 local_answer->description()->GetContentByName("audio");
2891 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002892 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002893
2894 // No crash is a pass.
2895}
2896
deadbeef5e97fb52015-10-15 12:49:08 -07002897// This tests that if a recvonly remote description is set, no remote streams
2898// will be created, even if the description contains SSRCs/MSIDs.
2899// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002900TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002901 RTCConfiguration config;
2902 config.enable_dtls_srtp = true;
2903 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002904
Steve Anton36da6ff2018-02-16 16:04:20 -08002905 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002906 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002907 CreateAndSetRemoteOffer(recvonly_offer);
2908
2909 EXPECT_EQ(0u, observer_.remote_streams()->count());
2910}
2911
deadbeefab9b2d12015-10-14 11:33:11 -07002912// This tests that a default MediaStream is created if a remote session
2913// description doesn't contain any streams and no MSID support.
2914// It also tests that the default stream is updated if a video m-line is added
2915// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002916// Don't run under Unified Plan since this behavior is Plan B specific.
2917TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002918 RTCConfiguration config;
2919 config.enable_dtls_srtp = true;
2920 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002921 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2922
2923 ASSERT_EQ(1u, observer_.remote_streams()->count());
2924 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2925
2926 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2927 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002928 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002929
2930 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2931 ASSERT_EQ(1u, observer_.remote_streams()->count());
2932 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2933 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002934 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2935 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002936 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2937 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002938 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2939 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002940}
2941
2942// This tests that a default MediaStream is created if a remote session
2943// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002944// Don't run under Unified Plan since this behavior is Plan B specific.
2945TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002946 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002947 RTCConfiguration config;
2948 config.enable_dtls_srtp = true;
2949 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002950 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2951
2952 ASSERT_EQ(1u, observer_.remote_streams()->count());
2953 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2954
2955 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2956 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002957 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002958}
2959
2960// This tests that it won't crash when PeerConnection tries to remove
2961// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002962// Don't run under Unified Plan since this behavior is Plan B specific.
2963TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002964 RTCConfiguration config;
2965 config.enable_dtls_srtp = true;
2966 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002967 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002968 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2969 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2970 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2971
2972 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2973
2974 // No crash is a pass.
2975}
2976
2977// This tests that a default MediaStream is created if the remote session
2978// description doesn't contain any streams and don't contain an indication if
2979// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002980// Don't run under Unified Plan since this behavior is Plan B specific.
2981TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002982 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002983 RTCConfiguration config;
2984 config.enable_dtls_srtp = true;
2985 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002986 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2987
2988 ASSERT_EQ(1u, observer_.remote_streams()->count());
2989 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2990 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2991 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2992}
2993
2994// This tests that a default MediaStream is not created if the remote session
2995// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002996// Don't run under Unified Plan since this behavior is Plan B specific.
2997TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002998 RTCConfiguration config;
2999 config.enable_dtls_srtp = true;
3000 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003001 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
3002 EXPECT_EQ(0u, observer_.remote_streams()->count());
3003}
3004
deadbeefbda7e0b2015-12-08 17:13:40 -08003005// This tests that when setting a new description, the old default tracks are
3006// not destroyed and recreated.
3007// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08003008// Don't run under Unified Plan since this behavior is Plan B specific.
3009TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003010 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003011 RTCConfiguration config;
3012 config.enable_dtls_srtp = true;
3013 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08003014 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3015
3016 ASSERT_EQ(1u, observer_.remote_streams()->count());
3017 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3018 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3019
3020 // Set the track to "disabled", then set a new description and ensure the
3021 // track is still disabled, which ensures it hasn't been recreated.
3022 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3023 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3024 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3025 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3026}
3027
deadbeefab9b2d12015-10-14 11:33:11 -07003028// This tests that a default MediaStream is not created if a remote session
3029// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003030// Don't run under Unified Plan since this behavior is Plan B specific.
3031TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003032 RTCConfiguration config;
3033 config.enable_dtls_srtp = true;
3034 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003035 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003036 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003037 EXPECT_TRUE(
3038 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3039
3040 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3041 EXPECT_EQ(0u, observer_.remote_streams()->count());
3042}
3043
Seth Hampson5897a6e2018-04-03 11:16:33 -07003044// This tests that a default MediaStream is created if a remote SDP comes from
3045// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3046TEST_F(PeerConnectionInterfaceTestPlanB,
3047 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003048 RTCConfiguration config;
3049 config.enable_dtls_srtp = true;
3050 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003051 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3052 // Add a=msid lines to simulate a Unified Plan endpoint that only
3053 // signals stream IDs with a=msid lines.
3054 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3055
3056 CreateAndSetRemoteOffer(sdp_string);
3057
3058 ASSERT_EQ(1u, observer_.remote_streams()->count());
3059 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3060 EXPECT_EQ("default", remote_stream->id());
3061 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3062}
3063
Seth Hampson5b4f0752018-04-02 16:31:36 -07003064// This tests that when a Plan B endpoint receives an SDP that signals no media
3065// stream IDs indicated by the special character "-" in the a=msid line, that
3066// a default stream ID will be used for the MediaStream ID. This can occur
3067// when a Unified Plan endpoint signals no media stream IDs, but signals both
3068// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3069TEST_F(PeerConnectionInterfaceTestPlanB,
3070 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003071 RTCConfiguration config;
3072 config.enable_dtls_srtp = true;
3073 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003074 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3075 // the sender's stream ID will be interpreted as no stream IDs.
3076 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3077 sdp_string.append("a=msid:- audiotrack0\n");
3078
3079 CreateAndSetRemoteOffer(sdp_string);
3080
3081 ASSERT_EQ(1u, observer_.remote_streams()->count());
3082 // Because SSRCs are signaled the track ID will be what was signaled in the
3083 // a=msid line.
3084 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3085 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3086 EXPECT_EQ("default", remote_stream->id());
3087 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003088
3089 // Previously a bug ocurred when setting the remote description a second time.
3090 // This is because we checked equality of the remote StreamParams stream ID
3091 // (empty), and the previously set stream ID for the remote sender
3092 // ("default"). This cause a track to be removed, then added, when really
3093 // nothing should occur because it is the same track.
3094 CreateAndSetRemoteOffer(sdp_string);
3095 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3096 EXPECT_EQ(1u, observer_.add_track_events_.size());
3097 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3098 remote_stream = observer_.remote_streams()->at(0);
3099 EXPECT_EQ("default", remote_stream->id());
3100 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003101}
3102
deadbeefab9b2d12015-10-14 11:33:11 -07003103// This tests that an RtpSender is created when the local description is set
3104// after adding a local stream.
3105// TODO(deadbeef): This test and the one below it need to be updated when
3106// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003107// Don't run under Unified Plan since this behavior is Plan B specific.
3108TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003109 RTCConfiguration config;
3110 config.enable_dtls_srtp = true;
3111 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003112
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003113 // Create an offer with 1 stream with 2 tracks of each type.
3114 rtc::scoped_refptr<StreamCollection> stream_collection =
3115 CreateStreamCollection(1, 2);
3116 pc_->AddStream(stream_collection->at(0));
3117 std::unique_ptr<SessionDescriptionInterface> offer;
3118 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003119 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003120
deadbeefab9b2d12015-10-14 11:33:11 -07003121 auto senders = pc_->GetSenders();
3122 EXPECT_EQ(4u, senders.size());
3123 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3124 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3125 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3126 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3127
3128 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003129 pc_->RemoveStream(stream_collection->at(0));
3130 stream_collection = CreateStreamCollection(1, 1);
3131 pc_->AddStream(stream_collection->at(0));
3132 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003133 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003134
deadbeefab9b2d12015-10-14 11:33:11 -07003135 senders = pc_->GetSenders();
3136 EXPECT_EQ(2u, senders.size());
3137 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3138 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3139 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3140 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3141}
3142
3143// This tests that an RtpSender is created when the local description is set
3144// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003145// Don't run under Unified Plan since this behavior is Plan B specific.
3146TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003147 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003148 RTCConfiguration config;
3149 config.enable_dtls_srtp = true;
3150 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003151
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003152 rtc::scoped_refptr<StreamCollection> stream_collection =
3153 CreateStreamCollection(1, 2);
3154 // Add a stream to create the offer, but remove it afterwards.
3155 pc_->AddStream(stream_collection->at(0));
3156 std::unique_ptr<SessionDescriptionInterface> offer;
3157 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3158 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003159
Steve Antondb45ca82017-09-11 18:27:34 -07003160 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003161 auto senders = pc_->GetSenders();
3162 EXPECT_EQ(0u, senders.size());
3163
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003164 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003165 senders = pc_->GetSenders();
3166 EXPECT_EQ(4u, senders.size());
3167 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3168 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3169 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3170 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3171}
3172
3173// This tests that the expected behavior occurs if the SSRC on a local track is
3174// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003175TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003176 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003177 RTCConfiguration config;
3178 config.enable_dtls_srtp = true;
3179 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003180
Steve Anton36da6ff2018-02-16 16:04:20 -08003181 AddAudioTrack(kAudioTracks[0]);
3182 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003183 std::unique_ptr<SessionDescriptionInterface> offer;
3184 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3185 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003186 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3187 webrtc::CreateSessionDescription(
3188 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02003189 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07003190 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003191
deadbeefab9b2d12015-10-14 11:33:11 -07003192 auto senders = pc_->GetSenders();
3193 EXPECT_EQ(2u, senders.size());
3194 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3195 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3196
3197 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003198 cricket::MediaContentDescription* desc =
3199 cricket::GetFirstAudioContentDescription(modified_offer->description());
3200 ASSERT_TRUE(desc != NULL);
3201 for (StreamParams& stream : desc->mutable_streams()) {
3202 for (unsigned int& ssrc : stream.ssrcs) {
3203 ++ssrc;
3204 }
3205 }
deadbeefab9b2d12015-10-14 11:33:11 -07003206
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003207 desc =
3208 cricket::GetFirstVideoContentDescription(modified_offer->description());
3209 ASSERT_TRUE(desc != NULL);
3210 for (StreamParams& stream : desc->mutable_streams()) {
3211 for (unsigned int& ssrc : stream.ssrcs) {
3212 ++ssrc;
3213 }
3214 }
3215
Steve Antondb45ca82017-09-11 18:27:34 -07003216 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003217 senders = pc_->GetSenders();
3218 EXPECT_EQ(2u, senders.size());
3219 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3220 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3221 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3222 // changed.
3223}
3224
3225// This tests that the expected behavior occurs if a new session description is
3226// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003227// Don't run under Unified Plan since the stream API is not available.
3228TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003229 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003230 RTCConfiguration config;
3231 config.enable_dtls_srtp = true;
3232 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003233
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003234 rtc::scoped_refptr<StreamCollection> stream_collection =
3235 CreateStreamCollection(2, 1);
3236 pc_->AddStream(stream_collection->at(0));
3237 std::unique_ptr<SessionDescriptionInterface> offer;
3238 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003239 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003240
deadbeefab9b2d12015-10-14 11:33:11 -07003241 auto senders = pc_->GetSenders();
3242 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003243 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3244 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003245
3246 // Add a new MediaStream but with the same tracks as in the first stream.
3247 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3248 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003249 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3250 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003251 pc_->AddStream(stream_1);
3252
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003253 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003254 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003255
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003256 auto new_senders = pc_->GetSenders();
3257 // Should be the same senders as before, but with updated stream id.
3258 // Note that this behavior is subject to change in the future.
3259 // We may decide the PC should ignore existing tracks in AddStream.
3260 EXPECT_EQ(senders, new_senders);
3261 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3262 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003263}
3264
zhihuang81c3a032016-11-17 12:06:24 -08003265// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003266TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003267 RTCConfiguration config;
3268 config.enable_dtls_srtp = true;
3269 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003270 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3271 EXPECT_EQ(observer_.num_added_tracks_, 1);
3272 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3273
3274 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003275 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003276 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003277 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3278}
3279
deadbeefd1a38b52016-12-10 13:15:33 -08003280// Test that when SetConfiguration is called and the configuration is
3281// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003282TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003283 PeerConnectionInterface::RTCConfiguration config;
3284 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003285 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003286 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003287 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3288 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003289
3290 // Do initial offer/answer so there's something to restart.
3291 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003292 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003293
3294 // Grab the ufrags.
3295 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3296
3297 // Change ICE policy, which should trigger an ICE restart on the next offer.
3298 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003299 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003300 CreateOfferAsLocalDescription();
3301
3302 // Grab the new ufrags.
3303 std::vector<std::string> subsequent_ufrags =
3304 GetUfrags(pc_->local_description());
3305
3306 // Sanity check.
3307 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3308 // Check that each ufrag is different.
3309 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3310 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3311 }
3312}
3313
3314// Test that when SetConfiguration is called and the configuration *isn't*
3315// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003316TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003317 PeerConnectionInterface::RTCConfiguration config;
3318 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003319 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003320 config = pc_->GetConfiguration();
3321 AddAudioTrack(kAudioTracks[0]);
3322 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003323
3324 // Do initial offer/answer so there's something to restart.
3325 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003326 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003327
3328 // Grab the ufrags.
3329 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3330
3331 // Call SetConfiguration with a config identical to what the PC was
3332 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003333 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003334 CreateOfferAsLocalDescription();
3335
3336 // Grab the new ufrags.
3337 std::vector<std::string> subsequent_ufrags =
3338 GetUfrags(pc_->local_description());
3339
3340 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3341}
3342
3343// Test for a weird corner case scenario:
3344// 1. Audio/video session established.
3345// 2. SetConfiguration changes ICE config; ICE restart needed.
3346// 3. ICE restart initiated by remote peer, but only for one m= section.
3347// 4. Next createOffer should initiate an ICE restart, but only for the other
3348// m= section; it would be pointless to do an ICE restart for the m= section
3349// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003350TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003351 PeerConnectionInterface::RTCConfiguration config;
3352 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003353 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003354 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003355 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3356 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003357
3358 // Do initial offer/answer so there's something to restart.
3359 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003360 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003361
3362 // Change ICE policy, which should set the "needs-ice-restart" flag.
3363 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003364 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003365
3366 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003367 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003368 webrtc::CreateSessionDescription(SdpType::kOffer,
3369 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003370 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003371 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3372 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003373 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003374 CreateAnswerAsLocalDescription();
3375
3376 // Grab the ufrags.
3377 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003378 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003379
3380 // Create offer and grab the new ufrags.
3381 CreateOfferAsLocalDescription();
3382 std::vector<std::string> subsequent_ufrags =
3383 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003384 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003385
3386 // Ensure that only the ufrag for the second m= section changed.
3387 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3388 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3389}
3390
deadbeeffe4a8a42016-12-20 17:56:17 -08003391// Tests that the methods to return current/pending descriptions work as
3392// expected at different points in the offer/answer exchange. This test does
3393// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003394TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003395 // This disables DTLS so we can apply an answer to ourselves.
3396 CreatePeerConnection();
3397
3398 // Create initial local offer and get SDP (which will also be used as
3399 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003400 std::unique_ptr<SessionDescriptionInterface> local_offer;
3401 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003402 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003403 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003404
3405 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003406 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3407 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3408 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003409 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3410 EXPECT_EQ(nullptr, pc_->current_local_description());
3411 EXPECT_EQ(nullptr, pc_->current_remote_description());
3412
3413 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003414 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003415 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003416 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3417 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3418 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3419 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003420 EXPECT_EQ(nullptr, pc_->current_local_description());
3421 EXPECT_EQ(nullptr, pc_->current_remote_description());
3422
3423 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003424 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003425 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003426 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3427 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003428 EXPECT_EQ(nullptr, pc_->pending_local_description());
3429 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003430 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3431 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003432
3433 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003434 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003435 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003436 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3437 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3438 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003439 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003440 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3441 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003442
3443 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003444 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003445 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003446 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3447 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3448 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3449 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3450 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3451 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003452
3453 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003454 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003455 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003456 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3457 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003458 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3459 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003460 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3461 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003462}
3463
zhihuang77985012017-02-07 15:45:16 -08003464// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3465// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003466// This version tests the StartRtcEventLog version that receives an object
3467// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003468TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003469 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3470 CreatePeerConnection();
3471 // The RtcEventLog will be reset when the PeerConnection is closed.
3472 pc_->Close();
3473
Niels Möllerdec9f742019-06-03 15:25:20 +02003474 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003475 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003476 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003477 pc_->StopRtcEventLog();
3478}
3479
deadbeef30952b42017-04-21 02:41:29 -07003480// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003481TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003482 CreatePeerConnection();
3483
3484 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003485 RTCOfferAnswerOptions options;
3486 options.offer_to_receive_audio = 1;
3487 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003488 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003489 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003490 cricket::SessionDescription* desc = offer->description();
3491 ASSERT_EQ(2u, desc->transport_infos().size());
3492 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3493 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3494
3495 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003496 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003497 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003498 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3499 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003500 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003501 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003502 desc = answer->description();
3503 ASSERT_EQ(2u, desc->transport_infos().size());
3504 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3505 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3506}
3507
deadbeef1dcb1642017-03-29 21:08:16 -07003508// Test that ICE renomination isn't offered if it's not enabled in the PC's
3509// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003510TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003511 PeerConnectionInterface::RTCConfiguration config;
3512 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003513 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003514 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003515
3516 std::unique_ptr<SessionDescriptionInterface> offer;
3517 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3518 cricket::SessionDescription* desc = offer->description();
3519 EXPECT_EQ(1u, desc->transport_infos().size());
3520 EXPECT_FALSE(
3521 desc->transport_infos()[0].description.GetIceParameters().renomination);
3522}
3523
3524// Test that the ICE renomination option is present in generated offers/answers
3525// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003526TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003527 PeerConnectionInterface::RTCConfiguration config;
3528 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003529 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003530 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003531
3532 std::unique_ptr<SessionDescriptionInterface> offer;
3533 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3534 cricket::SessionDescription* desc = offer->description();
3535 EXPECT_EQ(1u, desc->transport_infos().size());
3536 EXPECT_TRUE(
3537 desc->transport_infos()[0].description.GetIceParameters().renomination);
3538
3539 // Set the offer as a remote description, then create an answer and ensure it
3540 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003541 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003542 std::unique_ptr<SessionDescriptionInterface> answer;
3543 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3544 desc = answer->description();
3545 EXPECT_EQ(1u, desc->transport_infos().size());
3546 EXPECT_TRUE(
3547 desc->transport_infos()[0].description.GetIceParameters().renomination);
3548}
3549
3550// Test that if CreateOffer is called with the deprecated "offer to receive
3551// audio/video" constraints, they're processed and result in an offer with
3552// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003553TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003554 CreatePeerConnection();
3555
Niels Möllerf06f9232018-08-07 12:32:18 +02003556 RTCOfferAnswerOptions options;
3557 options.offer_to_receive_audio = 1;
3558 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003559 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003560 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003561
3562 cricket::SessionDescription* desc = offer->description();
3563 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3564 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3565 ASSERT_NE(nullptr, audio);
3566 ASSERT_NE(nullptr, video);
3567 EXPECT_FALSE(audio->rejected);
3568 EXPECT_FALSE(video->rejected);
3569}
3570
3571// Test that if CreateAnswer is called with the deprecated "offer to receive
3572// audio/video" constraints, they're processed and can be used to reject an
3573// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003574// Don't run under Unified Plan since this behavior is not supported.
3575TEST_F(PeerConnectionInterfaceTestPlanB,
3576 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003577 CreatePeerConnection();
3578
3579 // First, create an offer with audio/video and apply it as a remote
3580 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003581 RTCOfferAnswerOptions options;
3582 options.offer_to_receive_audio = 1;
3583 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003584 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003585 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003586 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003587
3588 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003589 options.offer_to_receive_audio = 0;
3590 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003591 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003592 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003593
3594 cricket::SessionDescription* desc = answer->description();
3595 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3596 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3597 ASSERT_NE(nullptr, audio);
3598 ASSERT_NE(nullptr, video);
3599 EXPECT_TRUE(audio->rejected);
3600 EXPECT_TRUE(video->rejected);
3601}
3602
deadbeef1dcb1642017-03-29 21:08:16 -07003603// Test that negotiation can succeed with a data channel only, and with the max
3604// bundle policy. Previously there was a bug that prevented this.
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003605#ifdef WEBRTC_HAVE_SCTP
Steve Anton36da6ff2018-02-16 16:04:20 -08003606TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3607#else
3608TEST_P(PeerConnectionInterfaceTest,
3609 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
Mirko Bonadei5eb43b42021-01-18 13:24:40 +01003610#endif // WEBRTC_HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003611 PeerConnectionInterface::RTCConfiguration config;
3612 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003613 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003614
3615 // First, create an offer with only a data channel and apply it as a remote
3616 // description.
3617 pc_->CreateDataChannel("test", nullptr);
3618 std::unique_ptr<SessionDescriptionInterface> offer;
3619 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003620 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003621
3622 // Create and set answer as well.
3623 std::unique_ptr<SessionDescriptionInterface> answer;
3624 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003625 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003626}
3627
Steve Anton36da6ff2018-02-16 16:04:20 -08003628TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003629 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003630 BitrateSettings bitrate;
3631 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003632 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3633}
3634
Steve Anton36da6ff2018-02-16 16:04:20 -08003635TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003636 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003637 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003638 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003639 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3640}
3641
Steve Anton36da6ff2018-02-16 16:04:20 -08003642TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003643 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003644 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003645 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003646 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003647 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3648}
3649
Steve Anton36da6ff2018-02-16 16:04:20 -08003650TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003651 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003652 BitrateSettings bitrate;
3653 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003654 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3655}
3656
Steve Anton36da6ff2018-02-16 16:04:20 -08003657TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003658 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003659 BitrateSettings bitrate;
3660 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003661 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003662 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3663}
3664
Steve Anton36da6ff2018-02-16 16:04:20 -08003665TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003666 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003667 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003668 bitrate.min_bitrate_bps = 10;
3669 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003670 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3671}
3672
Steve Anton36da6ff2018-02-16 16:04:20 -08003673TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003674 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003675 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003676 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003677 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3678}
3679
Niels Möller0c4f7be2018-05-07 14:01:37 +02003680// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003681// by Call's BitrateConstraints, which comes from the SDP or a default value.
3682// This test checks that a call to SetBitrate with a current bitrate that will
3683// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003684TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003685 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003686 BitrateSettings bitrate;
3687 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003688 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3689}
3690
zhihuang1c378ed2017-08-17 14:10:50 -07003691// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003692TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003693 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3694 RTCOfferAnswerOptions rtc_options;
3695
3696 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3697 // than kMaxOfferToReceiveMedia should be treated as invalid.
3698 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3699 CreatePeerConnection();
3700 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3701
3702 rtc_options.offer_to_receive_audio =
3703 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3704 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3705}
3706
Steve Anton36da6ff2018-02-16 16:04:20 -08003707TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003708 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3709 RTCOfferAnswerOptions rtc_options;
3710
3711 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3712 // than kMaxOfferToReceiveMedia should be treated as invalid.
3713 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3714 CreatePeerConnection();
3715 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3716
3717 rtc_options.offer_to_receive_video =
3718 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3719 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3720}
3721
3722// Test that the audio and video content will be added to an offer if both
3723// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003724TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003725 RTCOfferAnswerOptions rtc_options;
3726 rtc_options.offer_to_receive_audio = 1;
3727 rtc_options.offer_to_receive_video = 1;
3728
3729 std::unique_ptr<SessionDescriptionInterface> offer;
3730 CreatePeerConnection();
3731 offer = CreateOfferWithOptions(rtc_options);
3732 ASSERT_TRUE(offer);
3733 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3734 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3735}
3736
3737// Test that only audio content will be added to the offer if only
3738// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003739TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003740 RTCOfferAnswerOptions rtc_options;
3741 rtc_options.offer_to_receive_audio = 1;
3742 rtc_options.offer_to_receive_video = 0;
3743
3744 std::unique_ptr<SessionDescriptionInterface> offer;
3745 CreatePeerConnection();
3746 offer = CreateOfferWithOptions(rtc_options);
3747 ASSERT_TRUE(offer);
3748 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3749 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3750}
3751
3752// Test that only video content will be added if only |offer_to_receive_video|
3753// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003754TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003755 RTCOfferAnswerOptions rtc_options;
3756 rtc_options.offer_to_receive_audio = 0;
3757 rtc_options.offer_to_receive_video = 1;
3758
3759 std::unique_ptr<SessionDescriptionInterface> offer;
3760 CreatePeerConnection();
3761 offer = CreateOfferWithOptions(rtc_options);
3762 ASSERT_TRUE(offer);
3763 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3764 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3765}
3766
zhihuang1c378ed2017-08-17 14:10:50 -07003767// Test that no media content will be added to the offer if using default
3768// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003769TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003770 RTCOfferAnswerOptions rtc_options;
3771
3772 std::unique_ptr<SessionDescriptionInterface> offer;
3773 CreatePeerConnection();
3774 offer = CreateOfferWithOptions(rtc_options);
3775 ASSERT_TRUE(offer);
3776 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3777 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3778}
3779
3780// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3781// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003782TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3783 CreatePeerConnection();
3784
zhihuang1c378ed2017-08-17 14:10:50 -07003785 RTCOfferAnswerOptions rtc_options;
3786 rtc_options.ice_restart = false;
3787 rtc_options.offer_to_receive_audio = 1;
3788
3789 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003790 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003791 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3792 auto ufrag1 =
3793 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3794 auto pwd1 =
3795 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003796
3797 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3798 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003799 auto ufrag2 =
3800 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3801 auto pwd2 =
3802 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003803
3804 // |ice_restart| is true, the ufrag/pwd should change.
3805 rtc_options.ice_restart = true;
3806 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003807 auto ufrag3 =
3808 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3809 auto pwd3 =
3810 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003811
3812 EXPECT_EQ(ufrag1, ufrag2);
3813 EXPECT_EQ(pwd1, pwd2);
3814 EXPECT_NE(ufrag2, ufrag3);
3815 EXPECT_NE(pwd2, pwd3);
3816}
3817
3818// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3819// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003820TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003821 RTCOfferAnswerOptions rtc_options;
3822 rtc_options.offer_to_receive_audio = 1;
3823 rtc_options.offer_to_receive_video = 1;
3824
3825 std::unique_ptr<SessionDescriptionInterface> offer;
3826 CreatePeerConnection();
3827
3828 rtc_options.use_rtp_mux = true;
3829 offer = CreateOfferWithOptions(rtc_options);
3830 ASSERT_TRUE(offer);
3831 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3832 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3833 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3834
3835 rtc_options.use_rtp_mux = false;
3836 offer = CreateOfferWithOptions(rtc_options);
3837 ASSERT_TRUE(offer);
3838 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3839 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3840 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3841}
3842
zhihuang141aacb2017-08-29 13:23:53 -07003843// This test ensures OnRenegotiationNeeded is called when we add track with
3844// MediaStream -> AddTrack in the same way it is called when we add track with
3845// PeerConnection -> AddTrack.
3846// The test can be removed once addStream is rewritten in terms of addTrack
3847// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003848// Don't run under Unified Plan since the stream API is not available.
3849TEST_F(PeerConnectionInterfaceTestPlanB,
3850 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003851 CreatePeerConnectionWithoutDtls();
3852 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003853 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003854 pc_->AddStream(stream);
3855 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003856 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003857 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003858 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003859 stream->AddTrack(audio_track);
3860 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3861 observer_.renegotiation_needed_ = false;
3862
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003863 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003864 stream->AddTrack(video_track);
3865 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3866 observer_.renegotiation_needed_ = false;
3867
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003868 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003869 stream->RemoveTrack(audio_track);
3870 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3871 observer_.renegotiation_needed_ = false;
3872
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003873 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003874 stream->RemoveTrack(video_track);
3875 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3876 observer_.renegotiation_needed_ = false;
3877}
3878
Zhi Huangb2d355e2017-10-26 17:26:37 -07003879// Tests that an error is returned if a description is applied that has fewer
3880// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003881TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003882 MediaSectionCountEnforcedForSubsequentOffer) {
3883 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003884 AddAudioTrack("audio_label");
3885 AddVideoTrack("video_label");
3886
Zhi Huangb2d355e2017-10-26 17:26:37 -07003887 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003888 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003889 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3890
3891 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003892 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003893 offer->description()->contents().pop_back();
3894 offer->description()->contents().pop_back();
3895 ASSERT_TRUE(offer->description()->contents().empty());
3896 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3897
3898 std::unique_ptr<SessionDescriptionInterface> answer;
3899 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3900 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3901
3902 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003903 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003904 offer->description()->contents().pop_back();
3905 offer->description()->contents().pop_back();
3906 ASSERT_TRUE(offer->description()->contents().empty());
3907 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3908}
3909
Johannes Kron89f874e2018-11-12 10:25:48 +01003910TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3911 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003912 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003913 CreatePeerConnection(config);
3914 std::unique_ptr<SessionDescriptionInterface> offer;
3915 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003916 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3917 // Possible to set to false.
3918 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003919 CreatePeerConnection(config);
Artem Titov4f3a2eb2021-01-26 09:58:21 +01003920 offer = nullptr;
Johannes Kron89f874e2018-11-12 10:25:48 +01003921 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003922 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003923}
3924
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003925INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3926 PeerConnectionInterfaceTest,
3927 Values(SdpSemantics::kPlanB,
3928 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003929
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003930class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003931 protected:
3932 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003933 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003934 }
nisseeaabdf62017-05-05 02:23:02 -07003935 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003936 const RTCConfiguration& config) {
3937 rtc::scoped_refptr<PeerConnectionInterface> pc(
3938 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003939 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003940 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003941 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003942 }
3943
zhihuang9763d562016-08-05 11:14:50 -07003944 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003945 MockPeerConnectionObserver observer_;
3946};
3947
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003948// This sanity check validates the test infrastructure itself.
3949TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3950 PeerConnectionInterface::RTCConfiguration config;
3951 rtc::scoped_refptr<PeerConnectionInterface> pc(
3952 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3953 EXPECT_TRUE(pc.get());
3954 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3955 pc->Close(); // No abort -> ok.
3956 SUCCEED();
3957}
3958
nisse51542be2016-02-12 02:27:06 -08003959// This test verifies the default behaviour with no constraints and a
3960// default RTCConfiguration.
3961TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3962 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003963
Niels Möllerf06f9232018-08-07 12:32:18 +02003964 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003965
3966 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003967 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3968 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003969 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003970 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003971}
3972
Niels Möller1d7ecd22018-01-18 15:25:12 +01003973// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003974// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003975TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3976 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003977
Niels Möller71bdda02016-03-31 12:59:59 +02003978 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003979 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003980
Niels Möller1d7ecd22018-01-18 15:25:12 +01003981 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003982}
3983
Niels Möller6539f692018-01-18 08:58:50 +01003984// This test verifies that the experiment_cpu_load_estimator flag is
3985// propagated from RTCConfiguration to the PeerConnection.
3986TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3987 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003988
3989 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003990 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003991
3992 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3993}
3994
deadbeef293e9262017-01-11 12:28:30 -08003995// Tests a few random fields being different.
3996TEST(RTCConfigurationTest, ComparisonOperators) {
3997 PeerConnectionInterface::RTCConfiguration a;
3998 PeerConnectionInterface::RTCConfiguration b;
3999 EXPECT_EQ(a, b);
4000
4001 PeerConnectionInterface::RTCConfiguration c;
4002 c.servers.push_back(PeerConnectionInterface::IceServer());
4003 EXPECT_NE(a, c);
4004
4005 PeerConnectionInterface::RTCConfiguration d;
4006 d.type = PeerConnectionInterface::kRelay;
4007 EXPECT_NE(a, d);
4008
4009 PeerConnectionInterface::RTCConfiguration e;
4010 e.audio_jitter_buffer_max_packets = 5;
4011 EXPECT_NE(a, e);
4012
4013 PeerConnectionInterface::RTCConfiguration f;
4014 f.ice_connection_receiving_timeout = 1337;
4015 EXPECT_NE(a, f);
4016
4017 PeerConnectionInterface::RTCConfiguration g;
4018 g.disable_ipv6 = true;
4019 EXPECT_NE(a, g);
4020
4021 PeerConnectionInterface::RTCConfiguration h(
4022 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4023 EXPECT_NE(a, h);
4024}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004025
4026} // namespace
4027} // namespace webrtc