blob: b6a8dd43efc231255b3349b416df02fa8b793df7 [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"
Anders Carlsson67537952018-05-03 11:28:29 +020046#include "api/video_codecs/builtin_video_decoder_factory.h"
47#include "api/video_codecs/builtin_video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010048#include "api/video_codecs/video_decoder_factory.h"
49#include "api/video_codecs/video_encoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010050#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080051#include "media/base/media_config.h"
52#include "media/base/media_engine.h"
53#include "media/base/stream_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080054#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020055#include "media/engine/webrtc_media_engine_defaults.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "media/sctp/sctp_transport_internal.h"
Yves Gerey3e707812018-11-28 16:47:49 +010057#include "modules/audio_device/include/audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080059#include "p2p/base/fake_port_allocator.h"
60#include "p2p/base/p2p_constants.h"
Yves Gerey3e707812018-11-28 16:47:49 +010061#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "p2p/base/port_allocator.h"
63#include "p2p/base/transport_description.h"
64#include "p2p/base/transport_info.h"
65#include "pc/audio_track.h"
66#include "pc/media_session.h"
67#include "pc/media_stream.h"
68#include "pc/peer_connection.h"
69#include "pc/peer_connection_factory.h"
70#include "pc/rtc_stats_collector.h"
71#include "pc/rtp_sender.h"
72#include "pc/session_description.h"
73#include "pc/stream_collection.h"
74#include "pc/test/fake_audio_capture_module.h"
75#include "pc/test/fake_rtc_certificate_generator.h"
76#include "pc/test/fake_video_track_source.h"
77#include "pc/test/mock_peer_connection_observers.h"
78#include "pc/test/test_sdp_strings.h"
79#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010080#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080081#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020082#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080083#include "rtc_base/ref_counted_object.h"
84#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080085#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010086#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080087#include "rtc_base/time_utils.h"
88#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020089#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010090#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080091#include "test/testsupport/file_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070092
93#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080094#include "pc/test/android_test_initializer.h"
kwibergac9f8762016-09-30 22:29:43 -070095#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010097namespace webrtc {
98namespace {
99
Seth Hampson845e8782018-03-02 11:34:10 -0800100static const char kStreamId1[] = "local_stream_1";
101static const char kStreamId2[] = "local_stream_2";
102static const char kStreamId3[] = "local_stream_3";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103static const int kDefaultStunPort = 3478;
104static const char kStunAddressOnly[] = "stun:address";
105static const char kStunInvalidPort[] = "stun:address:-1";
106static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
107static const char kStunAddressPortAndMore2[] = "stun:address:port more";
Niels Möllerdb4def92019-03-18 16:53:59 +0100108static const char kTurnIceServerUri[] = "turn:turn.example.org";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109static const char kTurnUsername[] = "user";
110static const char kTurnPassword[] = "password";
111static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200112static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
deadbeefab9b2d12015-10-14 11:33:11 -0700114static const char kStreams[][8] = {"stream1", "stream2"};
115static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
116static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
117
deadbeef5e97fb52015-10-15 12:49:08 -0700118static const char kRecvonly[] = "recvonly";
119static const char kSendrecv[] = "sendrecv";
120
deadbeefab9b2d12015-10-14 11:33:11 -0700121// Reference SDP with a MediaStream with label "stream1" and audio track with
122// id "audio_1" and a video track with id "video_1;
Steve Anton36da6ff2018-02-16 16:04:20 -0800123static const char kSdpStringWithStream1PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800128 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700129 "a=ice-ufrag:e5785931\r\n"
130 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
131 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
132 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800135 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700136 "a=rtpmap:103 ISAC/16000\r\n"
137 "a=ssrc:1 cname:stream1\r\n"
Philipp Hanckee3f257c2020-02-19 16:25:52 +0100138 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800140 "a=ice-ufrag:e5785931\r\n"
141 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
142 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
143 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700145 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800146 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700147 "a=rtpmap:120 VP8/90000\r\n"
148 "a=ssrc:2 cname:stream1\r\n"
Philipp Hanckee3f257c2020-02-19 16:25:52 +0100149 "a=ssrc:2 msid:stream1 videotrack0\r\n";
150// Same string as above but with the MID changed to the Unified Plan default and
151// a=msid added. This is needed so that this SDP can be used as an answer for a
152// Unified Plan offer.
Steve Anton36da6ff2018-02-16 16:04:20 -0800153static const char kSdpStringWithStream1UnifiedPlan[] =
154 "v=0\r\n"
155 "o=- 0 0 IN IP4 127.0.0.1\r\n"
156 "s=-\r\n"
157 "t=0 0\r\n"
158 "m=audio 1 RTP/AVPF 103\r\n"
159 "a=ice-ufrag:e5785931\r\n"
160 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
161 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
162 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
163 "a=mid:0\r\n"
164 "a=sendrecv\r\n"
165 "a=rtcp-mux\r\n"
166 "a=rtpmap:103 ISAC/16000\r\n"
Philipp Hanckee3f257c2020-02-19 16:25:52 +0100167 "a=msid:stream1 audiotrack0\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800168 "a=ssrc:1 cname:stream1\r\n"
Steve Anton36da6ff2018-02-16 16:04:20 -0800169 "m=video 1 RTP/AVPF 120\r\n"
170 "a=ice-ufrag:e5785931\r\n"
171 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
172 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
173 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
174 "a=mid:1\r\n"
175 "a=sendrecv\r\n"
176 "a=rtcp-mux\r\n"
177 "a=rtpmap:120 VP8/90000\r\n"
Philipp Hanckee3f257c2020-02-19 16:25:52 +0100178 "a=msid:stream1 videotrack0\r\n"
179 "a=ssrc:2 cname:stream1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700180
zhihuang81c3a032016-11-17 12:06:24 -0800181// Reference SDP with a MediaStream with label "stream1" and audio track with
182// id "audio_1";
183static const char kSdpStringWithStream1AudioTrackOnly[] =
184 "v=0\r\n"
185 "o=- 0 0 IN IP4 127.0.0.1\r\n"
186 "s=-\r\n"
187 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800188 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800189 "a=ice-ufrag:e5785931\r\n"
190 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
191 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
192 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800193 "a=mid:audio\r\n"
194 "a=sendrecv\r\n"
195 "a=rtpmap:103 ISAC/16000\r\n"
196 "a=ssrc:1 cname:stream1\r\n"
Philipp Hanckee3f257c2020-02-19 16:25:52 +0100197 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800198 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800199
deadbeefab9b2d12015-10-14 11:33:11 -0700200// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
201// MediaStreams have one audio track and one video track.
202// This uses MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -0800203static const char kSdpStringWithStream1And2PlanB[] =
deadbeefab9b2d12015-10-14 11:33:11 -0700204 "v=0\r\n"
205 "o=- 0 0 IN IP4 127.0.0.1\r\n"
206 "s=-\r\n"
207 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800208 "a=msid-semantic: WMS stream1 stream2\r\n"
209 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=ice-ufrag:e5785931\r\n"
211 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
212 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
213 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700214 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700215 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800216 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700217 "a=rtpmap:103 ISAC/16000\r\n"
218 "a=ssrc:1 cname:stream1\r\n"
219 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
220 "a=ssrc:3 cname:stream2\r\n"
221 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
222 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800223 "a=ice-ufrag:e5785931\r\n"
224 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
225 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
226 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700227 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700228 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800229 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700230 "a=rtpmap:120 VP8/0\r\n"
231 "a=ssrc:2 cname:stream1\r\n"
232 "a=ssrc:2 msid:stream1 videotrack0\r\n"
233 "a=ssrc:4 cname:stream2\r\n"
234 "a=ssrc:4 msid:stream2 videotrack1\r\n";
Steve Anton36da6ff2018-02-16 16:04:20 -0800235static const char kSdpStringWithStream1And2UnifiedPlan[] =
236 "v=0\r\n"
237 "o=- 0 0 IN IP4 127.0.0.1\r\n"
238 "s=-\r\n"
239 "t=0 0\r\n"
240 "a=msid-semantic: WMS stream1 stream2\r\n"
241 "m=audio 1 RTP/AVPF 103\r\n"
242 "a=ice-ufrag:e5785931\r\n"
243 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
244 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
245 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
246 "a=mid:0\r\n"
247 "a=sendrecv\r\n"
248 "a=rtcp-mux\r\n"
249 "a=rtpmap:103 ISAC/16000\r\n"
250 "a=ssrc:1 cname:stream1\r\n"
251 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
252 "m=video 1 RTP/AVPF 120\r\n"
253 "a=ice-ufrag:e5785931\r\n"
254 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
255 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
256 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
257 "a=mid:1\r\n"
258 "a=sendrecv\r\n"
259 "a=rtcp-mux\r\n"
260 "a=rtpmap:120 VP8/0\r\n"
261 "a=ssrc:2 cname:stream1\r\n"
262 "a=ssrc:2 msid:stream1 videotrack0\r\n"
263 "m=audio 1 RTP/AVPF 103\r\n"
264 "a=ice-ufrag:e5785931\r\n"
265 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
266 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
267 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
268 "a=mid:2\r\n"
269 "a=sendrecv\r\n"
270 "a=rtcp-mux\r\n"
271 "a=rtpmap:103 ISAC/16000\r\n"
272 "a=ssrc:3 cname:stream2\r\n"
273 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
274 "m=video 1 RTP/AVPF 120\r\n"
275 "a=ice-ufrag:e5785931\r\n"
276 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
277 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
278 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
279 "a=mid:3\r\n"
280 "a=sendrecv\r\n"
281 "a=rtcp-mux\r\n"
282 "a=rtpmap:120 VP8/0\r\n"
283 "a=ssrc:4 cname:stream2\r\n"
284 "a=ssrc:4 msid:stream2 videotrack1\r\n";
deadbeefab9b2d12015-10-14 11:33:11 -0700285
286// Reference SDP without MediaStreams. Msid is not supported.
287static const char kSdpStringWithoutStreams[] =
288 "v=0\r\n"
289 "o=- 0 0 IN IP4 127.0.0.1\r\n"
290 "s=-\r\n"
291 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800292 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700293 "a=ice-ufrag:e5785931\r\n"
294 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
295 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
296 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700297 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700298 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800299 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700300 "a=rtpmap:103 ISAC/16000\r\n"
301 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800302 "a=ice-ufrag:e5785931\r\n"
303 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
304 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
305 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700306 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700307 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800308 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700309 "a=rtpmap:120 VP8/90000\r\n";
310
311// Reference SDP without MediaStreams. Msid is supported.
312static const char kSdpStringWithMsidWithoutStreams[] =
313 "v=0\r\n"
314 "o=- 0 0 IN IP4 127.0.0.1\r\n"
315 "s=-\r\n"
316 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800317 "a=msid-semantic: WMS\r\n"
318 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700319 "a=ice-ufrag:e5785931\r\n"
320 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
321 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
322 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700323 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700324 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800325 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700326 "a=rtpmap:103 ISAC/16000\r\n"
327 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800328 "a=ice-ufrag:e5785931\r\n"
329 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
330 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
331 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700332 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700333 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800334 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700335 "a=rtpmap:120 VP8/90000\r\n";
336
337// Reference SDP without MediaStreams and audio only.
338static const char kSdpStringWithoutStreamsAudioOnly[] =
339 "v=0\r\n"
340 "o=- 0 0 IN IP4 127.0.0.1\r\n"
341 "s=-\r\n"
342 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800343 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700344 "a=ice-ufrag:e5785931\r\n"
345 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
346 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
347 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700348 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700349 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800350 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700351 "a=rtpmap:103 ISAC/16000\r\n";
352
353// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
354static const char kSdpStringSendOnlyWithoutStreams[] =
355 "v=0\r\n"
356 "o=- 0 0 IN IP4 127.0.0.1\r\n"
357 "s=-\r\n"
358 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800359 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700360 "a=ice-ufrag:e5785931\r\n"
361 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
362 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
363 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700364 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700365 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700366 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800367 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700368 "a=rtpmap:103 ISAC/16000\r\n"
369 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800370 "a=ice-ufrag:e5785931\r\n"
371 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
372 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
373 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700374 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700375 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700376 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800377 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700378 "a=rtpmap:120 VP8/90000\r\n";
379
380static const char kSdpStringInit[] =
381 "v=0\r\n"
382 "o=- 0 0 IN IP4 127.0.0.1\r\n"
383 "s=-\r\n"
384 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700385 "a=msid-semantic: WMS\r\n";
386
387static const char kSdpStringAudio[] =
388 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800389 "a=ice-ufrag:e5785931\r\n"
390 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
391 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
392 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700393 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700394 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800395 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700396 "a=rtpmap:103 ISAC/16000\r\n";
397
398static const char kSdpStringVideo[] =
399 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800400 "a=ice-ufrag:e5785931\r\n"
401 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
402 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
403 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700404 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700405 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800406 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700407 "a=rtpmap:120 VP8/90000\r\n";
408
409static const char kSdpStringMs1Audio0[] =
410 "a=ssrc:1 cname:stream1\r\n"
411 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
412
413static const char kSdpStringMs1Video0[] =
414 "a=ssrc:2 cname:stream1\r\n"
415 "a=ssrc:2 msid:stream1 videotrack0\r\n";
416
417static const char kSdpStringMs1Audio1[] =
418 "a=ssrc:3 cname:stream1\r\n"
419 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
420
421static const char kSdpStringMs1Video1[] =
422 "a=ssrc:4 cname:stream1\r\n"
423 "a=ssrc:4 msid:stream1 videotrack1\r\n";
424
deadbeef8662f942017-01-20 21:20:51 -0800425static const char kDtlsSdesFallbackSdp[] =
426 "v=0\r\n"
427 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
428 "s=-\r\n"
429 "c=IN IP4 0.0.0.0\r\n"
430 "t=0 0\r\n"
431 "a=group:BUNDLE audio\r\n"
432 "a=msid-semantic: WMS\r\n"
433 "m=audio 1 RTP/SAVPF 0\r\n"
434 "a=sendrecv\r\n"
435 "a=rtcp-mux\r\n"
436 "a=mid:audio\r\n"
437 "a=ssrc:1 cname:stream1\r\n"
deadbeef8662f942017-01-20 21:20:51 -0800438 "a=ice-ufrag:e5785931\r\n"
439 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
440 "a=rtpmap:0 pcmu/8000\r\n"
441 "a=fingerprint:sha-1 "
442 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
443 "a=setup:actpass\r\n"
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700444 "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
deadbeef8662f942017-01-20 21:20:51 -0800445 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
446 "dummy_session_params\r\n";
447
Niels Möllerdec9f742019-06-03 15:25:20 +0200448class RtcEventLogOutputNull final : public RtcEventLogOutput {
449 public:
450 bool IsActive() const override { return true; }
451 bool Write(const std::string& output) override { return true; }
452};
453
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +0100454using ::cricket::StreamParams;
perkjd61bf802016-03-24 03:16:19 -0700455using ::testing::Exactly;
Steve Anton36da6ff2018-02-16 16:04:20 -0800456using ::testing::Values;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
Steve Anton36da6ff2018-02-16 16:04:20 -0800458using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
459using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
deadbeefab9b2d12015-10-14 11:33:11 -0700460
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461// Gets the first ssrc of given content type from the ContentInfo.
462bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
463 if (!content_info || !ssrc) {
464 return false;
465 }
466 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800467 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 if (!media_desc || media_desc->streams().empty()) {
469 return false;
470 }
471 *ssrc = media_desc->streams().begin()->first_ssrc();
472 return true;
473}
474
deadbeefd1a38b52016-12-10 13:15:33 -0800475// Get the ufrags out of an SDP blob. Useful for testing ICE restart
476// behavior.
477std::vector<std::string> GetUfrags(
478 const webrtc::SessionDescriptionInterface* desc) {
479 std::vector<std::string> ufrags;
480 for (const cricket::TransportInfo& info :
481 desc->description()->transport_infos()) {
482 ufrags.push_back(info.description.ice_ufrag);
483 }
484 return ufrags;
485}
486
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487void SetSsrcToZero(std::string* sdp) {
488 const char kSdpSsrcAtribute[] = "a=ssrc:";
489 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
490 size_t ssrc_pos = 0;
491 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
Yves Gerey665174f2018-06-19 15:03:05 +0200492 std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 size_t end_ssrc = sdp->find(" ", ssrc_pos);
494 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
495 ssrc_pos = end_ssrc;
496 }
497}
498
deadbeefab9b2d12015-10-14 11:33:11 -0700499// Check if |streams| contains the specified track.
500bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
Seth Hampson845e8782018-03-02 11:34:10 -0800501 const std::string& stream_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700502 const std::string& track_id) {
503 for (const cricket::StreamParams& params : streams) {
Seth Hampson845e8782018-03-02 11:34:10 -0800504 if (params.first_stream_id() == stream_id && params.id == track_id) {
deadbeefab9b2d12015-10-14 11:33:11 -0700505 return true;
506 }
507 }
508 return false;
509}
510
511// Check if |senders| contains the specified sender, by id.
512bool ContainsSender(
513 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
514 const std::string& id) {
515 for (const auto& sender : senders) {
516 if (sender->id() == id) {
517 return true;
518 }
519 }
520 return false;
521}
522
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700523// Check if |senders| contains the specified sender, by id and stream id.
524bool ContainsSender(
525 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
526 const std::string& id,
527 const std::string& stream_id) {
528 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700529 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700530 return true;
531 }
532 }
533 return false;
534}
535
deadbeefab9b2d12015-10-14 11:33:11 -0700536// Create a collection of streams.
537// CreateStreamCollection(1) creates a collection that
538// correspond to kSdpStringWithStream1.
539// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
540rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700541 int number_of_streams,
542 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700543 rtc::scoped_refptr<StreamCollection> local_collection(
544 StreamCollection::Create());
545
546 for (int i = 0; i < number_of_streams; ++i) {
547 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
548 webrtc::MediaStream::Create(kStreams[i]));
549
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700550 for (int j = 0; j < tracks_per_stream; ++j) {
551 // Add a local audio track.
552 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
553 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
554 nullptr));
555 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700556
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700557 // Add a local video track.
558 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
559 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700560 webrtc::FakeVideoTrackSource::Create(),
561 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700562 stream->AddTrack(video_track);
563 }
deadbeefab9b2d12015-10-14 11:33:11 -0700564
565 local_collection->AddStream(stream);
566 }
567 return local_collection;
568}
569
570// Check equality of StreamCollections.
571bool CompareStreamCollections(StreamCollectionInterface* s1,
572 StreamCollectionInterface* s2) {
573 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
574 return false;
575 }
576
577 for (size_t i = 0; i != s1->count(); ++i) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700578 if (s1->at(i)->id() != s2->at(i)->id()) {
deadbeefab9b2d12015-10-14 11:33:11 -0700579 return false;
580 }
581 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
582 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
583 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
584 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
585
586 if (audio_tracks1.size() != audio_tracks2.size()) {
587 return false;
588 }
589 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
590 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
591 return false;
592 }
593 }
594 if (video_tracks1.size() != video_tracks2.size()) {
595 return false;
596 }
597 for (size_t j = 0; j != video_tracks1.size(); ++j) {
598 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
599 return false;
600 }
601 }
602 }
603 return true;
604}
605
perkjd61bf802016-03-24 03:16:19 -0700606// Helper class to test Observer.
607class MockTrackObserver : public ObserverInterface {
608 public:
609 explicit MockTrackObserver(NotifierInterface* notifier)
610 : notifier_(notifier) {
611 notifier_->RegisterObserver(this);
612 }
613
614 ~MockTrackObserver() { Unregister(); }
615
616 void Unregister() {
617 if (notifier_) {
618 notifier_->UnregisterObserver(this);
619 notifier_ = nullptr;
620 }
621 }
622
623 MOCK_METHOD0(OnChanged, void());
624
625 private:
626 NotifierInterface* notifier_;
627};
628
nisse528b7932017-05-08 03:21:43 -0700629// The PeerConnectionMediaConfig tests below verify that configuration and
630// constraints are propagated into the PeerConnection's MediaConfig. These
631// settings are intended for MediaChannel constructors, but that is not
632// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700633class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
634 public:
zhihuang38ede132017-06-15 12:52:32 -0700635 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
636 CreatePeerConnectionFactoryForTest() {
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100637 PeerConnectionFactoryDependencies dependencies;
638 dependencies.worker_thread = rtc::Thread::Current();
639 dependencies.network_thread = rtc::Thread::Current();
640 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200641 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
642 cricket::MediaEngineDependencies media_deps;
643 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
henrika919dc2e2017-10-12 14:24:55 +0200644 // Use fake audio device module since we're only testing the interface
645 // level, and using a real one could make tests flaky when run in parallel.
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200646 media_deps.adm = FakeAudioCaptureModule::Create();
647 SetMediaEngineDefaults(&media_deps);
648 dependencies.media_engine =
649 cricket::CreateMediaEngine(std::move(media_deps));
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100650 dependencies.call_factory = webrtc::CreateCallFactory();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200651 dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200652 dependencies.task_queue_factory.get());
zhihuang38ede132017-06-15 12:52:32 -0700653
654 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100655 std::move(dependencies));
zhihuang38ede132017-06-15 12:52:32 -0700656 }
657
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100658 using PeerConnectionFactory::PeerConnectionFactory;
kwiberg1e4e8cb2017-01-31 01:48:08 -0800659
Danil Chapovalovf5258be2019-03-19 17:45:24 +0100660 private:
deadbeefd7850b22017-08-23 10:59:19 -0700661 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700662};
663
Steve Anton36da6ff2018-02-16 16:04:20 -0800664// TODO(steveanton): Convert to use the new PeerConnectionWrapper.
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200665class PeerConnectionInterfaceBaseTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 protected:
Steve Anton36da6ff2018-02-16 16:04:20 -0800667 explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
668 : vss_(new rtc::VirtualSocketServer()),
669 main_(vss_.get()),
670 sdp_semantics_(sdp_semantics) {
phoglund37ebcf02016-01-08 05:04:57 -0800671#ifdef WEBRTC_ANDROID
672 webrtc::InitializeAndroidObjects();
673#endif
674 }
675
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700677 // Use fake audio capture module since we're only testing the interface
678 // level, and using a real one could make tests flaky when run in parallel.
679 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700681 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200682 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
683 fake_audio_capture_module_),
684 webrtc::CreateBuiltinAudioEncoderFactory(),
685 webrtc::CreateBuiltinAudioDecoderFactory(),
686 webrtc::CreateBuiltinVideoEncoderFactory(),
687 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
688 nullptr /* audio_processing */);
danilchape9021a32016-05-17 01:52:02 -0700689 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700690 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700691 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700692 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 }
694
695 void CreatePeerConnection() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200696 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
deadbeef293e9262017-01-11 12:28:30 -0800699 // DTLS does not work in a loopback call, so is disabled for most of the
700 // tests in this file.
701 void CreatePeerConnectionWithoutDtls() {
Niels Möllerf06f9232018-08-07 12:32:18 +0200702 RTCConfiguration config;
703 config.enable_dtls_srtp = false;
deadbeef293e9262017-01-11 12:28:30 -0800704
Niels Möllerf06f9232018-08-07 12:32:18 +0200705 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
707
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 void CreatePeerConnectionWithIceTransportsType(
709 PeerConnectionInterface::IceTransportsType type) {
710 PeerConnectionInterface::RTCConfiguration config;
711 config.type = type;
Niels Möllerf06f9232018-08-07 12:32:18 +0200712 return CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700713 }
714
715 void CreatePeerConnectionWithIceServer(const std::string& uri,
Niels Möllerdb4def92019-03-18 16:53:59 +0100716 const std::string& username,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720 server.uri = uri;
Niels Möllerdb4def92019-03-18 16:53:59 +0100721 server.username = username;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 server.password = password;
723 config.servers.push_back(server);
Niels Möllerf06f9232018-08-07 12:32:18 +0200724 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
Niels Möllerf06f9232018-08-07 12:32:18 +0200727 void CreatePeerConnection(const RTCConfiguration& config) {
kwibergd1fe2812016-04-27 06:47:29 -0700728 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
730 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000731
deadbeef1dcb1642017-03-29 21:08:16 -0700732 // Create certificate generator unless DTLS constraint is explicitly set to
733 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200734 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
Niels Möllerf06f9232018-08-07 12:32:18 +0200735
736 if (config.enable_dtls_srtp.value_or(true)) {
deadbeef8662f942017-01-20 21:20:51 -0800737 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
738 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000739 }
Steve Anton36da6ff2018-02-16 16:04:20 -0800740 RTCConfiguration modified_config = config;
741 modified_config.sdp_semantics = sdp_semantics_;
Henrik Boströmd79599d2016-06-01 13:58:50 +0200742 pc_ = pc_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +0200743 modified_config, std::move(port_allocator), std::move(cert_generator),
744 &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 ASSERT_TRUE(pc_.get() != NULL);
746 observer_.SetPeerConnectionInterface(pc_.get());
747 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
748 }
749
deadbeef0a6c4ca2015-10-06 11:38:28 -0700750 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700752 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700753 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800754 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800755 config.sdp_semantics = sdp_semantics_;
756 rtc::scoped_refptr<PeerConnectionInterface> pc =
Niels Möllerf06f9232018-08-07 12:32:18 +0200757 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700759 }
760
Steve Anton038834f2017-07-14 15:59:59 -0700761 void CreatePeerConnectionExpectFail(
762 PeerConnectionInterface::RTCConfiguration config) {
763 PeerConnectionInterface::IceServer server;
764 server.uri = kTurnIceServerUri;
765 server.password = kTurnPassword;
766 config.servers.push_back(server);
Steve Anton36da6ff2018-02-16 16:04:20 -0800767 config.sdp_semantics = sdp_semantics_;
Steve Anton038834f2017-07-14 15:59:59 -0700768 rtc::scoped_refptr<PeerConnectionInterface> pc =
769 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
770 EXPECT_EQ(nullptr, pc);
771 }
772
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 void CreatePeerConnectionWithDifferentConfigurations() {
Niels Möllerdb4def92019-03-18 16:53:59 +0100774 CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
776 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
777 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780
deadbeef0a6c4ca2015-10-06 11:38:28 -0700781 CreatePeerConnectionExpectFail(kStunInvalidPort);
782 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
783 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784
Niels Möllerdb4def92019-03-18 16:53:59 +0100785 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
786 kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800787 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
788 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800790 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800792 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800794 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 }
796
797 void ReleasePeerConnection() {
798 pc_ = NULL;
799 observer_.SetPeerConnectionInterface(NULL);
800 }
801
Steve Anton36da6ff2018-02-16 16:04:20 -0800802 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
803 const std::string& label) {
Niels Möller215ba602019-01-18 13:59:45 +0100804 return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton36da6ff2018-02-16 16:04:20 -0800805 }
806
807 void AddVideoTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800808 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800809 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800810 pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800811 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 }
813
Steve Anton36da6ff2018-02-16 16:04:20 -0800814 void AddVideoStream(const std::string& label) {
zhihuang9763d562016-08-05 11:14:50 -0700815 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 pc_factory_->CreateLocalMediaStream(label));
Steve Anton36da6ff2018-02-16 16:04:20 -0800817 stream->AddTrack(CreateVideoTrack(label + "v0"));
818 ASSERT_TRUE(pc_->AddStream(stream));
Steve Anton36da6ff2018-02-16 16:04:20 -0800819 }
820
821 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
822 const std::string& label) {
823 return pc_factory_->CreateAudioTrack(label, nullptr);
824 }
825
826 void AddAudioTrack(const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800827 const std::vector<std::string>& stream_ids = {}) {
Steve Anton36da6ff2018-02-16 16:04:20 -0800828 auto sender_or_error =
Seth Hampson845e8782018-03-02 11:34:10 -0800829 pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton36da6ff2018-02-16 16:04:20 -0800830 ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
Steve Anton36da6ff2018-02-16 16:04:20 -0800831 }
832
833 void AddAudioStream(const std::string& label) {
834 rtc::scoped_refptr<MediaStreamInterface> stream(
835 pc_factory_->CreateLocalMediaStream(label));
836 stream->AddTrack(CreateAudioTrack(label + "a0"));
837 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 }
839
Seth Hampson845e8782018-03-02 11:34:10 -0800840 void AddAudioVideoStream(const std::string& stream_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 const std::string& audio_track_label,
842 const std::string& video_track_label) {
843 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700844 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -0800845 pc_factory_->CreateLocalMediaStream(stream_id));
Steve Anton36da6ff2018-02-16 16:04:20 -0800846 stream->AddTrack(CreateAudioTrack(audio_track_label));
847 stream->AddTrack(CreateVideoTrack(video_track_label));
848 ASSERT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850
Steve Anton36da6ff2018-02-16 16:04:20 -0800851 rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
852 cricket::MediaType media_type) {
853 for (auto receiver : pc_->GetReceivers()) {
854 if (receiver->media_type() == media_type) {
855 return receiver;
856 }
857 }
858 return nullptr;
859 }
860
kwibergd1fe2812016-04-27 06:47:29 -0700861 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200862 const RTCOfferAnswerOptions* options,
863 bool offer) {
Yves Gerey665174f2018-06-19 15:03:05 +0200864 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
865 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 if (offer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200867 pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200869 pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 }
871 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700872 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return observer->result();
874 }
875
kwibergd1fe2812016-04-27 06:47:29 -0700876 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200877 const RTCOfferAnswerOptions* options) {
878 return DoCreateOfferAnswer(desc, options, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880
kwibergd1fe2812016-04-27 06:47:29 -0700881 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
Niels Möllerf06f9232018-08-07 12:32:18 +0200882 const RTCOfferAnswerOptions* options) {
883 return DoCreateOfferAnswer(desc, options, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 }
885
Steve Antondb45ca82017-09-11 18:27:34 -0700886 bool DoSetSessionDescription(
887 std::unique_ptr<SessionDescriptionInterface> desc,
888 bool local) {
Yves Gerey665174f2018-06-19 15:03:05 +0200889 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
890 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700892 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700894 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
zhihuang29ff8442016-07-27 11:07:25 -0700896 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
897 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 return observer->result();
900 }
901
Steve Antondb45ca82017-09-11 18:27:34 -0700902 bool DoSetLocalDescription(
903 std::unique_ptr<SessionDescriptionInterface> desc) {
904 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906
Steve Antondb45ca82017-09-11 18:27:34 -0700907 bool DoSetRemoteDescription(
908 std::unique_ptr<SessionDescriptionInterface> desc) {
909 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911
912 // Calls PeerConnection::GetStats and check the return value.
913 // It does not verify the values in the StatReports since a RTCP packet might
914 // be required.
915 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000916 rtc::scoped_refptr<MockStatsObserver> observer(
917 new rtc::RefCountedObject<MockStatsObserver>());
Yves Gerey665174f2018-06-19 15:03:05 +0200918 if (!pc_->GetStats(observer, track,
919 PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 return false;
921 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
922 return observer->called();
923 }
924
Harald Alvestrand89061872018-01-02 14:08:34 +0100925 // Call the standards-compliant GetStats function.
926 bool DoGetRTCStats() {
927 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
928 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
929 pc_->GetStats(callback);
930 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
931 return callback->called();
932 }
933
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800935 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 // Create a local stream with audio&video tracks.
Steve Anton36da6ff2018-02-16 16:04:20 -0800937 if (sdp_semantics_ == SdpSemantics::kPlanB) {
Seth Hampson845e8782018-03-02 11:34:10 -0800938 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
Steve Anton36da6ff2018-02-16 16:04:20 -0800939 } else {
940 // Unified Plan does not support AddStream, so just add an audio and video
941 // track.
Seth Hampson845e8782018-03-02 11:34:10 -0800942 AddAudioTrack(kAudioTracks[0], {kStreamId1});
943 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Steve Anton36da6ff2018-02-16 16:04:20 -0800944 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 CreateOfferReceiveAnswer();
946 }
947
948 // Verify that RTP Header extensions has been negotiated for audio and video.
949 void VerifyRemoteRtpHeaderExtensions() {
950 const cricket::MediaContentDescription* desc =
951 cricket::GetFirstAudioContentDescription(
952 pc_->remote_description()->description());
953 ASSERT_TRUE(desc != NULL);
954 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
955
956 desc = cricket::GetFirstVideoContentDescription(
957 pc_->remote_description()->description());
958 ASSERT_TRUE(desc != NULL);
959 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
960 }
961
962 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700963 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700964 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 std::string sdp;
966 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700967 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800968 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700969 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
971 }
972
deadbeefab9b2d12015-10-14 11:33:11 -0700973 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700974 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800975 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700976 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700977 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
978 }
979
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700981 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700982 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
984 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
985 // audio codec change, even if the parameter has nothing to do with
986 // receiving. Not all parameters are serialized to SDP.
987 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
988 // the SessionDescription, it is necessary to do that here to in order to
989 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
990 // https://code.google.com/p/webrtc/issues/detail?id=1356
991 std::string sdp;
992 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700993 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800994 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700995 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
997 }
998
999 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001001 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002
1003 std::string sdp;
1004 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001005 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001006 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001007 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1009 }
1010
1011 void CreateOfferReceiveAnswer() {
1012 CreateOfferAsLocalDescription();
1013 std::string sdp;
1014 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1015 CreateAnswerAsRemoteDescription(sdp);
1016 }
1017
1018 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001019 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001020 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1022 // audio codec change, even if the parameter has nothing to do with
1023 // receiving. Not all parameters are serialized to SDP.
1024 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1025 // the SessionDescription, it is necessary to do that here to in order to
1026 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1027 // https://code.google.com/p/webrtc/issues/detail?id=1356
1028 std::string sdp;
1029 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001030 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001031 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032
Steve Antondb45ca82017-09-11 18:27:34 -07001033 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001035 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -07001036 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 }
1038
deadbeefab9b2d12015-10-14 11:33:11 -07001039 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001040 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001041 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001042 ASSERT_TRUE(answer);
1043 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1045 }
1046
deadbeefab9b2d12015-10-14 11:33:11 -07001047 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001048 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001049 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001050 ASSERT_TRUE(pr_answer);
1051 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001053 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08001054 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001055 ASSERT_TRUE(answer);
1056 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1058 }
1059
Seth Hampson845e8782018-03-02 11:34:10 -08001060 // Waits until a remote stream with the given id is signaled. This helper
Steve Anton36da6ff2018-02-16 16:04:20 -08001061 // function will verify both OnAddTrack and OnAddStream (Plan B only) are
Seth Hampson845e8782018-03-02 11:34:10 -08001062 // called with the given stream id and expected number of tracks.
1063 void WaitAndVerifyOnAddStream(const std::string& stream_id,
Steve Anton36da6ff2018-02-16 16:04:20 -08001064 int expected_num_tracks) {
1065 // Verify that both OnAddStream and OnAddTrack are called.
Seth Hampson845e8782018-03-02 11:34:10 -08001066 EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
Steve Anton36da6ff2018-02-16 16:04:20 -08001067 EXPECT_EQ_WAIT(expected_num_tracks,
Seth Hampson845e8782018-03-02 11:34:10 -08001068 observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 }
1070
1071 // Creates an offer and applies it as a local session description.
1072 // Creates an answer with the same SDP an the offer but removes all lines
1073 // that start with a:ssrc"
1074 void CreateOfferReceiveAnswerWithoutSsrc() {
1075 CreateOfferAsLocalDescription();
1076 std::string sdp;
1077 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1078 SetSsrcToZero(&sdp);
1079 CreateAnswerAsRemoteDescription(sdp);
1080 }
1081
deadbeefab9b2d12015-10-14 11:33:11 -07001082 // This function creates a MediaStream with label kStreams[0] and
1083 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1084 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001085 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001086 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001087 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001088 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1089 size_t number_of_video_tracks) {
1090 EXPECT_LE(number_of_audio_tracks, 2u);
1091 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001092
1093 reference_collection_ = StreamCollection::Create();
1094 std::string sdp_ms1 = std::string(kSdpStringInit);
1095
Seth Hampson845e8782018-03-02 11:34:10 -08001096 std::string mediastream_id = kStreams[0];
deadbeefab9b2d12015-10-14 11:33:11 -07001097
1098 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001099 webrtc::MediaStream::Create(mediastream_id));
deadbeefab9b2d12015-10-14 11:33:11 -07001100 reference_collection_->AddStream(stream);
1101
1102 if (number_of_audio_tracks > 0) {
1103 sdp_ms1 += std::string(kSdpStringAudio);
1104 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1105 AddAudioTrack(kAudioTracks[0], stream);
1106 }
1107 if (number_of_audio_tracks > 1) {
1108 sdp_ms1 += kSdpStringMs1Audio1;
1109 AddAudioTrack(kAudioTracks[1], stream);
1110 }
1111
1112 if (number_of_video_tracks > 0) {
1113 sdp_ms1 += std::string(kSdpStringVideo);
1114 sdp_ms1 += std::string(kSdpStringMs1Video0);
1115 AddVideoTrack(kVideoTracks[0], stream);
1116 }
1117 if (number_of_video_tracks > 1) {
1118 sdp_ms1 += kSdpStringMs1Video1;
1119 AddVideoTrack(kVideoTracks[1], stream);
1120 }
1121
kwibergd1fe2812016-04-27 06:47:29 -07001122 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001123 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001124 }
1125
1126 void AddAudioTrack(const std::string& track_id,
1127 MediaStreamInterface* stream) {
1128 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1129 webrtc::AudioTrack::Create(track_id, nullptr));
1130 ASSERT_TRUE(stream->AddTrack(audio_track));
1131 }
1132
1133 void AddVideoTrack(const std::string& track_id,
1134 MediaStreamInterface* stream) {
1135 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001136 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001137 webrtc::FakeVideoTrackSource::Create(),
1138 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001139 ASSERT_TRUE(stream->AddTrack(video_track));
1140 }
1141
Steve Anton36da6ff2018-02-16 16:04:20 -08001142 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
deadbeef293e9262017-01-11 12:28:30 -08001143 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08001144 AddAudioTrack(kAudioTracks[0]);
kwibergfd8be342016-05-14 19:44:11 -07001145 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001146 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1147 return offer;
1148 }
1149
Steve Anton36da6ff2018-02-16 16:04:20 -08001150 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1151 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001152 AddAudioStream(kStreamId1);
Steve Anton36da6ff2018-02-16 16:04:20 -08001153 std::unique_ptr<SessionDescriptionInterface> offer;
1154 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1155 return offer;
1156 }
1157
1158 std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1159 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1160 std::unique_ptr<SessionDescriptionInterface> answer;
1161 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1162 return answer;
1163 }
1164
kwibergfd8be342016-05-14 19:44:11 -07001165 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001166 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001167 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001169 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1170 return answer;
1171 }
1172
1173 const std::string& GetFirstAudioStreamCname(
1174 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001176 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 return audio_desc->streams()[0].cname;
1178 }
1179
zhihuang1c378ed2017-08-17 14:10:50 -07001180 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1181 const RTCOfferAnswerOptions& offer_answer_options) {
1182 RTC_DCHECK(pc_);
1183 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1184 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1185 pc_->CreateOffer(observer, offer_answer_options);
1186 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1187 return observer->MoveDescription();
1188 }
1189
1190 void CreateOfferWithOptionsAsRemoteDescription(
1191 std::unique_ptr<SessionDescriptionInterface>* desc,
1192 const RTCOfferAnswerOptions& offer_answer_options) {
1193 *desc = CreateOfferWithOptions(offer_answer_options);
1194 ASSERT_TRUE(desc != nullptr);
1195 std::string sdp;
1196 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001197 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001198 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001199 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001200 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1201 }
1202
1203 void CreateOfferWithOptionsAsLocalDescription(
1204 std::unique_ptr<SessionDescriptionInterface>* desc,
1205 const RTCOfferAnswerOptions& offer_answer_options) {
1206 *desc = CreateOfferWithOptions(offer_answer_options);
1207 ASSERT_TRUE(desc != nullptr);
1208 std::string sdp;
1209 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001210 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001211 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001212
Steve Antondb45ca82017-09-11 18:27:34 -07001213 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001214 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1215 }
1216
1217 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001218 RTC_DCHECK(content);
1219 RTC_DCHECK(content->media_description());
1220 for (const cricket::AudioCodec& codec :
1221 content->media_description()->as_audio()->codecs()) {
1222 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001223 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001224 }
zhihuang1c378ed2017-08-17 14:10:50 -07001225 }
1226 return false;
1227 }
1228
Steve Anton36da6ff2018-02-16 16:04:20 -08001229 const char* GetSdpStringWithStream1() const {
1230 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1231 return kSdpStringWithStream1PlanB;
1232 } else {
1233 return kSdpStringWithStream1UnifiedPlan;
1234 }
1235 }
1236
1237 const char* GetSdpStringWithStream1And2() const {
1238 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1239 return kSdpStringWithStream1And2PlanB;
1240 } else {
1241 return kSdpStringWithStream1And2UnifiedPlan;
1242 }
1243 }
1244
deadbeef9a6f4d42017-05-15 19:43:33 -07001245 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1246 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001247 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001248 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001249 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001250 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1251 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1252 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001254 rtc::scoped_refptr<StreamCollection> reference_collection_;
Steve Anton36da6ff2018-02-16 16:04:20 -08001255 const SdpSemantics sdp_semantics_;
1256};
1257
1258class PeerConnectionInterfaceTest
1259 : public PeerConnectionInterfaceBaseTest,
1260 public ::testing::WithParamInterface<SdpSemantics> {
1261 protected:
1262 PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1263};
1264
1265class PeerConnectionInterfaceTestPlanB
1266 : public PeerConnectionInterfaceBaseTest {
1267 protected:
1268 PeerConnectionInterfaceTestPlanB()
1269 : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270};
1271
zhihuang8f65cdf2016-05-06 18:40:30 -07001272// Generate different CNAMEs when PeerConnections are created.
1273// The CNAMEs are expected to be generated randomly. It is possible
1274// that the test fails, though the possibility is very low.
Steve Anton36da6ff2018-02-16 16:04:20 -08001275TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001276 std::unique_ptr<SessionDescriptionInterface> offer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001277 CreateOfferWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001278 std::unique_ptr<SessionDescriptionInterface> offer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001279 CreateOfferWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001280 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1281 GetFirstAudioStreamCname(offer2.get()));
1282}
1283
Steve Anton36da6ff2018-02-16 16:04:20 -08001284TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001285 std::unique_ptr<SessionDescriptionInterface> answer1 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001286 CreateAnswerWithOneAudioTrack();
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> answer2 =
Steve Anton36da6ff2018-02-16 16:04:20 -08001288 CreateAnswerWithOneAudioTrack();
zhihuang8f65cdf2016-05-06 18:40:30 -07001289 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1290 GetFirstAudioStreamCname(answer2.get()));
1291}
1292
Steve Anton36da6ff2018-02-16 16:04:20 -08001293TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 CreatePeerConnectionWithDifferentConfigurations) {
1295 CreatePeerConnectionWithDifferentConfigurations();
1296}
1297
Steve Anton36da6ff2018-02-16 16:04:20 -08001298TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001299 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1300 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1301 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1302 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1303 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1304 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1305 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1306 port_allocator_->candidate_filter());
1307 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1308 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1309}
1310
1311// Test that when a PeerConnection is created with a nonzero candidate pool
1312// size, the pooled PortAllocatorSession is created with all the attributes
1313// in the RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001314TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315 PeerConnectionInterface::RTCConfiguration config;
1316 PeerConnectionInterface::IceServer server;
1317 server.uri = kStunAddressOnly;
1318 config.servers.push_back(server);
1319 config.type = PeerConnectionInterface::kRelay;
1320 config.disable_ipv6 = true;
1321 config.tcp_candidate_policy =
1322 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001323 config.candidate_network_policy =
1324 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325 config.ice_candidate_pool_size = 1;
Niels Möllerf06f9232018-08-07 12:32:18 +02001326 CreatePeerConnection(config);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001327
1328 const cricket::FakePortAllocatorSession* session =
1329 static_cast<const cricket::FakePortAllocatorSession*>(
1330 port_allocator_->GetPooledSession());
1331 ASSERT_NE(nullptr, session);
1332 EXPECT_EQ(1UL, session->stun_servers().size());
1333 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1334 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001335 EXPECT_LT(0U,
1336 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001337}
1338
deadbeefd21eab32017-07-26 16:50:11 -07001339// Test that network-related RTCConfiguration members are applied to the
1340// PortAllocator when CreatePeerConnection is called. Specifically:
1341// - disable_ipv6_on_wifi
1342// - max_ipv6_networks
1343// - tcp_candidate_policy
1344// - candidate_network_policy
1345// - prune_turn_ports
1346//
1347// Note that the candidate filter (RTCConfiguration::type) is already tested
1348// above.
Steve Anton36da6ff2018-02-16 16:04:20 -08001349TEST_P(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001350 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1351 // Create fake port allocator.
1352 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1353 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1354 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1355
1356 // Create RTCConfiguration with some network-related fields relevant to
1357 // PortAllocator populated.
1358 PeerConnectionInterface::RTCConfiguration config;
1359 config.disable_ipv6_on_wifi = true;
1360 config.max_ipv6_networks = 10;
1361 config.tcp_candidate_policy =
1362 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1363 config.candidate_network_policy =
1364 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1365 config.prune_turn_ports = true;
1366
1367 // Create the PC factory and PC with the above config.
1368 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1369 webrtc::CreatePeerConnectionFactory(
1370 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001371 rtc::Thread::Current(), fake_audio_capture_module_,
1372 webrtc::CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001373 webrtc::CreateBuiltinAudioDecoderFactory(),
1374 webrtc::CreateBuiltinVideoEncoderFactory(),
1375 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1376 nullptr /* audio_processing */));
deadbeefd21eab32017-07-26 16:50:11 -07001377 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001378 pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1379 nullptr, &observer_));
Yves Gerey4e933292018-10-31 15:36:05 +01001380 EXPECT_TRUE(pc.get());
1381 observer_.SetPeerConnectionInterface(pc.get());
deadbeefd21eab32017-07-26 16:50:11 -07001382
1383 // Now validate that the config fields set above were applied to the
1384 // PortAllocator, as flags or otherwise.
1385 EXPECT_FALSE(raw_port_allocator->flags() &
1386 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1387 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1388 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1389 EXPECT_TRUE(raw_port_allocator->flags() &
1390 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Honghai Zhangf8998cf2019-10-14 11:27:50 -07001391 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1392 raw_port_allocator->turn_port_prune_policy());
deadbeefd21eab32017-07-26 16:50:11 -07001393}
1394
deadbeef46c73892016-11-16 19:42:04 -08001395// Check that GetConfiguration returns the configuration the PeerConnection was
1396// constructed with, before SetConfiguration is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08001397TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
deadbeef46c73892016-11-16 19:42:04 -08001398 PeerConnectionInterface::RTCConfiguration config;
1399 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02001400 CreatePeerConnection(config);
deadbeef46c73892016-11-16 19:42:04 -08001401
1402 PeerConnectionInterface::RTCConfiguration returned_config =
1403 pc_->GetConfiguration();
1404 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1405}
1406
1407// Check that GetConfiguration returns the last configuration passed into
1408// SetConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08001409TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
Piotr (Peter) Slatala37227be2018-11-21 07:42:22 -08001410 PeerConnectionInterface::RTCConfiguration starting_config;
1411 starting_config.bundle_policy =
1412 webrtc::PeerConnection::kBundlePolicyMaxBundle;
1413 CreatePeerConnection(starting_config);
deadbeef46c73892016-11-16 19:42:04 -08001414
Steve Anton36da6ff2018-02-16 16:04:20 -08001415 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef46c73892016-11-16 19:42:04 -08001416 config.type = PeerConnectionInterface::kRelay;
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -08001417 config.use_datagram_transport = true;
1418 config.use_datagram_transport_for_data_channels = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02001419 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef46c73892016-11-16 19:42:04 -08001420
1421 PeerConnectionInterface::RTCConfiguration returned_config =
1422 pc_->GetConfiguration();
1423 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -08001424 EXPECT_TRUE(returned_config.use_datagram_transport);
1425 EXPECT_TRUE(returned_config.use_datagram_transport_for_data_channels);
deadbeef46c73892016-11-16 19:42:04 -08001426}
1427
Steve Antonc79268f2018-04-24 09:54:10 -07001428TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1429 CreatePeerConnection();
1430
1431 pc_->Close();
1432
1433 EXPECT_FALSE(
Niels Möller2579f0c2019-08-19 09:58:17 +02001434 pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
Steve Antonc79268f2018-04-24 09:54:10 -07001435}
1436
Steve Anton36da6ff2018-02-16 16:04:20 -08001437TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001438 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001439 AddVideoStream(kStreamId1);
1440 AddAudioStream(kStreamId2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 ASSERT_EQ(2u, pc_->local_streams()->count());
1442
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001443 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001444 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08001445 pc_factory_->CreateLocalMediaStream(kStreamId3));
zhihuang9763d562016-08-05 11:14:50 -07001446 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Seth Hampson845e8782018-03-02 11:34:10 -08001447 pc_factory_->CreateAudioTrack(kStreamId3,
zhihuang9763d562016-08-05 11:14:50 -07001448 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001450 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001451 EXPECT_EQ(3u, pc_->local_streams()->count());
1452
1453 // Remove the third stream.
1454 pc_->RemoveStream(pc_->local_streams()->at(2));
1455 EXPECT_EQ(2u, pc_->local_streams()->count());
1456
1457 // Remove the second stream.
1458 pc_->RemoveStream(pc_->local_streams()->at(1));
1459 EXPECT_EQ(1u, pc_->local_streams()->count());
1460
1461 // Remove the first stream.
1462 pc_->RemoveStream(pc_->local_streams()->at(0));
1463 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464}
1465
deadbeefab9b2d12015-10-14 11:33:11 -07001466// Test that the created offer includes streams we added.
Steve Anton36da6ff2018-02-16 16:04:20 -08001467// Don't run under Unified Plan since the stream API is not available.
1468TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001469 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001470 AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001471 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001472 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001473
deadbeefab9b2d12015-10-14 11:33:11 -07001474 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001475 cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001476 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001477
deadbeefab9b2d12015-10-14 11:33:11 -07001478 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001479 cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001480 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
deadbeefab9b2d12015-10-14 11:33:11 -07001481
1482 // Add another stream and ensure the offer includes both the old and new
1483 // streams.
Seth Hampson845e8782018-03-02 11:34:10 -08001484 AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001485 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001486
Steve Antonb1c1de12017-12-21 15:14:30 -08001487 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001488 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1489 EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001490
Steve Antonb1c1de12017-12-21 15:14:30 -08001491 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
Seth Hampson845e8782018-03-02 11:34:10 -08001492 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1493 EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
deadbeefab9b2d12015-10-14 11:33:11 -07001494}
1495
Steve Anton36da6ff2018-02-16 16:04:20 -08001496// Don't run under Unified Plan since the stream API is not available.
1497TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001498 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001499 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 ASSERT_EQ(1u, pc_->local_streams()->count());
1501 pc_->RemoveStream(pc_->local_streams()->at(0));
1502 EXPECT_EQ(0u, pc_->local_streams()->count());
1503}
1504
deadbeefe1f9d832016-01-14 15:35:42 -08001505// Test for AddTrack and RemoveTrack methods.
1506// Tests that the created offer includes tracks we added,
1507// and that the RtpSenders are created correctly.
1508// Also tests that RemoveTrack removes the tracks from subsequent offers.
Steve Anton36da6ff2018-02-16 16:04:20 -08001509// Only tested with Plan B since Unified Plan is covered in more detail by tests
1510// in peerconnection_jsep_unittests.cc
1511TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001512 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001513 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001514 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001515 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001516 CreateVideoTrack("video_track"));
Seth Hampson845e8782018-03-02 11:34:10 -08001517 auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1518 auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001519 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001520 EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001521 EXPECT_EQ("audio_track", audio_sender->id());
1522 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001523 EXPECT_EQ(1UL, video_sender->stream_ids().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001524 EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001525 EXPECT_EQ("video_track", video_sender->id());
1526 EXPECT_EQ(video_track, video_sender->track());
1527
1528 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001529 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001530 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001531
1532 const cricket::ContentInfo* audio_content =
1533 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001534 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001535 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001536
1537 const cricket::ContentInfo* video_content =
1538 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001539 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001540 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001541
Steve Antondb45ca82017-09-11 18:27:34 -07001542 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001543
1544 // Now try removing the tracks.
1545 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1546 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1547
1548 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001549 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001552 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001553 kStreamId1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001554
1555 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001556 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
Seth Hampson845e8782018-03-02 11:34:10 -08001557 kStreamId1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001558
Steve Antondb45ca82017-09-11 18:27:34 -07001559 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001560
1561 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1562 // should return false.
1563 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1564 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1565}
1566
1567// Test creating senders without a stream specified,
1568// expecting a random stream ID to be generated.
Steve Anton36da6ff2018-02-16 16:04:20 -08001569TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001570 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001571 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001572 CreateAudioTrack("audio_track"));
zhihuang9763d562016-08-05 11:14:50 -07001573 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001574 CreateVideoTrack("video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001575 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001576 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001577 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001578 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001579 EXPECT_EQ("audio_track", audio_sender->id());
1580 EXPECT_EQ(audio_track, audio_sender->track());
1581 EXPECT_EQ("video_track", video_sender->id());
1582 EXPECT_EQ(video_track, video_sender->track());
Seth Hampson5b4f0752018-04-02 16:31:36 -07001583 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1584 // If the ID is truly a random GUID, it should be infinitely unlikely they
1585 // will be the same.
1586 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1587 } else {
1588 // We allows creating tracks without stream ids under Unified Plan
1589 // semantics.
1590 EXPECT_EQ(0u, video_sender->stream_ids().size());
1591 EXPECT_EQ(0u, audio_sender->stream_ids().size());
1592 }
deadbeefe1f9d832016-01-14 15:35:42 -08001593}
1594
Harald Alvestrand89061872018-01-02 14:08:34 +01001595// Test that we can call GetStats() after AddTrack but before connecting
1596// the PeerConnection to a peer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001597TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001598 CreatePeerConnectionWithoutDtls();
1599 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001600 CreateAudioTrack("audio_track"));
Harald Alvestrand89061872018-01-02 14:08:34 +01001601 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001602 CreateVideoTrack("video_track"));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001603 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1604 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001605 EXPECT_TRUE(DoGetStats(nullptr));
1606}
1607
Steve Anton36da6ff2018-02-16 16:04:20 -08001608TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001609 CreatePeerConnectionWithoutDtls();
1610 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01001611 CreateAudioTrack("audio_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001612 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001613 CreateVideoTrack("video_track"));
Harald Alvestrandc72af932018-01-11 17:18:19 +01001614 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001615 ASSERT_TRUE(audio_sender.ok());
1616 auto* audio_sender_proxy =
1617 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1618 audio_sender.value().get());
1619 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1620
Harald Alvestrandc72af932018-01-11 17:18:19 +01001621 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001622 ASSERT_TRUE(video_sender.ok());
1623 auto* video_sender_proxy =
1624 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1625 video_sender.value().get());
1626 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001627}
1628
Steve Anton36da6ff2018-02-16 16:04:20 -08001629// Don't run under Unified Plan since the stream API is not available.
1630TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
Harald Alvestrandc72af932018-01-11 17:18:19 +01001631 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001632 AddVideoStream(kStreamId1);
Harald Alvestrandc72af932018-01-11 17:18:19 +01001633 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001634 ASSERT_EQ(1u, senders.size());
1635 auto* sender_proxy =
1636 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1637 senders[0].get());
1638 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001639}
1640
Steve Anton36da6ff2018-02-16 16:04:20 -08001641TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 InitiateCall();
Seth Hampson845e8782018-03-02 11:34:10 -08001643 WaitAndVerifyOnAddStream(kStreamId1, 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 VerifyRemoteRtpHeaderExtensions();
1645}
1646
Steve Anton36da6ff2018-02-16 16:04:20 -08001647TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001648 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001649 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 CreateOfferAsLocalDescription();
1651 std::string offer;
1652 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1653 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
Seth Hampson845e8782018-03-02 11:34:10 -08001654 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655}
1656
Steve Anton36da6ff2018-02-16 16:04:20 -08001657TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001658 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001659 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660
1661 CreateOfferAsRemoteDescription();
1662 CreateAnswerAsLocalDescription();
1663
Seth Hampson845e8782018-03-02 11:34:10 -08001664 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665}
1666
Steve Anton36da6ff2018-02-16 16:04:20 -08001667TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001668 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001669 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670
1671 CreateOfferAsRemoteDescription();
1672 CreatePrAnswerAsLocalDescription();
1673 CreateAnswerAsLocalDescription();
1674
Seth Hampson845e8782018-03-02 11:34:10 -08001675 WaitAndVerifyOnAddStream(kStreamId1, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676}
1677
Steve Anton36da6ff2018-02-16 16:04:20 -08001678// Don't run under Unified Plan since the stream API is not available.
1679TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 InitiateCall();
1681 ASSERT_EQ(1u, pc_->remote_streams()->count());
1682 pc_->RemoveStream(pc_->local_streams()->at(0));
1683 CreateOfferReceiveAnswer();
1684 EXPECT_EQ(0u, pc_->remote_streams()->count());
Seth Hampson845e8782018-03-02 11:34:10 -08001685 AddVideoStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 CreateOfferReceiveAnswer();
1687}
1688
1689// Tests that after negotiating an audio only call, the respondent can perform a
1690// renegotiation that removes the audio stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08001691TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001692 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001693 AddAudioStream(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 CreateOfferAsRemoteDescription();
1695 CreateAnswerAsLocalDescription();
1696
1697 ASSERT_EQ(1u, pc_->remote_streams()->count());
1698 pc_->RemoveStream(pc_->local_streams()->at(0));
1699 CreateOfferReceiveAnswer();
1700 EXPECT_EQ(0u, pc_->remote_streams()->count());
1701}
1702
1703// Test that candidates are generated and that we can parse our own candidates.
Steve Anton36da6ff2018-02-16 16:04:20 -08001704TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001705 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706
Steve Antonf1c6db12017-10-13 11:13:35 -07001707 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001709 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08001710 AddVideoTrack(kVideoTracks[0]);
deadbeefc80741f2015-10-22 13:14:45 -07001711 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001712 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713
1714 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001715 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001716 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001717 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718
Steve Antonf1c6db12017-10-13 11:13:35 -07001719 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001720 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
Steve Antonf1c6db12017-10-13 11:13:35 -07001722 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
deadbeefab9b2d12015-10-14 11:33:11 -07001725// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726// not unique.
Steve Anton07563732018-06-26 11:13:50 -07001727TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001728 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001730 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001731 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001732 EXPECT_TRUE(offer);
1733 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734
1735 // Create a local stream with audio&video tracks having same label.
Seth Hampson845e8782018-03-02 11:34:10 -08001736 AddAudioTrack("track_label", {kStreamId1});
1737 AddVideoTrack("track_label", {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738
1739 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001740 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741
1742 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001743 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001744 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
1747// Test that we will get different SSRCs for each tracks in the offer and answer
1748// we created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001749TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001750 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 // Create a local stream with audio&video tracks having different labels.
Seth Hampson845e8782018-03-02 11:34:10 -08001752 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1753 AddVideoTrack(kVideoTracks[0], {kStreamId1});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754
1755 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001756 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001757 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 int audio_ssrc = 0;
1759 int video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001760 EXPECT_TRUE(
1761 GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1762 EXPECT_TRUE(
1763 GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 EXPECT_NE(audio_ssrc, video_ssrc);
1765
1766 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001767 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001768 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001769 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 audio_ssrc = 0;
1771 video_ssrc = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001772 EXPECT_TRUE(
1773 GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1774 EXPECT_TRUE(
1775 GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 EXPECT_NE(audio_ssrc, video_ssrc);
1777}
1778
deadbeefeb459812015-12-15 19:24:43 -08001779// Test that it's possible to call AddTrack on a MediaStream after adding
1780// the stream to a PeerConnection.
1781// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001782// Don't run under Unified Plan since the stream API is not available.
1783TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001784 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001785 // Create audio stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001786 AddAudioStream(kStreamId1);
deadbeefeb459812015-12-15 19:24:43 -08001787 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1788
1789 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001790 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01001791 CreateVideoTrack("video_label"));
deadbeefeb459812015-12-15 19:24:43 -08001792 stream->AddTrack(video_track.get());
1793
kwibergd1fe2812016-04-27 06:47:29 -07001794 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001795 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001796
1797 const cricket::MediaContentDescription* video_desc =
1798 cricket::GetFirstVideoContentDescription(offer->description());
1799 EXPECT_TRUE(video_desc != nullptr);
1800}
1801
1802// Test that it's possible to call RemoveTrack on a MediaStream after adding
1803// the stream to a PeerConnection.
1804// TODO(deadbeef): Remove this test once this behavior is no longer supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08001805// Don't run under Unified Plan since the stream API is not available.
1806TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001807 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001808 // Create audio/video stream and add to PeerConnection.
Seth Hampson845e8782018-03-02 11:34:10 -08001809 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
deadbeefeb459812015-12-15 19:24:43 -08001810 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1811
1812 // Remove the video track.
1813 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1814
kwibergd1fe2812016-04-27 06:47:29 -07001815 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001816 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001817
1818 const cricket::MediaContentDescription* video_desc =
1819 cricket::GetFirstVideoContentDescription(offer->description());
1820 EXPECT_TRUE(video_desc == nullptr);
1821}
1822
deadbeefbd7d8f72015-12-18 16:58:44 -08001823// Test creating a sender with a stream ID, and ensure the ID is populated
1824// in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08001825// Don't run under Unified Plan since the stream API is not available.
1826TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001827 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08001828 pc_->CreateSender("video", kStreamId1);
deadbeefbd7d8f72015-12-18 16:58:44 -08001829
kwibergd1fe2812016-04-27 06:47:29 -07001830 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001831 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001832
1833 const cricket::MediaContentDescription* video_desc =
1834 cricket::GetFirstVideoContentDescription(offer->description());
1835 ASSERT_TRUE(video_desc != nullptr);
1836 ASSERT_EQ(1u, video_desc->streams().size());
Seth Hampson845e8782018-03-02 11:34:10 -08001837 EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
deadbeefbd7d8f72015-12-18 16:58:44 -08001838}
1839
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840// Test that we can specify a certain track that we want statistics about.
Steve Anton36da6ff2018-02-16 16:04:20 -08001841TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001843 ASSERT_LT(0u, pc_->GetSenders().size());
1844 ASSERT_LT(0u, pc_->GetReceivers().size());
zhihuang9763d562016-08-05 11:14:50 -07001845 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
Steve Anton36da6ff2018-02-16 16:04:20 -08001846 pc_->GetReceivers()[0]->track();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 EXPECT_TRUE(DoGetStats(remote_audio));
1848
1849 // Remove the stream. Since we are sending to our selves the local
1850 // and the remote stream is the same.
Steve Anton36da6ff2018-02-16 16:04:20 -08001851 pc_->RemoveTrack(pc_->GetSenders()[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 // Do a re-negotiation.
1853 CreateOfferReceiveAnswer();
1854
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 // Test that we still can get statistics for the old track. Even if it is not
1856 // sent any longer.
1857 EXPECT_TRUE(DoGetStats(remote_audio));
1858}
1859
1860// Test that we can get stats on a video track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001861TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08001863 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1864 ASSERT_TRUE(video_receiver);
1865 EXPECT_TRUE(DoGetStats(video_receiver->track()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866}
1867
1868// Test that we don't get statistics for an invalid track.
Steve Anton36da6ff2018-02-16 16:04:20 -08001869TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001871 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 pc_factory_->CreateAudioTrack("unknown track", NULL));
1873 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1874}
1875
Steve Anton36da6ff2018-02-16 16:04:20 -08001876TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
Harald Alvestrand89061872018-01-02 14:08:34 +01001877 CreatePeerConnectionWithoutDtls();
1878 EXPECT_TRUE(DoGetRTCStats());
1879 // Clearing stats cache is needed now, but should be temporary.
1880 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1881 pc_->ClearStatsCache();
Seth Hampson845e8782018-03-02 11:34:10 -08001882 AddAudioTrack(kAudioTracks[0], {kStreamId1});
1883 AddVideoTrack(kVideoTracks[0], {kStreamId1});
Harald Alvestrand89061872018-01-02 14:08:34 +01001884 EXPECT_TRUE(DoGetRTCStats());
1885 pc_->ClearStatsCache();
1886 CreateOfferReceiveAnswer();
1887 EXPECT_TRUE(DoGetRTCStats());
1888}
1889
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890// This test setup two RTP data channels in loop back.
Steve Anton36da6ff2018-02-16 16:04:20 -08001891TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001892 RTCConfiguration config;
1893 config.enable_rtp_data_channel = true;
1894 config.enable_dtls_srtp = false;
1895 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001896 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001898 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 pc_->CreateDataChannel("test2", NULL);
1900 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001901 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001903 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 new MockDataChannelObserver(data2));
1905
1906 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1907 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1908 std::string data_to_send1 = "testing testing";
1909 std::string data_to_send2 = "testing something else";
1910 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1911
1912 CreateOfferReceiveAnswer();
1913 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1914 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1915
1916 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1917 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1918 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1919 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1920
1921 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1922 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1923
1924 data1->Close();
1925 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1926 CreateOfferReceiveAnswer();
1927 EXPECT_FALSE(observer1->IsOpen());
1928 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1929 EXPECT_TRUE(observer2->IsOpen());
1930
1931 data_to_send2 = "testing something else again";
1932 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1933
1934 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1935}
1936
1937// This test verifies that sendnig binary data over RTP data channels should
1938// fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08001939TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001940 RTCConfiguration config;
1941 config.enable_rtp_data_channel = true;
1942 config.enable_dtls_srtp = false;
1943 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001944 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001946 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 pc_->CreateDataChannel("test2", NULL);
1948 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001949 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001951 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 new MockDataChannelObserver(data2));
1953
1954 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1955 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1956
1957 CreateOfferReceiveAnswer();
1958 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1959 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1960
1961 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1962 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1963
jbaucheec21bd2016-03-20 06:15:43 -07001964 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1966}
1967
1968// This test setup a RTP data channels in loop back and test that a channel is
1969// opened even if the remote end answer with a zero SSRC.
Steve Anton36da6ff2018-02-16 16:04:20 -08001970TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001971 RTCConfiguration config;
1972 config.enable_rtp_data_channel = true;
1973 config.enable_dtls_srtp = false;
1974 CreatePeerConnection(config);
zhihuang9763d562016-08-05 11:14:50 -07001975 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001977 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 new MockDataChannelObserver(data1));
1979
1980 CreateOfferReceiveAnswerWithoutSsrc();
1981
1982 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1983
1984 data1->Close();
1985 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1986 CreateOfferReceiveAnswerWithoutSsrc();
1987 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1988 EXPECT_FALSE(observer1->IsOpen());
1989}
1990
1991// This test that if a data channel is added in an answer a receive only channel
1992// channel is created.
Steve Anton36da6ff2018-02-16 16:04:20 -08001993TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02001994 RTCConfiguration config;
1995 config.enable_rtp_data_channel = true;
1996 config.enable_dtls_srtp = false;
1997
1998 CreatePeerConnection(config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999
2000 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07002001 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 pc_->CreateDataChannel(offer_label, NULL);
2003
2004 CreateOfferAsLocalDescription();
2005
2006 // Replace the data channel label in the offer and apply it as an answer.
2007 std::string receive_label = "answer_channel";
2008 std::string sdp;
2009 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002010 absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 CreateAnswerAsRemoteDescription(sdp);
2012
2013 // Verify that a new incoming data channel has been created and that
2014 // it is open but can't we written to.
2015 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2016 DataChannelInterface* received_channel = observer_.last_datachannel_;
2017 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2018 EXPECT_EQ(receive_label, received_channel->label());
2019 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2020
2021 // Verify that the channel we initially offered has been rejected.
2022 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2023
2024 // Do another offer / answer exchange and verify that the data channel is
2025 // opened.
2026 CreateOfferReceiveAnswer();
2027 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2028 kTimeout);
2029}
2030
2031// This test that no data channel is returned if a reliable channel is
2032// requested.
2033// TODO(perkj): Remove this test once reliable channels are implemented.
Steve Anton36da6ff2018-02-16 16:04:20 -08002034TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002035 RTCConfiguration rtc_config;
2036 rtc_config.enable_rtp_data_channel = true;
2037 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038
2039 std::string label = "test";
2040 webrtc::DataChannelInit config;
2041 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002042 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 pc_->CreateDataChannel(label, &config);
2044 EXPECT_TRUE(channel == NULL);
2045}
2046
deadbeefab9b2d12015-10-14 11:33:11 -07002047// Verifies that duplicated label is not allowed for RTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002048TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002049 RTCConfiguration config;
2050 config.enable_rtp_data_channel = true;
2051 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002052
2053 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002054 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002055 pc_->CreateDataChannel(label, nullptr);
2056 EXPECT_NE(channel, nullptr);
2057
zhihuang9763d562016-08-05 11:14:50 -07002058 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002059 pc_->CreateDataChannel(label, nullptr);
2060 EXPECT_EQ(dup_channel, nullptr);
2061}
2062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063// This tests that a SCTP data channel is returned using different
2064// DataChannelInit configurations.
Steve Anton36da6ff2018-02-16 16:04:20 -08002065TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002066 RTCConfiguration rtc_config;
2067 rtc_config.enable_dtls_srtp = true;
2068 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069
2070 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002071 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 pc_->CreateDataChannel("1", &config);
2073 EXPECT_TRUE(channel != NULL);
2074 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002075 EXPECT_TRUE(observer_.renegotiation_needed_);
2076 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077
2078 config.ordered = false;
2079 channel = pc_->CreateDataChannel("2", &config);
2080 EXPECT_TRUE(channel != NULL);
2081 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002082 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083
2084 config.ordered = true;
2085 config.maxRetransmits = 0;
2086 channel = pc_->CreateDataChannel("3", &config);
2087 EXPECT_TRUE(channel != NULL);
2088 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002089 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002091 config.maxRetransmits = absl::nullopt;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 config.maxRetransmitTime = 0;
2093 channel = pc_->CreateDataChannel("4", &config);
2094 EXPECT_TRUE(channel != NULL);
2095 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002096 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097}
2098
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02002099// For backwards compatibility, we want people who "unset" maxRetransmits
2100// and maxRetransmitTime by setting them to -1 to get what they want.
2101TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
2102 RTCConfiguration rtc_config;
2103 rtc_config.enable_dtls_srtp = true;
2104 CreatePeerConnection(rtc_config);
2105
2106 webrtc::DataChannelInit config;
2107 config.maxRetransmitTime = -1;
2108 config.maxRetransmits = -1;
2109 rtc::scoped_refptr<DataChannelInterface> channel =
2110 pc_->CreateDataChannel("1", &config);
2111 EXPECT_TRUE(channel != NULL);
2112}
2113
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114// This tests that no data channel is returned if both maxRetransmits and
2115// maxRetransmitTime are set for SCTP data channels.
Steve Anton36da6ff2018-02-16 16:04:20 -08002116TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 CreateSctpDataChannelShouldFailForInvalidConfig) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002118 RTCConfiguration rtc_config;
2119 rtc_config.enable_dtls_srtp = true;
2120 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121
2122 std::string label = "test";
2123 webrtc::DataChannelInit config;
2124 config.maxRetransmits = 0;
2125 config.maxRetransmitTime = 0;
2126
zhihuang9763d562016-08-05 11:14:50 -07002127 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 pc_->CreateDataChannel(label, &config);
2129 EXPECT_TRUE(channel == NULL);
2130}
2131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132// The test verifies that creating a SCTP data channel with an id already in use
2133// or out of range should fail.
Steve Anton36da6ff2018-02-16 16:04:20 -08002134TEST_P(PeerConnectionInterfaceTest,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 CreateSctpDataChannelWithInvalidIdShouldFail) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002136 RTCConfiguration rtc_config;
2137 rtc_config.enable_dtls_srtp = true;
2138 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
2140 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002141 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002143 config.id = 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002144 config.negotiated = true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002145 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 EXPECT_TRUE(channel != NULL);
2147 EXPECT_EQ(1, channel->id());
2148
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 channel = pc_->CreateDataChannel("x", &config);
2150 EXPECT_TRUE(channel == NULL);
2151
2152 config.id = cricket::kMaxSctpSid;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002153 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 channel = pc_->CreateDataChannel("max", &config);
2155 EXPECT_TRUE(channel != NULL);
2156 EXPECT_EQ(config.id, channel->id());
2157
2158 config.id = cricket::kMaxSctpSid + 1;
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02002159 config.negotiated = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 channel = pc_->CreateDataChannel("x", &config);
2161 EXPECT_TRUE(channel == NULL);
2162}
2163
deadbeefab9b2d12015-10-14 11:33:11 -07002164// Verifies that duplicated label is allowed for SCTP data channel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002165TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002166 RTCConfiguration rtc_config;
2167 rtc_config.enable_dtls_srtp = true;
2168 CreatePeerConnection(rtc_config);
deadbeefab9b2d12015-10-14 11:33:11 -07002169
2170 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002171 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002172 pc_->CreateDataChannel(label, nullptr);
2173 EXPECT_NE(channel, nullptr);
2174
zhihuang9763d562016-08-05 11:14:50 -07002175 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002176 pc_->CreateDataChannel(label, nullptr);
2177 EXPECT_NE(dup_channel, nullptr);
2178}
2179
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002180// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2181// DataChannel.
Steve Anton36da6ff2018-02-16 16:04:20 -08002182TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002183 RTCConfiguration rtc_config;
2184 rtc_config.enable_rtp_data_channel = true;
2185 rtc_config.enable_dtls_srtp = false;
2186 CreatePeerConnection(rtc_config);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002187
zhihuang9763d562016-08-05 11:14:50 -07002188 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002189 pc_->CreateDataChannel("test1", NULL);
2190 EXPECT_TRUE(observer_.renegotiation_needed_);
2191 observer_.renegotiation_needed_ = false;
2192
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002193 CreateOfferReceiveAnswer();
2194
zhihuang9763d562016-08-05 11:14:50 -07002195 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002196 pc_->CreateDataChannel("test2", NULL);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02002197 EXPECT_EQ(observer_.renegotiation_needed_,
2198 GetParam() == SdpSemantics::kPlanB);
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002199}
2200
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201// This test that a data channel closes when a PeerConnection is deleted/closed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002202TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002203 RTCConfiguration rtc_config;
2204 rtc_config.enable_rtp_data_channel = true;
2205 rtc_config.enable_dtls_srtp = false;
2206 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207
zhihuang9763d562016-08-05 11:14:50 -07002208 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002210 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 pc_->CreateDataChannel("test2", NULL);
2212 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002213 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002215 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 new MockDataChannelObserver(data2));
2217
2218 CreateOfferReceiveAnswer();
2219 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2220 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2221
2222 ReleasePeerConnection();
2223 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2224 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2225}
2226
Zhi Huang644fde42018-04-02 19:16:26 -07002227// This tests that RTP data channels can be rejected in an answer.
2228TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002229 RTCConfiguration rtc_config;
2230 rtc_config.enable_rtp_data_channel = true;
2231 rtc_config.enable_dtls_srtp = false;
2232 CreatePeerConnection(rtc_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233
zhihuang9763d562016-08-05 11:14:50 -07002234 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 pc_->CreateDataChannel("offer_channel", NULL));
2236
2237 CreateOfferAsLocalDescription();
2238
2239 // Create an answer where the m-line for data channels are rejected.
2240 std::string sdp;
2241 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002242 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002243 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002244 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 cricket::ContentInfo* data_info =
Steve Anton36da6ff2018-02-16 16:04:20 -08002246 cricket::GetFirstDataContent(answer->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 data_info->rejected = true;
2248
Steve Antondb45ca82017-09-11 18:27:34 -07002249 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2251}
2252
Zhi Huang644fde42018-04-02 19:16:26 -07002253#ifdef HAVE_SCTP
2254// This tests that SCTP data channels can be rejected in an answer.
2255TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2256#else
2257TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2258#endif
2259{
Niels Möllerf06f9232018-08-07 12:32:18 +02002260 RTCConfiguration rtc_config;
2261 CreatePeerConnection(rtc_config);
Zhi Huang644fde42018-04-02 19:16:26 -07002262
2263 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2264 pc_->CreateDataChannel("offer_channel", NULL));
2265
2266 CreateOfferAsLocalDescription();
2267
2268 // Create an answer where the m-line for data channels are rejected.
2269 std::string sdp;
2270 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2271 std::unique_ptr<SessionDescriptionInterface> answer(
2272 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2273 ASSERT_TRUE(answer);
2274 cricket::ContentInfo* data_info =
2275 cricket::GetFirstDataContent(answer->description());
2276 data_info->rejected = true;
2277
2278 DoSetRemoteDescription(std::move(answer));
2279 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2280}
2281
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282// Test that we can create a session description from an SDP string from
2283// FireFox, use it as a remote session description, generate an answer and use
2284// the answer as a local description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002285TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002286 RTCConfiguration rtc_config;
2287 rtc_config.enable_dtls_srtp = true;
2288 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002289 AddAudioTrack("audio_label");
2290 AddVideoTrack("video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002291 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002292 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002293 webrtc::kFireFoxSdpOffer, nullptr));
2294 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 CreateAnswerAsLocalDescription();
2296 ASSERT_TRUE(pc_->local_description() != NULL);
2297 ASSERT_TRUE(pc_->remote_description() != NULL);
2298
2299 const cricket::ContentInfo* content =
2300 cricket::GetFirstAudioContent(pc_->local_description()->description());
2301 ASSERT_TRUE(content != NULL);
2302 EXPECT_FALSE(content->rejected);
2303
2304 content =
2305 cricket::GetFirstVideoContent(pc_->local_description()->description());
2306 ASSERT_TRUE(content != NULL);
2307 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002308#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 content =
2310 cricket::GetFirstDataContent(pc_->local_description()->description());
2311 ASSERT_TRUE(content != NULL);
Zhi Huange830e682018-03-30 10:48:35 -07002312 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002313#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314}
2315
zhihuangb19012e2017-09-19 13:47:59 -07002316// Test that fallback from DTLS to SDES is not supported.
2317// The fallback was previously supported but was removed to simplify the code
2318// and because it's non-standard.
Steve Anton36da6ff2018-02-16 16:04:20 -08002319TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002320 RTCConfiguration rtc_config;
2321 rtc_config.enable_dtls_srtp = true;
2322 CreatePeerConnection(rtc_config);
deadbeef8662f942017-01-20 21:20:51 -08002323 // Wait for fake certificate to be generated. Previously, this is what caused
2324 // the "a=crypto" lines to be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08002325 AddAudioTrack("audio_label");
2326 AddVideoTrack("video_label");
deadbeef8662f942017-01-20 21:20:51 -08002327 ASSERT_NE(nullptr, fake_certificate_generator_);
2328 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2329 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002330 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002331 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2332 nullptr));
Zhi Huange830e682018-03-30 10:48:35 -07002333 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
deadbeef8662f942017-01-20 21:20:51 -08002334}
2335
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336// Test that we can create an audio only offer and receive an answer with a
2337// limited set of audio codecs and receive an updated offer with more audio
2338// codecs, where the added codecs are not supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002339TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002340 CreatePeerConnectionWithoutDtls();
Steve Anton36da6ff2018-02-16 16:04:20 -08002341 AddAudioTrack("audio_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 CreateOfferAsLocalDescription();
2343
Steve Anton36da6ff2018-02-16 16:04:20 -08002344 const char* answer_sdp =
2345 (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2346 : webrtc::kAudioSdpUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002347 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002348 webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002349 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350
Steve Anton36da6ff2018-02-16 16:04:20 -08002351 const char* reoffer_sdp =
2352 (sdp_semantics_ == SdpSemantics::kPlanB
2353 ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2354 : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
Steve Antondb45ca82017-09-11 18:27:34 -07002355 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002356 webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002357 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 CreateAnswerAsLocalDescription();
2359}
2360
deadbeefc80741f2015-10-22 13:14:45 -07002361// Test that if we're receiving (but not sending) a track, subsequent offers
2362// will have m-lines with a=recvonly.
Steve Anton36da6ff2018-02-16 16:04:20 -08002363TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002364 RTCConfiguration rtc_config;
2365 rtc_config.enable_dtls_srtp = true;
2366 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002367 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002368 CreateAnswerAsLocalDescription();
2369
2370 // At this point we should be receiving stream 1, but not sending anything.
2371 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002372 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002373 DoCreateOffer(&offer, nullptr);
2374
2375 const cricket::ContentInfo* video_content =
2376 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002377 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2378 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002379
2380 const cricket::ContentInfo* audio_content =
2381 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002382 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2383 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002384}
2385
2386// Test that if we're receiving (but not sending) a track, and the
2387// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2388// false, the generated m-lines will be a=inactive.
Steve Anton36da6ff2018-02-16 16:04:20 -08002389TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002390 RTCConfiguration rtc_config;
2391 rtc_config.enable_dtls_srtp = true;
2392 CreatePeerConnection(rtc_config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002393 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefc80741f2015-10-22 13:14:45 -07002394 CreateAnswerAsLocalDescription();
2395
2396 // At this point we should be receiving stream 1, but not sending anything.
2397 // A new offer would be recvonly, but we'll set the "no receive" constraints
2398 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002399 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02002400 RTCOfferAnswerOptions options;
2401 options.offer_to_receive_audio = 0;
2402 options.offer_to_receive_video = 0;
2403 DoCreateOffer(&offer, &options);
deadbeefc80741f2015-10-22 13:14:45 -07002404
2405 const cricket::ContentInfo* video_content =
2406 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002407 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2408 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002409
2410 const cricket::ContentInfo* audio_content =
2411 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002412 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2413 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002414}
2415
deadbeef653b8e02015-11-11 12:55:10 -08002416// Test that we can use SetConfiguration to change the ICE servers of the
2417// PortAllocator.
Steve Anton36da6ff2018-02-16 16:04:20 -08002418TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
deadbeef653b8e02015-11-11 12:55:10 -08002419 CreatePeerConnection();
2420
Steve Anton36da6ff2018-02-16 16:04:20 -08002421 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef653b8e02015-11-11 12:55:10 -08002422 PeerConnectionInterface::IceServer server;
2423 server.uri = "stun:test_hostname";
2424 config.servers.push_back(server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002425 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef653b8e02015-11-11 12:55:10 -08002426
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002427 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2428 EXPECT_EQ("test_hostname",
2429 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002430}
2431
Steve Anton36da6ff2018-02-16 16:04:20 -08002432TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002433 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002434 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002435 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002436 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002437 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2438}
2439
Steve Anton36da6ff2018-02-16 16:04:20 -08002440TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
deadbeef293e9262017-01-11 12:28:30 -08002441 PeerConnectionInterface::RTCConfiguration config;
2442 config.prune_turn_ports = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02002443 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002444 config = pc_->GetConfiguration();
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002445 EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002446
2447 config.prune_turn_ports = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002448 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Honghai Zhangf8998cf2019-10-14 11:27:50 -07002449 EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2450 port_allocator_->turn_port_prune_policy());
deadbeef293e9262017-01-11 12:28:30 -08002451}
2452
skvladd1f5fda2017-02-03 16:54:05 -08002453// Test that the ice check interval can be changed. This does not verify that
2454// the setting makes it all the way to P2PTransportChannel, as that would
2455// require a very complex set of mocks.
Steve Anton36da6ff2018-02-16 16:04:20 -08002456TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
skvladd1f5fda2017-02-03 16:54:05 -08002457 PeerConnectionInterface::RTCConfiguration config;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002458 config.ice_check_min_interval = absl::nullopt;
Niels Möllerf06f9232018-08-07 12:32:18 +02002459 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002460 config = pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002461 config.ice_check_min_interval = 100;
Niels Möller2579f0c2019-08-19 09:58:17 +02002462 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002463 config = pc_->GetConfiguration();
2464 EXPECT_EQ(config.ice_check_min_interval, 100);
2465}
2466
2467TEST_P(PeerConnectionInterfaceTest,
2468 SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2469 PeerConnectionInterface::RTCConfiguration config;
2470 config.surface_ice_candidates_on_ice_transport_type_changed = false;
2471 CreatePeerConnection(config);
2472 config = pc_->GetConfiguration();
2473 EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2474
2475 config.surface_ice_candidates_on_ice_transport_type_changed = true;
Niels Möller2579f0c2019-08-19 09:58:17 +02002476 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wangbca14852019-06-26 14:56:02 -07002477 config = pc_->GetConfiguration();
2478 EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
skvladd1f5fda2017-02-03 16:54:05 -08002479}
2480
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002481// Test that when SetConfiguration changes both the pool size and other
2482// attributes, the pooled session is created with the updated attributes.
Steve Anton36da6ff2018-02-16 16:04:20 -08002483TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002484 SetConfigurationCreatesPooledSessionCorrectly) {
2485 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08002486 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002487 config.ice_candidate_pool_size = 1;
2488 PeerConnectionInterface::IceServer server;
2489 server.uri = kStunAddressOnly;
2490 config.servers.push_back(server);
2491 config.type = PeerConnectionInterface::kRelay;
Niels Möller2579f0c2019-08-19 09:58:17 +02002492 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002493
2494 const cricket::FakePortAllocatorSession* session =
2495 static_cast<const cricket::FakePortAllocatorSession*>(
2496 port_allocator_->GetPooledSession());
2497 ASSERT_NE(nullptr, session);
2498 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002499}
2500
deadbeef293e9262017-01-11 12:28:30 -08002501// Test that after SetLocalDescription, changing the pool size is not allowed,
2502// and an invalid modification error is returned.
Steve Anton36da6ff2018-02-16 16:04:20 -08002503TEST_P(PeerConnectionInterfaceTest,
deadbeef6de92f92016-12-12 18:49:32 -08002504 CantChangePoolSizeAfterSetLocalDescription) {
2505 CreatePeerConnection();
2506 // Start by setting a size of 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08002507 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef6de92f92016-12-12 18:49:32 -08002508 config.ice_candidate_pool_size = 1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002509 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002510
2511 // Set remote offer; can still change pool size at this point.
2512 CreateOfferAsRemoteDescription();
2513 config.ice_candidate_pool_size = 2;
Niels Möller2579f0c2019-08-19 09:58:17 +02002514 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef6de92f92016-12-12 18:49:32 -08002515
2516 // Set local answer; now it's too late.
2517 CreateAnswerAsLocalDescription();
2518 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002519 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002520 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2521}
2522
deadbeef42a42632017-03-10 15:18:00 -08002523// Test that after setting an answer, extra pooled sessions are discarded. The
2524// ICE candidate pool is only intended to be used for the first offer/answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002525TEST_P(PeerConnectionInterfaceTest,
deadbeef42a42632017-03-10 15:18:00 -08002526 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2527 CreatePeerConnection();
2528
2529 // Set a larger-than-necessary size.
Steve Anton36da6ff2018-02-16 16:04:20 -08002530 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002531 config.ice_candidate_pool_size = 4;
Niels Möller2579f0c2019-08-19 09:58:17 +02002532 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002533
2534 // Do offer/answer.
2535 CreateOfferAsRemoteDescription();
2536 CreateAnswerAsLocalDescription();
2537
2538 // Expect no pooled sessions to be left.
2539 const cricket::PortAllocatorSession* session =
2540 port_allocator_->GetPooledSession();
2541 EXPECT_EQ(nullptr, session);
2542}
2543
2544// After Close is called, pooled candidates should be discarded so as to not
2545// waste network resources.
Steve Anton36da6ff2018-02-16 16:04:20 -08002546TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
deadbeef42a42632017-03-10 15:18:00 -08002547 CreatePeerConnection();
2548
Steve Anton36da6ff2018-02-16 16:04:20 -08002549 PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
deadbeef42a42632017-03-10 15:18:00 -08002550 config.ice_candidate_pool_size = 3;
Niels Möller2579f0c2019-08-19 09:58:17 +02002551 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeef42a42632017-03-10 15:18:00 -08002552 pc_->Close();
2553
2554 // Expect no pooled sessions to be left.
2555 const cricket::PortAllocatorSession* session =
2556 port_allocator_->GetPooledSession();
2557 EXPECT_EQ(nullptr, session);
2558}
2559
deadbeef293e9262017-01-11 12:28:30 -08002560// Test that SetConfiguration returns an invalid modification error if
2561// modifying a field in the configuration that isn't allowed to be modified.
Steve Anton36da6ff2018-02-16 16:04:20 -08002562TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002563 SetConfigurationReturnsInvalidModificationError) {
2564 PeerConnectionInterface::RTCConfiguration config;
2565 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2566 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2567 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
Niels Möllerf06f9232018-08-07 12:32:18 +02002568 CreatePeerConnection(config);
deadbeef293e9262017-01-11 12:28:30 -08002569
Steve Anton36da6ff2018-02-16 16:04:20 -08002570 PeerConnectionInterface::RTCConfiguration modified_config =
2571 pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002572 modified_config.bundle_policy =
2573 PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möller2579f0c2019-08-19 09:58:17 +02002574 RTCError error = pc_->SetConfiguration(modified_config);
deadbeef293e9262017-01-11 12:28:30 -08002575 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2576
Steve Anton36da6ff2018-02-16 16:04:20 -08002577 modified_config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002578 modified_config.rtcp_mux_policy =
2579 PeerConnectionInterface::kRtcpMuxPolicyRequire;
Niels Möller2579f0c2019-08-19 09:58:17 +02002580 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.continual_gathering_policy =
2585 PeerConnectionInterface::GATHER_CONTINUALLY;
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}
2589
2590// Test that SetConfiguration returns a range error if the candidate pool size
2591// is negative or larger than allowed by the spec.
Steve Anton36da6ff2018-02-16 16:04:20 -08002592TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002593 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2594 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002595 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002596 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002597
2598 config.ice_candidate_pool_size = -1;
Niels Möller2579f0c2019-08-19 09:58:17 +02002599 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002600 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2601
2602 config.ice_candidate_pool_size = INT_MAX;
Niels Möller2579f0c2019-08-19 09:58:17 +02002603 error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002604 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2605}
2606
2607// Test that SetConfiguration returns a syntax error if parsing an ICE server
2608// URL failed.
Steve Anton36da6ff2018-02-16 16:04:20 -08002609TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002610 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2611 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002612 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002613 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002614
2615 PeerConnectionInterface::IceServer bad_server;
2616 bad_server.uri = "stunn:www.example.com";
2617 config.servers.push_back(bad_server);
Niels Möller2579f0c2019-08-19 09:58:17 +02002618 RTCError error = pc_->SetConfiguration(config);
deadbeef293e9262017-01-11 12:28:30 -08002619 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2620}
2621
2622// Test that SetConfiguration returns an invalid parameter error if a TURN
2623// IceServer is missing a username or password.
Steve Anton36da6ff2018-02-16 16:04:20 -08002624TEST_P(PeerConnectionInterfaceTest,
deadbeef293e9262017-01-11 12:28:30 -08002625 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2626 PeerConnectionInterface::RTCConfiguration config;
Niels Möllerf06f9232018-08-07 12:32:18 +02002627 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002628 config = pc_->GetConfiguration();
deadbeef293e9262017-01-11 12:28:30 -08002629
2630 PeerConnectionInterface::IceServer bad_server;
2631 bad_server.uri = "turn:www.example.com";
2632 // Missing password.
2633 bad_server.username = "foo";
2634 config.servers.push_back(bad_server);
2635 RTCError error;
Niels Möller340e0c52019-08-26 11:03:47 +02002636 EXPECT_EQ(pc_->SetConfiguration(config).type(),
2637 RTCErrorType::INVALID_PARAMETER);
deadbeef6de92f92016-12-12 18:49:32 -08002638}
2639
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640// Test that PeerConnection::Close changes the states to closed and all remote
2641// tracks change state to ended.
Steve Anton36da6ff2018-02-16 16:04:20 -08002642TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643 // Initialize a PeerConnection and negotiate local and remote session
2644 // description.
2645 InitiateCall();
Steve Anton36da6ff2018-02-16 16:04:20 -08002646
2647 // With Plan B, verify the stream count. The analog with Unified Plan is the
2648 // RtpTransceiver count.
2649 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2650 ASSERT_EQ(1u, pc_->local_streams()->count());
2651 ASSERT_EQ(1u, pc_->remote_streams()->count());
2652 } else {
2653 ASSERT_EQ(2u, pc_->GetTransceivers().size());
2654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002655
2656 pc_->Close();
2657
2658 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2659 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2660 pc_->ice_connection_state());
2661 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2662 pc_->ice_gathering_state());
2663
Steve Anton36da6ff2018-02-16 16:04:20 -08002664 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2665 EXPECT_EQ(1u, pc_->local_streams()->count());
2666 EXPECT_EQ(1u, pc_->remote_streams()->count());
2667 } else {
2668 // Verify that the RtpTransceivers are still present but all stopped.
2669 EXPECT_EQ(2u, pc_->GetTransceivers().size());
Mirko Bonadei739baf02019-01-27 17:29:42 +01002670 for (const auto& transceiver : pc_->GetTransceivers()) {
Steve Anton36da6ff2018-02-16 16:04:20 -08002671 EXPECT_TRUE(transceiver->stopped());
2672 }
2673 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674
Steve Anton36da6ff2018-02-16 16:04:20 -08002675 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2676 ASSERT_TRUE(audio_receiver);
2677 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2678 ASSERT_TRUE(video_receiver);
2679
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002680 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002681 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002682 audio_receiver->track()->state(), kTimeout);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002683 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Steve Anton36da6ff2018-02-16 16:04:20 -08002684 video_receiver->track()->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002685}
2686
2687// Test that PeerConnection methods fails gracefully after
2688// PeerConnection::Close has been called.
Steve Anton36da6ff2018-02-16 16:04:20 -08002689// Don't run under Unified Plan since the stream API is not available.
2690TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002691 CreatePeerConnectionWithoutDtls();
Seth Hampson845e8782018-03-02 11:34:10 -08002692 AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693 CreateOfferAsRemoteDescription();
2694 CreateAnswerAsLocalDescription();
2695
2696 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002697 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002698 pc_->local_streams()->at(0);
2699
2700 pc_->Close();
2701
2702 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002703 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002704
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002705 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2706
2707 EXPECT_TRUE(pc_->local_description() != NULL);
2708 EXPECT_TRUE(pc_->remote_description() != NULL);
2709
kwibergd1fe2812016-04-27 06:47:29 -07002710 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002711 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002712 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002713 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714
2715 std::string sdp;
2716 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002717 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002718 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002719 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720
2721 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002722 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002723 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002724 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002725}
2726
2727// Test that GetStats can still be called after PeerConnection::Close.
Steve Anton36da6ff2018-02-16 16:04:20 -08002728TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002729 InitiateCall();
2730 pc_->Close();
2731 DoGetStats(NULL);
2732}
deadbeefab9b2d12015-10-14 11:33:11 -07002733
2734// NOTE: The series of tests below come from what used to be
2735// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2736// setting a remote or local description has the expected effects.
2737
2738// This test verifies that the remote MediaStreams corresponding to a received
2739// SDP string is created. In this test the two separate MediaStreams are
2740// signaled.
Steve Anton36da6ff2018-02-16 16:04:20 -08002741TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002742 RTCConfiguration config;
2743 config.enable_dtls_srtp = true;
2744 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002745 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002746
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002747 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002748 EXPECT_TRUE(
2749 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2750 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2751 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2752
2753 // Create a session description based on another SDP with another
2754 // MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002755 CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
deadbeefab9b2d12015-10-14 11:33:11 -07002756
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002757 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002758 EXPECT_TRUE(
2759 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2760}
2761
2762// This test verifies that when remote tracks are added/removed from SDP, the
2763// created remote streams are updated appropriately.
Steve Anton36da6ff2018-02-16 16:04:20 -08002764// Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2765// specific behavior.
2766TEST_F(PeerConnectionInterfaceTestPlanB,
deadbeefab9b2d12015-10-14 11:33:11 -07002767 AddRemoveTrackFromExistingRemoteMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002768 RTCConfiguration config;
2769 config.enable_dtls_srtp = true;
2770 CreatePeerConnection(config);
kwibergd1fe2812016-04-27 06:47:29 -07002771 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002772 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002773 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002774 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2775 reference_collection_));
2776
2777 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002778 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002779 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002780 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002781 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2782 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002783 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002784 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2785 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002786 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002787 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2788 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002789
2790 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002791 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002792 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002793 MockTrackObserver audio_track_observer(audio_track2);
2794 MockTrackObserver video_track_observer(video_track2);
2795
2796 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2797 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002798 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002799 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2800 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002801 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002802 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002803 audio_track2->state(), kTimeout);
2804 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2805 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002806}
2807
2808// This tests that remote tracks are ended if a local session description is set
2809// that rejects the media content type.
Steve Anton36da6ff2018-02-16 16:04:20 -08002810TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002811 RTCConfiguration config;
2812 config.enable_dtls_srtp = true;
2813 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002814 // First create and set a remote offer, then reject its video content in our
2815 // answer.
Steve Anton36da6ff2018-02-16 16:04:20 -08002816 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2817 auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2818 ASSERT_TRUE(audio_receiver);
2819 auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2820 ASSERT_TRUE(video_receiver);
deadbeefab9b2d12015-10-14 11:33:11 -07002821
Steve Anton36da6ff2018-02-16 16:04:20 -08002822 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2823 audio_receiver->track();
deadbeefab9b2d12015-10-14 11:33:11 -07002824 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
Steve Anton36da6ff2018-02-16 16:04:20 -08002825 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2826 video_receiver->track();
2827 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002828
kwibergd1fe2812016-04-27 06:47:29 -07002829 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002830 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002831 cricket::ContentInfo* video_info =
2832 local_answer->description()->GetContentByName("video");
2833 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002834 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
Steve Anton36da6ff2018-02-16 16:04:20 -08002835 EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2836 EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002837
2838 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002839 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002840 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002841 video_info = local_offer->description()->GetContentByName("video");
2842 ASSERT_TRUE(video_info != nullptr);
2843 video_info->rejected = true;
2844 cricket::ContentInfo* audio_info =
2845 local_offer->description()->GetContentByName("audio");
2846 ASSERT_TRUE(audio_info != nullptr);
2847 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002848 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002849 // Track state may be updated asynchronously.
Steve Anton36da6ff2018-02-16 16:04:20 -08002850 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2851 kTimeout);
2852 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2853 kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002854}
2855
2856// This tests that we won't crash if the remote track has been removed outside
2857// of PeerConnection and then PeerConnection tries to reject the track.
Steve Anton36da6ff2018-02-16 16:04:20 -08002858// Don't run under Unified Plan since the stream API is not available.
2859TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002860 RTCConfiguration config;
2861 config.enable_dtls_srtp = true;
2862 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002863 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002864 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2865 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2866 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2867
kwibergd1fe2812016-04-27 06:47:29 -07002868 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Anton36da6ff2018-02-16 16:04:20 -08002869 webrtc::CreateSessionDescription(SdpType::kAnswer,
2870 GetSdpStringWithStream1(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002871 cricket::ContentInfo* video_info =
2872 local_answer->description()->GetContentByName("video");
2873 video_info->rejected = true;
2874 cricket::ContentInfo* audio_info =
2875 local_answer->description()->GetContentByName("audio");
2876 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002877 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002878
2879 // No crash is a pass.
2880}
2881
deadbeef5e97fb52015-10-15 12:49:08 -07002882// This tests that if a recvonly remote description is set, no remote streams
2883// will be created, even if the description contains SSRCs/MSIDs.
2884// See: https://code.google.com/p/webrtc/issues/detail?id=5054
Steve Anton36da6ff2018-02-16 16:04:20 -08002885TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002886 RTCConfiguration config;
2887 config.enable_dtls_srtp = true;
2888 CreatePeerConnection(config);
deadbeef5e97fb52015-10-15 12:49:08 -07002889
Steve Anton36da6ff2018-02-16 16:04:20 -08002890 std::string recvonly_offer = GetSdpStringWithStream1();
Steve Anton1c9c9fc2019-02-14 15:13:09 -08002891 absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
deadbeef5e97fb52015-10-15 12:49:08 -07002892 CreateAndSetRemoteOffer(recvonly_offer);
2893
2894 EXPECT_EQ(0u, observer_.remote_streams()->count());
2895}
2896
deadbeefab9b2d12015-10-14 11:33:11 -07002897// This tests that a default MediaStream is created if a remote session
2898// description doesn't contain any streams and no MSID support.
2899// It also tests that the default stream is updated if a video m-line is added
2900// in a subsequent session description.
Steve Anton36da6ff2018-02-16 16:04:20 -08002901// Don't run under Unified Plan since this behavior is Plan B specific.
2902TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002903 RTCConfiguration config;
2904 config.enable_dtls_srtp = true;
2905 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002906 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2907
2908 ASSERT_EQ(1u, observer_.remote_streams()->count());
2909 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2910
2911 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2912 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002913 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002914
2915 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2916 ASSERT_EQ(1u, observer_.remote_streams()->count());
2917 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2918 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002919 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2920 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002921 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2922 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002923 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2924 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002925}
2926
2927// This tests that a default MediaStream is created if a remote session
2928// description doesn't contain any streams and media direction is send only.
Steve Anton36da6ff2018-02-16 16:04:20 -08002929// Don't run under Unified Plan since this behavior is Plan B specific.
2930TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002931 SendOnlySdpWithoutMsidCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002932 RTCConfiguration config;
2933 config.enable_dtls_srtp = true;
2934 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002935 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2936
2937 ASSERT_EQ(1u, observer_.remote_streams()->count());
2938 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2939
2940 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2941 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
Seth Hampson13b8bad2018-03-13 16:05:28 -07002942 EXPECT_EQ("default", remote_stream->id());
deadbeefab9b2d12015-10-14 11:33:11 -07002943}
2944
2945// This tests that it won't crash when PeerConnection tries to remove
2946// a remote track that as already been removed from the MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08002947// Don't run under Unified Plan since this behavior is Plan B specific.
2948TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002949 RTCConfiguration config;
2950 config.enable_dtls_srtp = true;
2951 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08002952 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
deadbeefab9b2d12015-10-14 11:33:11 -07002953 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2954 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2955 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2956
2957 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2958
2959 // No crash is a pass.
2960}
2961
2962// This tests that a default MediaStream is created if the remote session
2963// description doesn't contain any streams and don't contain an indication if
2964// MSID is supported.
Steve Anton36da6ff2018-02-16 16:04:20 -08002965// Don't run under Unified Plan since this behavior is Plan B specific.
2966TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002967 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002968 RTCConfiguration config;
2969 config.enable_dtls_srtp = true;
2970 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002971 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2972
2973 ASSERT_EQ(1u, observer_.remote_streams()->count());
2974 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2975 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2976 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2977}
2978
2979// This tests that a default MediaStream is not created if the remote session
2980// description doesn't contain any streams but does support MSID.
Steve Anton36da6ff2018-02-16 16:04:20 -08002981// Don't run under Unified Plan since this behavior is Plan B specific.
2982TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002983 RTCConfiguration config;
2984 config.enable_dtls_srtp = true;
2985 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07002986 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2987 EXPECT_EQ(0u, observer_.remote_streams()->count());
2988}
2989
deadbeefbda7e0b2015-12-08 17:13:40 -08002990// This tests that when setting a new description, the old default tracks are
2991// not destroyed and recreated.
2992// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Steve Anton36da6ff2018-02-16 16:04:20 -08002993// Don't run under Unified Plan since this behavior is Plan B specific.
2994TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002995 DefaultTracksNotDestroyedAndRecreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002996 RTCConfiguration config;
2997 config.enable_dtls_srtp = true;
2998 CreatePeerConnection(config);
deadbeefbda7e0b2015-12-08 17:13:40 -08002999 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3000
3001 ASSERT_EQ(1u, observer_.remote_streams()->count());
3002 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3003 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3004
3005 // Set the track to "disabled", then set a new description and ensure the
3006 // track is still disabled, which ensures it hasn't been recreated.
3007 remote_stream->GetAudioTracks()[0]->set_enabled(false);
3008 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3009 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3010 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3011}
3012
deadbeefab9b2d12015-10-14 11:33:11 -07003013// This tests that a default MediaStream is not created if a remote session
3014// description is updated to not have any MediaStreams.
Steve Anton36da6ff2018-02-16 16:04:20 -08003015// Don't run under Unified Plan since this behavior is Plan B specific.
3016TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003017 RTCConfiguration config;
3018 config.enable_dtls_srtp = true;
3019 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003020 CreateAndSetRemoteOffer(GetSdpStringWithStream1());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003021 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07003022 EXPECT_TRUE(
3023 CompareStreamCollections(observer_.remote_streams(), reference.get()));
3024
3025 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3026 EXPECT_EQ(0u, observer_.remote_streams()->count());
3027}
3028
Seth Hampson5897a6e2018-04-03 11:16:33 -07003029// This tests that a default MediaStream is created if a remote SDP comes from
3030// an endpoint that doesn't signal SSRCs, but signals media stream IDs.
3031TEST_F(PeerConnectionInterfaceTestPlanB,
3032 SdpWithMsidWithoutSsrcCreatesDefaultStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003033 RTCConfiguration config;
3034 config.enable_dtls_srtp = true;
3035 CreatePeerConnection(config);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003036 std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3037 // Add a=msid lines to simulate a Unified Plan endpoint that only
3038 // signals stream IDs with a=msid lines.
3039 sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3040
3041 CreateAndSetRemoteOffer(sdp_string);
3042
3043 ASSERT_EQ(1u, observer_.remote_streams()->count());
3044 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3045 EXPECT_EQ("default", remote_stream->id());
3046 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3047}
3048
Seth Hampson5b4f0752018-04-02 16:31:36 -07003049// This tests that when a Plan B endpoint receives an SDP that signals no media
3050// stream IDs indicated by the special character "-" in the a=msid line, that
3051// a default stream ID will be used for the MediaStream ID. This can occur
3052// when a Unified Plan endpoint signals no media stream IDs, but signals both
3053// a=ssrc msid and a=msid lines for interop signaling with Plan B.
3054TEST_F(PeerConnectionInterfaceTestPlanB,
3055 SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003056 RTCConfiguration config;
3057 config.enable_dtls_srtp = true;
3058 CreatePeerConnection(config);
Seth Hampson5b4f0752018-04-02 16:31:36 -07003059 // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3060 // the sender's stream ID will be interpreted as no stream IDs.
3061 std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3062 sdp_string.append("a=msid:- audiotrack0\n");
3063
3064 CreateAndSetRemoteOffer(sdp_string);
3065
3066 ASSERT_EQ(1u, observer_.remote_streams()->count());
3067 // Because SSRCs are signaled the track ID will be what was signaled in the
3068 // a=msid line.
3069 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3070 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3071 EXPECT_EQ("default", remote_stream->id());
3072 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson83d676b2018-04-05 18:12:09 -07003073
3074 // Previously a bug ocurred when setting the remote description a second time.
3075 // This is because we checked equality of the remote StreamParams stream ID
3076 // (empty), and the previously set stream ID for the remote sender
3077 // ("default"). This cause a track to be removed, then added, when really
3078 // nothing should occur because it is the same track.
3079 CreateAndSetRemoteOffer(sdp_string);
3080 EXPECT_EQ(0u, observer_.remove_track_events_.size());
3081 EXPECT_EQ(1u, observer_.add_track_events_.size());
3082 EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3083 remote_stream = observer_.remote_streams()->at(0);
3084 EXPECT_EQ("default", remote_stream->id());
3085 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
Seth Hampson5b4f0752018-04-02 16:31:36 -07003086}
3087
deadbeefab9b2d12015-10-14 11:33:11 -07003088// This tests that an RtpSender is created when the local description is set
3089// after adding a local stream.
3090// TODO(deadbeef): This test and the one below it need to be updated when
3091// an RtpSender's lifetime isn't determined by when a local description is set.
Steve Anton36da6ff2018-02-16 16:04:20 -08003092// Don't run under Unified Plan since this behavior is Plan B specific.
3093TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003094 RTCConfiguration config;
3095 config.enable_dtls_srtp = true;
3096 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003097
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003098 // Create an offer with 1 stream with 2 tracks of each type.
3099 rtc::scoped_refptr<StreamCollection> stream_collection =
3100 CreateStreamCollection(1, 2);
3101 pc_->AddStream(stream_collection->at(0));
3102 std::unique_ptr<SessionDescriptionInterface> offer;
3103 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003104 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003105
deadbeefab9b2d12015-10-14 11:33:11 -07003106 auto senders = pc_->GetSenders();
3107 EXPECT_EQ(4u, senders.size());
3108 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3109 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3110 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3111 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3112
3113 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003114 pc_->RemoveStream(stream_collection->at(0));
3115 stream_collection = CreateStreamCollection(1, 1);
3116 pc_->AddStream(stream_collection->at(0));
3117 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003118 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003119
deadbeefab9b2d12015-10-14 11:33:11 -07003120 senders = pc_->GetSenders();
3121 EXPECT_EQ(2u, senders.size());
3122 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3123 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3124 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3125 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3126}
3127
3128// This tests that an RtpSender is created when the local description is set
3129// before adding a local stream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003130// Don't run under Unified Plan since this behavior is Plan B specific.
3131TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003132 AddLocalStreamAfterLocalDescriptionChanged) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003133 RTCConfiguration config;
3134 config.enable_dtls_srtp = true;
3135 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003136
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003137 rtc::scoped_refptr<StreamCollection> stream_collection =
3138 CreateStreamCollection(1, 2);
3139 // Add a stream to create the offer, but remove it afterwards.
3140 pc_->AddStream(stream_collection->at(0));
3141 std::unique_ptr<SessionDescriptionInterface> offer;
3142 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3143 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003144
Steve Antondb45ca82017-09-11 18:27:34 -07003145 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003146 auto senders = pc_->GetSenders();
3147 EXPECT_EQ(0u, senders.size());
3148
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003149 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003150 senders = pc_->GetSenders();
3151 EXPECT_EQ(4u, senders.size());
3152 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3153 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3154 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3155 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3156}
3157
3158// This tests that the expected behavior occurs if the SSRC on a local track is
3159// changed when SetLocalDescription is called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003160TEST_P(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003161 ChangeSsrcOnTrackInLocalSessionDescription) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003162 RTCConfiguration config;
3163 config.enable_dtls_srtp = true;
3164 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003165
Steve Anton36da6ff2018-02-16 16:04:20 -08003166 AddAudioTrack(kAudioTracks[0]);
3167 AddVideoTrack(kVideoTracks[0]);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003168 std::unique_ptr<SessionDescriptionInterface> offer;
3169 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3170 // Grab a copy of the offer before it gets passed into the PC.
Steve Antond9e4a062018-07-24 18:23:33 -07003171 std::unique_ptr<SessionDescriptionInterface> modified_offer =
3172 webrtc::CreateSessionDescription(
3173 webrtc::SdpType::kOffer, offer->session_id(),
Harald Alvestrand4d7160e2019-04-12 07:01:29 +02003174 offer->session_version(), offer->description()->Clone());
Steve Antondb45ca82017-09-11 18:27:34 -07003175 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003176
deadbeefab9b2d12015-10-14 11:33:11 -07003177 auto senders = pc_->GetSenders();
3178 EXPECT_EQ(2u, senders.size());
3179 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3180 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3181
3182 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003183 cricket::MediaContentDescription* desc =
3184 cricket::GetFirstAudioContentDescription(modified_offer->description());
3185 ASSERT_TRUE(desc != NULL);
3186 for (StreamParams& stream : desc->mutable_streams()) {
3187 for (unsigned int& ssrc : stream.ssrcs) {
3188 ++ssrc;
3189 }
3190 }
deadbeefab9b2d12015-10-14 11:33:11 -07003191
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003192 desc =
3193 cricket::GetFirstVideoContentDescription(modified_offer->description());
3194 ASSERT_TRUE(desc != NULL);
3195 for (StreamParams& stream : desc->mutable_streams()) {
3196 for (unsigned int& ssrc : stream.ssrcs) {
3197 ++ssrc;
3198 }
3199 }
3200
Steve Antondb45ca82017-09-11 18:27:34 -07003201 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003202 senders = pc_->GetSenders();
3203 EXPECT_EQ(2u, senders.size());
3204 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3205 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3206 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3207 // changed.
3208}
3209
3210// This tests that the expected behavior occurs if a new session description is
3211// set with the same tracks, but on a different MediaStream.
Steve Anton36da6ff2018-02-16 16:04:20 -08003212// Don't run under Unified Plan since the stream API is not available.
3213TEST_F(PeerConnectionInterfaceTestPlanB,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003214 SignalSameTracksInSeparateMediaStream) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003215 RTCConfiguration config;
3216 config.enable_dtls_srtp = true;
3217 CreatePeerConnection(config);
deadbeefab9b2d12015-10-14 11:33:11 -07003218
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003219 rtc::scoped_refptr<StreamCollection> stream_collection =
3220 CreateStreamCollection(2, 1);
3221 pc_->AddStream(stream_collection->at(0));
3222 std::unique_ptr<SessionDescriptionInterface> offer;
3223 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003224 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003225
deadbeefab9b2d12015-10-14 11:33:11 -07003226 auto senders = pc_->GetSenders();
3227 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003228 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3229 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003230
3231 // Add a new MediaStream but with the same tracks as in the first stream.
3232 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3233 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003234 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3235 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003236 pc_->AddStream(stream_1);
3237
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003238 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003239 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003240
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003241 auto new_senders = pc_->GetSenders();
3242 // Should be the same senders as before, but with updated stream id.
3243 // Note that this behavior is subject to change in the future.
3244 // We may decide the PC should ignore existing tracks in AddStream.
3245 EXPECT_EQ(senders, new_senders);
3246 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3247 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003248}
3249
zhihuang81c3a032016-11-17 12:06:24 -08003250// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
Steve Anton36da6ff2018-02-16 16:04:20 -08003251TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003252 RTCConfiguration config;
3253 config.enable_dtls_srtp = true;
3254 CreatePeerConnection(config);
zhihuang81c3a032016-11-17 12:06:24 -08003255 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3256 EXPECT_EQ(observer_.num_added_tracks_, 1);
3257 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3258
3259 // Create and set the updated remote SDP.
Steve Anton36da6ff2018-02-16 16:04:20 -08003260 CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
Steve Anton0f5400a2018-07-17 14:25:36 -07003261 EXPECT_EQ(observer_.num_added_tracks_, 2);
zhihuang81c3a032016-11-17 12:06:24 -08003262 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3263}
3264
deadbeefd1a38b52016-12-10 13:15:33 -08003265// Test that when SetConfiguration is called and the configuration is
3266// changing, the next offer causes an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003267TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003268 PeerConnectionInterface::RTCConfiguration config;
3269 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003270 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003271 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003272 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3273 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003274
3275 // Do initial offer/answer so there's something to restart.
3276 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003277 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003278
3279 // Grab the ufrags.
3280 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3281
3282 // Change ICE policy, which should trigger an ICE restart on the next offer.
3283 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003284 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003285 CreateOfferAsLocalDescription();
3286
3287 // Grab the new ufrags.
3288 std::vector<std::string> subsequent_ufrags =
3289 GetUfrags(pc_->local_description());
3290
3291 // Sanity check.
3292 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3293 // Check that each ufrag is different.
3294 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3295 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3296 }
3297}
3298
3299// Test that when SetConfiguration is called and the configuration *isn't*
3300// changing, the next offer does *not* cause an ICE restart.
Steve Anton36da6ff2018-02-16 16:04:20 -08003301TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003302 PeerConnectionInterface::RTCConfiguration config;
3303 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003304 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003305 config = pc_->GetConfiguration();
3306 AddAudioTrack(kAudioTracks[0]);
3307 AddVideoTrack(kVideoTracks[0]);
deadbeefd1a38b52016-12-10 13:15:33 -08003308
3309 // Do initial offer/answer so there's something to restart.
3310 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003311 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003312
3313 // Grab the ufrags.
3314 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3315
3316 // Call SetConfiguration with a config identical to what the PC was
3317 // constructed with.
Niels Möller2579f0c2019-08-19 09:58:17 +02003318 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003319 CreateOfferAsLocalDescription();
3320
3321 // Grab the new ufrags.
3322 std::vector<std::string> subsequent_ufrags =
3323 GetUfrags(pc_->local_description());
3324
3325 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3326}
3327
3328// Test for a weird corner case scenario:
3329// 1. Audio/video session established.
3330// 2. SetConfiguration changes ICE config; ICE restart needed.
3331// 3. ICE restart initiated by remote peer, but only for one m= section.
3332// 4. Next createOffer should initiate an ICE restart, but only for the other
3333// m= section; it would be pointless to do an ICE restart for the m= section
3334// that was already restarted.
Steve Anton36da6ff2018-02-16 16:04:20 -08003335TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
deadbeefd1a38b52016-12-10 13:15:33 -08003336 PeerConnectionInterface::RTCConfiguration config;
3337 config.type = PeerConnectionInterface::kRelay;
Niels Möllerf06f9232018-08-07 12:32:18 +02003338 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003339 config = pc_->GetConfiguration();
Seth Hampson845e8782018-03-02 11:34:10 -08003340 AddAudioTrack(kAudioTracks[0], {kStreamId1});
3341 AddVideoTrack(kVideoTracks[0], {kStreamId1});
deadbeefd1a38b52016-12-10 13:15:33 -08003342
3343 // Do initial offer/answer so there's something to restart.
3344 CreateOfferAsLocalDescription();
Steve Anton36da6ff2018-02-16 16:04:20 -08003345 CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
deadbeefd1a38b52016-12-10 13:15:33 -08003346
3347 // Change ICE policy, which should set the "needs-ice-restart" flag.
3348 config.type = PeerConnectionInterface::kAll;
Niels Möller2579f0c2019-08-19 09:58:17 +02003349 EXPECT_TRUE(pc_->SetConfiguration(config).ok());
deadbeefd1a38b52016-12-10 13:15:33 -08003350
3351 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003352 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Anton36da6ff2018-02-16 16:04:20 -08003353 webrtc::CreateSessionDescription(SdpType::kOffer,
3354 GetSdpStringWithStream1(), nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003355 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003356 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3357 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003358 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003359 CreateAnswerAsLocalDescription();
3360
3361 // Grab the ufrags.
3362 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003363 ASSERT_EQ(2U, initial_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003364
3365 // Create offer and grab the new ufrags.
3366 CreateOfferAsLocalDescription();
3367 std::vector<std::string> subsequent_ufrags =
3368 GetUfrags(pc_->local_description());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003369 ASSERT_EQ(2U, subsequent_ufrags.size());
deadbeefd1a38b52016-12-10 13:15:33 -08003370
3371 // Ensure that only the ufrag for the second m= section changed.
3372 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3373 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3374}
3375
deadbeeffe4a8a42016-12-20 17:56:17 -08003376// Tests that the methods to return current/pending descriptions work as
3377// expected at different points in the offer/answer exchange. This test does
3378// one offer/answer exchange as the offerer, then another as the answerer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003379TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
deadbeeffe4a8a42016-12-20 17:56:17 -08003380 // This disables DTLS so we can apply an answer to ourselves.
3381 CreatePeerConnection();
3382
3383 // Create initial local offer and get SDP (which will also be used as
3384 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003385 std::unique_ptr<SessionDescriptionInterface> local_offer;
3386 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003387 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003388 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003389
3390 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003391 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3392 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3393 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003394 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3395 EXPECT_EQ(nullptr, pc_->current_local_description());
3396 EXPECT_EQ(nullptr, pc_->current_remote_description());
3397
3398 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003399 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003400 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003401 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3402 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3403 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3404 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003405 EXPECT_EQ(nullptr, pc_->current_local_description());
3406 EXPECT_EQ(nullptr, pc_->current_remote_description());
3407
3408 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003409 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003410 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003411 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3412 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003413 EXPECT_EQ(nullptr, pc_->pending_local_description());
3414 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003415 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3416 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003417
3418 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003419 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003420 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003421 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3422 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3423 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003424 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003425 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3426 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003427
3428 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003429 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003430 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003431 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3432 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3433 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3434 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3435 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3436 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003437
3438 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003439 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003440 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003441 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3442 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003443 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3444 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003445 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3446 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003447}
3448
zhihuang77985012017-02-07 15:45:16 -08003449// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3450// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003451// This version tests the StartRtcEventLog version that receives an object
3452// of type |RtcEventLogOutput|.
Steve Anton36da6ff2018-02-16 16:04:20 -08003453TEST_P(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003454 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3455 CreatePeerConnection();
3456 // The RtcEventLog will be reset when the PeerConnection is closed.
3457 pc_->Close();
3458
Niels Möllerdec9f742019-06-03 15:25:20 +02003459 EXPECT_FALSE(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003460 pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
Niels Möllerdec9f742019-06-03 15:25:20 +02003461 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003462 pc_->StopRtcEventLog();
3463}
3464
deadbeef30952b42017-04-21 02:41:29 -07003465// Test that generated offers/answers include "ice-option:trickle".
Steve Anton36da6ff2018-02-16 16:04:20 -08003466TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
deadbeef30952b42017-04-21 02:41:29 -07003467 CreatePeerConnection();
3468
3469 // First, create an offer with audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003470 RTCOfferAnswerOptions options;
3471 options.offer_to_receive_audio = 1;
3472 options.offer_to_receive_video = 1;
deadbeef30952b42017-04-21 02:41:29 -07003473 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003474 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003475 cricket::SessionDescription* desc = offer->description();
3476 ASSERT_EQ(2u, desc->transport_infos().size());
3477 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3478 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3479
3480 // Apply the offer as a remote description, then create an answer.
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003481 EXPECT_FALSE(pc_->can_trickle_ice_candidates());
Steve Antondb45ca82017-09-11 18:27:34 -07003482 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
Harald Alvestrand61f74d92020-03-02 11:20:00 +01003483 ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3484 EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
deadbeef30952b42017-04-21 02:41:29 -07003485 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003486 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef30952b42017-04-21 02:41:29 -07003487 desc = answer->description();
3488 ASSERT_EQ(2u, desc->transport_infos().size());
3489 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3490 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3491}
3492
deadbeef1dcb1642017-03-29 21:08:16 -07003493// Test that ICE renomination isn't offered if it's not enabled in the PC's
3494// RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003495TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003496 PeerConnectionInterface::RTCConfiguration config;
3497 config.enable_ice_renomination = false;
Niels Möllerf06f9232018-08-07 12:32:18 +02003498 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003499 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003500
3501 std::unique_ptr<SessionDescriptionInterface> offer;
3502 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3503 cricket::SessionDescription* desc = offer->description();
3504 EXPECT_EQ(1u, desc->transport_infos().size());
3505 EXPECT_FALSE(
3506 desc->transport_infos()[0].description.GetIceParameters().renomination);
3507}
3508
3509// Test that the ICE renomination option is present in generated offers/answers
3510// if it's enabled in the PC's RTCConfiguration.
Steve Anton36da6ff2018-02-16 16:04:20 -08003511TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003512 PeerConnectionInterface::RTCConfiguration config;
3513 config.enable_ice_renomination = true;
Niels Möllerf06f9232018-08-07 12:32:18 +02003514 CreatePeerConnection(config);
Steve Anton36da6ff2018-02-16 16:04:20 -08003515 AddAudioTrack("foo");
deadbeef1dcb1642017-03-29 21:08:16 -07003516
3517 std::unique_ptr<SessionDescriptionInterface> offer;
3518 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3519 cricket::SessionDescription* desc = offer->description();
3520 EXPECT_EQ(1u, desc->transport_infos().size());
3521 EXPECT_TRUE(
3522 desc->transport_infos()[0].description.GetIceParameters().renomination);
3523
3524 // Set the offer as a remote description, then create an answer and ensure it
3525 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003526 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003527 std::unique_ptr<SessionDescriptionInterface> answer;
3528 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3529 desc = answer->description();
3530 EXPECT_EQ(1u, desc->transport_infos().size());
3531 EXPECT_TRUE(
3532 desc->transport_infos()[0].description.GetIceParameters().renomination);
3533}
3534
3535// Test that if CreateOffer is called with the deprecated "offer to receive
3536// audio/video" constraints, they're processed and result in an offer with
3537// audio/video sections just as if RTCOfferAnswerOptions had been used.
Steve Anton36da6ff2018-02-16 16:04:20 -08003538TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003539 CreatePeerConnection();
3540
Niels Möllerf06f9232018-08-07 12:32:18 +02003541 RTCOfferAnswerOptions options;
3542 options.offer_to_receive_audio = 1;
3543 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003544 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003545 ASSERT_TRUE(DoCreateOffer(&offer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003546
3547 cricket::SessionDescription* desc = offer->description();
3548 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3549 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3550 ASSERT_NE(nullptr, audio);
3551 ASSERT_NE(nullptr, video);
3552 EXPECT_FALSE(audio->rejected);
3553 EXPECT_FALSE(video->rejected);
3554}
3555
3556// Test that if CreateAnswer is called with the deprecated "offer to receive
3557// audio/video" constraints, they're processed and can be used to reject an
3558// offered m= section just as can be done with RTCOfferAnswerOptions;
Steve Anton36da6ff2018-02-16 16:04:20 -08003559// Don't run under Unified Plan since this behavior is not supported.
3560TEST_F(PeerConnectionInterfaceTestPlanB,
3561 CreateAnswerWithOfferToReceiveConstraints) {
deadbeef1dcb1642017-03-29 21:08:16 -07003562 CreatePeerConnection();
3563
3564 // First, create an offer with audio/video and apply it as a remote
3565 // description.
Niels Möllerf06f9232018-08-07 12:32:18 +02003566 RTCOfferAnswerOptions options;
3567 options.offer_to_receive_audio = 1;
3568 options.offer_to_receive_video = 1;
deadbeef1dcb1642017-03-29 21:08:16 -07003569 std::unique_ptr<SessionDescriptionInterface> offer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003570 ASSERT_TRUE(DoCreateOffer(&offer, &options));
Steve Antondb45ca82017-09-11 18:27:34 -07003571 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003572
3573 // Now create answer that rejects audio/video.
Niels Möllerf06f9232018-08-07 12:32:18 +02003574 options.offer_to_receive_audio = 0;
3575 options.offer_to_receive_video = 0;
deadbeef1dcb1642017-03-29 21:08:16 -07003576 std::unique_ptr<SessionDescriptionInterface> answer;
Niels Möllerf06f9232018-08-07 12:32:18 +02003577 ASSERT_TRUE(DoCreateAnswer(&answer, &options));
deadbeef1dcb1642017-03-29 21:08:16 -07003578
3579 cricket::SessionDescription* desc = answer->description();
3580 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3581 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3582 ASSERT_NE(nullptr, audio);
3583 ASSERT_NE(nullptr, video);
3584 EXPECT_TRUE(audio->rejected);
3585 EXPECT_TRUE(video->rejected);
3586}
3587
deadbeef1dcb1642017-03-29 21:08:16 -07003588// Test that negotiation can succeed with a data channel only, and with the max
3589// bundle policy. Previously there was a bug that prevented this.
Steve Anton36da6ff2018-02-16 16:04:20 -08003590#ifdef HAVE_SCTP
3591TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3592#else
3593TEST_P(PeerConnectionInterfaceTest,
3594 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3595#endif // HAVE_SCTP
deadbeef1dcb1642017-03-29 21:08:16 -07003596 PeerConnectionInterface::RTCConfiguration config;
3597 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerf06f9232018-08-07 12:32:18 +02003598 CreatePeerConnection(config);
deadbeef1dcb1642017-03-29 21:08:16 -07003599
3600 // First, create an offer with only a data channel and apply it as a remote
3601 // description.
3602 pc_->CreateDataChannel("test", nullptr);
3603 std::unique_ptr<SessionDescriptionInterface> offer;
3604 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003605 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003606
3607 // Create and set answer as well.
3608 std::unique_ptr<SessionDescriptionInterface> answer;
3609 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003610 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003611}
3612
Steve Anton36da6ff2018-02-16 16:04:20 -08003613TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
zstein4b979802017-06-02 14:37:37 -07003614 CreatePeerConnection();
3615 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003616 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003617 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3618}
3619
Steve Anton36da6ff2018-02-16 16:04:20 -08003620TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
zstein4b979802017-06-02 14:37:37 -07003621 CreatePeerConnection();
3622 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003623 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003624 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3625}
3626
Steve Anton36da6ff2018-02-16 16:04:20 -08003627TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003628 CreatePeerConnection();
3629 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003630 bitrate.min_bitrate_bps = 5;
3631 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003632 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3633}
3634
Steve Anton36da6ff2018-02-16 16:04:20 -08003635TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003636 CreatePeerConnection();
3637 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003638 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003639 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3640}
3641
Steve Anton36da6ff2018-02-16 16:04:20 -08003642TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
zstein4b979802017-06-02 14:37:37 -07003643 CreatePeerConnection();
3644 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003645 bitrate.current_bitrate_bps = 10;
3646 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003647 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3648}
3649
Steve Anton36da6ff2018-02-16 16:04:20 -08003650TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
zstein4b979802017-06-02 14:37:37 -07003651 CreatePeerConnection();
3652 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003653 bitrate.min_bitrate_bps = 10;
3654 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003655 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3656}
3657
Steve Anton36da6ff2018-02-16 16:04:20 -08003658TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
zstein4b979802017-06-02 14:37:37 -07003659 CreatePeerConnection();
3660 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003661 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003662 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3663}
3664
Niels Möller0c4f7be2018-05-07 14:01:37 +02003665// The current bitrate from BitrateSettings is currently clamped
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01003666// by Call's BitrateConstraints, which comes from the SDP or a default value.
3667// This test checks that a call to SetBitrate with a current bitrate that will
3668// be clamped succeeds.
Steve Anton36da6ff2018-02-16 16:04:20 -08003669TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
zstein4b979802017-06-02 14:37:37 -07003670 CreatePeerConnection();
3671 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003672 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003673 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3674}
3675
zhihuang1c378ed2017-08-17 14:10:50 -07003676// The following tests verify that the offer can be created correctly.
Steve Anton36da6ff2018-02-16 16:04:20 -08003677TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003678 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3679 RTCOfferAnswerOptions rtc_options;
3680
3681 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3682 // than kMaxOfferToReceiveMedia should be treated as invalid.
3683 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3684 CreatePeerConnection();
3685 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3686
3687 rtc_options.offer_to_receive_audio =
3688 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3689 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3690}
3691
Steve Anton36da6ff2018-02-16 16:04:20 -08003692TEST_P(PeerConnectionInterfaceTest,
zhihuang1c378ed2017-08-17 14:10:50 -07003693 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3694 RTCOfferAnswerOptions rtc_options;
3695
3696 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3697 // than kMaxOfferToReceiveMedia should be treated as invalid.
3698 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3699 CreatePeerConnection();
3700 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3701
3702 rtc_options.offer_to_receive_video =
3703 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3704 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3705}
3706
3707// Test that the audio and video content will be added to an offer if both
3708// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003709TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003710 RTCOfferAnswerOptions rtc_options;
3711 rtc_options.offer_to_receive_audio = 1;
3712 rtc_options.offer_to_receive_video = 1;
3713
3714 std::unique_ptr<SessionDescriptionInterface> offer;
3715 CreatePeerConnection();
3716 offer = CreateOfferWithOptions(rtc_options);
3717 ASSERT_TRUE(offer);
3718 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3719 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3720}
3721
3722// Test that only audio content will be added to the offer if only
3723// |offer_to_receive_audio| options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003724TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003725 RTCOfferAnswerOptions rtc_options;
3726 rtc_options.offer_to_receive_audio = 1;
3727 rtc_options.offer_to_receive_video = 0;
3728
3729 std::unique_ptr<SessionDescriptionInterface> offer;
3730 CreatePeerConnection();
3731 offer = CreateOfferWithOptions(rtc_options);
3732 ASSERT_TRUE(offer);
3733 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3734 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3735}
3736
3737// Test that only video content will be added if only |offer_to_receive_video|
3738// options is 1.
Steve Anton36da6ff2018-02-16 16:04:20 -08003739TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003740 RTCOfferAnswerOptions rtc_options;
3741 rtc_options.offer_to_receive_audio = 0;
3742 rtc_options.offer_to_receive_video = 1;
3743
3744 std::unique_ptr<SessionDescriptionInterface> offer;
3745 CreatePeerConnection();
3746 offer = CreateOfferWithOptions(rtc_options);
3747 ASSERT_TRUE(offer);
3748 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3749 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3750}
3751
zhihuang1c378ed2017-08-17 14:10:50 -07003752// Test that no media content will be added to the offer if using default
3753// RTCOfferAnswerOptions.
Steve Anton36da6ff2018-02-16 16:04:20 -08003754TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
zhihuang1c378ed2017-08-17 14:10:50 -07003755 RTCOfferAnswerOptions rtc_options;
3756
3757 std::unique_ptr<SessionDescriptionInterface> offer;
3758 CreatePeerConnection();
3759 offer = CreateOfferWithOptions(rtc_options);
3760 ASSERT_TRUE(offer);
3761 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3762 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3763}
3764
3765// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3766// ufrag/pwd will be the same in the new offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003767TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3768 CreatePeerConnection();
3769
zhihuang1c378ed2017-08-17 14:10:50 -07003770 RTCOfferAnswerOptions rtc_options;
3771 rtc_options.ice_restart = false;
3772 rtc_options.offer_to_receive_audio = 1;
3773
3774 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang1c378ed2017-08-17 14:10:50 -07003775 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003776 std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3777 auto ufrag1 =
3778 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3779 auto pwd1 =
3780 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003781
3782 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3783 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003784 auto ufrag2 =
3785 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3786 auto pwd2 =
3787 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003788
3789 // |ice_restart| is true, the ufrag/pwd should change.
3790 rtc_options.ice_restart = true;
3791 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
Steve Anton36da6ff2018-02-16 16:04:20 -08003792 auto ufrag3 =
3793 offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3794 auto pwd3 =
3795 offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
zhihuang1c378ed2017-08-17 14:10:50 -07003796
3797 EXPECT_EQ(ufrag1, ufrag2);
3798 EXPECT_EQ(pwd1, pwd2);
3799 EXPECT_NE(ufrag2, ufrag3);
3800 EXPECT_NE(pwd2, pwd3);
3801}
3802
3803// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3804// offer; if it is false, there won't be any bundle group in the offer.
Steve Anton36da6ff2018-02-16 16:04:20 -08003805TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
zhihuang1c378ed2017-08-17 14:10:50 -07003806 RTCOfferAnswerOptions rtc_options;
3807 rtc_options.offer_to_receive_audio = 1;
3808 rtc_options.offer_to_receive_video = 1;
3809
3810 std::unique_ptr<SessionDescriptionInterface> offer;
3811 CreatePeerConnection();
3812
3813 rtc_options.use_rtp_mux = true;
3814 offer = CreateOfferWithOptions(rtc_options);
3815 ASSERT_TRUE(offer);
3816 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3817 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3818 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3819
3820 rtc_options.use_rtp_mux = false;
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_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3826}
3827
zhihuang141aacb2017-08-29 13:23:53 -07003828// This test ensures OnRenegotiationNeeded is called when we add track with
3829// MediaStream -> AddTrack in the same way it is called when we add track with
3830// PeerConnection -> AddTrack.
3831// The test can be removed once addStream is rewritten in terms of addTrack
3832// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
Steve Anton36da6ff2018-02-16 16:04:20 -08003833// Don't run under Unified Plan since the stream API is not available.
3834TEST_F(PeerConnectionInterfaceTestPlanB,
3835 MediaStreamAddTrackRemoveTrackRenegotiate) {
zhihuang141aacb2017-08-29 13:23:53 -07003836 CreatePeerConnectionWithoutDtls();
3837 rtc::scoped_refptr<MediaStreamInterface> stream(
Seth Hampson845e8782018-03-02 11:34:10 -08003838 pc_factory_->CreateLocalMediaStream(kStreamId1));
zhihuang141aacb2017-08-29 13:23:53 -07003839 pc_->AddStream(stream);
3840 rtc::scoped_refptr<AudioTrackInterface> audio_track(
Niels Möller215ba602019-01-18 13:59:45 +01003841 CreateAudioTrack("audio_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003842 rtc::scoped_refptr<VideoTrackInterface> video_track(
Niels Möller215ba602019-01-18 13:59:45 +01003843 CreateVideoTrack("video_track"));
zhihuang141aacb2017-08-29 13:23:53 -07003844 stream->AddTrack(audio_track);
3845 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3846 observer_.renegotiation_needed_ = false;
3847
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003848 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003849 stream->AddTrack(video_track);
3850 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3851 observer_.renegotiation_needed_ = false;
3852
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003853 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003854 stream->RemoveTrack(audio_track);
3855 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3856 observer_.renegotiation_needed_ = false;
3857
Guido Urdaneta70c2db12019-04-16 12:24:14 +02003858 CreateOfferReceiveAnswer();
zhihuang141aacb2017-08-29 13:23:53 -07003859 stream->RemoveTrack(video_track);
3860 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3861 observer_.renegotiation_needed_ = false;
3862}
3863
Zhi Huangb2d355e2017-10-26 17:26:37 -07003864// Tests that an error is returned if a description is applied that has fewer
3865// media sections than the existing description.
Steve Anton36da6ff2018-02-16 16:04:20 -08003866TEST_P(PeerConnectionInterfaceTest,
Zhi Huangb2d355e2017-10-26 17:26:37 -07003867 MediaSectionCountEnforcedForSubsequentOffer) {
3868 CreatePeerConnection();
Steve Anton36da6ff2018-02-16 16:04:20 -08003869 AddAudioTrack("audio_label");
3870 AddVideoTrack("video_label");
3871
Zhi Huangb2d355e2017-10-26 17:26:37 -07003872 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton36da6ff2018-02-16 16:04:20 -08003873 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003874 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3875
3876 // A remote offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003877 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003878 offer->description()->contents().pop_back();
3879 offer->description()->contents().pop_back();
3880 ASSERT_TRUE(offer->description()->contents().empty());
3881 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3882
3883 std::unique_ptr<SessionDescriptionInterface> answer;
3884 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3885 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3886
3887 // A subsequent local offer with fewer media sections should be rejected.
Steve Anton36da6ff2018-02-16 16:04:20 -08003888 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Zhi Huangb2d355e2017-10-26 17:26:37 -07003889 offer->description()->contents().pop_back();
3890 offer->description()->contents().pop_back();
3891 ASSERT_TRUE(offer->description()->contents().empty());
3892 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3893}
3894
Johannes Kron89f874e2018-11-12 10:25:48 +01003895TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3896 RTCConfiguration config;
3897 // Default behavior is false.
3898 CreatePeerConnection(config);
3899 std::unique_ptr<SessionDescriptionInterface> offer;
3900 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3901 EXPECT_FALSE(offer->description()->extmap_allow_mixed());
3902 // Possible to set to true.
3903 config.offer_extmap_allow_mixed = true;
3904 CreatePeerConnection(config);
3905 offer.release();
3906 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3907 EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3908}
3909
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003910INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3911 PeerConnectionInterfaceTest,
3912 Values(SdpSemantics::kPlanB,
3913 SdpSemantics::kUnifiedPlan));
Steve Anton36da6ff2018-02-16 16:04:20 -08003914
Mirko Bonadei6a489f22019-04-09 15:11:12 +02003915class PeerConnectionMediaConfigTest : public ::testing::Test {
nisse51542be2016-02-12 02:27:06 -08003916 protected:
3917 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003918 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003919 pcf_->Initialize();
3920 }
nisseeaabdf62017-05-05 02:23:02 -07003921 const cricket::MediaConfig TestCreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +02003922 const RTCConfiguration& config) {
3923 rtc::scoped_refptr<PeerConnectionInterface> pc(
3924 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003925 EXPECT_TRUE(pc.get());
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003926 observer_.SetPeerConnectionInterface(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003927 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003928 }
3929
zhihuang9763d562016-08-05 11:14:50 -07003930 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003931 MockPeerConnectionObserver observer_;
3932};
3933
Yves Gereyf3ff14c2018-10-25 10:28:12 +02003934// This sanity check validates the test infrastructure itself.
3935TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3936 PeerConnectionInterface::RTCConfiguration config;
3937 rtc::scoped_refptr<PeerConnectionInterface> pc(
3938 pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3939 EXPECT_TRUE(pc.get());
3940 observer_.SetPeerConnectionInterface(pc.get()); // Required.
3941 pc->Close(); // No abort -> ok.
3942 SUCCEED();
3943}
3944
nisse51542be2016-02-12 02:27:06 -08003945// This test verifies the default behaviour with no constraints and a
3946// default RTCConfiguration.
3947TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3948 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003949
Niels Möllerf06f9232018-08-07 12:32:18 +02003950 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003951
3952 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003953 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3954 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003955 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003956 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003957}
3958
Niels Möller1d7ecd22018-01-18 15:25:12 +01003959// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003960// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003961TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3962 PeerConnectionInterface::RTCConfiguration config;
nisse51542be2016-02-12 02:27:06 -08003963
Niels Möller71bdda02016-03-31 12:59:59 +02003964 config.set_prerenderer_smoothing(false);
Niels Möllerf06f9232018-08-07 12:32:18 +02003965 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
nisse51542be2016-02-12 02:27:06 -08003966
Niels Möller1d7ecd22018-01-18 15:25:12 +01003967 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003968}
3969
Niels Möller6539f692018-01-18 08:58:50 +01003970// This test verifies that the experiment_cpu_load_estimator flag is
3971// propagated from RTCConfiguration to the PeerConnection.
3972TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3973 PeerConnectionInterface::RTCConfiguration config;
Niels Möller6539f692018-01-18 08:58:50 +01003974
3975 config.set_experiment_cpu_load_estimator(true);
Niels Möllerf06f9232018-08-07 12:32:18 +02003976 const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
Niels Möller6539f692018-01-18 08:58:50 +01003977
3978 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3979}
3980
deadbeef293e9262017-01-11 12:28:30 -08003981// Tests a few random fields being different.
3982TEST(RTCConfigurationTest, ComparisonOperators) {
3983 PeerConnectionInterface::RTCConfiguration a;
3984 PeerConnectionInterface::RTCConfiguration b;
3985 EXPECT_EQ(a, b);
3986
3987 PeerConnectionInterface::RTCConfiguration c;
3988 c.servers.push_back(PeerConnectionInterface::IceServer());
3989 EXPECT_NE(a, c);
3990
3991 PeerConnectionInterface::RTCConfiguration d;
3992 d.type = PeerConnectionInterface::kRelay;
3993 EXPECT_NE(a, d);
3994
3995 PeerConnectionInterface::RTCConfiguration e;
3996 e.audio_jitter_buffer_max_packets = 5;
3997 EXPECT_NE(a, e);
3998
3999 PeerConnectionInterface::RTCConfiguration f;
4000 f.ice_connection_receiving_timeout = 1337;
4001 EXPECT_NE(a, f);
4002
4003 PeerConnectionInterface::RTCConfiguration g;
4004 g.disable_ipv6 = true;
4005 EXPECT_NE(a, g);
4006
4007 PeerConnectionInterface::RTCConfiguration h(
4008 PeerConnectionInterface::RTCConfigurationType::kAggressive);
4009 EXPECT_NE(a, h);
4010}
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01004011
4012} // namespace
4013} // namespace webrtc