blob: a0e3845fd6ecbb865da46b7885b9167bf2fb7176 [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();
zhihuang29ff8442016-07-27 11:07:25 -0700702 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 }
704
705 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200706 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708
deadbeef293e9262017-01-11 12:28:30 -0800709 // DTLS does not work in a loopback call, so is disabled for most of the
710 // tests in this file.
711 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200712 RTCConfiguration config;
713 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800714
Niels Möllerf06f9232018-08-07 12:32:18 +0200715 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 }
717
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718 void CreatePeerConnectionWithIceTransportsType(
719 PeerConnectionInterface::IceTransportsType type) {
720 PeerConnectionInterface::RTCConfiguration config;
721 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200722 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700723 }
724
725 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100726 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100731 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700732 server.password = password;
733 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200734 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700735 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736
Niels Möllerf06f9232018-08-07 12:32:18 +0200737 void CreatePeerConnection(const RTCConfiguration& config) {
kwibergd1fe2812016-04-27 06:47:29 -0700738 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800739 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
740 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000741
deadbeef1dcb1642017-03-29 21:08:16 -0700742 // Create certificate generator unless DTLS constraint is explicitly set to
743 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200744 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200745
746 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800747 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
748 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000749 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800750 RTCConfiguration modified_config = config;
751 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200752 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200753 modified_config, std::move(port_allocator), std::move(cert_generator),
754 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 ASSERT_TRUE(pc_.get() != NULL);
756 observer_.SetPeerConnectionInterface(pc_.get());
757 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
758 }
759
deadbeef0a6c4ca2015-10-06 11:38:28 -0700760 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800761 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700762 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700763 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800764 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800765 config.sdp_semantics = sdp_semantics_;
766 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200767 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700769 }
770
Steve Anton038834f2017-07-14 15:59:59 -0700771 void CreatePeerConnectionExpectFail(
772 PeerConnectionInterface::RTCConfiguration config) {
773 PeerConnectionInterface::IceServer server;
774 server.uri = kTurnIceServerUri;
775 server.password = kTurnPassword;
776 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800777 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700778 rtc::scoped_refptr<PeerConnectionInterface> pc =
779 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
780 EXPECT_EQ(nullptr, pc);
781 }
782
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100784 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800785 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
786 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
787 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800789 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790
deadbeef0a6c4ca2015-10-06 11:38:28 -0700791 CreatePeerConnectionExpectFail(kStunInvalidPort);
792 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
793 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794
Niels Möllerdb4def92019-03-18 16:53:59 +0100795 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
796 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
798 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 }
806
807 void ReleasePeerConnection() {
808 pc_ = NULL;
809 observer_.SetPeerConnectionInterface(NULL);
810 }
811
Steve Anton36da6ff2018-02-16 16:04:20 -0800812 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
813 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100814 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800815 }
816
817 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800818 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800819 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800820 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800821 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823
Steve Anton36da6ff2018-02-16 16:04:20 -0800824 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700825 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800827 stream->AddTrack(CreateVideoTrack(label + "v0"));
828 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800829 }
830
831 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
832 const std::string& label) {
833 return pc_factory_->CreateAudioTrack(label, nullptr);
834 }
835
836 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800837 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800838 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800839 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800840 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800841 }
842
843 void AddAudioStream(const std::string& label) {
844 rtc::scoped_refptr<MediaStreamInterface> stream(
845 pc_factory_->CreateLocalMediaStream(label));
846 stream->AddTrack(CreateAudioTrack(label + "a0"));
847 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
849
Seth Hampson845e8782018-03-02 11:34:10 -0800850 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 const std::string& audio_track_label,
852 const std::string& video_track_label) {
853 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700854 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800855 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800856 stream->AddTrack(CreateAudioTrack(audio_track_label));
857 stream->AddTrack(CreateVideoTrack(video_track_label));
858 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
Steve Anton36da6ff2018-02-16 16:04:20 -0800861 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
862 cricket::MediaType media_type) {
863 for (auto receiver : pc_->GetReceivers()) {
864 if (receiver->media_type() == media_type) {
865 return receiver;
866 }
867 }
868 return nullptr;
869 }
870
kwibergd1fe2812016-04-27 06:47:29 -0700871 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200872 const RTCOfferAnswerOptions* options,
873 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200874 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
875 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200877 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200879 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700882 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 return observer->result();
884 }
885
kwibergd1fe2812016-04-27 06:47:29 -0700886 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200887 const RTCOfferAnswerOptions* options) {
888 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890
kwibergd1fe2812016-04-27 06:47:29 -0700891 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200892 const RTCOfferAnswerOptions* options) {
893 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895
Steve Antondb45ca82017-09-11 18:27:34 -0700896 bool DoSetSessionDescription(
897 std::unique_ptr<SessionDescriptionInterface> desc,
898 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200899 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
900 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700902 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700904 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
zhihuang29ff8442016-07-27 11:07:25 -0700906 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
907 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
908 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 return observer->result();
910 }
911
Steve Antondb45ca82017-09-11 18:27:34 -0700912 bool DoSetLocalDescription(
913 std::unique_ptr<SessionDescriptionInterface> desc) {
914 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
916
Steve Antondb45ca82017-09-11 18:27:34 -0700917 bool DoSetRemoteDescription(
918 std::unique_ptr<SessionDescriptionInterface> desc) {
919 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921
922 // Calls PeerConnection::GetStats and check the return value.
923 // It does not verify the values in the StatReports since a RTCP packet might
924 // be required.
925 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000926 rtc::scoped_refptr<MockStatsObserver> observer(
927 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200928 if (!pc_->GetStats(observer, track,
929 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 return false;
931 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
932 return observer->called();
933 }
934
Harald Alvestrand89061872018-01-02 14:08:34 +0100935 // Call the standards-compliant GetStats function.
936 bool DoGetRTCStats() {
937 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
938 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
939 pc_->GetStats(callback);
940 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
941 return callback->called();
942 }
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800945 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800947 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800948 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800949 } else {
950 // Unified Plan does not support AddStream, so just add an audio and video
951 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800952 AddAudioTrack(kAudioTracks[0], {kStreamId1});
953 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800954 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 CreateOfferReceiveAnswer();
956 }
957
958 // Verify that RTP Header extensions has been negotiated for audio and video.
959 void VerifyRemoteRtpHeaderExtensions() {
960 const cricket::MediaContentDescription* desc =
961 cricket::GetFirstAudioContentDescription(
962 pc_->remote_description()->description());
963 ASSERT_TRUE(desc != NULL);
964 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
965
966 desc = cricket::GetFirstVideoContentDescription(
967 pc_->remote_description()->description());
968 ASSERT_TRUE(desc != NULL);
969 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
970 }
971
972 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700973 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700974 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 std::string sdp;
976 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700977 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800978 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700979 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
981 }
982
deadbeefab9b2d12015-10-14 11:33:11 -0700983 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700984 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800985 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700986 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700987 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
988 }
989
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700991 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700992 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993
994 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
995 // audio codec change, even if the parameter has nothing to do with
996 // receiving. Not all parameters are serialized to SDP.
997 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
998 // the SessionDescription, it is necessary to do that here to in order to
999 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1000 // https://code.google.com/p/webrtc/issues/detail?id=1356
1001 std::string sdp;
1002 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001003 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001004 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001005 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1007 }
1008
1009 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001010 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001011 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
1013 std::string sdp;
1014 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001015 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001016 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001017 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1019 }
1020
1021 void CreateOfferReceiveAnswer() {
1022 CreateOfferAsLocalDescription();
1023 std::string sdp;
1024 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1025 CreateAnswerAsRemoteDescription(sdp);
1026 }
1027
1028 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001029 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001030 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1032 // audio codec change, even if the parameter has nothing to do with
1033 // receiving. Not all parameters are serialized to SDP.
1034 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1035 // the SessionDescription, it is necessary to do that here to in order to
1036 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1037 // https://code.google.com/p/webrtc/issues/detail?id=1356
1038 std::string sdp;
1039 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001040 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001041 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042
Steve Antondb45ca82017-09-11 18:27:34 -07001043 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001045 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001046 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 }
1048
deadbeefab9b2d12015-10-14 11:33:11 -07001049 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001050 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001051 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001052 ASSERT_TRUE(answer);
1053 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1055 }
1056
deadbeefab9b2d12015-10-14 11:33:11 -07001057 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001058 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001059 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001060 ASSERT_TRUE(pr_answer);
1061 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001063 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001064 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001065 ASSERT_TRUE(answer);
1066 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1068 }
1069
Seth Hampson845e8782018-03-02 11:34:10 -08001070 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001071 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001072 // called with the given stream id and expected number of tracks.
1073 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001074 int expected_num_tracks) {
1075 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001076 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001077 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001078 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 }
1080
1081 // Creates an offer and applies it as a local session description.
1082 // Creates an answer with the same SDP an the offer but removes all lines
1083 // that start with a:ssrc"
1084 void CreateOfferReceiveAnswerWithoutSsrc() {
1085 CreateOfferAsLocalDescription();
1086 std::string sdp;
1087 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1088 SetSsrcToZero(&sdp);
1089 CreateAnswerAsRemoteDescription(sdp);
1090 }
1091
deadbeefab9b2d12015-10-14 11:33:11 -07001092 // This function creates a MediaStream with label kStreams[0] and
1093 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1094 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001095 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001096 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001097 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001098 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1099 size_t number_of_video_tracks) {
1100 EXPECT_LE(number_of_audio_tracks, 2u);
1101 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001102
1103 reference_collection_ = StreamCollection::Create();
1104 std::string sdp_ms1 = std::string(kSdpStringInit);
1105
Seth Hampson845e8782018-03-02 11:34:10 -08001106 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001107
1108 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001109 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001110 reference_collection_->AddStream(stream);
1111
1112 if (number_of_audio_tracks > 0) {
1113 sdp_ms1 += std::string(kSdpStringAudio);
1114 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1115 AddAudioTrack(kAudioTracks[0], stream);
1116 }
1117 if (number_of_audio_tracks > 1) {
1118 sdp_ms1 += kSdpStringMs1Audio1;
1119 AddAudioTrack(kAudioTracks[1], stream);
1120 }
1121
1122 if (number_of_video_tracks > 0) {
1123 sdp_ms1 += std::string(kSdpStringVideo);
1124 sdp_ms1 += std::string(kSdpStringMs1Video0);
1125 AddVideoTrack(kVideoTracks[0], stream);
1126 }
1127 if (number_of_video_tracks > 1) {
1128 sdp_ms1 += kSdpStringMs1Video1;
1129 AddVideoTrack(kVideoTracks[1], stream);
1130 }
1131
kwibergd1fe2812016-04-27 06:47:29 -07001132 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001133 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001134 }
1135
1136 void AddAudioTrack(const std::string& track_id,
1137 MediaStreamInterface* stream) {
1138 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1139 webrtc::AudioTrack::Create(track_id, nullptr));
1140 ASSERT_TRUE(stream->AddTrack(audio_track));
1141 }
1142
1143 void AddVideoTrack(const std::string& track_id,
1144 MediaStreamInterface* stream) {
1145 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001146 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001147 webrtc::FakeVideoTrackSource::Create(),
1148 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001149 ASSERT_TRUE(stream->AddTrack(video_track));
1150 }
1151
Steve Anton36da6ff2018-02-16 16:04:20 -08001152 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001153 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001154 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001155 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001156 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1157 return offer;
1158 }
1159
Steve Anton36da6ff2018-02-16 16:04:20 -08001160 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1161 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001162 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001163 std::unique_ptr<SessionDescriptionInterface> offer;
1164 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1165 return offer;
1166 }
1167
1168 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1169 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1170 std::unique_ptr<SessionDescriptionInterface> answer;
1171 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1172 return answer;
1173 }
1174
kwibergfd8be342016-05-14 19:44:11 -07001175 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001176 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001177 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1180 return answer;
1181 }
1182
1183 const std::string& GetFirstAudioStreamCname(
1184 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001186 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001187 return audio_desc->streams()[0].cname;
1188 }
1189
zhihuang1c378ed2017-08-17 14:10:50 -07001190 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1191 const RTCOfferAnswerOptions& offer_answer_options) {
1192 RTC_DCHECK(pc_);
1193 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1194 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1195 pc_->CreateOffer(observer, offer_answer_options);
1196 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1197 return observer->MoveDescription();
1198 }
1199
1200 void CreateOfferWithOptionsAsRemoteDescription(
1201 std::unique_ptr<SessionDescriptionInterface>* desc,
1202 const RTCOfferAnswerOptions& offer_answer_options) {
1203 *desc = CreateOfferWithOptions(offer_answer_options);
1204 ASSERT_TRUE(desc != nullptr);
1205 std::string sdp;
1206 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001207 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001208 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001209 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001210 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1211 }
1212
1213 void CreateOfferWithOptionsAsLocalDescription(
1214 std::unique_ptr<SessionDescriptionInterface>* desc,
1215 const RTCOfferAnswerOptions& offer_answer_options) {
1216 *desc = CreateOfferWithOptions(offer_answer_options);
1217 ASSERT_TRUE(desc != nullptr);
1218 std::string sdp;
1219 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001220 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001221 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001222
Steve Antondb45ca82017-09-11 18:27:34 -07001223 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001224 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1225 }
1226
1227 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001228 RTC_DCHECK(content);
1229 RTC_DCHECK(content->media_description());
1230 for (const cricket::AudioCodec& codec :
1231 content->media_description()->as_audio()->codecs()) {
1232 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001233 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001234 }
zhihuang1c378ed2017-08-17 14:10:50 -07001235 }
1236 return false;
1237 }
1238
Steve Anton36da6ff2018-02-16 16:04:20 -08001239 const char* GetSdpStringWithStream1() const {
1240 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1241 return kSdpStringWithStream1PlanB;
1242 } else {
1243 return kSdpStringWithStream1UnifiedPlan;
1244 }
1245 }
1246
1247 const char* GetSdpStringWithStream1And2() const {
1248 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1249 return kSdpStringWithStream1And2PlanB;
1250 } else {
1251 return kSdpStringWithStream1And2UnifiedPlan;
1252 }
1253 }
1254
deadbeef9a6f4d42017-05-15 19:43:33 -07001255 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1256 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001257 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001258 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001259 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001260 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1261 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1262 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001264 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001265 const SdpSemantics sdp_semantics_;
1266};
1267
1268class PeerConnectionInterfaceTest
1269 : public PeerConnectionInterfaceBaseTest,
1270 public ::testing::WithParamInterface<SdpSemantics> {
1271 protected:
1272 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1273};
1274
1275class PeerConnectionInterfaceTestPlanB
1276 : public PeerConnectionInterfaceBaseTest {
1277 protected:
1278 PeerConnectionInterfaceTestPlanB()
1279 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280};
1281
zhihuang8f65cdf2016-05-06 18:40:30 -07001282// Generate different CNAMEs when PeerConnections are created.
1283// The CNAMEs are expected to be generated randomly. It is possible
1284// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001285TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001286 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001287 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001288 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001289 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001290 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1291 GetFirstAudioStreamCname(offer2.get()));
1292}
1293
Steve Anton36da6ff2018-02-16 16:04:20 -08001294TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001295 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001296 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001297 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001298 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001299 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1300 GetFirstAudioStreamCname(answer2.get()));
1301}
1302
Steve Anton36da6ff2018-02-16 16:04:20 -08001303TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 CreatePeerConnectionWithDifferentConfigurations) {
1305 CreatePeerConnectionWithDifferentConfigurations();
1306}
1307
Steve Anton36da6ff2018-02-16 16:04:20 -08001308TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001309 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1310 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1311 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1312 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1313 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1314 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1315 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1316 port_allocator_->candidate_filter());
1317 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1318 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1319}
1320
1321// Test that when a PeerConnection is created with a nonzero candidate pool
1322// size, the pooled PortAllocatorSession is created with all the attributes
1323// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001324TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325 PeerConnectionInterface::RTCConfiguration config;
1326 PeerConnectionInterface::IceServer server;
1327 server.uri = kStunAddressOnly;
1328 config.servers.push_back(server);
1329 config.type = PeerConnectionInterface::kRelay;
1330 config.disable_ipv6 = true;
1331 config.tcp_candidate_policy =
1332 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001333 config.candidate_network_policy =
1334 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001335 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001336 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001337
1338 const cricket::FakePortAllocatorSession* session =
1339 static_cast<const cricket::FakePortAllocatorSession*>(
1340 port_allocator_->GetPooledSession());
1341 ASSERT_NE(nullptr, session);
1342 EXPECT_EQ(1UL, session->stun_servers().size());
1343 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1344 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001345 EXPECT_LT(0U,
1346 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001347}
1348
deadbeefd21eab32017-07-26 16:50:11 -07001349// Test that network-related RTCConfiguration members are applied to the
1350// PortAllocator when CreatePeerConnection is called. Specifically:
1351// - disable_ipv6_on_wifi
1352// - max_ipv6_networks
1353// - tcp_candidate_policy
1354// - candidate_network_policy
1355// - prune_turn_ports
1356//
1357// Note that the candidate filter (RTCConfiguration::type) is already tested
1358// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001359TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001360 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1361 // Create fake port allocator.
1362 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1363 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1364 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1365
1366 // Create RTCConfiguration with some network-related fields relevant to
1367 // PortAllocator populated.
1368 PeerConnectionInterface::RTCConfiguration config;
1369 config.disable_ipv6_on_wifi = true;
1370 config.max_ipv6_networks = 10;
1371 config.tcp_candidate_policy =
1372 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1373 config.candidate_network_policy =
1374 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1375 config.prune_turn_ports = true;
1376
1377 // Create the PC factory and PC with the above config.
1378 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1379 webrtc::CreatePeerConnectionFactory(
1380 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001381 rtc::Thread::Current(), fake_audio_capture_module_,
1382 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001383 webrtc::CreateBuiltinAudioDecoderFactory(),
1384 webrtc::CreateBuiltinVideoEncoderFactory(),
1385 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1386 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001387 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001388 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1389 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001390 EXPECT_TRUE(pc.get());
1391 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001392
1393 // Now validate that the config fields set above were applied to the
1394 // PortAllocator, as flags or otherwise.
1395 EXPECT_FALSE(raw_port_allocator->flags() &
1396 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1397 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1398 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1399 EXPECT_TRUE(raw_port_allocator->flags() &
1400 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001401 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1402 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001403}
1404
deadbeef46c73892016-11-16 19:42:04 -08001405// Check that GetConfiguration returns the configuration the PeerConnection was
1406// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001407TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001408 PeerConnectionInterface::RTCConfiguration config;
1409 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001410 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001411
1412 PeerConnectionInterface::RTCConfiguration returned_config =
1413 pc_->GetConfiguration();
1414 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1415}
1416
1417// Check that GetConfiguration returns the last configuration passed into
1418// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001419TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001420 PeerConnectionInterface::RTCConfiguration starting_config;
1421 starting_config.bundle_policy =
1422 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1423 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001424
Steve Anton36da6ff2018-02-16 16:04:20 -08001425 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001426 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02001427 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001428
1429 PeerConnectionInterface::RTCConfiguration returned_config =
1430 pc_->GetConfiguration();
1431 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1432}
1433
Steve Antonc79268f2018-04-24 09:54:10 -07001434TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1435 CreatePeerConnection();
1436
1437 pc_->Close();
1438
1439 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001440 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001441}
1442
Steve Anton36da6ff2018-02-16 16:04:20 -08001443TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001444 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001445 AddVideoStream(kStreamId1);
1446 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 ASSERT_EQ(2u, pc_->local_streams()->count());
1448
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001449 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001450 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001451 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001452 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001453 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001454 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001456 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001457 EXPECT_EQ(3u, pc_->local_streams()->count());
1458
1459 // Remove the third stream.
1460 pc_->RemoveStream(pc_->local_streams()->at(2));
1461 EXPECT_EQ(2u, pc_->local_streams()->count());
1462
1463 // Remove the second stream.
1464 pc_->RemoveStream(pc_->local_streams()->at(1));
1465 EXPECT_EQ(1u, pc_->local_streams()->count());
1466
1467 // Remove the first stream.
1468 pc_->RemoveStream(pc_->local_streams()->at(0));
1469 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470}
1471
deadbeefab9b2d12015-10-14 11:33:11 -07001472// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001473// Don't run under Unified Plan since the stream API is not available.
1474TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001475 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001476 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001477 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001478 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001479
deadbeefab9b2d12015-10-14 11:33:11 -07001480 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001481 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001482 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001483
deadbeefab9b2d12015-10-14 11:33:11 -07001484 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001485 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001486 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001487
1488 // Add another stream and ensure the offer includes both the old and new
1489 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001490 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001491 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001492
Steve Antonb1c1de12017-12-21 15:14:30 -08001493 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001494 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1495 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001496
Steve Antonb1c1de12017-12-21 15:14:30 -08001497 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001498 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1499 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001500}
1501
Steve Anton36da6ff2018-02-16 16:04:20 -08001502// Don't run under Unified Plan since the stream API is not available.
1503TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001504 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001505 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 ASSERT_EQ(1u, pc_->local_streams()->count());
1507 pc_->RemoveStream(pc_->local_streams()->at(0));
1508 EXPECT_EQ(0u, pc_->local_streams()->count());
1509}
1510
deadbeefe1f9d832016-01-14 15:35:42 -08001511// Test for AddTrack and RemoveTrack methods.
1512// Tests that the created offer includes tracks we added,
1513// and that the RtpSenders are created correctly.
1514// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001515// Only tested with Plan B since Unified Plan is covered in more detail by tests
1516// in peerconnection_jsep_unittests.cc
1517TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001518 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001519 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001520 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001521 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001522 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001523 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1524 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001525 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001526 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001527 EXPECT_EQ("audio_track", audio_sender->id());
1528 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001529 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001530 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001531 EXPECT_EQ("video_track", video_sender->id());
1532 EXPECT_EQ(video_track, video_sender->track());
1533
1534 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001536 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001537
1538 const cricket::ContentInfo* audio_content =
1539 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001540 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001541 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001542
1543 const cricket::ContentInfo* video_content =
1544 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001545 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001546 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001547
Steve Antondb45ca82017-09-11 18:27:34 -07001548 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001549
1550 // Now try removing the tracks.
1551 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1552 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1553
1554 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001555 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001556
1557 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001558 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001559 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001560
1561 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001562 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001563 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001564
Steve Antondb45ca82017-09-11 18:27:34 -07001565 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001566
1567 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1568 // should return false.
1569 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1570 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1571}
1572
1573// Test creating senders without a stream specified,
1574// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001575TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001576 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001577 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001578 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001579 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001580 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001581 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001582 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001583 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001584 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001585 EXPECT_EQ("audio_track", audio_sender->id());
1586 EXPECT_EQ(audio_track, audio_sender->track());
1587 EXPECT_EQ("video_track", video_sender->id());
1588 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001589 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1590 // If the ID is truly a random GUID, it should be infinitely unlikely they
1591 // will be the same.
1592 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1593 } else {
1594 // We allows creating tracks without stream ids under Unified Plan
1595 // semantics.
1596 EXPECT_EQ(0u, video_sender->stream_ids().size());
1597 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1598 }
deadbeefe1f9d832016-01-14 15:35:42 -08001599}
1600
Harald Alvestrand89061872018-01-02 14:08:34 +01001601// Test that we can call GetStats() after AddTrack but before connecting
1602// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001603TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001604 CreatePeerConnectionWithoutDtls();
1605 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001606 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001607 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001608 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001609 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1610 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001611 EXPECT_TRUE(DoGetStats(nullptr));
1612}
1613
Steve Anton36da6ff2018-02-16 16:04:20 -08001614TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001615 CreatePeerConnectionWithoutDtls();
1616 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001617 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001618 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001619 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001620 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001621 ASSERT_TRUE(audio_sender.ok());
1622 auto* audio_sender_proxy =
1623 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1624 audio_sender.value().get());
1625 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1626
Harald Alvestrandc72af932018-01-11 17:18:19 +01001627 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001628 ASSERT_TRUE(video_sender.ok());
1629 auto* video_sender_proxy =
1630 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1631 video_sender.value().get());
1632 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001633}
1634
Steve Anton36da6ff2018-02-16 16:04:20 -08001635// Don't run under Unified Plan since the stream API is not available.
1636TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001637 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001638 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001639 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001640 ASSERT_EQ(1u, senders.size());
1641 auto* sender_proxy =
1642 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1643 senders[0].get());
1644 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001645}
1646
Steve Anton36da6ff2018-02-16 16:04:20 -08001647TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001649 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 VerifyRemoteRtpHeaderExtensions();
1651}
1652
Steve Anton36da6ff2018-02-16 16:04:20 -08001653TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001654 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001655 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 CreateOfferAsLocalDescription();
1657 std::string offer;
1658 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1659 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001660 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661}
1662
Steve Anton36da6ff2018-02-16 16:04:20 -08001663TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001664 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001665 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666
1667 CreateOfferAsRemoteDescription();
1668 CreateAnswerAsLocalDescription();
1669
Seth Hampson845e8782018-03-02 11:34:10 -08001670 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671}
1672
Steve Anton36da6ff2018-02-16 16:04:20 -08001673TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001674 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001675 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676
1677 CreateOfferAsRemoteDescription();
1678 CreatePrAnswerAsLocalDescription();
1679 CreateAnswerAsLocalDescription();
1680
Seth Hampson845e8782018-03-02 11:34:10 -08001681 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682}
1683
Steve Anton36da6ff2018-02-16 16:04:20 -08001684// Don't run under Unified Plan since the stream API is not available.
1685TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 InitiateCall();
1687 ASSERT_EQ(1u, pc_->remote_streams()->count());
1688 pc_->RemoveStream(pc_->local_streams()->at(0));
1689 CreateOfferReceiveAnswer();
1690 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001691 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 CreateOfferReceiveAnswer();
1693}
1694
1695// Tests that after negotiating an audio only call, the respondent can perform a
1696// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001697TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001698 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001699 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 CreateOfferAsRemoteDescription();
1701 CreateAnswerAsLocalDescription();
1702
1703 ASSERT_EQ(1u, pc_->remote_streams()->count());
1704 pc_->RemoveStream(pc_->local_streams()->at(0));
1705 CreateOfferReceiveAnswer();
1706 EXPECT_EQ(0u, pc_->remote_streams()->count());
1707}
1708
1709// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001710TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001711 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712
Steve Antonf1c6db12017-10-13 11:13:35 -07001713 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001715 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001716 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001717 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001718 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719
1720 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001722 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001723 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724
Steve Antonf1c6db12017-10-13 11:13:35 -07001725 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001726 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
Steve Antonf1c6db12017-10-13 11:13:35 -07001728 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729}
1730
deadbeefab9b2d12015-10-14 11:33:11 -07001731// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001733TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001734 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001736 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001737 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001738 EXPECT_TRUE(offer);
1739 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740
1741 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001742 AddAudioTrack("track_label", {kStreamId1});
1743 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744
1745 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001746 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747
1748 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001749 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001750 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751}
1752
1753// Test that we will get different SSRCs for each tracks in the offer and answer
1754// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001755TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001756 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001758 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1759 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760
1761 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001762 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001763 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 int audio_ssrc = 0;
1765 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001766 EXPECT_TRUE(
1767 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1768 EXPECT_TRUE(
1769 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 EXPECT_NE(audio_ssrc, video_ssrc);
1771
1772 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001773 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001774 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001775 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 audio_ssrc = 0;
1777 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001778 EXPECT_TRUE(
1779 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1780 EXPECT_TRUE(
1781 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 EXPECT_NE(audio_ssrc, video_ssrc);
1783}
1784
deadbeefeb459812015-12-15 19:24:43 -08001785// Test that it's possible to call AddTrack on a MediaStream after adding
1786// the stream to a PeerConnection.
1787// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001788// Don't run under Unified Plan since the stream API is not available.
1789TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001790 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001791 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001792 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001793 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1794
1795 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001796 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001797 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001798 stream->AddTrack(video_track.get());
1799
kwibergd1fe2812016-04-27 06:47:29 -07001800 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001801 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001802
1803 const cricket::MediaContentDescription* video_desc =
1804 cricket::GetFirstVideoContentDescription(offer->description());
1805 EXPECT_TRUE(video_desc != nullptr);
1806}
1807
1808// Test that it's possible to call RemoveTrack on a MediaStream after adding
1809// the stream to a PeerConnection.
1810// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001811// Don't run under Unified Plan since the stream API is not available.
1812TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001813 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001814 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001815 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001816 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1817
1818 // Remove the video track.
1819 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1820
kwibergd1fe2812016-04-27 06:47:29 -07001821 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001822 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001823
1824 const cricket::MediaContentDescription* video_desc =
1825 cricket::GetFirstVideoContentDescription(offer->description());
1826 EXPECT_TRUE(video_desc == nullptr);
1827}
1828
deadbeefbd7d8f72015-12-18 16:58:44 -08001829// Test creating a sender with a stream ID, and ensure the ID is populated
1830// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001831// Don't run under Unified Plan since the stream API is not available.
1832TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001833 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001834 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001835
kwibergd1fe2812016-04-27 06:47:29 -07001836 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001837 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001838
1839 const cricket::MediaContentDescription* video_desc =
1840 cricket::GetFirstVideoContentDescription(offer->description());
1841 ASSERT_TRUE(video_desc != nullptr);
1842 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001843 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001844}
1845
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001847TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001849 ASSERT_LT(0u, pc_->GetSenders().size());
1850 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001851 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001852 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 EXPECT_TRUE(DoGetStats(remote_audio));
1854
1855 // Remove the stream. Since we are sending to our selves the local
1856 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001857 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 // Do a re-negotiation.
1859 CreateOfferReceiveAnswer();
1860
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 // Test that we still can get statistics for the old track. Even if it is not
1862 // sent any longer.
1863 EXPECT_TRUE(DoGetStats(remote_audio));
1864}
1865
1866// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001867TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001869 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1870 ASSERT_TRUE(video_receiver);
1871 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872}
1873
1874// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001875TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 pc_factory_->CreateAudioTrack("unknown track", NULL));
1879 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1880}
1881
Steve Anton36da6ff2018-02-16 16:04:20 -08001882TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001883 CreatePeerConnectionWithoutDtls();
1884 EXPECT_TRUE(DoGetRTCStats());
1885 // Clearing stats cache is needed now, but should be temporary.
1886 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1887 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001888 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1889 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001890 EXPECT_TRUE(DoGetRTCStats());
1891 pc_->ClearStatsCache();
1892 CreateOfferReceiveAnswer();
1893 EXPECT_TRUE(DoGetRTCStats());
1894}
1895
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001897TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001898 RTCConfiguration config;
1899 config.enable_rtp_data_channel = true;
1900 config.enable_dtls_srtp = false;
1901 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001902 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001904 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 pc_->CreateDataChannel("test2", NULL);
1906 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001907 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001909 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 new MockDataChannelObserver(data2));
1911
1912 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1913 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1914 std::string data_to_send1 = "testing testing";
1915 std::string data_to_send2 = "testing something else";
1916 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1917
1918 CreateOfferReceiveAnswer();
1919 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1920 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1921
1922 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1923 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1924 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1925 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1926
1927 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1928 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1929
1930 data1->Close();
1931 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1932 CreateOfferReceiveAnswer();
1933 EXPECT_FALSE(observer1->IsOpen());
1934 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1935 EXPECT_TRUE(observer2->IsOpen());
1936
1937 data_to_send2 = "testing something else again";
1938 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1939
1940 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1941}
1942
1943// This test verifies that sendnig binary data over RTP data channels should
1944// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001945TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001946 RTCConfiguration config;
1947 config.enable_rtp_data_channel = true;
1948 config.enable_dtls_srtp = false;
1949 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001950 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001952 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 pc_->CreateDataChannel("test2", NULL);
1954 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001955 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001957 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 new MockDataChannelObserver(data2));
1959
1960 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1961 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1962
1963 CreateOfferReceiveAnswer();
1964 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1965 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1966
1967 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1968 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1969
jbaucheec21bd2016-03-20 06:15:43 -07001970 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1972}
1973
1974// This test setup a RTP data channels in loop back and test that a channel is
1975// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001976TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001977 RTCConfiguration config;
1978 config.enable_rtp_data_channel = true;
1979 config.enable_dtls_srtp = false;
1980 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001981 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001983 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 new MockDataChannelObserver(data1));
1985
1986 CreateOfferReceiveAnswerWithoutSsrc();
1987
1988 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1989
1990 data1->Close();
1991 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1992 CreateOfferReceiveAnswerWithoutSsrc();
1993 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1994 EXPECT_FALSE(observer1->IsOpen());
1995}
1996
1997// This test that if a data channel is added in an answer a receive only channel
1998// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001999TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002000 RTCConfiguration config;
2001 config.enable_rtp_data_channel = true;
2002 config.enable_dtls_srtp = false;
2003
2004 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005
2006 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002007 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 pc_->CreateDataChannel(offer_label, NULL);
2009
2010 CreateOfferAsLocalDescription();
2011
2012 // Replace the data channel label in the offer and apply it as an answer.
2013 std::string receive_label = "answer_channel";
2014 std::string sdp;
2015 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002016 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 CreateAnswerAsRemoteDescription(sdp);
2018
2019 // Verify that a new incoming data channel has been created and that
2020 // it is open but can't we written to.
2021 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2022 DataChannelInterface* received_channel = observer_.last_datachannel_;
2023 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2024 EXPECT_EQ(receive_label, received_channel->label());
2025 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2026
2027 // Verify that the channel we initially offered has been rejected.
2028 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2029
2030 // Do another offer / answer exchange and verify that the data channel is
2031 // opened.
2032 CreateOfferReceiveAnswer();
2033 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2034 kTimeout);
2035}
2036
2037// This test that no data channel is returned if a reliable channel is
2038// requested.
2039// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002040TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002041 RTCConfiguration rtc_config;
2042 rtc_config.enable_rtp_data_channel = true;
2043 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044
2045 std::string label = "test";
2046 webrtc::DataChannelInit config;
2047 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002048 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 pc_->CreateDataChannel(label, &config);
2050 EXPECT_TRUE(channel == NULL);
2051}
2052
deadbeefab9b2d12015-10-14 11:33:11 -07002053// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002054TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002055 RTCConfiguration config;
2056 config.enable_rtp_data_channel = true;
2057 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002058
2059 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002060 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002061 pc_->CreateDataChannel(label, nullptr);
2062 EXPECT_NE(channel, nullptr);
2063
zhihuang9763d562016-08-05 11:14:50 -07002064 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002065 pc_->CreateDataChannel(label, nullptr);
2066 EXPECT_EQ(dup_channel, nullptr);
2067}
2068
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069// This tests that a SCTP data channel is returned using different
2070// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002071TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002072 RTCConfiguration rtc_config;
2073 rtc_config.enable_dtls_srtp = true;
2074 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075
2076 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002077 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 pc_->CreateDataChannel("1", &config);
2079 EXPECT_TRUE(channel != NULL);
2080 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002081 EXPECT_TRUE(observer_.renegotiation_needed_);
2082 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083
2084 config.ordered = false;
2085 channel = pc_->CreateDataChannel("2", &config);
2086 EXPECT_TRUE(channel != NULL);
2087 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002088 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089
2090 config.ordered = true;
2091 config.maxRetransmits = 0;
2092 channel = pc_->CreateDataChannel("3", &config);
2093 EXPECT_TRUE(channel != NULL);
2094 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002095 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002097 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 config.maxRetransmitTime = 0;
2099 channel = pc_->CreateDataChannel("4", &config);
2100 EXPECT_TRUE(channel != NULL);
2101 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002102 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103}
2104
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002105// For backwards compatibility, we want people who "unset" maxRetransmits
2106// and maxRetransmitTime by setting them to -1 to get what they want.
2107TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
2108 RTCConfiguration rtc_config;
2109 rtc_config.enable_dtls_srtp = true;
2110 CreatePeerConnection(rtc_config);
2111
2112 webrtc::DataChannelInit config;
2113 config.maxRetransmitTime = -1;
2114 config.maxRetransmits = -1;
2115 rtc::scoped_refptr<DataChannelInterface> channel =
2116 pc_->CreateDataChannel("1", &config);
2117 EXPECT_TRUE(channel != NULL);
2118}
2119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120// This tests that no data channel is returned if both maxRetransmits and
2121// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002122TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002124 RTCConfiguration rtc_config;
2125 rtc_config.enable_dtls_srtp = true;
2126 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127
2128 std::string label = "test";
2129 webrtc::DataChannelInit config;
2130 config.maxRetransmits = 0;
2131 config.maxRetransmitTime = 0;
2132
zhihuang9763d562016-08-05 11:14:50 -07002133 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134 pc_->CreateDataChannel(label, &config);
2135 EXPECT_TRUE(channel == NULL);
2136}
2137
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138// The test verifies that creating a SCTP data channel with an id already in use
2139// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002140TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002142 RTCConfiguration rtc_config;
2143 rtc_config.enable_dtls_srtp = true;
2144 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
2146 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002147 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002149 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002150 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002151 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 EXPECT_TRUE(channel != NULL);
2153 EXPECT_EQ(1, channel->id());
2154
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 channel = pc_->CreateDataChannel("x", &config);
2156 EXPECT_TRUE(channel == NULL);
2157
2158 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002159 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 channel = pc_->CreateDataChannel("max", &config);
2161 EXPECT_TRUE(channel != NULL);
2162 EXPECT_EQ(config.id, channel->id());
2163
2164 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002165 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 channel = pc_->CreateDataChannel("x", &config);
2167 EXPECT_TRUE(channel == NULL);
2168}
2169
deadbeefab9b2d12015-10-14 11:33:11 -07002170// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002171TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002172 RTCConfiguration rtc_config;
2173 rtc_config.enable_dtls_srtp = true;
2174 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002175
2176 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002177 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002178 pc_->CreateDataChannel(label, nullptr);
2179 EXPECT_NE(channel, nullptr);
2180
zhihuang9763d562016-08-05 11:14:50 -07002181 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002182 pc_->CreateDataChannel(label, nullptr);
2183 EXPECT_NE(dup_channel, nullptr);
2184}
2185
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002186// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2187// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002188TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002189 RTCConfiguration rtc_config;
2190 rtc_config.enable_rtp_data_channel = true;
2191 rtc_config.enable_dtls_srtp = false;
2192 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002193
zhihuang9763d562016-08-05 11:14:50 -07002194 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002195 pc_->CreateDataChannel("test1", NULL);
2196 EXPECT_TRUE(observer_.renegotiation_needed_);
2197 observer_.renegotiation_needed_ = false;
2198
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002199 CreateOfferReceiveAnswer();
2200
zhihuang9763d562016-08-05 11:14:50 -07002201 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002202 pc_->CreateDataChannel("test2", NULL);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002203 EXPECT_EQ(observer_.renegotiation_needed_,
2204 GetParam() == SdpSemantics::kPlanB);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002205}
2206
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002208TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002209 RTCConfiguration rtc_config;
2210 rtc_config.enable_rtp_data_channel = true;
2211 rtc_config.enable_dtls_srtp = false;
2212 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213
zhihuang9763d562016-08-05 11:14:50 -07002214 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002216 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 pc_->CreateDataChannel("test2", NULL);
2218 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002219 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002221 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 new MockDataChannelObserver(data2));
2223
2224 CreateOfferReceiveAnswer();
2225 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2226 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2227
2228 ReleasePeerConnection();
2229 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2230 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2231}
2232
Zhi Huang644fde42018-04-02 19:16:26 -07002233// This tests that RTP data channels can be rejected in an answer.
2234TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002235 RTCConfiguration rtc_config;
2236 rtc_config.enable_rtp_data_channel = true;
2237 rtc_config.enable_dtls_srtp = false;
2238 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239
zhihuang9763d562016-08-05 11:14:50 -07002240 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241 pc_->CreateDataChannel("offer_channel", NULL));
2242
2243 CreateOfferAsLocalDescription();
2244
2245 // Create an answer where the m-line for data channels are rejected.
2246 std::string sdp;
2247 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002248 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002249 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002250 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002252 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 data_info->rejected = true;
2254
Steve Antondb45ca82017-09-11 18:27:34 -07002255 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2257}
2258
Zhi Huang644fde42018-04-02 19:16:26 -07002259#ifdef HAVE_SCTP
2260// This tests that SCTP data channels can be rejected in an answer.
2261TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2262#else
2263TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2264#endif
2265{
Niels Möllerf06f9232018-08-07 12:32:18 +02002266 RTCConfiguration rtc_config;
2267 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002268
2269 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2270 pc_->CreateDataChannel("offer_channel", NULL));
2271
2272 CreateOfferAsLocalDescription();
2273
2274 // Create an answer where the m-line for data channels are rejected.
2275 std::string sdp;
2276 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2277 std::unique_ptr<SessionDescriptionInterface> answer(
2278 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2279 ASSERT_TRUE(answer);
2280 cricket::ContentInfo* data_info =
2281 cricket::GetFirstDataContent(answer->description());
2282 data_info->rejected = true;
2283
2284 DoSetRemoteDescription(std::move(answer));
2285 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2286}
2287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288// Test that we can create a session description from an SDP string from
2289// FireFox, use it as a remote session description, generate an answer and use
2290// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002291TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002292 RTCConfiguration rtc_config;
2293 rtc_config.enable_dtls_srtp = true;
2294 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002295 AddAudioTrack("audio_label");
2296 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002297 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002298 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002299 webrtc::kFireFoxSdpOffer, nullptr));
2300 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 CreateAnswerAsLocalDescription();
2302 ASSERT_TRUE(pc_->local_description() != NULL);
2303 ASSERT_TRUE(pc_->remote_description() != NULL);
2304
2305 const cricket::ContentInfo* content =
2306 cricket::GetFirstAudioContent(pc_->local_description()->description());
2307 ASSERT_TRUE(content != NULL);
2308 EXPECT_FALSE(content->rejected);
2309
2310 content =
2311 cricket::GetFirstVideoContent(pc_->local_description()->description());
2312 ASSERT_TRUE(content != NULL);
2313 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002314#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 content =
2316 cricket::GetFirstDataContent(pc_->local_description()->description());
2317 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002318 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002319#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320}
2321
zhihuangb19012e2017-09-19 13:47:59 -07002322// Test that fallback from DTLS to SDES is not supported.
2323// The fallback was previously supported but was removed to simplify the code
2324// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002325TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002326 RTCConfiguration rtc_config;
2327 rtc_config.enable_dtls_srtp = true;
2328 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002329 // Wait for fake certificate to be generated. Previously, this is what caused
2330 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002331 AddAudioTrack("audio_label");
2332 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002333 ASSERT_NE(nullptr, fake_certificate_generator_);
2334 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2335 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002336 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002337 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2338 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002339 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002340}
2341
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342// Test that we can create an audio only offer and receive an answer with a
2343// limited set of audio codecs and receive an updated offer with more audio
2344// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002345TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002346 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002347 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 CreateOfferAsLocalDescription();
2349
Steve Anton36da6ff2018-02-16 16:04:20 -08002350 const char* answer_sdp =
2351 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2352 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002353 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002354 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002355 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356
Steve Anton36da6ff2018-02-16 16:04:20 -08002357 const char* reoffer_sdp =
2358 (sdp_semantics_ == SdpSemantics::kPlanB
2359 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2360 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002361 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002362 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002363 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 CreateAnswerAsLocalDescription();
2365}
2366
deadbeefc80741f2015-10-22 13:14:45 -07002367// Test that if we're receiving (but not sending) a track, subsequent offers
2368// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002369TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002370 RTCConfiguration rtc_config;
2371 rtc_config.enable_dtls_srtp = true;
2372 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002373 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002374 CreateAnswerAsLocalDescription();
2375
2376 // At this point we should be receiving stream 1, but not sending anything.
2377 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002378 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002379 DoCreateOffer(&offer, nullptr);
2380
2381 const cricket::ContentInfo* video_content =
2382 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002383 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2384 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002385
2386 const cricket::ContentInfo* audio_content =
2387 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002388 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2389 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002390}
2391
2392// Test that if we're receiving (but not sending) a track, and the
2393// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2394// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002395TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002396 RTCConfiguration rtc_config;
2397 rtc_config.enable_dtls_srtp = true;
2398 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002399 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002400 CreateAnswerAsLocalDescription();
2401
2402 // At this point we should be receiving stream 1, but not sending anything.
2403 // A new offer would be recvonly, but we'll set the "no receive" constraints
2404 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002405 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002406 RTCOfferAnswerOptions options;
2407 options.offer_to_receive_audio = 0;
2408 options.offer_to_receive_video = 0;
2409 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002410
2411 const cricket::ContentInfo* video_content =
2412 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002413 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2414 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002415
2416 const cricket::ContentInfo* audio_content =
2417 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002418 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2419 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002420}
2421
deadbeef653b8e02015-11-11 12:55:10 -08002422// Test that we can use SetConfiguration to change the ICE servers of the
2423// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002424TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002425 CreatePeerConnection();
2426
Steve Anton36da6ff2018-02-16 16:04:20 -08002427 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002428 PeerConnectionInterface::IceServer server;
2429 server.uri = "stun:test_hostname";
2430 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002431 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002432
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002433 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2434 EXPECT_EQ("test_hostname",
2435 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002436}
2437
Steve Anton36da6ff2018-02-16 16:04:20 -08002438TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002439 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002440 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002441 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002442 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002443 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2444}
2445
Steve Anton36da6ff2018-02-16 16:04:20 -08002446TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002447 PeerConnectionInterface::RTCConfiguration config;
2448 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002449 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002450 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002451 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002452
2453 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002454 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002455 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2456 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002457}
2458
skvladd1f5fda2017-02-03 16:54:05 -08002459// Test that the ice check interval can be changed. This does not verify that
2460// the setting makes it all the way to P2PTransportChannel, as that would
2461// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002462TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002463 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002464 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002465 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002466 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002467 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002468 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002469 config = pc_->GetConfiguration();
2470 EXPECT_EQ(config.ice_check_min_interval, 100);
2471}
2472
2473TEST_P(PeerConnectionInterfaceTest,
2474 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2475 PeerConnectionInterface::RTCConfiguration config;
2476 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2477 CreatePeerConnection(config);
2478 config = pc_->GetConfiguration();
2479 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2480
2481 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002482 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002483 config = pc_->GetConfiguration();
2484 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002485}
2486
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002487// Test that when SetConfiguration changes both the pool size and other
2488// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002489TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002490 SetConfigurationCreatesPooledSessionCorrectly) {
2491 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002492 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002493 config.ice_candidate_pool_size = 1;
2494 PeerConnectionInterface::IceServer server;
2495 server.uri = kStunAddressOnly;
2496 config.servers.push_back(server);
2497 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002498 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002499
2500 const cricket::FakePortAllocatorSession* session =
2501 static_cast<const cricket::FakePortAllocatorSession*>(
2502 port_allocator_->GetPooledSession());
2503 ASSERT_NE(nullptr, session);
2504 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002505}
2506
deadbeef293e9262017-01-11 12:28:30 -08002507// Test that after SetLocalDescription, changing the pool size is not allowed,
2508// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002509TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002510 CantChangePoolSizeAfterSetLocalDescription) {
2511 CreatePeerConnection();
2512 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002513 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002514 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002515 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002516
2517 // Set remote offer; can still change pool size at this point.
2518 CreateOfferAsRemoteDescription();
2519 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002520 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002521
2522 // Set local answer; now it's too late.
2523 CreateAnswerAsLocalDescription();
2524 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002525 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002526 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2527}
2528
deadbeef42a42632017-03-10 15:18:00 -08002529// Test that after setting an answer, extra pooled sessions are discarded. The
2530// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002531TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002532 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2533 CreatePeerConnection();
2534
2535 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002536 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002537 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002538 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002539
2540 // Do offer/answer.
2541 CreateOfferAsRemoteDescription();
2542 CreateAnswerAsLocalDescription();
2543
2544 // Expect no pooled sessions to be left.
2545 const cricket::PortAllocatorSession* session =
2546 port_allocator_->GetPooledSession();
2547 EXPECT_EQ(nullptr, session);
2548}
2549
2550// After Close is called, pooled candidates should be discarded so as to not
2551// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002552TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002553 CreatePeerConnection();
2554
Steve Anton36da6ff2018-02-16 16:04:20 -08002555 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002556 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002557 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002558 pc_->Close();
2559
2560 // Expect no pooled sessions to be left.
2561 const cricket::PortAllocatorSession* session =
2562 port_allocator_->GetPooledSession();
2563 EXPECT_EQ(nullptr, session);
2564}
2565
deadbeef293e9262017-01-11 12:28:30 -08002566// Test that SetConfiguration returns an invalid modification error if
2567// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002568TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002569 SetConfigurationReturnsInvalidModificationError) {
2570 PeerConnectionInterface::RTCConfiguration config;
2571 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2572 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2573 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002574 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002575
Steve Anton36da6ff2018-02-16 16:04:20 -08002576 PeerConnectionInterface::RTCConfiguration modified_config =
2577 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002578 modified_config.bundle_policy =
2579 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002580 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002581 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2582
Steve Anton36da6ff2018-02-16 16:04:20 -08002583 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002584 modified_config.rtcp_mux_policy =
2585 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002586 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002587 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2588
Steve Anton36da6ff2018-02-16 16:04:20 -08002589 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002590 modified_config.continual_gathering_policy =
2591 PeerConnectionInterface::GATHER_CONTINUALLY;
Niels Möller2579f0c2019-08-19 09:58:17 +02002592 error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002593 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2594}
2595
2596// Test that SetConfiguration returns a range error if the candidate pool size
2597// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002598TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002599 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2600 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002601 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002602 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002603
2604 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002605 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002606 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2607
2608 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002609 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002610 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2611}
2612
2613// Test that SetConfiguration returns a syntax error if parsing an ICE server
2614// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002615TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002616 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2617 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002618 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002619 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002620
2621 PeerConnectionInterface::IceServer bad_server;
2622 bad_server.uri = "stunn:www.example.com";
2623 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002624 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002625 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2626}
2627
2628// Test that SetConfiguration returns an invalid parameter error if a TURN
2629// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002630TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002631 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2632 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002633 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002634 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002635
2636 PeerConnectionInterface::IceServer bad_server;
2637 bad_server.uri = "turn:www.example.com";
2638 // Missing password.
2639 bad_server.username = "foo";
2640 config.servers.push_back(bad_server);
2641 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002642 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2643 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002644}
2645
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002646// Test that PeerConnection::Close changes the states to closed and all remote
2647// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002648TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649 // Initialize a PeerConnection and negotiate local and remote session
2650 // description.
2651 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002652
2653 // With Plan B, verify the stream count. The analog with Unified Plan is the
2654 // RtpTransceiver count.
2655 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2656 ASSERT_EQ(1u, pc_->local_streams()->count());
2657 ASSERT_EQ(1u, pc_->remote_streams()->count());
2658 } else {
2659 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2660 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002661
2662 pc_->Close();
2663
2664 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2665 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2666 pc_->ice_connection_state());
2667 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2668 pc_->ice_gathering_state());
2669
Steve Anton36da6ff2018-02-16 16:04:20 -08002670 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2671 EXPECT_EQ(1u, pc_->local_streams()->count());
2672 EXPECT_EQ(1u, pc_->remote_streams()->count());
2673 } else {
Harald Alvestrand936f1af2020-09-22 07:41:50 +00002674 // Verify that the RtpTransceivers are still returned.
2675 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Steve Anton36da6ff2018-02-16 16:04:20 -08002676 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002677
Steve Anton36da6ff2018-02-16 16:04:20 -08002678 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
Steve Anton36da6ff2018-02-16 16:04:20 -08002679 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02002680 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2681 ASSERT_TRUE(audio_receiver);
2682 ASSERT_TRUE(video_receiver);
2683 // Track state may be updated asynchronously.
2684 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2685 audio_receiver->track()->state(), kTimeout);
2686 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2687 video_receiver->track()->state(), kTimeout);
2688 } else {
2689 ASSERT_FALSE(audio_receiver);
2690 ASSERT_FALSE(video_receiver);
2691 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692}
2693
2694// Test that PeerConnection methods fails gracefully after
2695// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002696// Don't run under Unified Plan since the stream API is not available.
2697TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002698 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002699 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 CreateOfferAsRemoteDescription();
2701 CreateAnswerAsLocalDescription();
2702
2703 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002704 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002705 pc_->local_streams()->at(0);
2706
2707 pc_->Close();
2708
2709 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002710 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002712 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2713
2714 EXPECT_TRUE(pc_->local_description() != NULL);
2715 EXPECT_TRUE(pc_->remote_description() != NULL);
2716
kwibergd1fe2812016-04-27 06:47:29 -07002717 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002718 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002719 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002720 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721
2722 std::string sdp;
2723 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002724 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002725 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002726 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002727
2728 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002729 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002730 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002731 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002732}
2733
2734// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002735TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736 InitiateCall();
2737 pc_->Close();
2738 DoGetStats(NULL);
2739}
deadbeefab9b2d12015-10-14 11:33:11 -07002740
2741// NOTE: The series of tests below come from what used to be
2742// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2743// setting a remote or local description has the expected effects.
2744
2745// This test verifies that the remote MediaStreams corresponding to a received
2746// SDP string is created. In this test the two separate MediaStreams are
2747// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002748TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002749 RTCConfiguration config;
2750 config.enable_dtls_srtp = true;
2751 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002752 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002753
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002754 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002755 EXPECT_TRUE(
2756 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2757 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2758 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2759
2760 // Create a session description based on another SDP with another
2761 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002762 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002763
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002764 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002765 EXPECT_TRUE(
2766 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2767}
2768
2769// This test verifies that when remote tracks are added/removed from SDP, the
2770// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002771// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2772// specific behavior.
2773TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002774 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002775 RTCConfiguration config;
2776 config.enable_dtls_srtp = true;
2777 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002778 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002779 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002780 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002781 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2782 reference_collection_));
2783
2784 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002785 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002786 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002787 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002788 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2789 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002790 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002791 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2792 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002793 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002794 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2795 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002796
2797 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002798 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002799 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002800 MockTrackObserver audio_track_observer(audio_track2);
2801 MockTrackObserver video_track_observer(video_track2);
2802
2803 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2804 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002805 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002806 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2807 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002808 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002809 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002810 audio_track2->state(), kTimeout);
2811 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2812 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002813}
2814
2815// This tests that remote tracks are ended if a local session description is set
2816// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002817TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002818 RTCConfiguration config;
2819 config.enable_dtls_srtp = true;
2820 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002821 // First create and set a remote offer, then reject its video content in our
2822 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002823 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2824 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2825 ASSERT_TRUE(audio_receiver);
2826 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2827 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002828
Steve Anton36da6ff2018-02-16 16:04:20 -08002829 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2830 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002831 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002832 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2833 video_receiver->track();
2834 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002835
kwibergd1fe2812016-04-27 06:47:29 -07002836 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002837 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002838 cricket::ContentInfo* video_info =
2839 local_answer->description()->GetContentByName("video");
2840 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002841 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002842 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2843 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002844
2845 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002846 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002847 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002848 video_info = local_offer->description()->GetContentByName("video");
2849 ASSERT_TRUE(video_info != nullptr);
2850 video_info->rejected = true;
2851 cricket::ContentInfo* audio_info =
2852 local_offer->description()->GetContentByName("audio");
2853 ASSERT_TRUE(audio_info != nullptr);
2854 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002855 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002856 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002857 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2858 kTimeout);
2859 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2860 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002861}
2862
2863// This tests that we won't crash if the remote track has been removed outside
2864// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002865// Don't run under Unified Plan since the stream API is not available.
2866TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002867 RTCConfiguration config;
2868 config.enable_dtls_srtp = true;
2869 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002870 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002871 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2872 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2873 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2874
kwibergd1fe2812016-04-27 06:47:29 -07002875 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002876 webrtc::CreateSessionDescription(SdpType::kAnswer,
2877 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002878 cricket::ContentInfo* video_info =
2879 local_answer->description()->GetContentByName("video");
2880 video_info->rejected = true;
2881 cricket::ContentInfo* audio_info =
2882 local_answer->description()->GetContentByName("audio");
2883 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002884 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002885
2886 // No crash is a pass.
2887}
2888
deadbeef5e97fb52015-10-15 12:49:08 -07002889// This tests that if a recvonly remote description is set, no remote streams
2890// will be created, even if the description contains SSRCs/MSIDs.
2891// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002892TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002893 RTCConfiguration config;
2894 config.enable_dtls_srtp = true;
2895 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002896
Steve Anton36da6ff2018-02-16 16:04:20 -08002897 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002898 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002899 CreateAndSetRemoteOffer(recvonly_offer);
2900
2901 EXPECT_EQ(0u, observer_.remote_streams()->count());
2902}
2903
deadbeefab9b2d12015-10-14 11:33:11 -07002904// This tests that a default MediaStream is created if a remote session
2905// description doesn't contain any streams and no MSID support.
2906// It also tests that the default stream is updated if a video m-line is added
2907// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002908// Don't run under Unified Plan since this behavior is Plan B specific.
2909TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002910 RTCConfiguration config;
2911 config.enable_dtls_srtp = true;
2912 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002913 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2914
2915 ASSERT_EQ(1u, observer_.remote_streams()->count());
2916 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2917
2918 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2919 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002920 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002921
2922 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2923 ASSERT_EQ(1u, observer_.remote_streams()->count());
2924 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2925 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002926 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2927 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002928 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2929 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002930 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2931 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002932}
2933
2934// This tests that a default MediaStream is created if a remote session
2935// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002936// Don't run under Unified Plan since this behavior is Plan B specific.
2937TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002938 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002939 RTCConfiguration config;
2940 config.enable_dtls_srtp = true;
2941 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002942 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2943
2944 ASSERT_EQ(1u, observer_.remote_streams()->count());
2945 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2946
2947 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2948 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002949 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002950}
2951
2952// This tests that it won't crash when PeerConnection tries to remove
2953// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002954// Don't run under Unified Plan since this behavior is Plan B specific.
2955TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002956 RTCConfiguration config;
2957 config.enable_dtls_srtp = true;
2958 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002959 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002960 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2961 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2962 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2963
2964 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2965
2966 // No crash is a pass.
2967}
2968
2969// This tests that a default MediaStream is created if the remote session
2970// description doesn't contain any streams and don't contain an indication if
2971// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002972// Don't run under Unified Plan since this behavior is Plan B specific.
2973TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002974 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002975 RTCConfiguration config;
2976 config.enable_dtls_srtp = true;
2977 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002978 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2979
2980 ASSERT_EQ(1u, observer_.remote_streams()->count());
2981 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2982 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2983 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2984}
2985
2986// This tests that a default MediaStream is not created if the remote session
2987// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002988// Don't run under Unified Plan since this behavior is Plan B specific.
2989TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002990 RTCConfiguration config;
2991 config.enable_dtls_srtp = true;
2992 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002993 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2994 EXPECT_EQ(0u, observer_.remote_streams()->count());
2995}
2996
deadbeefbda7e0b2015-12-08 17:13:40 -08002997// This tests that when setting a new description, the old default tracks are
2998// not destroyed and recreated.
2999// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08003000// Don't run under Unified Plan since this behavior is Plan B specific.
3001TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003002 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003003 RTCConfiguration config;
3004 config.enable_dtls_srtp = true;
3005 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08003006 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3007
3008 ASSERT_EQ(1u, observer_.remote_streams()->count());
3009 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3010 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3011
3012 // Set the track to "disabled", then set a new description and ensure the
3013 // track is still disabled, which ensures it hasn't been recreated.
3014 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3015 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3016 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3017 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3018}
3019
deadbeefab9b2d12015-10-14 11:33:11 -07003020// This tests that a default MediaStream is not created if a remote session
3021// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003022// Don't run under Unified Plan since this behavior is Plan B specific.
3023TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003024 RTCConfiguration config;
3025 config.enable_dtls_srtp = true;
3026 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003027 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003028 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003029 EXPECT_TRUE(
3030 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3031
3032 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3033 EXPECT_EQ(0u, observer_.remote_streams()->count());
3034}
3035
Seth Hampson5897a6e2018-04-03 11:16:33 -07003036// This tests that a default MediaStream is created if a remote SDP comes from
3037// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3038TEST_F(PeerConnectionInterfaceTestPlanB,
3039 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003040 RTCConfiguration config;
3041 config.enable_dtls_srtp = true;
3042 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003043 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3044 // Add a=msid lines to simulate a Unified Plan endpoint that only
3045 // signals stream IDs with a=msid lines.
3046 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3047
3048 CreateAndSetRemoteOffer(sdp_string);
3049
3050 ASSERT_EQ(1u, observer_.remote_streams()->count());
3051 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3052 EXPECT_EQ("default", remote_stream->id());
3053 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3054}
3055
Seth Hampson5b4f0752018-04-02 16:31:36 -07003056// This tests that when a Plan B endpoint receives an SDP that signals no media
3057// stream IDs indicated by the special character "-" in the a=msid line, that
3058// a default stream ID will be used for the MediaStream ID. This can occur
3059// when a Unified Plan endpoint signals no media stream IDs, but signals both
3060// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3061TEST_F(PeerConnectionInterfaceTestPlanB,
3062 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003063 RTCConfiguration config;
3064 config.enable_dtls_srtp = true;
3065 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003066 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3067 // the sender's stream ID will be interpreted as no stream IDs.
3068 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3069 sdp_string.append("a=msid:- audiotrack0\n");
3070
3071 CreateAndSetRemoteOffer(sdp_string);
3072
3073 ASSERT_EQ(1u, observer_.remote_streams()->count());
3074 // Because SSRCs are signaled the track ID will be what was signaled in the
3075 // a=msid line.
3076 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3077 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3078 EXPECT_EQ("default", remote_stream->id());
3079 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003080
3081 // Previously a bug ocurred when setting the remote description a second time.
3082 // This is because we checked equality of the remote StreamParams stream ID
3083 // (empty), and the previously set stream ID for the remote sender
3084 // ("default"). This cause a track to be removed, then added, when really
3085 // nothing should occur because it is the same track.
3086 CreateAndSetRemoteOffer(sdp_string);
3087 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3088 EXPECT_EQ(1u, observer_.add_track_events_.size());
3089 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3090 remote_stream = observer_.remote_streams()->at(0);
3091 EXPECT_EQ("default", remote_stream->id());
3092 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003093}
3094
deadbeefab9b2d12015-10-14 11:33:11 -07003095// This tests that an RtpSender is created when the local description is set
3096// after adding a local stream.
3097// TODO(deadbeef): This test and the one below it need to be updated when
3098// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003099// Don't run under Unified Plan since this behavior is Plan B specific.
3100TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003101 RTCConfiguration config;
3102 config.enable_dtls_srtp = true;
3103 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003104
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003105 // Create an offer with 1 stream with 2 tracks of each type.
3106 rtc::scoped_refptr<StreamCollection> stream_collection =
3107 CreateStreamCollection(1, 2);
3108 pc_->AddStream(stream_collection->at(0));
3109 std::unique_ptr<SessionDescriptionInterface> offer;
3110 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003111 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003112
deadbeefab9b2d12015-10-14 11:33:11 -07003113 auto senders = pc_->GetSenders();
3114 EXPECT_EQ(4u, senders.size());
3115 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3116 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3117 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3118 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3119
3120 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003121 pc_->RemoveStream(stream_collection->at(0));
3122 stream_collection = CreateStreamCollection(1, 1);
3123 pc_->AddStream(stream_collection->at(0));
3124 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003125 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003126
deadbeefab9b2d12015-10-14 11:33:11 -07003127 senders = pc_->GetSenders();
3128 EXPECT_EQ(2u, senders.size());
3129 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3130 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3131 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3132 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3133}
3134
3135// This tests that an RtpSender is created when the local description is set
3136// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003137// Don't run under Unified Plan since this behavior is Plan B specific.
3138TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003139 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003140 RTCConfiguration config;
3141 config.enable_dtls_srtp = true;
3142 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003143
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003144 rtc::scoped_refptr<StreamCollection> stream_collection =
3145 CreateStreamCollection(1, 2);
3146 // Add a stream to create the offer, but remove it afterwards.
3147 pc_->AddStream(stream_collection->at(0));
3148 std::unique_ptr<SessionDescriptionInterface> offer;
3149 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3150 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003151
Steve Antondb45ca82017-09-11 18:27:34 -07003152 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003153 auto senders = pc_->GetSenders();
3154 EXPECT_EQ(0u, senders.size());
3155
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003156 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003157 senders = pc_->GetSenders();
3158 EXPECT_EQ(4u, senders.size());
3159 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3160 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3161 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3162 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3163}
3164
3165// This tests that the expected behavior occurs if the SSRC on a local track is
3166// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003167TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003168 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003169 RTCConfiguration config;
3170 config.enable_dtls_srtp = true;
3171 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003172
Steve Anton36da6ff2018-02-16 16:04:20 -08003173 AddAudioTrack(kAudioTracks[0]);
3174 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003175 std::unique_ptr<SessionDescriptionInterface> offer;
3176 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3177 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003178 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3179 webrtc::CreateSessionDescription(
3180 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02003181 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07003182 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003183
deadbeefab9b2d12015-10-14 11:33:11 -07003184 auto senders = pc_->GetSenders();
3185 EXPECT_EQ(2u, senders.size());
3186 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3187 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3188
3189 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003190 cricket::MediaContentDescription* desc =
3191 cricket::GetFirstAudioContentDescription(modified_offer->description());
3192 ASSERT_TRUE(desc != NULL);
3193 for (StreamParams& stream : desc->mutable_streams()) {
3194 for (unsigned int& ssrc : stream.ssrcs) {
3195 ++ssrc;
3196 }
3197 }
deadbeefab9b2d12015-10-14 11:33:11 -07003198
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003199 desc =
3200 cricket::GetFirstVideoContentDescription(modified_offer->description());
3201 ASSERT_TRUE(desc != NULL);
3202 for (StreamParams& stream : desc->mutable_streams()) {
3203 for (unsigned int& ssrc : stream.ssrcs) {
3204 ++ssrc;
3205 }
3206 }
3207
Steve Antondb45ca82017-09-11 18:27:34 -07003208 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003209 senders = pc_->GetSenders();
3210 EXPECT_EQ(2u, senders.size());
3211 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3212 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3213 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3214 // changed.
3215}
3216
3217// This tests that the expected behavior occurs if a new session description is
3218// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003219// Don't run under Unified Plan since the stream API is not available.
3220TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003221 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003222 RTCConfiguration config;
3223 config.enable_dtls_srtp = true;
3224 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003225
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003226 rtc::scoped_refptr<StreamCollection> stream_collection =
3227 CreateStreamCollection(2, 1);
3228 pc_->AddStream(stream_collection->at(0));
3229 std::unique_ptr<SessionDescriptionInterface> offer;
3230 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003231 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003232
deadbeefab9b2d12015-10-14 11:33:11 -07003233 auto senders = pc_->GetSenders();
3234 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003235 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3236 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003237
3238 // Add a new MediaStream but with the same tracks as in the first stream.
3239 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3240 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003241 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3242 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003243 pc_->AddStream(stream_1);
3244
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003245 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003246 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003247
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003248 auto new_senders = pc_->GetSenders();
3249 // Should be the same senders as before, but with updated stream id.
3250 // Note that this behavior is subject to change in the future.
3251 // We may decide the PC should ignore existing tracks in AddStream.
3252 EXPECT_EQ(senders, new_senders);
3253 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3254 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003255}
3256
zhihuang81c3a032016-11-17 12:06:24 -08003257// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003258TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003259 RTCConfiguration config;
3260 config.enable_dtls_srtp = true;
3261 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003262 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3263 EXPECT_EQ(observer_.num_added_tracks_, 1);
3264 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3265
3266 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003267 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003268 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003269 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3270}
3271
deadbeefd1a38b52016-12-10 13:15:33 -08003272// Test that when SetConfiguration is called and the configuration is
3273// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003274TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003275 PeerConnectionInterface::RTCConfiguration config;
3276 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003277 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003278 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003279 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3280 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003281
3282 // Do initial offer/answer so there's something to restart.
3283 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003284 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003285
3286 // Grab the ufrags.
3287 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3288
3289 // Change ICE policy, which should trigger an ICE restart on the next offer.
3290 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003291 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003292 CreateOfferAsLocalDescription();
3293
3294 // Grab the new ufrags.
3295 std::vector<std::string> subsequent_ufrags =
3296 GetUfrags(pc_->local_description());
3297
3298 // Sanity check.
3299 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3300 // Check that each ufrag is different.
3301 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3302 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3303 }
3304}
3305
3306// Test that when SetConfiguration is called and the configuration *isn't*
3307// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003308TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003309 PeerConnectionInterface::RTCConfiguration config;
3310 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003311 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003312 config = pc_->GetConfiguration();
3313 AddAudioTrack(kAudioTracks[0]);
3314 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003315
3316 // Do initial offer/answer so there's something to restart.
3317 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003318 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003319
3320 // Grab the ufrags.
3321 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3322
3323 // Call SetConfiguration with a config identical to what the PC was
3324 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003325 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003326 CreateOfferAsLocalDescription();
3327
3328 // Grab the new ufrags.
3329 std::vector<std::string> subsequent_ufrags =
3330 GetUfrags(pc_->local_description());
3331
3332 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3333}
3334
3335// Test for a weird corner case scenario:
3336// 1. Audio/video session established.
3337// 2. SetConfiguration changes ICE config; ICE restart needed.
3338// 3. ICE restart initiated by remote peer, but only for one m= section.
3339// 4. Next createOffer should initiate an ICE restart, but only for the other
3340// m= section; it would be pointless to do an ICE restart for the m= section
3341// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003342TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003343 PeerConnectionInterface::RTCConfiguration config;
3344 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003345 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003346 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003347 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3348 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003349
3350 // Do initial offer/answer so there's something to restart.
3351 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003352 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003353
3354 // Change ICE policy, which should set the "needs-ice-restart" flag.
3355 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003356 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003357
3358 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003359 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003360 webrtc::CreateSessionDescription(SdpType::kOffer,
3361 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003362 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003363 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3364 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003365 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003366 CreateAnswerAsLocalDescription();
3367
3368 // Grab the ufrags.
3369 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003370 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003371
3372 // Create offer and grab the new ufrags.
3373 CreateOfferAsLocalDescription();
3374 std::vector<std::string> subsequent_ufrags =
3375 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003376 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003377
3378 // Ensure that only the ufrag for the second m= section changed.
3379 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3380 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3381}
3382
deadbeeffe4a8a42016-12-20 17:56:17 -08003383// Tests that the methods to return current/pending descriptions work as
3384// expected at different points in the offer/answer exchange. This test does
3385// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003386TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003387 // This disables DTLS so we can apply an answer to ourselves.
3388 CreatePeerConnection();
3389
3390 // Create initial local offer and get SDP (which will also be used as
3391 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003392 std::unique_ptr<SessionDescriptionInterface> local_offer;
3393 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003394 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003395 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003396
3397 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003398 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3399 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3400 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003401 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3402 EXPECT_EQ(nullptr, pc_->current_local_description());
3403 EXPECT_EQ(nullptr, pc_->current_remote_description());
3404
3405 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003406 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003407 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003408 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3409 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3410 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3411 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003412 EXPECT_EQ(nullptr, pc_->current_local_description());
3413 EXPECT_EQ(nullptr, pc_->current_remote_description());
3414
3415 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003416 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003417 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003418 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3419 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003420 EXPECT_EQ(nullptr, pc_->pending_local_description());
3421 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003422 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3423 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003424
3425 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003426 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003427 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003428 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3429 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3430 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003431 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003432 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3433 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003434
3435 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003436 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003437 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003438 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3439 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3440 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3441 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3442 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3443 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003444
3445 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003446 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003447 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003448 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3449 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003450 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3451 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003452 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3453 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003454}
3455
zhihuang77985012017-02-07 15:45:16 -08003456// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3457// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003458// This version tests the StartRtcEventLog version that receives an object
3459// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003460TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003461 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3462 CreatePeerConnection();
3463 // The RtcEventLog will be reset when the PeerConnection is closed.
3464 pc_->Close();
3465
Niels Möllerdec9f742019-06-03 15:25:20 +02003466 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003467 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003468 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003469 pc_->StopRtcEventLog();
3470}
3471
deadbeef30952b42017-04-21 02:41:29 -07003472// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003473TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003474 CreatePeerConnection();
3475
3476 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003477 RTCOfferAnswerOptions options;
3478 options.offer_to_receive_audio = 1;
3479 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003480 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003481 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003482 cricket::SessionDescription* desc = offer->description();
3483 ASSERT_EQ(2u, desc->transport_infos().size());
3484 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3485 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3486
3487 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003488 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003489 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003490 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3491 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003492 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003493 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003494 desc = answer->description();
3495 ASSERT_EQ(2u, desc->transport_infos().size());
3496 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3497 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3498}
3499
deadbeef1dcb1642017-03-29 21:08:16 -07003500// Test that ICE renomination isn't offered if it's not enabled in the PC's
3501// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003502TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003503 PeerConnectionInterface::RTCConfiguration config;
3504 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003505 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003506 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003507
3508 std::unique_ptr<SessionDescriptionInterface> offer;
3509 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3510 cricket::SessionDescription* desc = offer->description();
3511 EXPECT_EQ(1u, desc->transport_infos().size());
3512 EXPECT_FALSE(
3513 desc->transport_infos()[0].description.GetIceParameters().renomination);
3514}
3515
3516// Test that the ICE renomination option is present in generated offers/answers
3517// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003518TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003519 PeerConnectionInterface::RTCConfiguration config;
3520 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003521 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003522 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003523
3524 std::unique_ptr<SessionDescriptionInterface> offer;
3525 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3526 cricket::SessionDescription* desc = offer->description();
3527 EXPECT_EQ(1u, desc->transport_infos().size());
3528 EXPECT_TRUE(
3529 desc->transport_infos()[0].description.GetIceParameters().renomination);
3530
3531 // Set the offer as a remote description, then create an answer and ensure it
3532 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003533 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003534 std::unique_ptr<SessionDescriptionInterface> answer;
3535 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3536 desc = answer->description();
3537 EXPECT_EQ(1u, desc->transport_infos().size());
3538 EXPECT_TRUE(
3539 desc->transport_infos()[0].description.GetIceParameters().renomination);
3540}
3541
3542// Test that if CreateOffer is called with the deprecated "offer to receive
3543// audio/video" constraints, they're processed and result in an offer with
3544// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003545TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003546 CreatePeerConnection();
3547
Niels Möllerf06f9232018-08-07 12:32:18 +02003548 RTCOfferAnswerOptions options;
3549 options.offer_to_receive_audio = 1;
3550 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003551 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003552 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003553
3554 cricket::SessionDescription* desc = offer->description();
3555 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3556 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3557 ASSERT_NE(nullptr, audio);
3558 ASSERT_NE(nullptr, video);
3559 EXPECT_FALSE(audio->rejected);
3560 EXPECT_FALSE(video->rejected);
3561}
3562
3563// Test that if CreateAnswer is called with the deprecated "offer to receive
3564// audio/video" constraints, they're processed and can be used to reject an
3565// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003566// Don't run under Unified Plan since this behavior is not supported.
3567TEST_F(PeerConnectionInterfaceTestPlanB,
3568 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003569 CreatePeerConnection();
3570
3571 // First, create an offer with audio/video and apply it as a remote
3572 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003573 RTCOfferAnswerOptions options;
3574 options.offer_to_receive_audio = 1;
3575 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003576 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003577 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003578 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003579
3580 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003581 options.offer_to_receive_audio = 0;
3582 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003583 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003584 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003585
3586 cricket::SessionDescription* desc = answer->description();
3587 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3588 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3589 ASSERT_NE(nullptr, audio);
3590 ASSERT_NE(nullptr, video);
3591 EXPECT_TRUE(audio->rejected);
3592 EXPECT_TRUE(video->rejected);
3593}
3594
deadbeef1dcb1642017-03-29 21:08:16 -07003595// Test that negotiation can succeed with a data channel only, and with the max
3596// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003597#ifdef HAVE_SCTP
3598TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3599#else
3600TEST_P(PeerConnectionInterfaceTest,
3601 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3602#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003603 PeerConnectionInterface::RTCConfiguration config;
3604 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003605 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003606
3607 // First, create an offer with only a data channel and apply it as a remote
3608 // description.
3609 pc_->CreateDataChannel("test", nullptr);
3610 std::unique_ptr<SessionDescriptionInterface> offer;
3611 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003612 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003613
3614 // Create and set answer as well.
3615 std::unique_ptr<SessionDescriptionInterface> answer;
3616 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003617 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003618}
3619
Steve Anton36da6ff2018-02-16 16:04:20 -08003620TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003621 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003622 BitrateSettings bitrate;
3623 bitrate.start_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003624 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3625}
3626
Steve Anton36da6ff2018-02-16 16:04:20 -08003627TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003628 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003629 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003630 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003631 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3632}
3633
Steve Anton36da6ff2018-02-16 16:04:20 -08003634TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003635 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003636 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003637 bitrate.min_bitrate_bps = 5;
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003638 bitrate.start_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003639 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3640}
3641
Steve Anton36da6ff2018-02-16 16:04:20 -08003642TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003643 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003644 BitrateSettings bitrate;
3645 bitrate.start_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003646 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3647}
3648
Steve Anton36da6ff2018-02-16 16:04:20 -08003649TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003650 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003651 BitrateSettings bitrate;
3652 bitrate.start_bitrate_bps = 10;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003653 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003654 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3655}
3656
Steve Anton36da6ff2018-02-16 16:04:20 -08003657TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003658 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003659 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003660 bitrate.min_bitrate_bps = 10;
3661 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003662 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3663}
3664
Steve Anton36da6ff2018-02-16 16:04:20 -08003665TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003666 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003667 BitrateSettings bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003668 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003669 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3670}
3671
Niels Möller0c4f7be2018-05-07 14:01:37 +02003672// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003673// by Call's BitrateConstraints, which comes from the SDP or a default value.
3674// This test checks that a call to SetBitrate with a current bitrate that will
3675// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003676TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003677 CreatePeerConnection();
Niels Möller9ad1f6f2020-07-13 10:25:41 +02003678 BitrateSettings bitrate;
3679 bitrate.start_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003680 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3681}
3682
zhihuang1c378ed2017-08-17 14:10:50 -07003683// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003684TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003685 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3686 RTCOfferAnswerOptions rtc_options;
3687
3688 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3689 // than kMaxOfferToReceiveMedia should be treated as invalid.
3690 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3691 CreatePeerConnection();
3692 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3693
3694 rtc_options.offer_to_receive_audio =
3695 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3696 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3697}
3698
Steve Anton36da6ff2018-02-16 16:04:20 -08003699TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003700 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3701 RTCOfferAnswerOptions rtc_options;
3702
3703 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3704 // than kMaxOfferToReceiveMedia should be treated as invalid.
3705 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3706 CreatePeerConnection();
3707 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3708
3709 rtc_options.offer_to_receive_video =
3710 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3711 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3712}
3713
3714// Test that the audio and video content will be added to an offer if both
3715// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003716TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003717 RTCOfferAnswerOptions rtc_options;
3718 rtc_options.offer_to_receive_audio = 1;
3719 rtc_options.offer_to_receive_video = 1;
3720
3721 std::unique_ptr<SessionDescriptionInterface> offer;
3722 CreatePeerConnection();
3723 offer = CreateOfferWithOptions(rtc_options);
3724 ASSERT_TRUE(offer);
3725 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3726 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3727}
3728
3729// Test that only audio content will be added to the offer if only
3730// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003731TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003732 RTCOfferAnswerOptions rtc_options;
3733 rtc_options.offer_to_receive_audio = 1;
3734 rtc_options.offer_to_receive_video = 0;
3735
3736 std::unique_ptr<SessionDescriptionInterface> offer;
3737 CreatePeerConnection();
3738 offer = CreateOfferWithOptions(rtc_options);
3739 ASSERT_TRUE(offer);
3740 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3741 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3742}
3743
3744// Test that only video content will be added if only |offer_to_receive_video|
3745// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003746TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003747 RTCOfferAnswerOptions rtc_options;
3748 rtc_options.offer_to_receive_audio = 0;
3749 rtc_options.offer_to_receive_video = 1;
3750
3751 std::unique_ptr<SessionDescriptionInterface> offer;
3752 CreatePeerConnection();
3753 offer = CreateOfferWithOptions(rtc_options);
3754 ASSERT_TRUE(offer);
3755 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3756 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3757}
3758
zhihuang1c378ed2017-08-17 14:10:50 -07003759// Test that no media content will be added to the offer if using default
3760// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003761TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003762 RTCOfferAnswerOptions rtc_options;
3763
3764 std::unique_ptr<SessionDescriptionInterface> offer;
3765 CreatePeerConnection();
3766 offer = CreateOfferWithOptions(rtc_options);
3767 ASSERT_TRUE(offer);
3768 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3769 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3770}
3771
3772// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3773// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003774TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3775 CreatePeerConnection();
3776
zhihuang1c378ed2017-08-17 14:10:50 -07003777 RTCOfferAnswerOptions rtc_options;
3778 rtc_options.ice_restart = false;
3779 rtc_options.offer_to_receive_audio = 1;
3780
3781 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003782 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003783 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3784 auto ufrag1 =
3785 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3786 auto pwd1 =
3787 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003788
3789 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3790 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003791 auto ufrag2 =
3792 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3793 auto pwd2 =
3794 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003795
3796 // |ice_restart| is true, the ufrag/pwd should change.
3797 rtc_options.ice_restart = true;
3798 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003799 auto ufrag3 =
3800 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3801 auto pwd3 =
3802 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003803
3804 EXPECT_EQ(ufrag1, ufrag2);
3805 EXPECT_EQ(pwd1, pwd2);
3806 EXPECT_NE(ufrag2, ufrag3);
3807 EXPECT_NE(pwd2, pwd3);
3808}
3809
3810// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3811// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003812TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003813 RTCOfferAnswerOptions rtc_options;
3814 rtc_options.offer_to_receive_audio = 1;
3815 rtc_options.offer_to_receive_video = 1;
3816
3817 std::unique_ptr<SessionDescriptionInterface> offer;
3818 CreatePeerConnection();
3819
3820 rtc_options.use_rtp_mux = true;
3821 offer = CreateOfferWithOptions(rtc_options);
3822 ASSERT_TRUE(offer);
3823 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3824 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3825 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3826
3827 rtc_options.use_rtp_mux = false;
3828 offer = CreateOfferWithOptions(rtc_options);
3829 ASSERT_TRUE(offer);
3830 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3831 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3832 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3833}
3834
zhihuang141aacb2017-08-29 13:23:53 -07003835// This test ensures OnRenegotiationNeeded is called when we add track with
3836// MediaStream -> AddTrack in the same way it is called when we add track with
3837// PeerConnection -> AddTrack.
3838// The test can be removed once addStream is rewritten in terms of addTrack
3839// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003840// Don't run under Unified Plan since the stream API is not available.
3841TEST_F(PeerConnectionInterfaceTestPlanB,
3842 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003843 CreatePeerConnectionWithoutDtls();
3844 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003845 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003846 pc_->AddStream(stream);
3847 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003848 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003849 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003850 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003851 stream->AddTrack(audio_track);
3852 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3853 observer_.renegotiation_needed_ = false;
3854
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003855 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003856 stream->AddTrack(video_track);
3857 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3858 observer_.renegotiation_needed_ = false;
3859
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003860 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003861 stream->RemoveTrack(audio_track);
3862 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3863 observer_.renegotiation_needed_ = false;
3864
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003865 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003866 stream->RemoveTrack(video_track);
3867 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3868 observer_.renegotiation_needed_ = false;
3869}
3870
Zhi Huangb2d355e2017-10-26 17:26:37 -07003871// Tests that an error is returned if a description is applied that has fewer
3872// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003873TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003874 MediaSectionCountEnforcedForSubsequentOffer) {
3875 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003876 AddAudioTrack("audio_label");
3877 AddVideoTrack("video_label");
3878
Zhi Huangb2d355e2017-10-26 17:26:37 -07003879 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003880 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003881 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3882
3883 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003884 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003885 offer->description()->contents().pop_back();
3886 offer->description()->contents().pop_back();
3887 ASSERT_TRUE(offer->description()->contents().empty());
3888 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3889
3890 std::unique_ptr<SessionDescriptionInterface> answer;
3891 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3892 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3893
3894 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003895 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003896 offer->description()->contents().pop_back();
3897 offer->description()->contents().pop_back();
3898 ASSERT_TRUE(offer->description()->contents().empty());
3899 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3900}
3901
Johannes Kron89f874e2018-11-12 10:25:48 +01003902TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3903 RTCConfiguration config;
3904 // Default behavior is false.
3905 CreatePeerConnection(config);
3906 std::unique_ptr<SessionDescriptionInterface> offer;
3907 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3908 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
3909 // Possible to set to true.
3910 config.offer_extmap_allow_mixed = true;
3911 CreatePeerConnection(config);
3912 offer.release();
3913 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3914 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3915}
3916
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003917INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3918 PeerConnectionInterfaceTest,
3919 Values(SdpSemantics::kPlanB,
3920 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003921
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003922class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003923 protected:
3924 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003925 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003926 pcf_->Initialize();
3927 }
nisseeaabdf62017-05-05 02:23:02 -07003928 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003929 const RTCConfiguration& config) {
3930 rtc::scoped_refptr<PeerConnectionInterface> pc(
3931 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003932 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003933 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003934 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003935 }
3936
zhihuang9763d562016-08-05 11:14:50 -07003937 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003938 MockPeerConnectionObserver observer_;
3939};
3940
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003941// This sanity check validates the test infrastructure itself.
3942TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3943 PeerConnectionInterface::RTCConfiguration config;
3944 rtc::scoped_refptr<PeerConnectionInterface> pc(
3945 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3946 EXPECT_TRUE(pc.get());
3947 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3948 pc->Close(); // No abort -> ok.
3949 SUCCEED();
3950}
3951
nisse51542be2016-02-12 02:27:06 -08003952// This test verifies the default behaviour with no constraints and a
3953// default RTCConfiguration.
3954TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3955 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003956
Niels Möllerf06f9232018-08-07 12:32:18 +02003957 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003958
3959 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003960 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3961 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003962 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003963 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003964}
3965
Niels Möller1d7ecd22018-01-18 15:25:12 +01003966// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003967// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003968TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3969 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003970
Niels Möller71bdda02016-03-31 12:59:59 +02003971 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003972 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003973
Niels Möller1d7ecd22018-01-18 15:25:12 +01003974 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003975}
3976
Niels Möller6539f692018-01-18 08:58:50 +01003977// This test verifies that the experiment_cpu_load_estimator flag is
3978// propagated from RTCConfiguration to the PeerConnection.
3979TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3980 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003981
3982 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003983 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003984
3985 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3986}
3987
deadbeef293e9262017-01-11 12:28:30 -08003988// Tests a few random fields being different.
3989TEST(RTCConfigurationTest, ComparisonOperators) {
3990 PeerConnectionInterface::RTCConfiguration a;
3991 PeerConnectionInterface::RTCConfiguration b;
3992 EXPECT_EQ(a, b);
3993
3994 PeerConnectionInterface::RTCConfiguration c;
3995 c.servers.push_back(PeerConnectionInterface::IceServer());
3996 EXPECT_NE(a, c);
3997
3998 PeerConnectionInterface::RTCConfiguration d;
3999 d.type = PeerConnectionInterface::kRelay;
4000 EXPECT_NE(a, d);
4001
4002 PeerConnectionInterface::RTCConfiguration e;
4003 e.audio_jitter_buffer_max_packets = 5;
4004 EXPECT_NE(a, e);
4005
4006 PeerConnectionInterface::RTCConfiguration f;
4007 f.ice_connection_receiving_timeout = 1337;
4008 EXPECT_NE(a, f);
4009
4010 PeerConnectionInterface::RTCConfiguration g;
4011 g.disable_ipv6 = true;
4012 EXPECT_NE(a, g);
4013
4014 PeerConnectionInterface::RTCConfiguration h(
4015 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4016 EXPECT_NE(a, h);
4017}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004018
4019} // namespace
4020} // namespace webrtc