blob: abe5666311e8866be644cf3628976624623ca656 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Zhi Huang2a5e4262017-09-14 01:15:03 -070011#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070012#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080013#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "api/rtpreceiverinterface.h"
23#include "api/rtpsenderinterface.h"
24#include "api/test/fakeconstraints.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020025#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakevideocapturer.h"
27#include "media/engine/webrtcmediaengine.h"
28#include "media/sctp/sctptransportinternal.h"
29#include "modules/audio_processing/include/audio_processing.h"
30#include "p2p/base/fakeportallocator.h"
31#include "pc/audiotrack.h"
32#include "pc/mediasession.h"
33#include "pc/mediastream.h"
34#include "pc/peerconnection.h"
35#include "pc/streamcollection.h"
36#include "pc/test/fakeaudiocapturemodule.h"
37#include "pc/test/fakertccertificategenerator.h"
38#include "pc/test/fakevideotracksource.h"
39#include "pc/test/mockpeerconnectionobservers.h"
40#include "pc/test/testsdpstrings.h"
41#include "pc/videocapturertracksource.h"
42#include "pc/videotrack.h"
43#include "rtc_base/gunit.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020044#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/virtualsocketserver.h"
47#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020048#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070049
50#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070052#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54static const char kStreamLabel1[] = "local_stream_1";
55static const char kStreamLabel2[] = "local_stream_2";
56static const char kStreamLabel3[] = "local_stream_3";
57static const int kDefaultStunPort = 3478;
58static const char kStunAddressOnly[] = "stun:address";
59static const char kStunInvalidPort[] = "stun:address:-1";
60static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
61static const char kStunAddressPortAndMore2[] = "stun:address:port more";
62static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
63static const char kTurnUsername[] = "user";
64static const char kTurnPassword[] = "password";
65static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020066static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
deadbeefab9b2d12015-10-14 11:33:11 -070068static const char kStreams[][8] = {"stream1", "stream2"};
69static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
70static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
71
deadbeef5e97fb52015-10-15 12:49:08 -070072static const char kRecvonly[] = "recvonly";
73static const char kSendrecv[] = "sendrecv";
74
deadbeefab9b2d12015-10-14 11:33:11 -070075// Reference SDP with a MediaStream with label "stream1" and audio track with
76// id "audio_1" and a video track with id "video_1;
77static const char kSdpStringWithStream1[] =
78 "v=0\r\n"
79 "o=- 0 0 IN IP4 127.0.0.1\r\n"
80 "s=-\r\n"
81 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080082 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=ice-ufrag:e5785931\r\n"
84 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
85 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
86 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080089 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070090 "a=rtpmap:103 ISAC/16000\r\n"
91 "a=ssrc:1 cname:stream1\r\n"
92 "a=ssrc:1 mslabel:stream1\r\n"
93 "a=ssrc:1 label:audiotrack0\r\n"
94 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080095 "a=ice-ufrag:e5785931\r\n"
96 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
97 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
98 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700100 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800101 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700102 "a=rtpmap:120 VP8/90000\r\n"
103 "a=ssrc:2 cname:stream1\r\n"
104 "a=ssrc:2 mslabel:stream1\r\n"
105 "a=ssrc:2 label:videotrack0\r\n";
106
zhihuang81c3a032016-11-17 12:06:24 -0800107// Reference SDP with a MediaStream with label "stream1" and audio track with
108// id "audio_1";
109static const char kSdpStringWithStream1AudioTrackOnly[] =
110 "v=0\r\n"
111 "o=- 0 0 IN IP4 127.0.0.1\r\n"
112 "s=-\r\n"
113 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800114 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800115 "a=ice-ufrag:e5785931\r\n"
116 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
117 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
118 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800119 "a=mid:audio\r\n"
120 "a=sendrecv\r\n"
121 "a=rtpmap:103 ISAC/16000\r\n"
122 "a=ssrc:1 cname:stream1\r\n"
123 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800124 "a=ssrc:1 label:audiotrack0\r\n"
125 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800126
deadbeefab9b2d12015-10-14 11:33:11 -0700127// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
128// MediaStreams have one audio track and one video track.
129// This uses MSID.
130static const char kSdpStringWithStream1And2[] =
131 "v=0\r\n"
132 "o=- 0 0 IN IP4 127.0.0.1\r\n"
133 "s=-\r\n"
134 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800135 "a=msid-semantic: WMS stream1 stream2\r\n"
136 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=ice-ufrag:e5785931\r\n"
138 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
139 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
140 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700142 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800143 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700144 "a=rtpmap:103 ISAC/16000\r\n"
145 "a=ssrc:1 cname:stream1\r\n"
146 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
147 "a=ssrc:3 cname:stream2\r\n"
148 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
149 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800150 "a=ice-ufrag:e5785931\r\n"
151 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
152 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
153 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700155 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800156 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:120 VP8/0\r\n"
158 "a=ssrc:2 cname:stream1\r\n"
159 "a=ssrc:2 msid:stream1 videotrack0\r\n"
160 "a=ssrc:4 cname:stream2\r\n"
161 "a=ssrc:4 msid:stream2 videotrack1\r\n";
162
163// Reference SDP without MediaStreams. Msid is not supported.
164static const char kSdpStringWithoutStreams[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800169 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=ice-ufrag:e5785931\r\n"
171 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
172 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
173 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800176 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=rtpmap:103 ISAC/16000\r\n"
178 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800179 "a=ice-ufrag:e5785931\r\n"
180 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
181 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
182 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700184 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800185 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700186 "a=rtpmap:120 VP8/90000\r\n";
187
188// Reference SDP without MediaStreams. Msid is supported.
189static const char kSdpStringWithMsidWithoutStreams[] =
190 "v=0\r\n"
191 "o=- 0 0 IN IP4 127.0.0.1\r\n"
192 "s=-\r\n"
193 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800194 "a=msid-semantic: WMS\r\n"
195 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=ice-ufrag:e5785931\r\n"
197 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
198 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
199 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700201 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800202 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=rtpmap:103 ISAC/16000\r\n"
204 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800205 "a=ice-ufrag:e5785931\r\n"
206 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
207 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
208 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800211 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:120 VP8/90000\r\n";
213
214// Reference SDP without MediaStreams and audio only.
215static const char kSdpStringWithoutStreamsAudioOnly[] =
216 "v=0\r\n"
217 "o=- 0 0 IN IP4 127.0.0.1\r\n"
218 "s=-\r\n"
219 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800220 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=ice-ufrag:e5785931\r\n"
222 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
223 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
224 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700226 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:103 ISAC/16000\r\n";
229
230// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
231static const char kSdpStringSendOnlyWithoutStreams[] =
232 "v=0\r\n"
233 "o=- 0 0 IN IP4 127.0.0.1\r\n"
234 "s=-\r\n"
235 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800236 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=ice-ufrag:e5785931\r\n"
238 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
239 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
240 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700242 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800244 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=rtpmap:103 ISAC/16000\r\n"
246 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800247 "a=ice-ufrag:e5785931\r\n"
248 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
249 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
250 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700252 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800254 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700255 "a=rtpmap:120 VP8/90000\r\n";
256
257static const char kSdpStringInit[] =
258 "v=0\r\n"
259 "o=- 0 0 IN IP4 127.0.0.1\r\n"
260 "s=-\r\n"
261 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700262 "a=msid-semantic: WMS\r\n";
263
264static const char kSdpStringAudio[] =
265 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800266 "a=ice-ufrag:e5785931\r\n"
267 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
268 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
269 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700271 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800272 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700273 "a=rtpmap:103 ISAC/16000\r\n";
274
275static const char kSdpStringVideo[] =
276 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800277 "a=ice-ufrag:e5785931\r\n"
278 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
279 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
280 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700282 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800283 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700284 "a=rtpmap:120 VP8/90000\r\n";
285
286static const char kSdpStringMs1Audio0[] =
287 "a=ssrc:1 cname:stream1\r\n"
288 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
289
290static const char kSdpStringMs1Video0[] =
291 "a=ssrc:2 cname:stream1\r\n"
292 "a=ssrc:2 msid:stream1 videotrack0\r\n";
293
294static const char kSdpStringMs1Audio1[] =
295 "a=ssrc:3 cname:stream1\r\n"
296 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
297
298static const char kSdpStringMs1Video1[] =
299 "a=ssrc:4 cname:stream1\r\n"
300 "a=ssrc:4 msid:stream1 videotrack1\r\n";
301
deadbeef8662f942017-01-20 21:20:51 -0800302static const char kDtlsSdesFallbackSdp[] =
303 "v=0\r\n"
304 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
305 "s=-\r\n"
306 "c=IN IP4 0.0.0.0\r\n"
307 "t=0 0\r\n"
308 "a=group:BUNDLE audio\r\n"
309 "a=msid-semantic: WMS\r\n"
310 "m=audio 1 RTP/SAVPF 0\r\n"
311 "a=sendrecv\r\n"
312 "a=rtcp-mux\r\n"
313 "a=mid:audio\r\n"
314 "a=ssrc:1 cname:stream1\r\n"
315 "a=ssrc:1 mslabel:stream1\r\n"
316 "a=ssrc:1 label:audiotrack0\r\n"
317 "a=ice-ufrag:e5785931\r\n"
318 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
319 "a=rtpmap:0 pcmu/8000\r\n"
320 "a=fingerprint:sha-1 "
321 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
322 "a=setup:actpass\r\n"
323 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
324 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
325 "dummy_session_params\r\n";
326
perkjd61bf802016-03-24 03:16:19 -0700327using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700328using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700330using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331using webrtc::AudioTrackInterface;
332using webrtc::DataBuffer;
333using webrtc::DataChannelInterface;
334using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700336using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700337using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338using webrtc::MediaStreamInterface;
339using webrtc::MediaStreamTrackInterface;
340using webrtc::MockCreateSessionDescriptionObserver;
341using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700342using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343using webrtc::MockSetSessionDescriptionObserver;
344using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700345using webrtc::NotifierInterface;
346using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347using webrtc::PeerConnectionInterface;
348using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800349using webrtc::RTCError;
350using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700351using webrtc::RtpReceiverInterface;
352using webrtc::RtpSenderInterface;
Steve Anton4e70a722017-11-28 14:57:10 -0800353using webrtc::RtpTransceiverDirection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354using webrtc::SdpParseError;
355using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700356using webrtc::StreamCollection;
357using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100358using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700359using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360using webrtc::VideoTrackInterface;
361
deadbeefab9b2d12015-10-14 11:33:11 -0700362typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
363
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364namespace {
365
366// Gets the first ssrc of given content type from the ContentInfo.
367bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
368 if (!content_info || !ssrc) {
369 return false;
370 }
371 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000372 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 content_info->description);
374 if (!media_desc || media_desc->streams().empty()) {
375 return false;
376 }
377 *ssrc = media_desc->streams().begin()->first_ssrc();
378 return true;
379}
380
deadbeefd1a38b52016-12-10 13:15:33 -0800381// Get the ufrags out of an SDP blob. Useful for testing ICE restart
382// behavior.
383std::vector<std::string> GetUfrags(
384 const webrtc::SessionDescriptionInterface* desc) {
385 std::vector<std::string> ufrags;
386 for (const cricket::TransportInfo& info :
387 desc->description()->transport_infos()) {
388 ufrags.push_back(info.description.ice_ufrag);
389 }
390 return ufrags;
391}
392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393void SetSsrcToZero(std::string* sdp) {
394 const char kSdpSsrcAtribute[] = "a=ssrc:";
395 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
396 size_t ssrc_pos = 0;
397 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
398 std::string::npos) {
399 size_t end_ssrc = sdp->find(" ", ssrc_pos);
400 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
401 ssrc_pos = end_ssrc;
402 }
403}
404
deadbeefab9b2d12015-10-14 11:33:11 -0700405// Check if |streams| contains the specified track.
406bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
407 const std::string& stream_label,
408 const std::string& track_id) {
409 for (const cricket::StreamParams& params : streams) {
410 if (params.sync_label == stream_label && params.id == track_id) {
411 return true;
412 }
413 }
414 return false;
415}
416
417// Check if |senders| contains the specified sender, by id.
418bool ContainsSender(
419 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
420 const std::string& id) {
421 for (const auto& sender : senders) {
422 if (sender->id() == id) {
423 return true;
424 }
425 }
426 return false;
427}
428
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700429// Check if |senders| contains the specified sender, by id and stream id.
430bool ContainsSender(
431 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
432 const std::string& id,
433 const std::string& stream_id) {
434 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700435 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700436 return true;
437 }
438 }
439 return false;
440}
441
deadbeefab9b2d12015-10-14 11:33:11 -0700442// Create a collection of streams.
443// CreateStreamCollection(1) creates a collection that
444// correspond to kSdpStringWithStream1.
445// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
446rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700447 int number_of_streams,
448 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700449 rtc::scoped_refptr<StreamCollection> local_collection(
450 StreamCollection::Create());
451
452 for (int i = 0; i < number_of_streams; ++i) {
453 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
454 webrtc::MediaStream::Create(kStreams[i]));
455
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700456 for (int j = 0; j < tracks_per_stream; ++j) {
457 // Add a local audio track.
458 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
459 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
460 nullptr));
461 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700462
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700463 // Add a local video track.
464 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
465 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700466 webrtc::FakeVideoTrackSource::Create(),
467 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700468 stream->AddTrack(video_track);
469 }
deadbeefab9b2d12015-10-14 11:33:11 -0700470
471 local_collection->AddStream(stream);
472 }
473 return local_collection;
474}
475
476// Check equality of StreamCollections.
477bool CompareStreamCollections(StreamCollectionInterface* s1,
478 StreamCollectionInterface* s2) {
479 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
480 return false;
481 }
482
483 for (size_t i = 0; i != s1->count(); ++i) {
484 if (s1->at(i)->label() != s2->at(i)->label()) {
485 return false;
486 }
487 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
488 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
489 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
490 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
491
492 if (audio_tracks1.size() != audio_tracks2.size()) {
493 return false;
494 }
495 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
496 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
497 return false;
498 }
499 }
500 if (video_tracks1.size() != video_tracks2.size()) {
501 return false;
502 }
503 for (size_t j = 0; j != video_tracks1.size(); ++j) {
504 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
505 return false;
506 }
507 }
508 }
509 return true;
510}
511
perkjd61bf802016-03-24 03:16:19 -0700512// Helper class to test Observer.
513class MockTrackObserver : public ObserverInterface {
514 public:
515 explicit MockTrackObserver(NotifierInterface* notifier)
516 : notifier_(notifier) {
517 notifier_->RegisterObserver(this);
518 }
519
520 ~MockTrackObserver() { Unregister(); }
521
522 void Unregister() {
523 if (notifier_) {
524 notifier_->UnregisterObserver(this);
525 notifier_ = nullptr;
526 }
527 }
528
529 MOCK_METHOD0(OnChanged, void());
530
531 private:
532 NotifierInterface* notifier_;
533};
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700536
nisse528b7932017-05-08 03:21:43 -0700537// The PeerConnectionMediaConfig tests below verify that configuration and
538// constraints are propagated into the PeerConnection's MediaConfig. These
539// settings are intended for MediaChannel constructors, but that is not
540// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700541class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
542 public:
zhihuang38ede132017-06-15 12:52:32 -0700543 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
544 CreatePeerConnectionFactoryForTest() {
545 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
546 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
547
henrika919dc2e2017-10-12 14:24:55 +0200548 // Use fake audio device module since we're only testing the interface
549 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700550 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
551 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200552 FakeAudioCaptureModule::Create(), audio_encoder_factory,
553 audio_decoder_factory, nullptr, nullptr, nullptr,
554 webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700555
556 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
557 webrtc::CreateCallFactory();
558
559 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
560 webrtc::CreateRtcEventLogFactory();
561
562 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
563 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200564 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700565 std::move(event_log_factory));
566 }
567
568 PeerConnectionFactoryForTest(
569 rtc::Thread* network_thread,
570 rtc::Thread* worker_thread,
571 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700572 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
573 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
574 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
575 : webrtc::PeerConnectionFactory(network_thread,
576 worker_thread,
577 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700578 std::move(media_engine),
579 std::move(call_factory),
580 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800581
zhihuang29ff8442016-07-27 11:07:25 -0700582 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700583 cricket::PortAllocator* port_allocator,
584 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700585 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700586 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700587 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700588 return transport_controller;
589 }
590
deadbeefd7850b22017-08-23 10:59:19 -0700591 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700592 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700593};
594
Steve Anton94286cb2017-09-26 16:20:19 -0700595// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
596// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597class PeerConnectionInterfaceTest : public testing::Test {
598 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700599 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700600 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800601#ifdef WEBRTC_ANDROID
602 webrtc::InitializeAndroidObjects();
603#endif
604 }
605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700607 // Use fake audio capture module since we're only testing the interface
608 // level, and using a real one could make tests flaky when run in parallel.
609 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700611 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200612 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
613 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700614 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700615 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700616 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700617 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623
deadbeef293e9262017-01-11 12:28:30 -0800624 // DTLS does not work in a loopback call, so is disabled for most of the
625 // tests in this file.
626 void CreatePeerConnectionWithoutDtls() {
627 FakeConstraints no_dtls_constraints;
628 no_dtls_constraints.AddMandatory(
629 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
630
631 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
632 &no_dtls_constraints);
633 }
634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700636 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
637 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 }
639
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700640 void CreatePeerConnectionWithIceTransportsType(
641 PeerConnectionInterface::IceTransportsType type) {
642 PeerConnectionInterface::RTCConfiguration config;
643 config.type = type;
644 return CreatePeerConnection(config, nullptr);
645 }
646
647 void CreatePeerConnectionWithIceServer(const std::string& uri,
648 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800649 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700651 server.uri = uri;
652 server.password = password;
653 config.servers.push_back(server);
654 CreatePeerConnection(config, nullptr);
655 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100657 void CreatePeerConnection(
658 const PeerConnectionInterface::RTCConfiguration& config,
659 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700660 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800661 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
662 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000663
deadbeef1dcb1642017-03-29 21:08:16 -0700664 // Create certificate generator unless DTLS constraint is explicitly set to
665 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200666 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000667 bool dtls;
668 if (FindConstraint(constraints,
669 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
670 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200671 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800672 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
673 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000674 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200675 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800676 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200677 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 ASSERT_TRUE(pc_.get() != NULL);
679 observer_.SetPeerConnectionInterface(pc_.get());
680 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
681 }
682
deadbeef0a6c4ca2015-10-06 11:38:28 -0700683 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800684 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700685 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700686 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800687 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700688
zhihuang9763d562016-08-05 11:14:50 -0700689 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700690 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
691 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700693 }
694
Steve Anton038834f2017-07-14 15:59:59 -0700695 void CreatePeerConnectionExpectFail(
696 PeerConnectionInterface::RTCConfiguration config) {
697 PeerConnectionInterface::IceServer server;
698 server.uri = kTurnIceServerUri;
699 server.password = kTurnPassword;
700 config.servers.push_back(server);
701 rtc::scoped_refptr<PeerConnectionInterface> pc =
702 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
703 EXPECT_EQ(nullptr, pc);
704 }
705
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700707 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
709 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
710 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800712 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713
deadbeef0a6c4ca2015-10-06 11:38:28 -0700714 CreatePeerConnectionExpectFail(kStunInvalidPort);
715 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
716 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
720 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800722 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 }
728
729 void ReleasePeerConnection() {
730 pc_ = NULL;
731 observer_.SetPeerConnectionInterface(NULL);
732 }
733
deadbeefab9b2d12015-10-14 11:33:11 -0700734 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700736 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700738 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800739 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
740 new cricket::FakeVideoCapturer()),
741 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700742 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 pc_factory_->CreateVideoTrack(label + "v0", video_source));
744 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000745 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
747 observer_.renegotiation_needed_ = false;
748 }
749
750 void AddVoiceStream(const std::string& label) {
751 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700752 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700754 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 pc_factory_->CreateAudioTrack(label + "a0", NULL));
756 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000757 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
759 observer_.renegotiation_needed_ = false;
760 }
761
762 void AddAudioVideoStream(const std::string& stream_label,
763 const std::string& audio_track_label,
764 const std::string& video_track_label) {
765 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700766 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700768 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 pc_factory_->CreateAudioTrack(
770 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
771 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700773 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800774 video_track_label, pc_factory_->CreateVideoSource(
775 std::unique_ptr<cricket::VideoCapturer>(
776 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000778 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
780 observer_.renegotiation_needed_ = false;
781 }
782
kwibergd1fe2812016-04-27 06:47:29 -0700783 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700784 bool offer,
785 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000786 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
787 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 MockCreateSessionDescriptionObserver>());
789 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700790 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700792 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700795 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return observer->result();
797 }
798
kwibergd1fe2812016-04-27 06:47:29 -0700799 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700800 MediaConstraintsInterface* constraints) {
801 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
kwibergd1fe2812016-04-27 06:47:29 -0700804 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700805 MediaConstraintsInterface* constraints) {
806 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 }
808
Steve Antondb45ca82017-09-11 18:27:34 -0700809 bool DoSetSessionDescription(
810 std::unique_ptr<SessionDescriptionInterface> desc,
811 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000812 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
813 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 MockSetSessionDescriptionObserver>());
815 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700816 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700818 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
zhihuang29ff8442016-07-27 11:07:25 -0700820 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
821 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
822 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 return observer->result();
824 }
825
Steve Antondb45ca82017-09-11 18:27:34 -0700826 bool DoSetLocalDescription(
827 std::unique_ptr<SessionDescriptionInterface> desc) {
828 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 }
830
Steve Antondb45ca82017-09-11 18:27:34 -0700831 bool DoSetRemoteDescription(
832 std::unique_ptr<SessionDescriptionInterface> desc) {
833 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 }
835
836 // Calls PeerConnection::GetStats and check the return value.
837 // It does not verify the values in the StatReports since a RTCP packet might
838 // be required.
839 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000840 rtc::scoped_refptr<MockStatsObserver> observer(
841 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000842 if (!pc_->GetStats(
843 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return false;
845 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
846 return observer->called();
847 }
848
849 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800850 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 // Create a local stream with audio&video tracks.
852 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
853 CreateOfferReceiveAnswer();
854 }
855
856 // Verify that RTP Header extensions has been negotiated for audio and video.
857 void VerifyRemoteRtpHeaderExtensions() {
858 const cricket::MediaContentDescription* desc =
859 cricket::GetFirstAudioContentDescription(
860 pc_->remote_description()->description());
861 ASSERT_TRUE(desc != NULL);
862 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
863
864 desc = cricket::GetFirstVideoContentDescription(
865 pc_->remote_description()->description());
866 ASSERT_TRUE(desc != NULL);
867 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
868 }
869
870 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700871 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700872 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 std::string sdp;
874 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700875 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700877 sdp, nullptr));
878 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
880 }
881
deadbeefab9b2d12015-10-14 11:33:11 -0700882 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700883 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700884 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700885 sdp, nullptr));
886 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700887 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
888 }
889
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700891 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700892 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893
894 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
895 // audio codec change, even if the parameter has nothing to do with
896 // receiving. Not all parameters are serialized to SDP.
897 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
898 // the SessionDescription, it is necessary to do that here to in order to
899 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
900 // https://code.google.com/p/webrtc/issues/detail?id=1356
901 std::string sdp;
902 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700903 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700905 sdp, nullptr));
906 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
908 }
909
910 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700911 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700912 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913
914 std::string sdp;
915 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700916 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700918 sdp, nullptr));
919 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
921 }
922
923 void CreateOfferReceiveAnswer() {
924 CreateOfferAsLocalDescription();
925 std::string sdp;
926 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
927 CreateAnswerAsRemoteDescription(sdp);
928 }
929
930 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700931 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700932 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
934 // audio codec change, even if the parameter has nothing to do with
935 // receiving. Not all parameters are serialized to SDP.
936 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
937 // the SessionDescription, it is necessary to do that here to in order to
938 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
939 // https://code.google.com/p/webrtc/issues/detail?id=1356
940 std::string sdp;
941 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700942 std::unique_ptr<SessionDescriptionInterface> new_offer(
943 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
944 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945
Steve Antondb45ca82017-09-11 18:27:34 -0700946 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000948 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700949 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 }
951
deadbeefab9b2d12015-10-14 11:33:11 -0700952 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700953 std::unique_ptr<SessionDescriptionInterface> answer(
954 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
955 sdp, nullptr));
956 ASSERT_TRUE(answer);
957 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
959 }
960
deadbeefab9b2d12015-10-14 11:33:11 -0700961 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700962 std::unique_ptr<SessionDescriptionInterface> pr_answer(
963 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
964 sdp, nullptr));
965 ASSERT_TRUE(pr_answer);
966 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700968 std::unique_ptr<SessionDescriptionInterface> answer(
969 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
970 sdp, nullptr));
971 ASSERT_TRUE(answer);
972 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
974 }
975
976 // Help function used for waiting until a the last signaled remote stream has
977 // the same label as |stream_label|. In a few of the tests in this file we
978 // answer with the same session description as we offer and thus we can
979 // check if OnAddStream have been called with the same stream as we offer to
980 // send.
981 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
982 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
983 }
984
985 // Creates an offer and applies it as a local session description.
986 // Creates an answer with the same SDP an the offer but removes all lines
987 // that start with a:ssrc"
988 void CreateOfferReceiveAnswerWithoutSsrc() {
989 CreateOfferAsLocalDescription();
990 std::string sdp;
991 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
992 SetSsrcToZero(&sdp);
993 CreateAnswerAsRemoteDescription(sdp);
994 }
995
deadbeefab9b2d12015-10-14 11:33:11 -0700996 // This function creates a MediaStream with label kStreams[0] and
997 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
998 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700999 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001000 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001001 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001002 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1003 size_t number_of_video_tracks) {
1004 EXPECT_LE(number_of_audio_tracks, 2u);
1005 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001006
1007 reference_collection_ = StreamCollection::Create();
1008 std::string sdp_ms1 = std::string(kSdpStringInit);
1009
1010 std::string mediastream_label = kStreams[0];
1011
1012 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1013 webrtc::MediaStream::Create(mediastream_label));
1014 reference_collection_->AddStream(stream);
1015
1016 if (number_of_audio_tracks > 0) {
1017 sdp_ms1 += std::string(kSdpStringAudio);
1018 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1019 AddAudioTrack(kAudioTracks[0], stream);
1020 }
1021 if (number_of_audio_tracks > 1) {
1022 sdp_ms1 += kSdpStringMs1Audio1;
1023 AddAudioTrack(kAudioTracks[1], stream);
1024 }
1025
1026 if (number_of_video_tracks > 0) {
1027 sdp_ms1 += std::string(kSdpStringVideo);
1028 sdp_ms1 += std::string(kSdpStringMs1Video0);
1029 AddVideoTrack(kVideoTracks[0], stream);
1030 }
1031 if (number_of_video_tracks > 1) {
1032 sdp_ms1 += kSdpStringMs1Video1;
1033 AddVideoTrack(kVideoTracks[1], stream);
1034 }
1035
kwibergd1fe2812016-04-27 06:47:29 -07001036 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001037 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1038 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001039 }
1040
1041 void AddAudioTrack(const std::string& track_id,
1042 MediaStreamInterface* stream) {
1043 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1044 webrtc::AudioTrack::Create(track_id, nullptr));
1045 ASSERT_TRUE(stream->AddTrack(audio_track));
1046 }
1047
1048 void AddVideoTrack(const std::string& track_id,
1049 MediaStreamInterface* stream) {
1050 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001051 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001052 webrtc::FakeVideoTrackSource::Create(),
1053 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001054 ASSERT_TRUE(stream->AddTrack(video_track));
1055 }
1056
kwibergfd8be342016-05-14 19:44:11 -07001057 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001058 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001059 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001060 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001061 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1062 return offer;
1063 }
1064
kwibergfd8be342016-05-14 19:44:11 -07001065 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001066 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001067 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001068 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001069 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1070 return answer;
1071 }
1072
1073 const std::string& GetFirstAudioStreamCname(
1074 const SessionDescriptionInterface* desc) {
1075 const cricket::ContentInfo* audio_content =
1076 cricket::GetFirstAudioContent(desc->description());
1077 const cricket::AudioContentDescription* audio_desc =
1078 static_cast<const cricket::AudioContentDescription*>(
1079 audio_content->description);
1080 return audio_desc->streams()[0].cname;
1081 }
1082
zhihuang1c378ed2017-08-17 14:10:50 -07001083 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1084 const RTCOfferAnswerOptions& offer_answer_options) {
1085 RTC_DCHECK(pc_);
1086 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1087 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1088 pc_->CreateOffer(observer, offer_answer_options);
1089 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1090 return observer->MoveDescription();
1091 }
1092
1093 void CreateOfferWithOptionsAsRemoteDescription(
1094 std::unique_ptr<SessionDescriptionInterface>* desc,
1095 const RTCOfferAnswerOptions& offer_answer_options) {
1096 *desc = CreateOfferWithOptions(offer_answer_options);
1097 ASSERT_TRUE(desc != nullptr);
1098 std::string sdp;
1099 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001100 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001101 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001102 sdp, nullptr));
1103 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001104 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1105 }
1106
1107 void CreateOfferWithOptionsAsLocalDescription(
1108 std::unique_ptr<SessionDescriptionInterface>* desc,
1109 const RTCOfferAnswerOptions& offer_answer_options) {
1110 *desc = CreateOfferWithOptions(offer_answer_options);
1111 ASSERT_TRUE(desc != nullptr);
1112 std::string sdp;
1113 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001114 std::unique_ptr<SessionDescriptionInterface> new_offer(
1115 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1116 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001117
Steve Antondb45ca82017-09-11 18:27:34 -07001118 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001119 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1120 }
1121
1122 bool HasCNCodecs(const cricket::ContentInfo* content) {
1123 const cricket::ContentDescription* description = content->description;
1124 RTC_DCHECK(description);
1125 const cricket::AudioContentDescription* audio_content_desc =
1126 static_cast<const cricket::AudioContentDescription*>(description);
1127 RTC_DCHECK(audio_content_desc);
1128 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1129 if (audio_content_desc->codecs()[i].name == "CN")
1130 return true;
1131 }
1132 return false;
1133 }
1134
deadbeef9a6f4d42017-05-15 19:43:33 -07001135 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1136 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001137 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001138 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001139 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001140 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1141 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1142 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001144 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145};
1146
zhihuang29ff8442016-07-27 11:07:25 -07001147// Test that no callbacks on the PeerConnectionObserver are called after the
1148// PeerConnection is closed.
1149TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001150 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001151 pc_factory_for_test_->CreatePeerConnection(
1152 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1153 nullptr, &observer_));
1154 observer_.SetPeerConnectionInterface(pc.get());
1155 pc->Close();
1156
1157 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001158 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001159 std::vector<cricket::Candidate> candidates;
1160 pc_factory_for_test_->transport_controller->SignalGatheringState(
1161 cricket::IceGatheringState{});
1162 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1163 "", candidates);
1164 pc_factory_for_test_->transport_controller->SignalConnectionState(
1165 cricket::IceConnectionState{});
1166 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1167 candidates);
1168 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001169 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001170}
1171
zhihuang8f65cdf2016-05-06 18:40:30 -07001172// Generate different CNAMEs when PeerConnections are created.
1173// The CNAMEs are expected to be generated randomly. It is possible
1174// that the test fails, though the possibility is very low.
1175TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 CreateOfferWithOneAudioStream();
1180 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1181 GetFirstAudioStreamCname(offer2.get()));
1182}
1183
1184TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001185 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001187 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001188 CreateAnswerWithOneAudioStream();
1189 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1190 GetFirstAudioStreamCname(answer2.get()));
1191}
1192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193TEST_F(PeerConnectionInterfaceTest,
1194 CreatePeerConnectionWithDifferentConfigurations) {
1195 CreatePeerConnectionWithDifferentConfigurations();
1196}
1197
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001198TEST_F(PeerConnectionInterfaceTest,
1199 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1200 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1201 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1202 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1203 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1204 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1205 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1206 port_allocator_->candidate_filter());
1207 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1208 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1209}
1210
1211// Test that when a PeerConnection is created with a nonzero candidate pool
1212// size, the pooled PortAllocatorSession is created with all the attributes
1213// in the RTCConfiguration.
1214TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1215 PeerConnectionInterface::RTCConfiguration config;
1216 PeerConnectionInterface::IceServer server;
1217 server.uri = kStunAddressOnly;
1218 config.servers.push_back(server);
1219 config.type = PeerConnectionInterface::kRelay;
1220 config.disable_ipv6 = true;
1221 config.tcp_candidate_policy =
1222 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001223 config.candidate_network_policy =
1224 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001225 config.ice_candidate_pool_size = 1;
1226 CreatePeerConnection(config, nullptr);
1227
1228 const cricket::FakePortAllocatorSession* session =
1229 static_cast<const cricket::FakePortAllocatorSession*>(
1230 port_allocator_->GetPooledSession());
1231 ASSERT_NE(nullptr, session);
1232 EXPECT_EQ(1UL, session->stun_servers().size());
1233 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1234 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001235 EXPECT_LT(0U,
1236 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001237}
1238
deadbeefd21eab32017-07-26 16:50:11 -07001239// Test that network-related RTCConfiguration members are applied to the
1240// PortAllocator when CreatePeerConnection is called. Specifically:
1241// - disable_ipv6_on_wifi
1242// - max_ipv6_networks
1243// - tcp_candidate_policy
1244// - candidate_network_policy
1245// - prune_turn_ports
1246//
1247// Note that the candidate filter (RTCConfiguration::type) is already tested
1248// above.
1249TEST_F(PeerConnectionInterfaceTest,
1250 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1251 // Create fake port allocator.
1252 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1253 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1254 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1255
1256 // Create RTCConfiguration with some network-related fields relevant to
1257 // PortAllocator populated.
1258 PeerConnectionInterface::RTCConfiguration config;
1259 config.disable_ipv6_on_wifi = true;
1260 config.max_ipv6_networks = 10;
1261 config.tcp_candidate_policy =
1262 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1263 config.candidate_network_policy =
1264 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1265 config.prune_turn_ports = true;
1266
1267 // Create the PC factory and PC with the above config.
1268 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1269 webrtc::CreatePeerConnectionFactory(
1270 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001271 rtc::Thread::Current(), fake_audio_capture_module_,
1272 webrtc::CreateBuiltinAudioEncoderFactory(),
1273 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001274 rtc::scoped_refptr<PeerConnectionInterface> pc(
1275 pc_factory->CreatePeerConnection(
1276 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1277
1278 // Now validate that the config fields set above were applied to the
1279 // PortAllocator, as flags or otherwise.
1280 EXPECT_FALSE(raw_port_allocator->flags() &
1281 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1282 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1283 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1284 EXPECT_TRUE(raw_port_allocator->flags() &
1285 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1286 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1287}
1288
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001289// Test that the PeerConnection initializes the port allocator passed into it,
1290// and on the correct thread.
1291TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001292 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001293 std::unique_ptr<rtc::Thread> network_thread(
1294 rtc::Thread::CreateWithSocketServer());
1295 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001296 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1297 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001298 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001299 fake_audio_capture_module_,
1300 webrtc::CreateBuiltinAudioEncoderFactory(),
1301 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001302 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001303 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001304 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1305 PeerConnectionInterface::RTCConfiguration config;
1306 rtc::scoped_refptr<PeerConnectionInterface> pc(
1307 pc_factory->CreatePeerConnection(
1308 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1309 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1310 // so all we have to do here is check that it's initialized.
1311 EXPECT_TRUE(raw_port_allocator->initialized());
1312}
1313
deadbeef46c73892016-11-16 19:42:04 -08001314// Check that GetConfiguration returns the configuration the PeerConnection was
1315// constructed with, before SetConfiguration is called.
1316TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1317 PeerConnectionInterface::RTCConfiguration config;
1318 config.type = PeerConnectionInterface::kRelay;
1319 CreatePeerConnection(config, nullptr);
1320
1321 PeerConnectionInterface::RTCConfiguration returned_config =
1322 pc_->GetConfiguration();
1323 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1324}
1325
1326// Check that GetConfiguration returns the last configuration passed into
1327// SetConfiguration.
1328TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1329 CreatePeerConnection();
1330
1331 PeerConnectionInterface::RTCConfiguration config;
1332 config.type = PeerConnectionInterface::kRelay;
1333 EXPECT_TRUE(pc_->SetConfiguration(config));
1334
1335 PeerConnectionInterface::RTCConfiguration returned_config =
1336 pc_->GetConfiguration();
1337 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1338}
1339
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001341 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001342 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 AddVoiceStream(kStreamLabel2);
1344 ASSERT_EQ(2u, pc_->local_streams()->count());
1345
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001346 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001347 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001349 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1350 pc_factory_->CreateAudioTrack(kStreamLabel3,
1351 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001353 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001354 EXPECT_EQ(3u, pc_->local_streams()->count());
1355
1356 // Remove the third stream.
1357 pc_->RemoveStream(pc_->local_streams()->at(2));
1358 EXPECT_EQ(2u, pc_->local_streams()->count());
1359
1360 // Remove the second stream.
1361 pc_->RemoveStream(pc_->local_streams()->at(1));
1362 EXPECT_EQ(1u, pc_->local_streams()->count());
1363
1364 // Remove the first stream.
1365 pc_->RemoveStream(pc_->local_streams()->at(0));
1366 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367}
1368
deadbeefab9b2d12015-10-14 11:33:11 -07001369// Test that the created offer includes streams we added.
1370TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001371 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001372 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001373 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001374 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001375
1376 const cricket::ContentInfo* audio_content =
1377 cricket::GetFirstAudioContent(offer->description());
1378 const cricket::AudioContentDescription* audio_desc =
1379 static_cast<const cricket::AudioContentDescription*>(
1380 audio_content->description);
1381 EXPECT_TRUE(
1382 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1383
1384 const cricket::ContentInfo* video_content =
1385 cricket::GetFirstVideoContent(offer->description());
1386 const cricket::VideoContentDescription* video_desc =
1387 static_cast<const cricket::VideoContentDescription*>(
1388 video_content->description);
1389 EXPECT_TRUE(
1390 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1391
1392 // Add another stream and ensure the offer includes both the old and new
1393 // streams.
1394 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001395 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001396
1397 audio_content = cricket::GetFirstAudioContent(offer->description());
1398 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1399 audio_content->description);
1400 EXPECT_TRUE(
1401 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1402 EXPECT_TRUE(
1403 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1404
1405 video_content = cricket::GetFirstVideoContent(offer->description());
1406 video_desc = static_cast<const cricket::VideoContentDescription*>(
1407 video_content->description);
1408 EXPECT_TRUE(
1409 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1410 EXPECT_TRUE(
1411 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1412}
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001415 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001416 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 ASSERT_EQ(1u, pc_->local_streams()->count());
1418 pc_->RemoveStream(pc_->local_streams()->at(0));
1419 EXPECT_EQ(0u, pc_->local_streams()->count());
1420}
1421
deadbeefe1f9d832016-01-14 15:35:42 -08001422// Test for AddTrack and RemoveTrack methods.
1423// Tests that the created offer includes tracks we added,
1424// and that the RtpSenders are created correctly.
1425// Also tests that RemoveTrack removes the tracks from subsequent offers.
1426TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001427 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001428 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001429 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001430 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1431 std::vector<MediaStreamInterface*> stream_list;
1432 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001433 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001434 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001435 rtc::scoped_refptr<VideoTrackInterface> video_track(
1436 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001437 "video_track", pc_factory_->CreateVideoSource(
1438 std::unique_ptr<cricket::VideoCapturer>(
1439 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001440 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1441 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001442 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1443 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001444 EXPECT_EQ("audio_track", audio_sender->id());
1445 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001446 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1447 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001448 EXPECT_EQ("video_track", video_sender->id());
1449 EXPECT_EQ(video_track, video_sender->track());
1450
1451 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001452 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001453 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001454
1455 const cricket::ContentInfo* audio_content =
1456 cricket::GetFirstAudioContent(offer->description());
1457 const cricket::AudioContentDescription* audio_desc =
1458 static_cast<const cricket::AudioContentDescription*>(
1459 audio_content->description);
1460 EXPECT_TRUE(
1461 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1462
1463 const cricket::ContentInfo* video_content =
1464 cricket::GetFirstVideoContent(offer->description());
1465 const cricket::VideoContentDescription* video_desc =
1466 static_cast<const cricket::VideoContentDescription*>(
1467 video_content->description);
1468 EXPECT_TRUE(
1469 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1470
Steve Antondb45ca82017-09-11 18:27:34 -07001471 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001472
1473 // Now try removing the tracks.
1474 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1475 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1476
1477 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001478 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001479
1480 audio_content = cricket::GetFirstAudioContent(offer->description());
1481 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1482 audio_content->description);
1483 EXPECT_FALSE(
1484 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1485
1486 video_content = cricket::GetFirstVideoContent(offer->description());
1487 video_desc = static_cast<const cricket::VideoContentDescription*>(
1488 video_content->description);
1489 EXPECT_FALSE(
1490 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1491
Steve Antondb45ca82017-09-11 18:27:34 -07001492 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001493
1494 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1495 // should return false.
1496 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1497 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1498}
1499
1500// Test creating senders without a stream specified,
1501// expecting a random stream ID to be generated.
1502TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001503 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001504 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001505 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001506 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001507 rtc::scoped_refptr<VideoTrackInterface> video_track(
1508 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001509 "video_track", pc_factory_->CreateVideoSource(
1510 std::unique_ptr<cricket::VideoCapturer>(
1511 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001512 auto audio_sender =
1513 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1514 auto video_sender =
1515 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1516 EXPECT_EQ("audio_track", audio_sender->id());
1517 EXPECT_EQ(audio_track, audio_sender->track());
1518 EXPECT_EQ("video_track", video_sender->id());
1519 EXPECT_EQ(video_track, video_sender->track());
1520 // If the ID is truly a random GUID, it should be infinitely unlikely they
1521 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001522 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001523}
1524
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1526 InitiateCall();
1527 WaitAndVerifyOnAddStream(kStreamLabel1);
1528 VerifyRemoteRtpHeaderExtensions();
1529}
1530
1531TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001532 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001533 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 CreateOfferAsLocalDescription();
1535 std::string offer;
1536 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1537 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1538 WaitAndVerifyOnAddStream(kStreamLabel1);
1539}
1540
1541TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001542 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001543 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
1545 CreateOfferAsRemoteDescription();
1546 CreateAnswerAsLocalDescription();
1547
1548 WaitAndVerifyOnAddStream(kStreamLabel1);
1549}
1550
1551TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001552 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001553 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554
1555 CreateOfferAsRemoteDescription();
1556 CreatePrAnswerAsLocalDescription();
1557 CreateAnswerAsLocalDescription();
1558
1559 WaitAndVerifyOnAddStream(kStreamLabel1);
1560}
1561
1562TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1563 InitiateCall();
1564 ASSERT_EQ(1u, pc_->remote_streams()->count());
1565 pc_->RemoveStream(pc_->local_streams()->at(0));
1566 CreateOfferReceiveAnswer();
1567 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001568 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 CreateOfferReceiveAnswer();
1570}
1571
1572// Tests that after negotiating an audio only call, the respondent can perform a
1573// renegotiation that removes the audio stream.
1574TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001575 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 AddVoiceStream(kStreamLabel1);
1577 CreateOfferAsRemoteDescription();
1578 CreateAnswerAsLocalDescription();
1579
1580 ASSERT_EQ(1u, pc_->remote_streams()->count());
1581 pc_->RemoveStream(pc_->local_streams()->at(0));
1582 CreateOfferReceiveAnswer();
1583 EXPECT_EQ(0u, pc_->remote_streams()->count());
1584}
1585
1586// Test that candidates are generated and that we can parse our own candidates.
1587TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001588 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589
Steve Antonf1c6db12017-10-13 11:13:35 -07001590 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001592 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001593 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001594 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001595 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596
1597 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001599 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001600 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
Steve Antonf1c6db12017-10-13 11:13:35 -07001602 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001603 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604
Steve Antonf1c6db12017-10-13 11:13:35 -07001605 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
deadbeefab9b2d12015-10-14 11:33:11 -07001608// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609// not unique.
1610TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001611 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001613 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001614 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001615 EXPECT_TRUE(offer);
1616 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617
1618 // Create a local stream with audio&video tracks having same label.
1619 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1620
1621 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001622 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623
1624 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001625 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001626 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627}
1628
1629// Test that we will get different SSRCs for each tracks in the offer and answer
1630// we created.
1631TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001632 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 // Create a local stream with audio&video tracks having different labels.
1634 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1635
1636 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001637 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001638 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 int audio_ssrc = 0;
1640 int video_ssrc = 0;
1641 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1642 &audio_ssrc));
1643 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1644 &video_ssrc));
1645 EXPECT_NE(audio_ssrc, video_ssrc);
1646
1647 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001648 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001649 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001650 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 audio_ssrc = 0;
1652 video_ssrc = 0;
1653 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1654 &audio_ssrc));
1655 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1656 &video_ssrc));
1657 EXPECT_NE(audio_ssrc, video_ssrc);
1658}
1659
deadbeefeb459812015-12-15 19:24:43 -08001660// Test that it's possible to call AddTrack on a MediaStream after adding
1661// the stream to a PeerConnection.
1662// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1663TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001664 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001665 // Create audio stream and add to PeerConnection.
1666 AddVoiceStream(kStreamLabel1);
1667 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1668
1669 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001670 rtc::scoped_refptr<VideoTrackInterface> video_track(
1671 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001672 "video_label", pc_factory_->CreateVideoSource(
1673 std::unique_ptr<cricket::VideoCapturer>(
1674 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001675 stream->AddTrack(video_track.get());
1676
kwibergd1fe2812016-04-27 06:47:29 -07001677 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001678 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001679
1680 const cricket::MediaContentDescription* video_desc =
1681 cricket::GetFirstVideoContentDescription(offer->description());
1682 EXPECT_TRUE(video_desc != nullptr);
1683}
1684
1685// Test that it's possible to call RemoveTrack on a MediaStream after adding
1686// the stream to a PeerConnection.
1687// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1688TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001689 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001690 // Create audio/video stream and add to PeerConnection.
1691 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1692 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1693
1694 // Remove the video track.
1695 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1696
kwibergd1fe2812016-04-27 06:47:29 -07001697 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001698 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001699
1700 const cricket::MediaContentDescription* video_desc =
1701 cricket::GetFirstVideoContentDescription(offer->description());
1702 EXPECT_TRUE(video_desc == nullptr);
1703}
1704
deadbeef1dcb1642017-03-29 21:08:16 -07001705// Verify that CreateDtmfSender only succeeds if called with a valid local
1706// track. Other aspects of DtmfSenders are tested in
1707// peerconnection_integrationtest.cc.
1708TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1709 CreatePeerConnection();
1710 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1711 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1712 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1713 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1714 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1715}
1716
deadbeefbd7d8f72015-12-18 16:58:44 -08001717// Test creating a sender with a stream ID, and ensure the ID is populated
1718// in the offer.
1719TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001720 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001721 pc_->CreateSender("video", kStreamLabel1);
1722
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001724 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001725
1726 const cricket::MediaContentDescription* video_desc =
1727 cricket::GetFirstVideoContentDescription(offer->description());
1728 ASSERT_TRUE(video_desc != nullptr);
1729 ASSERT_EQ(1u, video_desc->streams().size());
1730 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1731}
1732
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733// Test that we can specify a certain track that we want statistics about.
1734TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1735 InitiateCall();
1736 ASSERT_LT(0u, pc_->remote_streams()->count());
1737 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001738 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1740 EXPECT_TRUE(DoGetStats(remote_audio));
1741
1742 // Remove the stream. Since we are sending to our selves the local
1743 // and the remote stream is the same.
1744 pc_->RemoveStream(pc_->local_streams()->at(0));
1745 // Do a re-negotiation.
1746 CreateOfferReceiveAnswer();
1747
1748 ASSERT_EQ(0u, pc_->remote_streams()->count());
1749
1750 // Test that we still can get statistics for the old track. Even if it is not
1751 // sent any longer.
1752 EXPECT_TRUE(DoGetStats(remote_audio));
1753}
1754
1755// Test that we can get stats on a video track.
1756TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1757 InitiateCall();
1758 ASSERT_LT(0u, pc_->remote_streams()->count());
1759 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001760 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1762 EXPECT_TRUE(DoGetStats(remote_video));
1763}
1764
1765// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001766TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001768 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 pc_factory_->CreateAudioTrack("unknown track", NULL));
1770 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1771}
1772
1773// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 FakeConstraints constraints;
1776 constraints.SetAllowRtpDataChannels();
1777 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001778 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001780 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 pc_->CreateDataChannel("test2", NULL);
1782 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001785 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 new MockDataChannelObserver(data2));
1787
1788 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1789 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1790 std::string data_to_send1 = "testing testing";
1791 std::string data_to_send2 = "testing something else";
1792 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1793
1794 CreateOfferReceiveAnswer();
1795 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1796 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1797
1798 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1799 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1800 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1801 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1802
1803 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1804 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1805
1806 data1->Close();
1807 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1808 CreateOfferReceiveAnswer();
1809 EXPECT_FALSE(observer1->IsOpen());
1810 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1811 EXPECT_TRUE(observer2->IsOpen());
1812
1813 data_to_send2 = "testing something else again";
1814 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1815
1816 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1817}
1818
1819// This test verifies that sendnig binary data over RTP data channels should
1820// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 FakeConstraints constraints;
1823 constraints.SetAllowRtpDataChannels();
1824 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001825 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001827 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 pc_->CreateDataChannel("test2", NULL);
1829 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001830 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001832 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 new MockDataChannelObserver(data2));
1834
1835 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1836 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1837
1838 CreateOfferReceiveAnswer();
1839 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1840 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1841
1842 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1843 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1844
jbaucheec21bd2016-03-20 06:15:43 -07001845 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1847}
1848
1849// This test setup a RTP data channels in loop back and test that a channel is
1850// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 FakeConstraints constraints;
1853 constraints.SetAllowRtpDataChannels();
1854 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001855 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001857 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 new MockDataChannelObserver(data1));
1859
1860 CreateOfferReceiveAnswerWithoutSsrc();
1861
1862 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1863
1864 data1->Close();
1865 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1866 CreateOfferReceiveAnswerWithoutSsrc();
1867 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1868 EXPECT_FALSE(observer1->IsOpen());
1869}
1870
1871// This test that if a data channel is added in an answer a receive only channel
1872// channel is created.
1873TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1874 FakeConstraints constraints;
1875 constraints.SetAllowRtpDataChannels();
1876 CreatePeerConnection(&constraints);
1877
1878 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001879 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 pc_->CreateDataChannel(offer_label, NULL);
1881
1882 CreateOfferAsLocalDescription();
1883
1884 // Replace the data channel label in the offer and apply it as an answer.
1885 std::string receive_label = "answer_channel";
1886 std::string sdp;
1887 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001888 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 receive_label.c_str(), receive_label.length(),
1890 &sdp);
1891 CreateAnswerAsRemoteDescription(sdp);
1892
1893 // Verify that a new incoming data channel has been created and that
1894 // it is open but can't we written to.
1895 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1896 DataChannelInterface* received_channel = observer_.last_datachannel_;
1897 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1898 EXPECT_EQ(receive_label, received_channel->label());
1899 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1900
1901 // Verify that the channel we initially offered has been rejected.
1902 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1903
1904 // Do another offer / answer exchange and verify that the data channel is
1905 // opened.
1906 CreateOfferReceiveAnswer();
1907 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1908 kTimeout);
1909}
1910
1911// This test that no data channel is returned if a reliable channel is
1912// requested.
1913// TODO(perkj): Remove this test once reliable channels are implemented.
1914TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1915 FakeConstraints constraints;
1916 constraints.SetAllowRtpDataChannels();
1917 CreatePeerConnection(&constraints);
1918
1919 std::string label = "test";
1920 webrtc::DataChannelInit config;
1921 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001922 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 pc_->CreateDataChannel(label, &config);
1924 EXPECT_TRUE(channel == NULL);
1925}
1926
deadbeefab9b2d12015-10-14 11:33:11 -07001927// Verifies that duplicated label is not allowed for RTP data channel.
1928TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1929 FakeConstraints constraints;
1930 constraints.SetAllowRtpDataChannels();
1931 CreatePeerConnection(&constraints);
1932
1933 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001934 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001935 pc_->CreateDataChannel(label, nullptr);
1936 EXPECT_NE(channel, nullptr);
1937
zhihuang9763d562016-08-05 11:14:50 -07001938 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001939 pc_->CreateDataChannel(label, nullptr);
1940 EXPECT_EQ(dup_channel, nullptr);
1941}
1942
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943// This tests that a SCTP data channel is returned using different
1944// DataChannelInit configurations.
1945TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1946 FakeConstraints constraints;
1947 constraints.SetAllowDtlsSctpDataChannels();
1948 CreatePeerConnection(&constraints);
1949
1950 webrtc::DataChannelInit config;
1951
zhihuang9763d562016-08-05 11:14:50 -07001952 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 pc_->CreateDataChannel("1", &config);
1954 EXPECT_TRUE(channel != NULL);
1955 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001956 EXPECT_TRUE(observer_.renegotiation_needed_);
1957 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958
1959 config.ordered = false;
1960 channel = pc_->CreateDataChannel("2", &config);
1961 EXPECT_TRUE(channel != NULL);
1962 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001963 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964
1965 config.ordered = true;
1966 config.maxRetransmits = 0;
1967 channel = pc_->CreateDataChannel("3", &config);
1968 EXPECT_TRUE(channel != NULL);
1969 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001970 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971
1972 config.maxRetransmits = -1;
1973 config.maxRetransmitTime = 0;
1974 channel = pc_->CreateDataChannel("4", &config);
1975 EXPECT_TRUE(channel != NULL);
1976 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001977 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978}
1979
1980// This tests that no data channel is returned if both maxRetransmits and
1981// maxRetransmitTime are set for SCTP data channels.
1982TEST_F(PeerConnectionInterfaceTest,
1983 CreateSctpDataChannelShouldFailForInvalidConfig) {
1984 FakeConstraints constraints;
1985 constraints.SetAllowDtlsSctpDataChannels();
1986 CreatePeerConnection(&constraints);
1987
1988 std::string label = "test";
1989 webrtc::DataChannelInit config;
1990 config.maxRetransmits = 0;
1991 config.maxRetransmitTime = 0;
1992
zhihuang9763d562016-08-05 11:14:50 -07001993 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 pc_->CreateDataChannel(label, &config);
1995 EXPECT_TRUE(channel == NULL);
1996}
1997
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998// The test verifies that creating a SCTP data channel with an id already in use
1999// or out of range should fail.
2000TEST_F(PeerConnectionInterfaceTest,
2001 CreateSctpDataChannelWithInvalidIdShouldFail) {
2002 FakeConstraints constraints;
2003 constraints.SetAllowDtlsSctpDataChannels();
2004 CreatePeerConnection(&constraints);
2005
2006 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002007 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002009 config.id = 1;
2010 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 EXPECT_TRUE(channel != NULL);
2012 EXPECT_EQ(1, channel->id());
2013
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 channel = pc_->CreateDataChannel("x", &config);
2015 EXPECT_TRUE(channel == NULL);
2016
2017 config.id = cricket::kMaxSctpSid;
2018 channel = pc_->CreateDataChannel("max", &config);
2019 EXPECT_TRUE(channel != NULL);
2020 EXPECT_EQ(config.id, channel->id());
2021
2022 config.id = cricket::kMaxSctpSid + 1;
2023 channel = pc_->CreateDataChannel("x", &config);
2024 EXPECT_TRUE(channel == NULL);
2025}
2026
deadbeefab9b2d12015-10-14 11:33:11 -07002027// Verifies that duplicated label is allowed for SCTP data channel.
2028TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2029 FakeConstraints constraints;
2030 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2031 true);
2032 CreatePeerConnection(&constraints);
2033
2034 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002035 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002036 pc_->CreateDataChannel(label, nullptr);
2037 EXPECT_NE(channel, nullptr);
2038
zhihuang9763d562016-08-05 11:14:50 -07002039 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002040 pc_->CreateDataChannel(label, nullptr);
2041 EXPECT_NE(dup_channel, nullptr);
2042}
2043
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002044// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2045// DataChannel.
2046TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2047 FakeConstraints constraints;
2048 constraints.SetAllowRtpDataChannels();
2049 CreatePeerConnection(&constraints);
2050
zhihuang9763d562016-08-05 11:14:50 -07002051 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002052 pc_->CreateDataChannel("test1", NULL);
2053 EXPECT_TRUE(observer_.renegotiation_needed_);
2054 observer_.renegotiation_needed_ = false;
2055
zhihuang9763d562016-08-05 11:14:50 -07002056 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002057 pc_->CreateDataChannel("test2", NULL);
2058 EXPECT_TRUE(observer_.renegotiation_needed_);
2059}
2060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 FakeConstraints constraints;
2064 constraints.SetAllowRtpDataChannels();
2065 CreatePeerConnection(&constraints);
2066
zhihuang9763d562016-08-05 11:14:50 -07002067 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002069 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 pc_->CreateDataChannel("test2", NULL);
2071 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002072 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002074 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 new MockDataChannelObserver(data2));
2076
2077 CreateOfferReceiveAnswer();
2078 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2079 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2080
2081 ReleasePeerConnection();
2082 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2083 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2084}
2085
2086// This test that data channels can be rejected in an answer.
2087TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2088 FakeConstraints constraints;
2089 constraints.SetAllowRtpDataChannels();
2090 CreatePeerConnection(&constraints);
2091
zhihuang9763d562016-08-05 11:14:50 -07002092 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 pc_->CreateDataChannel("offer_channel", NULL));
2094
2095 CreateOfferAsLocalDescription();
2096
2097 // Create an answer where the m-line for data channels are rejected.
2098 std::string sdp;
2099 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002100 std::unique_ptr<SessionDescriptionInterface> answer(
2101 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2102 sdp, nullptr));
2103 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 cricket::ContentInfo* data_info =
2105 answer->description()->GetContentByName("data");
2106 data_info->rejected = true;
2107
Steve Antondb45ca82017-09-11 18:27:34 -07002108 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2110}
2111
2112// Test that we can create a session description from an SDP string from
2113// FireFox, use it as a remote session description, generate an answer and use
2114// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002115TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 FakeConstraints constraints;
2117 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2118 true);
2119 CreatePeerConnection(&constraints);
2120 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002121 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002123 webrtc::kFireFoxSdpOffer, nullptr));
2124 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 CreateAnswerAsLocalDescription();
2126 ASSERT_TRUE(pc_->local_description() != NULL);
2127 ASSERT_TRUE(pc_->remote_description() != NULL);
2128
2129 const cricket::ContentInfo* content =
2130 cricket::GetFirstAudioContent(pc_->local_description()->description());
2131 ASSERT_TRUE(content != NULL);
2132 EXPECT_FALSE(content->rejected);
2133
2134 content =
2135 cricket::GetFirstVideoContent(pc_->local_description()->description());
2136 ASSERT_TRUE(content != NULL);
2137 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002138#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 content =
2140 cricket::GetFirstDataContent(pc_->local_description()->description());
2141 ASSERT_TRUE(content != NULL);
2142 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002143#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144}
2145
zhihuangb19012e2017-09-19 13:47:59 -07002146// Test that fallback from DTLS to SDES is not supported.
2147// The fallback was previously supported but was removed to simplify the code
2148// and because it's non-standard.
2149TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002150 FakeConstraints constraints;
2151 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2152 true);
2153 CreatePeerConnection(&constraints);
2154 // Wait for fake certificate to be generated. Previously, this is what caused
2155 // the "a=crypto" lines to be rejected.
2156 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2157 ASSERT_NE(nullptr, fake_certificate_generator_);
2158 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2159 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002160 std::unique_ptr<SessionDescriptionInterface> desc(
2161 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2162 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002163 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002164}
2165
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166// Test that we can create an audio only offer and receive an answer with a
2167// limited set of audio codecs and receive an updated offer with more audio
2168// codecs, where the added codecs are not supported.
2169TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002170 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 AddVoiceStream("audio_label");
2172 CreateOfferAsLocalDescription();
2173
Steve Antondb45ca82017-09-11 18:27:34 -07002174 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002176 webrtc::kAudioSdp, nullptr));
2177 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178
Steve Antondb45ca82017-09-11 18:27:34 -07002179 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002181 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002182 nullptr));
2183 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 CreateAnswerAsLocalDescription();
2185}
2186
deadbeefc80741f2015-10-22 13:14:45 -07002187// Test that if we're receiving (but not sending) a track, subsequent offers
2188// will have m-lines with a=recvonly.
2189TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2190 FakeConstraints constraints;
2191 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2192 true);
2193 CreatePeerConnection(&constraints);
2194 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2195 CreateAnswerAsLocalDescription();
2196
2197 // At this point we should be receiving stream 1, but not sending anything.
2198 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002199 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002200 DoCreateOffer(&offer, nullptr);
2201
2202 const cricket::ContentInfo* video_content =
2203 cricket::GetFirstVideoContent(offer->description());
2204 const cricket::VideoContentDescription* video_desc =
2205 static_cast<const cricket::VideoContentDescription*>(
2206 video_content->description);
Steve Anton4e70a722017-11-28 14:57:10 -08002207 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly, video_desc->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002208
2209 const cricket::ContentInfo* audio_content =
2210 cricket::GetFirstAudioContent(offer->description());
2211 const cricket::AudioContentDescription* audio_desc =
2212 static_cast<const cricket::AudioContentDescription*>(
2213 audio_content->description);
Steve Anton4e70a722017-11-28 14:57:10 -08002214 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly, audio_desc->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002215}
2216
2217// Test that if we're receiving (but not sending) a track, and the
2218// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2219// false, the generated m-lines will be a=inactive.
2220TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2221 FakeConstraints constraints;
2222 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2223 true);
2224 CreatePeerConnection(&constraints);
2225 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2226 CreateAnswerAsLocalDescription();
2227
2228 // At this point we should be receiving stream 1, but not sending anything.
2229 // A new offer would be recvonly, but we'll set the "no receive" constraints
2230 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002231 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002232 FakeConstraints offer_constraints;
2233 offer_constraints.AddMandatory(
2234 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2235 offer_constraints.AddMandatory(
2236 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2237 DoCreateOffer(&offer, &offer_constraints);
2238
2239 const cricket::ContentInfo* video_content =
2240 cricket::GetFirstVideoContent(offer->description());
2241 const cricket::VideoContentDescription* video_desc =
2242 static_cast<const cricket::VideoContentDescription*>(
2243 video_content->description);
Steve Anton4e70a722017-11-28 14:57:10 -08002244 ASSERT_EQ(RtpTransceiverDirection::kInactive, video_desc->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002245
2246 const cricket::ContentInfo* audio_content =
2247 cricket::GetFirstAudioContent(offer->description());
2248 const cricket::AudioContentDescription* audio_desc =
2249 static_cast<const cricket::AudioContentDescription*>(
2250 audio_content->description);
Steve Anton4e70a722017-11-28 14:57:10 -08002251 ASSERT_EQ(RtpTransceiverDirection::kInactive, audio_desc->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002252}
2253
deadbeef653b8e02015-11-11 12:55:10 -08002254// Test that we can use SetConfiguration to change the ICE servers of the
2255// PortAllocator.
2256TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2257 CreatePeerConnection();
2258
2259 PeerConnectionInterface::RTCConfiguration config;
2260 PeerConnectionInterface::IceServer server;
2261 server.uri = "stun:test_hostname";
2262 config.servers.push_back(server);
2263 EXPECT_TRUE(pc_->SetConfiguration(config));
2264
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002265 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2266 EXPECT_EQ("test_hostname",
2267 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002268}
2269
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002270TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2271 CreatePeerConnection();
2272 PeerConnectionInterface::RTCConfiguration config;
2273 config.type = PeerConnectionInterface::kRelay;
2274 EXPECT_TRUE(pc_->SetConfiguration(config));
2275 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2276}
2277
deadbeef293e9262017-01-11 12:28:30 -08002278TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2279 PeerConnectionInterface::RTCConfiguration config;
2280 config.prune_turn_ports = false;
2281 CreatePeerConnection(config, nullptr);
2282 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2283
2284 config.prune_turn_ports = true;
2285 EXPECT_TRUE(pc_->SetConfiguration(config));
2286 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2287}
2288
skvladd1f5fda2017-02-03 16:54:05 -08002289// Test that the ice check interval can be changed. This does not verify that
2290// the setting makes it all the way to P2PTransportChannel, as that would
2291// require a very complex set of mocks.
2292TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2293 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002294 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002295 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002296 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002297 EXPECT_TRUE(pc_->SetConfiguration(config));
2298 PeerConnectionInterface::RTCConfiguration new_config =
2299 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002300 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002301}
2302
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002303// Test that when SetConfiguration changes both the pool size and other
2304// attributes, the pooled session is created with the updated attributes.
2305TEST_F(PeerConnectionInterfaceTest,
2306 SetConfigurationCreatesPooledSessionCorrectly) {
2307 CreatePeerConnection();
2308 PeerConnectionInterface::RTCConfiguration config;
2309 config.ice_candidate_pool_size = 1;
2310 PeerConnectionInterface::IceServer server;
2311 server.uri = kStunAddressOnly;
2312 config.servers.push_back(server);
2313 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002314 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002315
2316 const cricket::FakePortAllocatorSession* session =
2317 static_cast<const cricket::FakePortAllocatorSession*>(
2318 port_allocator_->GetPooledSession());
2319 ASSERT_NE(nullptr, session);
2320 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002321}
2322
deadbeef293e9262017-01-11 12:28:30 -08002323// Test that after SetLocalDescription, changing the pool size is not allowed,
2324// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002325TEST_F(PeerConnectionInterfaceTest,
2326 CantChangePoolSizeAfterSetLocalDescription) {
2327 CreatePeerConnection();
2328 // Start by setting a size of 1.
2329 PeerConnectionInterface::RTCConfiguration config;
2330 config.ice_candidate_pool_size = 1;
2331 EXPECT_TRUE(pc_->SetConfiguration(config));
2332
2333 // Set remote offer; can still change pool size at this point.
2334 CreateOfferAsRemoteDescription();
2335 config.ice_candidate_pool_size = 2;
2336 EXPECT_TRUE(pc_->SetConfiguration(config));
2337
2338 // Set local answer; now it's too late.
2339 CreateAnswerAsLocalDescription();
2340 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002341 RTCError error;
2342 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2343 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2344}
2345
deadbeef42a42632017-03-10 15:18:00 -08002346// Test that after setting an answer, extra pooled sessions are discarded. The
2347// ICE candidate pool is only intended to be used for the first offer/answer.
2348TEST_F(PeerConnectionInterfaceTest,
2349 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2350 CreatePeerConnection();
2351
2352 // Set a larger-than-necessary size.
2353 PeerConnectionInterface::RTCConfiguration config;
2354 config.ice_candidate_pool_size = 4;
2355 EXPECT_TRUE(pc_->SetConfiguration(config));
2356
2357 // Do offer/answer.
2358 CreateOfferAsRemoteDescription();
2359 CreateAnswerAsLocalDescription();
2360
2361 // Expect no pooled sessions to be left.
2362 const cricket::PortAllocatorSession* session =
2363 port_allocator_->GetPooledSession();
2364 EXPECT_EQ(nullptr, session);
2365}
2366
2367// After Close is called, pooled candidates should be discarded so as to not
2368// waste network resources.
2369TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2370 CreatePeerConnection();
2371
2372 PeerConnectionInterface::RTCConfiguration config;
2373 config.ice_candidate_pool_size = 3;
2374 EXPECT_TRUE(pc_->SetConfiguration(config));
2375 pc_->Close();
2376
2377 // Expect no pooled sessions to be left.
2378 const cricket::PortAllocatorSession* session =
2379 port_allocator_->GetPooledSession();
2380 EXPECT_EQ(nullptr, session);
2381}
2382
deadbeef293e9262017-01-11 12:28:30 -08002383// Test that SetConfiguration returns an invalid modification error if
2384// modifying a field in the configuration that isn't allowed to be modified.
2385TEST_F(PeerConnectionInterfaceTest,
2386 SetConfigurationReturnsInvalidModificationError) {
2387 PeerConnectionInterface::RTCConfiguration config;
2388 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2389 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2390 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2391 CreatePeerConnection(config, nullptr);
2392
2393 PeerConnectionInterface::RTCConfiguration modified_config = config;
2394 modified_config.bundle_policy =
2395 PeerConnectionInterface::kBundlePolicyMaxBundle;
2396 RTCError error;
2397 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2398 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2399
2400 modified_config = config;
2401 modified_config.rtcp_mux_policy =
2402 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2403 error.set_type(RTCErrorType::NONE);
2404 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2405 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2406
2407 modified_config = config;
2408 modified_config.continual_gathering_policy =
2409 PeerConnectionInterface::GATHER_CONTINUALLY;
2410 error.set_type(RTCErrorType::NONE);
2411 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2412 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2413}
2414
2415// Test that SetConfiguration returns a range error if the candidate pool size
2416// is negative or larger than allowed by the spec.
2417TEST_F(PeerConnectionInterfaceTest,
2418 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2419 PeerConnectionInterface::RTCConfiguration config;
2420 CreatePeerConnection(config, nullptr);
2421
2422 config.ice_candidate_pool_size = -1;
2423 RTCError error;
2424 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2425 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2426
2427 config.ice_candidate_pool_size = INT_MAX;
2428 error.set_type(RTCErrorType::NONE);
2429 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2430 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2431}
2432
2433// Test that SetConfiguration returns a syntax error if parsing an ICE server
2434// URL failed.
2435TEST_F(PeerConnectionInterfaceTest,
2436 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2437 PeerConnectionInterface::RTCConfiguration config;
2438 CreatePeerConnection(config, nullptr);
2439
2440 PeerConnectionInterface::IceServer bad_server;
2441 bad_server.uri = "stunn:www.example.com";
2442 config.servers.push_back(bad_server);
2443 RTCError error;
2444 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2445 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2446}
2447
2448// Test that SetConfiguration returns an invalid parameter error if a TURN
2449// IceServer is missing a username or password.
2450TEST_F(PeerConnectionInterfaceTest,
2451 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2452 PeerConnectionInterface::RTCConfiguration config;
2453 CreatePeerConnection(config, nullptr);
2454
2455 PeerConnectionInterface::IceServer bad_server;
2456 bad_server.uri = "turn:www.example.com";
2457 // Missing password.
2458 bad_server.username = "foo";
2459 config.servers.push_back(bad_server);
2460 RTCError error;
2461 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2462 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002463}
2464
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465// Test that PeerConnection::Close changes the states to closed and all remote
2466// tracks change state to ended.
2467TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2468 // Initialize a PeerConnection and negotiate local and remote session
2469 // description.
2470 InitiateCall();
2471 ASSERT_EQ(1u, pc_->local_streams()->count());
2472 ASSERT_EQ(1u, pc_->remote_streams()->count());
2473
2474 pc_->Close();
2475
2476 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2477 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2478 pc_->ice_connection_state());
2479 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2480 pc_->ice_gathering_state());
2481
2482 EXPECT_EQ(1u, pc_->local_streams()->count());
2483 EXPECT_EQ(1u, pc_->remote_streams()->count());
2484
zhihuang9763d562016-08-05 11:14:50 -07002485 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2486 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002487 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002488 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002489 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2490 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2491 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492}
2493
2494// Test that PeerConnection methods fails gracefully after
2495// PeerConnection::Close has been called.
2496TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002497 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2499 CreateOfferAsRemoteDescription();
2500 CreateAnswerAsLocalDescription();
2501
2502 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002503 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504 pc_->local_streams()->at(0);
2505
2506 pc_->Close();
2507
2508 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002509 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510
2511 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002512 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002514 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002515
2516 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2517
2518 EXPECT_TRUE(pc_->local_description() != NULL);
2519 EXPECT_TRUE(pc_->remote_description() != NULL);
2520
kwibergd1fe2812016-04-27 06:47:29 -07002521 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002522 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002523 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002524 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525
2526 std::string sdp;
2527 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002528 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2529 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2530 nullptr));
2531 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532
2533 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002534 std::unique_ptr<SessionDescriptionInterface> local_offer(
2535 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2536 nullptr));
2537 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002538}
2539
2540// Test that GetStats can still be called after PeerConnection::Close.
2541TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2542 InitiateCall();
2543 pc_->Close();
2544 DoGetStats(NULL);
2545}
deadbeefab9b2d12015-10-14 11:33:11 -07002546
2547// NOTE: The series of tests below come from what used to be
2548// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2549// setting a remote or local description has the expected effects.
2550
2551// This test verifies that the remote MediaStreams corresponding to a received
2552// SDP string is created. In this test the two separate MediaStreams are
2553// signaled.
2554TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2555 FakeConstraints constraints;
2556 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2557 true);
2558 CreatePeerConnection(&constraints);
2559 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2560
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002561 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002562 EXPECT_TRUE(
2563 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2564 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2565 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2566
2567 // Create a session description based on another SDP with another
2568 // MediaStream.
2569 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2570
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002571 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002572 EXPECT_TRUE(
2573 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2574}
2575
2576// This test verifies that when remote tracks are added/removed from SDP, the
2577// created remote streams are updated appropriately.
2578TEST_F(PeerConnectionInterfaceTest,
2579 AddRemoveTrackFromExistingRemoteMediaStream) {
2580 FakeConstraints constraints;
2581 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2582 true);
2583 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002584 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002585 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002586 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002587 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2588 reference_collection_));
2589
2590 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002592 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002593 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002594 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2595 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002596 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002597 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2598 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002599 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002600 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2601 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002602
2603 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002604 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002605 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002606 MockTrackObserver audio_track_observer(audio_track2);
2607 MockTrackObserver video_track_observer(video_track2);
2608
2609 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2610 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002611 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002612 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2613 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002614 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002615 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002616 audio_track2->state(), kTimeout);
2617 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2618 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002619}
2620
2621// This tests that remote tracks are ended if a local session description is set
2622// that rejects the media content type.
2623TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2624 FakeConstraints constraints;
2625 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2626 true);
2627 CreatePeerConnection(&constraints);
2628 // First create and set a remote offer, then reject its video content in our
2629 // answer.
2630 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2631 ASSERT_EQ(1u, observer_.remote_streams()->count());
2632 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2633 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2634 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2635
2636 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2637 remote_stream->GetVideoTracks()[0];
2638 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2639 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2640 remote_stream->GetAudioTracks()[0];
2641 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2642
kwibergd1fe2812016-04-27 06:47:29 -07002643 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002644 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002645 cricket::ContentInfo* video_info =
2646 local_answer->description()->GetContentByName("video");
2647 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002648 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002649 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2650 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2651
2652 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002653 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002654 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002655 video_info = local_offer->description()->GetContentByName("video");
2656 ASSERT_TRUE(video_info != nullptr);
2657 video_info->rejected = true;
2658 cricket::ContentInfo* audio_info =
2659 local_offer->description()->GetContentByName("audio");
2660 ASSERT_TRUE(audio_info != nullptr);
2661 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002662 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002663 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002664 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002665 remote_audio->state(), kTimeout);
2666 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2667 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002668}
2669
2670// This tests that we won't crash if the remote track has been removed outside
2671// of PeerConnection and then PeerConnection tries to reject the track.
2672TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2673 FakeConstraints constraints;
2674 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2675 true);
2676 CreatePeerConnection(&constraints);
2677 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2678 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2679 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2680 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2681
kwibergd1fe2812016-04-27 06:47:29 -07002682 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002683 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2684 kSdpStringWithStream1, nullptr));
2685 cricket::ContentInfo* video_info =
2686 local_answer->description()->GetContentByName("video");
2687 video_info->rejected = true;
2688 cricket::ContentInfo* audio_info =
2689 local_answer->description()->GetContentByName("audio");
2690 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002691 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002692
2693 // No crash is a pass.
2694}
2695
deadbeef5e97fb52015-10-15 12:49:08 -07002696// This tests that if a recvonly remote description is set, no remote streams
2697// will be created, even if the description contains SSRCs/MSIDs.
2698// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2699TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2700 FakeConstraints constraints;
2701 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2702 true);
2703 CreatePeerConnection(&constraints);
2704
2705 std::string recvonly_offer = kSdpStringWithStream1;
2706 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2707 strlen(kRecvonly), &recvonly_offer);
2708 CreateAndSetRemoteOffer(recvonly_offer);
2709
2710 EXPECT_EQ(0u, observer_.remote_streams()->count());
2711}
2712
deadbeefab9b2d12015-10-14 11:33:11 -07002713// This tests that a default MediaStream is created if a remote session
2714// description doesn't contain any streams and no MSID support.
2715// It also tests that the default stream is updated if a video m-line is added
2716// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002717TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002718 FakeConstraints constraints;
2719 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2720 true);
2721 CreatePeerConnection(&constraints);
2722 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2723
2724 ASSERT_EQ(1u, observer_.remote_streams()->count());
2725 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2726
2727 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2728 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2729 EXPECT_EQ("default", remote_stream->label());
2730
2731 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2732 ASSERT_EQ(1u, observer_.remote_streams()->count());
2733 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2734 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002735 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2736 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002737 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2738 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002739 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2740 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002741}
2742
2743// This tests that a default MediaStream is created if a remote session
2744// description doesn't contain any streams and media direction is send only.
2745TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002746 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002747 FakeConstraints constraints;
2748 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2749 true);
2750 CreatePeerConnection(&constraints);
2751 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2752
2753 ASSERT_EQ(1u, observer_.remote_streams()->count());
2754 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2755
2756 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2757 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2758 EXPECT_EQ("default", remote_stream->label());
2759}
2760
2761// This tests that it won't crash when PeerConnection tries to remove
2762// a remote track that as already been removed from the MediaStream.
2763TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2764 FakeConstraints constraints;
2765 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2766 true);
2767 CreatePeerConnection(&constraints);
2768 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2769 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2770 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2771 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2772
2773 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2774
2775 // No crash is a pass.
2776}
2777
2778// This tests that a default MediaStream is created if the remote session
2779// description doesn't contain any streams and don't contain an indication if
2780// MSID is supported.
2781TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002782 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002783 FakeConstraints constraints;
2784 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2785 true);
2786 CreatePeerConnection(&constraints);
2787 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2788
2789 ASSERT_EQ(1u, observer_.remote_streams()->count());
2790 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2791 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2792 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2793}
2794
2795// This tests that a default MediaStream is not created if the remote session
2796// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002797TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002798 FakeConstraints constraints;
2799 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2800 true);
2801 CreatePeerConnection(&constraints);
2802 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2803 EXPECT_EQ(0u, observer_.remote_streams()->count());
2804}
2805
deadbeefbda7e0b2015-12-08 17:13:40 -08002806// This tests that when setting a new description, the old default tracks are
2807// not destroyed and recreated.
2808// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002809TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002810 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002811 FakeConstraints constraints;
2812 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2813 true);
2814 CreatePeerConnection(&constraints);
2815 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2816
2817 ASSERT_EQ(1u, observer_.remote_streams()->count());
2818 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2819 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2820
2821 // Set the track to "disabled", then set a new description and ensure the
2822 // track is still disabled, which ensures it hasn't been recreated.
2823 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2824 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2825 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2826 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2827}
2828
deadbeefab9b2d12015-10-14 11:33:11 -07002829// This tests that a default MediaStream is not created if a remote session
2830// description is updated to not have any MediaStreams.
2831TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2832 FakeConstraints constraints;
2833 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2834 true);
2835 CreatePeerConnection(&constraints);
2836 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002837 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002838 EXPECT_TRUE(
2839 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2840
2841 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2842 EXPECT_EQ(0u, observer_.remote_streams()->count());
2843}
2844
2845// This tests that an RtpSender is created when the local description is set
2846// after adding a local stream.
2847// TODO(deadbeef): This test and the one below it need to be updated when
2848// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002849TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002850 FakeConstraints constraints;
2851 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2852 true);
2853 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002854
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002855 // Create an offer with 1 stream with 2 tracks of each type.
2856 rtc::scoped_refptr<StreamCollection> stream_collection =
2857 CreateStreamCollection(1, 2);
2858 pc_->AddStream(stream_collection->at(0));
2859 std::unique_ptr<SessionDescriptionInterface> offer;
2860 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002861 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002862
deadbeefab9b2d12015-10-14 11:33:11 -07002863 auto senders = pc_->GetSenders();
2864 EXPECT_EQ(4u, senders.size());
2865 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2866 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2867 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2868 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2869
2870 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002871 pc_->RemoveStream(stream_collection->at(0));
2872 stream_collection = CreateStreamCollection(1, 1);
2873 pc_->AddStream(stream_collection->at(0));
2874 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002875 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002876
deadbeefab9b2d12015-10-14 11:33:11 -07002877 senders = pc_->GetSenders();
2878 EXPECT_EQ(2u, senders.size());
2879 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2880 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2881 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2882 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2883}
2884
2885// This tests that an RtpSender is created when the local description is set
2886// before adding a local stream.
2887TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002888 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002889 FakeConstraints constraints;
2890 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2891 true);
2892 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002893
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002894 rtc::scoped_refptr<StreamCollection> stream_collection =
2895 CreateStreamCollection(1, 2);
2896 // Add a stream to create the offer, but remove it afterwards.
2897 pc_->AddStream(stream_collection->at(0));
2898 std::unique_ptr<SessionDescriptionInterface> offer;
2899 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2900 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002901
Steve Antondb45ca82017-09-11 18:27:34 -07002902 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002903 auto senders = pc_->GetSenders();
2904 EXPECT_EQ(0u, senders.size());
2905
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002906 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002907 senders = pc_->GetSenders();
2908 EXPECT_EQ(4u, senders.size());
2909 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2910 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2911 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2912 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2913}
2914
2915// This tests that the expected behavior occurs if the SSRC on a local track is
2916// changed when SetLocalDescription is called.
2917TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002918 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002919 FakeConstraints constraints;
2920 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2921 true);
2922 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002923
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002924 rtc::scoped_refptr<StreamCollection> stream_collection =
2925 CreateStreamCollection(2, 1);
2926 pc_->AddStream(stream_collection->at(0));
2927 std::unique_ptr<SessionDescriptionInterface> offer;
2928 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2929 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002930 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2931 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002932 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2933 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002934 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002935
deadbeefab9b2d12015-10-14 11:33:11 -07002936 auto senders = pc_->GetSenders();
2937 EXPECT_EQ(2u, senders.size());
2938 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2939 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2940
2941 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 cricket::MediaContentDescription* desc =
2943 cricket::GetFirstAudioContentDescription(modified_offer->description());
2944 ASSERT_TRUE(desc != NULL);
2945 for (StreamParams& stream : desc->mutable_streams()) {
2946 for (unsigned int& ssrc : stream.ssrcs) {
2947 ++ssrc;
2948 }
2949 }
deadbeefab9b2d12015-10-14 11:33:11 -07002950
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002951 desc =
2952 cricket::GetFirstVideoContentDescription(modified_offer->description());
2953 ASSERT_TRUE(desc != NULL);
2954 for (StreamParams& stream : desc->mutable_streams()) {
2955 for (unsigned int& ssrc : stream.ssrcs) {
2956 ++ssrc;
2957 }
2958 }
2959
Steve Antondb45ca82017-09-11 18:27:34 -07002960 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002961 senders = pc_->GetSenders();
2962 EXPECT_EQ(2u, senders.size());
2963 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2964 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2965 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2966 // changed.
2967}
2968
2969// This tests that the expected behavior occurs if a new session description is
2970// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002971TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002972 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002973 FakeConstraints constraints;
2974 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2975 true);
2976 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002977
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 rtc::scoped_refptr<StreamCollection> stream_collection =
2979 CreateStreamCollection(2, 1);
2980 pc_->AddStream(stream_collection->at(0));
2981 std::unique_ptr<SessionDescriptionInterface> offer;
2982 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002983 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002984
deadbeefab9b2d12015-10-14 11:33:11 -07002985 auto senders = pc_->GetSenders();
2986 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002987 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2988 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002989
2990 // Add a new MediaStream but with the same tracks as in the first stream.
2991 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2992 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002993 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2994 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002995 pc_->AddStream(stream_1);
2996
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002997 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002998 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002999
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003000 auto new_senders = pc_->GetSenders();
3001 // Should be the same senders as before, but with updated stream id.
3002 // Note that this behavior is subject to change in the future.
3003 // We may decide the PC should ignore existing tracks in AddStream.
3004 EXPECT_EQ(senders, new_senders);
3005 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3006 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003007}
3008
zhihuang81c3a032016-11-17 12:06:24 -08003009// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3010TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3011 FakeConstraints constraints;
3012 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3013 true);
3014 CreatePeerConnection(&constraints);
3015 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3016 EXPECT_EQ(observer_.num_added_tracks_, 1);
3017 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3018
3019 // Create and set the updated remote SDP.
3020 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3021 EXPECT_EQ(observer_.num_added_tracks_, 2);
3022 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3023}
3024
deadbeefd1a38b52016-12-10 13:15:33 -08003025// Test that when SetConfiguration is called and the configuration is
3026// changing, the next offer causes an ICE restart.
3027TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3028 PeerConnectionInterface::RTCConfiguration config;
3029 config.type = PeerConnectionInterface::kRelay;
3030 // Need to pass default constraints to prevent disabling of DTLS...
3031 FakeConstraints default_constraints;
3032 CreatePeerConnection(config, &default_constraints);
3033 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3034
3035 // Do initial offer/answer so there's something to restart.
3036 CreateOfferAsLocalDescription();
3037 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3038
3039 // Grab the ufrags.
3040 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3041
3042 // Change ICE policy, which should trigger an ICE restart on the next offer.
3043 config.type = PeerConnectionInterface::kAll;
3044 EXPECT_TRUE(pc_->SetConfiguration(config));
3045 CreateOfferAsLocalDescription();
3046
3047 // Grab the new ufrags.
3048 std::vector<std::string> subsequent_ufrags =
3049 GetUfrags(pc_->local_description());
3050
3051 // Sanity check.
3052 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3053 // Check that each ufrag is different.
3054 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3055 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3056 }
3057}
3058
3059// Test that when SetConfiguration is called and the configuration *isn't*
3060// changing, the next offer does *not* cause an ICE restart.
3061TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3062 PeerConnectionInterface::RTCConfiguration config;
3063 config.type = PeerConnectionInterface::kRelay;
3064 // Need to pass default constraints to prevent disabling of DTLS...
3065 FakeConstraints default_constraints;
3066 CreatePeerConnection(config, &default_constraints);
3067 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3068
3069 // Do initial offer/answer so there's something to restart.
3070 CreateOfferAsLocalDescription();
3071 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3072
3073 // Grab the ufrags.
3074 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3075
3076 // Call SetConfiguration with a config identical to what the PC was
3077 // constructed with.
3078 EXPECT_TRUE(pc_->SetConfiguration(config));
3079 CreateOfferAsLocalDescription();
3080
3081 // Grab the new ufrags.
3082 std::vector<std::string> subsequent_ufrags =
3083 GetUfrags(pc_->local_description());
3084
3085 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3086}
3087
3088// Test for a weird corner case scenario:
3089// 1. Audio/video session established.
3090// 2. SetConfiguration changes ICE config; ICE restart needed.
3091// 3. ICE restart initiated by remote peer, but only for one m= section.
3092// 4. Next createOffer should initiate an ICE restart, but only for the other
3093// m= section; it would be pointless to do an ICE restart for the m= section
3094// that was already restarted.
3095TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3096 PeerConnectionInterface::RTCConfiguration config;
3097 config.type = PeerConnectionInterface::kRelay;
3098 // Need to pass default constraints to prevent disabling of DTLS...
3099 FakeConstraints default_constraints;
3100 CreatePeerConnection(config, &default_constraints);
3101 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3102
3103 // Do initial offer/answer so there's something to restart.
3104 CreateOfferAsLocalDescription();
3105 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3106
3107 // Change ICE policy, which should set the "needs-ice-restart" flag.
3108 config.type = PeerConnectionInterface::kAll;
3109 EXPECT_TRUE(pc_->SetConfiguration(config));
3110
3111 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003112 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3113 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3114 kSdpStringWithStream1, nullptr));
3115 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003116 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3117 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003118 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003119 CreateAnswerAsLocalDescription();
3120
3121 // Grab the ufrags.
3122 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3123 ASSERT_EQ(2, initial_ufrags.size());
3124
3125 // Create offer and grab the new ufrags.
3126 CreateOfferAsLocalDescription();
3127 std::vector<std::string> subsequent_ufrags =
3128 GetUfrags(pc_->local_description());
3129 ASSERT_EQ(2, subsequent_ufrags.size());
3130
3131 // Ensure that only the ufrag for the second m= section changed.
3132 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3133 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3134}
3135
deadbeeffe4a8a42016-12-20 17:56:17 -08003136// Tests that the methods to return current/pending descriptions work as
3137// expected at different points in the offer/answer exchange. This test does
3138// one offer/answer exchange as the offerer, then another as the answerer.
3139TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3140 // This disables DTLS so we can apply an answer to ourselves.
3141 CreatePeerConnection();
3142
3143 // Create initial local offer and get SDP (which will also be used as
3144 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003145 std::unique_ptr<SessionDescriptionInterface> local_offer;
3146 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003147 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003148 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003149
3150 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003151 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3152 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3153 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003154 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3155 EXPECT_EQ(nullptr, pc_->current_local_description());
3156 EXPECT_EQ(nullptr, pc_->current_remote_description());
3157
3158 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003159 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003160 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003161 sdp, nullptr));
3162 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3163 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3164 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3165 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003166 EXPECT_EQ(nullptr, pc_->current_local_description());
3167 EXPECT_EQ(nullptr, pc_->current_remote_description());
3168
3169 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003170 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3171 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3172 sdp, nullptr));
3173 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3174 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003175 EXPECT_EQ(nullptr, pc_->pending_local_description());
3176 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003177 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3178 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003179
3180 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003181 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3182 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3183 nullptr));
3184 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3185 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3186 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003187 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003188 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3189 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003190
3191 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003192 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003193 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003194 sdp, nullptr));
3195 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3196 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3197 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3198 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3199 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3200 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003201
3202 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003203 std::unique_ptr<SessionDescriptionInterface> local_answer(
3204 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3205 sdp, nullptr));
3206 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3207 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003208 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3209 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003210 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3211 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003212}
3213
zhihuang77985012017-02-07 15:45:16 -08003214// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3215// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003216// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003217TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003218 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003219 CreatePeerConnection();
3220 // The RtcEventLog will be reset when the PeerConnection is closed.
3221 pc_->Close();
3222
Elad Alon9e6565b2017-10-11 16:04:13 +02003223 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3224 std::string filename = webrtc::test::OutputPath() +
3225 test_info->test_case_name() + test_info->name();
3226 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3227
3228 constexpr int64_t max_size_bytes = 1024;
3229
zhihuang77985012017-02-07 15:45:16 -08003230 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3231 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003232
3233 // Cleanup.
3234 rtc::ClosePlatformFile(file);
3235 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003236}
3237
Elad Alon99c3fe52017-10-13 16:29:40 +02003238// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3239// after the PeerConnection is closed.
3240// This version tests the StartRtcEventLog version that receives an object
3241// of type |RtcEventLogOutput|.
3242TEST_F(PeerConnectionInterfaceTest,
3243 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3244 CreatePeerConnection();
3245 // The RtcEventLog will be reset when the PeerConnection is closed.
3246 pc_->Close();
3247
3248 rtc::PlatformFile file = 0;
3249 int64_t max_size_bytes = 1024;
3250 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003251 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3252 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003253 pc_->StopRtcEventLog();
3254}
3255
deadbeef30952b42017-04-21 02:41:29 -07003256// Test that generated offers/answers include "ice-option:trickle".
3257TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3258 CreatePeerConnection();
3259
3260 // First, create an offer with audio/video.
3261 FakeConstraints constraints;
3262 constraints.SetMandatoryReceiveAudio(true);
3263 constraints.SetMandatoryReceiveVideo(true);
3264 std::unique_ptr<SessionDescriptionInterface> offer;
3265 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3266 cricket::SessionDescription* desc = offer->description();
3267 ASSERT_EQ(2u, desc->transport_infos().size());
3268 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3269 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3270
3271 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003272 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003273 std::unique_ptr<SessionDescriptionInterface> answer;
3274 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3275 desc = answer->description();
3276 ASSERT_EQ(2u, desc->transport_infos().size());
3277 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3278 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3279}
3280
deadbeef1dcb1642017-03-29 21:08:16 -07003281// Test that ICE renomination isn't offered if it's not enabled in the PC's
3282// RTCConfiguration.
3283TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3284 PeerConnectionInterface::RTCConfiguration config;
3285 config.enable_ice_renomination = false;
3286 CreatePeerConnection(config, nullptr);
3287 AddVoiceStream("foo");
3288
3289 std::unique_ptr<SessionDescriptionInterface> offer;
3290 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3291 cricket::SessionDescription* desc = offer->description();
3292 EXPECT_EQ(1u, desc->transport_infos().size());
3293 EXPECT_FALSE(
3294 desc->transport_infos()[0].description.GetIceParameters().renomination);
3295}
3296
3297// Test that the ICE renomination option is present in generated offers/answers
3298// if it's enabled in the PC's RTCConfiguration.
3299TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3300 PeerConnectionInterface::RTCConfiguration config;
3301 config.enable_ice_renomination = true;
3302 CreatePeerConnection(config, nullptr);
3303 AddVoiceStream("foo");
3304
3305 std::unique_ptr<SessionDescriptionInterface> offer;
3306 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3307 cricket::SessionDescription* desc = offer->description();
3308 EXPECT_EQ(1u, desc->transport_infos().size());
3309 EXPECT_TRUE(
3310 desc->transport_infos()[0].description.GetIceParameters().renomination);
3311
3312 // Set the offer as a remote description, then create an answer and ensure it
3313 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003314 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003315 std::unique_ptr<SessionDescriptionInterface> answer;
3316 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3317 desc = answer->description();
3318 EXPECT_EQ(1u, desc->transport_infos().size());
3319 EXPECT_TRUE(
3320 desc->transport_infos()[0].description.GetIceParameters().renomination);
3321}
3322
3323// Test that if CreateOffer is called with the deprecated "offer to receive
3324// audio/video" constraints, they're processed and result in an offer with
3325// audio/video sections just as if RTCOfferAnswerOptions had been used.
3326TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3327 CreatePeerConnection();
3328
3329 FakeConstraints constraints;
3330 constraints.SetMandatoryReceiveAudio(true);
3331 constraints.SetMandatoryReceiveVideo(true);
3332 std::unique_ptr<SessionDescriptionInterface> offer;
3333 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3334
3335 cricket::SessionDescription* desc = offer->description();
3336 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3337 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3338 ASSERT_NE(nullptr, audio);
3339 ASSERT_NE(nullptr, video);
3340 EXPECT_FALSE(audio->rejected);
3341 EXPECT_FALSE(video->rejected);
3342}
3343
3344// Test that if CreateAnswer is called with the deprecated "offer to receive
3345// audio/video" constraints, they're processed and can be used to reject an
3346// offered m= section just as can be done with RTCOfferAnswerOptions;
3347TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3348 CreatePeerConnection();
3349
3350 // First, create an offer with audio/video and apply it as a remote
3351 // description.
3352 FakeConstraints constraints;
3353 constraints.SetMandatoryReceiveAudio(true);
3354 constraints.SetMandatoryReceiveVideo(true);
3355 std::unique_ptr<SessionDescriptionInterface> offer;
3356 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003357 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003358
3359 // Now create answer that rejects audio/video.
3360 constraints.SetMandatoryReceiveAudio(false);
3361 constraints.SetMandatoryReceiveVideo(false);
3362 std::unique_ptr<SessionDescriptionInterface> answer;
3363 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3364
3365 cricket::SessionDescription* desc = answer->description();
3366 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3367 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3368 ASSERT_NE(nullptr, audio);
3369 ASSERT_NE(nullptr, video);
3370 EXPECT_TRUE(audio->rejected);
3371 EXPECT_TRUE(video->rejected);
3372}
3373
3374#ifdef HAVE_SCTP
3375#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3376 DataChannelOnlyOfferWithMaxBundlePolicy
3377#else
3378#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3379 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3380#endif
3381
3382// Test that negotiation can succeed with a data channel only, and with the max
3383// bundle policy. Previously there was a bug that prevented this.
3384TEST_F(PeerConnectionInterfaceTest,
3385 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3386 PeerConnectionInterface::RTCConfiguration config;
3387 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3388 CreatePeerConnection(config, nullptr);
3389
3390 // First, create an offer with only a data channel and apply it as a remote
3391 // description.
3392 pc_->CreateDataChannel("test", nullptr);
3393 std::unique_ptr<SessionDescriptionInterface> offer;
3394 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003395 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003396
3397 // Create and set answer as well.
3398 std::unique_ptr<SessionDescriptionInterface> answer;
3399 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003400 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003401}
3402
zstein4b979802017-06-02 14:37:37 -07003403TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3404 CreatePeerConnection();
3405 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003406 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003407 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3408}
3409
3410TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3411 CreatePeerConnection();
3412 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003413 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003414 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3415}
3416
3417TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3418 CreatePeerConnection();
3419 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003420 bitrate.min_bitrate_bps = 5;
3421 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003422 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3423}
3424
3425TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3426 CreatePeerConnection();
3427 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003428 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003429 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3430}
3431
3432TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3433 CreatePeerConnection();
3434 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003435 bitrate.current_bitrate_bps = 10;
3436 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003437 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3438}
3439
3440TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3441 CreatePeerConnection();
3442 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003443 bitrate.min_bitrate_bps = 10;
3444 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003445 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3446}
3447
3448TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3449 CreatePeerConnection();
3450 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003451 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003452 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3453}
3454
Steve Anton038834f2017-07-14 15:59:59 -07003455// ice_regather_interval_range requires WebRTC to be configured for continual
3456// gathering already.
3457TEST_F(PeerConnectionInterfaceTest,
3458 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3459 PeerConnectionInterface::RTCConfiguration config;
3460 config.ice_regather_interval_range.emplace(1000, 2000);
3461 config.continual_gathering_policy =
3462 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3463 CreatePeerConnectionExpectFail(config);
3464}
3465
3466// Ensures that there is no error when ice_regather_interval_range is set with
3467// continual gathering enabled.
3468TEST_F(PeerConnectionInterfaceTest,
3469 SetIceRegatherIntervalRangeWithContinualGathering) {
3470 PeerConnectionInterface::RTCConfiguration config;
3471 config.ice_regather_interval_range.emplace(1000, 2000);
3472 config.continual_gathering_policy =
3473 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3474 CreatePeerConnection(config, nullptr);
3475}
3476
zstein4b979802017-06-02 14:37:37 -07003477// The current bitrate from Call's BitrateConfigMask is currently clamped by
3478// Call's BitrateConfig, which comes from the SDP or a default value. This test
3479// checks that a call to SetBitrate with a current bitrate that will be clamped
3480// succeeds.
3481TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3482 CreatePeerConnection();
3483 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003484 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003485 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3486}
3487
zhihuang1c378ed2017-08-17 14:10:50 -07003488// The following tests verify that the offer can be created correctly.
3489TEST_F(PeerConnectionInterfaceTest,
3490 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3491 RTCOfferAnswerOptions rtc_options;
3492
3493 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3494 // than kMaxOfferToReceiveMedia should be treated as invalid.
3495 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3496 CreatePeerConnection();
3497 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3498
3499 rtc_options.offer_to_receive_audio =
3500 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3501 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3502}
3503
3504TEST_F(PeerConnectionInterfaceTest,
3505 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3506 RTCOfferAnswerOptions rtc_options;
3507
3508 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3509 // than kMaxOfferToReceiveMedia should be treated as invalid.
3510 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3511 CreatePeerConnection();
3512 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3513
3514 rtc_options.offer_to_receive_video =
3515 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3516 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3517}
3518
3519// Test that the audio and video content will be added to an offer if both
3520// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3521TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3522 RTCOfferAnswerOptions rtc_options;
3523 rtc_options.offer_to_receive_audio = 1;
3524 rtc_options.offer_to_receive_video = 1;
3525
3526 std::unique_ptr<SessionDescriptionInterface> offer;
3527 CreatePeerConnection();
3528 offer = CreateOfferWithOptions(rtc_options);
3529 ASSERT_TRUE(offer);
3530 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3531 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3532}
3533
3534// Test that only audio content will be added to the offer if only
3535// |offer_to_receive_audio| options is 1.
3536TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3537 RTCOfferAnswerOptions rtc_options;
3538 rtc_options.offer_to_receive_audio = 1;
3539 rtc_options.offer_to_receive_video = 0;
3540
3541 std::unique_ptr<SessionDescriptionInterface> offer;
3542 CreatePeerConnection();
3543 offer = CreateOfferWithOptions(rtc_options);
3544 ASSERT_TRUE(offer);
3545 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3546 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3547}
3548
3549// Test that only video content will be added if only |offer_to_receive_video|
3550// options is 1.
3551TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3552 RTCOfferAnswerOptions rtc_options;
3553 rtc_options.offer_to_receive_audio = 0;
3554 rtc_options.offer_to_receive_video = 1;
3555
3556 std::unique_ptr<SessionDescriptionInterface> offer;
3557 CreatePeerConnection();
3558 offer = CreateOfferWithOptions(rtc_options);
3559 ASSERT_TRUE(offer);
3560 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3561 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3562}
3563
zhihuang1c378ed2017-08-17 14:10:50 -07003564// Test that no media content will be added to the offer if using default
3565// RTCOfferAnswerOptions.
3566TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3567 RTCOfferAnswerOptions rtc_options;
3568
3569 std::unique_ptr<SessionDescriptionInterface> offer;
3570 CreatePeerConnection();
3571 offer = CreateOfferWithOptions(rtc_options);
3572 ASSERT_TRUE(offer);
3573 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3574 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3575}
3576
3577// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3578// ufrag/pwd will be the same in the new offer.
3579TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3580 RTCOfferAnswerOptions rtc_options;
3581 rtc_options.ice_restart = false;
3582 rtc_options.offer_to_receive_audio = 1;
3583
3584 std::unique_ptr<SessionDescriptionInterface> offer;
3585 CreatePeerConnection();
3586 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3587 auto ufrag1 = offer->description()
3588 ->GetTransportInfoByName(cricket::CN_AUDIO)
3589 ->description.ice_ufrag;
3590 auto pwd1 = offer->description()
3591 ->GetTransportInfoByName(cricket::CN_AUDIO)
3592 ->description.ice_pwd;
3593
3594 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3595 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3596 auto ufrag2 = offer->description()
3597 ->GetTransportInfoByName(cricket::CN_AUDIO)
3598 ->description.ice_ufrag;
3599 auto pwd2 = offer->description()
3600 ->GetTransportInfoByName(cricket::CN_AUDIO)
3601 ->description.ice_pwd;
3602
3603 // |ice_restart| is true, the ufrag/pwd should change.
3604 rtc_options.ice_restart = true;
3605 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3606 auto ufrag3 = offer->description()
3607 ->GetTransportInfoByName(cricket::CN_AUDIO)
3608 ->description.ice_ufrag;
3609 auto pwd3 = offer->description()
3610 ->GetTransportInfoByName(cricket::CN_AUDIO)
3611 ->description.ice_pwd;
3612
3613 EXPECT_EQ(ufrag1, ufrag2);
3614 EXPECT_EQ(pwd1, pwd2);
3615 EXPECT_NE(ufrag2, ufrag3);
3616 EXPECT_NE(pwd2, pwd3);
3617}
3618
3619// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3620// offer; if it is false, there won't be any bundle group in the offer.
3621TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3622 RTCOfferAnswerOptions rtc_options;
3623 rtc_options.offer_to_receive_audio = 1;
3624 rtc_options.offer_to_receive_video = 1;
3625
3626 std::unique_ptr<SessionDescriptionInterface> offer;
3627 CreatePeerConnection();
3628
3629 rtc_options.use_rtp_mux = true;
3630 offer = CreateOfferWithOptions(rtc_options);
3631 ASSERT_TRUE(offer);
3632 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3633 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3634 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3635
3636 rtc_options.use_rtp_mux = false;
3637 offer = CreateOfferWithOptions(rtc_options);
3638 ASSERT_TRUE(offer);
3639 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3640 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3641 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3642}
3643
zhihuang141aacb2017-08-29 13:23:53 -07003644// This test ensures OnRenegotiationNeeded is called when we add track with
3645// MediaStream -> AddTrack in the same way it is called when we add track with
3646// PeerConnection -> AddTrack.
3647// The test can be removed once addStream is rewritten in terms of addTrack
3648// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3649TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3650 CreatePeerConnectionWithoutDtls();
3651 rtc::scoped_refptr<MediaStreamInterface> stream(
3652 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3653 pc_->AddStream(stream);
3654 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3655 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3656 rtc::scoped_refptr<VideoTrackInterface> video_track(
3657 pc_factory_->CreateVideoTrack(
3658 "video_track", pc_factory_->CreateVideoSource(
3659 std::unique_ptr<cricket::VideoCapturer>(
3660 new cricket::FakeVideoCapturer()))));
3661 stream->AddTrack(audio_track);
3662 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3663 observer_.renegotiation_needed_ = false;
3664
3665 stream->AddTrack(video_track);
3666 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3667 observer_.renegotiation_needed_ = false;
3668
3669 stream->RemoveTrack(audio_track);
3670 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3671 observer_.renegotiation_needed_ = false;
3672
3673 stream->RemoveTrack(video_track);
3674 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3675 observer_.renegotiation_needed_ = false;
3676}
3677
Zhi Huangb2d355e2017-10-26 17:26:37 -07003678// Tests that an error is returned if a description is applied that has fewer
3679// media sections than the existing description.
3680TEST_F(PeerConnectionInterfaceTest,
3681 MediaSectionCountEnforcedForSubsequentOffer) {
3682 CreatePeerConnection();
3683 FakeConstraints constraints;
3684 constraints.SetMandatoryReceiveAudio(true);
3685 constraints.SetMandatoryReceiveVideo(true);
3686 std::unique_ptr<SessionDescriptionInterface> offer;
3687 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3688 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3689
3690 // A remote offer with fewer media sections should be rejected.
3691 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3692 offer->description()->contents().pop_back();
3693 offer->description()->contents().pop_back();
3694 ASSERT_TRUE(offer->description()->contents().empty());
3695 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3696
3697 std::unique_ptr<SessionDescriptionInterface> answer;
3698 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3699 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3700
3701 // A subsequent local offer with fewer media sections should be rejected.
3702 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3703 offer->description()->contents().pop_back();
3704 offer->description()->contents().pop_back();
3705 ASSERT_TRUE(offer->description()->contents().empty());
3706 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3707}
3708
nisse51542be2016-02-12 02:27:06 -08003709class PeerConnectionMediaConfigTest : public testing::Test {
3710 protected:
3711 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003712 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003713 pcf_->Initialize();
3714 }
nisseeaabdf62017-05-05 02:23:02 -07003715 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003716 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003717 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003718 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3719 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003720 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003721 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003722 }
3723
zhihuang9763d562016-08-05 11:14:50 -07003724 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003725 MockPeerConnectionObserver observer_;
3726};
3727
3728// This test verifies the default behaviour with no constraints and a
3729// default RTCConfiguration.
3730TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3731 PeerConnectionInterface::RTCConfiguration config;
3732 FakeConstraints constraints;
3733
3734 const cricket::MediaConfig& media_config =
3735 TestCreatePeerConnection(config, &constraints);
3736
3737 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003738 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3739 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3740 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003741}
3742
3743// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003744// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003745TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3746 PeerConnectionInterface::RTCConfiguration config;
3747 FakeConstraints constraints;
3748
3749 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3750 const cricket::MediaConfig& media_config =
3751 TestCreatePeerConnection(config, &constraints);
3752
3753 EXPECT_TRUE(media_config.enable_dscp);
3754}
3755
3756// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003757// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003758TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3759 PeerConnectionInterface::RTCConfiguration config;
3760 FakeConstraints constraints;
3761
3762 constraints.AddOptional(
3763 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3764 const cricket::MediaConfig media_config =
3765 TestCreatePeerConnection(config, &constraints);
3766
nisse0db023a2016-03-01 04:29:59 -08003767 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003768}
3769
3770// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003771// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003772TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3773 PeerConnectionInterface::RTCConfiguration config;
3774 FakeConstraints constraints;
3775
Niels Möller71bdda02016-03-31 12:59:59 +02003776 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003777 const cricket::MediaConfig& media_config =
3778 TestCreatePeerConnection(config, &constraints);
3779
nisse0db023a2016-03-01 04:29:59 -08003780 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3781}
3782
3783// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003784// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003785TEST_F(PeerConnectionMediaConfigTest,
3786 TestSuspendBelowMinBitrateConstraintTrue) {
3787 PeerConnectionInterface::RTCConfiguration config;
3788 FakeConstraints constraints;
3789
3790 constraints.AddOptional(
3791 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3792 true);
3793 const cricket::MediaConfig media_config =
3794 TestCreatePeerConnection(config, &constraints);
3795
3796 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003797}
3798
deadbeef293e9262017-01-11 12:28:30 -08003799// Tests a few random fields being different.
3800TEST(RTCConfigurationTest, ComparisonOperators) {
3801 PeerConnectionInterface::RTCConfiguration a;
3802 PeerConnectionInterface::RTCConfiguration b;
3803 EXPECT_EQ(a, b);
3804
3805 PeerConnectionInterface::RTCConfiguration c;
3806 c.servers.push_back(PeerConnectionInterface::IceServer());
3807 EXPECT_NE(a, c);
3808
3809 PeerConnectionInterface::RTCConfiguration d;
3810 d.type = PeerConnectionInterface::kRelay;
3811 EXPECT_NE(a, d);
3812
3813 PeerConnectionInterface::RTCConfiguration e;
3814 e.audio_jitter_buffer_max_packets = 5;
3815 EXPECT_NE(a, e);
3816
3817 PeerConnectionInterface::RTCConfiguration f;
3818 f.ice_connection_receiving_timeout = 1337;
3819 EXPECT_NE(a, f);
3820
3821 PeerConnectionInterface::RTCConfiguration g;
3822 g.disable_ipv6 = true;
3823 EXPECT_NE(a, g);
3824
3825 PeerConnectionInterface::RTCConfiguration h(
3826 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3827 EXPECT_NE(a, h);
3828}