blob: 084126b2ee2a759b11f9af57f347f036747db1de [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(),
deadbeefd7850b22017-08-23 10:59:19 -0700611 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700612 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700613 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700614 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700615 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 }
617
618 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700619 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 }
621
deadbeef293e9262017-01-11 12:28:30 -0800622 // DTLS does not work in a loopback call, so is disabled for most of the
623 // tests in this file.
624 void CreatePeerConnectionWithoutDtls() {
625 FakeConstraints no_dtls_constraints;
626 no_dtls_constraints.AddMandatory(
627 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
628
629 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
630 &no_dtls_constraints);
631 }
632
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700634 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
635 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 }
637
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700638 void CreatePeerConnectionWithIceTransportsType(
639 PeerConnectionInterface::IceTransportsType type) {
640 PeerConnectionInterface::RTCConfiguration config;
641 config.type = type;
642 return CreatePeerConnection(config, nullptr);
643 }
644
645 void CreatePeerConnectionWithIceServer(const std::string& uri,
646 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800647 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700649 server.uri = uri;
650 server.password = password;
651 config.servers.push_back(server);
652 CreatePeerConnection(config, nullptr);
653 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700655 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
656 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700657 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800658 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
659 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000660
deadbeef1dcb1642017-03-29 21:08:16 -0700661 // Create certificate generator unless DTLS constraint is explicitly set to
662 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200663 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000664 bool dtls;
665 if (FindConstraint(constraints,
666 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
667 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200668 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800669 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
670 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000671 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200672 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800673 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200674 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 ASSERT_TRUE(pc_.get() != NULL);
676 observer_.SetPeerConnectionInterface(pc_.get());
677 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
678 }
679
deadbeef0a6c4ca2015-10-06 11:38:28 -0700680 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800681 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700682 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700683 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800684 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700685
zhihuang9763d562016-08-05 11:14:50 -0700686 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700687 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
688 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800689 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700690 }
691
Steve Anton038834f2017-07-14 15:59:59 -0700692 void CreatePeerConnectionExpectFail(
693 PeerConnectionInterface::RTCConfiguration config) {
694 PeerConnectionInterface::IceServer server;
695 server.uri = kTurnIceServerUri;
696 server.password = kTurnPassword;
697 config.servers.push_back(server);
698 rtc::scoped_refptr<PeerConnectionInterface> pc =
699 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
700 EXPECT_EQ(nullptr, pc);
701 }
702
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700704 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800705 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
706 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
707 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710
deadbeef0a6c4ca2015-10-06 11:38:28 -0700711 CreatePeerConnectionExpectFail(kStunInvalidPort);
712 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
713 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700715 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800716 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
717 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800721 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 void ReleasePeerConnection() {
727 pc_ = NULL;
728 observer_.SetPeerConnectionInterface(NULL);
729 }
730
deadbeefab9b2d12015-10-14 11:33:11 -0700731 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700733 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700735 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800736 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
737 new cricket::FakeVideoCapturer()),
738 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700739 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 pc_factory_->CreateVideoTrack(label + "v0", video_source));
741 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000742 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
744 observer_.renegotiation_needed_ = false;
745 }
746
747 void AddVoiceStream(const std::string& label) {
748 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700749 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700751 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 pc_factory_->CreateAudioTrack(label + "a0", NULL));
753 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000754 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
756 observer_.renegotiation_needed_ = false;
757 }
758
759 void AddAudioVideoStream(const std::string& stream_label,
760 const std::string& audio_track_label,
761 const std::string& video_track_label) {
762 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700763 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700765 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 pc_factory_->CreateAudioTrack(
767 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
768 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700769 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700770 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800771 video_track_label, pc_factory_->CreateVideoSource(
772 std::unique_ptr<cricket::VideoCapturer>(
773 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000775 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
777 observer_.renegotiation_needed_ = false;
778 }
779
kwibergd1fe2812016-04-27 06:47:29 -0700780 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700781 bool offer,
782 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000783 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
784 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 MockCreateSessionDescriptionObserver>());
786 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700787 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700789 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 }
791 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700792 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 return observer->result();
794 }
795
kwibergd1fe2812016-04-27 06:47:29 -0700796 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700797 MediaConstraintsInterface* constraints) {
798 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 }
800
kwibergd1fe2812016-04-27 06:47:29 -0700801 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700802 MediaConstraintsInterface* constraints) {
803 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 }
805
Steve Antondb45ca82017-09-11 18:27:34 -0700806 bool DoSetSessionDescription(
807 std::unique_ptr<SessionDescriptionInterface> desc,
808 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000809 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
810 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 MockSetSessionDescriptionObserver>());
812 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700813 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700815 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
zhihuang29ff8442016-07-27 11:07:25 -0700817 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
818 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
819 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 return observer->result();
821 }
822
Steve Antondb45ca82017-09-11 18:27:34 -0700823 bool DoSetLocalDescription(
824 std::unique_ptr<SessionDescriptionInterface> desc) {
825 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
Steve Antondb45ca82017-09-11 18:27:34 -0700828 bool DoSetRemoteDescription(
829 std::unique_ptr<SessionDescriptionInterface> desc) {
830 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
832
833 // Calls PeerConnection::GetStats and check the return value.
834 // It does not verify the values in the StatReports since a RTCP packet might
835 // be required.
836 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000837 rtc::scoped_refptr<MockStatsObserver> observer(
838 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000839 if (!pc_->GetStats(
840 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return false;
842 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
843 return observer->called();
844 }
845
846 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800847 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 // Create a local stream with audio&video tracks.
849 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
850 CreateOfferReceiveAnswer();
851 }
852
853 // Verify that RTP Header extensions has been negotiated for audio and video.
854 void VerifyRemoteRtpHeaderExtensions() {
855 const cricket::MediaContentDescription* desc =
856 cricket::GetFirstAudioContentDescription(
857 pc_->remote_description()->description());
858 ASSERT_TRUE(desc != NULL);
859 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
860
861 desc = cricket::GetFirstVideoContentDescription(
862 pc_->remote_description()->description());
863 ASSERT_TRUE(desc != NULL);
864 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
865 }
866
867 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700868 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700869 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 std::string sdp;
871 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700872 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700874 sdp, nullptr));
875 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
877 }
878
deadbeefab9b2d12015-10-14 11:33:11 -0700879 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700880 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700881 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700882 sdp, nullptr));
883 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700884 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
885 }
886
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700888 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700889 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
891 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
892 // audio codec change, even if the parameter has nothing to do with
893 // receiving. Not all parameters are serialized to SDP.
894 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
895 // the SessionDescription, it is necessary to do that here to in order to
896 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
897 // https://code.google.com/p/webrtc/issues/detail?id=1356
898 std::string sdp;
899 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700900 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700902 sdp, nullptr));
903 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
905 }
906
907 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700908 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700909 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910
911 std::string sdp;
912 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700913 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700915 sdp, nullptr));
916 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
918 }
919
920 void CreateOfferReceiveAnswer() {
921 CreateOfferAsLocalDescription();
922 std::string sdp;
923 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
924 CreateAnswerAsRemoteDescription(sdp);
925 }
926
927 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700928 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700929 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
931 // audio codec change, even if the parameter has nothing to do with
932 // receiving. Not all parameters are serialized to SDP.
933 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
934 // the SessionDescription, it is necessary to do that here to in order to
935 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
936 // https://code.google.com/p/webrtc/issues/detail?id=1356
937 std::string sdp;
938 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700939 std::unique_ptr<SessionDescriptionInterface> new_offer(
940 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
941 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942
Steve Antondb45ca82017-09-11 18:27:34 -0700943 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000945 // Wait for the ice_complete message, so that SDP will have candidates.
946 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 }
948
deadbeefab9b2d12015-10-14 11:33:11 -0700949 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700950 std::unique_ptr<SessionDescriptionInterface> answer(
951 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
952 sdp, nullptr));
953 ASSERT_TRUE(answer);
954 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
956 }
957
deadbeefab9b2d12015-10-14 11:33:11 -0700958 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700959 std::unique_ptr<SessionDescriptionInterface> pr_answer(
960 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
961 sdp, nullptr));
962 ASSERT_TRUE(pr_answer);
963 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700965 std::unique_ptr<SessionDescriptionInterface> answer(
966 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
967 sdp, nullptr));
968 ASSERT_TRUE(answer);
969 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
971 }
972
973 // Help function used for waiting until a the last signaled remote stream has
974 // the same label as |stream_label|. In a few of the tests in this file we
975 // answer with the same session description as we offer and thus we can
976 // check if OnAddStream have been called with the same stream as we offer to
977 // send.
978 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
979 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
980 }
981
982 // Creates an offer and applies it as a local session description.
983 // Creates an answer with the same SDP an the offer but removes all lines
984 // that start with a:ssrc"
985 void CreateOfferReceiveAnswerWithoutSsrc() {
986 CreateOfferAsLocalDescription();
987 std::string sdp;
988 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
989 SetSsrcToZero(&sdp);
990 CreateAnswerAsRemoteDescription(sdp);
991 }
992
deadbeefab9b2d12015-10-14 11:33:11 -0700993 // This function creates a MediaStream with label kStreams[0] and
994 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
995 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700996 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700997 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700998 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700999 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1000 size_t number_of_video_tracks) {
1001 EXPECT_LE(number_of_audio_tracks, 2u);
1002 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001003
1004 reference_collection_ = StreamCollection::Create();
1005 std::string sdp_ms1 = std::string(kSdpStringInit);
1006
1007 std::string mediastream_label = kStreams[0];
1008
1009 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1010 webrtc::MediaStream::Create(mediastream_label));
1011 reference_collection_->AddStream(stream);
1012
1013 if (number_of_audio_tracks > 0) {
1014 sdp_ms1 += std::string(kSdpStringAudio);
1015 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1016 AddAudioTrack(kAudioTracks[0], stream);
1017 }
1018 if (number_of_audio_tracks > 1) {
1019 sdp_ms1 += kSdpStringMs1Audio1;
1020 AddAudioTrack(kAudioTracks[1], stream);
1021 }
1022
1023 if (number_of_video_tracks > 0) {
1024 sdp_ms1 += std::string(kSdpStringVideo);
1025 sdp_ms1 += std::string(kSdpStringMs1Video0);
1026 AddVideoTrack(kVideoTracks[0], stream);
1027 }
1028 if (number_of_video_tracks > 1) {
1029 sdp_ms1 += kSdpStringMs1Video1;
1030 AddVideoTrack(kVideoTracks[1], stream);
1031 }
1032
kwibergd1fe2812016-04-27 06:47:29 -07001033 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001034 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1035 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001036 }
1037
1038 void AddAudioTrack(const std::string& track_id,
1039 MediaStreamInterface* stream) {
1040 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1041 webrtc::AudioTrack::Create(track_id, nullptr));
1042 ASSERT_TRUE(stream->AddTrack(audio_track));
1043 }
1044
1045 void AddVideoTrack(const std::string& track_id,
1046 MediaStreamInterface* stream) {
1047 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001048 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001049 webrtc::FakeVideoTrackSource::Create(),
1050 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001051 ASSERT_TRUE(stream->AddTrack(video_track));
1052 }
1053
kwibergfd8be342016-05-14 19:44:11 -07001054 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001055 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001056 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001057 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001058 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1059 return offer;
1060 }
1061
kwibergfd8be342016-05-14 19:44:11 -07001062 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001063 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001064 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001065 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001066 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1067 return answer;
1068 }
1069
1070 const std::string& GetFirstAudioStreamCname(
1071 const SessionDescriptionInterface* desc) {
1072 const cricket::ContentInfo* audio_content =
1073 cricket::GetFirstAudioContent(desc->description());
1074 const cricket::AudioContentDescription* audio_desc =
1075 static_cast<const cricket::AudioContentDescription*>(
1076 audio_content->description);
1077 return audio_desc->streams()[0].cname;
1078 }
1079
zhihuang1c378ed2017-08-17 14:10:50 -07001080 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1081 const RTCOfferAnswerOptions& offer_answer_options) {
1082 RTC_DCHECK(pc_);
1083 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1084 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1085 pc_->CreateOffer(observer, offer_answer_options);
1086 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1087 return observer->MoveDescription();
1088 }
1089
1090 void CreateOfferWithOptionsAsRemoteDescription(
1091 std::unique_ptr<SessionDescriptionInterface>* desc,
1092 const RTCOfferAnswerOptions& offer_answer_options) {
1093 *desc = CreateOfferWithOptions(offer_answer_options);
1094 ASSERT_TRUE(desc != nullptr);
1095 std::string sdp;
1096 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001097 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001098 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001099 sdp, nullptr));
1100 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001101 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1102 }
1103
1104 void CreateOfferWithOptionsAsLocalDescription(
1105 std::unique_ptr<SessionDescriptionInterface>* desc,
1106 const RTCOfferAnswerOptions& offer_answer_options) {
1107 *desc = CreateOfferWithOptions(offer_answer_options);
1108 ASSERT_TRUE(desc != nullptr);
1109 std::string sdp;
1110 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001111 std::unique_ptr<SessionDescriptionInterface> new_offer(
1112 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1113 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001114
Steve Antondb45ca82017-09-11 18:27:34 -07001115 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001116 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1117 }
1118
1119 bool HasCNCodecs(const cricket::ContentInfo* content) {
1120 const cricket::ContentDescription* description = content->description;
1121 RTC_DCHECK(description);
1122 const cricket::AudioContentDescription* audio_content_desc =
1123 static_cast<const cricket::AudioContentDescription*>(description);
1124 RTC_DCHECK(audio_content_desc);
1125 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1126 if (audio_content_desc->codecs()[i].name == "CN")
1127 return true;
1128 }
1129 return false;
1130 }
1131
deadbeef9a6f4d42017-05-15 19:43:33 -07001132 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1133 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001134 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001135 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001136 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001137 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1138 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1139 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001141 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142};
1143
zhihuang29ff8442016-07-27 11:07:25 -07001144// Test that no callbacks on the PeerConnectionObserver are called after the
1145// PeerConnection is closed.
1146TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001147 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001148 pc_factory_for_test_->CreatePeerConnection(
1149 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1150 nullptr, &observer_));
1151 observer_.SetPeerConnectionInterface(pc.get());
1152 pc->Close();
1153
1154 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001155 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001156 std::vector<cricket::Candidate> candidates;
1157 pc_factory_for_test_->transport_controller->SignalGatheringState(
1158 cricket::IceGatheringState{});
1159 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1160 "", candidates);
1161 pc_factory_for_test_->transport_controller->SignalConnectionState(
1162 cricket::IceConnectionState{});
1163 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1164 candidates);
1165 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001166 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001167}
1168
zhihuang8f65cdf2016-05-06 18:40:30 -07001169// Generate different CNAMEs when PeerConnections are created.
1170// The CNAMEs are expected to be generated randomly. It is possible
1171// that the test fails, though the possibility is very low.
1172TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001173 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001174 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001175 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001176 CreateOfferWithOneAudioStream();
1177 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1178 GetFirstAudioStreamCname(offer2.get()));
1179}
1180
1181TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001182 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001183 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001184 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001185 CreateAnswerWithOneAudioStream();
1186 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1187 GetFirstAudioStreamCname(answer2.get()));
1188}
1189
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190TEST_F(PeerConnectionInterfaceTest,
1191 CreatePeerConnectionWithDifferentConfigurations) {
1192 CreatePeerConnectionWithDifferentConfigurations();
1193}
1194
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001195TEST_F(PeerConnectionInterfaceTest,
1196 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1197 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1198 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1199 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1200 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1201 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1202 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1203 port_allocator_->candidate_filter());
1204 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1205 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1206}
1207
1208// Test that when a PeerConnection is created with a nonzero candidate pool
1209// size, the pooled PortAllocatorSession is created with all the attributes
1210// in the RTCConfiguration.
1211TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1212 PeerConnectionInterface::RTCConfiguration config;
1213 PeerConnectionInterface::IceServer server;
1214 server.uri = kStunAddressOnly;
1215 config.servers.push_back(server);
1216 config.type = PeerConnectionInterface::kRelay;
1217 config.disable_ipv6 = true;
1218 config.tcp_candidate_policy =
1219 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001220 config.candidate_network_policy =
1221 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001222 config.ice_candidate_pool_size = 1;
1223 CreatePeerConnection(config, nullptr);
1224
1225 const cricket::FakePortAllocatorSession* session =
1226 static_cast<const cricket::FakePortAllocatorSession*>(
1227 port_allocator_->GetPooledSession());
1228 ASSERT_NE(nullptr, session);
1229 EXPECT_EQ(1UL, session->stun_servers().size());
1230 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1231 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001232 EXPECT_LT(0U,
1233 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001234}
1235
deadbeefd21eab32017-07-26 16:50:11 -07001236// Test that network-related RTCConfiguration members are applied to the
1237// PortAllocator when CreatePeerConnection is called. Specifically:
1238// - disable_ipv6_on_wifi
1239// - max_ipv6_networks
1240// - tcp_candidate_policy
1241// - candidate_network_policy
1242// - prune_turn_ports
1243//
1244// Note that the candidate filter (RTCConfiguration::type) is already tested
1245// above.
1246TEST_F(PeerConnectionInterfaceTest,
1247 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1248 // Create fake port allocator.
1249 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1250 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1251 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1252
1253 // Create RTCConfiguration with some network-related fields relevant to
1254 // PortAllocator populated.
1255 PeerConnectionInterface::RTCConfiguration config;
1256 config.disable_ipv6_on_wifi = true;
1257 config.max_ipv6_networks = 10;
1258 config.tcp_candidate_policy =
1259 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1260 config.candidate_network_policy =
1261 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1262 config.prune_turn_ports = true;
1263
1264 // Create the PC factory and PC with the above config.
1265 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1266 webrtc::CreatePeerConnectionFactory(
1267 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001268 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1269 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001270 rtc::scoped_refptr<PeerConnectionInterface> pc(
1271 pc_factory->CreatePeerConnection(
1272 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1273
1274 // Now validate that the config fields set above were applied to the
1275 // PortAllocator, as flags or otherwise.
1276 EXPECT_FALSE(raw_port_allocator->flags() &
1277 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1278 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1279 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1280 EXPECT_TRUE(raw_port_allocator->flags() &
1281 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1282 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1283}
1284
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001285// Test that the PeerConnection initializes the port allocator passed into it,
1286// and on the correct thread.
1287TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001288 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001289 std::unique_ptr<rtc::Thread> network_thread(
1290 rtc::Thread::CreateWithSocketServer());
1291 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001292 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1293 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001294 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001295 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001296 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001297 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001298 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1299 PeerConnectionInterface::RTCConfiguration config;
1300 rtc::scoped_refptr<PeerConnectionInterface> pc(
1301 pc_factory->CreatePeerConnection(
1302 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1303 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1304 // so all we have to do here is check that it's initialized.
1305 EXPECT_TRUE(raw_port_allocator->initialized());
1306}
1307
deadbeef46c73892016-11-16 19:42:04 -08001308// Check that GetConfiguration returns the configuration the PeerConnection was
1309// constructed with, before SetConfiguration is called.
1310TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1311 PeerConnectionInterface::RTCConfiguration config;
1312 config.type = PeerConnectionInterface::kRelay;
1313 CreatePeerConnection(config, nullptr);
1314
1315 PeerConnectionInterface::RTCConfiguration returned_config =
1316 pc_->GetConfiguration();
1317 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1318}
1319
1320// Check that GetConfiguration returns the last configuration passed into
1321// SetConfiguration.
1322TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1323 CreatePeerConnection();
1324
1325 PeerConnectionInterface::RTCConfiguration config;
1326 config.type = PeerConnectionInterface::kRelay;
1327 EXPECT_TRUE(pc_->SetConfiguration(config));
1328
1329 PeerConnectionInterface::RTCConfiguration returned_config =
1330 pc_->GetConfiguration();
1331 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1332}
1333
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001335 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001336 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 AddVoiceStream(kStreamLabel2);
1338 ASSERT_EQ(2u, pc_->local_streams()->count());
1339
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001340 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001341 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001343 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1344 pc_factory_->CreateAudioTrack(kStreamLabel3,
1345 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001347 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001348 EXPECT_EQ(3u, pc_->local_streams()->count());
1349
1350 // Remove the third stream.
1351 pc_->RemoveStream(pc_->local_streams()->at(2));
1352 EXPECT_EQ(2u, pc_->local_streams()->count());
1353
1354 // Remove the second stream.
1355 pc_->RemoveStream(pc_->local_streams()->at(1));
1356 EXPECT_EQ(1u, pc_->local_streams()->count());
1357
1358 // Remove the first stream.
1359 pc_->RemoveStream(pc_->local_streams()->at(0));
1360 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361}
1362
deadbeefab9b2d12015-10-14 11:33:11 -07001363// Test that the created offer includes streams we added.
1364TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001365 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001366 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001367 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001368 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001369
1370 const cricket::ContentInfo* audio_content =
1371 cricket::GetFirstAudioContent(offer->description());
1372 const cricket::AudioContentDescription* audio_desc =
1373 static_cast<const cricket::AudioContentDescription*>(
1374 audio_content->description);
1375 EXPECT_TRUE(
1376 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1377
1378 const cricket::ContentInfo* video_content =
1379 cricket::GetFirstVideoContent(offer->description());
1380 const cricket::VideoContentDescription* video_desc =
1381 static_cast<const cricket::VideoContentDescription*>(
1382 video_content->description);
1383 EXPECT_TRUE(
1384 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1385
1386 // Add another stream and ensure the offer includes both the old and new
1387 // streams.
1388 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001389 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001390
1391 audio_content = cricket::GetFirstAudioContent(offer->description());
1392 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1393 audio_content->description);
1394 EXPECT_TRUE(
1395 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1396 EXPECT_TRUE(
1397 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1398
1399 video_content = cricket::GetFirstVideoContent(offer->description());
1400 video_desc = static_cast<const cricket::VideoContentDescription*>(
1401 video_content->description);
1402 EXPECT_TRUE(
1403 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1404 EXPECT_TRUE(
1405 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1406}
1407
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001409 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001410 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411 ASSERT_EQ(1u, pc_->local_streams()->count());
1412 pc_->RemoveStream(pc_->local_streams()->at(0));
1413 EXPECT_EQ(0u, pc_->local_streams()->count());
1414}
1415
deadbeefe1f9d832016-01-14 15:35:42 -08001416// Test for AddTrack and RemoveTrack methods.
1417// Tests that the created offer includes tracks we added,
1418// and that the RtpSenders are created correctly.
1419// Also tests that RemoveTrack removes the tracks from subsequent offers.
1420TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001421 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001422 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001423 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001424 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1425 std::vector<MediaStreamInterface*> stream_list;
1426 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001427 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001428 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001429 rtc::scoped_refptr<VideoTrackInterface> video_track(
1430 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001431 "video_track", pc_factory_->CreateVideoSource(
1432 std::unique_ptr<cricket::VideoCapturer>(
1433 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001434 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1435 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001436 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1437 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001438 EXPECT_EQ("audio_track", audio_sender->id());
1439 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001440 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1441 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001442 EXPECT_EQ("video_track", video_sender->id());
1443 EXPECT_EQ(video_track, video_sender->track());
1444
1445 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001446 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001447 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001448
1449 const cricket::ContentInfo* audio_content =
1450 cricket::GetFirstAudioContent(offer->description());
1451 const cricket::AudioContentDescription* audio_desc =
1452 static_cast<const cricket::AudioContentDescription*>(
1453 audio_content->description);
1454 EXPECT_TRUE(
1455 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1456
1457 const cricket::ContentInfo* video_content =
1458 cricket::GetFirstVideoContent(offer->description());
1459 const cricket::VideoContentDescription* video_desc =
1460 static_cast<const cricket::VideoContentDescription*>(
1461 video_content->description);
1462 EXPECT_TRUE(
1463 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1464
Steve Antondb45ca82017-09-11 18:27:34 -07001465 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001466
1467 // Now try removing the tracks.
1468 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1469 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1470
1471 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001472 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001473
1474 audio_content = cricket::GetFirstAudioContent(offer->description());
1475 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1476 audio_content->description);
1477 EXPECT_FALSE(
1478 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1479
1480 video_content = cricket::GetFirstVideoContent(offer->description());
1481 video_desc = static_cast<const cricket::VideoContentDescription*>(
1482 video_content->description);
1483 EXPECT_FALSE(
1484 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1485
Steve Antondb45ca82017-09-11 18:27:34 -07001486 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001487
1488 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1489 // should return false.
1490 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1491 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1492}
1493
1494// Test creating senders without a stream specified,
1495// expecting a random stream ID to be generated.
1496TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001497 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001498 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001499 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001500 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001501 rtc::scoped_refptr<VideoTrackInterface> video_track(
1502 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001503 "video_track", pc_factory_->CreateVideoSource(
1504 std::unique_ptr<cricket::VideoCapturer>(
1505 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001506 auto audio_sender =
1507 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1508 auto video_sender =
1509 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1510 EXPECT_EQ("audio_track", audio_sender->id());
1511 EXPECT_EQ(audio_track, audio_sender->track());
1512 EXPECT_EQ("video_track", video_sender->id());
1513 EXPECT_EQ(video_track, video_sender->track());
1514 // If the ID is truly a random GUID, it should be infinitely unlikely they
1515 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001516 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001517}
1518
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1520 InitiateCall();
1521 WaitAndVerifyOnAddStream(kStreamLabel1);
1522 VerifyRemoteRtpHeaderExtensions();
1523}
1524
1525TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001526 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001527 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 CreateOfferAsLocalDescription();
1529 std::string offer;
1530 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1531 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1532 WaitAndVerifyOnAddStream(kStreamLabel1);
1533}
1534
1535TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001536 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001537 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538
1539 CreateOfferAsRemoteDescription();
1540 CreateAnswerAsLocalDescription();
1541
1542 WaitAndVerifyOnAddStream(kStreamLabel1);
1543}
1544
1545TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001546 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001547 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548
1549 CreateOfferAsRemoteDescription();
1550 CreatePrAnswerAsLocalDescription();
1551 CreateAnswerAsLocalDescription();
1552
1553 WaitAndVerifyOnAddStream(kStreamLabel1);
1554}
1555
1556TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1557 InitiateCall();
1558 ASSERT_EQ(1u, pc_->remote_streams()->count());
1559 pc_->RemoveStream(pc_->local_streams()->at(0));
1560 CreateOfferReceiveAnswer();
1561 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001562 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 CreateOfferReceiveAnswer();
1564}
1565
1566// Tests that after negotiating an audio only call, the respondent can perform a
1567// renegotiation that removes the audio stream.
1568TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001569 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 AddVoiceStream(kStreamLabel1);
1571 CreateOfferAsRemoteDescription();
1572 CreateAnswerAsLocalDescription();
1573
1574 ASSERT_EQ(1u, pc_->remote_streams()->count());
1575 pc_->RemoveStream(pc_->local_streams()->at(0));
1576 CreateOfferReceiveAnswer();
1577 EXPECT_EQ(0u, pc_->remote_streams()->count());
1578}
1579
1580// Test that candidates are generated and that we can parse our own candidates.
1581TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001582 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583
Steve Antonf1c6db12017-10-13 11:13:35 -07001584 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001586 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001587 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001588 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001589 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590
1591 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001592 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001593 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001594 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595
Steve Antonf1c6db12017-10-13 11:13:35 -07001596 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1598
Steve Antonf1c6db12017-10-13 11:13:35 -07001599 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600}
1601
deadbeefab9b2d12015-10-14 11:33:11 -07001602// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603// not unique.
1604TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001605 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001607 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001608 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001609 EXPECT_TRUE(offer);
1610 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611
1612 // Create a local stream with audio&video tracks having same label.
1613 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1614
1615 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001616 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617
1618 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001619 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001620 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621}
1622
1623// Test that we will get different SSRCs for each tracks in the offer and answer
1624// we created.
1625TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001626 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 // Create a local stream with audio&video tracks having different labels.
1628 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1629
1630 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001631 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001632 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 int audio_ssrc = 0;
1634 int video_ssrc = 0;
1635 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1636 &audio_ssrc));
1637 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1638 &video_ssrc));
1639 EXPECT_NE(audio_ssrc, video_ssrc);
1640
1641 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001642 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001643 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001644 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 audio_ssrc = 0;
1646 video_ssrc = 0;
1647 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1648 &audio_ssrc));
1649 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1650 &video_ssrc));
1651 EXPECT_NE(audio_ssrc, video_ssrc);
1652}
1653
deadbeefeb459812015-12-15 19:24:43 -08001654// Test that it's possible to call AddTrack on a MediaStream after adding
1655// the stream to a PeerConnection.
1656// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1657TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001658 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001659 // Create audio stream and add to PeerConnection.
1660 AddVoiceStream(kStreamLabel1);
1661 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1662
1663 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001664 rtc::scoped_refptr<VideoTrackInterface> video_track(
1665 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001666 "video_label", pc_factory_->CreateVideoSource(
1667 std::unique_ptr<cricket::VideoCapturer>(
1668 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001669 stream->AddTrack(video_track.get());
1670
kwibergd1fe2812016-04-27 06:47:29 -07001671 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001672 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001673
1674 const cricket::MediaContentDescription* video_desc =
1675 cricket::GetFirstVideoContentDescription(offer->description());
1676 EXPECT_TRUE(video_desc != nullptr);
1677}
1678
1679// Test that it's possible to call RemoveTrack on a MediaStream after adding
1680// the stream to a PeerConnection.
1681// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1682TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001683 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001684 // Create audio/video stream and add to PeerConnection.
1685 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1686 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1687
1688 // Remove the video track.
1689 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1690
kwibergd1fe2812016-04-27 06:47:29 -07001691 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001692 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001693
1694 const cricket::MediaContentDescription* video_desc =
1695 cricket::GetFirstVideoContentDescription(offer->description());
1696 EXPECT_TRUE(video_desc == nullptr);
1697}
1698
deadbeef1dcb1642017-03-29 21:08:16 -07001699// Verify that CreateDtmfSender only succeeds if called with a valid local
1700// track. Other aspects of DtmfSenders are tested in
1701// peerconnection_integrationtest.cc.
1702TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1703 CreatePeerConnection();
1704 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1705 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1706 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1707 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1708 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1709}
1710
deadbeefbd7d8f72015-12-18 16:58:44 -08001711// Test creating a sender with a stream ID, and ensure the ID is populated
1712// in the offer.
1713TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001714 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001715 pc_->CreateSender("video", kStreamLabel1);
1716
kwibergd1fe2812016-04-27 06:47:29 -07001717 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001718 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001719
1720 const cricket::MediaContentDescription* video_desc =
1721 cricket::GetFirstVideoContentDescription(offer->description());
1722 ASSERT_TRUE(video_desc != nullptr);
1723 ASSERT_EQ(1u, video_desc->streams().size());
1724 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1725}
1726
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727// Test that we can specify a certain track that we want statistics about.
1728TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1729 InitiateCall();
1730 ASSERT_LT(0u, pc_->remote_streams()->count());
1731 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001732 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1734 EXPECT_TRUE(DoGetStats(remote_audio));
1735
1736 // Remove the stream. Since we are sending to our selves the local
1737 // and the remote stream is the same.
1738 pc_->RemoveStream(pc_->local_streams()->at(0));
1739 // Do a re-negotiation.
1740 CreateOfferReceiveAnswer();
1741
1742 ASSERT_EQ(0u, pc_->remote_streams()->count());
1743
1744 // Test that we still can get statistics for the old track. Even if it is not
1745 // sent any longer.
1746 EXPECT_TRUE(DoGetStats(remote_audio));
1747}
1748
1749// Test that we can get stats on a video track.
1750TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1751 InitiateCall();
1752 ASSERT_LT(0u, pc_->remote_streams()->count());
1753 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001754 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1756 EXPECT_TRUE(DoGetStats(remote_video));
1757}
1758
1759// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001760TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001762 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 pc_factory_->CreateAudioTrack("unknown track", NULL));
1764 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1765}
1766
1767// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 FakeConstraints constraints;
1770 constraints.SetAllowRtpDataChannels();
1771 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001772 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001774 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 pc_->CreateDataChannel("test2", NULL);
1776 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001777 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001779 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 new MockDataChannelObserver(data2));
1781
1782 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1783 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1784 std::string data_to_send1 = "testing testing";
1785 std::string data_to_send2 = "testing something else";
1786 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1787
1788 CreateOfferReceiveAnswer();
1789 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1790 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1791
1792 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1793 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1794 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1795 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1796
1797 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1798 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1799
1800 data1->Close();
1801 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1802 CreateOfferReceiveAnswer();
1803 EXPECT_FALSE(observer1->IsOpen());
1804 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1805 EXPECT_TRUE(observer2->IsOpen());
1806
1807 data_to_send2 = "testing something else again";
1808 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1809
1810 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1811}
1812
1813// This test verifies that sendnig binary data over RTP data channels should
1814// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 FakeConstraints constraints;
1817 constraints.SetAllowRtpDataChannels();
1818 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001819 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001821 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 pc_->CreateDataChannel("test2", NULL);
1823 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001824 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001826 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 new MockDataChannelObserver(data2));
1828
1829 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1830 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1831
1832 CreateOfferReceiveAnswer();
1833 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1834 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1835
1836 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1837 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1838
jbaucheec21bd2016-03-20 06:15:43 -07001839 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1841}
1842
1843// This test setup a RTP data channels in loop back and test that a channel is
1844// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 FakeConstraints constraints;
1847 constraints.SetAllowRtpDataChannels();
1848 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001849 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001851 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 new MockDataChannelObserver(data1));
1853
1854 CreateOfferReceiveAnswerWithoutSsrc();
1855
1856 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1857
1858 data1->Close();
1859 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1860 CreateOfferReceiveAnswerWithoutSsrc();
1861 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1862 EXPECT_FALSE(observer1->IsOpen());
1863}
1864
1865// This test that if a data channel is added in an answer a receive only channel
1866// channel is created.
1867TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1868 FakeConstraints constraints;
1869 constraints.SetAllowRtpDataChannels();
1870 CreatePeerConnection(&constraints);
1871
1872 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001873 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 pc_->CreateDataChannel(offer_label, NULL);
1875
1876 CreateOfferAsLocalDescription();
1877
1878 // Replace the data channel label in the offer and apply it as an answer.
1879 std::string receive_label = "answer_channel";
1880 std::string sdp;
1881 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001882 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 receive_label.c_str(), receive_label.length(),
1884 &sdp);
1885 CreateAnswerAsRemoteDescription(sdp);
1886
1887 // Verify that a new incoming data channel has been created and that
1888 // it is open but can't we written to.
1889 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1890 DataChannelInterface* received_channel = observer_.last_datachannel_;
1891 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1892 EXPECT_EQ(receive_label, received_channel->label());
1893 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1894
1895 // Verify that the channel we initially offered has been rejected.
1896 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1897
1898 // Do another offer / answer exchange and verify that the data channel is
1899 // opened.
1900 CreateOfferReceiveAnswer();
1901 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1902 kTimeout);
1903}
1904
1905// This test that no data channel is returned if a reliable channel is
1906// requested.
1907// TODO(perkj): Remove this test once reliable channels are implemented.
1908TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1909 FakeConstraints constraints;
1910 constraints.SetAllowRtpDataChannels();
1911 CreatePeerConnection(&constraints);
1912
1913 std::string label = "test";
1914 webrtc::DataChannelInit config;
1915 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001916 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 pc_->CreateDataChannel(label, &config);
1918 EXPECT_TRUE(channel == NULL);
1919}
1920
deadbeefab9b2d12015-10-14 11:33:11 -07001921// Verifies that duplicated label is not allowed for RTP data channel.
1922TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1923 FakeConstraints constraints;
1924 constraints.SetAllowRtpDataChannels();
1925 CreatePeerConnection(&constraints);
1926
1927 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001928 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001929 pc_->CreateDataChannel(label, nullptr);
1930 EXPECT_NE(channel, nullptr);
1931
zhihuang9763d562016-08-05 11:14:50 -07001932 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001933 pc_->CreateDataChannel(label, nullptr);
1934 EXPECT_EQ(dup_channel, nullptr);
1935}
1936
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937// This tests that a SCTP data channel is returned using different
1938// DataChannelInit configurations.
1939TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1940 FakeConstraints constraints;
1941 constraints.SetAllowDtlsSctpDataChannels();
1942 CreatePeerConnection(&constraints);
1943
1944 webrtc::DataChannelInit config;
1945
zhihuang9763d562016-08-05 11:14:50 -07001946 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 pc_->CreateDataChannel("1", &config);
1948 EXPECT_TRUE(channel != NULL);
1949 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001950 EXPECT_TRUE(observer_.renegotiation_needed_);
1951 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952
1953 config.ordered = false;
1954 channel = pc_->CreateDataChannel("2", &config);
1955 EXPECT_TRUE(channel != NULL);
1956 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001957 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958
1959 config.ordered = true;
1960 config.maxRetransmits = 0;
1961 channel = pc_->CreateDataChannel("3", &config);
1962 EXPECT_TRUE(channel != NULL);
1963 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001964 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965
1966 config.maxRetransmits = -1;
1967 config.maxRetransmitTime = 0;
1968 channel = pc_->CreateDataChannel("4", &config);
1969 EXPECT_TRUE(channel != NULL);
1970 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001971 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972}
1973
1974// This tests that no data channel is returned if both maxRetransmits and
1975// maxRetransmitTime are set for SCTP data channels.
1976TEST_F(PeerConnectionInterfaceTest,
1977 CreateSctpDataChannelShouldFailForInvalidConfig) {
1978 FakeConstraints constraints;
1979 constraints.SetAllowDtlsSctpDataChannels();
1980 CreatePeerConnection(&constraints);
1981
1982 std::string label = "test";
1983 webrtc::DataChannelInit config;
1984 config.maxRetransmits = 0;
1985 config.maxRetransmitTime = 0;
1986
zhihuang9763d562016-08-05 11:14:50 -07001987 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 pc_->CreateDataChannel(label, &config);
1989 EXPECT_TRUE(channel == NULL);
1990}
1991
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992// The test verifies that creating a SCTP data channel with an id already in use
1993// or out of range should fail.
1994TEST_F(PeerConnectionInterfaceTest,
1995 CreateSctpDataChannelWithInvalidIdShouldFail) {
1996 FakeConstraints constraints;
1997 constraints.SetAllowDtlsSctpDataChannels();
1998 CreatePeerConnection(&constraints);
1999
2000 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002001 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002003 config.id = 1;
2004 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 EXPECT_TRUE(channel != NULL);
2006 EXPECT_EQ(1, channel->id());
2007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 channel = pc_->CreateDataChannel("x", &config);
2009 EXPECT_TRUE(channel == NULL);
2010
2011 config.id = cricket::kMaxSctpSid;
2012 channel = pc_->CreateDataChannel("max", &config);
2013 EXPECT_TRUE(channel != NULL);
2014 EXPECT_EQ(config.id, channel->id());
2015
2016 config.id = cricket::kMaxSctpSid + 1;
2017 channel = pc_->CreateDataChannel("x", &config);
2018 EXPECT_TRUE(channel == NULL);
2019}
2020
deadbeefab9b2d12015-10-14 11:33:11 -07002021// Verifies that duplicated label is allowed for SCTP data channel.
2022TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2023 FakeConstraints constraints;
2024 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2025 true);
2026 CreatePeerConnection(&constraints);
2027
2028 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002029 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002030 pc_->CreateDataChannel(label, nullptr);
2031 EXPECT_NE(channel, nullptr);
2032
zhihuang9763d562016-08-05 11:14:50 -07002033 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002034 pc_->CreateDataChannel(label, nullptr);
2035 EXPECT_NE(dup_channel, nullptr);
2036}
2037
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002038// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2039// DataChannel.
2040TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2041 FakeConstraints constraints;
2042 constraints.SetAllowRtpDataChannels();
2043 CreatePeerConnection(&constraints);
2044
zhihuang9763d562016-08-05 11:14:50 -07002045 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002046 pc_->CreateDataChannel("test1", NULL);
2047 EXPECT_TRUE(observer_.renegotiation_needed_);
2048 observer_.renegotiation_needed_ = false;
2049
zhihuang9763d562016-08-05 11:14:50 -07002050 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002051 pc_->CreateDataChannel("test2", NULL);
2052 EXPECT_TRUE(observer_.renegotiation_needed_);
2053}
2054
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 FakeConstraints constraints;
2058 constraints.SetAllowRtpDataChannels();
2059 CreatePeerConnection(&constraints);
2060
zhihuang9763d562016-08-05 11:14:50 -07002061 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002063 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 pc_->CreateDataChannel("test2", NULL);
2065 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002066 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002068 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 new MockDataChannelObserver(data2));
2070
2071 CreateOfferReceiveAnswer();
2072 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2073 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2074
2075 ReleasePeerConnection();
2076 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2077 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2078}
2079
2080// This test that data channels can be rejected in an answer.
2081TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2082 FakeConstraints constraints;
2083 constraints.SetAllowRtpDataChannels();
2084 CreatePeerConnection(&constraints);
2085
zhihuang9763d562016-08-05 11:14:50 -07002086 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 pc_->CreateDataChannel("offer_channel", NULL));
2088
2089 CreateOfferAsLocalDescription();
2090
2091 // Create an answer where the m-line for data channels are rejected.
2092 std::string sdp;
2093 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002094 std::unique_ptr<SessionDescriptionInterface> answer(
2095 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2096 sdp, nullptr));
2097 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 cricket::ContentInfo* data_info =
2099 answer->description()->GetContentByName("data");
2100 data_info->rejected = true;
2101
Steve Antondb45ca82017-09-11 18:27:34 -07002102 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2104}
2105
2106// Test that we can create a session description from an SDP string from
2107// FireFox, use it as a remote session description, generate an answer and use
2108// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002109TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 FakeConstraints constraints;
2111 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2112 true);
2113 CreatePeerConnection(&constraints);
2114 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002115 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002117 webrtc::kFireFoxSdpOffer, nullptr));
2118 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 CreateAnswerAsLocalDescription();
2120 ASSERT_TRUE(pc_->local_description() != NULL);
2121 ASSERT_TRUE(pc_->remote_description() != NULL);
2122
2123 const cricket::ContentInfo* content =
2124 cricket::GetFirstAudioContent(pc_->local_description()->description());
2125 ASSERT_TRUE(content != NULL);
2126 EXPECT_FALSE(content->rejected);
2127
2128 content =
2129 cricket::GetFirstVideoContent(pc_->local_description()->description());
2130 ASSERT_TRUE(content != NULL);
2131 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002132#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 content =
2134 cricket::GetFirstDataContent(pc_->local_description()->description());
2135 ASSERT_TRUE(content != NULL);
2136 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002137#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138}
2139
zhihuangb19012e2017-09-19 13:47:59 -07002140// Test that fallback from DTLS to SDES is not supported.
2141// The fallback was previously supported but was removed to simplify the code
2142// and because it's non-standard.
2143TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002144 FakeConstraints constraints;
2145 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2146 true);
2147 CreatePeerConnection(&constraints);
2148 // Wait for fake certificate to be generated. Previously, this is what caused
2149 // the "a=crypto" lines to be rejected.
2150 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2151 ASSERT_NE(nullptr, fake_certificate_generator_);
2152 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2153 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002154 std::unique_ptr<SessionDescriptionInterface> desc(
2155 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2156 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002157 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002158}
2159
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160// Test that we can create an audio only offer and receive an answer with a
2161// limited set of audio codecs and receive an updated offer with more audio
2162// codecs, where the added codecs are not supported.
2163TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002164 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 AddVoiceStream("audio_label");
2166 CreateOfferAsLocalDescription();
2167
Steve Antondb45ca82017-09-11 18:27:34 -07002168 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002170 webrtc::kAudioSdp, nullptr));
2171 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172
Steve Antondb45ca82017-09-11 18:27:34 -07002173 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002175 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002176 nullptr));
2177 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 CreateAnswerAsLocalDescription();
2179}
2180
deadbeefc80741f2015-10-22 13:14:45 -07002181// Test that if we're receiving (but not sending) a track, subsequent offers
2182// will have m-lines with a=recvonly.
2183TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2184 FakeConstraints constraints;
2185 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2186 true);
2187 CreatePeerConnection(&constraints);
2188 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2189 CreateAnswerAsLocalDescription();
2190
2191 // At this point we should be receiving stream 1, but not sending anything.
2192 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002193 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002194 DoCreateOffer(&offer, nullptr);
2195
2196 const cricket::ContentInfo* video_content =
2197 cricket::GetFirstVideoContent(offer->description());
2198 const cricket::VideoContentDescription* video_desc =
2199 static_cast<const cricket::VideoContentDescription*>(
2200 video_content->description);
2201 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2202
2203 const cricket::ContentInfo* audio_content =
2204 cricket::GetFirstAudioContent(offer->description());
2205 const cricket::AudioContentDescription* audio_desc =
2206 static_cast<const cricket::AudioContentDescription*>(
2207 audio_content->description);
2208 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2209}
2210
2211// Test that if we're receiving (but not sending) a track, and the
2212// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2213// false, the generated m-lines will be a=inactive.
2214TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2215 FakeConstraints constraints;
2216 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2217 true);
2218 CreatePeerConnection(&constraints);
2219 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2220 CreateAnswerAsLocalDescription();
2221
2222 // At this point we should be receiving stream 1, but not sending anything.
2223 // A new offer would be recvonly, but we'll set the "no receive" constraints
2224 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002225 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002226 FakeConstraints offer_constraints;
2227 offer_constraints.AddMandatory(
2228 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2229 offer_constraints.AddMandatory(
2230 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2231 DoCreateOffer(&offer, &offer_constraints);
2232
2233 const cricket::ContentInfo* video_content =
2234 cricket::GetFirstVideoContent(offer->description());
2235 const cricket::VideoContentDescription* video_desc =
2236 static_cast<const cricket::VideoContentDescription*>(
2237 video_content->description);
2238 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2239
2240 const cricket::ContentInfo* audio_content =
2241 cricket::GetFirstAudioContent(offer->description());
2242 const cricket::AudioContentDescription* audio_desc =
2243 static_cast<const cricket::AudioContentDescription*>(
2244 audio_content->description);
2245 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2246}
2247
deadbeef653b8e02015-11-11 12:55:10 -08002248// Test that we can use SetConfiguration to change the ICE servers of the
2249// PortAllocator.
2250TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2251 CreatePeerConnection();
2252
2253 PeerConnectionInterface::RTCConfiguration config;
2254 PeerConnectionInterface::IceServer server;
2255 server.uri = "stun:test_hostname";
2256 config.servers.push_back(server);
2257 EXPECT_TRUE(pc_->SetConfiguration(config));
2258
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002259 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2260 EXPECT_EQ("test_hostname",
2261 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002262}
2263
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002264TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2265 CreatePeerConnection();
2266 PeerConnectionInterface::RTCConfiguration config;
2267 config.type = PeerConnectionInterface::kRelay;
2268 EXPECT_TRUE(pc_->SetConfiguration(config));
2269 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2270}
2271
deadbeef293e9262017-01-11 12:28:30 -08002272TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2273 PeerConnectionInterface::RTCConfiguration config;
2274 config.prune_turn_ports = false;
2275 CreatePeerConnection(config, nullptr);
2276 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2277
2278 config.prune_turn_ports = true;
2279 EXPECT_TRUE(pc_->SetConfiguration(config));
2280 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2281}
2282
skvladd1f5fda2017-02-03 16:54:05 -08002283// Test that the ice check interval can be changed. This does not verify that
2284// the setting makes it all the way to P2PTransportChannel, as that would
2285// require a very complex set of mocks.
2286TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2287 PeerConnectionInterface::RTCConfiguration config;
2288 config.ice_check_min_interval = rtc::Optional<int>();
2289 CreatePeerConnection(config, nullptr);
2290 config.ice_check_min_interval = rtc::Optional<int>(100);
2291 EXPECT_TRUE(pc_->SetConfiguration(config));
2292 PeerConnectionInterface::RTCConfiguration new_config =
2293 pc_->GetConfiguration();
2294 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2295}
2296
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002297// Test that when SetConfiguration changes both the pool size and other
2298// attributes, the pooled session is created with the updated attributes.
2299TEST_F(PeerConnectionInterfaceTest,
2300 SetConfigurationCreatesPooledSessionCorrectly) {
2301 CreatePeerConnection();
2302 PeerConnectionInterface::RTCConfiguration config;
2303 config.ice_candidate_pool_size = 1;
2304 PeerConnectionInterface::IceServer server;
2305 server.uri = kStunAddressOnly;
2306 config.servers.push_back(server);
2307 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002308 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002309
2310 const cricket::FakePortAllocatorSession* session =
2311 static_cast<const cricket::FakePortAllocatorSession*>(
2312 port_allocator_->GetPooledSession());
2313 ASSERT_NE(nullptr, session);
2314 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002315}
2316
deadbeef293e9262017-01-11 12:28:30 -08002317// Test that after SetLocalDescription, changing the pool size is not allowed,
2318// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002319TEST_F(PeerConnectionInterfaceTest,
2320 CantChangePoolSizeAfterSetLocalDescription) {
2321 CreatePeerConnection();
2322 // Start by setting a size of 1.
2323 PeerConnectionInterface::RTCConfiguration config;
2324 config.ice_candidate_pool_size = 1;
2325 EXPECT_TRUE(pc_->SetConfiguration(config));
2326
2327 // Set remote offer; can still change pool size at this point.
2328 CreateOfferAsRemoteDescription();
2329 config.ice_candidate_pool_size = 2;
2330 EXPECT_TRUE(pc_->SetConfiguration(config));
2331
2332 // Set local answer; now it's too late.
2333 CreateAnswerAsLocalDescription();
2334 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002335 RTCError error;
2336 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2337 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2338}
2339
deadbeef42a42632017-03-10 15:18:00 -08002340// Test that after setting an answer, extra pooled sessions are discarded. The
2341// ICE candidate pool is only intended to be used for the first offer/answer.
2342TEST_F(PeerConnectionInterfaceTest,
2343 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2344 CreatePeerConnection();
2345
2346 // Set a larger-than-necessary size.
2347 PeerConnectionInterface::RTCConfiguration config;
2348 config.ice_candidate_pool_size = 4;
2349 EXPECT_TRUE(pc_->SetConfiguration(config));
2350
2351 // Do offer/answer.
2352 CreateOfferAsRemoteDescription();
2353 CreateAnswerAsLocalDescription();
2354
2355 // Expect no pooled sessions to be left.
2356 const cricket::PortAllocatorSession* session =
2357 port_allocator_->GetPooledSession();
2358 EXPECT_EQ(nullptr, session);
2359}
2360
2361// After Close is called, pooled candidates should be discarded so as to not
2362// waste network resources.
2363TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2364 CreatePeerConnection();
2365
2366 PeerConnectionInterface::RTCConfiguration config;
2367 config.ice_candidate_pool_size = 3;
2368 EXPECT_TRUE(pc_->SetConfiguration(config));
2369 pc_->Close();
2370
2371 // Expect no pooled sessions to be left.
2372 const cricket::PortAllocatorSession* session =
2373 port_allocator_->GetPooledSession();
2374 EXPECT_EQ(nullptr, session);
2375}
2376
deadbeef293e9262017-01-11 12:28:30 -08002377// Test that SetConfiguration returns an invalid modification error if
2378// modifying a field in the configuration that isn't allowed to be modified.
2379TEST_F(PeerConnectionInterfaceTest,
2380 SetConfigurationReturnsInvalidModificationError) {
2381 PeerConnectionInterface::RTCConfiguration config;
2382 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2383 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2384 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2385 CreatePeerConnection(config, nullptr);
2386
2387 PeerConnectionInterface::RTCConfiguration modified_config = config;
2388 modified_config.bundle_policy =
2389 PeerConnectionInterface::kBundlePolicyMaxBundle;
2390 RTCError error;
2391 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2392 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2393
2394 modified_config = config;
2395 modified_config.rtcp_mux_policy =
2396 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2397 error.set_type(RTCErrorType::NONE);
2398 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2399 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2400
2401 modified_config = config;
2402 modified_config.continual_gathering_policy =
2403 PeerConnectionInterface::GATHER_CONTINUALLY;
2404 error.set_type(RTCErrorType::NONE);
2405 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2406 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2407}
2408
2409// Test that SetConfiguration returns a range error if the candidate pool size
2410// is negative or larger than allowed by the spec.
2411TEST_F(PeerConnectionInterfaceTest,
2412 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2413 PeerConnectionInterface::RTCConfiguration config;
2414 CreatePeerConnection(config, nullptr);
2415
2416 config.ice_candidate_pool_size = -1;
2417 RTCError error;
2418 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2419 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2420
2421 config.ice_candidate_pool_size = INT_MAX;
2422 error.set_type(RTCErrorType::NONE);
2423 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2424 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2425}
2426
2427// Test that SetConfiguration returns a syntax error if parsing an ICE server
2428// URL failed.
2429TEST_F(PeerConnectionInterfaceTest,
2430 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2431 PeerConnectionInterface::RTCConfiguration config;
2432 CreatePeerConnection(config, nullptr);
2433
2434 PeerConnectionInterface::IceServer bad_server;
2435 bad_server.uri = "stunn:www.example.com";
2436 config.servers.push_back(bad_server);
2437 RTCError error;
2438 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2439 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2440}
2441
2442// Test that SetConfiguration returns an invalid parameter error if a TURN
2443// IceServer is missing a username or password.
2444TEST_F(PeerConnectionInterfaceTest,
2445 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2446 PeerConnectionInterface::RTCConfiguration config;
2447 CreatePeerConnection(config, nullptr);
2448
2449 PeerConnectionInterface::IceServer bad_server;
2450 bad_server.uri = "turn:www.example.com";
2451 // Missing password.
2452 bad_server.username = "foo";
2453 config.servers.push_back(bad_server);
2454 RTCError error;
2455 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2456 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002457}
2458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459// Test that PeerConnection::Close changes the states to closed and all remote
2460// tracks change state to ended.
2461TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2462 // Initialize a PeerConnection and negotiate local and remote session
2463 // description.
2464 InitiateCall();
2465 ASSERT_EQ(1u, pc_->local_streams()->count());
2466 ASSERT_EQ(1u, pc_->remote_streams()->count());
2467
2468 pc_->Close();
2469
2470 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2471 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2472 pc_->ice_connection_state());
2473 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2474 pc_->ice_gathering_state());
2475
2476 EXPECT_EQ(1u, pc_->local_streams()->count());
2477 EXPECT_EQ(1u, pc_->remote_streams()->count());
2478
zhihuang9763d562016-08-05 11:14:50 -07002479 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2480 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002481 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002482 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002483 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2484 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2485 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486}
2487
2488// Test that PeerConnection methods fails gracefully after
2489// PeerConnection::Close has been called.
2490TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002491 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2493 CreateOfferAsRemoteDescription();
2494 CreateAnswerAsLocalDescription();
2495
2496 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002497 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 pc_->local_streams()->at(0);
2499
2500 pc_->Close();
2501
2502 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002503 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504
2505 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002506 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002508 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509
2510 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2511
2512 EXPECT_TRUE(pc_->local_description() != NULL);
2513 EXPECT_TRUE(pc_->remote_description() != NULL);
2514
kwibergd1fe2812016-04-27 06:47:29 -07002515 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002516 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002517 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002518 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519
2520 std::string sdp;
2521 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002522 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2523 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2524 nullptr));
2525 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526
2527 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002528 std::unique_ptr<SessionDescriptionInterface> local_offer(
2529 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2530 nullptr));
2531 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532}
2533
2534// Test that GetStats can still be called after PeerConnection::Close.
2535TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2536 InitiateCall();
2537 pc_->Close();
2538 DoGetStats(NULL);
2539}
deadbeefab9b2d12015-10-14 11:33:11 -07002540
2541// NOTE: The series of tests below come from what used to be
2542// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2543// setting a remote or local description has the expected effects.
2544
2545// This test verifies that the remote MediaStreams corresponding to a received
2546// SDP string is created. In this test the two separate MediaStreams are
2547// signaled.
2548TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2549 FakeConstraints constraints;
2550 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2551 true);
2552 CreatePeerConnection(&constraints);
2553 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2554
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002555 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002556 EXPECT_TRUE(
2557 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2558 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2559 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2560
2561 // Create a session description based on another SDP with another
2562 // MediaStream.
2563 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2564
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002565 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002566 EXPECT_TRUE(
2567 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2568}
2569
2570// This test verifies that when remote tracks are added/removed from SDP, the
2571// created remote streams are updated appropriately.
2572TEST_F(PeerConnectionInterfaceTest,
2573 AddRemoveTrackFromExistingRemoteMediaStream) {
2574 FakeConstraints constraints;
2575 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2576 true);
2577 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002578 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002579 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002580 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002581 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2582 reference_collection_));
2583
2584 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002585 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002586 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002587 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002588 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2589 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002590 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002591 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2592 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002593 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002594 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2595 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002596
2597 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002598 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002599 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002600 MockTrackObserver audio_track_observer(audio_track2);
2601 MockTrackObserver video_track_observer(video_track2);
2602
2603 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2604 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002605 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002606 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2607 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002608 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002609 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002610 audio_track2->state(), kTimeout);
2611 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2612 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002613}
2614
2615// This tests that remote tracks are ended if a local session description is set
2616// that rejects the media content type.
2617TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2618 FakeConstraints constraints;
2619 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2620 true);
2621 CreatePeerConnection(&constraints);
2622 // First create and set a remote offer, then reject its video content in our
2623 // answer.
2624 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2625 ASSERT_EQ(1u, observer_.remote_streams()->count());
2626 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2627 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2628 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2629
2630 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2631 remote_stream->GetVideoTracks()[0];
2632 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2633 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2634 remote_stream->GetAudioTracks()[0];
2635 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2636
kwibergd1fe2812016-04-27 06:47:29 -07002637 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002638 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002639 cricket::ContentInfo* video_info =
2640 local_answer->description()->GetContentByName("video");
2641 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002642 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002643 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2644 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2645
2646 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002647 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002648 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002649 video_info = local_offer->description()->GetContentByName("video");
2650 ASSERT_TRUE(video_info != nullptr);
2651 video_info->rejected = true;
2652 cricket::ContentInfo* audio_info =
2653 local_offer->description()->GetContentByName("audio");
2654 ASSERT_TRUE(audio_info != nullptr);
2655 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002656 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002657 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002658 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002659 remote_audio->state(), kTimeout);
2660 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2661 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002662}
2663
2664// This tests that we won't crash if the remote track has been removed outside
2665// of PeerConnection and then PeerConnection tries to reject the track.
2666TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2667 FakeConstraints constraints;
2668 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2669 true);
2670 CreatePeerConnection(&constraints);
2671 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2672 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2673 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2674 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2675
kwibergd1fe2812016-04-27 06:47:29 -07002676 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002677 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2678 kSdpStringWithStream1, nullptr));
2679 cricket::ContentInfo* video_info =
2680 local_answer->description()->GetContentByName("video");
2681 video_info->rejected = true;
2682 cricket::ContentInfo* audio_info =
2683 local_answer->description()->GetContentByName("audio");
2684 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002685 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002686
2687 // No crash is a pass.
2688}
2689
deadbeef5e97fb52015-10-15 12:49:08 -07002690// This tests that if a recvonly remote description is set, no remote streams
2691// will be created, even if the description contains SSRCs/MSIDs.
2692// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2693TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2694 FakeConstraints constraints;
2695 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2696 true);
2697 CreatePeerConnection(&constraints);
2698
2699 std::string recvonly_offer = kSdpStringWithStream1;
2700 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2701 strlen(kRecvonly), &recvonly_offer);
2702 CreateAndSetRemoteOffer(recvonly_offer);
2703
2704 EXPECT_EQ(0u, observer_.remote_streams()->count());
2705}
2706
deadbeefab9b2d12015-10-14 11:33:11 -07002707// This tests that a default MediaStream is created if a remote session
2708// description doesn't contain any streams and no MSID support.
2709// It also tests that the default stream is updated if a video m-line is added
2710// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002711TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002712 FakeConstraints constraints;
2713 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2714 true);
2715 CreatePeerConnection(&constraints);
2716 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2717
2718 ASSERT_EQ(1u, observer_.remote_streams()->count());
2719 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2720
2721 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2722 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2723 EXPECT_EQ("default", remote_stream->label());
2724
2725 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2726 ASSERT_EQ(1u, observer_.remote_streams()->count());
2727 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2728 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002729 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2730 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002731 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2732 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002733 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2734 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002735}
2736
2737// This tests that a default MediaStream is created if a remote session
2738// description doesn't contain any streams and media direction is send only.
2739TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002740 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002741 FakeConstraints constraints;
2742 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2743 true);
2744 CreatePeerConnection(&constraints);
2745 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2746
2747 ASSERT_EQ(1u, observer_.remote_streams()->count());
2748 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2749
2750 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2751 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2752 EXPECT_EQ("default", remote_stream->label());
2753}
2754
2755// This tests that it won't crash when PeerConnection tries to remove
2756// a remote track that as already been removed from the MediaStream.
2757TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2758 FakeConstraints constraints;
2759 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2760 true);
2761 CreatePeerConnection(&constraints);
2762 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2763 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2764 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2765 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2766
2767 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2768
2769 // No crash is a pass.
2770}
2771
2772// This tests that a default MediaStream is created if the remote session
2773// description doesn't contain any streams and don't contain an indication if
2774// MSID is supported.
2775TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002776 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002777 FakeConstraints constraints;
2778 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2779 true);
2780 CreatePeerConnection(&constraints);
2781 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2782
2783 ASSERT_EQ(1u, observer_.remote_streams()->count());
2784 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2785 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2786 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2787}
2788
2789// This tests that a default MediaStream is not created if the remote session
2790// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002791TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002792 FakeConstraints constraints;
2793 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2794 true);
2795 CreatePeerConnection(&constraints);
2796 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2797 EXPECT_EQ(0u, observer_.remote_streams()->count());
2798}
2799
deadbeefbda7e0b2015-12-08 17:13:40 -08002800// This tests that when setting a new description, the old default tracks are
2801// not destroyed and recreated.
2802// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002803TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002804 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002805 FakeConstraints constraints;
2806 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2807 true);
2808 CreatePeerConnection(&constraints);
2809 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2810
2811 ASSERT_EQ(1u, observer_.remote_streams()->count());
2812 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2813 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2814
2815 // Set the track to "disabled", then set a new description and ensure the
2816 // track is still disabled, which ensures it hasn't been recreated.
2817 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2818 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2819 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2820 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2821}
2822
deadbeefab9b2d12015-10-14 11:33:11 -07002823// This tests that a default MediaStream is not created if a remote session
2824// description is updated to not have any MediaStreams.
2825TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2826 FakeConstraints constraints;
2827 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2828 true);
2829 CreatePeerConnection(&constraints);
2830 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002831 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002832 EXPECT_TRUE(
2833 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2834
2835 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2836 EXPECT_EQ(0u, observer_.remote_streams()->count());
2837}
2838
2839// This tests that an RtpSender is created when the local description is set
2840// after adding a local stream.
2841// TODO(deadbeef): This test and the one below it need to be updated when
2842// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002843TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002844 FakeConstraints constraints;
2845 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2846 true);
2847 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002848
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002849 // Create an offer with 1 stream with 2 tracks of each type.
2850 rtc::scoped_refptr<StreamCollection> stream_collection =
2851 CreateStreamCollection(1, 2);
2852 pc_->AddStream(stream_collection->at(0));
2853 std::unique_ptr<SessionDescriptionInterface> offer;
2854 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002855 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002856
deadbeefab9b2d12015-10-14 11:33:11 -07002857 auto senders = pc_->GetSenders();
2858 EXPECT_EQ(4u, senders.size());
2859 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2860 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2861 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2862 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2863
2864 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002865 pc_->RemoveStream(stream_collection->at(0));
2866 stream_collection = CreateStreamCollection(1, 1);
2867 pc_->AddStream(stream_collection->at(0));
2868 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002869 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002870
deadbeefab9b2d12015-10-14 11:33:11 -07002871 senders = pc_->GetSenders();
2872 EXPECT_EQ(2u, senders.size());
2873 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2874 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2875 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2876 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2877}
2878
2879// This tests that an RtpSender is created when the local description is set
2880// before adding a local stream.
2881TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002882 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002883 FakeConstraints constraints;
2884 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2885 true);
2886 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002887
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002888 rtc::scoped_refptr<StreamCollection> stream_collection =
2889 CreateStreamCollection(1, 2);
2890 // Add a stream to create the offer, but remove it afterwards.
2891 pc_->AddStream(stream_collection->at(0));
2892 std::unique_ptr<SessionDescriptionInterface> offer;
2893 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2894 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002895
Steve Antondb45ca82017-09-11 18:27:34 -07002896 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002897 auto senders = pc_->GetSenders();
2898 EXPECT_EQ(0u, senders.size());
2899
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002900 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002901 senders = pc_->GetSenders();
2902 EXPECT_EQ(4u, senders.size());
2903 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2904 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2905 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2906 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2907}
2908
2909// This tests that the expected behavior occurs if the SSRC on a local track is
2910// changed when SetLocalDescription is called.
2911TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002912 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002913 FakeConstraints constraints;
2914 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2915 true);
2916 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002917
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002918 rtc::scoped_refptr<StreamCollection> stream_collection =
2919 CreateStreamCollection(2, 1);
2920 pc_->AddStream(stream_collection->at(0));
2921 std::unique_ptr<SessionDescriptionInterface> offer;
2922 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2923 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002924 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2925 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002926 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2927 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002928 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002929
deadbeefab9b2d12015-10-14 11:33:11 -07002930 auto senders = pc_->GetSenders();
2931 EXPECT_EQ(2u, senders.size());
2932 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2933 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2934
2935 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002936 cricket::MediaContentDescription* desc =
2937 cricket::GetFirstAudioContentDescription(modified_offer->description());
2938 ASSERT_TRUE(desc != NULL);
2939 for (StreamParams& stream : desc->mutable_streams()) {
2940 for (unsigned int& ssrc : stream.ssrcs) {
2941 ++ssrc;
2942 }
2943 }
deadbeefab9b2d12015-10-14 11:33:11 -07002944
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002945 desc =
2946 cricket::GetFirstVideoContentDescription(modified_offer->description());
2947 ASSERT_TRUE(desc != NULL);
2948 for (StreamParams& stream : desc->mutable_streams()) {
2949 for (unsigned int& ssrc : stream.ssrcs) {
2950 ++ssrc;
2951 }
2952 }
2953
Steve Antondb45ca82017-09-11 18:27:34 -07002954 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002955 senders = pc_->GetSenders();
2956 EXPECT_EQ(2u, senders.size());
2957 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2958 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2959 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2960 // changed.
2961}
2962
2963// This tests that the expected behavior occurs if a new session description is
2964// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002965TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002966 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002967 FakeConstraints constraints;
2968 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2969 true);
2970 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002971
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002972 rtc::scoped_refptr<StreamCollection> stream_collection =
2973 CreateStreamCollection(2, 1);
2974 pc_->AddStream(stream_collection->at(0));
2975 std::unique_ptr<SessionDescriptionInterface> offer;
2976 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002977 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002978
deadbeefab9b2d12015-10-14 11:33:11 -07002979 auto senders = pc_->GetSenders();
2980 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002981 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2982 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002983
2984 // Add a new MediaStream but with the same tracks as in the first stream.
2985 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2986 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002987 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2988 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002989 pc_->AddStream(stream_1);
2990
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002991 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002992 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002993
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002994 auto new_senders = pc_->GetSenders();
2995 // Should be the same senders as before, but with updated stream id.
2996 // Note that this behavior is subject to change in the future.
2997 // We may decide the PC should ignore existing tracks in AddStream.
2998 EXPECT_EQ(senders, new_senders);
2999 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3000 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003001}
3002
zhihuang81c3a032016-11-17 12:06:24 -08003003// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3004TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3005 FakeConstraints constraints;
3006 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3007 true);
3008 CreatePeerConnection(&constraints);
3009 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3010 EXPECT_EQ(observer_.num_added_tracks_, 1);
3011 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3012
3013 // Create and set the updated remote SDP.
3014 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3015 EXPECT_EQ(observer_.num_added_tracks_, 2);
3016 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3017}
3018
deadbeefd1a38b52016-12-10 13:15:33 -08003019// Test that when SetConfiguration is called and the configuration is
3020// changing, the next offer causes an ICE restart.
3021TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3022 PeerConnectionInterface::RTCConfiguration config;
3023 config.type = PeerConnectionInterface::kRelay;
3024 // Need to pass default constraints to prevent disabling of DTLS...
3025 FakeConstraints default_constraints;
3026 CreatePeerConnection(config, &default_constraints);
3027 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3028
3029 // Do initial offer/answer so there's something to restart.
3030 CreateOfferAsLocalDescription();
3031 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3032
3033 // Grab the ufrags.
3034 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3035
3036 // Change ICE policy, which should trigger an ICE restart on the next offer.
3037 config.type = PeerConnectionInterface::kAll;
3038 EXPECT_TRUE(pc_->SetConfiguration(config));
3039 CreateOfferAsLocalDescription();
3040
3041 // Grab the new ufrags.
3042 std::vector<std::string> subsequent_ufrags =
3043 GetUfrags(pc_->local_description());
3044
3045 // Sanity check.
3046 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3047 // Check that each ufrag is different.
3048 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3049 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3050 }
3051}
3052
3053// Test that when SetConfiguration is called and the configuration *isn't*
3054// changing, the next offer does *not* cause an ICE restart.
3055TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3056 PeerConnectionInterface::RTCConfiguration config;
3057 config.type = PeerConnectionInterface::kRelay;
3058 // Need to pass default constraints to prevent disabling of DTLS...
3059 FakeConstraints default_constraints;
3060 CreatePeerConnection(config, &default_constraints);
3061 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3062
3063 // Do initial offer/answer so there's something to restart.
3064 CreateOfferAsLocalDescription();
3065 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3066
3067 // Grab the ufrags.
3068 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3069
3070 // Call SetConfiguration with a config identical to what the PC was
3071 // constructed with.
3072 EXPECT_TRUE(pc_->SetConfiguration(config));
3073 CreateOfferAsLocalDescription();
3074
3075 // Grab the new ufrags.
3076 std::vector<std::string> subsequent_ufrags =
3077 GetUfrags(pc_->local_description());
3078
3079 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3080}
3081
3082// Test for a weird corner case scenario:
3083// 1. Audio/video session established.
3084// 2. SetConfiguration changes ICE config; ICE restart needed.
3085// 3. ICE restart initiated by remote peer, but only for one m= section.
3086// 4. Next createOffer should initiate an ICE restart, but only for the other
3087// m= section; it would be pointless to do an ICE restart for the m= section
3088// that was already restarted.
3089TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3090 PeerConnectionInterface::RTCConfiguration config;
3091 config.type = PeerConnectionInterface::kRelay;
3092 // Need to pass default constraints to prevent disabling of DTLS...
3093 FakeConstraints default_constraints;
3094 CreatePeerConnection(config, &default_constraints);
3095 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3096
3097 // Do initial offer/answer so there's something to restart.
3098 CreateOfferAsLocalDescription();
3099 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3100
3101 // Change ICE policy, which should set the "needs-ice-restart" flag.
3102 config.type = PeerConnectionInterface::kAll;
3103 EXPECT_TRUE(pc_->SetConfiguration(config));
3104
3105 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003106 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3107 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3108 kSdpStringWithStream1, nullptr));
3109 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003110 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3111 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003112 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003113 CreateAnswerAsLocalDescription();
3114
3115 // Grab the ufrags.
3116 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3117 ASSERT_EQ(2, initial_ufrags.size());
3118
3119 // Create offer and grab the new ufrags.
3120 CreateOfferAsLocalDescription();
3121 std::vector<std::string> subsequent_ufrags =
3122 GetUfrags(pc_->local_description());
3123 ASSERT_EQ(2, subsequent_ufrags.size());
3124
3125 // Ensure that only the ufrag for the second m= section changed.
3126 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3127 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3128}
3129
deadbeeffe4a8a42016-12-20 17:56:17 -08003130// Tests that the methods to return current/pending descriptions work as
3131// expected at different points in the offer/answer exchange. This test does
3132// one offer/answer exchange as the offerer, then another as the answerer.
3133TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3134 // This disables DTLS so we can apply an answer to ourselves.
3135 CreatePeerConnection();
3136
3137 // Create initial local offer and get SDP (which will also be used as
3138 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003139 std::unique_ptr<SessionDescriptionInterface> local_offer;
3140 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003141 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003142 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003143
3144 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003145 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3146 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3147 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003148 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3149 EXPECT_EQ(nullptr, pc_->current_local_description());
3150 EXPECT_EQ(nullptr, pc_->current_remote_description());
3151
3152 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003153 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003154 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003155 sdp, nullptr));
3156 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3157 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3158 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3159 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003160 EXPECT_EQ(nullptr, pc_->current_local_description());
3161 EXPECT_EQ(nullptr, pc_->current_remote_description());
3162
3163 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003164 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3165 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3166 sdp, nullptr));
3167 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3168 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003169 EXPECT_EQ(nullptr, pc_->pending_local_description());
3170 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003171 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3172 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003173
3174 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003175 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3176 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3177 nullptr));
3178 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3179 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3180 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003181 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003182 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3183 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003184
3185 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003186 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003187 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003188 sdp, nullptr));
3189 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3190 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3191 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3192 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3193 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3194 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003195
3196 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003197 std::unique_ptr<SessionDescriptionInterface> local_answer(
3198 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3199 sdp, nullptr));
3200 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3201 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003202 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3203 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003204 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3205 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003206}
3207
zhihuang77985012017-02-07 15:45:16 -08003208// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3209// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003210// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003211TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003212 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003213 CreatePeerConnection();
3214 // The RtcEventLog will be reset when the PeerConnection is closed.
3215 pc_->Close();
3216
Elad Alon9e6565b2017-10-11 16:04:13 +02003217 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3218 std::string filename = webrtc::test::OutputPath() +
3219 test_info->test_case_name() + test_info->name();
3220 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3221
3222 constexpr int64_t max_size_bytes = 1024;
3223
zhihuang77985012017-02-07 15:45:16 -08003224 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3225 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003226
3227 // Cleanup.
3228 rtc::ClosePlatformFile(file);
3229 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003230}
3231
Elad Alon99c3fe52017-10-13 16:29:40 +02003232// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3233// after the PeerConnection is closed.
3234// This version tests the StartRtcEventLog version that receives an object
3235// of type |RtcEventLogOutput|.
3236TEST_F(PeerConnectionInterfaceTest,
3237 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3238 CreatePeerConnection();
3239 // The RtcEventLog will be reset when the PeerConnection is closed.
3240 pc_->Close();
3241
3242 rtc::PlatformFile file = 0;
3243 int64_t max_size_bytes = 1024;
3244 EXPECT_FALSE(pc_->StartRtcEventLog(
3245 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes)));
3246 pc_->StopRtcEventLog();
3247}
3248
deadbeef30952b42017-04-21 02:41:29 -07003249// Test that generated offers/answers include "ice-option:trickle".
3250TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3251 CreatePeerConnection();
3252
3253 // First, create an offer with audio/video.
3254 FakeConstraints constraints;
3255 constraints.SetMandatoryReceiveAudio(true);
3256 constraints.SetMandatoryReceiveVideo(true);
3257 std::unique_ptr<SessionDescriptionInterface> offer;
3258 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3259 cricket::SessionDescription* desc = offer->description();
3260 ASSERT_EQ(2u, desc->transport_infos().size());
3261 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3262 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3263
3264 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003265 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003266 std::unique_ptr<SessionDescriptionInterface> answer;
3267 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3268 desc = answer->description();
3269 ASSERT_EQ(2u, desc->transport_infos().size());
3270 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3271 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3272}
3273
deadbeef1dcb1642017-03-29 21:08:16 -07003274// Test that ICE renomination isn't offered if it's not enabled in the PC's
3275// RTCConfiguration.
3276TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3277 PeerConnectionInterface::RTCConfiguration config;
3278 config.enable_ice_renomination = false;
3279 CreatePeerConnection(config, nullptr);
3280 AddVoiceStream("foo");
3281
3282 std::unique_ptr<SessionDescriptionInterface> offer;
3283 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3284 cricket::SessionDescription* desc = offer->description();
3285 EXPECT_EQ(1u, desc->transport_infos().size());
3286 EXPECT_FALSE(
3287 desc->transport_infos()[0].description.GetIceParameters().renomination);
3288}
3289
3290// Test that the ICE renomination option is present in generated offers/answers
3291// if it's enabled in the PC's RTCConfiguration.
3292TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3293 PeerConnectionInterface::RTCConfiguration config;
3294 config.enable_ice_renomination = true;
3295 CreatePeerConnection(config, nullptr);
3296 AddVoiceStream("foo");
3297
3298 std::unique_ptr<SessionDescriptionInterface> offer;
3299 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3300 cricket::SessionDescription* desc = offer->description();
3301 EXPECT_EQ(1u, desc->transport_infos().size());
3302 EXPECT_TRUE(
3303 desc->transport_infos()[0].description.GetIceParameters().renomination);
3304
3305 // Set the offer as a remote description, then create an answer and ensure it
3306 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003307 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003308 std::unique_ptr<SessionDescriptionInterface> answer;
3309 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3310 desc = answer->description();
3311 EXPECT_EQ(1u, desc->transport_infos().size());
3312 EXPECT_TRUE(
3313 desc->transport_infos()[0].description.GetIceParameters().renomination);
3314}
3315
3316// Test that if CreateOffer is called with the deprecated "offer to receive
3317// audio/video" constraints, they're processed and result in an offer with
3318// audio/video sections just as if RTCOfferAnswerOptions had been used.
3319TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3320 CreatePeerConnection();
3321
3322 FakeConstraints constraints;
3323 constraints.SetMandatoryReceiveAudio(true);
3324 constraints.SetMandatoryReceiveVideo(true);
3325 std::unique_ptr<SessionDescriptionInterface> offer;
3326 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3327
3328 cricket::SessionDescription* desc = offer->description();
3329 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3330 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3331 ASSERT_NE(nullptr, audio);
3332 ASSERT_NE(nullptr, video);
3333 EXPECT_FALSE(audio->rejected);
3334 EXPECT_FALSE(video->rejected);
3335}
3336
3337// Test that if CreateAnswer is called with the deprecated "offer to receive
3338// audio/video" constraints, they're processed and can be used to reject an
3339// offered m= section just as can be done with RTCOfferAnswerOptions;
3340TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3341 CreatePeerConnection();
3342
3343 // First, create an offer with audio/video and apply it as a remote
3344 // description.
3345 FakeConstraints constraints;
3346 constraints.SetMandatoryReceiveAudio(true);
3347 constraints.SetMandatoryReceiveVideo(true);
3348 std::unique_ptr<SessionDescriptionInterface> offer;
3349 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003350 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003351
3352 // Now create answer that rejects audio/video.
3353 constraints.SetMandatoryReceiveAudio(false);
3354 constraints.SetMandatoryReceiveVideo(false);
3355 std::unique_ptr<SessionDescriptionInterface> answer;
3356 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3357
3358 cricket::SessionDescription* desc = answer->description();
3359 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3360 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3361 ASSERT_NE(nullptr, audio);
3362 ASSERT_NE(nullptr, video);
3363 EXPECT_TRUE(audio->rejected);
3364 EXPECT_TRUE(video->rejected);
3365}
3366
3367#ifdef HAVE_SCTP
3368#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3369 DataChannelOnlyOfferWithMaxBundlePolicy
3370#else
3371#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3372 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3373#endif
3374
3375// Test that negotiation can succeed with a data channel only, and with the max
3376// bundle policy. Previously there was a bug that prevented this.
3377TEST_F(PeerConnectionInterfaceTest,
3378 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3379 PeerConnectionInterface::RTCConfiguration config;
3380 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3381 CreatePeerConnection(config, nullptr);
3382
3383 // First, create an offer with only a data channel and apply it as a remote
3384 // description.
3385 pc_->CreateDataChannel("test", nullptr);
3386 std::unique_ptr<SessionDescriptionInterface> offer;
3387 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003388 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003389
3390 // Create and set answer as well.
3391 std::unique_ptr<SessionDescriptionInterface> answer;
3392 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003393 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003394}
3395
zstein4b979802017-06-02 14:37:37 -07003396TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3397 CreatePeerConnection();
3398 PeerConnectionInterface::BitrateParameters bitrate;
3399 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3400 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3401}
3402
3403TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3404 CreatePeerConnection();
3405 PeerConnectionInterface::BitrateParameters bitrate;
3406 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3407 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3408}
3409
3410TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3411 CreatePeerConnection();
3412 PeerConnectionInterface::BitrateParameters bitrate;
3413 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3414 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3415 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3416}
3417
3418TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3419 CreatePeerConnection();
3420 PeerConnectionInterface::BitrateParameters bitrate;
3421 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3422 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3423}
3424
3425TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3426 CreatePeerConnection();
3427 PeerConnectionInterface::BitrateParameters bitrate;
3428 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3429 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3430 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3431}
3432
3433TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3434 CreatePeerConnection();
3435 PeerConnectionInterface::BitrateParameters bitrate;
3436 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3437 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3438 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3439}
3440
3441TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3442 CreatePeerConnection();
3443 PeerConnectionInterface::BitrateParameters bitrate;
3444 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3445 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3446}
3447
Steve Anton038834f2017-07-14 15:59:59 -07003448// ice_regather_interval_range requires WebRTC to be configured for continual
3449// gathering already.
3450TEST_F(PeerConnectionInterfaceTest,
3451 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3452 PeerConnectionInterface::RTCConfiguration config;
3453 config.ice_regather_interval_range.emplace(1000, 2000);
3454 config.continual_gathering_policy =
3455 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3456 CreatePeerConnectionExpectFail(config);
3457}
3458
3459// Ensures that there is no error when ice_regather_interval_range is set with
3460// continual gathering enabled.
3461TEST_F(PeerConnectionInterfaceTest,
3462 SetIceRegatherIntervalRangeWithContinualGathering) {
3463 PeerConnectionInterface::RTCConfiguration config;
3464 config.ice_regather_interval_range.emplace(1000, 2000);
3465 config.continual_gathering_policy =
3466 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3467 CreatePeerConnection(config, nullptr);
3468}
3469
zstein4b979802017-06-02 14:37:37 -07003470// The current bitrate from Call's BitrateConfigMask is currently clamped by
3471// Call's BitrateConfig, which comes from the SDP or a default value. This test
3472// checks that a call to SetBitrate with a current bitrate that will be clamped
3473// succeeds.
3474TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3475 CreatePeerConnection();
3476 PeerConnectionInterface::BitrateParameters bitrate;
3477 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3478 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3479}
3480
zhihuang1c378ed2017-08-17 14:10:50 -07003481// The following tests verify that the offer can be created correctly.
3482TEST_F(PeerConnectionInterfaceTest,
3483 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3484 RTCOfferAnswerOptions rtc_options;
3485
3486 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3487 // than kMaxOfferToReceiveMedia should be treated as invalid.
3488 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3489 CreatePeerConnection();
3490 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3491
3492 rtc_options.offer_to_receive_audio =
3493 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3494 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3495}
3496
3497TEST_F(PeerConnectionInterfaceTest,
3498 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3499 RTCOfferAnswerOptions rtc_options;
3500
3501 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3502 // than kMaxOfferToReceiveMedia should be treated as invalid.
3503 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3504 CreatePeerConnection();
3505 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3506
3507 rtc_options.offer_to_receive_video =
3508 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3509 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3510}
3511
3512// Test that the audio and video content will be added to an offer if both
3513// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3514TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3515 RTCOfferAnswerOptions rtc_options;
3516 rtc_options.offer_to_receive_audio = 1;
3517 rtc_options.offer_to_receive_video = 1;
3518
3519 std::unique_ptr<SessionDescriptionInterface> offer;
3520 CreatePeerConnection();
3521 offer = CreateOfferWithOptions(rtc_options);
3522 ASSERT_TRUE(offer);
3523 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3524 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3525}
3526
3527// Test that only audio content will be added to the offer if only
3528// |offer_to_receive_audio| options is 1.
3529TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3530 RTCOfferAnswerOptions rtc_options;
3531 rtc_options.offer_to_receive_audio = 1;
3532 rtc_options.offer_to_receive_video = 0;
3533
3534 std::unique_ptr<SessionDescriptionInterface> offer;
3535 CreatePeerConnection();
3536 offer = CreateOfferWithOptions(rtc_options);
3537 ASSERT_TRUE(offer);
3538 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3539 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3540}
3541
3542// Test that only video content will be added if only |offer_to_receive_video|
3543// options is 1.
3544TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3545 RTCOfferAnswerOptions rtc_options;
3546 rtc_options.offer_to_receive_audio = 0;
3547 rtc_options.offer_to_receive_video = 1;
3548
3549 std::unique_ptr<SessionDescriptionInterface> offer;
3550 CreatePeerConnection();
3551 offer = CreateOfferWithOptions(rtc_options);
3552 ASSERT_TRUE(offer);
3553 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3554 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3555}
3556
3557// Test that if |voice_activity_detection| is false, no CN codec is added to the
3558// offer.
3559TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3560 RTCOfferAnswerOptions rtc_options;
3561 rtc_options.offer_to_receive_audio = 1;
3562 rtc_options.offer_to_receive_video = 0;
3563
3564 std::unique_ptr<SessionDescriptionInterface> offer;
3565 CreatePeerConnection();
3566 offer = CreateOfferWithOptions(rtc_options);
3567 ASSERT_TRUE(offer);
3568 const cricket::ContentInfo* audio_content =
3569 offer->description()->GetContentByName(cricket::CN_AUDIO);
3570 ASSERT_TRUE(audio_content);
3571 // |voice_activity_detection| is true by default.
3572 EXPECT_TRUE(HasCNCodecs(audio_content));
3573
3574 rtc_options.voice_activity_detection = false;
3575 CreatePeerConnection();
3576 offer = CreateOfferWithOptions(rtc_options);
3577 ASSERT_TRUE(offer);
3578 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3579 ASSERT_TRUE(audio_content);
3580 EXPECT_FALSE(HasCNCodecs(audio_content));
3581}
3582
3583// Test that no media content will be added to the offer if using default
3584// RTCOfferAnswerOptions.
3585TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3586 RTCOfferAnswerOptions rtc_options;
3587
3588 std::unique_ptr<SessionDescriptionInterface> offer;
3589 CreatePeerConnection();
3590 offer = CreateOfferWithOptions(rtc_options);
3591 ASSERT_TRUE(offer);
3592 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3593 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3594}
3595
3596// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3597// ufrag/pwd will be the same in the new offer.
3598TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3599 RTCOfferAnswerOptions rtc_options;
3600 rtc_options.ice_restart = false;
3601 rtc_options.offer_to_receive_audio = 1;
3602
3603 std::unique_ptr<SessionDescriptionInterface> offer;
3604 CreatePeerConnection();
3605 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3606 auto ufrag1 = offer->description()
3607 ->GetTransportInfoByName(cricket::CN_AUDIO)
3608 ->description.ice_ufrag;
3609 auto pwd1 = offer->description()
3610 ->GetTransportInfoByName(cricket::CN_AUDIO)
3611 ->description.ice_pwd;
3612
3613 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3614 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3615 auto ufrag2 = offer->description()
3616 ->GetTransportInfoByName(cricket::CN_AUDIO)
3617 ->description.ice_ufrag;
3618 auto pwd2 = offer->description()
3619 ->GetTransportInfoByName(cricket::CN_AUDIO)
3620 ->description.ice_pwd;
3621
3622 // |ice_restart| is true, the ufrag/pwd should change.
3623 rtc_options.ice_restart = true;
3624 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3625 auto ufrag3 = offer->description()
3626 ->GetTransportInfoByName(cricket::CN_AUDIO)
3627 ->description.ice_ufrag;
3628 auto pwd3 = offer->description()
3629 ->GetTransportInfoByName(cricket::CN_AUDIO)
3630 ->description.ice_pwd;
3631
3632 EXPECT_EQ(ufrag1, ufrag2);
3633 EXPECT_EQ(pwd1, pwd2);
3634 EXPECT_NE(ufrag2, ufrag3);
3635 EXPECT_NE(pwd2, pwd3);
3636}
3637
3638// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3639// offer; if it is false, there won't be any bundle group in the offer.
3640TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3641 RTCOfferAnswerOptions rtc_options;
3642 rtc_options.offer_to_receive_audio = 1;
3643 rtc_options.offer_to_receive_video = 1;
3644
3645 std::unique_ptr<SessionDescriptionInterface> offer;
3646 CreatePeerConnection();
3647
3648 rtc_options.use_rtp_mux = true;
3649 offer = CreateOfferWithOptions(rtc_options);
3650 ASSERT_TRUE(offer);
3651 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3652 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3653 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3654
3655 rtc_options.use_rtp_mux = false;
3656 offer = CreateOfferWithOptions(rtc_options);
3657 ASSERT_TRUE(offer);
3658 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3659 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3660 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3661}
3662
3663// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3664// called for the answer constraints, but an audio and a video section were
3665// offered, there will still be an audio and a video section in the answer.
3666TEST_F(PeerConnectionInterfaceTest,
3667 RejectAudioAndVideoInAnswerWithConstraints) {
3668 // Offer both audio and video.
3669 RTCOfferAnswerOptions rtc_offer_options;
3670 rtc_offer_options.offer_to_receive_audio = 1;
3671 rtc_offer_options.offer_to_receive_video = 1;
3672
3673 CreatePeerConnection();
3674 std::unique_ptr<SessionDescriptionInterface> offer;
3675 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3676 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3677 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3678
3679 // Since an offer has been created with both audio and video,
3680 // Answers will contain the media types that exist in the offer regardless of
3681 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3682 FakeConstraints answer_c;
3683 // Reject both audio and video.
3684 answer_c.SetMandatoryReceiveAudio(false);
3685 answer_c.SetMandatoryReceiveVideo(false);
3686
3687 std::unique_ptr<SessionDescriptionInterface> answer;
3688 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3689 const cricket::ContentInfo* audio_content =
3690 GetFirstAudioContent(answer->description());
3691 const cricket::ContentInfo* video_content =
3692 GetFirstVideoContent(answer->description());
3693 ASSERT_NE(nullptr, audio_content);
3694 ASSERT_NE(nullptr, video_content);
3695 EXPECT_TRUE(audio_content->rejected);
3696 EXPECT_TRUE(video_content->rejected);
3697}
3698
zhihuang141aacb2017-08-29 13:23:53 -07003699// This test ensures OnRenegotiationNeeded is called when we add track with
3700// MediaStream -> AddTrack in the same way it is called when we add track with
3701// PeerConnection -> AddTrack.
3702// The test can be removed once addStream is rewritten in terms of addTrack
3703// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3704TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3705 CreatePeerConnectionWithoutDtls();
3706 rtc::scoped_refptr<MediaStreamInterface> stream(
3707 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3708 pc_->AddStream(stream);
3709 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3710 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3711 rtc::scoped_refptr<VideoTrackInterface> video_track(
3712 pc_factory_->CreateVideoTrack(
3713 "video_track", pc_factory_->CreateVideoSource(
3714 std::unique_ptr<cricket::VideoCapturer>(
3715 new cricket::FakeVideoCapturer()))));
3716 stream->AddTrack(audio_track);
3717 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3718 observer_.renegotiation_needed_ = false;
3719
3720 stream->AddTrack(video_track);
3721 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3722 observer_.renegotiation_needed_ = false;
3723
3724 stream->RemoveTrack(audio_track);
3725 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3726 observer_.renegotiation_needed_ = false;
3727
3728 stream->RemoveTrack(video_track);
3729 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3730 observer_.renegotiation_needed_ = false;
3731}
3732
3733// Tests that creating answer would fail gracefully without being crashed if the
3734// remote description is unset.
3735TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3736 CreatePeerConnection();
3737 // Creating answer fails because the remote description is unset.
3738 std::unique_ptr<SessionDescriptionInterface> answer;
3739 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3740
3741 // Createing answer succeeds when the remote description is set.
3742 CreateOfferAsRemoteDescription();
3743 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3744}
3745
Tommi589ae452017-10-15 21:20:46 +00003746// Test that an error is returned if a description is applied that doesn't
Zhi Huang2a5e4262017-09-14 01:15:03 -07003747// respect the order of existing media sections.
3748TEST_F(PeerConnectionInterfaceTest,
3749 MediaSectionOrderEnforcedForSubsequentOffers) {
3750 CreatePeerConnection();
3751 FakeConstraints constraints;
3752 constraints.SetMandatoryReceiveAudio(true);
3753 constraints.SetMandatoryReceiveVideo(true);
3754 std::unique_ptr<SessionDescriptionInterface> offer;
3755 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3756 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3757
3758 std::unique_ptr<SessionDescriptionInterface> answer;
3759 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3760 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3761
3762 // A remote offer with different m=line order should be rejected.
3763 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3764 std::reverse(offer->description()->contents().begin(),
3765 offer->description()->contents().end());
3766 std::reverse(offer->description()->transport_infos().begin(),
3767 offer->description()->transport_infos().end());
3768 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3769
3770 // A subsequent local offer with different m=line order should be rejected.
3771 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3772 std::reverse(offer->description()->contents().begin(),
3773 offer->description()->contents().end());
3774 std::reverse(offer->description()->transport_infos().begin(),
3775 offer->description()->transport_infos().end());
3776 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3777}
3778
nisse51542be2016-02-12 02:27:06 -08003779class PeerConnectionMediaConfigTest : public testing::Test {
3780 protected:
3781 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003782 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003783 pcf_->Initialize();
3784 }
nisseeaabdf62017-05-05 02:23:02 -07003785 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003786 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003787 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003788 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3789 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003790 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003791 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003792 }
3793
zhihuang9763d562016-08-05 11:14:50 -07003794 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003795 MockPeerConnectionObserver observer_;
3796};
3797
3798// This test verifies the default behaviour with no constraints and a
3799// default RTCConfiguration.
3800TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3801 PeerConnectionInterface::RTCConfiguration config;
3802 FakeConstraints constraints;
3803
3804 const cricket::MediaConfig& media_config =
3805 TestCreatePeerConnection(config, &constraints);
3806
3807 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003808 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3809 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3810 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003811}
3812
3813// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003814// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003815TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3816 PeerConnectionInterface::RTCConfiguration config;
3817 FakeConstraints constraints;
3818
3819 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3820 const cricket::MediaConfig& media_config =
3821 TestCreatePeerConnection(config, &constraints);
3822
3823 EXPECT_TRUE(media_config.enable_dscp);
3824}
3825
3826// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003827// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003828TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3829 PeerConnectionInterface::RTCConfiguration config;
3830 FakeConstraints constraints;
3831
3832 constraints.AddOptional(
3833 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3834 const cricket::MediaConfig media_config =
3835 TestCreatePeerConnection(config, &constraints);
3836
nisse0db023a2016-03-01 04:29:59 -08003837 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003838}
3839
3840// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003841// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003842TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3843 PeerConnectionInterface::RTCConfiguration config;
3844 FakeConstraints constraints;
3845
Niels Möller71bdda02016-03-31 12:59:59 +02003846 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003847 const cricket::MediaConfig& media_config =
3848 TestCreatePeerConnection(config, &constraints);
3849
nisse0db023a2016-03-01 04:29:59 -08003850 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3851}
3852
3853// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003854// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003855TEST_F(PeerConnectionMediaConfigTest,
3856 TestSuspendBelowMinBitrateConstraintTrue) {
3857 PeerConnectionInterface::RTCConfiguration config;
3858 FakeConstraints constraints;
3859
3860 constraints.AddOptional(
3861 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3862 true);
3863 const cricket::MediaConfig media_config =
3864 TestCreatePeerConnection(config, &constraints);
3865
3866 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003867}
3868
deadbeef293e9262017-01-11 12:28:30 -08003869// Tests a few random fields being different.
3870TEST(RTCConfigurationTest, ComparisonOperators) {
3871 PeerConnectionInterface::RTCConfiguration a;
3872 PeerConnectionInterface::RTCConfiguration b;
3873 EXPECT_EQ(a, b);
3874
3875 PeerConnectionInterface::RTCConfiguration c;
3876 c.servers.push_back(PeerConnectionInterface::IceServer());
3877 EXPECT_NE(a, c);
3878
3879 PeerConnectionInterface::RTCConfiguration d;
3880 d.type = PeerConnectionInterface::kRelay;
3881 EXPECT_NE(a, d);
3882
3883 PeerConnectionInterface::RTCConfiguration e;
3884 e.audio_jitter_buffer_max_packets = 5;
3885 EXPECT_NE(a, e);
3886
3887 PeerConnectionInterface::RTCConfiguration f;
3888 f.ice_connection_receiving_timeout = 1337;
3889 EXPECT_NE(a, f);
3890
3891 PeerConnectionInterface::RTCConfiguration g;
3892 g.disable_ipv6 = true;
3893 EXPECT_NE(a, g);
3894
3895 PeerConnectionInterface::RTCConfiguration h(
3896 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3897 EXPECT_NE(a, h);
3898}