blob: 7a275e5b8959cf051941806a95b4e0831ce2f829 [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
547 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
548 cricket::WebRtcMediaEngineFactory::Create(
549 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700550 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700551
552 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
553 webrtc::CreateCallFactory();
554
555 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
556 webrtc::CreateRtcEventLogFactory();
557
558 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
559 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200560 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700561 std::move(event_log_factory));
562 }
563
564 PeerConnectionFactoryForTest(
565 rtc::Thread* network_thread,
566 rtc::Thread* worker_thread,
567 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700568 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
569 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
570 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
571 : webrtc::PeerConnectionFactory(network_thread,
572 worker_thread,
573 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700574 std::move(media_engine),
575 std::move(call_factory),
576 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800577
zhihuang29ff8442016-07-27 11:07:25 -0700578 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700579 cricket::PortAllocator* port_allocator,
580 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700581 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700582 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700583 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700584 return transport_controller;
585 }
586
deadbeefd7850b22017-08-23 10:59:19 -0700587 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700588 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700589};
590
Steve Anton94286cb2017-09-26 16:20:19 -0700591// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
592// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593class PeerConnectionInterfaceTest : public testing::Test {
594 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700595 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700596 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800597#ifdef WEBRTC_ANDROID
598 webrtc::InitializeAndroidObjects();
599#endif
600 }
601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700603 // Use fake audio capture module since we're only testing the interface
604 // level, and using a real one could make tests flaky when run in parallel.
605 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700607 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700608 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700609 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700610 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700611 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700612 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 }
614
615 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700616 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618
deadbeef293e9262017-01-11 12:28:30 -0800619 // DTLS does not work in a loopback call, so is disabled for most of the
620 // tests in this file.
621 void CreatePeerConnectionWithoutDtls() {
622 FakeConstraints no_dtls_constraints;
623 no_dtls_constraints.AddMandatory(
624 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
625
626 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
627 &no_dtls_constraints);
628 }
629
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700631 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
632 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 }
634
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700635 void CreatePeerConnectionWithIceTransportsType(
636 PeerConnectionInterface::IceTransportsType type) {
637 PeerConnectionInterface::RTCConfiguration config;
638 config.type = type;
639 return CreatePeerConnection(config, nullptr);
640 }
641
642 void CreatePeerConnectionWithIceServer(const std::string& uri,
643 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700646 server.uri = uri;
647 server.password = password;
648 config.servers.push_back(server);
649 CreatePeerConnection(config, nullptr);
650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700652 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
653 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700654 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800655 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
656 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000657
deadbeef1dcb1642017-03-29 21:08:16 -0700658 // Create certificate generator unless DTLS constraint is explicitly set to
659 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200660 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000661 bool dtls;
662 if (FindConstraint(constraints,
663 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
664 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200665 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800666 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
667 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000668 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200669 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800670 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200671 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 ASSERT_TRUE(pc_.get() != NULL);
673 observer_.SetPeerConnectionInterface(pc_.get());
674 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
675 }
676
deadbeef0a6c4ca2015-10-06 11:38:28 -0700677 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800678 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700679 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700680 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800681 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700682
zhihuang9763d562016-08-05 11:14:50 -0700683 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700684 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
685 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800686 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700687 }
688
Steve Anton038834f2017-07-14 15:59:59 -0700689 void CreatePeerConnectionExpectFail(
690 PeerConnectionInterface::RTCConfiguration config) {
691 PeerConnectionInterface::IceServer server;
692 server.uri = kTurnIceServerUri;
693 server.password = kTurnPassword;
694 config.servers.push_back(server);
695 rtc::scoped_refptr<PeerConnectionInterface> pc =
696 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
697 EXPECT_EQ(nullptr, pc);
698 }
699
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700701 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800702 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
703 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
704 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800706 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
deadbeef0a6c4ca2015-10-06 11:38:28 -0700708 CreatePeerConnectionExpectFail(kStunInvalidPort);
709 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
710 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700712 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
714 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800716 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800720 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 }
722
723 void ReleasePeerConnection() {
724 pc_ = NULL;
725 observer_.SetPeerConnectionInterface(NULL);
726 }
727
deadbeefab9b2d12015-10-14 11:33:11 -0700728 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700730 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700732 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800733 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
734 new cricket::FakeVideoCapturer()),
735 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700736 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 pc_factory_->CreateVideoTrack(label + "v0", video_source));
738 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000739 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
741 observer_.renegotiation_needed_ = false;
742 }
743
744 void AddVoiceStream(const std::string& label) {
745 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700746 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700748 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 pc_factory_->CreateAudioTrack(label + "a0", NULL));
750 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000751 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
753 observer_.renegotiation_needed_ = false;
754 }
755
756 void AddAudioVideoStream(const std::string& stream_label,
757 const std::string& audio_track_label,
758 const std::string& video_track_label) {
759 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700762 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 pc_factory_->CreateAudioTrack(
764 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
765 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700766 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700767 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800768 video_track_label, pc_factory_->CreateVideoSource(
769 std::unique_ptr<cricket::VideoCapturer>(
770 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000772 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
774 observer_.renegotiation_needed_ = false;
775 }
776
kwibergd1fe2812016-04-27 06:47:29 -0700777 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700778 bool offer,
779 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000780 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
781 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 MockCreateSessionDescriptionObserver>());
783 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700784 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700786 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 }
788 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700789 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 return observer->result();
791 }
792
kwibergd1fe2812016-04-27 06:47:29 -0700793 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700794 MediaConstraintsInterface* constraints) {
795 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 }
797
kwibergd1fe2812016-04-27 06:47:29 -0700798 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700799 MediaConstraintsInterface* constraints) {
800 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
Steve Antondb45ca82017-09-11 18:27:34 -0700803 bool DoSetSessionDescription(
804 std::unique_ptr<SessionDescriptionInterface> desc,
805 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
807 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 MockSetSessionDescriptionObserver>());
809 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700810 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700812 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
zhihuang29ff8442016-07-27 11:07:25 -0700814 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
815 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
816 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 return observer->result();
818 }
819
Steve Antondb45ca82017-09-11 18:27:34 -0700820 bool DoSetLocalDescription(
821 std::unique_ptr<SessionDescriptionInterface> desc) {
822 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
Steve Antondb45ca82017-09-11 18:27:34 -0700825 bool DoSetRemoteDescription(
826 std::unique_ptr<SessionDescriptionInterface> desc) {
827 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
829
830 // Calls PeerConnection::GetStats and check the return value.
831 // It does not verify the values in the StatReports since a RTCP packet might
832 // be required.
833 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 rtc::scoped_refptr<MockStatsObserver> observer(
835 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000836 if (!pc_->GetStats(
837 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return false;
839 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
840 return observer->called();
841 }
842
843 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800844 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 // Create a local stream with audio&video tracks.
846 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
847 CreateOfferReceiveAnswer();
848 }
849
850 // Verify that RTP Header extensions has been negotiated for audio and video.
851 void VerifyRemoteRtpHeaderExtensions() {
852 const cricket::MediaContentDescription* desc =
853 cricket::GetFirstAudioContentDescription(
854 pc_->remote_description()->description());
855 ASSERT_TRUE(desc != NULL);
856 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
857
858 desc = cricket::GetFirstVideoContentDescription(
859 pc_->remote_description()->description());
860 ASSERT_TRUE(desc != NULL);
861 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
862 }
863
864 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700865 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700866 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 std::string sdp;
868 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700869 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700871 sdp, nullptr));
872 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
874 }
875
deadbeefab9b2d12015-10-14 11:33:11 -0700876 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700877 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700878 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700879 sdp, nullptr));
880 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700881 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
882 }
883
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700885 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700886 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887
888 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
889 // audio codec change, even if the parameter has nothing to do with
890 // receiving. Not all parameters are serialized to SDP.
891 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
892 // the SessionDescription, it is necessary to do that here to in order to
893 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
894 // https://code.google.com/p/webrtc/issues/detail?id=1356
895 std::string sdp;
896 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700897 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700899 sdp, nullptr));
900 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
902 }
903
904 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700905 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700906 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907
908 std::string sdp;
909 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700910 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700912 sdp, nullptr));
913 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
915 }
916
917 void CreateOfferReceiveAnswer() {
918 CreateOfferAsLocalDescription();
919 std::string sdp;
920 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
921 CreateAnswerAsRemoteDescription(sdp);
922 }
923
924 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700925 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700926 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
928 // audio codec change, even if the parameter has nothing to do with
929 // receiving. Not all parameters are serialized to SDP.
930 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
931 // the SessionDescription, it is necessary to do that here to in order to
932 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
933 // https://code.google.com/p/webrtc/issues/detail?id=1356
934 std::string sdp;
935 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700936 std::unique_ptr<SessionDescriptionInterface> new_offer(
937 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
938 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939
Steve Antondb45ca82017-09-11 18:27:34 -0700940 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000942 // Wait for the ice_complete message, so that SDP will have candidates.
943 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 }
945
deadbeefab9b2d12015-10-14 11:33:11 -0700946 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700947 std::unique_ptr<SessionDescriptionInterface> answer(
948 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
949 sdp, nullptr));
950 ASSERT_TRUE(answer);
951 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
953 }
954
deadbeefab9b2d12015-10-14 11:33:11 -0700955 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700956 std::unique_ptr<SessionDescriptionInterface> pr_answer(
957 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
958 sdp, nullptr));
959 ASSERT_TRUE(pr_answer);
960 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700962 std::unique_ptr<SessionDescriptionInterface> answer(
963 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
964 sdp, nullptr));
965 ASSERT_TRUE(answer);
966 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
968 }
969
970 // Help function used for waiting until a the last signaled remote stream has
971 // the same label as |stream_label|. In a few of the tests in this file we
972 // answer with the same session description as we offer and thus we can
973 // check if OnAddStream have been called with the same stream as we offer to
974 // send.
975 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
976 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
977 }
978
979 // Creates an offer and applies it as a local session description.
980 // Creates an answer with the same SDP an the offer but removes all lines
981 // that start with a:ssrc"
982 void CreateOfferReceiveAnswerWithoutSsrc() {
983 CreateOfferAsLocalDescription();
984 std::string sdp;
985 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
986 SetSsrcToZero(&sdp);
987 CreateAnswerAsRemoteDescription(sdp);
988 }
989
deadbeefab9b2d12015-10-14 11:33:11 -0700990 // This function creates a MediaStream with label kStreams[0] and
991 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
992 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700993 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700994 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700995 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700996 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
997 size_t number_of_video_tracks) {
998 EXPECT_LE(number_of_audio_tracks, 2u);
999 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001000
1001 reference_collection_ = StreamCollection::Create();
1002 std::string sdp_ms1 = std::string(kSdpStringInit);
1003
1004 std::string mediastream_label = kStreams[0];
1005
1006 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1007 webrtc::MediaStream::Create(mediastream_label));
1008 reference_collection_->AddStream(stream);
1009
1010 if (number_of_audio_tracks > 0) {
1011 sdp_ms1 += std::string(kSdpStringAudio);
1012 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1013 AddAudioTrack(kAudioTracks[0], stream);
1014 }
1015 if (number_of_audio_tracks > 1) {
1016 sdp_ms1 += kSdpStringMs1Audio1;
1017 AddAudioTrack(kAudioTracks[1], stream);
1018 }
1019
1020 if (number_of_video_tracks > 0) {
1021 sdp_ms1 += std::string(kSdpStringVideo);
1022 sdp_ms1 += std::string(kSdpStringMs1Video0);
1023 AddVideoTrack(kVideoTracks[0], stream);
1024 }
1025 if (number_of_video_tracks > 1) {
1026 sdp_ms1 += kSdpStringMs1Video1;
1027 AddVideoTrack(kVideoTracks[1], stream);
1028 }
1029
kwibergd1fe2812016-04-27 06:47:29 -07001030 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001031 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1032 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001033 }
1034
1035 void AddAudioTrack(const std::string& track_id,
1036 MediaStreamInterface* stream) {
1037 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1038 webrtc::AudioTrack::Create(track_id, nullptr));
1039 ASSERT_TRUE(stream->AddTrack(audio_track));
1040 }
1041
1042 void AddVideoTrack(const std::string& track_id,
1043 MediaStreamInterface* stream) {
1044 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001045 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001046 webrtc::FakeVideoTrackSource::Create(),
1047 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001048 ASSERT_TRUE(stream->AddTrack(video_track));
1049 }
1050
kwibergfd8be342016-05-14 19:44:11 -07001051 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001052 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001053 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001054 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001055 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1056 return offer;
1057 }
1058
kwibergfd8be342016-05-14 19:44:11 -07001059 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001060 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001061 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001062 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001063 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1064 return answer;
1065 }
1066
1067 const std::string& GetFirstAudioStreamCname(
1068 const SessionDescriptionInterface* desc) {
1069 const cricket::ContentInfo* audio_content =
1070 cricket::GetFirstAudioContent(desc->description());
1071 const cricket::AudioContentDescription* audio_desc =
1072 static_cast<const cricket::AudioContentDescription*>(
1073 audio_content->description);
1074 return audio_desc->streams()[0].cname;
1075 }
1076
zhihuang1c378ed2017-08-17 14:10:50 -07001077 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1078 const RTCOfferAnswerOptions& offer_answer_options) {
1079 RTC_DCHECK(pc_);
1080 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1081 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1082 pc_->CreateOffer(observer, offer_answer_options);
1083 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1084 return observer->MoveDescription();
1085 }
1086
1087 void CreateOfferWithOptionsAsRemoteDescription(
1088 std::unique_ptr<SessionDescriptionInterface>* desc,
1089 const RTCOfferAnswerOptions& offer_answer_options) {
1090 *desc = CreateOfferWithOptions(offer_answer_options);
1091 ASSERT_TRUE(desc != nullptr);
1092 std::string sdp;
1093 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001094 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001095 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001096 sdp, nullptr));
1097 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001098 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1099 }
1100
1101 void CreateOfferWithOptionsAsLocalDescription(
1102 std::unique_ptr<SessionDescriptionInterface>* desc,
1103 const RTCOfferAnswerOptions& offer_answer_options) {
1104 *desc = CreateOfferWithOptions(offer_answer_options);
1105 ASSERT_TRUE(desc != nullptr);
1106 std::string sdp;
1107 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001108 std::unique_ptr<SessionDescriptionInterface> new_offer(
1109 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1110 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001111
Steve Antondb45ca82017-09-11 18:27:34 -07001112 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001113 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1114 }
1115
1116 bool HasCNCodecs(const cricket::ContentInfo* content) {
1117 const cricket::ContentDescription* description = content->description;
1118 RTC_DCHECK(description);
1119 const cricket::AudioContentDescription* audio_content_desc =
1120 static_cast<const cricket::AudioContentDescription*>(description);
1121 RTC_DCHECK(audio_content_desc);
1122 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1123 if (audio_content_desc->codecs()[i].name == "CN")
1124 return true;
1125 }
1126 return false;
1127 }
1128
deadbeef9a6f4d42017-05-15 19:43:33 -07001129 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1130 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001131 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001132 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001133 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001134 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1135 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1136 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001138 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139};
1140
zhihuang29ff8442016-07-27 11:07:25 -07001141// Test that no callbacks on the PeerConnectionObserver are called after the
1142// PeerConnection is closed.
1143TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001144 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001145 pc_factory_for_test_->CreatePeerConnection(
1146 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1147 nullptr, &observer_));
1148 observer_.SetPeerConnectionInterface(pc.get());
1149 pc->Close();
1150
1151 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001152 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001153 std::vector<cricket::Candidate> candidates;
1154 pc_factory_for_test_->transport_controller->SignalGatheringState(
1155 cricket::IceGatheringState{});
1156 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1157 "", candidates);
1158 pc_factory_for_test_->transport_controller->SignalConnectionState(
1159 cricket::IceConnectionState{});
1160 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1161 candidates);
1162 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001163 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001164}
1165
zhihuang8f65cdf2016-05-06 18:40:30 -07001166// Generate different CNAMEs when PeerConnections are created.
1167// The CNAMEs are expected to be generated randomly. It is possible
1168// that the test fails, though the possibility is very low.
1169TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 CreateOfferWithOneAudioStream();
1174 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1175 GetFirstAudioStreamCname(offer2.get()));
1176}
1177
1178TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 CreateAnswerWithOneAudioStream();
1183 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1184 GetFirstAudioStreamCname(answer2.get()));
1185}
1186
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187TEST_F(PeerConnectionInterfaceTest,
1188 CreatePeerConnectionWithDifferentConfigurations) {
1189 CreatePeerConnectionWithDifferentConfigurations();
1190}
1191
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001192TEST_F(PeerConnectionInterfaceTest,
1193 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1194 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1195 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1197 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1199 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1200 port_allocator_->candidate_filter());
1201 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1202 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1203}
1204
1205// Test that when a PeerConnection is created with a nonzero candidate pool
1206// size, the pooled PortAllocatorSession is created with all the attributes
1207// in the RTCConfiguration.
1208TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1209 PeerConnectionInterface::RTCConfiguration config;
1210 PeerConnectionInterface::IceServer server;
1211 server.uri = kStunAddressOnly;
1212 config.servers.push_back(server);
1213 config.type = PeerConnectionInterface::kRelay;
1214 config.disable_ipv6 = true;
1215 config.tcp_candidate_policy =
1216 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001217 config.candidate_network_policy =
1218 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001219 config.ice_candidate_pool_size = 1;
1220 CreatePeerConnection(config, nullptr);
1221
1222 const cricket::FakePortAllocatorSession* session =
1223 static_cast<const cricket::FakePortAllocatorSession*>(
1224 port_allocator_->GetPooledSession());
1225 ASSERT_NE(nullptr, session);
1226 EXPECT_EQ(1UL, session->stun_servers().size());
1227 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1228 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001229 EXPECT_LT(0U,
1230 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001231}
1232
deadbeefd21eab32017-07-26 16:50:11 -07001233// Test that network-related RTCConfiguration members are applied to the
1234// PortAllocator when CreatePeerConnection is called. Specifically:
1235// - disable_ipv6_on_wifi
1236// - max_ipv6_networks
1237// - tcp_candidate_policy
1238// - candidate_network_policy
1239// - prune_turn_ports
1240//
1241// Note that the candidate filter (RTCConfiguration::type) is already tested
1242// above.
1243TEST_F(PeerConnectionInterfaceTest,
1244 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1245 // Create fake port allocator.
1246 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1247 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1248 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1249
1250 // Create RTCConfiguration with some network-related fields relevant to
1251 // PortAllocator populated.
1252 PeerConnectionInterface::RTCConfiguration config;
1253 config.disable_ipv6_on_wifi = true;
1254 config.max_ipv6_networks = 10;
1255 config.tcp_candidate_policy =
1256 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1257 config.candidate_network_policy =
1258 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1259 config.prune_turn_ports = true;
1260
1261 // Create the PC factory and PC with the above config.
1262 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1263 webrtc::CreatePeerConnectionFactory(
1264 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001265 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1266 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001267 rtc::scoped_refptr<PeerConnectionInterface> pc(
1268 pc_factory->CreatePeerConnection(
1269 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1270
1271 // Now validate that the config fields set above were applied to the
1272 // PortAllocator, as flags or otherwise.
1273 EXPECT_FALSE(raw_port_allocator->flags() &
1274 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1275 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1276 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1277 EXPECT_TRUE(raw_port_allocator->flags() &
1278 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1279 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1280}
1281
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001282// Test that the PeerConnection initializes the port allocator passed into it,
1283// and on the correct thread.
1284TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001285 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001286 std::unique_ptr<rtc::Thread> network_thread(
1287 rtc::Thread::CreateWithSocketServer());
1288 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001289 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1290 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001291 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001292 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001293 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001294 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001295 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1296 PeerConnectionInterface::RTCConfiguration config;
1297 rtc::scoped_refptr<PeerConnectionInterface> pc(
1298 pc_factory->CreatePeerConnection(
1299 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1300 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1301 // so all we have to do here is check that it's initialized.
1302 EXPECT_TRUE(raw_port_allocator->initialized());
1303}
1304
deadbeef46c73892016-11-16 19:42:04 -08001305// Check that GetConfiguration returns the configuration the PeerConnection was
1306// constructed with, before SetConfiguration is called.
1307TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1308 PeerConnectionInterface::RTCConfiguration config;
1309 config.type = PeerConnectionInterface::kRelay;
1310 CreatePeerConnection(config, nullptr);
1311
1312 PeerConnectionInterface::RTCConfiguration returned_config =
1313 pc_->GetConfiguration();
1314 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1315}
1316
1317// Check that GetConfiguration returns the last configuration passed into
1318// SetConfiguration.
1319TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1320 CreatePeerConnection();
1321
1322 PeerConnectionInterface::RTCConfiguration config;
1323 config.type = PeerConnectionInterface::kRelay;
1324 EXPECT_TRUE(pc_->SetConfiguration(config));
1325
1326 PeerConnectionInterface::RTCConfiguration returned_config =
1327 pc_->GetConfiguration();
1328 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1329}
1330
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001332 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001333 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 AddVoiceStream(kStreamLabel2);
1335 ASSERT_EQ(2u, pc_->local_streams()->count());
1336
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001337 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001338 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001340 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1341 pc_factory_->CreateAudioTrack(kStreamLabel3,
1342 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001344 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001345 EXPECT_EQ(3u, pc_->local_streams()->count());
1346
1347 // Remove the third stream.
1348 pc_->RemoveStream(pc_->local_streams()->at(2));
1349 EXPECT_EQ(2u, pc_->local_streams()->count());
1350
1351 // Remove the second stream.
1352 pc_->RemoveStream(pc_->local_streams()->at(1));
1353 EXPECT_EQ(1u, pc_->local_streams()->count());
1354
1355 // Remove the first stream.
1356 pc_->RemoveStream(pc_->local_streams()->at(0));
1357 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358}
1359
deadbeefab9b2d12015-10-14 11:33:11 -07001360// Test that the created offer includes streams we added.
1361TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001362 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001363 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001364 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001365 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001366
1367 const cricket::ContentInfo* audio_content =
1368 cricket::GetFirstAudioContent(offer->description());
1369 const cricket::AudioContentDescription* audio_desc =
1370 static_cast<const cricket::AudioContentDescription*>(
1371 audio_content->description);
1372 EXPECT_TRUE(
1373 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1374
1375 const cricket::ContentInfo* video_content =
1376 cricket::GetFirstVideoContent(offer->description());
1377 const cricket::VideoContentDescription* video_desc =
1378 static_cast<const cricket::VideoContentDescription*>(
1379 video_content->description);
1380 EXPECT_TRUE(
1381 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1382
1383 // Add another stream and ensure the offer includes both the old and new
1384 // streams.
1385 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001386 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001387
1388 audio_content = cricket::GetFirstAudioContent(offer->description());
1389 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1390 audio_content->description);
1391 EXPECT_TRUE(
1392 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1393 EXPECT_TRUE(
1394 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1395
1396 video_content = cricket::GetFirstVideoContent(offer->description());
1397 video_desc = static_cast<const cricket::VideoContentDescription*>(
1398 video_content->description);
1399 EXPECT_TRUE(
1400 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1401 EXPECT_TRUE(
1402 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1403}
1404
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001406 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001407 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 ASSERT_EQ(1u, pc_->local_streams()->count());
1409 pc_->RemoveStream(pc_->local_streams()->at(0));
1410 EXPECT_EQ(0u, pc_->local_streams()->count());
1411}
1412
deadbeefe1f9d832016-01-14 15:35:42 -08001413// Test for AddTrack and RemoveTrack methods.
1414// Tests that the created offer includes tracks we added,
1415// and that the RtpSenders are created correctly.
1416// Also tests that RemoveTrack removes the tracks from subsequent offers.
1417TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001418 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001419 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001420 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001421 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1422 std::vector<MediaStreamInterface*> stream_list;
1423 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001424 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001425 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001426 rtc::scoped_refptr<VideoTrackInterface> video_track(
1427 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001428 "video_track", pc_factory_->CreateVideoSource(
1429 std::unique_ptr<cricket::VideoCapturer>(
1430 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001431 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1432 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001433 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1434 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001435 EXPECT_EQ("audio_track", audio_sender->id());
1436 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001437 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1438 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001439 EXPECT_EQ("video_track", video_sender->id());
1440 EXPECT_EQ(video_track, video_sender->track());
1441
1442 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001443 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001444 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001445
1446 const cricket::ContentInfo* audio_content =
1447 cricket::GetFirstAudioContent(offer->description());
1448 const cricket::AudioContentDescription* audio_desc =
1449 static_cast<const cricket::AudioContentDescription*>(
1450 audio_content->description);
1451 EXPECT_TRUE(
1452 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1453
1454 const cricket::ContentInfo* video_content =
1455 cricket::GetFirstVideoContent(offer->description());
1456 const cricket::VideoContentDescription* video_desc =
1457 static_cast<const cricket::VideoContentDescription*>(
1458 video_content->description);
1459 EXPECT_TRUE(
1460 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1461
Steve Antondb45ca82017-09-11 18:27:34 -07001462 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001463
1464 // Now try removing the tracks.
1465 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1466 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1467
1468 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001469 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001470
1471 audio_content = cricket::GetFirstAudioContent(offer->description());
1472 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1473 audio_content->description);
1474 EXPECT_FALSE(
1475 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1476
1477 video_content = cricket::GetFirstVideoContent(offer->description());
1478 video_desc = static_cast<const cricket::VideoContentDescription*>(
1479 video_content->description);
1480 EXPECT_FALSE(
1481 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1482
Steve Antondb45ca82017-09-11 18:27:34 -07001483 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001484
1485 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1486 // should return false.
1487 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1488 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1489}
1490
1491// Test creating senders without a stream specified,
1492// expecting a random stream ID to be generated.
1493TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001494 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001495 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001496 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001497 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001498 rtc::scoped_refptr<VideoTrackInterface> video_track(
1499 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001500 "video_track", pc_factory_->CreateVideoSource(
1501 std::unique_ptr<cricket::VideoCapturer>(
1502 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001503 auto audio_sender =
1504 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1505 auto video_sender =
1506 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1507 EXPECT_EQ("audio_track", audio_sender->id());
1508 EXPECT_EQ(audio_track, audio_sender->track());
1509 EXPECT_EQ("video_track", video_sender->id());
1510 EXPECT_EQ(video_track, video_sender->track());
1511 // If the ID is truly a random GUID, it should be infinitely unlikely they
1512 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001513 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001514}
1515
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1517 InitiateCall();
1518 WaitAndVerifyOnAddStream(kStreamLabel1);
1519 VerifyRemoteRtpHeaderExtensions();
1520}
1521
1522TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001523 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001524 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 CreateOfferAsLocalDescription();
1526 std::string offer;
1527 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1528 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1529 WaitAndVerifyOnAddStream(kStreamLabel1);
1530}
1531
1532TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001533 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001534 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535
1536 CreateOfferAsRemoteDescription();
1537 CreateAnswerAsLocalDescription();
1538
1539 WaitAndVerifyOnAddStream(kStreamLabel1);
1540}
1541
1542TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001543 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001544 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545
1546 CreateOfferAsRemoteDescription();
1547 CreatePrAnswerAsLocalDescription();
1548 CreateAnswerAsLocalDescription();
1549
1550 WaitAndVerifyOnAddStream(kStreamLabel1);
1551}
1552
1553TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1554 InitiateCall();
1555 ASSERT_EQ(1u, pc_->remote_streams()->count());
1556 pc_->RemoveStream(pc_->local_streams()->at(0));
1557 CreateOfferReceiveAnswer();
1558 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001559 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 CreateOfferReceiveAnswer();
1561}
1562
1563// Tests that after negotiating an audio only call, the respondent can perform a
1564// renegotiation that removes the audio stream.
1565TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001566 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 AddVoiceStream(kStreamLabel1);
1568 CreateOfferAsRemoteDescription();
1569 CreateAnswerAsLocalDescription();
1570
1571 ASSERT_EQ(1u, pc_->remote_streams()->count());
1572 pc_->RemoveStream(pc_->local_streams()->at(0));
1573 CreateOfferReceiveAnswer();
1574 EXPECT_EQ(0u, pc_->remote_streams()->count());
1575}
1576
1577// Test that candidates are generated and that we can parse our own candidates.
1578TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001579 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580
Steve Antonf1c6db12017-10-13 11:13:35 -07001581 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001583 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001584 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001585 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001586 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
1588 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001589 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001590 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001591 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592
Steve Antonf1c6db12017-10-13 11:13:35 -07001593 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1595
Steve Antonf1c6db12017-10-13 11:13:35 -07001596 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597}
1598
deadbeefab9b2d12015-10-14 11:33:11 -07001599// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600// not unique.
1601TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001602 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001604 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001605 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001606 EXPECT_TRUE(offer);
1607 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
1609 // Create a local stream with audio&video tracks having same label.
1610 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1611
1612 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001613 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614
1615 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001616 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001617 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618}
1619
1620// Test that we will get different SSRCs for each tracks in the offer and answer
1621// we created.
1622TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001623 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 // Create a local stream with audio&video tracks having different labels.
1625 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1626
1627 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001628 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001629 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 int audio_ssrc = 0;
1631 int video_ssrc = 0;
1632 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1633 &audio_ssrc));
1634 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1635 &video_ssrc));
1636 EXPECT_NE(audio_ssrc, video_ssrc);
1637
1638 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001639 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001640 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001641 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 audio_ssrc = 0;
1643 video_ssrc = 0;
1644 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1645 &audio_ssrc));
1646 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1647 &video_ssrc));
1648 EXPECT_NE(audio_ssrc, video_ssrc);
1649}
1650
deadbeefeb459812015-12-15 19:24:43 -08001651// Test that it's possible to call AddTrack on a MediaStream after adding
1652// the stream to a PeerConnection.
1653// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1654TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001655 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001656 // Create audio stream and add to PeerConnection.
1657 AddVoiceStream(kStreamLabel1);
1658 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1659
1660 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001661 rtc::scoped_refptr<VideoTrackInterface> video_track(
1662 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001663 "video_label", pc_factory_->CreateVideoSource(
1664 std::unique_ptr<cricket::VideoCapturer>(
1665 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001666 stream->AddTrack(video_track.get());
1667
kwibergd1fe2812016-04-27 06:47:29 -07001668 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001669 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001670
1671 const cricket::MediaContentDescription* video_desc =
1672 cricket::GetFirstVideoContentDescription(offer->description());
1673 EXPECT_TRUE(video_desc != nullptr);
1674}
1675
1676// Test that it's possible to call RemoveTrack on a MediaStream after adding
1677// the stream to a PeerConnection.
1678// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1679TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001680 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001681 // Create audio/video stream and add to PeerConnection.
1682 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1683 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1684
1685 // Remove the video track.
1686 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1687
kwibergd1fe2812016-04-27 06:47:29 -07001688 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001689 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001690
1691 const cricket::MediaContentDescription* video_desc =
1692 cricket::GetFirstVideoContentDescription(offer->description());
1693 EXPECT_TRUE(video_desc == nullptr);
1694}
1695
deadbeef1dcb1642017-03-29 21:08:16 -07001696// Verify that CreateDtmfSender only succeeds if called with a valid local
1697// track. Other aspects of DtmfSenders are tested in
1698// peerconnection_integrationtest.cc.
1699TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1700 CreatePeerConnection();
1701 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1702 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1703 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1704 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1705 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1706}
1707
deadbeefbd7d8f72015-12-18 16:58:44 -08001708// Test creating a sender with a stream ID, and ensure the ID is populated
1709// in the offer.
1710TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001711 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001712 pc_->CreateSender("video", kStreamLabel1);
1713
kwibergd1fe2812016-04-27 06:47:29 -07001714 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001715 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001716
1717 const cricket::MediaContentDescription* video_desc =
1718 cricket::GetFirstVideoContentDescription(offer->description());
1719 ASSERT_TRUE(video_desc != nullptr);
1720 ASSERT_EQ(1u, video_desc->streams().size());
1721 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1722}
1723
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724// Test that we can specify a certain track that we want statistics about.
1725TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1726 InitiateCall();
1727 ASSERT_LT(0u, pc_->remote_streams()->count());
1728 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001729 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1731 EXPECT_TRUE(DoGetStats(remote_audio));
1732
1733 // Remove the stream. Since we are sending to our selves the local
1734 // and the remote stream is the same.
1735 pc_->RemoveStream(pc_->local_streams()->at(0));
1736 // Do a re-negotiation.
1737 CreateOfferReceiveAnswer();
1738
1739 ASSERT_EQ(0u, pc_->remote_streams()->count());
1740
1741 // Test that we still can get statistics for the old track. Even if it is not
1742 // sent any longer.
1743 EXPECT_TRUE(DoGetStats(remote_audio));
1744}
1745
1746// Test that we can get stats on a video track.
1747TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1748 InitiateCall();
1749 ASSERT_LT(0u, pc_->remote_streams()->count());
1750 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001751 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1753 EXPECT_TRUE(DoGetStats(remote_video));
1754}
1755
1756// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001757TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001759 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 pc_factory_->CreateAudioTrack("unknown track", NULL));
1761 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1762}
1763
1764// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 FakeConstraints constraints;
1767 constraints.SetAllowRtpDataChannels();
1768 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001771 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 pc_->CreateDataChannel("test2", NULL);
1773 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001774 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001776 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 new MockDataChannelObserver(data2));
1778
1779 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1780 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1781 std::string data_to_send1 = "testing testing";
1782 std::string data_to_send2 = "testing something else";
1783 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1784
1785 CreateOfferReceiveAnswer();
1786 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1787 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1788
1789 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1790 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1791 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1792 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1793
1794 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1795 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1796
1797 data1->Close();
1798 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1799 CreateOfferReceiveAnswer();
1800 EXPECT_FALSE(observer1->IsOpen());
1801 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1802 EXPECT_TRUE(observer2->IsOpen());
1803
1804 data_to_send2 = "testing something else again";
1805 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1806
1807 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1808}
1809
1810// This test verifies that sendnig binary data over RTP data channels should
1811// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 FakeConstraints constraints;
1814 constraints.SetAllowRtpDataChannels();
1815 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001816 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001818 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 pc_->CreateDataChannel("test2", NULL);
1820 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001821 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001823 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 new MockDataChannelObserver(data2));
1825
1826 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1827 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1828
1829 CreateOfferReceiveAnswer();
1830 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1831 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1832
1833 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1834 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1835
jbaucheec21bd2016-03-20 06:15:43 -07001836 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1838}
1839
1840// This test setup a RTP data channels in loop back and test that a channel is
1841// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 FakeConstraints constraints;
1844 constraints.SetAllowRtpDataChannels();
1845 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001846 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001848 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 new MockDataChannelObserver(data1));
1850
1851 CreateOfferReceiveAnswerWithoutSsrc();
1852
1853 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1854
1855 data1->Close();
1856 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1857 CreateOfferReceiveAnswerWithoutSsrc();
1858 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1859 EXPECT_FALSE(observer1->IsOpen());
1860}
1861
1862// This test that if a data channel is added in an answer a receive only channel
1863// channel is created.
1864TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1865 FakeConstraints constraints;
1866 constraints.SetAllowRtpDataChannels();
1867 CreatePeerConnection(&constraints);
1868
1869 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001870 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 pc_->CreateDataChannel(offer_label, NULL);
1872
1873 CreateOfferAsLocalDescription();
1874
1875 // Replace the data channel label in the offer and apply it as an answer.
1876 std::string receive_label = "answer_channel";
1877 std::string sdp;
1878 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001879 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 receive_label.c_str(), receive_label.length(),
1881 &sdp);
1882 CreateAnswerAsRemoteDescription(sdp);
1883
1884 // Verify that a new incoming data channel has been created and that
1885 // it is open but can't we written to.
1886 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1887 DataChannelInterface* received_channel = observer_.last_datachannel_;
1888 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1889 EXPECT_EQ(receive_label, received_channel->label());
1890 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1891
1892 // Verify that the channel we initially offered has been rejected.
1893 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1894
1895 // Do another offer / answer exchange and verify that the data channel is
1896 // opened.
1897 CreateOfferReceiveAnswer();
1898 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1899 kTimeout);
1900}
1901
1902// This test that no data channel is returned if a reliable channel is
1903// requested.
1904// TODO(perkj): Remove this test once reliable channels are implemented.
1905TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1906 FakeConstraints constraints;
1907 constraints.SetAllowRtpDataChannels();
1908 CreatePeerConnection(&constraints);
1909
1910 std::string label = "test";
1911 webrtc::DataChannelInit config;
1912 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001913 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 pc_->CreateDataChannel(label, &config);
1915 EXPECT_TRUE(channel == NULL);
1916}
1917
deadbeefab9b2d12015-10-14 11:33:11 -07001918// Verifies that duplicated label is not allowed for RTP data channel.
1919TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1920 FakeConstraints constraints;
1921 constraints.SetAllowRtpDataChannels();
1922 CreatePeerConnection(&constraints);
1923
1924 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001925 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001926 pc_->CreateDataChannel(label, nullptr);
1927 EXPECT_NE(channel, nullptr);
1928
zhihuang9763d562016-08-05 11:14:50 -07001929 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001930 pc_->CreateDataChannel(label, nullptr);
1931 EXPECT_EQ(dup_channel, nullptr);
1932}
1933
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934// This tests that a SCTP data channel is returned using different
1935// DataChannelInit configurations.
1936TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1937 FakeConstraints constraints;
1938 constraints.SetAllowDtlsSctpDataChannels();
1939 CreatePeerConnection(&constraints);
1940
1941 webrtc::DataChannelInit config;
1942
zhihuang9763d562016-08-05 11:14:50 -07001943 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 pc_->CreateDataChannel("1", &config);
1945 EXPECT_TRUE(channel != NULL);
1946 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001947 EXPECT_TRUE(observer_.renegotiation_needed_);
1948 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949
1950 config.ordered = false;
1951 channel = pc_->CreateDataChannel("2", &config);
1952 EXPECT_TRUE(channel != NULL);
1953 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001954 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955
1956 config.ordered = true;
1957 config.maxRetransmits = 0;
1958 channel = pc_->CreateDataChannel("3", &config);
1959 EXPECT_TRUE(channel != NULL);
1960 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001961 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 config.maxRetransmits = -1;
1964 config.maxRetransmitTime = 0;
1965 channel = pc_->CreateDataChannel("4", &config);
1966 EXPECT_TRUE(channel != NULL);
1967 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001968 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969}
1970
1971// This tests that no data channel is returned if both maxRetransmits and
1972// maxRetransmitTime are set for SCTP data channels.
1973TEST_F(PeerConnectionInterfaceTest,
1974 CreateSctpDataChannelShouldFailForInvalidConfig) {
1975 FakeConstraints constraints;
1976 constraints.SetAllowDtlsSctpDataChannels();
1977 CreatePeerConnection(&constraints);
1978
1979 std::string label = "test";
1980 webrtc::DataChannelInit config;
1981 config.maxRetransmits = 0;
1982 config.maxRetransmitTime = 0;
1983
zhihuang9763d562016-08-05 11:14:50 -07001984 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 pc_->CreateDataChannel(label, &config);
1986 EXPECT_TRUE(channel == NULL);
1987}
1988
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989// The test verifies that creating a SCTP data channel with an id already in use
1990// or out of range should fail.
1991TEST_F(PeerConnectionInterfaceTest,
1992 CreateSctpDataChannelWithInvalidIdShouldFail) {
1993 FakeConstraints constraints;
1994 constraints.SetAllowDtlsSctpDataChannels();
1995 CreatePeerConnection(&constraints);
1996
1997 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001998 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002000 config.id = 1;
2001 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 EXPECT_TRUE(channel != NULL);
2003 EXPECT_EQ(1, channel->id());
2004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 channel = pc_->CreateDataChannel("x", &config);
2006 EXPECT_TRUE(channel == NULL);
2007
2008 config.id = cricket::kMaxSctpSid;
2009 channel = pc_->CreateDataChannel("max", &config);
2010 EXPECT_TRUE(channel != NULL);
2011 EXPECT_EQ(config.id, channel->id());
2012
2013 config.id = cricket::kMaxSctpSid + 1;
2014 channel = pc_->CreateDataChannel("x", &config);
2015 EXPECT_TRUE(channel == NULL);
2016}
2017
deadbeefab9b2d12015-10-14 11:33:11 -07002018// Verifies that duplicated label is allowed for SCTP data channel.
2019TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2020 FakeConstraints constraints;
2021 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2022 true);
2023 CreatePeerConnection(&constraints);
2024
2025 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002026 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002027 pc_->CreateDataChannel(label, nullptr);
2028 EXPECT_NE(channel, nullptr);
2029
zhihuang9763d562016-08-05 11:14:50 -07002030 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002031 pc_->CreateDataChannel(label, nullptr);
2032 EXPECT_NE(dup_channel, nullptr);
2033}
2034
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002035// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2036// DataChannel.
2037TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2038 FakeConstraints constraints;
2039 constraints.SetAllowRtpDataChannels();
2040 CreatePeerConnection(&constraints);
2041
zhihuang9763d562016-08-05 11:14:50 -07002042 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002043 pc_->CreateDataChannel("test1", NULL);
2044 EXPECT_TRUE(observer_.renegotiation_needed_);
2045 observer_.renegotiation_needed_ = false;
2046
zhihuang9763d562016-08-05 11:14:50 -07002047 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002048 pc_->CreateDataChannel("test2", NULL);
2049 EXPECT_TRUE(observer_.renegotiation_needed_);
2050}
2051
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 FakeConstraints constraints;
2055 constraints.SetAllowRtpDataChannels();
2056 CreatePeerConnection(&constraints);
2057
zhihuang9763d562016-08-05 11:14:50 -07002058 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002060 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 pc_->CreateDataChannel("test2", NULL);
2062 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002063 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002065 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 new MockDataChannelObserver(data2));
2067
2068 CreateOfferReceiveAnswer();
2069 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2070 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2071
2072 ReleasePeerConnection();
2073 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2074 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2075}
2076
2077// This test that data channels can be rejected in an answer.
2078TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2079 FakeConstraints constraints;
2080 constraints.SetAllowRtpDataChannels();
2081 CreatePeerConnection(&constraints);
2082
zhihuang9763d562016-08-05 11:14:50 -07002083 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 pc_->CreateDataChannel("offer_channel", NULL));
2085
2086 CreateOfferAsLocalDescription();
2087
2088 // Create an answer where the m-line for data channels are rejected.
2089 std::string sdp;
2090 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002091 std::unique_ptr<SessionDescriptionInterface> answer(
2092 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2093 sdp, nullptr));
2094 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 cricket::ContentInfo* data_info =
2096 answer->description()->GetContentByName("data");
2097 data_info->rejected = true;
2098
Steve Antondb45ca82017-09-11 18:27:34 -07002099 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2101}
2102
2103// Test that we can create a session description from an SDP string from
2104// FireFox, use it as a remote session description, generate an answer and use
2105// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002106TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 FakeConstraints constraints;
2108 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2109 true);
2110 CreatePeerConnection(&constraints);
2111 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002112 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002114 webrtc::kFireFoxSdpOffer, nullptr));
2115 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 CreateAnswerAsLocalDescription();
2117 ASSERT_TRUE(pc_->local_description() != NULL);
2118 ASSERT_TRUE(pc_->remote_description() != NULL);
2119
2120 const cricket::ContentInfo* content =
2121 cricket::GetFirstAudioContent(pc_->local_description()->description());
2122 ASSERT_TRUE(content != NULL);
2123 EXPECT_FALSE(content->rejected);
2124
2125 content =
2126 cricket::GetFirstVideoContent(pc_->local_description()->description());
2127 ASSERT_TRUE(content != NULL);
2128 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002129#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 content =
2131 cricket::GetFirstDataContent(pc_->local_description()->description());
2132 ASSERT_TRUE(content != NULL);
2133 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002134#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135}
2136
zhihuangb19012e2017-09-19 13:47:59 -07002137// Test that fallback from DTLS to SDES is not supported.
2138// The fallback was previously supported but was removed to simplify the code
2139// and because it's non-standard.
2140TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002141 FakeConstraints constraints;
2142 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2143 true);
2144 CreatePeerConnection(&constraints);
2145 // Wait for fake certificate to be generated. Previously, this is what caused
2146 // the "a=crypto" lines to be rejected.
2147 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2148 ASSERT_NE(nullptr, fake_certificate_generator_);
2149 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2150 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002151 std::unique_ptr<SessionDescriptionInterface> desc(
2152 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2153 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002154 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002155}
2156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157// Test that we can create an audio only offer and receive an answer with a
2158// limited set of audio codecs and receive an updated offer with more audio
2159// codecs, where the added codecs are not supported.
2160TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002161 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 AddVoiceStream("audio_label");
2163 CreateOfferAsLocalDescription();
2164
Steve Antondb45ca82017-09-11 18:27:34 -07002165 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002167 webrtc::kAudioSdp, nullptr));
2168 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169
Steve Antondb45ca82017-09-11 18:27:34 -07002170 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002172 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002173 nullptr));
2174 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 CreateAnswerAsLocalDescription();
2176}
2177
deadbeefc80741f2015-10-22 13:14:45 -07002178// Test that if we're receiving (but not sending) a track, subsequent offers
2179// will have m-lines with a=recvonly.
2180TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2181 FakeConstraints constraints;
2182 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2183 true);
2184 CreatePeerConnection(&constraints);
2185 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2186 CreateAnswerAsLocalDescription();
2187
2188 // At this point we should be receiving stream 1, but not sending anything.
2189 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002190 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002191 DoCreateOffer(&offer, nullptr);
2192
2193 const cricket::ContentInfo* video_content =
2194 cricket::GetFirstVideoContent(offer->description());
2195 const cricket::VideoContentDescription* video_desc =
2196 static_cast<const cricket::VideoContentDescription*>(
2197 video_content->description);
2198 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2199
2200 const cricket::ContentInfo* audio_content =
2201 cricket::GetFirstAudioContent(offer->description());
2202 const cricket::AudioContentDescription* audio_desc =
2203 static_cast<const cricket::AudioContentDescription*>(
2204 audio_content->description);
2205 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2206}
2207
2208// Test that if we're receiving (but not sending) a track, and the
2209// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2210// false, the generated m-lines will be a=inactive.
2211TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2212 FakeConstraints constraints;
2213 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2214 true);
2215 CreatePeerConnection(&constraints);
2216 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2217 CreateAnswerAsLocalDescription();
2218
2219 // At this point we should be receiving stream 1, but not sending anything.
2220 // A new offer would be recvonly, but we'll set the "no receive" constraints
2221 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002222 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002223 FakeConstraints offer_constraints;
2224 offer_constraints.AddMandatory(
2225 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2226 offer_constraints.AddMandatory(
2227 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2228 DoCreateOffer(&offer, &offer_constraints);
2229
2230 const cricket::ContentInfo* video_content =
2231 cricket::GetFirstVideoContent(offer->description());
2232 const cricket::VideoContentDescription* video_desc =
2233 static_cast<const cricket::VideoContentDescription*>(
2234 video_content->description);
2235 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2236
2237 const cricket::ContentInfo* audio_content =
2238 cricket::GetFirstAudioContent(offer->description());
2239 const cricket::AudioContentDescription* audio_desc =
2240 static_cast<const cricket::AudioContentDescription*>(
2241 audio_content->description);
2242 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2243}
2244
deadbeef653b8e02015-11-11 12:55:10 -08002245// Test that we can use SetConfiguration to change the ICE servers of the
2246// PortAllocator.
2247TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2248 CreatePeerConnection();
2249
2250 PeerConnectionInterface::RTCConfiguration config;
2251 PeerConnectionInterface::IceServer server;
2252 server.uri = "stun:test_hostname";
2253 config.servers.push_back(server);
2254 EXPECT_TRUE(pc_->SetConfiguration(config));
2255
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002256 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2257 EXPECT_EQ("test_hostname",
2258 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002259}
2260
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002261TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2262 CreatePeerConnection();
2263 PeerConnectionInterface::RTCConfiguration config;
2264 config.type = PeerConnectionInterface::kRelay;
2265 EXPECT_TRUE(pc_->SetConfiguration(config));
2266 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2267}
2268
deadbeef293e9262017-01-11 12:28:30 -08002269TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2270 PeerConnectionInterface::RTCConfiguration config;
2271 config.prune_turn_ports = false;
2272 CreatePeerConnection(config, nullptr);
2273 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2274
2275 config.prune_turn_ports = true;
2276 EXPECT_TRUE(pc_->SetConfiguration(config));
2277 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2278}
2279
skvladd1f5fda2017-02-03 16:54:05 -08002280// Test that the ice check interval can be changed. This does not verify that
2281// the setting makes it all the way to P2PTransportChannel, as that would
2282// require a very complex set of mocks.
2283TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2284 PeerConnectionInterface::RTCConfiguration config;
2285 config.ice_check_min_interval = rtc::Optional<int>();
2286 CreatePeerConnection(config, nullptr);
2287 config.ice_check_min_interval = rtc::Optional<int>(100);
2288 EXPECT_TRUE(pc_->SetConfiguration(config));
2289 PeerConnectionInterface::RTCConfiguration new_config =
2290 pc_->GetConfiguration();
2291 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2292}
2293
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002294// Test that when SetConfiguration changes both the pool size and other
2295// attributes, the pooled session is created with the updated attributes.
2296TEST_F(PeerConnectionInterfaceTest,
2297 SetConfigurationCreatesPooledSessionCorrectly) {
2298 CreatePeerConnection();
2299 PeerConnectionInterface::RTCConfiguration config;
2300 config.ice_candidate_pool_size = 1;
2301 PeerConnectionInterface::IceServer server;
2302 server.uri = kStunAddressOnly;
2303 config.servers.push_back(server);
2304 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002305 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002306
2307 const cricket::FakePortAllocatorSession* session =
2308 static_cast<const cricket::FakePortAllocatorSession*>(
2309 port_allocator_->GetPooledSession());
2310 ASSERT_NE(nullptr, session);
2311 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002312}
2313
deadbeef293e9262017-01-11 12:28:30 -08002314// Test that after SetLocalDescription, changing the pool size is not allowed,
2315// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002316TEST_F(PeerConnectionInterfaceTest,
2317 CantChangePoolSizeAfterSetLocalDescription) {
2318 CreatePeerConnection();
2319 // Start by setting a size of 1.
2320 PeerConnectionInterface::RTCConfiguration config;
2321 config.ice_candidate_pool_size = 1;
2322 EXPECT_TRUE(pc_->SetConfiguration(config));
2323
2324 // Set remote offer; can still change pool size at this point.
2325 CreateOfferAsRemoteDescription();
2326 config.ice_candidate_pool_size = 2;
2327 EXPECT_TRUE(pc_->SetConfiguration(config));
2328
2329 // Set local answer; now it's too late.
2330 CreateAnswerAsLocalDescription();
2331 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002332 RTCError error;
2333 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2334 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2335}
2336
deadbeef42a42632017-03-10 15:18:00 -08002337// Test that after setting an answer, extra pooled sessions are discarded. The
2338// ICE candidate pool is only intended to be used for the first offer/answer.
2339TEST_F(PeerConnectionInterfaceTest,
2340 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2341 CreatePeerConnection();
2342
2343 // Set a larger-than-necessary size.
2344 PeerConnectionInterface::RTCConfiguration config;
2345 config.ice_candidate_pool_size = 4;
2346 EXPECT_TRUE(pc_->SetConfiguration(config));
2347
2348 // Do offer/answer.
2349 CreateOfferAsRemoteDescription();
2350 CreateAnswerAsLocalDescription();
2351
2352 // Expect no pooled sessions to be left.
2353 const cricket::PortAllocatorSession* session =
2354 port_allocator_->GetPooledSession();
2355 EXPECT_EQ(nullptr, session);
2356}
2357
2358// After Close is called, pooled candidates should be discarded so as to not
2359// waste network resources.
2360TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2361 CreatePeerConnection();
2362
2363 PeerConnectionInterface::RTCConfiguration config;
2364 config.ice_candidate_pool_size = 3;
2365 EXPECT_TRUE(pc_->SetConfiguration(config));
2366 pc_->Close();
2367
2368 // Expect no pooled sessions to be left.
2369 const cricket::PortAllocatorSession* session =
2370 port_allocator_->GetPooledSession();
2371 EXPECT_EQ(nullptr, session);
2372}
2373
deadbeef293e9262017-01-11 12:28:30 -08002374// Test that SetConfiguration returns an invalid modification error if
2375// modifying a field in the configuration that isn't allowed to be modified.
2376TEST_F(PeerConnectionInterfaceTest,
2377 SetConfigurationReturnsInvalidModificationError) {
2378 PeerConnectionInterface::RTCConfiguration config;
2379 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2380 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2381 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2382 CreatePeerConnection(config, nullptr);
2383
2384 PeerConnectionInterface::RTCConfiguration modified_config = config;
2385 modified_config.bundle_policy =
2386 PeerConnectionInterface::kBundlePolicyMaxBundle;
2387 RTCError error;
2388 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2389 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2390
2391 modified_config = config;
2392 modified_config.rtcp_mux_policy =
2393 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2394 error.set_type(RTCErrorType::NONE);
2395 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2396 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2397
2398 modified_config = config;
2399 modified_config.continual_gathering_policy =
2400 PeerConnectionInterface::GATHER_CONTINUALLY;
2401 error.set_type(RTCErrorType::NONE);
2402 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2403 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2404}
2405
2406// Test that SetConfiguration returns a range error if the candidate pool size
2407// is negative or larger than allowed by the spec.
2408TEST_F(PeerConnectionInterfaceTest,
2409 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2410 PeerConnectionInterface::RTCConfiguration config;
2411 CreatePeerConnection(config, nullptr);
2412
2413 config.ice_candidate_pool_size = -1;
2414 RTCError error;
2415 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2416 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2417
2418 config.ice_candidate_pool_size = INT_MAX;
2419 error.set_type(RTCErrorType::NONE);
2420 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2421 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2422}
2423
2424// Test that SetConfiguration returns a syntax error if parsing an ICE server
2425// URL failed.
2426TEST_F(PeerConnectionInterfaceTest,
2427 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2428 PeerConnectionInterface::RTCConfiguration config;
2429 CreatePeerConnection(config, nullptr);
2430
2431 PeerConnectionInterface::IceServer bad_server;
2432 bad_server.uri = "stunn:www.example.com";
2433 config.servers.push_back(bad_server);
2434 RTCError error;
2435 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2436 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2437}
2438
2439// Test that SetConfiguration returns an invalid parameter error if a TURN
2440// IceServer is missing a username or password.
2441TEST_F(PeerConnectionInterfaceTest,
2442 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2443 PeerConnectionInterface::RTCConfiguration config;
2444 CreatePeerConnection(config, nullptr);
2445
2446 PeerConnectionInterface::IceServer bad_server;
2447 bad_server.uri = "turn:www.example.com";
2448 // Missing password.
2449 bad_server.username = "foo";
2450 config.servers.push_back(bad_server);
2451 RTCError error;
2452 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2453 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002454}
2455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456// Test that PeerConnection::Close changes the states to closed and all remote
2457// tracks change state to ended.
2458TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2459 // Initialize a PeerConnection and negotiate local and remote session
2460 // description.
2461 InitiateCall();
2462 ASSERT_EQ(1u, pc_->local_streams()->count());
2463 ASSERT_EQ(1u, pc_->remote_streams()->count());
2464
2465 pc_->Close();
2466
2467 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2468 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2469 pc_->ice_connection_state());
2470 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2471 pc_->ice_gathering_state());
2472
2473 EXPECT_EQ(1u, pc_->local_streams()->count());
2474 EXPECT_EQ(1u, pc_->remote_streams()->count());
2475
zhihuang9763d562016-08-05 11:14:50 -07002476 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2477 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002478 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002479 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002480 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2481 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2482 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483}
2484
2485// Test that PeerConnection methods fails gracefully after
2486// PeerConnection::Close has been called.
2487TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002488 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002489 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2490 CreateOfferAsRemoteDescription();
2491 CreateAnswerAsLocalDescription();
2492
2493 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002494 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495 pc_->local_streams()->at(0);
2496
2497 pc_->Close();
2498
2499 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002500 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501
2502 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002503 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002505 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506
2507 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2508
2509 EXPECT_TRUE(pc_->local_description() != NULL);
2510 EXPECT_TRUE(pc_->remote_description() != NULL);
2511
kwibergd1fe2812016-04-27 06:47:29 -07002512 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002513 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002514 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002515 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516
2517 std::string sdp;
2518 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002519 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2520 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2521 nullptr));
2522 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523
2524 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002525 std::unique_ptr<SessionDescriptionInterface> local_offer(
2526 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2527 nullptr));
2528 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002529}
2530
2531// Test that GetStats can still be called after PeerConnection::Close.
2532TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2533 InitiateCall();
2534 pc_->Close();
2535 DoGetStats(NULL);
2536}
deadbeefab9b2d12015-10-14 11:33:11 -07002537
2538// NOTE: The series of tests below come from what used to be
2539// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2540// setting a remote or local description has the expected effects.
2541
2542// This test verifies that the remote MediaStreams corresponding to a received
2543// SDP string is created. In this test the two separate MediaStreams are
2544// signaled.
2545TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2546 FakeConstraints constraints;
2547 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2548 true);
2549 CreatePeerConnection(&constraints);
2550 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2551
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002552 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002553 EXPECT_TRUE(
2554 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2555 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2556 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2557
2558 // Create a session description based on another SDP with another
2559 // MediaStream.
2560 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2561
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002562 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002563 EXPECT_TRUE(
2564 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2565}
2566
2567// This test verifies that when remote tracks are added/removed from SDP, the
2568// created remote streams are updated appropriately.
2569TEST_F(PeerConnectionInterfaceTest,
2570 AddRemoveTrackFromExistingRemoteMediaStream) {
2571 FakeConstraints constraints;
2572 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2573 true);
2574 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002575 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002576 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002577 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002578 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2579 reference_collection_));
2580
2581 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002582 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002583 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002584 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002585 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2586 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002587 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002588 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2589 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002590 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002591 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2592 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002593
2594 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002595 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002596 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002597 MockTrackObserver audio_track_observer(audio_track2);
2598 MockTrackObserver video_track_observer(video_track2);
2599
2600 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2601 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002602 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002603 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2604 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002605 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002606 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002607 audio_track2->state(), kTimeout);
2608 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2609 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002610}
2611
2612// This tests that remote tracks are ended if a local session description is set
2613// that rejects the media content type.
2614TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2615 FakeConstraints constraints;
2616 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2617 true);
2618 CreatePeerConnection(&constraints);
2619 // First create and set a remote offer, then reject its video content in our
2620 // answer.
2621 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2622 ASSERT_EQ(1u, observer_.remote_streams()->count());
2623 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2624 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2625 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2626
2627 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2628 remote_stream->GetVideoTracks()[0];
2629 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2630 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2631 remote_stream->GetAudioTracks()[0];
2632 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2633
kwibergd1fe2812016-04-27 06:47:29 -07002634 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002635 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002636 cricket::ContentInfo* video_info =
2637 local_answer->description()->GetContentByName("video");
2638 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002639 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002640 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2641 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2642
2643 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002644 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002645 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002646 video_info = local_offer->description()->GetContentByName("video");
2647 ASSERT_TRUE(video_info != nullptr);
2648 video_info->rejected = true;
2649 cricket::ContentInfo* audio_info =
2650 local_offer->description()->GetContentByName("audio");
2651 ASSERT_TRUE(audio_info != nullptr);
2652 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002653 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002654 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002655 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002656 remote_audio->state(), kTimeout);
2657 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2658 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002659}
2660
2661// This tests that we won't crash if the remote track has been removed outside
2662// of PeerConnection and then PeerConnection tries to reject the track.
2663TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2664 FakeConstraints constraints;
2665 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2666 true);
2667 CreatePeerConnection(&constraints);
2668 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2669 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2670 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2671 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2672
kwibergd1fe2812016-04-27 06:47:29 -07002673 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002674 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2675 kSdpStringWithStream1, nullptr));
2676 cricket::ContentInfo* video_info =
2677 local_answer->description()->GetContentByName("video");
2678 video_info->rejected = true;
2679 cricket::ContentInfo* audio_info =
2680 local_answer->description()->GetContentByName("audio");
2681 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002682 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002683
2684 // No crash is a pass.
2685}
2686
deadbeef5e97fb52015-10-15 12:49:08 -07002687// This tests that if a recvonly remote description is set, no remote streams
2688// will be created, even if the description contains SSRCs/MSIDs.
2689// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2690TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2691 FakeConstraints constraints;
2692 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2693 true);
2694 CreatePeerConnection(&constraints);
2695
2696 std::string recvonly_offer = kSdpStringWithStream1;
2697 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2698 strlen(kRecvonly), &recvonly_offer);
2699 CreateAndSetRemoteOffer(recvonly_offer);
2700
2701 EXPECT_EQ(0u, observer_.remote_streams()->count());
2702}
2703
deadbeefab9b2d12015-10-14 11:33:11 -07002704// This tests that a default MediaStream is created if a remote session
2705// description doesn't contain any streams and no MSID support.
2706// It also tests that the default stream is updated if a video m-line is added
2707// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002708TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002709 FakeConstraints constraints;
2710 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2711 true);
2712 CreatePeerConnection(&constraints);
2713 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2714
2715 ASSERT_EQ(1u, observer_.remote_streams()->count());
2716 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2717
2718 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2719 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2720 EXPECT_EQ("default", remote_stream->label());
2721
2722 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2723 ASSERT_EQ(1u, observer_.remote_streams()->count());
2724 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2725 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002726 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2727 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002728 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2729 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002730 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2731 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002732}
2733
2734// This tests that a default MediaStream is created if a remote session
2735// description doesn't contain any streams and media direction is send only.
2736TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002737 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002738 FakeConstraints constraints;
2739 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2740 true);
2741 CreatePeerConnection(&constraints);
2742 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2743
2744 ASSERT_EQ(1u, observer_.remote_streams()->count());
2745 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2746
2747 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2748 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2749 EXPECT_EQ("default", remote_stream->label());
2750}
2751
2752// This tests that it won't crash when PeerConnection tries to remove
2753// a remote track that as already been removed from the MediaStream.
2754TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2755 FakeConstraints constraints;
2756 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2757 true);
2758 CreatePeerConnection(&constraints);
2759 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2760 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2761 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2762 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2763
2764 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2765
2766 // No crash is a pass.
2767}
2768
2769// This tests that a default MediaStream is created if the remote session
2770// description doesn't contain any streams and don't contain an indication if
2771// MSID is supported.
2772TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002773 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002774 FakeConstraints constraints;
2775 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2776 true);
2777 CreatePeerConnection(&constraints);
2778 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2779
2780 ASSERT_EQ(1u, observer_.remote_streams()->count());
2781 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2782 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2783 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2784}
2785
2786// This tests that a default MediaStream is not created if the remote session
2787// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002788TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002789 FakeConstraints constraints;
2790 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2791 true);
2792 CreatePeerConnection(&constraints);
2793 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2794 EXPECT_EQ(0u, observer_.remote_streams()->count());
2795}
2796
deadbeefbda7e0b2015-12-08 17:13:40 -08002797// This tests that when setting a new description, the old default tracks are
2798// not destroyed and recreated.
2799// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002800TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002801 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002802 FakeConstraints constraints;
2803 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2804 true);
2805 CreatePeerConnection(&constraints);
2806 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2807
2808 ASSERT_EQ(1u, observer_.remote_streams()->count());
2809 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2810 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2811
2812 // Set the track to "disabled", then set a new description and ensure the
2813 // track is still disabled, which ensures it hasn't been recreated.
2814 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2815 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2816 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2817 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2818}
2819
deadbeefab9b2d12015-10-14 11:33:11 -07002820// This tests that a default MediaStream is not created if a remote session
2821// description is updated to not have any MediaStreams.
2822TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2823 FakeConstraints constraints;
2824 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2825 true);
2826 CreatePeerConnection(&constraints);
2827 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002828 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002829 EXPECT_TRUE(
2830 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2831
2832 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2833 EXPECT_EQ(0u, observer_.remote_streams()->count());
2834}
2835
2836// This tests that an RtpSender is created when the local description is set
2837// after adding a local stream.
2838// TODO(deadbeef): This test and the one below it need to be updated when
2839// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002840TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002841 FakeConstraints constraints;
2842 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2843 true);
2844 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002845
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002846 // Create an offer with 1 stream with 2 tracks of each type.
2847 rtc::scoped_refptr<StreamCollection> stream_collection =
2848 CreateStreamCollection(1, 2);
2849 pc_->AddStream(stream_collection->at(0));
2850 std::unique_ptr<SessionDescriptionInterface> offer;
2851 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002852 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002853
deadbeefab9b2d12015-10-14 11:33:11 -07002854 auto senders = pc_->GetSenders();
2855 EXPECT_EQ(4u, senders.size());
2856 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2857 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2858 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2859 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2860
2861 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002862 pc_->RemoveStream(stream_collection->at(0));
2863 stream_collection = CreateStreamCollection(1, 1);
2864 pc_->AddStream(stream_collection->at(0));
2865 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002866 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002867
deadbeefab9b2d12015-10-14 11:33:11 -07002868 senders = pc_->GetSenders();
2869 EXPECT_EQ(2u, senders.size());
2870 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2871 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2872 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2873 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2874}
2875
2876// This tests that an RtpSender is created when the local description is set
2877// before adding a local stream.
2878TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002879 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002880 FakeConstraints constraints;
2881 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2882 true);
2883 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002884
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002885 rtc::scoped_refptr<StreamCollection> stream_collection =
2886 CreateStreamCollection(1, 2);
2887 // Add a stream to create the offer, but remove it afterwards.
2888 pc_->AddStream(stream_collection->at(0));
2889 std::unique_ptr<SessionDescriptionInterface> offer;
2890 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2891 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002892
Steve Antondb45ca82017-09-11 18:27:34 -07002893 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002894 auto senders = pc_->GetSenders();
2895 EXPECT_EQ(0u, senders.size());
2896
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002897 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002898 senders = pc_->GetSenders();
2899 EXPECT_EQ(4u, senders.size());
2900 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2901 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2902 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2903 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2904}
2905
2906// This tests that the expected behavior occurs if the SSRC on a local track is
2907// changed when SetLocalDescription is called.
2908TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002909 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002910 FakeConstraints constraints;
2911 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2912 true);
2913 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002914
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002915 rtc::scoped_refptr<StreamCollection> stream_collection =
2916 CreateStreamCollection(2, 1);
2917 pc_->AddStream(stream_collection->at(0));
2918 std::unique_ptr<SessionDescriptionInterface> offer;
2919 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2920 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002921 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2922 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002923 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2924 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002925 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002926
deadbeefab9b2d12015-10-14 11:33:11 -07002927 auto senders = pc_->GetSenders();
2928 EXPECT_EQ(2u, senders.size());
2929 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2930 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2931
2932 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002933 cricket::MediaContentDescription* desc =
2934 cricket::GetFirstAudioContentDescription(modified_offer->description());
2935 ASSERT_TRUE(desc != NULL);
2936 for (StreamParams& stream : desc->mutable_streams()) {
2937 for (unsigned int& ssrc : stream.ssrcs) {
2938 ++ssrc;
2939 }
2940 }
deadbeefab9b2d12015-10-14 11:33:11 -07002941
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 desc =
2943 cricket::GetFirstVideoContentDescription(modified_offer->description());
2944 ASSERT_TRUE(desc != NULL);
2945 for (StreamParams& stream : desc->mutable_streams()) {
2946 for (unsigned int& ssrc : stream.ssrcs) {
2947 ++ssrc;
2948 }
2949 }
2950
Steve Antondb45ca82017-09-11 18:27:34 -07002951 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002952 senders = pc_->GetSenders();
2953 EXPECT_EQ(2u, senders.size());
2954 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2955 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2956 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2957 // changed.
2958}
2959
2960// This tests that the expected behavior occurs if a new session description is
2961// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002962TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002963 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002964 FakeConstraints constraints;
2965 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2966 true);
2967 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002968
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002969 rtc::scoped_refptr<StreamCollection> stream_collection =
2970 CreateStreamCollection(2, 1);
2971 pc_->AddStream(stream_collection->at(0));
2972 std::unique_ptr<SessionDescriptionInterface> offer;
2973 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002974 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002975
deadbeefab9b2d12015-10-14 11:33:11 -07002976 auto senders = pc_->GetSenders();
2977 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2979 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002980
2981 // Add a new MediaStream but with the same tracks as in the first stream.
2982 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2983 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002984 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2985 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002986 pc_->AddStream(stream_1);
2987
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002988 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002989 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002990
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002991 auto new_senders = pc_->GetSenders();
2992 // Should be the same senders as before, but with updated stream id.
2993 // Note that this behavior is subject to change in the future.
2994 // We may decide the PC should ignore existing tracks in AddStream.
2995 EXPECT_EQ(senders, new_senders);
2996 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2997 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002998}
2999
zhihuang81c3a032016-11-17 12:06:24 -08003000// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3001TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3002 FakeConstraints constraints;
3003 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3004 true);
3005 CreatePeerConnection(&constraints);
3006 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3007 EXPECT_EQ(observer_.num_added_tracks_, 1);
3008 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3009
3010 // Create and set the updated remote SDP.
3011 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3012 EXPECT_EQ(observer_.num_added_tracks_, 2);
3013 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3014}
3015
deadbeefd1a38b52016-12-10 13:15:33 -08003016// Test that when SetConfiguration is called and the configuration is
3017// changing, the next offer causes an ICE restart.
3018TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3019 PeerConnectionInterface::RTCConfiguration config;
3020 config.type = PeerConnectionInterface::kRelay;
3021 // Need to pass default constraints to prevent disabling of DTLS...
3022 FakeConstraints default_constraints;
3023 CreatePeerConnection(config, &default_constraints);
3024 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3025
3026 // Do initial offer/answer so there's something to restart.
3027 CreateOfferAsLocalDescription();
3028 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3029
3030 // Grab the ufrags.
3031 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3032
3033 // Change ICE policy, which should trigger an ICE restart on the next offer.
3034 config.type = PeerConnectionInterface::kAll;
3035 EXPECT_TRUE(pc_->SetConfiguration(config));
3036 CreateOfferAsLocalDescription();
3037
3038 // Grab the new ufrags.
3039 std::vector<std::string> subsequent_ufrags =
3040 GetUfrags(pc_->local_description());
3041
3042 // Sanity check.
3043 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3044 // Check that each ufrag is different.
3045 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3046 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3047 }
3048}
3049
3050// Test that when SetConfiguration is called and the configuration *isn't*
3051// changing, the next offer does *not* cause an ICE restart.
3052TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3053 PeerConnectionInterface::RTCConfiguration config;
3054 config.type = PeerConnectionInterface::kRelay;
3055 // Need to pass default constraints to prevent disabling of DTLS...
3056 FakeConstraints default_constraints;
3057 CreatePeerConnection(config, &default_constraints);
3058 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3059
3060 // Do initial offer/answer so there's something to restart.
3061 CreateOfferAsLocalDescription();
3062 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3063
3064 // Grab the ufrags.
3065 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3066
3067 // Call SetConfiguration with a config identical to what the PC was
3068 // constructed with.
3069 EXPECT_TRUE(pc_->SetConfiguration(config));
3070 CreateOfferAsLocalDescription();
3071
3072 // Grab the new ufrags.
3073 std::vector<std::string> subsequent_ufrags =
3074 GetUfrags(pc_->local_description());
3075
3076 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3077}
3078
3079// Test for a weird corner case scenario:
3080// 1. Audio/video session established.
3081// 2. SetConfiguration changes ICE config; ICE restart needed.
3082// 3. ICE restart initiated by remote peer, but only for one m= section.
3083// 4. Next createOffer should initiate an ICE restart, but only for the other
3084// m= section; it would be pointless to do an ICE restart for the m= section
3085// that was already restarted.
3086TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3087 PeerConnectionInterface::RTCConfiguration config;
3088 config.type = PeerConnectionInterface::kRelay;
3089 // Need to pass default constraints to prevent disabling of DTLS...
3090 FakeConstraints default_constraints;
3091 CreatePeerConnection(config, &default_constraints);
3092 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3093
3094 // Do initial offer/answer so there's something to restart.
3095 CreateOfferAsLocalDescription();
3096 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3097
3098 // Change ICE policy, which should set the "needs-ice-restart" flag.
3099 config.type = PeerConnectionInterface::kAll;
3100 EXPECT_TRUE(pc_->SetConfiguration(config));
3101
3102 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003103 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3104 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3105 kSdpStringWithStream1, nullptr));
3106 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003107 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3108 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003109 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003110 CreateAnswerAsLocalDescription();
3111
3112 // Grab the ufrags.
3113 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3114 ASSERT_EQ(2, initial_ufrags.size());
3115
3116 // Create offer and grab the new ufrags.
3117 CreateOfferAsLocalDescription();
3118 std::vector<std::string> subsequent_ufrags =
3119 GetUfrags(pc_->local_description());
3120 ASSERT_EQ(2, subsequent_ufrags.size());
3121
3122 // Ensure that only the ufrag for the second m= section changed.
3123 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3124 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3125}
3126
deadbeeffe4a8a42016-12-20 17:56:17 -08003127// Tests that the methods to return current/pending descriptions work as
3128// expected at different points in the offer/answer exchange. This test does
3129// one offer/answer exchange as the offerer, then another as the answerer.
3130TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3131 // This disables DTLS so we can apply an answer to ourselves.
3132 CreatePeerConnection();
3133
3134 // Create initial local offer and get SDP (which will also be used as
3135 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003136 std::unique_ptr<SessionDescriptionInterface> local_offer;
3137 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003138 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003139 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003140
3141 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003142 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3143 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3144 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003145 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3146 EXPECT_EQ(nullptr, pc_->current_local_description());
3147 EXPECT_EQ(nullptr, pc_->current_remote_description());
3148
3149 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003150 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003151 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003152 sdp, nullptr));
3153 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3154 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3155 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3156 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003157 EXPECT_EQ(nullptr, pc_->current_local_description());
3158 EXPECT_EQ(nullptr, pc_->current_remote_description());
3159
3160 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003161 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3162 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3163 sdp, nullptr));
3164 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3165 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003166 EXPECT_EQ(nullptr, pc_->pending_local_description());
3167 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003168 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3169 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003170
3171 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003172 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3173 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3174 nullptr));
3175 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3176 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3177 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003178 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003179 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3180 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003181
3182 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003183 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003184 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003185 sdp, nullptr));
3186 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3187 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3188 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3189 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3190 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3191 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003192
3193 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003194 std::unique_ptr<SessionDescriptionInterface> local_answer(
3195 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3196 sdp, nullptr));
3197 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3198 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003199 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3200 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003201 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3202 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003203}
3204
zhihuang77985012017-02-07 15:45:16 -08003205// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3206// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003207// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003208TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003209 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003210 CreatePeerConnection();
3211 // The RtcEventLog will be reset when the PeerConnection is closed.
3212 pc_->Close();
3213
Elad Alon9e6565b2017-10-11 16:04:13 +02003214 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3215 std::string filename = webrtc::test::OutputPath() +
3216 test_info->test_case_name() + test_info->name();
3217 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3218
3219 constexpr int64_t max_size_bytes = 1024;
3220
zhihuang77985012017-02-07 15:45:16 -08003221 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3222 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003223
3224 // Cleanup.
3225 rtc::ClosePlatformFile(file);
3226 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003227}
3228
Elad Alon99c3fe52017-10-13 16:29:40 +02003229// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3230// after the PeerConnection is closed.
3231// This version tests the StartRtcEventLog version that receives an object
3232// of type |RtcEventLogOutput|.
3233TEST_F(PeerConnectionInterfaceTest,
3234 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3235 CreatePeerConnection();
3236 // The RtcEventLog will be reset when the PeerConnection is closed.
3237 pc_->Close();
3238
3239 rtc::PlatformFile file = 0;
3240 int64_t max_size_bytes = 1024;
3241 EXPECT_FALSE(pc_->StartRtcEventLog(
3242 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes)));
3243 pc_->StopRtcEventLog();
3244}
3245
deadbeef30952b42017-04-21 02:41:29 -07003246// Test that generated offers/answers include "ice-option:trickle".
3247TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3248 CreatePeerConnection();
3249
3250 // First, create an offer with audio/video.
3251 FakeConstraints constraints;
3252 constraints.SetMandatoryReceiveAudio(true);
3253 constraints.SetMandatoryReceiveVideo(true);
3254 std::unique_ptr<SessionDescriptionInterface> offer;
3255 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3256 cricket::SessionDescription* desc = offer->description();
3257 ASSERT_EQ(2u, desc->transport_infos().size());
3258 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3259 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3260
3261 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003262 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003263 std::unique_ptr<SessionDescriptionInterface> answer;
3264 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3265 desc = answer->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
deadbeef1dcb1642017-03-29 21:08:16 -07003271// Test that ICE renomination isn't offered if it's not enabled in the PC's
3272// RTCConfiguration.
3273TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3274 PeerConnectionInterface::RTCConfiguration config;
3275 config.enable_ice_renomination = false;
3276 CreatePeerConnection(config, nullptr);
3277 AddVoiceStream("foo");
3278
3279 std::unique_ptr<SessionDescriptionInterface> offer;
3280 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3281 cricket::SessionDescription* desc = offer->description();
3282 EXPECT_EQ(1u, desc->transport_infos().size());
3283 EXPECT_FALSE(
3284 desc->transport_infos()[0].description.GetIceParameters().renomination);
3285}
3286
3287// Test that the ICE renomination option is present in generated offers/answers
3288// if it's enabled in the PC's RTCConfiguration.
3289TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3290 PeerConnectionInterface::RTCConfiguration config;
3291 config.enable_ice_renomination = true;
3292 CreatePeerConnection(config, nullptr);
3293 AddVoiceStream("foo");
3294
3295 std::unique_ptr<SessionDescriptionInterface> offer;
3296 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3297 cricket::SessionDescription* desc = offer->description();
3298 EXPECT_EQ(1u, desc->transport_infos().size());
3299 EXPECT_TRUE(
3300 desc->transport_infos()[0].description.GetIceParameters().renomination);
3301
3302 // Set the offer as a remote description, then create an answer and ensure it
3303 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003304 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003305 std::unique_ptr<SessionDescriptionInterface> answer;
3306 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3307 desc = answer->description();
3308 EXPECT_EQ(1u, desc->transport_infos().size());
3309 EXPECT_TRUE(
3310 desc->transport_infos()[0].description.GetIceParameters().renomination);
3311}
3312
3313// Test that if CreateOffer is called with the deprecated "offer to receive
3314// audio/video" constraints, they're processed and result in an offer with
3315// audio/video sections just as if RTCOfferAnswerOptions had been used.
3316TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3317 CreatePeerConnection();
3318
3319 FakeConstraints constraints;
3320 constraints.SetMandatoryReceiveAudio(true);
3321 constraints.SetMandatoryReceiveVideo(true);
3322 std::unique_ptr<SessionDescriptionInterface> offer;
3323 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3324
3325 cricket::SessionDescription* desc = offer->description();
3326 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3327 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3328 ASSERT_NE(nullptr, audio);
3329 ASSERT_NE(nullptr, video);
3330 EXPECT_FALSE(audio->rejected);
3331 EXPECT_FALSE(video->rejected);
3332}
3333
3334// Test that if CreateAnswer is called with the deprecated "offer to receive
3335// audio/video" constraints, they're processed and can be used to reject an
3336// offered m= section just as can be done with RTCOfferAnswerOptions;
3337TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3338 CreatePeerConnection();
3339
3340 // First, create an offer with audio/video and apply it as a remote
3341 // description.
3342 FakeConstraints constraints;
3343 constraints.SetMandatoryReceiveAudio(true);
3344 constraints.SetMandatoryReceiveVideo(true);
3345 std::unique_ptr<SessionDescriptionInterface> offer;
3346 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003347 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003348
3349 // Now create answer that rejects audio/video.
3350 constraints.SetMandatoryReceiveAudio(false);
3351 constraints.SetMandatoryReceiveVideo(false);
3352 std::unique_ptr<SessionDescriptionInterface> answer;
3353 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3354
3355 cricket::SessionDescription* desc = answer->description();
3356 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3357 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3358 ASSERT_NE(nullptr, audio);
3359 ASSERT_NE(nullptr, video);
3360 EXPECT_TRUE(audio->rejected);
3361 EXPECT_TRUE(video->rejected);
3362}
3363
3364#ifdef HAVE_SCTP
3365#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3366 DataChannelOnlyOfferWithMaxBundlePolicy
3367#else
3368#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3369 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3370#endif
3371
3372// Test that negotiation can succeed with a data channel only, and with the max
3373// bundle policy. Previously there was a bug that prevented this.
3374TEST_F(PeerConnectionInterfaceTest,
3375 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3376 PeerConnectionInterface::RTCConfiguration config;
3377 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3378 CreatePeerConnection(config, nullptr);
3379
3380 // First, create an offer with only a data channel and apply it as a remote
3381 // description.
3382 pc_->CreateDataChannel("test", nullptr);
3383 std::unique_ptr<SessionDescriptionInterface> offer;
3384 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003385 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003386
3387 // Create and set answer as well.
3388 std::unique_ptr<SessionDescriptionInterface> answer;
3389 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003390 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003391}
3392
zstein4b979802017-06-02 14:37:37 -07003393TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3394 CreatePeerConnection();
3395 PeerConnectionInterface::BitrateParameters bitrate;
3396 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3397 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3398}
3399
3400TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3401 CreatePeerConnection();
3402 PeerConnectionInterface::BitrateParameters bitrate;
3403 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3404 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3405}
3406
3407TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3408 CreatePeerConnection();
3409 PeerConnectionInterface::BitrateParameters bitrate;
3410 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3411 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3412 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3413}
3414
3415TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3416 CreatePeerConnection();
3417 PeerConnectionInterface::BitrateParameters bitrate;
3418 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3419 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3420}
3421
3422TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3423 CreatePeerConnection();
3424 PeerConnectionInterface::BitrateParameters bitrate;
3425 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3426 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3427 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3428}
3429
3430TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3431 CreatePeerConnection();
3432 PeerConnectionInterface::BitrateParameters bitrate;
3433 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3434 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3435 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3436}
3437
3438TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3439 CreatePeerConnection();
3440 PeerConnectionInterface::BitrateParameters bitrate;
3441 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3442 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3443}
3444
Steve Anton038834f2017-07-14 15:59:59 -07003445// ice_regather_interval_range requires WebRTC to be configured for continual
3446// gathering already.
3447TEST_F(PeerConnectionInterfaceTest,
3448 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3449 PeerConnectionInterface::RTCConfiguration config;
3450 config.ice_regather_interval_range.emplace(1000, 2000);
3451 config.continual_gathering_policy =
3452 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3453 CreatePeerConnectionExpectFail(config);
3454}
3455
3456// Ensures that there is no error when ice_regather_interval_range is set with
3457// continual gathering enabled.
3458TEST_F(PeerConnectionInterfaceTest,
3459 SetIceRegatherIntervalRangeWithContinualGathering) {
3460 PeerConnectionInterface::RTCConfiguration config;
3461 config.ice_regather_interval_range.emplace(1000, 2000);
3462 config.continual_gathering_policy =
3463 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3464 CreatePeerConnection(config, nullptr);
3465}
3466
zstein4b979802017-06-02 14:37:37 -07003467// The current bitrate from Call's BitrateConfigMask is currently clamped by
3468// Call's BitrateConfig, which comes from the SDP or a default value. This test
3469// checks that a call to SetBitrate with a current bitrate that will be clamped
3470// succeeds.
3471TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3472 CreatePeerConnection();
3473 PeerConnectionInterface::BitrateParameters bitrate;
3474 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3475 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3476}
3477
zhihuang1c378ed2017-08-17 14:10:50 -07003478// The following tests verify that the offer can be created correctly.
3479TEST_F(PeerConnectionInterfaceTest,
3480 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3481 RTCOfferAnswerOptions rtc_options;
3482
3483 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3484 // than kMaxOfferToReceiveMedia should be treated as invalid.
3485 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3486 CreatePeerConnection();
3487 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3488
3489 rtc_options.offer_to_receive_audio =
3490 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3491 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3492}
3493
3494TEST_F(PeerConnectionInterfaceTest,
3495 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3496 RTCOfferAnswerOptions rtc_options;
3497
3498 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3499 // than kMaxOfferToReceiveMedia should be treated as invalid.
3500 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3501 CreatePeerConnection();
3502 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3503
3504 rtc_options.offer_to_receive_video =
3505 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3506 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3507}
3508
3509// Test that the audio and video content will be added to an offer if both
3510// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3511TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3512 RTCOfferAnswerOptions rtc_options;
3513 rtc_options.offer_to_receive_audio = 1;
3514 rtc_options.offer_to_receive_video = 1;
3515
3516 std::unique_ptr<SessionDescriptionInterface> offer;
3517 CreatePeerConnection();
3518 offer = CreateOfferWithOptions(rtc_options);
3519 ASSERT_TRUE(offer);
3520 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3521 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3522}
3523
3524// Test that only audio content will be added to the offer if only
3525// |offer_to_receive_audio| options is 1.
3526TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3527 RTCOfferAnswerOptions rtc_options;
3528 rtc_options.offer_to_receive_audio = 1;
3529 rtc_options.offer_to_receive_video = 0;
3530
3531 std::unique_ptr<SessionDescriptionInterface> offer;
3532 CreatePeerConnection();
3533 offer = CreateOfferWithOptions(rtc_options);
3534 ASSERT_TRUE(offer);
3535 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3536 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3537}
3538
3539// Test that only video content will be added if only |offer_to_receive_video|
3540// options is 1.
3541TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3542 RTCOfferAnswerOptions rtc_options;
3543 rtc_options.offer_to_receive_audio = 0;
3544 rtc_options.offer_to_receive_video = 1;
3545
3546 std::unique_ptr<SessionDescriptionInterface> offer;
3547 CreatePeerConnection();
3548 offer = CreateOfferWithOptions(rtc_options);
3549 ASSERT_TRUE(offer);
3550 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3551 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3552}
3553
3554// Test that if |voice_activity_detection| is false, no CN codec is added to the
3555// offer.
3556TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3557 RTCOfferAnswerOptions rtc_options;
3558 rtc_options.offer_to_receive_audio = 1;
3559 rtc_options.offer_to_receive_video = 0;
3560
3561 std::unique_ptr<SessionDescriptionInterface> offer;
3562 CreatePeerConnection();
3563 offer = CreateOfferWithOptions(rtc_options);
3564 ASSERT_TRUE(offer);
3565 const cricket::ContentInfo* audio_content =
3566 offer->description()->GetContentByName(cricket::CN_AUDIO);
3567 ASSERT_TRUE(audio_content);
3568 // |voice_activity_detection| is true by default.
3569 EXPECT_TRUE(HasCNCodecs(audio_content));
3570
3571 rtc_options.voice_activity_detection = false;
3572 CreatePeerConnection();
3573 offer = CreateOfferWithOptions(rtc_options);
3574 ASSERT_TRUE(offer);
3575 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3576 ASSERT_TRUE(audio_content);
3577 EXPECT_FALSE(HasCNCodecs(audio_content));
3578}
3579
3580// Test that no media content will be added to the offer if using default
3581// RTCOfferAnswerOptions.
3582TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3583 RTCOfferAnswerOptions rtc_options;
3584
3585 std::unique_ptr<SessionDescriptionInterface> offer;
3586 CreatePeerConnection();
3587 offer = CreateOfferWithOptions(rtc_options);
3588 ASSERT_TRUE(offer);
3589 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3590 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3591}
3592
3593// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3594// ufrag/pwd will be the same in the new offer.
3595TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3596 RTCOfferAnswerOptions rtc_options;
3597 rtc_options.ice_restart = false;
3598 rtc_options.offer_to_receive_audio = 1;
3599
3600 std::unique_ptr<SessionDescriptionInterface> offer;
3601 CreatePeerConnection();
3602 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3603 auto ufrag1 = offer->description()
3604 ->GetTransportInfoByName(cricket::CN_AUDIO)
3605 ->description.ice_ufrag;
3606 auto pwd1 = offer->description()
3607 ->GetTransportInfoByName(cricket::CN_AUDIO)
3608 ->description.ice_pwd;
3609
3610 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3611 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3612 auto ufrag2 = offer->description()
3613 ->GetTransportInfoByName(cricket::CN_AUDIO)
3614 ->description.ice_ufrag;
3615 auto pwd2 = offer->description()
3616 ->GetTransportInfoByName(cricket::CN_AUDIO)
3617 ->description.ice_pwd;
3618
3619 // |ice_restart| is true, the ufrag/pwd should change.
3620 rtc_options.ice_restart = true;
3621 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3622 auto ufrag3 = offer->description()
3623 ->GetTransportInfoByName(cricket::CN_AUDIO)
3624 ->description.ice_ufrag;
3625 auto pwd3 = offer->description()
3626 ->GetTransportInfoByName(cricket::CN_AUDIO)
3627 ->description.ice_pwd;
3628
3629 EXPECT_EQ(ufrag1, ufrag2);
3630 EXPECT_EQ(pwd1, pwd2);
3631 EXPECT_NE(ufrag2, ufrag3);
3632 EXPECT_NE(pwd2, pwd3);
3633}
3634
3635// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3636// offer; if it is false, there won't be any bundle group in the offer.
3637TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3638 RTCOfferAnswerOptions rtc_options;
3639 rtc_options.offer_to_receive_audio = 1;
3640 rtc_options.offer_to_receive_video = 1;
3641
3642 std::unique_ptr<SessionDescriptionInterface> offer;
3643 CreatePeerConnection();
3644
3645 rtc_options.use_rtp_mux = true;
3646 offer = CreateOfferWithOptions(rtc_options);
3647 ASSERT_TRUE(offer);
3648 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3649 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3650 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3651
3652 rtc_options.use_rtp_mux = false;
3653 offer = CreateOfferWithOptions(rtc_options);
3654 ASSERT_TRUE(offer);
3655 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3656 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3657 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3658}
3659
3660// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3661// called for the answer constraints, but an audio and a video section were
3662// offered, there will still be an audio and a video section in the answer.
3663TEST_F(PeerConnectionInterfaceTest,
3664 RejectAudioAndVideoInAnswerWithConstraints) {
3665 // Offer both audio and video.
3666 RTCOfferAnswerOptions rtc_offer_options;
3667 rtc_offer_options.offer_to_receive_audio = 1;
3668 rtc_offer_options.offer_to_receive_video = 1;
3669
3670 CreatePeerConnection();
3671 std::unique_ptr<SessionDescriptionInterface> offer;
3672 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3673 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3674 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3675
3676 // Since an offer has been created with both audio and video,
3677 // Answers will contain the media types that exist in the offer regardless of
3678 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3679 FakeConstraints answer_c;
3680 // Reject both audio and video.
3681 answer_c.SetMandatoryReceiveAudio(false);
3682 answer_c.SetMandatoryReceiveVideo(false);
3683
3684 std::unique_ptr<SessionDescriptionInterface> answer;
3685 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3686 const cricket::ContentInfo* audio_content =
3687 GetFirstAudioContent(answer->description());
3688 const cricket::ContentInfo* video_content =
3689 GetFirstVideoContent(answer->description());
3690 ASSERT_NE(nullptr, audio_content);
3691 ASSERT_NE(nullptr, video_content);
3692 EXPECT_TRUE(audio_content->rejected);
3693 EXPECT_TRUE(video_content->rejected);
3694}
3695
zhihuang141aacb2017-08-29 13:23:53 -07003696// This test ensures OnRenegotiationNeeded is called when we add track with
3697// MediaStream -> AddTrack in the same way it is called when we add track with
3698// PeerConnection -> AddTrack.
3699// The test can be removed once addStream is rewritten in terms of addTrack
3700// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3701TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3702 CreatePeerConnectionWithoutDtls();
3703 rtc::scoped_refptr<MediaStreamInterface> stream(
3704 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3705 pc_->AddStream(stream);
3706 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3707 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3708 rtc::scoped_refptr<VideoTrackInterface> video_track(
3709 pc_factory_->CreateVideoTrack(
3710 "video_track", pc_factory_->CreateVideoSource(
3711 std::unique_ptr<cricket::VideoCapturer>(
3712 new cricket::FakeVideoCapturer()))));
3713 stream->AddTrack(audio_track);
3714 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3715 observer_.renegotiation_needed_ = false;
3716
3717 stream->AddTrack(video_track);
3718 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3719 observer_.renegotiation_needed_ = false;
3720
3721 stream->RemoveTrack(audio_track);
3722 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3723 observer_.renegotiation_needed_ = false;
3724
3725 stream->RemoveTrack(video_track);
3726 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3727 observer_.renegotiation_needed_ = false;
3728}
3729
3730// Tests that creating answer would fail gracefully without being crashed if the
3731// remote description is unset.
3732TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3733 CreatePeerConnection();
3734 // Creating answer fails because the remote description is unset.
3735 std::unique_ptr<SessionDescriptionInterface> answer;
3736 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3737
3738 // Createing answer succeeds when the remote description is set.
3739 CreateOfferAsRemoteDescription();
3740 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3741}
3742
Zhi Huang2a5e4262017-09-14 01:15:03 -07003743// Test that an error is returned if a description is applied that doesn't
3744// respect the order of existing media sections.
3745TEST_F(PeerConnectionInterfaceTest,
3746 MediaSectionOrderEnforcedForSubsequentOffers) {
3747 CreatePeerConnection();
3748 FakeConstraints constraints;
3749 constraints.SetMandatoryReceiveAudio(true);
3750 constraints.SetMandatoryReceiveVideo(true);
3751 std::unique_ptr<SessionDescriptionInterface> offer;
3752 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3753 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3754
3755 std::unique_ptr<SessionDescriptionInterface> answer;
3756 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3757 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3758
3759 // A remote offer with different m=line order should be rejected.
3760 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3761 std::reverse(offer->description()->contents().begin(),
3762 offer->description()->contents().end());
3763 std::reverse(offer->description()->transport_infos().begin(),
3764 offer->description()->transport_infos().end());
3765 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3766
3767 // A subsequent local offer with different m=line order should be rejected.
3768 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3769 std::reverse(offer->description()->contents().begin(),
3770 offer->description()->contents().end());
3771 std::reverse(offer->description()->transport_infos().begin(),
3772 offer->description()->transport_infos().end());
3773 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3774}
3775
nisse51542be2016-02-12 02:27:06 -08003776class PeerConnectionMediaConfigTest : public testing::Test {
3777 protected:
3778 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003779 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003780 pcf_->Initialize();
3781 }
nisseeaabdf62017-05-05 02:23:02 -07003782 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003783 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003784 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003785 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3786 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003787 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003788 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003789 }
3790
zhihuang9763d562016-08-05 11:14:50 -07003791 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003792 MockPeerConnectionObserver observer_;
3793};
3794
3795// This test verifies the default behaviour with no constraints and a
3796// default RTCConfiguration.
3797TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3798 PeerConnectionInterface::RTCConfiguration config;
3799 FakeConstraints constraints;
3800
3801 const cricket::MediaConfig& media_config =
3802 TestCreatePeerConnection(config, &constraints);
3803
3804 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003805 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3806 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3807 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003808}
3809
3810// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003811// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003812TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3813 PeerConnectionInterface::RTCConfiguration config;
3814 FakeConstraints constraints;
3815
3816 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3817 const cricket::MediaConfig& media_config =
3818 TestCreatePeerConnection(config, &constraints);
3819
3820 EXPECT_TRUE(media_config.enable_dscp);
3821}
3822
3823// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003824// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003825TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3826 PeerConnectionInterface::RTCConfiguration config;
3827 FakeConstraints constraints;
3828
3829 constraints.AddOptional(
3830 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3831 const cricket::MediaConfig media_config =
3832 TestCreatePeerConnection(config, &constraints);
3833
nisse0db023a2016-03-01 04:29:59 -08003834 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003835}
3836
3837// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003838// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003839TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3840 PeerConnectionInterface::RTCConfiguration config;
3841 FakeConstraints constraints;
3842
Niels Möller71bdda02016-03-31 12:59:59 +02003843 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003844 const cricket::MediaConfig& media_config =
3845 TestCreatePeerConnection(config, &constraints);
3846
nisse0db023a2016-03-01 04:29:59 -08003847 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3848}
3849
3850// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003851// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003852TEST_F(PeerConnectionMediaConfigTest,
3853 TestSuspendBelowMinBitrateConstraintTrue) {
3854 PeerConnectionInterface::RTCConfiguration config;
3855 FakeConstraints constraints;
3856
3857 constraints.AddOptional(
3858 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3859 true);
3860 const cricket::MediaConfig media_config =
3861 TestCreatePeerConnection(config, &constraints);
3862
3863 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003864}
3865
deadbeef293e9262017-01-11 12:28:30 -08003866// Tests a few random fields being different.
3867TEST(RTCConfigurationTest, ComparisonOperators) {
3868 PeerConnectionInterface::RTCConfiguration a;
3869 PeerConnectionInterface::RTCConfiguration b;
3870 EXPECT_EQ(a, b);
3871
3872 PeerConnectionInterface::RTCConfiguration c;
3873 c.servers.push_back(PeerConnectionInterface::IceServer());
3874 EXPECT_NE(a, c);
3875
3876 PeerConnectionInterface::RTCConfiguration d;
3877 d.type = PeerConnectionInterface::kRelay;
3878 EXPECT_NE(a, d);
3879
3880 PeerConnectionInterface::RTCConfiguration e;
3881 e.audio_jitter_buffer_max_packets = 5;
3882 EXPECT_NE(a, e);
3883
3884 PeerConnectionInterface::RTCConfiguration f;
3885 f.ice_connection_receiving_timeout = 1337;
3886 EXPECT_NE(a, f);
3887
3888 PeerConnectionInterface::RTCConfiguration g;
3889 g.disable_ipv6 = true;
3890 EXPECT_NE(a, g);
3891
3892 PeerConnectionInterface::RTCConfiguration h(
3893 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3894 EXPECT_NE(a, h);
3895}