blob: 0206003fabdd1fd289438b32881cd90bb73ed6ac [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
Zhi Huang2a5e4262017-09-14 01:15:03 -070011#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070012#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080013#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "api/rtpreceiverinterface.h"
23#include "api/rtpsenderinterface.h"
24#include "api/test/fakeconstraints.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020025#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakevideocapturer.h"
27#include "media/engine/webrtcmediaengine.h"
28#include "media/sctp/sctptransportinternal.h"
29#include "modules/audio_processing/include/audio_processing.h"
30#include "p2p/base/fakeportallocator.h"
31#include "pc/audiotrack.h"
32#include "pc/mediasession.h"
33#include "pc/mediastream.h"
34#include "pc/peerconnection.h"
35#include "pc/streamcollection.h"
36#include "pc/test/fakeaudiocapturemodule.h"
37#include "pc/test/fakertccertificategenerator.h"
38#include "pc/test/fakevideotracksource.h"
39#include "pc/test/mockpeerconnectionobservers.h"
40#include "pc/test/testsdpstrings.h"
41#include "pc/videocapturertracksource.h"
42#include "pc/videotrack.h"
43#include "rtc_base/gunit.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020044#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/virtualsocketserver.h"
47#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020048#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070049
50#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070052#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54static const char kStreamLabel1[] = "local_stream_1";
55static const char kStreamLabel2[] = "local_stream_2";
56static const char kStreamLabel3[] = "local_stream_3";
57static const int kDefaultStunPort = 3478;
58static const char kStunAddressOnly[] = "stun:address";
59static const char kStunInvalidPort[] = "stun:address:-1";
60static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
61static const char kStunAddressPortAndMore2[] = "stun:address:port more";
62static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
63static const char kTurnUsername[] = "user";
64static const char kTurnPassword[] = "password";
65static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020066static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
deadbeefab9b2d12015-10-14 11:33:11 -070068static const char kStreams[][8] = {"stream1", "stream2"};
69static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
70static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
71
deadbeef5e97fb52015-10-15 12:49:08 -070072static const char kRecvonly[] = "recvonly";
73static const char kSendrecv[] = "sendrecv";
74
deadbeefab9b2d12015-10-14 11:33:11 -070075// Reference SDP with a MediaStream with label "stream1" and audio track with
76// id "audio_1" and a video track with id "video_1;
77static const char kSdpStringWithStream1[] =
78 "v=0\r\n"
79 "o=- 0 0 IN IP4 127.0.0.1\r\n"
80 "s=-\r\n"
81 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080082 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=ice-ufrag:e5785931\r\n"
84 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
85 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
86 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080089 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070090 "a=rtpmap:103 ISAC/16000\r\n"
91 "a=ssrc:1 cname:stream1\r\n"
92 "a=ssrc:1 mslabel:stream1\r\n"
93 "a=ssrc:1 label:audiotrack0\r\n"
94 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080095 "a=ice-ufrag:e5785931\r\n"
96 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
97 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
98 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700100 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800101 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700102 "a=rtpmap:120 VP8/90000\r\n"
103 "a=ssrc:2 cname:stream1\r\n"
104 "a=ssrc:2 mslabel:stream1\r\n"
105 "a=ssrc:2 label:videotrack0\r\n";
106
zhihuang81c3a032016-11-17 12:06:24 -0800107// Reference SDP with a MediaStream with label "stream1" and audio track with
108// id "audio_1";
109static const char kSdpStringWithStream1AudioTrackOnly[] =
110 "v=0\r\n"
111 "o=- 0 0 IN IP4 127.0.0.1\r\n"
112 "s=-\r\n"
113 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800114 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800115 "a=ice-ufrag:e5785931\r\n"
116 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
117 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
118 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800119 "a=mid:audio\r\n"
120 "a=sendrecv\r\n"
121 "a=rtpmap:103 ISAC/16000\r\n"
122 "a=ssrc:1 cname:stream1\r\n"
123 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800124 "a=ssrc:1 label:audiotrack0\r\n"
125 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800126
deadbeefab9b2d12015-10-14 11:33:11 -0700127// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
128// MediaStreams have one audio track and one video track.
129// This uses MSID.
130static const char kSdpStringWithStream1And2[] =
131 "v=0\r\n"
132 "o=- 0 0 IN IP4 127.0.0.1\r\n"
133 "s=-\r\n"
134 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800135 "a=msid-semantic: WMS stream1 stream2\r\n"
136 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=ice-ufrag:e5785931\r\n"
138 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
139 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
140 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700142 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800143 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=rtpmap:103 ISAC/16000\r\n"
145 "a=ssrc:1 cname:stream1\r\n"
146 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
147 "a=ssrc:3 cname:stream2\r\n"
148 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
149 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800150 "a=ice-ufrag:e5785931\r\n"
151 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
152 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
153 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700155 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800156 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:120 VP8/0\r\n"
158 "a=ssrc:2 cname:stream1\r\n"
159 "a=ssrc:2 msid:stream1 videotrack0\r\n"
160 "a=ssrc:4 cname:stream2\r\n"
161 "a=ssrc:4 msid:stream2 videotrack1\r\n";
162
163// Reference SDP without MediaStreams. Msid is not supported.
164static const char kSdpStringWithoutStreams[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800169 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "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"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800176 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=rtpmap:103 ISAC/16000\r\n"
178 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800179 "a=ice-ufrag:e5785931\r\n"
180 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
181 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
182 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700184 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800185 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700186 "a=rtpmap:120 VP8/90000\r\n";
187
188// Reference SDP without MediaStreams. Msid is supported.
189static const char kSdpStringWithMsidWithoutStreams[] =
190 "v=0\r\n"
191 "o=- 0 0 IN IP4 127.0.0.1\r\n"
192 "s=-\r\n"
193 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800194 "a=msid-semantic: WMS\r\n"
195 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=ice-ufrag:e5785931\r\n"
197 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
198 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
199 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700201 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800202 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=rtpmap:103 ISAC/16000\r\n"
204 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800205 "a=ice-ufrag:e5785931\r\n"
206 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
207 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
208 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800211 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:120 VP8/90000\r\n";
213
214// Reference SDP without MediaStreams and audio only.
215static const char kSdpStringWithoutStreamsAudioOnly[] =
216 "v=0\r\n"
217 "o=- 0 0 IN IP4 127.0.0.1\r\n"
218 "s=-\r\n"
219 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800220 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=ice-ufrag:e5785931\r\n"
222 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
223 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
224 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700226 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:103 ISAC/16000\r\n";
229
230// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
231static const char kSdpStringSendOnlyWithoutStreams[] =
232 "v=0\r\n"
233 "o=- 0 0 IN IP4 127.0.0.1\r\n"
234 "s=-\r\n"
235 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800236 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=ice-ufrag:e5785931\r\n"
238 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
239 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
240 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700242 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800244 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=rtpmap:103 ISAC/16000\r\n"
246 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800247 "a=ice-ufrag:e5785931\r\n"
248 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
249 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
250 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700252 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800254 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700255 "a=rtpmap:120 VP8/90000\r\n";
256
257static const char kSdpStringInit[] =
258 "v=0\r\n"
259 "o=- 0 0 IN IP4 127.0.0.1\r\n"
260 "s=-\r\n"
261 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700262 "a=msid-semantic: WMS\r\n";
263
264static const char kSdpStringAudio[] =
265 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800266 "a=ice-ufrag:e5785931\r\n"
267 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
268 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
269 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700271 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800272 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700273 "a=rtpmap:103 ISAC/16000\r\n";
274
275static const char kSdpStringVideo[] =
276 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800277 "a=ice-ufrag:e5785931\r\n"
278 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
279 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
280 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700282 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800283 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700284 "a=rtpmap:120 VP8/90000\r\n";
285
286static const char kSdpStringMs1Audio0[] =
287 "a=ssrc:1 cname:stream1\r\n"
288 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
289
290static const char kSdpStringMs1Video0[] =
291 "a=ssrc:2 cname:stream1\r\n"
292 "a=ssrc:2 msid:stream1 videotrack0\r\n";
293
294static const char kSdpStringMs1Audio1[] =
295 "a=ssrc:3 cname:stream1\r\n"
296 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
297
298static const char kSdpStringMs1Video1[] =
299 "a=ssrc:4 cname:stream1\r\n"
300 "a=ssrc:4 msid:stream1 videotrack1\r\n";
301
deadbeef8662f942017-01-20 21:20:51 -0800302static const char kDtlsSdesFallbackSdp[] =
303 "v=0\r\n"
304 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
305 "s=-\r\n"
306 "c=IN IP4 0.0.0.0\r\n"
307 "t=0 0\r\n"
308 "a=group:BUNDLE audio\r\n"
309 "a=msid-semantic: WMS\r\n"
310 "m=audio 1 RTP/SAVPF 0\r\n"
311 "a=sendrecv\r\n"
312 "a=rtcp-mux\r\n"
313 "a=mid:audio\r\n"
314 "a=ssrc:1 cname:stream1\r\n"
315 "a=ssrc:1 mslabel:stream1\r\n"
316 "a=ssrc:1 label:audiotrack0\r\n"
317 "a=ice-ufrag:e5785931\r\n"
318 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
319 "a=rtpmap:0 pcmu/8000\r\n"
320 "a=fingerprint:sha-1 "
321 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
322 "a=setup:actpass\r\n"
Joachim Bauch6780c512018-02-04 18:05:00 +0100323 "a=crypto:0 AES_CM_128_HMAC_SHA1_32 "
deadbeef8662f942017-01-20 21:20:51 -0800324 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
325 "dummy_session_params\r\n";
326
perkjd61bf802016-03-24 03:16:19 -0700327using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700328using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700330using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331using webrtc::AudioTrackInterface;
332using webrtc::DataBuffer;
333using webrtc::DataChannelInterface;
334using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700336using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700337using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338using webrtc::MediaStreamInterface;
339using webrtc::MediaStreamTrackInterface;
340using webrtc::MockCreateSessionDescriptionObserver;
341using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700342using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343using webrtc::MockSetSessionDescriptionObserver;
344using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700345using webrtc::NotifierInterface;
346using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347using webrtc::PeerConnectionInterface;
348using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800349using webrtc::RTCError;
350using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700351using webrtc::RtpReceiverInterface;
352using webrtc::RtpSenderInterface;
Steve Anton4e70a722017-11-28 14:57:10 -0800353using webrtc::RtpTransceiverDirection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354using webrtc::SdpParseError;
Steve Antona3a92c22017-12-07 10:27:41 -0800355using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700357using webrtc::StreamCollection;
358using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100359using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700360using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361using webrtc::VideoTrackInterface;
362
deadbeefab9b2d12015-10-14 11:33:11 -0700363typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
364
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365namespace {
366
367// Gets the first ssrc of given content type from the ContentInfo.
368bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
369 if (!content_info || !ssrc) {
370 return false;
371 }
372 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800373 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 if (!media_desc || media_desc->streams().empty()) {
375 return false;
376 }
377 *ssrc = media_desc->streams().begin()->first_ssrc();
378 return true;
379}
380
deadbeefd1a38b52016-12-10 13:15:33 -0800381// Get the ufrags out of an SDP blob. Useful for testing ICE restart
382// behavior.
383std::vector<std::string> GetUfrags(
384 const webrtc::SessionDescriptionInterface* desc) {
385 std::vector<std::string> ufrags;
386 for (const cricket::TransportInfo& info :
387 desc->description()->transport_infos()) {
388 ufrags.push_back(info.description.ice_ufrag);
389 }
390 return ufrags;
391}
392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393void SetSsrcToZero(std::string* sdp) {
394 const char kSdpSsrcAtribute[] = "a=ssrc:";
395 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
396 size_t ssrc_pos = 0;
397 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
398 std::string::npos) {
399 size_t end_ssrc = sdp->find(" ", ssrc_pos);
400 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
401 ssrc_pos = end_ssrc;
402 }
403}
404
deadbeefab9b2d12015-10-14 11:33:11 -0700405// Check if |streams| contains the specified track.
406bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
407 const std::string& stream_label,
408 const std::string& track_id) {
409 for (const cricket::StreamParams& params : streams) {
410 if (params.sync_label == stream_label && params.id == track_id) {
411 return true;
412 }
413 }
414 return false;
415}
416
417// Check if |senders| contains the specified sender, by id.
418bool ContainsSender(
419 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
420 const std::string& id) {
421 for (const auto& sender : senders) {
422 if (sender->id() == id) {
423 return true;
424 }
425 }
426 return false;
427}
428
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700429// Check if |senders| contains the specified sender, by id and stream id.
430bool ContainsSender(
431 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
432 const std::string& id,
433 const std::string& stream_id) {
434 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700435 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700436 return true;
437 }
438 }
439 return false;
440}
441
deadbeefab9b2d12015-10-14 11:33:11 -0700442// Create a collection of streams.
443// CreateStreamCollection(1) creates a collection that
444// correspond to kSdpStringWithStream1.
445// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
446rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700447 int number_of_streams,
448 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700449 rtc::scoped_refptr<StreamCollection> local_collection(
450 StreamCollection::Create());
451
452 for (int i = 0; i < number_of_streams; ++i) {
453 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
454 webrtc::MediaStream::Create(kStreams[i]));
455
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700456 for (int j = 0; j < tracks_per_stream; ++j) {
457 // Add a local audio track.
458 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
459 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
460 nullptr));
461 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700462
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700463 // Add a local video track.
464 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
465 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700466 webrtc::FakeVideoTrackSource::Create(),
467 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700468 stream->AddTrack(video_track);
469 }
deadbeefab9b2d12015-10-14 11:33:11 -0700470
471 local_collection->AddStream(stream);
472 }
473 return local_collection;
474}
475
476// Check equality of StreamCollections.
477bool CompareStreamCollections(StreamCollectionInterface* s1,
478 StreamCollectionInterface* s2) {
479 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
480 return false;
481 }
482
483 for (size_t i = 0; i != s1->count(); ++i) {
484 if (s1->at(i)->label() != s2->at(i)->label()) {
485 return false;
486 }
487 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
488 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
489 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
490 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
491
492 if (audio_tracks1.size() != audio_tracks2.size()) {
493 return false;
494 }
495 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
496 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
497 return false;
498 }
499 }
500 if (video_tracks1.size() != video_tracks2.size()) {
501 return false;
502 }
503 for (size_t j = 0; j != video_tracks1.size(); ++j) {
504 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
505 return false;
506 }
507 }
508 }
509 return true;
510}
511
perkjd61bf802016-03-24 03:16:19 -0700512// Helper class to test Observer.
513class MockTrackObserver : public ObserverInterface {
514 public:
515 explicit MockTrackObserver(NotifierInterface* notifier)
516 : notifier_(notifier) {
517 notifier_->RegisterObserver(this);
518 }
519
520 ~MockTrackObserver() { Unregister(); }
521
522 void Unregister() {
523 if (notifier_) {
524 notifier_->UnregisterObserver(this);
525 notifier_ = nullptr;
526 }
527 }
528
529 MOCK_METHOD0(OnChanged, void());
530
531 private:
532 NotifierInterface* notifier_;
533};
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700536
nisse528b7932017-05-08 03:21:43 -0700537// The PeerConnectionMediaConfig tests below verify that configuration and
538// constraints are propagated into the PeerConnection's MediaConfig. These
539// settings are intended for MediaChannel constructors, but that is not
540// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700541class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
542 public:
zhihuang38ede132017-06-15 12:52:32 -0700543 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
544 CreatePeerConnectionFactoryForTest() {
545 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
546 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
547
henrika919dc2e2017-10-12 14:24:55 +0200548 // Use fake audio device module since we're only testing the interface
549 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700550 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
551 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200552 FakeAudioCaptureModule::Create(), audio_encoder_factory,
553 audio_decoder_factory, nullptr, nullptr, nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +0100554 webrtc::AudioProcessingBuilder().Create()));
zhihuang38ede132017-06-15 12:52:32 -0700555
556 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
557 webrtc::CreateCallFactory();
558
559 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
560 webrtc::CreateRtcEventLogFactory();
561
562 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
563 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200564 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700565 std::move(event_log_factory));
566 }
567
568 PeerConnectionFactoryForTest(
569 rtc::Thread* network_thread,
570 rtc::Thread* worker_thread,
571 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700572 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
573 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
574 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
575 : webrtc::PeerConnectionFactory(network_thread,
576 worker_thread,
577 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700578 std::move(media_engine),
579 std::move(call_factory),
580 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800581
zhihuang29ff8442016-07-27 11:07:25 -0700582 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700583 cricket::PortAllocator* port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800584 bool redetermine_role_on_ice_restart,
585 webrtc::RtcEventLog* event_log = nullptr) override {
zhihuang29ff8442016-07-27 11:07:25 -0700586 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700587 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800588 redetermine_role_on_ice_restart, rtc::CryptoOptions(), event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700589 return transport_controller;
590 }
591
deadbeefd7850b22017-08-23 10:59:19 -0700592 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700593 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700594};
595
Steve Anton94286cb2017-09-26 16:20:19 -0700596// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
597// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598class PeerConnectionInterfaceTest : public testing::Test {
599 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700600 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700601 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800602#ifdef WEBRTC_ANDROID
603 webrtc::InitializeAndroidObjects();
604#endif
605 }
606
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700608 // Use fake audio capture module since we're only testing the interface
609 // level, and using a real one could make tests flaky when run in parallel.
610 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700612 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200613 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
614 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700615 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700616 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700617 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700618 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 }
620
621 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700622 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 }
624
deadbeef293e9262017-01-11 12:28:30 -0800625 // DTLS does not work in a loopback call, so is disabled for most of the
626 // tests in this file.
627 void CreatePeerConnectionWithoutDtls() {
628 FakeConstraints no_dtls_constraints;
629 no_dtls_constraints.AddMandatory(
630 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
631
632 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
633 &no_dtls_constraints);
634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700637 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
638 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 }
640
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700641 void CreatePeerConnectionWithIceTransportsType(
642 PeerConnectionInterface::IceTransportsType type) {
643 PeerConnectionInterface::RTCConfiguration config;
644 config.type = type;
645 return CreatePeerConnection(config, nullptr);
646 }
647
648 void CreatePeerConnectionWithIceServer(const std::string& uri,
649 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800650 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700652 server.uri = uri;
653 server.password = password;
654 config.servers.push_back(server);
655 CreatePeerConnection(config, nullptr);
656 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100658 void CreatePeerConnection(
659 const PeerConnectionInterface::RTCConfiguration& config,
660 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700661 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800662 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
663 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000664
deadbeef1dcb1642017-03-29 21:08:16 -0700665 // Create certificate generator unless DTLS constraint is explicitly set to
666 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200667 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000668 bool dtls;
669 if (FindConstraint(constraints,
670 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
671 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200672 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800673 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
674 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000675 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200676 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800677 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200678 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 ASSERT_TRUE(pc_.get() != NULL);
680 observer_.SetPeerConnectionInterface(pc_.get());
681 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
682 }
683
deadbeef0a6c4ca2015-10-06 11:38:28 -0700684 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800685 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700686 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700687 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800688 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700689
zhihuang9763d562016-08-05 11:14:50 -0700690 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700691 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
692 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800693 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700694 }
695
Steve Anton038834f2017-07-14 15:59:59 -0700696 void CreatePeerConnectionExpectFail(
697 PeerConnectionInterface::RTCConfiguration config) {
698 PeerConnectionInterface::IceServer server;
699 server.uri = kTurnIceServerUri;
700 server.password = kTurnPassword;
701 config.servers.push_back(server);
702 rtc::scoped_refptr<PeerConnectionInterface> pc =
703 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
704 EXPECT_EQ(nullptr, pc);
705 }
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
710 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
711 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714
deadbeef0a6c4ca2015-10-06 11:38:28 -0700715 CreatePeerConnectionExpectFail(kStunInvalidPort);
716 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
717 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700719 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800720 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
721 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800725 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800727 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
730 void ReleasePeerConnection() {
731 pc_ = NULL;
732 observer_.SetPeerConnectionInterface(NULL);
733 }
734
deadbeefab9b2d12015-10-14 11:33:11 -0700735 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700737 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700739 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800740 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
741 new cricket::FakeVideoCapturer()),
742 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700743 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 pc_factory_->CreateVideoTrack(label + "v0", video_source));
745 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000746 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
748 observer_.renegotiation_needed_ = false;
749 }
750
751 void AddVoiceStream(const std::string& label) {
752 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700753 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700755 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 pc_factory_->CreateAudioTrack(label + "a0", NULL));
757 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000758 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
760 observer_.renegotiation_needed_ = false;
761 }
762
763 void AddAudioVideoStream(const std::string& stream_label,
764 const std::string& audio_track_label,
765 const std::string& video_track_label) {
766 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700767 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700769 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 pc_factory_->CreateAudioTrack(
771 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
772 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700773 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700774 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800775 video_track_label, pc_factory_->CreateVideoSource(
776 std::unique_ptr<cricket::VideoCapturer>(
777 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000779 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
781 observer_.renegotiation_needed_ = false;
782 }
783
kwibergd1fe2812016-04-27 06:47:29 -0700784 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700785 bool offer,
786 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000787 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
788 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 MockCreateSessionDescriptionObserver>());
790 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700791 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700793 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 }
795 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700796 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return observer->result();
798 }
799
kwibergd1fe2812016-04-27 06:47:29 -0700800 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700801 MediaConstraintsInterface* constraints) {
802 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 }
804
kwibergd1fe2812016-04-27 06:47:29 -0700805 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700806 MediaConstraintsInterface* constraints) {
807 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 }
809
Steve Antondb45ca82017-09-11 18:27:34 -0700810 bool DoSetSessionDescription(
811 std::unique_ptr<SessionDescriptionInterface> desc,
812 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
814 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 MockSetSessionDescriptionObserver>());
816 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700817 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700819 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
zhihuang29ff8442016-07-27 11:07:25 -0700821 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
822 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 return observer->result();
825 }
826
Steve Antondb45ca82017-09-11 18:27:34 -0700827 bool DoSetLocalDescription(
828 std::unique_ptr<SessionDescriptionInterface> desc) {
829 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
Steve Antondb45ca82017-09-11 18:27:34 -0700832 bool DoSetRemoteDescription(
833 std::unique_ptr<SessionDescriptionInterface> desc) {
834 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 }
836
837 // Calls PeerConnection::GetStats and check the return value.
838 // It does not verify the values in the StatReports since a RTCP packet might
839 // be required.
840 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 rtc::scoped_refptr<MockStatsObserver> observer(
842 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000843 if (!pc_->GetStats(
844 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 return false;
846 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
847 return observer->called();
848 }
849
Harald Alvestrand89061872018-01-02 14:08:34 +0100850 // Call the standards-compliant GetStats function.
851 bool DoGetRTCStats() {
852 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
853 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
854 pc_->GetStats(callback);
855 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
856 return callback->called();
857 }
858
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800860 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 // Create a local stream with audio&video tracks.
862 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
863 CreateOfferReceiveAnswer();
864 }
865
866 // Verify that RTP Header extensions has been negotiated for audio and video.
867 void VerifyRemoteRtpHeaderExtensions() {
868 const cricket::MediaContentDescription* desc =
869 cricket::GetFirstAudioContentDescription(
870 pc_->remote_description()->description());
871 ASSERT_TRUE(desc != NULL);
872 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
873
874 desc = cricket::GetFirstVideoContentDescription(
875 pc_->remote_description()->description());
876 ASSERT_TRUE(desc != NULL);
877 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
878 }
879
880 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700881 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700882 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 std::string sdp;
884 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700885 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800886 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700887 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
889 }
890
deadbeefab9b2d12015-10-14 11:33:11 -0700891 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700892 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800893 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700894 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700895 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
896 }
897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700899 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700900 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901
902 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
903 // audio codec change, even if the parameter has nothing to do with
904 // receiving. Not all parameters are serialized to SDP.
905 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
906 // the SessionDescription, it is necessary to do that here to in order to
907 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
908 // https://code.google.com/p/webrtc/issues/detail?id=1356
909 std::string sdp;
910 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700911 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800912 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700913 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
915 }
916
917 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700918 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700919 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920
921 std::string sdp;
922 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700923 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800924 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700925 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
927 }
928
929 void CreateOfferReceiveAnswer() {
930 CreateOfferAsLocalDescription();
931 std::string sdp;
932 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
933 CreateAnswerAsRemoteDescription(sdp);
934 }
935
936 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700937 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700938 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
940 // audio codec change, even if the parameter has nothing to do with
941 // receiving. Not all parameters are serialized to SDP.
942 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
943 // the SessionDescription, it is necessary to do that here to in order to
944 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
945 // https://code.google.com/p/webrtc/issues/detail?id=1356
946 std::string sdp;
947 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700948 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800949 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950
Steve Antondb45ca82017-09-11 18:27:34 -0700951 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000953 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700954 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 }
956
deadbeefab9b2d12015-10-14 11:33:11 -0700957 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700958 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800959 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700960 ASSERT_TRUE(answer);
961 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
963 }
964
deadbeefab9b2d12015-10-14 11:33:11 -0700965 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700966 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800967 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700968 ASSERT_TRUE(pr_answer);
969 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700971 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800972 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700973 ASSERT_TRUE(answer);
974 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
976 }
977
978 // Help function used for waiting until a the last signaled remote stream has
979 // the same label as |stream_label|. In a few of the tests in this file we
980 // answer with the same session description as we offer and thus we can
981 // check if OnAddStream have been called with the same stream as we offer to
982 // send.
983 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
984 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
985 }
986
987 // Creates an offer and applies it as a local session description.
988 // Creates an answer with the same SDP an the offer but removes all lines
989 // that start with a:ssrc"
990 void CreateOfferReceiveAnswerWithoutSsrc() {
991 CreateOfferAsLocalDescription();
992 std::string sdp;
993 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
994 SetSsrcToZero(&sdp);
995 CreateAnswerAsRemoteDescription(sdp);
996 }
997
deadbeefab9b2d12015-10-14 11:33:11 -0700998 // This function creates a MediaStream with label kStreams[0] and
999 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1000 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001001 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001002 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001003 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001004 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1005 size_t number_of_video_tracks) {
1006 EXPECT_LE(number_of_audio_tracks, 2u);
1007 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001008
1009 reference_collection_ = StreamCollection::Create();
1010 std::string sdp_ms1 = std::string(kSdpStringInit);
1011
1012 std::string mediastream_label = kStreams[0];
1013
1014 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1015 webrtc::MediaStream::Create(mediastream_label));
1016 reference_collection_->AddStream(stream);
1017
1018 if (number_of_audio_tracks > 0) {
1019 sdp_ms1 += std::string(kSdpStringAudio);
1020 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1021 AddAudioTrack(kAudioTracks[0], stream);
1022 }
1023 if (number_of_audio_tracks > 1) {
1024 sdp_ms1 += kSdpStringMs1Audio1;
1025 AddAudioTrack(kAudioTracks[1], stream);
1026 }
1027
1028 if (number_of_video_tracks > 0) {
1029 sdp_ms1 += std::string(kSdpStringVideo);
1030 sdp_ms1 += std::string(kSdpStringMs1Video0);
1031 AddVideoTrack(kVideoTracks[0], stream);
1032 }
1033 if (number_of_video_tracks > 1) {
1034 sdp_ms1 += kSdpStringMs1Video1;
1035 AddVideoTrack(kVideoTracks[1], stream);
1036 }
1037
kwibergd1fe2812016-04-27 06:47:29 -07001038 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001039 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001040 }
1041
1042 void AddAudioTrack(const std::string& track_id,
1043 MediaStreamInterface* stream) {
1044 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1045 webrtc::AudioTrack::Create(track_id, nullptr));
1046 ASSERT_TRUE(stream->AddTrack(audio_track));
1047 }
1048
1049 void AddVideoTrack(const std::string& track_id,
1050 MediaStreamInterface* stream) {
1051 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001052 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001053 webrtc::FakeVideoTrackSource::Create(),
1054 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001055 ASSERT_TRUE(stream->AddTrack(video_track));
1056 }
1057
kwibergfd8be342016-05-14 19:44:11 -07001058 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001059 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001060 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001061 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001062 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1063 return offer;
1064 }
1065
kwibergfd8be342016-05-14 19:44:11 -07001066 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001067 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001068 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001069 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001070 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1071 return answer;
1072 }
1073
1074 const std::string& GetFirstAudioStreamCname(
1075 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001076 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001077 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001078 return audio_desc->streams()[0].cname;
1079 }
1080
zhihuang1c378ed2017-08-17 14:10:50 -07001081 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1082 const RTCOfferAnswerOptions& offer_answer_options) {
1083 RTC_DCHECK(pc_);
1084 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1085 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1086 pc_->CreateOffer(observer, offer_answer_options);
1087 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1088 return observer->MoveDescription();
1089 }
1090
1091 void CreateOfferWithOptionsAsRemoteDescription(
1092 std::unique_ptr<SessionDescriptionInterface>* desc,
1093 const RTCOfferAnswerOptions& offer_answer_options) {
1094 *desc = CreateOfferWithOptions(offer_answer_options);
1095 ASSERT_TRUE(desc != nullptr);
1096 std::string sdp;
1097 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001098 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001099 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001100 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001101 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1102 }
1103
1104 void CreateOfferWithOptionsAsLocalDescription(
1105 std::unique_ptr<SessionDescriptionInterface>* desc,
1106 const RTCOfferAnswerOptions& offer_answer_options) {
1107 *desc = CreateOfferWithOptions(offer_answer_options);
1108 ASSERT_TRUE(desc != nullptr);
1109 std::string sdp;
1110 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001111 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001112 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001113
Steve Antondb45ca82017-09-11 18:27:34 -07001114 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001115 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1116 }
1117
1118 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001119 RTC_DCHECK(content);
1120 RTC_DCHECK(content->media_description());
1121 for (const cricket::AudioCodec& codec :
1122 content->media_description()->as_audio()->codecs()) {
1123 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001124 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001125 }
zhihuang1c378ed2017-08-17 14:10:50 -07001126 }
1127 return false;
1128 }
1129
deadbeef9a6f4d42017-05-15 19:43:33 -07001130 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1131 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001132 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001133 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001134 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001135 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1136 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1137 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001139 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140};
1141
zhihuang8f65cdf2016-05-06 18:40:30 -07001142// Generate different CNAMEs when PeerConnections are created.
1143// The CNAMEs are expected to be generated randomly. It is possible
1144// that the test fails, though the possibility is very low.
1145TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001146 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001147 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001148 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001149 CreateOfferWithOneAudioStream();
1150 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1151 GetFirstAudioStreamCname(offer2.get()));
1152}
1153
1154TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001155 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001156 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001157 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001158 CreateAnswerWithOneAudioStream();
1159 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1160 GetFirstAudioStreamCname(answer2.get()));
1161}
1162
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163TEST_F(PeerConnectionInterfaceTest,
1164 CreatePeerConnectionWithDifferentConfigurations) {
1165 CreatePeerConnectionWithDifferentConfigurations();
1166}
1167
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001168TEST_F(PeerConnectionInterfaceTest,
1169 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1170 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1171 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1172 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1173 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1174 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1175 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1176 port_allocator_->candidate_filter());
1177 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1178 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1179}
1180
1181// Test that when a PeerConnection is created with a nonzero candidate pool
1182// size, the pooled PortAllocatorSession is created with all the attributes
1183// in the RTCConfiguration.
1184TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1185 PeerConnectionInterface::RTCConfiguration config;
1186 PeerConnectionInterface::IceServer server;
1187 server.uri = kStunAddressOnly;
1188 config.servers.push_back(server);
1189 config.type = PeerConnectionInterface::kRelay;
1190 config.disable_ipv6 = true;
1191 config.tcp_candidate_policy =
1192 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001193 config.candidate_network_policy =
1194 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001195 config.ice_candidate_pool_size = 1;
1196 CreatePeerConnection(config, nullptr);
1197
1198 const cricket::FakePortAllocatorSession* session =
1199 static_cast<const cricket::FakePortAllocatorSession*>(
1200 port_allocator_->GetPooledSession());
1201 ASSERT_NE(nullptr, session);
1202 EXPECT_EQ(1UL, session->stun_servers().size());
1203 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1204 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001205 EXPECT_LT(0U,
1206 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001207}
1208
deadbeefd21eab32017-07-26 16:50:11 -07001209// Test that network-related RTCConfiguration members are applied to the
1210// PortAllocator when CreatePeerConnection is called. Specifically:
1211// - disable_ipv6_on_wifi
1212// - max_ipv6_networks
1213// - tcp_candidate_policy
1214// - candidate_network_policy
1215// - prune_turn_ports
1216//
1217// Note that the candidate filter (RTCConfiguration::type) is already tested
1218// above.
1219TEST_F(PeerConnectionInterfaceTest,
1220 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1221 // Create fake port allocator.
1222 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1223 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1224 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1225
1226 // Create RTCConfiguration with some network-related fields relevant to
1227 // PortAllocator populated.
1228 PeerConnectionInterface::RTCConfiguration config;
1229 config.disable_ipv6_on_wifi = true;
1230 config.max_ipv6_networks = 10;
1231 config.tcp_candidate_policy =
1232 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1233 config.candidate_network_policy =
1234 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1235 config.prune_turn_ports = true;
1236
1237 // Create the PC factory and PC with the above config.
1238 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1239 webrtc::CreatePeerConnectionFactory(
1240 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001241 rtc::Thread::Current(), fake_audio_capture_module_,
1242 webrtc::CreateBuiltinAudioEncoderFactory(),
1243 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001244 rtc::scoped_refptr<PeerConnectionInterface> pc(
1245 pc_factory->CreatePeerConnection(
1246 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1247
1248 // Now validate that the config fields set above were applied to the
1249 // PortAllocator, as flags or otherwise.
1250 EXPECT_FALSE(raw_port_allocator->flags() &
1251 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1252 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1253 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1254 EXPECT_TRUE(raw_port_allocator->flags() &
1255 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1256 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1257}
1258
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001259// Test that the PeerConnection initializes the port allocator passed into it,
1260// and on the correct thread.
1261TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001262 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001263 std::unique_ptr<rtc::Thread> network_thread(
1264 rtc::Thread::CreateWithSocketServer());
1265 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001266 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1267 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001268 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001269 fake_audio_capture_module_,
1270 webrtc::CreateBuiltinAudioEncoderFactory(),
1271 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001272 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001273 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001274 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1275 PeerConnectionInterface::RTCConfiguration config;
1276 rtc::scoped_refptr<PeerConnectionInterface> pc(
1277 pc_factory->CreatePeerConnection(
1278 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1279 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1280 // so all we have to do here is check that it's initialized.
1281 EXPECT_TRUE(raw_port_allocator->initialized());
1282}
1283
deadbeef46c73892016-11-16 19:42:04 -08001284// Check that GetConfiguration returns the configuration the PeerConnection was
1285// constructed with, before SetConfiguration is called.
1286TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1287 PeerConnectionInterface::RTCConfiguration config;
1288 config.type = PeerConnectionInterface::kRelay;
1289 CreatePeerConnection(config, nullptr);
1290
1291 PeerConnectionInterface::RTCConfiguration returned_config =
1292 pc_->GetConfiguration();
1293 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1294}
1295
1296// Check that GetConfiguration returns the last configuration passed into
1297// SetConfiguration.
1298TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1299 CreatePeerConnection();
1300
1301 PeerConnectionInterface::RTCConfiguration config;
1302 config.type = PeerConnectionInterface::kRelay;
1303 EXPECT_TRUE(pc_->SetConfiguration(config));
1304
1305 PeerConnectionInterface::RTCConfiguration returned_config =
1306 pc_->GetConfiguration();
1307 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1308}
1309
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001311 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001312 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 AddVoiceStream(kStreamLabel2);
1314 ASSERT_EQ(2u, pc_->local_streams()->count());
1315
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001316 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001317 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001319 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1320 pc_factory_->CreateAudioTrack(kStreamLabel3,
1321 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001323 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001324 EXPECT_EQ(3u, pc_->local_streams()->count());
1325
1326 // Remove the third stream.
1327 pc_->RemoveStream(pc_->local_streams()->at(2));
1328 EXPECT_EQ(2u, pc_->local_streams()->count());
1329
1330 // Remove the second stream.
1331 pc_->RemoveStream(pc_->local_streams()->at(1));
1332 EXPECT_EQ(1u, pc_->local_streams()->count());
1333
1334 // Remove the first stream.
1335 pc_->RemoveStream(pc_->local_streams()->at(0));
1336 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337}
1338
deadbeefab9b2d12015-10-14 11:33:11 -07001339// Test that the created offer includes streams we added.
1340TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001341 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001342 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001343 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001344 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001345
deadbeefab9b2d12015-10-14 11:33:11 -07001346 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001347 cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001348 EXPECT_TRUE(
1349 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1350
deadbeefab9b2d12015-10-14 11:33:11 -07001351 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001352 cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001353 EXPECT_TRUE(
1354 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1355
1356 // Add another stream and ensure the offer includes both the old and new
1357 // streams.
1358 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001359 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001360
Steve Antonb1c1de12017-12-21 15:14:30 -08001361 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001362 EXPECT_TRUE(
1363 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1364 EXPECT_TRUE(
1365 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1366
Steve Antonb1c1de12017-12-21 15:14:30 -08001367 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001368 EXPECT_TRUE(
1369 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1370 EXPECT_TRUE(
1371 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1372}
1373
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001375 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001376 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 ASSERT_EQ(1u, pc_->local_streams()->count());
1378 pc_->RemoveStream(pc_->local_streams()->at(0));
1379 EXPECT_EQ(0u, pc_->local_streams()->count());
1380}
1381
deadbeefe1f9d832016-01-14 15:35:42 -08001382// Test for AddTrack and RemoveTrack methods.
1383// Tests that the created offer includes tracks we added,
1384// and that the RtpSenders are created correctly.
1385// Also tests that RemoveTrack removes the tracks from subsequent offers.
1386TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001387 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001388 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001389 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001390 rtc::scoped_refptr<VideoTrackInterface> video_track(
1391 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001392 "video_track", pc_factory_->CreateVideoSource(
1393 std::unique_ptr<cricket::VideoCapturer>(
1394 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001395 auto audio_sender = pc_->AddTrack(audio_track, {kStreamLabel1}).MoveValue();
1396 auto video_sender = pc_->AddTrack(video_track, {kStreamLabel1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001397 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1398 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001399 EXPECT_EQ("audio_track", audio_sender->id());
1400 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001401 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1402 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001403 EXPECT_EQ("video_track", video_sender->id());
1404 EXPECT_EQ(video_track, video_sender->track());
1405
1406 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001407 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001408 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001409
1410 const cricket::ContentInfo* audio_content =
1411 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001412 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1413 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001414
1415 const cricket::ContentInfo* video_content =
1416 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001417 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1418 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001419
Steve Antondb45ca82017-09-11 18:27:34 -07001420 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001421
1422 // Now try removing the tracks.
1423 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1424 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1425
1426 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001427 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001428
1429 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001430 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1431 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001432
1433 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001434 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1435 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001436
Steve Antondb45ca82017-09-11 18:27:34 -07001437 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001438
1439 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1440 // should return false.
1441 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1442 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1443}
1444
1445// Test creating senders without a stream specified,
1446// expecting a random stream ID to be generated.
1447TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001448 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001449 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001450 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001451 rtc::scoped_refptr<VideoTrackInterface> video_track(
1452 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001453 "video_track", pc_factory_->CreateVideoSource(
1454 std::unique_ptr<cricket::VideoCapturer>(
1455 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001456 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001457 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001458 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001459 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001460 EXPECT_EQ("audio_track", audio_sender->id());
1461 EXPECT_EQ(audio_track, audio_sender->track());
1462 EXPECT_EQ("video_track", video_sender->id());
1463 EXPECT_EQ(video_track, video_sender->track());
1464 // If the ID is truly a random GUID, it should be infinitely unlikely they
1465 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001466 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001467}
1468
Harald Alvestrand89061872018-01-02 14:08:34 +01001469// Test that we can call GetStats() after AddTrack but before connecting
1470// the PeerConnection to a peer.
1471TEST_F(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
1472 CreatePeerConnectionWithoutDtls();
1473 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1474 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1475 rtc::scoped_refptr<VideoTrackInterface> video_track(
1476 pc_factory_->CreateVideoTrack(
1477 "video_track", pc_factory_->CreateVideoSource(
1478 std::unique_ptr<cricket::VideoCapturer>(
1479 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001480 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1481 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001482 EXPECT_TRUE(DoGetStats(nullptr));
1483}
1484
Harald Alvestrandc72af932018-01-11 17:18:19 +01001485TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
1486 CreatePeerConnectionWithoutDtls();
1487 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1488 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1489 rtc::scoped_refptr<VideoTrackInterface> video_track(
1490 pc_factory_->CreateVideoTrack(
1491 "video_track", pc_factory_->CreateVideoSource(
1492 std::unique_ptr<cricket::VideoCapturer>(
1493 new cricket::FakeVideoCapturer()))));
1494 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1495 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
1496 EXPECT_TRUE(audio_sender.ok());
1497 EXPECT_TRUE(video_sender.ok());
1498 EXPECT_NE(0, video_sender.value()->AttachmentId());
1499 EXPECT_NE(0, audio_sender.value()->AttachmentId());
1500}
1501
1502TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddStream) {
1503 CreatePeerConnectionWithoutDtls();
1504 AddVideoStream(kStreamLabel1);
1505 auto senders = pc_->GetSenders();
1506 EXPECT_EQ(1u, senders.size());
1507 EXPECT_NE(0, senders[0]->AttachmentId());
1508}
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1511 InitiateCall();
1512 WaitAndVerifyOnAddStream(kStreamLabel1);
1513 VerifyRemoteRtpHeaderExtensions();
1514}
1515
1516TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001517 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001518 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 CreateOfferAsLocalDescription();
1520 std::string offer;
1521 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1522 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1523 WaitAndVerifyOnAddStream(kStreamLabel1);
1524}
1525
1526TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001527 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001528 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529
1530 CreateOfferAsRemoteDescription();
1531 CreateAnswerAsLocalDescription();
1532
1533 WaitAndVerifyOnAddStream(kStreamLabel1);
1534}
1535
1536TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001537 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001538 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 CreateOfferAsRemoteDescription();
1541 CreatePrAnswerAsLocalDescription();
1542 CreateAnswerAsLocalDescription();
1543
1544 WaitAndVerifyOnAddStream(kStreamLabel1);
1545}
1546
1547TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1548 InitiateCall();
1549 ASSERT_EQ(1u, pc_->remote_streams()->count());
1550 pc_->RemoveStream(pc_->local_streams()->at(0));
1551 CreateOfferReceiveAnswer();
1552 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001553 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 CreateOfferReceiveAnswer();
1555}
1556
1557// Tests that after negotiating an audio only call, the respondent can perform a
1558// renegotiation that removes the audio stream.
1559TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001560 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 AddVoiceStream(kStreamLabel1);
1562 CreateOfferAsRemoteDescription();
1563 CreateAnswerAsLocalDescription();
1564
1565 ASSERT_EQ(1u, pc_->remote_streams()->count());
1566 pc_->RemoveStream(pc_->local_streams()->at(0));
1567 CreateOfferReceiveAnswer();
1568 EXPECT_EQ(0u, pc_->remote_streams()->count());
1569}
1570
1571// Test that candidates are generated and that we can parse our own candidates.
1572TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001573 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574
Steve Antonf1c6db12017-10-13 11:13:35 -07001575 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001577 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001578 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001579 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001580 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581
1582 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001583 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001584 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001585 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
Steve Antonf1c6db12017-10-13 11:13:35 -07001587 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001588 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589
Steve Antonf1c6db12017-10-13 11:13:35 -07001590 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591}
1592
deadbeefab9b2d12015-10-14 11:33:11 -07001593// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594// not unique.
1595TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001596 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001599 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001600 EXPECT_TRUE(offer);
1601 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
1603 // Create a local stream with audio&video tracks having same label.
1604 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1605
1606 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001607 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
1609 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001610 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001611 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612}
1613
1614// Test that we will get different SSRCs for each tracks in the offer and answer
1615// we created.
1616TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001617 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 // Create a local stream with audio&video tracks having different labels.
1619 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1620
1621 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001622 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001623 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 int audio_ssrc = 0;
1625 int video_ssrc = 0;
1626 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1627 &audio_ssrc));
1628 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1629 &video_ssrc));
1630 EXPECT_NE(audio_ssrc, video_ssrc);
1631
1632 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001633 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001634 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001635 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 audio_ssrc = 0;
1637 video_ssrc = 0;
1638 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1639 &audio_ssrc));
1640 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1641 &video_ssrc));
1642 EXPECT_NE(audio_ssrc, video_ssrc);
1643}
1644
deadbeefeb459812015-12-15 19:24:43 -08001645// Test that it's possible to call AddTrack on a MediaStream after adding
1646// the stream to a PeerConnection.
1647// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1648TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001649 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001650 // Create audio stream and add to PeerConnection.
1651 AddVoiceStream(kStreamLabel1);
1652 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1653
1654 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001655 rtc::scoped_refptr<VideoTrackInterface> video_track(
1656 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001657 "video_label", pc_factory_->CreateVideoSource(
1658 std::unique_ptr<cricket::VideoCapturer>(
1659 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001660 stream->AddTrack(video_track.get());
1661
kwibergd1fe2812016-04-27 06:47:29 -07001662 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001663 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001664
1665 const cricket::MediaContentDescription* video_desc =
1666 cricket::GetFirstVideoContentDescription(offer->description());
1667 EXPECT_TRUE(video_desc != nullptr);
1668}
1669
1670// Test that it's possible to call RemoveTrack on a MediaStream after adding
1671// the stream to a PeerConnection.
1672// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1673TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001674 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001675 // Create audio/video stream and add to PeerConnection.
1676 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1677 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1678
1679 // Remove the video track.
1680 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1681
kwibergd1fe2812016-04-27 06:47:29 -07001682 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001683 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001684
1685 const cricket::MediaContentDescription* video_desc =
1686 cricket::GetFirstVideoContentDescription(offer->description());
1687 EXPECT_TRUE(video_desc == nullptr);
1688}
1689
deadbeef1dcb1642017-03-29 21:08:16 -07001690// Verify that CreateDtmfSender only succeeds if called with a valid local
1691// track. Other aspects of DtmfSenders are tested in
1692// peerconnection_integrationtest.cc.
1693TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1694 CreatePeerConnection();
1695 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1696 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1697 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1698 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1699 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1700}
1701
deadbeefbd7d8f72015-12-18 16:58:44 -08001702// Test creating a sender with a stream ID, and ensure the ID is populated
1703// in the offer.
1704TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001705 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001706 pc_->CreateSender("video", kStreamLabel1);
1707
kwibergd1fe2812016-04-27 06:47:29 -07001708 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001709 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001710
1711 const cricket::MediaContentDescription* video_desc =
1712 cricket::GetFirstVideoContentDescription(offer->description());
1713 ASSERT_TRUE(video_desc != nullptr);
1714 ASSERT_EQ(1u, video_desc->streams().size());
1715 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1716}
1717
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718// Test that we can specify a certain track that we want statistics about.
1719TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1720 InitiateCall();
1721 ASSERT_LT(0u, pc_->remote_streams()->count());
1722 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001723 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1725 EXPECT_TRUE(DoGetStats(remote_audio));
1726
1727 // Remove the stream. Since we are sending to our selves the local
1728 // and the remote stream is the same.
1729 pc_->RemoveStream(pc_->local_streams()->at(0));
1730 // Do a re-negotiation.
1731 CreateOfferReceiveAnswer();
1732
1733 ASSERT_EQ(0u, pc_->remote_streams()->count());
1734
1735 // Test that we still can get statistics for the old track. Even if it is not
1736 // sent any longer.
1737 EXPECT_TRUE(DoGetStats(remote_audio));
1738}
1739
1740// Test that we can get stats on a video track.
1741TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1742 InitiateCall();
1743 ASSERT_LT(0u, pc_->remote_streams()->count());
1744 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001745 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1747 EXPECT_TRUE(DoGetStats(remote_video));
1748}
1749
1750// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001751TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001753 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 pc_factory_->CreateAudioTrack("unknown track", NULL));
1755 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1756}
1757
Harald Alvestrand89061872018-01-02 14:08:34 +01001758TEST_F(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
1759 CreatePeerConnectionWithoutDtls();
1760 EXPECT_TRUE(DoGetRTCStats());
1761 // Clearing stats cache is needed now, but should be temporary.
1762 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1763 pc_->ClearStatsCache();
1764 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
1765 EXPECT_TRUE(DoGetRTCStats());
1766 pc_->ClearStatsCache();
1767 CreateOfferReceiveAnswer();
1768 EXPECT_TRUE(DoGetRTCStats());
1769}
1770
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 FakeConstraints constraints;
1774 constraints.SetAllowRtpDataChannels();
1775 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001776 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001778 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 pc_->CreateDataChannel("test2", NULL);
1780 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 new MockDataChannelObserver(data2));
1785
1786 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1787 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1788 std::string data_to_send1 = "testing testing";
1789 std::string data_to_send2 = "testing something else";
1790 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1791
1792 CreateOfferReceiveAnswer();
1793 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1794 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1795
1796 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1797 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1798 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1799 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1800
1801 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1802 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1803
1804 data1->Close();
1805 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1806 CreateOfferReceiveAnswer();
1807 EXPECT_FALSE(observer1->IsOpen());
1808 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1809 EXPECT_TRUE(observer2->IsOpen());
1810
1811 data_to_send2 = "testing something else again";
1812 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1813
1814 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1815}
1816
1817// This test verifies that sendnig binary data over RTP data channels should
1818// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 FakeConstraints constraints;
1821 constraints.SetAllowRtpDataChannels();
1822 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001823 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001825 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 pc_->CreateDataChannel("test2", NULL);
1827 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001828 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001830 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 new MockDataChannelObserver(data2));
1832
1833 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1834 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1835
1836 CreateOfferReceiveAnswer();
1837 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1838 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1839
1840 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1841 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1842
jbaucheec21bd2016-03-20 06:15:43 -07001843 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1845}
1846
1847// This test setup a RTP data channels in loop back and test that a channel is
1848// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 FakeConstraints constraints;
1851 constraints.SetAllowRtpDataChannels();
1852 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001853 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001855 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 new MockDataChannelObserver(data1));
1857
1858 CreateOfferReceiveAnswerWithoutSsrc();
1859
1860 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1861
1862 data1->Close();
1863 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1864 CreateOfferReceiveAnswerWithoutSsrc();
1865 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1866 EXPECT_FALSE(observer1->IsOpen());
1867}
1868
1869// This test that if a data channel is added in an answer a receive only channel
1870// channel is created.
1871TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1872 FakeConstraints constraints;
1873 constraints.SetAllowRtpDataChannels();
1874 CreatePeerConnection(&constraints);
1875
1876 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 pc_->CreateDataChannel(offer_label, NULL);
1879
1880 CreateOfferAsLocalDescription();
1881
1882 // Replace the data channel label in the offer and apply it as an answer.
1883 std::string receive_label = "answer_channel";
1884 std::string sdp;
1885 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001886 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 receive_label.c_str(), receive_label.length(),
1888 &sdp);
1889 CreateAnswerAsRemoteDescription(sdp);
1890
1891 // Verify that a new incoming data channel has been created and that
1892 // it is open but can't we written to.
1893 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1894 DataChannelInterface* received_channel = observer_.last_datachannel_;
1895 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1896 EXPECT_EQ(receive_label, received_channel->label());
1897 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1898
1899 // Verify that the channel we initially offered has been rejected.
1900 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1901
1902 // Do another offer / answer exchange and verify that the data channel is
1903 // opened.
1904 CreateOfferReceiveAnswer();
1905 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1906 kTimeout);
1907}
1908
1909// This test that no data channel is returned if a reliable channel is
1910// requested.
1911// TODO(perkj): Remove this test once reliable channels are implemented.
1912TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1913 FakeConstraints constraints;
1914 constraints.SetAllowRtpDataChannels();
1915 CreatePeerConnection(&constraints);
1916
1917 std::string label = "test";
1918 webrtc::DataChannelInit config;
1919 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001920 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 pc_->CreateDataChannel(label, &config);
1922 EXPECT_TRUE(channel == NULL);
1923}
1924
deadbeefab9b2d12015-10-14 11:33:11 -07001925// Verifies that duplicated label is not allowed for RTP data channel.
1926TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1927 FakeConstraints constraints;
1928 constraints.SetAllowRtpDataChannels();
1929 CreatePeerConnection(&constraints);
1930
1931 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001932 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001933 pc_->CreateDataChannel(label, nullptr);
1934 EXPECT_NE(channel, nullptr);
1935
zhihuang9763d562016-08-05 11:14:50 -07001936 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001937 pc_->CreateDataChannel(label, nullptr);
1938 EXPECT_EQ(dup_channel, nullptr);
1939}
1940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941// This tests that a SCTP data channel is returned using different
1942// DataChannelInit configurations.
1943TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1944 FakeConstraints constraints;
1945 constraints.SetAllowDtlsSctpDataChannels();
1946 CreatePeerConnection(&constraints);
1947
1948 webrtc::DataChannelInit config;
1949
zhihuang9763d562016-08-05 11:14:50 -07001950 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 pc_->CreateDataChannel("1", &config);
1952 EXPECT_TRUE(channel != NULL);
1953 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001954 EXPECT_TRUE(observer_.renegotiation_needed_);
1955 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956
1957 config.ordered = false;
1958 channel = pc_->CreateDataChannel("2", &config);
1959 EXPECT_TRUE(channel != NULL);
1960 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001961 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 config.ordered = true;
1964 config.maxRetransmits = 0;
1965 channel = pc_->CreateDataChannel("3", &config);
1966 EXPECT_TRUE(channel != NULL);
1967 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001968 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969
1970 config.maxRetransmits = -1;
1971 config.maxRetransmitTime = 0;
1972 channel = pc_->CreateDataChannel("4", &config);
1973 EXPECT_TRUE(channel != NULL);
1974 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001975 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976}
1977
1978// This tests that no data channel is returned if both maxRetransmits and
1979// maxRetransmitTime are set for SCTP data channels.
1980TEST_F(PeerConnectionInterfaceTest,
1981 CreateSctpDataChannelShouldFailForInvalidConfig) {
1982 FakeConstraints constraints;
1983 constraints.SetAllowDtlsSctpDataChannels();
1984 CreatePeerConnection(&constraints);
1985
1986 std::string label = "test";
1987 webrtc::DataChannelInit config;
1988 config.maxRetransmits = 0;
1989 config.maxRetransmitTime = 0;
1990
zhihuang9763d562016-08-05 11:14:50 -07001991 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 pc_->CreateDataChannel(label, &config);
1993 EXPECT_TRUE(channel == NULL);
1994}
1995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996// The test verifies that creating a SCTP data channel with an id already in use
1997// or out of range should fail.
1998TEST_F(PeerConnectionInterfaceTest,
1999 CreateSctpDataChannelWithInvalidIdShouldFail) {
2000 FakeConstraints constraints;
2001 constraints.SetAllowDtlsSctpDataChannels();
2002 CreatePeerConnection(&constraints);
2003
2004 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002005 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002007 config.id = 1;
2008 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 EXPECT_TRUE(channel != NULL);
2010 EXPECT_EQ(1, channel->id());
2011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 channel = pc_->CreateDataChannel("x", &config);
2013 EXPECT_TRUE(channel == NULL);
2014
2015 config.id = cricket::kMaxSctpSid;
2016 channel = pc_->CreateDataChannel("max", &config);
2017 EXPECT_TRUE(channel != NULL);
2018 EXPECT_EQ(config.id, channel->id());
2019
2020 config.id = cricket::kMaxSctpSid + 1;
2021 channel = pc_->CreateDataChannel("x", &config);
2022 EXPECT_TRUE(channel == NULL);
2023}
2024
deadbeefab9b2d12015-10-14 11:33:11 -07002025// Verifies that duplicated label is allowed for SCTP data channel.
2026TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2027 FakeConstraints constraints;
2028 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2029 true);
2030 CreatePeerConnection(&constraints);
2031
2032 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002033 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002034 pc_->CreateDataChannel(label, nullptr);
2035 EXPECT_NE(channel, nullptr);
2036
zhihuang9763d562016-08-05 11:14:50 -07002037 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002038 pc_->CreateDataChannel(label, nullptr);
2039 EXPECT_NE(dup_channel, nullptr);
2040}
2041
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002042// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2043// DataChannel.
2044TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2045 FakeConstraints constraints;
2046 constraints.SetAllowRtpDataChannels();
2047 CreatePeerConnection(&constraints);
2048
zhihuang9763d562016-08-05 11:14:50 -07002049 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002050 pc_->CreateDataChannel("test1", NULL);
2051 EXPECT_TRUE(observer_.renegotiation_needed_);
2052 observer_.renegotiation_needed_ = false;
2053
zhihuang9763d562016-08-05 11:14:50 -07002054 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002055 pc_->CreateDataChannel("test2", NULL);
2056 EXPECT_TRUE(observer_.renegotiation_needed_);
2057}
2058
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 FakeConstraints constraints;
2062 constraints.SetAllowRtpDataChannels();
2063 CreatePeerConnection(&constraints);
2064
zhihuang9763d562016-08-05 11:14:50 -07002065 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002067 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 pc_->CreateDataChannel("test2", NULL);
2069 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002070 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002072 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 new MockDataChannelObserver(data2));
2074
2075 CreateOfferReceiveAnswer();
2076 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2077 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2078
2079 ReleasePeerConnection();
2080 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2081 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2082}
2083
2084// This test that data channels can be rejected in an answer.
2085TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2086 FakeConstraints constraints;
2087 constraints.SetAllowRtpDataChannels();
2088 CreatePeerConnection(&constraints);
2089
zhihuang9763d562016-08-05 11:14:50 -07002090 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 pc_->CreateDataChannel("offer_channel", NULL));
2092
2093 CreateOfferAsLocalDescription();
2094
2095 // Create an answer where the m-line for data channels are rejected.
2096 std::string sdp;
2097 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002098 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002099 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002100 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 cricket::ContentInfo* data_info =
2102 answer->description()->GetContentByName("data");
2103 data_info->rejected = true;
2104
Steve Antondb45ca82017-09-11 18:27:34 -07002105 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2107}
2108
2109// Test that we can create a session description from an SDP string from
2110// FireFox, use it as a remote session description, generate an answer and use
2111// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002112TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 FakeConstraints constraints;
2114 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2115 true);
2116 CreatePeerConnection(&constraints);
2117 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002118 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002119 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002120 webrtc::kFireFoxSdpOffer, nullptr));
2121 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 CreateAnswerAsLocalDescription();
2123 ASSERT_TRUE(pc_->local_description() != NULL);
2124 ASSERT_TRUE(pc_->remote_description() != NULL);
2125
2126 const cricket::ContentInfo* content =
2127 cricket::GetFirstAudioContent(pc_->local_description()->description());
2128 ASSERT_TRUE(content != NULL);
2129 EXPECT_FALSE(content->rejected);
2130
2131 content =
2132 cricket::GetFirstVideoContent(pc_->local_description()->description());
2133 ASSERT_TRUE(content != NULL);
2134 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002135#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 content =
2137 cricket::GetFirstDataContent(pc_->local_description()->description());
2138 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002139 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002141#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142}
2143
zhihuangb19012e2017-09-19 13:47:59 -07002144// Test that fallback from DTLS to SDES is not supported.
2145// The fallback was previously supported but was removed to simplify the code
2146// and because it's non-standard.
2147TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002148 FakeConstraints constraints;
2149 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2150 true);
2151 CreatePeerConnection(&constraints);
2152 // Wait for fake certificate to be generated. Previously, this is what caused
2153 // the "a=crypto" lines to be rejected.
2154 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2155 ASSERT_NE(nullptr, fake_certificate_generator_);
2156 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2157 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002158 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002159 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2160 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002161 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002162}
2163
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164// Test that we can create an audio only offer and receive an answer with a
2165// limited set of audio codecs and receive an updated offer with more audio
2166// codecs, where the added codecs are not supported.
2167TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002168 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 AddVoiceStream("audio_label");
2170 CreateOfferAsLocalDescription();
2171
Steve Antondb45ca82017-09-11 18:27:34 -07002172 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002173 webrtc::CreateSessionDescription(SdpType::kAnswer, webrtc::kAudioSdp,
2174 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002175 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176
Steve Antondb45ca82017-09-11 18:27:34 -07002177 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002178 webrtc::CreateSessionDescription(
2179 SdpType::kOffer, webrtc::kAudioSdpWithUnsupportedCodecs, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002180 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 CreateAnswerAsLocalDescription();
2182}
2183
deadbeefc80741f2015-10-22 13:14:45 -07002184// Test that if we're receiving (but not sending) a track, subsequent offers
2185// will have m-lines with a=recvonly.
2186TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2187 FakeConstraints constraints;
2188 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2189 true);
2190 CreatePeerConnection(&constraints);
2191 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2192 CreateAnswerAsLocalDescription();
2193
2194 // At this point we should be receiving stream 1, but not sending anything.
2195 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002196 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002197 DoCreateOffer(&offer, nullptr);
2198
2199 const cricket::ContentInfo* video_content =
2200 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002201 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2202 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002203
2204 const cricket::ContentInfo* audio_content =
2205 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002206 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2207 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002208}
2209
2210// Test that if we're receiving (but not sending) a track, and the
2211// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2212// false, the generated m-lines will be a=inactive.
2213TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2214 FakeConstraints constraints;
2215 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2216 true);
2217 CreatePeerConnection(&constraints);
2218 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2219 CreateAnswerAsLocalDescription();
2220
2221 // At this point we should be receiving stream 1, but not sending anything.
2222 // A new offer would be recvonly, but we'll set the "no receive" constraints
2223 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002224 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002225 FakeConstraints offer_constraints;
2226 offer_constraints.AddMandatory(
2227 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2228 offer_constraints.AddMandatory(
2229 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2230 DoCreateOffer(&offer, &offer_constraints);
2231
2232 const cricket::ContentInfo* video_content =
2233 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002234 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2235 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002236
2237 const cricket::ContentInfo* audio_content =
2238 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002239 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2240 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002241}
2242
deadbeef653b8e02015-11-11 12:55:10 -08002243// Test that we can use SetConfiguration to change the ICE servers of the
2244// PortAllocator.
2245TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2246 CreatePeerConnection();
2247
2248 PeerConnectionInterface::RTCConfiguration config;
2249 PeerConnectionInterface::IceServer server;
2250 server.uri = "stun:test_hostname";
2251 config.servers.push_back(server);
2252 EXPECT_TRUE(pc_->SetConfiguration(config));
2253
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002254 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2255 EXPECT_EQ("test_hostname",
2256 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002257}
2258
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002259TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2260 CreatePeerConnection();
2261 PeerConnectionInterface::RTCConfiguration config;
2262 config.type = PeerConnectionInterface::kRelay;
2263 EXPECT_TRUE(pc_->SetConfiguration(config));
2264 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2265}
2266
deadbeef293e9262017-01-11 12:28:30 -08002267TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2268 PeerConnectionInterface::RTCConfiguration config;
2269 config.prune_turn_ports = false;
2270 CreatePeerConnection(config, nullptr);
2271 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2272
2273 config.prune_turn_ports = true;
2274 EXPECT_TRUE(pc_->SetConfiguration(config));
2275 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2276}
2277
skvladd1f5fda2017-02-03 16:54:05 -08002278// Test that the ice check interval can be changed. This does not verify that
2279// the setting makes it all the way to P2PTransportChannel, as that would
2280// require a very complex set of mocks.
2281TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2282 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002283 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002284 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002285 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002286 EXPECT_TRUE(pc_->SetConfiguration(config));
2287 PeerConnectionInterface::RTCConfiguration new_config =
2288 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002289 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002290}
2291
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002292// Test that when SetConfiguration changes both the pool size and other
2293// attributes, the pooled session is created with the updated attributes.
2294TEST_F(PeerConnectionInterfaceTest,
2295 SetConfigurationCreatesPooledSessionCorrectly) {
2296 CreatePeerConnection();
2297 PeerConnectionInterface::RTCConfiguration config;
2298 config.ice_candidate_pool_size = 1;
2299 PeerConnectionInterface::IceServer server;
2300 server.uri = kStunAddressOnly;
2301 config.servers.push_back(server);
2302 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002303 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002304
2305 const cricket::FakePortAllocatorSession* session =
2306 static_cast<const cricket::FakePortAllocatorSession*>(
2307 port_allocator_->GetPooledSession());
2308 ASSERT_NE(nullptr, session);
2309 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002310}
2311
deadbeef293e9262017-01-11 12:28:30 -08002312// Test that after SetLocalDescription, changing the pool size is not allowed,
2313// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002314TEST_F(PeerConnectionInterfaceTest,
2315 CantChangePoolSizeAfterSetLocalDescription) {
2316 CreatePeerConnection();
2317 // Start by setting a size of 1.
2318 PeerConnectionInterface::RTCConfiguration config;
2319 config.ice_candidate_pool_size = 1;
2320 EXPECT_TRUE(pc_->SetConfiguration(config));
2321
2322 // Set remote offer; can still change pool size at this point.
2323 CreateOfferAsRemoteDescription();
2324 config.ice_candidate_pool_size = 2;
2325 EXPECT_TRUE(pc_->SetConfiguration(config));
2326
2327 // Set local answer; now it's too late.
2328 CreateAnswerAsLocalDescription();
2329 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002330 RTCError error;
2331 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2332 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2333}
2334
deadbeef42a42632017-03-10 15:18:00 -08002335// Test that after setting an answer, extra pooled sessions are discarded. The
2336// ICE candidate pool is only intended to be used for the first offer/answer.
2337TEST_F(PeerConnectionInterfaceTest,
2338 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2339 CreatePeerConnection();
2340
2341 // Set a larger-than-necessary size.
2342 PeerConnectionInterface::RTCConfiguration config;
2343 config.ice_candidate_pool_size = 4;
2344 EXPECT_TRUE(pc_->SetConfiguration(config));
2345
2346 // Do offer/answer.
2347 CreateOfferAsRemoteDescription();
2348 CreateAnswerAsLocalDescription();
2349
2350 // Expect no pooled sessions to be left.
2351 const cricket::PortAllocatorSession* session =
2352 port_allocator_->GetPooledSession();
2353 EXPECT_EQ(nullptr, session);
2354}
2355
2356// After Close is called, pooled candidates should be discarded so as to not
2357// waste network resources.
2358TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2359 CreatePeerConnection();
2360
2361 PeerConnectionInterface::RTCConfiguration config;
2362 config.ice_candidate_pool_size = 3;
2363 EXPECT_TRUE(pc_->SetConfiguration(config));
2364 pc_->Close();
2365
2366 // Expect no pooled sessions to be left.
2367 const cricket::PortAllocatorSession* session =
2368 port_allocator_->GetPooledSession();
2369 EXPECT_EQ(nullptr, session);
2370}
2371
deadbeef293e9262017-01-11 12:28:30 -08002372// Test that SetConfiguration returns an invalid modification error if
2373// modifying a field in the configuration that isn't allowed to be modified.
2374TEST_F(PeerConnectionInterfaceTest,
2375 SetConfigurationReturnsInvalidModificationError) {
2376 PeerConnectionInterface::RTCConfiguration config;
2377 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2378 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2379 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2380 CreatePeerConnection(config, nullptr);
2381
2382 PeerConnectionInterface::RTCConfiguration modified_config = config;
2383 modified_config.bundle_policy =
2384 PeerConnectionInterface::kBundlePolicyMaxBundle;
2385 RTCError error;
2386 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2387 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2388
2389 modified_config = config;
2390 modified_config.rtcp_mux_policy =
2391 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2392 error.set_type(RTCErrorType::NONE);
2393 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2394 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2395
2396 modified_config = config;
2397 modified_config.continual_gathering_policy =
2398 PeerConnectionInterface::GATHER_CONTINUALLY;
2399 error.set_type(RTCErrorType::NONE);
2400 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2401 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2402}
2403
2404// Test that SetConfiguration returns a range error if the candidate pool size
2405// is negative or larger than allowed by the spec.
2406TEST_F(PeerConnectionInterfaceTest,
2407 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2408 PeerConnectionInterface::RTCConfiguration config;
2409 CreatePeerConnection(config, nullptr);
2410
2411 config.ice_candidate_pool_size = -1;
2412 RTCError error;
2413 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2414 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2415
2416 config.ice_candidate_pool_size = INT_MAX;
2417 error.set_type(RTCErrorType::NONE);
2418 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2419 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2420}
2421
2422// Test that SetConfiguration returns a syntax error if parsing an ICE server
2423// URL failed.
2424TEST_F(PeerConnectionInterfaceTest,
2425 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2426 PeerConnectionInterface::RTCConfiguration config;
2427 CreatePeerConnection(config, nullptr);
2428
2429 PeerConnectionInterface::IceServer bad_server;
2430 bad_server.uri = "stunn:www.example.com";
2431 config.servers.push_back(bad_server);
2432 RTCError error;
2433 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2434 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2435}
2436
2437// Test that SetConfiguration returns an invalid parameter error if a TURN
2438// IceServer is missing a username or password.
2439TEST_F(PeerConnectionInterfaceTest,
2440 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2441 PeerConnectionInterface::RTCConfiguration config;
2442 CreatePeerConnection(config, nullptr);
2443
2444 PeerConnectionInterface::IceServer bad_server;
2445 bad_server.uri = "turn:www.example.com";
2446 // Missing password.
2447 bad_server.username = "foo";
2448 config.servers.push_back(bad_server);
2449 RTCError error;
2450 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2451 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002452}
2453
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454// Test that PeerConnection::Close changes the states to closed and all remote
2455// tracks change state to ended.
2456TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2457 // Initialize a PeerConnection and negotiate local and remote session
2458 // description.
2459 InitiateCall();
2460 ASSERT_EQ(1u, pc_->local_streams()->count());
2461 ASSERT_EQ(1u, pc_->remote_streams()->count());
2462
2463 pc_->Close();
2464
2465 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2466 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2467 pc_->ice_connection_state());
2468 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2469 pc_->ice_gathering_state());
2470
2471 EXPECT_EQ(1u, pc_->local_streams()->count());
2472 EXPECT_EQ(1u, pc_->remote_streams()->count());
2473
zhihuang9763d562016-08-05 11:14:50 -07002474 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2475 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002476 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002477 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002478 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2479 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2480 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481}
2482
2483// Test that PeerConnection methods fails gracefully after
2484// PeerConnection::Close has been called.
2485TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002486 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2488 CreateOfferAsRemoteDescription();
2489 CreateAnswerAsLocalDescription();
2490
2491 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002492 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 pc_->local_streams()->at(0);
2494
2495 pc_->Close();
2496
2497 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002498 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499
2500 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002501 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002503 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504
2505 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2506
2507 EXPECT_TRUE(pc_->local_description() != NULL);
2508 EXPECT_TRUE(pc_->remote_description() != NULL);
2509
kwibergd1fe2812016-04-27 06:47:29 -07002510 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002511 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002512 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002513 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514
2515 std::string sdp;
2516 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002517 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002518 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002519 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002520
2521 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002522 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002523 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002524 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525}
2526
2527// Test that GetStats can still be called after PeerConnection::Close.
2528TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2529 InitiateCall();
2530 pc_->Close();
2531 DoGetStats(NULL);
2532}
deadbeefab9b2d12015-10-14 11:33:11 -07002533
2534// NOTE: The series of tests below come from what used to be
2535// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2536// setting a remote or local description has the expected effects.
2537
2538// This test verifies that the remote MediaStreams corresponding to a received
2539// SDP string is created. In this test the two separate MediaStreams are
2540// signaled.
2541TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2542 FakeConstraints constraints;
2543 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2544 true);
2545 CreatePeerConnection(&constraints);
2546 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2547
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002548 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002549 EXPECT_TRUE(
2550 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2551 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2552 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2553
2554 // Create a session description based on another SDP with another
2555 // MediaStream.
2556 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2557
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002558 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002559 EXPECT_TRUE(
2560 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2561}
2562
2563// This test verifies that when remote tracks are added/removed from SDP, the
2564// created remote streams are updated appropriately.
2565TEST_F(PeerConnectionInterfaceTest,
2566 AddRemoveTrackFromExistingRemoteMediaStream) {
2567 FakeConstraints constraints;
2568 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2569 true);
2570 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002571 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002572 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002573 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002574 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2575 reference_collection_));
2576
2577 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002578 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002579 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002580 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002581 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2582 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002583 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002584 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2585 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002586 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002587 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2588 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002589
2590 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002592 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002593 MockTrackObserver audio_track_observer(audio_track2);
2594 MockTrackObserver video_track_observer(video_track2);
2595
2596 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2597 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002598 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002599 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2600 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002601 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002602 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002603 audio_track2->state(), kTimeout);
2604 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2605 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002606}
2607
2608// This tests that remote tracks are ended if a local session description is set
2609// that rejects the media content type.
2610TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2611 FakeConstraints constraints;
2612 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2613 true);
2614 CreatePeerConnection(&constraints);
2615 // First create and set a remote offer, then reject its video content in our
2616 // answer.
2617 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2618 ASSERT_EQ(1u, observer_.remote_streams()->count());
2619 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2620 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2621 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2622
2623 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2624 remote_stream->GetVideoTracks()[0];
2625 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2626 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2627 remote_stream->GetAudioTracks()[0];
2628 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2629
kwibergd1fe2812016-04-27 06:47:29 -07002630 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002631 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002632 cricket::ContentInfo* video_info =
2633 local_answer->description()->GetContentByName("video");
2634 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002635 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002636 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2637 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2638
2639 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002640 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002641 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002642 video_info = local_offer->description()->GetContentByName("video");
2643 ASSERT_TRUE(video_info != nullptr);
2644 video_info->rejected = true;
2645 cricket::ContentInfo* audio_info =
2646 local_offer->description()->GetContentByName("audio");
2647 ASSERT_TRUE(audio_info != nullptr);
2648 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002649 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002650 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002651 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002652 remote_audio->state(), kTimeout);
2653 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2654 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002655}
2656
2657// This tests that we won't crash if the remote track has been removed outside
2658// of PeerConnection and then PeerConnection tries to reject the track.
2659TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2660 FakeConstraints constraints;
2661 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2662 true);
2663 CreatePeerConnection(&constraints);
2664 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2665 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2666 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2667 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2668
kwibergd1fe2812016-04-27 06:47:29 -07002669 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002670 webrtc::CreateSessionDescription(SdpType::kAnswer, kSdpStringWithStream1,
2671 nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002672 cricket::ContentInfo* video_info =
2673 local_answer->description()->GetContentByName("video");
2674 video_info->rejected = true;
2675 cricket::ContentInfo* audio_info =
2676 local_answer->description()->GetContentByName("audio");
2677 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002678 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002679
2680 // No crash is a pass.
2681}
2682
deadbeef5e97fb52015-10-15 12:49:08 -07002683// This tests that if a recvonly remote description is set, no remote streams
2684// will be created, even if the description contains SSRCs/MSIDs.
2685// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2686TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2687 FakeConstraints constraints;
2688 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2689 true);
2690 CreatePeerConnection(&constraints);
2691
2692 std::string recvonly_offer = kSdpStringWithStream1;
2693 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2694 strlen(kRecvonly), &recvonly_offer);
2695 CreateAndSetRemoteOffer(recvonly_offer);
2696
2697 EXPECT_EQ(0u, observer_.remote_streams()->count());
2698}
2699
deadbeefab9b2d12015-10-14 11:33:11 -07002700// This tests that a default MediaStream is created if a remote session
2701// description doesn't contain any streams and no MSID support.
2702// It also tests that the default stream is updated if a video m-line is added
2703// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002704TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002705 FakeConstraints constraints;
2706 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2707 true);
2708 CreatePeerConnection(&constraints);
2709 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2710
2711 ASSERT_EQ(1u, observer_.remote_streams()->count());
2712 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2713
2714 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2715 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2716 EXPECT_EQ("default", remote_stream->label());
2717
2718 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2719 ASSERT_EQ(1u, observer_.remote_streams()->count());
2720 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2721 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002722 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2723 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002724 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2725 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002726 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2727 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002728}
2729
2730// This tests that a default MediaStream is created if a remote session
2731// description doesn't contain any streams and media direction is send only.
2732TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002733 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002734 FakeConstraints constraints;
2735 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2736 true);
2737 CreatePeerConnection(&constraints);
2738 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2739
2740 ASSERT_EQ(1u, observer_.remote_streams()->count());
2741 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2742
2743 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2744 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2745 EXPECT_EQ("default", remote_stream->label());
2746}
2747
2748// This tests that it won't crash when PeerConnection tries to remove
2749// a remote track that as already been removed from the MediaStream.
2750TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2751 FakeConstraints constraints;
2752 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2753 true);
2754 CreatePeerConnection(&constraints);
2755 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2756 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2757 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2758 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2759
2760 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2761
2762 // No crash is a pass.
2763}
2764
2765// This tests that a default MediaStream is created if the remote session
2766// description doesn't contain any streams and don't contain an indication if
2767// MSID is supported.
2768TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002769 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002770 FakeConstraints constraints;
2771 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2772 true);
2773 CreatePeerConnection(&constraints);
2774 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2775
2776 ASSERT_EQ(1u, observer_.remote_streams()->count());
2777 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2778 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2779 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2780}
2781
2782// This tests that a default MediaStream is not created if the remote session
2783// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002784TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002785 FakeConstraints constraints;
2786 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2787 true);
2788 CreatePeerConnection(&constraints);
2789 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2790 EXPECT_EQ(0u, observer_.remote_streams()->count());
2791}
2792
deadbeefbda7e0b2015-12-08 17:13:40 -08002793// This tests that when setting a new description, the old default tracks are
2794// not destroyed and recreated.
2795// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002796TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002797 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002798 FakeConstraints constraints;
2799 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2800 true);
2801 CreatePeerConnection(&constraints);
2802 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2803
2804 ASSERT_EQ(1u, observer_.remote_streams()->count());
2805 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2806 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2807
2808 // Set the track to "disabled", then set a new description and ensure the
2809 // track is still disabled, which ensures it hasn't been recreated.
2810 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2811 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2812 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2813 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2814}
2815
deadbeefab9b2d12015-10-14 11:33:11 -07002816// This tests that a default MediaStream is not created if a remote session
2817// description is updated to not have any MediaStreams.
2818TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2819 FakeConstraints constraints;
2820 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2821 true);
2822 CreatePeerConnection(&constraints);
2823 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002824 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002825 EXPECT_TRUE(
2826 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2827
2828 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2829 EXPECT_EQ(0u, observer_.remote_streams()->count());
2830}
2831
2832// This tests that an RtpSender is created when the local description is set
2833// after adding a local stream.
2834// TODO(deadbeef): This test and the one below it need to be updated when
2835// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002836TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002837 FakeConstraints constraints;
2838 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2839 true);
2840 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002841
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002842 // Create an offer with 1 stream with 2 tracks of each type.
2843 rtc::scoped_refptr<StreamCollection> stream_collection =
2844 CreateStreamCollection(1, 2);
2845 pc_->AddStream(stream_collection->at(0));
2846 std::unique_ptr<SessionDescriptionInterface> offer;
2847 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002848 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002849
deadbeefab9b2d12015-10-14 11:33:11 -07002850 auto senders = pc_->GetSenders();
2851 EXPECT_EQ(4u, senders.size());
2852 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2853 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2854 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2855 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2856
2857 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002858 pc_->RemoveStream(stream_collection->at(0));
2859 stream_collection = CreateStreamCollection(1, 1);
2860 pc_->AddStream(stream_collection->at(0));
2861 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002862 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002863
deadbeefab9b2d12015-10-14 11:33:11 -07002864 senders = pc_->GetSenders();
2865 EXPECT_EQ(2u, senders.size());
2866 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2867 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2868 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2869 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2870}
2871
2872// This tests that an RtpSender is created when the local description is set
2873// before adding a local stream.
2874TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002875 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002876 FakeConstraints constraints;
2877 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2878 true);
2879 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002880
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002881 rtc::scoped_refptr<StreamCollection> stream_collection =
2882 CreateStreamCollection(1, 2);
2883 // Add a stream to create the offer, but remove it afterwards.
2884 pc_->AddStream(stream_collection->at(0));
2885 std::unique_ptr<SessionDescriptionInterface> offer;
2886 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2887 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002888
Steve Antondb45ca82017-09-11 18:27:34 -07002889 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002890 auto senders = pc_->GetSenders();
2891 EXPECT_EQ(0u, senders.size());
2892
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002893 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002894 senders = pc_->GetSenders();
2895 EXPECT_EQ(4u, senders.size());
2896 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2897 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2898 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2899 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2900}
2901
2902// This tests that the expected behavior occurs if the SSRC on a local track is
2903// changed when SetLocalDescription is called.
2904TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002905 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002906 FakeConstraints constraints;
2907 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2908 true);
2909 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002910
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002911 rtc::scoped_refptr<StreamCollection> stream_collection =
2912 CreateStreamCollection(2, 1);
2913 pc_->AddStream(stream_collection->at(0));
2914 std::unique_ptr<SessionDescriptionInterface> offer;
2915 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2916 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08002917 auto modified_offer =
2918 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002919 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2920 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002921 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002922
deadbeefab9b2d12015-10-14 11:33:11 -07002923 auto senders = pc_->GetSenders();
2924 EXPECT_EQ(2u, senders.size());
2925 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2926 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2927
2928 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002929 cricket::MediaContentDescription* desc =
2930 cricket::GetFirstAudioContentDescription(modified_offer->description());
2931 ASSERT_TRUE(desc != NULL);
2932 for (StreamParams& stream : desc->mutable_streams()) {
2933 for (unsigned int& ssrc : stream.ssrcs) {
2934 ++ssrc;
2935 }
2936 }
deadbeefab9b2d12015-10-14 11:33:11 -07002937
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002938 desc =
2939 cricket::GetFirstVideoContentDescription(modified_offer->description());
2940 ASSERT_TRUE(desc != NULL);
2941 for (StreamParams& stream : desc->mutable_streams()) {
2942 for (unsigned int& ssrc : stream.ssrcs) {
2943 ++ssrc;
2944 }
2945 }
2946
Steve Antondb45ca82017-09-11 18:27:34 -07002947 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002948 senders = pc_->GetSenders();
2949 EXPECT_EQ(2u, senders.size());
2950 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2951 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2952 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2953 // changed.
2954}
2955
2956// This tests that the expected behavior occurs if a new session description is
2957// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002958TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002959 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002960 FakeConstraints constraints;
2961 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2962 true);
2963 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002964
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002965 rtc::scoped_refptr<StreamCollection> stream_collection =
2966 CreateStreamCollection(2, 1);
2967 pc_->AddStream(stream_collection->at(0));
2968 std::unique_ptr<SessionDescriptionInterface> offer;
2969 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002970 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002971
deadbeefab9b2d12015-10-14 11:33:11 -07002972 auto senders = pc_->GetSenders();
2973 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002974 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2975 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002976
2977 // Add a new MediaStream but with the same tracks as in the first stream.
2978 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2979 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002980 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2981 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002982 pc_->AddStream(stream_1);
2983
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002984 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002985 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002986
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002987 auto new_senders = pc_->GetSenders();
2988 // Should be the same senders as before, but with updated stream id.
2989 // Note that this behavior is subject to change in the future.
2990 // We may decide the PC should ignore existing tracks in AddStream.
2991 EXPECT_EQ(senders, new_senders);
2992 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2993 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002994}
2995
zhihuang81c3a032016-11-17 12:06:24 -08002996// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2997TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2998 FakeConstraints constraints;
2999 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3000 true);
3001 CreatePeerConnection(&constraints);
3002 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3003 EXPECT_EQ(observer_.num_added_tracks_, 1);
3004 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3005
3006 // Create and set the updated remote SDP.
3007 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3008 EXPECT_EQ(observer_.num_added_tracks_, 2);
3009 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3010}
3011
deadbeefd1a38b52016-12-10 13:15:33 -08003012// Test that when SetConfiguration is called and the configuration is
3013// changing, the next offer causes an ICE restart.
3014TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3015 PeerConnectionInterface::RTCConfiguration config;
3016 config.type = PeerConnectionInterface::kRelay;
3017 // Need to pass default constraints to prevent disabling of DTLS...
3018 FakeConstraints default_constraints;
3019 CreatePeerConnection(config, &default_constraints);
3020 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3021
3022 // Do initial offer/answer so there's something to restart.
3023 CreateOfferAsLocalDescription();
3024 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3025
3026 // Grab the ufrags.
3027 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3028
3029 // Change ICE policy, which should trigger an ICE restart on the next offer.
3030 config.type = PeerConnectionInterface::kAll;
3031 EXPECT_TRUE(pc_->SetConfiguration(config));
3032 CreateOfferAsLocalDescription();
3033
3034 // Grab the new ufrags.
3035 std::vector<std::string> subsequent_ufrags =
3036 GetUfrags(pc_->local_description());
3037
3038 // Sanity check.
3039 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3040 // Check that each ufrag is different.
3041 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3042 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3043 }
3044}
3045
3046// Test that when SetConfiguration is called and the configuration *isn't*
3047// changing, the next offer does *not* cause an ICE restart.
3048TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3049 PeerConnectionInterface::RTCConfiguration config;
3050 config.type = PeerConnectionInterface::kRelay;
3051 // Need to pass default constraints to prevent disabling of DTLS...
3052 FakeConstraints default_constraints;
3053 CreatePeerConnection(config, &default_constraints);
3054 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3055
3056 // Do initial offer/answer so there's something to restart.
3057 CreateOfferAsLocalDescription();
3058 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3059
3060 // Grab the ufrags.
3061 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3062
3063 // Call SetConfiguration with a config identical to what the PC was
3064 // constructed with.
3065 EXPECT_TRUE(pc_->SetConfiguration(config));
3066 CreateOfferAsLocalDescription();
3067
3068 // Grab the new ufrags.
3069 std::vector<std::string> subsequent_ufrags =
3070 GetUfrags(pc_->local_description());
3071
3072 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3073}
3074
3075// Test for a weird corner case scenario:
3076// 1. Audio/video session established.
3077// 2. SetConfiguration changes ICE config; ICE restart needed.
3078// 3. ICE restart initiated by remote peer, but only for one m= section.
3079// 4. Next createOffer should initiate an ICE restart, but only for the other
3080// m= section; it would be pointless to do an ICE restart for the m= section
3081// that was already restarted.
3082TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3083 PeerConnectionInterface::RTCConfiguration config;
3084 config.type = PeerConnectionInterface::kRelay;
3085 // Need to pass default constraints to prevent disabling of DTLS...
3086 FakeConstraints default_constraints;
3087 CreatePeerConnection(config, &default_constraints);
3088 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3089
3090 // Do initial offer/answer so there's something to restart.
3091 CreateOfferAsLocalDescription();
3092 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3093
3094 // Change ICE policy, which should set the "needs-ice-restart" flag.
3095 config.type = PeerConnectionInterface::kAll;
3096 EXPECT_TRUE(pc_->SetConfiguration(config));
3097
3098 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003099 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003100 webrtc::CreateSessionDescription(SdpType::kOffer, kSdpStringWithStream1,
3101 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003102 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003103 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3104 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003105 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003106 CreateAnswerAsLocalDescription();
3107
3108 // Grab the ufrags.
3109 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3110 ASSERT_EQ(2, initial_ufrags.size());
3111
3112 // Create offer and grab the new ufrags.
3113 CreateOfferAsLocalDescription();
3114 std::vector<std::string> subsequent_ufrags =
3115 GetUfrags(pc_->local_description());
3116 ASSERT_EQ(2, subsequent_ufrags.size());
3117
3118 // Ensure that only the ufrag for the second m= section changed.
3119 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3120 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3121}
3122
deadbeeffe4a8a42016-12-20 17:56:17 -08003123// Tests that the methods to return current/pending descriptions work as
3124// expected at different points in the offer/answer exchange. This test does
3125// one offer/answer exchange as the offerer, then another as the answerer.
3126TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3127 // This disables DTLS so we can apply an answer to ourselves.
3128 CreatePeerConnection();
3129
3130 // Create initial local offer and get SDP (which will also be used as
3131 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003132 std::unique_ptr<SessionDescriptionInterface> local_offer;
3133 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003134 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003135 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003136
3137 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003138 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3139 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3140 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003141 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3142 EXPECT_EQ(nullptr, pc_->current_local_description());
3143 EXPECT_EQ(nullptr, pc_->current_remote_description());
3144
3145 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003146 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003147 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003148 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3149 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3150 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3151 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003152 EXPECT_EQ(nullptr, pc_->current_local_description());
3153 EXPECT_EQ(nullptr, pc_->current_remote_description());
3154
3155 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003156 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003157 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003158 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3159 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003160 EXPECT_EQ(nullptr, pc_->pending_local_description());
3161 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003162 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3163 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003164
3165 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003166 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003167 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003168 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3169 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3170 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003171 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003172 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3173 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003174
3175 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003176 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003177 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003178 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3179 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3180 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3181 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3182 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3183 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003184
3185 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003186 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003187 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003188 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3189 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003190 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3191 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003192 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3193 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003194}
3195
zhihuang77985012017-02-07 15:45:16 -08003196// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3197// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003198// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003199TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003200 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003201 CreatePeerConnection();
3202 // The RtcEventLog will be reset when the PeerConnection is closed.
3203 pc_->Close();
3204
Elad Alon9e6565b2017-10-11 16:04:13 +02003205 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3206 std::string filename = webrtc::test::OutputPath() +
3207 test_info->test_case_name() + test_info->name();
3208 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3209
3210 constexpr int64_t max_size_bytes = 1024;
3211
zhihuang77985012017-02-07 15:45:16 -08003212 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3213 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003214
3215 // Cleanup.
3216 rtc::ClosePlatformFile(file);
3217 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003218}
3219
Elad Alon99c3fe52017-10-13 16:29:40 +02003220// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3221// after the PeerConnection is closed.
3222// This version tests the StartRtcEventLog version that receives an object
3223// of type |RtcEventLogOutput|.
3224TEST_F(PeerConnectionInterfaceTest,
3225 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3226 CreatePeerConnection();
3227 // The RtcEventLog will be reset when the PeerConnection is closed.
3228 pc_->Close();
3229
3230 rtc::PlatformFile file = 0;
3231 int64_t max_size_bytes = 1024;
3232 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003233 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3234 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003235 pc_->StopRtcEventLog();
3236}
3237
deadbeef30952b42017-04-21 02:41:29 -07003238// Test that generated offers/answers include "ice-option:trickle".
3239TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3240 CreatePeerConnection();
3241
3242 // First, create an offer with audio/video.
3243 FakeConstraints constraints;
3244 constraints.SetMandatoryReceiveAudio(true);
3245 constraints.SetMandatoryReceiveVideo(true);
3246 std::unique_ptr<SessionDescriptionInterface> offer;
3247 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3248 cricket::SessionDescription* desc = offer->description();
3249 ASSERT_EQ(2u, desc->transport_infos().size());
3250 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3251 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3252
3253 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003254 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003255 std::unique_ptr<SessionDescriptionInterface> answer;
3256 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3257 desc = answer->description();
3258 ASSERT_EQ(2u, desc->transport_infos().size());
3259 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3260 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3261}
3262
deadbeef1dcb1642017-03-29 21:08:16 -07003263// Test that ICE renomination isn't offered if it's not enabled in the PC's
3264// RTCConfiguration.
3265TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3266 PeerConnectionInterface::RTCConfiguration config;
3267 config.enable_ice_renomination = false;
3268 CreatePeerConnection(config, nullptr);
3269 AddVoiceStream("foo");
3270
3271 std::unique_ptr<SessionDescriptionInterface> offer;
3272 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3273 cricket::SessionDescription* desc = offer->description();
3274 EXPECT_EQ(1u, desc->transport_infos().size());
3275 EXPECT_FALSE(
3276 desc->transport_infos()[0].description.GetIceParameters().renomination);
3277}
3278
3279// Test that the ICE renomination option is present in generated offers/answers
3280// if it's enabled in the PC's RTCConfiguration.
3281TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3282 PeerConnectionInterface::RTCConfiguration config;
3283 config.enable_ice_renomination = true;
3284 CreatePeerConnection(config, nullptr);
3285 AddVoiceStream("foo");
3286
3287 std::unique_ptr<SessionDescriptionInterface> offer;
3288 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3289 cricket::SessionDescription* desc = offer->description();
3290 EXPECT_EQ(1u, desc->transport_infos().size());
3291 EXPECT_TRUE(
3292 desc->transport_infos()[0].description.GetIceParameters().renomination);
3293
3294 // Set the offer as a remote description, then create an answer and ensure it
3295 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003296 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003297 std::unique_ptr<SessionDescriptionInterface> answer;
3298 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3299 desc = answer->description();
3300 EXPECT_EQ(1u, desc->transport_infos().size());
3301 EXPECT_TRUE(
3302 desc->transport_infos()[0].description.GetIceParameters().renomination);
3303}
3304
3305// Test that if CreateOffer is called with the deprecated "offer to receive
3306// audio/video" constraints, they're processed and result in an offer with
3307// audio/video sections just as if RTCOfferAnswerOptions had been used.
3308TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3309 CreatePeerConnection();
3310
3311 FakeConstraints constraints;
3312 constraints.SetMandatoryReceiveAudio(true);
3313 constraints.SetMandatoryReceiveVideo(true);
3314 std::unique_ptr<SessionDescriptionInterface> offer;
3315 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3316
3317 cricket::SessionDescription* desc = offer->description();
3318 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3319 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3320 ASSERT_NE(nullptr, audio);
3321 ASSERT_NE(nullptr, video);
3322 EXPECT_FALSE(audio->rejected);
3323 EXPECT_FALSE(video->rejected);
3324}
3325
3326// Test that if CreateAnswer is called with the deprecated "offer to receive
3327// audio/video" constraints, they're processed and can be used to reject an
3328// offered m= section just as can be done with RTCOfferAnswerOptions;
3329TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3330 CreatePeerConnection();
3331
3332 // First, create an offer with audio/video and apply it as a remote
3333 // description.
3334 FakeConstraints constraints;
3335 constraints.SetMandatoryReceiveAudio(true);
3336 constraints.SetMandatoryReceiveVideo(true);
3337 std::unique_ptr<SessionDescriptionInterface> offer;
3338 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003339 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003340
3341 // Now create answer that rejects audio/video.
3342 constraints.SetMandatoryReceiveAudio(false);
3343 constraints.SetMandatoryReceiveVideo(false);
3344 std::unique_ptr<SessionDescriptionInterface> answer;
3345 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3346
3347 cricket::SessionDescription* desc = answer->description();
3348 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3349 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3350 ASSERT_NE(nullptr, audio);
3351 ASSERT_NE(nullptr, video);
3352 EXPECT_TRUE(audio->rejected);
3353 EXPECT_TRUE(video->rejected);
3354}
3355
3356#ifdef HAVE_SCTP
3357#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3358 DataChannelOnlyOfferWithMaxBundlePolicy
3359#else
3360#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3361 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3362#endif
3363
3364// Test that negotiation can succeed with a data channel only, and with the max
3365// bundle policy. Previously there was a bug that prevented this.
3366TEST_F(PeerConnectionInterfaceTest,
3367 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3368 PeerConnectionInterface::RTCConfiguration config;
3369 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3370 CreatePeerConnection(config, nullptr);
3371
3372 // First, create an offer with only a data channel and apply it as a remote
3373 // description.
3374 pc_->CreateDataChannel("test", nullptr);
3375 std::unique_ptr<SessionDescriptionInterface> offer;
3376 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003377 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003378
3379 // Create and set answer as well.
3380 std::unique_ptr<SessionDescriptionInterface> answer;
3381 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003382 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003383}
3384
zstein4b979802017-06-02 14:37:37 -07003385TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3386 CreatePeerConnection();
3387 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003388 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003389 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3390}
3391
3392TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3393 CreatePeerConnection();
3394 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003395 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003396 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3397}
3398
3399TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3400 CreatePeerConnection();
3401 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003402 bitrate.min_bitrate_bps = 5;
3403 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003404 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3405}
3406
3407TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3408 CreatePeerConnection();
3409 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003410 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003411 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3412}
3413
3414TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3415 CreatePeerConnection();
3416 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003417 bitrate.current_bitrate_bps = 10;
3418 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003419 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3420}
3421
3422TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3423 CreatePeerConnection();
3424 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003425 bitrate.min_bitrate_bps = 10;
3426 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003427 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3428}
3429
3430TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3431 CreatePeerConnection();
3432 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003433 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003434 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3435}
3436
Steve Anton038834f2017-07-14 15:59:59 -07003437// ice_regather_interval_range requires WebRTC to be configured for continual
3438// gathering already.
3439TEST_F(PeerConnectionInterfaceTest,
3440 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3441 PeerConnectionInterface::RTCConfiguration config;
3442 config.ice_regather_interval_range.emplace(1000, 2000);
3443 config.continual_gathering_policy =
3444 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3445 CreatePeerConnectionExpectFail(config);
3446}
3447
3448// Ensures that there is no error when ice_regather_interval_range is set with
3449// continual gathering enabled.
3450TEST_F(PeerConnectionInterfaceTest,
3451 SetIceRegatherIntervalRangeWithContinualGathering) {
3452 PeerConnectionInterface::RTCConfiguration config;
3453 config.ice_regather_interval_range.emplace(1000, 2000);
3454 config.continual_gathering_policy =
3455 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3456 CreatePeerConnection(config, nullptr);
3457}
3458
zstein4b979802017-06-02 14:37:37 -07003459// The current bitrate from Call's BitrateConfigMask is currently clamped by
3460// Call's BitrateConfig, which comes from the SDP or a default value. This test
3461// checks that a call to SetBitrate with a current bitrate that will be clamped
3462// succeeds.
3463TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3464 CreatePeerConnection();
3465 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003466 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003467 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3468}
3469
zhihuang1c378ed2017-08-17 14:10:50 -07003470// The following tests verify that the offer can be created correctly.
3471TEST_F(PeerConnectionInterfaceTest,
3472 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3473 RTCOfferAnswerOptions rtc_options;
3474
3475 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3476 // than kMaxOfferToReceiveMedia should be treated as invalid.
3477 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3478 CreatePeerConnection();
3479 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3480
3481 rtc_options.offer_to_receive_audio =
3482 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3483 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3484}
3485
3486TEST_F(PeerConnectionInterfaceTest,
3487 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3488 RTCOfferAnswerOptions rtc_options;
3489
3490 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3491 // than kMaxOfferToReceiveMedia should be treated as invalid.
3492 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3493 CreatePeerConnection();
3494 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3495
3496 rtc_options.offer_to_receive_video =
3497 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3498 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3499}
3500
3501// Test that the audio and video content will be added to an offer if both
3502// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3503TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3504 RTCOfferAnswerOptions rtc_options;
3505 rtc_options.offer_to_receive_audio = 1;
3506 rtc_options.offer_to_receive_video = 1;
3507
3508 std::unique_ptr<SessionDescriptionInterface> offer;
3509 CreatePeerConnection();
3510 offer = CreateOfferWithOptions(rtc_options);
3511 ASSERT_TRUE(offer);
3512 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3513 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3514}
3515
3516// Test that only audio content will be added to the offer if only
3517// |offer_to_receive_audio| options is 1.
3518TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3519 RTCOfferAnswerOptions rtc_options;
3520 rtc_options.offer_to_receive_audio = 1;
3521 rtc_options.offer_to_receive_video = 0;
3522
3523 std::unique_ptr<SessionDescriptionInterface> offer;
3524 CreatePeerConnection();
3525 offer = CreateOfferWithOptions(rtc_options);
3526 ASSERT_TRUE(offer);
3527 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3528 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3529}
3530
3531// Test that only video content will be added if only |offer_to_receive_video|
3532// options is 1.
3533TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3534 RTCOfferAnswerOptions rtc_options;
3535 rtc_options.offer_to_receive_audio = 0;
3536 rtc_options.offer_to_receive_video = 1;
3537
3538 std::unique_ptr<SessionDescriptionInterface> offer;
3539 CreatePeerConnection();
3540 offer = CreateOfferWithOptions(rtc_options);
3541 ASSERT_TRUE(offer);
3542 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3543 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3544}
3545
zhihuang1c378ed2017-08-17 14:10:50 -07003546// Test that no media content will be added to the offer if using default
3547// RTCOfferAnswerOptions.
3548TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3549 RTCOfferAnswerOptions rtc_options;
3550
3551 std::unique_ptr<SessionDescriptionInterface> offer;
3552 CreatePeerConnection();
3553 offer = CreateOfferWithOptions(rtc_options);
3554 ASSERT_TRUE(offer);
3555 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3556 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3557}
3558
3559// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3560// ufrag/pwd will be the same in the new offer.
3561TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3562 RTCOfferAnswerOptions rtc_options;
3563 rtc_options.ice_restart = false;
3564 rtc_options.offer_to_receive_audio = 1;
3565
3566 std::unique_ptr<SessionDescriptionInterface> offer;
3567 CreatePeerConnection();
3568 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3569 auto ufrag1 = offer->description()
3570 ->GetTransportInfoByName(cricket::CN_AUDIO)
3571 ->description.ice_ufrag;
3572 auto pwd1 = offer->description()
3573 ->GetTransportInfoByName(cricket::CN_AUDIO)
3574 ->description.ice_pwd;
3575
3576 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3577 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3578 auto ufrag2 = offer->description()
3579 ->GetTransportInfoByName(cricket::CN_AUDIO)
3580 ->description.ice_ufrag;
3581 auto pwd2 = offer->description()
3582 ->GetTransportInfoByName(cricket::CN_AUDIO)
3583 ->description.ice_pwd;
3584
3585 // |ice_restart| is true, the ufrag/pwd should change.
3586 rtc_options.ice_restart = true;
3587 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3588 auto ufrag3 = offer->description()
3589 ->GetTransportInfoByName(cricket::CN_AUDIO)
3590 ->description.ice_ufrag;
3591 auto pwd3 = offer->description()
3592 ->GetTransportInfoByName(cricket::CN_AUDIO)
3593 ->description.ice_pwd;
3594
3595 EXPECT_EQ(ufrag1, ufrag2);
3596 EXPECT_EQ(pwd1, pwd2);
3597 EXPECT_NE(ufrag2, ufrag3);
3598 EXPECT_NE(pwd2, pwd3);
3599}
3600
3601// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3602// offer; if it is false, there won't be any bundle group in the offer.
3603TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3604 RTCOfferAnswerOptions rtc_options;
3605 rtc_options.offer_to_receive_audio = 1;
3606 rtc_options.offer_to_receive_video = 1;
3607
3608 std::unique_ptr<SessionDescriptionInterface> offer;
3609 CreatePeerConnection();
3610
3611 rtc_options.use_rtp_mux = true;
3612 offer = CreateOfferWithOptions(rtc_options);
3613 ASSERT_TRUE(offer);
3614 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3615 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3616 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3617
3618 rtc_options.use_rtp_mux = false;
3619 offer = CreateOfferWithOptions(rtc_options);
3620 ASSERT_TRUE(offer);
3621 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3622 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3623 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3624}
3625
zhihuang141aacb2017-08-29 13:23:53 -07003626// This test ensures OnRenegotiationNeeded is called when we add track with
3627// MediaStream -> AddTrack in the same way it is called when we add track with
3628// PeerConnection -> AddTrack.
3629// The test can be removed once addStream is rewritten in terms of addTrack
3630// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3631TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3632 CreatePeerConnectionWithoutDtls();
3633 rtc::scoped_refptr<MediaStreamInterface> stream(
3634 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3635 pc_->AddStream(stream);
3636 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3637 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3638 rtc::scoped_refptr<VideoTrackInterface> video_track(
3639 pc_factory_->CreateVideoTrack(
3640 "video_track", pc_factory_->CreateVideoSource(
3641 std::unique_ptr<cricket::VideoCapturer>(
3642 new cricket::FakeVideoCapturer()))));
3643 stream->AddTrack(audio_track);
3644 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3645 observer_.renegotiation_needed_ = false;
3646
3647 stream->AddTrack(video_track);
3648 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3649 observer_.renegotiation_needed_ = false;
3650
3651 stream->RemoveTrack(audio_track);
3652 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3653 observer_.renegotiation_needed_ = false;
3654
3655 stream->RemoveTrack(video_track);
3656 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3657 observer_.renegotiation_needed_ = false;
3658}
3659
Zhi Huangb2d355e2017-10-26 17:26:37 -07003660// Tests that an error is returned if a description is applied that has fewer
3661// media sections than the existing description.
3662TEST_F(PeerConnectionInterfaceTest,
3663 MediaSectionCountEnforcedForSubsequentOffer) {
3664 CreatePeerConnection();
3665 FakeConstraints constraints;
3666 constraints.SetMandatoryReceiveAudio(true);
3667 constraints.SetMandatoryReceiveVideo(true);
3668 std::unique_ptr<SessionDescriptionInterface> offer;
3669 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3670 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3671
3672 // A remote offer with fewer media sections should be rejected.
3673 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3674 offer->description()->contents().pop_back();
3675 offer->description()->contents().pop_back();
3676 ASSERT_TRUE(offer->description()->contents().empty());
3677 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3678
3679 std::unique_ptr<SessionDescriptionInterface> answer;
3680 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3681 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3682
3683 // A subsequent local offer with fewer media sections should be rejected.
3684 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3685 offer->description()->contents().pop_back();
3686 offer->description()->contents().pop_back();
3687 ASSERT_TRUE(offer->description()->contents().empty());
3688 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3689}
3690
nisse51542be2016-02-12 02:27:06 -08003691class PeerConnectionMediaConfigTest : public testing::Test {
3692 protected:
3693 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003694 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003695 pcf_->Initialize();
3696 }
nisseeaabdf62017-05-05 02:23:02 -07003697 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003698 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003699 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003700 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3701 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003702 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003703 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003704 }
3705
zhihuang9763d562016-08-05 11:14:50 -07003706 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003707 MockPeerConnectionObserver observer_;
3708};
3709
3710// This test verifies the default behaviour with no constraints and a
3711// default RTCConfiguration.
3712TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3713 PeerConnectionInterface::RTCConfiguration config;
3714 FakeConstraints constraints;
3715
3716 const cricket::MediaConfig& media_config =
3717 TestCreatePeerConnection(config, &constraints);
3718
3719 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003720 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3721 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003722 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003723 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003724}
3725
3726// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003727// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003728TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3729 PeerConnectionInterface::RTCConfiguration config;
3730 FakeConstraints constraints;
3731
3732 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3733 const cricket::MediaConfig& media_config =
3734 TestCreatePeerConnection(config, &constraints);
3735
3736 EXPECT_TRUE(media_config.enable_dscp);
3737}
3738
3739// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003740// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003741TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3742 PeerConnectionInterface::RTCConfiguration config;
3743 FakeConstraints constraints;
3744
3745 constraints.AddOptional(
3746 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3747 const cricket::MediaConfig media_config =
3748 TestCreatePeerConnection(config, &constraints);
3749
Niels Möller1d7ecd22018-01-18 15:25:12 +01003750 EXPECT_FALSE(media_config.video.enable_cpu_adaptation);
nisse51542be2016-02-12 02:27:06 -08003751}
3752
Niels Möller1d7ecd22018-01-18 15:25:12 +01003753// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003754// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003755TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3756 PeerConnectionInterface::RTCConfiguration config;
3757 FakeConstraints constraints;
3758
Niels Möller71bdda02016-03-31 12:59:59 +02003759 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003760 const cricket::MediaConfig& media_config =
3761 TestCreatePeerConnection(config, &constraints);
3762
Niels Möller1d7ecd22018-01-18 15:25:12 +01003763 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003764}
3765
Niels Möller6539f692018-01-18 08:58:50 +01003766// This test verifies that the experiment_cpu_load_estimator flag is
3767// propagated from RTCConfiguration to the PeerConnection.
3768TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3769 PeerConnectionInterface::RTCConfiguration config;
3770 FakeConstraints constraints;
3771
3772 config.set_experiment_cpu_load_estimator(true);
3773 const cricket::MediaConfig& media_config =
3774 TestCreatePeerConnection(config, &constraints);
3775
3776 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3777}
3778
nisse0db023a2016-03-01 04:29:59 -08003779// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003780// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003781TEST_F(PeerConnectionMediaConfigTest,
3782 TestSuspendBelowMinBitrateConstraintTrue) {
3783 PeerConnectionInterface::RTCConfiguration config;
3784 FakeConstraints constraints;
3785
3786 constraints.AddOptional(
3787 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3788 true);
3789 const cricket::MediaConfig media_config =
3790 TestCreatePeerConnection(config, &constraints);
3791
3792 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003793}
3794
deadbeef293e9262017-01-11 12:28:30 -08003795// Tests a few random fields being different.
3796TEST(RTCConfigurationTest, ComparisonOperators) {
3797 PeerConnectionInterface::RTCConfiguration a;
3798 PeerConnectionInterface::RTCConfiguration b;
3799 EXPECT_EQ(a, b);
3800
3801 PeerConnectionInterface::RTCConfiguration c;
3802 c.servers.push_back(PeerConnectionInterface::IceServer());
3803 EXPECT_NE(a, c);
3804
3805 PeerConnectionInterface::RTCConfiguration d;
3806 d.type = PeerConnectionInterface::kRelay;
3807 EXPECT_NE(a, d);
3808
3809 PeerConnectionInterface::RTCConfiguration e;
3810 e.audio_jitter_buffer_max_packets = 5;
3811 EXPECT_NE(a, e);
3812
3813 PeerConnectionInterface::RTCConfiguration f;
3814 f.ice_connection_receiving_timeout = 1337;
3815 EXPECT_NE(a, f);
3816
3817 PeerConnectionInterface::RTCConfiguration g;
3818 g.disable_ipv6 = true;
3819 EXPECT_NE(a, g);
3820
3821 PeerConnectionInterface::RTCConfiguration h(
3822 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3823 EXPECT_NE(a, h);
3824}