blob: 6bff2766ee2c962477d113ffd936762d0950ada3 [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;
Steve Antona3a92c22017-12-07 10:27:41 -0800355using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700357using webrtc::StreamCollection;
358using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100359using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700360using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361using webrtc::VideoTrackInterface;
362
deadbeefab9b2d12015-10-14 11:33:11 -0700363typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
364
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365namespace {
366
367// Gets the first ssrc of given content type from the ContentInfo.
368bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
369 if (!content_info || !ssrc) {
370 return false;
371 }
372 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800373 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 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(
Steve Antona3a92c22017-12-07 10:27:41 -0800876 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700877 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
879 }
880
deadbeefab9b2d12015-10-14 11:33:11 -0700881 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700882 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800883 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700884 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700885 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
886 }
887
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700889 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700890 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891
892 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
893 // audio codec change, even if the parameter has nothing to do with
894 // receiving. Not all parameters are serialized to SDP.
895 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
896 // the SessionDescription, it is necessary to do that here to in order to
897 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
898 // https://code.google.com/p/webrtc/issues/detail?id=1356
899 std::string sdp;
900 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700901 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800902 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700903 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
905 }
906
907 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700908 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700909 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910
911 std::string sdp;
912 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700913 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800914 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700915 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
917 }
918
919 void CreateOfferReceiveAnswer() {
920 CreateOfferAsLocalDescription();
921 std::string sdp;
922 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
923 CreateAnswerAsRemoteDescription(sdp);
924 }
925
926 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700927 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700928 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
930 // audio codec change, even if the parameter has nothing to do with
931 // receiving. Not all parameters are serialized to SDP.
932 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
933 // the SessionDescription, it is necessary to do that here to in order to
934 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
935 // https://code.google.com/p/webrtc/issues/detail?id=1356
936 std::string sdp;
937 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700938 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800939 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940
Steve Antondb45ca82017-09-11 18:27:34 -0700941 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000943 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700944 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
946
deadbeefab9b2d12015-10-14 11:33:11 -0700947 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700948 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800949 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700950 ASSERT_TRUE(answer);
951 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
953 }
954
deadbeefab9b2d12015-10-14 11:33:11 -0700955 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700956 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800957 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700958 ASSERT_TRUE(pr_answer);
959 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700961 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800962 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700963 ASSERT_TRUE(answer);
964 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
966 }
967
968 // Help function used for waiting until a the last signaled remote stream has
969 // the same label as |stream_label|. In a few of the tests in this file we
970 // answer with the same session description as we offer and thus we can
971 // check if OnAddStream have been called with the same stream as we offer to
972 // send.
973 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
974 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
975 }
976
977 // Creates an offer and applies it as a local session description.
978 // Creates an answer with the same SDP an the offer but removes all lines
979 // that start with a:ssrc"
980 void CreateOfferReceiveAnswerWithoutSsrc() {
981 CreateOfferAsLocalDescription();
982 std::string sdp;
983 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
984 SetSsrcToZero(&sdp);
985 CreateAnswerAsRemoteDescription(sdp);
986 }
987
deadbeefab9b2d12015-10-14 11:33:11 -0700988 // This function creates a MediaStream with label kStreams[0] and
989 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
990 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700991 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700992 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700993 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700994 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
995 size_t number_of_video_tracks) {
996 EXPECT_LE(number_of_audio_tracks, 2u);
997 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700998
999 reference_collection_ = StreamCollection::Create();
1000 std::string sdp_ms1 = std::string(kSdpStringInit);
1001
1002 std::string mediastream_label = kStreams[0];
1003
1004 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1005 webrtc::MediaStream::Create(mediastream_label));
1006 reference_collection_->AddStream(stream);
1007
1008 if (number_of_audio_tracks > 0) {
1009 sdp_ms1 += std::string(kSdpStringAudio);
1010 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1011 AddAudioTrack(kAudioTracks[0], stream);
1012 }
1013 if (number_of_audio_tracks > 1) {
1014 sdp_ms1 += kSdpStringMs1Audio1;
1015 AddAudioTrack(kAudioTracks[1], stream);
1016 }
1017
1018 if (number_of_video_tracks > 0) {
1019 sdp_ms1 += std::string(kSdpStringVideo);
1020 sdp_ms1 += std::string(kSdpStringMs1Video0);
1021 AddVideoTrack(kVideoTracks[0], stream);
1022 }
1023 if (number_of_video_tracks > 1) {
1024 sdp_ms1 += kSdpStringMs1Video1;
1025 AddVideoTrack(kVideoTracks[1], stream);
1026 }
1027
kwibergd1fe2812016-04-27 06:47:29 -07001028 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001029 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001030 }
1031
1032 void AddAudioTrack(const std::string& track_id,
1033 MediaStreamInterface* stream) {
1034 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1035 webrtc::AudioTrack::Create(track_id, nullptr));
1036 ASSERT_TRUE(stream->AddTrack(audio_track));
1037 }
1038
1039 void AddVideoTrack(const std::string& track_id,
1040 MediaStreamInterface* stream) {
1041 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001042 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001043 webrtc::FakeVideoTrackSource::Create(),
1044 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001045 ASSERT_TRUE(stream->AddTrack(video_track));
1046 }
1047
kwibergfd8be342016-05-14 19:44:11 -07001048 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001049 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001050 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001051 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001052 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1053 return offer;
1054 }
1055
kwibergfd8be342016-05-14 19:44:11 -07001056 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001057 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001058 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001059 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001060 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1061 return answer;
1062 }
1063
1064 const std::string& GetFirstAudioStreamCname(
1065 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001066 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001067 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001068 return audio_desc->streams()[0].cname;
1069 }
1070
zhihuang1c378ed2017-08-17 14:10:50 -07001071 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1072 const RTCOfferAnswerOptions& offer_answer_options) {
1073 RTC_DCHECK(pc_);
1074 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1075 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1076 pc_->CreateOffer(observer, offer_answer_options);
1077 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1078 return observer->MoveDescription();
1079 }
1080
1081 void CreateOfferWithOptionsAsRemoteDescription(
1082 std::unique_ptr<SessionDescriptionInterface>* desc,
1083 const RTCOfferAnswerOptions& offer_answer_options) {
1084 *desc = CreateOfferWithOptions(offer_answer_options);
1085 ASSERT_TRUE(desc != nullptr);
1086 std::string sdp;
1087 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001088 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001089 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001090 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001091 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1092 }
1093
1094 void CreateOfferWithOptionsAsLocalDescription(
1095 std::unique_ptr<SessionDescriptionInterface>* desc,
1096 const RTCOfferAnswerOptions& offer_answer_options) {
1097 *desc = CreateOfferWithOptions(offer_answer_options);
1098 ASSERT_TRUE(desc != nullptr);
1099 std::string sdp;
1100 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001101 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001102 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001103
Steve Antondb45ca82017-09-11 18:27:34 -07001104 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001105 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1106 }
1107
1108 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001109 RTC_DCHECK(content);
1110 RTC_DCHECK(content->media_description());
1111 for (const cricket::AudioCodec& codec :
1112 content->media_description()->as_audio()->codecs()) {
1113 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001114 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001115 }
zhihuang1c378ed2017-08-17 14:10:50 -07001116 }
1117 return false;
1118 }
1119
deadbeef9a6f4d42017-05-15 19:43:33 -07001120 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1121 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001122 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001123 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001124 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001125 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1126 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1127 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001129 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130};
1131
zhihuang29ff8442016-07-27 11:07:25 -07001132// Test that no callbacks on the PeerConnectionObserver are called after the
1133// PeerConnection is closed.
1134TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001135 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001136 pc_factory_for_test_->CreatePeerConnection(
1137 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1138 nullptr, &observer_));
1139 observer_.SetPeerConnectionInterface(pc.get());
1140 pc->Close();
1141
1142 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001143 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001144 std::vector<cricket::Candidate> candidates;
1145 pc_factory_for_test_->transport_controller->SignalGatheringState(
1146 cricket::IceGatheringState{});
1147 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1148 "", candidates);
1149 pc_factory_for_test_->transport_controller->SignalConnectionState(
1150 cricket::IceConnectionState{});
1151 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1152 candidates);
1153 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001154 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001155}
1156
zhihuang8f65cdf2016-05-06 18:40:30 -07001157// Generate different CNAMEs when PeerConnections are created.
1158// The CNAMEs are expected to be generated randomly. It is possible
1159// that the test fails, though the possibility is very low.
1160TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001161 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001162 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001163 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001164 CreateOfferWithOneAudioStream();
1165 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1166 GetFirstAudioStreamCname(offer2.get()));
1167}
1168
1169TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 CreateAnswerWithOneAudioStream();
1174 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1175 GetFirstAudioStreamCname(answer2.get()));
1176}
1177
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178TEST_F(PeerConnectionInterfaceTest,
1179 CreatePeerConnectionWithDifferentConfigurations) {
1180 CreatePeerConnectionWithDifferentConfigurations();
1181}
1182
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001183TEST_F(PeerConnectionInterfaceTest,
1184 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1185 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1186 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1187 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1188 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1189 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1190 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1191 port_allocator_->candidate_filter());
1192 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1193 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1194}
1195
1196// Test that when a PeerConnection is created with a nonzero candidate pool
1197// size, the pooled PortAllocatorSession is created with all the attributes
1198// in the RTCConfiguration.
1199TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1200 PeerConnectionInterface::RTCConfiguration config;
1201 PeerConnectionInterface::IceServer server;
1202 server.uri = kStunAddressOnly;
1203 config.servers.push_back(server);
1204 config.type = PeerConnectionInterface::kRelay;
1205 config.disable_ipv6 = true;
1206 config.tcp_candidate_policy =
1207 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001208 config.candidate_network_policy =
1209 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001210 config.ice_candidate_pool_size = 1;
1211 CreatePeerConnection(config, nullptr);
1212
1213 const cricket::FakePortAllocatorSession* session =
1214 static_cast<const cricket::FakePortAllocatorSession*>(
1215 port_allocator_->GetPooledSession());
1216 ASSERT_NE(nullptr, session);
1217 EXPECT_EQ(1UL, session->stun_servers().size());
1218 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1219 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001220 EXPECT_LT(0U,
1221 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001222}
1223
deadbeefd21eab32017-07-26 16:50:11 -07001224// Test that network-related RTCConfiguration members are applied to the
1225// PortAllocator when CreatePeerConnection is called. Specifically:
1226// - disable_ipv6_on_wifi
1227// - max_ipv6_networks
1228// - tcp_candidate_policy
1229// - candidate_network_policy
1230// - prune_turn_ports
1231//
1232// Note that the candidate filter (RTCConfiguration::type) is already tested
1233// above.
1234TEST_F(PeerConnectionInterfaceTest,
1235 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1236 // Create fake port allocator.
1237 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1238 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1239 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1240
1241 // Create RTCConfiguration with some network-related fields relevant to
1242 // PortAllocator populated.
1243 PeerConnectionInterface::RTCConfiguration config;
1244 config.disable_ipv6_on_wifi = true;
1245 config.max_ipv6_networks = 10;
1246 config.tcp_candidate_policy =
1247 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1248 config.candidate_network_policy =
1249 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1250 config.prune_turn_ports = true;
1251
1252 // Create the PC factory and PC with the above config.
1253 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1254 webrtc::CreatePeerConnectionFactory(
1255 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001256 rtc::Thread::Current(), fake_audio_capture_module_,
1257 webrtc::CreateBuiltinAudioEncoderFactory(),
1258 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001259 rtc::scoped_refptr<PeerConnectionInterface> pc(
1260 pc_factory->CreatePeerConnection(
1261 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1262
1263 // Now validate that the config fields set above were applied to the
1264 // PortAllocator, as flags or otherwise.
1265 EXPECT_FALSE(raw_port_allocator->flags() &
1266 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1267 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1268 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1269 EXPECT_TRUE(raw_port_allocator->flags() &
1270 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1271 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1272}
1273
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001274// Test that the PeerConnection initializes the port allocator passed into it,
1275// and on the correct thread.
1276TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001277 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001278 std::unique_ptr<rtc::Thread> network_thread(
1279 rtc::Thread::CreateWithSocketServer());
1280 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001281 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1282 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001283 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001284 fake_audio_capture_module_,
1285 webrtc::CreateBuiltinAudioEncoderFactory(),
1286 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001287 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001288 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001289 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1290 PeerConnectionInterface::RTCConfiguration config;
1291 rtc::scoped_refptr<PeerConnectionInterface> pc(
1292 pc_factory->CreatePeerConnection(
1293 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1294 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1295 // so all we have to do here is check that it's initialized.
1296 EXPECT_TRUE(raw_port_allocator->initialized());
1297}
1298
deadbeef46c73892016-11-16 19:42:04 -08001299// Check that GetConfiguration returns the configuration the PeerConnection was
1300// constructed with, before SetConfiguration is called.
1301TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1302 PeerConnectionInterface::RTCConfiguration config;
1303 config.type = PeerConnectionInterface::kRelay;
1304 CreatePeerConnection(config, nullptr);
1305
1306 PeerConnectionInterface::RTCConfiguration returned_config =
1307 pc_->GetConfiguration();
1308 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1309}
1310
1311// Check that GetConfiguration returns the last configuration passed into
1312// SetConfiguration.
1313TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1314 CreatePeerConnection();
1315
1316 PeerConnectionInterface::RTCConfiguration config;
1317 config.type = PeerConnectionInterface::kRelay;
1318 EXPECT_TRUE(pc_->SetConfiguration(config));
1319
1320 PeerConnectionInterface::RTCConfiguration returned_config =
1321 pc_->GetConfiguration();
1322 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1323}
1324
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001326 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001327 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 AddVoiceStream(kStreamLabel2);
1329 ASSERT_EQ(2u, pc_->local_streams()->count());
1330
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001331 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001332 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001334 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1335 pc_factory_->CreateAudioTrack(kStreamLabel3,
1336 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001338 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001339 EXPECT_EQ(3u, pc_->local_streams()->count());
1340
1341 // Remove the third stream.
1342 pc_->RemoveStream(pc_->local_streams()->at(2));
1343 EXPECT_EQ(2u, pc_->local_streams()->count());
1344
1345 // Remove the second stream.
1346 pc_->RemoveStream(pc_->local_streams()->at(1));
1347 EXPECT_EQ(1u, pc_->local_streams()->count());
1348
1349 // Remove the first stream.
1350 pc_->RemoveStream(pc_->local_streams()->at(0));
1351 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
1353
deadbeefab9b2d12015-10-14 11:33:11 -07001354// Test that the created offer includes streams we added.
1355TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001356 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001357 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001358 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001359 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001360
deadbeefab9b2d12015-10-14 11:33:11 -07001361 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001362 cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001363 EXPECT_TRUE(
1364 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1365
deadbeefab9b2d12015-10-14 11:33:11 -07001366 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001367 cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001368 EXPECT_TRUE(
1369 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1370
1371 // Add another stream and ensure the offer includes both the old and new
1372 // streams.
1373 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001374 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001375
Steve Antonb1c1de12017-12-21 15:14:30 -08001376 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001377 EXPECT_TRUE(
1378 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1379 EXPECT_TRUE(
1380 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1381
Steve Antonb1c1de12017-12-21 15:14:30 -08001382 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001383 EXPECT_TRUE(
1384 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1385 EXPECT_TRUE(
1386 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1387}
1388
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001390 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001391 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 ASSERT_EQ(1u, pc_->local_streams()->count());
1393 pc_->RemoveStream(pc_->local_streams()->at(0));
1394 EXPECT_EQ(0u, pc_->local_streams()->count());
1395}
1396
deadbeefe1f9d832016-01-14 15:35:42 -08001397// Test for AddTrack and RemoveTrack methods.
1398// Tests that the created offer includes tracks we added,
1399// and that the RtpSenders are created correctly.
1400// Also tests that RemoveTrack removes the tracks from subsequent offers.
1401TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001402 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001403 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001404 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001405 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1406 std::vector<MediaStreamInterface*> stream_list;
1407 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001408 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001409 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001410 rtc::scoped_refptr<VideoTrackInterface> video_track(
1411 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001412 "video_track", pc_factory_->CreateVideoSource(
1413 std::unique_ptr<cricket::VideoCapturer>(
1414 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001415 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1416 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001417 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1418 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001419 EXPECT_EQ("audio_track", audio_sender->id());
1420 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001421 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1422 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001423 EXPECT_EQ("video_track", video_sender->id());
1424 EXPECT_EQ(video_track, video_sender->track());
1425
1426 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001427 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001428 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001429
1430 const cricket::ContentInfo* audio_content =
1431 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001432 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1433 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001434
1435 const cricket::ContentInfo* video_content =
1436 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001437 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1438 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001439
Steve Antondb45ca82017-09-11 18:27:34 -07001440 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001441
1442 // Now try removing the tracks.
1443 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1444 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1445
1446 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001447 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001448
1449 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001450 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1451 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001452
1453 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001454 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1455 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001456
Steve Antondb45ca82017-09-11 18:27:34 -07001457 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001458
1459 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1460 // should return false.
1461 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1462 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1463}
1464
1465// Test creating senders without a stream specified,
1466// expecting a random stream ID to be generated.
1467TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001468 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001469 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001470 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001471 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001472 rtc::scoped_refptr<VideoTrackInterface> video_track(
1473 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001474 "video_track", pc_factory_->CreateVideoSource(
1475 std::unique_ptr<cricket::VideoCapturer>(
1476 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001477 auto audio_sender =
1478 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1479 auto video_sender =
1480 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1481 EXPECT_EQ("audio_track", audio_sender->id());
1482 EXPECT_EQ(audio_track, audio_sender->track());
1483 EXPECT_EQ("video_track", video_sender->id());
1484 EXPECT_EQ(video_track, video_sender->track());
1485 // If the ID is truly a random GUID, it should be infinitely unlikely they
1486 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001487 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001488}
1489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1491 InitiateCall();
1492 WaitAndVerifyOnAddStream(kStreamLabel1);
1493 VerifyRemoteRtpHeaderExtensions();
1494}
1495
1496TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001497 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001498 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 CreateOfferAsLocalDescription();
1500 std::string offer;
1501 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1502 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1503 WaitAndVerifyOnAddStream(kStreamLabel1);
1504}
1505
1506TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001507 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001508 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509
1510 CreateOfferAsRemoteDescription();
1511 CreateAnswerAsLocalDescription();
1512
1513 WaitAndVerifyOnAddStream(kStreamLabel1);
1514}
1515
1516TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001517 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001518 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519
1520 CreateOfferAsRemoteDescription();
1521 CreatePrAnswerAsLocalDescription();
1522 CreateAnswerAsLocalDescription();
1523
1524 WaitAndVerifyOnAddStream(kStreamLabel1);
1525}
1526
1527TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1528 InitiateCall();
1529 ASSERT_EQ(1u, pc_->remote_streams()->count());
1530 pc_->RemoveStream(pc_->local_streams()->at(0));
1531 CreateOfferReceiveAnswer();
1532 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001533 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 CreateOfferReceiveAnswer();
1535}
1536
1537// Tests that after negotiating an audio only call, the respondent can perform a
1538// renegotiation that removes the audio stream.
1539TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001540 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 AddVoiceStream(kStreamLabel1);
1542 CreateOfferAsRemoteDescription();
1543 CreateAnswerAsLocalDescription();
1544
1545 ASSERT_EQ(1u, pc_->remote_streams()->count());
1546 pc_->RemoveStream(pc_->local_streams()->at(0));
1547 CreateOfferReceiveAnswer();
1548 EXPECT_EQ(0u, pc_->remote_streams()->count());
1549}
1550
1551// Test that candidates are generated and that we can parse our own candidates.
1552TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001553 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554
Steve Antonf1c6db12017-10-13 11:13:35 -07001555 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001557 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001558 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001559 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001560 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561
1562 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001563 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001564 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001565 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
Steve Antonf1c6db12017-10-13 11:13:35 -07001567 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001568 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569
Steve Antonf1c6db12017-10-13 11:13:35 -07001570 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571}
1572
deadbeefab9b2d12015-10-14 11:33:11 -07001573// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574// not unique.
1575TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001576 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001578 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001579 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001580 EXPECT_TRUE(offer);
1581 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582
1583 // Create a local stream with audio&video tracks having same label.
1584 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1585
1586 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001587 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588
1589 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001590 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001591 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592}
1593
1594// Test that we will get different SSRCs for each tracks in the offer and answer
1595// we created.
1596TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001597 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 // Create a local stream with audio&video tracks having different labels.
1599 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1600
1601 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001602 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001603 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 int audio_ssrc = 0;
1605 int video_ssrc = 0;
1606 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1607 &audio_ssrc));
1608 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1609 &video_ssrc));
1610 EXPECT_NE(audio_ssrc, video_ssrc);
1611
1612 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001613 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001614 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001615 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 audio_ssrc = 0;
1617 video_ssrc = 0;
1618 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1619 &audio_ssrc));
1620 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1621 &video_ssrc));
1622 EXPECT_NE(audio_ssrc, video_ssrc);
1623}
1624
deadbeefeb459812015-12-15 19:24:43 -08001625// Test that it's possible to call AddTrack on a MediaStream after adding
1626// the stream to a PeerConnection.
1627// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1628TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001629 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001630 // Create audio stream and add to PeerConnection.
1631 AddVoiceStream(kStreamLabel1);
1632 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1633
1634 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001635 rtc::scoped_refptr<VideoTrackInterface> video_track(
1636 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001637 "video_label", pc_factory_->CreateVideoSource(
1638 std::unique_ptr<cricket::VideoCapturer>(
1639 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001640 stream->AddTrack(video_track.get());
1641
kwibergd1fe2812016-04-27 06:47:29 -07001642 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001643 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001644
1645 const cricket::MediaContentDescription* video_desc =
1646 cricket::GetFirstVideoContentDescription(offer->description());
1647 EXPECT_TRUE(video_desc != nullptr);
1648}
1649
1650// Test that it's possible to call RemoveTrack on a MediaStream after adding
1651// the stream to a PeerConnection.
1652// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1653TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001654 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001655 // Create audio/video stream and add to PeerConnection.
1656 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1657 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1658
1659 // Remove the video track.
1660 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1661
kwibergd1fe2812016-04-27 06:47:29 -07001662 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001663 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001664
1665 const cricket::MediaContentDescription* video_desc =
1666 cricket::GetFirstVideoContentDescription(offer->description());
1667 EXPECT_TRUE(video_desc == nullptr);
1668}
1669
deadbeef1dcb1642017-03-29 21:08:16 -07001670// Verify that CreateDtmfSender only succeeds if called with a valid local
1671// track. Other aspects of DtmfSenders are tested in
1672// peerconnection_integrationtest.cc.
1673TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1674 CreatePeerConnection();
1675 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1676 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1677 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1678 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1679 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1680}
1681
deadbeefbd7d8f72015-12-18 16:58:44 -08001682// Test creating a sender with a stream ID, and ensure the ID is populated
1683// in the offer.
1684TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001685 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001686 pc_->CreateSender("video", kStreamLabel1);
1687
kwibergd1fe2812016-04-27 06:47:29 -07001688 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001689 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001690
1691 const cricket::MediaContentDescription* video_desc =
1692 cricket::GetFirstVideoContentDescription(offer->description());
1693 ASSERT_TRUE(video_desc != nullptr);
1694 ASSERT_EQ(1u, video_desc->streams().size());
1695 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1696}
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698// Test that we can specify a certain track that we want statistics about.
1699TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1700 InitiateCall();
1701 ASSERT_LT(0u, pc_->remote_streams()->count());
1702 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001703 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1705 EXPECT_TRUE(DoGetStats(remote_audio));
1706
1707 // Remove the stream. Since we are sending to our selves the local
1708 // and the remote stream is the same.
1709 pc_->RemoveStream(pc_->local_streams()->at(0));
1710 // Do a re-negotiation.
1711 CreateOfferReceiveAnswer();
1712
1713 ASSERT_EQ(0u, pc_->remote_streams()->count());
1714
1715 // Test that we still can get statistics for the old track. Even if it is not
1716 // sent any longer.
1717 EXPECT_TRUE(DoGetStats(remote_audio));
1718}
1719
1720// Test that we can get stats on a video track.
1721TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1722 InitiateCall();
1723 ASSERT_LT(0u, pc_->remote_streams()->count());
1724 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001725 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1727 EXPECT_TRUE(DoGetStats(remote_video));
1728}
1729
1730// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001731TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001733 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 pc_factory_->CreateAudioTrack("unknown track", NULL));
1735 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1736}
1737
1738// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 FakeConstraints constraints;
1741 constraints.SetAllowRtpDataChannels();
1742 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001743 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001745 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 pc_->CreateDataChannel("test2", NULL);
1747 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001748 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001750 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 new MockDataChannelObserver(data2));
1752
1753 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1754 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1755 std::string data_to_send1 = "testing testing";
1756 std::string data_to_send2 = "testing something else";
1757 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1758
1759 CreateOfferReceiveAnswer();
1760 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1761 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1762
1763 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1764 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1765 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1766 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1767
1768 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1769 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1770
1771 data1->Close();
1772 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1773 CreateOfferReceiveAnswer();
1774 EXPECT_FALSE(observer1->IsOpen());
1775 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1776 EXPECT_TRUE(observer2->IsOpen());
1777
1778 data_to_send2 = "testing something else again";
1779 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1780
1781 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1782}
1783
1784// This test verifies that sendnig binary data over RTP data channels should
1785// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 FakeConstraints constraints;
1788 constraints.SetAllowRtpDataChannels();
1789 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001790 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001792 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 pc_->CreateDataChannel("test2", NULL);
1794 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001795 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001797 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 new MockDataChannelObserver(data2));
1799
1800 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1801 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1802
1803 CreateOfferReceiveAnswer();
1804 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1805 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1806
1807 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1808 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1809
jbaucheec21bd2016-03-20 06:15:43 -07001810 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1812}
1813
1814// This test setup a RTP data channels in loop back and test that a channel is
1815// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 FakeConstraints constraints;
1818 constraints.SetAllowRtpDataChannels();
1819 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001820 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001822 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 new MockDataChannelObserver(data1));
1824
1825 CreateOfferReceiveAnswerWithoutSsrc();
1826
1827 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1828
1829 data1->Close();
1830 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1831 CreateOfferReceiveAnswerWithoutSsrc();
1832 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1833 EXPECT_FALSE(observer1->IsOpen());
1834}
1835
1836// This test that if a data channel is added in an answer a receive only channel
1837// channel is created.
1838TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1839 FakeConstraints constraints;
1840 constraints.SetAllowRtpDataChannels();
1841 CreatePeerConnection(&constraints);
1842
1843 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 pc_->CreateDataChannel(offer_label, NULL);
1846
1847 CreateOfferAsLocalDescription();
1848
1849 // Replace the data channel label in the offer and apply it as an answer.
1850 std::string receive_label = "answer_channel";
1851 std::string sdp;
1852 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001853 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 receive_label.c_str(), receive_label.length(),
1855 &sdp);
1856 CreateAnswerAsRemoteDescription(sdp);
1857
1858 // Verify that a new incoming data channel has been created and that
1859 // it is open but can't we written to.
1860 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1861 DataChannelInterface* received_channel = observer_.last_datachannel_;
1862 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1863 EXPECT_EQ(receive_label, received_channel->label());
1864 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1865
1866 // Verify that the channel we initially offered has been rejected.
1867 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1868
1869 // Do another offer / answer exchange and verify that the data channel is
1870 // opened.
1871 CreateOfferReceiveAnswer();
1872 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1873 kTimeout);
1874}
1875
1876// This test that no data channel is returned if a reliable channel is
1877// requested.
1878// TODO(perkj): Remove this test once reliable channels are implemented.
1879TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1880 FakeConstraints constraints;
1881 constraints.SetAllowRtpDataChannels();
1882 CreatePeerConnection(&constraints);
1883
1884 std::string label = "test";
1885 webrtc::DataChannelInit config;
1886 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001887 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 pc_->CreateDataChannel(label, &config);
1889 EXPECT_TRUE(channel == NULL);
1890}
1891
deadbeefab9b2d12015-10-14 11:33:11 -07001892// Verifies that duplicated label is not allowed for RTP data channel.
1893TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1894 FakeConstraints constraints;
1895 constraints.SetAllowRtpDataChannels();
1896 CreatePeerConnection(&constraints);
1897
1898 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001899 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001900 pc_->CreateDataChannel(label, nullptr);
1901 EXPECT_NE(channel, nullptr);
1902
zhihuang9763d562016-08-05 11:14:50 -07001903 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001904 pc_->CreateDataChannel(label, nullptr);
1905 EXPECT_EQ(dup_channel, nullptr);
1906}
1907
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908// This tests that a SCTP data channel is returned using different
1909// DataChannelInit configurations.
1910TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1911 FakeConstraints constraints;
1912 constraints.SetAllowDtlsSctpDataChannels();
1913 CreatePeerConnection(&constraints);
1914
1915 webrtc::DataChannelInit config;
1916
zhihuang9763d562016-08-05 11:14:50 -07001917 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 pc_->CreateDataChannel("1", &config);
1919 EXPECT_TRUE(channel != NULL);
1920 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001921 EXPECT_TRUE(observer_.renegotiation_needed_);
1922 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923
1924 config.ordered = false;
1925 channel = pc_->CreateDataChannel("2", &config);
1926 EXPECT_TRUE(channel != NULL);
1927 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001928 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929
1930 config.ordered = true;
1931 config.maxRetransmits = 0;
1932 channel = pc_->CreateDataChannel("3", &config);
1933 EXPECT_TRUE(channel != NULL);
1934 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001935 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936
1937 config.maxRetransmits = -1;
1938 config.maxRetransmitTime = 0;
1939 channel = pc_->CreateDataChannel("4", &config);
1940 EXPECT_TRUE(channel != NULL);
1941 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001942 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943}
1944
1945// This tests that no data channel is returned if both maxRetransmits and
1946// maxRetransmitTime are set for SCTP data channels.
1947TEST_F(PeerConnectionInterfaceTest,
1948 CreateSctpDataChannelShouldFailForInvalidConfig) {
1949 FakeConstraints constraints;
1950 constraints.SetAllowDtlsSctpDataChannels();
1951 CreatePeerConnection(&constraints);
1952
1953 std::string label = "test";
1954 webrtc::DataChannelInit config;
1955 config.maxRetransmits = 0;
1956 config.maxRetransmitTime = 0;
1957
zhihuang9763d562016-08-05 11:14:50 -07001958 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 pc_->CreateDataChannel(label, &config);
1960 EXPECT_TRUE(channel == NULL);
1961}
1962
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963// The test verifies that creating a SCTP data channel with an id already in use
1964// or out of range should fail.
1965TEST_F(PeerConnectionInterfaceTest,
1966 CreateSctpDataChannelWithInvalidIdShouldFail) {
1967 FakeConstraints constraints;
1968 constraints.SetAllowDtlsSctpDataChannels();
1969 CreatePeerConnection(&constraints);
1970
1971 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001972 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001974 config.id = 1;
1975 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 EXPECT_TRUE(channel != NULL);
1977 EXPECT_EQ(1, channel->id());
1978
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 channel = pc_->CreateDataChannel("x", &config);
1980 EXPECT_TRUE(channel == NULL);
1981
1982 config.id = cricket::kMaxSctpSid;
1983 channel = pc_->CreateDataChannel("max", &config);
1984 EXPECT_TRUE(channel != NULL);
1985 EXPECT_EQ(config.id, channel->id());
1986
1987 config.id = cricket::kMaxSctpSid + 1;
1988 channel = pc_->CreateDataChannel("x", &config);
1989 EXPECT_TRUE(channel == NULL);
1990}
1991
deadbeefab9b2d12015-10-14 11:33:11 -07001992// Verifies that duplicated label is allowed for SCTP data channel.
1993TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1994 FakeConstraints constraints;
1995 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1996 true);
1997 CreatePeerConnection(&constraints);
1998
1999 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002000 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002001 pc_->CreateDataChannel(label, nullptr);
2002 EXPECT_NE(channel, nullptr);
2003
zhihuang9763d562016-08-05 11:14:50 -07002004 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002005 pc_->CreateDataChannel(label, nullptr);
2006 EXPECT_NE(dup_channel, nullptr);
2007}
2008
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002009// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2010// DataChannel.
2011TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2012 FakeConstraints constraints;
2013 constraints.SetAllowRtpDataChannels();
2014 CreatePeerConnection(&constraints);
2015
zhihuang9763d562016-08-05 11:14:50 -07002016 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002017 pc_->CreateDataChannel("test1", NULL);
2018 EXPECT_TRUE(observer_.renegotiation_needed_);
2019 observer_.renegotiation_needed_ = false;
2020
zhihuang9763d562016-08-05 11:14:50 -07002021 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002022 pc_->CreateDataChannel("test2", NULL);
2023 EXPECT_TRUE(observer_.renegotiation_needed_);
2024}
2025
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 FakeConstraints constraints;
2029 constraints.SetAllowRtpDataChannels();
2030 CreatePeerConnection(&constraints);
2031
zhihuang9763d562016-08-05 11:14:50 -07002032 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002034 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 pc_->CreateDataChannel("test2", NULL);
2036 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002037 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002039 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 new MockDataChannelObserver(data2));
2041
2042 CreateOfferReceiveAnswer();
2043 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2044 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2045
2046 ReleasePeerConnection();
2047 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2048 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2049}
2050
2051// This test that data channels can be rejected in an answer.
2052TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2053 FakeConstraints constraints;
2054 constraints.SetAllowRtpDataChannels();
2055 CreatePeerConnection(&constraints);
2056
zhihuang9763d562016-08-05 11:14:50 -07002057 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 pc_->CreateDataChannel("offer_channel", NULL));
2059
2060 CreateOfferAsLocalDescription();
2061
2062 // Create an answer where the m-line for data channels are rejected.
2063 std::string sdp;
2064 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002065 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002066 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002067 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 cricket::ContentInfo* data_info =
2069 answer->description()->GetContentByName("data");
2070 data_info->rejected = true;
2071
Steve Antondb45ca82017-09-11 18:27:34 -07002072 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2074}
2075
2076// Test that we can create a session description from an SDP string from
2077// FireFox, use it as a remote session description, generate an answer and use
2078// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002079TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 FakeConstraints constraints;
2081 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2082 true);
2083 CreatePeerConnection(&constraints);
2084 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002085 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002086 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002087 webrtc::kFireFoxSdpOffer, nullptr));
2088 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 CreateAnswerAsLocalDescription();
2090 ASSERT_TRUE(pc_->local_description() != NULL);
2091 ASSERT_TRUE(pc_->remote_description() != NULL);
2092
2093 const cricket::ContentInfo* content =
2094 cricket::GetFirstAudioContent(pc_->local_description()->description());
2095 ASSERT_TRUE(content != NULL);
2096 EXPECT_FALSE(content->rejected);
2097
2098 content =
2099 cricket::GetFirstVideoContent(pc_->local_description()->description());
2100 ASSERT_TRUE(content != NULL);
2101 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002102#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 content =
2104 cricket::GetFirstDataContent(pc_->local_description()->description());
2105 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002106 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002108#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109}
2110
zhihuangb19012e2017-09-19 13:47:59 -07002111// Test that fallback from DTLS to SDES is not supported.
2112// The fallback was previously supported but was removed to simplify the code
2113// and because it's non-standard.
2114TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002115 FakeConstraints constraints;
2116 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2117 true);
2118 CreatePeerConnection(&constraints);
2119 // Wait for fake certificate to be generated. Previously, this is what caused
2120 // the "a=crypto" lines to be rejected.
2121 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2122 ASSERT_NE(nullptr, fake_certificate_generator_);
2123 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2124 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002125 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002126 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2127 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002128 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002129}
2130
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131// Test that we can create an audio only offer and receive an answer with a
2132// limited set of audio codecs and receive an updated offer with more audio
2133// codecs, where the added codecs are not supported.
2134TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002135 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 AddVoiceStream("audio_label");
2137 CreateOfferAsLocalDescription();
2138
Steve Antondb45ca82017-09-11 18:27:34 -07002139 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002140 webrtc::CreateSessionDescription(SdpType::kAnswer, webrtc::kAudioSdp,
2141 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002142 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143
Steve Antondb45ca82017-09-11 18:27:34 -07002144 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002145 webrtc::CreateSessionDescription(
2146 SdpType::kOffer, webrtc::kAudioSdpWithUnsupportedCodecs, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002147 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 CreateAnswerAsLocalDescription();
2149}
2150
deadbeefc80741f2015-10-22 13:14:45 -07002151// Test that if we're receiving (but not sending) a track, subsequent offers
2152// will have m-lines with a=recvonly.
2153TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2154 FakeConstraints constraints;
2155 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2156 true);
2157 CreatePeerConnection(&constraints);
2158 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2159 CreateAnswerAsLocalDescription();
2160
2161 // At this point we should be receiving stream 1, but not sending anything.
2162 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002163 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002164 DoCreateOffer(&offer, nullptr);
2165
2166 const cricket::ContentInfo* video_content =
2167 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002168 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2169 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002170
2171 const cricket::ContentInfo* audio_content =
2172 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002173 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2174 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002175}
2176
2177// Test that if we're receiving (but not sending) a track, and the
2178// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2179// false, the generated m-lines will be a=inactive.
2180TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2181 FakeConstraints constraints;
2182 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2183 true);
2184 CreatePeerConnection(&constraints);
2185 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2186 CreateAnswerAsLocalDescription();
2187
2188 // At this point we should be receiving stream 1, but not sending anything.
2189 // A new offer would be recvonly, but we'll set the "no receive" constraints
2190 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002191 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002192 FakeConstraints offer_constraints;
2193 offer_constraints.AddMandatory(
2194 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2195 offer_constraints.AddMandatory(
2196 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2197 DoCreateOffer(&offer, &offer_constraints);
2198
2199 const cricket::ContentInfo* video_content =
2200 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002201 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2202 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002203
2204 const cricket::ContentInfo* audio_content =
2205 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002206 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2207 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002208}
2209
deadbeef653b8e02015-11-11 12:55:10 -08002210// Test that we can use SetConfiguration to change the ICE servers of the
2211// PortAllocator.
2212TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2213 CreatePeerConnection();
2214
2215 PeerConnectionInterface::RTCConfiguration config;
2216 PeerConnectionInterface::IceServer server;
2217 server.uri = "stun:test_hostname";
2218 config.servers.push_back(server);
2219 EXPECT_TRUE(pc_->SetConfiguration(config));
2220
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002221 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2222 EXPECT_EQ("test_hostname",
2223 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002224}
2225
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002226TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2227 CreatePeerConnection();
2228 PeerConnectionInterface::RTCConfiguration config;
2229 config.type = PeerConnectionInterface::kRelay;
2230 EXPECT_TRUE(pc_->SetConfiguration(config));
2231 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2232}
2233
deadbeef293e9262017-01-11 12:28:30 -08002234TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2235 PeerConnectionInterface::RTCConfiguration config;
2236 config.prune_turn_ports = false;
2237 CreatePeerConnection(config, nullptr);
2238 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2239
2240 config.prune_turn_ports = true;
2241 EXPECT_TRUE(pc_->SetConfiguration(config));
2242 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2243}
2244
skvladd1f5fda2017-02-03 16:54:05 -08002245// Test that the ice check interval can be changed. This does not verify that
2246// the setting makes it all the way to P2PTransportChannel, as that would
2247// require a very complex set of mocks.
2248TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2249 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002250 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002251 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002252 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002253 EXPECT_TRUE(pc_->SetConfiguration(config));
2254 PeerConnectionInterface::RTCConfiguration new_config =
2255 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002256 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002257}
2258
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002259// Test that when SetConfiguration changes both the pool size and other
2260// attributes, the pooled session is created with the updated attributes.
2261TEST_F(PeerConnectionInterfaceTest,
2262 SetConfigurationCreatesPooledSessionCorrectly) {
2263 CreatePeerConnection();
2264 PeerConnectionInterface::RTCConfiguration config;
2265 config.ice_candidate_pool_size = 1;
2266 PeerConnectionInterface::IceServer server;
2267 server.uri = kStunAddressOnly;
2268 config.servers.push_back(server);
2269 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002270 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002271
2272 const cricket::FakePortAllocatorSession* session =
2273 static_cast<const cricket::FakePortAllocatorSession*>(
2274 port_allocator_->GetPooledSession());
2275 ASSERT_NE(nullptr, session);
2276 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002277}
2278
deadbeef293e9262017-01-11 12:28:30 -08002279// Test that after SetLocalDescription, changing the pool size is not allowed,
2280// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002281TEST_F(PeerConnectionInterfaceTest,
2282 CantChangePoolSizeAfterSetLocalDescription) {
2283 CreatePeerConnection();
2284 // Start by setting a size of 1.
2285 PeerConnectionInterface::RTCConfiguration config;
2286 config.ice_candidate_pool_size = 1;
2287 EXPECT_TRUE(pc_->SetConfiguration(config));
2288
2289 // Set remote offer; can still change pool size at this point.
2290 CreateOfferAsRemoteDescription();
2291 config.ice_candidate_pool_size = 2;
2292 EXPECT_TRUE(pc_->SetConfiguration(config));
2293
2294 // Set local answer; now it's too late.
2295 CreateAnswerAsLocalDescription();
2296 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002297 RTCError error;
2298 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2299 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2300}
2301
deadbeef42a42632017-03-10 15:18:00 -08002302// Test that after setting an answer, extra pooled sessions are discarded. The
2303// ICE candidate pool is only intended to be used for the first offer/answer.
2304TEST_F(PeerConnectionInterfaceTest,
2305 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2306 CreatePeerConnection();
2307
2308 // Set a larger-than-necessary size.
2309 PeerConnectionInterface::RTCConfiguration config;
2310 config.ice_candidate_pool_size = 4;
2311 EXPECT_TRUE(pc_->SetConfiguration(config));
2312
2313 // Do offer/answer.
2314 CreateOfferAsRemoteDescription();
2315 CreateAnswerAsLocalDescription();
2316
2317 // Expect no pooled sessions to be left.
2318 const cricket::PortAllocatorSession* session =
2319 port_allocator_->GetPooledSession();
2320 EXPECT_EQ(nullptr, session);
2321}
2322
2323// After Close is called, pooled candidates should be discarded so as to not
2324// waste network resources.
2325TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2326 CreatePeerConnection();
2327
2328 PeerConnectionInterface::RTCConfiguration config;
2329 config.ice_candidate_pool_size = 3;
2330 EXPECT_TRUE(pc_->SetConfiguration(config));
2331 pc_->Close();
2332
2333 // Expect no pooled sessions to be left.
2334 const cricket::PortAllocatorSession* session =
2335 port_allocator_->GetPooledSession();
2336 EXPECT_EQ(nullptr, session);
2337}
2338
deadbeef293e9262017-01-11 12:28:30 -08002339// Test that SetConfiguration returns an invalid modification error if
2340// modifying a field in the configuration that isn't allowed to be modified.
2341TEST_F(PeerConnectionInterfaceTest,
2342 SetConfigurationReturnsInvalidModificationError) {
2343 PeerConnectionInterface::RTCConfiguration config;
2344 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2345 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2346 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2347 CreatePeerConnection(config, nullptr);
2348
2349 PeerConnectionInterface::RTCConfiguration modified_config = config;
2350 modified_config.bundle_policy =
2351 PeerConnectionInterface::kBundlePolicyMaxBundle;
2352 RTCError error;
2353 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2354 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2355
2356 modified_config = config;
2357 modified_config.rtcp_mux_policy =
2358 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2359 error.set_type(RTCErrorType::NONE);
2360 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2361 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2362
2363 modified_config = config;
2364 modified_config.continual_gathering_policy =
2365 PeerConnectionInterface::GATHER_CONTINUALLY;
2366 error.set_type(RTCErrorType::NONE);
2367 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2368 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2369}
2370
2371// Test that SetConfiguration returns a range error if the candidate pool size
2372// is negative or larger than allowed by the spec.
2373TEST_F(PeerConnectionInterfaceTest,
2374 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2375 PeerConnectionInterface::RTCConfiguration config;
2376 CreatePeerConnection(config, nullptr);
2377
2378 config.ice_candidate_pool_size = -1;
2379 RTCError error;
2380 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2381 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2382
2383 config.ice_candidate_pool_size = INT_MAX;
2384 error.set_type(RTCErrorType::NONE);
2385 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2386 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2387}
2388
2389// Test that SetConfiguration returns a syntax error if parsing an ICE server
2390// URL failed.
2391TEST_F(PeerConnectionInterfaceTest,
2392 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2393 PeerConnectionInterface::RTCConfiguration config;
2394 CreatePeerConnection(config, nullptr);
2395
2396 PeerConnectionInterface::IceServer bad_server;
2397 bad_server.uri = "stunn:www.example.com";
2398 config.servers.push_back(bad_server);
2399 RTCError error;
2400 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2401 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2402}
2403
2404// Test that SetConfiguration returns an invalid parameter error if a TURN
2405// IceServer is missing a username or password.
2406TEST_F(PeerConnectionInterfaceTest,
2407 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2408 PeerConnectionInterface::RTCConfiguration config;
2409 CreatePeerConnection(config, nullptr);
2410
2411 PeerConnectionInterface::IceServer bad_server;
2412 bad_server.uri = "turn:www.example.com";
2413 // Missing password.
2414 bad_server.username = "foo";
2415 config.servers.push_back(bad_server);
2416 RTCError error;
2417 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2418 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002419}
2420
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421// Test that PeerConnection::Close changes the states to closed and all remote
2422// tracks change state to ended.
2423TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2424 // Initialize a PeerConnection and negotiate local and remote session
2425 // description.
2426 InitiateCall();
2427 ASSERT_EQ(1u, pc_->local_streams()->count());
2428 ASSERT_EQ(1u, pc_->remote_streams()->count());
2429
2430 pc_->Close();
2431
2432 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2433 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2434 pc_->ice_connection_state());
2435 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2436 pc_->ice_gathering_state());
2437
2438 EXPECT_EQ(1u, pc_->local_streams()->count());
2439 EXPECT_EQ(1u, pc_->remote_streams()->count());
2440
zhihuang9763d562016-08-05 11:14:50 -07002441 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2442 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002443 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002444 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002445 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2446 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2447 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448}
2449
2450// Test that PeerConnection methods fails gracefully after
2451// PeerConnection::Close has been called.
2452TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002453 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2455 CreateOfferAsRemoteDescription();
2456 CreateAnswerAsLocalDescription();
2457
2458 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002459 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 pc_->local_streams()->at(0);
2461
2462 pc_->Close();
2463
2464 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002465 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002466
2467 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002468 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002470 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471
2472 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2473
2474 EXPECT_TRUE(pc_->local_description() != NULL);
2475 EXPECT_TRUE(pc_->remote_description() != NULL);
2476
kwibergd1fe2812016-04-27 06:47:29 -07002477 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002478 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002479 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002480 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481
2482 std::string sdp;
2483 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002484 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002485 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002486 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487
2488 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002489 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002490 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002491 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492}
2493
2494// Test that GetStats can still be called after PeerConnection::Close.
2495TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2496 InitiateCall();
2497 pc_->Close();
2498 DoGetStats(NULL);
2499}
deadbeefab9b2d12015-10-14 11:33:11 -07002500
2501// NOTE: The series of tests below come from what used to be
2502// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2503// setting a remote or local description has the expected effects.
2504
2505// This test verifies that the remote MediaStreams corresponding to a received
2506// SDP string is created. In this test the two separate MediaStreams are
2507// signaled.
2508TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2509 FakeConstraints constraints;
2510 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2511 true);
2512 CreatePeerConnection(&constraints);
2513 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2514
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002515 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002516 EXPECT_TRUE(
2517 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2518 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2519 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2520
2521 // Create a session description based on another SDP with another
2522 // MediaStream.
2523 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2524
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002525 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002526 EXPECT_TRUE(
2527 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2528}
2529
2530// This test verifies that when remote tracks are added/removed from SDP, the
2531// created remote streams are updated appropriately.
2532TEST_F(PeerConnectionInterfaceTest,
2533 AddRemoveTrackFromExistingRemoteMediaStream) {
2534 FakeConstraints constraints;
2535 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2536 true);
2537 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002538 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002539 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002540 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002541 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2542 reference_collection_));
2543
2544 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002545 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002546 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002547 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002548 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2549 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002550 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002551 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2552 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002553 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002554 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2555 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002556
2557 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002558 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002559 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002560 MockTrackObserver audio_track_observer(audio_track2);
2561 MockTrackObserver video_track_observer(video_track2);
2562
2563 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2564 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002565 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002566 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2567 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002568 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002569 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002570 audio_track2->state(), kTimeout);
2571 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2572 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002573}
2574
2575// This tests that remote tracks are ended if a local session description is set
2576// that rejects the media content type.
2577TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2578 FakeConstraints constraints;
2579 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2580 true);
2581 CreatePeerConnection(&constraints);
2582 // First create and set a remote offer, then reject its video content in our
2583 // answer.
2584 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2585 ASSERT_EQ(1u, observer_.remote_streams()->count());
2586 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2587 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2588 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2589
2590 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2591 remote_stream->GetVideoTracks()[0];
2592 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2593 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2594 remote_stream->GetAudioTracks()[0];
2595 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2596
kwibergd1fe2812016-04-27 06:47:29 -07002597 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002598 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002599 cricket::ContentInfo* video_info =
2600 local_answer->description()->GetContentByName("video");
2601 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002602 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002603 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2604 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2605
2606 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002607 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002608 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002609 video_info = local_offer->description()->GetContentByName("video");
2610 ASSERT_TRUE(video_info != nullptr);
2611 video_info->rejected = true;
2612 cricket::ContentInfo* audio_info =
2613 local_offer->description()->GetContentByName("audio");
2614 ASSERT_TRUE(audio_info != nullptr);
2615 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002616 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002617 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002618 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002619 remote_audio->state(), kTimeout);
2620 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2621 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002622}
2623
2624// This tests that we won't crash if the remote track has been removed outside
2625// of PeerConnection and then PeerConnection tries to reject the track.
2626TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2627 FakeConstraints constraints;
2628 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2629 true);
2630 CreatePeerConnection(&constraints);
2631 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2632 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2633 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2634 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2635
kwibergd1fe2812016-04-27 06:47:29 -07002636 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002637 webrtc::CreateSessionDescription(SdpType::kAnswer, kSdpStringWithStream1,
2638 nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002639 cricket::ContentInfo* video_info =
2640 local_answer->description()->GetContentByName("video");
2641 video_info->rejected = true;
2642 cricket::ContentInfo* audio_info =
2643 local_answer->description()->GetContentByName("audio");
2644 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002645 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002646
2647 // No crash is a pass.
2648}
2649
deadbeef5e97fb52015-10-15 12:49:08 -07002650// This tests that if a recvonly remote description is set, no remote streams
2651// will be created, even if the description contains SSRCs/MSIDs.
2652// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2653TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2654 FakeConstraints constraints;
2655 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2656 true);
2657 CreatePeerConnection(&constraints);
2658
2659 std::string recvonly_offer = kSdpStringWithStream1;
2660 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2661 strlen(kRecvonly), &recvonly_offer);
2662 CreateAndSetRemoteOffer(recvonly_offer);
2663
2664 EXPECT_EQ(0u, observer_.remote_streams()->count());
2665}
2666
deadbeefab9b2d12015-10-14 11:33:11 -07002667// This tests that a default MediaStream is created if a remote session
2668// description doesn't contain any streams and no MSID support.
2669// It also tests that the default stream is updated if a video m-line is added
2670// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002671TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002672 FakeConstraints constraints;
2673 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2674 true);
2675 CreatePeerConnection(&constraints);
2676 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2677
2678 ASSERT_EQ(1u, observer_.remote_streams()->count());
2679 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2680
2681 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2682 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2683 EXPECT_EQ("default", remote_stream->label());
2684
2685 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2686 ASSERT_EQ(1u, observer_.remote_streams()->count());
2687 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2688 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002689 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2690 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002691 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2692 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002693 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2694 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002695}
2696
2697// This tests that a default MediaStream is created if a remote session
2698// description doesn't contain any streams and media direction is send only.
2699TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002700 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002701 FakeConstraints constraints;
2702 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2703 true);
2704 CreatePeerConnection(&constraints);
2705 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2706
2707 ASSERT_EQ(1u, observer_.remote_streams()->count());
2708 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2709
2710 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2711 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2712 EXPECT_EQ("default", remote_stream->label());
2713}
2714
2715// This tests that it won't crash when PeerConnection tries to remove
2716// a remote track that as already been removed from the MediaStream.
2717TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2718 FakeConstraints constraints;
2719 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2720 true);
2721 CreatePeerConnection(&constraints);
2722 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2723 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2724 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2725 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2726
2727 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2728
2729 // No crash is a pass.
2730}
2731
2732// This tests that a default MediaStream is created if the remote session
2733// description doesn't contain any streams and don't contain an indication if
2734// MSID is supported.
2735TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002736 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002737 FakeConstraints constraints;
2738 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2739 true);
2740 CreatePeerConnection(&constraints);
2741 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2742
2743 ASSERT_EQ(1u, observer_.remote_streams()->count());
2744 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2745 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2746 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2747}
2748
2749// This tests that a default MediaStream is not created if the remote session
2750// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002751TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002752 FakeConstraints constraints;
2753 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2754 true);
2755 CreatePeerConnection(&constraints);
2756 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2757 EXPECT_EQ(0u, observer_.remote_streams()->count());
2758}
2759
deadbeefbda7e0b2015-12-08 17:13:40 -08002760// This tests that when setting a new description, the old default tracks are
2761// not destroyed and recreated.
2762// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002763TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002764 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002765 FakeConstraints constraints;
2766 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2767 true);
2768 CreatePeerConnection(&constraints);
2769 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2770
2771 ASSERT_EQ(1u, observer_.remote_streams()->count());
2772 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2773 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2774
2775 // Set the track to "disabled", then set a new description and ensure the
2776 // track is still disabled, which ensures it hasn't been recreated.
2777 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2778 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2779 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2780 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2781}
2782
deadbeefab9b2d12015-10-14 11:33:11 -07002783// This tests that a default MediaStream is not created if a remote session
2784// description is updated to not have any MediaStreams.
2785TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2786 FakeConstraints constraints;
2787 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2788 true);
2789 CreatePeerConnection(&constraints);
2790 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002791 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002792 EXPECT_TRUE(
2793 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2794
2795 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2796 EXPECT_EQ(0u, observer_.remote_streams()->count());
2797}
2798
2799// This tests that an RtpSender is created when the local description is set
2800// after adding a local stream.
2801// TODO(deadbeef): This test and the one below it need to be updated when
2802// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002803TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002804 FakeConstraints constraints;
2805 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2806 true);
2807 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002808
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002809 // Create an offer with 1 stream with 2 tracks of each type.
2810 rtc::scoped_refptr<StreamCollection> stream_collection =
2811 CreateStreamCollection(1, 2);
2812 pc_->AddStream(stream_collection->at(0));
2813 std::unique_ptr<SessionDescriptionInterface> offer;
2814 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002815 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002816
deadbeefab9b2d12015-10-14 11:33:11 -07002817 auto senders = pc_->GetSenders();
2818 EXPECT_EQ(4u, senders.size());
2819 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2820 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2821 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2822 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2823
2824 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002825 pc_->RemoveStream(stream_collection->at(0));
2826 stream_collection = CreateStreamCollection(1, 1);
2827 pc_->AddStream(stream_collection->at(0));
2828 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002829 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002830
deadbeefab9b2d12015-10-14 11:33:11 -07002831 senders = pc_->GetSenders();
2832 EXPECT_EQ(2u, senders.size());
2833 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2834 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2835 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2836 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2837}
2838
2839// This tests that an RtpSender is created when the local description is set
2840// before adding a local stream.
2841TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002842 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002843 FakeConstraints constraints;
2844 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2845 true);
2846 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002847
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002848 rtc::scoped_refptr<StreamCollection> stream_collection =
2849 CreateStreamCollection(1, 2);
2850 // Add a stream to create the offer, but remove it afterwards.
2851 pc_->AddStream(stream_collection->at(0));
2852 std::unique_ptr<SessionDescriptionInterface> offer;
2853 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2854 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002855
Steve Antondb45ca82017-09-11 18:27:34 -07002856 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002857 auto senders = pc_->GetSenders();
2858 EXPECT_EQ(0u, senders.size());
2859
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002860 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002861 senders = pc_->GetSenders();
2862 EXPECT_EQ(4u, senders.size());
2863 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2864 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2865 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2866 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2867}
2868
2869// This tests that the expected behavior occurs if the SSRC on a local track is
2870// changed when SetLocalDescription is called.
2871TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002872 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002873 FakeConstraints constraints;
2874 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2875 true);
2876 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002877
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002878 rtc::scoped_refptr<StreamCollection> stream_collection =
2879 CreateStreamCollection(2, 1);
2880 pc_->AddStream(stream_collection->at(0));
2881 std::unique_ptr<SessionDescriptionInterface> offer;
2882 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2883 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08002884 auto modified_offer =
2885 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002886 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2887 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002888 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002889
deadbeefab9b2d12015-10-14 11:33:11 -07002890 auto senders = pc_->GetSenders();
2891 EXPECT_EQ(2u, senders.size());
2892 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2893 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2894
2895 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002896 cricket::MediaContentDescription* desc =
2897 cricket::GetFirstAudioContentDescription(modified_offer->description());
2898 ASSERT_TRUE(desc != NULL);
2899 for (StreamParams& stream : desc->mutable_streams()) {
2900 for (unsigned int& ssrc : stream.ssrcs) {
2901 ++ssrc;
2902 }
2903 }
deadbeefab9b2d12015-10-14 11:33:11 -07002904
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002905 desc =
2906 cricket::GetFirstVideoContentDescription(modified_offer->description());
2907 ASSERT_TRUE(desc != NULL);
2908 for (StreamParams& stream : desc->mutable_streams()) {
2909 for (unsigned int& ssrc : stream.ssrcs) {
2910 ++ssrc;
2911 }
2912 }
2913
Steve Antondb45ca82017-09-11 18:27:34 -07002914 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002915 senders = pc_->GetSenders();
2916 EXPECT_EQ(2u, senders.size());
2917 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2918 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2919 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2920 // changed.
2921}
2922
2923// This tests that the expected behavior occurs if a new session description is
2924// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002925TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002926 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002927 FakeConstraints constraints;
2928 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2929 true);
2930 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002931
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002932 rtc::scoped_refptr<StreamCollection> stream_collection =
2933 CreateStreamCollection(2, 1);
2934 pc_->AddStream(stream_collection->at(0));
2935 std::unique_ptr<SessionDescriptionInterface> offer;
2936 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002937 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002938
deadbeefab9b2d12015-10-14 11:33:11 -07002939 auto senders = pc_->GetSenders();
2940 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002941 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2942 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002943
2944 // Add a new MediaStream but with the same tracks as in the first stream.
2945 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2946 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002947 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2948 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002949 pc_->AddStream(stream_1);
2950
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002951 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002952 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002953
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002954 auto new_senders = pc_->GetSenders();
2955 // Should be the same senders as before, but with updated stream id.
2956 // Note that this behavior is subject to change in the future.
2957 // We may decide the PC should ignore existing tracks in AddStream.
2958 EXPECT_EQ(senders, new_senders);
2959 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2960 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002961}
2962
zhihuang81c3a032016-11-17 12:06:24 -08002963// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2964TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2965 FakeConstraints constraints;
2966 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2967 true);
2968 CreatePeerConnection(&constraints);
2969 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2970 EXPECT_EQ(observer_.num_added_tracks_, 1);
2971 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2972
2973 // Create and set the updated remote SDP.
2974 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2975 EXPECT_EQ(observer_.num_added_tracks_, 2);
2976 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2977}
2978
deadbeefd1a38b52016-12-10 13:15:33 -08002979// Test that when SetConfiguration is called and the configuration is
2980// changing, the next offer causes an ICE restart.
2981TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2982 PeerConnectionInterface::RTCConfiguration config;
2983 config.type = PeerConnectionInterface::kRelay;
2984 // Need to pass default constraints to prevent disabling of DTLS...
2985 FakeConstraints default_constraints;
2986 CreatePeerConnection(config, &default_constraints);
2987 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2988
2989 // Do initial offer/answer so there's something to restart.
2990 CreateOfferAsLocalDescription();
2991 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2992
2993 // Grab the ufrags.
2994 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2995
2996 // Change ICE policy, which should trigger an ICE restart on the next offer.
2997 config.type = PeerConnectionInterface::kAll;
2998 EXPECT_TRUE(pc_->SetConfiguration(config));
2999 CreateOfferAsLocalDescription();
3000
3001 // Grab the new ufrags.
3002 std::vector<std::string> subsequent_ufrags =
3003 GetUfrags(pc_->local_description());
3004
3005 // Sanity check.
3006 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3007 // Check that each ufrag is different.
3008 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3009 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3010 }
3011}
3012
3013// Test that when SetConfiguration is called and the configuration *isn't*
3014// changing, the next offer does *not* cause an ICE restart.
3015TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3016 PeerConnectionInterface::RTCConfiguration config;
3017 config.type = PeerConnectionInterface::kRelay;
3018 // Need to pass default constraints to prevent disabling of DTLS...
3019 FakeConstraints default_constraints;
3020 CreatePeerConnection(config, &default_constraints);
3021 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3022
3023 // Do initial offer/answer so there's something to restart.
3024 CreateOfferAsLocalDescription();
3025 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3026
3027 // Grab the ufrags.
3028 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3029
3030 // Call SetConfiguration with a config identical to what the PC was
3031 // constructed with.
3032 EXPECT_TRUE(pc_->SetConfiguration(config));
3033 CreateOfferAsLocalDescription();
3034
3035 // Grab the new ufrags.
3036 std::vector<std::string> subsequent_ufrags =
3037 GetUfrags(pc_->local_description());
3038
3039 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3040}
3041
3042// Test for a weird corner case scenario:
3043// 1. Audio/video session established.
3044// 2. SetConfiguration changes ICE config; ICE restart needed.
3045// 3. ICE restart initiated by remote peer, but only for one m= section.
3046// 4. Next createOffer should initiate an ICE restart, but only for the other
3047// m= section; it would be pointless to do an ICE restart for the m= section
3048// that was already restarted.
3049TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3050 PeerConnectionInterface::RTCConfiguration config;
3051 config.type = PeerConnectionInterface::kRelay;
3052 // Need to pass default constraints to prevent disabling of DTLS...
3053 FakeConstraints default_constraints;
3054 CreatePeerConnection(config, &default_constraints);
3055 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3056
3057 // Do initial offer/answer so there's something to restart.
3058 CreateOfferAsLocalDescription();
3059 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3060
3061 // Change ICE policy, which should set the "needs-ice-restart" flag.
3062 config.type = PeerConnectionInterface::kAll;
3063 EXPECT_TRUE(pc_->SetConfiguration(config));
3064
3065 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003066 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003067 webrtc::CreateSessionDescription(SdpType::kOffer, kSdpStringWithStream1,
3068 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003069 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003070 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3071 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003072 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003073 CreateAnswerAsLocalDescription();
3074
3075 // Grab the ufrags.
3076 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3077 ASSERT_EQ(2, initial_ufrags.size());
3078
3079 // Create offer and grab the new ufrags.
3080 CreateOfferAsLocalDescription();
3081 std::vector<std::string> subsequent_ufrags =
3082 GetUfrags(pc_->local_description());
3083 ASSERT_EQ(2, subsequent_ufrags.size());
3084
3085 // Ensure that only the ufrag for the second m= section changed.
3086 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3087 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3088}
3089
deadbeeffe4a8a42016-12-20 17:56:17 -08003090// Tests that the methods to return current/pending descriptions work as
3091// expected at different points in the offer/answer exchange. This test does
3092// one offer/answer exchange as the offerer, then another as the answerer.
3093TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3094 // This disables DTLS so we can apply an answer to ourselves.
3095 CreatePeerConnection();
3096
3097 // Create initial local offer and get SDP (which will also be used as
3098 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003099 std::unique_ptr<SessionDescriptionInterface> local_offer;
3100 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003101 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003102 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003103
3104 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003105 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3106 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3107 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003108 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3109 EXPECT_EQ(nullptr, pc_->current_local_description());
3110 EXPECT_EQ(nullptr, pc_->current_remote_description());
3111
3112 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003113 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003114 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003115 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3116 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3117 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3118 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003119 EXPECT_EQ(nullptr, pc_->current_local_description());
3120 EXPECT_EQ(nullptr, pc_->current_remote_description());
3121
3122 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003123 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003124 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003125 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3126 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003127 EXPECT_EQ(nullptr, pc_->pending_local_description());
3128 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003129 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3130 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003131
3132 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003133 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003134 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003135 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3136 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3137 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003138 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003139 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3140 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003141
3142 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003143 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003144 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003145 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3146 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3147 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3148 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3149 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3150 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003151
3152 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003153 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003154 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003155 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3156 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003157 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3158 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003159 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3160 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003161}
3162
zhihuang77985012017-02-07 15:45:16 -08003163// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3164// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003165// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003166TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003167 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003168 CreatePeerConnection();
3169 // The RtcEventLog will be reset when the PeerConnection is closed.
3170 pc_->Close();
3171
Elad Alon9e6565b2017-10-11 16:04:13 +02003172 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3173 std::string filename = webrtc::test::OutputPath() +
3174 test_info->test_case_name() + test_info->name();
3175 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3176
3177 constexpr int64_t max_size_bytes = 1024;
3178
zhihuang77985012017-02-07 15:45:16 -08003179 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3180 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003181
3182 // Cleanup.
3183 rtc::ClosePlatformFile(file);
3184 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003185}
3186
Elad Alon99c3fe52017-10-13 16:29:40 +02003187// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3188// after the PeerConnection is closed.
3189// This version tests the StartRtcEventLog version that receives an object
3190// of type |RtcEventLogOutput|.
3191TEST_F(PeerConnectionInterfaceTest,
3192 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3193 CreatePeerConnection();
3194 // The RtcEventLog will be reset when the PeerConnection is closed.
3195 pc_->Close();
3196
3197 rtc::PlatformFile file = 0;
3198 int64_t max_size_bytes = 1024;
3199 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003200 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3201 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003202 pc_->StopRtcEventLog();
3203}
3204
deadbeef30952b42017-04-21 02:41:29 -07003205// Test that generated offers/answers include "ice-option:trickle".
3206TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3207 CreatePeerConnection();
3208
3209 // First, create an offer with audio/video.
3210 FakeConstraints constraints;
3211 constraints.SetMandatoryReceiveAudio(true);
3212 constraints.SetMandatoryReceiveVideo(true);
3213 std::unique_ptr<SessionDescriptionInterface> offer;
3214 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3215 cricket::SessionDescription* desc = offer->description();
3216 ASSERT_EQ(2u, desc->transport_infos().size());
3217 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3218 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3219
3220 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003221 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003222 std::unique_ptr<SessionDescriptionInterface> answer;
3223 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3224 desc = answer->description();
3225 ASSERT_EQ(2u, desc->transport_infos().size());
3226 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3227 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3228}
3229
deadbeef1dcb1642017-03-29 21:08:16 -07003230// Test that ICE renomination isn't offered if it's not enabled in the PC's
3231// RTCConfiguration.
3232TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3233 PeerConnectionInterface::RTCConfiguration config;
3234 config.enable_ice_renomination = false;
3235 CreatePeerConnection(config, nullptr);
3236 AddVoiceStream("foo");
3237
3238 std::unique_ptr<SessionDescriptionInterface> offer;
3239 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3240 cricket::SessionDescription* desc = offer->description();
3241 EXPECT_EQ(1u, desc->transport_infos().size());
3242 EXPECT_FALSE(
3243 desc->transport_infos()[0].description.GetIceParameters().renomination);
3244}
3245
3246// Test that the ICE renomination option is present in generated offers/answers
3247// if it's enabled in the PC's RTCConfiguration.
3248TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3249 PeerConnectionInterface::RTCConfiguration config;
3250 config.enable_ice_renomination = true;
3251 CreatePeerConnection(config, nullptr);
3252 AddVoiceStream("foo");
3253
3254 std::unique_ptr<SessionDescriptionInterface> offer;
3255 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3256 cricket::SessionDescription* desc = offer->description();
3257 EXPECT_EQ(1u, desc->transport_infos().size());
3258 EXPECT_TRUE(
3259 desc->transport_infos()[0].description.GetIceParameters().renomination);
3260
3261 // Set the offer as a remote description, then create an answer and ensure it
3262 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003263 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003264 std::unique_ptr<SessionDescriptionInterface> answer;
3265 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3266 desc = answer->description();
3267 EXPECT_EQ(1u, desc->transport_infos().size());
3268 EXPECT_TRUE(
3269 desc->transport_infos()[0].description.GetIceParameters().renomination);
3270}
3271
3272// Test that if CreateOffer is called with the deprecated "offer to receive
3273// audio/video" constraints, they're processed and result in an offer with
3274// audio/video sections just as if RTCOfferAnswerOptions had been used.
3275TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3276 CreatePeerConnection();
3277
3278 FakeConstraints constraints;
3279 constraints.SetMandatoryReceiveAudio(true);
3280 constraints.SetMandatoryReceiveVideo(true);
3281 std::unique_ptr<SessionDescriptionInterface> offer;
3282 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3283
3284 cricket::SessionDescription* desc = offer->description();
3285 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3286 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3287 ASSERT_NE(nullptr, audio);
3288 ASSERT_NE(nullptr, video);
3289 EXPECT_FALSE(audio->rejected);
3290 EXPECT_FALSE(video->rejected);
3291}
3292
3293// Test that if CreateAnswer is called with the deprecated "offer to receive
3294// audio/video" constraints, they're processed and can be used to reject an
3295// offered m= section just as can be done with RTCOfferAnswerOptions;
3296TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3297 CreatePeerConnection();
3298
3299 // First, create an offer with audio/video and apply it as a remote
3300 // description.
3301 FakeConstraints constraints;
3302 constraints.SetMandatoryReceiveAudio(true);
3303 constraints.SetMandatoryReceiveVideo(true);
3304 std::unique_ptr<SessionDescriptionInterface> offer;
3305 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003306 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003307
3308 // Now create answer that rejects audio/video.
3309 constraints.SetMandatoryReceiveAudio(false);
3310 constraints.SetMandatoryReceiveVideo(false);
3311 std::unique_ptr<SessionDescriptionInterface> answer;
3312 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3313
3314 cricket::SessionDescription* desc = answer->description();
3315 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3316 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3317 ASSERT_NE(nullptr, audio);
3318 ASSERT_NE(nullptr, video);
3319 EXPECT_TRUE(audio->rejected);
3320 EXPECT_TRUE(video->rejected);
3321}
3322
3323#ifdef HAVE_SCTP
3324#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3325 DataChannelOnlyOfferWithMaxBundlePolicy
3326#else
3327#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3328 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3329#endif
3330
3331// Test that negotiation can succeed with a data channel only, and with the max
3332// bundle policy. Previously there was a bug that prevented this.
3333TEST_F(PeerConnectionInterfaceTest,
3334 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3335 PeerConnectionInterface::RTCConfiguration config;
3336 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3337 CreatePeerConnection(config, nullptr);
3338
3339 // First, create an offer with only a data channel and apply it as a remote
3340 // description.
3341 pc_->CreateDataChannel("test", nullptr);
3342 std::unique_ptr<SessionDescriptionInterface> offer;
3343 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003344 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003345
3346 // Create and set answer as well.
3347 std::unique_ptr<SessionDescriptionInterface> answer;
3348 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003349 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003350}
3351
zstein4b979802017-06-02 14:37:37 -07003352TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3353 CreatePeerConnection();
3354 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003355 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003356 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3357}
3358
3359TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3360 CreatePeerConnection();
3361 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003362 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003363 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3364}
3365
3366TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3367 CreatePeerConnection();
3368 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003369 bitrate.min_bitrate_bps = 5;
3370 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003371 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3372}
3373
3374TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3375 CreatePeerConnection();
3376 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003377 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003378 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3379}
3380
3381TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3382 CreatePeerConnection();
3383 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003384 bitrate.current_bitrate_bps = 10;
3385 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003386 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3387}
3388
3389TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3390 CreatePeerConnection();
3391 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003392 bitrate.min_bitrate_bps = 10;
3393 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003394 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3395}
3396
3397TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3398 CreatePeerConnection();
3399 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003400 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003401 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3402}
3403
Steve Anton038834f2017-07-14 15:59:59 -07003404// ice_regather_interval_range requires WebRTC to be configured for continual
3405// gathering already.
3406TEST_F(PeerConnectionInterfaceTest,
3407 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3408 PeerConnectionInterface::RTCConfiguration config;
3409 config.ice_regather_interval_range.emplace(1000, 2000);
3410 config.continual_gathering_policy =
3411 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3412 CreatePeerConnectionExpectFail(config);
3413}
3414
3415// Ensures that there is no error when ice_regather_interval_range is set with
3416// continual gathering enabled.
3417TEST_F(PeerConnectionInterfaceTest,
3418 SetIceRegatherIntervalRangeWithContinualGathering) {
3419 PeerConnectionInterface::RTCConfiguration config;
3420 config.ice_regather_interval_range.emplace(1000, 2000);
3421 config.continual_gathering_policy =
3422 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3423 CreatePeerConnection(config, nullptr);
3424}
3425
zstein4b979802017-06-02 14:37:37 -07003426// The current bitrate from Call's BitrateConfigMask is currently clamped by
3427// Call's BitrateConfig, which comes from the SDP or a default value. This test
3428// checks that a call to SetBitrate with a current bitrate that will be clamped
3429// succeeds.
3430TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3431 CreatePeerConnection();
3432 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003433 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003434 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3435}
3436
zhihuang1c378ed2017-08-17 14:10:50 -07003437// The following tests verify that the offer can be created correctly.
3438TEST_F(PeerConnectionInterfaceTest,
3439 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3440 RTCOfferAnswerOptions rtc_options;
3441
3442 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3443 // than kMaxOfferToReceiveMedia should be treated as invalid.
3444 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3445 CreatePeerConnection();
3446 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3447
3448 rtc_options.offer_to_receive_audio =
3449 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3450 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3451}
3452
3453TEST_F(PeerConnectionInterfaceTest,
3454 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3455 RTCOfferAnswerOptions rtc_options;
3456
3457 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3458 // than kMaxOfferToReceiveMedia should be treated as invalid.
3459 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3460 CreatePeerConnection();
3461 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3462
3463 rtc_options.offer_to_receive_video =
3464 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3465 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3466}
3467
3468// Test that the audio and video content will be added to an offer if both
3469// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3470TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3471 RTCOfferAnswerOptions rtc_options;
3472 rtc_options.offer_to_receive_audio = 1;
3473 rtc_options.offer_to_receive_video = 1;
3474
3475 std::unique_ptr<SessionDescriptionInterface> offer;
3476 CreatePeerConnection();
3477 offer = CreateOfferWithOptions(rtc_options);
3478 ASSERT_TRUE(offer);
3479 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3480 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3481}
3482
3483// Test that only audio content will be added to the offer if only
3484// |offer_to_receive_audio| options is 1.
3485TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3486 RTCOfferAnswerOptions rtc_options;
3487 rtc_options.offer_to_receive_audio = 1;
3488 rtc_options.offer_to_receive_video = 0;
3489
3490 std::unique_ptr<SessionDescriptionInterface> offer;
3491 CreatePeerConnection();
3492 offer = CreateOfferWithOptions(rtc_options);
3493 ASSERT_TRUE(offer);
3494 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3495 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3496}
3497
3498// Test that only video content will be added if only |offer_to_receive_video|
3499// options is 1.
3500TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3501 RTCOfferAnswerOptions rtc_options;
3502 rtc_options.offer_to_receive_audio = 0;
3503 rtc_options.offer_to_receive_video = 1;
3504
3505 std::unique_ptr<SessionDescriptionInterface> offer;
3506 CreatePeerConnection();
3507 offer = CreateOfferWithOptions(rtc_options);
3508 ASSERT_TRUE(offer);
3509 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3510 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3511}
3512
zhihuang1c378ed2017-08-17 14:10:50 -07003513// Test that no media content will be added to the offer if using default
3514// RTCOfferAnswerOptions.
3515TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3516 RTCOfferAnswerOptions rtc_options;
3517
3518 std::unique_ptr<SessionDescriptionInterface> offer;
3519 CreatePeerConnection();
3520 offer = CreateOfferWithOptions(rtc_options);
3521 ASSERT_TRUE(offer);
3522 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3523 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3524}
3525
3526// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3527// ufrag/pwd will be the same in the new offer.
3528TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3529 RTCOfferAnswerOptions rtc_options;
3530 rtc_options.ice_restart = false;
3531 rtc_options.offer_to_receive_audio = 1;
3532
3533 std::unique_ptr<SessionDescriptionInterface> offer;
3534 CreatePeerConnection();
3535 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3536 auto ufrag1 = offer->description()
3537 ->GetTransportInfoByName(cricket::CN_AUDIO)
3538 ->description.ice_ufrag;
3539 auto pwd1 = offer->description()
3540 ->GetTransportInfoByName(cricket::CN_AUDIO)
3541 ->description.ice_pwd;
3542
3543 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3544 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3545 auto ufrag2 = offer->description()
3546 ->GetTransportInfoByName(cricket::CN_AUDIO)
3547 ->description.ice_ufrag;
3548 auto pwd2 = offer->description()
3549 ->GetTransportInfoByName(cricket::CN_AUDIO)
3550 ->description.ice_pwd;
3551
3552 // |ice_restart| is true, the ufrag/pwd should change.
3553 rtc_options.ice_restart = true;
3554 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3555 auto ufrag3 = offer->description()
3556 ->GetTransportInfoByName(cricket::CN_AUDIO)
3557 ->description.ice_ufrag;
3558 auto pwd3 = offer->description()
3559 ->GetTransportInfoByName(cricket::CN_AUDIO)
3560 ->description.ice_pwd;
3561
3562 EXPECT_EQ(ufrag1, ufrag2);
3563 EXPECT_EQ(pwd1, pwd2);
3564 EXPECT_NE(ufrag2, ufrag3);
3565 EXPECT_NE(pwd2, pwd3);
3566}
3567
3568// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3569// offer; if it is false, there won't be any bundle group in the offer.
3570TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3571 RTCOfferAnswerOptions rtc_options;
3572 rtc_options.offer_to_receive_audio = 1;
3573 rtc_options.offer_to_receive_video = 1;
3574
3575 std::unique_ptr<SessionDescriptionInterface> offer;
3576 CreatePeerConnection();
3577
3578 rtc_options.use_rtp_mux = true;
3579 offer = CreateOfferWithOptions(rtc_options);
3580 ASSERT_TRUE(offer);
3581 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3582 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3583 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3584
3585 rtc_options.use_rtp_mux = false;
3586 offer = CreateOfferWithOptions(rtc_options);
3587 ASSERT_TRUE(offer);
3588 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3589 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3590 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3591}
3592
zhihuang141aacb2017-08-29 13:23:53 -07003593// This test ensures OnRenegotiationNeeded is called when we add track with
3594// MediaStream -> AddTrack in the same way it is called when we add track with
3595// PeerConnection -> AddTrack.
3596// The test can be removed once addStream is rewritten in terms of addTrack
3597// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3598TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3599 CreatePeerConnectionWithoutDtls();
3600 rtc::scoped_refptr<MediaStreamInterface> stream(
3601 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3602 pc_->AddStream(stream);
3603 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3604 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3605 rtc::scoped_refptr<VideoTrackInterface> video_track(
3606 pc_factory_->CreateVideoTrack(
3607 "video_track", pc_factory_->CreateVideoSource(
3608 std::unique_ptr<cricket::VideoCapturer>(
3609 new cricket::FakeVideoCapturer()))));
3610 stream->AddTrack(audio_track);
3611 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3612 observer_.renegotiation_needed_ = false;
3613
3614 stream->AddTrack(video_track);
3615 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3616 observer_.renegotiation_needed_ = false;
3617
3618 stream->RemoveTrack(audio_track);
3619 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3620 observer_.renegotiation_needed_ = false;
3621
3622 stream->RemoveTrack(video_track);
3623 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3624 observer_.renegotiation_needed_ = false;
3625}
3626
Zhi Huangb2d355e2017-10-26 17:26:37 -07003627// Tests that an error is returned if a description is applied that has fewer
3628// media sections than the existing description.
3629TEST_F(PeerConnectionInterfaceTest,
3630 MediaSectionCountEnforcedForSubsequentOffer) {
3631 CreatePeerConnection();
3632 FakeConstraints constraints;
3633 constraints.SetMandatoryReceiveAudio(true);
3634 constraints.SetMandatoryReceiveVideo(true);
3635 std::unique_ptr<SessionDescriptionInterface> offer;
3636 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3637 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3638
3639 // A remote offer with fewer media sections should be rejected.
3640 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3641 offer->description()->contents().pop_back();
3642 offer->description()->contents().pop_back();
3643 ASSERT_TRUE(offer->description()->contents().empty());
3644 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3645
3646 std::unique_ptr<SessionDescriptionInterface> answer;
3647 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3648 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3649
3650 // A subsequent local offer with fewer media sections should be rejected.
3651 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3652 offer->description()->contents().pop_back();
3653 offer->description()->contents().pop_back();
3654 ASSERT_TRUE(offer->description()->contents().empty());
3655 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3656}
3657
nisse51542be2016-02-12 02:27:06 -08003658class PeerConnectionMediaConfigTest : public testing::Test {
3659 protected:
3660 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003661 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003662 pcf_->Initialize();
3663 }
nisseeaabdf62017-05-05 02:23:02 -07003664 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003665 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003666 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003667 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3668 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003669 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003670 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003671 }
3672
zhihuang9763d562016-08-05 11:14:50 -07003673 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003674 MockPeerConnectionObserver observer_;
3675};
3676
3677// This test verifies the default behaviour with no constraints and a
3678// default RTCConfiguration.
3679TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3680 PeerConnectionInterface::RTCConfiguration config;
3681 FakeConstraints constraints;
3682
3683 const cricket::MediaConfig& media_config =
3684 TestCreatePeerConnection(config, &constraints);
3685
3686 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003687 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3688 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3689 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003690}
3691
3692// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003693// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003694TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3695 PeerConnectionInterface::RTCConfiguration config;
3696 FakeConstraints constraints;
3697
3698 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3699 const cricket::MediaConfig& media_config =
3700 TestCreatePeerConnection(config, &constraints);
3701
3702 EXPECT_TRUE(media_config.enable_dscp);
3703}
3704
3705// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003706// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003707TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3708 PeerConnectionInterface::RTCConfiguration config;
3709 FakeConstraints constraints;
3710
3711 constraints.AddOptional(
3712 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3713 const cricket::MediaConfig media_config =
3714 TestCreatePeerConnection(config, &constraints);
3715
nisse0db023a2016-03-01 04:29:59 -08003716 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003717}
3718
3719// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003720// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003721TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3722 PeerConnectionInterface::RTCConfiguration config;
3723 FakeConstraints constraints;
3724
Niels Möller71bdda02016-03-31 12:59:59 +02003725 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003726 const cricket::MediaConfig& media_config =
3727 TestCreatePeerConnection(config, &constraints);
3728
nisse0db023a2016-03-01 04:29:59 -08003729 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3730}
3731
3732// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003733// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003734TEST_F(PeerConnectionMediaConfigTest,
3735 TestSuspendBelowMinBitrateConstraintTrue) {
3736 PeerConnectionInterface::RTCConfiguration config;
3737 FakeConstraints constraints;
3738
3739 constraints.AddOptional(
3740 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3741 true);
3742 const cricket::MediaConfig media_config =
3743 TestCreatePeerConnection(config, &constraints);
3744
3745 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003746}
3747
deadbeef293e9262017-01-11 12:28:30 -08003748// Tests a few random fields being different.
3749TEST(RTCConfigurationTest, ComparisonOperators) {
3750 PeerConnectionInterface::RTCConfiguration a;
3751 PeerConnectionInterface::RTCConfiguration b;
3752 EXPECT_EQ(a, b);
3753
3754 PeerConnectionInterface::RTCConfiguration c;
3755 c.servers.push_back(PeerConnectionInterface::IceServer());
3756 EXPECT_NE(a, c);
3757
3758 PeerConnectionInterface::RTCConfiguration d;
3759 d.type = PeerConnectionInterface::kRelay;
3760 EXPECT_NE(a, d);
3761
3762 PeerConnectionInterface::RTCConfiguration e;
3763 e.audio_jitter_buffer_max_packets = 5;
3764 EXPECT_NE(a, e);
3765
3766 PeerConnectionInterface::RTCConfiguration f;
3767 f.ice_connection_receiving_timeout = 1337;
3768 EXPECT_NE(a, f);
3769
3770 PeerConnectionInterface::RTCConfiguration g;
3771 g.disable_ipv6 = true;
3772 EXPECT_NE(a, g);
3773
3774 PeerConnectionInterface::RTCConfiguration h(
3775 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3776 EXPECT_NE(a, h);
3777}