blob: 8d0c6f49aeb6671634f132510045623148fe7d59 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353using webrtc::SdpParseError;
354using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700355using webrtc::StreamCollection;
356using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100357using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700358using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359using webrtc::VideoTrackInterface;
360
deadbeefab9b2d12015-10-14 11:33:11 -0700361typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
362
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363namespace {
364
365// Gets the first ssrc of given content type from the ContentInfo.
366bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
367 if (!content_info || !ssrc) {
368 return false;
369 }
370 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000371 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 content_info->description);
373 if (!media_desc || media_desc->streams().empty()) {
374 return false;
375 }
376 *ssrc = media_desc->streams().begin()->first_ssrc();
377 return true;
378}
379
deadbeefd1a38b52016-12-10 13:15:33 -0800380// Get the ufrags out of an SDP blob. Useful for testing ICE restart
381// behavior.
382std::vector<std::string> GetUfrags(
383 const webrtc::SessionDescriptionInterface* desc) {
384 std::vector<std::string> ufrags;
385 for (const cricket::TransportInfo& info :
386 desc->description()->transport_infos()) {
387 ufrags.push_back(info.description.ice_ufrag);
388 }
389 return ufrags;
390}
391
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392void SetSsrcToZero(std::string* sdp) {
393 const char kSdpSsrcAtribute[] = "a=ssrc:";
394 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
395 size_t ssrc_pos = 0;
396 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
397 std::string::npos) {
398 size_t end_ssrc = sdp->find(" ", ssrc_pos);
399 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
400 ssrc_pos = end_ssrc;
401 }
402}
403
deadbeefab9b2d12015-10-14 11:33:11 -0700404// Check if |streams| contains the specified track.
405bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
406 const std::string& stream_label,
407 const std::string& track_id) {
408 for (const cricket::StreamParams& params : streams) {
409 if (params.sync_label == stream_label && params.id == track_id) {
410 return true;
411 }
412 }
413 return false;
414}
415
416// Check if |senders| contains the specified sender, by id.
417bool ContainsSender(
418 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
419 const std::string& id) {
420 for (const auto& sender : senders) {
421 if (sender->id() == id) {
422 return true;
423 }
424 }
425 return false;
426}
427
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700428// Check if |senders| contains the specified sender, by id and stream id.
429bool ContainsSender(
430 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
431 const std::string& id,
432 const std::string& stream_id) {
433 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700434 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700435 return true;
436 }
437 }
438 return false;
439}
440
deadbeefab9b2d12015-10-14 11:33:11 -0700441// Create a collection of streams.
442// CreateStreamCollection(1) creates a collection that
443// correspond to kSdpStringWithStream1.
444// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
445rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700446 int number_of_streams,
447 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700448 rtc::scoped_refptr<StreamCollection> local_collection(
449 StreamCollection::Create());
450
451 for (int i = 0; i < number_of_streams; ++i) {
452 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
453 webrtc::MediaStream::Create(kStreams[i]));
454
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700455 for (int j = 0; j < tracks_per_stream; ++j) {
456 // Add a local audio track.
457 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
458 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
459 nullptr));
460 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700461
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700462 // Add a local video track.
463 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
464 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700465 webrtc::FakeVideoTrackSource::Create(),
466 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700467 stream->AddTrack(video_track);
468 }
deadbeefab9b2d12015-10-14 11:33:11 -0700469
470 local_collection->AddStream(stream);
471 }
472 return local_collection;
473}
474
475// Check equality of StreamCollections.
476bool CompareStreamCollections(StreamCollectionInterface* s1,
477 StreamCollectionInterface* s2) {
478 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
479 return false;
480 }
481
482 for (size_t i = 0; i != s1->count(); ++i) {
483 if (s1->at(i)->label() != s2->at(i)->label()) {
484 return false;
485 }
486 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
487 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
488 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
489 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
490
491 if (audio_tracks1.size() != audio_tracks2.size()) {
492 return false;
493 }
494 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
495 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
496 return false;
497 }
498 }
499 if (video_tracks1.size() != video_tracks2.size()) {
500 return false;
501 }
502 for (size_t j = 0; j != video_tracks1.size(); ++j) {
503 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
504 return false;
505 }
506 }
507 }
508 return true;
509}
510
perkjd61bf802016-03-24 03:16:19 -0700511// Helper class to test Observer.
512class MockTrackObserver : public ObserverInterface {
513 public:
514 explicit MockTrackObserver(NotifierInterface* notifier)
515 : notifier_(notifier) {
516 notifier_->RegisterObserver(this);
517 }
518
519 ~MockTrackObserver() { Unregister(); }
520
521 void Unregister() {
522 if (notifier_) {
523 notifier_->UnregisterObserver(this);
524 notifier_ = nullptr;
525 }
526 }
527
528 MOCK_METHOD0(OnChanged, void());
529
530 private:
531 NotifierInterface* notifier_;
532};
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700535
nisse528b7932017-05-08 03:21:43 -0700536// The PeerConnectionMediaConfig tests below verify that configuration and
537// constraints are propagated into the PeerConnection's MediaConfig. These
538// settings are intended for MediaChannel constructors, but that is not
539// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700540class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
541 public:
zhihuang38ede132017-06-15 12:52:32 -0700542 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
543 CreatePeerConnectionFactoryForTest() {
544 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
545 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
546
henrika919dc2e2017-10-12 14:24:55 +0200547 // Use fake audio device module since we're only testing the interface
548 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700549 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
550 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200551 FakeAudioCaptureModule::Create(), audio_encoder_factory,
552 audio_decoder_factory, nullptr, nullptr, nullptr,
553 webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700554
555 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
556 webrtc::CreateCallFactory();
557
558 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
559 webrtc::CreateRtcEventLogFactory();
560
561 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
562 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200563 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700564 std::move(event_log_factory));
565 }
566
567 PeerConnectionFactoryForTest(
568 rtc::Thread* network_thread,
569 rtc::Thread* worker_thread,
570 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700571 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
572 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
573 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
574 : webrtc::PeerConnectionFactory(network_thread,
575 worker_thread,
576 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700577 std::move(media_engine),
578 std::move(call_factory),
579 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800580
zhihuang29ff8442016-07-27 11:07:25 -0700581 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700582 cricket::PortAllocator* port_allocator,
583 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700584 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700585 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700586 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700587 return transport_controller;
588 }
589
deadbeefd7850b22017-08-23 10:59:19 -0700590 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700591 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700592};
593
Steve Anton94286cb2017-09-26 16:20:19 -0700594// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
595// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596class PeerConnectionInterfaceTest : public testing::Test {
597 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700598 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700599 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800600#ifdef WEBRTC_ANDROID
601 webrtc::InitializeAndroidObjects();
602#endif
603 }
604
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700606 // Use fake audio capture module since we're only testing the interface
607 // level, and using a real one could make tests flaky when run in parallel.
608 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700610 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200611 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
612 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700613 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700614 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700615 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700616 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618
619 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700620 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 }
622
deadbeef293e9262017-01-11 12:28:30 -0800623 // DTLS does not work in a loopback call, so is disabled for most of the
624 // tests in this file.
625 void CreatePeerConnectionWithoutDtls() {
626 FakeConstraints no_dtls_constraints;
627 no_dtls_constraints.AddMandatory(
628 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
629
630 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
631 &no_dtls_constraints);
632 }
633
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700635 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
636 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 }
638
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700639 void CreatePeerConnectionWithIceTransportsType(
640 PeerConnectionInterface::IceTransportsType type) {
641 PeerConnectionInterface::RTCConfiguration config;
642 config.type = type;
643 return CreatePeerConnection(config, nullptr);
644 }
645
646 void CreatePeerConnectionWithIceServer(const std::string& uri,
647 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800648 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700650 server.uri = uri;
651 server.password = password;
652 config.servers.push_back(server);
653 CreatePeerConnection(config, nullptr);
654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100656 void CreatePeerConnection(
657 const PeerConnectionInterface::RTCConfiguration& config,
658 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700659 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800660 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
661 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000662
deadbeef1dcb1642017-03-29 21:08:16 -0700663 // Create certificate generator unless DTLS constraint is explicitly set to
664 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200665 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000666 bool dtls;
667 if (FindConstraint(constraints,
668 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
669 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200670 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800671 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
672 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000673 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200674 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200676 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 ASSERT_TRUE(pc_.get() != NULL);
678 observer_.SetPeerConnectionInterface(pc_.get());
679 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
680 }
681
deadbeef0a6c4ca2015-10-06 11:38:28 -0700682 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800683 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700684 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700685 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800686 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700687
zhihuang9763d562016-08-05 11:14:50 -0700688 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700689 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
690 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700692 }
693
Steve Anton038834f2017-07-14 15:59:59 -0700694 void CreatePeerConnectionExpectFail(
695 PeerConnectionInterface::RTCConfiguration config) {
696 PeerConnectionInterface::IceServer server;
697 server.uri = kTurnIceServerUri;
698 server.password = kTurnPassword;
699 config.servers.push_back(server);
700 rtc::scoped_refptr<PeerConnectionInterface> pc =
701 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
702 EXPECT_EQ(nullptr, pc);
703 }
704
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700706 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800707 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
708 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
709 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800711 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712
deadbeef0a6c4ca2015-10-06 11:38:28 -0700713 CreatePeerConnectionExpectFail(kStunInvalidPort);
714 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
715 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
719 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800721 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800725 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 }
727
728 void ReleasePeerConnection() {
729 pc_ = NULL;
730 observer_.SetPeerConnectionInterface(NULL);
731 }
732
deadbeefab9b2d12015-10-14 11:33:11 -0700733 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700735 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700737 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800738 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
739 new cricket::FakeVideoCapturer()),
740 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700741 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 pc_factory_->CreateVideoTrack(label + "v0", video_source));
743 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000744 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
746 observer_.renegotiation_needed_ = false;
747 }
748
749 void AddVoiceStream(const std::string& label) {
750 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700751 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700753 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 pc_factory_->CreateAudioTrack(label + "a0", NULL));
755 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000756 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
758 observer_.renegotiation_needed_ = false;
759 }
760
761 void AddAudioVideoStream(const std::string& stream_label,
762 const std::string& audio_track_label,
763 const std::string& video_track_label) {
764 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700765 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700767 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 pc_factory_->CreateAudioTrack(
769 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
770 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700771 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700772 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800773 video_track_label, pc_factory_->CreateVideoSource(
774 std::unique_ptr<cricket::VideoCapturer>(
775 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000777 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
779 observer_.renegotiation_needed_ = false;
780 }
781
kwibergd1fe2812016-04-27 06:47:29 -0700782 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700783 bool offer,
784 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000785 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
786 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 MockCreateSessionDescriptionObserver>());
788 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700789 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700791 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 }
793 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700794 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return observer->result();
796 }
797
kwibergd1fe2812016-04-27 06:47:29 -0700798 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700799 MediaConstraintsInterface* constraints) {
800 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
kwibergd1fe2812016-04-27 06:47:29 -0700803 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700804 MediaConstraintsInterface* constraints) {
805 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
Steve Antondb45ca82017-09-11 18:27:34 -0700808 bool DoSetSessionDescription(
809 std::unique_ptr<SessionDescriptionInterface> desc,
810 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000811 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
812 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 MockSetSessionDescriptionObserver>());
814 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700815 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700817 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 }
zhihuang29ff8442016-07-27 11:07:25 -0700819 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
820 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
821 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 return observer->result();
823 }
824
Steve Antondb45ca82017-09-11 18:27:34 -0700825 bool DoSetLocalDescription(
826 std::unique_ptr<SessionDescriptionInterface> desc) {
827 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
829
Steve Antondb45ca82017-09-11 18:27:34 -0700830 bool DoSetRemoteDescription(
831 std::unique_ptr<SessionDescriptionInterface> desc) {
832 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
835 // Calls PeerConnection::GetStats and check the return value.
836 // It does not verify the values in the StatReports since a RTCP packet might
837 // be required.
838 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000839 rtc::scoped_refptr<MockStatsObserver> observer(
840 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000841 if (!pc_->GetStats(
842 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 return false;
844 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
845 return observer->called();
846 }
847
848 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800849 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 // Create a local stream with audio&video tracks.
851 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
852 CreateOfferReceiveAnswer();
853 }
854
855 // Verify that RTP Header extensions has been negotiated for audio and video.
856 void VerifyRemoteRtpHeaderExtensions() {
857 const cricket::MediaContentDescription* desc =
858 cricket::GetFirstAudioContentDescription(
859 pc_->remote_description()->description());
860 ASSERT_TRUE(desc != NULL);
861 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
862
863 desc = cricket::GetFirstVideoContentDescription(
864 pc_->remote_description()->description());
865 ASSERT_TRUE(desc != NULL);
866 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
867 }
868
869 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700870 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700871 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 std::string sdp;
873 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700874 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700876 sdp, nullptr));
877 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
879 }
880
deadbeefab9b2d12015-10-14 11:33:11 -0700881 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700882 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700883 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700884 sdp, nullptr));
885 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700886 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
887 }
888
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700890 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700891 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892
893 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
894 // audio codec change, even if the parameter has nothing to do with
895 // receiving. Not all parameters are serialized to SDP.
896 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
897 // the SessionDescription, it is necessary to do that here to in order to
898 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
899 // https://code.google.com/p/webrtc/issues/detail?id=1356
900 std::string sdp;
901 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700902 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700904 sdp, nullptr));
905 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
907 }
908
909 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700910 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700911 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912
913 std::string sdp;
914 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700915 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700917 sdp, nullptr));
918 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
920 }
921
922 void CreateOfferReceiveAnswer() {
923 CreateOfferAsLocalDescription();
924 std::string sdp;
925 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
926 CreateAnswerAsRemoteDescription(sdp);
927 }
928
929 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700930 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700931 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
933 // audio codec change, even if the parameter has nothing to do with
934 // receiving. Not all parameters are serialized to SDP.
935 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
936 // the SessionDescription, it is necessary to do that here to in order to
937 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
938 // https://code.google.com/p/webrtc/issues/detail?id=1356
939 std::string sdp;
940 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700941 std::unique_ptr<SessionDescriptionInterface> new_offer(
942 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
943 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944
Steve Antondb45ca82017-09-11 18:27:34 -0700945 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000947 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700948 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 }
950
deadbeefab9b2d12015-10-14 11:33:11 -0700951 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700952 std::unique_ptr<SessionDescriptionInterface> answer(
953 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
954 sdp, nullptr));
955 ASSERT_TRUE(answer);
956 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
958 }
959
deadbeefab9b2d12015-10-14 11:33:11 -0700960 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700961 std::unique_ptr<SessionDescriptionInterface> pr_answer(
962 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
963 sdp, nullptr));
964 ASSERT_TRUE(pr_answer);
965 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700967 std::unique_ptr<SessionDescriptionInterface> answer(
968 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
969 sdp, nullptr));
970 ASSERT_TRUE(answer);
971 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
973 }
974
975 // Help function used for waiting until a the last signaled remote stream has
976 // the same label as |stream_label|. In a few of the tests in this file we
977 // answer with the same session description as we offer and thus we can
978 // check if OnAddStream have been called with the same stream as we offer to
979 // send.
980 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
981 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
982 }
983
984 // Creates an offer and applies it as a local session description.
985 // Creates an answer with the same SDP an the offer but removes all lines
986 // that start with a:ssrc"
987 void CreateOfferReceiveAnswerWithoutSsrc() {
988 CreateOfferAsLocalDescription();
989 std::string sdp;
990 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
991 SetSsrcToZero(&sdp);
992 CreateAnswerAsRemoteDescription(sdp);
993 }
994
deadbeefab9b2d12015-10-14 11:33:11 -0700995 // This function creates a MediaStream with label kStreams[0] and
996 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
997 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700998 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700999 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001001 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1002 size_t number_of_video_tracks) {
1003 EXPECT_LE(number_of_audio_tracks, 2u);
1004 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001005
1006 reference_collection_ = StreamCollection::Create();
1007 std::string sdp_ms1 = std::string(kSdpStringInit);
1008
1009 std::string mediastream_label = kStreams[0];
1010
1011 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1012 webrtc::MediaStream::Create(mediastream_label));
1013 reference_collection_->AddStream(stream);
1014
1015 if (number_of_audio_tracks > 0) {
1016 sdp_ms1 += std::string(kSdpStringAudio);
1017 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1018 AddAudioTrack(kAudioTracks[0], stream);
1019 }
1020 if (number_of_audio_tracks > 1) {
1021 sdp_ms1 += kSdpStringMs1Audio1;
1022 AddAudioTrack(kAudioTracks[1], stream);
1023 }
1024
1025 if (number_of_video_tracks > 0) {
1026 sdp_ms1 += std::string(kSdpStringVideo);
1027 sdp_ms1 += std::string(kSdpStringMs1Video0);
1028 AddVideoTrack(kVideoTracks[0], stream);
1029 }
1030 if (number_of_video_tracks > 1) {
1031 sdp_ms1 += kSdpStringMs1Video1;
1032 AddVideoTrack(kVideoTracks[1], stream);
1033 }
1034
kwibergd1fe2812016-04-27 06:47:29 -07001035 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001036 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1037 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001038 }
1039
1040 void AddAudioTrack(const std::string& track_id,
1041 MediaStreamInterface* stream) {
1042 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1043 webrtc::AudioTrack::Create(track_id, nullptr));
1044 ASSERT_TRUE(stream->AddTrack(audio_track));
1045 }
1046
1047 void AddVideoTrack(const std::string& track_id,
1048 MediaStreamInterface* stream) {
1049 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001050 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001051 webrtc::FakeVideoTrackSource::Create(),
1052 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001053 ASSERT_TRUE(stream->AddTrack(video_track));
1054 }
1055
kwibergfd8be342016-05-14 19:44:11 -07001056 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001057 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001058 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001059 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001060 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1061 return offer;
1062 }
1063
kwibergfd8be342016-05-14 19:44:11 -07001064 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001065 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001066 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001067 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001068 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1069 return answer;
1070 }
1071
1072 const std::string& GetFirstAudioStreamCname(
1073 const SessionDescriptionInterface* desc) {
1074 const cricket::ContentInfo* audio_content =
1075 cricket::GetFirstAudioContent(desc->description());
1076 const cricket::AudioContentDescription* audio_desc =
1077 static_cast<const cricket::AudioContentDescription*>(
1078 audio_content->description);
1079 return audio_desc->streams()[0].cname;
1080 }
1081
zhihuang1c378ed2017-08-17 14:10:50 -07001082 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1083 const RTCOfferAnswerOptions& offer_answer_options) {
1084 RTC_DCHECK(pc_);
1085 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1086 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1087 pc_->CreateOffer(observer, offer_answer_options);
1088 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1089 return observer->MoveDescription();
1090 }
1091
1092 void CreateOfferWithOptionsAsRemoteDescription(
1093 std::unique_ptr<SessionDescriptionInterface>* desc,
1094 const RTCOfferAnswerOptions& offer_answer_options) {
1095 *desc = CreateOfferWithOptions(offer_answer_options);
1096 ASSERT_TRUE(desc != nullptr);
1097 std::string sdp;
1098 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001099 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001100 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001101 sdp, nullptr));
1102 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001103 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1104 }
1105
1106 void CreateOfferWithOptionsAsLocalDescription(
1107 std::unique_ptr<SessionDescriptionInterface>* desc,
1108 const RTCOfferAnswerOptions& offer_answer_options) {
1109 *desc = CreateOfferWithOptions(offer_answer_options);
1110 ASSERT_TRUE(desc != nullptr);
1111 std::string sdp;
1112 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001113 std::unique_ptr<SessionDescriptionInterface> new_offer(
1114 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1115 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001116
Steve Antondb45ca82017-09-11 18:27:34 -07001117 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001118 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1119 }
1120
1121 bool HasCNCodecs(const cricket::ContentInfo* content) {
1122 const cricket::ContentDescription* description = content->description;
1123 RTC_DCHECK(description);
1124 const cricket::AudioContentDescription* audio_content_desc =
1125 static_cast<const cricket::AudioContentDescription*>(description);
1126 RTC_DCHECK(audio_content_desc);
1127 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1128 if (audio_content_desc->codecs()[i].name == "CN")
1129 return true;
1130 }
1131 return false;
1132 }
1133
deadbeef9a6f4d42017-05-15 19:43:33 -07001134 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1135 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001136 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001137 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001138 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001139 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1140 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1141 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001143 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144};
1145
zhihuang29ff8442016-07-27 11:07:25 -07001146// Test that no callbacks on the PeerConnectionObserver are called after the
1147// PeerConnection is closed.
1148TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001149 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001150 pc_factory_for_test_->CreatePeerConnection(
1151 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1152 nullptr, &observer_));
1153 observer_.SetPeerConnectionInterface(pc.get());
1154 pc->Close();
1155
1156 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001157 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001158 std::vector<cricket::Candidate> candidates;
1159 pc_factory_for_test_->transport_controller->SignalGatheringState(
1160 cricket::IceGatheringState{});
1161 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1162 "", candidates);
1163 pc_factory_for_test_->transport_controller->SignalConnectionState(
1164 cricket::IceConnectionState{});
1165 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1166 candidates);
1167 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001168 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001169}
1170
zhihuang8f65cdf2016-05-06 18:40:30 -07001171// Generate different CNAMEs when PeerConnections are created.
1172// The CNAMEs are expected to be generated randomly. It is possible
1173// that the test fails, though the possibility is very low.
1174TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001175 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001176 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 CreateOfferWithOneAudioStream();
1179 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1180 GetFirstAudioStreamCname(offer2.get()));
1181}
1182
1183TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001184 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001186 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001187 CreateAnswerWithOneAudioStream();
1188 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1189 GetFirstAudioStreamCname(answer2.get()));
1190}
1191
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192TEST_F(PeerConnectionInterfaceTest,
1193 CreatePeerConnectionWithDifferentConfigurations) {
1194 CreatePeerConnectionWithDifferentConfigurations();
1195}
1196
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001197TEST_F(PeerConnectionInterfaceTest,
1198 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1199 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1200 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1201 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1202 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1203 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1204 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1205 port_allocator_->candidate_filter());
1206 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1207 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1208}
1209
1210// Test that when a PeerConnection is created with a nonzero candidate pool
1211// size, the pooled PortAllocatorSession is created with all the attributes
1212// in the RTCConfiguration.
1213TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1214 PeerConnectionInterface::RTCConfiguration config;
1215 PeerConnectionInterface::IceServer server;
1216 server.uri = kStunAddressOnly;
1217 config.servers.push_back(server);
1218 config.type = PeerConnectionInterface::kRelay;
1219 config.disable_ipv6 = true;
1220 config.tcp_candidate_policy =
1221 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001222 config.candidate_network_policy =
1223 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001224 config.ice_candidate_pool_size = 1;
1225 CreatePeerConnection(config, nullptr);
1226
1227 const cricket::FakePortAllocatorSession* session =
1228 static_cast<const cricket::FakePortAllocatorSession*>(
1229 port_allocator_->GetPooledSession());
1230 ASSERT_NE(nullptr, session);
1231 EXPECT_EQ(1UL, session->stun_servers().size());
1232 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1233 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001234 EXPECT_LT(0U,
1235 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001236}
1237
deadbeefd21eab32017-07-26 16:50:11 -07001238// Test that network-related RTCConfiguration members are applied to the
1239// PortAllocator when CreatePeerConnection is called. Specifically:
1240// - disable_ipv6_on_wifi
1241// - max_ipv6_networks
1242// - tcp_candidate_policy
1243// - candidate_network_policy
1244// - prune_turn_ports
1245//
1246// Note that the candidate filter (RTCConfiguration::type) is already tested
1247// above.
1248TEST_F(PeerConnectionInterfaceTest,
1249 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1250 // Create fake port allocator.
1251 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1252 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1253 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1254
1255 // Create RTCConfiguration with some network-related fields relevant to
1256 // PortAllocator populated.
1257 PeerConnectionInterface::RTCConfiguration config;
1258 config.disable_ipv6_on_wifi = true;
1259 config.max_ipv6_networks = 10;
1260 config.tcp_candidate_policy =
1261 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1262 config.candidate_network_policy =
1263 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1264 config.prune_turn_ports = true;
1265
1266 // Create the PC factory and PC with the above config.
1267 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1268 webrtc::CreatePeerConnectionFactory(
1269 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001270 rtc::Thread::Current(), fake_audio_capture_module_,
1271 webrtc::CreateBuiltinAudioEncoderFactory(),
1272 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001273 rtc::scoped_refptr<PeerConnectionInterface> pc(
1274 pc_factory->CreatePeerConnection(
1275 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1276
1277 // Now validate that the config fields set above were applied to the
1278 // PortAllocator, as flags or otherwise.
1279 EXPECT_FALSE(raw_port_allocator->flags() &
1280 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1281 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1282 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1283 EXPECT_TRUE(raw_port_allocator->flags() &
1284 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1285 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1286}
1287
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001288// Test that the PeerConnection initializes the port allocator passed into it,
1289// and on the correct thread.
1290TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001291 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001292 std::unique_ptr<rtc::Thread> network_thread(
1293 rtc::Thread::CreateWithSocketServer());
1294 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001295 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1296 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001297 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001298 fake_audio_capture_module_,
1299 webrtc::CreateBuiltinAudioEncoderFactory(),
1300 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001301 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001302 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001303 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1304 PeerConnectionInterface::RTCConfiguration config;
1305 rtc::scoped_refptr<PeerConnectionInterface> pc(
1306 pc_factory->CreatePeerConnection(
1307 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1308 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1309 // so all we have to do here is check that it's initialized.
1310 EXPECT_TRUE(raw_port_allocator->initialized());
1311}
1312
deadbeef46c73892016-11-16 19:42:04 -08001313// Check that GetConfiguration returns the configuration the PeerConnection was
1314// constructed with, before SetConfiguration is called.
1315TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1316 PeerConnectionInterface::RTCConfiguration config;
1317 config.type = PeerConnectionInterface::kRelay;
1318 CreatePeerConnection(config, nullptr);
1319
1320 PeerConnectionInterface::RTCConfiguration returned_config =
1321 pc_->GetConfiguration();
1322 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1323}
1324
1325// Check that GetConfiguration returns the last configuration passed into
1326// SetConfiguration.
1327TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1328 CreatePeerConnection();
1329
1330 PeerConnectionInterface::RTCConfiguration config;
1331 config.type = PeerConnectionInterface::kRelay;
1332 EXPECT_TRUE(pc_->SetConfiguration(config));
1333
1334 PeerConnectionInterface::RTCConfiguration returned_config =
1335 pc_->GetConfiguration();
1336 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1337}
1338
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001340 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001341 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 AddVoiceStream(kStreamLabel2);
1343 ASSERT_EQ(2u, pc_->local_streams()->count());
1344
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001345 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001346 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001348 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1349 pc_factory_->CreateAudioTrack(kStreamLabel3,
1350 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001352 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001353 EXPECT_EQ(3u, pc_->local_streams()->count());
1354
1355 // Remove the third stream.
1356 pc_->RemoveStream(pc_->local_streams()->at(2));
1357 EXPECT_EQ(2u, pc_->local_streams()->count());
1358
1359 // Remove the second stream.
1360 pc_->RemoveStream(pc_->local_streams()->at(1));
1361 EXPECT_EQ(1u, pc_->local_streams()->count());
1362
1363 // Remove the first stream.
1364 pc_->RemoveStream(pc_->local_streams()->at(0));
1365 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366}
1367
deadbeefab9b2d12015-10-14 11:33:11 -07001368// Test that the created offer includes streams we added.
1369TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001370 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001371 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001372 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001373 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001374
1375 const cricket::ContentInfo* audio_content =
1376 cricket::GetFirstAudioContent(offer->description());
1377 const cricket::AudioContentDescription* audio_desc =
1378 static_cast<const cricket::AudioContentDescription*>(
1379 audio_content->description);
1380 EXPECT_TRUE(
1381 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1382
1383 const cricket::ContentInfo* video_content =
1384 cricket::GetFirstVideoContent(offer->description());
1385 const cricket::VideoContentDescription* video_desc =
1386 static_cast<const cricket::VideoContentDescription*>(
1387 video_content->description);
1388 EXPECT_TRUE(
1389 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1390
1391 // Add another stream and ensure the offer includes both the old and new
1392 // streams.
1393 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001394 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001395
1396 audio_content = cricket::GetFirstAudioContent(offer->description());
1397 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1398 audio_content->description);
1399 EXPECT_TRUE(
1400 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1401 EXPECT_TRUE(
1402 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1403
1404 video_content = cricket::GetFirstVideoContent(offer->description());
1405 video_desc = static_cast<const cricket::VideoContentDescription*>(
1406 video_content->description);
1407 EXPECT_TRUE(
1408 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1409 EXPECT_TRUE(
1410 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1411}
1412
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001414 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001415 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 ASSERT_EQ(1u, pc_->local_streams()->count());
1417 pc_->RemoveStream(pc_->local_streams()->at(0));
1418 EXPECT_EQ(0u, pc_->local_streams()->count());
1419}
1420
deadbeefe1f9d832016-01-14 15:35:42 -08001421// Test for AddTrack and RemoveTrack methods.
1422// Tests that the created offer includes tracks we added,
1423// and that the RtpSenders are created correctly.
1424// Also tests that RemoveTrack removes the tracks from subsequent offers.
1425TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001426 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001427 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001428 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001429 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1430 std::vector<MediaStreamInterface*> stream_list;
1431 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001432 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001433 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001434 rtc::scoped_refptr<VideoTrackInterface> video_track(
1435 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001436 "video_track", pc_factory_->CreateVideoSource(
1437 std::unique_ptr<cricket::VideoCapturer>(
1438 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001439 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1440 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001441 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1442 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001443 EXPECT_EQ("audio_track", audio_sender->id());
1444 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001445 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1446 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001447 EXPECT_EQ("video_track", video_sender->id());
1448 EXPECT_EQ(video_track, video_sender->track());
1449
1450 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001451 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001452 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001453
1454 const cricket::ContentInfo* audio_content =
1455 cricket::GetFirstAudioContent(offer->description());
1456 const cricket::AudioContentDescription* audio_desc =
1457 static_cast<const cricket::AudioContentDescription*>(
1458 audio_content->description);
1459 EXPECT_TRUE(
1460 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1461
1462 const cricket::ContentInfo* video_content =
1463 cricket::GetFirstVideoContent(offer->description());
1464 const cricket::VideoContentDescription* video_desc =
1465 static_cast<const cricket::VideoContentDescription*>(
1466 video_content->description);
1467 EXPECT_TRUE(
1468 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1469
Steve Antondb45ca82017-09-11 18:27:34 -07001470 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001471
1472 // Now try removing the tracks.
1473 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1474 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1475
1476 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001477 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001478
1479 audio_content = cricket::GetFirstAudioContent(offer->description());
1480 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1481 audio_content->description);
1482 EXPECT_FALSE(
1483 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1484
1485 video_content = cricket::GetFirstVideoContent(offer->description());
1486 video_desc = static_cast<const cricket::VideoContentDescription*>(
1487 video_content->description);
1488 EXPECT_FALSE(
1489 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1490
Steve Antondb45ca82017-09-11 18:27:34 -07001491 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001492
1493 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1494 // should return false.
1495 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1496 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1497}
1498
1499// Test creating senders without a stream specified,
1500// expecting a random stream ID to be generated.
1501TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001502 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001503 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001504 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001505 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001506 rtc::scoped_refptr<VideoTrackInterface> video_track(
1507 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001508 "video_track", pc_factory_->CreateVideoSource(
1509 std::unique_ptr<cricket::VideoCapturer>(
1510 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001511 auto audio_sender =
1512 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1513 auto video_sender =
1514 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1515 EXPECT_EQ("audio_track", audio_sender->id());
1516 EXPECT_EQ(audio_track, audio_sender->track());
1517 EXPECT_EQ("video_track", video_sender->id());
1518 EXPECT_EQ(video_track, video_sender->track());
1519 // If the ID is truly a random GUID, it should be infinitely unlikely they
1520 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001521 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001522}
1523
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1525 InitiateCall();
1526 WaitAndVerifyOnAddStream(kStreamLabel1);
1527 VerifyRemoteRtpHeaderExtensions();
1528}
1529
1530TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001531 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001532 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 CreateOfferAsLocalDescription();
1534 std::string offer;
1535 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1536 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1537 WaitAndVerifyOnAddStream(kStreamLabel1);
1538}
1539
1540TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
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
1544 CreateOfferAsRemoteDescription();
1545 CreateAnswerAsLocalDescription();
1546
1547 WaitAndVerifyOnAddStream(kStreamLabel1);
1548}
1549
1550TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
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 CreatePrAnswerAsLocalDescription();
1556 CreateAnswerAsLocalDescription();
1557
1558 WaitAndVerifyOnAddStream(kStreamLabel1);
1559}
1560
1561TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1562 InitiateCall();
1563 ASSERT_EQ(1u, pc_->remote_streams()->count());
1564 pc_->RemoveStream(pc_->local_streams()->at(0));
1565 CreateOfferReceiveAnswer();
1566 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001567 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 CreateOfferReceiveAnswer();
1569}
1570
1571// Tests that after negotiating an audio only call, the respondent can perform a
1572// renegotiation that removes the audio stream.
1573TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001574 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 AddVoiceStream(kStreamLabel1);
1576 CreateOfferAsRemoteDescription();
1577 CreateAnswerAsLocalDescription();
1578
1579 ASSERT_EQ(1u, pc_->remote_streams()->count());
1580 pc_->RemoveStream(pc_->local_streams()->at(0));
1581 CreateOfferReceiveAnswer();
1582 EXPECT_EQ(0u, pc_->remote_streams()->count());
1583}
1584
1585// Test that candidates are generated and that we can parse our own candidates.
1586TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001587 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588
Steve Antonf1c6db12017-10-13 11:13:35 -07001589 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001591 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001592 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001593 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001594 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595
1596 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001597 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001598 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001599 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600
Steve Antonf1c6db12017-10-13 11:13:35 -07001601 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001602 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603
Steve Antonf1c6db12017-10-13 11:13:35 -07001604 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605}
1606
deadbeefab9b2d12015-10-14 11:33:11 -07001607// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608// not unique.
1609TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001610 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001612 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001613 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001614 EXPECT_TRUE(offer);
1615 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616
1617 // Create a local stream with audio&video tracks having same label.
1618 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1619
1620 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001621 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622
1623 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001624 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001625 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626}
1627
1628// Test that we will get different SSRCs for each tracks in the offer and answer
1629// we created.
1630TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001631 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 // Create a local stream with audio&video tracks having different labels.
1633 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1634
1635 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001636 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001637 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 int audio_ssrc = 0;
1639 int video_ssrc = 0;
1640 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1641 &audio_ssrc));
1642 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1643 &video_ssrc));
1644 EXPECT_NE(audio_ssrc, video_ssrc);
1645
1646 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001647 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001648 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001649 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 audio_ssrc = 0;
1651 video_ssrc = 0;
1652 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1653 &audio_ssrc));
1654 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1655 &video_ssrc));
1656 EXPECT_NE(audio_ssrc, video_ssrc);
1657}
1658
deadbeefeb459812015-12-15 19:24:43 -08001659// Test that it's possible to call AddTrack on a MediaStream after adding
1660// the stream to a PeerConnection.
1661// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1662TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001663 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001664 // Create audio stream and add to PeerConnection.
1665 AddVoiceStream(kStreamLabel1);
1666 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1667
1668 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001669 rtc::scoped_refptr<VideoTrackInterface> video_track(
1670 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001671 "video_label", pc_factory_->CreateVideoSource(
1672 std::unique_ptr<cricket::VideoCapturer>(
1673 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001674 stream->AddTrack(video_track.get());
1675
kwibergd1fe2812016-04-27 06:47:29 -07001676 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001677 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001678
1679 const cricket::MediaContentDescription* video_desc =
1680 cricket::GetFirstVideoContentDescription(offer->description());
1681 EXPECT_TRUE(video_desc != nullptr);
1682}
1683
1684// Test that it's possible to call RemoveTrack on a MediaStream after adding
1685// the stream to a PeerConnection.
1686// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1687TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001688 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001689 // Create audio/video stream and add to PeerConnection.
1690 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1691 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1692
1693 // Remove the video track.
1694 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1695
kwibergd1fe2812016-04-27 06:47:29 -07001696 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001697 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001698
1699 const cricket::MediaContentDescription* video_desc =
1700 cricket::GetFirstVideoContentDescription(offer->description());
1701 EXPECT_TRUE(video_desc == nullptr);
1702}
1703
deadbeef1dcb1642017-03-29 21:08:16 -07001704// Verify that CreateDtmfSender only succeeds if called with a valid local
1705// track. Other aspects of DtmfSenders are tested in
1706// peerconnection_integrationtest.cc.
1707TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1708 CreatePeerConnection();
1709 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1710 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1711 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1712 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1713 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1714}
1715
deadbeefbd7d8f72015-12-18 16:58:44 -08001716// Test creating a sender with a stream ID, and ensure the ID is populated
1717// in the offer.
1718TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001720 pc_->CreateSender("video", kStreamLabel1);
1721
kwibergd1fe2812016-04-27 06:47:29 -07001722 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001723 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001724
1725 const cricket::MediaContentDescription* video_desc =
1726 cricket::GetFirstVideoContentDescription(offer->description());
1727 ASSERT_TRUE(video_desc != nullptr);
1728 ASSERT_EQ(1u, video_desc->streams().size());
1729 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1730}
1731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732// Test that we can specify a certain track that we want statistics about.
1733TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1734 InitiateCall();
1735 ASSERT_LT(0u, pc_->remote_streams()->count());
1736 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001737 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1739 EXPECT_TRUE(DoGetStats(remote_audio));
1740
1741 // Remove the stream. Since we are sending to our selves the local
1742 // and the remote stream is the same.
1743 pc_->RemoveStream(pc_->local_streams()->at(0));
1744 // Do a re-negotiation.
1745 CreateOfferReceiveAnswer();
1746
1747 ASSERT_EQ(0u, pc_->remote_streams()->count());
1748
1749 // Test that we still can get statistics for the old track. Even if it is not
1750 // sent any longer.
1751 EXPECT_TRUE(DoGetStats(remote_audio));
1752}
1753
1754// Test that we can get stats on a video track.
1755TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1756 InitiateCall();
1757 ASSERT_LT(0u, pc_->remote_streams()->count());
1758 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001759 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1761 EXPECT_TRUE(DoGetStats(remote_video));
1762}
1763
1764// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001765TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001767 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 pc_factory_->CreateAudioTrack("unknown track", NULL));
1769 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1770}
1771
1772// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 FakeConstraints constraints;
1775 constraints.SetAllowRtpDataChannels();
1776 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001777 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001779 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 pc_->CreateDataChannel("test2", NULL);
1781 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001784 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 new MockDataChannelObserver(data2));
1786
1787 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1788 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1789 std::string data_to_send1 = "testing testing";
1790 std::string data_to_send2 = "testing something else";
1791 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1792
1793 CreateOfferReceiveAnswer();
1794 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1795 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1796
1797 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1798 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1799 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1800 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1801
1802 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1803 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1804
1805 data1->Close();
1806 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1807 CreateOfferReceiveAnswer();
1808 EXPECT_FALSE(observer1->IsOpen());
1809 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1810 EXPECT_TRUE(observer2->IsOpen());
1811
1812 data_to_send2 = "testing something else again";
1813 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1814
1815 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1816}
1817
1818// This test verifies that sendnig binary data over RTP data channels should
1819// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 FakeConstraints constraints;
1822 constraints.SetAllowRtpDataChannels();
1823 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001824 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001826 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 pc_->CreateDataChannel("test2", NULL);
1828 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001831 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 new MockDataChannelObserver(data2));
1833
1834 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1835 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1836
1837 CreateOfferReceiveAnswer();
1838 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1839 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1840
1841 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1842 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1843
jbaucheec21bd2016-03-20 06:15:43 -07001844 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1846}
1847
1848// This test setup a RTP data channels in loop back and test that a channel is
1849// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 FakeConstraints constraints;
1852 constraints.SetAllowRtpDataChannels();
1853 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001854 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001856 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 new MockDataChannelObserver(data1));
1858
1859 CreateOfferReceiveAnswerWithoutSsrc();
1860
1861 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1862
1863 data1->Close();
1864 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1865 CreateOfferReceiveAnswerWithoutSsrc();
1866 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1867 EXPECT_FALSE(observer1->IsOpen());
1868}
1869
1870// This test that if a data channel is added in an answer a receive only channel
1871// channel is created.
1872TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1873 FakeConstraints constraints;
1874 constraints.SetAllowRtpDataChannels();
1875 CreatePeerConnection(&constraints);
1876
1877 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001878 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 pc_->CreateDataChannel(offer_label, NULL);
1880
1881 CreateOfferAsLocalDescription();
1882
1883 // Replace the data channel label in the offer and apply it as an answer.
1884 std::string receive_label = "answer_channel";
1885 std::string sdp;
1886 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001887 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 receive_label.c_str(), receive_label.length(),
1889 &sdp);
1890 CreateAnswerAsRemoteDescription(sdp);
1891
1892 // Verify that a new incoming data channel has been created and that
1893 // it is open but can't we written to.
1894 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1895 DataChannelInterface* received_channel = observer_.last_datachannel_;
1896 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1897 EXPECT_EQ(receive_label, received_channel->label());
1898 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1899
1900 // Verify that the channel we initially offered has been rejected.
1901 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1902
1903 // Do another offer / answer exchange and verify that the data channel is
1904 // opened.
1905 CreateOfferReceiveAnswer();
1906 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1907 kTimeout);
1908}
1909
1910// This test that no data channel is returned if a reliable channel is
1911// requested.
1912// TODO(perkj): Remove this test once reliable channels are implemented.
1913TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1914 FakeConstraints constraints;
1915 constraints.SetAllowRtpDataChannels();
1916 CreatePeerConnection(&constraints);
1917
1918 std::string label = "test";
1919 webrtc::DataChannelInit config;
1920 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001921 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 pc_->CreateDataChannel(label, &config);
1923 EXPECT_TRUE(channel == NULL);
1924}
1925
deadbeefab9b2d12015-10-14 11:33:11 -07001926// Verifies that duplicated label is not allowed for RTP data channel.
1927TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1928 FakeConstraints constraints;
1929 constraints.SetAllowRtpDataChannels();
1930 CreatePeerConnection(&constraints);
1931
1932 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001933 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001934 pc_->CreateDataChannel(label, nullptr);
1935 EXPECT_NE(channel, nullptr);
1936
zhihuang9763d562016-08-05 11:14:50 -07001937 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001938 pc_->CreateDataChannel(label, nullptr);
1939 EXPECT_EQ(dup_channel, nullptr);
1940}
1941
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942// This tests that a SCTP data channel is returned using different
1943// DataChannelInit configurations.
1944TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1945 FakeConstraints constraints;
1946 constraints.SetAllowDtlsSctpDataChannels();
1947 CreatePeerConnection(&constraints);
1948
1949 webrtc::DataChannelInit config;
1950
zhihuang9763d562016-08-05 11:14:50 -07001951 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 pc_->CreateDataChannel("1", &config);
1953 EXPECT_TRUE(channel != NULL);
1954 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001955 EXPECT_TRUE(observer_.renegotiation_needed_);
1956 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957
1958 config.ordered = false;
1959 channel = pc_->CreateDataChannel("2", &config);
1960 EXPECT_TRUE(channel != NULL);
1961 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001962 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963
1964 config.ordered = true;
1965 config.maxRetransmits = 0;
1966 channel = pc_->CreateDataChannel("3", &config);
1967 EXPECT_TRUE(channel != NULL);
1968 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001969 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
1971 config.maxRetransmits = -1;
1972 config.maxRetransmitTime = 0;
1973 channel = pc_->CreateDataChannel("4", &config);
1974 EXPECT_TRUE(channel != NULL);
1975 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001976 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977}
1978
1979// This tests that no data channel is returned if both maxRetransmits and
1980// maxRetransmitTime are set for SCTP data channels.
1981TEST_F(PeerConnectionInterfaceTest,
1982 CreateSctpDataChannelShouldFailForInvalidConfig) {
1983 FakeConstraints constraints;
1984 constraints.SetAllowDtlsSctpDataChannels();
1985 CreatePeerConnection(&constraints);
1986
1987 std::string label = "test";
1988 webrtc::DataChannelInit config;
1989 config.maxRetransmits = 0;
1990 config.maxRetransmitTime = 0;
1991
zhihuang9763d562016-08-05 11:14:50 -07001992 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 pc_->CreateDataChannel(label, &config);
1994 EXPECT_TRUE(channel == NULL);
1995}
1996
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997// The test verifies that creating a SCTP data channel with an id already in use
1998// or out of range should fail.
1999TEST_F(PeerConnectionInterfaceTest,
2000 CreateSctpDataChannelWithInvalidIdShouldFail) {
2001 FakeConstraints constraints;
2002 constraints.SetAllowDtlsSctpDataChannels();
2003 CreatePeerConnection(&constraints);
2004
2005 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002006 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002008 config.id = 1;
2009 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 EXPECT_TRUE(channel != NULL);
2011 EXPECT_EQ(1, channel->id());
2012
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 channel = pc_->CreateDataChannel("x", &config);
2014 EXPECT_TRUE(channel == NULL);
2015
2016 config.id = cricket::kMaxSctpSid;
2017 channel = pc_->CreateDataChannel("max", &config);
2018 EXPECT_TRUE(channel != NULL);
2019 EXPECT_EQ(config.id, channel->id());
2020
2021 config.id = cricket::kMaxSctpSid + 1;
2022 channel = pc_->CreateDataChannel("x", &config);
2023 EXPECT_TRUE(channel == NULL);
2024}
2025
deadbeefab9b2d12015-10-14 11:33:11 -07002026// Verifies that duplicated label is allowed for SCTP data channel.
2027TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2028 FakeConstraints constraints;
2029 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2030 true);
2031 CreatePeerConnection(&constraints);
2032
2033 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002034 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002035 pc_->CreateDataChannel(label, nullptr);
2036 EXPECT_NE(channel, nullptr);
2037
zhihuang9763d562016-08-05 11:14:50 -07002038 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002039 pc_->CreateDataChannel(label, nullptr);
2040 EXPECT_NE(dup_channel, nullptr);
2041}
2042
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002043// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2044// DataChannel.
2045TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2046 FakeConstraints constraints;
2047 constraints.SetAllowRtpDataChannels();
2048 CreatePeerConnection(&constraints);
2049
zhihuang9763d562016-08-05 11:14:50 -07002050 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002051 pc_->CreateDataChannel("test1", NULL);
2052 EXPECT_TRUE(observer_.renegotiation_needed_);
2053 observer_.renegotiation_needed_ = false;
2054
zhihuang9763d562016-08-05 11:14:50 -07002055 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002056 pc_->CreateDataChannel("test2", NULL);
2057 EXPECT_TRUE(observer_.renegotiation_needed_);
2058}
2059
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 FakeConstraints constraints;
2063 constraints.SetAllowRtpDataChannels();
2064 CreatePeerConnection(&constraints);
2065
zhihuang9763d562016-08-05 11:14:50 -07002066 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002068 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 pc_->CreateDataChannel("test2", NULL);
2070 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002071 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002073 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 new MockDataChannelObserver(data2));
2075
2076 CreateOfferReceiveAnswer();
2077 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2078 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2079
2080 ReleasePeerConnection();
2081 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2082 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2083}
2084
2085// This test that data channels can be rejected in an answer.
2086TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2087 FakeConstraints constraints;
2088 constraints.SetAllowRtpDataChannels();
2089 CreatePeerConnection(&constraints);
2090
zhihuang9763d562016-08-05 11:14:50 -07002091 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 pc_->CreateDataChannel("offer_channel", NULL));
2093
2094 CreateOfferAsLocalDescription();
2095
2096 // Create an answer where the m-line for data channels are rejected.
2097 std::string sdp;
2098 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002099 std::unique_ptr<SessionDescriptionInterface> answer(
2100 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2101 sdp, nullptr));
2102 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 cricket::ContentInfo* data_info =
2104 answer->description()->GetContentByName("data");
2105 data_info->rejected = true;
2106
Steve Antondb45ca82017-09-11 18:27:34 -07002107 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2109}
2110
2111// Test that we can create a session description from an SDP string from
2112// FireFox, use it as a remote session description, generate an answer and use
2113// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002114TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 FakeConstraints constraints;
2116 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2117 true);
2118 CreatePeerConnection(&constraints);
2119 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002120 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002122 webrtc::kFireFoxSdpOffer, nullptr));
2123 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 CreateAnswerAsLocalDescription();
2125 ASSERT_TRUE(pc_->local_description() != NULL);
2126 ASSERT_TRUE(pc_->remote_description() != NULL);
2127
2128 const cricket::ContentInfo* content =
2129 cricket::GetFirstAudioContent(pc_->local_description()->description());
2130 ASSERT_TRUE(content != NULL);
2131 EXPECT_FALSE(content->rejected);
2132
2133 content =
2134 cricket::GetFirstVideoContent(pc_->local_description()->description());
2135 ASSERT_TRUE(content != NULL);
2136 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002137#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 content =
2139 cricket::GetFirstDataContent(pc_->local_description()->description());
2140 ASSERT_TRUE(content != NULL);
2141 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002142#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143}
2144
zhihuangb19012e2017-09-19 13:47:59 -07002145// Test that fallback from DTLS to SDES is not supported.
2146// The fallback was previously supported but was removed to simplify the code
2147// and because it's non-standard.
2148TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002149 FakeConstraints constraints;
2150 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2151 true);
2152 CreatePeerConnection(&constraints);
2153 // Wait for fake certificate to be generated. Previously, this is what caused
2154 // the "a=crypto" lines to be rejected.
2155 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2156 ASSERT_NE(nullptr, fake_certificate_generator_);
2157 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2158 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002159 std::unique_ptr<SessionDescriptionInterface> desc(
2160 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2161 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002162 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002163}
2164
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165// Test that we can create an audio only offer and receive an answer with a
2166// limited set of audio codecs and receive an updated offer with more audio
2167// codecs, where the added codecs are not supported.
2168TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002169 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 AddVoiceStream("audio_label");
2171 CreateOfferAsLocalDescription();
2172
Steve Antondb45ca82017-09-11 18:27:34 -07002173 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002175 webrtc::kAudioSdp, nullptr));
2176 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177
Steve Antondb45ca82017-09-11 18:27:34 -07002178 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002180 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002181 nullptr));
2182 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 CreateAnswerAsLocalDescription();
2184}
2185
deadbeefc80741f2015-10-22 13:14:45 -07002186// Test that if we're receiving (but not sending) a track, subsequent offers
2187// will have m-lines with a=recvonly.
2188TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2189 FakeConstraints constraints;
2190 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2191 true);
2192 CreatePeerConnection(&constraints);
2193 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2194 CreateAnswerAsLocalDescription();
2195
2196 // At this point we should be receiving stream 1, but not sending anything.
2197 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002198 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002199 DoCreateOffer(&offer, nullptr);
2200
2201 const cricket::ContentInfo* video_content =
2202 cricket::GetFirstVideoContent(offer->description());
2203 const cricket::VideoContentDescription* video_desc =
2204 static_cast<const cricket::VideoContentDescription*>(
2205 video_content->description);
2206 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2207
2208 const cricket::ContentInfo* audio_content =
2209 cricket::GetFirstAudioContent(offer->description());
2210 const cricket::AudioContentDescription* audio_desc =
2211 static_cast<const cricket::AudioContentDescription*>(
2212 audio_content->description);
2213 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2214}
2215
2216// Test that if we're receiving (but not sending) a track, and the
2217// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2218// false, the generated m-lines will be a=inactive.
2219TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2220 FakeConstraints constraints;
2221 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2222 true);
2223 CreatePeerConnection(&constraints);
2224 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2225 CreateAnswerAsLocalDescription();
2226
2227 // At this point we should be receiving stream 1, but not sending anything.
2228 // A new offer would be recvonly, but we'll set the "no receive" constraints
2229 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002230 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002231 FakeConstraints offer_constraints;
2232 offer_constraints.AddMandatory(
2233 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2234 offer_constraints.AddMandatory(
2235 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2236 DoCreateOffer(&offer, &offer_constraints);
2237
2238 const cricket::ContentInfo* video_content =
2239 cricket::GetFirstVideoContent(offer->description());
2240 const cricket::VideoContentDescription* video_desc =
2241 static_cast<const cricket::VideoContentDescription*>(
2242 video_content->description);
2243 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2244
2245 const cricket::ContentInfo* audio_content =
2246 cricket::GetFirstAudioContent(offer->description());
2247 const cricket::AudioContentDescription* audio_desc =
2248 static_cast<const cricket::AudioContentDescription*>(
2249 audio_content->description);
2250 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2251}
2252
deadbeef653b8e02015-11-11 12:55:10 -08002253// Test that we can use SetConfiguration to change the ICE servers of the
2254// PortAllocator.
2255TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2256 CreatePeerConnection();
2257
2258 PeerConnectionInterface::RTCConfiguration config;
2259 PeerConnectionInterface::IceServer server;
2260 server.uri = "stun:test_hostname";
2261 config.servers.push_back(server);
2262 EXPECT_TRUE(pc_->SetConfiguration(config));
2263
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002264 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2265 EXPECT_EQ("test_hostname",
2266 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002267}
2268
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002269TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2270 CreatePeerConnection();
2271 PeerConnectionInterface::RTCConfiguration config;
2272 config.type = PeerConnectionInterface::kRelay;
2273 EXPECT_TRUE(pc_->SetConfiguration(config));
2274 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2275}
2276
deadbeef293e9262017-01-11 12:28:30 -08002277TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2278 PeerConnectionInterface::RTCConfiguration config;
2279 config.prune_turn_ports = false;
2280 CreatePeerConnection(config, nullptr);
2281 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2282
2283 config.prune_turn_ports = true;
2284 EXPECT_TRUE(pc_->SetConfiguration(config));
2285 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2286}
2287
skvladd1f5fda2017-02-03 16:54:05 -08002288// Test that the ice check interval can be changed. This does not verify that
2289// the setting makes it all the way to P2PTransportChannel, as that would
2290// require a very complex set of mocks.
2291TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2292 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002293 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002294 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002295 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002296 EXPECT_TRUE(pc_->SetConfiguration(config));
2297 PeerConnectionInterface::RTCConfiguration new_config =
2298 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002299 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002300}
2301
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002302// Test that when SetConfiguration changes both the pool size and other
2303// attributes, the pooled session is created with the updated attributes.
2304TEST_F(PeerConnectionInterfaceTest,
2305 SetConfigurationCreatesPooledSessionCorrectly) {
2306 CreatePeerConnection();
2307 PeerConnectionInterface::RTCConfiguration config;
2308 config.ice_candidate_pool_size = 1;
2309 PeerConnectionInterface::IceServer server;
2310 server.uri = kStunAddressOnly;
2311 config.servers.push_back(server);
2312 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002313 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002314
2315 const cricket::FakePortAllocatorSession* session =
2316 static_cast<const cricket::FakePortAllocatorSession*>(
2317 port_allocator_->GetPooledSession());
2318 ASSERT_NE(nullptr, session);
2319 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002320}
2321
deadbeef293e9262017-01-11 12:28:30 -08002322// Test that after SetLocalDescription, changing the pool size is not allowed,
2323// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002324TEST_F(PeerConnectionInterfaceTest,
2325 CantChangePoolSizeAfterSetLocalDescription) {
2326 CreatePeerConnection();
2327 // Start by setting a size of 1.
2328 PeerConnectionInterface::RTCConfiguration config;
2329 config.ice_candidate_pool_size = 1;
2330 EXPECT_TRUE(pc_->SetConfiguration(config));
2331
2332 // Set remote offer; can still change pool size at this point.
2333 CreateOfferAsRemoteDescription();
2334 config.ice_candidate_pool_size = 2;
2335 EXPECT_TRUE(pc_->SetConfiguration(config));
2336
2337 // Set local answer; now it's too late.
2338 CreateAnswerAsLocalDescription();
2339 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002340 RTCError error;
2341 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2342 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2343}
2344
deadbeef42a42632017-03-10 15:18:00 -08002345// Test that after setting an answer, extra pooled sessions are discarded. The
2346// ICE candidate pool is only intended to be used for the first offer/answer.
2347TEST_F(PeerConnectionInterfaceTest,
2348 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2349 CreatePeerConnection();
2350
2351 // Set a larger-than-necessary size.
2352 PeerConnectionInterface::RTCConfiguration config;
2353 config.ice_candidate_pool_size = 4;
2354 EXPECT_TRUE(pc_->SetConfiguration(config));
2355
2356 // Do offer/answer.
2357 CreateOfferAsRemoteDescription();
2358 CreateAnswerAsLocalDescription();
2359
2360 // Expect no pooled sessions to be left.
2361 const cricket::PortAllocatorSession* session =
2362 port_allocator_->GetPooledSession();
2363 EXPECT_EQ(nullptr, session);
2364}
2365
2366// After Close is called, pooled candidates should be discarded so as to not
2367// waste network resources.
2368TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2369 CreatePeerConnection();
2370
2371 PeerConnectionInterface::RTCConfiguration config;
2372 config.ice_candidate_pool_size = 3;
2373 EXPECT_TRUE(pc_->SetConfiguration(config));
2374 pc_->Close();
2375
2376 // Expect no pooled sessions to be left.
2377 const cricket::PortAllocatorSession* session =
2378 port_allocator_->GetPooledSession();
2379 EXPECT_EQ(nullptr, session);
2380}
2381
deadbeef293e9262017-01-11 12:28:30 -08002382// Test that SetConfiguration returns an invalid modification error if
2383// modifying a field in the configuration that isn't allowed to be modified.
2384TEST_F(PeerConnectionInterfaceTest,
2385 SetConfigurationReturnsInvalidModificationError) {
2386 PeerConnectionInterface::RTCConfiguration config;
2387 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2388 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2389 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2390 CreatePeerConnection(config, nullptr);
2391
2392 PeerConnectionInterface::RTCConfiguration modified_config = config;
2393 modified_config.bundle_policy =
2394 PeerConnectionInterface::kBundlePolicyMaxBundle;
2395 RTCError error;
2396 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2397 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2398
2399 modified_config = config;
2400 modified_config.rtcp_mux_policy =
2401 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2402 error.set_type(RTCErrorType::NONE);
2403 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2404 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2405
2406 modified_config = config;
2407 modified_config.continual_gathering_policy =
2408 PeerConnectionInterface::GATHER_CONTINUALLY;
2409 error.set_type(RTCErrorType::NONE);
2410 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2411 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2412}
2413
2414// Test that SetConfiguration returns a range error if the candidate pool size
2415// is negative or larger than allowed by the spec.
2416TEST_F(PeerConnectionInterfaceTest,
2417 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2418 PeerConnectionInterface::RTCConfiguration config;
2419 CreatePeerConnection(config, nullptr);
2420
2421 config.ice_candidate_pool_size = -1;
2422 RTCError error;
2423 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2424 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2425
2426 config.ice_candidate_pool_size = INT_MAX;
2427 error.set_type(RTCErrorType::NONE);
2428 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2429 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2430}
2431
2432// Test that SetConfiguration returns a syntax error if parsing an ICE server
2433// URL failed.
2434TEST_F(PeerConnectionInterfaceTest,
2435 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2436 PeerConnectionInterface::RTCConfiguration config;
2437 CreatePeerConnection(config, nullptr);
2438
2439 PeerConnectionInterface::IceServer bad_server;
2440 bad_server.uri = "stunn:www.example.com";
2441 config.servers.push_back(bad_server);
2442 RTCError error;
2443 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2444 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2445}
2446
2447// Test that SetConfiguration returns an invalid parameter error if a TURN
2448// IceServer is missing a username or password.
2449TEST_F(PeerConnectionInterfaceTest,
2450 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2451 PeerConnectionInterface::RTCConfiguration config;
2452 CreatePeerConnection(config, nullptr);
2453
2454 PeerConnectionInterface::IceServer bad_server;
2455 bad_server.uri = "turn:www.example.com";
2456 // Missing password.
2457 bad_server.username = "foo";
2458 config.servers.push_back(bad_server);
2459 RTCError error;
2460 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2461 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002462}
2463
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464// Test that PeerConnection::Close changes the states to closed and all remote
2465// tracks change state to ended.
2466TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2467 // Initialize a PeerConnection and negotiate local and remote session
2468 // description.
2469 InitiateCall();
2470 ASSERT_EQ(1u, pc_->local_streams()->count());
2471 ASSERT_EQ(1u, pc_->remote_streams()->count());
2472
2473 pc_->Close();
2474
2475 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2476 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2477 pc_->ice_connection_state());
2478 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2479 pc_->ice_gathering_state());
2480
2481 EXPECT_EQ(1u, pc_->local_streams()->count());
2482 EXPECT_EQ(1u, pc_->remote_streams()->count());
2483
zhihuang9763d562016-08-05 11:14:50 -07002484 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2485 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002486 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002487 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002488 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2489 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2490 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491}
2492
2493// Test that PeerConnection methods fails gracefully after
2494// PeerConnection::Close has been called.
2495TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002496 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2498 CreateOfferAsRemoteDescription();
2499 CreateAnswerAsLocalDescription();
2500
2501 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002502 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 pc_->local_streams()->at(0);
2504
2505 pc_->Close();
2506
2507 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002508 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509
2510 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002511 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002513 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514
2515 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2516
2517 EXPECT_TRUE(pc_->local_description() != NULL);
2518 EXPECT_TRUE(pc_->remote_description() != NULL);
2519
kwibergd1fe2812016-04-27 06:47:29 -07002520 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002521 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002522 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002523 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524
2525 std::string sdp;
2526 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002527 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2528 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2529 nullptr));
2530 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002531
2532 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002533 std::unique_ptr<SessionDescriptionInterface> local_offer(
2534 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2535 nullptr));
2536 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537}
2538
2539// Test that GetStats can still be called after PeerConnection::Close.
2540TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2541 InitiateCall();
2542 pc_->Close();
2543 DoGetStats(NULL);
2544}
deadbeefab9b2d12015-10-14 11:33:11 -07002545
2546// NOTE: The series of tests below come from what used to be
2547// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2548// setting a remote or local description has the expected effects.
2549
2550// This test verifies that the remote MediaStreams corresponding to a received
2551// SDP string is created. In this test the two separate MediaStreams are
2552// signaled.
2553TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2554 FakeConstraints constraints;
2555 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2556 true);
2557 CreatePeerConnection(&constraints);
2558 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2559
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002560 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002561 EXPECT_TRUE(
2562 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2563 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2564 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2565
2566 // Create a session description based on another SDP with another
2567 // MediaStream.
2568 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2569
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002570 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002571 EXPECT_TRUE(
2572 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2573}
2574
2575// This test verifies that when remote tracks are added/removed from SDP, the
2576// created remote streams are updated appropriately.
2577TEST_F(PeerConnectionInterfaceTest,
2578 AddRemoveTrackFromExistingRemoteMediaStream) {
2579 FakeConstraints constraints;
2580 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2581 true);
2582 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002583 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002584 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002585 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002586 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2587 reference_collection_));
2588
2589 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002590 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002591 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002592 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002593 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2594 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002595 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002596 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2597 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002598 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002599 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2600 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002601
2602 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002603 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002604 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002605 MockTrackObserver audio_track_observer(audio_track2);
2606 MockTrackObserver video_track_observer(video_track2);
2607
2608 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2609 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002610 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002611 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2612 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002613 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002614 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002615 audio_track2->state(), kTimeout);
2616 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2617 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002618}
2619
2620// This tests that remote tracks are ended if a local session description is set
2621// that rejects the media content type.
2622TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2623 FakeConstraints constraints;
2624 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2625 true);
2626 CreatePeerConnection(&constraints);
2627 // First create and set a remote offer, then reject its video content in our
2628 // answer.
2629 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2630 ASSERT_EQ(1u, observer_.remote_streams()->count());
2631 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2632 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2633 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2634
2635 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2636 remote_stream->GetVideoTracks()[0];
2637 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2638 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2639 remote_stream->GetAudioTracks()[0];
2640 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2641
kwibergd1fe2812016-04-27 06:47:29 -07002642 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002643 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002644 cricket::ContentInfo* video_info =
2645 local_answer->description()->GetContentByName("video");
2646 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002647 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002648 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2649 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2650
2651 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002652 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002653 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002654 video_info = local_offer->description()->GetContentByName("video");
2655 ASSERT_TRUE(video_info != nullptr);
2656 video_info->rejected = true;
2657 cricket::ContentInfo* audio_info =
2658 local_offer->description()->GetContentByName("audio");
2659 ASSERT_TRUE(audio_info != nullptr);
2660 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002661 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002662 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002663 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002664 remote_audio->state(), kTimeout);
2665 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2666 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002667}
2668
2669// This tests that we won't crash if the remote track has been removed outside
2670// of PeerConnection and then PeerConnection tries to reject the track.
2671TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2672 FakeConstraints constraints;
2673 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2674 true);
2675 CreatePeerConnection(&constraints);
2676 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2677 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2678 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2679 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2680
kwibergd1fe2812016-04-27 06:47:29 -07002681 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002682 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2683 kSdpStringWithStream1, nullptr));
2684 cricket::ContentInfo* video_info =
2685 local_answer->description()->GetContentByName("video");
2686 video_info->rejected = true;
2687 cricket::ContentInfo* audio_info =
2688 local_answer->description()->GetContentByName("audio");
2689 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002690 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002691
2692 // No crash is a pass.
2693}
2694
deadbeef5e97fb52015-10-15 12:49:08 -07002695// This tests that if a recvonly remote description is set, no remote streams
2696// will be created, even if the description contains SSRCs/MSIDs.
2697// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2698TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2699 FakeConstraints constraints;
2700 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2701 true);
2702 CreatePeerConnection(&constraints);
2703
2704 std::string recvonly_offer = kSdpStringWithStream1;
2705 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2706 strlen(kRecvonly), &recvonly_offer);
2707 CreateAndSetRemoteOffer(recvonly_offer);
2708
2709 EXPECT_EQ(0u, observer_.remote_streams()->count());
2710}
2711
deadbeefab9b2d12015-10-14 11:33:11 -07002712// This tests that a default MediaStream is created if a remote session
2713// description doesn't contain any streams and no MSID support.
2714// It also tests that the default stream is updated if a video m-line is added
2715// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002716TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002717 FakeConstraints constraints;
2718 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2719 true);
2720 CreatePeerConnection(&constraints);
2721 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2722
2723 ASSERT_EQ(1u, observer_.remote_streams()->count());
2724 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2725
2726 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2727 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2728 EXPECT_EQ("default", remote_stream->label());
2729
2730 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2731 ASSERT_EQ(1u, observer_.remote_streams()->count());
2732 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2733 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002734 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2735 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002736 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2737 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002738 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2739 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002740}
2741
2742// This tests that a default MediaStream is created if a remote session
2743// description doesn't contain any streams and media direction is send only.
2744TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002745 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002746 FakeConstraints constraints;
2747 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2748 true);
2749 CreatePeerConnection(&constraints);
2750 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2751
2752 ASSERT_EQ(1u, observer_.remote_streams()->count());
2753 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2754
2755 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2756 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2757 EXPECT_EQ("default", remote_stream->label());
2758}
2759
2760// This tests that it won't crash when PeerConnection tries to remove
2761// a remote track that as already been removed from the MediaStream.
2762TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2763 FakeConstraints constraints;
2764 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2765 true);
2766 CreatePeerConnection(&constraints);
2767 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2768 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2769 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2770 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2771
2772 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2773
2774 // No crash is a pass.
2775}
2776
2777// This tests that a default MediaStream is created if the remote session
2778// description doesn't contain any streams and don't contain an indication if
2779// MSID is supported.
2780TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002781 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002782 FakeConstraints constraints;
2783 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2784 true);
2785 CreatePeerConnection(&constraints);
2786 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2787
2788 ASSERT_EQ(1u, observer_.remote_streams()->count());
2789 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2790 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2791 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2792}
2793
2794// This tests that a default MediaStream is not created if the remote session
2795// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002796TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002797 FakeConstraints constraints;
2798 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2799 true);
2800 CreatePeerConnection(&constraints);
2801 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2802 EXPECT_EQ(0u, observer_.remote_streams()->count());
2803}
2804
deadbeefbda7e0b2015-12-08 17:13:40 -08002805// This tests that when setting a new description, the old default tracks are
2806// not destroyed and recreated.
2807// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002808TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002809 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002810 FakeConstraints constraints;
2811 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2812 true);
2813 CreatePeerConnection(&constraints);
2814 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2815
2816 ASSERT_EQ(1u, observer_.remote_streams()->count());
2817 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2818 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2819
2820 // Set the track to "disabled", then set a new description and ensure the
2821 // track is still disabled, which ensures it hasn't been recreated.
2822 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2823 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2824 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2825 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2826}
2827
deadbeefab9b2d12015-10-14 11:33:11 -07002828// This tests that a default MediaStream is not created if a remote session
2829// description is updated to not have any MediaStreams.
2830TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2831 FakeConstraints constraints;
2832 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2833 true);
2834 CreatePeerConnection(&constraints);
2835 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002836 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002837 EXPECT_TRUE(
2838 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2839
2840 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2841 EXPECT_EQ(0u, observer_.remote_streams()->count());
2842}
2843
2844// This tests that an RtpSender is created when the local description is set
2845// after adding a local stream.
2846// TODO(deadbeef): This test and the one below it need to be updated when
2847// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002848TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002849 FakeConstraints constraints;
2850 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2851 true);
2852 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002853
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002854 // Create an offer with 1 stream with 2 tracks of each type.
2855 rtc::scoped_refptr<StreamCollection> stream_collection =
2856 CreateStreamCollection(1, 2);
2857 pc_->AddStream(stream_collection->at(0));
2858 std::unique_ptr<SessionDescriptionInterface> offer;
2859 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002860 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002861
deadbeefab9b2d12015-10-14 11:33:11 -07002862 auto senders = pc_->GetSenders();
2863 EXPECT_EQ(4u, senders.size());
2864 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2865 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2866 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2867 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2868
2869 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002870 pc_->RemoveStream(stream_collection->at(0));
2871 stream_collection = CreateStreamCollection(1, 1);
2872 pc_->AddStream(stream_collection->at(0));
2873 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002874 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002875
deadbeefab9b2d12015-10-14 11:33:11 -07002876 senders = pc_->GetSenders();
2877 EXPECT_EQ(2u, senders.size());
2878 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2879 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2880 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2881 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2882}
2883
2884// This tests that an RtpSender is created when the local description is set
2885// before adding a local stream.
2886TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002887 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002888 FakeConstraints constraints;
2889 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2890 true);
2891 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002892
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002893 rtc::scoped_refptr<StreamCollection> stream_collection =
2894 CreateStreamCollection(1, 2);
2895 // Add a stream to create the offer, but remove it afterwards.
2896 pc_->AddStream(stream_collection->at(0));
2897 std::unique_ptr<SessionDescriptionInterface> offer;
2898 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2899 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002900
Steve Antondb45ca82017-09-11 18:27:34 -07002901 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002902 auto senders = pc_->GetSenders();
2903 EXPECT_EQ(0u, senders.size());
2904
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002905 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002906 senders = pc_->GetSenders();
2907 EXPECT_EQ(4u, senders.size());
2908 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2909 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2910 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2911 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2912}
2913
2914// This tests that the expected behavior occurs if the SSRC on a local track is
2915// changed when SetLocalDescription is called.
2916TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002917 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002918 FakeConstraints constraints;
2919 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2920 true);
2921 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002922
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002923 rtc::scoped_refptr<StreamCollection> stream_collection =
2924 CreateStreamCollection(2, 1);
2925 pc_->AddStream(stream_collection->at(0));
2926 std::unique_ptr<SessionDescriptionInterface> offer;
2927 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2928 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002929 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2930 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2932 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002933 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002934
deadbeefab9b2d12015-10-14 11:33:11 -07002935 auto senders = pc_->GetSenders();
2936 EXPECT_EQ(2u, senders.size());
2937 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2938 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2939
2940 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002941 cricket::MediaContentDescription* desc =
2942 cricket::GetFirstAudioContentDescription(modified_offer->description());
2943 ASSERT_TRUE(desc != NULL);
2944 for (StreamParams& stream : desc->mutable_streams()) {
2945 for (unsigned int& ssrc : stream.ssrcs) {
2946 ++ssrc;
2947 }
2948 }
deadbeefab9b2d12015-10-14 11:33:11 -07002949
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002950 desc =
2951 cricket::GetFirstVideoContentDescription(modified_offer->description());
2952 ASSERT_TRUE(desc != NULL);
2953 for (StreamParams& stream : desc->mutable_streams()) {
2954 for (unsigned int& ssrc : stream.ssrcs) {
2955 ++ssrc;
2956 }
2957 }
2958
Steve Antondb45ca82017-09-11 18:27:34 -07002959 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002960 senders = pc_->GetSenders();
2961 EXPECT_EQ(2u, senders.size());
2962 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2963 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2964 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2965 // changed.
2966}
2967
2968// This tests that the expected behavior occurs if a new session description is
2969// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002970TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002971 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002972 FakeConstraints constraints;
2973 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2974 true);
2975 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002976
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002977 rtc::scoped_refptr<StreamCollection> stream_collection =
2978 CreateStreamCollection(2, 1);
2979 pc_->AddStream(stream_collection->at(0));
2980 std::unique_ptr<SessionDescriptionInterface> offer;
2981 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002982 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002983
deadbeefab9b2d12015-10-14 11:33:11 -07002984 auto senders = pc_->GetSenders();
2985 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2987 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002988
2989 // Add a new MediaStream but with the same tracks as in the first stream.
2990 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2991 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002992 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2993 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002994 pc_->AddStream(stream_1);
2995
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002996 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002997 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002998
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002999 auto new_senders = pc_->GetSenders();
3000 // Should be the same senders as before, but with updated stream id.
3001 // Note that this behavior is subject to change in the future.
3002 // We may decide the PC should ignore existing tracks in AddStream.
3003 EXPECT_EQ(senders, new_senders);
3004 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3005 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003006}
3007
zhihuang81c3a032016-11-17 12:06:24 -08003008// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3009TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3010 FakeConstraints constraints;
3011 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3012 true);
3013 CreatePeerConnection(&constraints);
3014 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3015 EXPECT_EQ(observer_.num_added_tracks_, 1);
3016 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3017
3018 // Create and set the updated remote SDP.
3019 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3020 EXPECT_EQ(observer_.num_added_tracks_, 2);
3021 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3022}
3023
deadbeefd1a38b52016-12-10 13:15:33 -08003024// Test that when SetConfiguration is called and the configuration is
3025// changing, the next offer causes an ICE restart.
3026TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3027 PeerConnectionInterface::RTCConfiguration config;
3028 config.type = PeerConnectionInterface::kRelay;
3029 // Need to pass default constraints to prevent disabling of DTLS...
3030 FakeConstraints default_constraints;
3031 CreatePeerConnection(config, &default_constraints);
3032 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3033
3034 // Do initial offer/answer so there's something to restart.
3035 CreateOfferAsLocalDescription();
3036 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3037
3038 // Grab the ufrags.
3039 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3040
3041 // Change ICE policy, which should trigger an ICE restart on the next offer.
3042 config.type = PeerConnectionInterface::kAll;
3043 EXPECT_TRUE(pc_->SetConfiguration(config));
3044 CreateOfferAsLocalDescription();
3045
3046 // Grab the new ufrags.
3047 std::vector<std::string> subsequent_ufrags =
3048 GetUfrags(pc_->local_description());
3049
3050 // Sanity check.
3051 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3052 // Check that each ufrag is different.
3053 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3054 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3055 }
3056}
3057
3058// Test that when SetConfiguration is called and the configuration *isn't*
3059// changing, the next offer does *not* cause an ICE restart.
3060TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3061 PeerConnectionInterface::RTCConfiguration config;
3062 config.type = PeerConnectionInterface::kRelay;
3063 // Need to pass default constraints to prevent disabling of DTLS...
3064 FakeConstraints default_constraints;
3065 CreatePeerConnection(config, &default_constraints);
3066 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3067
3068 // Do initial offer/answer so there's something to restart.
3069 CreateOfferAsLocalDescription();
3070 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3071
3072 // Grab the ufrags.
3073 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3074
3075 // Call SetConfiguration with a config identical to what the PC was
3076 // constructed with.
3077 EXPECT_TRUE(pc_->SetConfiguration(config));
3078 CreateOfferAsLocalDescription();
3079
3080 // Grab the new ufrags.
3081 std::vector<std::string> subsequent_ufrags =
3082 GetUfrags(pc_->local_description());
3083
3084 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3085}
3086
3087// Test for a weird corner case scenario:
3088// 1. Audio/video session established.
3089// 2. SetConfiguration changes ICE config; ICE restart needed.
3090// 3. ICE restart initiated by remote peer, but only for one m= section.
3091// 4. Next createOffer should initiate an ICE restart, but only for the other
3092// m= section; it would be pointless to do an ICE restart for the m= section
3093// that was already restarted.
3094TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3095 PeerConnectionInterface::RTCConfiguration config;
3096 config.type = PeerConnectionInterface::kRelay;
3097 // Need to pass default constraints to prevent disabling of DTLS...
3098 FakeConstraints default_constraints;
3099 CreatePeerConnection(config, &default_constraints);
3100 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3101
3102 // Do initial offer/answer so there's something to restart.
3103 CreateOfferAsLocalDescription();
3104 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3105
3106 // Change ICE policy, which should set the "needs-ice-restart" flag.
3107 config.type = PeerConnectionInterface::kAll;
3108 EXPECT_TRUE(pc_->SetConfiguration(config));
3109
3110 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003111 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3112 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3113 kSdpStringWithStream1, nullptr));
3114 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003115 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3116 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003117 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003118 CreateAnswerAsLocalDescription();
3119
3120 // Grab the ufrags.
3121 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3122 ASSERT_EQ(2, initial_ufrags.size());
3123
3124 // Create offer and grab the new ufrags.
3125 CreateOfferAsLocalDescription();
3126 std::vector<std::string> subsequent_ufrags =
3127 GetUfrags(pc_->local_description());
3128 ASSERT_EQ(2, subsequent_ufrags.size());
3129
3130 // Ensure that only the ufrag for the second m= section changed.
3131 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3132 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3133}
3134
deadbeeffe4a8a42016-12-20 17:56:17 -08003135// Tests that the methods to return current/pending descriptions work as
3136// expected at different points in the offer/answer exchange. This test does
3137// one offer/answer exchange as the offerer, then another as the answerer.
3138TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3139 // This disables DTLS so we can apply an answer to ourselves.
3140 CreatePeerConnection();
3141
3142 // Create initial local offer and get SDP (which will also be used as
3143 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003144 std::unique_ptr<SessionDescriptionInterface> local_offer;
3145 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003146 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003147 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003148
3149 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003150 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3151 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3152 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003153 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3154 EXPECT_EQ(nullptr, pc_->current_local_description());
3155 EXPECT_EQ(nullptr, pc_->current_remote_description());
3156
3157 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003158 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003160 sdp, nullptr));
3161 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3162 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3163 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3164 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003165 EXPECT_EQ(nullptr, pc_->current_local_description());
3166 EXPECT_EQ(nullptr, pc_->current_remote_description());
3167
3168 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003169 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3170 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3171 sdp, nullptr));
3172 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3173 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003174 EXPECT_EQ(nullptr, pc_->pending_local_description());
3175 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003176 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3177 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003178
3179 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003180 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3181 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3182 nullptr));
3183 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3184 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3185 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003186 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003187 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3188 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003189
3190 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003191 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003192 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003193 sdp, nullptr));
3194 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3195 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3196 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3197 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3198 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3199 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003200
3201 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003202 std::unique_ptr<SessionDescriptionInterface> local_answer(
3203 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3204 sdp, nullptr));
3205 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3206 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003207 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3208 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003209 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3210 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003211}
3212
zhihuang77985012017-02-07 15:45:16 -08003213// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3214// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003215// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003216TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003217 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003218 CreatePeerConnection();
3219 // The RtcEventLog will be reset when the PeerConnection is closed.
3220 pc_->Close();
3221
Elad Alon9e6565b2017-10-11 16:04:13 +02003222 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3223 std::string filename = webrtc::test::OutputPath() +
3224 test_info->test_case_name() + test_info->name();
3225 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3226
3227 constexpr int64_t max_size_bytes = 1024;
3228
zhihuang77985012017-02-07 15:45:16 -08003229 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3230 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003231
3232 // Cleanup.
3233 rtc::ClosePlatformFile(file);
3234 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003235}
3236
Elad Alon99c3fe52017-10-13 16:29:40 +02003237// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3238// after the PeerConnection is closed.
3239// This version tests the StartRtcEventLog version that receives an object
3240// of type |RtcEventLogOutput|.
3241TEST_F(PeerConnectionInterfaceTest,
3242 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3243 CreatePeerConnection();
3244 // The RtcEventLog will be reset when the PeerConnection is closed.
3245 pc_->Close();
3246
3247 rtc::PlatformFile file = 0;
3248 int64_t max_size_bytes = 1024;
3249 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003250 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3251 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003252 pc_->StopRtcEventLog();
3253}
3254
deadbeef30952b42017-04-21 02:41:29 -07003255// Test that generated offers/answers include "ice-option:trickle".
3256TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3257 CreatePeerConnection();
3258
3259 // First, create an offer with audio/video.
3260 FakeConstraints constraints;
3261 constraints.SetMandatoryReceiveAudio(true);
3262 constraints.SetMandatoryReceiveVideo(true);
3263 std::unique_ptr<SessionDescriptionInterface> offer;
3264 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3265 cricket::SessionDescription* desc = offer->description();
3266 ASSERT_EQ(2u, desc->transport_infos().size());
3267 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3268 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3269
3270 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003271 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003272 std::unique_ptr<SessionDescriptionInterface> answer;
3273 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3274 desc = answer->description();
3275 ASSERT_EQ(2u, desc->transport_infos().size());
3276 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3277 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3278}
3279
deadbeef1dcb1642017-03-29 21:08:16 -07003280// Test that ICE renomination isn't offered if it's not enabled in the PC's
3281// RTCConfiguration.
3282TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3283 PeerConnectionInterface::RTCConfiguration config;
3284 config.enable_ice_renomination = false;
3285 CreatePeerConnection(config, nullptr);
3286 AddVoiceStream("foo");
3287
3288 std::unique_ptr<SessionDescriptionInterface> offer;
3289 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3290 cricket::SessionDescription* desc = offer->description();
3291 EXPECT_EQ(1u, desc->transport_infos().size());
3292 EXPECT_FALSE(
3293 desc->transport_infos()[0].description.GetIceParameters().renomination);
3294}
3295
3296// Test that the ICE renomination option is present in generated offers/answers
3297// if it's enabled in the PC's RTCConfiguration.
3298TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3299 PeerConnectionInterface::RTCConfiguration config;
3300 config.enable_ice_renomination = true;
3301 CreatePeerConnection(config, nullptr);
3302 AddVoiceStream("foo");
3303
3304 std::unique_ptr<SessionDescriptionInterface> offer;
3305 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3306 cricket::SessionDescription* desc = offer->description();
3307 EXPECT_EQ(1u, desc->transport_infos().size());
3308 EXPECT_TRUE(
3309 desc->transport_infos()[0].description.GetIceParameters().renomination);
3310
3311 // Set the offer as a remote description, then create an answer and ensure it
3312 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003313 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003314 std::unique_ptr<SessionDescriptionInterface> answer;
3315 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3316 desc = answer->description();
3317 EXPECT_EQ(1u, desc->transport_infos().size());
3318 EXPECT_TRUE(
3319 desc->transport_infos()[0].description.GetIceParameters().renomination);
3320}
3321
3322// Test that if CreateOffer is called with the deprecated "offer to receive
3323// audio/video" constraints, they're processed and result in an offer with
3324// audio/video sections just as if RTCOfferAnswerOptions had been used.
3325TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3326 CreatePeerConnection();
3327
3328 FakeConstraints constraints;
3329 constraints.SetMandatoryReceiveAudio(true);
3330 constraints.SetMandatoryReceiveVideo(true);
3331 std::unique_ptr<SessionDescriptionInterface> offer;
3332 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3333
3334 cricket::SessionDescription* desc = offer->description();
3335 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3336 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3337 ASSERT_NE(nullptr, audio);
3338 ASSERT_NE(nullptr, video);
3339 EXPECT_FALSE(audio->rejected);
3340 EXPECT_FALSE(video->rejected);
3341}
3342
3343// Test that if CreateAnswer is called with the deprecated "offer to receive
3344// audio/video" constraints, they're processed and can be used to reject an
3345// offered m= section just as can be done with RTCOfferAnswerOptions;
3346TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3347 CreatePeerConnection();
3348
3349 // First, create an offer with audio/video and apply it as a remote
3350 // description.
3351 FakeConstraints constraints;
3352 constraints.SetMandatoryReceiveAudio(true);
3353 constraints.SetMandatoryReceiveVideo(true);
3354 std::unique_ptr<SessionDescriptionInterface> offer;
3355 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003356 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003357
3358 // Now create answer that rejects audio/video.
3359 constraints.SetMandatoryReceiveAudio(false);
3360 constraints.SetMandatoryReceiveVideo(false);
3361 std::unique_ptr<SessionDescriptionInterface> answer;
3362 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3363
3364 cricket::SessionDescription* desc = answer->description();
3365 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3366 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3367 ASSERT_NE(nullptr, audio);
3368 ASSERT_NE(nullptr, video);
3369 EXPECT_TRUE(audio->rejected);
3370 EXPECT_TRUE(video->rejected);
3371}
3372
3373#ifdef HAVE_SCTP
3374#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3375 DataChannelOnlyOfferWithMaxBundlePolicy
3376#else
3377#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3378 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3379#endif
3380
3381// Test that negotiation can succeed with a data channel only, and with the max
3382// bundle policy. Previously there was a bug that prevented this.
3383TEST_F(PeerConnectionInterfaceTest,
3384 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3385 PeerConnectionInterface::RTCConfiguration config;
3386 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3387 CreatePeerConnection(config, nullptr);
3388
3389 // First, create an offer with only a data channel and apply it as a remote
3390 // description.
3391 pc_->CreateDataChannel("test", nullptr);
3392 std::unique_ptr<SessionDescriptionInterface> offer;
3393 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003394 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003395
3396 // Create and set answer as well.
3397 std::unique_ptr<SessionDescriptionInterface> answer;
3398 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003399 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003400}
3401
zstein4b979802017-06-02 14:37:37 -07003402TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3403 CreatePeerConnection();
3404 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003405 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003406 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3407}
3408
3409TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3410 CreatePeerConnection();
3411 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003412 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003413 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3414}
3415
3416TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3417 CreatePeerConnection();
3418 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003419 bitrate.min_bitrate_bps = 5;
3420 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003421 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3422}
3423
3424TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3425 CreatePeerConnection();
3426 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003427 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003428 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3429}
3430
3431TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3432 CreatePeerConnection();
3433 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003434 bitrate.current_bitrate_bps = 10;
3435 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003436 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3437}
3438
3439TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3440 CreatePeerConnection();
3441 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003442 bitrate.min_bitrate_bps = 10;
3443 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003444 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3445}
3446
3447TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3448 CreatePeerConnection();
3449 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003450 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003451 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3452}
3453
Steve Anton038834f2017-07-14 15:59:59 -07003454// ice_regather_interval_range requires WebRTC to be configured for continual
3455// gathering already.
3456TEST_F(PeerConnectionInterfaceTest,
3457 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3458 PeerConnectionInterface::RTCConfiguration config;
3459 config.ice_regather_interval_range.emplace(1000, 2000);
3460 config.continual_gathering_policy =
3461 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3462 CreatePeerConnectionExpectFail(config);
3463}
3464
3465// Ensures that there is no error when ice_regather_interval_range is set with
3466// continual gathering enabled.
3467TEST_F(PeerConnectionInterfaceTest,
3468 SetIceRegatherIntervalRangeWithContinualGathering) {
3469 PeerConnectionInterface::RTCConfiguration config;
3470 config.ice_regather_interval_range.emplace(1000, 2000);
3471 config.continual_gathering_policy =
3472 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3473 CreatePeerConnection(config, nullptr);
3474}
3475
zstein4b979802017-06-02 14:37:37 -07003476// The current bitrate from Call's BitrateConfigMask is currently clamped by
3477// Call's BitrateConfig, which comes from the SDP or a default value. This test
3478// checks that a call to SetBitrate with a current bitrate that will be clamped
3479// succeeds.
3480TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3481 CreatePeerConnection();
3482 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003483 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003484 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3485}
3486
zhihuang1c378ed2017-08-17 14:10:50 -07003487// The following tests verify that the offer can be created correctly.
3488TEST_F(PeerConnectionInterfaceTest,
3489 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3490 RTCOfferAnswerOptions rtc_options;
3491
3492 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3493 // than kMaxOfferToReceiveMedia should be treated as invalid.
3494 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3495 CreatePeerConnection();
3496 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3497
3498 rtc_options.offer_to_receive_audio =
3499 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3500 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3501}
3502
3503TEST_F(PeerConnectionInterfaceTest,
3504 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3505 RTCOfferAnswerOptions rtc_options;
3506
3507 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3508 // than kMaxOfferToReceiveMedia should be treated as invalid.
3509 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3510 CreatePeerConnection();
3511 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3512
3513 rtc_options.offer_to_receive_video =
3514 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3515 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3516}
3517
3518// Test that the audio and video content will be added to an offer if both
3519// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3520TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3521 RTCOfferAnswerOptions rtc_options;
3522 rtc_options.offer_to_receive_audio = 1;
3523 rtc_options.offer_to_receive_video = 1;
3524
3525 std::unique_ptr<SessionDescriptionInterface> offer;
3526 CreatePeerConnection();
3527 offer = CreateOfferWithOptions(rtc_options);
3528 ASSERT_TRUE(offer);
3529 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3530 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3531}
3532
3533// Test that only audio content will be added to the offer if only
3534// |offer_to_receive_audio| options is 1.
3535TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3536 RTCOfferAnswerOptions rtc_options;
3537 rtc_options.offer_to_receive_audio = 1;
3538 rtc_options.offer_to_receive_video = 0;
3539
3540 std::unique_ptr<SessionDescriptionInterface> offer;
3541 CreatePeerConnection();
3542 offer = CreateOfferWithOptions(rtc_options);
3543 ASSERT_TRUE(offer);
3544 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3545 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3546}
3547
3548// Test that only video content will be added if only |offer_to_receive_video|
3549// options is 1.
3550TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3551 RTCOfferAnswerOptions rtc_options;
3552 rtc_options.offer_to_receive_audio = 0;
3553 rtc_options.offer_to_receive_video = 1;
3554
3555 std::unique_ptr<SessionDescriptionInterface> offer;
3556 CreatePeerConnection();
3557 offer = CreateOfferWithOptions(rtc_options);
3558 ASSERT_TRUE(offer);
3559 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3560 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3561}
3562
zhihuang1c378ed2017-08-17 14:10:50 -07003563// Test that no media content will be added to the offer if using default
3564// RTCOfferAnswerOptions.
3565TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3566 RTCOfferAnswerOptions rtc_options;
3567
3568 std::unique_ptr<SessionDescriptionInterface> offer;
3569 CreatePeerConnection();
3570 offer = CreateOfferWithOptions(rtc_options);
3571 ASSERT_TRUE(offer);
3572 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3573 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3574}
3575
3576// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3577// ufrag/pwd will be the same in the new offer.
3578TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3579 RTCOfferAnswerOptions rtc_options;
3580 rtc_options.ice_restart = false;
3581 rtc_options.offer_to_receive_audio = 1;
3582
3583 std::unique_ptr<SessionDescriptionInterface> offer;
3584 CreatePeerConnection();
3585 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3586 auto ufrag1 = offer->description()
3587 ->GetTransportInfoByName(cricket::CN_AUDIO)
3588 ->description.ice_ufrag;
3589 auto pwd1 = offer->description()
3590 ->GetTransportInfoByName(cricket::CN_AUDIO)
3591 ->description.ice_pwd;
3592
3593 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3594 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3595 auto ufrag2 = offer->description()
3596 ->GetTransportInfoByName(cricket::CN_AUDIO)
3597 ->description.ice_ufrag;
3598 auto pwd2 = offer->description()
3599 ->GetTransportInfoByName(cricket::CN_AUDIO)
3600 ->description.ice_pwd;
3601
3602 // |ice_restart| is true, the ufrag/pwd should change.
3603 rtc_options.ice_restart = true;
3604 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3605 auto ufrag3 = offer->description()
3606 ->GetTransportInfoByName(cricket::CN_AUDIO)
3607 ->description.ice_ufrag;
3608 auto pwd3 = offer->description()
3609 ->GetTransportInfoByName(cricket::CN_AUDIO)
3610 ->description.ice_pwd;
3611
3612 EXPECT_EQ(ufrag1, ufrag2);
3613 EXPECT_EQ(pwd1, pwd2);
3614 EXPECT_NE(ufrag2, ufrag3);
3615 EXPECT_NE(pwd2, pwd3);
3616}
3617
3618// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3619// offer; if it is false, there won't be any bundle group in the offer.
3620TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3621 RTCOfferAnswerOptions rtc_options;
3622 rtc_options.offer_to_receive_audio = 1;
3623 rtc_options.offer_to_receive_video = 1;
3624
3625 std::unique_ptr<SessionDescriptionInterface> offer;
3626 CreatePeerConnection();
3627
3628 rtc_options.use_rtp_mux = true;
3629 offer = CreateOfferWithOptions(rtc_options);
3630 ASSERT_TRUE(offer);
3631 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3632 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3633 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3634
3635 rtc_options.use_rtp_mux = false;
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_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3641}
3642
zhihuang141aacb2017-08-29 13:23:53 -07003643// This test ensures OnRenegotiationNeeded is called when we add track with
3644// MediaStream -> AddTrack in the same way it is called when we add track with
3645// PeerConnection -> AddTrack.
3646// The test can be removed once addStream is rewritten in terms of addTrack
3647// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3648TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3649 CreatePeerConnectionWithoutDtls();
3650 rtc::scoped_refptr<MediaStreamInterface> stream(
3651 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3652 pc_->AddStream(stream);
3653 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3654 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3655 rtc::scoped_refptr<VideoTrackInterface> video_track(
3656 pc_factory_->CreateVideoTrack(
3657 "video_track", pc_factory_->CreateVideoSource(
3658 std::unique_ptr<cricket::VideoCapturer>(
3659 new cricket::FakeVideoCapturer()))));
3660 stream->AddTrack(audio_track);
3661 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3662 observer_.renegotiation_needed_ = false;
3663
3664 stream->AddTrack(video_track);
3665 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3666 observer_.renegotiation_needed_ = false;
3667
3668 stream->RemoveTrack(audio_track);
3669 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3670 observer_.renegotiation_needed_ = false;
3671
3672 stream->RemoveTrack(video_track);
3673 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3674 observer_.renegotiation_needed_ = false;
3675}
3676
Zhi Huangb2d355e2017-10-26 17:26:37 -07003677// Tests that an error is returned if a description is applied that has fewer
3678// media sections than the existing description.
3679TEST_F(PeerConnectionInterfaceTest,
3680 MediaSectionCountEnforcedForSubsequentOffer) {
3681 CreatePeerConnection();
3682 FakeConstraints constraints;
3683 constraints.SetMandatoryReceiveAudio(true);
3684 constraints.SetMandatoryReceiveVideo(true);
3685 std::unique_ptr<SessionDescriptionInterface> offer;
3686 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3687 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3688
3689 // A remote offer with fewer media sections should be rejected.
3690 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3691 offer->description()->contents().pop_back();
3692 offer->description()->contents().pop_back();
3693 ASSERT_TRUE(offer->description()->contents().empty());
3694 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3695
3696 std::unique_ptr<SessionDescriptionInterface> answer;
3697 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3698 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3699
3700 // A subsequent local offer with fewer media sections should be rejected.
3701 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3702 offer->description()->contents().pop_back();
3703 offer->description()->contents().pop_back();
3704 ASSERT_TRUE(offer->description()->contents().empty());
3705 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3706}
3707
nisse51542be2016-02-12 02:27:06 -08003708class PeerConnectionMediaConfigTest : public testing::Test {
3709 protected:
3710 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003711 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003712 pcf_->Initialize();
3713 }
nisseeaabdf62017-05-05 02:23:02 -07003714 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003715 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003716 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003717 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3718 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003719 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003720 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003721 }
3722
zhihuang9763d562016-08-05 11:14:50 -07003723 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003724 MockPeerConnectionObserver observer_;
3725};
3726
3727// This test verifies the default behaviour with no constraints and a
3728// default RTCConfiguration.
3729TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3730 PeerConnectionInterface::RTCConfiguration config;
3731 FakeConstraints constraints;
3732
3733 const cricket::MediaConfig& media_config =
3734 TestCreatePeerConnection(config, &constraints);
3735
3736 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003737 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3738 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3739 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003740}
3741
3742// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003743// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003744TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3745 PeerConnectionInterface::RTCConfiguration config;
3746 FakeConstraints constraints;
3747
3748 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3749 const cricket::MediaConfig& media_config =
3750 TestCreatePeerConnection(config, &constraints);
3751
3752 EXPECT_TRUE(media_config.enable_dscp);
3753}
3754
3755// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003756// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003757TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3758 PeerConnectionInterface::RTCConfiguration config;
3759 FakeConstraints constraints;
3760
3761 constraints.AddOptional(
3762 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3763 const cricket::MediaConfig media_config =
3764 TestCreatePeerConnection(config, &constraints);
3765
nisse0db023a2016-03-01 04:29:59 -08003766 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003767}
3768
3769// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003770// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003771TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3772 PeerConnectionInterface::RTCConfiguration config;
3773 FakeConstraints constraints;
3774
Niels Möller71bdda02016-03-31 12:59:59 +02003775 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003776 const cricket::MediaConfig& media_config =
3777 TestCreatePeerConnection(config, &constraints);
3778
nisse0db023a2016-03-01 04:29:59 -08003779 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3780}
3781
3782// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003783// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003784TEST_F(PeerConnectionMediaConfigTest,
3785 TestSuspendBelowMinBitrateConstraintTrue) {
3786 PeerConnectionInterface::RTCConfiguration config;
3787 FakeConstraints constraints;
3788
3789 constraints.AddOptional(
3790 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3791 true);
3792 const cricket::MediaConfig media_config =
3793 TestCreatePeerConnection(config, &constraints);
3794
3795 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003796}
3797
deadbeef293e9262017-01-11 12:28:30 -08003798// Tests a few random fields being different.
3799TEST(RTCConfigurationTest, ComparisonOperators) {
3800 PeerConnectionInterface::RTCConfiguration a;
3801 PeerConnectionInterface::RTCConfiguration b;
3802 EXPECT_EQ(a, b);
3803
3804 PeerConnectionInterface::RTCConfiguration c;
3805 c.servers.push_back(PeerConnectionInterface::IceServer());
3806 EXPECT_NE(a, c);
3807
3808 PeerConnectionInterface::RTCConfiguration d;
3809 d.type = PeerConnectionInterface::kRelay;
3810 EXPECT_NE(a, d);
3811
3812 PeerConnectionInterface::RTCConfiguration e;
3813 e.audio_jitter_buffer_max_packets = 5;
3814 EXPECT_NE(a, e);
3815
3816 PeerConnectionInterface::RTCConfiguration f;
3817 f.ice_connection_receiving_timeout = 1337;
3818 EXPECT_NE(a, f);
3819
3820 PeerConnectionInterface::RTCConfiguration g;
3821 g.disable_ipv6 = true;
3822 EXPECT_NE(a, g);
3823
3824 PeerConnectionInterface::RTCConfiguration h(
3825 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3826 EXPECT_NE(a, h);
3827}