blob: d8fa486e187cb8c2820ecd5ac00516608a83f7bb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Zhi Huang2a5e4262017-09-14 01:15:03 -070011#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070012#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080013#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "api/rtpreceiverinterface.h"
23#include "api/rtpsenderinterface.h"
24#include "api/test/fakeconstraints.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020025#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakevideocapturer.h"
27#include "media/engine/webrtcmediaengine.h"
28#include "media/sctp/sctptransportinternal.h"
29#include "modules/audio_processing/include/audio_processing.h"
30#include "p2p/base/fakeportallocator.h"
31#include "pc/audiotrack.h"
32#include "pc/mediasession.h"
33#include "pc/mediastream.h"
34#include "pc/peerconnection.h"
35#include "pc/streamcollection.h"
36#include "pc/test/fakeaudiocapturemodule.h"
37#include "pc/test/fakertccertificategenerator.h"
38#include "pc/test/fakevideotracksource.h"
39#include "pc/test/mockpeerconnectionobservers.h"
40#include "pc/test/testsdpstrings.h"
41#include "pc/videocapturertracksource.h"
42#include "pc/videotrack.h"
43#include "rtc_base/gunit.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020044#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/virtualsocketserver.h"
47#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020048#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070049
50#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070052#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54static const char kStreamLabel1[] = "local_stream_1";
55static const char kStreamLabel2[] = "local_stream_2";
56static const char kStreamLabel3[] = "local_stream_3";
57static const int kDefaultStunPort = 3478;
58static const char kStunAddressOnly[] = "stun:address";
59static const char kStunInvalidPort[] = "stun:address:-1";
60static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
61static const char kStunAddressPortAndMore2[] = "stun:address:port more";
62static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
63static const char kTurnUsername[] = "user";
64static const char kTurnPassword[] = "password";
65static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020066static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
deadbeefab9b2d12015-10-14 11:33:11 -070068static const char kStreams[][8] = {"stream1", "stream2"};
69static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
70static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
71
deadbeef5e97fb52015-10-15 12:49:08 -070072static const char kRecvonly[] = "recvonly";
73static const char kSendrecv[] = "sendrecv";
74
deadbeefab9b2d12015-10-14 11:33:11 -070075// Reference SDP with a MediaStream with label "stream1" and audio track with
76// id "audio_1" and a video track with id "video_1;
77static const char kSdpStringWithStream1[] =
78 "v=0\r\n"
79 "o=- 0 0 IN IP4 127.0.0.1\r\n"
80 "s=-\r\n"
81 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080082 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=ice-ufrag:e5785931\r\n"
84 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
85 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
86 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080089 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070090 "a=rtpmap:103 ISAC/16000\r\n"
91 "a=ssrc:1 cname:stream1\r\n"
92 "a=ssrc:1 mslabel:stream1\r\n"
93 "a=ssrc:1 label:audiotrack0\r\n"
94 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080095 "a=ice-ufrag:e5785931\r\n"
96 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
97 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
98 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700100 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800101 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700102 "a=rtpmap:120 VP8/90000\r\n"
103 "a=ssrc:2 cname:stream1\r\n"
104 "a=ssrc:2 mslabel:stream1\r\n"
105 "a=ssrc:2 label:videotrack0\r\n";
106
zhihuang81c3a032016-11-17 12:06:24 -0800107// Reference SDP with a MediaStream with label "stream1" and audio track with
108// id "audio_1";
109static const char kSdpStringWithStream1AudioTrackOnly[] =
110 "v=0\r\n"
111 "o=- 0 0 IN IP4 127.0.0.1\r\n"
112 "s=-\r\n"
113 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800114 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800115 "a=ice-ufrag:e5785931\r\n"
116 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
117 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
118 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800119 "a=mid:audio\r\n"
120 "a=sendrecv\r\n"
121 "a=rtpmap:103 ISAC/16000\r\n"
122 "a=ssrc:1 cname:stream1\r\n"
123 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800124 "a=ssrc:1 label:audiotrack0\r\n"
125 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800126
deadbeefab9b2d12015-10-14 11:33:11 -0700127// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
128// MediaStreams have one audio track and one video track.
129// This uses MSID.
130static const char kSdpStringWithStream1And2[] =
131 "v=0\r\n"
132 "o=- 0 0 IN IP4 127.0.0.1\r\n"
133 "s=-\r\n"
134 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800135 "a=msid-semantic: WMS stream1 stream2\r\n"
136 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=ice-ufrag:e5785931\r\n"
138 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
139 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
140 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700142 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800143 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=rtpmap:103 ISAC/16000\r\n"
145 "a=ssrc:1 cname:stream1\r\n"
146 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
147 "a=ssrc:3 cname:stream2\r\n"
148 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
149 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800150 "a=ice-ufrag:e5785931\r\n"
151 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
152 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
153 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700155 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800156 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:120 VP8/0\r\n"
158 "a=ssrc:2 cname:stream1\r\n"
159 "a=ssrc:2 msid:stream1 videotrack0\r\n"
160 "a=ssrc:4 cname:stream2\r\n"
161 "a=ssrc:4 msid:stream2 videotrack1\r\n";
162
163// Reference SDP without MediaStreams. Msid is not supported.
164static const char kSdpStringWithoutStreams[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800169 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=ice-ufrag:e5785931\r\n"
171 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
172 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
173 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800176 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=rtpmap:103 ISAC/16000\r\n"
178 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800179 "a=ice-ufrag:e5785931\r\n"
180 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
181 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
182 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700184 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800185 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700186 "a=rtpmap:120 VP8/90000\r\n";
187
188// Reference SDP without MediaStreams. Msid is supported.
189static const char kSdpStringWithMsidWithoutStreams[] =
190 "v=0\r\n"
191 "o=- 0 0 IN IP4 127.0.0.1\r\n"
192 "s=-\r\n"
193 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800194 "a=msid-semantic: WMS\r\n"
195 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=ice-ufrag:e5785931\r\n"
197 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
198 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
199 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700201 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800202 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=rtpmap:103 ISAC/16000\r\n"
204 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800205 "a=ice-ufrag:e5785931\r\n"
206 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
207 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
208 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800211 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:120 VP8/90000\r\n";
213
214// Reference SDP without MediaStreams and audio only.
215static const char kSdpStringWithoutStreamsAudioOnly[] =
216 "v=0\r\n"
217 "o=- 0 0 IN IP4 127.0.0.1\r\n"
218 "s=-\r\n"
219 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800220 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=ice-ufrag:e5785931\r\n"
222 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
223 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
224 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700226 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:103 ISAC/16000\r\n";
229
230// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
231static const char kSdpStringSendOnlyWithoutStreams[] =
232 "v=0\r\n"
233 "o=- 0 0 IN IP4 127.0.0.1\r\n"
234 "s=-\r\n"
235 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800236 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=ice-ufrag:e5785931\r\n"
238 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
239 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
240 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700242 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800244 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=rtpmap:103 ISAC/16000\r\n"
246 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800247 "a=ice-ufrag:e5785931\r\n"
248 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
249 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
250 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700252 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800254 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700255 "a=rtpmap:120 VP8/90000\r\n";
256
257static const char kSdpStringInit[] =
258 "v=0\r\n"
259 "o=- 0 0 IN IP4 127.0.0.1\r\n"
260 "s=-\r\n"
261 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700262 "a=msid-semantic: WMS\r\n";
263
264static const char kSdpStringAudio[] =
265 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800266 "a=ice-ufrag:e5785931\r\n"
267 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
268 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
269 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700271 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800272 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700273 "a=rtpmap:103 ISAC/16000\r\n";
274
275static const char kSdpStringVideo[] =
276 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800277 "a=ice-ufrag:e5785931\r\n"
278 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
279 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
280 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700282 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800283 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700284 "a=rtpmap:120 VP8/90000\r\n";
285
286static const char kSdpStringMs1Audio0[] =
287 "a=ssrc:1 cname:stream1\r\n"
288 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
289
290static const char kSdpStringMs1Video0[] =
291 "a=ssrc:2 cname:stream1\r\n"
292 "a=ssrc:2 msid:stream1 videotrack0\r\n";
293
294static const char kSdpStringMs1Audio1[] =
295 "a=ssrc:3 cname:stream1\r\n"
296 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
297
298static const char kSdpStringMs1Video1[] =
299 "a=ssrc:4 cname:stream1\r\n"
300 "a=ssrc:4 msid:stream1 videotrack1\r\n";
301
deadbeef8662f942017-01-20 21:20:51 -0800302static const char kDtlsSdesFallbackSdp[] =
303 "v=0\r\n"
304 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
305 "s=-\r\n"
306 "c=IN IP4 0.0.0.0\r\n"
307 "t=0 0\r\n"
308 "a=group:BUNDLE audio\r\n"
309 "a=msid-semantic: WMS\r\n"
310 "m=audio 1 RTP/SAVPF 0\r\n"
311 "a=sendrecv\r\n"
312 "a=rtcp-mux\r\n"
313 "a=mid:audio\r\n"
314 "a=ssrc:1 cname:stream1\r\n"
315 "a=ssrc:1 mslabel:stream1\r\n"
316 "a=ssrc:1 label:audiotrack0\r\n"
317 "a=ice-ufrag:e5785931\r\n"
318 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
319 "a=rtpmap:0 pcmu/8000\r\n"
320 "a=fingerprint:sha-1 "
321 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
322 "a=setup:actpass\r\n"
323 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
324 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
325 "dummy_session_params\r\n";
326
perkjd61bf802016-03-24 03:16:19 -0700327using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700328using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700330using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331using webrtc::AudioTrackInterface;
332using webrtc::DataBuffer;
333using webrtc::DataChannelInterface;
334using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700336using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700337using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338using webrtc::MediaStreamInterface;
339using webrtc::MediaStreamTrackInterface;
340using webrtc::MockCreateSessionDescriptionObserver;
341using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700342using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343using webrtc::MockSetSessionDescriptionObserver;
344using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700345using webrtc::NotifierInterface;
346using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347using webrtc::PeerConnectionInterface;
348using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800349using webrtc::RTCError;
350using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700351using webrtc::RtpReceiverInterface;
352using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353using webrtc::SdpParseError;
354using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700355using webrtc::StreamCollection;
356using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100357using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700358using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359using webrtc::VideoTrackInterface;
360
deadbeefab9b2d12015-10-14 11:33:11 -0700361typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
362
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363namespace {
364
365// Gets the first ssrc of given content type from the ContentInfo.
366bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
367 if (!content_info || !ssrc) {
368 return false;
369 }
370 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000371 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 content_info->description);
373 if (!media_desc || media_desc->streams().empty()) {
374 return false;
375 }
376 *ssrc = media_desc->streams().begin()->first_ssrc();
377 return true;
378}
379
deadbeefd1a38b52016-12-10 13:15:33 -0800380// Get the ufrags out of an SDP blob. Useful for testing ICE restart
381// behavior.
382std::vector<std::string> GetUfrags(
383 const webrtc::SessionDescriptionInterface* desc) {
384 std::vector<std::string> ufrags;
385 for (const cricket::TransportInfo& info :
386 desc->description()->transport_infos()) {
387 ufrags.push_back(info.description.ice_ufrag);
388 }
389 return ufrags;
390}
391
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392void SetSsrcToZero(std::string* sdp) {
393 const char kSdpSsrcAtribute[] = "a=ssrc:";
394 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
395 size_t ssrc_pos = 0;
396 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
397 std::string::npos) {
398 size_t end_ssrc = sdp->find(" ", ssrc_pos);
399 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
400 ssrc_pos = end_ssrc;
401 }
402}
403
deadbeefab9b2d12015-10-14 11:33:11 -0700404// Check if |streams| contains the specified track.
405bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
406 const std::string& stream_label,
407 const std::string& track_id) {
408 for (const cricket::StreamParams& params : streams) {
409 if (params.sync_label == stream_label && params.id == track_id) {
410 return true;
411 }
412 }
413 return false;
414}
415
416// Check if |senders| contains the specified sender, by id.
417bool ContainsSender(
418 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
419 const std::string& id) {
420 for (const auto& sender : senders) {
421 if (sender->id() == id) {
422 return true;
423 }
424 }
425 return false;
426}
427
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700428// Check if |senders| contains the specified sender, by id and stream id.
429bool ContainsSender(
430 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
431 const std::string& id,
432 const std::string& stream_id) {
433 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700434 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700435 return true;
436 }
437 }
438 return false;
439}
440
deadbeefab9b2d12015-10-14 11:33:11 -0700441// Create a collection of streams.
442// CreateStreamCollection(1) creates a collection that
443// correspond to kSdpStringWithStream1.
444// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
445rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700446 int number_of_streams,
447 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700448 rtc::scoped_refptr<StreamCollection> local_collection(
449 StreamCollection::Create());
450
451 for (int i = 0; i < number_of_streams; ++i) {
452 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
453 webrtc::MediaStream::Create(kStreams[i]));
454
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700455 for (int j = 0; j < tracks_per_stream; ++j) {
456 // Add a local audio track.
457 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
458 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
459 nullptr));
460 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700461
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700462 // Add a local video track.
463 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
464 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700465 webrtc::FakeVideoTrackSource::Create(),
466 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700467 stream->AddTrack(video_track);
468 }
deadbeefab9b2d12015-10-14 11:33:11 -0700469
470 local_collection->AddStream(stream);
471 }
472 return local_collection;
473}
474
475// Check equality of StreamCollections.
476bool CompareStreamCollections(StreamCollectionInterface* s1,
477 StreamCollectionInterface* s2) {
478 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
479 return false;
480 }
481
482 for (size_t i = 0; i != s1->count(); ++i) {
483 if (s1->at(i)->label() != s2->at(i)->label()) {
484 return false;
485 }
486 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
487 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
488 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
489 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
490
491 if (audio_tracks1.size() != audio_tracks2.size()) {
492 return false;
493 }
494 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
495 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
496 return false;
497 }
498 }
499 if (video_tracks1.size() != video_tracks2.size()) {
500 return false;
501 }
502 for (size_t j = 0; j != video_tracks1.size(); ++j) {
503 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
504 return false;
505 }
506 }
507 }
508 return true;
509}
510
perkjd61bf802016-03-24 03:16:19 -0700511// Helper class to test Observer.
512class MockTrackObserver : public ObserverInterface {
513 public:
514 explicit MockTrackObserver(NotifierInterface* notifier)
515 : notifier_(notifier) {
516 notifier_->RegisterObserver(this);
517 }
518
519 ~MockTrackObserver() { Unregister(); }
520
521 void Unregister() {
522 if (notifier_) {
523 notifier_->UnregisterObserver(this);
524 notifier_ = nullptr;
525 }
526 }
527
528 MOCK_METHOD0(OnChanged, void());
529
530 private:
531 NotifierInterface* notifier_;
532};
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700535
nisse528b7932017-05-08 03:21:43 -0700536// The PeerConnectionMediaConfig tests below verify that configuration and
537// constraints are propagated into the PeerConnection's MediaConfig. These
538// settings are intended for MediaChannel constructors, but that is not
539// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700540class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
541 public:
zhihuang38ede132017-06-15 12:52:32 -0700542 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
543 CreatePeerConnectionFactoryForTest() {
544 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
545 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
546
henrika919dc2e2017-10-12 14:24:55 +0200547 // Use fake audio device module since we're only testing the interface
548 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700549 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
550 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200551 FakeAudioCaptureModule::Create(), audio_encoder_factory,
552 audio_decoder_factory, nullptr, nullptr, nullptr,
553 webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700554
555 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
556 webrtc::CreateCallFactory();
557
558 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
559 webrtc::CreateRtcEventLogFactory();
560
561 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
562 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200563 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700564 std::move(event_log_factory));
565 }
566
567 PeerConnectionFactoryForTest(
568 rtc::Thread* network_thread,
569 rtc::Thread* worker_thread,
570 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700571 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
572 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
573 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
574 : webrtc::PeerConnectionFactory(network_thread,
575 worker_thread,
576 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700577 std::move(media_engine),
578 std::move(call_factory),
579 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800580
zhihuang29ff8442016-07-27 11:07:25 -0700581 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700582 cricket::PortAllocator* port_allocator,
583 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700584 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700585 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700586 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700587 return transport_controller;
588 }
589
deadbeefd7850b22017-08-23 10:59:19 -0700590 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700591 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700592};
593
Steve Anton94286cb2017-09-26 16:20:19 -0700594// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
595// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596class PeerConnectionInterfaceTest : public testing::Test {
597 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700598 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700599 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800600#ifdef WEBRTC_ANDROID
601 webrtc::InitializeAndroidObjects();
602#endif
603 }
604
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700606 // Use fake audio capture module since we're only testing the interface
607 // level, and using a real one could make tests flaky when run in parallel.
608 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700610 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200611 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
612 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700613 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700614 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700615 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700616 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618
619 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700620 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 }
622
deadbeef293e9262017-01-11 12:28:30 -0800623 // DTLS does not work in a loopback call, so is disabled for most of the
624 // tests in this file.
625 void CreatePeerConnectionWithoutDtls() {
626 FakeConstraints no_dtls_constraints;
627 no_dtls_constraints.AddMandatory(
628 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
629
630 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
631 &no_dtls_constraints);
632 }
633
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700635 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
636 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 }
638
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700639 void CreatePeerConnectionWithIceTransportsType(
640 PeerConnectionInterface::IceTransportsType type) {
641 PeerConnectionInterface::RTCConfiguration config;
642 config.type = type;
643 return CreatePeerConnection(config, nullptr);
644 }
645
646 void CreatePeerConnectionWithIceServer(const std::string& uri,
647 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800648 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700650 server.uri = uri;
651 server.password = password;
652 config.servers.push_back(server);
653 CreatePeerConnection(config, nullptr);
654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700656 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
657 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700658 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800659 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
660 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000661
deadbeef1dcb1642017-03-29 21:08:16 -0700662 // Create certificate generator unless DTLS constraint is explicitly set to
663 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200664 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000665 bool dtls;
666 if (FindConstraint(constraints,
667 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
668 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200669 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800670 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
671 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000672 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200673 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800674 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200675 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 ASSERT_TRUE(pc_.get() != NULL);
677 observer_.SetPeerConnectionInterface(pc_.get());
678 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
679 }
680
deadbeef0a6c4ca2015-10-06 11:38:28 -0700681 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800682 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700683 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700684 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800685 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700686
zhihuang9763d562016-08-05 11:14:50 -0700687 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700688 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
689 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800690 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700691 }
692
Steve Anton038834f2017-07-14 15:59:59 -0700693 void CreatePeerConnectionExpectFail(
694 PeerConnectionInterface::RTCConfiguration config) {
695 PeerConnectionInterface::IceServer server;
696 server.uri = kTurnIceServerUri;
697 server.password = kTurnPassword;
698 config.servers.push_back(server);
699 rtc::scoped_refptr<PeerConnectionInterface> pc =
700 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
701 EXPECT_EQ(nullptr, pc);
702 }
703
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700705 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800706 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
707 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
708 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800710 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711
deadbeef0a6c4ca2015-10-06 11:38:28 -0700712 CreatePeerConnectionExpectFail(kStunInvalidPort);
713 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
714 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700716 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800717 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
718 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800720 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800722 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726
727 void ReleasePeerConnection() {
728 pc_ = NULL;
729 observer_.SetPeerConnectionInterface(NULL);
730 }
731
deadbeefab9b2d12015-10-14 11:33:11 -0700732 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700734 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700736 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800737 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
738 new cricket::FakeVideoCapturer()),
739 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700740 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 pc_factory_->CreateVideoTrack(label + "v0", video_source));
742 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000743 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
745 observer_.renegotiation_needed_ = false;
746 }
747
748 void AddVoiceStream(const std::string& label) {
749 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700750 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700752 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 pc_factory_->CreateAudioTrack(label + "a0", NULL));
754 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000755 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
757 observer_.renegotiation_needed_ = false;
758 }
759
760 void AddAudioVideoStream(const std::string& stream_label,
761 const std::string& audio_track_label,
762 const std::string& video_track_label) {
763 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700764 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700766 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 pc_factory_->CreateAudioTrack(
768 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
769 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700770 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700771 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800772 video_track_label, pc_factory_->CreateVideoSource(
773 std::unique_ptr<cricket::VideoCapturer>(
774 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000776 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
778 observer_.renegotiation_needed_ = false;
779 }
780
kwibergd1fe2812016-04-27 06:47:29 -0700781 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700782 bool offer,
783 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000784 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
785 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 MockCreateSessionDescriptionObserver>());
787 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700788 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700790 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 }
792 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700793 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 return observer->result();
795 }
796
kwibergd1fe2812016-04-27 06:47:29 -0700797 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700798 MediaConstraintsInterface* constraints) {
799 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801
kwibergd1fe2812016-04-27 06:47:29 -0700802 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700803 MediaConstraintsInterface* constraints) {
804 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 }
806
Steve Antondb45ca82017-09-11 18:27:34 -0700807 bool DoSetSessionDescription(
808 std::unique_ptr<SessionDescriptionInterface> desc,
809 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000810 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
811 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 MockSetSessionDescriptionObserver>());
813 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700814 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700816 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
zhihuang29ff8442016-07-27 11:07:25 -0700818 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
819 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
820 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 return observer->result();
822 }
823
Steve Antondb45ca82017-09-11 18:27:34 -0700824 bool DoSetLocalDescription(
825 std::unique_ptr<SessionDescriptionInterface> desc) {
826 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
828
Steve Antondb45ca82017-09-11 18:27:34 -0700829 bool DoSetRemoteDescription(
830 std::unique_ptr<SessionDescriptionInterface> desc) {
831 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833
834 // Calls PeerConnection::GetStats and check the return value.
835 // It does not verify the values in the StatReports since a RTCP packet might
836 // be required.
837 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000838 rtc::scoped_refptr<MockStatsObserver> observer(
839 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000840 if (!pc_->GetStats(
841 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 return false;
843 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
844 return observer->called();
845 }
846
847 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800848 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 // Create a local stream with audio&video tracks.
850 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
851 CreateOfferReceiveAnswer();
852 }
853
854 // Verify that RTP Header extensions has been negotiated for audio and video.
855 void VerifyRemoteRtpHeaderExtensions() {
856 const cricket::MediaContentDescription* desc =
857 cricket::GetFirstAudioContentDescription(
858 pc_->remote_description()->description());
859 ASSERT_TRUE(desc != NULL);
860 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
861
862 desc = cricket::GetFirstVideoContentDescription(
863 pc_->remote_description()->description());
864 ASSERT_TRUE(desc != NULL);
865 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
866 }
867
868 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700869 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700870 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 std::string sdp;
872 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700873 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700875 sdp, nullptr));
876 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
878 }
879
deadbeefab9b2d12015-10-14 11:33:11 -0700880 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700881 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700882 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700883 sdp, nullptr));
884 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700885 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
886 }
887
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700889 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700890 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891
892 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
893 // audio codec change, even if the parameter has nothing to do with
894 // receiving. Not all parameters are serialized to SDP.
895 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
896 // the SessionDescription, it is necessary to do that here to in order to
897 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
898 // https://code.google.com/p/webrtc/issues/detail?id=1356
899 std::string sdp;
900 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700901 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700903 sdp, nullptr));
904 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
906 }
907
908 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700909 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700910 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911
912 std::string sdp;
913 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700914 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700916 sdp, nullptr));
917 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
919 }
920
921 void CreateOfferReceiveAnswer() {
922 CreateOfferAsLocalDescription();
923 std::string sdp;
924 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
925 CreateAnswerAsRemoteDescription(sdp);
926 }
927
928 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700929 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700930 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
932 // audio codec change, even if the parameter has nothing to do with
933 // receiving. Not all parameters are serialized to SDP.
934 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
935 // the SessionDescription, it is necessary to do that here to in order to
936 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
937 // https://code.google.com/p/webrtc/issues/detail?id=1356
938 std::string sdp;
939 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700940 std::unique_ptr<SessionDescriptionInterface> new_offer(
941 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
942 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943
Steve Antondb45ca82017-09-11 18:27:34 -0700944 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000946 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700947 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 }
949
deadbeefab9b2d12015-10-14 11:33:11 -0700950 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700951 std::unique_ptr<SessionDescriptionInterface> answer(
952 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
953 sdp, nullptr));
954 ASSERT_TRUE(answer);
955 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
957 }
958
deadbeefab9b2d12015-10-14 11:33:11 -0700959 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700960 std::unique_ptr<SessionDescriptionInterface> pr_answer(
961 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
962 sdp, nullptr));
963 ASSERT_TRUE(pr_answer);
964 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700966 std::unique_ptr<SessionDescriptionInterface> answer(
967 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
968 sdp, nullptr));
969 ASSERT_TRUE(answer);
970 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
972 }
973
974 // Help function used for waiting until a the last signaled remote stream has
975 // the same label as |stream_label|. In a few of the tests in this file we
976 // answer with the same session description as we offer and thus we can
977 // check if OnAddStream have been called with the same stream as we offer to
978 // send.
979 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
980 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
981 }
982
983 // Creates an offer and applies it as a local session description.
984 // Creates an answer with the same SDP an the offer but removes all lines
985 // that start with a:ssrc"
986 void CreateOfferReceiveAnswerWithoutSsrc() {
987 CreateOfferAsLocalDescription();
988 std::string sdp;
989 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
990 SetSsrcToZero(&sdp);
991 CreateAnswerAsRemoteDescription(sdp);
992 }
993
deadbeefab9b2d12015-10-14 11:33:11 -0700994 // This function creates a MediaStream with label kStreams[0] and
995 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
996 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700997 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700998 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700999 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001000 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1001 size_t number_of_video_tracks) {
1002 EXPECT_LE(number_of_audio_tracks, 2u);
1003 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001004
1005 reference_collection_ = StreamCollection::Create();
1006 std::string sdp_ms1 = std::string(kSdpStringInit);
1007
1008 std::string mediastream_label = kStreams[0];
1009
1010 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1011 webrtc::MediaStream::Create(mediastream_label));
1012 reference_collection_->AddStream(stream);
1013
1014 if (number_of_audio_tracks > 0) {
1015 sdp_ms1 += std::string(kSdpStringAudio);
1016 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1017 AddAudioTrack(kAudioTracks[0], stream);
1018 }
1019 if (number_of_audio_tracks > 1) {
1020 sdp_ms1 += kSdpStringMs1Audio1;
1021 AddAudioTrack(kAudioTracks[1], stream);
1022 }
1023
1024 if (number_of_video_tracks > 0) {
1025 sdp_ms1 += std::string(kSdpStringVideo);
1026 sdp_ms1 += std::string(kSdpStringMs1Video0);
1027 AddVideoTrack(kVideoTracks[0], stream);
1028 }
1029 if (number_of_video_tracks > 1) {
1030 sdp_ms1 += kSdpStringMs1Video1;
1031 AddVideoTrack(kVideoTracks[1], stream);
1032 }
1033
kwibergd1fe2812016-04-27 06:47:29 -07001034 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001035 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1036 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001037 }
1038
1039 void AddAudioTrack(const std::string& track_id,
1040 MediaStreamInterface* stream) {
1041 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1042 webrtc::AudioTrack::Create(track_id, nullptr));
1043 ASSERT_TRUE(stream->AddTrack(audio_track));
1044 }
1045
1046 void AddVideoTrack(const std::string& track_id,
1047 MediaStreamInterface* stream) {
1048 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001049 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001050 webrtc::FakeVideoTrackSource::Create(),
1051 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001052 ASSERT_TRUE(stream->AddTrack(video_track));
1053 }
1054
kwibergfd8be342016-05-14 19:44:11 -07001055 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001056 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001057 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001058 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001059 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1060 return offer;
1061 }
1062
kwibergfd8be342016-05-14 19:44:11 -07001063 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001064 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001065 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001066 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001067 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1068 return answer;
1069 }
1070
1071 const std::string& GetFirstAudioStreamCname(
1072 const SessionDescriptionInterface* desc) {
1073 const cricket::ContentInfo* audio_content =
1074 cricket::GetFirstAudioContent(desc->description());
1075 const cricket::AudioContentDescription* audio_desc =
1076 static_cast<const cricket::AudioContentDescription*>(
1077 audio_content->description);
1078 return audio_desc->streams()[0].cname;
1079 }
1080
zhihuang1c378ed2017-08-17 14:10:50 -07001081 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1082 const RTCOfferAnswerOptions& offer_answer_options) {
1083 RTC_DCHECK(pc_);
1084 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1085 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1086 pc_->CreateOffer(observer, offer_answer_options);
1087 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1088 return observer->MoveDescription();
1089 }
1090
1091 void CreateOfferWithOptionsAsRemoteDescription(
1092 std::unique_ptr<SessionDescriptionInterface>* desc,
1093 const RTCOfferAnswerOptions& offer_answer_options) {
1094 *desc = CreateOfferWithOptions(offer_answer_options);
1095 ASSERT_TRUE(desc != nullptr);
1096 std::string sdp;
1097 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001098 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001099 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001100 sdp, nullptr));
1101 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001102 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1103 }
1104
1105 void CreateOfferWithOptionsAsLocalDescription(
1106 std::unique_ptr<SessionDescriptionInterface>* desc,
1107 const RTCOfferAnswerOptions& offer_answer_options) {
1108 *desc = CreateOfferWithOptions(offer_answer_options);
1109 ASSERT_TRUE(desc != nullptr);
1110 std::string sdp;
1111 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001112 std::unique_ptr<SessionDescriptionInterface> new_offer(
1113 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1114 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001115
Steve Antondb45ca82017-09-11 18:27:34 -07001116 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001117 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1118 }
1119
1120 bool HasCNCodecs(const cricket::ContentInfo* content) {
1121 const cricket::ContentDescription* description = content->description;
1122 RTC_DCHECK(description);
1123 const cricket::AudioContentDescription* audio_content_desc =
1124 static_cast<const cricket::AudioContentDescription*>(description);
1125 RTC_DCHECK(audio_content_desc);
1126 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1127 if (audio_content_desc->codecs()[i].name == "CN")
1128 return true;
1129 }
1130 return false;
1131 }
1132
deadbeef9a6f4d42017-05-15 19:43:33 -07001133 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1134 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001135 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001136 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001137 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001138 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1139 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1140 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001142 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143};
1144
zhihuang29ff8442016-07-27 11:07:25 -07001145// Test that no callbacks on the PeerConnectionObserver are called after the
1146// PeerConnection is closed.
1147TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001148 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001149 pc_factory_for_test_->CreatePeerConnection(
1150 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1151 nullptr, &observer_));
1152 observer_.SetPeerConnectionInterface(pc.get());
1153 pc->Close();
1154
1155 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001156 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001157 std::vector<cricket::Candidate> candidates;
1158 pc_factory_for_test_->transport_controller->SignalGatheringState(
1159 cricket::IceGatheringState{});
1160 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1161 "", candidates);
1162 pc_factory_for_test_->transport_controller->SignalConnectionState(
1163 cricket::IceConnectionState{});
1164 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1165 candidates);
1166 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001167 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001168}
1169
zhihuang8f65cdf2016-05-06 18:40:30 -07001170// Generate different CNAMEs when PeerConnections are created.
1171// The CNAMEs are expected to be generated randomly. It is possible
1172// that the test fails, though the possibility is very low.
1173TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001174 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 CreateOfferWithOneAudioStream();
1178 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1179 GetFirstAudioStreamCname(offer2.get()));
1180}
1181
1182TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001185 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 CreateAnswerWithOneAudioStream();
1187 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1188 GetFirstAudioStreamCname(answer2.get()));
1189}
1190
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191TEST_F(PeerConnectionInterfaceTest,
1192 CreatePeerConnectionWithDifferentConfigurations) {
1193 CreatePeerConnectionWithDifferentConfigurations();
1194}
1195
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196TEST_F(PeerConnectionInterfaceTest,
1197 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1199 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1200 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1201 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1202 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1203 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1204 port_allocator_->candidate_filter());
1205 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1206 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1207}
1208
1209// Test that when a PeerConnection is created with a nonzero candidate pool
1210// size, the pooled PortAllocatorSession is created with all the attributes
1211// in the RTCConfiguration.
1212TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1213 PeerConnectionInterface::RTCConfiguration config;
1214 PeerConnectionInterface::IceServer server;
1215 server.uri = kStunAddressOnly;
1216 config.servers.push_back(server);
1217 config.type = PeerConnectionInterface::kRelay;
1218 config.disable_ipv6 = true;
1219 config.tcp_candidate_policy =
1220 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001221 config.candidate_network_policy =
1222 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001223 config.ice_candidate_pool_size = 1;
1224 CreatePeerConnection(config, nullptr);
1225
1226 const cricket::FakePortAllocatorSession* session =
1227 static_cast<const cricket::FakePortAllocatorSession*>(
1228 port_allocator_->GetPooledSession());
1229 ASSERT_NE(nullptr, session);
1230 EXPECT_EQ(1UL, session->stun_servers().size());
1231 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1232 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001233 EXPECT_LT(0U,
1234 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001235}
1236
deadbeefd21eab32017-07-26 16:50:11 -07001237// Test that network-related RTCConfiguration members are applied to the
1238// PortAllocator when CreatePeerConnection is called. Specifically:
1239// - disable_ipv6_on_wifi
1240// - max_ipv6_networks
1241// - tcp_candidate_policy
1242// - candidate_network_policy
1243// - prune_turn_ports
1244//
1245// Note that the candidate filter (RTCConfiguration::type) is already tested
1246// above.
1247TEST_F(PeerConnectionInterfaceTest,
1248 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1249 // Create fake port allocator.
1250 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1251 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1252 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1253
1254 // Create RTCConfiguration with some network-related fields relevant to
1255 // PortAllocator populated.
1256 PeerConnectionInterface::RTCConfiguration config;
1257 config.disable_ipv6_on_wifi = true;
1258 config.max_ipv6_networks = 10;
1259 config.tcp_candidate_policy =
1260 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1261 config.candidate_network_policy =
1262 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1263 config.prune_turn_ports = true;
1264
1265 // Create the PC factory and PC with the above config.
1266 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1267 webrtc::CreatePeerConnectionFactory(
1268 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001269 rtc::Thread::Current(), fake_audio_capture_module_,
1270 webrtc::CreateBuiltinAudioEncoderFactory(),
1271 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001272 rtc::scoped_refptr<PeerConnectionInterface> pc(
1273 pc_factory->CreatePeerConnection(
1274 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1275
1276 // Now validate that the config fields set above were applied to the
1277 // PortAllocator, as flags or otherwise.
1278 EXPECT_FALSE(raw_port_allocator->flags() &
1279 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1280 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1281 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1282 EXPECT_TRUE(raw_port_allocator->flags() &
1283 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1284 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1285}
1286
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001287// Test that the PeerConnection initializes the port allocator passed into it,
1288// and on the correct thread.
1289TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001290 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001291 std::unique_ptr<rtc::Thread> network_thread(
1292 rtc::Thread::CreateWithSocketServer());
1293 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001294 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1295 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001296 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001297 fake_audio_capture_module_,
1298 webrtc::CreateBuiltinAudioEncoderFactory(),
1299 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001300 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001301 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001302 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1303 PeerConnectionInterface::RTCConfiguration config;
1304 rtc::scoped_refptr<PeerConnectionInterface> pc(
1305 pc_factory->CreatePeerConnection(
1306 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1307 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1308 // so all we have to do here is check that it's initialized.
1309 EXPECT_TRUE(raw_port_allocator->initialized());
1310}
1311
deadbeef46c73892016-11-16 19:42:04 -08001312// Check that GetConfiguration returns the configuration the PeerConnection was
1313// constructed with, before SetConfiguration is called.
1314TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1315 PeerConnectionInterface::RTCConfiguration config;
1316 config.type = PeerConnectionInterface::kRelay;
1317 CreatePeerConnection(config, nullptr);
1318
1319 PeerConnectionInterface::RTCConfiguration returned_config =
1320 pc_->GetConfiguration();
1321 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1322}
1323
1324// Check that GetConfiguration returns the last configuration passed into
1325// SetConfiguration.
1326TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1327 CreatePeerConnection();
1328
1329 PeerConnectionInterface::RTCConfiguration config;
1330 config.type = PeerConnectionInterface::kRelay;
1331 EXPECT_TRUE(pc_->SetConfiguration(config));
1332
1333 PeerConnectionInterface::RTCConfiguration returned_config =
1334 pc_->GetConfiguration();
1335 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1336}
1337
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001339 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001340 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 AddVoiceStream(kStreamLabel2);
1342 ASSERT_EQ(2u, pc_->local_streams()->count());
1343
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001344 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001345 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001347 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1348 pc_factory_->CreateAudioTrack(kStreamLabel3,
1349 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001351 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001352 EXPECT_EQ(3u, pc_->local_streams()->count());
1353
1354 // Remove the third stream.
1355 pc_->RemoveStream(pc_->local_streams()->at(2));
1356 EXPECT_EQ(2u, pc_->local_streams()->count());
1357
1358 // Remove the second stream.
1359 pc_->RemoveStream(pc_->local_streams()->at(1));
1360 EXPECT_EQ(1u, pc_->local_streams()->count());
1361
1362 // Remove the first stream.
1363 pc_->RemoveStream(pc_->local_streams()->at(0));
1364 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365}
1366
deadbeefab9b2d12015-10-14 11:33:11 -07001367// Test that the created offer includes streams we added.
1368TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001369 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001370 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001371 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001372 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001373
1374 const cricket::ContentInfo* audio_content =
1375 cricket::GetFirstAudioContent(offer->description());
1376 const cricket::AudioContentDescription* audio_desc =
1377 static_cast<const cricket::AudioContentDescription*>(
1378 audio_content->description);
1379 EXPECT_TRUE(
1380 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1381
1382 const cricket::ContentInfo* video_content =
1383 cricket::GetFirstVideoContent(offer->description());
1384 const cricket::VideoContentDescription* video_desc =
1385 static_cast<const cricket::VideoContentDescription*>(
1386 video_content->description);
1387 EXPECT_TRUE(
1388 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1389
1390 // Add another stream and ensure the offer includes both the old and new
1391 // streams.
1392 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001393 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001394
1395 audio_content = cricket::GetFirstAudioContent(offer->description());
1396 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1397 audio_content->description);
1398 EXPECT_TRUE(
1399 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1400 EXPECT_TRUE(
1401 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1402
1403 video_content = cricket::GetFirstVideoContent(offer->description());
1404 video_desc = static_cast<const cricket::VideoContentDescription*>(
1405 video_content->description);
1406 EXPECT_TRUE(
1407 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1408 EXPECT_TRUE(
1409 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1410}
1411
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001413 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001414 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 ASSERT_EQ(1u, pc_->local_streams()->count());
1416 pc_->RemoveStream(pc_->local_streams()->at(0));
1417 EXPECT_EQ(0u, pc_->local_streams()->count());
1418}
1419
deadbeefe1f9d832016-01-14 15:35:42 -08001420// Test for AddTrack and RemoveTrack methods.
1421// Tests that the created offer includes tracks we added,
1422// and that the RtpSenders are created correctly.
1423// Also tests that RemoveTrack removes the tracks from subsequent offers.
1424TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001425 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001426 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001427 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001428 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1429 std::vector<MediaStreamInterface*> stream_list;
1430 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001431 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001432 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001433 rtc::scoped_refptr<VideoTrackInterface> video_track(
1434 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001435 "video_track", pc_factory_->CreateVideoSource(
1436 std::unique_ptr<cricket::VideoCapturer>(
1437 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001438 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1439 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001440 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1441 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001442 EXPECT_EQ("audio_track", audio_sender->id());
1443 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001444 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1445 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001446 EXPECT_EQ("video_track", video_sender->id());
1447 EXPECT_EQ(video_track, video_sender->track());
1448
1449 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001450 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001451 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001452
1453 const cricket::ContentInfo* audio_content =
1454 cricket::GetFirstAudioContent(offer->description());
1455 const cricket::AudioContentDescription* audio_desc =
1456 static_cast<const cricket::AudioContentDescription*>(
1457 audio_content->description);
1458 EXPECT_TRUE(
1459 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1460
1461 const cricket::ContentInfo* video_content =
1462 cricket::GetFirstVideoContent(offer->description());
1463 const cricket::VideoContentDescription* video_desc =
1464 static_cast<const cricket::VideoContentDescription*>(
1465 video_content->description);
1466 EXPECT_TRUE(
1467 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1468
Steve Antondb45ca82017-09-11 18:27:34 -07001469 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001470
1471 // Now try removing the tracks.
1472 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1473 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1474
1475 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001476 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001477
1478 audio_content = cricket::GetFirstAudioContent(offer->description());
1479 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1480 audio_content->description);
1481 EXPECT_FALSE(
1482 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1483
1484 video_content = cricket::GetFirstVideoContent(offer->description());
1485 video_desc = static_cast<const cricket::VideoContentDescription*>(
1486 video_content->description);
1487 EXPECT_FALSE(
1488 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1489
Steve Antondb45ca82017-09-11 18:27:34 -07001490 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001491
1492 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1493 // should return false.
1494 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1495 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1496}
1497
1498// Test creating senders without a stream specified,
1499// expecting a random stream ID to be generated.
1500TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001501 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001502 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001503 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001504 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001505 rtc::scoped_refptr<VideoTrackInterface> video_track(
1506 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001507 "video_track", pc_factory_->CreateVideoSource(
1508 std::unique_ptr<cricket::VideoCapturer>(
1509 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001510 auto audio_sender =
1511 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1512 auto video_sender =
1513 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1514 EXPECT_EQ("audio_track", audio_sender->id());
1515 EXPECT_EQ(audio_track, audio_sender->track());
1516 EXPECT_EQ("video_track", video_sender->id());
1517 EXPECT_EQ(video_track, video_sender->track());
1518 // If the ID is truly a random GUID, it should be infinitely unlikely they
1519 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001520 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1524 InitiateCall();
1525 WaitAndVerifyOnAddStream(kStreamLabel1);
1526 VerifyRemoteRtpHeaderExtensions();
1527}
1528
1529TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001530 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001531 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 CreateOfferAsLocalDescription();
1533 std::string offer;
1534 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1535 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1536 WaitAndVerifyOnAddStream(kStreamLabel1);
1537}
1538
1539TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001540 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001541 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542
1543 CreateOfferAsRemoteDescription();
1544 CreateAnswerAsLocalDescription();
1545
1546 WaitAndVerifyOnAddStream(kStreamLabel1);
1547}
1548
1549TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001550 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001551 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552
1553 CreateOfferAsRemoteDescription();
1554 CreatePrAnswerAsLocalDescription();
1555 CreateAnswerAsLocalDescription();
1556
1557 WaitAndVerifyOnAddStream(kStreamLabel1);
1558}
1559
1560TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1561 InitiateCall();
1562 ASSERT_EQ(1u, pc_->remote_streams()->count());
1563 pc_->RemoveStream(pc_->local_streams()->at(0));
1564 CreateOfferReceiveAnswer();
1565 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001566 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 CreateOfferReceiveAnswer();
1568}
1569
1570// Tests that after negotiating an audio only call, the respondent can perform a
1571// renegotiation that removes the audio stream.
1572TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001573 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 AddVoiceStream(kStreamLabel1);
1575 CreateOfferAsRemoteDescription();
1576 CreateAnswerAsLocalDescription();
1577
1578 ASSERT_EQ(1u, pc_->remote_streams()->count());
1579 pc_->RemoveStream(pc_->local_streams()->at(0));
1580 CreateOfferReceiveAnswer();
1581 EXPECT_EQ(0u, pc_->remote_streams()->count());
1582}
1583
1584// Test that candidates are generated and that we can parse our own candidates.
1585TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001586 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
Steve Antonf1c6db12017-10-13 11:13:35 -07001588 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001590 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001591 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001592 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001593 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594
1595 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001596 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001597 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001598 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599
Steve Antonf1c6db12017-10-13 11:13:35 -07001600 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001601 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
Steve Antonf1c6db12017-10-13 11:13:35 -07001603 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604}
1605
deadbeefab9b2d12015-10-14 11:33:11 -07001606// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607// not unique.
1608TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001609 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001611 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001612 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001613 EXPECT_TRUE(offer);
1614 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615
1616 // Create a local stream with audio&video tracks having same label.
1617 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1618
1619 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001620 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621
1622 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001623 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001624 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625}
1626
1627// Test that we will get different SSRCs for each tracks in the offer and answer
1628// we created.
1629TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001630 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 // Create a local stream with audio&video tracks having different labels.
1632 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1633
1634 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001635 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001636 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 int audio_ssrc = 0;
1638 int video_ssrc = 0;
1639 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1640 &audio_ssrc));
1641 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1642 &video_ssrc));
1643 EXPECT_NE(audio_ssrc, video_ssrc);
1644
1645 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001646 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001648 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 audio_ssrc = 0;
1650 video_ssrc = 0;
1651 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1652 &audio_ssrc));
1653 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1654 &video_ssrc));
1655 EXPECT_NE(audio_ssrc, video_ssrc);
1656}
1657
deadbeefeb459812015-12-15 19:24:43 -08001658// Test that it's possible to call AddTrack on a MediaStream after adding
1659// the stream to a PeerConnection.
1660// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1661TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001663 // Create audio stream and add to PeerConnection.
1664 AddVoiceStream(kStreamLabel1);
1665 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1666
1667 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001668 rtc::scoped_refptr<VideoTrackInterface> video_track(
1669 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001670 "video_label", pc_factory_->CreateVideoSource(
1671 std::unique_ptr<cricket::VideoCapturer>(
1672 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001673 stream->AddTrack(video_track.get());
1674
kwibergd1fe2812016-04-27 06:47:29 -07001675 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001676 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001677
1678 const cricket::MediaContentDescription* video_desc =
1679 cricket::GetFirstVideoContentDescription(offer->description());
1680 EXPECT_TRUE(video_desc != nullptr);
1681}
1682
1683// Test that it's possible to call RemoveTrack on a MediaStream after adding
1684// the stream to a PeerConnection.
1685// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1686TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001687 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001688 // Create audio/video stream and add to PeerConnection.
1689 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1690 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1691
1692 // Remove the video track.
1693 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1694
kwibergd1fe2812016-04-27 06:47:29 -07001695 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001696 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001697
1698 const cricket::MediaContentDescription* video_desc =
1699 cricket::GetFirstVideoContentDescription(offer->description());
1700 EXPECT_TRUE(video_desc == nullptr);
1701}
1702
deadbeef1dcb1642017-03-29 21:08:16 -07001703// Verify that CreateDtmfSender only succeeds if called with a valid local
1704// track. Other aspects of DtmfSenders are tested in
1705// peerconnection_integrationtest.cc.
1706TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1707 CreatePeerConnection();
1708 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1709 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1710 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1711 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1712 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1713}
1714
deadbeefbd7d8f72015-12-18 16:58:44 -08001715// Test creating a sender with a stream ID, and ensure the ID is populated
1716// in the offer.
1717TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001718 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001719 pc_->CreateSender("video", kStreamLabel1);
1720
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001722 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001723
1724 const cricket::MediaContentDescription* video_desc =
1725 cricket::GetFirstVideoContentDescription(offer->description());
1726 ASSERT_TRUE(video_desc != nullptr);
1727 ASSERT_EQ(1u, video_desc->streams().size());
1728 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1729}
1730
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731// Test that we can specify a certain track that we want statistics about.
1732TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1733 InitiateCall();
1734 ASSERT_LT(0u, pc_->remote_streams()->count());
1735 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001736 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1738 EXPECT_TRUE(DoGetStats(remote_audio));
1739
1740 // Remove the stream. Since we are sending to our selves the local
1741 // and the remote stream is the same.
1742 pc_->RemoveStream(pc_->local_streams()->at(0));
1743 // Do a re-negotiation.
1744 CreateOfferReceiveAnswer();
1745
1746 ASSERT_EQ(0u, pc_->remote_streams()->count());
1747
1748 // Test that we still can get statistics for the old track. Even if it is not
1749 // sent any longer.
1750 EXPECT_TRUE(DoGetStats(remote_audio));
1751}
1752
1753// Test that we can get stats on a video track.
1754TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1755 InitiateCall();
1756 ASSERT_LT(0u, pc_->remote_streams()->count());
1757 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001758 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1760 EXPECT_TRUE(DoGetStats(remote_video));
1761}
1762
1763// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001764TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_factory_->CreateAudioTrack("unknown track", NULL));
1768 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1769}
1770
1771// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 FakeConstraints constraints;
1774 constraints.SetAllowRtpDataChannels();
1775 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001776 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001778 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 pc_->CreateDataChannel("test2", NULL);
1780 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 new MockDataChannelObserver(data2));
1785
1786 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1787 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1788 std::string data_to_send1 = "testing testing";
1789 std::string data_to_send2 = "testing something else";
1790 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1791
1792 CreateOfferReceiveAnswer();
1793 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1794 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1795
1796 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1797 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1798 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1799 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1800
1801 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1802 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1803
1804 data1->Close();
1805 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1806 CreateOfferReceiveAnswer();
1807 EXPECT_FALSE(observer1->IsOpen());
1808 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1809 EXPECT_TRUE(observer2->IsOpen());
1810
1811 data_to_send2 = "testing something else again";
1812 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1813
1814 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1815}
1816
1817// This test verifies that sendnig binary data over RTP data channels should
1818// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 FakeConstraints constraints;
1821 constraints.SetAllowRtpDataChannels();
1822 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001823 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001825 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 pc_->CreateDataChannel("test2", NULL);
1827 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001828 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001830 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 new MockDataChannelObserver(data2));
1832
1833 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1834 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1835
1836 CreateOfferReceiveAnswer();
1837 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1838 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1839
1840 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1841 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1842
jbaucheec21bd2016-03-20 06:15:43 -07001843 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1845}
1846
1847// This test setup a RTP data channels in loop back and test that a channel is
1848// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 FakeConstraints constraints;
1851 constraints.SetAllowRtpDataChannels();
1852 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001853 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001855 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 new MockDataChannelObserver(data1));
1857
1858 CreateOfferReceiveAnswerWithoutSsrc();
1859
1860 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1861
1862 data1->Close();
1863 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1864 CreateOfferReceiveAnswerWithoutSsrc();
1865 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1866 EXPECT_FALSE(observer1->IsOpen());
1867}
1868
1869// This test that if a data channel is added in an answer a receive only channel
1870// channel is created.
1871TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1872 FakeConstraints constraints;
1873 constraints.SetAllowRtpDataChannels();
1874 CreatePeerConnection(&constraints);
1875
1876 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 pc_->CreateDataChannel(offer_label, NULL);
1879
1880 CreateOfferAsLocalDescription();
1881
1882 // Replace the data channel label in the offer and apply it as an answer.
1883 std::string receive_label = "answer_channel";
1884 std::string sdp;
1885 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001886 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 receive_label.c_str(), receive_label.length(),
1888 &sdp);
1889 CreateAnswerAsRemoteDescription(sdp);
1890
1891 // Verify that a new incoming data channel has been created and that
1892 // it is open but can't we written to.
1893 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1894 DataChannelInterface* received_channel = observer_.last_datachannel_;
1895 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1896 EXPECT_EQ(receive_label, received_channel->label());
1897 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1898
1899 // Verify that the channel we initially offered has been rejected.
1900 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1901
1902 // Do another offer / answer exchange and verify that the data channel is
1903 // opened.
1904 CreateOfferReceiveAnswer();
1905 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1906 kTimeout);
1907}
1908
1909// This test that no data channel is returned if a reliable channel is
1910// requested.
1911// TODO(perkj): Remove this test once reliable channels are implemented.
1912TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1913 FakeConstraints constraints;
1914 constraints.SetAllowRtpDataChannels();
1915 CreatePeerConnection(&constraints);
1916
1917 std::string label = "test";
1918 webrtc::DataChannelInit config;
1919 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001920 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 pc_->CreateDataChannel(label, &config);
1922 EXPECT_TRUE(channel == NULL);
1923}
1924
deadbeefab9b2d12015-10-14 11:33:11 -07001925// Verifies that duplicated label is not allowed for RTP data channel.
1926TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1927 FakeConstraints constraints;
1928 constraints.SetAllowRtpDataChannels();
1929 CreatePeerConnection(&constraints);
1930
1931 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001932 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001933 pc_->CreateDataChannel(label, nullptr);
1934 EXPECT_NE(channel, nullptr);
1935
zhihuang9763d562016-08-05 11:14:50 -07001936 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001937 pc_->CreateDataChannel(label, nullptr);
1938 EXPECT_EQ(dup_channel, nullptr);
1939}
1940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941// This tests that a SCTP data channel is returned using different
1942// DataChannelInit configurations.
1943TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1944 FakeConstraints constraints;
1945 constraints.SetAllowDtlsSctpDataChannels();
1946 CreatePeerConnection(&constraints);
1947
1948 webrtc::DataChannelInit config;
1949
zhihuang9763d562016-08-05 11:14:50 -07001950 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 pc_->CreateDataChannel("1", &config);
1952 EXPECT_TRUE(channel != NULL);
1953 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001954 EXPECT_TRUE(observer_.renegotiation_needed_);
1955 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956
1957 config.ordered = false;
1958 channel = pc_->CreateDataChannel("2", &config);
1959 EXPECT_TRUE(channel != NULL);
1960 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001961 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 config.ordered = true;
1964 config.maxRetransmits = 0;
1965 channel = pc_->CreateDataChannel("3", &config);
1966 EXPECT_TRUE(channel != NULL);
1967 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001968 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969
1970 config.maxRetransmits = -1;
1971 config.maxRetransmitTime = 0;
1972 channel = pc_->CreateDataChannel("4", &config);
1973 EXPECT_TRUE(channel != NULL);
1974 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001975 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976}
1977
1978// This tests that no data channel is returned if both maxRetransmits and
1979// maxRetransmitTime are set for SCTP data channels.
1980TEST_F(PeerConnectionInterfaceTest,
1981 CreateSctpDataChannelShouldFailForInvalidConfig) {
1982 FakeConstraints constraints;
1983 constraints.SetAllowDtlsSctpDataChannels();
1984 CreatePeerConnection(&constraints);
1985
1986 std::string label = "test";
1987 webrtc::DataChannelInit config;
1988 config.maxRetransmits = 0;
1989 config.maxRetransmitTime = 0;
1990
zhihuang9763d562016-08-05 11:14:50 -07001991 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 pc_->CreateDataChannel(label, &config);
1993 EXPECT_TRUE(channel == NULL);
1994}
1995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996// The test verifies that creating a SCTP data channel with an id already in use
1997// or out of range should fail.
1998TEST_F(PeerConnectionInterfaceTest,
1999 CreateSctpDataChannelWithInvalidIdShouldFail) {
2000 FakeConstraints constraints;
2001 constraints.SetAllowDtlsSctpDataChannels();
2002 CreatePeerConnection(&constraints);
2003
2004 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002005 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002007 config.id = 1;
2008 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 EXPECT_TRUE(channel != NULL);
2010 EXPECT_EQ(1, channel->id());
2011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 channel = pc_->CreateDataChannel("x", &config);
2013 EXPECT_TRUE(channel == NULL);
2014
2015 config.id = cricket::kMaxSctpSid;
2016 channel = pc_->CreateDataChannel("max", &config);
2017 EXPECT_TRUE(channel != NULL);
2018 EXPECT_EQ(config.id, channel->id());
2019
2020 config.id = cricket::kMaxSctpSid + 1;
2021 channel = pc_->CreateDataChannel("x", &config);
2022 EXPECT_TRUE(channel == NULL);
2023}
2024
deadbeefab9b2d12015-10-14 11:33:11 -07002025// Verifies that duplicated label is allowed for SCTP data channel.
2026TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2027 FakeConstraints constraints;
2028 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2029 true);
2030 CreatePeerConnection(&constraints);
2031
2032 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002033 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002034 pc_->CreateDataChannel(label, nullptr);
2035 EXPECT_NE(channel, nullptr);
2036
zhihuang9763d562016-08-05 11:14:50 -07002037 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002038 pc_->CreateDataChannel(label, nullptr);
2039 EXPECT_NE(dup_channel, nullptr);
2040}
2041
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002042// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2043// DataChannel.
2044TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2045 FakeConstraints constraints;
2046 constraints.SetAllowRtpDataChannels();
2047 CreatePeerConnection(&constraints);
2048
zhihuang9763d562016-08-05 11:14:50 -07002049 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002050 pc_->CreateDataChannel("test1", NULL);
2051 EXPECT_TRUE(observer_.renegotiation_needed_);
2052 observer_.renegotiation_needed_ = false;
2053
zhihuang9763d562016-08-05 11:14:50 -07002054 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002055 pc_->CreateDataChannel("test2", NULL);
2056 EXPECT_TRUE(observer_.renegotiation_needed_);
2057}
2058
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 FakeConstraints constraints;
2062 constraints.SetAllowRtpDataChannels();
2063 CreatePeerConnection(&constraints);
2064
zhihuang9763d562016-08-05 11:14:50 -07002065 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002067 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 pc_->CreateDataChannel("test2", NULL);
2069 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002070 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002072 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 new MockDataChannelObserver(data2));
2074
2075 CreateOfferReceiveAnswer();
2076 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2077 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2078
2079 ReleasePeerConnection();
2080 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2081 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2082}
2083
2084// This test that data channels can be rejected in an answer.
2085TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2086 FakeConstraints constraints;
2087 constraints.SetAllowRtpDataChannels();
2088 CreatePeerConnection(&constraints);
2089
zhihuang9763d562016-08-05 11:14:50 -07002090 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 pc_->CreateDataChannel("offer_channel", NULL));
2092
2093 CreateOfferAsLocalDescription();
2094
2095 // Create an answer where the m-line for data channels are rejected.
2096 std::string sdp;
2097 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002098 std::unique_ptr<SessionDescriptionInterface> answer(
2099 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2100 sdp, nullptr));
2101 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 cricket::ContentInfo* data_info =
2103 answer->description()->GetContentByName("data");
2104 data_info->rejected = true;
2105
Steve Antondb45ca82017-09-11 18:27:34 -07002106 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2108}
2109
2110// Test that we can create a session description from an SDP string from
2111// FireFox, use it as a remote session description, generate an answer and use
2112// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002113TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 FakeConstraints constraints;
2115 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2116 true);
2117 CreatePeerConnection(&constraints);
2118 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002119 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002121 webrtc::kFireFoxSdpOffer, nullptr));
2122 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 CreateAnswerAsLocalDescription();
2124 ASSERT_TRUE(pc_->local_description() != NULL);
2125 ASSERT_TRUE(pc_->remote_description() != NULL);
2126
2127 const cricket::ContentInfo* content =
2128 cricket::GetFirstAudioContent(pc_->local_description()->description());
2129 ASSERT_TRUE(content != NULL);
2130 EXPECT_FALSE(content->rejected);
2131
2132 content =
2133 cricket::GetFirstVideoContent(pc_->local_description()->description());
2134 ASSERT_TRUE(content != NULL);
2135 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002136#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 content =
2138 cricket::GetFirstDataContent(pc_->local_description()->description());
2139 ASSERT_TRUE(content != NULL);
2140 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002141#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142}
2143
zhihuangb19012e2017-09-19 13:47:59 -07002144// Test that fallback from DTLS to SDES is not supported.
2145// The fallback was previously supported but was removed to simplify the code
2146// and because it's non-standard.
2147TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002148 FakeConstraints constraints;
2149 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2150 true);
2151 CreatePeerConnection(&constraints);
2152 // Wait for fake certificate to be generated. Previously, this is what caused
2153 // the "a=crypto" lines to be rejected.
2154 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2155 ASSERT_NE(nullptr, fake_certificate_generator_);
2156 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2157 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002158 std::unique_ptr<SessionDescriptionInterface> desc(
2159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2160 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002161 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002162}
2163
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164// Test that we can create an audio only offer and receive an answer with a
2165// limited set of audio codecs and receive an updated offer with more audio
2166// codecs, where the added codecs are not supported.
2167TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002168 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 AddVoiceStream("audio_label");
2170 CreateOfferAsLocalDescription();
2171
Steve Antondb45ca82017-09-11 18:27:34 -07002172 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002174 webrtc::kAudioSdp, nullptr));
2175 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176
Steve Antondb45ca82017-09-11 18:27:34 -07002177 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002179 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002180 nullptr));
2181 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 CreateAnswerAsLocalDescription();
2183}
2184
deadbeefc80741f2015-10-22 13:14:45 -07002185// Test that if we're receiving (but not sending) a track, subsequent offers
2186// will have m-lines with a=recvonly.
2187TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2188 FakeConstraints constraints;
2189 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2190 true);
2191 CreatePeerConnection(&constraints);
2192 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2193 CreateAnswerAsLocalDescription();
2194
2195 // At this point we should be receiving stream 1, but not sending anything.
2196 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002197 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002198 DoCreateOffer(&offer, nullptr);
2199
2200 const cricket::ContentInfo* video_content =
2201 cricket::GetFirstVideoContent(offer->description());
2202 const cricket::VideoContentDescription* video_desc =
2203 static_cast<const cricket::VideoContentDescription*>(
2204 video_content->description);
2205 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2206
2207 const cricket::ContentInfo* audio_content =
2208 cricket::GetFirstAudioContent(offer->description());
2209 const cricket::AudioContentDescription* audio_desc =
2210 static_cast<const cricket::AudioContentDescription*>(
2211 audio_content->description);
2212 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2213}
2214
2215// Test that if we're receiving (but not sending) a track, and the
2216// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2217// false, the generated m-lines will be a=inactive.
2218TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2219 FakeConstraints constraints;
2220 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2221 true);
2222 CreatePeerConnection(&constraints);
2223 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2224 CreateAnswerAsLocalDescription();
2225
2226 // At this point we should be receiving stream 1, but not sending anything.
2227 // A new offer would be recvonly, but we'll set the "no receive" constraints
2228 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002229 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002230 FakeConstraints offer_constraints;
2231 offer_constraints.AddMandatory(
2232 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2233 offer_constraints.AddMandatory(
2234 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2235 DoCreateOffer(&offer, &offer_constraints);
2236
2237 const cricket::ContentInfo* video_content =
2238 cricket::GetFirstVideoContent(offer->description());
2239 const cricket::VideoContentDescription* video_desc =
2240 static_cast<const cricket::VideoContentDescription*>(
2241 video_content->description);
2242 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2243
2244 const cricket::ContentInfo* audio_content =
2245 cricket::GetFirstAudioContent(offer->description());
2246 const cricket::AudioContentDescription* audio_desc =
2247 static_cast<const cricket::AudioContentDescription*>(
2248 audio_content->description);
2249 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2250}
2251
deadbeef653b8e02015-11-11 12:55:10 -08002252// Test that we can use SetConfiguration to change the ICE servers of the
2253// PortAllocator.
2254TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2255 CreatePeerConnection();
2256
2257 PeerConnectionInterface::RTCConfiguration config;
2258 PeerConnectionInterface::IceServer server;
2259 server.uri = "stun:test_hostname";
2260 config.servers.push_back(server);
2261 EXPECT_TRUE(pc_->SetConfiguration(config));
2262
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002263 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2264 EXPECT_EQ("test_hostname",
2265 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002266}
2267
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002268TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2269 CreatePeerConnection();
2270 PeerConnectionInterface::RTCConfiguration config;
2271 config.type = PeerConnectionInterface::kRelay;
2272 EXPECT_TRUE(pc_->SetConfiguration(config));
2273 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2274}
2275
deadbeef293e9262017-01-11 12:28:30 -08002276TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2277 PeerConnectionInterface::RTCConfiguration config;
2278 config.prune_turn_ports = false;
2279 CreatePeerConnection(config, nullptr);
2280 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2281
2282 config.prune_turn_ports = true;
2283 EXPECT_TRUE(pc_->SetConfiguration(config));
2284 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2285}
2286
skvladd1f5fda2017-02-03 16:54:05 -08002287// Test that the ice check interval can be changed. This does not verify that
2288// the setting makes it all the way to P2PTransportChannel, as that would
2289// require a very complex set of mocks.
2290TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2291 PeerConnectionInterface::RTCConfiguration config;
2292 config.ice_check_min_interval = rtc::Optional<int>();
2293 CreatePeerConnection(config, nullptr);
2294 config.ice_check_min_interval = rtc::Optional<int>(100);
2295 EXPECT_TRUE(pc_->SetConfiguration(config));
2296 PeerConnectionInterface::RTCConfiguration new_config =
2297 pc_->GetConfiguration();
2298 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2299}
2300
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002301// Test that when SetConfiguration changes both the pool size and other
2302// attributes, the pooled session is created with the updated attributes.
2303TEST_F(PeerConnectionInterfaceTest,
2304 SetConfigurationCreatesPooledSessionCorrectly) {
2305 CreatePeerConnection();
2306 PeerConnectionInterface::RTCConfiguration config;
2307 config.ice_candidate_pool_size = 1;
2308 PeerConnectionInterface::IceServer server;
2309 server.uri = kStunAddressOnly;
2310 config.servers.push_back(server);
2311 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002312 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002313
2314 const cricket::FakePortAllocatorSession* session =
2315 static_cast<const cricket::FakePortAllocatorSession*>(
2316 port_allocator_->GetPooledSession());
2317 ASSERT_NE(nullptr, session);
2318 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002319}
2320
deadbeef293e9262017-01-11 12:28:30 -08002321// Test that after SetLocalDescription, changing the pool size is not allowed,
2322// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002323TEST_F(PeerConnectionInterfaceTest,
2324 CantChangePoolSizeAfterSetLocalDescription) {
2325 CreatePeerConnection();
2326 // Start by setting a size of 1.
2327 PeerConnectionInterface::RTCConfiguration config;
2328 config.ice_candidate_pool_size = 1;
2329 EXPECT_TRUE(pc_->SetConfiguration(config));
2330
2331 // Set remote offer; can still change pool size at this point.
2332 CreateOfferAsRemoteDescription();
2333 config.ice_candidate_pool_size = 2;
2334 EXPECT_TRUE(pc_->SetConfiguration(config));
2335
2336 // Set local answer; now it's too late.
2337 CreateAnswerAsLocalDescription();
2338 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002339 RTCError error;
2340 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2341 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2342}
2343
deadbeef42a42632017-03-10 15:18:00 -08002344// Test that after setting an answer, extra pooled sessions are discarded. The
2345// ICE candidate pool is only intended to be used for the first offer/answer.
2346TEST_F(PeerConnectionInterfaceTest,
2347 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2348 CreatePeerConnection();
2349
2350 // Set a larger-than-necessary size.
2351 PeerConnectionInterface::RTCConfiguration config;
2352 config.ice_candidate_pool_size = 4;
2353 EXPECT_TRUE(pc_->SetConfiguration(config));
2354
2355 // Do offer/answer.
2356 CreateOfferAsRemoteDescription();
2357 CreateAnswerAsLocalDescription();
2358
2359 // Expect no pooled sessions to be left.
2360 const cricket::PortAllocatorSession* session =
2361 port_allocator_->GetPooledSession();
2362 EXPECT_EQ(nullptr, session);
2363}
2364
2365// After Close is called, pooled candidates should be discarded so as to not
2366// waste network resources.
2367TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2368 CreatePeerConnection();
2369
2370 PeerConnectionInterface::RTCConfiguration config;
2371 config.ice_candidate_pool_size = 3;
2372 EXPECT_TRUE(pc_->SetConfiguration(config));
2373 pc_->Close();
2374
2375 // Expect no pooled sessions to be left.
2376 const cricket::PortAllocatorSession* session =
2377 port_allocator_->GetPooledSession();
2378 EXPECT_EQ(nullptr, session);
2379}
2380
deadbeef293e9262017-01-11 12:28:30 -08002381// Test that SetConfiguration returns an invalid modification error if
2382// modifying a field in the configuration that isn't allowed to be modified.
2383TEST_F(PeerConnectionInterfaceTest,
2384 SetConfigurationReturnsInvalidModificationError) {
2385 PeerConnectionInterface::RTCConfiguration config;
2386 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2387 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2388 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2389 CreatePeerConnection(config, nullptr);
2390
2391 PeerConnectionInterface::RTCConfiguration modified_config = config;
2392 modified_config.bundle_policy =
2393 PeerConnectionInterface::kBundlePolicyMaxBundle;
2394 RTCError error;
2395 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2396 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2397
2398 modified_config = config;
2399 modified_config.rtcp_mux_policy =
2400 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2401 error.set_type(RTCErrorType::NONE);
2402 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2403 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2404
2405 modified_config = config;
2406 modified_config.continual_gathering_policy =
2407 PeerConnectionInterface::GATHER_CONTINUALLY;
2408 error.set_type(RTCErrorType::NONE);
2409 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2410 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2411}
2412
2413// Test that SetConfiguration returns a range error if the candidate pool size
2414// is negative or larger than allowed by the spec.
2415TEST_F(PeerConnectionInterfaceTest,
2416 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2417 PeerConnectionInterface::RTCConfiguration config;
2418 CreatePeerConnection(config, nullptr);
2419
2420 config.ice_candidate_pool_size = -1;
2421 RTCError error;
2422 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2423 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2424
2425 config.ice_candidate_pool_size = INT_MAX;
2426 error.set_type(RTCErrorType::NONE);
2427 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2428 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2429}
2430
2431// Test that SetConfiguration returns a syntax error if parsing an ICE server
2432// URL failed.
2433TEST_F(PeerConnectionInterfaceTest,
2434 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2435 PeerConnectionInterface::RTCConfiguration config;
2436 CreatePeerConnection(config, nullptr);
2437
2438 PeerConnectionInterface::IceServer bad_server;
2439 bad_server.uri = "stunn:www.example.com";
2440 config.servers.push_back(bad_server);
2441 RTCError error;
2442 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2443 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2444}
2445
2446// Test that SetConfiguration returns an invalid parameter error if a TURN
2447// IceServer is missing a username or password.
2448TEST_F(PeerConnectionInterfaceTest,
2449 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2450 PeerConnectionInterface::RTCConfiguration config;
2451 CreatePeerConnection(config, nullptr);
2452
2453 PeerConnectionInterface::IceServer bad_server;
2454 bad_server.uri = "turn:www.example.com";
2455 // Missing password.
2456 bad_server.username = "foo";
2457 config.servers.push_back(bad_server);
2458 RTCError error;
2459 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2460 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002461}
2462
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463// Test that PeerConnection::Close changes the states to closed and all remote
2464// tracks change state to ended.
2465TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2466 // Initialize a PeerConnection and negotiate local and remote session
2467 // description.
2468 InitiateCall();
2469 ASSERT_EQ(1u, pc_->local_streams()->count());
2470 ASSERT_EQ(1u, pc_->remote_streams()->count());
2471
2472 pc_->Close();
2473
2474 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2475 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2476 pc_->ice_connection_state());
2477 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2478 pc_->ice_gathering_state());
2479
2480 EXPECT_EQ(1u, pc_->local_streams()->count());
2481 EXPECT_EQ(1u, pc_->remote_streams()->count());
2482
zhihuang9763d562016-08-05 11:14:50 -07002483 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2484 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002485 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002486 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002487 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2488 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2489 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490}
2491
2492// Test that PeerConnection methods fails gracefully after
2493// PeerConnection::Close has been called.
2494TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002495 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2497 CreateOfferAsRemoteDescription();
2498 CreateAnswerAsLocalDescription();
2499
2500 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002501 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 pc_->local_streams()->at(0);
2503
2504 pc_->Close();
2505
2506 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002507 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508
2509 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002510 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002512 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513
2514 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2515
2516 EXPECT_TRUE(pc_->local_description() != NULL);
2517 EXPECT_TRUE(pc_->remote_description() != NULL);
2518
kwibergd1fe2812016-04-27 06:47:29 -07002519 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002520 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002521 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002522 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523
2524 std::string sdp;
2525 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002526 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2527 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2528 nullptr));
2529 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530
2531 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002532 std::unique_ptr<SessionDescriptionInterface> local_offer(
2533 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2534 nullptr));
2535 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002536}
2537
2538// Test that GetStats can still be called after PeerConnection::Close.
2539TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2540 InitiateCall();
2541 pc_->Close();
2542 DoGetStats(NULL);
2543}
deadbeefab9b2d12015-10-14 11:33:11 -07002544
2545// NOTE: The series of tests below come from what used to be
2546// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2547// setting a remote or local description has the expected effects.
2548
2549// This test verifies that the remote MediaStreams corresponding to a received
2550// SDP string is created. In this test the two separate MediaStreams are
2551// signaled.
2552TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2553 FakeConstraints constraints;
2554 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2555 true);
2556 CreatePeerConnection(&constraints);
2557 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2558
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002559 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002560 EXPECT_TRUE(
2561 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2562 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2563 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2564
2565 // Create a session description based on another SDP with another
2566 // MediaStream.
2567 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2568
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002569 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002570 EXPECT_TRUE(
2571 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2572}
2573
2574// This test verifies that when remote tracks are added/removed from SDP, the
2575// created remote streams are updated appropriately.
2576TEST_F(PeerConnectionInterfaceTest,
2577 AddRemoveTrackFromExistingRemoteMediaStream) {
2578 FakeConstraints constraints;
2579 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2580 true);
2581 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002582 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002583 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002584 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002585 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2586 reference_collection_));
2587
2588 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002589 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002590 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002591 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002592 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2593 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002594 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002595 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2596 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002597 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002598 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2599 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002600
2601 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002602 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002603 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002604 MockTrackObserver audio_track_observer(audio_track2);
2605 MockTrackObserver video_track_observer(video_track2);
2606
2607 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2608 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002609 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002610 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2611 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002612 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002613 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002614 audio_track2->state(), kTimeout);
2615 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2616 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002617}
2618
2619// This tests that remote tracks are ended if a local session description is set
2620// that rejects the media content type.
2621TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2622 FakeConstraints constraints;
2623 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2624 true);
2625 CreatePeerConnection(&constraints);
2626 // First create and set a remote offer, then reject its video content in our
2627 // answer.
2628 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2629 ASSERT_EQ(1u, observer_.remote_streams()->count());
2630 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2631 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2632 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2633
2634 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2635 remote_stream->GetVideoTracks()[0];
2636 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2637 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2638 remote_stream->GetAudioTracks()[0];
2639 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2640
kwibergd1fe2812016-04-27 06:47:29 -07002641 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002642 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002643 cricket::ContentInfo* video_info =
2644 local_answer->description()->GetContentByName("video");
2645 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002646 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002647 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2648 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2649
2650 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002651 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002652 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002653 video_info = local_offer->description()->GetContentByName("video");
2654 ASSERT_TRUE(video_info != nullptr);
2655 video_info->rejected = true;
2656 cricket::ContentInfo* audio_info =
2657 local_offer->description()->GetContentByName("audio");
2658 ASSERT_TRUE(audio_info != nullptr);
2659 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002660 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002661 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002662 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002663 remote_audio->state(), kTimeout);
2664 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2665 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002666}
2667
2668// This tests that we won't crash if the remote track has been removed outside
2669// of PeerConnection and then PeerConnection tries to reject the track.
2670TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2671 FakeConstraints constraints;
2672 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2673 true);
2674 CreatePeerConnection(&constraints);
2675 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2676 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2677 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2678 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2679
kwibergd1fe2812016-04-27 06:47:29 -07002680 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002681 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2682 kSdpStringWithStream1, nullptr));
2683 cricket::ContentInfo* video_info =
2684 local_answer->description()->GetContentByName("video");
2685 video_info->rejected = true;
2686 cricket::ContentInfo* audio_info =
2687 local_answer->description()->GetContentByName("audio");
2688 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002689 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002690
2691 // No crash is a pass.
2692}
2693
deadbeef5e97fb52015-10-15 12:49:08 -07002694// This tests that if a recvonly remote description is set, no remote streams
2695// will be created, even if the description contains SSRCs/MSIDs.
2696// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2697TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2698 FakeConstraints constraints;
2699 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2700 true);
2701 CreatePeerConnection(&constraints);
2702
2703 std::string recvonly_offer = kSdpStringWithStream1;
2704 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2705 strlen(kRecvonly), &recvonly_offer);
2706 CreateAndSetRemoteOffer(recvonly_offer);
2707
2708 EXPECT_EQ(0u, observer_.remote_streams()->count());
2709}
2710
deadbeefab9b2d12015-10-14 11:33:11 -07002711// This tests that a default MediaStream is created if a remote session
2712// description doesn't contain any streams and no MSID support.
2713// It also tests that the default stream is updated if a video m-line is added
2714// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002715TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002716 FakeConstraints constraints;
2717 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2718 true);
2719 CreatePeerConnection(&constraints);
2720 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2721
2722 ASSERT_EQ(1u, observer_.remote_streams()->count());
2723 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2724
2725 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2726 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2727 EXPECT_EQ("default", remote_stream->label());
2728
2729 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2730 ASSERT_EQ(1u, observer_.remote_streams()->count());
2731 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2732 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002733 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2734 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002735 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2736 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002737 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2738 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002739}
2740
2741// This tests that a default MediaStream is created if a remote session
2742// description doesn't contain any streams and media direction is send only.
2743TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002744 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002745 FakeConstraints constraints;
2746 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2747 true);
2748 CreatePeerConnection(&constraints);
2749 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2750
2751 ASSERT_EQ(1u, observer_.remote_streams()->count());
2752 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2753
2754 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2755 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2756 EXPECT_EQ("default", remote_stream->label());
2757}
2758
2759// This tests that it won't crash when PeerConnection tries to remove
2760// a remote track that as already been removed from the MediaStream.
2761TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2762 FakeConstraints constraints;
2763 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2764 true);
2765 CreatePeerConnection(&constraints);
2766 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2767 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2768 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2769 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2770
2771 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2772
2773 // No crash is a pass.
2774}
2775
2776// This tests that a default MediaStream is created if the remote session
2777// description doesn't contain any streams and don't contain an indication if
2778// MSID is supported.
2779TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002780 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002781 FakeConstraints constraints;
2782 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2783 true);
2784 CreatePeerConnection(&constraints);
2785 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2786
2787 ASSERT_EQ(1u, observer_.remote_streams()->count());
2788 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2789 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2790 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2791}
2792
2793// This tests that a default MediaStream is not created if the remote session
2794// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002795TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002796 FakeConstraints constraints;
2797 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2798 true);
2799 CreatePeerConnection(&constraints);
2800 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2801 EXPECT_EQ(0u, observer_.remote_streams()->count());
2802}
2803
deadbeefbda7e0b2015-12-08 17:13:40 -08002804// This tests that when setting a new description, the old default tracks are
2805// not destroyed and recreated.
2806// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002807TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002808 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002809 FakeConstraints constraints;
2810 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2811 true);
2812 CreatePeerConnection(&constraints);
2813 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2814
2815 ASSERT_EQ(1u, observer_.remote_streams()->count());
2816 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2817 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2818
2819 // Set the track to "disabled", then set a new description and ensure the
2820 // track is still disabled, which ensures it hasn't been recreated.
2821 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2822 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2823 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2824 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2825}
2826
deadbeefab9b2d12015-10-14 11:33:11 -07002827// This tests that a default MediaStream is not created if a remote session
2828// description is updated to not have any MediaStreams.
2829TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2830 FakeConstraints constraints;
2831 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2832 true);
2833 CreatePeerConnection(&constraints);
2834 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002835 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002836 EXPECT_TRUE(
2837 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2838
2839 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2840 EXPECT_EQ(0u, observer_.remote_streams()->count());
2841}
2842
2843// This tests that an RtpSender is created when the local description is set
2844// after adding a local stream.
2845// TODO(deadbeef): This test and the one below it need to be updated when
2846// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002847TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002848 FakeConstraints constraints;
2849 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2850 true);
2851 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002852
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002853 // Create an offer with 1 stream with 2 tracks of each type.
2854 rtc::scoped_refptr<StreamCollection> stream_collection =
2855 CreateStreamCollection(1, 2);
2856 pc_->AddStream(stream_collection->at(0));
2857 std::unique_ptr<SessionDescriptionInterface> offer;
2858 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002859 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002860
deadbeefab9b2d12015-10-14 11:33:11 -07002861 auto senders = pc_->GetSenders();
2862 EXPECT_EQ(4u, senders.size());
2863 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2864 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2865 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2866 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2867
2868 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002869 pc_->RemoveStream(stream_collection->at(0));
2870 stream_collection = CreateStreamCollection(1, 1);
2871 pc_->AddStream(stream_collection->at(0));
2872 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002873 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002874
deadbeefab9b2d12015-10-14 11:33:11 -07002875 senders = pc_->GetSenders();
2876 EXPECT_EQ(2u, senders.size());
2877 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2878 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2879 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2880 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2881}
2882
2883// This tests that an RtpSender is created when the local description is set
2884// before adding a local stream.
2885TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002886 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002887 FakeConstraints constraints;
2888 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2889 true);
2890 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002891
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002892 rtc::scoped_refptr<StreamCollection> stream_collection =
2893 CreateStreamCollection(1, 2);
2894 // Add a stream to create the offer, but remove it afterwards.
2895 pc_->AddStream(stream_collection->at(0));
2896 std::unique_ptr<SessionDescriptionInterface> offer;
2897 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2898 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002899
Steve Antondb45ca82017-09-11 18:27:34 -07002900 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002901 auto senders = pc_->GetSenders();
2902 EXPECT_EQ(0u, senders.size());
2903
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002904 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002905 senders = pc_->GetSenders();
2906 EXPECT_EQ(4u, senders.size());
2907 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2908 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2909 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2910 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2911}
2912
2913// This tests that the expected behavior occurs if the SSRC on a local track is
2914// changed when SetLocalDescription is called.
2915TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002916 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002917 FakeConstraints constraints;
2918 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2919 true);
2920 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002921
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002922 rtc::scoped_refptr<StreamCollection> stream_collection =
2923 CreateStreamCollection(2, 1);
2924 pc_->AddStream(stream_collection->at(0));
2925 std::unique_ptr<SessionDescriptionInterface> offer;
2926 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2927 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002928 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2929 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002930 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2931 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002932 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002933
deadbeefab9b2d12015-10-14 11:33:11 -07002934 auto senders = pc_->GetSenders();
2935 EXPECT_EQ(2u, senders.size());
2936 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2937 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2938
2939 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002940 cricket::MediaContentDescription* desc =
2941 cricket::GetFirstAudioContentDescription(modified_offer->description());
2942 ASSERT_TRUE(desc != NULL);
2943 for (StreamParams& stream : desc->mutable_streams()) {
2944 for (unsigned int& ssrc : stream.ssrcs) {
2945 ++ssrc;
2946 }
2947 }
deadbeefab9b2d12015-10-14 11:33:11 -07002948
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002949 desc =
2950 cricket::GetFirstVideoContentDescription(modified_offer->description());
2951 ASSERT_TRUE(desc != NULL);
2952 for (StreamParams& stream : desc->mutable_streams()) {
2953 for (unsigned int& ssrc : stream.ssrcs) {
2954 ++ssrc;
2955 }
2956 }
2957
Steve Antondb45ca82017-09-11 18:27:34 -07002958 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002959 senders = pc_->GetSenders();
2960 EXPECT_EQ(2u, senders.size());
2961 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2962 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2963 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2964 // changed.
2965}
2966
2967// This tests that the expected behavior occurs if a new session description is
2968// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002969TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002970 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002971 FakeConstraints constraints;
2972 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2973 true);
2974 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002975
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 rtc::scoped_refptr<StreamCollection> stream_collection =
2977 CreateStreamCollection(2, 1);
2978 pc_->AddStream(stream_collection->at(0));
2979 std::unique_ptr<SessionDescriptionInterface> offer;
2980 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002981 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002982
deadbeefab9b2d12015-10-14 11:33:11 -07002983 auto senders = pc_->GetSenders();
2984 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2986 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002987
2988 // Add a new MediaStream but with the same tracks as in the first stream.
2989 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2990 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002991 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2992 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002993 pc_->AddStream(stream_1);
2994
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002995 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002996 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002997
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002998 auto new_senders = pc_->GetSenders();
2999 // Should be the same senders as before, but with updated stream id.
3000 // Note that this behavior is subject to change in the future.
3001 // We may decide the PC should ignore existing tracks in AddStream.
3002 EXPECT_EQ(senders, new_senders);
3003 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3004 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003005}
3006
zhihuang81c3a032016-11-17 12:06:24 -08003007// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3008TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3009 FakeConstraints constraints;
3010 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3011 true);
3012 CreatePeerConnection(&constraints);
3013 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3014 EXPECT_EQ(observer_.num_added_tracks_, 1);
3015 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3016
3017 // Create and set the updated remote SDP.
3018 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3019 EXPECT_EQ(observer_.num_added_tracks_, 2);
3020 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3021}
3022
deadbeefd1a38b52016-12-10 13:15:33 -08003023// Test that when SetConfiguration is called and the configuration is
3024// changing, the next offer causes an ICE restart.
3025TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3026 PeerConnectionInterface::RTCConfiguration config;
3027 config.type = PeerConnectionInterface::kRelay;
3028 // Need to pass default constraints to prevent disabling of DTLS...
3029 FakeConstraints default_constraints;
3030 CreatePeerConnection(config, &default_constraints);
3031 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3032
3033 // Do initial offer/answer so there's something to restart.
3034 CreateOfferAsLocalDescription();
3035 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3036
3037 // Grab the ufrags.
3038 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3039
3040 // Change ICE policy, which should trigger an ICE restart on the next offer.
3041 config.type = PeerConnectionInterface::kAll;
3042 EXPECT_TRUE(pc_->SetConfiguration(config));
3043 CreateOfferAsLocalDescription();
3044
3045 // Grab the new ufrags.
3046 std::vector<std::string> subsequent_ufrags =
3047 GetUfrags(pc_->local_description());
3048
3049 // Sanity check.
3050 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3051 // Check that each ufrag is different.
3052 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3053 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3054 }
3055}
3056
3057// Test that when SetConfiguration is called and the configuration *isn't*
3058// changing, the next offer does *not* cause an ICE restart.
3059TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3060 PeerConnectionInterface::RTCConfiguration config;
3061 config.type = PeerConnectionInterface::kRelay;
3062 // Need to pass default constraints to prevent disabling of DTLS...
3063 FakeConstraints default_constraints;
3064 CreatePeerConnection(config, &default_constraints);
3065 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3066
3067 // Do initial offer/answer so there's something to restart.
3068 CreateOfferAsLocalDescription();
3069 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3070
3071 // Grab the ufrags.
3072 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3073
3074 // Call SetConfiguration with a config identical to what the PC was
3075 // constructed with.
3076 EXPECT_TRUE(pc_->SetConfiguration(config));
3077 CreateOfferAsLocalDescription();
3078
3079 // Grab the new ufrags.
3080 std::vector<std::string> subsequent_ufrags =
3081 GetUfrags(pc_->local_description());
3082
3083 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3084}
3085
3086// Test for a weird corner case scenario:
3087// 1. Audio/video session established.
3088// 2. SetConfiguration changes ICE config; ICE restart needed.
3089// 3. ICE restart initiated by remote peer, but only for one m= section.
3090// 4. Next createOffer should initiate an ICE restart, but only for the other
3091// m= section; it would be pointless to do an ICE restart for the m= section
3092// that was already restarted.
3093TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3094 PeerConnectionInterface::RTCConfiguration config;
3095 config.type = PeerConnectionInterface::kRelay;
3096 // Need to pass default constraints to prevent disabling of DTLS...
3097 FakeConstraints default_constraints;
3098 CreatePeerConnection(config, &default_constraints);
3099 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3100
3101 // Do initial offer/answer so there's something to restart.
3102 CreateOfferAsLocalDescription();
3103 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3104
3105 // Change ICE policy, which should set the "needs-ice-restart" flag.
3106 config.type = PeerConnectionInterface::kAll;
3107 EXPECT_TRUE(pc_->SetConfiguration(config));
3108
3109 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003110 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3111 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3112 kSdpStringWithStream1, nullptr));
3113 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003114 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3115 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003116 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003117 CreateAnswerAsLocalDescription();
3118
3119 // Grab the ufrags.
3120 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3121 ASSERT_EQ(2, initial_ufrags.size());
3122
3123 // Create offer and grab the new ufrags.
3124 CreateOfferAsLocalDescription();
3125 std::vector<std::string> subsequent_ufrags =
3126 GetUfrags(pc_->local_description());
3127 ASSERT_EQ(2, subsequent_ufrags.size());
3128
3129 // Ensure that only the ufrag for the second m= section changed.
3130 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3131 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3132}
3133
deadbeeffe4a8a42016-12-20 17:56:17 -08003134// Tests that the methods to return current/pending descriptions work as
3135// expected at different points in the offer/answer exchange. This test does
3136// one offer/answer exchange as the offerer, then another as the answerer.
3137TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3138 // This disables DTLS so we can apply an answer to ourselves.
3139 CreatePeerConnection();
3140
3141 // Create initial local offer and get SDP (which will also be used as
3142 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003143 std::unique_ptr<SessionDescriptionInterface> local_offer;
3144 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003145 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003146 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003147
3148 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003149 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3150 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3151 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003152 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3153 EXPECT_EQ(nullptr, pc_->current_local_description());
3154 EXPECT_EQ(nullptr, pc_->current_remote_description());
3155
3156 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003157 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003158 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003159 sdp, nullptr));
3160 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3161 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3162 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3163 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003164 EXPECT_EQ(nullptr, pc_->current_local_description());
3165 EXPECT_EQ(nullptr, pc_->current_remote_description());
3166
3167 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003168 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3169 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3170 sdp, nullptr));
3171 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3172 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003173 EXPECT_EQ(nullptr, pc_->pending_local_description());
3174 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003175 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3176 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003177
3178 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003179 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3180 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3181 nullptr));
3182 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3183 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3184 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003185 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003186 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3187 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003188
3189 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003190 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003191 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003192 sdp, nullptr));
3193 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3194 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3195 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3196 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3197 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3198 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003199
3200 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003201 std::unique_ptr<SessionDescriptionInterface> local_answer(
3202 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3203 sdp, nullptr));
3204 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3205 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003206 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3207 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003208 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3209 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003210}
3211
zhihuang77985012017-02-07 15:45:16 -08003212// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3213// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003214// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003215TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003216 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003217 CreatePeerConnection();
3218 // The RtcEventLog will be reset when the PeerConnection is closed.
3219 pc_->Close();
3220
Elad Alon9e6565b2017-10-11 16:04:13 +02003221 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3222 std::string filename = webrtc::test::OutputPath() +
3223 test_info->test_case_name() + test_info->name();
3224 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3225
3226 constexpr int64_t max_size_bytes = 1024;
3227
zhihuang77985012017-02-07 15:45:16 -08003228 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3229 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003230
3231 // Cleanup.
3232 rtc::ClosePlatformFile(file);
3233 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003234}
3235
Elad Alon99c3fe52017-10-13 16:29:40 +02003236// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3237// after the PeerConnection is closed.
3238// This version tests the StartRtcEventLog version that receives an object
3239// of type |RtcEventLogOutput|.
3240TEST_F(PeerConnectionInterfaceTest,
3241 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3242 CreatePeerConnection();
3243 // The RtcEventLog will be reset when the PeerConnection is closed.
3244 pc_->Close();
3245
3246 rtc::PlatformFile file = 0;
3247 int64_t max_size_bytes = 1024;
3248 EXPECT_FALSE(pc_->StartRtcEventLog(
3249 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes)));
3250 pc_->StopRtcEventLog();
3251}
3252
deadbeef30952b42017-04-21 02:41:29 -07003253// Test that generated offers/answers include "ice-option:trickle".
3254TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3255 CreatePeerConnection();
3256
3257 // First, create an offer with audio/video.
3258 FakeConstraints constraints;
3259 constraints.SetMandatoryReceiveAudio(true);
3260 constraints.SetMandatoryReceiveVideo(true);
3261 std::unique_ptr<SessionDescriptionInterface> offer;
3262 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3263 cricket::SessionDescription* desc = offer->description();
3264 ASSERT_EQ(2u, desc->transport_infos().size());
3265 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3266 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3267
3268 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003269 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003270 std::unique_ptr<SessionDescriptionInterface> answer;
3271 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3272 desc = answer->description();
3273 ASSERT_EQ(2u, desc->transport_infos().size());
3274 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3275 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3276}
3277
deadbeef1dcb1642017-03-29 21:08:16 -07003278// Test that ICE renomination isn't offered if it's not enabled in the PC's
3279// RTCConfiguration.
3280TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3281 PeerConnectionInterface::RTCConfiguration config;
3282 config.enable_ice_renomination = false;
3283 CreatePeerConnection(config, nullptr);
3284 AddVoiceStream("foo");
3285
3286 std::unique_ptr<SessionDescriptionInterface> offer;
3287 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3288 cricket::SessionDescription* desc = offer->description();
3289 EXPECT_EQ(1u, desc->transport_infos().size());
3290 EXPECT_FALSE(
3291 desc->transport_infos()[0].description.GetIceParameters().renomination);
3292}
3293
3294// Test that the ICE renomination option is present in generated offers/answers
3295// if it's enabled in the PC's RTCConfiguration.
3296TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3297 PeerConnectionInterface::RTCConfiguration config;
3298 config.enable_ice_renomination = true;
3299 CreatePeerConnection(config, nullptr);
3300 AddVoiceStream("foo");
3301
3302 std::unique_ptr<SessionDescriptionInterface> offer;
3303 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3304 cricket::SessionDescription* desc = offer->description();
3305 EXPECT_EQ(1u, desc->transport_infos().size());
3306 EXPECT_TRUE(
3307 desc->transport_infos()[0].description.GetIceParameters().renomination);
3308
3309 // Set the offer as a remote description, then create an answer and ensure it
3310 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003311 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003312 std::unique_ptr<SessionDescriptionInterface> answer;
3313 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3314 desc = answer->description();
3315 EXPECT_EQ(1u, desc->transport_infos().size());
3316 EXPECT_TRUE(
3317 desc->transport_infos()[0].description.GetIceParameters().renomination);
3318}
3319
3320// Test that if CreateOffer is called with the deprecated "offer to receive
3321// audio/video" constraints, they're processed and result in an offer with
3322// audio/video sections just as if RTCOfferAnswerOptions had been used.
3323TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3324 CreatePeerConnection();
3325
3326 FakeConstraints constraints;
3327 constraints.SetMandatoryReceiveAudio(true);
3328 constraints.SetMandatoryReceiveVideo(true);
3329 std::unique_ptr<SessionDescriptionInterface> offer;
3330 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3331
3332 cricket::SessionDescription* desc = offer->description();
3333 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3334 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3335 ASSERT_NE(nullptr, audio);
3336 ASSERT_NE(nullptr, video);
3337 EXPECT_FALSE(audio->rejected);
3338 EXPECT_FALSE(video->rejected);
3339}
3340
3341// Test that if CreateAnswer is called with the deprecated "offer to receive
3342// audio/video" constraints, they're processed and can be used to reject an
3343// offered m= section just as can be done with RTCOfferAnswerOptions;
3344TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3345 CreatePeerConnection();
3346
3347 // First, create an offer with audio/video and apply it as a remote
3348 // description.
3349 FakeConstraints constraints;
3350 constraints.SetMandatoryReceiveAudio(true);
3351 constraints.SetMandatoryReceiveVideo(true);
3352 std::unique_ptr<SessionDescriptionInterface> offer;
3353 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003354 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003355
3356 // Now create answer that rejects audio/video.
3357 constraints.SetMandatoryReceiveAudio(false);
3358 constraints.SetMandatoryReceiveVideo(false);
3359 std::unique_ptr<SessionDescriptionInterface> answer;
3360 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3361
3362 cricket::SessionDescription* desc = answer->description();
3363 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3364 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3365 ASSERT_NE(nullptr, audio);
3366 ASSERT_NE(nullptr, video);
3367 EXPECT_TRUE(audio->rejected);
3368 EXPECT_TRUE(video->rejected);
3369}
3370
3371#ifdef HAVE_SCTP
3372#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3373 DataChannelOnlyOfferWithMaxBundlePolicy
3374#else
3375#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3376 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3377#endif
3378
3379// Test that negotiation can succeed with a data channel only, and with the max
3380// bundle policy. Previously there was a bug that prevented this.
3381TEST_F(PeerConnectionInterfaceTest,
3382 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3383 PeerConnectionInterface::RTCConfiguration config;
3384 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3385 CreatePeerConnection(config, nullptr);
3386
3387 // First, create an offer with only a data channel and apply it as a remote
3388 // description.
3389 pc_->CreateDataChannel("test", nullptr);
3390 std::unique_ptr<SessionDescriptionInterface> offer;
3391 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003392 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003393
3394 // Create and set answer as well.
3395 std::unique_ptr<SessionDescriptionInterface> answer;
3396 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003397 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003398}
3399
zstein4b979802017-06-02 14:37:37 -07003400TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3401 CreatePeerConnection();
3402 PeerConnectionInterface::BitrateParameters bitrate;
3403 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3404 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3405}
3406
3407TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3408 CreatePeerConnection();
3409 PeerConnectionInterface::BitrateParameters bitrate;
3410 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3411 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3412}
3413
3414TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3415 CreatePeerConnection();
3416 PeerConnectionInterface::BitrateParameters bitrate;
3417 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3418 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3419 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3420}
3421
3422TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3423 CreatePeerConnection();
3424 PeerConnectionInterface::BitrateParameters bitrate;
3425 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3426 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3427}
3428
3429TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3430 CreatePeerConnection();
3431 PeerConnectionInterface::BitrateParameters bitrate;
3432 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3433 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3434 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3435}
3436
3437TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3438 CreatePeerConnection();
3439 PeerConnectionInterface::BitrateParameters bitrate;
3440 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3441 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3442 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3443}
3444
3445TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3446 CreatePeerConnection();
3447 PeerConnectionInterface::BitrateParameters bitrate;
3448 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3449 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3450}
3451
Steve Anton038834f2017-07-14 15:59:59 -07003452// ice_regather_interval_range requires WebRTC to be configured for continual
3453// gathering already.
3454TEST_F(PeerConnectionInterfaceTest,
3455 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3456 PeerConnectionInterface::RTCConfiguration config;
3457 config.ice_regather_interval_range.emplace(1000, 2000);
3458 config.continual_gathering_policy =
3459 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3460 CreatePeerConnectionExpectFail(config);
3461}
3462
3463// Ensures that there is no error when ice_regather_interval_range is set with
3464// continual gathering enabled.
3465TEST_F(PeerConnectionInterfaceTest,
3466 SetIceRegatherIntervalRangeWithContinualGathering) {
3467 PeerConnectionInterface::RTCConfiguration config;
3468 config.ice_regather_interval_range.emplace(1000, 2000);
3469 config.continual_gathering_policy =
3470 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3471 CreatePeerConnection(config, nullptr);
3472}
3473
zstein4b979802017-06-02 14:37:37 -07003474// The current bitrate from Call's BitrateConfigMask is currently clamped by
3475// Call's BitrateConfig, which comes from the SDP or a default value. This test
3476// checks that a call to SetBitrate with a current bitrate that will be clamped
3477// succeeds.
3478TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3479 CreatePeerConnection();
3480 PeerConnectionInterface::BitrateParameters bitrate;
3481 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3482 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3483}
3484
zhihuang1c378ed2017-08-17 14:10:50 -07003485// The following tests verify that the offer can be created correctly.
3486TEST_F(PeerConnectionInterfaceTest,
3487 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3488 RTCOfferAnswerOptions rtc_options;
3489
3490 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3491 // than kMaxOfferToReceiveMedia should be treated as invalid.
3492 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3493 CreatePeerConnection();
3494 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3495
3496 rtc_options.offer_to_receive_audio =
3497 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3498 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3499}
3500
3501TEST_F(PeerConnectionInterfaceTest,
3502 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3503 RTCOfferAnswerOptions rtc_options;
3504
3505 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3506 // than kMaxOfferToReceiveMedia should be treated as invalid.
3507 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3508 CreatePeerConnection();
3509 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3510
3511 rtc_options.offer_to_receive_video =
3512 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3513 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3514}
3515
3516// Test that the audio and video content will be added to an offer if both
3517// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3518TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3519 RTCOfferAnswerOptions rtc_options;
3520 rtc_options.offer_to_receive_audio = 1;
3521 rtc_options.offer_to_receive_video = 1;
3522
3523 std::unique_ptr<SessionDescriptionInterface> offer;
3524 CreatePeerConnection();
3525 offer = CreateOfferWithOptions(rtc_options);
3526 ASSERT_TRUE(offer);
3527 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3528 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3529}
3530
3531// Test that only audio content will be added to the offer if only
3532// |offer_to_receive_audio| options is 1.
3533TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3534 RTCOfferAnswerOptions rtc_options;
3535 rtc_options.offer_to_receive_audio = 1;
3536 rtc_options.offer_to_receive_video = 0;
3537
3538 std::unique_ptr<SessionDescriptionInterface> offer;
3539 CreatePeerConnection();
3540 offer = CreateOfferWithOptions(rtc_options);
3541 ASSERT_TRUE(offer);
3542 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3543 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3544}
3545
3546// Test that only video content will be added if only |offer_to_receive_video|
3547// options is 1.
3548TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3549 RTCOfferAnswerOptions rtc_options;
3550 rtc_options.offer_to_receive_audio = 0;
3551 rtc_options.offer_to_receive_video = 1;
3552
3553 std::unique_ptr<SessionDescriptionInterface> offer;
3554 CreatePeerConnection();
3555 offer = CreateOfferWithOptions(rtc_options);
3556 ASSERT_TRUE(offer);
3557 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3558 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3559}
3560
zhihuang1c378ed2017-08-17 14:10:50 -07003561// Test that no media content will be added to the offer if using default
3562// RTCOfferAnswerOptions.
3563TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3564 RTCOfferAnswerOptions rtc_options;
3565
3566 std::unique_ptr<SessionDescriptionInterface> offer;
3567 CreatePeerConnection();
3568 offer = CreateOfferWithOptions(rtc_options);
3569 ASSERT_TRUE(offer);
3570 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3571 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3572}
3573
3574// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3575// ufrag/pwd will be the same in the new offer.
3576TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3577 RTCOfferAnswerOptions rtc_options;
3578 rtc_options.ice_restart = false;
3579 rtc_options.offer_to_receive_audio = 1;
3580
3581 std::unique_ptr<SessionDescriptionInterface> offer;
3582 CreatePeerConnection();
3583 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3584 auto ufrag1 = offer->description()
3585 ->GetTransportInfoByName(cricket::CN_AUDIO)
3586 ->description.ice_ufrag;
3587 auto pwd1 = offer->description()
3588 ->GetTransportInfoByName(cricket::CN_AUDIO)
3589 ->description.ice_pwd;
3590
3591 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3592 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3593 auto ufrag2 = offer->description()
3594 ->GetTransportInfoByName(cricket::CN_AUDIO)
3595 ->description.ice_ufrag;
3596 auto pwd2 = offer->description()
3597 ->GetTransportInfoByName(cricket::CN_AUDIO)
3598 ->description.ice_pwd;
3599
3600 // |ice_restart| is true, the ufrag/pwd should change.
3601 rtc_options.ice_restart = true;
3602 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3603 auto ufrag3 = offer->description()
3604 ->GetTransportInfoByName(cricket::CN_AUDIO)
3605 ->description.ice_ufrag;
3606 auto pwd3 = offer->description()
3607 ->GetTransportInfoByName(cricket::CN_AUDIO)
3608 ->description.ice_pwd;
3609
3610 EXPECT_EQ(ufrag1, ufrag2);
3611 EXPECT_EQ(pwd1, pwd2);
3612 EXPECT_NE(ufrag2, ufrag3);
3613 EXPECT_NE(pwd2, pwd3);
3614}
3615
3616// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3617// offer; if it is false, there won't be any bundle group in the offer.
3618TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3619 RTCOfferAnswerOptions rtc_options;
3620 rtc_options.offer_to_receive_audio = 1;
3621 rtc_options.offer_to_receive_video = 1;
3622
3623 std::unique_ptr<SessionDescriptionInterface> offer;
3624 CreatePeerConnection();
3625
3626 rtc_options.use_rtp_mux = true;
3627 offer = CreateOfferWithOptions(rtc_options);
3628 ASSERT_TRUE(offer);
3629 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3630 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3631 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3632
3633 rtc_options.use_rtp_mux = false;
3634 offer = CreateOfferWithOptions(rtc_options);
3635 ASSERT_TRUE(offer);
3636 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3637 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3638 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3639}
3640
zhihuang141aacb2017-08-29 13:23:53 -07003641// This test ensures OnRenegotiationNeeded is called when we add track with
3642// MediaStream -> AddTrack in the same way it is called when we add track with
3643// PeerConnection -> AddTrack.
3644// The test can be removed once addStream is rewritten in terms of addTrack
3645// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3646TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3647 CreatePeerConnectionWithoutDtls();
3648 rtc::scoped_refptr<MediaStreamInterface> stream(
3649 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3650 pc_->AddStream(stream);
3651 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3652 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3653 rtc::scoped_refptr<VideoTrackInterface> video_track(
3654 pc_factory_->CreateVideoTrack(
3655 "video_track", pc_factory_->CreateVideoSource(
3656 std::unique_ptr<cricket::VideoCapturer>(
3657 new cricket::FakeVideoCapturer()))));
3658 stream->AddTrack(audio_track);
3659 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3660 observer_.renegotiation_needed_ = false;
3661
3662 stream->AddTrack(video_track);
3663 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3664 observer_.renegotiation_needed_ = false;
3665
3666 stream->RemoveTrack(audio_track);
3667 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3668 observer_.renegotiation_needed_ = false;
3669
3670 stream->RemoveTrack(video_track);
3671 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3672 observer_.renegotiation_needed_ = false;
3673}
3674
nisse51542be2016-02-12 02:27:06 -08003675class PeerConnectionMediaConfigTest : public testing::Test {
3676 protected:
3677 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003678 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003679 pcf_->Initialize();
3680 }
nisseeaabdf62017-05-05 02:23:02 -07003681 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003682 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003683 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003684 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3685 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003686 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003687 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003688 }
3689
zhihuang9763d562016-08-05 11:14:50 -07003690 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003691 MockPeerConnectionObserver observer_;
3692};
3693
3694// This test verifies the default behaviour with no constraints and a
3695// default RTCConfiguration.
3696TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3697 PeerConnectionInterface::RTCConfiguration config;
3698 FakeConstraints constraints;
3699
3700 const cricket::MediaConfig& media_config =
3701 TestCreatePeerConnection(config, &constraints);
3702
3703 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003704 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3705 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3706 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003707}
3708
3709// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003710// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003711TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3712 PeerConnectionInterface::RTCConfiguration config;
3713 FakeConstraints constraints;
3714
3715 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3716 const cricket::MediaConfig& media_config =
3717 TestCreatePeerConnection(config, &constraints);
3718
3719 EXPECT_TRUE(media_config.enable_dscp);
3720}
3721
3722// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003723// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003724TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3725 PeerConnectionInterface::RTCConfiguration config;
3726 FakeConstraints constraints;
3727
3728 constraints.AddOptional(
3729 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3730 const cricket::MediaConfig media_config =
3731 TestCreatePeerConnection(config, &constraints);
3732
nisse0db023a2016-03-01 04:29:59 -08003733 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003734}
3735
3736// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003737// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003738TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3739 PeerConnectionInterface::RTCConfiguration config;
3740 FakeConstraints constraints;
3741
Niels Möller71bdda02016-03-31 12:59:59 +02003742 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003743 const cricket::MediaConfig& media_config =
3744 TestCreatePeerConnection(config, &constraints);
3745
nisse0db023a2016-03-01 04:29:59 -08003746 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3747}
3748
3749// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003750// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003751TEST_F(PeerConnectionMediaConfigTest,
3752 TestSuspendBelowMinBitrateConstraintTrue) {
3753 PeerConnectionInterface::RTCConfiguration config;
3754 FakeConstraints constraints;
3755
3756 constraints.AddOptional(
3757 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3758 true);
3759 const cricket::MediaConfig media_config =
3760 TestCreatePeerConnection(config, &constraints);
3761
3762 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003763}
3764
deadbeef293e9262017-01-11 12:28:30 -08003765// Tests a few random fields being different.
3766TEST(RTCConfigurationTest, ComparisonOperators) {
3767 PeerConnectionInterface::RTCConfiguration a;
3768 PeerConnectionInterface::RTCConfiguration b;
3769 EXPECT_EQ(a, b);
3770
3771 PeerConnectionInterface::RTCConfiguration c;
3772 c.servers.push_back(PeerConnectionInterface::IceServer());
3773 EXPECT_NE(a, c);
3774
3775 PeerConnectionInterface::RTCConfiguration d;
3776 d.type = PeerConnectionInterface::kRelay;
3777 EXPECT_NE(a, d);
3778
3779 PeerConnectionInterface::RTCConfiguration e;
3780 e.audio_jitter_buffer_max_packets = 5;
3781 EXPECT_NE(a, e);
3782
3783 PeerConnectionInterface::RTCConfiguration f;
3784 f.ice_connection_receiving_timeout = 1337;
3785 EXPECT_NE(a, f);
3786
3787 PeerConnectionInterface::RTCConfiguration g;
3788 g.disable_ipv6 = true;
3789 EXPECT_NE(a, g);
3790
3791 PeerConnectionInterface::RTCConfiguration h(
3792 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3793 EXPECT_NE(a, h);
3794}