blob: b7be44ddb59b3b57e56a86c7916f19396fb7220c [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 virtual void SetUp() {
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
704 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200705 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
707
deadbeef293e9262017-01-11 12:28:30 -0800708 // DTLS does not work in a loopback call, so is disabled for most of the
709 // tests in this file.
710 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200711 RTCConfiguration config;
712 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800713
Niels Möllerf06f9232018-08-07 12:32:18 +0200714 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
716
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 void CreatePeerConnectionWithIceTransportsType(
718 PeerConnectionInterface::IceTransportsType type) {
719 PeerConnectionInterface::RTCConfiguration config;
720 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200721 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 }
723
724 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100725 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800727 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700729 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100730 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 server.password = password;
732 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200733 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
Niels Möllerf06f9232018-08-07 12:32:18 +0200736 void CreatePeerConnection(const RTCConfiguration& config) {
kwibergd1fe2812016-04-27 06:47:29 -0700737 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800738 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
739 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000740
deadbeef1dcb1642017-03-29 21:08:16 -0700741 // Create certificate generator unless DTLS constraint is explicitly set to
742 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200743 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200744
745 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800746 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
747 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000748 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800749 RTCConfiguration modified_config = config;
750 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200751 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200752 modified_config, std::move(port_allocator), std::move(cert_generator),
753 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 ASSERT_TRUE(pc_.get() != NULL);
755 observer_.SetPeerConnectionInterface(pc_.get());
756 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
757 }
758
deadbeef0a6c4ca2015-10-06 11:38:28 -0700759 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700761 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700762 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800764 config.sdp_semantics = sdp_semantics_;
765 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200766 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700768 }
769
Steve Anton038834f2017-07-14 15:59:59 -0700770 void CreatePeerConnectionExpectFail(
771 PeerConnectionInterface::RTCConfiguration config) {
772 PeerConnectionInterface::IceServer server;
773 server.uri = kTurnIceServerUri;
774 server.password = kTurnPassword;
775 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800776 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700777 rtc::scoped_refptr<PeerConnectionInterface> pc =
778 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
779 EXPECT_EQ(nullptr, pc);
780 }
781
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100783 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
785 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
786 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800788 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789
deadbeef0a6c4ca2015-10-06 11:38:28 -0700790 CreatePeerConnectionExpectFail(kStunInvalidPort);
791 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
792 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
Niels Möllerdb4def92019-03-18 16:53:59 +0100794 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
795 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
797 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800799 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800801 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800803 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 }
805
806 void ReleasePeerConnection() {
807 pc_ = NULL;
808 observer_.SetPeerConnectionInterface(NULL);
809 }
810
Steve Anton36da6ff2018-02-16 16:04:20 -0800811 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
812 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100813 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800814 }
815
816 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800817 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800818 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800819 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800820 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
Steve Anton36da6ff2018-02-16 16:04:20 -0800823 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700824 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800826 stream->AddTrack(CreateVideoTrack(label + "v0"));
827 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800828 }
829
830 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
831 const std::string& label) {
832 return pc_factory_->CreateAudioTrack(label, nullptr);
833 }
834
835 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800836 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800837 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800838 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800839 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800840 }
841
842 void AddAudioStream(const std::string& label) {
843 rtc::scoped_refptr<MediaStreamInterface> stream(
844 pc_factory_->CreateLocalMediaStream(label));
845 stream->AddTrack(CreateAudioTrack(label + "a0"));
846 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
848
Seth Hampson845e8782018-03-02 11:34:10 -0800849 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 const std::string& audio_track_label,
851 const std::string& video_track_label) {
852 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700853 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800854 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800855 stream->AddTrack(CreateAudioTrack(audio_track_label));
856 stream->AddTrack(CreateVideoTrack(video_track_label));
857 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859
Steve Anton36da6ff2018-02-16 16:04:20 -0800860 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
861 cricket::MediaType media_type) {
862 for (auto receiver : pc_->GetReceivers()) {
863 if (receiver->media_type() == media_type) {
864 return receiver;
865 }
866 }
867 return nullptr;
868 }
869
kwibergd1fe2812016-04-27 06:47:29 -0700870 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200871 const RTCOfferAnswerOptions* options,
872 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200873 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
874 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200876 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200878 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700881 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 return observer->result();
883 }
884
kwibergd1fe2812016-04-27 06:47:29 -0700885 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200886 const RTCOfferAnswerOptions* options) {
887 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889
kwibergd1fe2812016-04-27 06:47:29 -0700890 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200891 const RTCOfferAnswerOptions* options) {
892 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 }
894
Steve Antondb45ca82017-09-11 18:27:34 -0700895 bool DoSetSessionDescription(
896 std::unique_ptr<SessionDescriptionInterface> desc,
897 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200898 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
899 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700901 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700903 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
zhihuang29ff8442016-07-27 11:07:25 -0700905 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
906 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
907 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 return observer->result();
909 }
910
Steve Antondb45ca82017-09-11 18:27:34 -0700911 bool DoSetLocalDescription(
912 std::unique_ptr<SessionDescriptionInterface> desc) {
913 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
Steve Antondb45ca82017-09-11 18:27:34 -0700916 bool DoSetRemoteDescription(
917 std::unique_ptr<SessionDescriptionInterface> desc) {
918 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
921 // Calls PeerConnection::GetStats and check the return value.
922 // It does not verify the values in the StatReports since a RTCP packet might
923 // be required.
924 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000925 rtc::scoped_refptr<MockStatsObserver> observer(
926 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200927 if (!pc_->GetStats(observer, track,
928 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 return false;
930 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
931 return observer->called();
932 }
933
Harald Alvestrand89061872018-01-02 14:08:34 +0100934 // Call the standards-compliant GetStats function.
935 bool DoGetRTCStats() {
936 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
937 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
938 pc_->GetStats(callback);
939 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
940 return callback->called();
941 }
942
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800944 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800946 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800947 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800948 } else {
949 // Unified Plan does not support AddStream, so just add an audio and video
950 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800951 AddAudioTrack(kAudioTracks[0], {kStreamId1});
952 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800953 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 CreateOfferReceiveAnswer();
955 }
956
957 // Verify that RTP Header extensions has been negotiated for audio and video.
958 void VerifyRemoteRtpHeaderExtensions() {
959 const cricket::MediaContentDescription* desc =
960 cricket::GetFirstAudioContentDescription(
961 pc_->remote_description()->description());
962 ASSERT_TRUE(desc != NULL);
963 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
964
965 desc = cricket::GetFirstVideoContentDescription(
966 pc_->remote_description()->description());
967 ASSERT_TRUE(desc != NULL);
968 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
969 }
970
971 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700972 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700973 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 std::string sdp;
975 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700976 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800977 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700978 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
980 }
981
deadbeefab9b2d12015-10-14 11:33:11 -0700982 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700983 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800984 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700985 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700986 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
987 }
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700990 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700991 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992
993 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
994 // audio codec change, even if the parameter has nothing to do with
995 // receiving. Not all parameters are serialized to SDP.
996 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
997 // the SessionDescription, it is necessary to do that here to in order to
998 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
999 // https://code.google.com/p/webrtc/issues/detail?id=1356
1000 std::string sdp;
1001 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001002 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001003 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001004 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1006 }
1007
1008 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001009 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001010 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011
1012 std::string sdp;
1013 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001014 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001015 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001016 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1018 }
1019
1020 void CreateOfferReceiveAnswer() {
1021 CreateOfferAsLocalDescription();
1022 std::string sdp;
1023 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1024 CreateAnswerAsRemoteDescription(sdp);
1025 }
1026
1027 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001028 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001029 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1031 // audio codec change, even if the parameter has nothing to do with
1032 // receiving. Not all parameters are serialized to SDP.
1033 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1034 // the SessionDescription, it is necessary to do that here to in order to
1035 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1036 // https://code.google.com/p/webrtc/issues/detail?id=1356
1037 std::string sdp;
1038 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001039 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001040 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
Steve Antondb45ca82017-09-11 18:27:34 -07001042 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001044 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001045 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 }
1047
deadbeefab9b2d12015-10-14 11:33:11 -07001048 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001049 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001050 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001051 ASSERT_TRUE(answer);
1052 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1054 }
1055
deadbeefab9b2d12015-10-14 11:33:11 -07001056 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001057 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001058 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001059 ASSERT_TRUE(pr_answer);
1060 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001062 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001063 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001064 ASSERT_TRUE(answer);
1065 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1067 }
1068
Seth Hampson845e8782018-03-02 11:34:10 -08001069 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001070 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001071 // called with the given stream id and expected number of tracks.
1072 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001073 int expected_num_tracks) {
1074 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001075 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001076 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001077 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 }
1079
1080 // Creates an offer and applies it as a local session description.
1081 // Creates an answer with the same SDP an the offer but removes all lines
1082 // that start with a:ssrc"
1083 void CreateOfferReceiveAnswerWithoutSsrc() {
1084 CreateOfferAsLocalDescription();
1085 std::string sdp;
1086 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1087 SetSsrcToZero(&sdp);
1088 CreateAnswerAsRemoteDescription(sdp);
1089 }
1090
deadbeefab9b2d12015-10-14 11:33:11 -07001091 // This function creates a MediaStream with label kStreams[0] and
1092 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1093 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001094 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001095 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001096 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001097 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1098 size_t number_of_video_tracks) {
1099 EXPECT_LE(number_of_audio_tracks, 2u);
1100 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001101
1102 reference_collection_ = StreamCollection::Create();
1103 std::string sdp_ms1 = std::string(kSdpStringInit);
1104
Seth Hampson845e8782018-03-02 11:34:10 -08001105 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001106
1107 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001108 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001109 reference_collection_->AddStream(stream);
1110
1111 if (number_of_audio_tracks > 0) {
1112 sdp_ms1 += std::string(kSdpStringAudio);
1113 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1114 AddAudioTrack(kAudioTracks[0], stream);
1115 }
1116 if (number_of_audio_tracks > 1) {
1117 sdp_ms1 += kSdpStringMs1Audio1;
1118 AddAudioTrack(kAudioTracks[1], stream);
1119 }
1120
1121 if (number_of_video_tracks > 0) {
1122 sdp_ms1 += std::string(kSdpStringVideo);
1123 sdp_ms1 += std::string(kSdpStringMs1Video0);
1124 AddVideoTrack(kVideoTracks[0], stream);
1125 }
1126 if (number_of_video_tracks > 1) {
1127 sdp_ms1 += kSdpStringMs1Video1;
1128 AddVideoTrack(kVideoTracks[1], stream);
1129 }
1130
kwibergd1fe2812016-04-27 06:47:29 -07001131 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001132 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001133 }
1134
1135 void AddAudioTrack(const std::string& track_id,
1136 MediaStreamInterface* stream) {
1137 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1138 webrtc::AudioTrack::Create(track_id, nullptr));
1139 ASSERT_TRUE(stream->AddTrack(audio_track));
1140 }
1141
1142 void AddVideoTrack(const std::string& track_id,
1143 MediaStreamInterface* stream) {
1144 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001145 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001146 webrtc::FakeVideoTrackSource::Create(),
1147 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001148 ASSERT_TRUE(stream->AddTrack(video_track));
1149 }
1150
Steve Anton36da6ff2018-02-16 16:04:20 -08001151 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001152 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001153 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001154 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001155 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1156 return offer;
1157 }
1158
Steve Anton36da6ff2018-02-16 16:04:20 -08001159 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1160 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001161 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001162 std::unique_ptr<SessionDescriptionInterface> offer;
1163 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1164 return offer;
1165 }
1166
1167 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1168 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1169 std::unique_ptr<SessionDescriptionInterface> answer;
1170 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1171 return answer;
1172 }
1173
kwibergfd8be342016-05-14 19:44:11 -07001174 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001176 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1179 return answer;
1180 }
1181
1182 const std::string& GetFirstAudioStreamCname(
1183 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001185 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 return audio_desc->streams()[0].cname;
1187 }
1188
zhihuang1c378ed2017-08-17 14:10:50 -07001189 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1190 const RTCOfferAnswerOptions& offer_answer_options) {
1191 RTC_DCHECK(pc_);
1192 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1193 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1194 pc_->CreateOffer(observer, offer_answer_options);
1195 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1196 return observer->MoveDescription();
1197 }
1198
1199 void CreateOfferWithOptionsAsRemoteDescription(
1200 std::unique_ptr<SessionDescriptionInterface>* desc,
1201 const RTCOfferAnswerOptions& offer_answer_options) {
1202 *desc = CreateOfferWithOptions(offer_answer_options);
1203 ASSERT_TRUE(desc != nullptr);
1204 std::string sdp;
1205 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001206 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001207 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001208 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001209 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1210 }
1211
1212 void CreateOfferWithOptionsAsLocalDescription(
1213 std::unique_ptr<SessionDescriptionInterface>* desc,
1214 const RTCOfferAnswerOptions& offer_answer_options) {
1215 *desc = CreateOfferWithOptions(offer_answer_options);
1216 ASSERT_TRUE(desc != nullptr);
1217 std::string sdp;
1218 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001219 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001220 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001221
Steve Antondb45ca82017-09-11 18:27:34 -07001222 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001223 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1224 }
1225
1226 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001227 RTC_DCHECK(content);
1228 RTC_DCHECK(content->media_description());
1229 for (const cricket::AudioCodec& codec :
1230 content->media_description()->as_audio()->codecs()) {
1231 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001232 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001233 }
zhihuang1c378ed2017-08-17 14:10:50 -07001234 }
1235 return false;
1236 }
1237
Steve Anton36da6ff2018-02-16 16:04:20 -08001238 const char* GetSdpStringWithStream1() const {
1239 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1240 return kSdpStringWithStream1PlanB;
1241 } else {
1242 return kSdpStringWithStream1UnifiedPlan;
1243 }
1244 }
1245
1246 const char* GetSdpStringWithStream1And2() const {
1247 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1248 return kSdpStringWithStream1And2PlanB;
1249 } else {
1250 return kSdpStringWithStream1And2UnifiedPlan;
1251 }
1252 }
1253
deadbeef9a6f4d42017-05-15 19:43:33 -07001254 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1255 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001256 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001257 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001258 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001259 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1260 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1261 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001263 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001264 const SdpSemantics sdp_semantics_;
1265};
1266
1267class PeerConnectionInterfaceTest
1268 : public PeerConnectionInterfaceBaseTest,
1269 public ::testing::WithParamInterface<SdpSemantics> {
1270 protected:
1271 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1272};
1273
1274class PeerConnectionInterfaceTestPlanB
1275 : public PeerConnectionInterfaceBaseTest {
1276 protected:
1277 PeerConnectionInterfaceTestPlanB()
1278 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279};
1280
zhihuang8f65cdf2016-05-06 18:40:30 -07001281// Generate different CNAMEs when PeerConnections are created.
1282// The CNAMEs are expected to be generated randomly. It is possible
1283// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001284TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001285 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001286 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001288 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001289 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1290 GetFirstAudioStreamCname(offer2.get()));
1291}
1292
Steve Anton36da6ff2018-02-16 16:04:20 -08001293TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001295 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001296 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001297 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001298 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1299 GetFirstAudioStreamCname(answer2.get()));
1300}
1301
Steve Anton36da6ff2018-02-16 16:04:20 -08001302TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 CreatePeerConnectionWithDifferentConfigurations) {
1304 CreatePeerConnectionWithDifferentConfigurations();
1305}
1306
Steve Anton36da6ff2018-02-16 16:04:20 -08001307TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001308 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1309 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1310 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1311 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1312 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1313 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1314 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1315 port_allocator_->candidate_filter());
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1317 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1318}
1319
1320// Test that when a PeerConnection is created with a nonzero candidate pool
1321// size, the pooled PortAllocatorSession is created with all the attributes
1322// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001323TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001324 PeerConnectionInterface::RTCConfiguration config;
1325 PeerConnectionInterface::IceServer server;
1326 server.uri = kStunAddressOnly;
1327 config.servers.push_back(server);
1328 config.type = PeerConnectionInterface::kRelay;
1329 config.disable_ipv6 = true;
1330 config.tcp_candidate_policy =
1331 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001332 config.candidate_network_policy =
1333 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001335 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336
1337 const cricket::FakePortAllocatorSession* session =
1338 static_cast<const cricket::FakePortAllocatorSession*>(
1339 port_allocator_->GetPooledSession());
1340 ASSERT_NE(nullptr, session);
1341 EXPECT_EQ(1UL, session->stun_servers().size());
1342 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1343 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001344 EXPECT_LT(0U,
1345 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346}
1347
deadbeefd21eab32017-07-26 16:50:11 -07001348// Test that network-related RTCConfiguration members are applied to the
1349// PortAllocator when CreatePeerConnection is called. Specifically:
1350// - disable_ipv6_on_wifi
1351// - max_ipv6_networks
1352// - tcp_candidate_policy
1353// - candidate_network_policy
1354// - prune_turn_ports
1355//
1356// Note that the candidate filter (RTCConfiguration::type) is already tested
1357// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001358TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001359 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1360 // Create fake port allocator.
1361 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1362 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1363 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1364
1365 // Create RTCConfiguration with some network-related fields relevant to
1366 // PortAllocator populated.
1367 PeerConnectionInterface::RTCConfiguration config;
1368 config.disable_ipv6_on_wifi = true;
1369 config.max_ipv6_networks = 10;
1370 config.tcp_candidate_policy =
1371 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1372 config.candidate_network_policy =
1373 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1374 config.prune_turn_ports = true;
1375
1376 // Create the PC factory and PC with the above config.
1377 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1378 webrtc::CreatePeerConnectionFactory(
1379 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001380 rtc::Thread::Current(), fake_audio_capture_module_,
1381 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001382 webrtc::CreateBuiltinAudioDecoderFactory(),
1383 webrtc::CreateBuiltinVideoEncoderFactory(),
1384 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1385 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001386 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001387 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1388 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001389 EXPECT_TRUE(pc.get());
1390 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001391
1392 // Now validate that the config fields set above were applied to the
1393 // PortAllocator, as flags or otherwise.
1394 EXPECT_FALSE(raw_port_allocator->flags() &
1395 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1396 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1397 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1398 EXPECT_TRUE(raw_port_allocator->flags() &
1399 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001400 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1401 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001402}
1403
deadbeef46c73892016-11-16 19:42:04 -08001404// Check that GetConfiguration returns the configuration the PeerConnection was
1405// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001406TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001407 PeerConnectionInterface::RTCConfiguration config;
1408 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001409 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001410
1411 PeerConnectionInterface::RTCConfiguration returned_config =
1412 pc_->GetConfiguration();
1413 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1414}
1415
1416// Check that GetConfiguration returns the last configuration passed into
1417// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001418TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001419 PeerConnectionInterface::RTCConfiguration starting_config;
1420 starting_config.bundle_policy =
1421 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1422 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001423
Steve Anton36da6ff2018-02-16 16:04:20 -08001424 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001425 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001426 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001427
1428 PeerConnectionInterface::RTCConfiguration returned_config =
1429 pc_->GetConfiguration();
1430 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1431}
1432
Steve Antonc79268f2018-04-24 09:54:10 -07001433TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1434 CreatePeerConnection();
1435
1436 pc_->Close();
1437
1438 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001439 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001440}
1441
Steve Anton36da6ff2018-02-16 16:04:20 -08001442TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001443 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001444 AddVideoStream(kStreamId1);
1445 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 ASSERT_EQ(2u, pc_->local_streams()->count());
1447
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001448 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001449 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001450 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001451 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001452 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001453 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001455 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001456 EXPECT_EQ(3u, pc_->local_streams()->count());
1457
1458 // Remove the third stream.
1459 pc_->RemoveStream(pc_->local_streams()->at(2));
1460 EXPECT_EQ(2u, pc_->local_streams()->count());
1461
1462 // Remove the second stream.
1463 pc_->RemoveStream(pc_->local_streams()->at(1));
1464 EXPECT_EQ(1u, pc_->local_streams()->count());
1465
1466 // Remove the first stream.
1467 pc_->RemoveStream(pc_->local_streams()->at(0));
1468 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469}
1470
deadbeefab9b2d12015-10-14 11:33:11 -07001471// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001472// Don't run under Unified Plan since the stream API is not available.
1473TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001474 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001475 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001476 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001477 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001478
deadbeefab9b2d12015-10-14 11:33:11 -07001479 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001480 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001481 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001482
deadbeefab9b2d12015-10-14 11:33:11 -07001483 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001484 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001485 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001486
1487 // Add another stream and ensure the offer includes both the old and new
1488 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001489 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001490 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001491
Steve Antonb1c1de12017-12-21 15:14:30 -08001492 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001493 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1494 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001495
Steve Antonb1c1de12017-12-21 15:14:30 -08001496 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001497 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1498 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001499}
1500
Steve Anton36da6ff2018-02-16 16:04:20 -08001501// Don't run under Unified Plan since the stream API is not available.
1502TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001503 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001504 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 ASSERT_EQ(1u, pc_->local_streams()->count());
1506 pc_->RemoveStream(pc_->local_streams()->at(0));
1507 EXPECT_EQ(0u, pc_->local_streams()->count());
1508}
1509
deadbeefe1f9d832016-01-14 15:35:42 -08001510// Test for AddTrack and RemoveTrack methods.
1511// Tests that the created offer includes tracks we added,
1512// and that the RtpSenders are created correctly.
1513// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001514// Only tested with Plan B since Unified Plan is covered in more detail by tests
1515// in peerconnection_jsep_unittests.cc
1516TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001517 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001518 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001519 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001520 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001521 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001522 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1523 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001524 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001525 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001526 EXPECT_EQ("audio_track", audio_sender->id());
1527 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001528 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001529 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001530 EXPECT_EQ("video_track", video_sender->id());
1531 EXPECT_EQ(video_track, video_sender->track());
1532
1533 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001534 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001535 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001536
1537 const cricket::ContentInfo* audio_content =
1538 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001539 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001540 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001541
1542 const cricket::ContentInfo* video_content =
1543 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001544 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001545 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001546
Steve Antondb45ca82017-09-11 18:27:34 -07001547 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001548
1549 // Now try removing the tracks.
1550 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1551 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1552
1553 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001554 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001555
1556 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001557 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001558 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001559
1560 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001561 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001562 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001563
Steve Antondb45ca82017-09-11 18:27:34 -07001564 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001565
1566 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1567 // should return false.
1568 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1569 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1570}
1571
1572// Test creating senders without a stream specified,
1573// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001574TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001575 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001576 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001577 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001578 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001579 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001580 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001581 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001582 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001583 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001584 EXPECT_EQ("audio_track", audio_sender->id());
1585 EXPECT_EQ(audio_track, audio_sender->track());
1586 EXPECT_EQ("video_track", video_sender->id());
1587 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001588 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1589 // If the ID is truly a random GUID, it should be infinitely unlikely they
1590 // will be the same.
1591 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1592 } else {
1593 // We allows creating tracks without stream ids under Unified Plan
1594 // semantics.
1595 EXPECT_EQ(0u, video_sender->stream_ids().size());
1596 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1597 }
deadbeefe1f9d832016-01-14 15:35:42 -08001598}
1599
Harald Alvestrand89061872018-01-02 14:08:34 +01001600// Test that we can call GetStats() after AddTrack but before connecting
1601// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001602TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001603 CreatePeerConnectionWithoutDtls();
1604 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001605 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001606 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001607 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001608 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1609 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001610 EXPECT_TRUE(DoGetStats(nullptr));
1611}
1612
Steve Anton36da6ff2018-02-16 16:04:20 -08001613TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001614 CreatePeerConnectionWithoutDtls();
1615 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001616 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001617 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001618 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001619 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001620 ASSERT_TRUE(audio_sender.ok());
1621 auto* audio_sender_proxy =
1622 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1623 audio_sender.value().get());
1624 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1625
Harald Alvestrandc72af932018-01-11 17:18:19 +01001626 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001627 ASSERT_TRUE(video_sender.ok());
1628 auto* video_sender_proxy =
1629 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1630 video_sender.value().get());
1631 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001632}
1633
Steve Anton36da6ff2018-02-16 16:04:20 -08001634// Don't run under Unified Plan since the stream API is not available.
1635TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001636 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001637 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001638 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001639 ASSERT_EQ(1u, senders.size());
1640 auto* sender_proxy =
1641 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1642 senders[0].get());
1643 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001644}
1645
Steve Anton36da6ff2018-02-16 16:04:20 -08001646TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001648 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 VerifyRemoteRtpHeaderExtensions();
1650}
1651
Steve Anton36da6ff2018-02-16 16:04:20 -08001652TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001653 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001654 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 CreateOfferAsLocalDescription();
1656 std::string offer;
1657 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1658 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001659 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660}
1661
Steve Anton36da6ff2018-02-16 16:04:20 -08001662TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001663 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001664 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665
1666 CreateOfferAsRemoteDescription();
1667 CreateAnswerAsLocalDescription();
1668
Seth Hampson845e8782018-03-02 11:34:10 -08001669 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670}
1671
Steve Anton36da6ff2018-02-16 16:04:20 -08001672TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001673 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001674 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675
1676 CreateOfferAsRemoteDescription();
1677 CreatePrAnswerAsLocalDescription();
1678 CreateAnswerAsLocalDescription();
1679
Seth Hampson845e8782018-03-02 11:34:10 -08001680 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681}
1682
Steve Anton36da6ff2018-02-16 16:04:20 -08001683// Don't run under Unified Plan since the stream API is not available.
1684TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 InitiateCall();
1686 ASSERT_EQ(1u, pc_->remote_streams()->count());
1687 pc_->RemoveStream(pc_->local_streams()->at(0));
1688 CreateOfferReceiveAnswer();
1689 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001690 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 CreateOfferReceiveAnswer();
1692}
1693
1694// Tests that after negotiating an audio only call, the respondent can perform a
1695// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001696TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001697 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001698 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 CreateOfferAsRemoteDescription();
1700 CreateAnswerAsLocalDescription();
1701
1702 ASSERT_EQ(1u, pc_->remote_streams()->count());
1703 pc_->RemoveStream(pc_->local_streams()->at(0));
1704 CreateOfferReceiveAnswer();
1705 EXPECT_EQ(0u, pc_->remote_streams()->count());
1706}
1707
1708// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001709TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001710 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711
Steve Antonf1c6db12017-10-13 11:13:35 -07001712 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001714 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001715 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001716 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001717 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718
1719 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001720 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001721 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001722 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723
Steve Antonf1c6db12017-10-13 11:13:35 -07001724 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001725 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726
Steve Antonf1c6db12017-10-13 11:13:35 -07001727 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728}
1729
deadbeefab9b2d12015-10-14 11:33:11 -07001730// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001732TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001733 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001735 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001736 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001737 EXPECT_TRUE(offer);
1738 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739
1740 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001741 AddAudioTrack("track_label", {kStreamId1});
1742 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743
1744 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001745 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746
1747 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001748 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001749 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750}
1751
1752// Test that we will get different SSRCs for each tracks in the offer and answer
1753// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001754TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001755 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001757 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1758 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759
1760 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001761 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001762 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 int audio_ssrc = 0;
1764 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001765 EXPECT_TRUE(
1766 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1767 EXPECT_TRUE(
1768 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 EXPECT_NE(audio_ssrc, video_ssrc);
1770
1771 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001772 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001773 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001774 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 audio_ssrc = 0;
1776 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001777 EXPECT_TRUE(
1778 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1779 EXPECT_TRUE(
1780 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 EXPECT_NE(audio_ssrc, video_ssrc);
1782}
1783
deadbeefeb459812015-12-15 19:24:43 -08001784// Test that it's possible to call AddTrack on a MediaStream after adding
1785// the stream to a PeerConnection.
1786// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001787// Don't run under Unified Plan since the stream API is not available.
1788TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001789 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001790 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001791 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001792 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1793
1794 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001795 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001796 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001797 stream->AddTrack(video_track.get());
1798
kwibergd1fe2812016-04-27 06:47:29 -07001799 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001800 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001801
1802 const cricket::MediaContentDescription* video_desc =
1803 cricket::GetFirstVideoContentDescription(offer->description());
1804 EXPECT_TRUE(video_desc != nullptr);
1805}
1806
1807// Test that it's possible to call RemoveTrack on a MediaStream after adding
1808// the stream to a PeerConnection.
1809// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001810// Don't run under Unified Plan since the stream API is not available.
1811TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001812 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001813 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001814 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001815 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1816
1817 // Remove the video track.
1818 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1819
kwibergd1fe2812016-04-27 06:47:29 -07001820 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001821 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001822
1823 const cricket::MediaContentDescription* video_desc =
1824 cricket::GetFirstVideoContentDescription(offer->description());
1825 EXPECT_TRUE(video_desc == nullptr);
1826}
1827
deadbeefbd7d8f72015-12-18 16:58:44 -08001828// Test creating a sender with a stream ID, and ensure the ID is populated
1829// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001830// Don't run under Unified Plan since the stream API is not available.
1831TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001832 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001833 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001834
kwibergd1fe2812016-04-27 06:47:29 -07001835 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001836 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001837
1838 const cricket::MediaContentDescription* video_desc =
1839 cricket::GetFirstVideoContentDescription(offer->description());
1840 ASSERT_TRUE(video_desc != nullptr);
1841 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001842 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001843}
1844
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001846TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001848 ASSERT_LT(0u, pc_->GetSenders().size());
1849 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001850 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001851 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 EXPECT_TRUE(DoGetStats(remote_audio));
1853
1854 // Remove the stream. Since we are sending to our selves the local
1855 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001856 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 // Do a re-negotiation.
1858 CreateOfferReceiveAnswer();
1859
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 // Test that we still can get statistics for the old track. Even if it is not
1861 // sent any longer.
1862 EXPECT_TRUE(DoGetStats(remote_audio));
1863}
1864
1865// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001866TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001868 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1869 ASSERT_TRUE(video_receiver);
1870 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871}
1872
1873// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001874TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_factory_->CreateAudioTrack("unknown track", NULL));
1878 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1879}
1880
Steve Anton36da6ff2018-02-16 16:04:20 -08001881TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001882 CreatePeerConnectionWithoutDtls();
1883 EXPECT_TRUE(DoGetRTCStats());
1884 // Clearing stats cache is needed now, but should be temporary.
1885 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1886 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001887 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1888 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001889 EXPECT_TRUE(DoGetRTCStats());
1890 pc_->ClearStatsCache();
1891 CreateOfferReceiveAnswer();
1892 EXPECT_TRUE(DoGetRTCStats());
1893}
1894
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001896TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001897 RTCConfiguration config;
1898 config.enable_rtp_data_channel = true;
1899 config.enable_dtls_srtp = false;
1900 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001901 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001903 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 pc_->CreateDataChannel("test2", NULL);
1905 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001906 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001908 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 new MockDataChannelObserver(data2));
1910
1911 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1912 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1913 std::string data_to_send1 = "testing testing";
1914 std::string data_to_send2 = "testing something else";
1915 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1916
1917 CreateOfferReceiveAnswer();
1918 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1919 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1920
1921 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1922 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1923 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1924 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1925
1926 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1927 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1928
1929 data1->Close();
1930 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1931 CreateOfferReceiveAnswer();
1932 EXPECT_FALSE(observer1->IsOpen());
1933 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1934 EXPECT_TRUE(observer2->IsOpen());
1935
1936 data_to_send2 = "testing something else again";
1937 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1938
1939 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1940}
1941
1942// This test verifies that sendnig binary data over RTP data channels should
1943// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001944TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001945 RTCConfiguration config;
1946 config.enable_rtp_data_channel = true;
1947 config.enable_dtls_srtp = false;
1948 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001949 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001951 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 pc_->CreateDataChannel("test2", NULL);
1953 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001954 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001956 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 new MockDataChannelObserver(data2));
1958
1959 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1960 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1961
1962 CreateOfferReceiveAnswer();
1963 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1964 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1965
1966 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1967 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1968
jbaucheec21bd2016-03-20 06:15:43 -07001969 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1971}
1972
1973// This test setup a RTP data channels in loop back and test that a channel is
1974// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001975TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001976 RTCConfiguration config;
1977 config.enable_rtp_data_channel = true;
1978 config.enable_dtls_srtp = false;
1979 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001980 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001982 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 new MockDataChannelObserver(data1));
1984
1985 CreateOfferReceiveAnswerWithoutSsrc();
1986
1987 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1988
1989 data1->Close();
1990 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1991 CreateOfferReceiveAnswerWithoutSsrc();
1992 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1993 EXPECT_FALSE(observer1->IsOpen());
1994}
1995
1996// This test that if a data channel is added in an answer a receive only channel
1997// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001998TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001999 RTCConfiguration config;
2000 config.enable_rtp_data_channel = true;
2001 config.enable_dtls_srtp = false;
2002
2003 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004
2005 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002006 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 pc_->CreateDataChannel(offer_label, NULL);
2008
2009 CreateOfferAsLocalDescription();
2010
2011 // Replace the data channel label in the offer and apply it as an answer.
2012 std::string receive_label = "answer_channel";
2013 std::string sdp;
2014 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002015 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 CreateAnswerAsRemoteDescription(sdp);
2017
2018 // Verify that a new incoming data channel has been created and that
2019 // it is open but can't we written to.
2020 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2021 DataChannelInterface* received_channel = observer_.last_datachannel_;
2022 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2023 EXPECT_EQ(receive_label, received_channel->label());
2024 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2025
2026 // Verify that the channel we initially offered has been rejected.
2027 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2028
2029 // Do another offer / answer exchange and verify that the data channel is
2030 // opened.
2031 CreateOfferReceiveAnswer();
2032 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2033 kTimeout);
2034}
2035
2036// This test that no data channel is returned if a reliable channel is
2037// requested.
2038// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002039TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002040 RTCConfiguration rtc_config;
2041 rtc_config.enable_rtp_data_channel = true;
2042 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043
2044 std::string label = "test";
2045 webrtc::DataChannelInit config;
2046 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002047 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 pc_->CreateDataChannel(label, &config);
2049 EXPECT_TRUE(channel == NULL);
2050}
2051
deadbeefab9b2d12015-10-14 11:33:11 -07002052// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002053TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002054 RTCConfiguration config;
2055 config.enable_rtp_data_channel = true;
2056 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002057
2058 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002059 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002060 pc_->CreateDataChannel(label, nullptr);
2061 EXPECT_NE(channel, nullptr);
2062
zhihuang9763d562016-08-05 11:14:50 -07002063 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002064 pc_->CreateDataChannel(label, nullptr);
2065 EXPECT_EQ(dup_channel, nullptr);
2066}
2067
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068// This tests that a SCTP data channel is returned using different
2069// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002070TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002071 RTCConfiguration rtc_config;
2072 rtc_config.enable_dtls_srtp = true;
2073 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074
2075 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002076 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 pc_->CreateDataChannel("1", &config);
2078 EXPECT_TRUE(channel != NULL);
2079 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002080 EXPECT_TRUE(observer_.renegotiation_needed_);
2081 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082
2083 config.ordered = false;
2084 channel = pc_->CreateDataChannel("2", &config);
2085 EXPECT_TRUE(channel != NULL);
2086 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002087 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088
2089 config.ordered = true;
2090 config.maxRetransmits = 0;
2091 channel = pc_->CreateDataChannel("3", &config);
2092 EXPECT_TRUE(channel != NULL);
2093 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002094 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002096 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 config.maxRetransmitTime = 0;
2098 channel = pc_->CreateDataChannel("4", &config);
2099 EXPECT_TRUE(channel != NULL);
2100 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002101 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102}
2103
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002104// For backwards compatibility, we want people who "unset" maxRetransmits
2105// and maxRetransmitTime by setting them to -1 to get what they want.
2106TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
2107 RTCConfiguration rtc_config;
2108 rtc_config.enable_dtls_srtp = true;
2109 CreatePeerConnection(rtc_config);
2110
2111 webrtc::DataChannelInit config;
2112 config.maxRetransmitTime = -1;
2113 config.maxRetransmits = -1;
2114 rtc::scoped_refptr<DataChannelInterface> channel =
2115 pc_->CreateDataChannel("1", &config);
2116 EXPECT_TRUE(channel != NULL);
2117}
2118
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119// This tests that no data channel is returned if both maxRetransmits and
2120// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002121TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002123 RTCConfiguration rtc_config;
2124 rtc_config.enable_dtls_srtp = true;
2125 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126
2127 std::string label = "test";
2128 webrtc::DataChannelInit config;
2129 config.maxRetransmits = 0;
2130 config.maxRetransmitTime = 0;
2131
zhihuang9763d562016-08-05 11:14:50 -07002132 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 pc_->CreateDataChannel(label, &config);
2134 EXPECT_TRUE(channel == NULL);
2135}
2136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137// The test verifies that creating a SCTP data channel with an id already in use
2138// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002139TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002141 RTCConfiguration rtc_config;
2142 rtc_config.enable_dtls_srtp = true;
2143 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002146 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002148 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002149 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002150 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 EXPECT_TRUE(channel != NULL);
2152 EXPECT_EQ(1, channel->id());
2153
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 channel = pc_->CreateDataChannel("x", &config);
2155 EXPECT_TRUE(channel == NULL);
2156
2157 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002158 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 channel = pc_->CreateDataChannel("max", &config);
2160 EXPECT_TRUE(channel != NULL);
2161 EXPECT_EQ(config.id, channel->id());
2162
2163 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002164 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 channel = pc_->CreateDataChannel("x", &config);
2166 EXPECT_TRUE(channel == NULL);
2167}
2168
deadbeefab9b2d12015-10-14 11:33:11 -07002169// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002170TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002171 RTCConfiguration rtc_config;
2172 rtc_config.enable_dtls_srtp = true;
2173 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002174
2175 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002176 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002177 pc_->CreateDataChannel(label, nullptr);
2178 EXPECT_NE(channel, nullptr);
2179
zhihuang9763d562016-08-05 11:14:50 -07002180 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002181 pc_->CreateDataChannel(label, nullptr);
2182 EXPECT_NE(dup_channel, nullptr);
2183}
2184
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002185// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2186// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002187TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002188 RTCConfiguration rtc_config;
2189 rtc_config.enable_rtp_data_channel = true;
2190 rtc_config.enable_dtls_srtp = false;
2191 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002192
zhihuang9763d562016-08-05 11:14:50 -07002193 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002194 pc_->CreateDataChannel("test1", NULL);
2195 EXPECT_TRUE(observer_.renegotiation_needed_);
2196 observer_.renegotiation_needed_ = false;
2197
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002198 CreateOfferReceiveAnswer();
2199
zhihuang9763d562016-08-05 11:14:50 -07002200 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002201 pc_->CreateDataChannel("test2", NULL);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002202 EXPECT_EQ(observer_.renegotiation_needed_,
2203 GetParam() == SdpSemantics::kPlanB);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002204}
2205
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002207TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002208 RTCConfiguration rtc_config;
2209 rtc_config.enable_rtp_data_channel = true;
2210 rtc_config.enable_dtls_srtp = false;
2211 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212
zhihuang9763d562016-08-05 11:14:50 -07002213 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002215 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 pc_->CreateDataChannel("test2", NULL);
2217 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002218 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002220 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 new MockDataChannelObserver(data2));
2222
2223 CreateOfferReceiveAnswer();
2224 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2225 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2226
2227 ReleasePeerConnection();
2228 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2229 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2230}
2231
Zhi Huang644fde42018-04-02 19:16:26 -07002232// This tests that RTP data channels can be rejected in an answer.
2233TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002234 RTCConfiguration rtc_config;
2235 rtc_config.enable_rtp_data_channel = true;
2236 rtc_config.enable_dtls_srtp = false;
2237 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238
zhihuang9763d562016-08-05 11:14:50 -07002239 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 pc_->CreateDataChannel("offer_channel", NULL));
2241
2242 CreateOfferAsLocalDescription();
2243
2244 // Create an answer where the m-line for data channels are rejected.
2245 std::string sdp;
2246 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002247 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002248 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002249 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002251 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 data_info->rejected = true;
2253
Steve Antondb45ca82017-09-11 18:27:34 -07002254 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2256}
2257
Zhi Huang644fde42018-04-02 19:16:26 -07002258#ifdef HAVE_SCTP
2259// This tests that SCTP data channels can be rejected in an answer.
2260TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2261#else
2262TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2263#endif
2264{
Niels Möllerf06f9232018-08-07 12:32:18 +02002265 RTCConfiguration rtc_config;
2266 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002267
2268 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2269 pc_->CreateDataChannel("offer_channel", NULL));
2270
2271 CreateOfferAsLocalDescription();
2272
2273 // Create an answer where the m-line for data channels are rejected.
2274 std::string sdp;
2275 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2276 std::unique_ptr<SessionDescriptionInterface> answer(
2277 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2278 ASSERT_TRUE(answer);
2279 cricket::ContentInfo* data_info =
2280 cricket::GetFirstDataContent(answer->description());
2281 data_info->rejected = true;
2282
2283 DoSetRemoteDescription(std::move(answer));
2284 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2285}
2286
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287// Test that we can create a session description from an SDP string from
2288// FireFox, use it as a remote session description, generate an answer and use
2289// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002290TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002291 RTCConfiguration rtc_config;
2292 rtc_config.enable_dtls_srtp = true;
2293 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002294 AddAudioTrack("audio_label");
2295 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002296 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002297 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002298 webrtc::kFireFoxSdpOffer, nullptr));
2299 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 CreateAnswerAsLocalDescription();
2301 ASSERT_TRUE(pc_->local_description() != NULL);
2302 ASSERT_TRUE(pc_->remote_description() != NULL);
2303
2304 const cricket::ContentInfo* content =
2305 cricket::GetFirstAudioContent(pc_->local_description()->description());
2306 ASSERT_TRUE(content != NULL);
2307 EXPECT_FALSE(content->rejected);
2308
2309 content =
2310 cricket::GetFirstVideoContent(pc_->local_description()->description());
2311 ASSERT_TRUE(content != NULL);
2312 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002313#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 content =
2315 cricket::GetFirstDataContent(pc_->local_description()->description());
2316 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002317 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002318#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319}
2320
zhihuangb19012e2017-09-19 13:47:59 -07002321// Test that fallback from DTLS to SDES is not supported.
2322// The fallback was previously supported but was removed to simplify the code
2323// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002324TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002325 RTCConfiguration rtc_config;
2326 rtc_config.enable_dtls_srtp = true;
2327 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002328 // Wait for fake certificate to be generated. Previously, this is what caused
2329 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002330 AddAudioTrack("audio_label");
2331 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002332 ASSERT_NE(nullptr, fake_certificate_generator_);
2333 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2334 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002335 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002336 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2337 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002338 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002339}
2340
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341// Test that we can create an audio only offer and receive an answer with a
2342// limited set of audio codecs and receive an updated offer with more audio
2343// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002344TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002345 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002346 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 CreateOfferAsLocalDescription();
2348
Steve Anton36da6ff2018-02-16 16:04:20 -08002349 const char* answer_sdp =
2350 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2351 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002352 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002353 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002354 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355
Steve Anton36da6ff2018-02-16 16:04:20 -08002356 const char* reoffer_sdp =
2357 (sdp_semantics_ == SdpSemantics::kPlanB
2358 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2359 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002360 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002361 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002362 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 CreateAnswerAsLocalDescription();
2364}
2365
deadbeefc80741f2015-10-22 13:14:45 -07002366// Test that if we're receiving (but not sending) a track, subsequent offers
2367// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002368TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002369 RTCConfiguration rtc_config;
2370 rtc_config.enable_dtls_srtp = true;
2371 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002372 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002373 CreateAnswerAsLocalDescription();
2374
2375 // At this point we should be receiving stream 1, but not sending anything.
2376 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002377 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002378 DoCreateOffer(&offer, nullptr);
2379
2380 const cricket::ContentInfo* video_content =
2381 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002382 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2383 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002384
2385 const cricket::ContentInfo* audio_content =
2386 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002387 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2388 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002389}
2390
2391// Test that if we're receiving (but not sending) a track, and the
2392// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2393// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002394TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002395 RTCConfiguration rtc_config;
2396 rtc_config.enable_dtls_srtp = true;
2397 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002398 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002399 CreateAnswerAsLocalDescription();
2400
2401 // At this point we should be receiving stream 1, but not sending anything.
2402 // A new offer would be recvonly, but we'll set the "no receive" constraints
2403 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002404 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002405 RTCOfferAnswerOptions options;
2406 options.offer_to_receive_audio = 0;
2407 options.offer_to_receive_video = 0;
2408 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002409
2410 const cricket::ContentInfo* video_content =
2411 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002412 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2413 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002414
2415 const cricket::ContentInfo* audio_content =
2416 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002417 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2418 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002419}
2420
deadbeef653b8e02015-11-11 12:55:10 -08002421// Test that we can use SetConfiguration to change the ICE servers of the
2422// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002423TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002424 CreatePeerConnection();
2425
Steve Anton36da6ff2018-02-16 16:04:20 -08002426 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002427 PeerConnectionInterface::IceServer server;
2428 server.uri = "stun:test_hostname";
2429 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002430 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002431
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002432 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2433 EXPECT_EQ("test_hostname",
2434 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002435}
2436
Steve Anton36da6ff2018-02-16 16:04:20 -08002437TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002438 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002439 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002440 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002441 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002442 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2443}
2444
Steve Anton36da6ff2018-02-16 16:04:20 -08002445TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002446 PeerConnectionInterface::RTCConfiguration config;
2447 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002448 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002449 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002450 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002451
2452 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002453 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002454 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2455 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002456}
2457
skvladd1f5fda2017-02-03 16:54:05 -08002458// Test that the ice check interval can be changed. This does not verify that
2459// the setting makes it all the way to P2PTransportChannel, as that would
2460// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002461TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002462 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002463 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002464 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002465 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002466 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002467 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002468 config = pc_->GetConfiguration();
2469 EXPECT_EQ(config.ice_check_min_interval, 100);
2470}
2471
2472TEST_P(PeerConnectionInterfaceTest,
2473 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2474 PeerConnectionInterface::RTCConfiguration config;
2475 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2476 CreatePeerConnection(config);
2477 config = pc_->GetConfiguration();
2478 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2479
2480 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002481 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002482 config = pc_->GetConfiguration();
2483 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002484}
2485
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002486// Test that when SetConfiguration changes both the pool size and other
2487// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002488TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002489 SetConfigurationCreatesPooledSessionCorrectly) {
2490 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002491 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002492 config.ice_candidate_pool_size = 1;
2493 PeerConnectionInterface::IceServer server;
2494 server.uri = kStunAddressOnly;
2495 config.servers.push_back(server);
2496 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002497 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002498
2499 const cricket::FakePortAllocatorSession* session =
2500 static_cast<const cricket::FakePortAllocatorSession*>(
2501 port_allocator_->GetPooledSession());
2502 ASSERT_NE(nullptr, session);
2503 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002504}
2505
deadbeef293e9262017-01-11 12:28:30 -08002506// Test that after SetLocalDescription, changing the pool size is not allowed,
2507// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002508TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002509 CantChangePoolSizeAfterSetLocalDescription) {
2510 CreatePeerConnection();
2511 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002512 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002513 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002514 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002515
2516 // Set remote offer; can still change pool size at this point.
2517 CreateOfferAsRemoteDescription();
2518 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002519 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002520
2521 // Set local answer; now it's too late.
2522 CreateAnswerAsLocalDescription();
2523 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002524 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002525 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2526}
2527
deadbeef42a42632017-03-10 15:18:00 -08002528// Test that after setting an answer, extra pooled sessions are discarded. The
2529// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002530TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002531 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2532 CreatePeerConnection();
2533
2534 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002535 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002536 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002537 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002538
2539 // Do offer/answer.
2540 CreateOfferAsRemoteDescription();
2541 CreateAnswerAsLocalDescription();
2542
2543 // Expect no pooled sessions to be left.
2544 const cricket::PortAllocatorSession* session =
2545 port_allocator_->GetPooledSession();
2546 EXPECT_EQ(nullptr, session);
2547}
2548
2549// After Close is called, pooled candidates should be discarded so as to not
2550// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002551TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002552 CreatePeerConnection();
2553
Steve Anton36da6ff2018-02-16 16:04:20 -08002554 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002555 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002556 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002557 pc_->Close();
2558
2559 // Expect no pooled sessions to be left.
2560 const cricket::PortAllocatorSession* session =
2561 port_allocator_->GetPooledSession();
2562 EXPECT_EQ(nullptr, session);
2563}
2564
deadbeef293e9262017-01-11 12:28:30 -08002565// Test that SetConfiguration returns an invalid modification error if
2566// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002567TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002568 SetConfigurationReturnsInvalidModificationError) {
2569 PeerConnectionInterface::RTCConfiguration config;
2570 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2571 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2572 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002573 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002574
Steve Anton36da6ff2018-02-16 16:04:20 -08002575 PeerConnectionInterface::RTCConfiguration modified_config =
2576 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002577 modified_config.bundle_policy =
2578 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002579 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002580 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2581
Steve Anton36da6ff2018-02-16 16:04:20 -08002582 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002583 modified_config.rtcp_mux_policy =
2584 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002585 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002586 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2587
Steve Anton36da6ff2018-02-16 16:04:20 -08002588 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002589 modified_config.continual_gathering_policy =
2590 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002591 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002592 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2593}
2594
2595// Test that SetConfiguration returns a range error if the candidate pool size
2596// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002597TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002598 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2599 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002600 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002601 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002602
2603 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002604 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002605 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2606
2607 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002608 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002609 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2610}
2611
2612// Test that SetConfiguration returns a syntax error if parsing an ICE server
2613// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002614TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002615 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2616 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002617 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002618 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002619
2620 PeerConnectionInterface::IceServer bad_server;
2621 bad_server.uri = "stunn:www.example.com";
2622 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002623 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002624 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2625}
2626
2627// Test that SetConfiguration returns an invalid parameter error if a TURN
2628// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002629TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002630 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2631 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002632 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002633 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002634
2635 PeerConnectionInterface::IceServer bad_server;
2636 bad_server.uri = "turn:www.example.com";
2637 // Missing password.
2638 bad_server.username = "foo";
2639 config.servers.push_back(bad_server);
2640 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002641 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2642 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002643}
2644
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645// Test that PeerConnection::Close changes the states to closed and all remote
2646// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002647TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648 // Initialize a PeerConnection and negotiate local and remote session
2649 // description.
2650 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002651
2652 // With Plan B, verify the stream count. The analog with Unified Plan is the
2653 // RtpTransceiver count.
2654 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2655 ASSERT_EQ(1u, pc_->local_streams()->count());
2656 ASSERT_EQ(1u, pc_->remote_streams()->count());
2657 } else {
2658 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660
2661 pc_->Close();
2662
2663 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2664 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2665 pc_->ice_connection_state());
2666 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2667 pc_->ice_gathering_state());
2668
Steve Anton36da6ff2018-02-16 16:04:20 -08002669 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2670 EXPECT_EQ(1u, pc_->local_streams()->count());
2671 EXPECT_EQ(1u, pc_->remote_streams()->count());
2672 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002673 // Verify that the RtpTransceivers are still returned.
2674 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676
Steve Anton36da6ff2018-02-16 16:04:20 -08002677 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002678 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002679 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2680 ASSERT_TRUE(audio_receiver);
2681 ASSERT_TRUE(video_receiver);
2682 // Track state may be updated asynchronously.
2683 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2684 audio_receiver->track()->state(), kTimeout);
2685 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2686 video_receiver->track()->state(), kTimeout);
2687 } else {
2688 ASSERT_FALSE(audio_receiver);
2689 ASSERT_FALSE(video_receiver);
2690 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002691}
2692
2693// Test that PeerConnection methods fails gracefully after
2694// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002695// Don't run under Unified Plan since the stream API is not available.
2696TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002697 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002698 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699 CreateOfferAsRemoteDescription();
2700 CreateAnswerAsLocalDescription();
2701
2702 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002703 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002704 pc_->local_streams()->at(0);
2705
2706 pc_->Close();
2707
2708 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002709 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2712
2713 EXPECT_TRUE(pc_->local_description() != NULL);
2714 EXPECT_TRUE(pc_->remote_description() != NULL);
2715
kwibergd1fe2812016-04-27 06:47:29 -07002716 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002717 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002718 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002719 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720
2721 std::string sdp;
2722 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002723 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002724 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002725 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002726
2727 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002728 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002729 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002730 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002731}
2732
2733// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002734TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002735 InitiateCall();
2736 pc_->Close();
2737 DoGetStats(NULL);
2738}
deadbeefab9b2d12015-10-14 11:33:11 -07002739
2740// NOTE: The series of tests below come from what used to be
2741// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2742// setting a remote or local description has the expected effects.
2743
2744// This test verifies that the remote MediaStreams corresponding to a received
2745// SDP string is created. In this test the two separate MediaStreams are
2746// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002747TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002748 RTCConfiguration config;
2749 config.enable_dtls_srtp = true;
2750 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002751 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002752
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002753 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002754 EXPECT_TRUE(
2755 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2756 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2757 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2758
2759 // Create a session description based on another SDP with another
2760 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002761 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002762
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002763 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002764 EXPECT_TRUE(
2765 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2766}
2767
2768// This test verifies that when remote tracks are added/removed from SDP, the
2769// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002770// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2771// specific behavior.
2772TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002773 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002774 RTCConfiguration config;
2775 config.enable_dtls_srtp = true;
2776 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002777 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002778 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002779 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002780 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2781 reference_collection_));
2782
2783 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002784 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002785 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002786 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002787 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2788 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002789 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002790 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2791 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002792 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002793 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2794 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002795
2796 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002797 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002798 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002799 MockTrackObserver audio_track_observer(audio_track2);
2800 MockTrackObserver video_track_observer(video_track2);
2801
2802 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2803 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002804 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002805 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2806 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002807 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002808 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002809 audio_track2->state(), kTimeout);
2810 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2811 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002812}
2813
2814// This tests that remote tracks are ended if a local session description is set
2815// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002816TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002817 RTCConfiguration config;
2818 config.enable_dtls_srtp = true;
2819 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002820 // First create and set a remote offer, then reject its video content in our
2821 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002822 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2823 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2824 ASSERT_TRUE(audio_receiver);
2825 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2826 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002827
Steve Anton36da6ff2018-02-16 16:04:20 -08002828 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2829 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002830 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002831 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2832 video_receiver->track();
2833 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002834
kwibergd1fe2812016-04-27 06:47:29 -07002835 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002836 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002837 cricket::ContentInfo* video_info =
2838 local_answer->description()->GetContentByName("video");
2839 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002840 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002841 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2842 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002843
2844 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002845 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002846 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002847 video_info = local_offer->description()->GetContentByName("video");
2848 ASSERT_TRUE(video_info != nullptr);
2849 video_info->rejected = true;
2850 cricket::ContentInfo* audio_info =
2851 local_offer->description()->GetContentByName("audio");
2852 ASSERT_TRUE(audio_info != nullptr);
2853 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002854 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002855 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002856 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2857 kTimeout);
2858 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2859 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002860}
2861
2862// This tests that we won't crash if the remote track has been removed outside
2863// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002864// Don't run under Unified Plan since the stream API is not available.
2865TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002866 RTCConfiguration config;
2867 config.enable_dtls_srtp = true;
2868 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002869 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002870 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2871 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2872 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2873
kwibergd1fe2812016-04-27 06:47:29 -07002874 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002875 webrtc::CreateSessionDescription(SdpType::kAnswer,
2876 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002877 cricket::ContentInfo* video_info =
2878 local_answer->description()->GetContentByName("video");
2879 video_info->rejected = true;
2880 cricket::ContentInfo* audio_info =
2881 local_answer->description()->GetContentByName("audio");
2882 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002883 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002884
2885 // No crash is a pass.
2886}
2887
deadbeef5e97fb52015-10-15 12:49:08 -07002888// This tests that if a recvonly remote description is set, no remote streams
2889// will be created, even if the description contains SSRCs/MSIDs.
2890// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002891TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002892 RTCConfiguration config;
2893 config.enable_dtls_srtp = true;
2894 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002895
Steve Anton36da6ff2018-02-16 16:04:20 -08002896 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002897 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002898 CreateAndSetRemoteOffer(recvonly_offer);
2899
2900 EXPECT_EQ(0u, observer_.remote_streams()->count());
2901}
2902
deadbeefab9b2d12015-10-14 11:33:11 -07002903// This tests that a default MediaStream is created if a remote session
2904// description doesn't contain any streams and no MSID support.
2905// It also tests that the default stream is updated if a video m-line is added
2906// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002907// Don't run under Unified Plan since this behavior is Plan B specific.
2908TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002909 RTCConfiguration config;
2910 config.enable_dtls_srtp = true;
2911 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002912 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2913
2914 ASSERT_EQ(1u, observer_.remote_streams()->count());
2915 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2916
2917 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2918 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002919 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002920
2921 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2922 ASSERT_EQ(1u, observer_.remote_streams()->count());
2923 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2924 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002925 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2926 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002927 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2928 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002929 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2930 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002931}
2932
2933// This tests that a default MediaStream is created if a remote session
2934// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002935// Don't run under Unified Plan since this behavior is Plan B specific.
2936TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002937 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002938 RTCConfiguration config;
2939 config.enable_dtls_srtp = true;
2940 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002941 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2942
2943 ASSERT_EQ(1u, observer_.remote_streams()->count());
2944 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2945
2946 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2947 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002948 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002949}
2950
2951// This tests that it won't crash when PeerConnection tries to remove
2952// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002953// Don't run under Unified Plan since this behavior is Plan B specific.
2954TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002955 RTCConfiguration config;
2956 config.enable_dtls_srtp = true;
2957 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002958 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002959 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2960 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2961 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2962
2963 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2964
2965 // No crash is a pass.
2966}
2967
2968// This tests that a default MediaStream is created if the remote session
2969// description doesn't contain any streams and don't contain an indication if
2970// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002971// Don't run under Unified Plan since this behavior is Plan B specific.
2972TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002973 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002974 RTCConfiguration config;
2975 config.enable_dtls_srtp = true;
2976 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002977 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2978
2979 ASSERT_EQ(1u, observer_.remote_streams()->count());
2980 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2981 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2982 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2983}
2984
2985// This tests that a default MediaStream is not created if the remote session
2986// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002987// Don't run under Unified Plan since this behavior is Plan B specific.
2988TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002989 RTCConfiguration config;
2990 config.enable_dtls_srtp = true;
2991 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002992 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2993 EXPECT_EQ(0u, observer_.remote_streams()->count());
2994}
2995
deadbeefbda7e0b2015-12-08 17:13:40 -08002996// This tests that when setting a new description, the old default tracks are
2997// not destroyed and recreated.
2998// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002999// Don't run under Unified Plan since this behavior is Plan B specific.
3000TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003001 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003002 RTCConfiguration config;
3003 config.enable_dtls_srtp = true;
3004 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08003005 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3006
3007 ASSERT_EQ(1u, observer_.remote_streams()->count());
3008 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3009 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3010
3011 // Set the track to "disabled", then set a new description and ensure the
3012 // track is still disabled, which ensures it hasn't been recreated.
3013 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3014 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3015 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3016 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3017}
3018
deadbeefab9b2d12015-10-14 11:33:11 -07003019// This tests that a default MediaStream is not created if a remote session
3020// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003021// Don't run under Unified Plan since this behavior is Plan B specific.
3022TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003023 RTCConfiguration config;
3024 config.enable_dtls_srtp = true;
3025 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003026 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003027 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003028 EXPECT_TRUE(
3029 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3030
3031 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3032 EXPECT_EQ(0u, observer_.remote_streams()->count());
3033}
3034
Seth Hampson5897a6e2018-04-03 11:16:33 -07003035// This tests that a default MediaStream is created if a remote SDP comes from
3036// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3037TEST_F(PeerConnectionInterfaceTestPlanB,
3038 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003039 RTCConfiguration config;
3040 config.enable_dtls_srtp = true;
3041 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003042 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3043 // Add a=msid lines to simulate a Unified Plan endpoint that only
3044 // signals stream IDs with a=msid lines.
3045 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3046
3047 CreateAndSetRemoteOffer(sdp_string);
3048
3049 ASSERT_EQ(1u, observer_.remote_streams()->count());
3050 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3051 EXPECT_EQ("default", remote_stream->id());
3052 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3053}
3054
Seth Hampson5b4f0752018-04-02 16:31:36 -07003055// This tests that when a Plan B endpoint receives an SDP that signals no media
3056// stream IDs indicated by the special character "-" in the a=msid line, that
3057// a default stream ID will be used for the MediaStream ID. This can occur
3058// when a Unified Plan endpoint signals no media stream IDs, but signals both
3059// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3060TEST_F(PeerConnectionInterfaceTestPlanB,
3061 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003062 RTCConfiguration config;
3063 config.enable_dtls_srtp = true;
3064 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003065 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3066 // the sender's stream ID will be interpreted as no stream IDs.
3067 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3068 sdp_string.append("a=msid:- audiotrack0\n");
3069
3070 CreateAndSetRemoteOffer(sdp_string);
3071
3072 ASSERT_EQ(1u, observer_.remote_streams()->count());
3073 // Because SSRCs are signaled the track ID will be what was signaled in the
3074 // a=msid line.
3075 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3076 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3077 EXPECT_EQ("default", remote_stream->id());
3078 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003079
3080 // Previously a bug ocurred when setting the remote description a second time.
3081 // This is because we checked equality of the remote StreamParams stream ID
3082 // (empty), and the previously set stream ID for the remote sender
3083 // ("default"). This cause a track to be removed, then added, when really
3084 // nothing should occur because it is the same track.
3085 CreateAndSetRemoteOffer(sdp_string);
3086 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3087 EXPECT_EQ(1u, observer_.add_track_events_.size());
3088 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3089 remote_stream = observer_.remote_streams()->at(0);
3090 EXPECT_EQ("default", remote_stream->id());
3091 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003092}
3093
deadbeefab9b2d12015-10-14 11:33:11 -07003094// This tests that an RtpSender is created when the local description is set
3095// after adding a local stream.
3096// TODO(deadbeef): This test and the one below it need to be updated when
3097// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003098// Don't run under Unified Plan since this behavior is Plan B specific.
3099TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003100 RTCConfiguration config;
3101 config.enable_dtls_srtp = true;
3102 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003103
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003104 // Create an offer with 1 stream with 2 tracks of each type.
3105 rtc::scoped_refptr<StreamCollection> stream_collection =
3106 CreateStreamCollection(1, 2);
3107 pc_->AddStream(stream_collection->at(0));
3108 std::unique_ptr<SessionDescriptionInterface> offer;
3109 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003110 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003111
deadbeefab9b2d12015-10-14 11:33:11 -07003112 auto senders = pc_->GetSenders();
3113 EXPECT_EQ(4u, senders.size());
3114 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3115 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3116 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3117 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3118
3119 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003120 pc_->RemoveStream(stream_collection->at(0));
3121 stream_collection = CreateStreamCollection(1, 1);
3122 pc_->AddStream(stream_collection->at(0));
3123 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003124 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003125
deadbeefab9b2d12015-10-14 11:33:11 -07003126 senders = pc_->GetSenders();
3127 EXPECT_EQ(2u, senders.size());
3128 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3129 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3130 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3131 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3132}
3133
3134// This tests that an RtpSender is created when the local description is set
3135// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003136// Don't run under Unified Plan since this behavior is Plan B specific.
3137TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003138 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003139 RTCConfiguration config;
3140 config.enable_dtls_srtp = true;
3141 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003142
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003143 rtc::scoped_refptr<StreamCollection> stream_collection =
3144 CreateStreamCollection(1, 2);
3145 // Add a stream to create the offer, but remove it afterwards.
3146 pc_->AddStream(stream_collection->at(0));
3147 std::unique_ptr<SessionDescriptionInterface> offer;
3148 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3149 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003150
Steve Antondb45ca82017-09-11 18:27:34 -07003151 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003152 auto senders = pc_->GetSenders();
3153 EXPECT_EQ(0u, senders.size());
3154
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003155 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003156 senders = pc_->GetSenders();
3157 EXPECT_EQ(4u, senders.size());
3158 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3159 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3160 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3161 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3162}
3163
3164// This tests that the expected behavior occurs if the SSRC on a local track is
3165// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003166TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003167 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003168 RTCConfiguration config;
3169 config.enable_dtls_srtp = true;
3170 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003171
Steve Anton36da6ff2018-02-16 16:04:20 -08003172 AddAudioTrack(kAudioTracks[0]);
3173 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003174 std::unique_ptr<SessionDescriptionInterface> offer;
3175 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3176 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003177 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3178 webrtc::CreateSessionDescription(
3179 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02003180 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07003181 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003182
deadbeefab9b2d12015-10-14 11:33:11 -07003183 auto senders = pc_->GetSenders();
3184 EXPECT_EQ(2u, senders.size());
3185 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3186 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3187
3188 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003189 cricket::MediaContentDescription* desc =
3190 cricket::GetFirstAudioContentDescription(modified_offer->description());
3191 ASSERT_TRUE(desc != NULL);
3192 for (StreamParams& stream : desc->mutable_streams()) {
3193 for (unsigned int& ssrc : stream.ssrcs) {
3194 ++ssrc;
3195 }
3196 }
deadbeefab9b2d12015-10-14 11:33:11 -07003197
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003198 desc =
3199 cricket::GetFirstVideoContentDescription(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 }
3206
Steve Antondb45ca82017-09-11 18:27:34 -07003207 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003208 senders = pc_->GetSenders();
3209 EXPECT_EQ(2u, senders.size());
3210 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3211 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3212 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3213 // changed.
3214}
3215
3216// This tests that the expected behavior occurs if a new session description is
3217// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003218// Don't run under Unified Plan since the stream API is not available.
3219TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003220 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003221 RTCConfiguration config;
3222 config.enable_dtls_srtp = true;
3223 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003224
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003225 rtc::scoped_refptr<StreamCollection> stream_collection =
3226 CreateStreamCollection(2, 1);
3227 pc_->AddStream(stream_collection->at(0));
3228 std::unique_ptr<SessionDescriptionInterface> offer;
3229 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003230 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003231
deadbeefab9b2d12015-10-14 11:33:11 -07003232 auto senders = pc_->GetSenders();
3233 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003234 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3235 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003236
3237 // Add a new MediaStream but with the same tracks as in the first stream.
3238 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3239 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003240 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3241 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003242 pc_->AddStream(stream_1);
3243
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003244 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003245 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003246
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003247 auto new_senders = pc_->GetSenders();
3248 // Should be the same senders as before, but with updated stream id.
3249 // Note that this behavior is subject to change in the future.
3250 // We may decide the PC should ignore existing tracks in AddStream.
3251 EXPECT_EQ(senders, new_senders);
3252 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3253 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003254}
3255
zhihuang81c3a032016-11-17 12:06:24 -08003256// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003257TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003258 RTCConfiguration config;
3259 config.enable_dtls_srtp = true;
3260 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003261 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3262 EXPECT_EQ(observer_.num_added_tracks_, 1);
3263 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3264
3265 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003266 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003267 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003268 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3269}
3270
deadbeefd1a38b52016-12-10 13:15:33 -08003271// Test that when SetConfiguration is called and the configuration is
3272// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003273TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003274 PeerConnectionInterface::RTCConfiguration config;
3275 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003276 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003277 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003278 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3279 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003280
3281 // Do initial offer/answer so there's something to restart.
3282 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003283 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003284
3285 // Grab the ufrags.
3286 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3287
3288 // Change ICE policy, which should trigger an ICE restart on the next offer.
3289 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003290 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003291 CreateOfferAsLocalDescription();
3292
3293 // Grab the new ufrags.
3294 std::vector<std::string> subsequent_ufrags =
3295 GetUfrags(pc_->local_description());
3296
3297 // Sanity check.
3298 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3299 // Check that each ufrag is different.
3300 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3301 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3302 }
3303}
3304
3305// Test that when SetConfiguration is called and the configuration *isn't*
3306// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003307TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003308 PeerConnectionInterface::RTCConfiguration config;
3309 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003310 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003311 config = pc_->GetConfiguration();
3312 AddAudioTrack(kAudioTracks[0]);
3313 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003314
3315 // Do initial offer/answer so there's something to restart.
3316 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003317 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003318
3319 // Grab the ufrags.
3320 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3321
3322 // Call SetConfiguration with a config identical to what the PC was
3323 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003324 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003325 CreateOfferAsLocalDescription();
3326
3327 // Grab the new ufrags.
3328 std::vector<std::string> subsequent_ufrags =
3329 GetUfrags(pc_->local_description());
3330
3331 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3332}
3333
3334// Test for a weird corner case scenario:
3335// 1. Audio/video session established.
3336// 2. SetConfiguration changes ICE config; ICE restart needed.
3337// 3. ICE restart initiated by remote peer, but only for one m= section.
3338// 4. Next createOffer should initiate an ICE restart, but only for the other
3339// m= section; it would be pointless to do an ICE restart for the m= section
3340// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003341TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003342 PeerConnectionInterface::RTCConfiguration config;
3343 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003344 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003345 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003346 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3347 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003348
3349 // Do initial offer/answer so there's something to restart.
3350 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003351 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003352
3353 // Change ICE policy, which should set the "needs-ice-restart" flag.
3354 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003355 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003356
3357 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003358 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003359 webrtc::CreateSessionDescription(SdpType::kOffer,
3360 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003361 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003362 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3363 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003364 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003365 CreateAnswerAsLocalDescription();
3366
3367 // Grab the ufrags.
3368 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003369 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003370
3371 // Create offer and grab the new ufrags.
3372 CreateOfferAsLocalDescription();
3373 std::vector<std::string> subsequent_ufrags =
3374 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003375 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003376
3377 // Ensure that only the ufrag for the second m= section changed.
3378 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3379 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3380}
3381
deadbeeffe4a8a42016-12-20 17:56:17 -08003382// Tests that the methods to return current/pending descriptions work as
3383// expected at different points in the offer/answer exchange. This test does
3384// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003385TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003386 // This disables DTLS so we can apply an answer to ourselves.
3387 CreatePeerConnection();
3388
3389 // Create initial local offer and get SDP (which will also be used as
3390 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003391 std::unique_ptr<SessionDescriptionInterface> local_offer;
3392 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003393 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003394 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003395
3396 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003397 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3398 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3399 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003400 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3401 EXPECT_EQ(nullptr, pc_->current_local_description());
3402 EXPECT_EQ(nullptr, pc_->current_remote_description());
3403
3404 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003405 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003406 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003407 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3408 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3409 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3410 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003411 EXPECT_EQ(nullptr, pc_->current_local_description());
3412 EXPECT_EQ(nullptr, pc_->current_remote_description());
3413
3414 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003415 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003416 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003417 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3418 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003419 EXPECT_EQ(nullptr, pc_->pending_local_description());
3420 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003421 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3422 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003423
3424 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003425 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003426 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003427 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3428 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3429 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003430 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003431 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3432 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003433
3434 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003435 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003436 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003437 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3438 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3439 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3440 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3441 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3442 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003443
3444 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003445 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003446 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003447 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3448 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003449 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3450 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003451 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3452 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003453}
3454
zhihuang77985012017-02-07 15:45:16 -08003455// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3456// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003457// This version tests the StartRtcEventLog version that receives an object
3458// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003459TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003460 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3461 CreatePeerConnection();
3462 // The RtcEventLog will be reset when the PeerConnection is closed.
3463 pc_->Close();
3464
Niels Möllerdec9f742019-06-03 15:25:20 +02003465 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003466 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003467 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003468 pc_->StopRtcEventLog();
3469}
3470
deadbeef30952b42017-04-21 02:41:29 -07003471// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003472TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003473 CreatePeerConnection();
3474
3475 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003476 RTCOfferAnswerOptions options;
3477 options.offer_to_receive_audio = 1;
3478 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003479 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003480 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003481 cricket::SessionDescription* desc = offer->description();
3482 ASSERT_EQ(2u, desc->transport_infos().size());
3483 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3484 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3485
3486 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003487 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003488 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003489 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3490 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003491 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003492 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003493 desc = answer->description();
3494 ASSERT_EQ(2u, desc->transport_infos().size());
3495 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3496 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3497}
3498
deadbeef1dcb1642017-03-29 21:08:16 -07003499// Test that ICE renomination isn't offered if it's not enabled in the PC's
3500// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003501TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003502 PeerConnectionInterface::RTCConfiguration config;
3503 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003504 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003505 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003506
3507 std::unique_ptr<SessionDescriptionInterface> offer;
3508 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3509 cricket::SessionDescription* desc = offer->description();
3510 EXPECT_EQ(1u, desc->transport_infos().size());
3511 EXPECT_FALSE(
3512 desc->transport_infos()[0].description.GetIceParameters().renomination);
3513}
3514
3515// Test that the ICE renomination option is present in generated offers/answers
3516// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003517TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003518 PeerConnectionInterface::RTCConfiguration config;
3519 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003520 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003521 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003522
3523 std::unique_ptr<SessionDescriptionInterface> offer;
3524 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3525 cricket::SessionDescription* desc = offer->description();
3526 EXPECT_EQ(1u, desc->transport_infos().size());
3527 EXPECT_TRUE(
3528 desc->transport_infos()[0].description.GetIceParameters().renomination);
3529
3530 // Set the offer as a remote description, then create an answer and ensure it
3531 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003532 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003533 std::unique_ptr<SessionDescriptionInterface> answer;
3534 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3535 desc = answer->description();
3536 EXPECT_EQ(1u, desc->transport_infos().size());
3537 EXPECT_TRUE(
3538 desc->transport_infos()[0].description.GetIceParameters().renomination);
3539}
3540
3541// Test that if CreateOffer is called with the deprecated "offer to receive
3542// audio/video" constraints, they're processed and result in an offer with
3543// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003544TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003545 CreatePeerConnection();
3546
Niels Möllerf06f9232018-08-07 12:32:18 +02003547 RTCOfferAnswerOptions options;
3548 options.offer_to_receive_audio = 1;
3549 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003550 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003551 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003552
3553 cricket::SessionDescription* desc = offer->description();
3554 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3555 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3556 ASSERT_NE(nullptr, audio);
3557 ASSERT_NE(nullptr, video);
3558 EXPECT_FALSE(audio->rejected);
3559 EXPECT_FALSE(video->rejected);
3560}
3561
3562// Test that if CreateAnswer is called with the deprecated "offer to receive
3563// audio/video" constraints, they're processed and can be used to reject an
3564// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003565// Don't run under Unified Plan since this behavior is not supported.
3566TEST_F(PeerConnectionInterfaceTestPlanB,
3567 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003568 CreatePeerConnection();
3569
3570 // First, create an offer with audio/video and apply it as a remote
3571 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003572 RTCOfferAnswerOptions options;
3573 options.offer_to_receive_audio = 1;
3574 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003575 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003576 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003577 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003578
3579 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003580 options.offer_to_receive_audio = 0;
3581 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003582 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003583 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003584
3585 cricket::SessionDescription* desc = answer->description();
3586 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3587 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3588 ASSERT_NE(nullptr, audio);
3589 ASSERT_NE(nullptr, video);
3590 EXPECT_TRUE(audio->rejected);
3591 EXPECT_TRUE(video->rejected);
3592}
3593
deadbeef1dcb1642017-03-29 21:08:16 -07003594// Test that negotiation can succeed with a data channel only, and with the max
3595// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003596#ifdef HAVE_SCTP
3597TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3598#else
3599TEST_P(PeerConnectionInterfaceTest,
3600 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3601#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003602 PeerConnectionInterface::RTCConfiguration config;
3603 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003604 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003605
3606 // First, create an offer with only a data channel and apply it as a remote
3607 // description.
3608 pc_->CreateDataChannel("test", nullptr);
3609 std::unique_ptr<SessionDescriptionInterface> offer;
3610 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003611 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003612
3613 // Create and set answer as well.
3614 std::unique_ptr<SessionDescriptionInterface> answer;
3615 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003616 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003617}
3618
Steve Anton36da6ff2018-02-16 16:04:20 -08003619TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003620 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003621 BitrateSettings bitrate;
3622 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003623 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3624}
3625
Steve Anton36da6ff2018-02-16 16:04:20 -08003626TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003627 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003628 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003629 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003630 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3631}
3632
Steve Anton36da6ff2018-02-16 16:04:20 -08003633TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003634 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003635 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003636 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003637 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003638 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3639}
3640
Steve Anton36da6ff2018-02-16 16:04:20 -08003641TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003642 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003643 BitrateSettings bitrate;
3644 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003645 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3646}
3647
Steve Anton36da6ff2018-02-16 16:04:20 -08003648TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003649 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003650 BitrateSettings bitrate;
3651 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003652 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003653 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3654}
3655
Steve Anton36da6ff2018-02-16 16:04:20 -08003656TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003657 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003658 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003659 bitrate.min_bitrate_bps = 10;
3660 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003661 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3662}
3663
Steve Anton36da6ff2018-02-16 16:04:20 -08003664TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003665 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003666 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003667 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003668 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3669}
3670
Niels Möller0c4f7be2018-05-07 14:01:37 +02003671// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003672// by Call's BitrateConstraints, which comes from the SDP or a default value.
3673// This test checks that a call to SetBitrate with a current bitrate that will
3674// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003675TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003676 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003677 BitrateSettings bitrate;
3678 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003679 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3680}
3681
zhihuang1c378ed2017-08-17 14:10:50 -07003682// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003683TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003684 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3685 RTCOfferAnswerOptions rtc_options;
3686
3687 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3688 // than kMaxOfferToReceiveMedia should be treated as invalid.
3689 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3690 CreatePeerConnection();
3691 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3692
3693 rtc_options.offer_to_receive_audio =
3694 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3695 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3696}
3697
Steve Anton36da6ff2018-02-16 16:04:20 -08003698TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003699 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3700 RTCOfferAnswerOptions rtc_options;
3701
3702 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3703 // than kMaxOfferToReceiveMedia should be treated as invalid.
3704 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3705 CreatePeerConnection();
3706 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3707
3708 rtc_options.offer_to_receive_video =
3709 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3710 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3711}
3712
3713// Test that the audio and video content will be added to an offer if both
3714// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003715TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003716 RTCOfferAnswerOptions rtc_options;
3717 rtc_options.offer_to_receive_audio = 1;
3718 rtc_options.offer_to_receive_video = 1;
3719
3720 std::unique_ptr<SessionDescriptionInterface> offer;
3721 CreatePeerConnection();
3722 offer = CreateOfferWithOptions(rtc_options);
3723 ASSERT_TRUE(offer);
3724 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3725 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3726}
3727
3728// Test that only audio content will be added to the offer if only
3729// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003730TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003731 RTCOfferAnswerOptions rtc_options;
3732 rtc_options.offer_to_receive_audio = 1;
3733 rtc_options.offer_to_receive_video = 0;
3734
3735 std::unique_ptr<SessionDescriptionInterface> offer;
3736 CreatePeerConnection();
3737 offer = CreateOfferWithOptions(rtc_options);
3738 ASSERT_TRUE(offer);
3739 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3740 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3741}
3742
3743// Test that only video content will be added if only |offer_to_receive_video|
3744// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003745TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003746 RTCOfferAnswerOptions rtc_options;
3747 rtc_options.offer_to_receive_audio = 0;
3748 rtc_options.offer_to_receive_video = 1;
3749
3750 std::unique_ptr<SessionDescriptionInterface> offer;
3751 CreatePeerConnection();
3752 offer = CreateOfferWithOptions(rtc_options);
3753 ASSERT_TRUE(offer);
3754 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3755 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3756}
3757
zhihuang1c378ed2017-08-17 14:10:50 -07003758// Test that no media content will be added to the offer if using default
3759// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003760TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003761 RTCOfferAnswerOptions rtc_options;
3762
3763 std::unique_ptr<SessionDescriptionInterface> offer;
3764 CreatePeerConnection();
3765 offer = CreateOfferWithOptions(rtc_options);
3766 ASSERT_TRUE(offer);
3767 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3768 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3769}
3770
3771// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3772// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003773TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3774 CreatePeerConnection();
3775
zhihuang1c378ed2017-08-17 14:10:50 -07003776 RTCOfferAnswerOptions rtc_options;
3777 rtc_options.ice_restart = false;
3778 rtc_options.offer_to_receive_audio = 1;
3779
3780 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003781 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003782 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3783 auto ufrag1 =
3784 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3785 auto pwd1 =
3786 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003787
3788 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3789 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003790 auto ufrag2 =
3791 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3792 auto pwd2 =
3793 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003794
3795 // |ice_restart| is true, the ufrag/pwd should change.
3796 rtc_options.ice_restart = true;
3797 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003798 auto ufrag3 =
3799 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3800 auto pwd3 =
3801 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003802
3803 EXPECT_EQ(ufrag1, ufrag2);
3804 EXPECT_EQ(pwd1, pwd2);
3805 EXPECT_NE(ufrag2, ufrag3);
3806 EXPECT_NE(pwd2, pwd3);
3807}
3808
3809// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3810// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003811TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003812 RTCOfferAnswerOptions rtc_options;
3813 rtc_options.offer_to_receive_audio = 1;
3814 rtc_options.offer_to_receive_video = 1;
3815
3816 std::unique_ptr<SessionDescriptionInterface> offer;
3817 CreatePeerConnection();
3818
3819 rtc_options.use_rtp_mux = true;
3820 offer = CreateOfferWithOptions(rtc_options);
3821 ASSERT_TRUE(offer);
3822 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3823 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3824 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3825
3826 rtc_options.use_rtp_mux = false;
3827 offer = CreateOfferWithOptions(rtc_options);
3828 ASSERT_TRUE(offer);
3829 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3830 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3831 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3832}
3833
zhihuang141aacb2017-08-29 13:23:53 -07003834// This test ensures OnRenegotiationNeeded is called when we add track with
3835// MediaStream -> AddTrack in the same way it is called when we add track with
3836// PeerConnection -> AddTrack.
3837// The test can be removed once addStream is rewritten in terms of addTrack
3838// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003839// Don't run under Unified Plan since the stream API is not available.
3840TEST_F(PeerConnectionInterfaceTestPlanB,
3841 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003842 CreatePeerConnectionWithoutDtls();
3843 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003844 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003845 pc_->AddStream(stream);
3846 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003847 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003848 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003849 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003850 stream->AddTrack(audio_track);
3851 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3852 observer_.renegotiation_needed_ = false;
3853
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003854 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003855 stream->AddTrack(video_track);
3856 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3857 observer_.renegotiation_needed_ = false;
3858
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003859 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003860 stream->RemoveTrack(audio_track);
3861 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3862 observer_.renegotiation_needed_ = false;
3863
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003864 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003865 stream->RemoveTrack(video_track);
3866 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3867 observer_.renegotiation_needed_ = false;
3868}
3869
Zhi Huangb2d355e2017-10-26 17:26:37 -07003870// Tests that an error is returned if a description is applied that has fewer
3871// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003872TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003873 MediaSectionCountEnforcedForSubsequentOffer) {
3874 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003875 AddAudioTrack("audio_label");
3876 AddVideoTrack("video_label");
3877
Zhi Huangb2d355e2017-10-26 17:26:37 -07003878 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003879 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003880 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3881
3882 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003883 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003884 offer->description()->contents().pop_back();
3885 offer->description()->contents().pop_back();
3886 ASSERT_TRUE(offer->description()->contents().empty());
3887 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3888
3889 std::unique_ptr<SessionDescriptionInterface> answer;
3890 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3891 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3892
3893 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003894 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003895 offer->description()->contents().pop_back();
3896 offer->description()->contents().pop_back();
3897 ASSERT_TRUE(offer->description()->contents().empty());
3898 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3899}
3900
Johannes Kron89f874e2018-11-12 10:25:48 +01003901TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3902 RTCConfiguration config;
Emil Lundmark801c9992021-01-19 13:06:32 +01003903 // Default behavior is true.
Johannes Kron89f874e2018-11-12 10:25:48 +01003904 CreatePeerConnection(config);
3905 std::unique_ptr<SessionDescriptionInterface> offer;
3906 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003907 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3908 // Possible to set to false.
3909 config.offer_extmap_allow_mixed = false;
Johannes Kron89f874e2018-11-12 10:25:48 +01003910 CreatePeerConnection(config);
3911 offer.release();
3912 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Emil Lundmark801c9992021-01-19 13:06:32 +01003913 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
Johannes Kron89f874e2018-11-12 10:25:48 +01003914}
3915
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003916INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3917 PeerConnectionInterfaceTest,
3918 Values(SdpSemantics::kPlanB,
3919 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003920
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003921class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003922 protected:
3923 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003924 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003925 }
nisseeaabdf62017-05-05 02:23:02 -07003926 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003927 const RTCConfiguration& config) {
3928 rtc::scoped_refptr<PeerConnectionInterface> pc(
3929 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003930 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003931 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003932 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003933 }
3934
zhihuang9763d562016-08-05 11:14:50 -07003935 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003936 MockPeerConnectionObserver observer_;
3937};
3938
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003939// This sanity check validates the test infrastructure itself.
3940TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3941 PeerConnectionInterface::RTCConfiguration config;
3942 rtc::scoped_refptr<PeerConnectionInterface> pc(
3943 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3944 EXPECT_TRUE(pc.get());
3945 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3946 pc->Close(); // No abort -> ok.
3947 SUCCEED();
3948}
3949
nisse51542be2016-02-12 02:27:06 -08003950// This test verifies the default behaviour with no constraints and a
3951// default RTCConfiguration.
3952TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3953 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003954
Niels Möllerf06f9232018-08-07 12:32:18 +02003955 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003956
3957 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003958 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3959 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003960 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003961 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003962}
3963
Niels Möller1d7ecd22018-01-18 15:25:12 +01003964// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003965// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003966TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3967 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003968
Niels Möller71bdda02016-03-31 12:59:59 +02003969 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003970 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003971
Niels Möller1d7ecd22018-01-18 15:25:12 +01003972 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003973}
3974
Niels Möller6539f692018-01-18 08:58:50 +01003975// This test verifies that the experiment_cpu_load_estimator flag is
3976// propagated from RTCConfiguration to the PeerConnection.
3977TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3978 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003979
3980 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003981 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003982
3983 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3984}
3985
deadbeef293e9262017-01-11 12:28:30 -08003986// Tests a few random fields being different.
3987TEST(RTCConfigurationTest, ComparisonOperators) {
3988 PeerConnectionInterface::RTCConfiguration a;
3989 PeerConnectionInterface::RTCConfiguration b;
3990 EXPECT_EQ(a, b);
3991
3992 PeerConnectionInterface::RTCConfiguration c;
3993 c.servers.push_back(PeerConnectionInterface::IceServer());
3994 EXPECT_NE(a, c);
3995
3996 PeerConnectionInterface::RTCConfiguration d;
3997 d.type = PeerConnectionInterface::kRelay;
3998 EXPECT_NE(a, d);
3999
4000 PeerConnectionInterface::RTCConfiguration e;
4001 e.audio_jitter_buffer_max_packets = 5;
4002 EXPECT_NE(a, e);
4003
4004 PeerConnectionInterface::RTCConfiguration f;
4005 f.ice_connection_receiving_timeout = 1337;
4006 EXPECT_NE(a, f);
4007
4008 PeerConnectionInterface::RTCConfiguration g;
4009 g.disable_ipv6 = true;
4010 EXPECT_NE(a, g);
4011
4012 PeerConnectionInterface::RTCConfiguration h(
4013 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4014 EXPECT_NE(a, h);
4015}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004016
4017} // namespace
4018} // namespace webrtc