blob: 46ac29076e23017e64d57d2e657575cef98691a1 [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"
323 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
324 "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,
584 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700585 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700586 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700587 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700588 return transport_controller;
589 }
590
deadbeefd7850b22017-08-23 10:59:19 -0700591 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700592 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700593};
594
Steve Anton94286cb2017-09-26 16:20:19 -0700595// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
596// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597class PeerConnectionInterfaceTest : public testing::Test {
598 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700599 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700600 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800601#ifdef WEBRTC_ANDROID
602 webrtc::InitializeAndroidObjects();
603#endif
604 }
605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700607 // Use fake audio capture module since we're only testing the interface
608 // level, and using a real one could make tests flaky when run in parallel.
609 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700611 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200612 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
613 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700614 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700615 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700616 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700617 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623
deadbeef293e9262017-01-11 12:28:30 -0800624 // DTLS does not work in a loopback call, so is disabled for most of the
625 // tests in this file.
626 void CreatePeerConnectionWithoutDtls() {
627 FakeConstraints no_dtls_constraints;
628 no_dtls_constraints.AddMandatory(
629 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
630
631 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
632 &no_dtls_constraints);
633 }
634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700636 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
637 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 }
639
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700640 void CreatePeerConnectionWithIceTransportsType(
641 PeerConnectionInterface::IceTransportsType type) {
642 PeerConnectionInterface::RTCConfiguration config;
643 config.type = type;
644 return CreatePeerConnection(config, nullptr);
645 }
646
647 void CreatePeerConnectionWithIceServer(const std::string& uri,
648 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800649 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700651 server.uri = uri;
652 server.password = password;
653 config.servers.push_back(server);
654 CreatePeerConnection(config, nullptr);
655 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100657 void CreatePeerConnection(
658 const PeerConnectionInterface::RTCConfiguration& config,
659 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700660 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800661 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
662 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000663
deadbeef1dcb1642017-03-29 21:08:16 -0700664 // Create certificate generator unless DTLS constraint is explicitly set to
665 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200666 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000667 bool dtls;
668 if (FindConstraint(constraints,
669 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
670 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200671 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800672 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
673 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000674 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200675 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800676 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200677 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 ASSERT_TRUE(pc_.get() != NULL);
679 observer_.SetPeerConnectionInterface(pc_.get());
680 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
681 }
682
deadbeef0a6c4ca2015-10-06 11:38:28 -0700683 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800684 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700685 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700686 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800687 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700688
zhihuang9763d562016-08-05 11:14:50 -0700689 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700690 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
691 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700693 }
694
Steve Anton038834f2017-07-14 15:59:59 -0700695 void CreatePeerConnectionExpectFail(
696 PeerConnectionInterface::RTCConfiguration config) {
697 PeerConnectionInterface::IceServer server;
698 server.uri = kTurnIceServerUri;
699 server.password = kTurnPassword;
700 config.servers.push_back(server);
701 rtc::scoped_refptr<PeerConnectionInterface> pc =
702 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
703 EXPECT_EQ(nullptr, pc);
704 }
705
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700707 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
709 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
710 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800712 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713
deadbeef0a6c4ca2015-10-06 11:38:28 -0700714 CreatePeerConnectionExpectFail(kStunInvalidPort);
715 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
716 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
720 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800722 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 }
728
729 void ReleasePeerConnection() {
730 pc_ = NULL;
731 observer_.SetPeerConnectionInterface(NULL);
732 }
733
deadbeefab9b2d12015-10-14 11:33:11 -0700734 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700736 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700738 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800739 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
740 new cricket::FakeVideoCapturer()),
741 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700742 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 pc_factory_->CreateVideoTrack(label + "v0", video_source));
744 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000745 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
747 observer_.renegotiation_needed_ = false;
748 }
749
750 void AddVoiceStream(const std::string& label) {
751 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700752 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700754 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 pc_factory_->CreateAudioTrack(label + "a0", NULL));
756 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000757 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
759 observer_.renegotiation_needed_ = false;
760 }
761
762 void AddAudioVideoStream(const std::string& stream_label,
763 const std::string& audio_track_label,
764 const std::string& video_track_label) {
765 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700766 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700768 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 pc_factory_->CreateAudioTrack(
770 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
771 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700773 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800774 video_track_label, pc_factory_->CreateVideoSource(
775 std::unique_ptr<cricket::VideoCapturer>(
776 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000778 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
780 observer_.renegotiation_needed_ = false;
781 }
782
kwibergd1fe2812016-04-27 06:47:29 -0700783 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700784 bool offer,
785 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000786 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
787 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 MockCreateSessionDescriptionObserver>());
789 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700790 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700792 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700795 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return observer->result();
797 }
798
kwibergd1fe2812016-04-27 06:47:29 -0700799 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700800 MediaConstraintsInterface* constraints) {
801 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
kwibergd1fe2812016-04-27 06:47:29 -0700804 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700805 MediaConstraintsInterface* constraints) {
806 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 }
808
Steve Antondb45ca82017-09-11 18:27:34 -0700809 bool DoSetSessionDescription(
810 std::unique_ptr<SessionDescriptionInterface> desc,
811 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000812 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
813 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 MockSetSessionDescriptionObserver>());
815 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700816 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700818 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
zhihuang29ff8442016-07-27 11:07:25 -0700820 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
821 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
822 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 return observer->result();
824 }
825
Steve Antondb45ca82017-09-11 18:27:34 -0700826 bool DoSetLocalDescription(
827 std::unique_ptr<SessionDescriptionInterface> desc) {
828 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 }
830
Steve Antondb45ca82017-09-11 18:27:34 -0700831 bool DoSetRemoteDescription(
832 std::unique_ptr<SessionDescriptionInterface> desc) {
833 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 }
835
836 // Calls PeerConnection::GetStats and check the return value.
837 // It does not verify the values in the StatReports since a RTCP packet might
838 // be required.
839 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000840 rtc::scoped_refptr<MockStatsObserver> observer(
841 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000842 if (!pc_->GetStats(
843 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return false;
845 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
846 return observer->called();
847 }
848
Harald Alvestrand89061872018-01-02 14:08:34 +0100849 // Call the standards-compliant GetStats function.
850 bool DoGetRTCStats() {
851 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
852 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
853 pc_->GetStats(callback);
854 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
855 return callback->called();
856 }
857
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800859 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 // Create a local stream with audio&video tracks.
861 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
862 CreateOfferReceiveAnswer();
863 }
864
865 // Verify that RTP Header extensions has been negotiated for audio and video.
866 void VerifyRemoteRtpHeaderExtensions() {
867 const cricket::MediaContentDescription* desc =
868 cricket::GetFirstAudioContentDescription(
869 pc_->remote_description()->description());
870 ASSERT_TRUE(desc != NULL);
871 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
872
873 desc = cricket::GetFirstVideoContentDescription(
874 pc_->remote_description()->description());
875 ASSERT_TRUE(desc != NULL);
876 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
877 }
878
879 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700880 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700881 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 std::string sdp;
883 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700884 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800885 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700886 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
888 }
889
deadbeefab9b2d12015-10-14 11:33:11 -0700890 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700891 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800892 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700893 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700894 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
895 }
896
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700898 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700899 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900
901 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
902 // audio codec change, even if the parameter has nothing to do with
903 // receiving. Not all parameters are serialized to SDP.
904 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
905 // the SessionDescription, it is necessary to do that here to in order to
906 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
907 // https://code.google.com/p/webrtc/issues/detail?id=1356
908 std::string sdp;
909 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700910 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800911 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700912 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
914 }
915
916 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700917 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700918 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919
920 std::string sdp;
921 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700922 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800923 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700924 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
926 }
927
928 void CreateOfferReceiveAnswer() {
929 CreateOfferAsLocalDescription();
930 std::string sdp;
931 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
932 CreateAnswerAsRemoteDescription(sdp);
933 }
934
935 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700936 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700937 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
939 // audio codec change, even if the parameter has nothing to do with
940 // receiving. Not all parameters are serialized to SDP.
941 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
942 // the SessionDescription, it is necessary to do that here to in order to
943 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
944 // https://code.google.com/p/webrtc/issues/detail?id=1356
945 std::string sdp;
946 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700947 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800948 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949
Steve Antondb45ca82017-09-11 18:27:34 -0700950 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000952 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700953 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 }
955
deadbeefab9b2d12015-10-14 11:33:11 -0700956 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700957 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800958 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700959 ASSERT_TRUE(answer);
960 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
962 }
963
deadbeefab9b2d12015-10-14 11:33:11 -0700964 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700965 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800966 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700967 ASSERT_TRUE(pr_answer);
968 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700970 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800971 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700972 ASSERT_TRUE(answer);
973 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
975 }
976
977 // Help function used for waiting until a the last signaled remote stream has
978 // the same label as |stream_label|. In a few of the tests in this file we
979 // answer with the same session description as we offer and thus we can
980 // check if OnAddStream have been called with the same stream as we offer to
981 // send.
982 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
983 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
984 }
985
986 // Creates an offer and applies it as a local session description.
987 // Creates an answer with the same SDP an the offer but removes all lines
988 // that start with a:ssrc"
989 void CreateOfferReceiveAnswerWithoutSsrc() {
990 CreateOfferAsLocalDescription();
991 std::string sdp;
992 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
993 SetSsrcToZero(&sdp);
994 CreateAnswerAsRemoteDescription(sdp);
995 }
996
deadbeefab9b2d12015-10-14 11:33:11 -0700997 // This function creates a MediaStream with label kStreams[0] and
998 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
999 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001000 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001001 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001002 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001003 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1004 size_t number_of_video_tracks) {
1005 EXPECT_LE(number_of_audio_tracks, 2u);
1006 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001007
1008 reference_collection_ = StreamCollection::Create();
1009 std::string sdp_ms1 = std::string(kSdpStringInit);
1010
1011 std::string mediastream_label = kStreams[0];
1012
1013 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1014 webrtc::MediaStream::Create(mediastream_label));
1015 reference_collection_->AddStream(stream);
1016
1017 if (number_of_audio_tracks > 0) {
1018 sdp_ms1 += std::string(kSdpStringAudio);
1019 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1020 AddAudioTrack(kAudioTracks[0], stream);
1021 }
1022 if (number_of_audio_tracks > 1) {
1023 sdp_ms1 += kSdpStringMs1Audio1;
1024 AddAudioTrack(kAudioTracks[1], stream);
1025 }
1026
1027 if (number_of_video_tracks > 0) {
1028 sdp_ms1 += std::string(kSdpStringVideo);
1029 sdp_ms1 += std::string(kSdpStringMs1Video0);
1030 AddVideoTrack(kVideoTracks[0], stream);
1031 }
1032 if (number_of_video_tracks > 1) {
1033 sdp_ms1 += kSdpStringMs1Video1;
1034 AddVideoTrack(kVideoTracks[1], stream);
1035 }
1036
kwibergd1fe2812016-04-27 06:47:29 -07001037 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001038 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001039 }
1040
1041 void AddAudioTrack(const std::string& track_id,
1042 MediaStreamInterface* stream) {
1043 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1044 webrtc::AudioTrack::Create(track_id, nullptr));
1045 ASSERT_TRUE(stream->AddTrack(audio_track));
1046 }
1047
1048 void AddVideoTrack(const std::string& track_id,
1049 MediaStreamInterface* stream) {
1050 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001051 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001052 webrtc::FakeVideoTrackSource::Create(),
1053 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001054 ASSERT_TRUE(stream->AddTrack(video_track));
1055 }
1056
kwibergfd8be342016-05-14 19:44:11 -07001057 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001058 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001059 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001060 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001061 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1062 return offer;
1063 }
1064
kwibergfd8be342016-05-14 19:44:11 -07001065 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001066 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001067 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001068 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001069 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1070 return answer;
1071 }
1072
1073 const std::string& GetFirstAudioStreamCname(
1074 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001075 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001076 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001077 return audio_desc->streams()[0].cname;
1078 }
1079
zhihuang1c378ed2017-08-17 14:10:50 -07001080 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1081 const RTCOfferAnswerOptions& offer_answer_options) {
1082 RTC_DCHECK(pc_);
1083 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1084 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1085 pc_->CreateOffer(observer, offer_answer_options);
1086 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1087 return observer->MoveDescription();
1088 }
1089
1090 void CreateOfferWithOptionsAsRemoteDescription(
1091 std::unique_ptr<SessionDescriptionInterface>* desc,
1092 const RTCOfferAnswerOptions& offer_answer_options) {
1093 *desc = CreateOfferWithOptions(offer_answer_options);
1094 ASSERT_TRUE(desc != nullptr);
1095 std::string sdp;
1096 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001097 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001098 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001099 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001100 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1101 }
1102
1103 void CreateOfferWithOptionsAsLocalDescription(
1104 std::unique_ptr<SessionDescriptionInterface>* desc,
1105 const RTCOfferAnswerOptions& offer_answer_options) {
1106 *desc = CreateOfferWithOptions(offer_answer_options);
1107 ASSERT_TRUE(desc != nullptr);
1108 std::string sdp;
1109 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001110 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001111 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001112
Steve Antondb45ca82017-09-11 18:27:34 -07001113 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001114 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1115 }
1116
1117 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001118 RTC_DCHECK(content);
1119 RTC_DCHECK(content->media_description());
1120 for (const cricket::AudioCodec& codec :
1121 content->media_description()->as_audio()->codecs()) {
1122 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001123 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001124 }
zhihuang1c378ed2017-08-17 14:10:50 -07001125 }
1126 return false;
1127 }
1128
deadbeef9a6f4d42017-05-15 19:43:33 -07001129 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1130 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001131 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001132 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001133 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001134 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1135 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1136 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001138 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139};
1140
zhihuang29ff8442016-07-27 11:07:25 -07001141// Test that no callbacks on the PeerConnectionObserver are called after the
1142// PeerConnection is closed.
1143TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001144 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001145 pc_factory_for_test_->CreatePeerConnection(
1146 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1147 nullptr, &observer_));
1148 observer_.SetPeerConnectionInterface(pc.get());
1149 pc->Close();
1150
1151 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001152 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001153 std::vector<cricket::Candidate> candidates;
1154 pc_factory_for_test_->transport_controller->SignalGatheringState(
1155 cricket::IceGatheringState{});
1156 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1157 "", candidates);
1158 pc_factory_for_test_->transport_controller->SignalConnectionState(
1159 cricket::IceConnectionState{});
1160 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1161 candidates);
1162 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001163 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001164}
1165
zhihuang8f65cdf2016-05-06 18:40:30 -07001166// Generate different CNAMEs when PeerConnections are created.
1167// The CNAMEs are expected to be generated randomly. It is possible
1168// that the test fails, though the possibility is very low.
1169TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 CreateOfferWithOneAudioStream();
1174 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1175 GetFirstAudioStreamCname(offer2.get()));
1176}
1177
1178TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 CreateAnswerWithOneAudioStream();
1183 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1184 GetFirstAudioStreamCname(answer2.get()));
1185}
1186
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187TEST_F(PeerConnectionInterfaceTest,
1188 CreatePeerConnectionWithDifferentConfigurations) {
1189 CreatePeerConnectionWithDifferentConfigurations();
1190}
1191
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001192TEST_F(PeerConnectionInterfaceTest,
1193 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1194 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1195 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1197 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1199 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1200 port_allocator_->candidate_filter());
1201 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1202 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1203}
1204
1205// Test that when a PeerConnection is created with a nonzero candidate pool
1206// size, the pooled PortAllocatorSession is created with all the attributes
1207// in the RTCConfiguration.
1208TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1209 PeerConnectionInterface::RTCConfiguration config;
1210 PeerConnectionInterface::IceServer server;
1211 server.uri = kStunAddressOnly;
1212 config.servers.push_back(server);
1213 config.type = PeerConnectionInterface::kRelay;
1214 config.disable_ipv6 = true;
1215 config.tcp_candidate_policy =
1216 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001217 config.candidate_network_policy =
1218 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001219 config.ice_candidate_pool_size = 1;
1220 CreatePeerConnection(config, nullptr);
1221
1222 const cricket::FakePortAllocatorSession* session =
1223 static_cast<const cricket::FakePortAllocatorSession*>(
1224 port_allocator_->GetPooledSession());
1225 ASSERT_NE(nullptr, session);
1226 EXPECT_EQ(1UL, session->stun_servers().size());
1227 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1228 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001229 EXPECT_LT(0U,
1230 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001231}
1232
deadbeefd21eab32017-07-26 16:50:11 -07001233// Test that network-related RTCConfiguration members are applied to the
1234// PortAllocator when CreatePeerConnection is called. Specifically:
1235// - disable_ipv6_on_wifi
1236// - max_ipv6_networks
1237// - tcp_candidate_policy
1238// - candidate_network_policy
1239// - prune_turn_ports
1240//
1241// Note that the candidate filter (RTCConfiguration::type) is already tested
1242// above.
1243TEST_F(PeerConnectionInterfaceTest,
1244 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1245 // Create fake port allocator.
1246 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1247 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1248 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1249
1250 // Create RTCConfiguration with some network-related fields relevant to
1251 // PortAllocator populated.
1252 PeerConnectionInterface::RTCConfiguration config;
1253 config.disable_ipv6_on_wifi = true;
1254 config.max_ipv6_networks = 10;
1255 config.tcp_candidate_policy =
1256 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1257 config.candidate_network_policy =
1258 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1259 config.prune_turn_ports = true;
1260
1261 // Create the PC factory and PC with the above config.
1262 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1263 webrtc::CreatePeerConnectionFactory(
1264 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001265 rtc::Thread::Current(), fake_audio_capture_module_,
1266 webrtc::CreateBuiltinAudioEncoderFactory(),
1267 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001268 rtc::scoped_refptr<PeerConnectionInterface> pc(
1269 pc_factory->CreatePeerConnection(
1270 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1271
1272 // Now validate that the config fields set above were applied to the
1273 // PortAllocator, as flags or otherwise.
1274 EXPECT_FALSE(raw_port_allocator->flags() &
1275 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1276 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1277 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1278 EXPECT_TRUE(raw_port_allocator->flags() &
1279 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1280 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1281}
1282
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001283// Test that the PeerConnection initializes the port allocator passed into it,
1284// and on the correct thread.
1285TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001286 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001287 std::unique_ptr<rtc::Thread> network_thread(
1288 rtc::Thread::CreateWithSocketServer());
1289 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001290 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1291 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001292 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001293 fake_audio_capture_module_,
1294 webrtc::CreateBuiltinAudioEncoderFactory(),
1295 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001296 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001297 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001298 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1299 PeerConnectionInterface::RTCConfiguration config;
1300 rtc::scoped_refptr<PeerConnectionInterface> pc(
1301 pc_factory->CreatePeerConnection(
1302 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1303 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1304 // so all we have to do here is check that it's initialized.
1305 EXPECT_TRUE(raw_port_allocator->initialized());
1306}
1307
deadbeef46c73892016-11-16 19:42:04 -08001308// Check that GetConfiguration returns the configuration the PeerConnection was
1309// constructed with, before SetConfiguration is called.
1310TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1311 PeerConnectionInterface::RTCConfiguration config;
1312 config.type = PeerConnectionInterface::kRelay;
1313 CreatePeerConnection(config, nullptr);
1314
1315 PeerConnectionInterface::RTCConfiguration returned_config =
1316 pc_->GetConfiguration();
1317 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1318}
1319
1320// Check that GetConfiguration returns the last configuration passed into
1321// SetConfiguration.
1322TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1323 CreatePeerConnection();
1324
1325 PeerConnectionInterface::RTCConfiguration config;
1326 config.type = PeerConnectionInterface::kRelay;
1327 EXPECT_TRUE(pc_->SetConfiguration(config));
1328
1329 PeerConnectionInterface::RTCConfiguration returned_config =
1330 pc_->GetConfiguration();
1331 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1332}
1333
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001335 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001336 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 AddVoiceStream(kStreamLabel2);
1338 ASSERT_EQ(2u, pc_->local_streams()->count());
1339
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001340 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001341 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001343 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1344 pc_factory_->CreateAudioTrack(kStreamLabel3,
1345 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001347 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001348 EXPECT_EQ(3u, pc_->local_streams()->count());
1349
1350 // Remove the third stream.
1351 pc_->RemoveStream(pc_->local_streams()->at(2));
1352 EXPECT_EQ(2u, pc_->local_streams()->count());
1353
1354 // Remove the second stream.
1355 pc_->RemoveStream(pc_->local_streams()->at(1));
1356 EXPECT_EQ(1u, pc_->local_streams()->count());
1357
1358 // Remove the first stream.
1359 pc_->RemoveStream(pc_->local_streams()->at(0));
1360 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361}
1362
deadbeefab9b2d12015-10-14 11:33:11 -07001363// Test that the created offer includes streams we added.
1364TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001365 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001366 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001367 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001368 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001369
deadbeefab9b2d12015-10-14 11:33:11 -07001370 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001371 cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001372 EXPECT_TRUE(
1373 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1374
deadbeefab9b2d12015-10-14 11:33:11 -07001375 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001376 cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001377 EXPECT_TRUE(
1378 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1379
1380 // Add another stream and ensure the offer includes both the old and new
1381 // streams.
1382 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001383 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001384
Steve Antonb1c1de12017-12-21 15:14:30 -08001385 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001386 EXPECT_TRUE(
1387 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1388 EXPECT_TRUE(
1389 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1390
Steve Antonb1c1de12017-12-21 15:14:30 -08001391 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001392 EXPECT_TRUE(
1393 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1394 EXPECT_TRUE(
1395 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1396}
1397
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001399 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001400 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 ASSERT_EQ(1u, pc_->local_streams()->count());
1402 pc_->RemoveStream(pc_->local_streams()->at(0));
1403 EXPECT_EQ(0u, pc_->local_streams()->count());
1404}
1405
deadbeefe1f9d832016-01-14 15:35:42 -08001406// Test for AddTrack and RemoveTrack methods.
1407// Tests that the created offer includes tracks we added,
1408// and that the RtpSenders are created correctly.
1409// Also tests that RemoveTrack removes the tracks from subsequent offers.
1410TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001411 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001412 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001413 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001414 rtc::scoped_refptr<VideoTrackInterface> video_track(
1415 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001416 "video_track", pc_factory_->CreateVideoSource(
1417 std::unique_ptr<cricket::VideoCapturer>(
1418 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001419 auto audio_sender = pc_->AddTrack(audio_track, {kStreamLabel1}).MoveValue();
1420 auto video_sender = pc_->AddTrack(video_track, {kStreamLabel1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001421 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1422 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001423 EXPECT_EQ("audio_track", audio_sender->id());
1424 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001425 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1426 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001427 EXPECT_EQ("video_track", video_sender->id());
1428 EXPECT_EQ(video_track, video_sender->track());
1429
1430 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001431 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001432 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001433
1434 const cricket::ContentInfo* audio_content =
1435 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001436 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1437 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001438
1439 const cricket::ContentInfo* video_content =
1440 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001441 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1442 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001443
Steve Antondb45ca82017-09-11 18:27:34 -07001444 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001445
1446 // Now try removing the tracks.
1447 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1448 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1449
1450 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001451 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001452
1453 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001454 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1455 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001456
1457 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001458 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1459 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001460
Steve Antondb45ca82017-09-11 18:27:34 -07001461 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001462
1463 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1464 // should return false.
1465 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1466 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1467}
1468
1469// Test creating senders without a stream specified,
1470// expecting a random stream ID to be generated.
1471TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001472 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001473 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001474 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001475 rtc::scoped_refptr<VideoTrackInterface> video_track(
1476 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001477 "video_track", pc_factory_->CreateVideoSource(
1478 std::unique_ptr<cricket::VideoCapturer>(
1479 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001480 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001481 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001482 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001483 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001484 EXPECT_EQ("audio_track", audio_sender->id());
1485 EXPECT_EQ(audio_track, audio_sender->track());
1486 EXPECT_EQ("video_track", video_sender->id());
1487 EXPECT_EQ(video_track, video_sender->track());
1488 // If the ID is truly a random GUID, it should be infinitely unlikely they
1489 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001490 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001491}
1492
Harald Alvestrand89061872018-01-02 14:08:34 +01001493// Test that we can call GetStats() after AddTrack but before connecting
1494// the PeerConnection to a peer.
1495TEST_F(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
1496 CreatePeerConnectionWithoutDtls();
1497 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1498 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1499 rtc::scoped_refptr<VideoTrackInterface> video_track(
1500 pc_factory_->CreateVideoTrack(
1501 "video_track", pc_factory_->CreateVideoSource(
1502 std::unique_ptr<cricket::VideoCapturer>(
1503 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001504 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1505 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001506 EXPECT_TRUE(DoGetStats(nullptr));
1507}
1508
Harald Alvestrandc72af932018-01-11 17:18:19 +01001509TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
1510 CreatePeerConnectionWithoutDtls();
1511 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1512 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1513 rtc::scoped_refptr<VideoTrackInterface> video_track(
1514 pc_factory_->CreateVideoTrack(
1515 "video_track", pc_factory_->CreateVideoSource(
1516 std::unique_ptr<cricket::VideoCapturer>(
1517 new cricket::FakeVideoCapturer()))));
1518 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1519 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
1520 EXPECT_TRUE(audio_sender.ok());
1521 EXPECT_TRUE(video_sender.ok());
1522 EXPECT_NE(0, video_sender.value()->AttachmentId());
1523 EXPECT_NE(0, audio_sender.value()->AttachmentId());
1524}
1525
1526TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddStream) {
1527 CreatePeerConnectionWithoutDtls();
1528 AddVideoStream(kStreamLabel1);
1529 auto senders = pc_->GetSenders();
1530 EXPECT_EQ(1u, senders.size());
1531 EXPECT_NE(0, senders[0]->AttachmentId());
1532}
1533
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1535 InitiateCall();
1536 WaitAndVerifyOnAddStream(kStreamLabel1);
1537 VerifyRemoteRtpHeaderExtensions();
1538}
1539
1540TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001541 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001542 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 CreateOfferAsLocalDescription();
1544 std::string offer;
1545 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1546 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1547 WaitAndVerifyOnAddStream(kStreamLabel1);
1548}
1549
1550TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001551 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001552 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553
1554 CreateOfferAsRemoteDescription();
1555 CreateAnswerAsLocalDescription();
1556
1557 WaitAndVerifyOnAddStream(kStreamLabel1);
1558}
1559
1560TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001561 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001562 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563
1564 CreateOfferAsRemoteDescription();
1565 CreatePrAnswerAsLocalDescription();
1566 CreateAnswerAsLocalDescription();
1567
1568 WaitAndVerifyOnAddStream(kStreamLabel1);
1569}
1570
1571TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1572 InitiateCall();
1573 ASSERT_EQ(1u, pc_->remote_streams()->count());
1574 pc_->RemoveStream(pc_->local_streams()->at(0));
1575 CreateOfferReceiveAnswer();
1576 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001577 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 CreateOfferReceiveAnswer();
1579}
1580
1581// Tests that after negotiating an audio only call, the respondent can perform a
1582// renegotiation that removes the audio stream.
1583TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001584 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 AddVoiceStream(kStreamLabel1);
1586 CreateOfferAsRemoteDescription();
1587 CreateAnswerAsLocalDescription();
1588
1589 ASSERT_EQ(1u, pc_->remote_streams()->count());
1590 pc_->RemoveStream(pc_->local_streams()->at(0));
1591 CreateOfferReceiveAnswer();
1592 EXPECT_EQ(0u, pc_->remote_streams()->count());
1593}
1594
1595// Test that candidates are generated and that we can parse our own candidates.
1596TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001597 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598
Steve Antonf1c6db12017-10-13 11:13:35 -07001599 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001601 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001602 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001603 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001604 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605
1606 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001607 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001608 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001609 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610
Steve Antonf1c6db12017-10-13 11:13:35 -07001611 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001612 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613
Steve Antonf1c6db12017-10-13 11:13:35 -07001614 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615}
1616
deadbeefab9b2d12015-10-14 11:33:11 -07001617// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618// not unique.
1619TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001620 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001622 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001623 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001624 EXPECT_TRUE(offer);
1625 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626
1627 // Create a local stream with audio&video tracks having same label.
1628 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1629
1630 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001631 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632
1633 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001634 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001635 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636}
1637
1638// Test that we will get different SSRCs for each tracks in the offer and answer
1639// we created.
1640TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001641 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 // Create a local stream with audio&video tracks having different labels.
1643 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1644
1645 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001646 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001647 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 int audio_ssrc = 0;
1649 int video_ssrc = 0;
1650 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1651 &audio_ssrc));
1652 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1653 &video_ssrc));
1654 EXPECT_NE(audio_ssrc, video_ssrc);
1655
1656 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001657 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001658 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001659 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 audio_ssrc = 0;
1661 video_ssrc = 0;
1662 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1663 &audio_ssrc));
1664 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1665 &video_ssrc));
1666 EXPECT_NE(audio_ssrc, video_ssrc);
1667}
1668
deadbeefeb459812015-12-15 19:24:43 -08001669// Test that it's possible to call AddTrack on a MediaStream after adding
1670// the stream to a PeerConnection.
1671// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1672TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001673 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001674 // Create audio stream and add to PeerConnection.
1675 AddVoiceStream(kStreamLabel1);
1676 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1677
1678 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001679 rtc::scoped_refptr<VideoTrackInterface> video_track(
1680 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001681 "video_label", pc_factory_->CreateVideoSource(
1682 std::unique_ptr<cricket::VideoCapturer>(
1683 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001684 stream->AddTrack(video_track.get());
1685
kwibergd1fe2812016-04-27 06:47:29 -07001686 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001687 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001688
1689 const cricket::MediaContentDescription* video_desc =
1690 cricket::GetFirstVideoContentDescription(offer->description());
1691 EXPECT_TRUE(video_desc != nullptr);
1692}
1693
1694// Test that it's possible to call RemoveTrack on a MediaStream after adding
1695// the stream to a PeerConnection.
1696// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1697TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001698 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001699 // Create audio/video stream and add to PeerConnection.
1700 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1701 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1702
1703 // Remove the video track.
1704 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1705
kwibergd1fe2812016-04-27 06:47:29 -07001706 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001707 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001708
1709 const cricket::MediaContentDescription* video_desc =
1710 cricket::GetFirstVideoContentDescription(offer->description());
1711 EXPECT_TRUE(video_desc == nullptr);
1712}
1713
deadbeef1dcb1642017-03-29 21:08:16 -07001714// Verify that CreateDtmfSender only succeeds if called with a valid local
1715// track. Other aspects of DtmfSenders are tested in
1716// peerconnection_integrationtest.cc.
1717TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1718 CreatePeerConnection();
1719 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1720 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1721 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1722 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1723 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1724}
1725
deadbeefbd7d8f72015-12-18 16:58:44 -08001726// Test creating a sender with a stream ID, and ensure the ID is populated
1727// in the offer.
1728TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001729 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001730 pc_->CreateSender("video", kStreamLabel1);
1731
kwibergd1fe2812016-04-27 06:47:29 -07001732 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001733 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001734
1735 const cricket::MediaContentDescription* video_desc =
1736 cricket::GetFirstVideoContentDescription(offer->description());
1737 ASSERT_TRUE(video_desc != nullptr);
1738 ASSERT_EQ(1u, video_desc->streams().size());
1739 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1740}
1741
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742// Test that we can specify a certain track that we want statistics about.
1743TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1744 InitiateCall();
1745 ASSERT_LT(0u, pc_->remote_streams()->count());
1746 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001747 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1749 EXPECT_TRUE(DoGetStats(remote_audio));
1750
1751 // Remove the stream. Since we are sending to our selves the local
1752 // and the remote stream is the same.
1753 pc_->RemoveStream(pc_->local_streams()->at(0));
1754 // Do a re-negotiation.
1755 CreateOfferReceiveAnswer();
1756
1757 ASSERT_EQ(0u, pc_->remote_streams()->count());
1758
1759 // Test that we still can get statistics for the old track. Even if it is not
1760 // sent any longer.
1761 EXPECT_TRUE(DoGetStats(remote_audio));
1762}
1763
1764// Test that we can get stats on a video track.
1765TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1766 InitiateCall();
1767 ASSERT_LT(0u, pc_->remote_streams()->count());
1768 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1771 EXPECT_TRUE(DoGetStats(remote_video));
1772}
1773
1774// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001775TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001777 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 pc_factory_->CreateAudioTrack("unknown track", NULL));
1779 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1780}
1781
Harald Alvestrand89061872018-01-02 14:08:34 +01001782TEST_F(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
1783 CreatePeerConnectionWithoutDtls();
1784 EXPECT_TRUE(DoGetRTCStats());
1785 // Clearing stats cache is needed now, but should be temporary.
1786 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1787 pc_->ClearStatsCache();
1788 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
1789 EXPECT_TRUE(DoGetRTCStats());
1790 pc_->ClearStatsCache();
1791 CreateOfferReceiveAnswer();
1792 EXPECT_TRUE(DoGetRTCStats());
1793}
1794
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 FakeConstraints constraints;
1798 constraints.SetAllowRtpDataChannels();
1799 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001800 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001802 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 pc_->CreateDataChannel("test2", NULL);
1804 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001805 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001807 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 new MockDataChannelObserver(data2));
1809
1810 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1811 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1812 std::string data_to_send1 = "testing testing";
1813 std::string data_to_send2 = "testing something else";
1814 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1815
1816 CreateOfferReceiveAnswer();
1817 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1818 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1819
1820 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1821 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1822 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1823 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1824
1825 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1826 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1827
1828 data1->Close();
1829 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1830 CreateOfferReceiveAnswer();
1831 EXPECT_FALSE(observer1->IsOpen());
1832 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1833 EXPECT_TRUE(observer2->IsOpen());
1834
1835 data_to_send2 = "testing something else again";
1836 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1837
1838 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1839}
1840
1841// This test verifies that sendnig binary data over RTP data channels should
1842// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 FakeConstraints constraints;
1845 constraints.SetAllowRtpDataChannels();
1846 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001847 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001849 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 pc_->CreateDataChannel("test2", NULL);
1851 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001852 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001854 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 new MockDataChannelObserver(data2));
1856
1857 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1858 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1859
1860 CreateOfferReceiveAnswer();
1861 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1862 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1863
1864 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1865 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1866
jbaucheec21bd2016-03-20 06:15:43 -07001867 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1869}
1870
1871// This test setup a RTP data channels in loop back and test that a channel is
1872// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 FakeConstraints constraints;
1875 constraints.SetAllowRtpDataChannels();
1876 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001879 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 new MockDataChannelObserver(data1));
1881
1882 CreateOfferReceiveAnswerWithoutSsrc();
1883
1884 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1885
1886 data1->Close();
1887 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1888 CreateOfferReceiveAnswerWithoutSsrc();
1889 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1890 EXPECT_FALSE(observer1->IsOpen());
1891}
1892
1893// This test that if a data channel is added in an answer a receive only channel
1894// channel is created.
1895TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1896 FakeConstraints constraints;
1897 constraints.SetAllowRtpDataChannels();
1898 CreatePeerConnection(&constraints);
1899
1900 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001901 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 pc_->CreateDataChannel(offer_label, NULL);
1903
1904 CreateOfferAsLocalDescription();
1905
1906 // Replace the data channel label in the offer and apply it as an answer.
1907 std::string receive_label = "answer_channel";
1908 std::string sdp;
1909 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001910 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 receive_label.c_str(), receive_label.length(),
1912 &sdp);
1913 CreateAnswerAsRemoteDescription(sdp);
1914
1915 // Verify that a new incoming data channel has been created and that
1916 // it is open but can't we written to.
1917 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1918 DataChannelInterface* received_channel = observer_.last_datachannel_;
1919 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1920 EXPECT_EQ(receive_label, received_channel->label());
1921 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1922
1923 // Verify that the channel we initially offered has been rejected.
1924 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1925
1926 // Do another offer / answer exchange and verify that the data channel is
1927 // opened.
1928 CreateOfferReceiveAnswer();
1929 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1930 kTimeout);
1931}
1932
1933// This test that no data channel is returned if a reliable channel is
1934// requested.
1935// TODO(perkj): Remove this test once reliable channels are implemented.
1936TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1937 FakeConstraints constraints;
1938 constraints.SetAllowRtpDataChannels();
1939 CreatePeerConnection(&constraints);
1940
1941 std::string label = "test";
1942 webrtc::DataChannelInit config;
1943 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001944 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 pc_->CreateDataChannel(label, &config);
1946 EXPECT_TRUE(channel == NULL);
1947}
1948
deadbeefab9b2d12015-10-14 11:33:11 -07001949// Verifies that duplicated label is not allowed for RTP data channel.
1950TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1951 FakeConstraints constraints;
1952 constraints.SetAllowRtpDataChannels();
1953 CreatePeerConnection(&constraints);
1954
1955 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001956 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001957 pc_->CreateDataChannel(label, nullptr);
1958 EXPECT_NE(channel, nullptr);
1959
zhihuang9763d562016-08-05 11:14:50 -07001960 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001961 pc_->CreateDataChannel(label, nullptr);
1962 EXPECT_EQ(dup_channel, nullptr);
1963}
1964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965// This tests that a SCTP data channel is returned using different
1966// DataChannelInit configurations.
1967TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1968 FakeConstraints constraints;
1969 constraints.SetAllowDtlsSctpDataChannels();
1970 CreatePeerConnection(&constraints);
1971
1972 webrtc::DataChannelInit config;
1973
zhihuang9763d562016-08-05 11:14:50 -07001974 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 pc_->CreateDataChannel("1", &config);
1976 EXPECT_TRUE(channel != NULL);
1977 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001978 EXPECT_TRUE(observer_.renegotiation_needed_);
1979 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980
1981 config.ordered = false;
1982 channel = pc_->CreateDataChannel("2", &config);
1983 EXPECT_TRUE(channel != NULL);
1984 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001985 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986
1987 config.ordered = true;
1988 config.maxRetransmits = 0;
1989 channel = pc_->CreateDataChannel("3", &config);
1990 EXPECT_TRUE(channel != NULL);
1991 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001992 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993
1994 config.maxRetransmits = -1;
1995 config.maxRetransmitTime = 0;
1996 channel = pc_->CreateDataChannel("4", &config);
1997 EXPECT_TRUE(channel != NULL);
1998 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001999 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000}
2001
2002// This tests that no data channel is returned if both maxRetransmits and
2003// maxRetransmitTime are set for SCTP data channels.
2004TEST_F(PeerConnectionInterfaceTest,
2005 CreateSctpDataChannelShouldFailForInvalidConfig) {
2006 FakeConstraints constraints;
2007 constraints.SetAllowDtlsSctpDataChannels();
2008 CreatePeerConnection(&constraints);
2009
2010 std::string label = "test";
2011 webrtc::DataChannelInit config;
2012 config.maxRetransmits = 0;
2013 config.maxRetransmitTime = 0;
2014
zhihuang9763d562016-08-05 11:14:50 -07002015 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 pc_->CreateDataChannel(label, &config);
2017 EXPECT_TRUE(channel == NULL);
2018}
2019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020// The test verifies that creating a SCTP data channel with an id already in use
2021// or out of range should fail.
2022TEST_F(PeerConnectionInterfaceTest,
2023 CreateSctpDataChannelWithInvalidIdShouldFail) {
2024 FakeConstraints constraints;
2025 constraints.SetAllowDtlsSctpDataChannels();
2026 CreatePeerConnection(&constraints);
2027
2028 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002029 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002031 config.id = 1;
2032 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 EXPECT_TRUE(channel != NULL);
2034 EXPECT_EQ(1, channel->id());
2035
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 channel = pc_->CreateDataChannel("x", &config);
2037 EXPECT_TRUE(channel == NULL);
2038
2039 config.id = cricket::kMaxSctpSid;
2040 channel = pc_->CreateDataChannel("max", &config);
2041 EXPECT_TRUE(channel != NULL);
2042 EXPECT_EQ(config.id, channel->id());
2043
2044 config.id = cricket::kMaxSctpSid + 1;
2045 channel = pc_->CreateDataChannel("x", &config);
2046 EXPECT_TRUE(channel == NULL);
2047}
2048
deadbeefab9b2d12015-10-14 11:33:11 -07002049// Verifies that duplicated label is allowed for SCTP data channel.
2050TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2051 FakeConstraints constraints;
2052 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2053 true);
2054 CreatePeerConnection(&constraints);
2055
2056 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002057 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002058 pc_->CreateDataChannel(label, nullptr);
2059 EXPECT_NE(channel, nullptr);
2060
zhihuang9763d562016-08-05 11:14:50 -07002061 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002062 pc_->CreateDataChannel(label, nullptr);
2063 EXPECT_NE(dup_channel, nullptr);
2064}
2065
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002066// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2067// DataChannel.
2068TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2069 FakeConstraints constraints;
2070 constraints.SetAllowRtpDataChannels();
2071 CreatePeerConnection(&constraints);
2072
zhihuang9763d562016-08-05 11:14:50 -07002073 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002074 pc_->CreateDataChannel("test1", NULL);
2075 EXPECT_TRUE(observer_.renegotiation_needed_);
2076 observer_.renegotiation_needed_ = false;
2077
zhihuang9763d562016-08-05 11:14:50 -07002078 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002079 pc_->CreateDataChannel("test2", NULL);
2080 EXPECT_TRUE(observer_.renegotiation_needed_);
2081}
2082
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 FakeConstraints constraints;
2086 constraints.SetAllowRtpDataChannels();
2087 CreatePeerConnection(&constraints);
2088
zhihuang9763d562016-08-05 11:14:50 -07002089 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002091 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 pc_->CreateDataChannel("test2", NULL);
2093 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002094 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002096 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 new MockDataChannelObserver(data2));
2098
2099 CreateOfferReceiveAnswer();
2100 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2101 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2102
2103 ReleasePeerConnection();
2104 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2105 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2106}
2107
2108// This test that data channels can be rejected in an answer.
2109TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2110 FakeConstraints constraints;
2111 constraints.SetAllowRtpDataChannels();
2112 CreatePeerConnection(&constraints);
2113
zhihuang9763d562016-08-05 11:14:50 -07002114 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 pc_->CreateDataChannel("offer_channel", NULL));
2116
2117 CreateOfferAsLocalDescription();
2118
2119 // Create an answer where the m-line for data channels are rejected.
2120 std::string sdp;
2121 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002122 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002123 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002124 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 cricket::ContentInfo* data_info =
2126 answer->description()->GetContentByName("data");
2127 data_info->rejected = true;
2128
Steve Antondb45ca82017-09-11 18:27:34 -07002129 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2131}
2132
2133// Test that we can create a session description from an SDP string from
2134// FireFox, use it as a remote session description, generate an answer and use
2135// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002136TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 FakeConstraints constraints;
2138 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2139 true);
2140 CreatePeerConnection(&constraints);
2141 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002142 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002143 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002144 webrtc::kFireFoxSdpOffer, nullptr));
2145 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 CreateAnswerAsLocalDescription();
2147 ASSERT_TRUE(pc_->local_description() != NULL);
2148 ASSERT_TRUE(pc_->remote_description() != NULL);
2149
2150 const cricket::ContentInfo* content =
2151 cricket::GetFirstAudioContent(pc_->local_description()->description());
2152 ASSERT_TRUE(content != NULL);
2153 EXPECT_FALSE(content->rejected);
2154
2155 content =
2156 cricket::GetFirstVideoContent(pc_->local_description()->description());
2157 ASSERT_TRUE(content != NULL);
2158 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002159#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 content =
2161 cricket::GetFirstDataContent(pc_->local_description()->description());
2162 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002163 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002165#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166}
2167
zhihuangb19012e2017-09-19 13:47:59 -07002168// Test that fallback from DTLS to SDES is not supported.
2169// The fallback was previously supported but was removed to simplify the code
2170// and because it's non-standard.
2171TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002172 FakeConstraints constraints;
2173 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2174 true);
2175 CreatePeerConnection(&constraints);
2176 // Wait for fake certificate to be generated. Previously, this is what caused
2177 // the "a=crypto" lines to be rejected.
2178 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2179 ASSERT_NE(nullptr, fake_certificate_generator_);
2180 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2181 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002182 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002183 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2184 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002185 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002186}
2187
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188// Test that we can create an audio only offer and receive an answer with a
2189// limited set of audio codecs and receive an updated offer with more audio
2190// codecs, where the added codecs are not supported.
2191TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002192 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 AddVoiceStream("audio_label");
2194 CreateOfferAsLocalDescription();
2195
Steve Antondb45ca82017-09-11 18:27:34 -07002196 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002197 webrtc::CreateSessionDescription(SdpType::kAnswer, webrtc::kAudioSdp,
2198 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002199 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200
Steve Antondb45ca82017-09-11 18:27:34 -07002201 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002202 webrtc::CreateSessionDescription(
2203 SdpType::kOffer, webrtc::kAudioSdpWithUnsupportedCodecs, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002204 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 CreateAnswerAsLocalDescription();
2206}
2207
deadbeefc80741f2015-10-22 13:14:45 -07002208// Test that if we're receiving (but not sending) a track, subsequent offers
2209// will have m-lines with a=recvonly.
2210TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2211 FakeConstraints constraints;
2212 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2213 true);
2214 CreatePeerConnection(&constraints);
2215 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2216 CreateAnswerAsLocalDescription();
2217
2218 // At this point we should be receiving stream 1, but not sending anything.
2219 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002220 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002221 DoCreateOffer(&offer, nullptr);
2222
2223 const cricket::ContentInfo* video_content =
2224 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002225 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2226 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002227
2228 const cricket::ContentInfo* audio_content =
2229 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002230 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2231 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002232}
2233
2234// Test that if we're receiving (but not sending) a track, and the
2235// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2236// false, the generated m-lines will be a=inactive.
2237TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2238 FakeConstraints constraints;
2239 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2240 true);
2241 CreatePeerConnection(&constraints);
2242 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2243 CreateAnswerAsLocalDescription();
2244
2245 // At this point we should be receiving stream 1, but not sending anything.
2246 // A new offer would be recvonly, but we'll set the "no receive" constraints
2247 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002248 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002249 FakeConstraints offer_constraints;
2250 offer_constraints.AddMandatory(
2251 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2252 offer_constraints.AddMandatory(
2253 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2254 DoCreateOffer(&offer, &offer_constraints);
2255
2256 const cricket::ContentInfo* video_content =
2257 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002258 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2259 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002260
2261 const cricket::ContentInfo* audio_content =
2262 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002263 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2264 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002265}
2266
deadbeef653b8e02015-11-11 12:55:10 -08002267// Test that we can use SetConfiguration to change the ICE servers of the
2268// PortAllocator.
2269TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2270 CreatePeerConnection();
2271
2272 PeerConnectionInterface::RTCConfiguration config;
2273 PeerConnectionInterface::IceServer server;
2274 server.uri = "stun:test_hostname";
2275 config.servers.push_back(server);
2276 EXPECT_TRUE(pc_->SetConfiguration(config));
2277
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002278 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2279 EXPECT_EQ("test_hostname",
2280 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002281}
2282
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002283TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2284 CreatePeerConnection();
2285 PeerConnectionInterface::RTCConfiguration config;
2286 config.type = PeerConnectionInterface::kRelay;
2287 EXPECT_TRUE(pc_->SetConfiguration(config));
2288 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2289}
2290
deadbeef293e9262017-01-11 12:28:30 -08002291TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2292 PeerConnectionInterface::RTCConfiguration config;
2293 config.prune_turn_ports = false;
2294 CreatePeerConnection(config, nullptr);
2295 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2296
2297 config.prune_turn_ports = true;
2298 EXPECT_TRUE(pc_->SetConfiguration(config));
2299 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2300}
2301
skvladd1f5fda2017-02-03 16:54:05 -08002302// Test that the ice check interval can be changed. This does not verify that
2303// the setting makes it all the way to P2PTransportChannel, as that would
2304// require a very complex set of mocks.
2305TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2306 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002307 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002308 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002309 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002310 EXPECT_TRUE(pc_->SetConfiguration(config));
2311 PeerConnectionInterface::RTCConfiguration new_config =
2312 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002313 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002314}
2315
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002316// Test that when SetConfiguration changes both the pool size and other
2317// attributes, the pooled session is created with the updated attributes.
2318TEST_F(PeerConnectionInterfaceTest,
2319 SetConfigurationCreatesPooledSessionCorrectly) {
2320 CreatePeerConnection();
2321 PeerConnectionInterface::RTCConfiguration config;
2322 config.ice_candidate_pool_size = 1;
2323 PeerConnectionInterface::IceServer server;
2324 server.uri = kStunAddressOnly;
2325 config.servers.push_back(server);
2326 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002327 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002328
2329 const cricket::FakePortAllocatorSession* session =
2330 static_cast<const cricket::FakePortAllocatorSession*>(
2331 port_allocator_->GetPooledSession());
2332 ASSERT_NE(nullptr, session);
2333 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002334}
2335
deadbeef293e9262017-01-11 12:28:30 -08002336// Test that after SetLocalDescription, changing the pool size is not allowed,
2337// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002338TEST_F(PeerConnectionInterfaceTest,
2339 CantChangePoolSizeAfterSetLocalDescription) {
2340 CreatePeerConnection();
2341 // Start by setting a size of 1.
2342 PeerConnectionInterface::RTCConfiguration config;
2343 config.ice_candidate_pool_size = 1;
2344 EXPECT_TRUE(pc_->SetConfiguration(config));
2345
2346 // Set remote offer; can still change pool size at this point.
2347 CreateOfferAsRemoteDescription();
2348 config.ice_candidate_pool_size = 2;
2349 EXPECT_TRUE(pc_->SetConfiguration(config));
2350
2351 // Set local answer; now it's too late.
2352 CreateAnswerAsLocalDescription();
2353 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002354 RTCError error;
2355 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2356 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2357}
2358
deadbeef42a42632017-03-10 15:18:00 -08002359// Test that after setting an answer, extra pooled sessions are discarded. The
2360// ICE candidate pool is only intended to be used for the first offer/answer.
2361TEST_F(PeerConnectionInterfaceTest,
2362 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2363 CreatePeerConnection();
2364
2365 // Set a larger-than-necessary size.
2366 PeerConnectionInterface::RTCConfiguration config;
2367 config.ice_candidate_pool_size = 4;
2368 EXPECT_TRUE(pc_->SetConfiguration(config));
2369
2370 // Do offer/answer.
2371 CreateOfferAsRemoteDescription();
2372 CreateAnswerAsLocalDescription();
2373
2374 // Expect no pooled sessions to be left.
2375 const cricket::PortAllocatorSession* session =
2376 port_allocator_->GetPooledSession();
2377 EXPECT_EQ(nullptr, session);
2378}
2379
2380// After Close is called, pooled candidates should be discarded so as to not
2381// waste network resources.
2382TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2383 CreatePeerConnection();
2384
2385 PeerConnectionInterface::RTCConfiguration config;
2386 config.ice_candidate_pool_size = 3;
2387 EXPECT_TRUE(pc_->SetConfiguration(config));
2388 pc_->Close();
2389
2390 // Expect no pooled sessions to be left.
2391 const cricket::PortAllocatorSession* session =
2392 port_allocator_->GetPooledSession();
2393 EXPECT_EQ(nullptr, session);
2394}
2395
deadbeef293e9262017-01-11 12:28:30 -08002396// Test that SetConfiguration returns an invalid modification error if
2397// modifying a field in the configuration that isn't allowed to be modified.
2398TEST_F(PeerConnectionInterfaceTest,
2399 SetConfigurationReturnsInvalidModificationError) {
2400 PeerConnectionInterface::RTCConfiguration config;
2401 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2402 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2403 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2404 CreatePeerConnection(config, nullptr);
2405
2406 PeerConnectionInterface::RTCConfiguration modified_config = config;
2407 modified_config.bundle_policy =
2408 PeerConnectionInterface::kBundlePolicyMaxBundle;
2409 RTCError error;
2410 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2411 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2412
2413 modified_config = config;
2414 modified_config.rtcp_mux_policy =
2415 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2416 error.set_type(RTCErrorType::NONE);
2417 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2418 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2419
2420 modified_config = config;
2421 modified_config.continual_gathering_policy =
2422 PeerConnectionInterface::GATHER_CONTINUALLY;
2423 error.set_type(RTCErrorType::NONE);
2424 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2425 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2426}
2427
2428// Test that SetConfiguration returns a range error if the candidate pool size
2429// is negative or larger than allowed by the spec.
2430TEST_F(PeerConnectionInterfaceTest,
2431 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2432 PeerConnectionInterface::RTCConfiguration config;
2433 CreatePeerConnection(config, nullptr);
2434
2435 config.ice_candidate_pool_size = -1;
2436 RTCError error;
2437 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2438 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2439
2440 config.ice_candidate_pool_size = INT_MAX;
2441 error.set_type(RTCErrorType::NONE);
2442 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2443 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2444}
2445
2446// Test that SetConfiguration returns a syntax error if parsing an ICE server
2447// URL failed.
2448TEST_F(PeerConnectionInterfaceTest,
2449 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2450 PeerConnectionInterface::RTCConfiguration config;
2451 CreatePeerConnection(config, nullptr);
2452
2453 PeerConnectionInterface::IceServer bad_server;
2454 bad_server.uri = "stunn:www.example.com";
2455 config.servers.push_back(bad_server);
2456 RTCError error;
2457 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2458 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2459}
2460
2461// Test that SetConfiguration returns an invalid parameter error if a TURN
2462// IceServer is missing a username or password.
2463TEST_F(PeerConnectionInterfaceTest,
2464 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2465 PeerConnectionInterface::RTCConfiguration config;
2466 CreatePeerConnection(config, nullptr);
2467
2468 PeerConnectionInterface::IceServer bad_server;
2469 bad_server.uri = "turn:www.example.com";
2470 // Missing password.
2471 bad_server.username = "foo";
2472 config.servers.push_back(bad_server);
2473 RTCError error;
2474 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2475 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002476}
2477
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478// Test that PeerConnection::Close changes the states to closed and all remote
2479// tracks change state to ended.
2480TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2481 // Initialize a PeerConnection and negotiate local and remote session
2482 // description.
2483 InitiateCall();
2484 ASSERT_EQ(1u, pc_->local_streams()->count());
2485 ASSERT_EQ(1u, pc_->remote_streams()->count());
2486
2487 pc_->Close();
2488
2489 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2490 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2491 pc_->ice_connection_state());
2492 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2493 pc_->ice_gathering_state());
2494
2495 EXPECT_EQ(1u, pc_->local_streams()->count());
2496 EXPECT_EQ(1u, pc_->remote_streams()->count());
2497
zhihuang9763d562016-08-05 11:14:50 -07002498 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2499 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002500 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002501 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002502 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2503 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2504 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002505}
2506
2507// Test that PeerConnection methods fails gracefully after
2508// PeerConnection::Close has been called.
2509TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002510 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2512 CreateOfferAsRemoteDescription();
2513 CreateAnswerAsLocalDescription();
2514
2515 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002516 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 pc_->local_streams()->at(0);
2518
2519 pc_->Close();
2520
2521 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002522 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523
2524 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002525 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002527 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002528
2529 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2530
2531 EXPECT_TRUE(pc_->local_description() != NULL);
2532 EXPECT_TRUE(pc_->remote_description() != NULL);
2533
kwibergd1fe2812016-04-27 06:47:29 -07002534 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002535 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002536 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002537 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002538
2539 std::string sdp;
2540 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002541 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002542 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002543 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002544
2545 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002546 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002547 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002548 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002549}
2550
2551// Test that GetStats can still be called after PeerConnection::Close.
2552TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2553 InitiateCall();
2554 pc_->Close();
2555 DoGetStats(NULL);
2556}
deadbeefab9b2d12015-10-14 11:33:11 -07002557
2558// NOTE: The series of tests below come from what used to be
2559// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2560// setting a remote or local description has the expected effects.
2561
2562// This test verifies that the remote MediaStreams corresponding to a received
2563// SDP string is created. In this test the two separate MediaStreams are
2564// signaled.
2565TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2566 FakeConstraints constraints;
2567 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2568 true);
2569 CreatePeerConnection(&constraints);
2570 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2571
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002572 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002573 EXPECT_TRUE(
2574 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2575 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2576 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2577
2578 // Create a session description based on another SDP with another
2579 // MediaStream.
2580 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2581
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002582 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002583 EXPECT_TRUE(
2584 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2585}
2586
2587// This test verifies that when remote tracks are added/removed from SDP, the
2588// created remote streams are updated appropriately.
2589TEST_F(PeerConnectionInterfaceTest,
2590 AddRemoveTrackFromExistingRemoteMediaStream) {
2591 FakeConstraints constraints;
2592 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2593 true);
2594 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002595 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002596 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002597 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002598 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2599 reference_collection_));
2600
2601 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002602 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002603 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002604 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002605 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2606 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002607 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002608 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2609 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002610 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002611 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2612 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002613
2614 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002615 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002616 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002617 MockTrackObserver audio_track_observer(audio_track2);
2618 MockTrackObserver video_track_observer(video_track2);
2619
2620 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2621 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002622 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002623 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2624 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002625 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002626 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002627 audio_track2->state(), kTimeout);
2628 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2629 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002630}
2631
2632// This tests that remote tracks are ended if a local session description is set
2633// that rejects the media content type.
2634TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2635 FakeConstraints constraints;
2636 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2637 true);
2638 CreatePeerConnection(&constraints);
2639 // First create and set a remote offer, then reject its video content in our
2640 // answer.
2641 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2642 ASSERT_EQ(1u, observer_.remote_streams()->count());
2643 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2644 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2645 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2646
2647 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2648 remote_stream->GetVideoTracks()[0];
2649 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2650 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2651 remote_stream->GetAudioTracks()[0];
2652 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2653
kwibergd1fe2812016-04-27 06:47:29 -07002654 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002655 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002656 cricket::ContentInfo* video_info =
2657 local_answer->description()->GetContentByName("video");
2658 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002659 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002660 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2661 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2662
2663 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002664 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002665 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002666 video_info = local_offer->description()->GetContentByName("video");
2667 ASSERT_TRUE(video_info != nullptr);
2668 video_info->rejected = true;
2669 cricket::ContentInfo* audio_info =
2670 local_offer->description()->GetContentByName("audio");
2671 ASSERT_TRUE(audio_info != nullptr);
2672 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002673 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002674 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002675 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002676 remote_audio->state(), kTimeout);
2677 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2678 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002679}
2680
2681// This tests that we won't crash if the remote track has been removed outside
2682// of PeerConnection and then PeerConnection tries to reject the track.
2683TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2684 FakeConstraints constraints;
2685 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2686 true);
2687 CreatePeerConnection(&constraints);
2688 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2689 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2690 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2691 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2692
kwibergd1fe2812016-04-27 06:47:29 -07002693 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002694 webrtc::CreateSessionDescription(SdpType::kAnswer, kSdpStringWithStream1,
2695 nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002696 cricket::ContentInfo* video_info =
2697 local_answer->description()->GetContentByName("video");
2698 video_info->rejected = true;
2699 cricket::ContentInfo* audio_info =
2700 local_answer->description()->GetContentByName("audio");
2701 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002702 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002703
2704 // No crash is a pass.
2705}
2706
deadbeef5e97fb52015-10-15 12:49:08 -07002707// This tests that if a recvonly remote description is set, no remote streams
2708// will be created, even if the description contains SSRCs/MSIDs.
2709// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2710TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2711 FakeConstraints constraints;
2712 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2713 true);
2714 CreatePeerConnection(&constraints);
2715
2716 std::string recvonly_offer = kSdpStringWithStream1;
2717 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2718 strlen(kRecvonly), &recvonly_offer);
2719 CreateAndSetRemoteOffer(recvonly_offer);
2720
2721 EXPECT_EQ(0u, observer_.remote_streams()->count());
2722}
2723
deadbeefab9b2d12015-10-14 11:33:11 -07002724// This tests that a default MediaStream is created if a remote session
2725// description doesn't contain any streams and no MSID support.
2726// It also tests that the default stream is updated if a video m-line is added
2727// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002728TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002729 FakeConstraints constraints;
2730 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2731 true);
2732 CreatePeerConnection(&constraints);
2733 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2734
2735 ASSERT_EQ(1u, observer_.remote_streams()->count());
2736 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2737
2738 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2739 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2740 EXPECT_EQ("default", remote_stream->label());
2741
2742 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2743 ASSERT_EQ(1u, observer_.remote_streams()->count());
2744 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2745 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002746 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2747 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002748 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2749 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002750 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2751 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002752}
2753
2754// This tests that a default MediaStream is created if a remote session
2755// description doesn't contain any streams and media direction is send only.
2756TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002757 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002758 FakeConstraints constraints;
2759 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2760 true);
2761 CreatePeerConnection(&constraints);
2762 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2763
2764 ASSERT_EQ(1u, observer_.remote_streams()->count());
2765 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2766
2767 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2768 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2769 EXPECT_EQ("default", remote_stream->label());
2770}
2771
2772// This tests that it won't crash when PeerConnection tries to remove
2773// a remote track that as already been removed from the MediaStream.
2774TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2775 FakeConstraints constraints;
2776 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2777 true);
2778 CreatePeerConnection(&constraints);
2779 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2780 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2781 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2782 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2783
2784 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2785
2786 // No crash is a pass.
2787}
2788
2789// This tests that a default MediaStream is created if the remote session
2790// description doesn't contain any streams and don't contain an indication if
2791// MSID is supported.
2792TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002793 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002794 FakeConstraints constraints;
2795 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2796 true);
2797 CreatePeerConnection(&constraints);
2798 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2799
2800 ASSERT_EQ(1u, observer_.remote_streams()->count());
2801 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2802 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2803 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2804}
2805
2806// This tests that a default MediaStream is not created if the remote session
2807// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002808TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002809 FakeConstraints constraints;
2810 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2811 true);
2812 CreatePeerConnection(&constraints);
2813 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2814 EXPECT_EQ(0u, observer_.remote_streams()->count());
2815}
2816
deadbeefbda7e0b2015-12-08 17:13:40 -08002817// This tests that when setting a new description, the old default tracks are
2818// not destroyed and recreated.
2819// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002820TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002821 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002822 FakeConstraints constraints;
2823 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2824 true);
2825 CreatePeerConnection(&constraints);
2826 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2827
2828 ASSERT_EQ(1u, observer_.remote_streams()->count());
2829 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2830 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2831
2832 // Set the track to "disabled", then set a new description and ensure the
2833 // track is still disabled, which ensures it hasn't been recreated.
2834 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2835 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2836 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2837 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2838}
2839
deadbeefab9b2d12015-10-14 11:33:11 -07002840// This tests that a default MediaStream is not created if a remote session
2841// description is updated to not have any MediaStreams.
2842TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2843 FakeConstraints constraints;
2844 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2845 true);
2846 CreatePeerConnection(&constraints);
2847 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002848 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002849 EXPECT_TRUE(
2850 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2851
2852 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2853 EXPECT_EQ(0u, observer_.remote_streams()->count());
2854}
2855
2856// This tests that an RtpSender is created when the local description is set
2857// after adding a local stream.
2858// TODO(deadbeef): This test and the one below it need to be updated when
2859// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002860TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002861 FakeConstraints constraints;
2862 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2863 true);
2864 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002865
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002866 // Create an offer with 1 stream with 2 tracks of each type.
2867 rtc::scoped_refptr<StreamCollection> stream_collection =
2868 CreateStreamCollection(1, 2);
2869 pc_->AddStream(stream_collection->at(0));
2870 std::unique_ptr<SessionDescriptionInterface> offer;
2871 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002872 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002873
deadbeefab9b2d12015-10-14 11:33:11 -07002874 auto senders = pc_->GetSenders();
2875 EXPECT_EQ(4u, senders.size());
2876 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2877 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2878 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2879 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2880
2881 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002882 pc_->RemoveStream(stream_collection->at(0));
2883 stream_collection = CreateStreamCollection(1, 1);
2884 pc_->AddStream(stream_collection->at(0));
2885 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002886 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002887
deadbeefab9b2d12015-10-14 11:33:11 -07002888 senders = pc_->GetSenders();
2889 EXPECT_EQ(2u, senders.size());
2890 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2891 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2892 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2893 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2894}
2895
2896// This tests that an RtpSender is created when the local description is set
2897// before adding a local stream.
2898TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002899 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002900 FakeConstraints constraints;
2901 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2902 true);
2903 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002904
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002905 rtc::scoped_refptr<StreamCollection> stream_collection =
2906 CreateStreamCollection(1, 2);
2907 // Add a stream to create the offer, but remove it afterwards.
2908 pc_->AddStream(stream_collection->at(0));
2909 std::unique_ptr<SessionDescriptionInterface> offer;
2910 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2911 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002912
Steve Antondb45ca82017-09-11 18:27:34 -07002913 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002914 auto senders = pc_->GetSenders();
2915 EXPECT_EQ(0u, senders.size());
2916
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002917 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002918 senders = pc_->GetSenders();
2919 EXPECT_EQ(4u, senders.size());
2920 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2921 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2922 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2923 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2924}
2925
2926// This tests that the expected behavior occurs if the SSRC on a local track is
2927// changed when SetLocalDescription is called.
2928TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002929 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002930 FakeConstraints constraints;
2931 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2932 true);
2933 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002934
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002935 rtc::scoped_refptr<StreamCollection> stream_collection =
2936 CreateStreamCollection(2, 1);
2937 pc_->AddStream(stream_collection->at(0));
2938 std::unique_ptr<SessionDescriptionInterface> offer;
2939 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2940 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08002941 auto modified_offer =
2942 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002943 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2944 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002945 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002946
deadbeefab9b2d12015-10-14 11:33:11 -07002947 auto senders = pc_->GetSenders();
2948 EXPECT_EQ(2u, senders.size());
2949 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2950 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2951
2952 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002953 cricket::MediaContentDescription* desc =
2954 cricket::GetFirstAudioContentDescription(modified_offer->description());
2955 ASSERT_TRUE(desc != NULL);
2956 for (StreamParams& stream : desc->mutable_streams()) {
2957 for (unsigned int& ssrc : stream.ssrcs) {
2958 ++ssrc;
2959 }
2960 }
deadbeefab9b2d12015-10-14 11:33:11 -07002961
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002962 desc =
2963 cricket::GetFirstVideoContentDescription(modified_offer->description());
2964 ASSERT_TRUE(desc != NULL);
2965 for (StreamParams& stream : desc->mutable_streams()) {
2966 for (unsigned int& ssrc : stream.ssrcs) {
2967 ++ssrc;
2968 }
2969 }
2970
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002972 senders = pc_->GetSenders();
2973 EXPECT_EQ(2u, senders.size());
2974 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2975 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2976 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2977 // changed.
2978}
2979
2980// This tests that the expected behavior occurs if a new session description is
2981// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002982TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002983 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002984 FakeConstraints constraints;
2985 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2986 true);
2987 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002988
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002989 rtc::scoped_refptr<StreamCollection> stream_collection =
2990 CreateStreamCollection(2, 1);
2991 pc_->AddStream(stream_collection->at(0));
2992 std::unique_ptr<SessionDescriptionInterface> offer;
2993 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002994 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002995
deadbeefab9b2d12015-10-14 11:33:11 -07002996 auto senders = pc_->GetSenders();
2997 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002998 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2999 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003000
3001 // Add a new MediaStream but with the same tracks as in the first stream.
3002 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3003 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003004 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3005 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003006 pc_->AddStream(stream_1);
3007
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003008 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003009 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003010
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003011 auto new_senders = pc_->GetSenders();
3012 // Should be the same senders as before, but with updated stream id.
3013 // Note that this behavior is subject to change in the future.
3014 // We may decide the PC should ignore existing tracks in AddStream.
3015 EXPECT_EQ(senders, new_senders);
3016 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3017 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003018}
3019
zhihuang81c3a032016-11-17 12:06:24 -08003020// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3021TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3022 FakeConstraints constraints;
3023 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3024 true);
3025 CreatePeerConnection(&constraints);
3026 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3027 EXPECT_EQ(observer_.num_added_tracks_, 1);
3028 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3029
3030 // Create and set the updated remote SDP.
3031 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3032 EXPECT_EQ(observer_.num_added_tracks_, 2);
3033 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3034}
3035
deadbeefd1a38b52016-12-10 13:15:33 -08003036// Test that when SetConfiguration is called and the configuration is
3037// changing, the next offer causes an ICE restart.
3038TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3039 PeerConnectionInterface::RTCConfiguration config;
3040 config.type = PeerConnectionInterface::kRelay;
3041 // Need to pass default constraints to prevent disabling of DTLS...
3042 FakeConstraints default_constraints;
3043 CreatePeerConnection(config, &default_constraints);
3044 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3045
3046 // Do initial offer/answer so there's something to restart.
3047 CreateOfferAsLocalDescription();
3048 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3049
3050 // Grab the ufrags.
3051 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3052
3053 // Change ICE policy, which should trigger an ICE restart on the next offer.
3054 config.type = PeerConnectionInterface::kAll;
3055 EXPECT_TRUE(pc_->SetConfiguration(config));
3056 CreateOfferAsLocalDescription();
3057
3058 // Grab the new ufrags.
3059 std::vector<std::string> subsequent_ufrags =
3060 GetUfrags(pc_->local_description());
3061
3062 // Sanity check.
3063 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3064 // Check that each ufrag is different.
3065 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3066 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3067 }
3068}
3069
3070// Test that when SetConfiguration is called and the configuration *isn't*
3071// changing, the next offer does *not* cause an ICE restart.
3072TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3073 PeerConnectionInterface::RTCConfiguration config;
3074 config.type = PeerConnectionInterface::kRelay;
3075 // Need to pass default constraints to prevent disabling of DTLS...
3076 FakeConstraints default_constraints;
3077 CreatePeerConnection(config, &default_constraints);
3078 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3079
3080 // Do initial offer/answer so there's something to restart.
3081 CreateOfferAsLocalDescription();
3082 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3083
3084 // Grab the ufrags.
3085 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3086
3087 // Call SetConfiguration with a config identical to what the PC was
3088 // constructed with.
3089 EXPECT_TRUE(pc_->SetConfiguration(config));
3090 CreateOfferAsLocalDescription();
3091
3092 // Grab the new ufrags.
3093 std::vector<std::string> subsequent_ufrags =
3094 GetUfrags(pc_->local_description());
3095
3096 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3097}
3098
3099// Test for a weird corner case scenario:
3100// 1. Audio/video session established.
3101// 2. SetConfiguration changes ICE config; ICE restart needed.
3102// 3. ICE restart initiated by remote peer, but only for one m= section.
3103// 4. Next createOffer should initiate an ICE restart, but only for the other
3104// m= section; it would be pointless to do an ICE restart for the m= section
3105// that was already restarted.
3106TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3107 PeerConnectionInterface::RTCConfiguration config;
3108 config.type = PeerConnectionInterface::kRelay;
3109 // Need to pass default constraints to prevent disabling of DTLS...
3110 FakeConstraints default_constraints;
3111 CreatePeerConnection(config, &default_constraints);
3112 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3113
3114 // Do initial offer/answer so there's something to restart.
3115 CreateOfferAsLocalDescription();
3116 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3117
3118 // Change ICE policy, which should set the "needs-ice-restart" flag.
3119 config.type = PeerConnectionInterface::kAll;
3120 EXPECT_TRUE(pc_->SetConfiguration(config));
3121
3122 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003123 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003124 webrtc::CreateSessionDescription(SdpType::kOffer, kSdpStringWithStream1,
3125 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003126 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003127 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3128 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003129 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003130 CreateAnswerAsLocalDescription();
3131
3132 // Grab the ufrags.
3133 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3134 ASSERT_EQ(2, initial_ufrags.size());
3135
3136 // Create offer and grab the new ufrags.
3137 CreateOfferAsLocalDescription();
3138 std::vector<std::string> subsequent_ufrags =
3139 GetUfrags(pc_->local_description());
3140 ASSERT_EQ(2, subsequent_ufrags.size());
3141
3142 // Ensure that only the ufrag for the second m= section changed.
3143 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3144 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3145}
3146
deadbeeffe4a8a42016-12-20 17:56:17 -08003147// Tests that the methods to return current/pending descriptions work as
3148// expected at different points in the offer/answer exchange. This test does
3149// one offer/answer exchange as the offerer, then another as the answerer.
3150TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3151 // This disables DTLS so we can apply an answer to ourselves.
3152 CreatePeerConnection();
3153
3154 // Create initial local offer and get SDP (which will also be used as
3155 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003156 std::unique_ptr<SessionDescriptionInterface> local_offer;
3157 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003158 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003159 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003160
3161 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003162 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3163 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3164 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003165 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3166 EXPECT_EQ(nullptr, pc_->current_local_description());
3167 EXPECT_EQ(nullptr, pc_->current_remote_description());
3168
3169 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003170 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003171 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003172 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3173 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3174 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3175 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003176 EXPECT_EQ(nullptr, pc_->current_local_description());
3177 EXPECT_EQ(nullptr, pc_->current_remote_description());
3178
3179 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003180 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003181 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003182 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3183 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003184 EXPECT_EQ(nullptr, pc_->pending_local_description());
3185 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003186 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3187 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003188
3189 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003190 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003191 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003192 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3193 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3194 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003195 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003196 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3197 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003198
3199 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003200 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003201 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003202 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3203 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3204 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3205 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3206 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3207 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003208
3209 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003210 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003211 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003212 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3213 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003214 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3215 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003216 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3217 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003218}
3219
zhihuang77985012017-02-07 15:45:16 -08003220// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3221// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003222// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003223TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003224 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003225 CreatePeerConnection();
3226 // The RtcEventLog will be reset when the PeerConnection is closed.
3227 pc_->Close();
3228
Elad Alon9e6565b2017-10-11 16:04:13 +02003229 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3230 std::string filename = webrtc::test::OutputPath() +
3231 test_info->test_case_name() + test_info->name();
3232 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3233
3234 constexpr int64_t max_size_bytes = 1024;
3235
zhihuang77985012017-02-07 15:45:16 -08003236 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3237 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003238
3239 // Cleanup.
3240 rtc::ClosePlatformFile(file);
3241 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003242}
3243
Elad Alon99c3fe52017-10-13 16:29:40 +02003244// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3245// after the PeerConnection is closed.
3246// This version tests the StartRtcEventLog version that receives an object
3247// of type |RtcEventLogOutput|.
3248TEST_F(PeerConnectionInterfaceTest,
3249 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3250 CreatePeerConnection();
3251 // The RtcEventLog will be reset when the PeerConnection is closed.
3252 pc_->Close();
3253
3254 rtc::PlatformFile file = 0;
3255 int64_t max_size_bytes = 1024;
3256 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003257 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3258 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003259 pc_->StopRtcEventLog();
3260}
3261
deadbeef30952b42017-04-21 02:41:29 -07003262// Test that generated offers/answers include "ice-option:trickle".
3263TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3264 CreatePeerConnection();
3265
3266 // First, create an offer with audio/video.
3267 FakeConstraints constraints;
3268 constraints.SetMandatoryReceiveAudio(true);
3269 constraints.SetMandatoryReceiveVideo(true);
3270 std::unique_ptr<SessionDescriptionInterface> offer;
3271 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3272 cricket::SessionDescription* desc = offer->description();
3273 ASSERT_EQ(2u, desc->transport_infos().size());
3274 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3275 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3276
3277 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003278 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003279 std::unique_ptr<SessionDescriptionInterface> answer;
3280 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3281 desc = answer->description();
3282 ASSERT_EQ(2u, desc->transport_infos().size());
3283 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3284 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3285}
3286
deadbeef1dcb1642017-03-29 21:08:16 -07003287// Test that ICE renomination isn't offered if it's not enabled in the PC's
3288// RTCConfiguration.
3289TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3290 PeerConnectionInterface::RTCConfiguration config;
3291 config.enable_ice_renomination = false;
3292 CreatePeerConnection(config, nullptr);
3293 AddVoiceStream("foo");
3294
3295 std::unique_ptr<SessionDescriptionInterface> offer;
3296 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3297 cricket::SessionDescription* desc = offer->description();
3298 EXPECT_EQ(1u, desc->transport_infos().size());
3299 EXPECT_FALSE(
3300 desc->transport_infos()[0].description.GetIceParameters().renomination);
3301}
3302
3303// Test that the ICE renomination option is present in generated offers/answers
3304// if it's enabled in the PC's RTCConfiguration.
3305TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3306 PeerConnectionInterface::RTCConfiguration config;
3307 config.enable_ice_renomination = true;
3308 CreatePeerConnection(config, nullptr);
3309 AddVoiceStream("foo");
3310
3311 std::unique_ptr<SessionDescriptionInterface> offer;
3312 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3313 cricket::SessionDescription* desc = offer->description();
3314 EXPECT_EQ(1u, desc->transport_infos().size());
3315 EXPECT_TRUE(
3316 desc->transport_infos()[0].description.GetIceParameters().renomination);
3317
3318 // Set the offer as a remote description, then create an answer and ensure it
3319 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003320 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003321 std::unique_ptr<SessionDescriptionInterface> answer;
3322 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3323 desc = answer->description();
3324 EXPECT_EQ(1u, desc->transport_infos().size());
3325 EXPECT_TRUE(
3326 desc->transport_infos()[0].description.GetIceParameters().renomination);
3327}
3328
3329// Test that if CreateOffer is called with the deprecated "offer to receive
3330// audio/video" constraints, they're processed and result in an offer with
3331// audio/video sections just as if RTCOfferAnswerOptions had been used.
3332TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3333 CreatePeerConnection();
3334
3335 FakeConstraints constraints;
3336 constraints.SetMandatoryReceiveAudio(true);
3337 constraints.SetMandatoryReceiveVideo(true);
3338 std::unique_ptr<SessionDescriptionInterface> offer;
3339 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3340
3341 cricket::SessionDescription* desc = offer->description();
3342 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3343 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3344 ASSERT_NE(nullptr, audio);
3345 ASSERT_NE(nullptr, video);
3346 EXPECT_FALSE(audio->rejected);
3347 EXPECT_FALSE(video->rejected);
3348}
3349
3350// Test that if CreateAnswer is called with the deprecated "offer to receive
3351// audio/video" constraints, they're processed and can be used to reject an
3352// offered m= section just as can be done with RTCOfferAnswerOptions;
3353TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3354 CreatePeerConnection();
3355
3356 // First, create an offer with audio/video and apply it as a remote
3357 // description.
3358 FakeConstraints constraints;
3359 constraints.SetMandatoryReceiveAudio(true);
3360 constraints.SetMandatoryReceiveVideo(true);
3361 std::unique_ptr<SessionDescriptionInterface> offer;
3362 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003363 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003364
3365 // Now create answer that rejects audio/video.
3366 constraints.SetMandatoryReceiveAudio(false);
3367 constraints.SetMandatoryReceiveVideo(false);
3368 std::unique_ptr<SessionDescriptionInterface> answer;
3369 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3370
3371 cricket::SessionDescription* desc = answer->description();
3372 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3373 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3374 ASSERT_NE(nullptr, audio);
3375 ASSERT_NE(nullptr, video);
3376 EXPECT_TRUE(audio->rejected);
3377 EXPECT_TRUE(video->rejected);
3378}
3379
3380#ifdef HAVE_SCTP
3381#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3382 DataChannelOnlyOfferWithMaxBundlePolicy
3383#else
3384#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3385 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3386#endif
3387
3388// Test that negotiation can succeed with a data channel only, and with the max
3389// bundle policy. Previously there was a bug that prevented this.
3390TEST_F(PeerConnectionInterfaceTest,
3391 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3392 PeerConnectionInterface::RTCConfiguration config;
3393 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3394 CreatePeerConnection(config, nullptr);
3395
3396 // First, create an offer with only a data channel and apply it as a remote
3397 // description.
3398 pc_->CreateDataChannel("test", nullptr);
3399 std::unique_ptr<SessionDescriptionInterface> offer;
3400 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003401 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003402
3403 // Create and set answer as well.
3404 std::unique_ptr<SessionDescriptionInterface> answer;
3405 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003406 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003407}
3408
zstein4b979802017-06-02 14:37:37 -07003409TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3410 CreatePeerConnection();
3411 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003412 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003413 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3414}
3415
3416TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3417 CreatePeerConnection();
3418 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003419 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003420 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3421}
3422
3423TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3424 CreatePeerConnection();
3425 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003426 bitrate.min_bitrate_bps = 5;
3427 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003428 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3429}
3430
3431TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3432 CreatePeerConnection();
3433 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003434 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003435 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3436}
3437
3438TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3439 CreatePeerConnection();
3440 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003441 bitrate.current_bitrate_bps = 10;
3442 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003443 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3444}
3445
3446TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3447 CreatePeerConnection();
3448 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003449 bitrate.min_bitrate_bps = 10;
3450 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003451 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3452}
3453
3454TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3455 CreatePeerConnection();
3456 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003457 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003458 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3459}
3460
Steve Anton038834f2017-07-14 15:59:59 -07003461// ice_regather_interval_range requires WebRTC to be configured for continual
3462// gathering already.
3463TEST_F(PeerConnectionInterfaceTest,
3464 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3465 PeerConnectionInterface::RTCConfiguration config;
3466 config.ice_regather_interval_range.emplace(1000, 2000);
3467 config.continual_gathering_policy =
3468 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3469 CreatePeerConnectionExpectFail(config);
3470}
3471
3472// Ensures that there is no error when ice_regather_interval_range is set with
3473// continual gathering enabled.
3474TEST_F(PeerConnectionInterfaceTest,
3475 SetIceRegatherIntervalRangeWithContinualGathering) {
3476 PeerConnectionInterface::RTCConfiguration config;
3477 config.ice_regather_interval_range.emplace(1000, 2000);
3478 config.continual_gathering_policy =
3479 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3480 CreatePeerConnection(config, nullptr);
3481}
3482
zstein4b979802017-06-02 14:37:37 -07003483// The current bitrate from Call's BitrateConfigMask is currently clamped by
3484// Call's BitrateConfig, which comes from the SDP or a default value. This test
3485// checks that a call to SetBitrate with a current bitrate that will be clamped
3486// succeeds.
3487TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3488 CreatePeerConnection();
3489 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003490 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003491 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3492}
3493
zhihuang1c378ed2017-08-17 14:10:50 -07003494// The following tests verify that the offer can be created correctly.
3495TEST_F(PeerConnectionInterfaceTest,
3496 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3497 RTCOfferAnswerOptions rtc_options;
3498
3499 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3500 // than kMaxOfferToReceiveMedia should be treated as invalid.
3501 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3502 CreatePeerConnection();
3503 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3504
3505 rtc_options.offer_to_receive_audio =
3506 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3507 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3508}
3509
3510TEST_F(PeerConnectionInterfaceTest,
3511 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3512 RTCOfferAnswerOptions rtc_options;
3513
3514 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3515 // than kMaxOfferToReceiveMedia should be treated as invalid.
3516 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3517 CreatePeerConnection();
3518 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3519
3520 rtc_options.offer_to_receive_video =
3521 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3522 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3523}
3524
3525// Test that the audio and video content will be added to an offer if both
3526// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3527TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3528 RTCOfferAnswerOptions rtc_options;
3529 rtc_options.offer_to_receive_audio = 1;
3530 rtc_options.offer_to_receive_video = 1;
3531
3532 std::unique_ptr<SessionDescriptionInterface> offer;
3533 CreatePeerConnection();
3534 offer = CreateOfferWithOptions(rtc_options);
3535 ASSERT_TRUE(offer);
3536 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3537 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3538}
3539
3540// Test that only audio content will be added to the offer if only
3541// |offer_to_receive_audio| options is 1.
3542TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3543 RTCOfferAnswerOptions rtc_options;
3544 rtc_options.offer_to_receive_audio = 1;
3545 rtc_options.offer_to_receive_video = 0;
3546
3547 std::unique_ptr<SessionDescriptionInterface> offer;
3548 CreatePeerConnection();
3549 offer = CreateOfferWithOptions(rtc_options);
3550 ASSERT_TRUE(offer);
3551 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3552 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3553}
3554
3555// Test that only video content will be added if only |offer_to_receive_video|
3556// options is 1.
3557TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3558 RTCOfferAnswerOptions rtc_options;
3559 rtc_options.offer_to_receive_audio = 0;
3560 rtc_options.offer_to_receive_video = 1;
3561
3562 std::unique_ptr<SessionDescriptionInterface> offer;
3563 CreatePeerConnection();
3564 offer = CreateOfferWithOptions(rtc_options);
3565 ASSERT_TRUE(offer);
3566 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3567 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3568}
3569
zhihuang1c378ed2017-08-17 14:10:50 -07003570// Test that no media content will be added to the offer if using default
3571// RTCOfferAnswerOptions.
3572TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3573 RTCOfferAnswerOptions rtc_options;
3574
3575 std::unique_ptr<SessionDescriptionInterface> offer;
3576 CreatePeerConnection();
3577 offer = CreateOfferWithOptions(rtc_options);
3578 ASSERT_TRUE(offer);
3579 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3580 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3581}
3582
3583// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3584// ufrag/pwd will be the same in the new offer.
3585TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3586 RTCOfferAnswerOptions rtc_options;
3587 rtc_options.ice_restart = false;
3588 rtc_options.offer_to_receive_audio = 1;
3589
3590 std::unique_ptr<SessionDescriptionInterface> offer;
3591 CreatePeerConnection();
3592 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3593 auto ufrag1 = offer->description()
3594 ->GetTransportInfoByName(cricket::CN_AUDIO)
3595 ->description.ice_ufrag;
3596 auto pwd1 = offer->description()
3597 ->GetTransportInfoByName(cricket::CN_AUDIO)
3598 ->description.ice_pwd;
3599
3600 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3601 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3602 auto ufrag2 = offer->description()
3603 ->GetTransportInfoByName(cricket::CN_AUDIO)
3604 ->description.ice_ufrag;
3605 auto pwd2 = offer->description()
3606 ->GetTransportInfoByName(cricket::CN_AUDIO)
3607 ->description.ice_pwd;
3608
3609 // |ice_restart| is true, the ufrag/pwd should change.
3610 rtc_options.ice_restart = true;
3611 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3612 auto ufrag3 = offer->description()
3613 ->GetTransportInfoByName(cricket::CN_AUDIO)
3614 ->description.ice_ufrag;
3615 auto pwd3 = offer->description()
3616 ->GetTransportInfoByName(cricket::CN_AUDIO)
3617 ->description.ice_pwd;
3618
3619 EXPECT_EQ(ufrag1, ufrag2);
3620 EXPECT_EQ(pwd1, pwd2);
3621 EXPECT_NE(ufrag2, ufrag3);
3622 EXPECT_NE(pwd2, pwd3);
3623}
3624
3625// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3626// offer; if it is false, there won't be any bundle group in the offer.
3627TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3628 RTCOfferAnswerOptions rtc_options;
3629 rtc_options.offer_to_receive_audio = 1;
3630 rtc_options.offer_to_receive_video = 1;
3631
3632 std::unique_ptr<SessionDescriptionInterface> offer;
3633 CreatePeerConnection();
3634
3635 rtc_options.use_rtp_mux = true;
3636 offer = CreateOfferWithOptions(rtc_options);
3637 ASSERT_TRUE(offer);
3638 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3639 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3640 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3641
3642 rtc_options.use_rtp_mux = false;
3643 offer = CreateOfferWithOptions(rtc_options);
3644 ASSERT_TRUE(offer);
3645 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3646 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3647 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3648}
3649
zhihuang141aacb2017-08-29 13:23:53 -07003650// This test ensures OnRenegotiationNeeded is called when we add track with
3651// MediaStream -> AddTrack in the same way it is called when we add track with
3652// PeerConnection -> AddTrack.
3653// The test can be removed once addStream is rewritten in terms of addTrack
3654// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3655TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3656 CreatePeerConnectionWithoutDtls();
3657 rtc::scoped_refptr<MediaStreamInterface> stream(
3658 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3659 pc_->AddStream(stream);
3660 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3661 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3662 rtc::scoped_refptr<VideoTrackInterface> video_track(
3663 pc_factory_->CreateVideoTrack(
3664 "video_track", pc_factory_->CreateVideoSource(
3665 std::unique_ptr<cricket::VideoCapturer>(
3666 new cricket::FakeVideoCapturer()))));
3667 stream->AddTrack(audio_track);
3668 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3669 observer_.renegotiation_needed_ = false;
3670
3671 stream->AddTrack(video_track);
3672 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3673 observer_.renegotiation_needed_ = false;
3674
3675 stream->RemoveTrack(audio_track);
3676 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3677 observer_.renegotiation_needed_ = false;
3678
3679 stream->RemoveTrack(video_track);
3680 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3681 observer_.renegotiation_needed_ = false;
3682}
3683
Zhi Huangb2d355e2017-10-26 17:26:37 -07003684// Tests that an error is returned if a description is applied that has fewer
3685// media sections than the existing description.
3686TEST_F(PeerConnectionInterfaceTest,
3687 MediaSectionCountEnforcedForSubsequentOffer) {
3688 CreatePeerConnection();
3689 FakeConstraints constraints;
3690 constraints.SetMandatoryReceiveAudio(true);
3691 constraints.SetMandatoryReceiveVideo(true);
3692 std::unique_ptr<SessionDescriptionInterface> offer;
3693 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3694 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3695
3696 // A remote offer with fewer media sections should be rejected.
3697 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3698 offer->description()->contents().pop_back();
3699 offer->description()->contents().pop_back();
3700 ASSERT_TRUE(offer->description()->contents().empty());
3701 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3702
3703 std::unique_ptr<SessionDescriptionInterface> answer;
3704 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3705 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3706
3707 // A subsequent local offer with fewer media sections should be rejected.
3708 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3709 offer->description()->contents().pop_back();
3710 offer->description()->contents().pop_back();
3711 ASSERT_TRUE(offer->description()->contents().empty());
3712 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3713}
3714
nisse51542be2016-02-12 02:27:06 -08003715class PeerConnectionMediaConfigTest : public testing::Test {
3716 protected:
3717 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003718 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003719 pcf_->Initialize();
3720 }
nisseeaabdf62017-05-05 02:23:02 -07003721 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003722 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003723 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003724 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3725 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003726 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003727 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003728 }
3729
zhihuang9763d562016-08-05 11:14:50 -07003730 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003731 MockPeerConnectionObserver observer_;
3732};
3733
3734// This test verifies the default behaviour with no constraints and a
3735// default RTCConfiguration.
3736TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3737 PeerConnectionInterface::RTCConfiguration config;
3738 FakeConstraints constraints;
3739
3740 const cricket::MediaConfig& media_config =
3741 TestCreatePeerConnection(config, &constraints);
3742
3743 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003744 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3745 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3746 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003747 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003748}
3749
3750// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003751// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003752TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3753 PeerConnectionInterface::RTCConfiguration config;
3754 FakeConstraints constraints;
3755
3756 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3757 const cricket::MediaConfig& media_config =
3758 TestCreatePeerConnection(config, &constraints);
3759
3760 EXPECT_TRUE(media_config.enable_dscp);
3761}
3762
3763// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003764// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003765TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3766 PeerConnectionInterface::RTCConfiguration config;
3767 FakeConstraints constraints;
3768
3769 constraints.AddOptional(
3770 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3771 const cricket::MediaConfig media_config =
3772 TestCreatePeerConnection(config, &constraints);
3773
nisse0db023a2016-03-01 04:29:59 -08003774 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003775}
3776
3777// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003778// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003779TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3780 PeerConnectionInterface::RTCConfiguration config;
3781 FakeConstraints constraints;
3782
Niels Möller71bdda02016-03-31 12:59:59 +02003783 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003784 const cricket::MediaConfig& media_config =
3785 TestCreatePeerConnection(config, &constraints);
3786
nisse0db023a2016-03-01 04:29:59 -08003787 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3788}
3789
Niels Möller6539f692018-01-18 08:58:50 +01003790// This test verifies that the experiment_cpu_load_estimator flag is
3791// propagated from RTCConfiguration to the PeerConnection.
3792TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3793 PeerConnectionInterface::RTCConfiguration config;
3794 FakeConstraints constraints;
3795
3796 config.set_experiment_cpu_load_estimator(true);
3797 const cricket::MediaConfig& media_config =
3798 TestCreatePeerConnection(config, &constraints);
3799
3800 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3801}
3802
nisse0db023a2016-03-01 04:29:59 -08003803// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003804// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003805TEST_F(PeerConnectionMediaConfigTest,
3806 TestSuspendBelowMinBitrateConstraintTrue) {
3807 PeerConnectionInterface::RTCConfiguration config;
3808 FakeConstraints constraints;
3809
3810 constraints.AddOptional(
3811 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3812 true);
3813 const cricket::MediaConfig media_config =
3814 TestCreatePeerConnection(config, &constraints);
3815
3816 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003817}
3818
deadbeef293e9262017-01-11 12:28:30 -08003819// Tests a few random fields being different.
3820TEST(RTCConfigurationTest, ComparisonOperators) {
3821 PeerConnectionInterface::RTCConfiguration a;
3822 PeerConnectionInterface::RTCConfiguration b;
3823 EXPECT_EQ(a, b);
3824
3825 PeerConnectionInterface::RTCConfiguration c;
3826 c.servers.push_back(PeerConnectionInterface::IceServer());
3827 EXPECT_NE(a, c);
3828
3829 PeerConnectionInterface::RTCConfiguration d;
3830 d.type = PeerConnectionInterface::kRelay;
3831 EXPECT_NE(a, d);
3832
3833 PeerConnectionInterface::RTCConfiguration e;
3834 e.audio_jitter_buffer_max_packets = 5;
3835 EXPECT_NE(a, e);
3836
3837 PeerConnectionInterface::RTCConfiguration f;
3838 f.ice_connection_receiving_timeout = 1337;
3839 EXPECT_NE(a, f);
3840
3841 PeerConnectionInterface::RTCConfiguration g;
3842 g.disable_ipv6 = true;
3843 EXPECT_NE(a, g);
3844
3845 PeerConnectionInterface::RTCConfiguration h(
3846 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3847 EXPECT_NE(a, h);
3848}